[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [59277] trunk/blender: Optimization and threading fix for shapekeys weights calculation

Sergey Sharybin sergey.vfx at gmail.com
Mon Aug 19 12:36:39 CEST 2013


Revision: 59277
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=59277
Author:   nazgul
Date:     2013-08-19 10:36:39 +0000 (Mon, 19 Aug 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, speedup measured in depsgraph_mt branch,
trunk might be not so much high speedup).

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.

--
svn merge -r58786:58787  ^/branches/soc-2013-depsgraph_mt

Revision Links:
--------------
    http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=58786

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

Property Changed:
----------------
    trunk/blender/
    trunk/blender/source/blender/editors/interface/interface.c
    trunk/blender/source/blender/editors/space_outliner/


Property changes on: trunk/blender
___________________________________________________________________
Modified: svn:mergeinfo
   - /branches/ge_dev:58091-58422
/branches/ge_harmony:42255,42279-42282,42286,42302,42338,42349,42616,42620,42698-42699,42739,42753,42773-42774,42832,44568,44597-44598,44793-44794
/branches/soc-2011-cucumber:37517,38166-38167,38177,38179-38180,38187,38242,38384,38387,38403-38404,38407,38968,38970,38973,39045,40845,42997-42998,43439
/branches/soc-2011-tomato:42376,42378-42379,42383,42385,42395,42397-42400,42407,42411,42418,42443-42444,42446,42467,42472,42486,42650-42652,42654-42655,42709-42710,42733-42734,42801,43872,44130,44141,44147-44149,44151-44152,44229-44230,45623-45625,46037,48089,48092,48551-48552,48679,48790,48792-48793,49076,49087,49292,49294,49466,49894,50052,50126,52854-52856,54573,58822,58989
/branches/soc-2013-depsgraph_mt:57516,57900,57939-57940,57945,57958-57961,58151,58155-58156,58169,58277-58279,58282-58283,58711,58796,59259
/branches/soc-2013-dingto:57424,57487,57507,57525,57599,57670,57918-57919,57981,58091,58245,58253,58587,58772,58774-58775,58828,58835,59032
/tags/blender-2.67b-release/blender:57122
   + /branches/ge_dev:58091-58422
/branches/ge_harmony:42255,42279-42282,42286,42302,42338,42349,42616,42620,42698-42699,42739,42753,42773-42774,42832,44568,44597-44598,44793-44794
/branches/soc-2011-cucumber:37517,38166-38167,38177,38179-38180,38187,38242,38384,38387,38403-38404,38407,38968,38970,38973,39045,40845,42997-42998,43439
/branches/soc-2011-tomato:42376,42378-42379,42383,42385,42395,42397-42400,42407,42411,42418,42443-42444,42446,42467,42472,42486,42650-42652,42654-42655,42709-42710,42733-42734,42801,43872,44130,44141,44147-44149,44151-44152,44229-44230,45623-45625,46037,48089,48092,48551-48552,48679,48790,48792-48793,49076,49087,49292,49294,49466,49894,50052,50126,52854-52856,54573,58822,58989
/branches/soc-2013-depsgraph_mt:57516,57900,57939-57940,57945,57958-57961,58151,58155-58156,58169,58277-58279,58282-58283,58711,58787,58796,59259
/branches/soc-2013-dingto:57424,57487,57507,57525,57599,57670,57918-57919,57981,58091,58245,58253,58587,58772,58774-58775,58828,58835,59032
/tags/blender-2.67b-release/blender:57122

Modified: trunk/blender/source/blender/blenkernel/BKE_key.h
===================================================================
--- trunk/blender/source/blender/blenkernel/BKE_key.h	2013-08-19 10:18:25 UTC (rev 59276)
+++ trunk/blender/source/blender/blenkernel/BKE_key.h	2013-08-19 10:36:39 UTC (rev 59277)
@@ -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: trunk/blender/source/blender/blenkernel/intern/key.c
===================================================================
--- trunk/blender/source/blender/blenkernel/intern/key.c	2013-08-19 10:18:25 UTC (rev 59276)
+++ trunk/blender/source/blender/blenkernel/intern/key.c	2013-08-19 10:36:39 UTC (rev 59277)
@@ -734,12 +734,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;
@@ -763,14 +764,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 */
@@ -1058,7 +1059,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;
@@ -1091,7 +1092,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) {
@@ -1107,11 +1122,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);
@@ -1144,17 +1209,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;
@@ -1197,11 +1256,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;
@@ -1315,17 +1374,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;
@@ -1415,7 +1467,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);
 


Property changes on: trunk/blender/source/blender/editors/interface/interface.c
___________________________________________________________________
Modified: svn:mergeinfo
   - /branches/ge_candy/source/blender/editors/interface/interface.c:45070-46163

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list