[Mesa-dev] [PATCH] mesa: reallocate uniform bindings when the max value changes.

Dave Airlie airlied at gmail.com
Tue May 14 01:39:50 PDT 2013


From: Dave Airlie <airlied at redhat.com>

Some drivers change the default for Const.MaxUniformBufferBindings,
the current code allocates them before the point drivers would ever get
a chance to do such modifications.

Instead allocate them, and if the driver requires it can later reallocate
them to there larger side.

I'm not sure I like this patch, but it does fix softpipe and nouveau for me,
we may do something like this already somewhere and redo this patch to be
consistent.

Signed-off-by: Dave Airlie <airlied at redhat.com>
---
 src/mesa/main/bufferobj.c           | 67 +++++++++++++++++++++++++------------
 src/mesa/main/bufferobj.h           |  3 ++
 src/mesa/main/mtypes.h              |  1 +
 src/mesa/state_tracker/st_context.c |  2 ++
 4 files changed, 51 insertions(+), 22 deletions(-)

diff --git a/src/mesa/main/bufferobj.c b/src/mesa/main/bufferobj.c
index 1566cb4..0978a7a 100644
--- a/src/mesa/main/bufferobj.c
+++ b/src/mesa/main/bufferobj.c
@@ -597,7 +597,50 @@ _mesa_copy_buffer_subdata(struct gl_context *ctx,
       ctx->Driver.UnmapBuffer(ctx, dst);
 }
 
+/*
+ * Allocate memory for buffer object state
+ */
+static void
+_mesa_allocate_buffer_bindings( struct gl_context *ctx )
+{
+   GLuint i;
+
+   ctx->AllocatedUniformBufferBindings = ctx->Const.MaxUniformBufferBindings;
+   ctx->UniformBufferBindings = calloc(ctx->AllocatedUniformBufferBindings,
+				      sizeof(*ctx->UniformBufferBindings));
+
+   for (i = 0; i < ctx->AllocatedUniformBufferBindings; i++) {
+      _mesa_reference_buffer_object(ctx,
+				    &ctx->UniformBufferBindings[i].BufferObject,
+				    ctx->Shared->NullBufferObj);
+      ctx->UniformBufferBindings[i].Offset = -1;
+      ctx->UniformBufferBindings[i].Size = -1;
+   }
+}
+
+static void
+_mesa_free_buffer_bindings( struct gl_context *ctx )
+{
+   GLuint i;
+   for (i = 0; i < ctx->AllocatedUniformBufferBindings; i++) {
+      _mesa_reference_buffer_object(ctx,
+				    &ctx->UniformBufferBindings[i].BufferObject,
+				    NULL);
+   }
+
+   free(ctx->UniformBufferBindings);
+   ctx->UniformBufferBindings = NULL;
+   ctx->AllocatedUniformBufferBindings = 0;
+}
 
+void
+_mesa_reallocate_buffer_bindings( struct gl_context *ctx )
+{
+   if (ctx->AllocatedUniformBufferBindings < ctx->Const.MaxUniformBufferBindings) {
+      _mesa_free_buffer_bindings(ctx);
+      _mesa_allocate_buffer_bindings(ctx);
+   }
+}
 
 /**
  * Initialize the state associated with buffer objects
@@ -605,8 +648,6 @@ _mesa_copy_buffer_subdata(struct gl_context *ctx,
 void
 _mesa_init_buffer_objects( struct gl_context *ctx )
 {
-   GLuint i;
-
    memset(&DummyBufferObject, 0, sizeof(DummyBufferObject));
    _glthread_INIT_MUTEX(DummyBufferObject.Mutex);
    DummyBufferObject.RefCount = 1000*1000*1000; /* never delete */
@@ -619,27 +660,16 @@ _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++) {
-      _mesa_reference_buffer_object(ctx,
-				    &ctx->UniformBufferBindings[i].BufferObject,
-				    ctx->Shared->NullBufferObj);
-      ctx->UniformBufferBindings[i].Offset = -1;
-      ctx->UniformBufferBindings[i].Size = -1;
-   }
+   _mesa_allocate_buffer_bindings(ctx);
 }
 
 
 void
 _mesa_free_buffer_objects( struct gl_context *ctx )
 {
-   GLuint i;
-
    _mesa_reference_buffer_object(ctx, &ctx->Array.ArrayBufferObj, NULL);
 
    _mesa_reference_buffer_object(ctx, &ctx->CopyReadBuffer, NULL);
@@ -647,14 +677,7 @@ _mesa_free_buffer_objects( struct gl_context *ctx )
 
    _mesa_reference_buffer_object(ctx, &ctx->UniformBuffer, NULL);
 
-   for (i = 0; i < ctx->Const.MaxUniformBufferBindings; i++) {
-      _mesa_reference_buffer_object(ctx,
-				    &ctx->UniformBufferBindings[i].BufferObject,
-				    NULL);
-   }
-
-   free(ctx->UniformBufferBindings);
-   ctx->UniformBufferBindings = NULL;
+   _mesa_free_buffer_bindings(ctx);
 }
 
 static bool
diff --git a/src/mesa/main/bufferobj.h b/src/mesa/main/bufferobj.h
index 249178a..5fb6693 100644
--- a/src/mesa/main/bufferobj.h
+++ b/src/mesa/main/bufferobj.h
@@ -64,6 +64,9 @@ extern void
 _mesa_free_buffer_objects( struct gl_context *ctx );
 
 extern void
+_mesa_reallocate_buffer_bindings( struct gl_context *ctx );
+
+extern void
 _mesa_update_default_objects_buffer_objects(struct gl_context *ctx);
 
 
diff --git a/src/mesa/main/mtypes.h b/src/mesa/main/mtypes.h
index b68853b..91a0521 100644
--- a/src/mesa/main/mtypes.h
+++ b/src/mesa/main/mtypes.h
@@ -3527,6 +3527,7 @@ struct gl_context
     * associated with uniform blocks by glUniformBlockBinding()'s state in the
     * shader program.
     */
+   GLuint AllocatedUniformBufferBindings;
    struct gl_uniform_buffer_binding *UniformBufferBindings;
 
    /*@}*/
diff --git a/src/mesa/state_tracker/st_context.c b/src/mesa/state_tracker/st_context.c
index 7d18c25..8aa51f0 100644
--- a/src/mesa/state_tracker/st_context.c
+++ b/src/mesa/state_tracker/st_context.c
@@ -28,6 +28,7 @@
 #include "main/imports.h"
 #include "main/accum.h"
 #include "main/api_exec.h"
+#include "main/bufferobj.h"
 #include "main/context.h"
 #include "main/samplerobj.h"
 #include "main/shaderobj.h"
@@ -197,6 +198,7 @@ st_create_context_priv( struct gl_context *ctx, struct pipe_context *pipe,
    st_init_limits(st);
    st_init_extensions(st);
 
+   _mesa_reallocate_buffer_bindings(ctx);
    _mesa_compute_version(ctx);
 
    _mesa_initialize_dispatch_tables(ctx);
-- 
1.8.2.1



More information about the mesa-dev mailing list