Blender V5.3
svm.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include <algorithm>
6#include <functional>
7
8#include "device/device.h"
9
10#include "scene/background.h"
11#include "scene/light.h"
12#include "scene/mesh.h"
13#include "scene/scene.h"
14#include "scene/shader.h"
15#include "scene/shader_graph.h"
16#include "scene/shader_nodes.h"
17#include "scene/stats.h"
18#include "scene/svm.h"
19
21
22#include "util/log.h"
23#include "util/map.h"
24#include "util/math_float3.h"
25#include "util/progress.h"
26#include "util/queue.h"
27#include "util/task.h"
28
30
31/* Shader Manager */
32
34
36
38 Shader *shader,
39 Progress &progress,
40 array<int> *svm_nodes)
41{
42 if (progress.get_cancel()) {
43 return;
44 }
45 assert(shader->graph);
46
48 SVMCompiler compiler(scene, progress);
49 compiler.background = (shader == scene->background->get_shader(scene));
50 compiler.compile(shader, *svm_nodes, 0, &summary);
51
52 LOG_DEBUG << "Compilation summary:\n"
53 << "Shader name: " << shader->name << "\n"
54 << summary.full_report();
55}
56
58 DeviceScene *dscene,
59 Scene *scene,
60 Progress &progress)
61{
62 if (!need_update()) {
63 return;
64 }
65
66 const scoped_callback_timer timer([scene](double time) {
67 if (scene->update_stats) {
68 scene->update_stats->svm.times.add_entry({"device_update", time});
69 }
70 });
71
72 const int num_shaders = scene->shaders.size();
73
74 LOG_INFO << "Total " << num_shaders << " shaders.";
75
76 const double start_time = time_dt();
77
78 /* test if we need to update */
79 device_free(device, dscene, scene);
80
81 /* Build all shaders. */
82 TaskPool task_pool;
83 vector<array<int>> shader_svm_nodes(num_shaders);
84 for (int i = 0; i < num_shaders; i++) {
85 task_pool.push([this, scene, &progress, &shader_svm_nodes, i] {
86 device_update_shader(scene, scene->shaders[i], progress, &shader_svm_nodes[i]);
87 });
88 }
89 task_pool.wait_work();
90
91 if (progress.get_cancel()) {
92 return;
93 }
94
95 /* The global node list contains a jump table (one jump node per shader)
96 * followed by the nodes of all shaders. */
97 const int jump_node_size = 1 + sizeof(SVMNodeShaderJump) / sizeof(int);
98 int svm_nodes_size = num_shaders * jump_node_size;
99 for (int i = 0; i < num_shaders; i++) {
100 /* Since we're not copying the local jump node, the size ends up lower. */
101 svm_nodes_size += shader_svm_nodes[i].size() - jump_node_size;
102 }
103
104 int *svm_nodes = dscene->svm_nodes.alloc(svm_nodes_size);
105
106 int node_offset = num_shaders * jump_node_size;
107 for (int i = 0; i < num_shaders; i++) {
108 Shader *shader = scene->shaders[i];
109
110 shader->clear_modified();
112 scene->light_manager->tag_update(scene, LightManager::SHADER_COMPILED);
113 }
114
115 /* Update the global jump table.
116 * Each compiled shader starts with a jump node that has offsets local
117 * to the shader, so copy those and add the offset into the global node list. */
118 const int base = shader->id * jump_node_size;
119 svm_nodes[base + 0] = NODE_SHADER_JUMP;
120 *reinterpret_cast<SVMNodeShaderJump *>(&svm_nodes[base + 1]) = SVMNodeShaderJump{
121 .offset_surface = shader_svm_nodes[i][1] - jump_node_size + node_offset,
122 .offset_volume = shader_svm_nodes[i][2] - jump_node_size + node_offset,
123 .offset_displacement = shader_svm_nodes[i][3] - jump_node_size + node_offset};
124 node_offset += shader_svm_nodes[i].size() - jump_node_size;
125 }
126
127 /* Copy the nodes of each shader into the correct location. */
128 int *dst = svm_nodes + num_shaders * jump_node_size;
129 for (int i = 0; i < num_shaders; i++) {
130 const int shader_size = shader_svm_nodes[i].size() - jump_node_size;
131
132 std::copy_n(&shader_svm_nodes[i][jump_node_size], shader_size, dst);
133 dst += shader_size;
134 }
135
136 if (progress.get_cancel()) {
137 return;
138 }
139
140 device_update_common(device, dscene, scene, progress);
141
143
144 LOG_INFO << "Shader manager updated " << num_shaders << " shaders in " << time_dt() - start_time
145 << " seconds.";
146}
147
149{
150 device_free_common(device, dscene, scene);
151
152 dscene->svm_nodes.free();
153}
154
155/* Graph Compiler */
156
158{
159 max_stack_use = 0;
161 current_shader = nullptr;
162 current_graph = nullptr;
163 background = false;
166 compile_failed = false;
167
168 /* This struct has one entry for every node, in order of ShaderNodeType definition. */
169 svm_node_types_used = (std::atomic_int *)&scene->dscene.data.svm_usage;
170}
171
173{
174 int size = 0;
175
176 switch (type) {
178 case SocketType::INT:
179 size = 1;
180 break;
185 size = 3;
186 break;
188 size = 0;
189 break;
190 default:
191 assert(0);
192 break;
193 }
194
195 return size;
196}
197
199{
200 const SocketType::Type type = io->type();
201 const bool derivative = io->parent->need_derivatives();
202
203 return derivative ? stack_size(type) * 3 : stack_size(type);
204}
205
207{
208 int offset = -1;
209
210 /* find free space in stack & mark as used */
211 for (int i = 0, num_unused = 0; i < SVM_STACK_SIZE; i++) {
212 if (active_stack.users[i]) {
213 num_unused = 0;
214 }
215 else {
216 num_unused++;
217 }
218
219 if (num_unused == size) {
220 offset = i + 1 - size;
222
223 while (i >= offset) {
224 active_stack.users[i--] = 1;
225 }
226
227 return offset;
228 }
229 }
230
231 if (!compile_failed) {
232 compile_failed = true;
233 LOG_ERROR << "Shader graph: out of SVM stack space, shader \"" << current_shader->name
234 << "\" too big.";
235 }
236
237 return 0;
238}
239
244
246{
247 const int size = stack_size(io);
248
249 for (int i = 0; i < size; i++) {
250 active_stack.users[offset + i]--;
251 }
252}
253
255{
256 /* stack offset assign? */
257 if (input->stack_offset == SVM_STACK_INVALID) {
258 if (input->link) {
259 /* linked to output -> use output offset */
261 input->stack_offset = input->link->stack_offset;
262 }
263 else {
264 ShaderNode *node = input->parent;
265
266 /* not linked to output -> add nodes to load default value */
267 input->stack_offset = stack_find_offset(input);
268
269 if (input->type() == SocketType::FLOAT) {
270 add_value_node(node, node->get_float(input->socket_type), input->stack_offset);
271 }
272 else if (input->type() == SocketType::INT) {
274 node, __int_as_float(node->get_int(input->socket_type)), input->stack_offset);
275 }
276 else if (input->type() == SocketType::VECTOR || input->type() == SocketType::NORMAL ||
277 input->type() == SocketType::POINT || input->type() == SocketType::COLOR)
278 {
279 add_value_node(node, node->get_float3(input->socket_type), input->stack_offset);
280 }
281 else { /* should not get called for closure */
282 assert(0);
283 }
284 }
285 }
286
287 return input->stack_offset;
288}
289
291{
292 /* if no stack offset assigned yet, find one */
293 if (output->stack_offset == SVM_STACK_INVALID) {
294 output->stack_offset = stack_find_offset(output);
295 }
296
297 return output->stack_offset;
298}
299
301{
302 ShaderInput *input = current_node->input(name);
303 SVMInputInt result = {input->parent->get_int(input->socket_type), SVM_STACK_INVALID};
304 if (input->link) {
305 result.offset = stack_assign(input);
306 }
307 return result;
308}
309
311{
312 ShaderInput *input = current_node->input(name);
313 if (input->link) {
315 }
316 float default_value = input->parent->get_float(input->socket_type);
317 /* Filter out NaN that would collide with SVM_INPUT_STACK_OFFSET_MASK. */
318 if (!isfinite_safe(default_value)) {
319 default_value = 0.0f;
320 }
321 return SVMInputFloat{__float_as_uint(default_value)};
322}
323
325{
326 ShaderInput *input = current_node->input(name);
327 if (input->link) {
328 return SVMInputFloat3{
329 SVMInputFloat{SVM_INPUT_STACK_OFFSET_MASK | uint(stack_assign(input))}, {0}, {0}};
330 }
331 float3 default_value = input->parent->get_float3(input->socket_type);
332 /* Filter out NaN that would collide with SVM_INPUT_STACK_OFFSET_MASK. */
333 if (!isfinite_safe(default_value)) {
334 default_value = zero_float3();
335 }
336 return SVMInputFloat3{
337 {__float_as_uint(default_value.x)},
338 {__float_as_uint(default_value.y)},
339 {__float_as_uint(default_value.z)},
340 };
341}
342
347
349{
350 /* This is for sockets like normal which always expect a link. For the constant_folded_in we have
351 * to write the value to the stack with another load and return a linked svm offset, as these
352 * never store the default value in the SVMNode. */
353 ShaderInput *input = current_node->input(name);
354 /* Ensure input link is pushed to SVM before the node itself. */
355 assert(!(current_node->added_to_svm && input->constant_folded_in && input->link == nullptr &&
357 return (input->link || input->constant_folded_in) ? stack_assign(input) : SVM_STACK_INVALID;
358}
359
361{
362 ShaderOutput *shader_output = current_node->output(name);
363 return output(shader_output);
364}
365
367{
368 return (!shader_output->links.empty()) ? stack_assign(shader_output) : SVM_STACK_INVALID;
369}
370
372{
373 if (output->stack_offset == SVM_STACK_INVALID) {
374 assert(input->link);
375 assert(stack_size(output->type()) == stack_size(input->link->type()));
376
377 output->stack_offset = input->link->stack_offset;
378 const int size = stack_size(output);
379
380 for (int i = 0; i < size; i++) {
381 active_stack.users[output->stack_offset + i]++;
382 }
383 }
384}
385
387 const ShaderOutput *output,
388 const ShaderNodeSet &done)
389{
390 /* Check if the node is the only remaining user of the output, meaning the
391 * output's stack space can be freed once the node is compiled. */
392
393 /* optimization we should add: verify if in->parent is actually used */
394 for (const ShaderInput *in : output->links) {
395 if (in->parent != node && !done.contains(in->parent)) {
396 return false;
397 }
398 }
399 return true;
400}
401
403{
404 /* Possible minor optimization: If all nodes read all inputs before writing outputs,
405 * the input stack space could be reused for the output and cache locality would be
406 * improved. This was tried at some point but disabled, it would need careful validation
407 * of stack assignment code and every SVM node implementation. It's not obvious if it's
408 * worth it. */
409
410 for (ShaderInput *input : node->inputs) {
411 ShaderOutput *output = input->link;
412
413 if (output && output->stack_offset != SVM_STACK_INVALID && is_sole_user(node, output, done)) {
414 stack_clear_offset(output, output->stack_offset);
415 output->stack_offset = SVM_STACK_INVALID;
416
417 for (ShaderInput *in : output->links) {
418 in->stack_offset = SVM_STACK_INVALID;
419 }
420 }
421 }
422}
423
425{
426 for (ShaderInput *input : node->inputs) {
427 if (!input->link && input->stack_offset != SVM_STACK_INVALID) {
428 stack_clear_offset(input, input->stack_offset);
430 }
431 }
432}
433
439
441{
442 switch (type) {
443#define SHADER_NODE_TYPE_DERIVATIVE(name) \
444 case name: \
445 return name##_DERIVATIVE;
447
448 default:
449 break;
450 }
451
452 return type;
453}
454
456 const ShaderNodeType type,
457 const bool use_derivatives)
458{
459 if ((use_derivatives || (shader_node && shader_node->need_derivatives())) &&
461 {
463 }
464 return type;
465}
466
474
479
481 const float value,
482 const int stack_offset)
483{
484 add_node(shader_node,
485 NODE_VALUE_F,
487 .value = value,
488 .out_offset = (SVMStackOffset)stack_offset,
489 });
490}
491
493 const float3 &value,
494 const int stack_offset)
495{
496 add_node(shader_node,
497 NODE_VALUE_V,
499 .out_offset = (SVMStackOffset)stack_offset,
500 .value = value,
501 });
502}
503
505{
506 /* No derivatives in volumes yet. */
508 return;
509 }
510 /* Does this node need derivatives but it doesn't have a derivative variation? */
511 const bool incomplete_derivatives = node->need_derivatives() &&
512 svm_node_type_with_derivatives(node->shader_node_type()) ==
513 node->shader_node_type();
514 if (!incomplete_derivatives) {
515 return;
516 }
517
518 /* Zero derivatives. Note we can not use add_value_node since it will
519 * automatically write derivatives. */
520 for (const ShaderOutput *output : node->outputs) {
521 if (output->stack_offset == SVM_STACK_INVALID) {
522 continue;
523 }
524 const int base_size = stack_size(output->type());
525 if (base_size == 3) {
526 add_value_node(nullptr, zero_float3(), output->stack_offset + 3);
527 add_value_node(nullptr, zero_float3(), output->stack_offset + 6);
528 }
529 else if (base_size == 1) {
530 add_value_node(nullptr, 0.0f, output->stack_offset + 1);
531 add_value_node(nullptr, 0.0f, output->stack_offset + 2);
532 }
533 }
534}
535
536uint SVMCompiler::attribute(ustring name)
537{
538 return scene->shader_manager->get_attribute_id(name);
539}
540
542{
543 return scene->shader_manager->get_attribute_id(std);
544}
545
547{
549 return (std) ? attribute(std) : attribute(name);
550}
551
553 const ShaderNodeSet &done,
554 ShaderInput *input,
555 ShaderNode *skip_node)
556{
557 ShaderNode *node = (input->link) ? input->link->parent : nullptr;
558 if (node != nullptr && !done.contains(node) && node != skip_node && !dependencies.contains(node))
559 {
560 for (ShaderInput *in : node->inputs) {
561 find_dependencies(dependencies, done, in, skip_node);
562 }
563 dependencies.insert(node);
564 }
565}
566
568{
570 node->compile(*this);
571 current_node = nullptr;
573 stack_clear_users(node, done);
575
577 if (node->has_spatial_varying()) {
579 }
580 if (node->get_feature() & KERNEL_FEATURE_NODE_RAYTRACE) {
582 }
583 }
584 else if (current_type == SHADER_TYPE_VOLUME) {
585 if (node->has_spatial_varying()) {
587 }
588 if (node->has_attribute_dependency()) {
590 }
591 }
592}
593
595{
596 /* Compute stack size that will be allocated by this node. */
597 int size = 0;
598 for (const ShaderOutput *output : node->outputs) {
599 if (!output->links.empty() && output->stack_offset == SVM_STACK_INVALID) {
600 size += stack_size(output);
601 }
602 }
603 return size;
604}
605
607{
608 ShaderNodeSet &done = state->nodes_done;
609 vector<bool> &done_flag = state->nodes_done_flag;
610
611 /* Schedule the nodes to reduce peak SVM stack usage with a Sethi-Ullman style
612 * heuristic. This is optimal for trees, but only a heuristic for DAGs where
613 * it's an NP-hard problem.
614 *
615 * Nodes whose sub-graphs need the most stack are generated first, which helps
616 * complete tightly related sub-graphs before handling other parts of the graph.
617 *
618 * See "Generalizations of the Sethi-Ullman algorithm for register allocation"
619 * by Appel and Supowit for details. We use different terminology as some of it
620 * conflicts with our own.
621 *
622 * The Sethi-Ullman number is the peak SVM stack size needed to evaluate a node
623 * and "producer" nodes feeding into it, including the node output stack size.
624 *
625 * It was proven that evaluating producer nodes by descending order of this number
626 * minus the output stack size is optimal for trees. For a graph, we approximate
627 * this by only counting the output stack size for a producer node with multiple
628 * consumers. */
629
630 /* Producer nodes feeding into #node that have not been scheduled yet. */
631 auto get_producers = [&](const ShaderNode *node, vector<ShaderNode *> &producers) {
632 producers.clear();
633 for (const ShaderInput *input : node->inputs) {
634 if (input->link) {
635 ShaderNode *producer = input->link->parent;
636 if (!done_flag[producer->id] &&
637 std::find(producers.begin(), producers.end(), producer) == producers.end())
638 {
639 producers.push_back(producer);
640 }
641 }
642 }
643 };
644
645 /* Number of unique unscheduled consumer nodes for each producer node. */
646 unordered_map<const ShaderNode *, int> num_consumers;
647 vector<ShaderNode *> consumers;
648 for (ShaderNode *node : nodes) {
649 if (done_flag[node->id]) {
650 continue;
651 }
652 consumers.clear();
653 for (const ShaderOutput *output : node->outputs) {
654 for (const ShaderInput *in : output->links) {
655 ShaderNode *consumer = in->parent;
656 if (!done_flag[consumer->id] && nodes.contains(consumer) &&
657 std::find(consumers.begin(), consumers.end(), consumer) == consumers.end())
658 {
659 consumers.push_back(consumer);
660 }
661 }
662 }
663 num_consumers[node] = consumers.size();
664 }
665
666 /* Sethi-Ullman number for each node. */
667 unordered_map<const ShaderNode *, int> sethi_ullman_number;
668
669 /* Current Sethi-Ullman number for graph scheduling, only counting the output
670 * size when there are multiple consumers. */
671 auto current_sethi_ullman_number = [&](ShaderNode *node) -> int {
672 return (num_consumers[node] > 1) ? stack_node_output_size(node) : sethi_ullman_number[node];
673 };
674
675 /* Order producers by Sethi-Ullman number. Node ID is the tie breaker. */
676 auto node_order_key = [&](ShaderNode *node) -> int {
677 return current_sethi_ullman_number(node) - stack_node_output_size(node);
678 };
679 auto node_order_compare = [&](ShaderNode *a, ShaderNode *b) {
680 return node_order_key(a) > node_order_key(b) ||
681 (node_order_key(a) == node_order_key(b) && a->id < b->id);
682 };
683
684 /* Compute Sethi-Ullman number recursively. */
685 std::function<int(ShaderNode *)> compute_sethi_ullman_number = [&](ShaderNode *node) -> int {
686 const auto it = sethi_ullman_number.find(node);
687 if (it != sethi_ullman_number.end()) {
688 return it->second;
689 }
690
691 vector<ShaderNode *> producers;
692 get_producers(node, producers);
693 for (ShaderNode *producer : producers) {
694 compute_sethi_ullman_number(producer);
695 }
696 std::sort(producers.begin(), producers.end(), node_order_compare);
697
698 /* Sum output and peak stack usage of producers. */
699 int output_size = 0;
700 int peak_size = 0;
701 for (ShaderNode *producer : producers) {
702 peak_size = max(peak_size, output_size + current_sethi_ullman_number(producer));
703 output_size += stack_node_output_size(producer);
704 }
705 peak_size = max(peak_size, output_size + stack_node_output_size(node));
706 sethi_ullman_number[node] = peak_size;
707
708 return peak_size;
709 };
710
711 /* Gather all sink nodes (that have no unscheduled consumers) and sort by
712 * Sethi-Ullman number. */
714 for (ShaderNode *node : nodes) {
715 if (done_flag[node->id]) {
716 continue;
717 }
718 compute_sethi_ullman_number(node);
719 bool is_sink = true;
720 for (const ShaderOutput *output : node->outputs) {
721 for (const ShaderInput *in : output->links) {
722 if (!done_flag[in->parent->id] && nodes.contains(in->parent)) {
723 is_sink = false;
724 break;
725 }
726 }
727 if (!is_sink) {
728 break;
729 }
730 }
731 if (is_sink) {
732 sinks.push_back(node);
733 }
734 }
735 std::sort(sinks.begin(), sinks.end(), node_order_compare);
736
737 /* Generate nodes recursively from sink nodes. */
738 std::function<void(ShaderNode *)> generate = [&](ShaderNode *node) {
739 if (done_flag[node->id]) {
740 return;
741 }
742 vector<ShaderNode *> producers;
743 get_producers(node, producers);
744 std::sort(producers.begin(), producers.end(), node_order_compare);
745 for (ShaderNode *producer : producers) {
746 generate(producer);
747 }
748 generate_node(node, done);
749 done.insert(node);
750 done_flag[node->id] = true;
751 };
752
753 for (ShaderNode *node : sinks) {
754 generate(node);
755 }
756}
757
759{
760 /* Skip generating closure that are not supported or needed for a particular
761 * type of shader. For example a BSDF in a volume shader. */
762 const uint64_t node_feature = node->get_feature();
763 if ((state->node_feature_mask & node_feature) != node_feature) {
764 return;
765 }
766
767 /* execute dependencies for closure */
768 for (ShaderInput *in : node->inputs) {
769 if (in->link != nullptr) {
770 ShaderNodeSet dependencies;
771 find_dependencies(dependencies, state->nodes_done, in);
772 generate_svm_nodes(dependencies, state);
773 }
774 }
775
776 /* closure mix weight */
777 const char *weight_name = (current_type == SHADER_TYPE_VOLUME) ? "VolumeMixWeight" :
778 "SurfaceMixWeight";
779 ShaderInput *weight_in = node->input(weight_name);
780
781 if (weight_in && (weight_in->link || node->get_float(weight_in->socket_type) != 1.0f)) {
782 mix_weight_offset = stack_assign(weight_in);
783 }
784 else {
786 }
787
788 /* compile closure itself */
789 generate_node(node, state->nodes_done);
790
792
794 if (node->has_surface_transparent()) {
796 }
797 if (node->has_surface_bssrdf()) {
799 if (node->has_bssrdf_bump()) {
801 }
802 }
803 if (node->has_bump()) {
805 }
806 if (node->has_dispersion()) {
808 }
809 }
810}
811
815 const ShaderNodeSet &shared)
816{
817 if (shared.contains(node)) {
818 generate_multi_closure(root_node, node, state);
819 }
820 else {
821 for (ShaderInput *in : node->inputs) {
822 if (in->type() == SocketType::CLOSURE && in->link) {
823 generated_shared_closure_nodes(root_node, in->link->parent, state, shared);
824 }
825 }
826 }
827}
828
830 ShaderGraph *graph,
832{
833 for (ShaderNode *node : graph->nodes) {
834 if (node->special_type == SHADER_SPECIAL_TYPE_OUTPUT_AOV) {
835 OutputAOVNode *aov_node = static_cast<OutputAOVNode *>(node);
836 if (aov_node->offset >= 0) {
837 aov_nodes.insert(aov_node);
838 for (ShaderInput *in : node->inputs) {
839 if (in->link != nullptr) {
840 find_dependencies(aov_nodes, state->nodes_done, in);
841 }
842 }
843 }
844 }
845 }
846}
847
851{
852 /* only generate once */
853 if (state->closure_done.contains(node)) {
854 return;
855 }
856
857 state->closure_done.insert(node);
858
859 if (node->special_type == SHADER_SPECIAL_TYPE_COMBINE_CLOSURE) {
860 /* weighting is already taken care of in ShaderGraph::transform_multi_closure */
861 ShaderInput *cl1in = node->input("Closure1");
862 ShaderInput *cl2in = node->input("Closure2");
863 ShaderInput *facin = node->input("Fac");
864
865 /* skip empty mix/add closure nodes */
866 if (!cl1in->link && !cl2in->link) {
867 return;
868 }
869
870 if (facin && facin->link) {
871 /* mix closure: generate instructions to compute mix weight */
872 ShaderNodeSet dependencies;
873 find_dependencies(dependencies, state->nodes_done, facin);
874 generate_svm_nodes(dependencies, state);
875
876 /* execute shared dependencies. this is needed to allow skipping
877 * of zero weight closures and their dependencies later, so we
878 * ensure that they only skip dependencies that are unique to them */
879 ShaderNodeSet cl1deps;
880 ShaderNodeSet cl2deps;
881 ShaderNodeSet shareddeps;
882
883 find_dependencies(cl1deps, state->nodes_done, cl1in);
884 find_dependencies(cl2deps, state->nodes_done, cl2in);
885
886 const ShaderNodeIDComparator node_id_comp;
887 set_intersection(cl1deps.begin(),
888 cl1deps.end(),
889 cl2deps.begin(),
890 cl2deps.end(),
891 std::inserter(shareddeps, shareddeps.begin()),
892 node_id_comp);
893
894 /* it's possible some nodes are not shared between this mix node
895 * inputs, but still needed to be always executed, this mainly
896 * happens when a node of current subbranch is used by a parent
897 * node or so */
898 if (root_node != node) {
899 for (ShaderInput *in : root_node->inputs) {
900 ShaderNodeSet rootdeps;
901 find_dependencies(rootdeps, state->nodes_done, in, node);
902 set_intersection(rootdeps.begin(),
903 rootdeps.end(),
904 cl1deps.begin(),
905 cl1deps.end(),
906 std::inserter(shareddeps, shareddeps.begin()),
907 node_id_comp);
908 set_intersection(rootdeps.begin(),
909 rootdeps.end(),
910 cl2deps.begin(),
911 cl2deps.end(),
912 std::inserter(shareddeps, shareddeps.begin()),
913 node_id_comp);
914 }
915 }
916
917 /* For dependencies AOV nodes, prevent them from being categorized
918 * as exclusive deps of one or the other closure, since the need to
919 * execute them for AOV writing is not dependent on the closure
920 * weights. */
921 if (!state->aov_nodes.empty()) {
922 set_intersection(state->aov_nodes.begin(),
923 state->aov_nodes.end(),
924 cl1deps.begin(),
925 cl1deps.end(),
926 std::inserter(shareddeps, shareddeps.begin()),
927 node_id_comp);
928 set_intersection(state->aov_nodes.begin(),
929 state->aov_nodes.end(),
930 cl2deps.begin(),
931 cl2deps.end(),
932 std::inserter(shareddeps, shareddeps.begin()),
933 node_id_comp);
934 }
935
936 if (!shareddeps.empty()) {
937 if (cl1in->link) {
938 generated_shared_closure_nodes(root_node, cl1in->link->parent, state, shareddeps);
939 }
940 if (cl2in->link) {
941 generated_shared_closure_nodes(root_node, cl2in->link->parent, state, shareddeps);
942 }
943
944 generate_svm_nodes(shareddeps, state);
945 }
946
947 /* generate instructions for input closure 1 */
948 if (cl1in->link) {
949 /* Add instruction to skip closure and its dependencies if mix
950 * weight is zero.
951 */
952 const int node_start = current_svm_nodes.size();
953 add_node(nullptr, NODE_JUMP_IF_ONE, SVMNodeJumpIfOne{0, stack_assign(facin)});
954
955 generate_multi_closure(root_node, cl1in->link->parent, state);
956
957 /* Fill in jump instruction location to be after closure. */
958 const int jump_node_size = 1 + sizeof(SVMNodeJumpIfOne) / sizeof(int);
959 current_svm_nodes[node_start + 1] = current_svm_nodes.size() -
960 (node_start + jump_node_size);
961 }
962
963 /* generate instructions for input closure 2 */
964 if (cl2in->link) {
965 /* Add instruction to skip closure and its dependencies if mix
966 * weight is zero.
967 */
968 const int node_start = current_svm_nodes.size();
969 add_node(nullptr, NODE_JUMP_IF_ZERO, SVMNodeJumpIfZero{0, stack_assign(facin)});
970
971 generate_multi_closure(root_node, cl2in->link->parent, state);
972
973 /* Fill in jump instruction location to be after closure. */
974 const int jump_node_size = 1 + sizeof(SVMNodeJumpIfZero) / sizeof(int);
975 current_svm_nodes[node_start + 1] = current_svm_nodes.size() -
976 (node_start + jump_node_size);
977 }
978
979 /* unassign */
981 }
982 else {
983 /* execute closures and their dependencies, no runtime checks
984 * to skip closures here because was already optimized due to
985 * fixed weight or add closure that always needs both */
986 if (cl1in->link) {
987 generate_multi_closure(root_node, cl1in->link->parent, state);
988 }
989 if (cl2in->link) {
990 generate_multi_closure(root_node, cl2in->link->parent, state);
991 }
992 }
993 }
994 else {
996 }
997
998 state->nodes_done.insert(node);
999 state->nodes_done_flag[node->id] = true;
1000}
1001
1003 ShaderGraph *graph,
1004 const ShaderType type)
1005{
1006 if (type == SHADER_TYPE_VOLUME) {
1007 /* Only support derivatives for surface for now. */
1008 return;
1009 }
1010 queue<ShaderNode *> traverse_queue;
1011 ShaderNodeSet scheduled;
1012 /* Check if texture nodes need derivatives. */
1013 for (ShaderNode *node : graph->nodes) {
1014 if (node->is_texture_node_and_needs_derivatives(compiler)) {
1015 traverse_queue.push(node);
1016 scheduled.insert(node);
1017 }
1018 }
1019 /* Mark all ancestors of texture nodes as requiring derivatives, if the texture nodes themselves
1020 * need derivatives. */
1021 while (!traverse_queue.empty()) {
1022 ShaderNode *node = traverse_queue.front();
1023 traverse_queue.pop();
1024 node->set_need_derivatives();
1025 LOG_DEBUG << "Marking " << node->name << " as requiring derivatives";
1026 for (ShaderInput *input : node->inputs) {
1027 if (input->link == nullptr) {
1028 continue;
1029 }
1030 if (scheduled.contains(input->link->parent)) {
1031 continue;
1032 }
1033 traverse_queue.push(input->link->parent);
1034 scheduled.insert(input->link->parent);
1035 }
1036 }
1037}
1038
1040{
1041 /* Converting a shader graph into svm_nodes that can be executed
1042 * sequentially on the virtual machine is fairly simple. We can keep
1043 * looping over nodes and each time all the inputs of a node are
1044 * ready, we add svm_nodes for it that read the inputs from the
1045 * stack and write outputs back to the stack.
1046 *
1047 * With the SVM, we always sample only a single closure. We can think
1048 * of all closures nodes as a binary tree with mix closures as inner
1049 * nodes and other closures as leafs. The SVM will traverse that tree,
1050 * each time deciding to go left or right depending on the mix weights,
1051 * until a closure is found.
1052 *
1053 * We only execute nodes that are needed for the mix weights and chosen
1054 * closure.
1055 */
1056
1057 current_type = type;
1058 current_graph = graph;
1059
1060 /* get input in output node */
1061 ShaderNode *output = graph->output();
1062 ShaderInput *clin = nullptr;
1063
1064 switch (type) {
1066 clin = output->input("Surface");
1067 break;
1068 case SHADER_TYPE_VOLUME:
1069 clin = output->input("Volume");
1070 break;
1072 clin = output->input("Displacement");
1073 break;
1074 case SHADER_TYPE_BUMP:
1075 clin = output->input("Normal");
1076 break;
1077 default:
1078 assert(0);
1079 break;
1080 }
1081
1082 /* clear all compiler state */
1083 memset((void *)&active_stack, 0, sizeof(active_stack));
1085
1086 for (ShaderNode *node : graph->nodes) {
1087 node->added_to_svm = false;
1088 for (ShaderInput *input : node->inputs) {
1090 }
1091 for (ShaderOutput *output : node->outputs) {
1092 output->stack_offset = SVM_STACK_INVALID;
1093 }
1094 }
1095
1096 mark_nodes_requiring_derivatives(*this, graph, type);
1097
1098 /* for the bump shader we need add a node to store the shader state */
1099 const bool need_bump_state = (type == SHADER_TYPE_BUMP) &&
1100 (shader->get_displacement_method() == DISPLACE_BOTH);
1101 if (need_bump_state) {
1103 add_node(
1104 nullptr, NODE_ENTER_BUMP_EVAL, SVMNodeEnterBumpEval{.state_offset = bump_state_offset});
1105 }
1106
1107 if (shader->reference_count()) {
1108 CompilerState state(graph);
1109
1110 switch (type) {
1111 case SHADER_TYPE_SURFACE: /* generate surface shader */
1112 find_aov_nodes_and_dependencies(state.aov_nodes, graph, &state);
1113 if (shader->has_surface) {
1114 state.node_feature_mask = KERNEL_FEATURE_NODE_MASK_SURFACE;
1115 }
1116 break;
1117 case SHADER_TYPE_VOLUME: /* generate volume shader */
1118 if (shader->has_volume) {
1119 state.node_feature_mask = KERNEL_FEATURE_NODE_MASK_VOLUME;
1120 }
1121 break;
1122 case SHADER_TYPE_DISPLACEMENT: /* generate displacement shader */
1123 if (shader->has_displacement) {
1124 state.node_feature_mask = KERNEL_FEATURE_NODE_MASK_DISPLACEMENT;
1125 }
1126 break;
1127 case SHADER_TYPE_BUMP: /* generate bump shader */
1128 if (clin->link) {
1129 state.node_feature_mask = KERNEL_FEATURE_NODE_MASK_BUMP;
1130 }
1131 break;
1132 default:
1133 break;
1134 }
1135
1136 if (clin->link) {
1138 }
1139
1140 /* compile output node */
1142 output->compile(*this);
1143 current_node = nullptr;
1144
1145 if (!state.aov_nodes.empty()) {
1146 /* AOV passes are only written if the object is directly visible, so
1147 * there is no point in evaluating all the nodes generated only for the
1148 * AOV outputs if that's not the case. Therefore, we insert
1149 * NODE_AOV_START into the shader before the AOV-only nodes are
1150 * generated which tells the kernel that it can stop evaluation
1151 * early if AOVs will not be written. */
1152 add_node(NODE_AOV_START);
1153 generate_svm_nodes(state.aov_nodes, &state);
1154 }
1155 }
1156
1157 /* add node to restore state after bump shader has finished */
1158 if (need_bump_state) {
1159 add_node(
1160 nullptr, NODE_LEAVE_BUMP_EVAL, SVMNodeLeaveBumpEval{.state_offset = bump_state_offset});
1162 }
1163
1164 /* if compile failed, generate empty shader */
1165 if (compile_failed) {
1167 compile_failed = false;
1168 }
1169
1170 /* for bump shaders we fall thru to the surface shader, but if this is any other kind of shader
1171 * it ends here */
1172 if (type != SHADER_TYPE_BUMP) {
1173 add_node(NODE_END);
1174 }
1175}
1176
1177void SVMCompiler::compile(Shader *shader, array<int> &svm_nodes, const int index, Summary *summary)
1178{
1179 svm_node_types_used[NODE_SHADER_JUMP] = true;
1180 add_node(nullptr, NODE_SHADER_JUMP, SVMNodeShaderJump{0, 0, 0});
1181 svm_nodes.append(current_svm_nodes);
1183
1184 /* copy graph for shader with bump mapping */
1185 const int start_num_svm_nodes = svm_nodes.size();
1186
1187 const double time_start = time_dt();
1188
1189 const bool has_bump_from_displacement = shader->has_bump_from_displacement;
1190
1191 current_shader = shader;
1192
1193 /* generate bump shader */
1194 if (has_bump_from_displacement) {
1195 const scoped_timer timer((summary != nullptr) ? &summary->time_generate_bump : nullptr);
1196 compile_type(shader, shader->graph.get(), SHADER_TYPE_BUMP);
1197 svm_nodes[index + 1] = svm_nodes.size();
1198 svm_nodes.append(current_svm_nodes);
1199 }
1200
1201 /* generate surface shader */
1202 {
1203 const scoped_timer timer((summary != nullptr) ? &summary->time_generate_surface : nullptr);
1204 compile_type(shader, shader->graph.get(), SHADER_TYPE_SURFACE);
1205 /* only set jump offset if there's no bump shader, as the bump shader will fall thru to this
1206 * one if it exists */
1207 if (!has_bump_from_displacement) {
1208 svm_nodes[index + 1] = svm_nodes.size();
1209 }
1210 svm_nodes.append(current_svm_nodes);
1211 }
1212
1213 /* generate volume shader */
1214 {
1215 const scoped_timer timer((summary != nullptr) ? &summary->time_generate_volume : nullptr);
1216 compile_type(shader, shader->graph.get(), SHADER_TYPE_VOLUME);
1217 svm_nodes[index + 2] = svm_nodes.size();
1218 svm_nodes.append(current_svm_nodes);
1219 }
1220
1221 /* generate displacement shader */
1222 {
1223 const scoped_timer timer((summary != nullptr) ? &summary->time_generate_displacement :
1224 nullptr);
1225 compile_type(shader, shader->graph.get(), SHADER_TYPE_DISPLACEMENT);
1226 svm_nodes[index + 3] = svm_nodes.size();
1227 svm_nodes.append(current_svm_nodes);
1228 }
1229
1230 /* Fill in summary information. */
1231 if (summary != nullptr) {
1232 summary->time_total = time_dt() - time_start;
1234 summary->num_svm_nodes = svm_nodes.size() - start_num_svm_nodes;
1235 }
1236
1237 /* Estimate emission for MIS. */
1238 shader->estimate_emission();
1239}
1240
1241/* Compiler summary implementation. */
1242
1253
1255{
1256 string report;
1257 report += string_printf("Number of SVM nodes: %d\n", num_svm_nodes);
1258 report += string_printf("Peak stack usage: %d\n", peak_stack_usage);
1259
1260 report += string_printf("Time (in seconds):\n");
1261 report += string_printf("Generate: %f\n",
1264 report += string_printf(" Surface: %f\n", time_generate_surface);
1265 report += string_printf(" Bump: %f\n", time_generate_bump);
1266 report += string_printf(" Volume: %f\n", time_generate_volume);
1267 report += string_printf(" Displacement: %f\n", time_generate_displacement);
1268
1269 return report;
1270}
1271
1272/* Global state of the compiler. */
1273
1275{
1276 int max_id = 0;
1277 for (ShaderNode *node : graph->nodes) {
1278 max_id = max(node->id, max_id);
1279 }
1280 nodes_done_flag.resize(max_id + 1, false);
1282}
1283
unsigned long long int uint64_t
btScalar max
static AttributeStandard name_standard(ustring name)
Shader * get_shader(const Scene *scene)
device_vector< int > svm_nodes
Definition devicescene.h:79
KernelData data
bool get_cancel() const
Definition progress.h:77
ShaderGraph * current_graph
Definition scene/svm.h:165
Progress & progress
Definition scene/svm.h:164
void compile_type(Shader *shader, ShaderGraph *graph, ShaderType type)
Definition svm.cpp:1039
int max_stack_use
Definition scene/svm.h:286
SVMStackOffset input_link(const char *name)
Definition svm.cpp:348
ShaderNodeType node_type(const ShaderNode *shader_node, const ShaderNodeType type, const bool use_derivatives)
Definition svm.cpp:455
void find_aov_nodes_and_dependencies(ShaderNodeSet &aov_nodes, ShaderGraph *graph, CompilerState *state)
Definition svm.cpp:829
SVMCompiler(Scene *scene, Progress &progress)
Definition svm.cpp:157
bool background
Definition scene/svm.h:167
SVMStackOffset output(const char *name)
Definition svm.cpp:360
void compile(Shader *shader, array< int > &svm_nodes, const int index, Summary *summary=nullptr)
Definition svm.cpp:1177
SVMInputFloat input_float(const char *name)
Definition svm.cpp:310
void generate_closure_node(ShaderNode *node, CompilerState *state)
Definition svm.cpp:758
void add_value_node(ShaderNode *shader_node, const float value, const int stack_offset)
Definition svm.cpp:480
ShaderType current_type
Definition scene/svm.h:283
SVMStackOffset bump_state_offset
Definition scene/svm.h:288
void generate_node(ShaderNode *node, ShaderNodeSet &done)
Definition svm.cpp:567
void stack_zero_incomplete_derivatives(const ShaderNode *node)
Definition svm.cpp:504
SVMStackOffset stack_find_offset(const ShaderIO *io)
Definition svm.cpp:240
Shader * current_shader
Definition scene/svm.h:284
void stack_clear_users(ShaderNode *node, ShaderNodeSet &done)
Definition svm.cpp:402
int stack_size(SocketType::Type type)
Definition svm.cpp:172
int stack_node_output_size(const ShaderNode *node)
Definition svm.cpp:594
void add_node(ShaderNodeType type)
Definition svm.cpp:434
void find_dependencies(ShaderNodeSet &dependencies, const ShaderNodeSet &done, ShaderInput *input, ShaderNode *skip_node=nullptr)
Definition svm.cpp:552
void stack_clear_temporary(ShaderNode *node)
Definition svm.cpp:424
uint attribute_standard(ustring name)
Definition svm.cpp:546
Stack active_stack
Definition scene/svm.h:285
bool is_sole_user(const ShaderNode *node, const ShaderOutput *output, const ShaderNodeSet &done)
Definition svm.cpp:386
void stack_link(ShaderInput *input, ShaderOutput *output)
Definition svm.cpp:371
ShaderNode * current_node
Definition scene/svm.h:166
void add_node_data_float(const float f)
Definition svm.cpp:475
SVMStackOffset mix_weight_offset
Definition scene/svm.h:287
void generate_svm_nodes(const ShaderNodeSet &nodes, CompilerState *state)
Definition svm.cpp:606
array< int > current_svm_nodes
Definition scene/svm.h:282
void generate_multi_closure(ShaderNode *root_node, ShaderNode *node, CompilerState *state)
Definition svm.cpp:848
void add_node_data_float4(const float4 &f)
Definition svm.cpp:467
SVMInputInt input_int(const char *name)
Definition svm.cpp:300
uint attribute(ustring name)
Definition svm.cpp:536
SVMStackOffset stack_assign(ShaderInput *input)
Definition svm.cpp:254
std::atomic_int * svm_node_types_used
Definition scene/svm.h:281
Scene * scene
Definition scene/svm.h:163
SVMInputFloat3 input_float3(const char *name)
Definition svm.cpp:324
bool compile_failed
Definition scene/svm.h:289
void stack_clear_offset(const ShaderIO *io, const SVMStackOffset offset)
Definition svm.cpp:245
void generated_shared_closure_nodes(ShaderNode *root_node, ShaderNode *node, CompilerState *state, const ShaderNodeSet &shared)
Definition svm.cpp:812
SVMInputFloat3 input_float3_from_offset(SVMStackOffset offset)
Definition svm.cpp:343
void device_free(Device *device, DeviceScene *dscene, Scene *scene) override
Definition svm.cpp:148
void device_update_specific(Device *device, DeviceScene *dscene, Scene *scene, Progress &progress) override
Definition svm.cpp:57
~SVMShaderManager() override
void device_update_shader(Scene *scene, Shader *shader, Progress &progress, array< int > *svm_nodes)
Definition svm.cpp:37
Definition scene.h:133
unique_ptr< LightManager > light_manager
Definition scene.h:167
unique_ptr< SceneUpdateStats > update_stats
Definition scene.h:197
Background * background
Definition scene.h:148
unique_ptr_vector< Shader > shaders
Definition scene.h:158
unique_ptr< ShaderManager > shader_manager
Definition scene.h:169
DeviceScene dscene
Definition scene.h:187
OutputNode * output()
unique_ptr_vector< ShaderNode > nodes
const SocketType & socket_type
ShaderNode * parent
int stack_offset
SocketType::Type type() const
ShaderOutput * link
bool constant_folded_in
void device_update_common(Device *device, DeviceScene *dscene, Scene *scene, Progress &progress)
bool need_update() const
void device_free_common(Device *device, DeviceScene *dscene, Scene *scene)
uint32_t update_flags
ShaderInput * input(const char *name)
bool need_derivatives() const
bool added_to_svm
unique_ptr_vector< ShaderInput > inputs
ShaderOutput * output(const char *name)
vector< ShaderInput * > links
bool has_dispersion
bool has_surface_spatial_varying
bool has_bump_from_surface
bool has_surface_bssrdf
void estimate_emission()
bool has_volume_attribute_dependency
bool has_volume
bool has_surface
bool has_bssrdf_bump
bool has_bump_from_displacement
EmissionSampling emission_sampling
bool has_surface_raytrace
bool has_displacement
bool has_surface_transparent
NODE_DECLARE unique_ptr< ShaderGraph > graph
bool has_volume_spatial_varying
void push(TaskRunFunction &&task)
Definition task.cpp:21
void wait_work(Summary *stats=nullptr)
Definition task.cpp:27
void append(const array< T > &from)
size_t size() const
void push_back_slow(const T &t)
T * alloc(const size_t width, const size_t height=0)
size_t size() const
#define SVM_STACK_SIZE
#define KERNEL_FEATURE_NODE_MASK_DISPLACEMENT
#define KERNEL_FEATURE_NODE_MASK_BUMP
#define KERNEL_FEATURE_NODE_MASK_VOLUME
#define SVM_BUMP_EVAL_STATE_SIZE
#define KERNEL_FEATURE_NODE_MASK_SURFACE
#define SVM_STACK_INVALID
#define SVM_INPUT_STACK_OFFSET_MASK
#define KERNEL_FEATURE_NODE_RAYTRACE
#define CCL_NAMESPACE_END
#define __int_as_float(x)
#define __float_as_int(x)
#define __float_as_uint(x)
#define assert(assertion)
#define shared
#define index(i)
const int state
ShaderNodeType
@ SHADER_TYPE_BUMP
@ SHADER_TYPE_SURFACE
@ SHADER_TYPE_VOLUME
@ SHADER_TYPE_DISPLACEMENT
uint8_t SVMStackOffset
@ EMISSION_SAMPLING_NONE
AttributeStandard
#define LOG_DEBUG
Definition log.h:107
#define LOG_ERROR
Definition log.h:101
#define LOG_INFO
Definition log.h:106
ccl_device_inline bool isfinite_safe(const float f)
Definition math_base.h:362
CCL_NAMESPACE_BEGIN ccl_device_inline float3 zero_float3()
Definition math_float3.h:17
@ DISPLACE_BOTH
@ SHADER_SPECIAL_TYPE_OUTPUT_AOV
@ SHADER_SPECIAL_TYPE_COMBINE_CLOSURE
set< ShaderNode *, ShaderNodeIDComparator > ShaderNodeSet
CCL_NAMESPACE_BEGIN string string_printf(const char *format,...)
Definition string.cpp:25
float get_float(const SocketType &input) const
float3 get_float3(const SocketType &input) const
ustring name
Definition graph/node.h:178
int reference_count() const
Definition graph/node.h:184
void clear_modified()
int get_int(const SocketType &input) const
vector< bool > nodes_done_flag
Definition scene/svm.h:235
CompilerState(ShaderGraph *graph)
Definition svm.cpp:1274
int users[SVM_STACK_SIZE]
Definition scene/svm.h:208
double time_generate_volume
Definition scene/svm.h:67
double time_generate_surface
Definition scene/svm.h:61
double time_generate_bump
Definition scene/svm.h:64
string full_report() const
Definition svm.cpp:1254
double time_generate_displacement
Definition scene/svm.h:70
float z
Definition sky_math.h:136
float y
Definition sky_math.h:136
float x
Definition sky_math.h:136
float y
Definition sky_math.h:225
float z
Definition sky_math.h:225
float x
Definition sky_math.h:225
float w
Definition sky_math.h:225
static void mark_nodes_requiring_derivatives(const SVMCompiler &compiler, ShaderGraph *graph, const ShaderType type)
Definition svm.cpp:1002
static ShaderNodeType svm_node_type_with_derivatives(ShaderNodeType type)
Definition svm.cpp:440
CCL_NAMESPACE_BEGIN double time_dt()
Definition time.cpp:31