Blender V5.3
shader_operation.cc
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#include <memory>
6#include <sstream>
7#include <string>
8
9#include <fmt/format.h>
10
11#include "BLI_assert.hh"
13#include "BLI_listbase.hh"
14#include "BLI_map.hh"
15#include "BLI_math_euler.hh"
16#include "BLI_math_vector_c.hh"
18#include "BLI_string_ref.hh"
19#include "BLI_ustring.hh"
20#include "BLI_vector_set.hh"
21
22#include "GPU_debug.hh"
23#include "GPU_material.hh"
24#include "GPU_shader.hh"
25#include "GPU_texture.hh"
26#include "GPU_uniform_buffer.hh"
27
29
31#include "DNA_node_types.h"
32
34
35#include "BKE_node.hh"
36#include "BKE_node_runtime.hh"
38
39#include "COM_context.hh"
42#include "COM_result.hh"
43#include "COM_scheduler.hh"
44#include "COM_shader_node.hh"
46#include "COM_utilities.hh"
47
48namespace blender::compositor {
49
51 NodeTreeEvaluator &node_tree_evaluator,
52 const ComputeContext &compute_context)
53 : PixelOperation(context, node_tree_evaluator, compute_context, false)
54{
55 const int64_t uuid = this->context().get_precision() == ResultPrecision::Full ? 0 : 1;
57 GPU_MAT_COMPOSITOR, &construct_material, &generate_code, this, uuid);
58}
59
64
66{
67 GPU_debug_group_begin("ShaderOperation");
68 const Domain domain = compute_domain();
69 for (StringRef identifier : output_sockets_to_output_identifiers_map_.values()) {
70 Result &result = get_result(identifier);
71 result.allocate_texture(domain);
72 }
73
76
77 bind_material_resources(shader);
78 bind_inputs(shader);
79 bind_outputs(shader);
80
82
88}
89
90void ShaderOperation::bind_material_resources(gpu::Shader *shader)
91{
92 /* Bind the uniform buffer of the material if it exists. It may not exist if the GPU material has
93 * no uniforms. */
95 if (ubo) {
97 }
98
99 /* Bind color band textures needed by curve and ramp nodes. */
100 ListBaseT<GPUMaterialTexture> textures = GPU_material_textures(material_);
101 for (GPUMaterialTexture &texture : textures) {
102 if (texture.colorband) {
103 const int texture_image_unit = GPU_shader_get_sampler_binding(shader, texture.sampler_name);
104 GPU_texture_bind(*texture.colorband, texture_image_unit);
105 }
106 }
107}
108
109void ShaderOperation::bind_inputs(gpu::Shader *shader)
110{
111 /* Attributes represents the inputs of the operation and their names match those of the inputs of
112 * the operation as well as the corresponding texture samples in the shader. */
113 ListBaseT<GPUMaterialAttribute> attributes = GPU_material_attributes(material_);
114 for (GPUMaterialAttribute &attribute : attributes) {
115 get_input(attribute.name).bind_as_texture(shader, attribute.name);
116 }
117}
118
119void ShaderOperation::bind_outputs(gpu::Shader *shader)
120{
121 for (StringRefNull output_identifier : output_sockets_to_output_identifiers_map_.values()) {
122 get_result(output_identifier).bind_as_image(shader, output_identifier.c_str());
123 }
124}
125
126void ShaderOperation::construct_material(void *thunk, GPUMaterial *material)
127{
128 ShaderOperation *operation = static_cast<ShaderOperation *>(thunk);
129 operation->material_ = material;
130 for (const bNode *node : operation->node_tree_evaluator_.pixel_compile_unit()) {
131 operation->shader_nodes_.add_new(node, std::make_unique<ShaderNode>(*node));
132
133 operation->link_node_inputs(*node);
134
135 operation->shader_nodes_.lookup(node)->compile(material);
136
137 operation->populate_results_for_node(*node);
138 }
139}
140
141void ShaderOperation::link_node_inputs(const bNode &node)
142{
143 for (const bNodeSocket *input : node.input_sockets()) {
144 /* The input is unavailable and unused, but it still needs to be linked as this is what the GPU
145 * material compiler expects. */
146 if (!is_socket_available(input)) {
147 this->link_node_input_unavailable(*input);
148 continue;
149 }
150
151 if (node_tree_evaluator_.schedule().unneeded_inputs.contains(input)) {
152 this->link_node_input_unavailable(*input);
153 continue;
154 }
155
156 const bNodeSocket *output = get_output_linked_to_input(*input);
157 if (!output) {
158 const InputDescriptor input_descriptor = input_descriptor_from_input_socket(input);
159 if (!input_descriptor.implicit_input.has_value()) {
160 /* No implicit input, so link a constant setter node for it that holds the input value. */
161 this->link_node_input_constant(*input);
162 }
163 else {
164 this->link_node_input_implicit(*input);
165 }
166 continue;
167 }
168
169 /* If the source node is part of the shader operation, then the link is internal to the GPU
170 * material graph and is linked appropriately. */
172 this->link_node_input_internal(*input, *output);
173 }
174 else {
175 /* Otherwise, the source node is not part of the shader operation, then the link is
176 * external to the GPU material graph and an input to the shader operation must be declared
177 * and linked to the node input. */
178 this->link_node_input_external(*input, *output);
179 }
180
181 /* Implicitly convert the input link type to the expected input type if needed. */
182 this->convert_input_link_type(*input, *output);
183 }
184}
185
186static const char *get_set_function_name(const ResultType type)
187{
188 switch (type) {
190 return "set_float";
192 return "set_float2";
194 return "set_float3";
196 return "set_float4";
198 return "set_color";
199 case ResultType::Int:
200 return "set_int";
201 case ResultType::Int2:
202 return "set_int2";
203 case ResultType::Int3:
204 return "set_int3";
205 case ResultType::Int4:
206 return "set_int4";
207 case ResultType::Bool:
208 return "set_bool";
210 return "set_float4x4";
211 case ResultType::Menu:
212 return "set_int";
214 return "set_quaternion";
218 case ResultType::Font:
220 case ResultType::Text:
221 case ResultType::Mask:
224 /* Single only types do not support GPU code path. */
227 break;
228 }
229
231 return nullptr;
232}
233
234void ShaderOperation::link_node_input_unavailable(const bNodeSocket &input)
235{
236 ShaderNode &node = *shader_nodes_.lookup(&input.owner_node());
237 GPUNodeStack &stack = node.get_input(input.identifier);
238
239 /* Create a constant link with some zero value. The value is arbitrary and ignored. See the
240 * method description. */
242 GPUNodeLink *link = GPU_constant(stack);
243
244 const ResultType type = get_node_socket_result_type(&input);
245 GPU_link(material_, get_set_function_name(type), link, &stack.link);
246}
247
248/* Initializes the given GPU node stack from the default value of the given input socket
249 * and creates a constant or uniform link that references the typed stack storage. */
251{
252 /* Use a constant for socket types that rarely change like booleans and menus, and use a
253 * uniform for socket types that might change a lot to avoid excessive shader recompilation. */
254 switch (input.type) {
255 case SOCK_INT: {
256 stack.value = input.default_value_typed<bNodeSocketValueInt>()->value;
257 return GPU_uniform(stack);
258 }
259 case SOCK_MENU: {
260 stack.value = input.default_value_typed<bNodeSocketValueMenu>()->value;
261 return GPU_constant(stack);
262 }
263 case SOCK_INT_VECTOR: {
265 input.default_value_typed<bNodeSocketValueIntVector>();
266 switch (storage->dimensions) {
267 case 2:
268 stack.value = int2(storage->value);
269 return GPU_uniform(stack);
270 case 3:
271 stack.value = int3(storage->value);
272 return GPU_uniform(stack);
273 default:
274 break;
275 }
276 break;
277 }
278 case SOCK_BOOLEAN: {
279 stack.value = bool(input.default_value_typed<bNodeSocketValueBoolean>()->value);
280 return GPU_constant(stack);
281 }
282 case SOCK_FLOAT: {
283 stack.value = input.default_value_typed<bNodeSocketValueFloat>()->value;
284 return GPU_uniform(stack);
285 }
286 case SOCK_VECTOR: {
287 const bNodeSocketValueVector *storage = input.default_value_typed<bNodeSocketValueVector>();
288 switch (storage->dimensions) {
289 case 2:
290 stack.value = float2(storage->value);
291 return GPU_uniform(stack);
292 case 3:
293 stack.value = float3(storage->value);
294 return GPU_uniform(stack);
295 case 4:
296 stack.value = float4(storage->value);
297 return GPU_uniform(stack);
298 default:
299 break;
300 }
301 break;
302 }
303 case SOCK_RGBA: {
304 stack.value = float4(Color(input.default_value_typed<bNodeSocketValueRGBA>()->value));
305 return GPU_uniform(stack);
306 }
307 case SOCK_ROTATION: {
308 const bNodeSocketValueRotation *rotation =
309 input.default_value_typed<bNodeSocketValueRotation>();
310 const math::EulerXYZ euler(float3(rotation->value_euler));
311 stack.value = float4(math::to_quaternion(euler));
312 return GPU_uniform(stack);
313 }
314 case SOCK_MATRIX:
315 /* Matrix sockets do not have default values. */
316 break;
317 case SOCK_STRING:
318 case SOCK_OBJECT:
319 case SOCK_IMAGE:
320 case SOCK_FONT:
321 case SOCK_SCENE:
322 case SOCK_TEXT_ID:
323 case SOCK_MASK:
324 case SOCK_BUNDLE:
325 case SOCK_CLOSURE:
326 /* Single only types do not support GPU code path. */
328 break;
329 default:
330 break;
331 }
332
334 return nullptr;
335}
336
337void ShaderOperation::link_node_input_constant(const bNodeSocket &input)
338{
339 ShaderNode &node = *shader_nodes_.lookup(&input.owner_node());
340 GPUNodeStack &stack = node.get_input(input.identifier);
341
342 /* Matrix sockets have no socket default values, so we default initialize them. */
343 if (input.type == SOCK_MATRIX) {
344 GPU_link(material_, "set_float4x4_default", &stack.link);
345 return;
346 }
347
348 GPUNodeLink *link = get_input_value_link(input, stack);
349
350 const ResultType type = get_node_socket_result_type(&input);
351 const char *function_name = get_set_function_name(type);
352 GPU_link(material_, function_name, link, &stack.link);
353}
354
355void ShaderOperation::link_node_input_implicit(const bNodeSocket &input)
356{
357 ShaderNode &node = *shader_nodes_.lookup(&input.owner_node());
358 GPUNodeStack &stack = node.get_input(input.identifier);
359
360 const InputDescriptor input_descriptor = input_descriptor_from_input_socket(&input);
361 const ImplicitInputType implicit_input = input_descriptor.implicit_input.value();
362
363 /* An input was already declared for that implicit input, so no need to declare it again and we
364 * just link it. */
365 if (implicit_input_to_material_attribute_map_.contains(implicit_input)) {
366 /* But first we update the domain priority of the input descriptor to be the higher priority of
367 * the existing descriptor and the descriptor of the new input socket. That's because the same
368 * implicit input might be used in inputs inside the shader operation which have different
369 * priorities. */
370 InputDescriptor &existing_input_descriptor = this->get_input_descriptor(
372 existing_input_descriptor.domain_priority = math::min(
373 existing_input_descriptor.domain_priority, input_descriptor.domain_priority);
374
375 /* Link the attribute representing the shader operation input corresponding to the implicit
376 * input. */
377 stack.link = implicit_input_to_material_attribute_map_.lookup(implicit_input);
378 return;
379 }
380
381 const int implicit_input_index = implicit_inputs_to_input_identifiers_map_.size();
382 const std::string input_identifier = "implicit_input" + std::to_string(implicit_input_index);
383 declare_input_descriptor(input_identifier, input_descriptor);
384
385 /* Map the implicit input to the identifier of the operation input that was declared for it. */
386 implicit_inputs_to_input_identifiers_map_.add_new(implicit_input, input_identifier);
387
388 /* Add a new GPU attribute representing an input to the GPU material. Instead of using the
389 * attribute directly, we link it to an appropriate set function and use its output link instead.
390 * This is needed because the `gputype` member of the attribute is only initialized if it is
391 * linked to a GPU node. */
392 GPUNodeLink *attribute_link;
393 GPU_link(material_,
394 get_set_function_name(input_descriptor.type),
395 GPU_attribute(material_, CD_AUTO_FROM_NAME, input_identifier.c_str()),
396 &attribute_link);
397
398 /* Map the implicit input to the attribute that was created for it. */
399 implicit_input_to_material_attribute_map_.add(implicit_input, attribute_link);
400
401 /* Link the attribute representing the shader operation input corresponding to the implicit
402 * input. */
403 stack.link = attribute_link;
404}
405
406void ShaderOperation::link_node_input_internal(const bNodeSocket &input_socket,
407 const bNodeSocket &output_socket)
408{
409 ShaderNode &output_node = *shader_nodes_.lookup(&output_socket.owner_node());
410 GPUNodeStack &output_stack = output_node.get_output(output_socket.identifier);
411
412 ShaderNode &input_node = *shader_nodes_.lookup(&input_socket.owner_node());
413 GPUNodeStack &input_stack = input_node.get_input(input_socket.identifier);
414
415 input_stack.link = output_stack.link;
416}
417
418/* Creates a constant or a uniform link that references the single value of the given result. */
420{
421 /* Use a constant for types that rarely change like booleans and menus, and use a uniform for
422 * socket types that might change a lot to avoid excessive shader recompilation. */
423 switch (result.type()) {
425 return GPU_uniform(&result.get_single_value<float>());
427 return GPU_uniform(result.get_single_value<float2>());
429 return GPU_uniform(result.get_single_value<float3>());
431 return GPU_uniform(result.get_single_value<float4>());
433 return GPU_uniform(result.get_single_value<Color>());
434 case ResultType::Int:
435 return GPU_uniform(&result.get_single_value<int32_t>());
436 case ResultType::Int2:
437 return GPU_uniform(result.get_single_value<int2>());
438 case ResultType::Int3:
439 return GPU_uniform(result.get_single_value<int3>());
440 case ResultType::Int4:
441 return GPU_uniform(result.get_single_value<int4>());
442 case ResultType::Bool:
443 return GPU_constant(&result.get_single_value<bool>());
445 return GPU_uniform(result.get_single_value<float4x4>().base_ptr());
446 case ResultType::Menu:
447 return GPU_constant(&result.get_single_value<nodes::MenuValue>().value);
449 return GPU_uniform(result.get_single_value<math::Quaternion>());
453 case ResultType::Font:
455 case ResultType::Text:
456 case ResultType::Mask:
459 /* Single only types do not support GPU code path. */
462 break;
463 }
464
466 return nullptr;
467}
468
469void ShaderOperation::link_node_input_external(const bNodeSocket &input_socket,
470 const bNodeSocket &output_socket)
471{
472 ShaderNode &node = *shader_nodes_.lookup(&input_socket.owner_node());
473 GPUNodeStack &stack = node.get_input(input_socket.identifier);
474
475 /* The output is a single value, so create an internal constant link instead of declaring an
476 * input for it, it follows that the result needs to be released since it will no longer be
477 * referenced by the operation. */
479 if (result.is_single_value()) {
480 /* Single only types do not support GPU code path. */
482 this->link_node_input_unavailable(input_socket);
483 result.release();
484 return;
485 }
486
487 GPUNodeLink *link = get_result_single_value_link(result);
488 const char *function_name = get_set_function_name(result.type());
489 GPU_link(material_, function_name, link, &stack.link);
490 result.release();
491 return;
492 }
493
494 if (!output_to_material_attribute_map_.contains(&output_socket)) {
495 /* No input was declared for that output yet, so declare it. */
496 declare_operation_input(input_socket, output_socket);
497 }
498 else {
499 /* An input was already declared for that same output socket, so no need to declare it again.
500 * But we update the domain priority of the input descriptor to be the higher priority of the
501 * existing descriptor and the descriptor of the new input socket. That's because the same
502 * output might be connected to multiple inputs inside the shader operation which have
503 * different priorities. */
504 const std::string input_identifier = outputs_to_declared_inputs_map_.lookup(&output_socket);
505 InputDescriptor &input_descriptor = this->get_input_descriptor(input_identifier);
506 input_descriptor.domain_priority = math::min(
507 input_descriptor.domain_priority,
508 input_descriptor_from_input_socket(&input_socket).domain_priority);
509
510 /* Increment the input's reference count. */
511 inputs_to_reference_counts_map_.lookup(input_identifier)++;
512 }
513
514 /* Link the attribute representing the shader operation input corresponding to the given output
515 * socket. */
516 stack.link = output_to_material_attribute_map_.lookup(&output_socket);
517}
518
519ResultType ShaderOperation::get_source_output_type(const bNodeSocket &input,
520 const bNodeSocket &output)
521{
522 /* Output is inside the pixel operation, get its type from socket directly. */
525 }
526
527 /* Otherwise, it is an external input and we know its result, so get the type from the result
528 * directly. An exception is when the output is a single value only type, which is not supported
529 * on GPU, so we assume the input type. */
532 return get_node_socket_result_type(&input);
533 }
534 return result.type();
535}
536
537void ShaderOperation::declare_operation_input(const bNodeSocket &input_socket,
538 const bNodeSocket &output_socket)
539{
540 const int input_index = output_to_material_attribute_map_.size();
541 std::string input_identifier = "input" + std::to_string(input_index);
542
543 /* Declare the input descriptor for this input and prefer to declare its type to be the same as
544 * the type of the output because doing type conversion in the shader is much cheaper. */
545 InputDescriptor input_descriptor = input_descriptor_from_input_socket(&input_socket);
546 input_descriptor.type = this->get_source_output_type(input_socket, output_socket);
547
548 declare_input_descriptor(input_identifier, input_descriptor);
549
550 /* Add a new GPU attribute representing an input to the GPU material. Instead of using the
551 * attribute directly, we link it to an appropriate set function and use its output link instead.
552 * This is needed because the `gputype` member of the attribute is only initialized if it is
553 * linked to a GPU node. */
554 GPUNodeLink *attribute_link;
555 GPU_link(material_,
556 get_set_function_name(input_descriptor.type),
557 GPU_attribute(material_, CD_AUTO_FROM_NAME, input_identifier.c_str()),
558 &attribute_link);
559
560 /* Map the output socket to the attribute that was created for it. */
561 output_to_material_attribute_map_.add(&output_socket, attribute_link);
562
563 /* Map the identifier of the operation input to the output socket it is linked to. */
564 inputs_to_linked_outputs_map_.add_new(input_identifier, &output_socket);
565
566 /* Map the output socket to the identifier of the operation input that was declared for it. */
567 outputs_to_declared_inputs_map_.add_new(&output_socket, input_identifier);
568
569 /* Map the identifier of the operation input to a reference count of 1, this will later be
570 * incremented if that same output was referenced again. */
571 inputs_to_reference_counts_map_.add_new(input_identifier, 1);
572}
573
574void ShaderOperation::populate_results_for_node(const bNode &node)
575{
576 /* Only compute previews if they are needed and the node group is active. */
577 const bool node_needs_preview = is_node_preview_needed(node);
578 const bool needs_node_previews = flag_is_set(this->context().needed_side_effect_output_types(),
580 const bNodeSocket *preview_output = nullptr;
581 if (node_needs_preview && needs_node_previews) {
582 preview_output = find_preview_output_socket(node);
583 }
584
585 for (const bNodeSocket *output : node.output_sockets()) {
587 continue;
588 }
589
590 /* If the output is referenced by the schedule outside of the pixel compile unit, then an
591 * output result needs to be populated for it. */
592 const bool is_operation_output = compute_output_reference_count(
593 *output,
596
597 /* If the output is used as the node preview, then an output result needs to be populated for
598 * it, and we additionally keep track of that output to later compute the previews from. */
599 const bool is_preview_output = output == preview_output;
600 if (is_preview_output) {
602 }
603
604 if (is_operation_output || is_preview_output) {
605 populate_operation_result(*output);
606 }
607 }
608}
609
610static const char *get_store_function_name(ResultType type)
611{
612 switch (type) {
614 return "node_compositor_store_output_float";
616 return "node_compositor_store_output_float2";
618 return "node_compositor_store_output_float3";
620 return "node_compositor_store_output_float4";
622 return "node_compositor_store_output_color";
623 case ResultType::Int:
624 return "node_compositor_store_output_int";
625 case ResultType::Int2:
626 return "node_compositor_store_output_int2";
627 case ResultType::Int3:
628 return "node_compositor_store_output_int3";
629 case ResultType::Int4:
630 return "node_compositor_store_output_int4";
631 case ResultType::Bool:
632 return "node_compositor_store_output_bool";
634 return "node_compositor_store_output_float4x4";
635 case ResultType::Menu:
636 return "node_compositor_store_output_menu";
638 return "node_compositor_store_output_quaternion";
642 case ResultType::Font:
644 case ResultType::Text:
645 case ResultType::Mask:
648 /* Single only types do not support GPU code path. */
651 break;
652 }
653
655 return nullptr;
656}
657
658void ShaderOperation::populate_operation_result(const bNodeSocket &output_socket)
659{
660 const uint output_id = output_sockets_to_output_identifiers_map_.size();
661 std::string output_identifier = "output" + std::to_string(output_id);
662
663 const ResultType result_type = get_node_socket_result_type(&output_socket);
664 populate_result(output_identifier, result_type);
665
666 /* Map the output socket to the identifier of the newly populated result. */
667 output_sockets_to_output_identifiers_map_.add_new(&output_socket, output_identifier);
668
669 ShaderNode &node = *shader_nodes_.lookup(&output_socket.owner_node());
670 GPUNodeLink *output_link = node.get_output(output_socket.identifier).link;
671
672 /* Link the output node stack to an output storer storing in the appropriate result. The result
673 * is identified by its index in the operation and the index is encoded as a float to be passed
674 * to the GPU function. Additionally, create an output link from the storer node to declare as an
675 * output to the GPU material. This storer output link is a dummy link in the sense that its
676 * value is ignored since it is already written in the output, but it is used to track nodes that
677 * contribute to the output of the compositor node tree. */
678 GPUNodeLink *storer_output_link;
679 GPUNodeLink *id_link = GPU_constant(reinterpret_cast<float *>(const_cast<uint *>(&output_id)));
680 const char *store_function_name = get_store_function_name(result_type);
681 GPU_link(material_, store_function_name, id_link, output_link, &storer_output_link);
682
683 /* Declare the output link of the storer node as an output of the GPU material to help the GPU
684 * code generator to track the nodes that contribute to the output of the shader. */
685 GPU_material_add_output_link_composite(material_, storer_output_link);
686}
687
689{
690 const void *value = Result::cpp_type(type).default_value();
691 switch (type) {
699 return GPU_constant(static_cast<const float *>(value));
700 case ResultType::Int:
701 case ResultType::Int2:
702 case ResultType::Int3:
703 case ResultType::Int4:
704 case ResultType::Menu:
705 return GPU_constant(static_cast<const int *>(value));
706 case ResultType::Bool:
707 return GPU_constant(static_cast<const bool *>(value));
711 case ResultType::Font:
713 case ResultType::Text:
714 case ResultType::Mask:
717 /* Single only types do not support GPU code path. */
720 break;
721 }
722
724 return nullptr;
725}
726
727void ShaderOperation::convert_input_link_type(const bNodeSocket &input, const bNodeSocket &output)
728{
729 const ResultType source_type = this->get_source_output_type(input, output);
730 const ResultType target_type = get_node_socket_result_type(&input);
731 if (target_type == source_type) {
732 return;
733 }
734
735 ShaderNode &input_node = *shader_nodes_.lookup(&input.owner_node());
736 GPUNodeStack &input_stack = input_node.get_input(input.identifier);
737
738 /* Conversion is not possible, link a zero constant instead. */
739 const bke::DataTypeConversions &conversions = bke::get_implicit_type_conversions();
740 if (!conversions.is_convertible(Result::cpp_type(source_type), Result::cpp_type(target_type))) {
741 GPU_link(material_,
742 get_set_function_name(target_type),
743 get_default_input_value_link(target_type),
744 &input_stack.link);
745 return;
746 }
747
748 const UString function_name = UString(
749 fmt::format("{}_to_{}", Result::type_name(source_type), Result::type_name(target_type)));
750
751 if (source_type == ResultType::Color &&
753 {
754 float luminance_coefficients[3];
756 GPU_link(material_,
757 function_name.c_str(),
758 input_stack.link,
759 GPU_constant(luminance_coefficients),
760 &input_stack.link);
761 }
762 else {
763 GPU_link(material_, function_name.c_str(), input_stack.link, &input_stack.link);
764 }
765}
766
767using namespace gpu::shader;
768
769void ShaderOperation::generate_code(void *thunk,
770 GPUMaterial *material,
771 GPUCodegenOutput *code_generator_output)
772{
773 ShaderOperation *operation = static_cast<ShaderOperation *>(thunk);
774 ShaderCreateInfo &shader_create_info = *reinterpret_cast<ShaderCreateInfo *>(
775 code_generator_output->create_info);
776
777 shader_create_info.local_group_size(16, 16);
778
779 /* Add implementation for the functions inserted by the code generator.. */
780 shader_create_info.typedef_source("gpu_shader_compositor_code_generation.glsl");
781
782 /* The source shader is a compute shader with a main function that calls the dynamically
783 * generated evaluate function. The evaluate function includes the serialized GPU material graph
784 * preceded by code that initialized the inputs of the operation. Additionally, the storer
785 * functions that writes the outputs are defined outside the evaluate function. */
786 shader_create_info.compute_source("gpu_shader_compositor_main.glsl");
787
788 std::string generated_resource_header = shader_create_info.typedef_source_generated;
789 /* Insert resource declaration after types declaration. */
790 generated_resource_header += "#ifdef CREATE_INFO_RES_PASS_compositor_nodetrees\n";
791 generated_resource_header += "CREATE_INFO_RES_PASS_compositor_nodetrees\n";
792 generated_resource_header += "#endif\n";
793 generated_resource_header += "#ifdef CREATE_INFO_RES_BATCH_compositor_nodetrees\n";
794 generated_resource_header += "CREATE_INFO_RES_BATCH_compositor_nodetrees\n";
795 generated_resource_header += "#endif\n";
796 generated_resource_header += "#ifdef CREATE_INFO_RES_GEOMETRY_compositor_nodetrees\n";
797 generated_resource_header += "CREATE_INFO_RES_GEOMETRY_compositor_nodetrees\n";
798 generated_resource_header += "#endif\n";
799 generated_resource_header += "\n";
800
801 shader_create_info.generated_sources.append(
802 {"gpu_shader_compositor_nodetree_type.glsl", {}, generated_resource_header});
803
804 std::string store_code = operation->generate_code_for_outputs(shader_create_info);
805 shader_create_info.generated_sources.append(
806 {"gpu_shader_compositor_store.glsl", {}, store_code});
807
808 std::string eval_code;
809 eval_code += "void evaluate()\n{\n";
810
811 eval_code += operation->generate_code_for_inputs(material, shader_create_info);
812
813 eval_code += code_generator_output->composite.serialized;
814
815 eval_code += "}\n";
816
817 /* Bit of a workaround. Make sure that the nodetree UBO is part of the compositor_nodetrees
818 * interface and not the interface with the shader name. */
819 for (auto &res : shader_create_info.batch_resources_) {
820 res.info_name = "compositor_nodetrees";
821 }
822 for (auto &res : shader_create_info.pass_resources_) {
823 res.info_name = "compositor_nodetrees";
824 }
825 for (auto &res : shader_create_info.geometry_resources_) {
826 res.info_name = "compositor_nodetrees";
827 }
828
829 Vector<StringRefNull> dependencies = code_generator_output->composite.dependencies;
830 dependencies.prepend("compositor_node_tree_infos.hh");
831 dependencies.prepend("gpu_shader_compositor_texture_utilities.glsl");
832 dependencies.prepend("gpu_shader_compositor_code_generation.glsl");
833
834 shader_create_info.generated_sources.append(
835 {"gpu_shader_compositor_eval.glsl", dependencies, eval_code});
836}
837
838/* Texture storers in the shader always take a [i]vec4 as an argument, so encode each type in an
839 * [i]vec4 appropriately. */
841{
842 switch (type) {
844 return "vec4(value)";
846 return "vec4(value, 0.0f, 0.0f)";
848 return "vec4(value, 0.0f)";
850 return "value";
852 return "value";
853 case ResultType::Int:
854 return "ivec4(value)";
855 case ResultType::Int2:
856 return "ivec4(value, 0, 0)";
857 case ResultType::Int3:
858 return "ivec4(value, 0)";
859 case ResultType::Int4:
860 return "ivec4(value)";
861 case ResultType::Bool:
862 return "ivec4(int(value))";
864 return "value";
865 case ResultType::Menu:
866 return "ivec4(value)";
868 return "value";
872 case ResultType::Font:
874 case ResultType::Text:
875 case ResultType::Mask:
878 /* Single only types do not support GPU code path. */
881 break;
882 }
883
885 return nullptr;
886}
887
889{
890 switch (type) {
897 return ImageType::Float2D;
898 case ResultType::Int:
899 case ResultType::Int2:
900 case ResultType::Int3:
901 case ResultType::Int4:
902 case ResultType::Bool:
903 case ResultType::Menu:
904 return ImageType::Int2D;
906 return ImageType::Float2DArray;
910 case ResultType::Font:
912 case ResultType::Text:
913 case ResultType::Mask:
916 /* Single only types do not support GPU code path. */
919 break;
920 }
921
923 return ImageType::Float2D;
924}
925
926std::string ShaderOperation::generate_code_for_outputs(ShaderCreateInfo &shader_create_info)
927{
928 const std::string store_float_function_header = "void store_float(const uint id, float value)";
929 const std::string store_float2_function_header = "void store_float2(const uint id, vec2 value)";
930 const std::string store_float3_function_header = "void store_float3(const uint id, vec3 value)";
931 const std::string store_float4_function_header = "void store_float4(const uint id, vec4 value)";
932 const std::string store_color_function_header = "void store_color(const uint id, vec4 value)";
933 const std::string store_int_function_header = "void store_int(const uint id, int value)";
934 const std::string store_int2_function_header = "void store_int2(const uint id, ivec2 value)";
935 const std::string store_int3_function_header = "void store_int3(const uint id, ivec3 value)";
936 const std::string store_int4_function_header = "void store_int4(const uint id, ivec4 value)";
937 const std::string store_bool_function_header = "void store_bool(const uint id, bool value)";
938 const std::string store_float4x4_function_header =
939 "void store_float4x4(const uint id, float4x4 value)";
940 const std::string store_menu_function_header = "void store_menu(const uint id, int value)";
941 const std::string store_quaternion_function_header =
942 "void store_quaternion(const uint id, vec4 value)";
943
944 /* Each of the store functions is essentially a single switch case on the given ID, so start by
945 * opening the function with a curly bracket followed by opening a switch statement in each of
946 * the functions. */
947 std::stringstream store_float_function;
948 std::stringstream store_float2_function;
949 std::stringstream store_float3_function;
950 std::stringstream store_float4_function;
951 std::stringstream store_color_function;
952 std::stringstream store_int_function;
953 std::stringstream store_int2_function;
954 std::stringstream store_int3_function;
955 std::stringstream store_int4_function;
956 std::stringstream store_bool_function;
957 std::stringstream store_float4x4_function;
958 std::stringstream store_menu_function;
959 std::stringstream store_quaternion_function;
960 const std::string store_function_start = "\n{\n switch (id) {\n";
961 store_float_function << store_float_function_header << store_function_start;
962 store_float2_function << store_float2_function_header << store_function_start;
963 store_float3_function << store_float3_function_header << store_function_start;
964 store_float4_function << store_float4_function_header << store_function_start;
965 store_color_function << store_color_function_header << store_function_start;
966 store_int_function << store_int_function_header << store_function_start;
967 store_int2_function << store_int2_function_header << store_function_start;
968 store_int3_function << store_int3_function_header << store_function_start;
969 store_int4_function << store_int4_function_header << store_function_start;
970 store_bool_function << store_bool_function_header << store_function_start;
971 store_float4x4_function << store_float4x4_function_header << store_function_start;
972 store_menu_function << store_menu_function_header << store_function_start;
973 store_quaternion_function << store_quaternion_function_header << store_function_start;
974
975 shader_create_info.builtins(BuiltinBits::GLOBAL_INVOCATION_ID);
976
977 int output_index = 0;
978 for (StringRefNull output_identifier : output_sockets_to_output_identifiers_map_.values()) {
979 const Result &result = get_result(output_identifier);
980
981 /* Add a write-only image for this output where its values will be written. */
982 shader_create_info.image(output_index,
983 result.get_gpu_texture_format(),
984 Qualifier::write,
986 output_identifier,
987 Frequency::PASS);
988 output_index++;
989
990 const std::string case_identifier = StringRef(output_identifier).drop_known_prefix("output");
991 const std::string case_line = " case " + case_identifier + ":\n";
992 const std::string break_line = " break;\n";
993
994 const std::string store_expression = glsl_store_expression_from_result_type(result.type());
995
996 /* Add a case for the index of this output followed by a break statement. */
997 std::stringstream common_case_code;
998 common_case_code << case_line;
999 common_case_code << " imageStore(" << output_identifier
1000 << ", ivec2(gl_GlobalInvocationID.xy), " << store_expression << ");\n";
1001 common_case_code << break_line;
1002
1003 /* Only add the case to the function with the matching type. */
1004 switch (result.type()) {
1005 case ResultType::Float:
1006 store_float_function << common_case_code.str();
1007 break;
1008 case ResultType::Float2:
1009 store_float2_function << common_case_code.str();
1010 break;
1011 case ResultType::Float3:
1012 store_float3_function << common_case_code.str();
1013 break;
1014 case ResultType::Float4:
1015 store_float4_function << common_case_code.str();
1016 break;
1017 case ResultType::Color:
1018 store_color_function << common_case_code.str();
1019 break;
1020 case ResultType::Int:
1021 store_int_function << common_case_code.str();
1022 break;
1023 case ResultType::Int2:
1024 store_int2_function << common_case_code.str();
1025 break;
1026 case ResultType::Int3:
1027 store_int3_function << common_case_code.str();
1028 break;
1029 case ResultType::Int4:
1030 store_int4_function << common_case_code.str();
1031 break;
1032 case ResultType::Bool:
1033 store_bool_function << common_case_code.str();
1034 break;
1036 /* Each column of the matrix is stored in one layer of the texture. */
1037 store_float4x4_function << case_line;
1038 store_float4x4_function << " for (int i = 0; i < 4; i++) {\n";
1039 store_float4x4_function << " imageStore(" << output_identifier
1040 << ", ivec3(gl_GlobalInvocationID.xy, i), value[i]);\n";
1041 store_float4x4_function << " }\n";
1042 store_float4x4_function << break_line;
1043 break;
1044 case ResultType::Menu:
1045 store_menu_function << common_case_code.str();
1046 break;
1048 store_quaternion_function << common_case_code.str();
1049 break;
1050 case ResultType::String:
1051 case ResultType::Object:
1052 case ResultType::Image:
1053 case ResultType::Font:
1054 case ResultType::Scene:
1055 case ResultType::Text:
1056 case ResultType::Mask:
1057 case ResultType::Bundle:
1059 /* Single only types do not support GPU code path. */
1062 break;
1063 }
1064 }
1065
1066 /* Close the previously opened switch statement as well as the function itself. */
1067 const std::string store_function_end = " }\n}\n\n";
1068 store_float_function << store_function_end;
1069 store_float2_function << store_function_end;
1070 store_float3_function << store_function_end;
1071 store_float4_function << store_function_end;
1072 store_color_function << store_function_end;
1073 store_int_function << store_function_end;
1074 store_int2_function << store_function_end;
1075 store_int3_function << store_function_end;
1076 store_int4_function << store_function_end;
1077 store_bool_function << store_function_end;
1078 store_float4x4_function << store_function_end;
1079 store_menu_function << store_function_end;
1080 store_quaternion_function << store_function_end;
1081
1082 return store_float_function.str() + store_float2_function.str() + store_float3_function.str() +
1083 store_float4_function.str() + store_color_function.str() + store_int_function.str() +
1084 store_int2_function.str() + store_int3_function.str() + store_int4_function.str() +
1085 store_bool_function.str() + store_float4x4_function.str() + store_menu_function.str() +
1086 store_quaternion_function.str();
1087}
1088
1090{
1091 switch (type) {
1092 case ResultType::Float:
1093 return "float";
1094 case ResultType::Float2:
1095 return "vec2";
1096 case ResultType::Float3:
1097 return "vec3";
1098 case ResultType::Float4:
1099 return "vec4";
1100 case ResultType::Color:
1101 return "vec4";
1102 case ResultType::Int:
1103 return "int";
1104 case ResultType::Int2:
1105 return "ivec2";
1106 case ResultType::Int3:
1107 return "ivec3";
1108 case ResultType::Int4:
1109 return "ivec4";
1110 case ResultType::Bool:
1111 return "bool";
1113 return "float4x4";
1114 case ResultType::Menu:
1115 return "int";
1117 return "vec4";
1118 case ResultType::String:
1119 case ResultType::Object:
1120 case ResultType::Image:
1121 case ResultType::Font:
1122 case ResultType::Scene:
1123 case ResultType::Text:
1124 case ResultType::Mask:
1125 case ResultType::Bundle:
1127 /* Single only types do not support GPU code path. */
1130 break;
1131 }
1132
1134 return nullptr;
1135}
1136
1137/* Texture loaders in the shader always return an [i]vec4, so a swizzle is needed to retrieve the
1138 * actual value for each type. */
1140{
1141 switch (type) {
1142 case ResultType::Float:
1143 return "x";
1144 case ResultType::Float2:
1145 return "xy";
1146 case ResultType::Float3:
1147 return "xyz";
1148 case ResultType::Float4:
1149 return "xyzw";
1150 case ResultType::Color:
1151 return "rgba";
1152 case ResultType::Int:
1153 return "x";
1154 case ResultType::Int2:
1155 return "xy";
1156 case ResultType::Int3:
1157 return "xyz";
1158 case ResultType::Int4:
1159 return "xyzw";
1160 case ResultType::Bool:
1161 return "x";
1163 return "xyzw";
1164 case ResultType::Menu:
1165 return "x";
1167 return "xyzw";
1168 case ResultType::String:
1169 case ResultType::Object:
1170 case ResultType::Image:
1171 case ResultType::Font:
1172 case ResultType::Scene:
1173 case ResultType::Text:
1174 case ResultType::Mask:
1175 case ResultType::Bundle:
1177 /* Single only types do not support GPU code path. */
1180 break;
1181 }
1182
1184 return nullptr;
1185}
1186
1187std::string ShaderOperation::generate_code_for_inputs(GPUMaterial *material,
1188 ShaderCreateInfo &shader_create_info)
1189{
1190 /* The attributes of the GPU material represents the inputs of the operation. */
1191 ListBaseT<GPUMaterialAttribute> attributes = GPU_material_attributes(material);
1192
1193 if (attributes.is_empty()) {
1194 return "";
1195 }
1196
1197 std::string code;
1198
1199 /* Add a texture sampler for each of the inputs with the same name as the attribute, we start
1200 * counting the sampler slot location from the number of textures in the material, since some
1201 * sampler slots may be reserved for things like color band textures. */
1202 const ListBaseT<GPUMaterialTexture> textures = GPU_material_textures(material);
1203 int input_slot_location = textures.count();
1204 for (GPUMaterialAttribute &attribute : attributes) {
1205 const InputDescriptor &input_descriptor = get_input_descriptor(attribute.name);
1206 shader_create_info.sampler(input_slot_location,
1207 gpu_image_type_from_result_type(input_descriptor.type),
1208 attribute.name,
1209 Frequency::PASS);
1210 input_slot_location++;
1211 }
1212
1213 /* Declare a struct called var_attrs that includes an appropriately typed member for each of the
1214 * inputs. The names of the members should be the letter v followed by the ID of the attribute
1215 * corresponding to the input. Such names are expected by the code generator. */
1216 std::stringstream declare_attributes;
1217 declare_attributes << "struct {\n";
1218 for (GPUMaterialAttribute &attribute : attributes) {
1219 const InputDescriptor &input_descriptor = get_input_descriptor(attribute.name);
1220 const std::string type = glsl_type_from_result_type(input_descriptor.type);
1221 declare_attributes << " " << type << " v" << attribute.id << ";\n";
1222 }
1223 declare_attributes << "} var_attrs;\n\n";
1224
1225 code += declare_attributes.str();
1226
1227 /* The texture loader utilities are needed to sample the input textures and initialize the
1228 * attributes. */
1229 shader_create_info.typedef_source("gpu_shader_compositor_texture_utilities.glsl");
1230
1231 /* Initialize each member of the previously declared struct by loading its corresponding texture
1232 * with an appropriate swizzle and cast for its type. */
1233 std::stringstream initialize_attributes;
1234 for (GPUMaterialAttribute &attribute : attributes) {
1235 const InputDescriptor &input_descriptor = get_input_descriptor(attribute.name);
1236 initialize_attributes << "var_attrs.v" << attribute.id << " = ";
1237 if (input_descriptor.type == ResultType::Float4x4) {
1238 initialize_attributes << "texture_load_float4x4(" << attribute.name
1239 << ", ivec2(gl_GlobalInvocationID.xy));\n";
1240 }
1241 else {
1242 const std::string swizzle = glsl_swizzle_from_result_type(input_descriptor.type);
1243 const std::string type = glsl_type_from_result_type(input_descriptor.type);
1244 initialize_attributes << type << "(texture_load(" << attribute.name;
1245 initialize_attributes << ", ivec2(gl_GlobalInvocationID.xy))." << swizzle << ");\n";
1246 }
1247 }
1248 initialize_attributes << "\n";
1249
1250 code += initialize_attributes.str();
1251
1252 return code;
1253}
1254
1255} // namespace blender::compositor
#define GPU_UBO_BLOCK_NAME
#define BLI_assert_unreachable()
#define ELEM(...)
#define output
const Value & lookup(const Key &key) const
Definition BLI_map.hh:545
void add_new(const Key &key, const Value &value)
Definition BLI_map.hh:265
int64_t size() const
Definition BLI_map.hh:976
constexpr StringRef()
UString()=default
bool contains(const Key &key) const
virtual ResultPrecision get_precision() const
Result & get_result_from_output_socket(const bNodeSocket &output)
Result & get_result(StringRef identifier)
Definition operation.cc:40
void populate_result(StringRef identifier, ResultType type)
Definition operation.cc:122
Context & context() const
Definition operation.cc:57
Result & get_input(StringRef identifier) const
Definition operation.cc:35
virtual Domain compute_domain()
Definition operation.cc:62
InputDescriptor & get_input_descriptor(StringRef identifier)
Definition operation.cc:132
void declare_input_descriptor(StringRef identifier, InputDescriptor descriptor)
Definition operation.cc:127
Map< ImplicitInputType, std::string > implicit_inputs_to_input_identifiers_map_
VectorSet< const bNodeSocket * > preview_outputs_
Map< std::string, const bNodeSocket * > inputs_to_linked_outputs_map_
Map< const bNodeSocket *, std::string > output_sockets_to_output_identifiers_map_
Map< std::string, int > inputs_to_reference_counts_map_
PixelOperation(Context &context, NodeTreeEvaluator &node_tree_evaluator, const ComputeContext &compute_context, const bool is_single_value)
Map< const bNodeSocket *, std::string > outputs_to_declared_inputs_map_
static const char * type_name(const ResultType type)
Definition result.cc:424
static bool is_single_value_only_type(ResultType type)
Definition result.cc:51
static ResultType type(gpu::TextureFormat format)
Definition result.cc:337
void bind_as_texture(gpu::Shader *shader, const char *texture_name) const
Definition result.cc:854
static const CPPType & cpp_type(const ResultType type)
Definition result.cc:371
void bind_as_image(gpu::Shader *shader, const char *image_name, bool read=false) const
Definition result.cc:951
ShaderOperation(Context &context, NodeTreeEvaluator &node_tree_evaluator, const ComputeContext &compute_context)
#define storage(slot, qualifiers)
#define attribute(slot)
TEX_TEMPLATE DataVec texture(T, FltCoord, float=0.0f) RET
const DataTypeConversions & get_implicit_type_conversions()
int compute_output_reference_count(const bNodeSocket &output, const Schedule &schedule, const VectorSet< const bNode * > *ignored_nodes=nullptr)
Definition scheduler.cc:780
static GPUNodeLink * get_result_single_value_link(const Result &result)
void compute_dispatch_threads_at_least(gpu::Shader *shader, int2 threads_range, int2 local_size=int2(16))
Definition utilities.cc:235
const bNodeSocket * find_preview_output_socket(const bNode &node)
Definition utilities.cc:258
bool is_node_preview_needed(const bNode &node)
Definition utilities.cc:245
const bNodeSocket * get_output_linked_to_input(const bNodeSocket &input)
Definition utilities.cc:31
ColorSceneLinear4f< eAlpha::Premultiplied > Color
Definition COM_result.hh:96
static const char * glsl_store_expression_from_result_type(ResultType type)
static GPUNodeLink * get_input_value_link(const bNodeSocket &input, GPUNodeStack &stack)
static const char * glsl_swizzle_from_result_type(ResultType type)
static ImageType gpu_image_type_from_result_type(const ResultType type)
static const char * glsl_type_from_result_type(ResultType type)
ResultType get_node_socket_result_type(const bNodeSocket *socket)
Definition utilities.cc:103
static GPUNodeLink * get_default_input_value_link(const ResultType type)
InputDescriptor input_descriptor_from_input_socket(const bNodeSocket *socket)
Definition utilities.cc:195
static const char * get_store_function_name(ResultType type)
bool is_socket_available(const bNodeSocket *socket)
Definition utilities.cc:26
static const char * get_set_function_name(const ResultType type)
QuaternionBase< float > Quaternion
QuaternionBase< T > to_quaternion(const AxisAngleBase< T, AngleT > &axis_angle)
EulerXYZBase< float > EulerXYZ
T min(const T &a, const T &b)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
int GPU_shader_get_sampler_binding(gpu::Shader *shader, const char *name)
__int64 int64_t
Definition BKE_text.h:90
unsigned int uint
Definition BKE_text.h:55
BLI_INLINE void IMB_colormanagement_get_luminance_coefficients(float r_rgb[3])
void GPU_texture_unbind_all()
gpu::Shader * GPU_material_get_shader(GPUMaterial *material)
gpu::UniformBuf * GPU_material_uniform_buffer_get(GPUMaterial *material)
@ GPU_MAT_COMPOSITOR
GPUNodeLink * GPU_uniform(const float *num)
GPUNodeStackValue GPU_node_stack_default_value(const GPUType type)
void GPU_material_add_output_link_composite(GPUMaterial *material, GPUNodeLink *link)
GPUNodeLink * GPU_constant(const float *num)
bool GPU_link(GPUMaterial *mat, const char *name,...)
void GPU_shader_unbind()
ListBaseT< GPUMaterialAttribute > GPU_material_attributes(const GPUMaterial *material)
GPUMaterial * GPU_material_from_callbacks(eGPUMaterialEngine engine, ConstructGPUMaterialFn construct_function_cb, GPUCodegenCallbackFn generate_code_function_cb, void *thunk, const uint64_t uuid)
void GPU_shader_bind(gpu::Shader *shader, const gpu::shader::SpecializationConstants *constants_state=nullptr)
void GPU_uniformbuf_bind(gpu::UniformBuf *ubo, int slot)
void GPU_debug_group_end()
Definition gpu_debug.cc:34
ListBaseT< GPUMaterialTexture > GPU_material_textures(GPUMaterial *material)
void GPU_material_free_single(GPUMaterial *material)
void GPU_texture_bind(gpu::Texture *texture, int unit)
int GPU_shader_get_ubo_binding(gpu::Shader *shader, const char *name)
signed int int32_t
Definition BKE_text.h:78
void GPU_texture_image_unbind_all()
GPUNodeLink * GPU_attribute(GPUMaterial *mat, eCustomDataType type, const char *name)
void GPU_uniformbuf_debug_unbind_all()
void GPU_debug_group_begin(const char *name)
Definition gpu_debug.cc:23
GPUNodeStackValue value
const T * base_ptr() const
eNodeSocketDatatype type
Set< const bNodeSocket * > unneeded_inputs
Describe inputs & outputs, stage interfaces, resources and sources of a shader. If all data is correc...
Self & image(int slot, TextureFormat format, Qualifier qualifiers, ImageReadWriteType type, StringRefNull name, Frequency freq=Frequency::PASS, ConditionFn cond=nullptr)
Self & typedef_source(StringRefNull filename)
Self & builtins(BuiltinBits builtin, ConditionFn cond=nullptr)
Self & sampler(int slot, ImageType type, StringRefNull name, Frequency freq=Frequency::PASS, ConditionFn cond=nullptr)