Blender V5.3
scene/shader.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 "device/device.h"
6
7#include "scene/background.h"
8#include "scene/camera.h"
9#include "scene/integrator.h"
10#include "scene/light.h"
11#include "scene/mesh.h"
12#include "scene/object.h"
13#include "scene/osl.h"
14#include "scene/procedural.h"
15#include "scene/scene.h"
16#include "scene/shader.h"
17#include "scene/shader_graph.h"
18#include "scene/shader_nodes.h"
19#include "scene/svm.h"
20#include "scene/tables.h"
21#include "scene/volume.h"
22
23#include "util/colorspace.h"
24#include "util/log.h"
25#include "util/murmurhash.h"
26#include "util/transform.h"
27
28#include "scene/shader.tables"
29
31
33
34/* Shader */
35
37{
38 NodeType *type = NodeType::add("shader", create);
39
40 static NodeEnum emission_sampling_method_enum;
41 emission_sampling_method_enum.insert("none", EMISSION_SAMPLING_NONE);
42 emission_sampling_method_enum.insert("auto", EMISSION_SAMPLING_AUTO);
43 emission_sampling_method_enum.insert("front", EMISSION_SAMPLING_FRONT);
44 emission_sampling_method_enum.insert("back", EMISSION_SAMPLING_BACK);
45 emission_sampling_method_enum.insert("front_back", EMISSION_SAMPLING_FRONT_BACK);
46 SOCKET_ENUM(emission_sampling_method,
47 "Emission Sampling Method",
48 emission_sampling_method_enum,
50
51 SOCKET_BOOLEAN(use_transparent_shadow, "Use Transparent Shadow", true);
52 SOCKET_BOOLEAN(use_bump_map_correction, "Bump Map Correction", true);
53
54 static NodeEnum volume_sampling_method_enum;
55 volume_sampling_method_enum.insert("distance", VOLUME_SAMPLING_DISTANCE);
56 volume_sampling_method_enum.insert("equiangular", VOLUME_SAMPLING_EQUIANGULAR);
57 volume_sampling_method_enum.insert("multiple_importance", VOLUME_SAMPLING_MULTIPLE_IMPORTANCE);
58 SOCKET_ENUM(volume_sampling_method,
59 "Volume Sampling Method",
60 volume_sampling_method_enum,
62
63 static NodeEnum volume_interpolation_method_enum;
64 volume_interpolation_method_enum.insert("linear", VOLUME_INTERPOLATION_LINEAR);
65 volume_interpolation_method_enum.insert("cubic", VOLUME_INTERPOLATION_CUBIC);
66 SOCKET_ENUM(volume_interpolation_method,
67 "Volume Interpolation Method",
68 volume_interpolation_method_enum,
70
71 SOCKET_FLOAT(volume_step_rate, "Volume Step Rate", 1.0f);
72
73 static NodeEnum displacement_method_enum;
74 displacement_method_enum.insert("bump", DISPLACE_BUMP);
75 displacement_method_enum.insert("true", DISPLACE_TRUE);
76 displacement_method_enum.insert("both", DISPLACE_BOTH);
77 SOCKET_ENUM(displacement_method, "Displacement Method", displacement_method_enum, DISPLACE_BUMP);
78
79 SOCKET_INT(pass_id, "Pass ID", 0);
80
81 return type;
82}
83
84Shader::Shader() : Node(get_node_type())
85{
86 pass_id = 0;
87
88 graph = nullptr;
89
90 has_surface = false;
93 has_surface_bssrdf = false;
94 has_volume = false;
95 has_displacement = false;
98 has_bssrdf_bump = false;
102 has_volume_connected = false;
105 has_light_path_node = false;
106 has_aov_output_node = false;
107 has_time_dependency = false;
108 has_dispersion = false;
109
113
114 displacement_method = DISPLACE_BUMP;
115
116 id = -1;
117
118 need_update_uvs = true;
123}
124
126{
127 /* Only supports a few nodes for now, not arbitrary shader graphs. */
128 ShaderNode *node = (output) ? output->parent : nullptr;
129
130 if (node == nullptr) {
131 return zero_float3();
132 }
133 if (node->type == EmissionNode::get_node_type() ||
134 node->type == BackgroundNode::get_node_type() ||
135 node->type == PrincipledBsdfNode::get_node_type())
136 {
137 const bool is_principled = (node->type == PrincipledBsdfNode::get_node_type());
138 /* Emission and Background node. */
139 ShaderInput *color_in = node->input(is_principled ? "Emission Color" : "Color");
140 ShaderInput *strength_in = node->input(is_principled ? "Emission Strength" : "Strength");
141
142 if (is_principled) {
143 /* Too many parameters (coat, sheen, alpha) influence Emission for the Principled BSDF. */
144 is_constant = false;
145 }
146
147 float3 estimate = one_float3();
148
149 if (color_in->link) {
150 is_constant = false;
151 }
152 else {
153 estimate *= node->get_float3(color_in->socket_type);
154 }
155
156 if (strength_in->link) {
157 is_constant = false;
158 estimate *= output_estimate_emission(strength_in->link, is_constant);
159 }
160 else {
161 estimate *= node->get_float(strength_in->socket_type);
162 }
163
164 return estimate;
165 }
166 if (node->type == LightFalloffNode::get_node_type() ||
167 node->type == IESLightNode::get_node_type())
168 {
169 /* Get strength from Light Falloff and IES texture node. */
170 ShaderInput *strength_in = node->input("Strength");
171 is_constant = false;
172
173 return (strength_in->link) ? output_estimate_emission(strength_in->link, is_constant) :
174 make_float3(node->get_float(strength_in->socket_type));
175 }
176 if (node->type == AddClosureNode::get_node_type()) {
177 /* Add Closure. */
178 ShaderInput *closure1_in = node->input("Closure1");
179 ShaderInput *closure2_in = node->input("Closure2");
180
181 const float3 estimate1 = (closure1_in->link) ?
182 output_estimate_emission(closure1_in->link, is_constant) :
183 zero_float3();
184 const float3 estimate2 = (closure2_in->link) ?
185 output_estimate_emission(closure2_in->link, is_constant) :
186 zero_float3();
187
188 return estimate1 + estimate2;
189 }
190 if (node->type == MixClosureNode::get_node_type()) {
191 /* Mix Closure. */
192 ShaderInput *fac_in = node->input("Fac");
193 ShaderInput *closure1_in = node->input("Closure1");
194 ShaderInput *closure2_in = node->input("Closure2");
195
196 const float3 estimate1 = (closure1_in->link) ?
197 output_estimate_emission(closure1_in->link, is_constant) :
198 zero_float3();
199 const float3 estimate2 = (closure2_in->link) ?
200 output_estimate_emission(closure2_in->link, is_constant) :
201 zero_float3();
202
203 if (fac_in->link) {
204 is_constant = false;
205 return estimate1 + estimate2;
206 }
207
208 const float fac = node->get_float(fac_in->socket_type);
209 return (1.0f - fac) * estimate1 + fac * estimate2;
210 }
211
212 /* Other nodes, potentially OSL nodes with arbitrary code for which all we can
213 * determine is if it has emission or not. */
214 const bool has_emission = node->has_surface_emission();
215 float3 estimate;
216
217 if (output->type() == SocketType::CLOSURE) {
218 if (has_emission) {
219 estimate = one_float3();
220 is_constant = false;
221 }
222 else {
223 estimate = zero_float3();
224 }
225
226 for (const ShaderInput *in : node->inputs) {
227 if (in->type() == SocketType::CLOSURE && in->link) {
228 estimate += output_estimate_emission(in->link, is_constant);
229 }
230 }
231 }
232 else {
233 estimate = one_float3();
234 is_constant = false;
235 }
236
237 return estimate;
238}
239
241{
242 /* If the shader has AOVs, they need to be evaluated, so we can't skip the shader. */
244
245 for (ShaderNode *node : graph->nodes) {
246 if (node->special_type == SHADER_SPECIAL_TYPE_OUTPUT_AOV) {
247 emission_is_constant = false;
248 break;
249 }
250 }
251
252 ShaderInput *surf = graph->output()->input("Surface");
254
257 }
258 else if (emission_sampling_method == EMISSION_SAMPLING_AUTO) {
259 /* Automatically disable MIS when emission is low, to avoid weakly emitting
260 * using a lot of memory in the light tree and potentially wasting samples
261 * where indirect light samples are sufficient.
262 * Possible optimization: estimate front and back emission separately. */
263
264 /* Lower importance of emission nodes from automatic value/color to shader conversion, as these
265 * are likely used for previewing and can be slow to build a light tree for on dense meshes. */
266 float scale = 1.0f;
267 const ShaderOutput *output = surf->link;
268 if (output && output->parent->type == EmissionNode::get_node_type()) {
269 const EmissionNode *emission_node = static_cast<const EmissionNode *>(output->parent);
270 if (emission_node->from_auto_conversion) {
271 scale = 0.1f;
272 }
273 }
274
278 }
279 else {
280 emission_sampling = emission_sampling_method;
281 }
282}
283
285{
286 /* do this here already so that we can detect if mesh or object attributes
287 * are needed, since the node attribute callbacks check if their sockets
288 * are connected but proxy nodes should not count */
289 if (graph_) {
290 graph_->remove_proxy_nodes();
291
292 if (displacement_method != DISPLACE_BUMP) {
293 graph_->compute_displacement_hash();
294 }
295 }
296
297 /* update geometry if displacement changed */
298 if (displacement_method != DISPLACE_BUMP) {
299 const char *old_hash = (graph) ? graph->displacement_hash.c_str() : "";
300 const char *new_hash = (graph_) ? graph_->displacement_hash.c_str() : "";
301
302 if (strcmp(old_hash, new_hash) != 0) {
304 }
305 }
306
307 /* assign graph */
308 graph = std::move(graph_);
309
310 /* Store info here before graph optimization to make sure that
311 * nodes that get optimized away still count. */
312 has_volume_connected = (graph->output()->input("Volume")->link != nullptr);
313}
314
316{
317 if (!use_transparent_shadow) {
318 return false;
319 }
320
321 for (ShaderNode *node : graph->nodes) {
322 if (node->has_surface_transparent()) {
323 return true;
324 }
325 }
326
327 return false;
328}
329
331{
332 /* update tag */
333 tag_modified();
334
335 scene->shader_manager->tag_update(scene, ShaderManager::SHADER_MODIFIED);
336
337 /* if the shader previously was emissive, update light distribution,
338 * if the new shader is emissive, a light manager update tag will be
339 * done in the shader manager device update. */
341 scene->light_manager->tag_update(scene, LightManager::SHADER_MODIFIED);
342 }
343
344 /* Special handle of background MIS light for now: for some reason it
345 * has use_mis set to false. We are quite close to release now, so
346 * better to be safe.
347 */
348 if (this == scene->background->get_shader(scene)) {
349 scene->light_manager->need_update_background = true;
350 if (scene->light_manager->has_background_light(scene)) {
351 scene->light_manager->tag_update(scene, LightManager::SHADER_MODIFIED);
352 }
353 }
354
355 /* quick detection of which kind of shaders we have to avoid loading
356 * e.g. surface attributes when there is only a volume shader. this could
357 * be more fine grained but it's better than nothing */
358 OutputNode *output = graph->output();
359 const bool prev_has_volume = has_volume;
360 has_surface = has_surface || output->input("Surface")->link;
361 has_volume = has_volume || output->input("Volume")->link;
362 has_displacement = has_displacement || output->input("Displacement")->link;
363
364 if (!has_surface && !has_volume) {
365 /* If we need to output surface AOVs, add a Transparent BSDF so that the
366 * surface shader runs. */
367 for (ShaderNode *node : graph->nodes) {
368 if (node->special_type == SHADER_SPECIAL_TYPE_OUTPUT_AOV) {
369 for (const ShaderInput *in : node->inputs) {
370 if (in->link) {
371 TransparentBsdfNode *transparent = graph->create_node<TransparentBsdfNode>();
372 graph->connect(transparent->output("BSDF"), output->input("Surface"));
373 has_surface = true;
374 break;
375 }
376 }
377 if (has_surface) {
378 break;
379 }
380 }
381 }
382 }
383
384 /* get requested attributes. this could be optimized by pruning unused
385 * nodes here already, but that's the job of the shader manager currently,
386 * and may not be so great for interactive rendering where you temporarily
387 * disconnect a node */
388
389 const AttributeRequestSet prev_attributes = attributes;
390 const AttributeRequestSet prev_global_attributes = global_attributes;
391
394 for (ShaderNode *node : graph->nodes) {
395 node->attributes(this, &attributes);
396 node->global_attributes(this, &global_attributes);
397 }
398
399 if (has_displacement) {
400 if (displacement_method == DISPLACE_BOTH) {
403 }
404 if (displacement_method_is_modified()) {
407 scene->object_manager->need_flags_update = true;
408 }
409 }
410
411 /* compare if the attributes changed, mesh manager will check
412 * need_update_attribute, update the relevant meshes and clear it. */
413 if (attributes.modified(prev_attributes) || global_attributes.modified(prev_global_attributes)) {
416 scene->procedural_manager->tag_update();
417 }
418
422 }
423
425
426 if (has_volume != prev_has_volume || volume_step_rate != prev_volume_step_rate) {
427 scene->geometry_manager->need_flags_update = true;
428 scene->object_manager->need_flags_update = true;
429 scene->volume_manager->need_update_step_size = true;
430 prev_volume_step_rate = volume_step_rate;
431 }
432
433 if (has_volume || prev_has_volume) {
434 scene->volume_manager->tag_update(this);
435 }
436}
437
439{
440 /* if an unused shader suddenly gets used somewhere, it needs to be
441 * recompiled because it was skipped for compilation before */
442 if (!reference_count()) {
443 tag_modified();
444 /* We do not reference here as the shader will be referenced when added to a socket. */
445 scene->shader_manager->tag_update(scene, ShaderManager::SHADER_MODIFIED);
446 }
447}
448
453
454/* Shader Manager */
455
462
464
466{
468
469 (void)shadingsystem; /* Ignored when built without OSL. */
470
471#ifdef WITH_OSL
472 if (shadingsystem == SHADINGSYSTEM_OSL) {
473 manager = make_unique<OSLShaderManager>();
474 }
475 else
476#endif
477 {
478 manager = make_unique<SVMShaderManager>();
479 }
480
481 return manager;
482}
483
485{
487
488 /* get a unique id for each name, for SVM attribute lookup */
489 const AttributeIDMap::iterator it = unique_attribute_id.find(name);
490
491 if (it != unique_attribute_id.end()) {
492 return it->second;
493 }
494
495 const uint64_t id = ATTR_STD_NUM + unique_attribute_id.size();
496 unique_attribute_id[name] = id;
497 return id;
498}
499
504
506{
507 /* get a shader id to pass to the kernel */
508 int id = shader->id;
509
510 /* smooth flag */
511 if (smooth) {
513 }
514
515 /* default flags */
516 id |= SHADER_CAST_SHADOW;
517
518 return id;
519}
520
522 DeviceScene *dscene,
523 Scene *scene,
524 Progress & /*progress*/)
525{
526 /* This optimizes the shader graphs, but does not update anything on the device yet.
527 * After this we'll know the kernel features actually used, to load the kernels. */
528 if (!need_update()) {
529 return;
530 }
531
532 uint id = 0;
533 for (Shader *shader : scene->shaders) {
534 shader->id = id++;
535 }
536
537 /* Those shaders should always be compiled as they are used as a fallback if a shader cannot be
538 * found, e.g. bad shader index for the triangle shaders on a Mesh. */
539 assert(scene->default_surface->reference_count() != 0);
540 assert(scene->default_light->reference_count() != 0);
542 assert(scene->default_empty->reference_count() != 0);
543 assert(scene->default_gsplat->reference_count() != 0);
544
545 /* Preprocess shader graph. */
546 bool has_volumes = false;
547
548 for (Shader *shader : scene->shaders) {
549 if (shader->is_modified()) {
550 ShaderNode *output = shader->graph->output();
551 shader->has_bump_from_displacement = (shader->get_displacement_method() != DISPLACE_TRUE) &&
552 output->input("Surface")->link &&
553 output->input("Displacement")->link;
555
556 shader->graph->finalize(scene,
558 shader->get_displacement_method() == DISPLACE_BOTH);
559
560 shader->has_surface = output->input("Surface")->link != nullptr;
561 shader->has_surface_transparent = false;
562 shader->has_surface_raytrace = false;
563 shader->has_surface_bssrdf = false;
564 shader->has_surface_spatial_varying = false;
565 shader->has_volume = output->input("Volume")->link != nullptr;
566 shader->has_volume_spatial_varying = false;
567 shader->has_volume_attribute_dependency = false;
568 shader->has_displacement = output->input("Displacement")->link != nullptr;
569 shader->has_bump_from_surface = false;
570 shader->has_dispersion = false;
571
572 /* Determine both properties. */
573 shader->has_light_path_node = false;
574 shader->has_aov_output_node = false;
575 shader->has_time_dependency = false;
576 for (ShaderNode *node : shader->graph->nodes) {
577 if (node->special_type == SHADER_SPECIAL_TYPE_LIGHT_PATH) {
578 /* TODO: check if the light path node is linked to the volume output. */
579 shader->has_light_path_node = true;
580 }
581 else if (node->special_type == SHADER_SPECIAL_TYPE_OUTPUT_AOV) {
582 shader->has_aov_output_node = true;
583 }
584 else if (node->special_type == SHADER_SPECIAL_TYPE_SCENE_TIME) {
585 shader->has_time_dependency = true;
586 }
587 }
588 }
589
590 if (shader->reference_count()) {
591 has_volumes |= shader->has_volume;
592 }
593 }
594
595 /* Set this early as it is needed by volume rendering passes. */
596 KernelIntegrator *kintegrator = &dscene->data.integrator;
597 if (bool(kintegrator->use_volumes) != has_volumes) {
599 kintegrator->use_volumes = has_volumes;
600 }
601}
602
604 DeviceScene *dscene,
605 Scene *scene,
606 Progress &progress)
607{
608 device_update_specific(device, dscene, scene, progress);
609
610 /* This runs after kernels have been loaded, so can copy to device. */
613}
614
616 DeviceScene *dscene,
617 Scene *scene,
618 Progress & /*progress*/)
619{
620 dscene->shaders.free();
621
622 if (scene->shaders.empty()) {
623 return;
624 }
625
626 KernelShader *kshader = dscene->shaders.alloc(scene->shaders.size());
627 bool has_transparent_shadow = false;
628
629 for (Shader *shader : scene->shaders) {
630 uint flag = 0;
631
633 flag |= SD_MIS_FRONT;
634 }
635 else if (shader->emission_sampling == EMISSION_SAMPLING_BACK) {
636 flag |= SD_MIS_BACK;
637 }
639 flag |= SD_MIS_FRONT | SD_MIS_BACK;
640 }
641
642 if (!is_zero(shader->emission_estimate)) {
643 flag |= SD_HAS_EMISSION;
644 }
645 if (shader->has_surface_transparent && shader->get_use_transparent_shadow()) {
647 }
648 if (shader->has_surface_raytrace) {
649 flag |= SD_HAS_RAYTRACE;
650 }
651 if (shader->has_volume) {
652 flag |= SD_HAS_VOLUME;
653 /* todo: this could check more fine grained, to skip useless volumes
654 * enclosed inside an opaque bsdf.
655 */
657 }
658 /* in this case we can assume transparent surface */
659 if (shader->has_volume_connected && !shader->has_surface) {
660 flag |= SD_HAS_ONLY_VOLUME;
661 }
662 if (shader->has_volume && shader->has_volume_spatial_varying) {
664 }
667 }
668 if (shader->has_bssrdf_bump) {
669 flag |= SD_HAS_BSSRDF_BUMP;
670 }
671 if (shader->get_volume_sampling_method() == VOLUME_SAMPLING_EQUIANGULAR) {
672 flag |= SD_VOLUME_EQUIANGULAR;
673 }
674 if (shader->get_volume_sampling_method() == VOLUME_SAMPLING_MULTIPLE_IMPORTANCE) {
675 flag |= SD_VOLUME_MIS;
676 }
677 if (shader->get_volume_interpolation_method() == VOLUME_INTERPOLATION_CUBIC) {
678 flag |= SD_VOLUME_CUBIC;
679 }
680 if (shader->has_bump_from_displacement) {
682 }
683 if (shader->has_bump_from_surface) {
685 }
686 if (shader->get_displacement_method() != DISPLACE_BUMP) {
687 flag |= SD_HAS_DISPLACEMENT;
688 }
689 if (shader->get_use_bump_map_correction()) {
691 }
692
693 /* constant emission check */
694 if (shader->emission_is_constant) {
696 }
697
698 if (shader->has_light_path_node) {
700 }
701 if (shader->has_dispersion) {
703 }
704
705 const uint32_t cryptomatte_id = util_murmur_hash3(
706 shader->name.c_str(), shader->name.length(), 0);
707
708 /* regular shader */
709 kshader->flags = flag;
710 kshader->pass_id = shader->get_pass_id();
711 kshader->constant_emission[0] = shader->emission_estimate.x;
712 kshader->constant_emission[1] = shader->emission_estimate.y;
713 kshader->constant_emission[2] = shader->emission_estimate.z;
714 kshader->cryptomatte_id = util_hash_to_float(cryptomatte_id);
715 kshader++;
716
717 has_transparent_shadow |= (flag & SD_HAS_TRANSPARENT_SHADOW) != 0;
718 }
719
720 /* lookup tables */
721 KernelTables *ktables = &dscene->data.tables;
722 ktables->ggx_E = ensure_bsdf_table(dscene, scene, table_ggx_E);
723 ktables->ggx_Eavg = ensure_bsdf_table(dscene, scene, table_ggx_Eavg);
724 ktables->ggx_glass_E = ensure_bsdf_table(dscene, scene, table_ggx_glass_E);
725 ktables->ggx_glass_Eavg = ensure_bsdf_table(dscene, scene, table_ggx_glass_Eavg);
726 ktables->ggx_glass_inv_E = ensure_bsdf_table(dscene, scene, table_ggx_glass_inv_E);
727 ktables->ggx_glass_inv_Eavg = ensure_bsdf_table(dscene, scene, table_ggx_glass_inv_Eavg);
728 ktables->sheen_ltc = ensure_bsdf_table(dscene, scene, table_sheen_ltc);
729 ktables->ggx_gen_schlick_ior_s = ensure_bsdf_table(dscene, scene, table_ggx_gen_schlick_ior_s);
730 ktables->ggx_gen_schlick_s = ensure_bsdf_table(dscene, scene, table_ggx_gen_schlick_s);
731
733 thin_film_table_offset_ = scene->lookup_tables->add_table(dscene, thin_film_table);
734 }
735 dscene->data.tables.thin_film_table = (int)thin_film_table_offset_;
736
737 /* integrator */
738 KernelIntegrator *kintegrator = &dscene->data.integrator;
739 /* TODO(sergey): De-duplicate with flags set in integrator.cpp. */
740 kintegrator->transparent_shadows = has_transparent_shadow;
741
742 /* film */
743 KernelFilm *kfilm = &dscene->data.film;
744 /* color space, needs to be here because e.g. displacement shaders could depend on it */
745 kfilm->xyz_to_r = make_float4(xyz_to_r);
746 kfilm->xyz_to_g = make_float4(xyz_to_g);
747 kfilm->xyz_to_b = make_float4(xyz_to_b);
748 kfilm->rgb_to_y = make_float4(rgb_to_y);
749 kfilm->white_xyz = make_float4(white_xyz);
750 kfilm->rec709_to_r = make_float4(rec709_to_r);
751 kfilm->rec709_to_g = make_float4(rec709_to_g);
752 kfilm->rec709_to_b = make_float4(rec709_to_b);
753 kfilm->is_rec709 = scene_linear_interop_id == "lin_rec709_scene";
754}
755
757{
758 for (auto &entry : bsdf_tables) {
759 scene->lookup_tables->remove_table(&entry.second);
760 }
761 bsdf_tables.clear();
762 scene->lookup_tables->remove_table(&thin_film_table_offset_);
764
765 dscene->shaders.free();
766}
767
769{
770 /* default surface */
771 {
772 unique_ptr<ShaderGraph> graph = make_unique<ShaderGraph>();
773
774 PrincipledBsdfNode *bsdf = graph->create_node<PrincipledBsdfNode>();
775 graph->connect(bsdf->output("BSDF"), graph->output()->input("Surface"));
776
777 Shader *shader = scene->create_node<Shader>();
778 shader->name = "default_surface";
779 shader->set_graph(std::move(graph));
780 shader->reference();
781 scene->default_surface = shader;
782 shader->tag_update(scene);
783 }
784
785 /* default volume */
786 {
787 unique_ptr<ShaderGraph> graph = make_unique<ShaderGraph>();
788
789 PrincipledVolumeNode *principled = graph->create_node<PrincipledVolumeNode>();
790
791 graph->connect(principled->output("Volume"), graph->output()->input("Volume"));
792
793 Shader *shader = scene->create_node<Shader>();
794 shader->name = "default_volume";
795 shader->set_graph(std::move(graph));
796 scene->default_volume = shader;
797 shader->tag_update(scene);
798 /* No default reference for the volume to avoid compiling volume kernels if there are no
799 * actual volumes in the scene */
800 }
801
802 /* default light */
803 {
804 unique_ptr<ShaderGraph> graph = make_unique<ShaderGraph>();
805
806 EmissionNode *emission = graph->create_node<EmissionNode>();
807 emission->set_color(make_float3(0.8f, 0.8f, 0.8f));
808 emission->set_strength(0.0f);
809
810 graph->connect(emission->output("Emission"), graph->output()->input("Surface"));
811
812 Shader *shader = scene->create_node<Shader>();
813 shader->name = "default_light";
814 shader->set_graph(std::move(graph));
815 shader->reference();
816 scene->default_light = shader;
817 shader->tag_update(scene);
818 }
819
820 /* default background */
821 {
822 unique_ptr<ShaderGraph> graph = make_unique<ShaderGraph>();
823
824 Shader *shader = scene->create_node<Shader>();
825 shader->name = "default_background";
826 shader->set_graph(std::move(graph));
827 shader->reference();
828 scene->default_background = shader;
829 shader->tag_update(scene);
830 }
831
832 /* default empty */
833 {
834 unique_ptr<ShaderGraph> graph = make_unique<ShaderGraph>();
835
836 Shader *shader = scene->create_node<Shader>();
837 shader->name = "default_empty";
838 shader->set_graph(std::move(graph));
839 shader->reference();
840 scene->default_empty = shader;
841 shader->tag_update(scene);
842 }
843
844 /* Default Gaussian splat. */
845 {
846 unique_ptr<ShaderGraph> graph = make_unique<ShaderGraph>();
847
848 AttributeNode *attribute = graph->create_node<AttributeNode>();
849 attribute->set_attribute(ustring("radiance"));
850 EmissionNode *emission = graph->create_node<EmissionNode>();
851 emission->set_strength(1.0f);
852 graph->connect(attribute->output("Color"), emission->input("Color"));
853 graph->connect(emission->output("Emission"), graph->output()->input("Surface"));
854
855 Shader *shader = scene->create_node<Shader>();
856 shader->name = "default_gsplat";
857 shader->set_graph(std::move(graph));
858 shader->reference();
859 scene->default_gsplat = shader;
860 shader->tag_update(scene);
861 }
862}
863
865{
866 uint64_t kernel_features = 0;
867
868 for (ShaderNode *node : graph->nodes) {
869 kernel_features |= node->get_feature();
870 if (node->special_type == SHADER_SPECIAL_TYPE_CLOSURE) {
871 BsdfBaseNode *bsdf_node = static_cast<BsdfBaseNode *>(node);
872 if (CLOSURE_IS_VOLUME(bsdf_node->get_closure_type())) {
873 kernel_features |= KERNEL_FEATURE_NODE_VOLUME;
874 }
875 }
876 if (node->has_surface_bssrdf()) {
877 kernel_features |= KERNEL_FEATURE_SUBSURFACE;
878 }
879 if (node->has_surface_transparent()) {
880 kernel_features |= KERNEL_FEATURE_TRANSPARENT;
881 }
882 }
883
884 return kernel_features;
885}
886
888{
890 for (int i = 0; i < scene->shaders.size(); i++) {
891 Shader *shader = scene->shaders[i];
892 if (!shader->reference_count()) {
893 continue;
894 }
895
896 /* Gather requested features from all the nodes from the graph nodes. */
897 kernel_features |= get_graph_kernel_features(shader->graph.get());
898 ShaderNode *output_node = shader->graph->output();
899 if (output_node->input("Displacement")->link != nullptr) {
900 kernel_features |= KERNEL_FEATURE_NODE_BUMP;
901 if (shader->get_displacement_method() == DISPLACE_BOTH) {
902 kernel_features |= KERNEL_FEATURE_NODE_BUMP_STATE;
903 }
904 }
905 /* On top of volume nodes, also check if we need volume sampling because
906 * e.g. an Emission node would slip through the KERNEL_FEATURE_NODE_VOLUME check */
907 if (shader->has_volume_connected) {
908 kernel_features |= KERNEL_FEATURE_VOLUME;
909 }
910 }
911
912 if (use_osl()) {
913 kernel_features |= KERNEL_FEATURE_OSL_SHADING;
914 }
915
916 return kernel_features;
917}
918
920{
921 return dot(c, rgb_to_y);
922}
923
928
930{
931 string manifest = "{";
932 unordered_set<ustring> materials;
933 for (Shader *shader : scene->shaders) {
934 if (materials.contains(shader->name)) {
935 continue;
936 }
937 materials.insert(shader->name);
938 const uint32_t cryptomatte_id = util_murmur_hash3(
939 shader->name.c_str(), shader->name.length(), 0);
940 manifest += string_printf("\"%s\":\"%08x\",", shader->name.c_str(), cryptomatte_id);
941 }
942 manifest[manifest.size() - 1] = '}';
943 return manifest;
944}
945
946void ShaderManager::tag_update(Scene * /*scene*/, uint32_t /*flag*/)
947{
948 /* update everything for now */
950}
951
953{
954 return update_flags != UPDATE_NONE;
955}
956
958{
959 /* Our implementation of Thin Film Fresnel is based on
960 * "A Practical Extension to Microfacet Theory for the Modeling of Varying Iridescence"
961 * by Laurent Belcour and Pascal Barla
962 * (https://belcour.github.io/blog/research/publication/2017/05/01/brdf-thin-film.html).
963 *
964 * The idea there is that for a naive implementation of Thin Film interference, you'd compute
965 * the reflectivity for a given wavelength using Airy summation, and then numerically integrate
966 * the product of this reflectivity function and the Color Matching Functions of the colorspace
967 * you're working in to obtain the RGB (or XYZ) values.
968 * However, this integration would require too many evaluations to be practical.
969 * Therefore, they reformulate the computation as a rapidly converging series involving the
970 * Fourier transform of the CMFs.
971 *
972 * Specifically, we need to:
973 * - Compute the RGB CMFs from the XYZ CMFs using the working color space's XYZ-to-RGB matrix
974 * - Resample the RGB CMFs to be parametrized by frequency instead of wavelength as usual
975 * - Compute the FFT of the CMFs
976 * - Store the result as a LUT
977 * - Look up the values for each channel at runtime based on the optical path difference and
978 * phase shift.
979 *
980 * Computing an FFT here would be annoying, so we'd like to precompute it, but we only know
981 * the XYZ-to-RGB matrix at runtime. Luckily, both resampling and FFT are linear operations,
982 * so we can precompute the FFT of the resampled XYZ CMFs and then multiply each entry with
983 * the XYZ-to-RGB matrix to get the RGB LUT.
984 *
985 * That's what this function does: We load the precomputed values, convert to RGB, normalize
986 * the result to make the DC term equal to 1, and then store that into the final table that's
987 * used by the kernel.
988 */
989 assert(sizeof(table_thin_film_cmf) == 6 * THIN_FILM_TABLE_SIZE * sizeof(float));
991
992 float3 normalization;
993 for (int i = 0; i < THIN_FILM_TABLE_SIZE; i++) {
994 const float *table_row = table_thin_film_cmf[i];
995 /* Load precomputed resampled Fourier-transformed XYZ CMFs. */
996 const float3 xyzReal = make_float3(table_row[0], table_row[1], table_row[2]);
997 const float3 xyzImag = make_float3(table_row[3], table_row[4], table_row[5]);
998
999 /* Linearly combine precomputed data to produce the RGB equivalents. Works since both
1000 * resampling and Fourier transformation are linear operations. */
1001 const float3 rgbReal = transform_direction(&xyz_to_rgb, xyzReal);
1002 const float3 rgbImag = transform_direction(&xyz_to_rgb, xyzImag);
1003
1004 /* We normalize all entries by the first element. Since that is the DC component, it normalizes
1005 * the CMF (in non-Fourier space) to an area of 1. */
1006 if (i == 0) {
1007 normalization = 1.0f / rgbReal;
1008 }
1009
1010 /* Store in lookup table. */
1011 thin_film_table[i + 0 * THIN_FILM_TABLE_SIZE] = rgbReal.x * normalization.x;
1012 thin_film_table[i + 1 * THIN_FILM_TABLE_SIZE] = rgbReal.y * normalization.y;
1013 thin_film_table[i + 2 * THIN_FILM_TABLE_SIZE] = rgbReal.z * normalization.z;
1014 thin_film_table[i + 3 * THIN_FILM_TABLE_SIZE] = rgbImag.x * normalization.x;
1015 thin_film_table[i + 4 * THIN_FILM_TABLE_SIZE] = rgbImag.y * normalization.y;
1016 thin_film_table[i + 5 * THIN_FILM_TABLE_SIZE] = rgbImag.z * normalization.z;
1017 }
1018}
1019
1021{
1023
1025
1026 if (scene_linear_interop_id == "lin_rec709_scene") {
1027 rec709_to_r = make_float3(1.0f, 0.0f, 0.0f);
1028 rec709_to_g = make_float3(0.0f, 1.0f, 0.0f);
1029 rec709_to_b = make_float3(0.0f, 0.0f, 1.0f);
1030 }
1031 else {
1032 const Transform xyz_to_rec709 = ColorSpaceManager::get_xyz_to_rec709();
1033 const Transform rec709_to_rgb = xyz_to_rgb * transform_inverse(xyz_to_rec709);
1037 }
1038
1042
1043 const Transform rgb_to_xyz = transform_inverse(xyz_to_rgb);
1044 rgb_to_y = make_float3(rgb_to_xyz.y);
1045 white_xyz = transform_direction(&rgb_to_xyz, one_float3());
1046
1048}
1049
1051 Scene *scene,
1052 const float *table,
1053 const size_t n)
1054{
1055 /* Since the BSDF tables are static arrays, we can use their address to identify them. */
1056 if (!(bsdf_tables.count(table))) {
1057 vector<float> entries(table, table + n);
1058 bsdf_tables[table] = scene->lookup_tables->add_table(dscene, entries);
1059 }
1060 return bsdf_tables[table];
1061}
1062
volatile int lock
unsigned long long int uint64_t
#define output
void add(ustring name)
bool modified(const AttributeRequestSet &other) const
Shader * get_shader(const Scene *scene)
ClosureType get_closure_type() override
static Transform get_xyz_to_scene_linear_rgb()
static Transform get_xyz_to_rec709()
static const char * get_scene_linear_interop_id(const bool srgb_encoded=false)
device_vector< KernelShader > shaders
Definition devicescene.h:80
device_vector< int > svm_nodes
Definition devicescene.h:79
KernelData data
bool from_auto_conversion
Definition scene.h:133
unique_ptr< ObjectManager > object_manager
Definition scene.h:171
unique_ptr< LightManager > light_manager
Definition scene.h:167
Shader * default_volume
Definition scene.h:179
Shader * default_surface
Definition scene.h:178
Shader * default_empty
Definition scene.h:182
Shader * default_background
Definition scene.h:181
Shader * default_gsplat
Definition scene.h:183
T * create_node(Args &&...)=delete
Background * background
Definition scene.h:148
void tag_has_volume_modified()
Definition scene.cpp:854
unique_ptr_vector< Shader > shaders
Definition scene.h:158
unique_ptr< ShaderManager > shader_manager
Definition scene.h:169
unique_ptr< GeometryManager > geometry_manager
Definition scene.h:170
unique_ptr< ProceduralManager > procedural_manager
Definition scene.h:174
Shader * default_light
Definition scene.h:180
unique_ptr< VolumeManager > volume_manager
Definition scene.h:175
unique_ptr< LookupTables > lookup_tables
Definition scene.h:143
unique_ptr_vector< ShaderNode > nodes
const SocketType & socket_type
ShaderOutput * link
string get_cryptomatte_materials(Scene *scene)
size_t thin_film_table_offset_
float3 rec709_to_scene_linear(const float3 c)
static unique_ptr< ShaderManager > create(const int shadingsystem)
void device_update_common(Device *device, DeviceScene *dscene, Scene *scene, Progress &progress)
size_t ensure_bsdf_table_impl(DeviceScene *dscene, Scene *scene, const float *table, const size_t n)
void device_update_pre(Device *device, DeviceScene *dscene, Scene *scene, Progress &progress)
AttributeIDMap unique_attribute_id
float linear_rgb_to_gray(const float3 c)
virtual uint64_t get_attribute_id(ustring name)
void init_xyz_transforms()
virtual void device_update_specific(Device *device, DeviceScene *dscene, Scene *scene, Progress &progress)=0
thread_spin_lock attribute_lock_
virtual ~ShaderManager()
void compute_thin_film_table(const Transform &xyz_to_rgb)
unordered_map< const float *, size_t > bsdf_tables
void device_update_post(Device *device, DeviceScene *dscene, Scene *scene, Progress &progress)
int get_shader_id(const Shader *shader, bool smooth=false)
bool need_update() const
void tag_update(Scene *scene, const uint32_t flag)
void device_free_common(Device *device, DeviceScene *dscene, Scene *scene)
string scene_linear_interop_id
static thread_mutex lookup_table_mutex
uint64_t get_kernel_features(Scene *scene)
uint64_t get_graph_kernel_features(ShaderGraph *graph)
size_t ensure_bsdf_table(DeviceScene *dscene, Scene *scene, const float(&table)[n])
static void add_default(Scene *scene)
uint32_t update_flags
virtual bool use_osl()
vector< float > thin_film_table
ShaderInput * input(const char *name)
ShaderOutput * output(const char *name)
bool has_dispersion
AttributeRequestSet global_attributes
bool has_surface_spatial_varying
bool has_bump_from_surface
bool has_surface_bssrdf
void estimate_emission()
bool need_update_attribute
bool has_volume_attribute_dependency
bool need_update_geometry() const
void set_graph(unique_ptr< ShaderGraph > &&graph)
bool has_volume
bool emission_is_constant
float3 emission_estimate
bool has_surface
bool has_light_path_node
bool has_time_dependency
bool has_bssrdf_bump
float prev_volume_step_rate
bool has_bump_from_displacement
bool need_update_displacement
EmissionSampling emission_sampling
bool shadow_transparency_needs_realloc
bool has_surface_raytrace
bool need_update_uvs
bool has_displacement
AttributeRequestSet attributes
bool has_surface_transparent
void tag_update(Scene *scene)
NODE_DECLARE unique_ptr< ShaderGraph > graph
bool need_update_shadow_transparency
bool prev_has_surface_shadow_transparency
bool has_aov_output_node
bool has_surface_shadow_transparency() const
bool has_volume_connected
void tag_used(Scene *scene)
bool has_volume_spatial_varying
T * alloc(const size_t width, const size_t height=0)
size_t size() const
#define KERNEL_FEATURE_VOLUME
#define CLOSURE_IS_VOLUME(type)
#define KERNEL_FEATURE_NODE_BUMP_STATE
#define KERNEL_FEATURE_TRANSPARENT
#define KERNEL_FEATURE_OSL_SHADING
#define KERNEL_FEATURE_SUBSURFACE
#define KERNEL_FEATURE_NODE_BSDF
#define THIN_FILM_TABLE_SIZE
#define KERNEL_FEATURE_NODE_VOLUME
#define KERNEL_FEATURE_NODE_EMISSION
#define KERNEL_FEATURE_NODE_BUMP
#define CCL_NAMESPACE_END
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
#define assert(assertion)
#define smooth
#define attribute(slot)
float dot(VecOp< float, D >, VecOp< float, D >) RET
float3x3 scale(float3x3 mat, float2 scale)
bool is_zero(float3x3 a)
float3 transform_direction(float3x3 mat, float3 direction)
float reduce_max(float2 a)
@ SD_VOLUME_MIS
@ SD_MIS_BACK
@ SD_HAS_TRANSPARENT_SHADOW
@ SD_VOLUME_EQUIANGULAR
@ SD_HAS_CONSTANT_EMISSION
@ SD_HAS_DISPLACEMENT
@ SD_HAS_EMISSION
@ SD_MIS_FRONT
@ SD_HAS_BSSRDF_BUMP
@ SD_HAS_ONLY_VOLUME
@ SD_HAS_LIGHT_PATH_NODE
@ SD_HAS_RAYTRACE
@ SD_VOLUME_CUBIC
@ SD_HAS_VOLUME
@ SD_REQUIRES_WAVELENGTH
@ SD_NEED_VOLUME_ATTRIBUTES
@ SD_USE_BUMP_MAP_CORRECTION
@ SD_HAS_BUMP_FROM_DISPLACEMENT
@ SD_HETEROGENEOUS_VOLUME
@ SD_HAS_BUMP_FROM_SURFACE
@ EMISSION_SAMPLING_NONE
@ EMISSION_SAMPLING_AUTO
@ EMISSION_SAMPLING_BACK
@ EMISSION_SAMPLING_FRONT_BACK
@ EMISSION_SAMPLING_FRONT
@ SHADER_SMOOTH_NORMAL
@ SHADER_CAST_SHADOW
AttributeStandard
@ ATTR_STD_NUM
@ ATTR_STD_POSITION_UNDISPLACED
@ ATTR_STD_NORMAL_UNDISPLACED
ccl_device float3 rec709_to_rgb(KernelGlobals kg, const float3 rec709)
ccl_device_inline dual3 fabs(const ccl_private dual3 &a)
Definition math_dual.h:241
ccl_device_inline float3 one_float3()
Definition math_float3.h:26
CCL_NAMESPACE_BEGIN ccl_device_inline float3 zero_float3()
Definition math_float3.h:17
uint32_t util_murmur_hash3(const void *key, const int len, const uint32_t seed)
float util_hash_to_float(const uint32_t hash)
color xyz_to_rgb(float x, float y, float z)
Definition node_color.h:73
#define SOCKET_FLOAT(name, ui_name, default_value,...)
Definition node_type.h:219
#define SOCKET_INT(name, ui_name, default_value,...)
Definition node_type.h:213
#define NODE_DEFINE(structname)
Definition node_type.h:157
#define SOCKET_BOOLEAN(name, ui_name, default_value,...)
Definition node_type.h:211
#define SOCKET_ENUM(name, ui_name, values, default_value,...)
Definition node_type.h:235
#define make_float4
static float3 output_estimate_emission(ShaderOutput *output, bool &is_constant)
@ VOLUME_INTERPOLATION_LINEAR
@ VOLUME_INTERPOLATION_CUBIC
@ DISPLACE_BUMP
@ DISPLACE_TRUE
@ DISPLACE_BOTH
@ SHADINGSYSTEM_OSL
@ VOLUME_SAMPLING_DISTANCE
@ VOLUME_SAMPLING_EQUIANGULAR
@ VOLUME_SAMPLING_MULTIPLE_IMPORTANCE
@ TABLE_OFFSET_INVALID
@ SHADER_SPECIAL_TYPE_LIGHT_PATH
@ SHADER_SPECIAL_TYPE_OUTPUT_AOV
@ SHADER_SPECIAL_TYPE_SCENE_TIME
@ SHADER_SPECIAL_TYPE_CLOSURE
CCL_NAMESPACE_BEGIN string string_printf(const char *format,...)
Definition string.cpp:25
float constant_emission[3]
int ggx_gen_schlick_ior_s
void insert(const char *x, const int y)
Definition node_enum.h:21
static NodeType * add(const char *name, CreateFunc create, Type type=NONE, const NodeType *base=nullptr)
ustring name
Definition graph/node.h:178
int reference_count() const
Definition graph/node.h:184
void reference()
Definition graph/node.h:189
void tag_modified()
bool is_modified() const
Node(const NodeType *type, ustring name=ustring())
float4 y
Definition transform.h:53
float z
Definition sky_math.h:136
float y
Definition sky_math.h:136
float x
Definition sky_math.h:136
std::mutex thread_mutex
Definition thread.h:27
ccl_device_inline Transform transform_inverse(const Transform tfm)
Definition transform.h:563
ccl_device_inline float2 to_local(const T p, const T X, const T Y)
Definition transform.h:33