[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [58787] branches/soc-2013-depsgraph_mt/ source: Optimization and threading fix for shapekeys weights calculation

Sergey Sharybin sergey.vfx at gmail.com
Wed Jul 31 23:55:25 CEST 2013


Revision: 58787
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=58787
Author:   nazgul
Date:     2013-07-31 21:55:25 +0000 (Wed, 31 Jul 2013)
Log Message:
-----------
Optimization and threading fix for shapekeys weights calculation

This commit fixes two different issues, which were caused by
how weights are being calculated for relative shapekeys.

Weights for key block used to saved in KeyBlock DNA structure,
which lead to situations when different objects could start
writing to the same weights array if they're sharing the same
key datablock.

Solved this in a way so weights are never stored in KeyBlock
and being passed to shapekeys routines as an array of pointers.
This way weights are still computed run-time (meaning they're
calculated before shapekey evaluation and freed afterwards).

This required some changes to GameEngine as well, to make it
never cache weights in the key blocks.

Another aspect of this commit makes it so weight for a given
vertex group is only computed once. So if multiple key blocks
are using the same influence vertex group, they'll share the
same exact weights array. This gave around 1.7x speedup in
test chinchilla file which is close enough to if we've been
caching weights permanently in DNA (test machine is dual-code
4 threads laptop).

Some further speed is optimization possible, but it could be
done later as well.

Thanks Brecht for idea of how the things might be solved in
really clear way.

Modified Paths:
--------------
    branches/soc-2013-depsgraph_mt/source/blender/blenkernel/BKE_key.h
    branches/soc-2013-depsgraph_mt/source/blender/blenkernel/intern/key.c
    branches/soc-2013-depsgraph_mt/source/blender/editors/mesh/meshtools.c
    branches/soc-2013-depsgraph_mt/source/blender/makesdna/DNA_key_types.h
    branches/soc-2013-depsgraph_mt/source/gameengine/Converter/BL_ShapeDeformer.cpp
    branches/soc-2013-depsgraph_mt/source/gameengine/Rasterizer/RAS_MeshObject.cpp
    branches/soc-2013-depsgraph_mt/source/gameengine/Rasterizer/RAS_MeshObject.h

Modified: branches/soc-2013-depsgraph_mt/source/blender/blenkernel/BKE_key.h
===================================================================
--- branches/soc-2013-depsgraph_mt/source/blender/blenkernel/BKE_key.h	2013-07-31 21:51:05 UTC (rev 58786)
+++ branches/soc-2013-depsgraph_mt/source/blender/blenkernel/BKE_key.h	2013-07-31 21:55:25 UTC (rev 58787)
@@ -41,6 +41,7 @@
 struct Scene;
 struct Lattice;
 struct Mesh;
+struct WeightsArrayCache;
 
 /* Kernel prototypes */
 #ifdef __cplusplus
@@ -73,9 +74,18 @@
 struct KeyBlock *BKE_keyblock_find_name(struct Key *key, const char name[]);
 void             BKE_keyblock_copy_settings(struct KeyBlock *kb_dst, const struct KeyBlock *kb_src);
 char            *BKE_keyblock_curval_rnapath_get(struct Key *key, struct KeyBlock *kb);
+
 // needed for the GE
-void BKE_key_evaluate_relative(const int start, int end, const int tot, char *basispoin, struct Key *key, struct KeyBlock *actkb, const int mode);
+typedef struct WeightsArrayCache {
+	int num_defgroup_weights;
+	float **defgroup_weights;
+} WeightsArrayCache;
 
+float **BKE_keyblock_get_per_block_weights(struct Object *ob, struct Key *key, struct WeightsArrayCache *cache);
+void BKE_keyblock_free_per_block_weights(struct Key *key, float **per_keyblock_weights, struct WeightsArrayCache *cache);
+void BKE_key_evaluate_relative(const int start, int end, const int tot, char *basispoin, struct Key *key, struct KeyBlock *actkb,
+                               float **per_keyblock_weights, const int mode);
+
 /* conversion functions */
 void    BKE_key_convert_to_mesh(struct KeyBlock *kb, struct Mesh *me);
 void    BKE_key_convert_from_mesh(struct Mesh *me, struct KeyBlock *kb);

Modified: branches/soc-2013-depsgraph_mt/source/blender/blenkernel/intern/key.c
===================================================================
--- branches/soc-2013-depsgraph_mt/source/blender/blenkernel/intern/key.c	2013-07-31 21:51:05 UTC (rev 58786)
+++ branches/soc-2013-depsgraph_mt/source/blender/blenkernel/intern/key.c	2013-07-31 21:55:25 UTC (rev 58787)
@@ -733,12 +733,13 @@
 	}
 }
 
