[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [49055] branches/ge_harmony/source/blender : Making shader uniforms more reliable.

Daniel Stokes kupomail at gmail.com
Thu Jul 19 06:50:21 CEST 2012


Revision: 49055
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=49055
Author:   kupoman
Date:     2012-07-19 04:50:21 +0000 (Thu, 19 Jul 2012)
Log Message:
-----------
Making shader uniforms more reliable. Now when you change shaders, the uniform values are saved (until the file closes) in case you switch back to that shader. This also means you can easily switch between two shaders using the same uniforms without losing the uniform setting.

Modified Paths:
--------------
    branches/ge_harmony/source/blender/blenkernel/intern/shader.c
    branches/ge_harmony/source/blender/blenloader/intern/readfile.c
    branches/ge_harmony/source/blender/makesdna/DNA_shader_types.h

Modified: branches/ge_harmony/source/blender/blenkernel/intern/shader.c
===================================================================
--- branches/ge_harmony/source/blender/blenkernel/intern/shader.c	2012-07-19 04:47:53 UTC (rev 49054)
+++ branches/ge_harmony/source/blender/blenkernel/intern/shader.c	2012-07-19 04:50:21 UTC (rev 49055)
@@ -32,7 +32,6 @@
 #include <stdlib.h>
 #include <string.h>
 
-#include "DNA_ID.h"
 #include "DNA_shader_types.h"
 
 #include "MEM_guardedalloc.h"
@@ -43,7 +42,6 @@
 #include "BKE_shader.h"
 #include "BKE_text.h"
 
-#include "BLI_listbase.h"
 #include "BLI_string.h"
 
 void init_shader(Shader *sh)
@@ -53,6 +51,8 @@
 
 	sh->uniforms.first = NULL;
 	sh->uniforms.last = NULL;
+
+	sh->uniform_cache = BLI_ghash_str_new("Uniform Cache");
 }
 
 void BKE_shader_free(Shader *sh)
@@ -61,6 +61,7 @@
 		MEM_freeN(sh->source);
 
 	BLI_freelistN(&sh->uniforms);
+	BLI_ghash_free(sh->uniform_cache, NULL, (GHashValFreeFP)MEM_freeN);
 }
 
 struct Shader *BKE_shader_add(const char *name)
@@ -143,6 +144,7 @@
 	else
 		uni->data = 0;
 
+	uni->prev = uni->next = NULL;
 
 	return uni;
 }
@@ -195,6 +197,7 @@
 
 void gather_uniforms(Shader *sh)
 {
+	Uniform *uni;
 	char *src = sh->source;
 	char *type, *name;
 	int line_len;
@@ -215,7 +218,10 @@
 			type = extract_token(&src);
 			name = extract_token(&src);
 
-			BLI_addtail(&sh->uniforms, uniform_init(type, name));
+			uni = (Uniform *)BLI_ghash_lookup(sh->uniform_cache, name);
+			if (!uni)
+				uni = uniform_init(type, name);
+			BLI_addtail(&sh->uniforms, uni);
 			MEM_freeN(type);
 			MEM_freeN(name);
 
@@ -231,8 +237,7 @@
 
 void BKE_shader_read_source(Shader *sh)
 {
-	/* Clear the uniform list to rebuild it */
-	//BLI_freelistN(&sh->uniforms);
+	Uniform *uni;
 
 	/* Clean up an previous source text */
 	if (sh->source)
@@ -242,6 +247,11 @@
 		sh->source = txt_to_buf(sh->sourcetext);
 	else if (sh->location == SHADER_LOC_EXTERNAL)
 		sh->source = file_to_buf(sh->sourcepath);
-	if (!sh->uniforms.first)
-		gather_uniforms(sh);
+
+	/* Cache the uniform list and rebuild it */
+	for (uni = sh->uniforms.first; uni; uni = uni->next)
+		BLI_ghash_insert(sh->uniform_cache, uni->name, uni);
+
+	sh->uniforms.first = sh->uniforms.last = NULL;
+	gather_uniforms(sh);
 }
\ No newline at end of file

Modified: branches/ge_harmony/source/blender/blenloader/intern/readfile.c
===================================================================
--- branches/ge_harmony/source/blender/blenloader/intern/readfile.c	2012-07-19 04:47:53 UTC (rev 49054)
+++ branches/ge_harmony/source/blender/blenloader/intern/readfile.c	2012-07-19 04:50:21 UTC (rev 49055)
@@ -3297,6 +3297,8 @@
 	Uniform *uni;
 
 	link_list(fd, &sh->uniforms);
+	sh->uniform_cache = BLI_ghash_str_new("Uniform Cache");
+	sh->source = NULL;
 
 	for (uni = sh->uniforms.first; uni; uni = uni->next) {
 		if (uni->type != MA_UNF_FLOAT &&

Modified: branches/ge_harmony/source/blender/makesdna/DNA_shader_types.h
===================================================================
--- branches/ge_harmony/source/blender/makesdna/DNA_shader_types.h	2012-07-19 04:47:53 UTC (rev 49054)
+++ branches/ge_harmony/source/blender/makesdna/DNA_shader_types.h	2012-07-19 04:50:21 UTC (rev 49055)
@@ -32,11 +32,13 @@
 #ifndef __DNA_SHADER_TYPES_H__
 #define __DNA_SHADER_TYPES_H__
 
-#include "DNA_listBase.h"
 #include "DNA_ID.h"
 
+#include "BLI_ghash.h"
+#include "BLI_listbase.h"
+
 typedef struct Uniform {
-	struct Uniform *prev, *next;
+	struct Uniform *next, *prev;
 	/* name array is 68 bytes for padding, if this changes update shader_init in shader.c */
 	char name[68];
 
@@ -56,6 +58,7 @@
 	struct Text *sourcetext;
 	char *source;
 	ListBase uniforms;
+	GHash *uniform_cache;
 } Shader;
 
 /* type */




More information about the Bf-blender-cvs mailing list