[Mesa-dev] [PATCH v4 1/3] mesa/glthread: track buffer creation/destruction

Gregory Hainaut gregory.hainaut at gmail.com
Thu Apr 13 17:32:03 UTC 2017


It would be used in next commit to allow asynchronous PBO transfer.

The tracking saves the buffer name into a hash. Saving pointer
will be more complex as the buffer is created in BindBuffer due to IsBuffer
insanity.

Perf wise DeleteBuffers is now synchronous for robustness.

Signed-off-by: Gregory Hainaut <gregory.hainaut at gmail.com>
---
 src/mapi/glapi/gen/ARB_direct_state_access.xml |   2 +-
 src/mapi/glapi/gen/gl_API.xml                  |   4 +-
 src/mesa/main/glthread.h                       |  10 +++
 src/mesa/main/marshal.c                        | 113 +++++++++++++++++++++++++
 src/mesa/main/marshal.h                        |  24 ++++++
 src/mesa/main/mtypes.h                         |   5 ++
 src/mesa/main/shared.c                         |   4 +
 7 files changed, 159 insertions(+), 3 deletions(-)

diff --git a/src/mapi/glapi/gen/ARB_direct_state_access.xml b/src/mapi/glapi/gen/ARB_direct_state_access.xml
index 43841bb..566f157 100644
--- a/src/mapi/glapi/gen/ARB_direct_state_access.xml
+++ b/src/mapi/glapi/gen/ARB_direct_state_access.xml
@@ -49,7 +49,7 @@
 
    <!-- Buffer object functions -->
 
-   <function name="CreateBuffers">
+   <function name="CreateBuffers" marshal="custom">
       <param name="n" type="GLsizei" />
       <param name="buffers" type="GLuint *" />
    </function>
diff --git a/src/mapi/glapi/gen/gl_API.xml b/src/mapi/glapi/gen/gl_API.xml
index c0ee2f2..ad841a1 100644
--- a/src/mapi/glapi/gen/gl_API.xml
+++ b/src/mapi/glapi/gen/gl_API.xml
@@ -5049,13 +5049,13 @@
         <glx ignore="true"/>
     </function>
 
-    <function name="DeleteBuffers" es1="1.1" es2="2.0">
+    <function name="DeleteBuffers" es1="1.1" es2="2.0" marshal="custom">
         <param name="n" type="GLsizei" counter="true"/>
         <param name="buffer" type="const GLuint *" count="n"/>
         <glx ignore="true"/>
     </function>
 
-    <function name="GenBuffers" es1="1.1" es2="2.0">
+    <function name="GenBuffers" es1="1.1" es2="2.0" marshal="custom">
         <param name="n" type="GLsizei" counter="true"/>
         <param name="buffer" type="GLuint *" output="true" count="n"/>
         <glx ignore="true"/>
diff --git a/src/mesa/main/glthread.h b/src/mesa/main/glthread.h
index 50c1db2..494e942 100644
--- a/src/mesa/main/glthread.h
+++ b/src/mesa/main/glthread.h
@@ -92,6 +92,16 @@ struct glthread_state
     * buffer) binding is in a VBO.
     */
    bool element_array_is_vbo;
