[Mesa-dev] [PATCH 4/5] mesa: Improve static error checking of arrays sized by MESA_SHADER_TYPES.

Paul Berry stereotype441 at gmail.com
Tue Dec 17 10:23:37 PST 2013


This patch replaces the following pattern:

    foo bar[MESA_SHADER_TYPES] = {
       ...
    };

With:

    foo bar[] = {
       ...
    };
    STATIC_ASSERT(Elements(bar) == MESA_SHADER_TYPES);

This way, when a new shader type is added in a future version of Mesa,
we will get a compile error to remind us that the array needs to be
updated.
---
 src/glsl/link_atomics.cpp  |  7 ++++---
 src/glsl/linker.cpp        | 14 ++++++++++----
 src/mesa/program/program.h |  3 ++-
 3 files changed, 16 insertions(+), 8 deletions(-)

diff --git a/src/glsl/link_atomics.cpp b/src/glsl/link_atomics.cpp
index cac5615..a53d4f4 100644
--- a/src/glsl/link_atomics.cpp
+++ b/src/glsl/link_atomics.cpp
@@ -214,17 +214,18 @@ void
 link_check_atomic_counter_resources(struct gl_context *ctx,
                                     struct gl_shader_program *prog)
 {
-   STATIC_ASSERT(MESA_SHADER_TYPES == 3);
-   const unsigned max_atomic_counters[MESA_SHADER_TYPES] = {
+   const unsigned max_atomic_counters[] = {
       ctx->Const.VertexProgram.MaxAtomicCounters,
       ctx->Const.GeometryProgram.MaxAtomicCounters,
       ctx->Const.FragmentProgram.MaxAtomicCounters
    };
-   const unsigned max_atomic_buffers[MESA_SHADER_TYPES] = {
+   STATIC_ASSERT(Elements(max_atomic_counters) == MESA_SHADER_TYPES);
+   const unsigned max_atomic_buffers[] = {
       ctx->Const.VertexProgram.MaxAtomicBuffers,
       ctx->Const.GeometryProgram.MaxAtomicBuffers,
       ctx->Const.FragmentProgram.MaxAtomicBuffers
    };
+   STATIC_ASSERT(Elements(max_atomic_buffers) == MESA_SHADER_TYPES);
    unsigned num_buffers;
    active_atomic_buffer *const abs =
       find_active_atomic_counters(ctx, prog, &num_buffers);
diff --git a/src/glsl/linker.cpp b/src/glsl/linker.cpp
index f2e91b2..bc04b41 100644
--- a/src/glsl/linker.cpp
+++ b/src/glsl/linker.cpp
@@ -1894,29 +1894,35 @@ store_fragdepth_layout(struct gl_shader_program *prog)
 static void
 check_resources(struct gl_context *ctx, struct gl_shader_program *prog)
 {
-   const unsigned max_samplers[MESA_SHADER_TYPES] = {
+   const unsigned max_samplers[] = {
       ctx->Const.VertexProgram.MaxTextureImageUnits,
       ctx->Const.GeometryProgram.MaxTextureImageUnits,
       ctx->Const.FragmentProgram.MaxTextureImageUnits
    };
+   STATIC_ASSERT(Elements(max_samplers) == MESA_SHADER_TYPES);
 
-   const unsigned max_default_uniform_components[MESA_SHADER_TYPES] = {
+   const unsigned max_default_uniform_components[] = {
       ctx->Const.VertexProgram.MaxUniformComponents,
       ctx->Const.GeometryProgram.MaxUniformComponents,
       ctx->Const.FragmentProgram.MaxUniformComponents
    };
+   STATIC_ASSERT(Elements(max_default_uniform_components) ==
+                 MESA_SHADER_TYPES);
 
-   const unsigned max_combined_uniform_components[MESA_SHADER_TYPES] = {
+   const unsigned max_combined_uniform_components[] = {
       ctx->Const.VertexProgram.MaxCombinedUniformComponents,
       ctx->Const.GeometryProgram.MaxCombinedUniformComponents,
       ctx->Const.FragmentProgram.MaxCombinedUniformComponents
    };
+   STATIC_ASSERT(Elements(max_combined_uniform_components) ==
+                 MESA_SHADER_TYPES);
 
-   const unsigned max_uniform_blocks[MESA_SHADER_TYPES] = {
+   const unsigned max_uniform_blocks[] = {
       ctx->Const.VertexProgram.MaxUniformBlocks,
       ctx->Const.GeometryProgram.MaxUniformBlocks,
       ctx->Const.FragmentProgram.MaxUniformBlocks
    };
+   STATIC_ASSERT(Elements(max_uniform_blocks) == MESA_SHADER_TYPES);
 
    for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
       struct gl_shader *sh = prog->_LinkedShaders[i];
diff --git a/src/mesa/program/program.h b/src/mesa/program/program.h
index 353ccab..135271c 100644
--- a/src/mesa/program/program.h
+++ b/src/mesa/program/program.h
@@ -210,11 +210,12 @@ _mesa_program_target_to_index(GLenum v)
 static inline GLenum
 _mesa_program_index_to_target(GLuint i)
 {
-   static const GLenum enums[MESA_SHADER_TYPES] = {
+   static const GLenum enums[] = {
       GL_VERTEX_PROGRAM_ARB,
       GL_GEOMETRY_PROGRAM_NV,
       GL_FRAGMENT_PROGRAM_ARB
    };
+   STATIC_ASSERT(Elements(enums) == MESA_SHADER_TYPES);
    if(i >= MESA_SHADER_TYPES)
       return 0;
    else
-- 
1.8.4.2



More information about the mesa-dev mailing list