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

COM_NodeOperationBuilder.h « intern « compositor « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1f9c86b51cd18f9154bfecbde75b61dd8db9913a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
 * 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.
 *
 * Copyright 2013, Blender Foundation.
 */

#pragma once

#include <map>
#include <set>
#include <vector>

#include "COM_NodeGraph.h"

namespace blender::compositor {

class CompositorContext;

class Node;
class NodeInput;
class NodeOutput;

class ExecutionSystem;
class ExecutionGroup;
class NodeOperation;
class NodeOperationInput;
class NodeOperationOutput;

class PreviewOperation;
class WriteBufferOperation;
class ViewerOperation;
class ConstantOperation;

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;
    }
  };

 private:
  const CompositorContext *m_context;
  NodeGraph m_graph;
  ExecutionSystem *exec_system_;

  Vector<NodeOperation *> m_operations;
  Vector<Link> m_links;
  Vector<ExecutionGroup *> m_groups;

  /** Maps operation inputs to node inputs */
  Map<NodeOperationInput *, NodeInput *> m_input_map;
  /** Maps node outputs to operation outputs */
  Map<NodeOutput *, NodeOperationOutput *> m_output_map;

  Node *m_current_node;

  /** Operation that will be writing to the viewer image
   *  Only one operation can occupy this place at a time,
   *  to avoid race conditions
   */
  ViewerOperation *m_active_viewer;

 public:
  NodeOperationBuilder(const CompositorContext *context,
                       bNodeTree *b_nodetree,
                       ExecutionSystem *system);

  const CompositorContext &context() const
  {
    return *m_context;
  }

  void convertToOperations(ExecutionSystem *system);

  void addOperation(NodeOperation *operation);
  void replace_operation_with_constant(NodeOperation *operation,
                                       ConstantOperation *constant_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);

  /** Define a viewer operation as the active output, if possible */
  void registerViewer(ViewerOperation *viewer);
  /** The currently active viewer output operation */
  ViewerOperation *active_viewer() const
  {
    return m_active_viewer;
  }

  const Vector<NodeOperation *> &get_operations() const
  {
    return m_operations;
  }

  const Vector<Link> &get_links() const
  {
    return m_links;
  }

 protected:
  /** 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, const NodeInput *node_input);

  /** Replace proxy operations with direct links */
  void resolve_proxies();

  /** Calculate canvas area for each operation. */
  void determine_canvases();

  /** Helper function to store connected inputs for replacement */
  Vector<NodeOperationInput *> 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;
  void unlink_inputs_and_relink_outputs(NodeOperation *unlinked_op, NodeOperation *linked_op);
  void merge_equal_operations();
  void merge_equal_operations(NodeOperation *from, NodeOperation *into);
  void save_graphviz(StringRefNull name = "");
#ifdef WITH_CXX_GUARDEDALLOC
  MEM_CXX_CLASS_ALLOC_FUNCS("COM:NodeCompilerImpl")
#endif
};

std::ostream &operator<<(std::ostream &os, const NodeOperationBuilder &builder);
std::ostream &operator<<(std::ostream &os, const NodeOperationBuilder::Link &link);

}  // namespace blender::compositor