Blender V5.3
COM_shader_operation.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 <memory>
8
9#include "BLI_map.hh"
10#include "BLI_vector_set.hh"
11
12#include "GPU_material.hh"
13#include "GPU_shader.hh"
14
16
17#include "COM_context.hh"
20#include "COM_shader_node.hh"
21
22namespace blender::compositor {
23
24struct Schedule;
25
26/* ------------------------------------------------------------------------------------------------
27 * Shader Operation
28 *
29 * A pixel operation that evaluates a shader compiled from the pixel compile unit using the GPU
30 * material compiler, see GPU_material.hh for more information. Also see the PixelOperation class
31 * for more information on pixel operations.
32 *
33 * An input to the pixel operation is declared for a distinct output socket as follows:
34 *
35 * - A texture is added to the shader, which will be bound to the result of the output socket
36 * during evaluation.
37 * - A GPU attribute is added to the GPU material for that output socket and is linked to the GPU
38 * input stack of the inputs linked to the output socket.
39 * - Code is emitted to initialize the values of the attributes by sampling the textures
40 * corresponding to each of the inputs.
41 * - The newly added attribute is mapped to the output socket in output_to_material_attribute_map_
42 * to share that same attributes for all inputs linked to the same output socket.
43 *
44 * An output to the pixel operation is declared for an output socket as follows:
45 *
46 * - An image is added in the shader where the output value will be written.
47 * - A storer GPU material node that stores the value of the output is added and linked to the GPU
48 * output stack of the output. The storer will store the value in the image identified by the
49 * index of the output given to the storer.
50 * - The storer functions are generated dynamically to map each index with its appropriate image.
51 *
52 * The GPU material code generator source is used to construct a compute shader that is then
53 * dispatched during operation evaluation after binding the inputs, outputs, and any necessary
54 * resources. */
56 private:
57 /* The GPU material backing the operation. This is created and compiled during construction and
58 * freed during destruction. */
59 GPUMaterial *material_;
60 /* A map that associates each node in the compile unit with an instance of its shader node. */
62 /* A map that associates the output socket of a node that is not part of the shader operation to
63 * the attribute that was created for it. This is used to share the same attribute with all
64 * inputs that are linked to the same output socket. */
65 Map<const bNodeSocket *, GPUNodeLink *> output_to_material_attribute_map_;
66 /* A map that associates implicit inputs to the attributes that were created for them. */
67 Map<ImplicitInputType, GPUNodeLink *> implicit_input_to_material_attribute_map_;
68
69 public:
70 /* Shaders operations have a limit on how many outputs and inputs they can support. Inputs use
71 * attributes which can't be more than 15 and outputs use images which can't be more than 8. */
72 static const int maximum_inputs_count = 15;
73 static const int maximum_outputs_count = 8;
74
75 /* Construct and compile a GPU material from the given shader compile unit and execution schedule
76 * by calling GPU_material_from_callbacks with the appropriate callbacks. */
78 NodeTreeEvaluator &node_tree_evaluator,
79 const ComputeContext &compute_context);
80
81 /* Free the GPU material. */
82 ~ShaderOperation() override;
83
84 /* Allocate the output results, bind the shader and all its needed resources, then dispatch the
85 * shader. */
86 void execute() override;
87
88 private:
89 /* Bind the uniform buffer of the GPU material as well as any color band textures needed by the
90 * GPU material. The compiled shader of the material is given as an argument and assumed to be
91 * bound. */
92 void bind_material_resources(gpu::Shader *shader);
93
94 /* Bind the input results of the operation to the appropriate textures in the GPU material. The
95 * attributes stored in output_to_material_attribute_map_ have names that match the texture
96 * samplers in the shader as well as the identifiers of the operation inputs that they correspond
97 * to. The compiled shader of the material is given as an argument and assumed to be bound. */
98 void bind_inputs(gpu::Shader *shader);
99
100 /* Bind the output results of the operation to the appropriate images in the GPU material. The
101 * name of the images in the shader match the identifier of their corresponding outputs. The
102 * compiled shader of the material is given as an argument and assumed to be bound. */
103 void bind_outputs(gpu::Shader *shader);
104
105 /* A static callback method of interface ConstructGPUMaterialFn that is passed to
106 * GPU_material_from_callbacks to construct the GPU material graph. The thunk parameter will be a
107 * pointer to the instance of ShaderOperation that is being compiled. The method goes over the
108 * compile unit and does the following for each node:
109 *
110 * - Instantiate a ShaderNode from the node and add it to shader_nodes_.
111 * - Link the inputs of the node if needed. The inputs are either linked to other nodes in the
112 * GPU material graph or are exposed as inputs to the shader operation itself if they are
113 * linked to nodes that are not part of the shader operation.
114 * - Call the compile method of the shader node to actually add and link the GPU material graph
115 * nodes.
116 * - If any of the outputs of the node are linked to nodes that are not part of the shader
117 * operation, they are exposed as outputs to the shader operation itself. */
118 static void construct_material(void *thunk, GPUMaterial *material);
119
120 /* Link the inputs of the node if needed. Unlinked inputs will be linked to constant values. If
121 * the input is linked to a node that is not part of the shader operation, the input will be
122 * exposed as an input to the shader operation and linked to it. While if the input is linked to
123 * a node that is part of the shader operation, then it is linked to that node in the GPU
124 * material node graph. */
125 void link_node_inputs(const bNode &node);
126
127 /* Link the GPU stack of the given unavailable input to a constant zero value setter GPU node.
128 * The value is ignored since the socket is unavailable, but the GPU Material compiler expects
129 * all inputs to be linked, even unavailable ones. */
130 void link_node_input_unavailable(const bNodeSocket &input);
131
132 /* Link the GPU stack of the given unlinked input to a constant value setter GPU node that
133 * supplies the value of the unlinked input. */
134 void link_node_input_constant(const bNodeSocket &input);
135
136 /* Given an unlinked input with an implicit input. Declare a new input to the operation for that
137 * implicit input if not done already and link it to the input link of the GPU node stack of the
138 * input socket. */
139 void link_node_input_implicit(const bNodeSocket &input);
140
141 /* Given the input socket of a node that is part of the shader operation which is linked to the
142 * given output socket of a node that is also part of the shader operation, just link the output
143 * link of the GPU node stack of the output socket to the input link of the GPU node stack of the
144 * input socket. This essentially establishes the needed links in the GPU material node graph. */
145 void link_node_input_internal(const bNodeSocket &input_socket, const bNodeSocket &output_socket);
146
147 /* Given the input socket of a node that is part of the shader operation which is linked to the
148 * given output socket of a node that is not part of the shader operation, declare a new
149 * operation input and link it to the input link of the GPU node stack of the input socket. An
150 * operation input is only declared if no input was already declared for that same output socket
151 * before. */
152 void link_node_input_external(const bNodeSocket &input_socket, const bNodeSocket &output_socket);
153
154 /* Gets the type of the result associated with the given output socket that is linked to the
155 * given input socket. In the base case, this is just derived from the type of the socket.
156 * However, if it belongs to a node that is outside of the pixel operation, get the type from the
157 * result associated with that output directly. */
158 ResultType get_source_output_type(const bNodeSocket &input, const bNodeSocket &output);
159
160 /* Given the input socket of a node that is part of the shader operation which is linked to the
161 * given output socket of a node that is not part of the shader operation, declare a new input to
162 * the operation that is represented in the GPU material by a newly created GPU attribute. It is
163 * assumed that no operation input was declared for this same output socket before. In the
164 * generate_code_for_inputs method, a texture will be added in the shader for each of the
165 * declared inputs, having the same name as the attribute. Additionally, code will be emitted to
166 * initialize the attributes by sampling their corresponding textures. */
167 void declare_operation_input(const bNodeSocket &input_socket, const bNodeSocket &output_socket);
168
169 /* Populate the output results of the shader operation for output sockets of the given node that
170 * are linked to nodes outside of the shader operation or are used to compute a preview for the
171 * node. */
172 void populate_results_for_node(const bNode &node);
173
174 /* Given the output socket of a node that is part of the shader operation which is linked to an
175 * input socket of a node that is not part of the shader operation, declare a new output to the
176 * operation and link it to an output storer passing in the index of the output. In the
177 * generate_code_for_outputs method, an image will be added in the shader for each of the
178 * declared outputs. Additionally, code will be emitted to define the storer functions that store
179 * the value in the appropriate image identified by the given index. */
180 void populate_operation_result(const bNodeSocket &output_socket);
181
182 /* Inserts an implicit conversion function that converts from the type of the output to the type
183 * of the input if not already the same. We assume the input is already linked. */
184 void convert_input_link_type(const bNodeSocket &input, const bNodeSocket &output);
185
186 /* A static callback method of interface GPUCodegenCallbackFn that is passed to
187 * GPU_material_from_callbacks to create the shader create info of the GPU material. The thunk
188 * parameter will be a pointer to the instance of ShaderOperation that is being compiled.
189 *
190 * This method first generates the necessary code to load the inputs and store the outputs. Then,
191 * it creates a compute shader from the generated sources. Finally, it adds the necessary GPU
192 * resources to the shader. */
193 static void generate_code(void *thunk, GPUMaterial *material, GPUCodegenOutput *code_generator);
194
195 /* Add an image in the shader for each of the declared outputs. Additionally, emit code to define
196 * the storer functions that store the given value in the appropriate image identified by the
197 * given index. */
198 std::string generate_code_for_outputs(gpu::shader::ShaderCreateInfo &shader_create_info);
199
200 /* Add a texture will in the shader for each of the declared inputs/attributes in the operation,
201 * having the same name as the attribute. Additionally, emit code to initialize the attributes by
202 * sampling their corresponding textures. */
203 std::string generate_code_for_inputs(GPUMaterial *material,
204 gpu::shader::ShaderCreateInfo &shader_create_info);
205};
206
207} // namespace blender::compositor
#define output
Context & context() const
Definition operation.cc:57
PixelOperation(Context &context, NodeTreeEvaluator &node_tree_evaluator, const ComputeContext &compute_context, const bool is_single_value)
ShaderOperation(Context &context, NodeTreeEvaluator &node_tree_evaluator, const ComputeContext &compute_context)
Describe inputs & outputs, stage interfaces, resources and sources of a shader. If all data is correc...