glDraw/CopyPixels of stencil data didn't obey the stencil write mask. Fixed.
authorBrian Paul <brian.paul@tungstengraphics.com>
Mon, 26 Sep 2005 23:06:01 +0000 (23:06 +0000)
committerBrian Paul <brian.paul@tungstengraphics.com>
Mon, 26 Sep 2005 23:06:01 +0000 (23:06 +0000)
src/mesa/swrast/s_stencil.c

index b465b5a2c684be8f512ad81ff60fd36bc2d71b1b..2c484d5393c5673a3abf2ab7eef30b81e1eb51d8 100644 (file)
@@ -1085,7 +1085,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
@@ -1097,6 +1098,8 @@ _swrast_write_stencil_span(GLcontext *ctx, GLint n, GLint x, GLint y,
 {
    struct gl_framebuffer *fb = ctx->DrawBuffer;
    struct gl_renderbuffer *rb = fb->Attachment[BUFFER_STENCIL].Renderbuffer;
+   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) {
       /* span is completely outside framebuffer */
@@ -1116,7 +1119,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);
+   }
 }