Gawain: Make builtin uniform lookup to be O(1)
authorSergey Sharybin <sergey.vfx@gmail.com>
Thu, 5 Oct 2017 11:19:14 +0000 (16:19 +0500)
committerSergey Sharybin <sergey.vfx@gmail.com>
Thu, 5 Oct 2017 11:19:14 +0000 (16:19 +0500)
intern/gawain/gawain/gwn_shader_interface.h
intern/gawain/src/gwn_shader_interface.c

index a1d4d82e0808d701cc53c8b1ec238cd9e5baf547..4c3d44cadbd5d297a45d0750ca22406d50eaa634 100644 (file)
@@ -14,7 +14,7 @@
 #include "gwn_common.h"
 
 typedef enum {
 #include "gwn_common.h"
 
 typedef enum {
-       GWN_UNIFORM_NONE, // uninitialized/unknown
+       GWN_UNIFORM_NONE = 0, // uninitialized/unknown
 
        GWN_UNIFORM_MODELVIEW,  // mat4 ModelViewMatrix
        GWN_UNIFORM_PROJECTION, // mat4 ProjectionMatrix
 
        GWN_UNIFORM_MODELVIEW,  // mat4 ModelViewMatrix
        GWN_UNIFORM_PROJECTION, // mat4 ProjectionMatrix
@@ -27,7 +27,9 @@ typedef enum {
 
        GWN_UNIFORM_COLOR, // vec4 color
 
 
        GWN_UNIFORM_COLOR, // vec4 color
 
-       GWN_UNIFORM_CUSTOM // custom uniform, not one of the above built-ins
+       GWN_UNIFORM_CUSTOM, // custom uniform, not one of the above built-ins
+
+       GWN_NUM_UNIFORMS, // Special value, denotes number of builtin uniforms.
 } Gwn_UniformBuiltin;
 
 typedef struct Gwn_ShaderInput {
 } Gwn_UniformBuiltin;
 
 typedef struct Gwn_ShaderInput {
@@ -51,6 +53,7 @@ typedef struct Gwn_ShaderInterface {
        uint16_t attrib_ct;
        Gwn_ShaderInput_Entry* uniform_buckets[GWN_NUM_SHADERINTERFACE_BUCKETS];
        Gwn_ShaderInput_Entry* attrib_buckets[GWN_NUM_SHADERINTERFACE_BUCKETS];
        uint16_t attrib_ct;
        Gwn_ShaderInput_Entry* uniform_buckets[GWN_NUM_SHADERINTERFACE_BUCKETS];
        Gwn_ShaderInput_Entry* attrib_buckets[GWN_NUM_SHADERINTERFACE_BUCKETS];
+       Gwn_ShaderInput* builtin_uniforms[GWN_NUM_UNIFORMS];
        Gwn_ShaderInput inputs[0]; // dynamic size, uniforms followed by attribs
 } Gwn_ShaderInterface;
 
        Gwn_ShaderInput inputs[0]; // dynamic size, uniforms followed by attribs
 } Gwn_ShaderInterface;
 
index 076d0b71e15b571e550b32a8e3615d6a31a1b27b..5305dae9a9a5ed44258656791241dcfa69132a67 100644 (file)
@@ -38,7 +38,8 @@ static const char* BuiltinUniform_name(Gwn_UniformBuiltin u)
 
                [GWN_UNIFORM_COLOR] = "color",
 
 
                [GWN_UNIFORM_COLOR] = "color",
 
-               [GWN_UNIFORM_CUSTOM] = NULL
+               [GWN_UNIFORM_CUSTOM] = NULL,
+               [GWN_NUM_UNIFORMS] = NULL,
                };
 
        return names[u];
                };
 
        return names[u];
@@ -309,10 +310,16 @@ Gwn_ShaderInterface* GWN_shaderinterface_create(GLint program)
                        }
                }
 
                        }
                }
 
+       memset(shaderface->builtin_uniforms, 0, sizeof(shaderface->builtin_uniforms));
        for (uint32_t i = 0; i < shaderface->uniform_ct; ++i)
                {
                        Gwn_ShaderInput* input = &shaderface->inputs[i];
                        shader_input_to_bucket(input, shaderface->uniform_buckets);
        for (uint32_t i = 0; i < shaderface->uniform_ct; ++i)
                {
                        Gwn_ShaderInput* input = &shaderface->inputs[i];
                        shader_input_to_bucket(input, shaderface->uniform_buckets);
+                       if (input->builtin_type != GWN_UNIFORM_NONE &&
+                           input->builtin_type != GWN_UNIFORM_CUSTOM)
+                               {
+                               shaderface->builtin_uniforms[input->builtin_type] = input;
+                               }
                }
        for (uint32_t i = 0; i < shaderface->attrib_ct; ++i)
                {
                }
        for (uint32_t i = 0; i < shaderface->attrib_ct; ++i)
                {
@@ -343,17 +350,10 @@ const Gwn_ShaderInput* GWN_shaderinterface_uniform_builtin(const Gwn_ShaderInter
 #if TRUST_NO_ONE
        assert(builtin != GWN_UNIFORM_NONE);
        assert(builtin != GWN_UNIFORM_CUSTOM);
 #if TRUST_NO_ONE
        assert(builtin != GWN_UNIFORM_NONE);
        assert(builtin != GWN_UNIFORM_CUSTOM);
+       assert(builtin != GWN_NUM_UNIFORMS);
 #endif
 
 #endif
 
-       // look up by enum, not name
-       for (uint32_t i = 0; i < shaderface->uniform_ct; ++i)
-               {
-               const Gwn_ShaderInput* uniform = shaderface->inputs + i;
-
-               if (uniform->builtin_type == builtin)
-                       return uniform;
-               }
-       return NULL; // not found
+       return shaderface->builtin_uniforms[builtin];
        }
 
 const Gwn_ShaderInput* GWN_shaderinterface_attr(const Gwn_ShaderInterface* shaderface, const char* name)
        }
 
 const Gwn_ShaderInput* GWN_shaderinterface_attr(const Gwn_ShaderInterface* shaderface, const char* name)