drisw: Move pointer dereferences to after NULL checks.
[mesa.git] / src / mesa / swrast / s_fragprog.c
index 3735f041d66c0ff1aff7734d65e9f28dcc99081b..7c1de62e878b54eb40acf12317e2acb706925c06 100644 (file)
@@ -25,7 +25,6 @@
 #include "main/glheader.h"
 #include "main/colormac.h"
 #include "main/context.h"
-#include "main/texstate.h"
 #include "shader/prog_instruction.h"
 
 #include "s_fragprog.h"
  * and return results in 'colorOut'.
  */
 static INLINE void
-swizzle_texel(const GLchan texel[4], GLfloat colorOut[4], GLuint swizzle)
+swizzle_texel(const GLfloat texel[4], GLfloat colorOut[4], GLuint swizzle)
 {
    if (swizzle == SWIZZLE_NOOP) {
-      colorOut[0] = CHAN_TO_FLOAT(texel[0]);
-      colorOut[1] = CHAN_TO_FLOAT(texel[1]);
-      colorOut[2] = CHAN_TO_FLOAT(texel[2]);
-      colorOut[3] = CHAN_TO_FLOAT(texel[3]);
+      COPY_4V(colorOut, texel);
    }
    else {
       GLfloat vector[6];
-      vector[SWIZZLE_X] = CHAN_TO_FLOAT(texel[0]);
-      vector[SWIZZLE_Y] = CHAN_TO_FLOAT(texel[1]);
-      vector[SWIZZLE_Z] = CHAN_TO_FLOAT(texel[2]);
-      vector[SWIZZLE_W] = CHAN_TO_FLOAT(texel[3]);
+      vector[SWIZZLE_X] = texel[0];
+      vector[SWIZZLE_Y] = texel[1];
+      vector[SWIZZLE_Z] = texel[2];
+      vector[SWIZZLE_W] = texel[3];
       vector[SWIZZLE_ZERO] = 0.0F;
       vector[SWIZZLE_ONE] = 1.0F;
       colorOut[0] = vector[GET_SWZ(swizzle, 0)];
@@ -73,11 +69,10 @@ fetch_texel_lod( GLcontext *ctx, const GLfloat texcoord[4], GLfloat lambda,
 
    if (texObj) {
       SWcontext *swrast = SWRAST_CONTEXT(ctx);
-      GLchan rgba[4];
+      GLfloat rgba[4];
 
       lambda = CLAMP(lambda, texObj->MinLod, texObj->MaxLod);
 
-      /* XXX use a float-valued TextureSample routine here!!! */
       swrast->TextureSample[unit](ctx, texObj, 1,
                                   (const GLfloat (*)[4]) texcoord,
                                   &lambda, &rgba);
@@ -93,6 +88,8 @@ fetch_texel_lod( GLcontext *ctx, const GLfloat texcoord[4], GLfloat lambda,
  * Fetch a texel with the given partial derivatives to compute a level
  * of detail in the mipmap.
  * Called via machine->FetchTexelDeriv()
+ * \param lodBias  the lod bias which may be specified by a TXB instruction,
+ *                 otherwise zero.
  */
 static void
 fetch_texel_deriv( GLcontext *ctx, const GLfloat texcoord[4],
@@ -100,7 +97,8 @@ fetch_texel_deriv( GLcontext *ctx, const GLfloat texcoord[4],
                    GLfloat lodBias, GLuint unit, GLfloat color[4] )
 {
    SWcontext *swrast = SWRAST_CONTEXT(ctx);
-   const struct gl_texture_object *texObj = ctx->Texture.Unit[unit]._Current;
+   const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
+   const struct gl_texture_object *texObj = texUnit->_Current;
 
    if (texObj) {
       const struct gl_texture_image *texImg =
@@ -108,18 +106,19 @@ fetch_texel_deriv( GLcontext *ctx, const GLfloat texcoord[4],
       const GLfloat texW = (GLfloat) texImg->WidthScale;
       const GLfloat texH = (GLfloat) texImg->HeightScale;
       GLfloat lambda;
-      GLchan rgba[4];
+      GLfloat rgba[4];
 
       lambda = _swrast_compute_lambda(texdx[0], texdy[0], /* ds/dx, ds/dy */
                                       texdx[1], texdy[1], /* dt/dx, dt/dy */
-                                      texdx[3], texdy[2], /* dq/dx, dq/dy */
+                                      texdx[3], texdy[3], /* dq/dx, dq/dy */
                                       texW, texH,
                                       texcoord[0], texcoord[1], texcoord[3],
-                                      1.0F / texcoord[3]) + lodBias;
+                                      1.0F / texcoord[3]);
+
+      lambda += lodBias + texUnit->LodBias + texObj->LodBias;
 
       lambda = CLAMP(lambda, texObj->MinLod, texObj->MaxLod);
 
-      /* XXX use a float-valued TextureSample routine here!!! */
       swrast->TextureSample[unit](ctx, texObj, 1,
                                   (const GLfloat (*)[4]) texcoord,
                                   &lambda, &rgba);
@@ -145,10 +144,19 @@ init_machine(GLcontext *ctx, struct gl_program_machine *machine,
              const struct gl_fragment_program *program,
              const SWspan *span, GLuint col)
 {
+   GLfloat *wpos = span->array->attribs[FRAG_ATTRIB_WPOS][col];
+
    if (program->Base.Target == GL_FRAGMENT_PROGRAM_NV) {
       /* Clear temporary registers (undefined for ARB_f_p) */
-      _mesa_bzero(machine->Temporaries,
-                  MAX_PROGRAM_TEMPS * 4 * sizeof(GLfloat));
+      memset(machine->Temporaries, 0, MAX_PROGRAM_TEMPS * 4 * sizeof(GLfloat));
+   }
+
+   /* ARB_fragment_coord_conventions */
+   if (program->OriginUpperLeft)
+      wpos[1] = ctx->DrawBuffer->Height - 1 - wpos[1];
+   if (!program->PixelCenterInteger) {
+      wpos[0] += 0.5F;
+      wpos[1] += 0.5F;
    }
 
    /* Setup pointer to input attributes */
@@ -162,9 +170,8 @@ init_machine(GLcontext *ctx, struct gl_program_machine *machine,
 
    /* if running a GLSL program (not ARB_fragment_program) */
    if (ctx->Shader.CurrentProgram) {
-      /* Store front/back facing value in register FOGC.Y */
-      machine->Attribs[FRAG_ATTRIB_FOGC][col][1] = 1.0 - span->facing;
-      /* Note FOGC.ZW is gl_PointCoord if drawing a sprite */
+      /* Store front/back facing value */
+      machine->Attribs[FRAG_ATTRIB_FACE][col][0] = 1.0F - span->facing;
    }
 
    machine->CurElement = col;
@@ -191,7 +198,7 @@ run_program(GLcontext *ctx, SWspan *span, GLuint start, GLuint end)
 {
    SWcontext *swrast = SWRAST_CONTEXT(ctx);
    const struct gl_fragment_program *program = ctx->FragmentProgram._Current;
-   const GLbitfield outputsWritten = program->Base.OutputsWritten;
+   const GLbitfield64 outputsWritten = program->Base.OutputsWritten;
    struct gl_program_machine *machine = &swrast->FragProgMachine;
    GLuint i;
 
@@ -202,7 +209,7 @@ run_program(GLcontext *ctx, SWspan *span, GLuint start, GLuint end)
          if (_mesa_execute_program(ctx, &program->Base, machine)) {
 
             /* Store result color */
-            if (outputsWritten & (1 << FRAG_RESULT_COLOR)) {
+           if (outputsWritten & BITFIELD64_BIT(FRAG_RESULT_COLOR)) {
                COPY_4V(span->array->attribs[FRAG_ATTRIB_COL0][i],
                        machine->Outputs[FRAG_RESULT_COLOR]);
             }
@@ -213,7 +220,7 @@ run_program(GLcontext *ctx, SWspan *span, GLuint start, GLuint end)
                 */
                GLuint buf;
                for (buf = 0; buf < ctx->DrawBuffer->_NumColorDrawBuffers; buf++) {
-                  if (outputsWritten & (1 << (FRAG_RESULT_DATA0 + buf))) {
+                  if (outputsWritten & BITFIELD64_BIT(FRAG_RESULT_DATA0 + buf)) {
                      COPY_4V(span->array->attribs[FRAG_ATTRIB_COL0 + buf][i],
                              machine->Outputs[FRAG_RESULT_DATA0 + buf]);
                   }
@@ -221,7 +228,7 @@ run_program(GLcontext *ctx, SWspan *span, GLuint start, GLuint end)
             }
 
             /* Store result depth/z */
-            if (outputsWritten & (1 << FRAG_RESULT_DEPTH)) {
+            if (outputsWritten & BITFIELD64_BIT(FRAG_RESULT_DEPTH)) {
                const GLfloat depth = machine->Outputs[FRAG_RESULT_DEPTH][2];
                if (depth <= 0.0)
                   span->array->z[i] = 0;
@@ -255,20 +262,16 @@ _swrast_exec_fragment_program( GLcontext *ctx, SWspan *span )
       ASSERT(span->array->ChanType == GL_FLOAT);
    }
 
-   ctx->_CurrentProgram = GL_FRAGMENT_PROGRAM_ARB; /* or NV, doesn't matter */
-
    run_program(ctx, span, 0, span->end);
 
-   if (program->Base.OutputsWritten & (1 << FRAG_RESULT_COLOR)) {
+   if (program->Base.OutputsWritten & BITFIELD64_BIT(FRAG_RESULT_COLOR)) {
       span->interpMask &= ~SPAN_RGBA;
       span->arrayMask |= SPAN_RGBA;
    }
 
-   if (program->Base.OutputsWritten & (1 << FRAG_RESULT_DEPTH)) {
+   if (program->Base.OutputsWritten & BITFIELD64_BIT(FRAG_RESULT_DEPTH)) {
       span->interpMask &= ~SPAN_Z;
       span->arrayMask |= SPAN_Z;
    }
-
-   ctx->_CurrentProgram = 0;
 }