[Bf-blender-cvs] [0b5bdc42658] blender2.8: Gawain: Make builtin uniform lookup to be O(1)

Sergey Sharybin noreply at git.blender.org
Thu Oct 5 14:17:11 CEST 2017


Commit: 0b5bdc426588e1fed41c4d71ec06bdf9f2180b69
Author: Sergey Sharybin
Date:   Thu Oct 5 16:19:14 2017 +0500
Branches: blender2.8
https://developer.blender.org/rB0b5bdc426588e1fed41c4d71ec06bdf9f2180b69

Gawain: Make builtin uniform lookup to be O(1)

===================================================================

M	intern/gawain/gawain/gwn_shader_interface.h
M	intern/gawain/src/gwn_shader_interface.c

===================================================================

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)



More information about the Bf-blender-cvs mailing list