r300: Further reduced the radeon_span.c diff.
[mesa.git] / src / mesa / swrast / s_stencil.c
index f0d8bef39c2603330a66a284cee9236f908ad3c9..89991fad02d6a4d605d922efecc729fa121a5ddf 100644 (file)
@@ -25,9 +25,7 @@
 
 #include "glheader.h"
 #include "context.h"
-#include "macros.h"
 #include "imports.h"
-#include "fbobject.h"
 
 #include "s_context.h"
 #include "s_depth.h"
@@ -408,10 +406,10 @@ do_stencil_test( GLcontext *ctx, GLuint face, GLuint n, GLstencil stencil[],
  *
  */
 static GLboolean
-stencil_and_ztest_span(GLcontext *ctx, struct sw_span *span, GLuint face)
+stencil_and_ztest_span(GLcontext *ctx, SWspan *span, GLuint face)
 {
    struct gl_framebuffer *fb = ctx->DrawBuffer;
-   struct gl_renderbuffer *rb = fb->Attachment[BUFFER_STENCIL].Renderbuffer;
+   struct gl_renderbuffer *rb = fb->_StencilBuffer;
    GLstencil stencilRow[MAX_WIDTH];
    GLstencil *stencil;
    const GLuint n = span->end;
@@ -428,7 +426,7 @@ stencil_and_ztest_span(GLcontext *ctx, struct sw_span *span, GLuint face)
    }
 #endif
 
-   stencil = rb->GetPointer(ctx, rb, x, y);
+   stencil = (GLstencil *) rb->GetPointer(ctx, rb, x, y);
    if (!stencil) {
       rb->GetRow(ctx, rb, n, x, y, stencilRow);
       stencil = stencilRow;
@@ -441,6 +439,10 @@ stencil_and_ztest_span(GLcontext *ctx, struct sw_span *span, GLuint face)
    if (do_stencil_test( ctx, face, n, stencil, mask ) == GL_FALSE) {
       /* all fragments failed the stencil test, we're done. */
       span->writeAll = GL_FALSE;
+      if (!rb->GetPointer(ctx, rb, 0, 0)) {
+         /* put updated stencil values into buffer */
+         rb->PutRow(ctx, rb, n, x, y, stencil, NULL);
+      }
       return GL_FALSE;
    }
 
@@ -498,7 +500,7 @@ stencil_and_ztest_span(GLcontext *ctx, struct sw_span *span, GLuint face)
     * Write updated stencil values back into hardware stencil buffer.
     */
    if (!rb->GetPointer(ctx, rb, 0, 0)) {
-      rb->PutRow(ctx, rb, n, x, y, stencil, mask);
+      rb->PutRow(ctx, rb, n, x, y, stencil, NULL);
    }
    
    span->writeAll = GL_FALSE;
@@ -530,7 +532,7 @@ apply_stencil_op_to_pixels( GLcontext *ctx,
                             GLenum oper, GLuint face, const GLubyte mask[] )
 {
    struct gl_framebuffer *fb = ctx->DrawBuffer;
-   struct gl_renderbuffer *rb = fb->Attachment[BUFFER_STENCIL].Renderbuffer;
+   struct gl_renderbuffer *rb = fb->_StencilBuffer;
    const GLstencil stencilMax = (1 << fb->Visual.stencilBits) - 1;
    const GLstencil ref = ctx->Stencil.Ref[face];
    const GLstencil wrtmask = ctx->Stencil.WriteMask[face];
@@ -703,7 +705,7 @@ stencil_test_pixels( GLcontext *ctx, GLuint face, GLuint n,
                      const GLint x[], const GLint y[], GLubyte mask[] )
 {
    const struct gl_framebuffer *fb = ctx->DrawBuffer;
-   struct gl_renderbuffer *rb = fb->Attachment[BUFFER_STENCIL].Renderbuffer;
+   struct gl_renderbuffer *rb = fb->_StencilBuffer;
    GLubyte fail[MAX_WIDTH];
    GLstencil r, s;
    GLuint i;
@@ -898,10 +900,10 @@ stencil_test_pixels( GLcontext *ctx, GLuint face, GLuint n,
  *         GL_TRUE - one or more fragments passed the testing
  */
 static GLboolean
-stencil_and_ztest_pixels( GLcontext *ctx, struct sw_span *span, GLuint face )
+stencil_and_ztest_pixels( GLcontext *ctx, SWspan *span, GLuint face )
 {
    struct gl_framebuffer *fb = ctx->DrawBuffer;
-   struct gl_renderbuffer *rb = fb->Attachment[BUFFER_STENCIL].Renderbuffer;
+   struct gl_renderbuffer *rb = fb->_StencilBuffer;
    const GLuint n = span->end;
    const GLint *x = span->array->x;
    const GLint *y = span->array->y;
@@ -1005,10 +1007,8 @@ stencil_and_ztest_pixels( GLcontext *ctx, struct sw_span *span, GLuint face )
  * GL_FALSE = all fragments failed.
  */
 GLboolean
-_swrast_stencil_and_ztest_span(GLcontext *ctx, struct sw_span *span)
+_swrast_stencil_and_ztest_span(GLcontext *ctx, SWspan *span)
 {
-   /* span->facing can only be non-zero if using two-sided stencil */
-   ASSERT(ctx->Stencil._TestTwoSide || span->facing == 0);
    if (span->arrayMask & SPAN_XY)
       return stencil_and_ztest_pixels(ctx, span, span->facing);
    else
@@ -1060,7 +1060,8 @@ void
 _swrast_read_stencil_span(GLcontext *ctx, struct gl_renderbuffer *rb,
                           GLint n, GLint x, GLint y, GLstencil stencil[])
 {
-   if (y < 0 || y >= rb->Height || x + n <= 0 || x >= rb->Width) {
+   if (y < 0 || y >= (GLint) rb->Height ||
+       x + n <= 0 || x >= (GLint) rb->Width) {
       /* span is completely outside framebuffer */
       return; /* undefined values OK */
    }
@@ -1071,7 +1072,7 @@ _swrast_read_stencil_span(GLcontext *ctx, struct gl_renderbuffer *rb,
       n -= dx;
       stencil += dx;
    }
-   if (x + n > rb->Width) {
+   if (x + n > (GLint) rb->Width) {
       GLint dx = x + n - rb->Width;
       n -= dx;
    }
@@ -1085,7 +1086,8 @@ _swrast_read_stencil_span(GLcontext *ctx, struct gl_renderbuffer *rb,
 
 
 /**
- * Write a span of stencil values to the stencil buffer.
+ * Write a span of stencil values to the stencil buffer.  This function
+ * applies the stencil write mask when needed.
  * Used for glDraw/CopyPixels
  * Input:  n - how many pixels
  *         x, y - location of first pixel
@@ -1096,9 +1098,12 @@ _swrast_write_stencil_span(GLcontext *ctx, GLint n, GLint x, GLint y,
                            const GLstencil stencil[] )
 {
    struct gl_framebuffer *fb = ctx->DrawBuffer;
-   struct gl_renderbuffer *rb = fb->Attachment[BUFFER_STENCIL].Renderbuffer;
+   struct gl_renderbuffer *rb = fb->_StencilBuffer;
+   const GLuint stencilMax = (1 << fb->Visual.stencilBits) - 1;
+   const GLuint stencilMask = ctx->Stencil.WriteMask[0];
 
-   if (y < 0 || y >= rb->Height || x + n <= 0 || x >= rb->Width) {
+   if (y < 0 || y >= (GLint) rb->Height ||
+       x + n <= 0 || x >= (GLint) rb->Width) {
       /* span is completely outside framebuffer */
       return; /* undefined values OK */
    }
@@ -1108,7 +1113,7 @@ _swrast_write_stencil_span(GLcontext *ctx, GLint n, GLint x, GLint y,
       n -= dx;
       stencil += dx;
    }
-   if (x + n > rb->Width) {
+   if (x + n > (GLint) rb->Width) {
       GLint dx = x + n - rb->Width;
       n -= dx;
    }
@@ -1116,7 +1121,20 @@ _swrast_write_stencil_span(GLcontext *ctx, GLint n, GLint x, GLint y,
       return;
    }
 
-   rb->PutRow(ctx, rb, n, x, y, stencil, NULL);
+   if ((stencilMask & stencilMax) != stencilMax) {
+      /* need to apply writemask */
+      GLstencil destVals[MAX_WIDTH], newVals[MAX_WIDTH];
+      GLint i;
+      rb->GetRow(ctx, rb, n, x, y, destVals);
+      for (i = 0; i < n; i++) {
+         newVals[i]
+            = (stencil[i] & stencilMask) | (destVals[i] & ~stencilMask);
+      }
+      rb->PutRow(ctx, rb, n, x, y, newVals, NULL);
+   }
+   else {
+      rb->PutRow(ctx, rb, n, x, y, stencil, NULL);
+   }
 }
 
 
@@ -1127,7 +1145,7 @@ _swrast_write_stencil_span(GLcontext *ctx, GLint n, GLint x, GLint y,
 void
 _swrast_clear_stencil_buffer( GLcontext *ctx, struct gl_renderbuffer *rb )
 {
-   const GLubyte stencilBits = rb->ComponentSizes[0];
+   const GLubyte stencilBits = ctx->DrawBuffer->Visual.stencilBits;
    const GLuint mask = ctx->Stencil.WriteMask[0];
    const GLuint invMask = ~mask;
    const GLuint clearVal = (ctx->Stencil.Clear & mask);
@@ -1155,7 +1173,7 @@ _swrast_clear_stencil_buffer( GLcontext *ctx, struct gl_renderbuffer *rb )
          if (rb->DataType == GL_UNSIGNED_BYTE) {
             GLint i, j;
             for (i = 0; i < height; i++) {
-               GLubyte *stencil = rb->GetPointer(ctx, rb, x, y + i);
+               GLubyte *stencil = (GLubyte*) rb->GetPointer(ctx, rb, x, y + i);
                for (j = 0; j < width; j++) {
                   stencil[j] = (stencil[j] & invMask) | clearVal;
                }
@@ -1164,7 +1182,7 @@ _swrast_clear_stencil_buffer( GLcontext *ctx, struct gl_renderbuffer *rb )
          else {
             GLint i, j;
             for (i = 0; i < height; i++) {
-               GLushort *stencil = rb->GetPointer(ctx, rb, x, y + i);
+               GLushort *stencil = (GLushort*) rb->GetPointer(ctx, rb, x, y + i);
                for (j = 0; j < width; j++) {
                   stencil[j] = (stencil[j] & invMask) | clearVal;
                }
@@ -1173,11 +1191,10 @@ _swrast_clear_stencil_buffer( GLcontext *ctx, struct gl_renderbuffer *rb )
       }
       else {
          /* no bit masking */
-         if (width == rb->Width &&
-             rb->InternalFormat == GL_STENCIL_INDEX8_EXT) {
+         if (width == (GLint) rb->Width && rb->DataType == GL_UNSIGNED_BYTE) {
             /* optimized case */
-            /* XXX bottom-to-op raster assumed! */
-            GLubyte *stencil = rb->GetPointer(ctx, rb, x, y);
+            /* Note: bottom-to-top raster assumed! */
+            GLubyte *stencil = (GLubyte *) rb->GetPointer(ctx, rb, x, y);
             GLuint len = width * height * sizeof(GLubyte);
             _mesa_memset(stencil, clearVal, len);
          }
@@ -1190,7 +1207,7 @@ _swrast_clear_stencil_buffer( GLcontext *ctx, struct gl_renderbuffer *rb )
                   _mesa_memset(stencil, clearVal, width);
                }
                else {
-                  _mesa_memset16(stencil, clearVal, width);
+                  _mesa_memset16((short unsigned int*) stencil, clearVal, width);
                }
             }
          }