Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJacques Lucke <jacques@blender.org>2021-12-21 17:18:56 +0300
committerJacques Lucke <jacques@blender.org>2021-12-21 17:18:56 +0300
commit7e712b2d6a0d257d272ed35622b41d06274af8df (patch)
treed33cfb610da7fa43351a282f9c9a9ba25c77d1f8 /source/blender/nodes
parent1abf2f3c7c08051502ae0830ab5d4fa8d8d9f13a (diff)
Nodes: refactor node tree update handling
Goals of this refactor: * More unified approach to updating everything that needs to be updated after a change in a node tree. * The updates should happen in the correct order and quadratic or worse algorithms should be avoided. * Improve detection of changes to the output to avoid tagging the depsgraph when it's not necessary. * Move towards a more declarative style of defining nodes by having a more centralized update procedure. The refactor consists of two main parts: * Node tree tagging and update refactor. * Generally, when changes are done to a node tree, it is tagged dirty until a global update function is called that updates everything in the correct order. * The tagging is more fine-grained compared to before, to allow for more precise depsgraph update tagging. * Depsgraph changes. * The shading specific depsgraph node for node trees as been removed. * Instead, there is a new `NTREE_OUTPUT` depsgrap node, which is only tagged when the output of the node tree changed (e.g. the Group Output or Material Output node). * The copy-on-write relation from node trees to the data block they are embedded in is now non-flushing. This avoids e.g. triggering a material update after the shader node tree changed in unrelated ways. Instead the material has a flushing relation to the new `NTREE_OUTPUT` node now. * The depsgraph no longer reports data block changes through to cycles through `Depsgraph.updates` when only the node tree changed in ways that do not affect the output. Avoiding unnecessary updates seems to work well for geometry nodes and cycles. The situation is a bit worse when there are drivers on the node tree, but that could potentially be improved separately in the future. Avoiding updates in eevee and the compositor is more tricky, but also less urgent. * Eevee updates are triggered by calling `DRW_notify_view_update` in `ED_render_view3d_update` indirectly from `DEG_editors_update`. * Compositor updates are triggered by `ED_node_composite_job` in `node_area_refresh`. This is triggered by calling `ED_area_tag_refresh` in `node_area_listener`. Removing updates always has the risk of breaking some dependency that no one was aware of. It's not unlikely that this will happen here as well. Adding back missing updates should be quite a bit easier than getting rid of unnecessary updates though. Differential Revision: https://developer.blender.org/D13246
Diffstat (limited to 'source/blender/nodes')
-rw-r--r--source/blender/nodes/composite/node_composite_tree.cc11
-rw-r--r--source/blender/nodes/geometry/nodes/node_geo_curve_primitive_quadrilateral.cc29
-rw-r--r--source/blender/nodes/intern/node_common.cc3
-rw-r--r--source/blender/nodes/intern/node_exec.cc3
-rw-r--r--source/blender/nodes/intern/node_socket.cc8
-rw-r--r--source/blender/nodes/intern/node_tree_ref.cc2
-rw-r--r--source/blender/nodes/intern/node_util.c183
-rw-r--r--source/blender/nodes/shader/node_shader_tree.c26
-rw-r--r--source/blender/nodes/texture/node_texture_tree.c5
9 files changed, 39 insertions, 231 deletions
diff --git a/source/blender/nodes/composite/node_composite_tree.cc b/source/blender/nodes/composite/node_composite_tree.cc
index 1326c9edab1..54967c82562 100644
--- a/source/blender/nodes/composite/node_composite_tree.cc
+++ b/source/blender/nodes/composite/node_composite_tree.cc
@@ -33,6 +33,7 @@
#include "BKE_global.h"
#include "BKE_main.h"
#include "BKE_node.h"
+#include "BKE_node_tree_update.h"
#include "BKE_tracking.h"
#include "node_common.h"
@@ -186,11 +187,6 @@ static void update(bNodeTree *ntree)
ntreeSetOutput(ntree);
ntree_update_reroute_nodes(ntree);
-
- if (ntree->update & NTREE_UPDATE_NODES) {
- /* clean up preview cache, in case nodes have been removed */
- BKE_node_preview_remove_unused(ntree);
- }
}
static void composite_node_add_init(bNodeTree *UNUSED(bnodetree), bNode *bnode)
@@ -301,14 +297,15 @@ void ntreeCompositTagRender(Scene *scene)
if (sce_iter->nodetree) {
LISTBASE_FOREACH (bNode *, node, &sce_iter->nodetree->nodes) {
if (node->id == (ID *)scene || node->type == CMP_NODE_COMPOSITE) {
- nodeUpdate(sce_iter->nodetree, node);
+ BKE_ntree_update_tag_node_property(sce_iter->nodetree, node);
}
else if (node->type == CMP_NODE_TEXTURE) /* uses scene size_x/size_y */ {
- nodeUpdate(sce_iter->nodetree, node);
+ BKE_ntree_update_tag_node_property(sce_iter->nodetree, node);
}
}
}
}
+ BKE_ntree_update_main(G_MAIN, nullptr);
}
/* XXX after render animation system gets a refresh, this call allows composite to end clean */
diff --git a/source/blender/nodes/geometry/nodes/node_geo_curve_primitive_quadrilateral.cc b/source/blender/nodes/geometry/nodes/node_geo_curve_primitive_quadrilateral.cc
index 21bee2577b2..98480971fbd 100644
--- a/source/blender/nodes/geometry/nodes/node_geo_curve_primitive_quadrilateral.cc
+++ b/source/blender/nodes/geometry/nodes/node_geo_curve_primitive_quadrilateral.cc
@@ -112,35 +112,26 @@ static void node_update(bNodeTree *ntree, bNode *node)
bNodeSocket *p3 = p2->next;
bNodeSocket *p4 = p3->next;
- LISTBASE_FOREACH (bNodeSocket *, sock, &node->inputs) {
- nodeSetSocketAvailability(ntree, sock, false);
- }
+ Vector<bNodeSocket *> available_sockets;
if (mode == GEO_NODE_CURVE_PRIMITIVE_QUAD_MODE_RECTANGLE) {
- nodeSetSocketAvailability(ntree, width, true);
- nodeSetSocketAvailability(ntree, height, true);
+ available_sockets.extend({width, height});
}
else if (mode == GEO_NODE_CURVE_PRIMITIVE_QUAD_MODE_PARALLELOGRAM) {
- nodeSetSocketAvailability(ntree, width, true);
- nodeSetSocketAvailability(ntree, height, true);
- nodeSetSocketAvailability(ntree, offset, true);
+ available_sockets.extend({width, height, offset});
}
else if (mode == GEO_NODE_CURVE_PRIMITIVE_QUAD_MODE_TRAPEZOID) {
- nodeSetSocketAvailability(ntree, bottom, true);
- nodeSetSocketAvailability(ntree, top, true);
- nodeSetSocketAvailability(ntree, offset, true);
- nodeSetSocketAvailability(ntree, height, true);
+ available_sockets.extend({bottom, top, offset, height});
}
else if (mode == GEO_NODE_CURVE_PRIMITIVE_QUAD_MODE_KITE) {
- nodeSetSocketAvailability(ntree, width, true);
- nodeSetSocketAvailability(ntree, bottom_height, true);
- nodeSetSocketAvailability(ntree, top_height, true);
+ available_sockets.extend({width, bottom_height, top_height});
}
else if (mode == GEO_NODE_CURVE_PRIMITIVE_QUAD_MODE_POINTS) {
- nodeSetSocketAvailability(ntree, p1, true);
- nodeSetSocketAvailability(ntree, p2, true);
- nodeSetSocketAvailability(ntree, p3, true);
- nodeSetSocketAvailability(ntree, p4, true);
+ available_sockets.extend({p1, p2, p3, p4});
+ }
+
+ LISTBASE_FOREACH (bNodeSocket *, sock, &node->inputs) {
+ nodeSetSocketAvailability(ntree, sock, available_sockets.contains(sock));
}
}
diff --git a/source/blender/nodes/intern/node_common.cc b/source/blender/nodes/intern/node_common.cc
index c302b1081af..a1fccc401a4 100644
--- a/source/blender/nodes/intern/node_common.cc
+++ b/source/blender/nodes/intern/node_common.cc
@@ -38,6 +38,7 @@
#include "BLT_translation.h"
#include "BKE_node.h"
+#include "BKE_node_tree_update.h"
#include "RNA_types.h"
@@ -153,8 +154,6 @@ static void update_socket_to_match_interface(bNodeTree &node_tree,
/* Update socket type if necessary */
if (socket_to_update.typeinfo != interface_socket.typeinfo) {
nodeModifySocketType(&node_tree, &node, &socket_to_update, interface_socket.idname);
- /* Flag the tree to make sure link validity is updated after type changes. */
- node_tree.update |= NTREE_UPDATE_LINKS;
}
if (interface_socket.typeinfo->interface_verify_socket) {
diff --git a/source/blender/nodes/intern/node_exec.cc b/source/blender/nodes/intern/node_exec.cc
index 95070bf735e..f5b64f8499c 100644
--- a/source/blender/nodes/intern/node_exec.cc
+++ b/source/blender/nodes/intern/node_exec.cc
@@ -28,6 +28,7 @@
#include "BKE_global.h"
#include "BKE_node.h"
+#include "BKE_node_tree_update.h"
#include "MEM_guardedalloc.h"
@@ -170,7 +171,7 @@ bNodeTreeExec *ntree_exec_begin(bNodeExecContext *context,
/* Using global main here is likely totally wrong, not sure what to do about that one though...
* We cannot even check ntree is in global main,
* since most of the time it won't be (thanks to ntree design)!!! */
- ntreeUpdateTree(G.main, ntree);
+ BKE_ntree_update_main_tree(G.main, ntree, nullptr);
/* get a dependency-sorted list of nodes */
ntreeGetDependencyList(ntree, &nodelist, &totnodes);
diff --git a/source/blender/nodes/intern/node_socket.cc b/source/blender/nodes/intern/node_socket.cc
index d83c05b38a1..46cb9dcf891 100644
--- a/source/blender/nodes/intern/node_socket.cc
+++ b/source/blender/nodes/intern/node_socket.cc
@@ -236,6 +236,14 @@ static void refresh_socket_list(bNodeTree &ntree,
link->tosock = new_socket;
}
}
+ LISTBASE_FOREACH (bNodeLink *, internal_link, &node.internal_links) {
+ if (internal_link->fromsock == old_socket_with_same_identifier) {
+ internal_link->fromsock = new_socket;
+ }
+ else if (internal_link->tosock == old_socket_with_same_identifier) {
+ internal_link->tosock = new_socket;
+ }
+ }
}
}
}
diff --git a/source/blender/nodes/intern/node_tree_ref.cc b/source/blender/nodes/intern/node_tree_ref.cc
index 912d5e5322c..ffe0edb9762 100644
--- a/source/blender/nodes/intern/node_tree_ref.cc
+++ b/source/blender/nodes/intern/node_tree_ref.cc
@@ -70,6 +70,8 @@ NodeTreeRef::NodeTreeRef(bNodeTree *btree) : btree_(btree)
break;
}
}
+ BLI_assert(internal_link.from_ != nullptr);
+ BLI_assert(internal_link.to_ != nullptr);
node.internal_links_.append(&internal_link);
}
diff --git a/source/blender/nodes/intern/node_util.c b/source/blender/nodes/intern/node_util.c
index 7620c8fa1a8..5c2d84cf605 100644
--- a/source/blender/nodes/intern/node_util.c
+++ b/source/blender/nodes/intern/node_util.c
@@ -35,6 +35,7 @@
#include "BKE_colortools.h"
#include "BKE_node.h"
+#include "BKE_node_tree_update.h"
#include "RNA_access.h"
#include "RNA_enum_types.h"
@@ -340,188 +341,6 @@ void node_insert_link_default(bNodeTree *ntree, bNode *node, bNodeLink *link)
/** \} */
/* -------------------------------------------------------------------- */
-/** \name Internal Links (mute and disconnect)
- * \{ */
-
-/**
- * Common datatype priorities, works for compositor, shader and texture nodes alike
- * defines priority of datatype connection based on output type (to):
- * `< 0`: never connect these types.
- * `>= 0`: priority of connection (higher values chosen first).
- */
-static int node_datatype_priority(const bNodeSocketType *from, const bNodeSocketType *to)
-{
- switch (to->type) {
- case SOCK_RGBA:
- switch (from->type) {
- case SOCK_RGBA:
- return 4;
- case SOCK_FLOAT:
- return 3;
- case SOCK_INT:
- return 2;
- case SOCK_BOOLEAN:
- return 1;
- }
- return -1;
- case SOCK_VECTOR:
- switch (from->type) {
- case SOCK_VECTOR:
- return 4;
- case SOCK_FLOAT:
- return 3;
- case SOCK_INT:
- return 2;
- case SOCK_BOOLEAN:
- return 1;
- }
- return -1;
- case SOCK_FLOAT:
- switch (from->type) {
- case SOCK_FLOAT:
- return 5;
- case SOCK_INT:
- return 4;
- case SOCK_BOOLEAN:
- return 3;
- case SOCK_RGBA:
- return 2;
- case SOCK_VECTOR:
- return 1;
- }
- return -1;
- case SOCK_INT:
- switch (from->type) {
- case SOCK_INT:
- return 5;
- case SOCK_FLOAT:
- return 4;
- case SOCK_BOOLEAN:
- return 3;
- case SOCK_RGBA:
- return 2;
- case SOCK_VECTOR:
- return 1;
- }
- return -1;
- case SOCK_BOOLEAN:
- switch (from->type) {
- case SOCK_BOOLEAN:
- return 5;
- case SOCK_INT:
- return 4;
- case SOCK_FLOAT:
- return 3;
- case SOCK_RGBA:
- return 2;
- case SOCK_VECTOR:
- return 1;
- }
- return -1;
- }
-
- /* The rest of the socket types only allow an internal link if both the input and output socket
- * have the same type. If the sockets are custom, we check the idname instead. */
- if (to->type == from->type && (to->type != SOCK_CUSTOM || STREQ(to->idname, from->idname))) {
- return 1;
- }
-
- return -1;
-}
-
-/* select a suitable input socket for an output */
-static bNodeSocket *select_internal_link_input(bNode *node, bNodeSocket *output)
-{
- if (node->type == NODE_REROUTE) {
- return node->inputs.first;
- }
-
- bNodeSocket *selected = NULL, *input;
- int i;
- int sel_priority = -1;
- bool sel_is_linked = false;
-
- for (input = node->inputs.first, i = 0; input; input = input->next, i++) {
- int priority = node_datatype_priority(input->typeinfo, output->typeinfo);
- bool is_linked = (input->link != NULL);
- bool preferred;
-
- if (nodeSocketIsHidden(input) || /* ignore hidden sockets */
- input->flag &
- SOCK_NO_INTERNAL_LINK || /* ignore if input is not allowed for internal connections */
- priority < 0 || /* ignore incompatible types */
- priority < sel_priority) /* ignore if we already found a higher priority input */
- {
- continue;
- }
-
- /* determine if this input is preferred over the currently selected */
- preferred = (priority > sel_priority) || /* prefer higher datatype priority */
- (is_linked && !sel_is_linked); /* prefer linked over unlinked */
-
- if (preferred) {
- selected = input;
- sel_is_linked = is_linked;
- sel_priority = priority;
- }
- }
-
- return selected;
-}
-
-void node_internal_links_create(bNodeTree *ntree, bNode *node)
-{
- bNodeLink *link;
- bNodeSocket *output, *input;
-
- /* sanity check */
- if (!ntree) {
- return;
- }
-
- /* use link pointer as a tag for handled sockets (for outputs is unused anyway) */
- for (output = node->outputs.first; output; output = output->next) {
- output->link = NULL;
- }
-
- for (link = ntree->links.first; link; link = link->next) {
- if (nodeLinkIsHidden(link)) {
- continue;
- }
-
- output = link->fromsock;
- if (link->fromnode != node || output->link) {
- continue;
- }
- if (nodeSocketIsHidden(output) || output->flag & SOCK_NO_INTERNAL_LINK) {
- continue;
- }
- output->link = link; /* not really used, just for tagging handled sockets */
-
- /* look for suitable input */
- input = select_internal_link_input(node, output);
-
- if (input) {
- bNodeLink *ilink = MEM_callocN(sizeof(bNodeLink), "internal node link");
- ilink->fromnode = node;
- ilink->fromsock = input;
- ilink->tonode = node;
- ilink->tosock = output;
- /* internal link is always valid */
- ilink->flag |= NODE_LINK_VALID;
- BLI_addtail(&node->internal_links, ilink);
- }
- }
-
- /* clean up */
- for (output = node->outputs.first; output; output = output->next) {
- output->link = NULL;
- }
-}
-
-/** \} */
-
-/* -------------------------------------------------------------------- */
/** \name Default value RNA access
* \{ */
diff --git a/source/blender/nodes/shader/node_shader_tree.c b/source/blender/nodes/shader/node_shader_tree.c
index b79dbe3f3f8..b19835fae19 100644
--- a/source/blender/nodes/shader/node_shader_tree.c
+++ b/source/blender/nodes/shader/node_shader_tree.c
@@ -44,6 +44,7 @@
#include "BKE_lib_id.h"
#include "BKE_linestyle.h"
#include "BKE_node.h"
+#include "BKE_node_tree_update.h"
#include "BKE_scene.h"
#include "RNA_access.h"
@@ -153,11 +154,6 @@ static void update(bNodeTree *ntree)
ntreeSetOutput(ntree);
ntree_update_reroute_nodes(ntree);
-
- if (ntree->update & NTREE_UPDATE_NODES) {
- /* clean up preview cache, in case nodes have been removed */
- BKE_node_preview_remove_unused(ntree);
- }
}
static bool shader_validate_link(eNodeSocketDatatype from, eNodeSocketDatatype to)
@@ -352,7 +348,7 @@ static void ntree_shader_unlink_hidden_value_sockets(bNode *group_node, bNodeSoc
}
if (removed_link) {
- ntreeUpdateTree(G.main, group_ntree);
+ BKE_ntree_update_main_tree(G.main, group_ntree, NULL);
}
}
@@ -411,7 +407,7 @@ static void ntree_shader_groups_expand_inputs(bNodeTree *localtree)
}
if (link_added) {
- ntreeUpdateTree(G.main, localtree);
+ BKE_ntree_update_main_tree(G.main, localtree, NULL);
}
}
@@ -491,7 +487,7 @@ static void flatten_group_do(bNodeTree *ntree, bNode *gnode)
ntreeFreeLocalNode(ntree, node);
}
- ntree->update |= NTREE_UPDATE_NODES | NTREE_UPDATE_LINKS;
+ BKE_ntree_update_tag_all(ntree);
}
/* Flatten group to only have a simple single tree */
@@ -516,7 +512,7 @@ static void ntree_shader_groups_flatten(bNodeTree *localtree)
}
}
- ntreeUpdateTree(G.main, localtree);
+ BKE_ntree_update_main_tree(G.main, localtree, NULL);
}
/* Check whether shader has a displacement.
@@ -536,7 +532,7 @@ static bool ntree_shader_has_displacement(bNodeTree *ntree,
return false;
}
/* Make sure sockets links pointers are correct. */
- ntreeUpdateTree(G.main, ntree);
+ BKE_ntree_update_main_tree(G.main, ntree, NULL);
bNodeSocket *displacement = ntree_shader_node_find_input(output_node, "Displacement");
if (displacement == NULL) {
@@ -625,7 +621,7 @@ static void ntree_shader_bypass_tagged_bump_nodes(bNodeTree *ntree)
ntree_shader_bypass_bump_link(ntree, node, link);
}
}
- ntreeUpdateTree(G.main, ntree);
+ BKE_ntree_update_main_tree(G.main, ntree, NULL);
}
static bool ntree_branch_count_and_tag_nodes(bNode *fromnode, bNode *tonode, void *userdata)
@@ -710,7 +706,7 @@ static void ntree_shader_copy_branch_displacement(bNodeTree *ntree,
nodeRemLink(ntree, displacement_link);
nodeAddLink(ntree, displacement_node, displacement_socket, tonode, tosock);
- ntreeUpdateTree(G.main, ntree);
+ BKE_ntree_update_main_tree(G.main, ntree, NULL);
}
/* Re-link displacement output to unconnected normal sockets via bump node.
@@ -774,12 +770,12 @@ static void ntree_shader_relink_displacement(bNodeTree *ntree, bNode *output_nod
geo_node->tmp_flag = -2;
bump_node->tmp_flag = -2;
- ntreeUpdateTree(G.main, ntree);
+ BKE_ntree_update_main_tree(G.main, ntree, NULL);
/* Connect all free-standing Normal inputs and relink geometry/coordinate nodes. */
ntree_shader_link_builtin_normal(ntree, bump_node, bump_output_socket);
/* We modified the tree, it needs to be updated now. */
- ntreeUpdateTree(G.main, ntree);
+ BKE_ntree_update_main_tree(G.main, ntree, NULL);
}
static void node_tag_branch_as_derivative(bNode *node, int dx)
@@ -859,7 +855,7 @@ void ntree_shader_tag_nodes(bNodeTree *ntree, bNode *output_node, nTreeTags *tag
return;
}
/* Make sure sockets links pointers are correct. */
- ntreeUpdateTree(G.main, ntree);
+ BKE_ntree_update_main_tree(G.main, ntree, NULL);
nodeChainIterBackwards(ntree, output_node, ntree_tag_bsdf_cb, tags, 0);
}
diff --git a/source/blender/nodes/texture/node_texture_tree.c b/source/blender/nodes/texture/node_texture_tree.c
index 30ad0d1d629..1125936aded 100644
--- a/source/blender/nodes/texture/node_texture_tree.c
+++ b/source/blender/nodes/texture/node_texture_tree.c
@@ -135,11 +135,6 @@ static void localize(bNodeTree *UNUSED(localtree), bNodeTree *UNUSED(ntree))
static void update(bNodeTree *ntree)
{
ntree_update_reroute_nodes(ntree);
-
- if (ntree->update & NTREE_UPDATE_NODES) {
- /* clean up preview cache, in case nodes have been removed */
- BKE_node_preview_remove_unused(ntree);
- }
}
static bool texture_node_tree_socket_type_valid(bNodeTreeType *UNUSED(ntreetype),