+
+   /**
+    * Tracks on the main thread side the bound unpack pixel buffer
+    */
+   GLint pixel_unpack_buffer_bound;
+
+   /**
+    * Tracks on the main thread side the bound pack pixel buffer
+    */
+   GLint pixel_pack_buffer_bound;
 };
 
 /**
diff --git a/src/mesa/main/marshal.c b/src/mesa/main/marshal.c
index ae4efb5..4037b79 100644
--- a/src/mesa/main/marshal.c
+++ b/src/mesa/main/marshal.c
@@ -32,6 +32,7 @@
 #include "marshal.h"
 #include "dispatch.h"
 #include "marshal_generated.h"
+#include "hash.h"
 
 #ifdef HAVE_PTHREAD
 
@@ -196,6 +197,118 @@ _mesa_marshal_ShaderSource(GLuint shader, GLsizei count,
    free(length_tmp);
 }
 
+/**
+ * Used as a placeholder for track_buffers_creation/track_buffers_destruction
+ * so we know if buffer really exists in track_buffers_binding.
+ */
+static struct gl_buffer_object DummyBufferObject;
+
+static void track_buffers_creation(GLsizei n, GLuint * buffers)
+{
+   GET_CURRENT_CONTEXT(ctx);
+   GLsizei i;
+
+   if (n < 0 || !buffers)
+      return;
+
+   _mesa_HashLockMutex(ctx->Shared->ShadowBufferObjects);
+
+   for (i = 0; i < n ; i++) {
+      _mesa_HashInsertLocked(ctx->Shared->ShadowBufferObjects, buffers[i],
+            &DummyBufferObject);
+   }
+
+   _mesa_HashUnlockMutex(ctx->Shared->ShadowBufferObjects);
+}
+
+static void track_buffers_destruction(GLsizei n, const GLuint * buffers)
+{
+   GET_CURRENT_CONTEXT(ctx);
+   GLsizei i;
+   struct glthread_state *glthread = ctx->GLThread;
+
+   if (n < 0 || !buffers)
+      return;
+
+   _mesa_HashLockMutex(ctx->Shared->ShadowBufferObjects);
+
+   for (i = 0; i < n ; i++) {
+      if (buffers[i] == glthread->pixel_pack_buffer_bound)
+         glthread->pixel_pack_buffer_bound = 0;
+
+      if (buffers[i] == glthread->pixel_unpack_buffer_bound)
+         glthread->pixel_unpack_buffer_bound = 0;
+
+      /* Technically the buffer can still exist if it is bound to another
+       * context. It isn't important as next BindBuffer will consider
+       * the buffer invalid and following PBO transfer will be synchronous
+       * which is always safe.
+       */
+      _mesa_HashRemoveLocked(ctx->Shared->ShadowBufferObjects, buffers[i]);
+   }
+
+   _mesa_HashUnlockMutex(ctx->Shared->ShadowBufferObjects);
+}
+
+
+/* CreateBuffers: custom marshal to track buffers creation */
+void GLAPIENTRY
+_mesa_marshal_CreateBuffers(GLsizei n, GLuint * buffers)
+{
+   GET_CURRENT_CONTEXT(ctx);
+   _mesa_glthread_finish(ctx);
+   debug_print_sync("CreateBuffers");
+   CALL_CreateBuffers(ctx->CurrentServerDispatch, (n, buffers));
+
+   track_buffers_creation(n, buffers);
+}
+
+void
+_mesa_unmarshal_CreateBuffers(struct gl_context *ctx,
+                           const struct marshal_cmd_CreateBuffers *cmd)
+{
+   assert(0);
+}
+
+/* GenBuffers: custom marshal to track buffers creation */
+void GLAPIENTRY
+_mesa_marshal_GenBuffers(GLsizei n, GLuint * buffer)
+{
+   GET_CURRENT_CONTEXT(ctx);
+   _mesa_glthread_finish(ctx);
+   debug_print_sync("GenBuffers");
+   CALL_GenBuffers(ctx->CurrentServerDispatch, (n, buffer));
+
+   track_buffers_creation(n, buffer);
+}
+
+void
+_mesa_unmarshal_GenBuffers(struct gl_context *ctx,
+                           const struct marshal_cmd_GenBuffers *cmd)
+{
+   assert(0);
+}
+
+/* DeleteBuffers: custom marshal to track buffers destruction */
+void GLAPIENTRY
+_mesa_marshal_DeleteBuffers(GLsizei n, const GLuint * buffer)
+{
+   GET_CURRENT_CONTEXT(ctx);
+   _mesa_glthread_finish(ctx);
+   debug_print_sync("DeleteBuffers");
+
+   // It is done before CALL_DeleteBuffers to avoid any ABA multithread issue.
+   track_buffers_destruction(n, buffer);
+
+   CALL_DeleteBuffers(ctx->CurrentServerDispatch, (n, buffer));
+}
+
+void
+_mesa_unmarshal_DeleteBuffers(struct gl_context *ctx,
+                           const struct marshal_cmd_DeleteBuffers *cmd)
+{
+   assert(0);
+}
 
 /* BindBufferBase: marshalled asynchronously */
 struct marshal_cmd_BindBufferBase
