From 3bc09c1c1ec9c1699ac872bc643d3e38c250803f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Fri, 21 Aug 2020 11:56:03 +0200 Subject: Cleanup: split `BKE_scene_get_depsgraph()` into two functions Split the depsgraph allocation into a separate function `BKE_scene_ensure_depsgraph()`. Parameters are only passed to those functions that actually need them. This removes the the "if that boolean is `false` this pointer is allowed to be `NULL`" logic and more cleanly decouples code. No functional changes. --- source/blender/blenkernel/intern/context.c | 5 ++- source/blender/blenkernel/intern/scene.c | 54 ++++++++++++++++++---------- source/blender/blenkernel/intern/sequencer.c | 2 +- 3 files changed, 38 insertions(+), 23 deletions(-) (limited to 'source/blender/blenkernel/intern') diff --git a/source/blender/blenkernel/intern/context.c b/source/blender/blenkernel/intern/context.c index e9ba3a5f873..dce14c4c082 100644 --- a/source/blender/blenkernel/intern/context.c +++ b/source/blender/blenkernel/intern/context.c @@ -1349,7 +1349,7 @@ Depsgraph *CTX_data_depsgraph_pointer(const bContext *C) Main *bmain = CTX_data_main(C); Scene *scene = CTX_data_scene(C); ViewLayer *view_layer = CTX_data_view_layer(C); - Depsgraph *depsgraph = BKE_scene_get_depsgraph(bmain, scene, view_layer, true); + Depsgraph *depsgraph = BKE_scene_ensure_depsgraph(bmain, scene, view_layer); /* Dependency graph might have been just allocated, and hence it will not be marked. * This confuses redo system due to the lack of flushing changes back to the original data. * In the future we would need to check whether the CTX_wm_window(C) is in editing mode (as an @@ -1377,8 +1377,7 @@ Depsgraph *CTX_data_ensure_evaluated_depsgraph(const bContext *C) Depsgraph *CTX_data_depsgraph_on_load(const bContext *C) { - Main *bmain = CTX_data_main(C); Scene *scene = CTX_data_scene(C); ViewLayer *view_layer = CTX_data_view_layer(C); - return BKE_scene_get_depsgraph(bmain, scene, view_layer, false); + return BKE_scene_get_depsgraph(scene, view_layer); } diff --git a/source/blender/blenkernel/intern/scene.c b/source/blender/blenkernel/intern/scene.c index 552076f87ca..d11cc2289c8 100644 --- a/source/blender/blenkernel/intern/scene.c +++ b/source/blender/blenkernel/intern/scene.c @@ -1609,7 +1609,7 @@ void BKE_scene_graph_update_for_newframe(Depsgraph *depsgraph) */ void BKE_scene_view_layer_graph_evaluated_ensure(Main *bmain, Scene *scene, ViewLayer *view_layer) { - Depsgraph *depsgraph = BKE_scene_get_depsgraph(bmain, scene, view_layer, true); + Depsgraph *depsgraph = BKE_scene_ensure_depsgraph(bmain, scene, view_layer); DEG_make_active(depsgraph); BKE_scene_graph_update_tagged(depsgraph, bmain); } @@ -2243,11 +2243,9 @@ void BKE_scene_free_view_layer_depsgraph(Scene *scene, ViewLayer *view_layer) /* Query depsgraph for a specific contexts. */ -static Depsgraph **scene_get_depsgraph_p(Main *bmain, - Scene *scene, +static Depsgraph **scene_get_depsgraph_p(Scene *scene, ViewLayer *view_layer, - const bool allocate_ghash_entry, - const bool allocate_depsgraph) + const bool allocate_ghash_entry) { /* bmain may be NULL here! */ BLI_assert(scene != NULL); @@ -2261,9 +2259,7 @@ static Depsgraph **scene_get_depsgraph_p(Main *bmain, if (scene->depsgraph_hash == NULL) { return NULL; } - /* Either ensure item is in the hash or simply return NULL if it's not, - * depending on whether caller wants us to create depsgraph or not. - */ + DepsgraphKey key; key.view_layer = view_layer; @@ -2276,20 +2272,35 @@ static Depsgraph **scene_get_depsgraph_p(Main *bmain, DepsgraphKey **key_ptr; if (BLI_ghash_ensure_p_ex( scene->depsgraph_hash, &key, (void ***)&key_ptr, (void ***)&depsgraph_ptr)) { - /* Depsgraph was found in the ghash. */ - return depsgraph_ptr; - } - - if (!allocate_depsgraph) { - /* Not found and not allowed to allocate. */ - *depsgraph_ptr = NULL; return depsgraph_ptr; } + /* Depsgraph was not found in the ghash, but the key still needs allocating. */ *key_ptr = MEM_mallocN(sizeof(DepsgraphKey), __func__); **key_ptr = key; + *depsgraph_ptr = NULL; + return depsgraph_ptr; +} + +static Depsgraph **scene_ensure_depsgraph_p(Main *bmain, Scene *scene, ViewLayer *view_layer) +{ + BLI_assert(bmain != NULL); + + Depsgraph **depsgraph_ptr = scene_get_depsgraph_p(scene, view_layer, true); + if (depsgraph_ptr == NULL) { + /* The scene has no depsgraph hash. */ + return NULL; + } + if (*depsgraph_ptr != NULL) { + /* The depsgraph was found, no need to allocate. */ + return depsgraph_ptr; + } + + /* Allocate a new depsgraph. scene_get_depsgraph_p() already ensured that the pointer is stored + * in the scene's depsgraph hash. */ *depsgraph_ptr = DEG_graph_new(bmain, scene, view_layer, DAG_EVAL_VIEWPORT); + /* TODO(sergey): Would be cool to avoid string format print, * but is a bit tricky because we can't know in advance whether * we will ever enable debug messages for this depsgraph. @@ -2301,9 +2312,15 @@ static Depsgraph **scene_get_depsgraph_p(Main *bmain, return depsgraph_ptr; } -Depsgraph *BKE_scene_get_depsgraph(Main *bmain, Scene *scene, ViewLayer *view_layer, bool allocate) +Depsgraph *BKE_scene_get_depsgraph(Scene *scene, ViewLayer *view_layer) +{ + Depsgraph **depsgraph_ptr = scene_get_depsgraph_p(scene, view_layer, false); + return (depsgraph_ptr != NULL) ? *depsgraph_ptr : NULL; +} + +Depsgraph *BKE_scene_ensure_depsgraph(Main *bmain, Scene *scene, ViewLayer *view_layer) { - Depsgraph **depsgraph_ptr = scene_get_depsgraph_p(bmain, scene, view_layer, allocate, allocate); + Depsgraph **depsgraph_ptr = scene_ensure_depsgraph_p(bmain, scene, view_layer); return (depsgraph_ptr != NULL) ? *depsgraph_ptr : NULL; } @@ -2369,8 +2386,7 @@ void BKE_scene_undo_depsgraphs_restore(Main *bmain, GHash *depsgraph_extract) } BLI_assert(*depsgraph_extract_ptr != NULL); - Depsgraph **depsgraph_scene_ptr = scene_get_depsgraph_p( - bmain, scene, view_layer, true, false); + Depsgraph **depsgraph_scene_ptr = scene_get_depsgraph_p(scene, view_layer, true); BLI_assert(depsgraph_scene_ptr != NULL); BLI_assert(*depsgraph_scene_ptr == NULL); diff --git a/source/blender/blenkernel/intern/sequencer.c b/source/blender/blenkernel/intern/sequencer.c index b4da0c5bd33..3e75f09fed6 100644 --- a/source/blender/blenkernel/intern/sequencer.c +++ b/source/blender/blenkernel/intern/sequencer.c @@ -3592,7 +3592,7 @@ static ImBuf *seq_render_scene_strip(const SeqRenderData *context, } /* opengl offscreen render */ - depsgraph = BKE_scene_get_depsgraph(context->bmain, scene, view_layer, true); + depsgraph = BKE_scene_ensure_depsgraph(context->bmain, scene, view_layer); BKE_scene_graph_update_for_newframe(depsgraph); ibuf = sequencer_view3d_fn( /* set for OpenGL render (NULL when scrubbing) */ -- cgit v1.2.3