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:
authorLukas Tönne <lukas.toenne@gmail.com>2014-04-15 18:06:12 +0400
committerLukas Tönne <lukas.toenne@gmail.com>2014-04-15 18:28:10 +0400
commit09874df135888b89f51d7becaa369ebb1d1623c6 (patch)
treebccf5950d42963992adff95dbc3f00ed7db1cf16 /source/blender/compositor/intern/COM_NodeGraph.cpp
parent28a829893c702918afc5ac1945a06eaefa611594 (diff)
Structural cleanup and improvements for the compositor.
Many parts of the compositor are unnecessarily complicated. This patch aims at reducing the complexity of writing nodes and making the code more transparent. == Separating Nodes and Operations == Currently these are both mixed in the same graph, even though they have very different purposes and are used at distinct stages in the compositing process. The patch introduces dedicated graph classes for nodes and for operations. This removes the need for a lot of special case checks (isOperation etc.) and explicit type casts. It simplifies the code since it becomes clear at every stage what type of node we are dealing with. The compiler can use static typing to avoid common bugs from mixing up these types and fewer runtime sanity checks are needed. == Simplified Node Conversion == Converting nodes to operations was previously based on "relinking", i.e. nodes would start with by mirroring links in the Blender DNA node trees, then add operations and redirect these links to them. This was very hard to follow in many cases and required a lot of attention to avoid invalid states. Now there is a helper class called the NodeConverter, which is passed to nodes and implements a much simpler API for this process. Nodes can add operations and explicit connections as before, but defining "external" links to the inputs/outputs of the original node now uses mapping instead of directly modifying link data. Input data (node graph) and result (operations graph) are cleanly separated. == Removed Redundant Data Structures == A few redundant data structures have been removed, notably the SocketConnection. These are only needed temporarily during graph construction. For executing the compositor operations it is perfectly sufficient to store only the direct input link pointers. A common pointer indirection is avoided this way (which might also give a little performance improvement). == Avoid virtual recursive functions == Recursive virtual functions are evil. They are very hard to follow during debugging. At least in the parts this patch is concerned with these functions have been replaced by a non-virtual recursive core function (which might then call virtual non-recursive functions if needed). See for example NodeOperationBuilder::group_operations.
Diffstat (limited to 'source/blender/compositor/intern/COM_NodeGraph.cpp')
-rw-r--r--source/blender/compositor/intern/COM_NodeGraph.cpp280
1 files changed, 280 insertions, 0 deletions
diff --git a/source/blender/compositor/intern/COM_NodeGraph.cpp b/source/blender/compositor/intern/COM_NodeGraph.cpp
new file mode 100644
index 00000000000..d942d43befc
--- /dev/null
+++ b/source/blender/compositor/intern/COM_NodeGraph.cpp
@@ -0,0 +1,280 @@
+/*
+ * Copyright 2013, Blender Foundation.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * Contributor:
+ * Lukas Toenne
+ */
+
+#include <cstring>
+
+extern "C" {
+#include "BLI_listbase.h"
+#include "BLI_utildefines.h"
+
+#include "DNA_node_types.h"
+
+#include "BKE_node.h"
+}
+
+#include "COM_CompositorContext.h"
+#include "COM_Converter.h"
+#include "COM_Debug.h"
+#include "COM_Node.h"
+#include "COM_SocketProxyNode.h"
+
+#include "COM_NodeGraph.h" /* own include */
+
+/*******************
+ **** NodeGraph ****
+ *******************/
+
+NodeGraph::NodeGraph()
+{
+}
+
+NodeGraph::~NodeGraph()
+{
+ for (int index = 0; index < this->m_nodes.size(); index++) {
+ Node *node = this->m_nodes[index];
+ delete node;
+ }
+}
+
+void NodeGraph::from_bNodeTree(const CompositorContext &context, bNodeTree *tree)
+{
+ add_bNodeTree(context, 0, tree, NODE_INSTANCE_KEY_BASE);
+}
+
+bNodeSocket *NodeGraph::find_b_node_input(bNode *b_group_node, const char *identifier)
+{
+ for (bNodeSocket *b_sock = (bNodeSocket *)b_group_node->inputs.first; b_sock; b_sock = b_sock->next) {
+ if (STREQ(b_sock->identifier, identifier))
+ return b_sock;
+ }
+ return NULL;
+}
+
+bNodeSocket *NodeGraph::find_b_node_output(bNode *b_group_node, const char *identifier)
+{
+ for (bNodeSocket *b_sock = (bNodeSocket *)b_group_node->outputs.first; b_sock; b_sock = b_sock->next) {
+ if (STREQ(b_sock->identifier, identifier))
+ return b_sock;
+ }
+ return NULL;
+}
+
+void NodeGraph::add_node(Node *node, bNodeTree *b_ntree, bNodeInstanceKey key, bool is_active_group)
+{
+ node->setbNodeTree(b_ntree);
+ node->setInstanceKey(key);
+ node->setIsInActiveGroup(is_active_group);
+
+ m_nodes.push_back(node);
+
+ DebugInfo::node_added(node);
+}
+
+void NodeGraph::add_link(NodeOutput *fromSocket, NodeInput *toSocket)
+{
+ m_links.push_back(Link(fromSocket, toSocket));
+
+ /* register with the input */
+ toSocket->setLink(fromSocket);
+}
+
+void NodeGraph::add_bNodeTree(const CompositorContext &context, int nodes_start, bNodeTree *tree, bNodeInstanceKey parent_key)
+{
+ const bNodeTree *basetree = context.getbNodeTree();
+
+ /* update viewers in the active edittree as well the base tree (for backdrop) */
+ bool is_active_group = ((parent_key.value == basetree->active_viewer_key.value) ||
+ (tree == basetree));
+
+ /* add all nodes of the tree to the node list */
+ for (bNode *node = (bNode *)tree->nodes.first; node; node = node->next) {
+ bNodeInstanceKey key = BKE_node_instance_key(parent_key, tree, node);
+ add_bNode(context, tree, node, key, is_active_group);
+ }
+
+ NodeRange node_range(m_nodes.begin() + nodes_start, m_nodes.end());
+ /* add all nodelinks of the tree to the link list */
+ for (bNodeLink *nodelink = (bNodeLink *)tree->links.first; nodelink; nodelink = nodelink->next) {
+ add_bNodeLink(node_range, nodelink);
+ }
+}
+
+void NodeGraph::add_bNode(const CompositorContext &context, bNodeTree *b_ntree, bNode *b_node, bNodeInstanceKey key, bool is_active_group)
+{
+ /* replace muted nodes by proxies for internal links */
+ if (b_node->flag & NODE_MUTED) {
+ add_proxies_mute(b_ntree, b_node, key, is_active_group);
+ return;
+ }
+
+ /* replace slow nodes with proxies for fast execution */
+ if (context.isFastCalculation() && !Converter::is_fast_node(b_node)) {
+ add_proxies_skip(b_ntree, b_node, key, is_active_group);
+ return;
+ }
+
+ /* special node types */
+ if (b_node->type == NODE_GROUP) {
+ add_proxies_group(context, b_node, key);
+ return;
+ }
+
+ Node *node = Converter::convert(b_node);
+ if (node)
+ add_node(node, b_ntree, key, is_active_group);
+}
+
+NodeInput *NodeGraph::find_input(const NodeRange &node_range, bNodeSocket *b_socket)
+{
+ for (NodeGraph::NodeIterator it = node_range.first; it != node_range.second; ++it) {
+ Node *node = *it;
+ for (int index = 0; index < node->getNumberOfInputSockets(); index++) {
+ NodeInput *input = node->getInputSocket(index);
+ if (input->getbNodeSocket() == b_socket)
+ return input;
+ }
+ }
+ return NULL;
+}
+
+NodeOutput *NodeGraph::find_output(const NodeRange &node_range, bNodeSocket *b_socket)
+{
+ for (NodeGraph::NodeIterator it = node_range.first; it != node_range.second; ++it) {
+ Node *node = *it;
+ for (int index = 0; index < node->getNumberOfOutputSockets(); index++) {
+ NodeOutput *output = node->getOutputSocket(index);
+ if (output->getbNodeSocket() == b_socket)
+ return output;
+ }
+ }
+ return NULL;
+}
+
+void NodeGraph::add_bNodeLink(const NodeRange &node_range, bNodeLink *b_nodelink)
+{
+ /// @note: ignore invalid links
+ if (!(b_nodelink->flag & NODE_LINK_VALID))
+ return;
+
+ NodeInput *input = find_input(node_range, b_nodelink->tosock);
+ NodeOutput *output = find_output(node_range, b_nodelink->fromsock);
+ if (!input || !output)
+ return;
+ if (input->isLinked())
+ return;
+
+ add_link(output, input);
+}
+
+/* **** Special proxy node type conversions **** */
+
+void NodeGraph::add_proxies_mute(bNodeTree *b_ntree, bNode *b_node, bNodeInstanceKey key, bool is_active_group)
+{
+ for (bNodeLink *b_link = (bNodeLink *)b_node->internal_links.first; b_link; b_link = b_link->next) {
+ SocketProxyNode *proxy = new SocketProxyNode(b_node, b_link->fromsock, b_link->tosock);
+ add_node(proxy, b_ntree, key, is_active_group);
+ }
+}
+
+void NodeGraph::add_proxies_skip(bNodeTree *b_ntree, bNode *b_node, bNodeInstanceKey key, bool is_active_group)
+{
+ for (bNodeSocket *output = (bNodeSocket *)b_node->outputs.first; output; output = output->next) {
+ bNodeSocket *input;
+
+ /* look for first input with matching datatype for each output */
+ for (input = (bNodeSocket *)b_node->inputs.first; input; input = input->next) {
+ if (input->type == output->type)
+ break;
+ }
+
+ if (input) {
+ SocketProxyNode *proxy = new SocketProxyNode(b_node, input, output);
+ add_node(proxy, b_ntree, key, is_active_group);
+ }
+ }
+}
+
+void NodeGraph::add_proxies_group_inputs(bNode *b_node, bNode *b_node_io)
+{
+ bNodeTree *b_group_tree = (bNodeTree *)b_node->id;
+ BLI_assert(b_group_tree); /* should have been checked in advance */
+
+ /* not important for proxies */
+ bNodeInstanceKey key = NODE_INSTANCE_KEY_BASE;
+ bool is_active_group = false;
+
+ for (bNodeSocket *b_sock_io = (bNodeSocket *)b_node_io->outputs.first; b_sock_io; b_sock_io = b_sock_io->next) {
+ bNodeSocket *b_sock_group = find_b_node_input(b_node, b_sock_io->identifier);
+ if (b_sock_group) {
+ SocketProxyNode *proxy = new SocketProxyNode(b_node_io, b_sock_group, b_sock_io);
+ add_node(proxy, b_group_tree, key, is_active_group);
+ }
+ }
+}
+
+void NodeGraph::add_proxies_group_outputs(bNode *b_node, bNode *b_node_io, bool use_buffer)
+{
+ bNodeTree *b_group_tree = (bNodeTree *)b_node->id;
+ BLI_assert(b_group_tree); /* should have been checked in advance */
+
+ /* not important for proxies */
+ bNodeInstanceKey key = NODE_INSTANCE_KEY_BASE;
+ bool is_active_group = false;
+
+ for (bNodeSocket *b_sock_io = (bNodeSocket *)b_node_io->inputs.first; b_sock_io; b_sock_io = b_sock_io->next) {
+ bNodeSocket *b_sock_group = find_b_node_output(b_node, b_sock_io->identifier);
+ if (b_sock_group) {
+ if (use_buffer) {
+ SocketBufferNode *buffer = new SocketBufferNode(b_node_io, b_sock_io, b_sock_group);
+ add_node(buffer, b_group_tree, key, is_active_group);
+ }
+ else {
+ SocketProxyNode *proxy = new SocketProxyNode(b_node_io, b_sock_io, b_sock_group);
+ add_node(proxy, b_group_tree, key, is_active_group);
+ }
+ }
+ }
+}
+
+void NodeGraph::add_proxies_group(const CompositorContext &context, bNode *b_node, bNodeInstanceKey key)
+{
+ bNodeTree *b_group_tree = (bNodeTree *)b_node->id;
+
+ /* missing node group datablock can happen with library linking */
+ if (!b_group_tree) {
+ /* this error case its handled in convertToOperations() so we don't get un-convertred sockets */
+ return;
+ }
+
+ /* use node list size before adding proxies, so they can be connected in add_bNodeTree */
+ int nodes_start = m_nodes.size();
+
+ /* create proxy nodes for group input/output nodes */
+ for (bNode *b_node_io = (bNode *)b_group_tree->nodes.first; b_node_io; b_node_io = b_node_io->next) {
+ if (b_node_io->type == NODE_GROUP_INPUT)
+ add_proxies_group_inputs(b_node, b_node_io);
+
+ if (b_node_io->type == NODE_GROUP_OUTPUT && (b_node_io->flag & NODE_DO_OUTPUT))
+ add_proxies_group_outputs(b_node, b_node_io, context.isGroupnodeBufferEnabled());
+ }
+
+ add_bNodeTree(context, nodes_start, b_group_tree, key);
+}