glthread: use GLenum16 for enums, but clamp it to 0xffff to get correct errors
authorMarek Olšák <marek.olsak@amd.com>
Sun, 21 Aug 2022 19:49:53 +0000 (15:49 -0400)
committerMarge Bot <emma+marge@anholt.net>
Mon, 26 Sep 2022 22:58:16 +0000 (22:58 +0000)
0xffff is an invalid enum.

This was initially implemented without clamping and then reverted because
of the concern that we would randomly get correct enums and not report
errors if we just dropped the high bits.

This packs calls better in glthread_batch.

Reviewed-by: Pierre-Eric Pelloux-Prayer <pierre-eric.pelloux-prayer@amd.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/18828>

src/mapi/glapi/gen/gl_marshal.py
src/mesa/main/glthread.h
src/mesa/main/glthread_draw.c
src/mesa/main/glthread_marshal.h

index 74bd57b..43f70ef 100644 (file)
@@ -127,6 +127,8 @@ class PrintCode(gl_XML.gl_print_base):
             if p.count:
                 out('memcpy(cmd->{0}, {0}, {1});'.format(
                         p.name, p.size_string()))
+            elif p.type_string() == 'GLenum':
+                out('cmd->{0} = MIN2({0}, 0xffff); /* clamped to 0xffff (invalid enum) */'.format(p.name))
             else:
                 out('cmd->{0} = {0};'.format(p.name))
         if variable_params:
@@ -164,10 +166,10 @@ class PrintCode(gl_XML.gl_print_base):
             'GLboolean': 1,
             'GLbyte': 1,
             'GLubyte': 1,
+            'GLenum': 2, # uses GLenum16, clamped to 0xffff (invalid enum)
             'GLshort': 2,
             'GLushort': 2,
             'GLhalfNV': 2,
-            'GLenum': 4,
             'GLint': 4,
             'GLuint': 4,
             'GLbitfield': 4,
@@ -212,7 +214,10 @@ class PrintCode(gl_XML.gl_print_base):
                     out('{0} {1}[{2}];'.format(
                             p.get_base_type_string(), p.name, p.count))
                 else:
-                    out('{0} {1};'.format(p.type_string(), p.name))
+                    type = p.type_string()
+                    if type == 'GLenum':
+                        type = 'GLenum16'
+                    out('{0} {1};'.format(type, p.name))
 
             for p in variable_params:
                 if p.img_null_flag:
index 6ed17a3..efc579a 100644 (file)
@@ -52,6 +52,7 @@
 #include "GL/gl.h"
 #include "compiler/shader_enums.h"
 #include "main/config.h"