diff --git a/src/mesa/main/marshal.h b/src/mesa/main/marshal.h
index 4842d27..150581b 100644
--- a/src/mesa/main/marshal.h
+++ b/src/mesa/main/marshal.h
@@ -206,6 +206,9 @@ struct marshal_cmd_BindBuffer;
 struct marshal_cmd_BufferData;
 struct marshal_cmd_BufferSubData;
 struct marshal_cmd_ClearBufferfv;
+struct marshal_cmd_CreateBuffers;
+struct marshal_cmd_GenBuffers;
+struct marshal_cmd_DeleteBuffers;
 
 void
 _mesa_unmarshal_Enable(struct gl_context *ctx,
@@ -260,4 +263,25 @@ void GLAPIENTRY
 _mesa_marshal_ClearBufferfv(GLenum buffer, GLint drawbuffer,
                             const GLfloat *value);
 
+void GLAPIENTRY
+_mesa_marshal_CreateBuffers(GLsizei n, GLuint * buffers);
+
+void
+_mesa_unmarshal_CreateBuffers(struct gl_context *ctx,
+                           const struct marshal_cmd_CreateBuffers *cmd);
+
+void GLAPIENTRY
+_mesa_marshal_GenBuffers(GLsizei n, GLuint * buffer);
+
+void
+_mesa_unmarshal_GenBuffers(struct gl_context *ctx,
+                           const struct marshal_cmd_GenBuffers *cmd);
+
+void GLAPIENTRY
+_mesa_marshal_DeleteBuffers(GLsizei n, const GLuint * buffer);
+
+void
+_mesa_unmarshal_DeleteBuffers(struct gl_context *ctx,
+                           const struct marshal_cmd_DeleteBuffers *cmd);
+
 #endif /* MARSHAL_H */
diff --git a/src/mesa/main/mtypes.h b/src/mesa/main/mtypes.h
index e5f7cba..de6e704 100644
--- a/src/mesa/main/mtypes.h
+++ b/src/mesa/main/mtypes.h
@@ -3203,6 +3203,11 @@ struct gl_shared_state
 
    struct _mesa_HashTable *BufferObjects;
 
+   /**< shadow of BufferObjects to track buffer in application thread when
+    * glthread is enabled
+    */
+   struct _mesa_HashTable *ShadowBufferObjects;
+
    /** Table of both gl_shader and gl_shader_program objects */
    struct _mesa_HashTable *ShaderObjects;
 
diff --git a/src/mesa/main/shared.c b/src/mesa/main/shared.c
index 5344812..b0a7b63 100644
--- a/src/mesa/main/shared.c
+++ b/src/mesa/main/shared.c
@@ -81,6 +81,8 @@ _mesa_alloc_shared_state(struct gl_context *ctx)
 
    shared->BufferObjects = _mesa_NewHashTable();
 
+   shared->ShadowBufferObjects = _mesa_NewHashTable();
+
    /* GL_ARB_sampler_objects */
    shared->SamplerObjects = _mesa_NewHashTable();
 
@@ -340,6 +342,8 @@ free_shared_state(struct gl_context *ctx, struct gl_shared_state *shared)
    _mesa_HashDeleteAll(shared->BufferObjects, delete_bufferobj_cb, ctx);
    _mesa_DeleteHashTable(shared->BufferObjects);
 
+   _mesa_DeleteHashTable(shared->ShadowBufferObjects);
+
    _mesa_HashDeleteAll(shared->FrameBuffers, delete_framebuffer_cb, ctx);
    _mesa_DeleteHashTable(shared->FrameBuffers);
    _mesa_HashDeleteAll(shared->RenderBuffers, delete_renderbuffer_cb, ctx);
-- 
2.1.4



More information about the mesa-dev mailing list