[Mesa-dev] [PATCH 1/2] mesa/marshal: extract ClearBuffer helpers

Grigori Goronzy greg at chown.ath.cx
Sun Jul 9 16:21:10 UTC 2017


Extract clear buffer helper functions in preparation for adding
marshal/unmarshal functions for the various glClearBuffer variants.
---
 src/mesa/main/marshal.c | 74 +++++++++++++++++++++++++++++++------------------
 src/mesa/main/marshal.h |  5 ++--
 2 files changed, 50 insertions(+), 29 deletions(-)

diff --git a/src/mesa/main/marshal.c b/src/mesa/main/marshal.c
index 8db4531..1edc580 100644
--- a/src/mesa/main/marshal.c
+++ b/src/mesa/main/marshal.c
@@ -517,7 +517,7 @@ _mesa_marshal_NamedBufferSubData(GLuint buffer, GLintptr offset,
 }
 
 /* ClearBufferfv: marshalled asynchronously */
-struct marshal_cmd_ClearBufferfv
+struct marshal_cmd_ClearBuffer
 {
    struct marshal_cmd_base cmd_base;
    GLenum buffer;
@@ -526,7 +526,7 @@ struct marshal_cmd_ClearBufferfv
 
 void
 _mesa_unmarshal_ClearBufferfv(struct gl_context *ctx,
-                              const struct marshal_cmd_ClearBufferfv *cmd)
+                              const struct marshal_cmd_ClearBuffer *cmd)
 {
    const GLenum buffer = cmd->buffer;
    const GLint drawbuffer = cmd->drawbuffer;
@@ -537,6 +537,47 @@ _mesa_unmarshal_ClearBufferfv(struct gl_context *ctx,
                       (buffer, drawbuffer, value));
 }
 
+static inline size_t buffer_to_size(GLenum buffer)
+{
+   switch (buffer) {
+   case GL_COLOR:
+      return 4;
+   case GL_DEPTH_STENCIL:
+      return 2;
+   case GL_STENCIL:
+   case GL_DEPTH:
+      return 1;
+   default:
+      return 0;
+   }
+}
+
+static inline bool clear_buffer_add_command(struct gl_context *ctx, uint16_t id,
+                                            GLenum buffer, GLint drawbuffer,
+                                            const GLuint *value, size_t size)
+{
+   size_t cmd_size = sizeof(struct marshal_cmd_ClearBuffer) + size;
+   if (cmd_size <= MARSHAL_MAX_CMD_SIZE) {
+      struct marshal_cmd_ClearBuffer *cmd =
+         _mesa_glthread_allocate_command(ctx, id,
+                                         cmd_size);
+      cmd->buffer = buffer;
+      cmd->drawbuffer = drawbuffer;
+      GLuint *variable_data = (GLuint *) (cmd + 1);
+      if (size == 4)
+         COPY_4V(variable_data,  value);
+      else if (size == 2)
+         COPY_2V(variable_data, value);
+      else
+         *variable_data = *value;
+
+      _mesa_post_marshal_hook(ctx);
+      return true;
+   }
+
+   return false;
+}
+
 void GLAPIENTRY
 _mesa_marshal_ClearBufferfv(GLenum buffer, GLint drawbuffer,
                             const GLfloat *value)
@@ -544,15 +585,7 @@ _mesa_marshal_ClearBufferfv(GLenum buffer, GLint drawbuffer,
    GET_CURRENT_CONTEXT(ctx);
    debug_print_marshal("ClearBufferfv");
 
-   size_t size;
-   switch (buffer) {
-   case GL_DEPTH:
-      size = sizeof(GLfloat);
-      break;
-   case GL_COLOR:
-      size = sizeof(GLfloat) * 4;
-      break;
-   default:
+   if (!(buffer == GL_DEPTH || buffer == GL_COLOR)) {
       _mesa_glthread_finish(ctx);
 
       /* Page 498 of the PDF, section '17.4.3.1 Clearing Individual Buffers'
@@ -563,24 +596,11 @@ _mesa_marshal_ClearBufferfv(GLenum buffer, GLint drawbuffer,
        */
       _mesa_error(ctx, GL_INVALID_ENUM, "glClearBufferfv(buffer=%s)",
                   _mesa_enum_to_string(buffer));
-      return;
    }
 
-   size_t cmd_size = sizeof(struct marshal_cmd_ClearBufferfv) + size;
-   if (cmd_size <= MARSHAL_MAX_CMD_SIZE) {
-      struct marshal_cmd_ClearBufferfv *cmd =
-         _mesa_glthread_allocate_command(ctx, DISPATCH_CMD_ClearBufferfv,
-                                         cmd_size);
-      cmd->buffer = buffer;
-      cmd->drawbuffer = drawbuffer;
-      GLfloat *variable_data = (GLfloat *) (cmd + 1);
-      if (buffer == GL_COLOR)
-         COPY_4V(variable_data, value);
-      else
-         *variable_data = *value;
-
-      _mesa_post_marshal_hook(ctx);
-   } else {
+   size_t size = buffer_to_size(buffer);
+   if (!clear_buffer_add_command(ctx, DISPATCH_CMD_ClearBufferfv, buffer,
+                                 drawbuffer, (GLuint *)value, size)) {
       debug_print_sync("ClearBufferfv");
       _mesa_glthread_finish(ctx);
       CALL_ClearBufferfv(ctx->CurrentServerDispatch,
diff --git a/src/mesa/main/marshal.h b/src/mesa/main/marshal.h
index 999c75e..1567e7b 100644
--- a/src/mesa/main/marshal.h
+++ b/src/mesa/main/marshal.h
@@ -182,7 +182,8 @@ struct marshal_cmd_BufferData;
 struct marshal_cmd_BufferSubData;
 struct marshal_cmd_NamedBufferData;
 struct marshal_cmd_NamedBufferSubData;
-struct marshal_cmd_ClearBufferfv;
+struct marshal_cmd_ClearBuffer;
+#define marshal_cmd_ClearBufferfv marshal_cmd_ClearBuffer
 
 void
 _mesa_unmarshal_Enable(struct gl_context *ctx,
@@ -247,7 +248,7 @@ _mesa_marshal_NamedBufferSubData(GLuint buffer, GLintptr offset, GLsizeiptr size
 
 void
 _mesa_unmarshal_ClearBufferfv(struct gl_context *ctx,
-                              const struct marshal_cmd_ClearBufferfv *cmd);
+                              const struct marshal_cmd_ClearBuffer *cmd);
 
 void GLAPIENTRY
 _mesa_marshal_ClearBufferfv(GLenum buffer, GLint drawbuffer,
-- 
2.7.4



More information about the mesa-dev mailing list