+#include "glheader.h"
 
 #ifdef __cplusplus
 extern "C" {
@@ -128,7 +129,7 @@ struct glthread_client_attrib {
 struct glthread_attrib_node {
    GLbitfield Mask;
    int ActiveTexture;
-   GLenum MatrixMode;
+   GLenum16 MatrixMode;
    bool CullFace;
    bool DepthTest;
 };
@@ -157,7 +158,7 @@ struct glthread_state
    bool inside_begin_end;
 
    /** Display lists. */
-   GLenum ListMode; /**< Zero if not inside display list, else list mode. */
+   GLenum16 ListMode; /**< Zero if not inside display list, else list mode. */
    unsigned ListBase;
    unsigned ListCallDepth;
 
@@ -226,7 +227,7 @@ struct glthread_state
 
    /** Basic matrix state tracking. */
    int ActiveTexture;
-   GLenum MatrixMode;
+   GLenum16 MatrixMode;
    gl_matrix_index MatrixIndex;
    struct glthread_attrib_node AttribStack[MAX_ATTRIB_STACK_DEPTH];
    int AttribStackDepth;
index 3af1d3d..2e709c4 100644 (file)
@@ -568,8 +568,8 @@ sync:
 struct marshal_cmd_DrawElementsInstancedARB
 {
    struct marshal_cmd_base cmd_base;
-   GLenum mode;
-   GLenum type;
+   GLenum16 mode;
+   GLenum16 type;
    GLsizei count;
    GLsizei instance_count;
    GLint basevertex;
@@ -606,8 +606,8 @@ _mesa_unmarshal_DrawElementsInstancedARB(struct gl_context *ctx,
 struct marshal_cmd_DrawRangeElementsBaseVertex
 {
    struct marshal_cmd_base cmd_base;
-   GLenum mode;
-   GLenum type;
+   GLenum16 mode;
+   GLenum16 type;
    GLsizei count;
    GLint basevertex;
    GLuint min_index;
@@ -645,9 +645,9 @@ draw_elements_async(struct gl_context *ctx, GLenum mode, GLsizei count,
       struct marshal_cmd_DrawRangeElementsBaseVertex *cmd =
          _mesa_glthread_allocate_command(ctx, DISPATCH_CMD_DrawRangeElementsBaseVertex, cmd_size);
 
-      cmd->mode = mode;
+      cmd->mode = MIN2(mode, 0xffff);
+      cmd->type = MIN2(type, 0xffff);
       cmd->count = count;
-      cmd->type = type;
       cmd->indices = indices;
       cmd->basevertex = basevertex;
       cmd->min_index = min_index;
@@ -657,9 +657,9 @@ draw_elements_async(struct gl_context *ctx, GLenum mode, GLsizei count,
       struct marshal_cmd_DrawElementsInstancedARB *cmd =
          _mesa_glthread_allocate_command(ctx, DISPATCH_CMD_DrawElementsInstancedARB, cmd_size);
 
-      cmd->mode = mode;
+      cmd->mode = MIN2(mode, 0xffff);
+      cmd->type = MIN2(type, 0xffff);
       cmd->count = count;
-      cmd->type = type;
       cmd->indices = indices;
       cmd->instance_count = instance_count;
       cmd->basevertex = basevertex;
@@ -671,8 +671,8 @@ struct marshal_cmd_DrawElementsInstancedBaseVertexBaseInstance
 {
    struct marshal_cmd_base cmd_base;
    bool index_bounds_valid;
-   GLenum mode;
-   GLenum type;
+   GLenum16 mode;
+   GLenum16 type;
    GLsizei count;
    GLsizei instance_count;
    GLint basevertex;
@@ -756,9 +756,9 @@ draw_elements_async_user(struct gl_context *ctx, GLenum mode, GLsizei count,
    struct marshal_cmd_DrawElementsInstancedBaseVertexBaseInstance *cmd;
 
    cmd = _mesa_glthread_allocate_command(ctx, DISPATCH_CMD_DrawElementsInstancedBaseVertexBaseInstance, cmd_size);
-   cmd->mode = mode;
+   cmd->mode = MIN2(mode, 0xffff);
+   cmd->type = MIN2(type, 0xffff);
    cmd->count = count;
-   cmd->type = type;
    cmd->indices = indices;
    cmd->instance_count = instance_count;
    cmd->basevertex = basevertex;
@@ -887,8 +887,8 @@ struct marshal_cmd_MultiDrawElementsBaseVertex
 {
    struct marshal_cmd_base cmd_base;
    bool has_base_vertex;
-   GLenum mode;
-   GLenum type;
+   GLenum16 mode;
+   GLenum16 type;
    GLsizei draw_count;
    GLuint user_buffer_mask;
    struct gl_buffer_object *index_buffer;
@@ -972,8 +972,8 @@ multi_draw_elements_async(struct gl_context *ctx, GLenum mode,
    }
 
    cmd = _mesa_glthread_allocate_command(ctx, DISPATCH_CMD_MultiDrawElementsBaseVertex, cmd_size);
-   cmd->mode = mode;
-   cmd->type = type;
+   cmd->mode = MIN2(mode, 0xffff);
+   cmd->type = MIN2(type, 0xffff);
    cmd->draw_count = draw_count;
    cmd->user_buffer_mask = user_buffer_mask;
    cmd->index_buffer = index_buffer;
index 9c56e57..f390cb9 100644 (file)
@@ -613,7 +613,7 @@ _mesa_glthread_MatrixMode(struct gl_context *ctx, GLenum mode)
       return;
 
    ctx->GLThread.MatrixIndex = _mesa_get_matrix_index(ctx, mode);
-   ctx->GLThread.MatrixMode = mode;
+   ctx->GLThread.MatrixMode = MIN2(mode, 0xffff);
 }
 
 static inline void
@@ -756,10 +756,10 @@ _mesa_glthread_CallLists(struct gl_context *ctx, GLsizei n, GLenum type,
 }
 
 static inline void
-_mesa_glthread_NewList(struct gl_context *ctx, GLuint list, GLuint mode)
+_mesa_glthread_NewList(struct gl_context *ctx, GLuint list, GLenum mode)
 {
    if (!ctx->GLThread.ListMode)
-      ctx->GLThread.ListMode = mode;
+      ctx->GLThread.ListMode = MIN2(mode, 0xffff);
 }
 
 static inline void