mesa/main: Add more OpenGL ES types to glheader.h.
[mesa.git] / src / mesa / swrast / s_fragprog.c
index 525cf9d72457b383388fe7ff578a919916b606ba..77a77f0bcbb0e838bb1b6d646985ca72c734333c 100644 (file)
 #include "s_span.h"
 
 
+/**
+ * Apply texture object's swizzle (X/Y/Z/W/0/1) to incoming 'texel'
+ * and return results in 'colorOut'.
+ */
+static INLINE void
+swizzle_texel(const GLfloat texel[4], GLfloat colorOut[4], GLuint swizzle)
+{
+   if (swizzle == SWIZZLE_NOOP) {
+      COPY_4V(colorOut, texel);
+   }
+   else {
+      GLfloat vector[6];
+      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)];
+      colorOut[1] = vector[GET_SWZ(swizzle, 1)];
+      colorOut[2] = vector[GET_SWZ(swizzle, 2)];
+      colorOut[3] = vector[GET_SWZ(swizzle, 3)];
+   }
+}
+
+
 /**
  * Fetch a texel with given lod.
  * Called via machine->FetchTexelLod()
@@ -40,20 +66,22 @@ static void
 fetch_texel_lod( GLcontext *ctx, const GLfloat texcoord[4], GLfloat lambda,
                  GLuint unit, GLfloat color[4] )
 {
-   GLchan rgba[4];
-   SWcontext *swrast = SWRAST_CONTEXT(ctx);
    const struct gl_texture_object *texObj = ctx->Texture.Unit[unit]._Current;
 
-   if (texObj)
+   if (texObj) {
+      SWcontext *swrast = SWRAST_CONTEXT(ctx);
+      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);
-   color[0] = CHAN_TO_FLOAT(rgba[0]);
-   color[1] = CHAN_TO_FLOAT(rgba[1]);
-   color[2] = CHAN_TO_FLOAT(rgba[2]);
-   color[3] = CHAN_TO_FLOAT(rgba[3]);
+      swrast->TextureSample[unit](ctx, texObj, 1,
+                                  (const GLfloat (*)[4]) texcoord,
+                                  &lambda, &rgba);
+      swizzle_texel(rgba, color, texObj->_Swizzle);
+   }
+   else {
+      ASSIGN_4V(color, 0.0F, 0.0F, 0.0F, 1.0F);
+   }
 }
 
 
@@ -61,6 +89,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],
@@ -68,31 +98,36 @@ 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;
-   GLfloat lambda;
-   GLchan rgba[4];
+   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 = texObj->Image[0][texObj->BaseLevel];
+      const struct gl_texture_image *texImg =
+         texObj->Image[0][texObj->BaseLevel];
       const GLfloat texW = (GLfloat) texImg->WidthScale;
       const GLfloat texH = (GLfloat) texImg->HeightScale;
+      GLfloat lambda;
+      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);
-   }
 
-   swrast->TextureSample[unit](ctx, texObj, 1, (const GLfloat (*)[4]) texcoord,
-                               &lambda, &rgba);
-   color[0] = CHAN_TO_FLOAT(rgba[0]);
-   color[1] = CHAN_TO_FLOAT(rgba[1]);
-   color[2] = CHAN_TO_FLOAT(rgba[2]);
-   color[3] = CHAN_TO_FLOAT(rgba[3]);
+      swrast->TextureSample[unit](ctx, texObj, 1,
+                                  (const GLfloat (*)[4]) texcoord,
+                                  &lambda, &rgba);
+      swizzle_texel(rgba, color, texObj->_Swizzle);
+   }
+   else {
+      ASSIGN_4V(color, 0.0F, 0.0F, 0.0F, 1.0F);
+   }
 }
 
 
@@ -127,9 +162,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.0 - span->facing;
    }
 
    machine->CurElement = col;
@@ -167,9 +201,9 @@ 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_COLR)) {
+            if (outputsWritten & (1 << FRAG_RESULT_COLOR)) {
                COPY_4V(span->array->attribs[FRAG_ATTRIB_COL0][i],
-                       machine->Outputs[FRAG_RESULT_COLR]);
+                       machine->Outputs[FRAG_RESULT_COLOR]);
             }
             else {
                /* Multiple drawbuffers / render targets
@@ -186,8 +220,8 @@ run_program(GLcontext *ctx, SWspan *span, GLuint start, GLuint end)
             }
 
             /* Store result depth/z */
-            if (outputsWritten & (1 << FRAG_RESULT_DEPR)) {
-               const GLfloat depth = machine->Outputs[FRAG_RESULT_DEPR][2];
+            if (outputsWritten & (1 << FRAG_RESULT_DEPTH)) {
+               const GLfloat depth = machine->Outputs[FRAG_RESULT_DEPTH][2];
                if (depth <= 0.0)
                   span->array->z[i] = 0;
                else if (depth >= 1.0)
@@ -220,20 +254,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_COLR)) {
+   if (program->Base.OutputsWritten & (1 << FRAG_RESULT_COLOR)) {
       span->interpMask &= ~SPAN_RGBA;
       span->arrayMask |= SPAN_RGBA;
    }
 
-   if (program->Base.OutputsWritten & (1 << FRAG_RESULT_DEPR)) {
+   if (program->Base.OutputsWritten & (1 << FRAG_RESULT_DEPTH)) {
       span->interpMask &= ~SPAN_Z;
       span->arrayMask |= SPAN_Z;
    }
-
-   ctx->_CurrentProgram = 0;
 }