swrast: fix incorrect tri culling in selection/feedback mode.
[mesa.git] / src / mesa / swrast / s_span.c
index 214c2a1b6f338b4cf758d8876758d1aef19ae060..fa8ca1d0e26cefc96fa88d50e538423c7a68e637 100644 (file)
@@ -1,8 +1,9 @@
 /*
  * Mesa 3-D graphics library
- * Version:  7.1
+ * Version:  7.5
  *
- * Copyright (C) 1999-2007  Brian Paul   All Rights Reserved.
+ * Copyright (C) 1999-2008  Brian Paul   All Rights Reserved.
+ * Copyright (C) 2009  VMware, Inc.  All Rights Reserved.
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
@@ -188,10 +189,10 @@ interpolate_active_attribs(GLcontext *ctx, SWspan *span, GLbitfield attrMask)
          const GLfloat dv1dx = span->attrStepX[attr][1];
          const GLfloat dv2dx = span->attrStepX[attr][2];
          const GLfloat dv3dx = span->attrStepX[attr][3];
-         GLfloat v0 = span->attrStart[attr][0];
-         GLfloat v1 = span->attrStart[attr][1];
-         GLfloat v2 = span->attrStart[attr][2];
-         GLfloat v3 = span->attrStart[attr][3];
+         GLfloat v0 = span->attrStart[attr][0] + span->leftClip * dv0dx;
+         GLfloat v1 = span->attrStart[attr][1] + span->leftClip * dv1dx;
+         GLfloat v2 = span->attrStart[attr][2] + span->leftClip * dv2dx;
+         GLfloat v3 = span->attrStart[attr][3] + span->leftClip * dv3dx;
          GLuint k;
          for (k = 0; k < span->end; k++) {
             const GLfloat invW = 1.0f / w;
@@ -442,11 +443,10 @@ _swrast_span_interpolate_z( const GLcontext *ctx, SWspan *span )
  * Compute mipmap LOD from partial derivatives.
  * This the ideal solution, as given in the OpenGL spec.
  */
