Commit: 7a57a4f871d99ba495ae1036d9b6fe870580d865
Author: Lukas Tönne
Date:   Mon Apr 4 19:28:04 2016 +0200
Branches: object_nodes
https://developer.blender.org/rB7a57a4f871d99ba495ae1036d9b6fe870580d865

Use function caching in the same way for BVM as for LLVM.

The previous way of acquiring functions from the cache was actually not 
threadsafe.
To do this efficiently the cache locking, lookup and potential update is now 
done
inside the API functions to prevent race conditions on the cache.

===================================================================

M       source/blender/blenkernel/intern/DerivedMesh.c
M       source/blender/blenkernel/intern/depsgraph.c
M       source/blender/blenkernel/intern/effect.c
M       source/blender/blenkernel/intern/object_dupli.c
M       source/blender/blenvm/BVM_api.h
M       source/blender/blenvm/intern/bvm_api.cc

===================================================================

diff --git a/source/blender/blenkernel/intern/DerivedMesh.c 
b/source/blender/blenkernel/intern/DerivedMesh.c
index 6db7a80..5801561 100644
--- a/source/blender/blenkernel/intern/DerivedMesh.c
+++ b/source/blender/blenkernel/intern/DerivedMesh.c
@@ -1701,25 +1701,19 @@ static DerivedMesh *mesh_calc_modifier_nodes(Scene 
*UNUSED(scene), Object *ob, b
        Mesh *me = ob->data;
        DerivedMesh *dm, *result;
        
-       struct BVMFunction *fn = BVM_function_bvm_cache_acquire(ntree);
-       if (!fn) {
-               fn = BVM_gen_modifier_function_bvm(ntree);
-               BVM_function_bvm_cache_set(ntree, fn);
-       }
-       
-       {
+       struct BVMFunction *fn = BVM_gen_modifier_function_bvm(ntree, true);
+       if (fn) {
                struct BVMEvalGlobals *globals = BVM_globals_create();
                BVM_globals_add_nodetree_relations(globals, ntree);
                
                struct BVMEvalContext *context = BVM_context_create();
                dm = BVM_eval_modifier_bvm(globals, context, fn, ob, me);
-               BVM_context_free(context);
                
+               BVM_context_free(context);
                BVM_globals_free(globals);
+               BVM_function_bvm_release(fn);
        }
        
-       BVM_function_bvm_cache_release(fn);
-       
        /* XXX this is stupid, but currently required because of
         * the unreliability of dm->needsFree ...
         * This flag gets set in places to force freeing of meshes, can't 
expect this to work
diff --git a/source/blender/blenkernel/intern/depsgraph.c 
b/source/blender/blenkernel/intern/depsgraph.c
index aaed17f..1b8158c 100644
--- a/source/blender/blenkernel/intern/depsgraph.c
+++ b/source/blender/blenkernel/intern/depsgraph.c
@@ -2672,7 +2672,7 @@ static void dag_id_flush_update(Main *bmain, Scene *sce, 
ID *id)
 
                if (ELEM(idtype, ID_TE)) {
                        Tex *tex = (Tex *)id;
-                       BVM_function_bvm_cache_remove(tex->nodetree);
+                       BVM_function_llvm_cache_remove(tex->nodetree);
                }
 
                if (idtype == ID_MC) {
diff --git a/source/blender/blenkernel/intern/effect.c 
b/source/blender/blenkernel/intern/effect.c
index cc51502..714b356 100644
--- a/source/blender/blenkernel/intern/effect.c
+++ b/source/blender/blenkernel/intern/effect.c
@@ -215,7 +215,7 @@ static void add_object_nodes_to_effectors(EffectorContext 
*effctx, Scene *scene,
                                        
BVM_globals_add_nodetree_relations(effctx->eval_globals, ff_ntree);
                                        
                                        EffectorCache *eff = 
new_effector_cache(effctx, scene, ob, NULL, ob->pd);
-                                       eff->function = 
BVM_gen_forcefield_function_bvm(ff_ntree);
+                                       eff->function = 
BVM_gen_forcefield_function_bvm(ff_ntree, true);
                                }
                                
                                break;
@@ -286,7 +286,7 @@ void pdEndEffectors(EffectorContext *effctx)
                        if (eff->guide_data)
                                MEM_freeN(eff->guide_data);
                        if (eff->function)
-                               BVM_function_bvm_free(eff->function);
+                               BVM_function_bvm_release(eff->function);
                }
                
                BLI_freelistN(&effctx->effectors);
diff --git a/source/blender/blenkernel/intern/object_dupli.c 
b/source/blender/blenkernel/intern/object_dupli.c
index 773e23b..080a3f0 100644
--- a/source/blender/blenkernel/intern/object_dupli.c
+++ b/source/blender/blenkernel/intern/object_dupli.c
@@ -1153,13 +1153,9 @@ const DupliGenerator gen_dupli_particles = {
 
 static void make_duplis_nodetree(struct bNodeTree *ntree, const DupliContext 
*dupctx)
 {
-       struct BVMFunction *fn = BVM_function_bvm_cache_acquire(ntree);
-       if (!fn) {
-               fn = BVM_gen_dupli_function_bvm(ntree);
-               BVM_function_bvm_cache_set(ntree, fn);
-       }
+       struct BVMFunction *fn = BVM_gen_dupli_function_bvm(ntree, true);
        
-       {
+       if (fn) {
                struct BVMEvalGlobals *globals = BVM_globals_create();
                BVM_globals_add_nodetree_relations(globals, ntree);
                
@@ -1168,9 +1164,8 @@ static void make_duplis_nodetree(struct bNodeTree *ntree, 
const DupliContext *du
                BVM_context_free(context);
                
                BVM_globals_free(globals);
+               BVM_function_bvm_release(fn);
        }
-       
-       BVM_function_bvm_cache_release(fn);
 }
 
 static void make_duplis_nodes(const DupliContext *ctx)
diff --git a/source/blender/blenvm/BVM_api.h b/source/blender/blenvm/BVM_api.h
index 10cb383..c0e7064 100644
--- a/source/blender/blenvm/BVM_api.h
+++ b/source/blender/blenvm/BVM_api.h
@@ -115,22 +115,18 @@ typedef enum BVMDebugMode {
 
 /* ------------------------------------------------------------------------- */
 
-void BVM_function_bvm_free(struct BVMFunction *fn);
-
-struct BVMFunction *BVM_function_bvm_cache_acquire(void *key);
-void BVM_function_bvm_cache_release(struct BVMFunction *fn);
-void BVM_function_bvm_cache_set(void *key, struct BVMFunction *fn);
+void BVM_function_bvm_release(struct BVMFunction *fn);
 void BVM_function_bvm_cache_remove(void *key);
-void BVM_function_bvm_cache_clear(void);
 
 void BVM_function_llvm_release(struct BVMFunction *fn);
+void BVM_function_llvm_cache_remove(void *key);
 
 /* ------------------------------------------------------------------------- */
 
 struct Object;
 struct EffectedPoint;
 
-struct BVMFunction *BVM_gen_forcefield_function_bvm(struct bNodeTree *btree);
+struct BVMFunction *BVM_gen_forcefield_function_bvm(struct bNodeTree *btree, 
bool use_cache);
 void BVM_debug_forcefield_nodes(struct bNodeTree *btree, FILE *debug_file, 
const char *label, BVMDebugMode mode);
 
 void BVM_eval_forcefield_bvm(struct BVMEvalGlobals *globals, struct 
BVMEvalContext *context, struct BVMFunction *fn,
@@ -160,7 +156,7 @@ void BVM_eval_texture_llvm(struct BVMEvalContext *context, 
struct BVMFunction *f
 struct DerivedMesh;
 struct Mesh;
 
-struct BVMFunction *BVM_gen_modifier_function_bvm(struct bNodeTree *btree);
+struct BVMFunction *BVM_gen_modifier_function_bvm(struct bNodeTree *btree, 
bool use_cache);
 void BVM_debug_modifier_nodes(struct bNodeTree *btree, FILE *debug_file, const 
char *label, BVMDebugMode mode);
 
 struct DerivedMesh *BVM_eval_modifier_bvm(struct BVMEvalGlobals *globals,
@@ -173,7 +169,7 @@ struct DerivedMesh *BVM_eval_modifier_bvm(struct 
BVMEvalGlobals *globals,
 
 struct DupliContainer;
 
-struct BVMFunction *BVM_gen_dupli_function_bvm(struct bNodeTree *btree);
+struct BVMFunction *BVM_gen_dupli_function_bvm(struct bNodeTree *btree, bool 
use_cache);
 void BVM_debug_dupli_nodes(struct bNodeTree *btree, FILE *debug_file, const 
char *label, BVMDebugMode mode);
 
 void BVM_eval_dupli_bvm(struct BVMEvalGlobals *globals,
diff --git a/source/blender/blenvm/intern/bvm_api.cc 
b/source/blender/blenvm/intern/bvm_api.cc
index 6cc5287..cc8a3ad 100644
--- a/source/blender/blenvm/intern/bvm_api.cc
+++ b/source/blender/blenvm/intern/bvm_api.cc
@@ -97,7 +97,8 @@ void BVM_free(void)
 {
        using namespace blenvm;
        
-       BVM_function_bvm_cache_clear();
+       blenvm::function_bvm_cache_clear();
+       blenvm::function_llvm_cache_clear();
        
        nodes_free();
        
@@ -327,32 +328,20 @@ void BVM_context_free(struct BVMEvalContext *ctx)
 BLI_INLINE blenvm::FunctionBVM *_FUNC_BVM(struct BVMFunction *fn)
 { return (blenvm::FunctionBVM *)fn; }
 
-void BVM_function_bvm_free(struct BVMFunction *fn)
-{ delete _FUNC_BVM(fn); }
-
-struct BVMFunction *BVM_function_bvm_cache_acquire(void *key)
-{
-       return (BVMFunction *)blenvm::function_bvm_cache_acquire(key);
-}
+static blenvm::spin_lock bvm_lock = blenvm::spin_lock();
 
-void BVM_function_bvm_cache_release(BVMFunction *fn)
+void BVM_function_bvm_release(BVMFunction *fn)
 {
+       bvm_lock.lock();
        blenvm::function_bvm_cache_release(_FUNC_BVM(fn));
-}
-
-void BVM_function_bvm_cache_set(void *key, BVMFunction *fn)
-{
-       blenvm::function_bvm_cache_set(key, _FUNC_BVM(fn));
+       bvm_lock.unlock();
 }
 
 void BVM_function_bvm_cache_remove(void *key)
 {
+       bvm_lock.lock();
        blenvm::function_bvm_cache_remove(key);
-}
-
-void BVM_function_bvm_cache_clear(void)
-{
-       blenvm::function_bvm_cache_clear();
+       bvm_lock.unlock();
 }
 
 #ifdef WITH_LLVM
@@ -367,8 +356,16 @@ void BVM_function_llvm_release(BVMFunction *fn)
        blenvm::function_llvm_cache_release(_FUNC_LLVM(fn));
        llvm_lock.unlock();
 }
+
+void BVM_function_llvm_cache_remove(void *key)
+{
+       llvm_lock.lock();
+       blenvm::function_llvm_cache_remove(key);
+       llvm_lock.unlock();
+}
 #else
 void BVM_function_llvm_release(BVMFunction */*fn*/) {}
+void BVM_function_llvm_cache_remove(void */*key*/) {}
 #endif
 
 /* ------------------------------------------------------------------------- */
@@ -413,7 +410,7 @@ static void init_forcefield_graph(blenvm::NodeGraph &graph)
        graph.add_output("impulse", "FLOAT3", zero);
 }
 
-struct BVMFunction *BVM_gen_forcefield_function_bvm(bNodeTree *btree)
+struct BVMFunction *BVM_gen_forcefield_function_bvm(bNodeTree *btree, bool 
use_cache)
 {
        using namespace blenvm;
        
@@ -1111,7 +1108,7 @@ static void init_modifier_graph(blenvm::NodeGraph &graph)
        graph.add_output("mesh", "MESH", __empty_mesh__);
 }
 
-struct BVMFunction *BVM_gen_modifier_function_bvm(struct bNodeTree *btree)
+struct BVMFunction *BVM_gen_modifier_function_bvm(struct bNodeTree *btree, 
bool use_cache)
 {
        using namespace blenvm;
        
@@ -1185,7 +1182,7 @@ static void init_dupli_graph(blenvm::NodeGraph &graph)
        graph.add_output("dupli.result", "DUPLIS", __empty_duplilist__);
 }
 
-struct BVMFunction *BVM_gen_dupli_function_bvm(struct bNodeTree *btree)
+struct BVMFunction *BVM_gen_dupli_function_bvm(struct bNodeTree *btree, bool 
use_cache)
 {
        using namespace blenvm;

_______________________________________________
Bf-blender-cvs mailing list
[email protected]
https://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to