[Bf-blender-cvs] [da17928fd50] blender2.8: Gawain: look up builtin uniforms by enum, not by name

Mike Erwin noreply at git.blender.org
Sun Apr 16 01:19:46 CEST 2017


Commit: da17928fd50072ac5c1828baaf44bf08a02c0729
Author: Mike Erwin
Date:   Sat Apr 15 19:19:00 2017 -0400
Branches: blender2.8
https://developer.blender.org/rBda17928fd50072ac5c1828baaf44bf08a02c0729

Gawain: look up builtin uniforms by enum, not by name

This speeds up color and transformation matrix lookups at draw time (used by almost all shaders).

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

M	intern/gawain/gawain/shader_interface.h
M	intern/gawain/src/immediate.c
M	intern/gawain/src/shader_interface.c

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

diff --git a/intern/gawain/gawain/shader_interface.h b/intern/gawain/gawain/shader_interface.h
index 32b5098ad80..5c37d507806 100644
--- a/intern/gawain/gawain/shader_interface.h
+++ b/intern/gawain/gawain/shader_interface.h
@@ -19,11 +19,12 @@ typedef enum {
 	UNIFORM_MODELVIEW,  // mat4 ModelViewMatrix
 	UNIFORM_PROJECTION, // mat4 ProjectionMatrix
 	UNIFORM_MVP,        // mat4 ModelViewProjectionMatrix
-	UNIFORM_NORMAL,     // mat3 NormalMatrix
 
 	UNIFORM_MODELVIEW_INV,  // mat4 ModelViewInverseMatrix
 	UNIFORM_PROJECTION_INV, // mat4 ProjectionInverseMatrix
 
+	UNIFORM_NORMAL,     // mat3 NormalMatrix
+
 	UNIFORM_COLOR, // vec4 color
 
 	UNIFORM_CUSTOM // custom uniform, not one of the above built-ins
diff --git a/intern/gawain/src/immediate.c b/intern/gawain/src/immediate.c
index 95414021091..8c42ed54f75 100644
--- a/intern/gawain/src/immediate.c
+++ b/intern/gawain/src/immediate.c
@@ -810,27 +810,33 @@ void immUniform4iv(const char* name, const int data[4])
 
 void immUniformColor4f(float r, float g, float b, float a)
 	{
-	immUniform4f("color", r, g, b, a);
+	const ShaderInput* uniform = ShaderInterface_builtin_uniform(imm.shader_interface, UNIFORM_COLOR);
+
+#if TRUST_NO_ONE
+	assert(uniform != NULL);
+#endif
+
+	glUniform4f(uniform->location, r, g, b, a);
 	}
 
 void immUniformColor4fv(const float rgba[4])
 	{
-	immUniform4fv("color", rgba);
+	immUniformColor4f(rgba[0], rgba[1], rgba[2], rgba[3]);
 	}
 
 void immUniformColor3f(float r, float g, float b)
 	{
-	immUniform4f("color", r, g, b, 1.0f);
+	immUniformColor4f(r, g, b, 1.0f);
 	}
 
 void immUniformColor3fv(const float rgb[3])
 	{
-	immUniform4f("color", rgb[0], rgb[1], rgb[2], 1.0f);
+	immUniformColor4f(rgb[0], rgb[1], rgb[2], 1.0f);
 	}
 
 void immUniformColor3fvAlpha(const float rgb[3], float a)
 	{
-	immUniform4f("color", rgb[0], rgb[1], rgb[2], a);
+	immUniformColor4f(rgb[0], rgb[1], rgb[2], a);
 	}
 
 // TODO: v-- treat as sRGB? --v
@@ -838,13 +844,13 @@ void immUniformColor3fvAlpha(const float rgb[3], float a)
 void immUniformColor3ub(unsigned char r, unsigned char g, unsigned char b)
 	{
 	const float scale = 1.0f / 255.0f;
-	immUniform4f("color", scale * r, scale * g, scale * b, 1.0f);
+	immUniformColor4f(scale * r, scale * g, scale * b, 1.0f);
 	}
 
 void immUniformColor4ub(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
 	{
 	const float scale = 1.0f / 255.0f;
-	immUniform4f("color", scale * r, scale * g, scale * b, scale * a);
+	immUniformColor4f(scale * r, scale * g, scale * b, scale * a);
 	}
 
 void immUniformColor3ubv(const unsigned char rgb[3])
diff --git a/intern/gawain/src/shader_interface.c b/intern/gawain/src/shader_interface.c
index 5e55ceed992..7af26a2585e 100644
--- a/intern/gawain/src/shader_interface.c
+++ b/intern/gawain/src/shader_interface.c
@@ -30,11 +30,12 @@ static const char* BuiltinUniform_name(BuiltinUniform u)
 		[UNIFORM_MODELVIEW] = "ModelViewMatrix",
 		[UNIFORM_PROJECTION] = "ProjectionMatrix",
 		[UNIFORM_MVP] = "ModelViewProjectionMatrix",
-		[UNIFORM_NORMAL] = "NormalMatrix",
 
 		[UNIFORM_MODELVIEW_INV] = "ModelViewInverseMatrix",
 		[UNIFORM_PROJECTION_INV] = "ProjectionInverseMatrix",
 
+		[UNIFORM_NORMAL] = "NormalMatrix",
+
 		[UNIFORM_COLOR] = "color",
 
 		[UNIFORM_CUSTOM] = NULL
@@ -43,13 +44,61 @@ static const char* BuiltinUniform_name(BuiltinUniform u)
 	return names[u];
 	}
 
+static bool match(const char* a, const char* b)
+	{
+	return strcmp(a, b) == 0;
+	}
+
+// keep these in sync with BuiltinUniform order
+#define FIRST_MAT4_UNIFORM UNIFORM_MODELVIEW
+#define LAST_MAT4_UNIFORM UNIFORM_PROJECTION_INV
+
 static bool setup_builtin_uniform(ShaderInput* input, const char* name)
 	{
 	// TODO: reject DOUBLE, IMAGE, ATOMIC_COUNTER gl_types
 
-	// TODO: detect built-in uniforms (gl_type and name must match)
-	//       if a match is found, use BuiltinUniform_name so name buffer space can be reclaimed
-	input->name = name;
+	// detect built-in uniforms (gl_type and name must match)
+	// if a match is found, use BuiltinUniform_name so name buffer space can be reclaimed
+	switch (input->gl_type)
+		{
+		case GL_FLOAT_MAT4:
+			for (BuiltinUniform u = FIRST_MAT4_UNIFORM; u <= LAST_MAT4_UNIFORM; ++u)
+				{
+				const char* builtin_name = BuiltinUniform_name(u);
+				if (match(name, builtin_name))
+					{
+					input->name = builtin_name;
+					input->builtin_type = u;
+					return true;
+					}
+				}
+			break;
+		case GL_FLOAT_MAT3:
+			{
+			const char* builtin_name = BuiltinUniform_name(UNIFORM_NORMAL);
+			if (match(name, builtin_name))
+				{
+				input->name = builtin_name;
+				input->builtin_type = UNIFORM_NORMAL;
+				return true;
+				}
+			}
+			break;
+		case GL_FLOAT_VEC4:
+			{
+			const char* builtin_name = BuiltinUniform_name(UNIFORM_COLOR);
+			if (match(name, builtin_name))
+				{
+				input->name = builtin_name;
+				input->builtin_type = UNIFORM_COLOR;
+				return true;
+				}
+			}
+			break;
+		default:
+			;
+		} 
+
 	input->builtin_type = UNIFORM_CUSTOM;
 	return false;
 	}
@@ -170,7 +219,7 @@ const ShaderInput* ShaderInterface_uniform(const ShaderInterface* shaderface, co
 		if (uniform->name == NULL) continue;
 #endif
 
-		if (strcmp(uniform->name, name) == 0)
+		if (match(uniform->name, name))
 			return uniform;
 		}
 	return NULL; // not found
@@ -178,8 +227,15 @@ const ShaderInput* ShaderInterface_uniform(const ShaderInterface* shaderface, co
 
 const ShaderInput* ShaderInterface_builtin_uniform(const ShaderInterface* shaderface, BuiltinUniform builtin)
 	{
-	// TODO: look up by enum, not name (fix setup_builtin_uniform first)
-	return ShaderInterface_uniform(shaderface, BuiltinUniform_name(builtin));
+	// look up by enum, not name
+	for (uint32_t i = 0; i < shaderface->uniform_ct; ++i)
+		{
+		const ShaderInput* uniform = shaderface->inputs + i;
+
+		if (uniform->builtin_type == builtin)
+			return uniform;
+		}
+	return NULL; // not found
 	}
 
 const ShaderInput* ShaderInterface_attrib(const ShaderInterface* shaderface, const char* name)
@@ -194,7 +250,7 @@ const ShaderInput* ShaderInterface_attrib(const ShaderInterface* shaderface, con
 		if (attrib->name == NULL) continue;
 #endif
 
-		if (strcmp(attrib->name, name) == 0)
+		if (match(attrib->name, name))
 			return attrib;
 		}
 	return NULL; // not found




More information about the Bf-blender-cvs mailing list