-#if 0
-static GLfloat
-compute_lambda(GLfloat dsdx, GLfloat dsdy, GLfloat dtdx, GLfloat dtdy,
-               GLfloat dqdx, GLfloat dqdy, GLfloat texW, GLfloat texH,
-               GLfloat s, GLfloat t, GLfloat q, GLfloat invQ)
+GLfloat
+_swrast_compute_lambda(GLfloat dsdx, GLfloat dsdy, GLfloat dtdx, GLfloat dtdy,
+                       GLfloat dqdx, GLfloat dqdy, GLfloat texW, GLfloat texH,
+                       GLfloat s, GLfloat t, GLfloat q, GLfloat invQ)
 {
    GLfloat dudx = texW * ((s + dsdx) / (q + dqdx) - s * invQ);
    GLfloat dvdx = texH * ((t + dtdx) / (q + dqdx) - t * invQ);
@@ -458,13 +458,13 @@ compute_lambda(GLfloat dsdx, GLfloat dsdy, GLfloat dtdx, GLfloat dtdy,
    GLfloat lambda = LOG2(rho);
    return lambda;
 }
-#endif
 
 
 /**
  * Compute mipmap LOD from partial derivatives.
  * This is a faster approximation than above function.
  */
+#if 0
 GLfloat
 _swrast_compute_lambda(GLfloat dsdx, GLfloat dsdy, GLfloat dtdx, GLfloat dtdy,
                      GLfloat dqdx, GLfloat dqdy, GLfloat texW, GLfloat texH,
@@ -485,6 +485,7 @@ _swrast_compute_lambda(GLfloat dsdx, GLfloat dsdy, GLfloat dtdx, GLfloat dtdy,
    lambda = LOG2(rho);
    return lambda;
 }
+#endif
 
 
 /**
@@ -521,10 +522,10 @@ interpolate_texcoords(GLcontext *ctx, SWspan *span)
          const GLfloat drdx = span->attrStepX[attr][2];
          const GLfloat dqdx = span->attrStepX[attr][3];
          const GLfloat dqdy = span->attrStepY[attr][3];
-         GLfloat s = span->attrStart[attr][0];
-         GLfloat t = span->attrStart[attr][1];
-         GLfloat r = span->attrStart[attr][2];
-         GLfloat q = span->attrStart[attr][3];
+         GLfloat s = span->attrStart[attr][0] + span->leftClip * dsdx;
+         GLfloat t = span->attrStart[attr][1] + span->leftClip * dtdx;
+         GLfloat r = span->attrStart[attr][2] + span->leftClip * drdx;
+         GLfloat q = span->attrStart[attr][3] + span->leftClip * dqdx;
 
          if (obj) {
             const struct gl_texture_image *img = obj->Image[0][obj->BaseLevel];
@@ -546,7 +547,7 @@ interpolate_texcoords(GLcontext *ctx, SWspan *span)
                 || ctx->ATIFragmentShader._Enabled) {
                /* do perspective correction but don't divide s, t, r by q */
                const GLfloat dwdx = span->attrStepX[FRAG_ATTRIB_WPOS][3];
-               GLfloat w = span->attrStart[FRAG_ATTRIB_WPOS][3];
+               GLfloat w = span->attrStart[FRAG_ATTRIB_WPOS][3] + span->leftClip * dwdx;
                for (i = 0; i < span->end; i++) {
                   const GLfloat invW = 1.0F / w;
                   texcoord[i][0] = s * invW;
@@ -587,7 +588,7 @@ interpolate_texcoords(GLcontext *ctx, SWspan *span)
                 ctx->ATIFragmentShader._Enabled) {
                /* do perspective correction but don't divide s, t, r by q */
                const GLfloat dwdx = span->attrStepX[FRAG_ATTRIB_WPOS][3];
-               GLfloat w = span->attrStart[FRAG_ATTRIB_WPOS][3];
+               GLfloat w = span->attrStart[FRAG_ATTRIB_WPOS][3] + span->leftClip * dwdx;
                for (i = 0; i < span->end; i++) {
                   const GLfloat invW = 1.0F / w;
                   texcoord[i][0] = s * invW;
@@ -660,8 +661,8 @@ interpolate_wpos(GLcontext *ctx, SWspan *span)
       }
    }
 
-   w = span->attrStart[FRAG_ATTRIB_WPOS][3];
    dw = span->attrStepX[FRAG_ATTRIB_WPOS][3];
+   w = span->attrStart[FRAG_ATTRIB_WPOS][3] + span->leftClip * dw;
    for (i = 0; i < span->end; i++) {
       wpos[i][2] = (GLfloat) span->array->z[i] * zScale;
       wpos[i][3] = w;
@@ -726,6 +727,8 @@ clip_span( GLcontext *ctx, SWspan *span )
    const GLint ymin = ctx->DrawBuffer->_Ymin;
    const GLint ymax = ctx->DrawBuffer->_Ymax;
 
+   span->leftClip = 0;
+
    if (span->arrayMask & SPAN_XY) {
       /* arrays of x/y pixel coords */
       const GLint *x = span->array->x;
@@ -753,7 +756,7 @@ clip_span( GLcontext *ctx, SWspan *span )
       /* horizontal span of pixels */
       const GLint x = span->x;
       const GLint y = span->y;
-      const GLint n = span->end;
+      GLint n = span->end;
 
       /* Trivial rejection tests */
       if (y < ymin || y >= ymax || x + n <= xmin || x >= xmax) {
@@ -761,18 +764,44 @@ clip_span( GLcontext *ctx, SWspan *span )
          return GL_FALSE;  /* all pixels clipped */
       }
 
+      /* Clip to right */
+      if (x + n > xmax) {
+         ASSERT(x < xmax);
+         n = span->end = xmax - x;
+      }
+
       /* Clip to the left */
       if (x < xmin) {
+         const GLint leftClip = xmin - x;
+         GLuint i;
+
+         ASSERT(leftClip > 0);
          ASSERT(x + n > xmin);
+
+         /* Clip 'leftClip' pixels from the left side.
+          * The span->leftClip field will be applied when we interpolate
+          * fragment attributes.
+          * For arrays of values, shift them left.
+          */
+         for (i = 0; i < FRAG_ATTRIB_MAX; i++) {
+            if (span->arrayAttribs & (1 << i)) {
+               /* shift array elements left by 'leftClip' */
+               _mesa_memcpy(span->array->attribs[i],
+                            span->array->attribs[i] + leftClip,
+                            (n - leftClip) * 4 * sizeof(GLfloat));
+            }
+         }
+
+         span->leftClip = leftClip;
+         span->x = xmin;
+         span->end -= leftClip;
          span->writeAll = GL_FALSE;
-         _mesa_bzero(span->array->mask, (xmin - x) * sizeof(GLubyte));
       }
 
-      /* Clip to right */
-      if (x + n > xmax) {
-         ASSERT(x < xmax);
-         span->end = xmax - x;
-      }
+      ASSERT(span->x >= xmin);
+      ASSERT(span->x + span->end <= xmax);
+      ASSERT(span->y >= ymin);
+      ASSERT(span->y < ymax);
 
       return GL_TRUE;  /* some pixels visible */
    }
@@ -818,6 +847,12 @@ _swrast_write_index_span( GLcontext *ctx, SWspan *span)
       }
    }
 
+   if (!(span->arrayMask & SPAN_MASK)) {
+      /* post-clip sanity check */
+      assert(span->x >= 0);
+      assert(span->y >= 0);
+   }
+
    /* Depth bounds test */
    if (ctx->Depth.BoundsTest && fb->Visual.depthBits > 0) {
       if (!_swrast_depth_bounds_test(ctx, span)) {
@@ -846,11 +881,11 @@ _swrast_write_index_span( GLcontext *ctx, SWspan *span)
    }
 
    /* Stencil and Z testing */
-   if (ctx->Depth.Test || ctx->Stencil.Enabled) {
+   if (ctx->Stencil._Enabled || ctx->Depth.Test) {
       if (!(span->arrayMask & SPAN_Z))
          _swrast_span_interpolate_z(ctx, span);
 
-      if (ctx->Stencil.Enabled) {
+      if (ctx->Stencil._Enabled) {
          if (!_swrast_stencil_and_ztest_span(ctx, span)) {
             span->arrayMask = origArrayMask;
             return;
@@ -1032,6 +1067,7 @@ add_specular(GLcontext *ctx, SWspan *span)
    ASSERT(!ctx->FragmentProgram._Current);
    ASSERT(span->arrayMask & SPAN_RGBA);
    ASSERT(swrast->_ActiveAttribMask & FRAG_BIT_COL1);
+   (void) swrast; /* silence warning */
 
    if (span->array->ChanType == GL_FLOAT) {
       if ((span->arrayAttribs & FRAG_BIT_COL0) == 0) {
@@ -1210,7 +1246,7 @@ shade_texture_span(GLcontext *ctx, SWspan *span)
          _swrast_exec_fragment_shader(ctx, span);
       }
    }
-   else if (ctx->Texture._EnabledUnits) {
+   else if (ctx->Texture._EnabledCoordUnits) {
       /* conventional texturing */
 
 #if CHAN_BITS == 32
@@ -1249,7 +1285,7 @@ _swrast_write_rgba_span( GLcontext *ctx, SWspan *span)
    void * const origRgba = span->array->rgba;
    const GLboolean shader = (ctx->FragmentProgram._Current
                              || ctx->ATIFragmentShader._Enabled);
-   const GLboolean shaderOrTexture = shader || ctx->Texture._EnabledUnits;
+   const GLboolean shaderOrTexture = shader || ctx->Texture._EnabledCoordUnits;
    struct gl_framebuffer *fb = ctx->DrawBuffer;
 
    /*
@@ -1283,6 +1319,7 @@ _swrast_write_rgba_span( GLcontext *ctx, SWspan *span)
 #ifdef DEBUG
    /* Make sure all fragments are within window bounds */
    if (span->arrayMask & SPAN_XY) {
+      /* array of pixel locations */
       GLuint i;
       for (i = 0; i < span->end; i++) {
          if (span->array->mask[i]) {
@@ -1310,18 +1347,28 @@ _swrast_write_rgba_span( GLcontext *ctx, SWspan *span)
    /* Do the alpha test */
    if (ctx->Color.AlphaEnabled) {
       if (!_swrast_alpha_test(ctx, span)) {
+         /* all fragments failed test */
          goto end;
       }
    }
 
    /* Stencil and Z testing */
-   if (ctx->Stencil.Enabled || ctx->Depth.Test) {
+   if (ctx->Stencil._Enabled || ctx->Depth.Test) {
       if (!(span->arrayMask & SPAN_Z))
          _swrast_span_interpolate_z(ctx, span);
 
-      if (ctx->Stencil.Enabled && fb->Visual.stencilBits > 0) {
+      if ((span->arrayMask & SPAN_XY) == 0) {
+         if (span->x < fb->_Xmin || span->x + span->end > fb->_Xmax ||
+             span->y < fb->_Ymin || span->y >= fb->_Ymax) {
+            printf("Bad span clipping at %d, %d\n", span->x, span->y);
+            return;
+         }
+      }
+
+      if (ctx->Stencil._Enabled) {
          /* Combined Z/stencil tests */
          if (!_swrast_stencil_and_ztest_span(ctx, span)) {
+            /* all fragments failed test */
             goto end;
          }
       }
@@ -1330,6 +1377,7 @@ _swrast_write_rgba_span( GLcontext *ctx, SWspan *span)
          ASSERT(ctx->Depth.Test);
          ASSERT(span->arrayMask & SPAN_Z);
          if (!_swrast_depth_test_span(ctx, span)) {
+            /* all fragments failed test */
             goto end;
          }
       }
@@ -1349,6 +1397,7 @@ _swrast_write_rgba_span( GLcontext *ctx, SWspan *span)
     * the occlusion test.
     */
    if (colorMask == 0x0) {
+      /* no colors to write */
       goto end;
    }
 
@@ -1372,12 +1421,14 @@ _swrast_write_rgba_span( GLcontext *ctx, SWspan *span)
 
    ASSERT(span->arrayMask & SPAN_RGBA);
 
-   if (!shader) {
-      /* Add base and specular colors */
-      if (ctx->Fog.ColorSumEnabled ||
-          (ctx->Light.Enabled &&
-           ctx->Light.Model.ColorControl == GL_SEPARATE_SPECULAR_COLOR)) {
-         add_specular(ctx, span);
+   if (span->primitive == GL_BITMAP || !swrast->SpecularVertexAdd) {
+      /* Add primary and specular (diffuse + specular) colors */
+      if (!shader) {
+         if (ctx->Fog.ColorSumEnabled ||
+             (ctx->Light.Enabled &&
+              ctx->Light.Model.ColorControl == GL_SEPARATE_SPECULAR_COLOR)) {
+            add_specular(ctx, span);
+         }
       }
    }
 
@@ -1398,11 +1449,17 @@ _swrast_write_rgba_span( GLcontext *ctx, SWspan *span)
    }
 
    /*
-    * Write to renderbuffers
+    * Write to renderbuffers.
+    * Depending on glDrawBuffer() state and the which color outputs are
+    * written by the fragment shader, we may either replicate one color to
+    * all renderbuffers or write a different color to each renderbuffer.
+    * multiFragOutputs=TRUE for the later case.
     */
    {
       const GLuint numBuffers = fb->_NumColorDrawBuffers;
-      const GLboolean multiFragOutputs = numBuffers > 1;
+      const struct gl_fragment_program *fp = ctx->FragmentProgram._Current;
+      const GLboolean multiFragOutputs = 
+         (fp && fp->Base.OutputsWritten >= (1 << FRAG_RESULT_DATA0));
       GLuint buf;
 
       for (buf = 0; buf < numBuffers; buf++) {