diff options
author | Lukas Tönne <lukas.toenne@gmail.com> | 2014-04-15 18:06:12 +0400 |
---|---|---|
committer | Lukas Tönne <lukas.toenne@gmail.com> | 2014-04-15 18:28:10 +0400 |
commit | 09874df135888b89f51d7becaa369ebb1d1623c6 (patch) | |
tree | bccf5950d42963992adff95dbc3f00ed7db1cf16 /source/blender/compositor/intern/COM_NodeOperationBuilder.h | |
parent | 28a829893c702918afc5ac1945a06eaefa611594 (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_NodeOperationBuilder.h')
-rw-r--r-- | source/blender/compositor/intern/COM_NodeOperationBuilder.h | 158 |
1 files changed, 158 insertions, 0 deletions
diff --git a/source/blender/compositor/intern/COM_NodeOperationBuilder.h b/source/blender/compositor/intern/COM_NodeOperationBuilder.h new file mode 100644 index 00000000000..ab890282bab --- /dev/null +++ b/source/blender/compositor/intern/COM_NodeOperationBuilder.h @@ -0,0 +1,158 @@ +/* + * 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 + */ + +#ifndef _COM_NodeCompilerImpl_h +#define _COM_NodeCompilerImpl_h + +#include <map> +#include <set> +#include <vector> + +#include "COM_NodeGraph.h" + +using std::vector; + +class CompositorContext; + +class Node; +class NodeInput; +class NodeOutput; + +class ExecutionSystem; +class ExecutionGroup; +class NodeOperation; +class NodeOperationInput; +class NodeOperationOutput; + +class PreviewOperation; +class WriteBufferOperation; + +class NodeOperationBuilder { +public: + class Link { + private: + NodeOperationOutput *m_from; + NodeOperationInput *m_to; + + public: + Link(NodeOperationOutput *from, NodeOperationInput *to) : + m_from(from), + m_to(to) + {} + + NodeOperationOutput *from() const { return m_from; } + NodeOperationInput *to() const { return m_to; } + }; + + typedef std::vector<NodeOperation *> Operations; + typedef std::vector<Link> Links; + typedef std::vector<ExecutionGroup *> Groups; + + typedef std::map<NodeOperationInput *, NodeInput *> InputSocketMap; + typedef std::map<NodeOutput *, NodeOperationOutput *> OutputSocketMap; + + typedef std::vector<NodeOperationInput *> OpInputs; + typedef std::map<NodeInput *, OpInputs> OpInputInverseMap; + +private: + const CompositorContext *m_context; + NodeGraph m_graph; + + Operations m_operations; + Links m_links; + Groups m_groups; + + /** Maps operation inputs to node inputs */ + InputSocketMap m_input_map; + /** Maps node outputs to operation outputs */ + OutputSocketMap m_output_map; + + Node *m_current_node; + +public: + NodeOperationBuilder(const CompositorContext *context, bNodeTree *b_nodetree); + ~NodeOperationBuilder(); + + const CompositorContext &context() const { return *m_context; } + + void convertToOperations(ExecutionSystem *system); + + void addOperation(NodeOperation *operation); + + /** Map input socket of the current node to an operation socket */ + void mapInputSocket(NodeInput *node_socket, NodeOperationInput *operation_socket); + /** Map output socket of the current node to an operation socket */ + void mapOutputSocket(NodeOutput *node_socket, NodeOperationOutput *operation_socket); + + void addLink(NodeOperationOutput *from, NodeOperationInput *to); + void removeInputLink(NodeOperationInput *to); + + /** Add a preview operation for a operation output */ + void addPreview(NodeOperationOutput *output); + /** Add a preview operation for a node input */ + void addNodeInputPreview(NodeInput *input); + +protected: + static NodeInput *find_node_input(const InputSocketMap &map, NodeOperationInput *op_input); + static const OpInputs &find_operation_inputs(const OpInputInverseMap &map, NodeInput *node_input); + static NodeOperationOutput *find_operation_output(const OutputSocketMap &map, NodeOutput *node_output); + + /** Add datatype conversion where needed */ + void add_datatype_conversions(); + + /** Construct a constant value operation for every unconnected input */ + void add_operation_input_constants(); + void add_input_constant_value(NodeOperationInput *input, NodeInput *node_input); + + /** Replace proxy operations with direct links */ + void resolve_proxies(); + + /** Calculate resolution for each operation */ + void determineResolutions(); + + /** Helper function to store connected inputs for replacement */ + OpInputs cache_output_links(NodeOperationOutput *output) const; + /** Find a connected write buffer operation to an OpOutput */ + WriteBufferOperation *find_attached_write_buffer_operation(NodeOperationOutput *output) const; + /** Add read/write buffer operations around complex operations */ + void add_complex_operation_buffers(); + void add_input_buffers(NodeOperation *operation, NodeOperationInput *input); + void add_output_buffers(NodeOperation *operation, NodeOperationOutput *output); + + /** Remove unreachable operations */ + void prune_operations(); + + /** Sort operations by link dependencies */ + void sort_operations(); + + /** Create execution groups */ + void group_operations(); + ExecutionGroup *make_group(NodeOperation *op); + +private: + PreviewOperation *make_preview_operation() const; + +#ifdef WITH_CXX_GUARDEDALLOC + MEM_CXX_CLASS_ALLOC_FUNCS("COM:NodeCompilerImpl") +#endif +}; + +#endif /* _COM_NodeCompilerImpl_h */ |