main sync #3

Merged
Patrick Busch merged 318 commits from blender/blender:main into main 2023-03-17 15:52:21 +01:00
Showing only changes of commit 8bf4aa2fdc - Show all commits

View File

@ -5,6 +5,9 @@
* \ingroup depsgraph * \ingroup depsgraph
*/ */
#include <memory>
#include <mutex>
#include "intern/depsgraph_registry.h" #include "intern/depsgraph_registry.h"
#include "BLI_utildefines.h" #include "BLI_utildefines.h"
@ -13,7 +16,19 @@
namespace blender::deg { namespace blender::deg {
using GraphRegistry = Map<Main *, VectorSet<Depsgraph *>>; /* Global registry for dependency graphs associated with a main database.
*
* Threads may add or remove depsgraphs for different mains concurrently
* (for example for preview rendering), but not the same main. */
/* Use pointer for map value to ensure span returned by get_all_registered_graphs
* remains unchanged as other mains are added or removed. */
typedef std::unique_ptr<VectorSet<Depsgraph *>> GraphSetPtr;
struct GraphRegistry {
Map<Main *, GraphSetPtr> map;
std::mutex mutex;
};
static GraphRegistry &get_graph_registry() static GraphRegistry &get_graph_registry()
{ {
static GraphRegistry graph_registry; static GraphRegistry graph_registry;
@ -22,28 +37,37 @@ static GraphRegistry &get_graph_registry()
void register_graph(Depsgraph *depsgraph) void register_graph(Depsgraph *depsgraph)
{ {
GraphRegistry &graph_registry = get_graph_registry();
Main *bmain = depsgraph->bmain; Main *bmain = depsgraph->bmain;
get_graph_registry().lookup_or_add_default(bmain).add_new(depsgraph);
std::lock_guard<std::mutex> lock{graph_registry.mutex};
graph_registry.map
.lookup_or_add_cb(bmain, []() { return std::make_unique<VectorSet<Depsgraph *>>(); })
->add_new(depsgraph);
} }
void unregister_graph(Depsgraph *depsgraph) void unregister_graph(Depsgraph *depsgraph)
{ {
Main *bmain = depsgraph->bmain; Main *bmain = depsgraph->bmain;
GraphRegistry &graph_registry = get_graph_registry(); GraphRegistry &graph_registry = get_graph_registry();
VectorSet<Depsgraph *> &graphs = graph_registry.lookup(bmain);
graphs.remove(depsgraph); std::lock_guard<std::mutex> lock{graph_registry.mutex};
GraphSetPtr &graphs = graph_registry.map.lookup(bmain);
graphs->remove(depsgraph);
/* If this was the last depsgraph associated with the main, remove the main entry as well. */ /* If this was the last depsgraph associated with the main, remove the main entry as well. */
if (graphs.is_empty()) { if (graphs->is_empty()) {
graph_registry.remove(bmain); graph_registry.map.remove(bmain);
} }
} }
Span<Depsgraph *> get_all_registered_graphs(Main *bmain) Span<Depsgraph *> get_all_registered_graphs(Main *bmain)
{ {
VectorSet<Depsgraph *> *graphs = get_graph_registry().lookup_ptr(bmain); GraphRegistry &graph_registry = get_graph_registry();
if (graphs != nullptr) { std::lock_guard<std::mutex> lock{graph_registry.mutex};
return *graphs; GraphSetPtr *graphs = graph_registry.map.lookup_ptr(bmain);
if (graphs) {
return **graphs;
} }
return {}; return {};
} }