swrast: fix delayed texel buffer allocation regression for OpenMP
authorAndreas Fänger <a.faenger@e-sign.com>
Tue, 7 Jan 2014 10:10:00 +0000 (03:10 -0700)
committerBrian Paul <brianp@vmware.com>
Tue, 7 Jan 2014 15:03:49 +0000 (08:03 -0700)
Commit 9119269ca14ed42b51c7d8e2e662500311b29fa3 moved the texel
buffer allocation to _swrast_texture_span(), however, when compiled
with OpenMP support this code already runs multi-threaded so a
critical section is required to prevent multiple allocations and
rendering errors.

Cc: "10.0" <mesa-stable@lists.freedesktop.org>
Reviewed-by: Brian Paul <brianp@vmware.com>
src/mesa/swrast/s_texcombine.c

index 7e07f4f8a34f31c898db00208777b14f97d0a544..297491ed5b27a605bc5690bfe26d58d90046381f 100644 (file)
@@ -602,6 +602,14 @@ _swrast_texture_span( struct gl_context *ctx, SWspan *span )
    if (!swrast->TexelBuffer) {
 #ifdef _OPENMP
       const GLint maxThreads = omp_get_max_threads();
+
+      /* TexelBuffer memory allocation needs to be done in a critical section
+       * as this code runs in a parallel loop.
+       * When entering the section, first check if TexelBuffer has been
+       * initialized already by another thread while this thread was waiting.
+       */
+      #pragma omp critical
+      if (!swrast->TexelBuffer) {
 #else
       const GLint maxThreads = 1;
 #endif
@@ -613,6 +621,10 @@ _swrast_texture_span( struct gl_context *ctx, SWspan *span )
       swrast->TexelBuffer =
         malloc(ctx->Const.FragmentProgram.MaxTextureImageUnits * maxThreads *
                            SWRAST_MAX_WIDTH * 4 * sizeof(GLfloat));
+#ifdef _OPENMP
+      } /* critical section */
+#endif
+
       if (!swrast->TexelBuffer) {
         _mesa_error(ctx, GL_OUT_OF_MEMORY, "texture_combine");
         return;