-void BKE_key_evaluate_relative(const int start, int end, const int tot, char *basispoin, Key *key, KeyBlock *actkb, const int mode)
+void BKE_key_evaluate_relative(const int start, int end, const int tot, char *basispoin, Key *key, KeyBlock *actkb,
+                               float **per_keyblock_weights, const int mode)
 {
 	KeyBlock *kb;
 	int *ofsp, ofs[3], elemsize, b;
 	char *cp, *poin, *reffrom, *from, elemstr[8];
-	int poinsize;
+	int poinsize, keyblock_index;
 
 	/* currently always 0, in future key_pointer_size may assign */
 	ofs[1] = 0;
@@ -762,14 +763,14 @@
 	
 	/* step 2: do it */
 	
-	for (kb = key->block.first; kb; kb = kb->next) {
+	for (kb = key->block.first, keyblock_index = 0; kb; kb = kb->next, keyblock_index++) {
 		if (kb != key->refkey) {
 			float icuval = kb->curval;
 			
 			/* only with value, and no difference allowed */
 			if (!(kb->flag & KEYBLOCK_MUTE) && icuval != 0.0f && kb->totelem == tot) {
 				KeyBlock *refb;
-				float weight, *weights = kb->weights;
+				float weight, *weights = per_keyblock_weights ? per_keyblock_weights[keyblock_index] : NULL;
 				char *freefrom = NULL, *freereffrom = NULL;
 
 				/* reference now can be any block */
@@ -1057,7 +1058,7 @@
 	if (freek4) MEM_freeN(freek4);
 }
 
-static float *get_weights_array(Object *ob, char *vgroup)
+static float *get_weights_array(Object *ob, char *vgroup, WeightsArrayCache *cache)
 {
 	MDeformVert *dvert = NULL;
 	BMEditMesh *em = NULL;
@@ -1090,7 +1091,21 @@
 	if (defgrp_index != -1) {
 		float *weights;
 		int i;
-		
+
+		if (cache) {
+			if (cache->defgroup_weights == NULL) {
+				int num_defgroup = BLI_countlist(&ob->defbase);
+				cache->defgroup_weights =
+				    MEM_callocN(sizeof(*cache->defgroup_weights) * num_defgroup,
+				                "cached defgroup weights");
+				cache->num_defgroup_weights = num_defgroup;
+			}
+
+			if (cache->defgroup_weights[defgrp_index]) {
+				return cache->defgroup_weights[defgrp_index];
+			}
+		}
+
 		weights = MEM_mallocN(totvert * sizeof(float), "weights");
 
 		if (em) {
@@ -1106,11 +1121,61 @@
 			}
 		}
 
+		if (cache) {
+			cache->defgroup_weights[defgrp_index] = weights;
+		}
+
 		return weights;
 	}
 	return NULL;
 }
 
+float **BKE_keyblock_get_per_block_weights(Object *ob, Key *key, WeightsArrayCache *cache)
+{
+	KeyBlock *keyblock;
+	float **per_keyblock_weights;
+	int keyblock_index;
+
+	per_keyblock_weights =
+		MEM_mallocN(sizeof(*per_keyblock_weights) * key->totkey,
+		            "per keyblock weights");
+
+	for (keyblock = key->block.first, keyblock_index = 0;
+	     keyblock;
+	     keyblock = keyblock->next, keyblock_index++)
+	{
+		per_keyblock_weights[keyblock_index] = get_weights_array(ob, keyblock->vgroup, cache);
+	}
+
+	return per_keyblock_weights;
+}
+
+void BKE_keyblock_free_per_block_weights(Key *key, float **per_keyblock_weights, WeightsArrayCache *cache)
+{
+	int a;
+
+	if (cache) {
+		if (cache->num_defgroup_weights) {
+			for (a = 0; a < cache->num_defgroup_weights; a++) {
+				if (cache->defgroup_weights[a]) {
+					MEM_freeN(cache->defgroup_weights[a]);
+				}
+			}
+			MEM_freeN(cache->defgroup_weights);
+		}
+		cache->defgroup_weights = NULL;
+	}
+	else {
+		for (a = 0; a < key->totkey; a++) {
+			if (per_keyblock_weights[a]) {
+				MEM_freeN(per_keyblock_weights[a]);
+			}
+		}
+	}
+
+	MEM_freeN(per_keyblock_weights);
+}
+
 static void do_mesh_key(Scene *scene, Object *ob, Key *key, char *out, const int tot)
 {
 	KeyBlock *k[4], *actkb = BKE_keyblock_from_object(ob);
@@ -1143,17 +1208,11 @@
 	}
 	else {
 		if (key->type == KEY_RELATIVE) {
-			KeyBlock *kb;
-			for (kb = key->block.first; kb; kb = kb->next) {
-				kb->weights = get_weights_array(ob, kb->vgroup);
-			}
-
-			BKE_key_evaluate_relative(0, tot, tot, (char *)out, key, actkb, KEY_MODE_DUMMY);
-			
-			for (kb = key->block.first; kb; kb = kb->next) {
-				if (kb->weights) MEM_freeN(kb->weights);
-				kb->weights = NULL;
-			}
+			WeightsArrayCache cache = {0, NULL};
+			float **per_keyblock_weights;
+			per_keyblock_weights = BKE_keyblock_get_per_block_weights(ob, key, &cache);
+			BKE_key_evaluate_relative(0, tot, tot, (char *)out, key, actkb, per_keyblock_weights, KEY_MODE_DUMMY);
+			BKE_keyblock_free_per_block_weights(key, per_keyblock_weights, &cache);
 		}
 		else {
 			const float ctime_scaled = key->ctime / 100.0f;
@@ -1196,11 +1255,11 @@
 	for (a = 0, nu = cu->nurb.first; nu; nu = nu->next, a += step) {
 		if (nu->bp) {
 			step = nu->pntsu * nu->pntsv;
-			BKE_key_evaluate_relative(a, a + step, tot, out, key, actkb, KEY_MODE_BPOINT);
+			BKE_key_evaluate_relative(a, a + step, tot, out, key, actkb, NULL, KEY_MODE_BPOINT);
 		}
 		else if (nu->bezt) {
 			step = 3 * nu->pntsu;
-			BKE_key_evaluate_relative(a, a + step, tot, out, key, actkb, KEY_MODE_BEZTRIPLE);
+			BKE_key_evaluate_relative(a, a + step, tot, out, key, actkb, NULL, KEY_MODE_BEZTRIPLE);
 		}
 		else {
 			step = 0;
@@ -1314,17 +1373,10 @@
 	}
 	else {
 		if (key->type == KEY_RELATIVE) {
-			KeyBlock *kb;
-			
-			for (kb = key->block.first; kb; kb = kb->next)
-				kb->weights = get_weights_array(ob, kb->vgroup);
-			
-			BKE_key_evaluate_relative(0, tot, tot, out, key, actkb, KEY_MODE_DUMMY);
-			
-			for (kb = key->block.first; kb; kb = kb->next) {
-				if (kb->weights) MEM_freeN(kb->weights);
-				kb->weights = NULL;
-			}
+			float **per_keyblock_weights;
+			per_keyblock_weights = BKE_keyblock_get_per_block_weights(ob, key, NULL);
+			BKE_key_evaluate_relative(0, tot, tot, (char *)out, key, actkb, per_keyblock_weights, KEY_MODE_DUMMY);
+			BKE_keyblock_free_per_block_weights(key, per_keyblock_weights, NULL);
 		}
 		else {
 			const float ctime_scaled = key->ctime / 100.0f;
@@ -1414,7 +1466,7 @@
 		}
 		
 		if (OB_TYPE_SUPPORT_VGROUP(ob->type)) {
-			float *weights = get_weights_array(ob, kb->vgroup);
+			float *weights = get_weights_array(ob, kb->vgroup, NULL);
 
 			cp_key(0, tot, tot, out, key, actkb, kb, weights, 0);
 

Modified: branches/soc-2013-depsgraph_mt/source/blender/editors/mesh/meshtools.c
===================================================================
--- branches/soc-2013-depsgraph_mt/source/blender/editors/mesh/meshtools.c	2013-07-31 21:51:05 UTC (rev 58786)
+++ branches/soc-2013-depsgraph_mt/source/blender/editors/mesh/meshtools.c	2013-07-31 21:55:25 UTC (rev 58787)
@@ -178,7 +178,6 @@
 			if (kb->data) MEM_freeN(kb->data);
 			kb->data = MEM_callocN(sizeof(float) * 3 * totvert, "join_shapekey");
 			kb->totelem = totvert;
-			kb->weights = NULL;
 		}
 	}
 	else if (haskey) {

Modified: branches/soc-2013-depsgraph_mt/source/blender/makesdna/DNA_key_types.h
===================================================================
--- branches/soc-2013-depsgraph_mt/source/blender/makesdna/DNA_key_types.h	2013-07-31 21:51:05 UTC (rev 58786)
+++ branches/soc-2013-depsgraph_mt/source/blender/makesdna/DNA_key_types.h	2013-07-31 21:55:25 UTC (rev 58787)
@@ -60,7 +60,6 @@
 	int uid;           /* for meshes only, match the unique number with the customdata layer */
 	
 	void  *data;       /* array of shape key values, size is (Key->elemsize * KeyBlock->totelem) */
-	float *weights;    /* store an aligned array of weights from 'vgroup' */
 	char   name[64];   /* MAX_NAME (unique name, user assigned) */
 	char   vgroup[64]; /* MAX_VGROUP_NAME (optional vertex group), array gets allocated into 'weights' when set */
 

Modified: branches/soc-2013-depsgraph_mt/source/gameengine/Converter/BL_ShapeDeformer.cpp
===================================================================
--- branches/soc-2013-depsgraph_mt/source/gameengine/Converter/BL_ShapeDeformer.cpp	2013-07-31 21:51:05 UTC (rev 58786)
+++ branches/soc-2013-depsgraph_mt/source/gameengine/Converter/BL_ShapeDeformer.cpp	2013-07-31 21:55:25 UTC (rev 58787)
@@ -159,16 +159,20 @@
 		/* the key coefficient have been set already, we just need to blend the keys */
 		Object* blendobj = m_gameobj->GetBlendObject();
 		
-		// make sure the vertex weight cache is in line with this object
-		m_pMeshObject->CheckWeightCache(blendobj);
-
 		/* we will blend the key directly in m_transverts array: it is used by armature as the start position */
 		/* m_key can be NULL in case of Modifier deformer */
 		if (m_key) {
+			WeightsArrayCache cache = {0, NULL};
+			float **per_keyblock_weights;
+
 			/* store verts locally */
 			VerifyStorage();
 

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list