Merge branch 'mesa_7_5_branch' into mesa_7_6_branch
[mesa.git] / src / mesa / swrast / s_stencil.c
index e45f8eabb2b49cce5ff37f7ad6e9a57b3f928699..e9e9d3a4f10eccb88e63d19008d4356b42eb8acb 100644 (file)
@@ -1,8 +1,8 @@
 /*
  * Mesa 3-D graphics library
- * Version:  6.5
+ * Version:  7.1
  *
- * Copyright (C) 1999-2005  Brian Paul   All Rights Reserved.
+ * Copyright (C) 1999-2007  Brian Paul   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"),
  */
 
 
-#include "glheader.h"
-#include "context.h"
-#include "macros.h"
-#include "imports.h"
-#include "fbobject.h"
+#include "main/glheader.h"
+#include "main/context.h"
+#include "main/imports.h"
 
 #include "s_context.h"
 #include "s_depth.h"
 #include "s_stencil.h"
+#include "s_span.h"
 
 
 
@@ -68,6 +67,7 @@ apply_stencil_op( const GLcontext *ctx, GLenum oper, GLuint face,
    const GLstencil ref = ctx->Stencil.Ref[face];
    const GLstencil wrtmask = ctx->Stencil.WriteMask[face];
    const GLstencil invmask = (GLstencil) (~wrtmask);
+   const GLstencil stencilMax = (1 << ctx->DrawBuffer->Visual.stencilBits) - 1;
    GLuint i;
 
    switch (oper) {
@@ -112,7 +112,7 @@ apply_stencil_op( const GLcontext *ctx, GLenum oper, GLuint face,
            for (i=0;i<n;i++) {
               if (mask[i]) {
                  GLstencil s = stencil[i];
-                 if (s < STENCIL_MAX) {
+                 if (s < stencilMax) {
                     stencil[i] = (GLstencil) (s+1);
                  }
               }
@@ -123,7 +123,7 @@ apply_stencil_op( const GLcontext *ctx, GLenum oper, GLuint face,
               if (mask[i]) {
                  /* VERIFY logic of adding 1 to a write-masked value */
                  GLstencil s = stencil[i];
-                 if (s < STENCIL_MAX) {
+                 if (s < stencilMax) {
                     stencil[i] = (GLstencil) ((invmask & s) | (wrtmask & (s+1)));
                  }
               }
@@ -231,8 +231,9 @@ do_stencil_test( GLcontext *ctx, GLuint face, GLuint n, GLstencil stencil[],
    GLubyte fail[MAX_WIDTH];
    GLboolean allfail = GL_FALSE;
    GLuint i;
-   GLstencil r, s;
    const GLuint valueMask = ctx->Stencil.ValueMask[face];
+   const GLstencil r = (GLstencil) (ctx->Stencil.Ref[face] & valueMask);
+   GLstencil s;
 
    ASSERT(n <= MAX_WIDTH);
 
@@ -260,7 +261,6 @@ do_stencil_test( GLcontext *ctx, GLuint face, GLuint n, GLstencil stencil[],
         allfail = GL_TRUE;
         break;
       case GL_LESS:
-        r = (GLstencil) (ctx->Stencil.Ref[face] & valueMask);
         for (i=0;i<n;i++) {
            if (mask[i]) {
               s = (GLstencil) (stencil[i] & valueMask);
@@ -279,7 +279,6 @@ do_stencil_test( GLcontext *ctx, GLuint face, GLuint n, GLstencil stencil[],
         }
         break;
       case GL_LEQUAL:
-        r = (GLstencil) (ctx->Stencil.Ref[face] & valueMask);
         for (i=0;i<n;i++) {
            if (mask[i]) {
               s = (GLstencil) (stencil[i] & valueMask);
@@ -298,7 +297,6 @@ do_stencil_test( GLcontext *ctx, GLuint face, GLuint n, GLstencil stencil[],
         }
         break;
       case GL_GREATER:
-        r = (GLstencil) (ctx->Stencil.Ref[face] & valueMask);
         for (i=0;i<n;i++) {
            if (mask[i]) {
               s = (GLstencil) (stencil[i] & valueMask);
@@ -317,7 +315,6 @@ do_stencil_test( GLcontext *ctx, GLuint face, GLuint n, GLstencil stencil[],
         }
         break;
       case GL_GEQUAL:
-        r = (GLstencil) (ctx->Stencil.Ref[face] & valueMask);
         for (i=0;i<n;i++) {
            if (mask[i]) {
               s = (GLstencil) (stencil[i] & valueMask);
@@ -336,7 +333,6 @@ do_stencil_test( GLcontext *ctx, GLuint face, GLuint n, GLstencil stencil[],
         }
         break;
       case GL_EQUAL:
-        r = (GLstencil) (ctx->Stencil.Ref[face] & valueMask);
         for (i=0;i<n;i++) {
            if (mask[i]) {
               s = (GLstencil) (stencil[i] & valueMask);
@@ -355,7 +351,6 @@ do_stencil_test( GLcontext *ctx, GLuint face, GLuint n, GLstencil stencil[],
         }
         break;
       case GL_NOTEQUAL:
-        r = (GLstencil) (ctx->Stencil.Ref[face] & valueMask);
         for (i=0;i<n;i++) {
            if (mask[i]) {
               s = (GLstencil) (stencil[i] & valueMask);
@@ -392,6 +387,23 @@ do_stencil_test( GLcontext *ctx, GLuint face, GLuint n, GLstencil stencil[],
 }
 
 
+/**
+ * Compute the zpass/zfail masks by comparing the pre- and post-depth test
+ * masks.
+ */
+static INLINE void
+compute_pass_fail_masks(GLuint n, const GLubyte origMask[],
+                        const GLubyte newMask[],
+                        GLubyte passMask[], GLubyte failMask[])
+{
+   GLuint i;
+   for (i = 0; i < n; i++) {
+      ASSERT(newMask[i] == 0 || newMask[i] == 1);
+      passMask[i] = origMask[i] & newMask[i];
+      failMask[i] = origMask[i] & (newMask[i] ^ 1);
+   }
+}
+
 
 /**
  * Apply stencil and depth testing to the span of pixels.
@@ -406,10 +418,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;
@@ -426,7 +438,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;
@@ -439,6 +451,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;
    }
 
@@ -456,39 +472,24 @@ stencil_and_ztest_span(GLcontext *ctx, struct sw_span *span, GLuint face)
       /*
        * Perform depth buffering, then apply zpass or zfail stencil function.
        */
-      GLubyte passmask[MAX_WIDTH], failmask[MAX_WIDTH], oldmask[MAX_WIDTH];
-      GLuint i;
+      GLubyte passMask[MAX_WIDTH], failMask[MAX_WIDTH], origMask[MAX_WIDTH];
 
       /* save the current mask bits */
-      MEMCPY(oldmask, mask, n * sizeof(GLubyte));
+      _mesa_memcpy(origMask, mask, n * sizeof(GLubyte));
 
       /* apply the depth test */
       _swrast_depth_test_span(ctx, span);
 
-      /* Set the stencil pass/fail flags according to result of depth testing.
-       * if oldmask[i] == 0 then
-       *    Don't touch the stencil value
-       * else if oldmask[i] and newmask[i] then
-       *    Depth test passed
-       * else
-       *    assert(oldmask[i] && !newmask[i])
-       *    Depth test failed
-       * endif
-       */
-      for (i=0;i<n;i++) {
-         ASSERT(mask[i] == 0 || mask[i] == 1);
-         passmask[i] = oldmask[i] & mask[i];
-         failmask[i] = oldmask[i] & (mask[i] ^ 1);
-      }
+      compute_pass_fail_masks(n, origMask, mask, passMask, failMask);
 
       /* apply the pass and fail operations */
       if (ctx->Stencil.ZFailFunc[face] != GL_KEEP) {
          apply_stencil_op( ctx, ctx->Stencil.ZFailFunc[face], face,
-                           n, stencil, failmask );
+                           n, stencil, failMask );
       }
       if (ctx->Stencil.ZPassFunc[face] != GL_KEEP) {
          apply_stencil_op( ctx, ctx->Stencil.ZPassFunc[face], face,
-                           n, stencil, passmask );
+                           n, stencil, passMask );
       }
    }
 
@@ -496,7 +497,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;
@@ -528,7 +529,8 @@ 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];
    const GLstencil invmask = (GLstencil) (~wrtmask);
@@ -584,7 +586,7 @@ apply_stencil_op_to_pixels( GLcontext *ctx,
            for (i=0;i<n;i++) {
               if (mask[i]) {
                   GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] );
-                 if (*sptr < STENCIL_MAX) {
+                 if (*sptr < stencilMax) {
                     *sptr = (GLstencil) (*sptr + 1);
                  }
               }
@@ -594,7 +596,7 @@ apply_stencil_op_to_pixels( GLcontext *ctx,
            for (i=0;i<n;i++) {
               if (mask[i]) {
                   GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] );
-                 if (*sptr < STENCIL_MAX) {
+                 if (*sptr < stencilMax) {
                     *sptr = (GLstencil) ((invmask & *sptr) | (wrtmask & (*sptr+1)));
                  }
               }
@@ -700,7 +702,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;
@@ -895,10 +897,11 @@ 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 )
 {
+   GLubyte passMask[MAX_WIDTH], failMask[MAX_WIDTH], origMask[MAX_WIDTH];
    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;
@@ -911,11 +914,11 @@ stencil_and_ztest_pixels( GLcontext *ctx, struct sw_span *span, GLuint face )
    if (!rb->GetPointer(ctx, rb, 0, 0)) {
       /* No direct access */
       GLstencil stencil[MAX_WIDTH];
-      GLubyte origMask[MAX_WIDTH];
 
-      rb->GetValues(ctx, rb, n, x, y, stencil);
+      ASSERT(rb->DataType == GL_UNSIGNED_BYTE);
+      _swrast_get_values(ctx, rb, n, x, y, stencil, sizeof(GLubyte));
 
-      MEMCPY(origMask, mask, n * sizeof(GLubyte));
+      _mesa_memcpy(origMask, mask, n * sizeof(GLubyte));          
 
       (void) do_stencil_test(ctx, face, n, stencil, mask);
 
@@ -924,27 +927,20 @@ stencil_and_ztest_pixels( GLcontext *ctx, struct sw_span *span, GLuint face )
                           n, stencil, mask);
       }
       else {
+         GLubyte tmpMask[MAX_WIDTH]; 
+         _mesa_memcpy(tmpMask, mask, n * sizeof(GLubyte));
+
          _swrast_depth_test_span(ctx, span);
 
+         compute_pass_fail_masks(n, tmpMask, mask, passMask, failMask);
+
          if (ctx->Stencil.ZFailFunc[face] != GL_KEEP) {
-            GLubyte failmask[MAX_WIDTH];
-            GLuint i;
-            for (i = 0; i < n; i++) {
-               ASSERT(mask[i] == 0 || mask[i] == 1);
-               failmask[i] = origMask[i] & (mask[i] ^ 1);
-            }
             apply_stencil_op(ctx, ctx->Stencil.ZFailFunc[face], face,
-                             n, stencil, failmask);
+                             n, stencil, failMask);
          }
          if (ctx->Stencil.ZPassFunc[face] != GL_KEEP) {
-            GLubyte passmask[MAX_WIDTH];
-            GLuint i;
-            for (i = 0; i < n; i++) {
-               ASSERT(mask[i] == 0 || mask[i] == 1);
-               passmask[i] = origMask[i] & mask[i];
-            }
             apply_stencil_op(ctx, ctx->Stencil.ZPassFunc[face], face,
-                             n, stencil, passmask);
+                             n, stencil, passMask);
          }
       }
 
@@ -966,28 +962,21 @@ stencil_and_ztest_pixels( GLcontext *ctx, struct sw_span *span, GLuint face )
                                     ctx->Stencil.ZPassFunc[face], face, mask);
       }
       else {
-         GLubyte passmask[MAX_WIDTH], failmask[MAX_WIDTH], oldmask[MAX_WIDTH];
-         GLuint i;
-
-         MEMCPY(oldmask, mask, n * sizeof(GLubyte));
+         _mesa_memcpy(origMask, mask, n * sizeof(GLubyte));
 
          _swrast_depth_test_span(ctx, span);
 
-         for (i=0;i<n;i++) {
-            ASSERT(mask[i] == 0 || mask[i] == 1);
-            passmask[i] = oldmask[i] & mask[i];
-            failmask[i] = oldmask[i] & (mask[i] ^ 1);
-         }
+         compute_pass_fail_masks(n, origMask, mask, passMask, failMask);
 
          if (ctx->Stencil.ZFailFunc[face] != GL_KEEP) {
             apply_stencil_op_to_pixels(ctx, n, x, y,
                                        ctx->Stencil.ZFailFunc[face],
-                                       face, failmask);
+                                       face, failMask);
          }
          if (ctx->Stencil.ZPassFunc[face] != GL_KEEP) {
             apply_stencil_op_to_pixels(ctx, n, x, y,
                                        ctx->Stencil.ZPassFunc[face],
-                                       face, passmask);
+                                       face, passMask);
          }
       }
 
@@ -1001,14 +990,14 @@ 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);
+   const GLuint face = (span->facing == 0) ? 0 : ctx->Stencil._BackFace;
+
    if (span->arrayMask & SPAN_XY)
-      return stencil_and_ztest_pixels(ctx, span, span->facing);
+      return stencil_and_ztest_pixels(ctx, span, face);
    else
-      return stencil_and_ztest_span(ctx, span, span->facing);
+      return stencil_and_ztest_span(ctx, span, face);
 }
 
 
@@ -1056,7 +1045,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 */
    }
@@ -1067,7 +1057,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;
    }
@@ -1081,7 +1071,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
@@ -1092,9 +1083,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 */
    }
@@ -1104,7 +1098,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;
    }
@@ -1112,7 +1106,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);
+   }
 }
 
 
@@ -1123,7 +1130,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);
@@ -1151,7 +1158,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;
                }
@@ -1160,7 +1167,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;
                }
@@ -1169,11 +1176,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);
          }
@@ -1186,7 +1192,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);
                }
             }
          }