[Bf-blender-cvs] [3bc6b831d82] blender2.8: Gawain: shrink ShaderInterface name buffer

Mike Erwin noreply at git.blender.org
Mon Apr 17 07:46:18 CEST 2017


Commit: 3bc6b831d82724578fc8150d863b54f334db2bf4
Author: Mike Erwin
Date:   Mon Apr 17 01:45:21 2017 -0400
Branches: blender2.8
https://developer.blender.org/rB3bc6b831d82724578fc8150d863b54f334db2bf4

Gawain: shrink ShaderInterface name buffer

ShaderInterface stores names of custom uniforms and all attributes in an internal buffer, but it doesn't know how large to make this buffer until all inputs are scanned.

This commit shrinks the buffer to the exact size needed.

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

M	intern/gawain/src/shader_interface.c

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

diff --git a/intern/gawain/src/shader_interface.c b/intern/gawain/src/shader_interface.c
index b7a30f24c38..d5c78a4bbf0 100644
--- a/intern/gawain/src/shader_interface.c
+++ b/intern/gawain/src/shader_interface.c
@@ -192,14 +192,34 @@ ShaderInterface* ShaderInterface_create(GLint program)
 #endif
 		}
 
-	// TODO: realloc shaderface to shrink name buffer
-	//       each input->name will need adjustment (except static built-in names)
+	const uint32_t name_buffer_used = name_buffer_offset;
 
 #if DEBUG_SHADER_INTERFACE
-	printf("using %u of %u bytes from name buffer\n", name_buffer_offset, name_buffer_len);
+	printf("using %u of %u bytes from name buffer\n", name_buffer_used, name_buffer_len);
 	printf("}\n"); // exit function
 #endif
 
+	if (name_buffer_used < name_buffer_len)
+		{
+		// realloc shaderface to shrink name buffer
+		ShaderInterface* orig_pointer = shaderface;
+		shaderface = realloc(shaderface, offsetof(ShaderInterface, inputs) + input_ct * sizeof(ShaderInput) + name_buffer_used);
+		const ptrdiff_t delta = shaderface - orig_pointer;
+
+		if (delta)
+			{
+			// each input->name will need adjustment (except static built-in names)
+			const uint32_t input_ct = shaderface->uniform_ct + shaderface->attrib_ct;
+			for (uint32_t i = 0; i < input_ct; ++i)
+				{
+				ShaderInput* input = shaderface->inputs + i;
+
+				if (input->builtin_type == UNIFORM_CUSTOM || input->builtin_type == UNIFORM_NONE)
+					input->name += delta;
+				}
+			}
+		}
+
 	return shaderface;
 	}




More information about the Bf-blender-cvs mailing list