utils/testparse.c
changeset 1046 097aefb03885
parent 1044 1241d4a56b28
child 1051 96a838f4e680
equal deleted inserted replaced
1045:448766b7c981 1046:097aefb03885
   133         printf("\n");
   133         printf("\n");
   134     } // else
   134     } // else
   135 } // print_symbols
   135 } // print_symbols
   136 
   136 
   137 
   137 
       
   138 static void print_preshader_operand(const MOJOSHADER_preshader *preshader,
       
   139                                     const int instidx, const int opidx)
       
   140 {
       
   141     static char mask[] = { 'x', 'y', 'z', 'w' };
       
   142     const MOJOSHADER_preshaderInstruction *inst = &preshader->instructions[instidx];
       
   143     const MOJOSHADER_preshaderOperand *operand = &inst->operands[opidx];
       
   144     const int elems = inst->element_count;
       
   145     const int isscalarop = (inst->opcode >= MOJOSHADER_PRESHADEROP_SCALAR_OPS);
       
   146     const int isscalar = ((isscalarop) && (opidx == 1)); // probably wrong.
       
   147     int i;
       
   148 
       
   149     switch (operand->type)
       
   150     {
       
   151         case MOJOSHADER_PRESHADEROPERAND_LITERAL:
       
   152         {
       
   153             const double *lit = &preshader->literals[operand->index];
       
   154             printf("(");
       
   155             if (isscalar)
       
   156             {
       
   157                 const double val = *lit;
       
   158                 for (i = 0; i < elems-1; i++)
       
   159                     printf("%g, ", val);
       
   160                 printf("%g)", val);
       
   161             } // if
       
   162             else
       
   163             {
       
   164                 for (i = 0; i < elems-1; i++, lit++)
       
   165                     printf("%g, ", *lit);
       
   166                 printf("%g)", *lit);
       
   167             } // else
       
   168             break;
       
   169         } // case
       
   170 
       
   171         case MOJOSHADER_PRESHADEROPERAND_INPUT:
       
   172         case MOJOSHADER_PRESHADEROPERAND_OUTPUT:
       
   173         case MOJOSHADER_PRESHADEROPERAND_TEMP:
       
   174         {
       
   175             int idx = operand->index % 4;
       
   176             char regch = 'c';
       
   177             if (operand->type == MOJOSHADER_PRESHADEROPERAND_TEMP)
       
   178                 regch = 'r';
       
   179 
       
   180             printf("%c%d", regch, operand->index / 4);
       
   181             if (isscalar)
       
   182                 printf(".%c", mask[idx]);
       
   183             else if (elems != 4)
       
   184             {
       
   185                 printf(".");
       
   186                 for (i = 0; i < elems; i++)
       
   187                     printf("%c", mask[idx++]);
       
   188             } // else if
       
   189             break;
       
   190         } // case
       
   191 
       
   192         default:
       
   193             printf("[???{%d, %u}???]", (int) operand->type, operand->index);
       
   194             break;
       
   195     } // switch
       
   196 } // print_preshader_operand
       
   197 
       
   198 
   138 static void print_preshader(const MOJOSHADER_preshader *preshader,
   199 static void print_preshader(const MOJOSHADER_preshader *preshader,
   139                             const int indent)
   200                             const int indent)
   140 {
   201 {
   141     MOJOSHADER_preshaderInstruction *inst = preshader->instructions;
   202     MOJOSHADER_preshaderInstruction *inst = preshader->instructions;
   142     int i, j, k;
   203     int i, j;
   143 
   204 
   144     static const char *opcodestr[] = {
   205     static const char *opcodestr[] = {
   145         "nop", "mov", "neg", "rcp", "frc", "exp", "log", "rsq", "sin", "cos",
   206         "nop", "mov", "neg", "rcp", "frc", "exp", "log", "rsq", "sin", "cos",
   146         "asin", "acos", "atan", "min", "max", "lt", "ge", "add", "mul",
   207         "asin", "acos", "atan", "min", "max", "lt", "ge", "add", "mul",
   147         "atan2", "div", "cmp", "movc", "dot", "noise", "min", "max", "lt",
   208         "atan2", "div", "cmp", "movc", "dot", "noise", "min", "max", "lt",
   148         "ge", "add", "mul", "atan2", "div", "dot", "noise"
   209         "ge", "add", "mul", "atan2", "div", "dot", "noise"
   149     };
   210     };
   150 
   211 
   151     static char mask[] = { 'x', 'y', 'z', 'w' };
       
   152 
       
   153     INDENT(); printf("PRESHADER:\n");
   212     INDENT(); printf("PRESHADER:\n");
   154 
   213 
   155     print_symbols(preshader->symbols, preshader->symbol_count, indent + 1);
   214     print_symbols(preshader->symbols, preshader->symbol_count, indent + 1);
   156 
   215 
   157     for (i = 0; i < preshader->instruction_count; i++, inst++)
   216     for (i = 0; i < preshader->instruction_count; i++, inst++)
   158     {
   217     {
   159         const MOJOSHADER_preshaderOperand *operand = inst->operands;
   218         INDENT(); printf("    %s ", opcodestr[inst->opcode]);
   160         const int scalarstart = (int) MOJOSHADER_PRESHADEROP_SCALAR_OPS;
   219 
   161         const int isscalarop = (inst->opcode >= scalarstart);
   220         // print dest register first...
   162 
   221         print_preshader_operand(preshader, i, inst->operand_count - 1);
   163         INDENT(); printf("    %s", opcodestr[inst->opcode]);
   222 
   164 
   223         // ...then the source registers.
   165         for (j = 0; j < inst->operand_count; j++, operand++)
   224         for (j = 0; j < inst->operand_count - 1; j++)
   166         {
   225         {
   167             const int elems = inst->element_count;
   226             printf(", ");
   168             const int isscalar = ((isscalarop) && (j == 1)); // probably wrong.
   227             print_preshader_operand(preshader, i, j);
   169 
       
   170             if (j != 0)
       
   171                 printf(",");
       
   172             printf(" ");
       
   173 
       
   174             switch (operand->type)
       
   175             {
       
   176                 case MOJOSHADER_PRESHADEROPERAND_LITERAL:
       
   177                 {
       
   178                     const double *lit = &preshader->literals[operand->index];
       
   179                     printf("(");
       
   180                     if (isscalar)
       
   181                     {
       
   182                         const double val = *lit;
       
   183                         for (k = 0; k < elems-1; k++)
       
   184                             printf("%g, ", val);
       
   185                         printf("%g)", val);
       
   186                     } // if
       
   187                     else
       
   188                     {
       
   189                         for (k = 0; k < elems-1; k++, lit++)
       
   190                             printf("%g, ", *lit);
       
   191                         printf("%g)", *lit);
       
   192                     } // else
       
   193                     break;
       
   194                 } // case
       
   195 
       
   196                 case MOJOSHADER_PRESHADEROPERAND_INPUT:
       
   197                 case MOJOSHADER_PRESHADEROPERAND_OUTPUT:
       
   198                 case MOJOSHADER_PRESHADEROPERAND_TEMP:
       
   199                 {
       
   200                     int idx = operand->index % 4;
       
   201                     char regch = 'c';
       
   202                     if (operand->type == MOJOSHADER_PRESHADEROPERAND_TEMP)
       
   203                         regch = 'r';
       
   204 
       
   205                     printf("%c%d", regch, operand->index / 4);
       
   206                     if (isscalar)
       
   207                         printf(".%c", mask[idx]);
       
   208                     else if (elems != 4)
       
   209                     {
       
   210                         printf(".");
       
   211                         for (k = 0; k < elems; k++)
       
   212                             printf("%c", mask[idx++]);
       
   213                     } // else if
       
   214                     break;
       
   215                 } // case
       
   216 
       
   217                 default:
       
   218                     printf("[???{%d, %u}???]",
       
   219                             (int) operand->type, operand->index);
       
   220                     break;
       
   221             } // switch
       
   222         } // for
   228         } // for
   223 
   229 
   224         printf("\n");
   230         printf("\n");
   225     } // for
   231     } // for
   226 
   232