[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [31360] branches/soc-2010-moguri-2/source: Making the BL_BlenderShader responsible for reverting the uniform data.

Mitchell Stokes mogurijin at gmail.com
Mon Aug 16 02:56:30 CEST 2010


Revision: 31360
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=31360
Author:   moguri
Date:     2010-08-16 02:56:29 +0200 (Mon, 16 Aug 2010)

Log Message:
-----------
Making the BL_BlenderShader responsible for reverting the uniform data. Now the original uniform data is restored when the engine exits.

Modified Paths:
--------------
    branches/soc-2010-moguri-2/source/blender/blenkernel/BKE_material.h
    branches/soc-2010-moguri-2/source/blender/blenkernel/intern/material.c
    branches/soc-2010-moguri-2/source/gameengine/Ketsji/BL_BlenderShader.cpp
    branches/soc-2010-moguri-2/source/gameengine/Ketsji/BL_BlenderShader.h
    branches/soc-2010-moguri-2/source/gameengine/Ketsji/KX_PythonShaders.cpp
    branches/soc-2010-moguri-2/source/gameengine/Ketsji/KX_PythonShaders.h

Modified: branches/soc-2010-moguri-2/source/blender/blenkernel/BKE_material.h
===================================================================
--- branches/soc-2010-moguri-2/source/blender/blenkernel/BKE_material.h	2010-08-16 00:55:23 UTC (rev 31359)
+++ branches/soc-2010-moguri-2/source/blender/blenkernel/BKE_material.h	2010-08-16 00:56:29 UTC (rev 31360)
@@ -47,6 +47,7 @@
 void test_object_materials(struct ID *id);
 void init_material(struct Material *ma);
 void init_custom_uniform(struct CustomUniform *cu);
+struct CustomUniform *copy_custom_uniform(struct CustomUniform *cu);
 struct Material *add_material(char *name);
 struct Material *copy_material(struct Material *ma);
 struct Material *give_node_material(struct Material *ma); /* returns node material or self */

Modified: branches/soc-2010-moguri-2/source/blender/blenkernel/intern/material.c
===================================================================
--- branches/soc-2010-moguri-2/source/blender/blenkernel/intern/material.c	2010-08-16 00:55:23 UTC (rev 31359)
+++ branches/soc-2010-moguri-2/source/blender/blenkernel/intern/material.c	2010-08-16 00:56:29 UTC (rev 31360)
@@ -204,6 +204,44 @@
 	cu->type = MA_UNF_FLOAT;
 }
 
+CustomUniform *copy_custom_uniform(CustomUniform *cu)
+{
+	CustomUniform *copy = MEM_callocN(sizeof(CustomUniform), "copycustomuniform");
+
+	strcpy(copy->name, cu->name);
+	copy->type = cu->type;
+	copy->size = cu->size;
+
+	// Copy the data
+	switch(cu->type)
+	{
+	case MA_UNF_FLOAT:
+		copy->data = cu->data;
+		break;
+	case MA_UNF_VEC2:
+	case MA_UNF_VEC3:
+	case MA_UNF_VEC4:
+		copy->data = malloc(sizeof(float)*cu->size);
+		memcpy(copy->data, cu->data, sizeof(float)*cu->size);
+		break;
+	case MA_UNF_INT:
+		copy->data = cu->data;
+		break;
+	case MA_UNF_IVEC2:
+	case MA_UNF_IVEC3:
+	case MA_UNF_IVEC4:
+		copy->data = malloc(sizeof(int)*cu->size);
+		memcpy(copy->data, cu->data, sizeof(int)*cu->size);
+		break;
+	case MA_UNF_SAMPLER2D:
+		copy->data = malloc(sizeof(Tex));
+		memcpy(copy->data, cu->data, sizeof(Tex));
+		break;
+	}
+
+	return copy;
+}
+
 Material *add_material(char *name)
 {
 	Material *ma;

Modified: branches/soc-2010-moguri-2/source/gameengine/Ketsji/BL_BlenderShader.cpp
===================================================================
--- branches/soc-2010-moguri-2/source/gameengine/Ketsji/BL_BlenderShader.cpp	2010-08-16 00:55:23 UTC (rev 31359)
+++ branches/soc-2010-moguri-2/source/gameengine/Ketsji/BL_BlenderShader.cpp	2010-08-16 00:56:29 UTC (rev 31360)
@@ -1,10 +1,12 @@
 
 #include "DNA_customdata_types.h"
 #include "DNA_material_types.h"
+#include "DNA_texture_types.h"
 #include "DNA_scene_types.h"
 
 #include "BKE_global.h"
 #include "BKE_main.h"
+#include "BKE_material.h"
 
 #include "BL_BlenderShader.h"
 #include "BL_Material.h"
@@ -26,6 +28,20 @@
 	mBlenderScene = scene->GetBlenderScene();
 	mBlendMode = GPU_BLEND_SOLID;
 
+	// Store the custom uniforms so they can be restored on exit
+	CustomUniform* cu = (CustomUniform*)ma->csi.uniforms.first;
+	while (cu)
+	{
+		// Make a copy of the uniform
+		CustomUniform* copy = copy_custom_uniform(cu);
+
+		// Add the copy to the CustomUniform list
+		mCUList.push_back(copy);
+
+		// Move onto the next one
+		cu = cu->next;
+	}
+
 	ReloadMaterial();
 }
 
