[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [29535] branches/soc-2010-moguri/source/ blender: Working on getting vector uniforms supported.

Mitchell Stokes mogurijin at gmail.com
Fri Jun 18 05:12:38 CEST 2010


Revision: 29535
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=29535
Author:   moguri
Date:     2010-06-18 05:12:35 +0200 (Fri, 18 Jun 2010)

Log Message:
-----------
Working on getting vector uniforms supported. Float vectors (vec2, vec3, vec4) are working in the RNA and are properly being passed to the shader. Integer vectors (ivec2, ivec3, ivec4) are working in RNA, but it doesn't look like they are working in the shader.

Modified Paths:
--------------
    branches/soc-2010-moguri/source/blender/gpu/intern/gpu_material.c
    branches/soc-2010-moguri/source/blender/makesrna/intern/rna_material.c

Modified: branches/soc-2010-moguri/source/blender/gpu/intern/gpu_material.c
===================================================================
--- branches/soc-2010-moguri/source/blender/gpu/intern/gpu_material.c	2010-06-17 22:20:14 UTC (rev 29534)
+++ branches/soc-2010-moguri/source/blender/gpu/intern/gpu_material.c	2010-06-18 03:12:35 UTC (rev 29535)
@@ -390,10 +390,20 @@
 		/* handle custom uniforms */
 		for(cu=material->ma->csi.uniforms.first; cu; cu=cu->next) {
 			loc = GPU_shader_get_uniform(shader, cu->name);
-			if(cu->type == MA_UNF_FLOAT)
-				GPU_shader_uniform_vector(shader, loc, 1, 1, (float*)(&cu->data));
+			if(cu->type >= MA_UNF_FLOAT || cu->type <= MA_UNF_VEC4)
+			{
+				if (cu->size > 1)
+					GPU_shader_uniform_vector(shader, loc, cu->size, 1, (float*)(cu->data));
+				else
+					GPU_shader_uniform_vector(shader, loc, 1, 1, (float*)(&cu->data));
+			}
 			else if(cu->type == MA_UNF_INT)
-				GPU_shader_uniform_ivector(shader, loc, 1, 1, (int*)(&cu->data));
+			{
+				if (cu->size > 1)
+					GPU_shader_uniform_ivector(shader, loc, cu->size, 1, (int*)(cu->data));
+				else
+					GPU_shader_uniform_ivector(shader, loc, 1, 1, (int*)(&cu->data));
+			}
 			else if(cu->type == MA_UNF_SAMPLER2D)
 			{
 				Tex *tex;

Modified: branches/soc-2010-moguri/source/blender/makesrna/intern/rna_material.c
===================================================================
--- branches/soc-2010-moguri/source/blender/makesrna/intern/rna_material.c	2010-06-17 22:20:14 UTC (rev 29534)
+++ branches/soc-2010-moguri/source/blender/makesrna/intern/rna_material.c	2010-06-18 03:12:35 UTC (rev 29535)
@@ -191,9 +191,37 @@
 
 	if (value != cu->type)
 	{
-		// We'll get into trouble if we don't reset the value
+		// Clean up after ourselves
+		if (cu->size > 1 && cu->data)
+			MEM_freeN(cu->data);
+
 		cu->data = NULL;
 
+		// Change the size
+		if (value == MA_UNF_VEC2 || value == MA_UNF_IVEC2)
+			cu->size = 2;
+		else if (value == MA_UNF_VEC3 || value == MA_UNF_IVEC3)
+			cu->size = 3;
+		else if (value == MA_UNF_VEC4 || value == MA_UNF_IVEC4)
+			cu->size = 4;
+		else
+			cu->size = 1;
+
+		// We also need to do some array creation for vectors
+		if (value == MA_UNF_VEC2 ||
+			value == MA_UNF_VEC3 ||
+			value == MA_UNF_VEC4)
+		{
+			cu->data = MEM_mallocN(sizeof(float)*cu->size, "vec"+cu->size);
+			memset(cu->data, 0.f, sizeof(float)*cu->size);
+		}
+		else if (value == MA_UNF_IVEC2 ||
+				value == MA_UNF_IVEC3 ||
+				value == MA_UNF_IVEC4)
+		{
+			cu->data = MEM_mallocN(sizeof(int)*cu->size, "ivec"+cu->size);
+			memset(cu->data, 0, sizeof(int)*cu->size);
+		}
 		cu->type = value;
 	}
 }
@@ -210,20 +238,66 @@
 	*(float*)(&cu->data)= value;
 }
 
+static void rna_VecUniform_value_get(struct PointerRNA *ptr, float *values)
+{
+	CustomUniform *cu= (CustomUniform*)ptr->data;
+	int i;
+
+	memset(values, 0.f, sizeof(float)*cu->size);
+	for(i=0; i<cu->size; i++)
+		values[i] = ((float*)cu->data)[i];
+}
+
+static void rna_VecUniform_value_set(struct PointerRNA *ptr, const float *values)
+{
+	CustomUniform *cu= (CustomUniform*)ptr->data;
+	int i;
+
+	for (i=0; i<cu->size; i++)
+		((float*)cu->data)[i] = values[i];
+}
+
+static void rna_IVecUniform_value_get(struct PointerRNA *ptr, int *values)
+{
+	CustomUniform *cu= (CustomUniform*)ptr->data;
+	int i;
+
+	memset(values, 0, sizeof(int)*cu->size);
+
+	for(i=0; i<cu->size; i++)
+		values[i] = ((int*)cu->data)[i];
+}
+
+static void rna_IVecUniform_value_set(struct PointerRNA *ptr, const int *values)
+{
+	CustomUniform *cu= (CustomUniform*)ptr->data;
+	int i;
+
+	for (i=0; i<cu->size; i++)
+		((int*)cu->data)[i] = values[i];
+}
+
 static StructRNA* rna_CustomUniform_refine(struct PointerRNA *ptr)
 {
 	CustomUniform *cu= (CustomUniform*)ptr->data;
 
 	switch(cu->type){
 	case MA_UNF_FLOAT:
-		cu->size= 1;
 		return &RNA_FloatUniform;
+	case MA_UNF_VEC2:
+		return &RNA_Vec2Uniform;
+	case MA_UNF_VEC3:
+		return &RNA_Vec3Uniform;
+	case MA_UNF_VEC4:
+		return &RNA_Vec4Uniform;
 	case MA_UNF_INT:
-		cu->size= 1;
-		return &RNA_IntUniform;
-	//case MA_UNF_VEC3:
-	//	cu->size= 3;
-	//	return &RNA_Vec3Uniform;
+		return &RNA_IntUniform;	
+	case MA_UNF_IVEC2:
+		return &RNA_IVec2Uniform;
+	case MA_UNF_IVEC3:
+		return &RNA_IVec3Uniform;
+	case MA_UNF_IVEC4:
+		return &RNA_IVec4Uniform;
 	case MA_UNF_SAMPLER2D:
 		return &RNA_Sampler2DUniform;
 	default:
@@ -972,7 +1046,7 @@
 }
 
 /* Helper function for defining the different uniform data types */
-static void rna_def_uniformtype(BlenderRNA *brna, const char *name, const char *doc, int type, int subtype, int min, int max)
+static void rna_def_uniformtype(BlenderRNA *brna, const char *name, const char *doc, int type, int size)
 {
 	PropertyRNA *prop;
 	StructRNA *srna;
@@ -981,13 +1055,27 @@
 	RNA_def_struct_ui_text(srna, name, doc);
 	RNA_def_struct_sdna(srna, "CustomUniform");
 
-	prop= RNA_def_property(srna, "value", type, subtype);
+	prop= RNA_def_property(srna, "value", type, PROP_NONE);
+	if (size > 1)
+		RNA_def_property_array(prop, size);
+
 	if (type == PROP_INT)
-		RNA_def_property_int_sdna(prop, NULL, "data");
+	{
+		if (size > 1)
+			RNA_def_property_int_funcs(prop, "rna_IVecUniform_value_get", "rna_IVecUniform_value_set", NULL);
+		else
+			RNA_def_property_int_sdna(prop, NULL, "data");
+	}
+
 	else if (type == PROP_FLOAT)
 	{
-		RNA_def_property_float_sdna(prop, NULL, "data");
-		RNA_def_property_float_funcs(prop, "rna_FloatUniform_value_get", "rna_FloatUniform_value_set", NULL);
+		if (size > 1)
+			RNA_def_property_float_funcs(prop, "rna_VecUniform_value_get", "rna_VecUniform_value_set", NULL);
+		else
+		{
+			RNA_def_property_float_sdna(prop, NULL, "data");
+			RNA_def_property_float_funcs(prop, "rna_FloatUniform_value_get", "rna_FloatUniform_value_set", NULL);
+		}
 	}
 	else if (type == PROP_POINTER)
 	{	
@@ -998,12 +1086,6 @@
 
 	RNA_def_property_ui_text(prop, "Value", "Uniform value");
 	RNA_def_property_update(prop, 0, "rna_Material_update");
-
-	/*prop= RNA_def_property(srna, "size", PROP_INT, PROP_NONE);
-	RNA_def_property_int_sdna(prop, NULL, "size");
-	RNA_def_property_range(prop, min, max);
-	RNA_def_property_ui_text(prop, "Size", "The number of elements in the uniform");
-	RNA_def_property_update(prop, 0, "rna_Material_update");*/
 }
 
 static void rna_def_material_uniform(BlenderRNA *brna)
@@ -1013,10 +1095,13 @@
 
 	static EnumPropertyItem prop_type_items[] = {
 		{MA_UNF_FLOAT, "FLOAT", 0, "float", "float"},
+		{MA_UNF_VEC2, "VEC2", 0, "vec2", "vec2"},
 		{MA_UNF_VEC3, "VEC3", 0, "vec3", "vec3"},
-		//{MA_UNF_VEC, "VEC", 0, "vec", "vec"},
+		{MA_UNF_VEC4, "VEC4", 0, "vec4", "vec4"},
 		{MA_UNF_INT, "INT", 0, "int", "int"},
-		//{MA_UNF_IVEC2, "IVEC", 0, "ivec", "ivec"},
+		{MA_UNF_IVEC2, "IVEC2", 0, "ivec2", "ivec2"},
+		{MA_UNF_IVEC3, "IVEC3", 0, "ivec3", "ivec3"},
+		{MA_UNF_IVEC4, "IVEC4", 0, "ivec4", "ivec4"},
 		{MA_UNF_SAMPLER2D, "SAMPLER2D", 0, "sampler2D", "sampler2D"},
 		{0, NULL, 0, NULL, NULL}};
 
@@ -1039,10 +1124,15 @@
 	RNA_def_property_ui_text(prop, "Uniform type", "The data type of the uniform");
 	RNA_def_property_update(prop, 0, "rna_Material_update");
 
-	rna_def_uniformtype(brna, "FloatUniform", "Custom float uniform", PROP_FLOAT, PROP_NONE, 1, 1);
-	rna_def_uniformtype(brna, "IntUniform", "Custom integer uniform", PROP_INT, PROP_NONE, 1, 1);
-//	rna_def_uniformtype(brna, "Vec3Uniform", "Custom vector 3 uniform", PROP_FLOAT, PROP_TRANSLATION, 3, 3);
-	rna_def_uniformtype(brna, "Sampler2DUniform", "Custom sampler2D uniform", PROP_POINTER, PROP_NONE, 1, 1);
+	rna_def_uniformtype(brna, "FloatUniform", "Custom float uniform", PROP_FLOAT, 1);
+	rna_def_uniformtype(brna, "IntUniform", "Custom integer uniform", PROP_INT, 1);
+	rna_def_uniformtype(brna, "Vec2Uniform", "Custom float vector 2 uniform", PROP_FLOAT, 2);
+	rna_def_uniformtype(brna, "Vec3Uniform", "Custom float vector 3 uniform", PROP_FLOAT, 3);
+	rna_def_uniformtype(brna, "Vec4Uniform", "Custom float vector 4 uniform", PROP_FLOAT, 4);
+	rna_def_uniformtype(brna, "IVec2Uniform", "Custom integer vector 2 uniform", PROP_INT, 2);
+	rna_def_uniformtype(brna, "IVec3Uniform", "Custom integer vector 3 uniform", PROP_INT, 3);
+	rna_def_uniformtype(brna, "IVec4Uniform", "Custom integer vector 4 uniform", PROP_INT, 4);
+	rna_def_uniformtype(brna, "Sampler2DUniform", "Custom sampler2D uniform", PROP_POINTER, 1);
 
 }
 static void rna_def_material_raymirror(BlenderRNA *brna)





More information about the Bf-blender-cvs mailing list