[Mesa-dev] [PATCH 05/23] swrast: remove MAX_WIDTH arrays in stencil code

Brian Paul brian.e.paul at gmail.com
Mon Feb 20 12:14:53 PST 2012


From: Brian Paul <brianp at vmware.com>

Use some per-context temporary arrays instead.
---
 src/mesa/swrast/s_context.c |   18 ++++++++++++++++++
 src/mesa/swrast/s_context.h |    7 +++++++
 src/mesa/swrast/s_stencil.c |   15 +++++++++++----
 3 files changed, 36 insertions(+), 4 deletions(-)

diff --git a/src/mesa/swrast/s_context.c b/src/mesa/swrast/s_context.c
index 2ebb649..999e426 100644
--- a/src/mesa/swrast/s_context.c
+++ b/src/mesa/swrast/s_context.c
@@ -798,6 +798,19 @@ _swrast_CreateContext( struct gl_context *ctx )
 
    ctx->swrast_context = swrast;
 
+   swrast->stencil_temp.buf1 = (GLubyte *) malloc(MAX_WIDTH * sizeof(GLubyte));
+   swrast->stencil_temp.buf2 = (GLubyte *) malloc(MAX_WIDTH * sizeof(GLubyte));
+   swrast->stencil_temp.buf3 = (GLubyte *) malloc(MAX_WIDTH * sizeof(GLubyte));
+   swrast->stencil_temp.buf4 = (GLubyte *) malloc(MAX_WIDTH * sizeof(GLubyte));
+
+   if (!swrast->stencil_temp.buf1 ||
+       !swrast->stencil_temp.buf2 ||
+       !swrast->stencil_temp.buf3 ||
+       !swrast->stencil_temp.buf4) {
+      _swrast_DestroyContext(ctx);
+      return GL_FALSE;
+   }
+
    return GL_TRUE;
 }
 
@@ -816,6 +829,11 @@ _swrast_DestroyContext( struct gl_context *ctx )
    FREE( swrast->TexelBuffer );
    FREE( swrast );
 
+   free(swrast->stencil_temp.buf1);
+   free(swrast->stencil_temp.buf2);
+   free(swrast->stencil_temp.buf3);
+   free(swrast->stencil_temp.buf4);
+
    ctx->swrast_context = 0;
 }
 
diff --git a/src/mesa/swrast/s_context.h b/src/mesa/swrast/s_context.h
index 9388c35..26b97f7 100644
--- a/src/mesa/swrast/s_context.h
+++ b/src/mesa/swrast/s_context.h
@@ -306,6 +306,13 @@ typedef struct
    /** State used during execution of fragment programs */
    struct gl_program_machine FragProgMachine;
 
+   /** Temporary arrays for stencil operations.  To avoid large stack
+    * allocations.
+    */
+   struct {
+      GLubyte *buf1, *buf2, *buf3, *buf4;
+   } stencil_temp;
+
 } SWcontext;
 
 
diff --git a/src/mesa/swrast/s_stencil.c b/src/mesa/swrast/s_stencil.c
index bbfbf44..3423737 100644
--- a/src/mesa/swrast/s_stencil.c
+++ b/src/mesa/swrast/s_stencil.c
@@ -210,7 +210,8 @@ static GLboolean
 do_stencil_test(struct gl_context *ctx, GLuint face, GLuint n,
                 GLubyte stencil[], GLubyte mask[], GLint stride)
 {
-   GLubyte fail[MAX_WIDTH];
+   SWcontext *swrast = SWRAST_CONTEXT(ctx);
+   GLubyte *fail = swrast->stencil_temp.buf2;
    GLboolean allfail = GL_FALSE;
    GLuint i, j;
    const GLuint valueMask = ctx->Stencil.ValueMask[face];
@@ -347,6 +348,7 @@ put_s8_values(struct gl_context *ctx, struct gl_renderbuffer *rb,
 GLboolean
 _swrast_stencil_and_ztest_span(struct gl_context *ctx, SWspan *span)
 {
+   SWcontext *swrast = SWRAST_CONTEXT(ctx);
    struct gl_framebuffer *fb = ctx->DrawBuffer;
    struct gl_renderbuffer *rb = fb->Attachment[BUFFER_STENCIL].Renderbuffer;
    const GLint stencilOffset = get_stencil_offset(rb->Format);
@@ -354,7 +356,7 @@ _swrast_stencil_and_ztest_span(struct gl_context *ctx, SWspan *span)
    const GLuint face = (span->facing == 0) ? 0 : ctx->Stencil._BackFace;
    const GLuint count = span->end;
    GLubyte *mask = span->array->mask;
-   GLubyte stencilTemp[MAX_WIDTH];
+   GLubyte *stencilTemp = swrast->stencil_temp.buf1;
    GLubyte *stencilBuf;
 
    if (span->arrayMask & SPAN_XY) {
@@ -402,7 +404,10 @@ _swrast_stencil_and_ztest_span(struct gl_context *ctx, SWspan *span)
       /*
        * Perform depth buffering, then apply zpass or zfail stencil function.
        */
-      GLubyte passMask[MAX_WIDTH], failMask[MAX_WIDTH], origMask[MAX_WIDTH];
+      SWcontext *swrast = SWRAST_CONTEXT(ctx);
+      GLubyte *passMask = swrast->stencil_temp.buf2;
+      GLubyte *failMask = swrast->stencil_temp.buf3;
+      GLubyte *origMask = swrast->stencil_temp.buf4;
 
       /* save the current mask bits */
       memcpy(origMask, mask, count * sizeof(GLubyte));
@@ -488,6 +493,7 @@ void
 _swrast_write_stencil_span(struct gl_context *ctx, GLint n, GLint x, GLint y,
                            const GLubyte stencil[] )
 {
+   SWcontext *swrast = SWRAST_CONTEXT(ctx);
    struct gl_framebuffer *fb = ctx->DrawBuffer;
    struct gl_renderbuffer *rb = fb->Attachment[BUFFER_STENCIL].Renderbuffer;
    const GLuint stencilMax = (1 << fb->Visual.stencilBits) - 1;
@@ -517,7 +523,8 @@ _swrast_write_stencil_span(struct gl_context *ctx, GLint n, GLint x, GLint y,
 
    if ((stencilMask & stencilMax) != stencilMax) {
       /* need to apply writemask */
-      GLubyte destVals[MAX_WIDTH], newVals[MAX_WIDTH];
+      GLubyte *destVals = swrast->stencil_temp.buf1;
+      GLubyte *newVals = swrast->stencil_temp.buf2;
       GLint i;
 
       _mesa_unpack_ubyte_stencil_row(rb->Format, n, stencilBuf, destVals);
-- 
1.7.3.4



More information about the mesa-dev mailing list