@@ -33,6 +49,68 @@
 {
 	if(mGPUMat)
 		GPU_material_unbind(mGPUMat);
+	if(mMat)
+	{
+		// Restore the uniforms with their original value
+		CustomUniform* cu = (CustomUniform*)mMat->csi.uniforms.first;
+		CustomUniform* copy = NULL;
+		std::vector<CustomUniform*>::iterator cit;
+
+		while(cu)
+		{
+			// First we need to find a match based on name, type  and size
+			for(cit = mCUList.begin(); cit != mCUList.end(); cit++)
+			{
+				if (!strcmp((*cit)->name, cu->name) && (*cit)->type == cu->type &&
+						(*cit)->size == cu->size)
+				{
+					copy = (*cit);
+					break;
+				}
+			}
+
+			// Skip this uniform if we didn't find it
+			if(!copy)
+			{
+				cu = cu->next;
+				continue;
+			}
+
+			// Now copy the data
+			switch(copy->type)
+			{
+			case MA_UNF_FLOAT:
+				cu->data = copy->data;
+				break;
+			case MA_UNF_VEC2:
+			case MA_UNF_VEC3:
+			case MA_UNF_VEC4:
+				memcpy(cu->data, copy->data, sizeof(float)*copy->size);
+				free(copy->data);
+				break;
+			case MA_UNF_INT:
+				cu->data = copy->data;
+				break;
+			case MA_UNF_IVEC2:
+			case MA_UNF_IVEC3:
+			case MA_UNF_IVEC4:
+				memcpy(cu->data, copy->data, sizeof(int)*copy->size);
+				free(copy->data);
+				break;
+			case MA_UNF_SAMPLER2D:
+				memcpy(cu->data, copy->data, sizeof(Tex));
+				free(copy->data);
+				break;
+			}
+
+			// Get rid of the copied uniform as we don't need it anymore
+			copy = NULL;
+			mCUList.erase(cit);
+
+			// Move onto the next uniform
+			cu = cu->next;
+		}
+	}
 	if(mMat && mMat->csi.sources)
 	{
 		mMat->csi.sources = NULL;

Modified: branches/soc-2010-moguri-2/source/gameengine/Ketsji/BL_BlenderShader.h
===================================================================
--- branches/soc-2010-moguri-2/source/gameengine/Ketsji/BL_BlenderShader.h	2010-08-16 00:55:23 UTC (rev 31359)
+++ branches/soc-2010-moguri-2/source/gameengine/Ketsji/BL_BlenderShader.h	2010-08-16 00:56:29 UTC (rev 31360)
@@ -34,6 +34,7 @@
 	int				mLightLayer;
 	int				mBlendMode;
 	GPUMaterial     *mGPUMat;
+	std::vector<struct CustomUniform*> mCUList; 
 
 	bool			VerifyShader() 
 	{

Modified: branches/soc-2010-moguri-2/source/gameengine/Ketsji/KX_PythonShaders.cpp
===================================================================
--- branches/soc-2010-moguri-2/source/gameengine/Ketsji/KX_PythonShaders.cpp	2010-08-16 00:55:23 UTC (rev 31359)
+++ branches/soc-2010-moguri-2/source/gameengine/Ketsji/KX_PythonShaders.cpp	2010-08-16 00:56:29 UTC (rev 31360)
@@ -79,7 +79,7 @@
 PyObject *KX_PythonShader::py_shader_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
 {
 	KX_PythonShader* pyshader = new KX_PythonShader();
-	return pyshader->NewProxy(true);
+	return pyshader->GetProxy();
 }
 
 PyTypeObject KX_PythonShader::Type = {
@@ -204,9 +204,17 @@
 	m_type(type),
 	m_size(size),
 	m_data(NULL),
-	m_cu(NULL),
-	m_old_data(NULL)
+	m_cu(NULL)
 {
+	m_cu = (CustomUniform*)malloc(sizeof(CustomUniform));
+	m_cu->next = m_cu->prev = NULL;
+
+	strcpy(m_cu->name, name);
+	m_cu->type = type;
+	m_cu->size = size;
+	
+	m_cu->data =NULL;
+
 }
 
 KX_PythonUniform::KX_PythonUniform(CustomUniform *cu)
@@ -217,69 +225,12 @@
 	m_type = cu->type;
 	m_size = cu->size;
 	m_data = cu->data;
-
-	// We copy the original data so we can restore it later
-	switch(m_type)
-	{
-	case MA_UNF_FLOAT:
-		m_old_data = m_data;
-		break;
-	case MA_UNF_VEC2:
-	case MA_UNF_VEC3:
-	case MA_UNF_VEC4:
-		m_old_data = malloc(sizeof(float)*m_size);
-		memcpy(m_old_data, m_data, sizeof(float)*m_size);
-		break;
-	case MA_UNF_INT:
-		m_old_data = m_data;
-		break;
-	case MA_UNF_IVEC2:
-	case MA_UNF_IVEC3:
-	case MA_UNF_IVEC4:
-		m_old_data = malloc(sizeof(int)*m_size);
-		memcpy(m_old_data, m_data, sizeof(int)*m_size);
-		break;
-	case MA_UNF_SAMPLER2D:
-		m_old_data = malloc(sizeof(Tex));
-		memcpy(m_old_data, m_data, sizeof(Tex));
-		break;
-	}
 }
 
 
 KX_PythonUniform::~KX_PythonUniform()
 {
-	// Replace the possibly edited data with the original values
-	if (m_cu && m_old_data)
-	{
-		switch(m_type)
-		{
-		case MA_UNF_FLOAT:
-			m_cu->data = m_data;
-			break;
-		case MA_UNF_VEC2:
-		case MA_UNF_VEC3:
-		case MA_UNF_VEC4:
-			memcpy(m_data, m_old_data, sizeof(float)*m_size);
-			free(m_old_data);
-			break;
-		case MA_UNF_INT:
-			m_cu->data = m_data;
-			break;
-		case MA_UNF_IVEC2:
-		case MA_UNF_IVEC3:
-		case MA_UNF_IVEC4:
-			memcpy(m_data, m_old_data, sizeof(int)*m_size);
-			free(m_old_data);
-			break;
-		case MA_UNF_SAMPLER2D:
-			memcpy(m_data, m_old_data, sizeof(Tex));
-			free(m_old_data);
-			break;
-		}
-
-		// Now we can get rid of the old data
-	}
+	// Empty
 }
 
 PyObject *KX_PythonUniform::py_uniform_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
@@ -316,7 +267,7 @@
 	}
 
 	KX_PythonUniform* pyuniform = new KX_PythonUniform(name, type_const, size);
-	return pyuniform->NewProxy(true);
+	return pyuniform->GetProxy();
 }
 
 PyTypeObject KX_PythonUniform::Type = {
@@ -356,6 +307,9 @@
 {
 	KX_PythonUniform *self = static_cast<KX_PythonUniform*>(self_v);
 
+	if (!self->m_data)
+		Py_RETURN_NONE;
+
 	switch(self->m_type)
 	{
 		case MA_UNF_FLOAT:
@@ -398,8 +352,7 @@
 			return ret;
 		}
 		case MA_UNF_SAMPLER2D:
-			PyErr_SetString(PyExc_AttributeError, "Sampler2D value not yet supported");
-			return NULL;
+			return PyLong_FromLong(((Tex*)self->m_data)->ima->bindcode);
 		default:
 			// Should never happen
 			PyErr_SetString(PyExc_AttributeError, "invalid type for uniform, internal error");
@@ -498,7 +451,7 @@
 			return PY_SET_ATTR_SUCCESS;
 		}
 		case MA_UNF_SAMPLER2D:
-			PyErr_SetString(PyExc_AttributeError, "Sampler2D value not yet supported");
+			PyErr_SetString(PyExc_AttributeError, "Sampler2D value is read-only");
 			return PY_SET_ATTR_FAIL;
 		default:
 			// Should never happen

Modified: branches/soc-2010-moguri-2/source/gameengine/Ketsji/KX_PythonShaders.h
===================================================================
--- branches/soc-2010-moguri-2/source/gameengine/Ketsji/KX_PythonShaders.h	2010-08-16 00:55:23 UTC (rev 31359)
+++ branches/soc-2010-moguri-2/source/gameengine/Ketsji/KX_PythonShaders.h	2010-08-16 00:56:29 UTC (rev 31360)
@@ -73,9 +73,7 @@
 	int m_size;
 	void *m_data;
 
-	/* These are used so we can restore the original data to the Custom Uniform when the engine is done */
 	struct CustomUniform *m_cu;
-	void *m_old_data; 
 
 public:
 	KX_PythonUniform(char* name, short type, int size);





More information about the Bf-blender-cvs mailing list