mesa: declare UniformBufferBindings as an array with a static size
authorMarek Olšák <maraeo@gmail.com>
Tue, 14 May 2013 15:58:32 +0000 (17:58 +0200)
committerMarek Olšák <maraeo@gmail.com>
Tue, 28 May 2013 11:05:30 +0000 (13:05 +0200)
Some Gallium drivers were crashing, because the array was not large enough.

v2: clamp the per-shader maximum in st/mesa, then sum them all up

NOTE: This is a candidate for the stable branches.

src/mesa/main/bufferobj.c
src/mesa/main/config.h
src/mesa/main/mtypes.h
src/mesa/state_tracker/st_extensions.c

index 1566cb4c509271fff9695bbb9831c801aee134cc..ffb67b9b3e5e4084b4c235d9364dde1feffb167e 100644 (file)
@@ -619,13 +619,10 @@ _mesa_init_buffer_objects( struct gl_context *ctx )
    _mesa_reference_buffer_object(ctx, &ctx->CopyWriteBuffer,
                                  ctx->Shared->NullBufferObj);
 
-   ctx->UniformBufferBindings = calloc(ctx->Const.MaxUniformBufferBindings,
-                                     sizeof(*ctx->UniformBufferBindings));
-
    _mesa_reference_buffer_object(ctx, &ctx->UniformBuffer,
                                 ctx->Shared->NullBufferObj);
 
-   for (i = 0; i < ctx->Const.MaxUniformBufferBindings; i++) {
+   for (i = 0; i < MAX_COMBINED_UNIFORM_BUFFERS; i++) {
       _mesa_reference_buffer_object(ctx,
                                    &ctx->UniformBufferBindings[i].BufferObject,
                                    ctx->Shared->NullBufferObj);
@@ -647,14 +644,11 @@ _mesa_free_buffer_objects( struct gl_context *ctx )
 
    _mesa_reference_buffer_object(ctx, &ctx->UniformBuffer, NULL);
 
-   for (i = 0; i < ctx->Const.MaxUniformBufferBindings; i++) {
+   for (i = 0; i < MAX_COMBINED_UNIFORM_BUFFERS; i++) {
       _mesa_reference_buffer_object(ctx,
                                    &ctx->UniformBufferBindings[i].BufferObject,
                                    NULL);
    }
-
-   free(ctx->UniformBufferBindings);
-   ctx->UniformBufferBindings = NULL;
 }
 
 static bool
index ea87b754a40dc3c3dc569d6c491fbe09b469625e..4a4fdc98c1db1c93044db69982f22e1c42d84e3a 100644 (file)
 /*@{*/
 #define MAX_PROGRAM_LOCAL_PARAMS       4096
 #define MAX_UNIFORMS                   4096
+#define MAX_UNIFORM_BUFFERS            15 /* + 1 default uniform buffer */
+/* 6 is for vertex, hull, domain, geometry, fragment, and compute shader. */
+#define MAX_COMBINED_UNIFORM_BUFFERS   (MAX_UNIFORM_BUFFERS * 6)
 /*@}*/
 
 /**
index 244bee2e670f3f48b4b5a6621428c826c3abbe76..40ffb9296179382b3ffc9c5f3761f819292b283a 100644 (file)
@@ -3544,7 +3544,8 @@ struct gl_context
     * associated with uniform blocks by glUniformBlockBinding()'s state in the
     * shader program.
     */
-   struct gl_uniform_buffer_binding *UniformBufferBindings;
+   struct gl_uniform_buffer_binding
+      UniformBufferBindings[MAX_COMBINED_UNIFORM_BUFFERS];
 
    /*@}*/
 
index 982e652f1b006563d1f47e9f02328f26394f2f30..46acc87637894a2b73c59b5de7ce7953a94df3e4 100644 (file)
@@ -197,6 +197,7 @@ void st_init_limits(struct st_context *st)
          screen->get_shader_param(screen, sh, PIPE_SHADER_CAP_MAX_CONST_BUFFERS);
       if (pc->MaxUniformBlocks)
          pc->MaxUniformBlocks -= 1; /* The first one is for ordinary uniforms. */
+      pc->MaxUniformBlocks = _min(pc->MaxUniformBlocks, MAX_UNIFORM_BUFFERS);
 
       pc->MaxCombinedUniformComponents = (pc->MaxUniformComponents +
                                           c->MaxUniformBlockSize / 4 *
@@ -279,15 +280,11 @@ void st_init_limits(struct st_context *st)
       st->ctx->Extensions.ARB_uniform_buffer_object = GL_TRUE;
       c->UniformBufferOffsetAlignment =
          screen->get_param(screen, PIPE_CAP_CONSTANT_BUFFER_OFFSET_ALIGNMENT);
-      /* FIXME: _mesa_init_buffer_objects() already has been, and
-       * ctx->UniformBufferBindings allocated, so unfortunately we can't just
-       * change MaxUniformBufferBindings a posteriori. */
-#if 0
       c->MaxCombinedUniformBlocks = c->MaxUniformBufferBindings =
          c->VertexProgram.MaxUniformBlocks +
          c->GeometryProgram.MaxUniformBlocks +
          c->FragmentProgram.MaxUniformBlocks;
-#endif
+      assert(c->MaxCombinedUniformBlocks <= MAX_COMBINED_UNIFORM_BUFFERS);
    }
 }