From 429afe0c626a6d608385c6bc3a348b3ac8cfa8c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dietrich?= Date: Sun, 30 Aug 2020 23:20:51 +0200 Subject: Cycles: introduce an ownership system to protect nodes from unwanted deletions. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Problem: the Blender synchronization process creates and tags nodes for usage. It does this by directly adding and removing nodes from the scene data. If some node is not tagged as used at the end of a synchronization, it then deletes the node from the scene. This poses a problem when it comes to supporting procedural nodes who can create other nodes not known by the Blender synchonization system, which will remove them. Nodes now have a NodeOwner, which is set after creation. Those owners for now are the Scene for scene level nodes and ShaderGraph for shader nodes. Instead of creating and deleting nodes using `new` and `delete` explicitely, we now use `create_node` and `delete_node` methods found on the owners. `delete_node` will assert that the owner is the right one. Whenever a scene level node is created or deleted, the appropriate node manager is tagged for an update, freeing this responsability from BlenderSync or other software exporters. Concerning BlenderSync, the `id_maps` do not explicitely manipulate scene data anymore, they only keep track of which nodes are used, employing the scene to create and delete them. To achieve this, the ParticleSystem is now a Node, although it does not have any sockets. This is part of T79131. Reviewed By: #cycles, brecht Maniphest Tasks: T79131 Differential Revision: https://developer.blender.org/D8540 --- intern/cycles/render/graph.cpp | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) (limited to 'intern/cycles/render/graph.cpp') diff --git a/intern/cycles/render/graph.cpp b/intern/cycles/render/graph.cpp index 1b138455515..684fe6a82c4 100644 --- a/intern/cycles/render/graph.cpp +++ b/intern/cycles/render/graph.cpp @@ -221,7 +221,7 @@ ShaderGraph::ShaderGraph() finalized = false; simplified = false; num_node_ids = 0; - add(new OutputNode()); + add(create_node()); } ShaderGraph::~ShaderGraph() @@ -272,7 +272,7 @@ void ShaderGraph::connect(ShaderOutput *from, ShaderInput *to) ShaderInput *convert_in; if (to->type() == SocketType::CLOSURE) { - EmissionNode *emission = new EmissionNode(); + EmissionNode *emission = create_node(); emission->color = make_float3(1.0f, 1.0f, 1.0f); emission->strength = 1.0f; convert = add(emission); @@ -285,7 +285,7 @@ void ShaderGraph::connect(ShaderOutput *from, ShaderInput *to) } } else { - convert = add(new ConvertNode(from->type(), to->type(), true)); + convert = add(create_node(from->type(), to->type(), true)); convert_in = convert->inputs[0]; } @@ -416,7 +416,7 @@ void ShaderGraph::find_dependencies(ShaderNodeSet &dependencies, ShaderInput *in void ShaderGraph::clear_nodes() { foreach (ShaderNode *node, nodes) { - delete node; + delete_node(node); } nodes.clear(); } @@ -428,7 +428,7 @@ void ShaderGraph::copy_nodes(ShaderNodeSet &nodes, ShaderNodeMap &nnodemap) /* copy nodes */ foreach (ShaderNode *node, nodes) { - ShaderNode *nnode = node->clone(); + ShaderNode *nnode = node->clone(this); nnodemap[node] = nnode; /* create new inputs and outputs to recreate links and ensure @@ -523,7 +523,7 @@ void ShaderGraph::remove_proxy_nodes() if (!removed[node->id]) newnodes.push_back(node); else - delete node; + delete_node(node); } nodes = newnodes; @@ -821,7 +821,7 @@ void ShaderGraph::clean(Scene *scene) if (visited[node->id]) newnodes.push_back(node); else - delete node; + delete_node(node); } nodes = newnodes; @@ -848,43 +848,43 @@ void ShaderGraph::default_inputs(bool do_osl) if (!input->link && (!(input->flags() & SocketType::OSL_INTERNAL) || do_osl)) { if (input->flags() & SocketType::LINK_TEXTURE_GENERATED) { if (!texco) - texco = new TextureCoordinateNode(); + texco = create_node(); connect(texco->output("Generated"), input); } if (input->flags() & SocketType::LINK_TEXTURE_NORMAL) { if (!texco) - texco = new TextureCoordinateNode(); + texco = create_node(); connect(texco->output("Normal"), input); } else if (input->flags() & SocketType::LINK_TEXTURE_UV) { if (!texco) - texco = new TextureCoordinateNode(); + texco = create_node(); connect(texco->output("UV"), input); } else if (input->flags() & SocketType::LINK_INCOMING) { if (!geom) - geom = new GeometryNode(); + geom = create_node(); connect(geom->output("Incoming"), input); } else if (input->flags() & SocketType::LINK_NORMAL) { if (!geom) - geom = new GeometryNode(); + geom = create_node(); connect(geom->output("Normal"), input); } else if (input->flags() & SocketType::LINK_POSITION) { if (!geom) - geom = new GeometryNode(); + geom = create_node(); connect(geom->output("Position"), input); } else if (input->flags() & SocketType::LINK_TANGENT) { if (!geom) - geom = new GeometryNode(); + geom = create_node(); connect(geom->output("Tangent"), input); } @@ -1064,7 +1064,7 @@ void ShaderGraph::transform_multi_closure(ShaderNode *node, ShaderOutput *weight if (fin) { /* mix closure: add node to mix closure weights */ - MixClosureWeightNode *mix_node = new MixClosureWeightNode(); + MixClosureWeightNode *mix_node = create_node(); add(mix_node); ShaderInput *fac_in = mix_node->input("Fac"); ShaderInput *weight_in = mix_node->input("Weight"); @@ -1101,7 +1101,7 @@ void ShaderGraph::transform_multi_closure(ShaderNode *node, ShaderOutput *weight /* already has a weight connected to it? add weights */ float weight_value = node->get_float(weight_in->socket_type); if (weight_in->link || weight_value != 0.0f) { - MathNode *math_node = new MathNode(); + MathNode *math_node = create_node(); add(math_node); if (weight_in->link) -- cgit v1.2.3