Blender V5.0
COM_compile_state.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
7#include <optional>
8
9#include "BLI_map.hh"
10
12
13#include "COM_context.hh"
14#include "COM_domain.hh"
15#include "COM_node_operation.hh"
17#include "COM_scheduler.hh"
18
19namespace blender::compositor {
20
21using namespace nodes::derived_node_tree_types;
22
23/* ------------------------------------------------------------------------------------------------
24 * Compile State
25 *
26 * The compile state is a utility class used to track the state of compilation when compiling the
27 * node tree. In particular, it tracks two important pieces of information, each of which is
28 * described in one of the following sections.
29 *
30 * First, it stores a mapping between all nodes and the operations they were compiled into. The
31 * mapping are stored independently depending on the type of the operation in the node_operations_
32 * and pixel_operations_ maps. So those two maps are mutually exclusive. The compiler should call
33 * the map_node_to_node_operation and map_node_to_pixel_operation methods to populate those maps
34 * as soon as it compiles a node or multiple nodes into an operation. Those maps are used to
35 * retrieve the results of outputs linked to the inputs of operations. For more details, see the
36 * get_result_from_output_socket method. For the node tree shown below, nodes 1, 2, and 6 are
37 * mapped to their compiled operations in the node_operation_ map. While nodes 3 and 4 are both
38 * mapped to the first pixel operation, and node 5 is mapped to the second pixel operation in the
39 * pixel_operations_ map.
40 *
41 * Pixel Operation 1 Pixel Operation 2
42 * +-----------------------------------+ +------------------+
43 * .------------. | .------------. .------------. | | .------------. | .------------.
44 * | Node 1 | | | Node 3 | | Node 4 | | | | Node 5 | | | Node 6 |
45 * | |----|--| |--| |---|-----|--| |--|--| |
46 * | | .-|--| | | | | .--|--| | | | |
47 * '------------' | | '------------' '------------' | | | '------------' | '------------'
48 * | +-----------------------------------+ | +------------------+
49 * .------------. | |
50 * | Node 2 | | |
51 * | |--'----------------------------------------'
52 * | |
53 * '------------'
54 *
55 * Second, it stores the pixel compile unit, whether is operates on single values, and its domain
56 * if it was not operating on single values. One should first go over the discussion in
57 * COM_evaluator.hh for a high level description of the mechanism of the compile unit. The one
58 * important detail in this class is the should_compile_pixel_compile_unit method, which implements
59 * the criteria of whether the compile unit should be compiled given the node currently being
60 * processed as an argument. Those criteria are described as follows. If the compile unit is empty
61 * as is the case when processing nodes 1, 2, and 3, then it plainly shouldn't be compiled. If the
62 * given node is not a pixel node, then it can't be added to the compile unit and the unit is
63 * considered complete and should be compiled, as is the case when processing node 6. If the
64 * compile unit operates on single values and the given node operates on non-single values or vice
65 * versa, then it can't be added to the compile unit and the unit is considered complete and should
66 * be compiled, more on that in the next section. If the computed domain of the given node is not
67 * compatible with the domain of the compiled unit, then it can't be added to the unit and the unit
68 * is considered complete and should be compiled, as is the case when processing node 5, more on
69 * this in the next section. Otherwise, the given node is compatible with the compile unit and can
70 * be added to it, so the unit shouldn't be compiled just yet, as is the case when processing
71 * node 4.
72 *
73 * Special attention should be given to the aforementioned single value and domain compatibility
74 * criterion. One should first go over the discussion in COM_domain.hh for more information on
75 * domains. When a compile unit gets eventually compiled to a pixel operation, that operation will
76 * have a certain operation domain, and any node that gets added to the compile unit should itself
77 * have a computed node domain that is compatible with that operation domain, otherwise, had the
78 * node been compiled into its own operation separately, the result would have been be different.
79 * For instance, consider the above node tree where node 1 outputs a 100x100 result, node 2 outputs
80 * a 50x50 result, the first input in node 3 has the highest domain priority, and the second input
81 * in node 5 has the highest domain priority. In this case, pixel operation 1 will output a 100x100
82 * result, and pixel operation 2 will output a 50x50 result, because that's the computed operation
83 * domain for each of them. So node 6 will get a 50x50 result. Now consider the same node tree, but
84 * where all three nodes 3, 4, and 5 were compiled into a single pixel operation as shown the node
85 * tree below. In that case, pixel operation 1 will output a 100x100 result, because that's its
86 * computed operation domain. So node 6 will get a 100x100 result. As can be seen, the final result
87 * is different even though the node tree is the same. That's why the compiler can decide to
88 * compile the compile unit early even though further nodes can still be technically added to it.
89 *
90 * Pixel Operation 1
91 * +------------------------------------------------------+
92 * .------------. | .------------. .------------. .------------. | .------------.
93 * | Node 1 | | | Node 3 | | Node 4 | | Node 5 | | | Node 6 |
94 * | |----|--| |--| |------| |--|--| |
95 * | | .-|--| | | | .---| | | | |
96 * '------------' | | '------------' '------------' | '------------' | '------------'
97 * | +----------------------------------|-------------------+
98 * .------------. | |
99 * | Node 2 | | |
100 * | |--'------------------------------------'
101 * | |
102 * '------------'
103 *
104 * Similarly, all nodes in the compile unit should either be operating on single values or not.
105 * Otherwise, assuming a node operates on single values and its output is used in 1) a non-single
106 * value pixel operation and 2) another node that expects single values, if that node was added to
107 * the pixel operation, its output will be non-single value, while it would have been a single
108 * value if it was not added to the pixel operation.
109 *
110 * To check for the single value type and domain compatibility between the compile unit and the
111 * node being processed, the single value type and the domain of the compile unit is assumed to be
112 * the single value type and the domain of the first node added to the compile unit, noting that
113 * the domain is optional, since it is not used if the compile unit is a single value one. The
114 * single value type and the domain of the compile unit are computed and set in the
115 * add_node_to_pixel_compile_unit method. When processing a node, the computed single value type
116 * and the computed domain of node are compared to the compile unit single value type and domain in
117 * the should_compile_pixel_compile_unit method. Node single value types and domains are computed
118 * in the is_pixel_node_single_value and compute_pixel_node_domain methods respectively, the latter
119 * of which is analogous to the Operation::compute_domain method for nodes that are not yet
120 * compiled. */
122 private:
123 /* A reference to the compositor context. */
124 const Context &context_;
125 /* A reference to the node execution schedule that is being compiled. */
126 const Schedule &schedule_;
127 /* Those two maps associate each node with the operation it was compiled into. Each node is
128 * either compiled into a node operation and added to node_operations, or compiled into a pixel
129 * operation and added to pixel_operations. Those maps are used to retrieve the results of
130 * outputs linked to the inputs of operations. See the get_result_from_output_socket method for
131 * more information. */
132 Map<DNode, NodeOperation *> node_operations_;
133 Map<DNode, PixelOperation *> pixel_operations_;
134 /* A contiguous subset of the node execution schedule that contains the group of nodes that will
135 * be compiled together into a pixel operation. See the discussion in COM_evaluator.hh for more
136 * information. */
137 PixelCompileUnit pixel_compile_unit_;
138 /* Stores whether the current pixel compile unit operates on single values. Only initialized when
139 * the pixel compile unit is not empty. */
140 bool is_pixel_compile_unit_single_value_;
141 /* The domain of the pixel compile unit if it was not a single value. Only initialized when the
142 * pixel compile unit is not empty and is not a single value. */
143 std::optional<Domain> pixel_compile_unit_domain_;
144
145 public:
146 /* Construct a compile state from the node execution schedule being compiled. */
147 CompileState(const Context &context, const Schedule &schedule);
148
149 /* Get a reference to the node execution schedule being compiled. */
150 const Schedule &get_schedule();
151
152 /* Add an association between the given node and the give node operation that the node was
153 * compiled into in the node_operations_ map. */
154 void map_node_to_node_operation(DNode node, NodeOperation *operation);
155
156 /* Add an association between the given node and the give pixel operation that the node was
157 * compiled into in the pixel_operations_ map. */
158 void map_node_to_pixel_operation(DNode node, PixelOperation *operation);
159
160 /* Returns a reference to the result of the operation corresponding to the given output that the
161 * given output's node was compiled to. */
163
164 /* Add the given node to the compile unit. And if the domain of the compile unit is not yet
165 * determined or was determined to be an identity domain, update it to the computed domain for
166 * the give node. */
168
169 /* Get a reference to the pixel compile unit. */
171
172 /* Returns true if the pixel compile unit operates on single values. */
174
175 /* Clear the compile unit. This should be called once the compile unit is compiled to ready it to
176 * track the next potential compile unit. */
178
179 /* Determines if the compile unit should be compiled based on a number of criteria give the node
180 * currently being processed. See the class description for a description of the method. */
182
183 /* Computes the number of pixel operation outputs that will be added for this node in the current
184 * pixel compile unit. This is essentially the number of outputs that will be added for the node
185 * in PixelOperation::populate_results_for_node. */
187
188 private:
189 /* Determines if the given pixel node operates on single values or not. The node operates on
190 * single values if all its inputs are single values, and consequently will also output single
191 * values. */
192 bool is_pixel_node_single_value(DNode node);
193
194 /* Compute the node domain of the given pixel node. This is analogous to the
195 * Operation::compute_domain method, except it is computed from the node itself as opposed to a
196 * compiled operation. See the discussion in COM_domain.hh for more information. */
197 Domain compute_pixel_node_domain(DNode node);
198};
199
200} // namespace blender::compositor
PixelCompileUnit & get_pixel_compile_unit()
CompileState(const Context &context, const Schedule &schedule)
Result & get_result_from_output_socket(DOutputSocket output)
void map_node_to_node_operation(DNode node, NodeOperation *operation)
void add_node_to_pixel_compile_unit(DNode node)
bool should_compile_pixel_compile_unit(DNode node)
void map_node_to_pixel_operation(DNode node, PixelOperation *operation)
int compute_pixel_node_operation_outputs_count(DNode node)
#define output
VectorSet< DNode > Schedule
VectorSet< DNode > PixelCompileUnit