diff options
author | Sergey Sharybin <sergey.vfx@gmail.com> | 2013-08-19 14:36:39 +0400 |
---|---|---|
committer | Sergey Sharybin <sergey.vfx@gmail.com> | 2013-08-19 14:36:39 +0400 |
commit | 9f7961b6b10203733ddda62dac3170d6817ff69b (patch) | |
tree | 1ce6e57d9996290de4962dcd29524f2edef22265 /source/blender/blenkernel | |
parent | bec9bcc14c4e763b8f82d22dd7e1741b40155b3c (diff) |
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
Diffstat (limited to 'source/blender/blenkernel')
-rw-r--r-- | source/blender/blenkernel/BKE_key.h | 12 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/key.c | 114 |
2 files changed, 94 insertions, 32 deletions
diff --git a/source/blender/blenkernel/BKE_key.h b/source/blender/blenkernel/BKE_key.h index de60d316426..19264feaf48 100644 --- a/source/blender/blenkernel/BKE_key.h +++ b/source/blender/blenkernel/BKE_key.h @@ -41,6 +41,7 @@ struct Object; struct Scene; struct Lattice; struct Mesh; +struct WeightsArrayCache; /* Kernel prototypes */ #ifdef __cplusplus @@ -73,8 +74,17 @@ struct KeyBlock *BKE_keyblock_from_key(struct Key *key, int index); 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); diff --git a/source/blender/blenkernel/intern/key.c b/source/blender/blenkernel/intern/key.c index d2d2cb1c2d0..aaac17ac1ed 100644 --- a/source/blender/blenkernel/intern/key.c +++ b/source/blender/blenkernel/intern/key.c @@ -734,12 +734,13 @@ static void cp_cu_key(Curve *cu, Key *key, KeyBlock *actkb, KeyBlock *kb, const } } -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 @@ void BKE_key_evaluate_relative(const int start, int end, const int tot, char *ba /* 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 @@ static void do_key(const int start, int end, const int tot, char *poin, Key *key 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 @@ static float *get_weights_array(Object *ob, char *vgroup) 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 @@ static float *get_weights_array(Object *ob, char *vgroup) } } + 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 @@ static void do_mesh_key(Scene *scene, Object *ob, Key *key, char *out, const int } 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 @@ static void do_rel_cu_key(Curve *cu, Key *key, KeyBlock *actkb, char *out, const 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 @@ static void do_latt_key(Scene *scene, Object *ob, Key *key, char *out, const int } 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 @@ float *BKE_key_evaluate_object_ex(Scene *scene, Object *ob, int *r_totelem, } 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); |