diff options
author | Sergey Sharybin <sergey.vfx@gmail.com> | 2017-10-05 14:19:14 +0300 |
---|---|---|
committer | Sergey Sharybin <sergey.vfx@gmail.com> | 2017-10-05 14:19:14 +0300 |
commit | 0b5bdc426588e1fed41c4d71ec06bdf9f2180b69 (patch) | |
tree | 34baf1825a9b878abeadd59dd62f1cf54d37cbe9 /intern/gawain | |
parent | bb9b8b13e582bc5bc961159e1e9c6d8d03cbff4c (diff) |
Gawain: Make builtin uniform lookup to be O(1)
Diffstat (limited to 'intern/gawain')
-rw-r--r-- | intern/gawain/gawain/gwn_shader_interface.h | 7 | ||||
-rw-r--r-- | intern/gawain/src/gwn_shader_interface.c | 20 |
2 files changed, 15 insertions, 12 deletions
diff --git a/intern/gawain/gawain/gwn_shader_interface.h b/intern/gawain/gawain/gwn_shader_interface.h index a1d4d82e080..4c3d44cadbd 100644 --- a/intern/gawain/gawain/gwn_shader_interface.h +++ b/intern/gawain/gawain/gwn_shader_interface.h @@ -14,7 +14,7 @@ #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 @@ -27,7 +27,9 @@ typedef enum { 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 { @@ -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]; + Gwn_ShaderInput* builtin_uniforms[GWN_NUM_UNIFORMS]; Gwn_ShaderInput inputs[0]; // dynamic size, uniforms followed by attribs } Gwn_ShaderInterface; diff --git a/intern/gawain/src/gwn_shader_interface.c b/intern/gawain/src/gwn_shader_interface.c index 076d0b71e15..5305dae9a9a 100644 --- a/intern/gawain/src/gwn_shader_interface.c +++ b/intern/gawain/src/gwn_shader_interface.c @@ -38,7 +38,8 @@ static const char* BuiltinUniform_name(Gwn_UniformBuiltin u) [GWN_UNIFORM_COLOR] = "color", - [GWN_UNIFORM_CUSTOM] = NULL + [GWN_UNIFORM_CUSTOM] = NULL, + [GWN_NUM_UNIFORMS] = NULL, }; 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); + 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) { @@ -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); + assert(builtin != GWN_NUM_UNIFORMS); #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) |