Blender V5.3
gpu_node_graph.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2005 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include <cstdio>
12#include <type_traits>
13#include <variant>
14
15#include "MEM_guardedalloc.h"
16
17#include "DNA_node_types.h"
18
19#include "BLI_assert.hh"
20#include "BLI_ghash.hh"
21#include "BLI_listbase.hh"
22#include "BLI_memory_utils.hh"
23#include "BLI_stack.hh"
24#include "BLI_string.hh"
25#include "BLI_utildefines.hh"
26
27#include "BKE_image.hh"
28#include "BKE_node.hh"
29#include "BKE_node_runtime.hh"
30
31#include "GPU_texture.hh"
32#include "GPU_vertex_format.hh"
33
35#include "gpu_node_graph.hh"
36
37namespace blender {
38
39/* Node Link Functions */
40
42 const GPUType type)
43{
44 switch (type) {
45 case GPU_FLOAT:
46 return *std::get<const float *>(link->data);
47 case GPU_VEC2:
48 return float2(std::get<const float *>(link->data));
49 case GPU_VEC3:
50 return float3(std::get<const float *>(link->data));
51 case GPU_VEC4:
52 return float4(std::get<const float *>(link->data));
53 case GPU_MAT3:
54 return float3x3(std::get<const float *>(link->data));
55 case GPU_MAT4:
56 return float4x4(std::get<const float *>(link->data));
57 case GPU_INT:
58 return *std::get<const int *>(link->data);
59 case GPU_INT2:
60 return int2(std::get<const int *>(link->data));
61 case GPU_INT3:
62 return int3(std::get<const int *>(link->data));
63 case GPU_INT4:
64 return int4(std::get<const int *>(link->data));
65 case GPU_BOOL:
66 return *std::get<const bool *>(link->data);
67 default:
68 break;
69 }
70
72 return {};
73}
74
76{
77 switch (type) {
78 case GPU_FLOAT:
79 return Span<float>(&std::get<float>(data), 1);
80 case GPU_VEC2: {
81 const float2 &value = std::get<float2>(data);
82 return Span<float>(&value.x, 2);
83 }
84 case GPU_VEC3: {
85 const float3 &value = std::get<float3>(data);
86 return Span<float>(&value.x, 3);
87 }
88 case GPU_VEC4: {
89 const float4 &value = std::get<float4>(data);
90 return Span<float>(&value.x, 4);
91 }
92 case GPU_MAT3:
93 return Span<float>(std::get<float3x3>(data).base_ptr(), 9);
94 case GPU_MAT4:
95 return Span<float>(std::get<float4x4>(data).base_ptr(), 16);
96 default:
97 break;
98 }
99
101 return {};
102}
103
105{
106 switch (type) {
107 case GPU_INT:
108 return Span<int>(&std::get<int>(data), 1);
109 case GPU_INT2: {
110 const int2 &value = std::get<int2>(data);
111 return Span<int>(&value.x, 2);
112 }
113 case GPU_INT3: {
114 const int3 &value = std::get<int3>(data);
115 return Span<int>(&value.x, 3);
116 }
117 case GPU_INT4: {
118 const int4 &value = std::get<int4>(data);
119 return Span<int>(&value.x, 4);
120 }
121 default:
122 break;
123 }
124
126 return {};
127}
128
130{
131 return std::get<bool>(data);
132}
133
135{
136 GPUNodeLink *link = MEM_new<GPUNodeLink>("GPUNodeLink");
137 link->users++;
138
139 return link;
140}
141
143{
144 link->users--;
145
146 if (link->users < 0) {
147 fprintf(stderr, "gpu_node_link_free: negative refcount\n");
148 }
149
150 if (link->users == 0) {
151 if (link->output) {
152 link->output->link = nullptr;
153 }
154 MEM_delete(link);
155 }
156}
157
158/* Node Functions */
159
160static GPUNode *gpu_node_create(const char *name)
161{
163
164 node->name = name;
165 node->zone_index = -1;
166 node->is_zone_end = false;
167
168 return node;
169}
170
171static void gpu_node_input_link(GPUNode *node, GPUNodeLink *link, const GPUType type)
172{
173 GPUInput *input;
174 GPUNode *outnode;
175 const char *name;
176
177 if (link->link_type == GPU_NODE_LINK_OUTPUT) {
178 outnode = link->output->node;
179 name = outnode->name;
180 input = outnode->inputs.first();
181
182 if (STR_ELEM(name, "set_value", "set_vec2", "set_rgb", "set_rgba") && (input->type == type)) {
183 input = MEM_dupalloc(outnode->inputs.first());
184
185 switch (input->source) {
186 case GPU_SOURCE_ATTR:
187 input->attr->users++;
188 break;
190 input->uniform_attr->users++;
191 break;
193 input->layer_attr->users++;
194 break;
195 case GPU_SOURCE_TEX:
196 input->texture->users++;
197 break;
199 /* Already handled by GPU_SOURCE_TEX. */
200 default:
201 break;
202 }
203
204 if (input->link) {
205 input->link->users++;
206 }
207
208 BLI_addtail(&node->inputs, input);
209 return;
210 }
211 }
212
213 input = MEM_new<GPUInput>("GPUInput");
214 input->node = node;
215 input->type = type;
216
217 switch (link->link_type) {
219 input->source = GPU_SOURCE_OUTPUT;
220 input->link = link;
221 link->users++;
222 break;
227 input->source = GPU_SOURCE_TEX;
228 input->texture = link->texture;
229 break;
232 input->texture = link->texture;
233 break;
235 input->source = GPU_SOURCE_ATTR;
236 input->attr = link->attr;
237 /* Fail-safe handling if the same attribute is used with different data-types for
238 * some reason (only really makes sense with float/vec2/vec3/vec4 though). This
239 * can happen if mixing the generic Attribute node with specialized ones. */
240 if (input->attr->gputype == GPU_NONE ||
241 gpu_type_element_count(input->attr->gputype) < gpu_type_element_count(type))
242 {
243 input->attr->gputype = type;
244 }
245 break;
248 input->uniform_attr = link->uniform_attr;
249 break;
252 input->layer_attr = link->layer_attr;
253 break;
256 break;
259 break;
262 break;
264 input->source = GPU_SOURCE_UNIFORM;
265 break;
268 /* NOTE(@fclem): End of function call is the return variable set during codegen. */
269 SNPRINTF(input->function_call,
270 "dF_branch_incomplete(%s(kg, sd), %g, ",
271 link->differentiate_float.function_name,
272 link->differentiate_float.filter_width);
273 break;
274 default:
275 break;
276 }
277
279 input->constant_data = gpu_input_constant_data_from_link(link, type);
280 }
281
282 if (link->link_type != GPU_NODE_LINK_OUTPUT) {
283 MEM_delete(link);
284 }
285 BLI_addtail(&node->inputs, input);
286}
287
294
301
303{
306 link->data = num;
307 return link;
308}
309
311{
314 link->data = num;
315 return link;
316}
317
319{
322 link->data = num;
323 return link;
324}
325
327{
330 link->data = num;
331 return link;
332}
333
335{
338 link->data = num;
339 return link;
340}
341
343{
346 link->data = num;
347 return link;
348}
349
351{
352 return std::visit(
353 [](const auto &value) -> GPUNodeLink * {
354 using T = std::decay_t<decltype(value)>;
355 if constexpr (std::is_same_v<T, std::monostate>) {
356 /* Use fallback for sockets with no constant representation. */
357 static const float4 dummy(0.0f);
358 return GPU_constant(dummy);
359 }
360 else if constexpr (std::is_arithmetic_v<T>) {
361 return GPU_constant(&value);
362 }
363 else if constexpr (is_same_any_v<T, float3x3, float4x4>) {
364 return GPU_constant(value.base_ptr());
365 }
366 else {
367 return GPU_constant(value);
368 }
369 },
370 stack.value);
371}
372
374{
375 return std::visit(
376 [](const auto &value) -> GPUNodeLink * {
377 using T = std::decay_t<decltype(value)>;
378 if constexpr (std::is_same_v<T, std::monostate>) {
379 /* Use fallback for sockets with no constant representation. */
380 static const float4 dummy(0.0f);
381 return GPU_uniform(dummy);
382 }
383 else if constexpr (std::is_arithmetic_v<T>) {
384 return GPU_uniform(&value);
385 }
386 else if constexpr (is_same_any_v<T, float3x3, float4x4>) {
387 return GPU_uniform(value.base_ptr());
388 }
389 else {
390 return GPU_uniform(value);
391 }
392 },
393 stack.value);
394}
395
397{
398 switch (type) {
399 case GPU_FLOAT:
400 return "set_value";
401 case GPU_INT:
402 return "set_int";
403 case GPU_BOOL:
404 return "set_bool";
405 case GPU_VEC2:
406 return "set_vec2";
407 case GPU_VEC3:
408 return "set_rgb";
409 case GPU_VEC4:
410 return "set_rgba";
411 default:
412 break;
413 }
414
415 BLI_assert_msg(0, "No gpu function for non-supported GPUType");
416 return nullptr;
417}
418
424 const bNode *node,
425 GPUNodeStack *stack,
426 const int index,
427 const eNodeSocketInOut in_out)
428{
429 bNodeSocket *socket;
430
431 if (in_out == SOCK_IN) {
432 socket = static_cast<bNodeSocket *>(BLI_findlink(&node->inputs, index));
433 }
434 else {
435 socket = static_cast<bNodeSocket *>(BLI_findlink(&node->outputs, index));
436 }
437
438 BLI_assert(socket != nullptr);
439 BLI_assert(socket->in_out == in_out);
440
441 if (socket->flag & SOCK_HIDE_VALUE) {
442 return nullptr;
443 }
444
446 return nullptr;
447 }
448
449 GPUNodeLink *link = GPU_uniform(*stack);
450
451 if (in_out == SOCK_IN) {
452 GPU_link(mat, gpu_uniform_set_function_from_type(stack->type), link, &stack->link);
453 }
454
455 return link;
456}
457
459 GPUMaterial *material, const bNode *bnode, GPUNode *node, GPUNodeStack *sock, const int index)
460{
461 if (sock->link) {
462 gpu_node_input_link(node, sock->link, sock->type);
463 }
464 else if ((material != nullptr) &&
465 (gpu_uniformbuffer_link(material, bnode, sock, index, SOCK_IN) != nullptr))
466 {
467 gpu_node_input_link(node, sock->link, sock->type);
468 }
469 else {
471 }
472}
473
474static void gpu_node_output(GPUNode *node, const GPUType type, GPUNodeLink **link)
475{
477
478 output->type = type;
479 output->node = node;
480
481 if (link) {
482 *link = output->link = gpu_node_link_create();
483 output->link->link_type = GPU_NODE_LINK_OUTPUT;
484 output->link->output = output;
485
486 /* NOTE: the caller owns the reference to the link, GPUOutput
487 * merely points to it, and if the node is destroyed it will
488 * set that pointer to nullptr */
489 }
490
491 BLI_addtail(&node->outputs, output);
492}
493
494/* Uniform Attribute Functions */
495
496static int uniform_attr_sort_cmp(const void *a, const void *b)
497{
498 const GPUUniformAttr *attr_a = static_cast<const GPUUniformAttr *>(a),
499 *attr_b = static_cast<const GPUUniformAttr *>(b);
500
501 int cmps = strcmp(attr_a->name, attr_b->name);
502 if (cmps != 0) {
503 return cmps > 0 ? 1 : 0;
504 }
505
506 return (attr_a->use_dupli && !attr_b->use_dupli);
507}
508
509static uint uniform_attr_list_hash(const void *key)
510{
511 const GPUUniformAttrList *attrs = static_cast<const GPUUniformAttrList *>(key);
512 return attrs->hash_code;
513}
514
515static bool uniform_attr_list_cmp(const void *a, const void *b)
516{
517 const GPUUniformAttrList *set_a = static_cast<const GPUUniformAttrList *>(a),
518 *set_b = static_cast<const GPUUniformAttrList *>(b);
519
520 if (set_a->hash_code != set_b->hash_code || set_a->count != set_b->count) {
521 return true;
522 }
523
524 GPUUniformAttr *attr_a = set_a->list.first(), *attr_b = set_b->list.first();
525
526 for (; attr_a && attr_b; attr_a = attr_a->next, attr_b = attr_b->next) {
527 if (!STREQ(attr_a->name, attr_b->name) || attr_a->use_dupli != attr_b->use_dupli) {
528 return true;
529 }
530 }
531
532 return attr_a || attr_b;
533}
534
539
541{
542 dest->count = src->count;
543 dest->hash_code = src->hash_code;
544 BLI_duplicatelist(&dest->list, &src->list);
545}
546
548{
549 set->count = 0;
550 set->hash_code = 0;
551 set->list.free_no_destruct();
552}
553
555{
556 GPUUniformAttrList *attrs = &graph->uniform_attrs;
557 BLI_assert(attrs->count == attrs->list.count());
558
559 /* Sort the attributes by name to ensure a stable order. */
561
562 /* Compute the indices and the hash code. */
563 int next_id = 0;
564 attrs->hash_code = 0;
565
566 for (GPUUniformAttr &attr : attrs->list) {
567 attr.id = next_id++;
568 attrs->hash_code ^= BLI_ghashutil_uinthash(attr.hash_code + (1 << (attr.id + 1)));
569 }
570}
571
572/* Attributes and Textures */
573
574static char attr_prefix_get(const GPUMaterialAttribute *attr)
575{
576 if (attr->is_default_color) {
577 return 'c';
578 }
579 if (attr->is_hair_length) {
580 return 'l';
581 }
582 if (attr->is_hair_intercept) {
583 return 'i';
584 }
585 switch (attr->type) {
586 case CD_TANGENT:
587 return 't';
589 return 'a';
590 default:
591 BLI_assert_msg(0, "GPUVertAttr Prefix type not found : This should not happen!");
592 return '\0';
593 }
594}
595
597{
598 /* NOTE: Replicate changes to mesh_render_data_create() in draw_cache_impl_mesh.cc */
599 if (attr->type == CD_ORCO) {
600 /* OPTI: orco is computed from local positions, but only if no modifier is present. */
601 STRNCPY(attr->input_name, "orco");
602 }
603 else {
604 attr->input_name[0] = attr_prefix_get(attr);
605 attr->input_name[1] = '\0';
606 if (attr->name[0] != '\0') {
607 /* XXX FIXME: see notes in mesh_render_data_create() */
609 }
610 }
611}
612
615 eCustomDataType type,
616 const char *name,
617 const bool is_default_color,
618 const bool is_hair_length,
619 const bool is_hair_intercept)
620{
621 /* Find existing attribute. */
622 int num_attributes = 0;
623 GPUMaterialAttribute *attr = graph->attributes.first();
624 for (; attr; attr = attr->next) {
625 if (attr->type == type && STREQ(attr->name, name) &&
626 attr->is_default_color == is_default_color && attr->is_hair_length == is_hair_length &&
627 attr->is_hair_intercept == is_hair_intercept)
628 {
629 break;
630 }
631 num_attributes++;
632 }
633
634 /* Add new requested attribute if it's within GPU limits. */
635 if (attr == nullptr && num_attributes < GPU_MAX_ATTR) {
637 attr->is_default_color = is_default_color;
638 attr->is_hair_length = is_hair_length;
639 attr->is_hair_intercept = is_hair_intercept;
640 attr->type = type;
641 STRNCPY(attr->name, name);
642 attr_input_name(attr);
643 attr->id = num_attributes;
644 BLI_addtail(&graph->attributes, attr);
645 }
646
647 if (attr != nullptr) {
648 attr->users++;
649 }
650
651 return attr;
652}
653
656 const char *name,
657 bool use_dupli)
658{
659 /* Find existing attribute. */
660 GPUUniformAttrList *attrs = &graph->uniform_attrs;
661 GPUUniformAttr *attr = attrs->list.first();
662
663 for (; attr; attr = attr->next) {
664 if (STREQ(attr->name, name) && attr->use_dupli == use_dupli) {
665 break;
666 }
667 }
668
669 if (attr == nullptr) {
670 attr = MEM_new_zeroed<GPUUniformAttr>(__func__);
671 STRNCPY(attr->name, name);
672 attr->use_dupli = use_dupli;
673 attr->hash_code = BLI_ghashutil_strhash_p(attr->name) << 1 | (attr->use_dupli ? 0 : 1);
674 attr->id = -1;
675 BLI_addtail(&attrs->list, attr);
676 attrs->count++;
677 }
678
679 if (attr != nullptr) {
680 attr->users++;
681 }
682
683 return attr;
684}
685
688{
689 /* Find existing attribute. */
690 ListBaseT<GPULayerAttr> *attrs = &graph->layer_attrs;
691 GPULayerAttr *attr = attrs->first();
692
693 for (; attr; attr = attr->next) {
694 if (STREQ(attr->name, name)) {
695 break;
696 }
697 }
698
699 /* Add new requested attribute to the list. */
700 if (attr == nullptr) {
701 attr = MEM_new_zeroed<GPULayerAttr>(__func__);
702 STRNCPY(attr->name, name);
704 BLI_addtail(attrs, attr);
705 }
706
707 if (attr != nullptr) {
708 attr->users++;
709 }
710
711 return attr;
712}
713
714static bool gpu_image_user_match(const GPUMaterialTexture *tex, const ImageUser *iuser)
715{
716 const bool iuser_available = iuser != nullptr;
717 if (tex->iuser_available != iuser_available) {
718 return false;
719 }
720
721 return (tex->iuser_available) ? BKE_image_user_match(tex->iuser, *iuser) : true;
722}
723
725 Image *ima,
726 ImageUser *iuser,
727 gpu::Texture **colorband,
728 gpu::Texture **sky,
729 bool is_tiled,
730 GPUSamplerState sampler_state)
731{
732 /* Find existing texture. */
733 int num_textures = 0;
734 GPUMaterialTexture *tex = graph->textures.first();
735 for (; tex; tex = tex->next) {
736 if (tex->ima == ima && tex->colorband == colorband && tex->sky == sky &&
737 tex->sampler_state == sampler_state && gpu_image_user_match(tex, iuser))
738 {
739 break;
740 }
741 num_textures++;
742 }
743
744 /* Add new requested texture. */
745 if (tex == nullptr) {
746 tex = MEM_new<GPUMaterialTexture>(__func__);
747 tex->ima = ima;
748 if (iuser != nullptr) {
749 tex->iuser = *iuser;
750 tex->iuser_available = true;
751 }
752 tex->colorband = colorband;
753 tex->sky = sky;
754 tex->sampler_state = sampler_state;
755 SNPRINTF(tex->sampler_name, "samp%d", num_textures);
756 if (is_tiled) {
757 SNPRINTF(tex->tiled_mapping_name, "tsamp%d", num_textures);
758 }
759 BLI_addtail(&graph->textures, tex);
760 }
761
762 tex->users++;
763
764 return tex;
765}
766
767/* Creating Inputs */
768
769GPUNodeLink *GPU_attribute(GPUMaterial *mat, const eCustomDataType type, const char *name)
770{
773 graph, type, name, false, false, false);
774
775 if (type == CD_ORCO) {
776 /* OPTI: orco might be computed from local positions and needs object information. */
778 }
779
780 /* Dummy fallback if out of slots. */
781 if (attr == nullptr) {
782 static const float zero_data[GPU_MAX_CONSTANT_DATA] = {0.0f};
783 return GPU_constant(zero_data);
784 }
785
788 link->attr = attr;
789 return link;
790}
791
793{
796 graph, CD_AUTO_FROM_NAME, "", true, false, false);
797 if (attr == nullptr) {
798 static const float zero_data[GPU_MAX_CONSTANT_DATA] = {0.0f};
799 return GPU_constant(zero_data);
800 }
801 attr->is_default_color = true;
804 link->attr = attr;
805 return link;
806}
807
809{
812 graph, CD_AUTO_FROM_NAME, "", false, true, false);
813 if (attr == nullptr) {
814 static const float zero_data[GPU_MAX_CONSTANT_DATA] = {0.0f};
815 return GPU_constant(zero_data);
816 }
819 link->attr = attr;
820 return link;
821}
822
824{
827 graph, CD_AUTO_FROM_NAME, "", false, false, true);
828 if (attr == nullptr) {
829 static const float zero_data[GPU_MAX_CONSTANT_DATA] = {0.0f};
830 return GPU_constant(zero_data);
831 }
834 link->attr = attr;
835 return link;
836}
837
839 const eCustomDataType type,
840 const char *name,
841 GPUDefaultValue default_value)
842{
843 GPUNodeLink *link = GPU_attribute(mat, type, name);
844 if (link->link_type == GPU_NODE_LINK_ATTR) {
845 link->attr->default_value = default_value;
846 }
847 return link;
848}
849
851 const char *name,
852 bool use_dupli,
853 uint32_t *r_hash)
854{
856 GPUUniformAttr *attr = gpu_node_graph_add_uniform_attribute(graph, name, use_dupli);
857
858 /* Dummy fallback if out of slots. */
859 if (attr == nullptr) {
860 *r_hash = 0;
861 static const float zero_data[GPU_MAX_CONSTANT_DATA] = {0.0f};
862 return GPU_constant(zero_data);
863 }
864 *r_hash = attr->hash_code;
865
868 link->uniform_attr = attr;
869 return link;
870}
871
873{
876
879 link->layer_attr = attr;
880 return link;
881}
882
883GPUNodeLink *GPU_differentiate_float_function(const char *function_name, const float filter_width)
884{
887 link->differentiate_float.function_name = function_name;
888 link->differentiate_float.filter_width = filter_width;
889 return link;
890}
891
893 Image *ima,
894 ImageUser *iuser,
895 GPUSamplerState sampler_state)
896{
900 link->texture = gpu_node_graph_add_texture(
901 graph, ima, iuser, nullptr, nullptr, false, sampler_state);
902 return link;
903}
904
906 int width,
907 int height,
908 const float *pixels,
909 float *layer,
910 GPUSamplerState sampler_state)
911{
912 gpu::Texture **sky = gpu_material_sky_texture_layer_set(mat, width, height, pixels, layer);
913
917 link->texture = gpu_node_graph_add_texture(
918 graph, nullptr, nullptr, nullptr, sky, false, sampler_state);
919 return link;
920}
921
923 Image *ima,
924 ImageUser *iuser,
925 GPUSamplerState sampler_state,
926 GPUNodeLink **r_image_tiled_link,
927 GPUNodeLink **r_image_tiled_mapping_link)
928{
931 graph, ima, iuser, nullptr, nullptr, true, sampler_state);
932
933 (*r_image_tiled_link) = gpu_node_link_create();
934 (*r_image_tiled_link)->link_type = GPU_NODE_LINK_IMAGE_TILED;
935 (*r_image_tiled_link)->texture = texture;
936
937 (*r_image_tiled_mapping_link) = gpu_node_link_create();
938 (*r_image_tiled_mapping_link)->link_type = GPU_NODE_LINK_IMAGE_TILED_MAPPING;
939 (*r_image_tiled_mapping_link)->texture = texture;
940}
941
942GPUNodeLink *GPU_color_band(GPUMaterial *mat, int size, float *pixels, float *r_row)
943{
944 gpu::Texture **colorband = gpu_material_ramp_texture_row_set(mat, size, pixels, r_row);
945 MEM_delete(pixels);
946
950 link->texture = gpu_node_graph_add_texture(
951 graph, nullptr, nullptr, colorband, nullptr, false, GPUSamplerState::internal_sampler());
952 return link;
953}
954
955/* Creating Nodes */
956
957bool GPU_link(GPUMaterial *mat, const char *name, ...)
958{
960 GPUNode *node;
961 GPUFunction *function;
962 GPUNodeLink *link, **linkptr;
963 va_list params;
964 int i;
965
966 function = gpu_material_library_get_function(name);
967 if (!function) {
968 fprintf(stderr, "GPU failed to find function %s\n", name);
969 return false;
970 }
971
972 node = gpu_node_create(name);
973
974 va_start(params, name);
975 for (i = 0; i < function->totparam; i++) {
976 if (function->paramqual[i] == FUNCTION_QUAL_OUT) {
977 linkptr = va_arg(params, GPUNodeLink **);
978 gpu_node_output(node, function->paramtype[i], linkptr);
979 }
980 else {
981 link = va_arg(params, GPUNodeLink *);
982 gpu_node_input_link(node, link, function->paramtype[i]);
983 }
984 }
985 va_end(params);
986
987 BLI_addtail(&graph->nodes, node);
988
989 return true;
990}
991
992static bool gpu_stack_link_v(GPUMaterial *material,
993 const bNode *bnode,
994 const char *name,
997 va_list params // NOLINT
998)
999{
1000 GPUNodeGraph *graph = gpu_material_node_graph(material);
1001 GPUNode *node;
1002 GPUFunction *function;
1003 GPUNodeLink *link, **linkptr;
1004 int i, totin, totout;
1005
1006 function = gpu_material_library_get_function(name);
1007 if (!function) {
1008 fprintf(stderr, "GPU failed to find function %s\n", name);
1009 return false;
1010 }
1011
1012 node = gpu_node_create(name);
1013 totin = 0;
1014 totout = 0;
1015
1016 if (in) {
1017 for (i = 0; !in[i].end; i++) {
1018 if (in[i].type != GPU_NONE) {
1019 gpu_node_input_socket(material, bnode, node, &in[i], i);
1020 totin++;
1021 }
1022 }
1023 }
1024
1025 if (out) {
1026 for (i = 0; !out[i].end; i++) {
1027 if (out[i].type != GPU_NONE) {
1028 gpu_node_output(node, out[i].type, &out[i].link);
1029 totout++;
1030 }
1031 }
1032 }
1033
1034 for (i = 0; i < function->totparam; i++) {
1035 if (function->paramqual[i] == FUNCTION_QUAL_OUT) {
1036 if (totout == 0) {
1037 linkptr = va_arg(params, GPUNodeLink **);
1038 gpu_node_output(node, function->paramtype[i], linkptr);
1039 }
1040 else {
1041 totout--;
1042 }
1043 }
1044 else {
1045 if (totin == 0) {
1046 link = va_arg(params, GPUNodeLink *);
1047 if (link->socket) {
1048 gpu_node_input_socket(nullptr, nullptr, node, link->socket, -1);
1049 }
1050 else {
1051 gpu_node_input_link(node, link, function->paramtype[i]);
1052 }
1053 }
1054 else {
1055 totin--;
1056 }
1057 }
1058 }
1059
1060 BLI_addtail(&graph->nodes, node);
1061
1062 return true;
1063}
1064
1066 const bNode *bnode,
1067 const char *name,
1070 ...)
1071{
1072 va_list params;
1073 va_start(params, out);
1074 bool valid = gpu_stack_link_v(material, bnode, name, in, out, params);
1075 va_end(params);
1076
1077 return valid;
1078}
1079
1081 const bNode *bnode,
1082 const char *name,
1085 int zone_index,
1086 bool is_zone_end,
1087 int in_argument_count,
1088 int out_argument_count)
1089{
1090 GPUNodeGraph *graph = gpu_material_node_graph(material);
1091 GPUNode *node;
1092
1093 node = gpu_node_create(name);
1094 node->zone_index = zone_index;
1095 node->is_zone_end = is_zone_end;
1096
1097 if (in) {
1098 for (int i = 0; !in[i].end; i++) {
1099 if (in[i].type != GPU_NONE) {
1100 gpu_node_input_socket(material, bnode, node, &in[i], i);
1101 }
1102 }
1103 }
1104
1105 if (out) {
1106 for (int i = 0; !out[i].end; i++) {
1107 if (out[i].type != GPU_NONE) {
1108 gpu_node_output(node, out[i].type, &out[i].link);
1109 }
1110 }
1111 }
1112
1113 for (const auto [i, input] : node->inputs.enumerate()) {
1114 input.is_zone_io = i >= in_argument_count;
1115 input.is_duplicate = input.is_zone_io && is_zone_end;
1116 }
1117 for (const auto [i, output] : node->outputs.enumerate()) {
1118 output.is_zone_io = i >= out_argument_count;
1119 output.is_duplicate = output.is_zone_io;
1120 }
1121
1122 BLI_addtail(&graph->nodes, node);
1123
1124 return true;
1125}
1126
1127/* Node Graph */
1128
1129static void gpu_inputs_free(ListBaseT<GPUInput> *inputs)
1130{
1131 for (GPUInput &input : *inputs) {
1132 switch (input.source) {
1133 case GPU_SOURCE_ATTR:
1134 input.attr->users--;
1135 break;
1137 input.uniform_attr->users--;
1138 break;
1140 input.layer_attr->users--;
1141 break;
1142 case GPU_SOURCE_TEX:
1143 input.texture->users--;
1144 break;
1146 /* Already handled by GPU_SOURCE_TEX. */
1147 default:
1148 break;
1149 }
1150
1151 if (input.link) {
1152 gpu_node_link_free(input.link);
1153 }
1154 }
1155
1156 inputs->free_no_destruct();
1157}
1158
1160{
1161 gpu_inputs_free(&node->inputs);
1162
1163 for (GPUOutput &output : node->outputs) {
1164 if (output.link) {
1165 output.link->output = nullptr;
1167 }
1168 }
1169
1170 node->outputs.free_no_destruct();
1171 MEM_delete(node);
1172}
1173
1175{
1176 while (GPUNode *node = static_cast<GPUNode *>(BLI_pophead(&graph->nodes))) {
1178 }
1179
1180 graph->outlink_surface = nullptr;
1181 graph->outlink_volume = nullptr;
1182 graph->outlink_displacement = nullptr;
1183 graph->outlink_thickness = nullptr;
1184}
1185
1187{
1188 graph->outlink_aovs.free_no_destruct();
1189 graph->material_functions.free_no_destruct();
1190 graph->outlink_compositor.free_no_destruct();
1192
1193 graph->textures.free_no_destruct();
1194 graph->attributes.free_no_destruct();
1196 graph->layer_attrs.free_no_destruct();
1197}
1198
1199/* Prune Unused Nodes */
1200
1201void gpu_nodes_tag(GPUNodeGraph *graph, GPUNodeLink *link_start, GPUNodeTag tag)
1202{
1203 if (!link_start || !link_start->output) {
1204 return;
1205 }
1206
1207 Stack<GPUNode *> stack;
1208 Stack<GPUNode *> zone_stack;
1209 stack.push(link_start->output->node);
1210
1211 while (!stack.is_empty() || !zone_stack.is_empty()) {
1212 GPUNode *node = !stack.is_empty() ? stack.pop() : zone_stack.pop();
1213
1214 if (node->tag & tag) {
1215 continue;
1216 }
1217
1218 node->tag |= tag;
1219 for (GPUInput &input : node->inputs) {
1220 if (input.link && input.link->output) {
1221 stack.push(input.link->output->node);
1222 }
1223 }
1224
1225 /* Zone input nodes are implicitly linked to their corresponding zone output nodes,
1226 * even if there is no GPUNodeLink between them. */
1227 if (node->is_zone_end) {
1228 for (GPUNode &node2 : graph->nodes) {
1229 if (node2.zone_index == node->zone_index && !node2.is_zone_end && !(node2.tag & tag)) {
1230 node2.tag |= tag;
1231 for (GPUInput &input : node2.inputs) {
1232 if (input.link && input.link->output) {
1233 zone_stack.push(input.link->output->node);
1234 }
1235 }
1236 }
1237 }
1238 }
1239 }
1240}
1241
1243{
1244 for (GPUNode &node : graph->nodes) {
1245 node.tag = GPU_NODE_TAG_NONE;
1246 }
1247
1252
1253 for (GPUNodeGraphOutputLink &aovlink : graph->outlink_aovs) {
1254 gpu_nodes_tag(graph, aovlink.outlink, GPU_NODE_TAG_AOV);
1255 }
1256 for (GPUNodeGraphFunctionLink &funclink : graph->material_functions) {
1258 }
1259 for (GPUNodeGraphOutputLink &compositor_link : graph->outlink_compositor) {
1260 gpu_nodes_tag(graph, compositor_link.outlink, GPU_NODE_TAG_COMPOSITOR);
1261 }
1262
1263 for (GPUNode *node = graph->nodes.first(), *next = nullptr; node; node = next) {
1264 next = node->next;
1265
1266 if (node->tag == GPU_NODE_TAG_NONE) {
1267 BLI_remlink(&graph->nodes, node);
1269 }
1270 }
1271
1272 for (GPUMaterialAttribute *attr = graph->attributes.first(), *next = nullptr; attr; attr = next)
1273 {
1274 next = attr->next;
1275 if (attr->users == 0) {
1276 BLI_freelinkN(&graph->attributes, attr);
1277 }
1278 }
1279
1280 for (GPUMaterialTexture *tex = graph->textures.first(), *next = nullptr; tex; tex = next) {
1281 next = tex->next;
1282 if (tex->users == 0) {
1283 BLI_freelinkN(&graph->textures, tex);
1284 }
1285 }
1286
1287 GPUUniformAttrList *uattrs = &graph->uniform_attrs;
1288
1289 for (GPUUniformAttr &attr : uattrs->list.items_mutable()) {
1290 if (attr.users == 0) {
1291 BLI_freelinkN(&uattrs->list, &attr);
1292 uattrs->count--;
1293 }
1294 }
1295
1296 for (GPULayerAttr &attr : graph->layer_attrs.items_mutable()) {
1297 if (attr.users == 0) {
1298 BLI_freelinkN(&graph->layer_attrs, &attr);
1299 }
1300 }
1301}
1302
1304{
1305 /* Replace all uniform node links with constant. */
1306 for (GPUNode &node : graph->nodes) {
1307 for (GPUInput &input : node.inputs) {
1308 if (input.link) {
1309 if (input.link->link_type == GPU_NODE_LINK_UNIFORM) {
1311 }
1312 }
1313 if (input.source == GPU_SOURCE_UNIFORM) {
1315 }
1316 }
1317 }
1318
1319 /* TODO: Consider performing other node graph optimizations here. */
1320}
1321
1323 GPUNodeStack inputs[],
1324 const StringRef identifier)
1325{
1326 const bNodeSocket *input = node.input_by_identifier(UString(identifier));
1327 BLI_assert(input);
1328 return inputs[input->index()];
1329}
1330
1332 GPUNodeStack outputs[],
1333 const StringRef identifier)
1334{
1335 const bNodeSocket *output = node.output_by_identifier(UString(identifier));
1337 return outputs[output->index()];
1338}
1339
1341 GPUNodeStack inputs[],
1342 const StringRef identifier)
1343{
1344 GPUNodeStack &input = GPU_node_get_input(node, inputs, identifier);
1345 if (input.link) {
1346 return input.link;
1347 }
1348 return GPU_uniform(input);
1349}
1350
1351} // namespace blender
#define STR_ELEM(...)
#define SNPRINTF(dst, format,...)
Read Blender memory allocation and freeing.
#define BLI_assert_unreachable()
#define ELEM(...)
#define BLI_assert_msg(a, msg)
#define STREQ(a, b)
#define output
bool is_empty() const
Definition BLI_stack.hh:310
void push(const T &value)
Definition BLI_stack.hh:215
UString()=default
#define layer
TEX_TEMPLATE DataVec texture(T, FltCoord, float=0.0f) RET
#define T
BMesh Iterators.
static GPUNode * gpu_node_create(const char *name)
GPUNodeLink * GPU_uniform_attribute(GPUMaterial *mat, const char *name, bool use_dupli, uint32_t *r_hash)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
bool GPU_stack_link(GPUMaterial *mat, const bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
unsigned int BLI_ghashutil_uinthash(unsigned int key)
static void gpu_node_output(GPUNode *node, const GPUType type, GPUNodeLink **link)
char * STRNCPY(char(&dst)[N], const char *src)
static GPUNodeLink * gpu_uniformbuffer_link(GPUMaterial *mat, const bNode *node, GPUNodeStack *stack, const int index, const eNodeSocketInOut in_out)
constexpr bool is_same_any_v
VecBase< int32_t, 4 > int4
GPUNodeGraph * gpu_material_node_graph(GPUMaterial *material)
Span< float > gpu_constant_to_float_span(const GPUInputConstantData &data, const GPUType type)
unsigned int uint
Definition BKE_text.h:55
static void gpu_node_input_link(GPUNode *node, GPUNodeLink *link, const GPUType type)
Span< int > gpu_constant_to_int_span(const GPUInputConstantData &data, const GPUType type)
static void gpu_node_link_free(GPUNodeLink *link)
static bool uniform_attr_list_cmp(const void *a, const void *b)
constexpr int GPU_MAX_CONSTANT_DATA
void void void void void void BLI_duplicatelist(ListBase *dst, const ListBase *src) ATTR_NONNULL(1
Definition listbase.cc:834
static GPUMaterialTexture * gpu_node_graph_add_texture(GPUNodeGraph *graph, Image *ima, ImageUser *iuser, gpu::Texture **colorband, gpu::Texture **sky, bool is_tiled, GPUSamplerState sampler_state)
static constexpr int GPU_MAX_SAFE_ATTR_NAME
GPUNodeLink * GPU_image(GPUMaterial *mat, Image *ima, ImageUser *iuser, GPUSamplerState sampler_state)
GPUNodeLink * GPU_node_get_input_link(const bNode &node, GPUNodeStack inputs[], StringRef identifier)
GPUNodeLink * GPU_attribute_with_default(GPUMaterial *mat, eCustomDataType type, const char *name, GPUDefaultValue default_value)
@ GPU_MATFLAG_OBJECT_INFO
GPUNodeStack & GPU_node_get_input(const bNode &node, GPUNodeStack inputs[], StringRef identifier)
static constexpr int GPU_MAX_ATTR
Definition GPU_shader.hh:39
GPUNodeLink * GPU_layer_attribute(GPUMaterial *mat, const char *name)
void gpu_nodes_tag(GPUNodeGraph *graph, GPUNodeLink *link_start, GPUNodeTag tag)
MatBase< float, 4, 4 > float4x4
static GPULayerAttr * gpu_node_graph_add_layer_attribute(GPUNodeGraph *graph, const char *name)
gpu::Texture ** gpu_material_ramp_texture_row_set(GPUMaterial *mat, int size, const float *pixels, float *r_row)
GPUNodeLink * GPU_color_band(GPUMaterial *mat, int size, float *pixels, float *r_row)
GPUFunction * gpu_material_library_get_function(const char *name)
GPUNodeLink * GPU_uniform(const float *num)
bool GPU_stack_link_zone(GPUMaterial *material, const bNode *bnode, const char *name, GPUNodeStack *in, GPUNodeStack *out, int zone_index, bool is_zone_end, int in_argument_count, int out_argument_count)
void * BLI_pophead(ListBase *listbase) ATTR_NONNULL(1)
Definition listbase.cc:259
VecBase< float, 4 > float4
GPUNodeLink * GPU_attribute_hair_length(GPUMaterial *mat)
gpu::Texture ** gpu_material_sky_texture_layer_set(GPUMaterial *mat, int width, int height, const float *pixels, float *row)
void * BLI_findlink(const ListBase *listbase, int number) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:541
unsigned int uint32_t
Definition BKE_text.h:81
static int uniform_attr_sort_cmp(const void *a, const void *b)
GPUNodeLink * GPU_constant(const float *num)
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
VecBase< int32_t, 3 > int3
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
void GPU_vertformat_safe_attr_name(StringRef attr_name, char *r_safe_name, uint max_len)
bool GPU_link(GPUMaterial *mat, const char *name,...)
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:138
BMesh const char void * data
bool gpu_constant_to_bool(const GPUInputConstantData &data)
@ GPU_NODE_TAG_SURFACE
@ GPU_NODE_TAG_THICKNESS
@ GPU_NODE_TAG_VOLUME
@ GPU_NODE_TAG_FUNCTION
@ GPU_NODE_TAG_COMPOSITOR
@ GPU_NODE_TAG_DISPLACEMENT
GPUNodeLink * GPU_attribute_default_color(GPUMaterial *mat)
bool BKE_image_user_match(const ImageUser &a, const ImageUser &b)
GPUNodeStack & GPU_node_get_output(const bNode &node, GPUNodeStack outputs[], StringRef identifier)
static uint uniform_attr_list_hash(const void *key)
static void gpu_inputs_free(ListBaseT< GPUInput > *inputs)
void GPU_material_flag_set(GPUMaterial *mat, eGPUMaterialFlag flag)
void BLI_freelinkN(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:277
static GPUInputConstantData gpu_input_constant_data_from_link(const GPUNodeLink *link, const GPUType type)
void GPU_image_tiled(GPUMaterial *mat, Image *ima, ImageUser *iuser, GPUSamplerState sampler_state, GPUNodeLink **r_image_tiled_link, GPUNodeLink **r_image_tiled_mapping_link)
static void attr_input_name(GPUMaterialAttribute *attr)
static GPUNodeLink * gpu_node_link_create()
void GPU_uniform_attr_list_copy(GPUUniformAttrList *dest, const GPUUniformAttrList *src)
ATTR_WARN_UNUSED_RESULT const size_t num
void gpu_node_graph_free(GPUNodeGraph *graph)
MatBase< float, 3, 3 > float3x3
void gpu_node_graph_free_nodes(GPUNodeGraph *graph)
@ GPU_NODE_LINK_CONSTANT
@ GPU_NODE_LINK_IMAGE_SKY
@ GPU_NODE_LINK_IMAGE
@ GPU_NODE_LINK_LAYER_ATTR
@ GPU_NODE_LINK_KERNEL_GLOBALS
@ GPU_NODE_LINK_IMAGE_TILED
@ GPU_NODE_LINK_SHADING_DATA
@ GPU_NODE_LINK_DIFFERENTIATE_FLOAT_FN
@ GPU_NODE_LINK_IMAGE_TILED_MAPPING
@ GPU_NODE_LINK_COLORBAND
@ GPU_NODE_LINK_OUTPUT
@ GPU_NODE_LINK_UNIFORM
@ GPU_NODE_LINK_UNIFORM_ATTR
GPUNodeLink * GPU_differentiate_float_function(const char *function_name, const float filter_width)
static const char * gpu_uniform_set_function_from_type(GPUType type)
void void BLI_listbase_sort(ListBase *listbase, int(*cmp)(const void *, const void *)) ATTR_NONNULL(1
Definition listbase.cc:319
GPUNodeLink * GPU_shading_data()
@ GPU_SOURCE_TEX_TILED_MAPPING
@ GPU_SOURCE_SHADING_DATA
@ GPU_SOURCE_FUNCTION_CALL
@ GPU_SOURCE_LAYER_ATTR
@ GPU_SOURCE_CONSTANT
@ GPU_SOURCE_UNIFORM_ATTR
@ GPU_SOURCE_KERNEL_GLOBALS
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
static bool gpu_stack_link_v(GPUMaterial *material, const bNode *bnode, const char *name, GPUNodeStack *in, GPUNodeStack *out, va_list params)
GPUNodeLink * GPU_image_sky(GPUMaterial *mat, int width, int height, const float *pixels, float *layer, GPUSamplerState sampler_state)
static bool gpu_image_user_match(const GPUMaterialTexture *tex, const ImageUser *iuser)
void gpu_node_graph_finalize_uniform_attrs(GPUNodeGraph *graph)
unsigned int BLI_ghashutil_strhash_p(const void *ptr)
GPUNodeLink * GPU_attribute(GPUMaterial *mat, eCustomDataType type, const char *name)
static GPUMaterialAttribute * gpu_node_graph_add_attribute(GPUNodeGraph *graph, eCustomDataType type, const char *name, const bool is_default_color, const bool is_hair_length, const bool is_hair_intercept)
GPUNodeLink * GPU_kernel_globals()
static void gpu_node_free(GPUNode *node)
GHash * BLI_ghash_new(GHashHashFP hashfp, GHashCmpFP cmpfp, const char *info) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
Definition BLI_ghash.cc:690
VecBase< float, 3 > float3
void gpu_node_graph_prune_unused(GPUNodeGraph *graph)
static char attr_prefix_get(const GPUMaterialAttribute *attr)
static void gpu_node_input_socket(GPUMaterial *material, const bNode *bnode, GPUNode *node, GPUNodeStack *sock, const int index)
void GPU_uniform_attr_list_free(GPUUniformAttrList *set)
void gpu_node_graph_optimize(GPUNodeGraph *graph)
static GPUUniformAttr * gpu_node_graph_add_uniform_attribute(GPUNodeGraph *graph, const char *name, bool use_dupli)
constexpr int gpu_type_element_count(const GPUType type)
std::variant< float, float2, float3, float4, float3x3, float4x4, int, int2, int3, int4, bool > GPUInputConstantData
GHash * GPU_uniform_attr_list_hash_new(const char *info)
GPUNodeLink * GPU_attribute_hair_intercept(GPUMaterial *mat)
GPUFunctionQual paramqual[MAX_PARAMETER]
GPUType paramtype[MAX_PARAMETER]
GPUDataSource source
GPUMaterialAttribute * next
GPUMaterialTexture * next
ListBaseT< GPUMaterialAttribute > attributes
ListBaseT< GPUNodeGraphFunctionLink > material_functions
GPUNodeLink * outlink_surface
ListBaseT< GPULayerAttr > layer_attrs
GPUNodeLink * outlink_displacement
ListBaseT< GPUNodeGraphOutputLink > outlink_aovs
GPUNodeLink * outlink_thickness
ListBaseT< GPUNode > nodes
ListBaseT< GPUNodeGraphOutputLink > outlink_compositor
GPUNodeLink * outlink_volume
ListBaseT< GPUMaterialTexture > textures
GPUUniformAttrList uniform_attrs
GPUNodeStackValue value
const char * name
ListBaseT< GPUInput > inputs
static constexpr GPUSamplerState internal_sampler()
ListBaseT< GPUUniformAttr > list
GPUUniformAttr * next
eNodeSocketDatatype type
eNodeSocketInOut in_out