1D convolution (and post-conv scale/bias) were inadvertantly applied to 2D image...
[mesa.git] / src / mesa / swrast / s_nvfragprog.c
index b3f03282f10915dbbd83472125a927eceb0a4fbd..028ddc0090ddaa7bf0a73a484fd5397c06e4acbf 100644 (file)
@@ -44,7 +44,7 @@
 /* See comments below for info about this */
 #define LAMBDA_ZERO 1
 
-/* if 1, print some debugging info */
+/* debug predicate */
 #define DEBUG_FRAG 0
 
 
@@ -57,6 +57,9 @@ struct fp_machine
    GLfloat Inputs[MAX_NV_FRAGMENT_PROGRAM_INPUTS][4];
    GLfloat Outputs[MAX_NV_FRAGMENT_PROGRAM_OUTPUTS][4];
    GLuint CondCodes[4];  /**< COND_* value for x/y/z/w */
+
+   GLuint CallStack[MAX_PROGRAM_CALL_DEPTH]; /**< For CAL/RET instructions */
+   GLuint StackDepth; /**< Index/ptr to top of CallStack[] */
 };
 
 
@@ -461,7 +464,7 @@ store_vector4( const struct prog_instruction *inst,
          return;
    }
 
-#if DEBUG_FRAG
+#if 0
    if (value[0] > 1.0e10 ||
        IS_INF_OR_NAN(value[0]) ||
        IS_INF_OR_NAN(value[1]) ||
@@ -649,9 +652,9 @@ execute_program( GLcontext *ctx,
 {
    GLuint pc;
 
-#if DEBUG_FRAG
-   printf("execute fragment program --------------------\n");
-#endif
+   if (DEBUG_FRAG) {
+      printf("execute fragment program --------------------\n");
+   }
 
    for (pc = 0; pc < maxInst; pc++) {
       const struct prog_instruction *inst = program->Base.Instructions + pc;
@@ -663,9 +666,10 @@ execute_program( GLcontext *ctx,
                                        ctx->FragmentProgram.CallbackData);
       }
 
-#if DEBUG_FRAG
-      _mesa_print_instruction(inst);
-#endif
+      if (DEBUG_FRAG) {
+         _mesa_print_instruction(inst);
+      }
+
       switch (inst->Opcode) {
          case OPCODE_ABS:
             {
@@ -688,12 +692,43 @@ execute_program( GLcontext *ctx,
                result[2] = a[2] + b[2];
                result[3] = a[3] + b[3];
                store_vector4( inst, machine, result );
-#if DEBUG_FRAG
-               printf("ADD (%g %g %g %g) = (%g %g %g %g) + (%g %g %g %g)\n",
-                      result[0], result[1], result[2], result[3], 
-                      a[0], a[1], a[2], a[3],
-                      b[0], b[1], b[2], b[3]);
-#endif
+               if (DEBUG_FRAG) {
+                  printf("ADD (%g %g %g %g) = (%g %g %g %g) + (%g %g %g %g)\n",
+                         result[0], result[1], result[2], result[3], 
+                         a[0], a[1], a[2], a[3],
+                         b[0], b[1], b[2], b[3]);
+               }
+            }
+            break;
+         case OPCODE_BRA: /* conditional branch */
+            {
+               /* NOTE: The return is conditional! */
+               const GLuint swizzle = inst->DstReg.CondSwizzle;
+               const GLuint condMask = inst->DstReg.CondMask;
+               if (test_cc(machine->CondCodes[GET_SWZ(swizzle, 0)], condMask) ||
+                   test_cc(machine->CondCodes[GET_SWZ(swizzle, 1)], condMask) ||
+                   test_cc(machine->CondCodes[GET_SWZ(swizzle, 2)], condMask) ||
+                   test_cc(machine->CondCodes[GET_SWZ(swizzle, 3)], condMask)) {
+                  /* take branch */
+                  pc = inst->BranchTarget;
+               }
+            }
+            break;
+         case OPCODE_CAL: /* Call subroutine */
+            {
+               /* NOTE: The call is conditional! */
+               const GLuint swizzle = inst->DstReg.CondSwizzle;
+               const GLuint condMask = inst->DstReg.CondMask;
+               if (test_cc(machine->CondCodes[GET_SWZ(swizzle, 0)], condMask) ||
+                   test_cc(machine->CondCodes[GET_SWZ(swizzle, 1)], condMask) ||
+                   test_cc(machine->CondCodes[GET_SWZ(swizzle, 2)], condMask) ||
+                   test_cc(machine->CondCodes[GET_SWZ(swizzle, 3)], condMask)) {
+                  if (machine->StackDepth >= MAX_PROGRAM_CALL_DEPTH) {
+                     return GL_TRUE; /* Per GL_NV_vertex_program2 spec */
+                  }
+                  machine->CallStack[machine->StackDepth++] = pc + 1;
+                  pc = inst->BranchTarget;
+               }
             }
             break;
          case OPCODE_CMP:
@@ -770,10 +805,10 @@ execute_program( GLcontext *ctx,
                fetch_vector4( ctx, &inst->SrcReg[1], machine, program, b );
                result[0] = result[1] = result[2] = result[3] = DOT3(a, b);
                store_vector4( inst, machine, result );
-#if DEBUG_FRAG
-               printf("DP3 %g = (%g %g %g) . (%g %g %g)\n",
-                      result[0], a[0], a[1], a[2], b[0], b[1], b[2]);
-#endif
+               if (DEBUG_FRAG) {
+                  printf("DP3 %g = (%g %g %g) . (%g %g %g)\n",
+                         result[0], a[0], a[1], a[2], b[0], b[1], b[2]);
+               }
             }
             break;
          case OPCODE_DP4:
@@ -783,10 +818,11 @@ execute_program( GLcontext *ctx,
                fetch_vector4( ctx, &inst->SrcReg[1], machine, program, b );
                result[0] = result[1] = result[2] = result[3] = DOT4(a,b);
                store_vector4( inst, machine, result );
-#if DEBUG_FRAG
-               printf("DP4 %g = (%g, %g %g %g) . (%g, %g %g %g)\n",
-                      result[0], a[0], a[1], a[2], a[3], b[0], b[1], b[2], b[3]);
-#endif
+               if (DEBUG_FRAG) {
+                  printf("DP4 %g = (%g, %g %g %g) . (%g, %g %g %g)\n",
+                         result[0], a[0], a[1], a[2], a[3],
+                         b[0], b[1], b[2], b[3]);
+               }
             }
             break;
          case OPCODE_DPH:
@@ -894,11 +930,11 @@ execute_program( GLcontext *ctx,
                }
                result[3] = 1.0F;
                store_vector4( inst, machine, result );
-#if DEBUG_FRAG
-               printf("LIT (%g %g %g %g) : (%g %g %g %g)\n",
-                      result[0], result[1], result[2], result[3],
-                      a[0], a[1], a[2], a[3]);
-#endif
+               if (DEBUG_FRAG) {
+                  printf("LIT (%g %g %g %g) : (%g %g %g %g)\n",
+                         result[0], result[1], result[2], result[3],
+                         a[0], a[1], a[2], a[3]);
+               }
             }
             break;
          case OPCODE_LRP:
@@ -912,14 +948,14 @@ execute_program( GLcontext *ctx,
                result[2] = a[2] * b[2] + (1.0F - a[2]) * c[2];
                result[3] = a[3] * b[3] + (1.0F - a[3]) * c[3];
                store_vector4( inst, machine, result );
-#if DEBUG_FRAG
-               printf("LRP (%g %g %g %g) = (%g %g %g %g), "
-                      "(%g %g %g %g), (%g %g %g %g)\n",
-                      result[0], result[1], result[2], result[3],
-                      a[0], a[1], a[2], a[3],
-                      b[0], b[1], b[2], b[3],
-                      c[0], c[1], c[2], c[3]);
-#endif
+               if (DEBUG_FRAG) {
+                  printf("LRP (%g %g %g %g) = (%g %g %g %g), "
+                         "(%g %g %g %g), (%g %g %g %g)\n",
+                         result[0], result[1], result[2], result[3],
+                         a[0], a[1], a[2], a[3],
+                         b[0], b[1], b[2], b[3],
+                         c[0], c[1], c[2], c[3]);
+               }
             }
             break;
          case OPCODE_MAD:
@@ -933,14 +969,14 @@ execute_program( GLcontext *ctx,
                result[2] = a[2] * b[2] + c[2];
                result[3] = a[3] * b[3] + c[3];
                store_vector4( inst, machine, result );
-#if DEBUG_FRAG
-               printf("MAD (%g %g %g %g) = (%g %g %g %g) * "
-                      "(%g %g %g %g) + (%g %g %g %g)\n",
-                      result[0], result[1], result[2], result[3],
-                      a[0], a[1], a[2], a[3],
-                      b[0], b[1], b[2], b[3],
-                      c[0], c[1], c[2], c[3]);
-#endif
+               if (DEBUG_FRAG) {
+                  printf("MAD (%g %g %g %g) = (%g %g %g %g) * "
+                         "(%g %g %g %g) + (%g %g %g %g)\n",
+                         result[0], result[1], result[2], result[3],
+                         a[0], a[1], a[2], a[3],
+                         b[0], b[1], b[2], b[3],
+                         c[0], c[1], c[2], c[3]);
+               }
             }
             break;
          case OPCODE_MAX:
@@ -953,12 +989,12 @@ execute_program( GLcontext *ctx,
                result[2] = MAX2(a[2], b[2]);
                result[3] = MAX2(a[3], b[3]);
                store_vector4( inst, machine, result );
-#if DEBUG_FRAG
-               printf("MAX (%g %g %g %g) = (%g %g %g %g), (%g %g %g %g)\n",
-                      result[0], result[1], result[2], result[3], 
-                      a[0], a[1], a[2], a[3],
-                      b[0], b[1], b[2], b[3]);
-#endif
+               if (DEBUG_FRAG) {
+                  printf("MAX (%g %g %g %g) = (%g %g %g %g), (%g %g %g %g)\n",
+                         result[0], result[1], result[2], result[3], 
+                         a[0], a[1], a[2], a[3],
+                         b[0], b[1], b[2], b[3]);
+               }
             }
             break;
          case OPCODE_MIN:
@@ -978,10 +1014,10 @@ execute_program( GLcontext *ctx,
                GLfloat result[4];
                fetch_vector4( ctx, &inst->SrcReg[0], machine, program, result );
                store_vector4( inst, machine, result );
-#if DEBUG_FRAG
-               printf("MOV (%g %g %g %g)\n",
-                      result[0], result[1], result[2], result[3]);
-#endif
+               if (DEBUG_FRAG) {
+                  printf("MOV (%g %g %g %g)\n",
+                         result[0], result[1], result[2], result[3]);
+               }
             }
             break;
          case OPCODE_MUL:
@@ -994,12 +1030,12 @@ execute_program( GLcontext *ctx,
                result[2] = a[2] * b[2];
                result[3] = a[3] * b[3];
                store_vector4( inst, machine, result );
-#if DEBUG_FRAG
-               printf("MUL (%g %g %g %g) = (%g %g %g %g) * (%g %g %g %g)\n",
-                      result[0], result[1], result[2], result[3], 
-                      a[0], a[1], a[2], a[3],
-                      b[0], b[1], b[2], b[3]);
-#endif
+               if (DEBUG_FRAG) {
+                  printf("MUL (%g %g %g %g) = (%g %g %g %g) * (%g %g %g %g)\n",
+                         result[0], result[1], result[2], result[3], 
+                         a[0], a[1], a[2], a[3],
+                         b[0], b[1], b[2], b[3]);
+               }
             }
             break;
          case OPCODE_PK2H: /* pack two 16-bit floats in one 32-bit float */
@@ -1081,16 +1117,32 @@ execute_program( GLcontext *ctx,
             {
                GLfloat a[4], result[4];
                fetch_vector1( ctx, &inst->SrcReg[0], machine, program, a );
-#if DEBUG_FRAG
-               if (a[0] == 0)
-                  printf("RCP(0)\n");
-               else if (IS_INF_OR_NAN(a[0]))
-                  printf("RCP(inf)\n");
-#endif
+               if (DEBUG_FRAG) {
+                  if (a[0] == 0)
+                     printf("RCP(0)\n");
+                  else if (IS_INF_OR_NAN(a[0]))
+                     printf("RCP(inf)\n");
+               }
                result[0] = result[1] = result[2] = result[3] = 1.0F / a[0];
                store_vector4( inst, machine, result );
             }
             break;
+         case OPCODE_RET: /* return from subroutine */
+            {
+               /* NOTE: The return is conditional! */
+               const GLuint swizzle = inst->DstReg.CondSwizzle;
+               const GLuint condMask = inst->DstReg.CondMask;
+               if (test_cc(machine->CondCodes[GET_SWZ(swizzle, 0)], condMask) ||
+                   test_cc(machine->CondCodes[GET_SWZ(swizzle, 1)], condMask) ||
+                   test_cc(machine->CondCodes[GET_SWZ(swizzle, 2)], condMask) ||
+                   test_cc(machine->CondCodes[GET_SWZ(swizzle, 3)], condMask)) {
+                  if (machine->StackDepth == 0) {
+                     return GL_TRUE; /* Per GL_NV_vertex_program2 spec */
+                  }
+                  pc = machine->CallStack[--machine->StackDepth];
+               }
+            }
+            break;
          case OPCODE_RFL: /* reflection vector */
             {
                GLfloat axis[4], dir[4], result[4], tmpX, tmpW;
@@ -1112,9 +1164,9 @@ execute_program( GLcontext *ctx,
                a[0] = FABSF(a[0]);
                result[0] = result[1] = result[2] = result[3] = INV_SQRTF(a[0]);
                store_vector4( inst, machine, result );
-#if DEBUG_FRAG
-               printf("RSQ %g = 1/sqrt(|%g|)\n", result[0], a[0]);
-#endif
+               if (DEBUG_FRAG) {
+                  printf("RSQ %g = 1/sqrt(|%g|)\n", result[0], a[0]);
+               }
             }
             break;
          case OPCODE_SCS: /* sine and cos */
@@ -1231,11 +1283,11 @@ execute_program( GLcontext *ctx,
                result[2] = a[2] - b[2];
                result[3] = a[3] - b[3];
                store_vector4( inst, machine, result );
-#if DEBUG_FRAG
-               printf("SUB (%g %g %g %g) = (%g %g %g %g) - (%g %g %g %g)\n",
-                      result[0], result[1], result[2], result[3],
-                      a[0], a[1], a[2], a[3], b[0], b[1], b[2], b[3]);
-#endif
+               if (DEBUG_FRAG) {
+                  printf("SUB (%g %g %g %g) = (%g %g %g %g) - (%g %g %g %g)\n",
+                         result[0], result[1], result[2], result[3],
+                         a[0], a[1], a[2], a[3], b[0], b[1], b[2], b[3]);
+               }
             }
             break;
          case OPCODE_SWZ: /* extended swizzle */
@@ -1265,41 +1317,41 @@ execute_program( GLcontext *ctx,
          case OPCODE_TEX: /* Both ARB and NV frag prog */
             /* Texel lookup */
             {
-               /* Note: we're using zero instead of lambda for the LOD.
-                * The problem is we didn't necessarily use the right partial
-                * derivatives when we called _swrast_compute_lambda() earlier.
-                * A texture can be sampled with any coordinate, not just
-                * fragment.texcoord[n].
-                * Use zero for now because that's better than using a bad
-                * lambda value (I've seen a few test programs fail otherwise).
-                * We need to overhaul this stuff someday.
+               /* Note: only use the precomputed lambda value when we're
+                * sampling texture unit [K] with texcoord[K].
+                * Otherwise, the lambda value may have no relation to the
+                * instruction's texcoord or texture image.  Using the wrong
+                * lambda is usually bad news.
+                * The rest of the time, just use zero (until we get a more
+                * sophisticated way of computing lambda).
                 */
-#ifdef LAMBDA_ZERO
-               GLfloat lambda = 0.0;
-#else
-               GLfloat lambda = span->array->lambda[inst->TexSrcUnit][column];
-#endif
-               GLfloat coord[4], color[4];
+               GLfloat coord[4], color[4], lambda;
+               if (inst->SrcReg[0].File == PROGRAM_INPUT &&
+                   inst->SrcReg[0].Index == FRAG_ATTRIB_TEX0+inst->TexSrcUnit)
+                  lambda = span->array->lambda[inst->TexSrcUnit][column];
+               else
+                  lambda = 0.0;
                fetch_vector4(ctx, &inst->SrcReg[0], machine, program, coord);
                fetch_texel( ctx, coord, lambda, inst->TexSrcUnit, color );
-#if DEBUG_FRAG
-               printf("TEX (%g, %g, %g, %g) = texture[%d][%g, %g, %g, %g], "
-                      "lod %f\n",
-                      color[0], color[1], color[2], color[3], inst->TexSrcUnit,
-                      coord[0], coord[1], coord[2], coord[3], lambda);
-#endif
+               if (DEBUG_FRAG) {
+                  printf("TEX (%g, %g, %g, %g) = texture[%d][%g, %g, %g, %g], "
+                         "lod %f\n",
+                         color[0], color[1], color[2], color[3],
+                         inst->TexSrcUnit,
+                         coord[0], coord[1], coord[2], coord[3], lambda);
+               }
                store_vector4( inst, machine, color );
             }
             break;
          case OPCODE_TXB: /* GL_ARB_fragment_program only */
             /* Texel lookup with LOD bias */
             {
-#ifdef LAMBDA_ZERO
-               GLfloat lambda = 0.0;
-#else
-               GLfloat lambda = span->array->lambda[inst->TexSrcUnit][column];
-#endif
-               GLfloat coord[4], color[4], bias;
+               GLfloat coord[4], color[4], lambda, bias;
+               if (inst->SrcReg[0].File == PROGRAM_INPUT &&
+                   inst->SrcReg[0].Index == FRAG_ATTRIB_TEX0+inst->TexSrcUnit)
+                  lambda = span->array->lambda[inst->TexSrcUnit][column];
+               else
+                  lambda = 0.0;
                fetch_vector4(ctx, &inst->SrcReg[0], machine, program, coord);
                /* coord[3] is the bias to add to lambda */
                bias = ctx->Texture.Unit[inst->TexSrcUnit].LodBias
@@ -1324,12 +1376,12 @@ execute_program( GLcontext *ctx,
          case OPCODE_TXP: /* GL_ARB_fragment_program only */
             /* Texture lookup w/ projective divide */
             {
-#ifdef LAMBDA_ZERO
-               GLfloat lambda = 0.0;
-#else
-               GLfloat lambda = span->array->lambda[inst->TexSrcUnit][column];
-#endif
-               GLfloat texcoord[4], color[4];
+               GLfloat texcoord[4], color[4], lambda;
+               if (inst->SrcReg[0].File == PROGRAM_INPUT &&
+                   inst->SrcReg[0].Index == FRAG_ATTRIB_TEX0+inst->TexSrcUnit)
+                  lambda = span->array->lambda[inst->TexSrcUnit][column];
+               else
+                  lambda = 0.0;
                fetch_vector4(ctx, &inst->SrcReg[0], machine, program,texcoord);
               /* Not so sure about this test - if texcoord[3] is
                * zero, we'd probably be fine except for an ASSERT in
@@ -1347,13 +1399,13 @@ execute_program( GLcontext *ctx,
          case OPCODE_TXP_NV: /* GL_NV_fragment_program only */
             /* Texture lookup w/ projective divide */
             {
-#ifdef LAMBDA_ZERO
-               GLfloat lambda = 0.0;
-#else
-               GLfloat lambda = span->array->lambda[inst->TexSrcUnit][column];
-#endif
-               GLfloat texcoord[4], color[4];
-               fetch_vector4( ctx, &inst->SrcReg[0], machine, program, texcoord );
+               GLfloat texcoord[4], color[4], lambda;
+               if (inst->SrcReg[0].File == PROGRAM_INPUT &&
+                   inst->SrcReg[0].Index == FRAG_ATTRIB_TEX0+inst->TexSrcUnit)
+                  lambda = span->array->lambda[inst->TexSrcUnit][column];
+               else
+                  lambda = 0.0;
+               fetch_vector4(ctx, &inst->SrcReg[0], machine, program,texcoord);
                if (inst->TexSrcTarget != TEXTURE_CUBE_INDEX &&
                   texcoord[3] != 0.0) {
                   texcoord[0] /= texcoord[3];
@@ -1537,6 +1589,9 @@ init_machine( GLcontext *ctx, struct fp_machine *machine,
    machine->CondCodes[1] = COND_EQ;
    machine->CondCodes[2] = COND_EQ;
    machine->CondCodes[3] = COND_EQ;
+
+   /* init call stack */
+   machine->StackDepth = 0;
 }
 
 
@@ -1598,11 +1653,6 @@ _swrast_exec_fragment_program( GLcontext *ctx, SWspan *span )
 
    ctx->_CurrentProgram = GL_FRAGMENT_PROGRAM_ARB; /* or NV, doesn't matter */
 
-#if 1 /* we really shouldn't need this here... */
-   if (program->Base.Parameters) {
-      _mesa_load_state_parameters(ctx, program->Base.Parameters);
-   }
-#endif
    run_program(ctx, span, 0, span->end);
 
    if (program->Base.OutputsWritten & (1 << FRAG_RESULT_DEPR)) {