[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [53281] branches/ge_harmony/source/blender /blenkernel/intern/shader.c: Adding support for loading default uniform values in shader UIs.

Daniel Stokes kupomail at gmail.com
Sat Dec 22 21:44:35 CET 2012


Revision: 53281
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=53281
Author:   kupoman
Date:     2012-12-22 20:44:32 +0000 (Sat, 22 Dec 2012)
Log Message:
-----------
Adding support for loading default uniform values in shader UIs. Also cleaned up the shader parser to be more robust by not relying on new lines. Furthermore, the parser now ignores empty file paths (such as when a shader has not been selected yet).

Modified Paths:
--------------
    branches/ge_harmony/source/blender/blenkernel/intern/shader.c

Modified: branches/ge_harmony/source/blender/blenkernel/intern/shader.c
===================================================================
--- branches/ge_harmony/source/blender/blenkernel/intern/shader.c	2012-12-22 18:34:17 UTC (rev 53280)
+++ branches/ge_harmony/source/blender/blenkernel/intern/shader.c	2012-12-22 20:44:32 UTC (rev 53281)
@@ -162,6 +162,9 @@
 	char *shader = NULL;
 	char path[280] = {0};
 
+	if (strcmp(filename, "") == 0)
+		return shader;
+
 	strcpy(path, filename);
 
 	if (filename[0] == '/' && filename[1] == '/')
@@ -211,6 +214,46 @@
 	return token;
 }
 
+void extract_default(const char *src,  Uniform *uni)
+{
+	void *var = NULL;
+	int error = 0;
+
+	/* Ignore whitespace */
+	while (*src != '\0' && (*src == ' ' || *src == '\t' || *src == '\n'))
+		src++;
+
+	if (uni->type == SHADER_UNF_FLOAT) {
+		if (sscanf(src, "%f", &uni->data) != 1)
+			error = 1;
+	}
+	else if (uni->type == SHADER_UNF_VEC2) {
+		if (sscanf(src, "vec2(%f, %f)", uni->data, (float*)uni->data+1) != 2)
+			error = 1;
+	}
+	else if (uni->type == SHADER_UNF_VEC3) {
+		if (sscanf(src, "vec3(%f, %f, %f)", uni->data, (float*)uni->data+1, (float*)uni->data+2) != 3)
+			error = 1;
+	}
+	else if (uni->type == SHADER_UNF_INT) {
+		if (sscanf(src, "%d", &uni->data) != 1)
+			error = 1;
+	}
+	else if (uni->type == SHADER_UNF_IVEC2) {
+		if (sscanf(src, "vec2(%d, %d)", uni->data, (int*)uni->data+1) != 2)
+			error = 1;
+	}
+	else if (uni->type == SHADER_UNF_IVEC3) {
+		if (sscanf(src, "vec3(%d, %d, %d)", uni->data, (int*)uni->data+1, (int*)uni->data+2) != 3)
+			error = 1;
+	}
+
+	if (error)
+	{
+		printf("Unable to parse default value for uniform %s\n", uni->name);
+	}
+}
+
 void gather_uniforms(Shader *sh)
 {
 	Uniform *uni;
@@ -221,32 +264,23 @@
 	if (!src)
 		return;
 
-	while (*src != '\0') {
-		line_len = 0;
-		while (*src != '\n' && *src != '\0') {
-			line_len++;
-			src++;
+	while (src = strstr(src, "uniform")) {
+		src += 7;
+		type = extract_token(&src);
+		name = extract_token(&src);
+		id = BLI_strdupcat(name, type);
+		uni = (Uniform *)BLI_ghash_pop(sh->uniform_cache, id, NULL);
+		if (!uni)
+			uni = uniform_init(type, name);
+		
+		while (*src++ != ';') {
+			if (*src == '=')
+				extract_default(src+1, uni);
 		}
-		src -= line_len;
-		if (starts_with(src, "uniform")) {
-			src += 7;
-			type = extract_token(&src);
-			name = extract_token(&src);
-			id = BLI_strdupcat(name, type);
-			uni = (Uniform *)BLI_ghash_pop(sh->uniform_cache, id, NULL);
-			if (!uni)
-				uni = uniform_init(type, name);
-			BLI_addtail(&sh->uniforms, uni);
-			MEM_freeN(type);
-			MEM_freeN(name);
-			MEM_freeN(id);
-
-			while (*src != '\n' && *src != '\0')
-				src++;
-			src++;
-		}
-		else
-			src += line_len+1;
+		BLI_addtail(&sh->uniforms, uni);
+		MEM_freeN(type);
+		MEM_freeN(name);
+		MEM_freeN(id);
 	}
 
 }




More information about the Bf-blender-cvs mailing list