Place preshader destination register last in operand list.
authorRyan C. Gordon <icculus@icculus.org>
Wed, 01 Jun 2011 13:22:21 -0400
changeset 1046 097aefb03885
parent 1045 448766b7c981
child 1047 6b92ce2fc809
Place preshader destination register last in operand list. This is where it's located in the shader, and it simplies some things.
mojoshader.c
mojoshader_effects.c
utils/testparse.c
--- a/mojoshader.c	Wed Jun 01 05:35:50 2011 -0400
+++ b/mojoshader.c	Wed Jun 01 13:22:21 2011 -0400
@@ -7320,14 +7320,11 @@
             return;
         } // if
 
-        MOJOSHADER_preshaderOperand *operand = &inst->operands[1];
+        MOJOSHADER_preshaderOperand *operand = inst->operands;
         while (operand_count--)
         {
             const unsigned int item = (unsigned int) SWAP32(fxlc.tokens[2]);
 
-            if (operand_count == 0)  // List destination first.
-                operand = &inst->operands[0];
-
             // !!! FIXME: don't know what first token does.
             switch (SWAP32(fxlc.tokens[1]))
             {
--- a/mojoshader_effects.c	Wed Jun 01 05:35:50 2011 -0400
+++ b/mojoshader_effects.c	Wed Jun 01 13:22:21 2011 -0400
@@ -34,14 +34,14 @@
 
     for (instit = 0; instit < preshader->instruction_count; instit++, inst++)
     {
-        const MOJOSHADER_preshaderOperand *operand = &inst->operands[1];
+        const MOJOSHADER_preshaderOperand *operand = inst->operands;
         const int isscalar = (inst->opcode >= scalarstart);
         const int elems = inst->element_count;
         const int elemsbytes = sizeof (double) * elems;
 
         // load up our operands...
         int opiter, elemiter;
-        for (opiter = 1; opiter < inst->operand_count; opiter++, operand++)
+        for (opiter = 0; opiter < inst->operand_count-1; opiter++, operand++)
         {
             const unsigned int index = operand->index;
             switch (operand->type)
@@ -164,7 +164,6 @@
         } // switch
 
         // Figure out where dst wants to be stored.
-        operand = inst->operands;
         if (operand->type == MOJOSHADER_PRESHADEROPERAND_TEMP)
             memcpy(temps + operand->index, dst, elemsbytes);
         else
--- a/utils/testparse.c	Wed Jun 01 05:35:50 2011 -0400
+++ b/utils/testparse.c	Wed Jun 01 13:22:21 2011 -0400
@@ -135,11 +135,72 @@
 } // print_symbols
 
 
+static void print_preshader_operand(const MOJOSHADER_preshader *preshader,
+                                    const int instidx, const int opidx)
+{
+    static char mask[] = { 'x', 'y', 'z', 'w' };
+    const MOJOSHADER_preshaderInstruction *inst = &preshader->instructions[instidx];
+    const MOJOSHADER_preshaderOperand *operand = &inst->operands[opidx];
+    const int elems = inst->element_count;
+    const int isscalarop = (inst->opcode >= MOJOSHADER_PRESHADEROP_SCALAR_OPS);
+    const int isscalar = ((isscalarop) && (opidx == 1)); // probably wrong.
+    int i;
+
+    switch (operand->type)
+    {
+        case MOJOSHADER_PRESHADEROPERAND_LITERAL:
+        {
+            const double *lit = &preshader->literals[operand->index];
+            printf("(");
+            if (isscalar)
+            {
+                const double val = *lit;
+                for (i = 0; i < elems-1; i++)
+                    printf("%g, ", val);
+                printf("%g)", val);
+            } // if
+            else
+            {
+                for (i = 0; i < elems-1; i++, lit++)
+                    printf("%g, ", *lit);
+                printf("%g)", *lit);
+            } // else
+            break;
+        } // case
+
+        case MOJOSHADER_PRESHADEROPERAND_INPUT:
+        case MOJOSHADER_PRESHADEROPERAND_OUTPUT:
+        case MOJOSHADER_PRESHADEROPERAND_TEMP:
+        {
+            int idx = operand->index % 4;
+            char regch = 'c';
+            if (operand->type == MOJOSHADER_PRESHADEROPERAND_TEMP)
+                regch = 'r';
+
+            printf("%c%d", regch, operand->index / 4);
+            if (isscalar)
+                printf(".%c", mask[idx]);
+            else if (elems != 4)
+            {
+                printf(".");
+                for (i = 0; i < elems; i++)
+                    printf("%c", mask[idx++]);
+            } // else if
+            break;
+        } // case
+
+        default:
+            printf("[???{%d, %u}???]", (int) operand->type, operand->index);
+            break;
+    } // switch
+} // print_preshader_operand
+
+
 static void print_preshader(const MOJOSHADER_preshader *preshader,
                             const int indent)
 {
     MOJOSHADER_preshaderInstruction *inst = preshader->instructions;
-    int i, j, k;
+    int i, j;
 
     static const char *opcodestr[] = {
         "nop", "mov", "neg", "rcp", "frc", "exp", "log", "rsq", "sin", "cos",
@@ -148,77 +209,22 @@
         "ge", "add", "mul", "atan2", "div", "dot", "noise"
     };
 
-    static char mask[] = { 'x', 'y', 'z', 'w' };
-
     INDENT(); printf("PRESHADER:\n");
 
     print_symbols(preshader->symbols, preshader->symbol_count, indent + 1);
 
     for (i = 0; i < preshader->instruction_count; i++, inst++)
     {
-        const MOJOSHADER_preshaderOperand *operand = inst->operands;
-        const int scalarstart = (int) MOJOSHADER_PRESHADEROP_SCALAR_OPS;
-        const int isscalarop = (inst->opcode >= scalarstart);
-
-        INDENT(); printf("    %s", opcodestr[inst->opcode]);
+        INDENT(); printf("    %s ", opcodestr[inst->opcode]);
 
-        for (j = 0; j < inst->operand_count; j++, operand++)
-        {
-            const int elems = inst->element_count;
-            const int isscalar = ((isscalarop) && (j == 1)); // probably wrong.
-
-            if (j != 0)
-                printf(",");
-            printf(" ");
+        // print dest register first...
+        print_preshader_operand(preshader, i, inst->operand_count - 1);
 
-            switch (operand->type)
-            {
-                case MOJOSHADER_PRESHADEROPERAND_LITERAL:
-                {
-                    const double *lit = &preshader->literals[operand->index];
-                    printf("(");
-                    if (isscalar)
-                    {
-                        const double val = *lit;
-                        for (k = 0; k < elems-1; k++)
-                            printf("%g, ", val);
-                        printf("%g)", val);
-                    } // if
-                    else
-                    {
-                        for (k = 0; k < elems-1; k++, lit++)
-                            printf("%g, ", *lit);
-                        printf("%g)", *lit);
-                    } // else
-                    break;
-                } // case
-
-                case MOJOSHADER_PRESHADEROPERAND_INPUT:
-                case MOJOSHADER_PRESHADEROPERAND_OUTPUT:
-                case MOJOSHADER_PRESHADEROPERAND_TEMP:
-                {
-                    int idx = operand->index % 4;
-                    char regch = 'c';
-                    if (operand->type == MOJOSHADER_PRESHADEROPERAND_TEMP)
-                        regch = 'r';
-
-                    printf("%c%d", regch, operand->index / 4);
-                    if (isscalar)
-                        printf(".%c", mask[idx]);
-                    else if (elems != 4)
-                    {
-                        printf(".");
-                        for (k = 0; k < elems; k++)
-                            printf("%c", mask[idx++]);
-                    } // else if
-                    break;
-                } // case
-
-                default:
-                    printf("[???{%d, %u}???]",
-                            (int) operand->type, operand->index);
-                    break;
-            } // switch
+        // ...then the source registers.
+        for (j = 0; j < inst->operand_count - 1; j++)
+        {
+            printf(", ");
+            print_preshader_operand(preshader, i, j);
         } // for
 
         printf("\n");