Blender V5.3
closure.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#pragma once
6
13
14#include "kernel/geom/curve.h"
15#include "kernel/geom/object.h"
17
20#include "kernel/svm/util.h"
21
23#include "util/defines.h"
24
26
27/* Closure Nodes */
28
29ccl_device_inline int svm_node_closure_bsdf_skip(int offset, const uint type)
30{
31 switch (type) {
33 offset += sizeof(SVMNodePrincipledBsdfData) / sizeof(uint);
34 break;
37 offset += sizeof(SVMNodePrincipledHairBsdfData) / sizeof(uint);
38 break;
41 offset += sizeof(SVMNodeMetallicBsdfData) / sizeof(uint);
42 break;
46 offset += sizeof(SVMNodeDiffuseBsdfData) / sizeof(uint);
47 break;
52 offset += sizeof(SVMNodeSimpleBsdfData) / sizeof(uint);
53 break;
55 offset += sizeof(SVMNodeRayPortalBsdfData) / sizeof(uint);
56 break;
61 offset += sizeof(SVMNodeGlossyBsdfData) / sizeof(uint);
62 break;
65 offset += sizeof(SVMNodeRefractionBsdfData) / sizeof(uint);
66 break;
70 offset += sizeof(SVMNodeGlassBsdfData) / sizeof(uint);
71 break;
74 offset += sizeof(SVMNodeToonBsdfData) / sizeof(uint);
75 break;
78 offset += sizeof(SVMNodeHairBsdfData) / sizeof(uint);
79 break;
84 offset += sizeof(SVMNodeBssrdfData) / sizeof(uint);
85 break;
86 default:
87 offset += sizeof(SVMNodeSimpleBsdfData) / sizeof(uint);
88 break;
89 }
90 return offset;
91}
92
93/* Compute emission attenuated by coat and sheen for Principled BSDF, and return the weight of the
94 * layers after emission. */
97 ccl_private ShaderData *sd,
98 ccl_private float *ccl_restrict stack,
100 const float3 N,
101 const PathRayVisibility ray_visibility,
102 const uint32_t path_flag,
103 const float mix_weight)
104{
105 /* We're ignoring closure_weight here since it's always 1 for the Principled BSDF, so there's no
106 * point in setting it. */
107 Spectrum weight = make_spectrum(mix_weight);
108
109 /* Before any actual shader components, apply transparency. */
110 const float alpha = saturatef(stack_load(stack, data.alpha));
111 if (alpha < 1.0f) {
112 bsdf_transparent_setup(sd, weight * (1.0f - alpha), path_flag);
113 weight *= alpha;
114 }
115
116 /* First layer: Sheen */
117 const float coat_weight = fmaxf(stack_load(stack, data.coat_weight), 0.0f);
118 const float sheen_weight = fmaxf(stack_load(stack, data.sheen_weight), 0.0f);
119
120 if (sheen_weight > CLOSURE_WEIGHT_CUTOFF) {
121 const float3 sheen_tint = max(stack_load(stack, data.sheen_tint), zero_float3());
122 const float sheen_roughness = saturatef(stack_load(stack, data.sheen_roughness));
123 const float3 coat_normal = safe_normalize_fallback(
124 stack_load_float3_default(stack, data.coat_normal_offset, N), sd->N);
125 const float3 sheen_N = safe_normalize(mix(N, coat_normal, saturatef(coat_weight)));
126
127 const Spectrum closure_weight = sheen_weight * rgb_to_spectrum(sheen_tint) * weight;
128 const Spectrum albedo = bsdf_sheen_setup(kg, sd, closure_weight, sheen_N, sheen_roughness);
129
130 /* Attenuate lower layers */
131 weight = closure_layering_weight(albedo, weight);
132 }
133
134 /* Second layer: Coat */
135 if (coat_weight > CLOSURE_WEIGHT_CUTOFF) {
136 Coat coat;
137 coat.tint = rgb_to_spectrum(max(stack_load(stack, data.coat_tint), zero_float3()));
138 coat.ior = fmaxf(stack_load(stack, data.coat_ior), 1.0f);
139 coat.N = safe_normalize_fallback(stack_load_float3_default(stack, data.coat_normal_offset, N),
140 sd->N);
141 coat.roughness = saturatef(stack_load(stack, data.coat_roughness));
142 coat.weight = coat_weight * weight;
143
144 const Spectrum albedo = bsdf_coat_setup(kg, sd, ray_visibility, coat);
145
146 weight = closure_layering_weight(albedo, weight);
147 }
148
149 /* Emission (attenuated by sheen and coat) */
150 const Spectrum emission = rgb_to_spectrum(stack_load(stack, data.emission_color)) *
151 stack_load(stack, data.emission_strength);
152 if (!is_zero(emission)) {
153 emission_setup(sd, emission * weight);
154 }
155
156 return weight;
157}
158
159template<uint64_t node_feature_mask, ShaderType shader_type>
160#ifndef __KERNEL_ONEAPI__
162#else
164#endif
166 ccl_private ShaderData *sd,
167 ccl_private float *ccl_restrict stack,
168 Spectrum closure_weight,
170 const PathRayVisibility ray_visibility,
171 const uint32_t path_flag,
172 int offset)
173{
174 ClosureType type = node.closure_type;
175
176 const float mix_weight = stack_load_float_default(stack, node.mix_weight_offset, 1.0f);
177
178 /* Only compute BSDF for surfaces, transparent variable is shared with volume extinction. */
179 if constexpr (shader_type != SHADER_TYPE_SURFACE) {
180 return svm_node_closure_bsdf_skip(offset, type);
181 }
183 if (mix_weight == 0.0f) {
184 return svm_node_closure_bsdf_skip(offset, type);
185 }
186 }
187 else IF_KERNEL_NODES_FEATURE (EMISSION) {
188 if (mix_weight == 0.0f || type != CLOSURE_BSDF_PRINCIPLED_ID) {
189 /* Only principled BSDF can have emission. */
190 return svm_node_closure_bsdf_skip(offset, type);
191 }
192
194 kg, &offset);
195
196 float3 N = stack_load_float3_default(stack, data.normal_offset, sd->N);
197 N = safe_normalize_fallback(N, sd->N);
198
199 principled_bsdf_emission(kg, sd, stack, data, N, ray_visibility, path_flag, mix_weight);
200
201 return offset;
202 }
203 else {
204 return svm_node_closure_bsdf_skip(offset, type);
205 }
206
207 const Spectrum black = zero_spectrum();
208 const Spectrum white = one_spectrum();
209 switch (type) {
212 kg, &offset);
213
214 float3 N = stack_load_float3_default(stack, data.normal_offset, sd->N);
215 N = safe_normalize_fallback(N, sd->N);
216
218 kg, sd, stack, data, N, ray_visibility, path_flag, mix_weight);
219
220 const Spectrum base_color = rgb_to_spectrum(
221 max(stack_load(stack, data.base_color), zero_float3()));
222 const Spectrum clamped_base_color = min(base_color, white);
223 const float ior = fmaxf(stack_load(stack, data.ior), 1e-5f);
224 const float roughness = saturatef(stack_load(stack, data.roughness));
225 const float3 valid_reflection_N = maybe_ensure_valid_specular_reflection(sd, N);
226 const float anisotropic = saturatef(stack_load(stack, data.anisotropic));
227
228 const ClosureType distribution = data.distribution;
229 const Spectrum specular_tint = rgb_to_spectrum(
230 max(stack_load(stack, data.specular_tint), zero_float3()));
231 const float thinfilm_thickness = stack_load(stack, data.thin_film_thickness);
232 const float thinfilm_ior = (thinfilm_thickness > THINFILM_THICKNESS_CUTOFF) ?
233 fmaxf(stack_load(stack, data.thin_film_ior), 1e-5f) :
234 0.0f;
235
236 float alpha_x = sqr(roughness);
237 float alpha_y = sqr(roughness);
238 float3 T = zero_float3();
239 if (anisotropic > 0.0f && stack_valid(data.tangent_offset)) {
240 T = stack_load_float3(stack, data.tangent_offset);
241 const float aspect = sqrtf(1.0f - anisotropic * 0.9f);
242 alpha_x /= aspect;
243 alpha_y *= aspect;
244 const float anisotropic_rotation = stack_load(stack, data.anisotropic_rotation);
245 if (anisotropic_rotation != 0.0f) {
246 T = rotate_around_axis(T, N, anisotropic_rotation * M_2PI_F);
247 }
248 }
249
250#ifdef __CAUSTICS_TRICKS__
251 const bool reflective_caustics = (kernel_data.integrator.caustics_reflective ||
252 (ray_visibility & PATH_RAY_VISIBILITY_DIFFUSE) == 0);
253 const bool refractive_caustics = (kernel_data.integrator.caustics_refractive ||
254 (ray_visibility & PATH_RAY_VISIBILITY_DIFFUSE) == 0);
255#else
256 const bool reflective_caustics = true;
257 const bool refractive_caustics = true;
258#endif
259
260 /* Metallic component */
261 const float metallic = saturatef(stack_load(stack, data.metallic));
262 if (metallic > CLOSURE_WEIGHT_CUTOFF) {
263 if (reflective_caustics) {
265 sd, sizeof(MicrofacetBsdf), metallic * weight);
266 ccl_private FresnelF82Tint *fresnel =
267 (bsdf != nullptr) ?
269 nullptr;
270
271 if (bsdf && fresnel) {
272 bsdf->N = valid_reflection_N;
273 bsdf->ior = 1.0f;
274 bsdf->T = T;
275 bsdf->alpha_x = alpha_x;
276 bsdf->alpha_y = alpha_y;
277
278 fresnel->f0 = clamped_base_color;
279 const Spectrum f82 = min(specular_tint, white);
280
281 fresnel->thin_film.thickness = thinfilm_thickness;
282 fresnel->thin_film.ior = thinfilm_ior;
283
284 /* setup bsdf */
285 sd->runtime_flag |= bsdf_microfacet_ggx_setup(bsdf);
286 const bool is_multiggx = (distribution == CLOSURE_BSDF_MICROFACET_MULTI_GGX_GLASS_ID);
287 bsdf_microfacet_setup_fresnel_f82_tint(kg, bsdf, sd->wi, fresnel, f82, is_multiggx);
288 }
289 }
290 /* Attenuate other components */
291 weight *= (1.0f - metallic);
292 }
293
294 const bool thin_wall = stack_load(stack, data.thin_wall);
295
296 /* Transmission component */
297 const float transmission_weight = saturatef(stack_load(stack, data.transmission_weight));
298 if (transmission_weight > CLOSURE_WEIGHT_CUTOFF) {
299 if (reflective_caustics || refractive_caustics) {
300 FresnelThinFilm thinfilm = {thinfilm_thickness, thinfilm_ior};
301 if (thin_wall) {
303 sd,
304 reflective_caustics,
305 refractive_caustics,
306 {specular_tint, clamped_base_color},
307 transmission_weight * weight,
308 valid_reflection_N,
309 sqr(roughness),
310 ior,
311 thinfilm,
312 ray_visibility,
313 path_flag);
314 }
315 else {
317 sd, sizeof(MicrofacetBsdf), transmission_weight * weight);
320 sd, sizeof(FresnelGeneralizedSchlick)) :
321 nullptr;
322
323 if (bsdf && fresnel) {
324 const bool backfacing = (sd->runtime_flag & SR_BACKFACING);
325 bsdf->N = valid_reflection_N;
326 bsdf->T = T;
327 bsdf->alpha_x = alpha_x;
328 bsdf->alpha_y = alpha_y;
329
330 const float dispersion_scale = saturatef(
331 stack_load(stack, data.transmission_dispersion_scale));
332 const float abbe_number = fmaxf(
333 stack_load(stack, data.transmission_dispersion_abbe_number), 0.0f);
334 const float inv_abbe = safe_divide(dispersion_scale, abbe_number);
335 bsdf->ior = backfacing ? 1.0f / ior : ior;
336 bsdf->ior = bsdf_glass_ior(sd, bsdf->ior, inv_abbe);
337
338 if (backfacing) {
339 adjust_thin_film_ior_at_backface(thinfilm.ior, bsdf->ior);
340 }
341
342 *fresnel = generalized_schlick_setup(ior,
343 reflective_caustics,
344 refractive_caustics,
345 specular_tint,
346 sqrt(clamped_base_color),
347 thinfilm);
348
349 /* setup bsdf */
350 sd->runtime_flag |= bsdf_microfacet_ggx_glass_setup(bsdf);
351 const bool is_multiggx = (distribution ==
354 kg, bsdf, sd->wi, fresnel, is_multiggx);
355 }
356 }
357 }
358 /* Attenuate other components */
359 weight *= (1.0f - transmission_weight);
360 }
361
362 /* Apply IOR adjustment */
363 const float specular_ior_level = max(stack_load(stack, data.specular_ior_level), 0.0f);
364 float eta = ior;
365 float f0 = F0_from_ior(eta);
366 if (specular_ior_level != 0.5f) {
367 f0 *= 2.0f * specular_ior_level;
368 eta = ior_from_F0(f0);
369 if (ior < 1.0f) {
370 eta = 1.0f / eta;
371 }
372 }
373
374 /* Specular component */
375 if (reflective_caustics && (eta != 1.0f || thinfilm_thickness > 0.1f)) {
377 sd, sizeof(MicrofacetBsdf), weight);
380 sd, sizeof(FresnelGeneralizedSchlick)) :
381 nullptr;
382
383 if (bsdf && fresnel) {
384 bsdf->N = valid_reflection_N;
385 bsdf->ior = eta;
386 bsdf->T = T;
387 bsdf->alpha_x = alpha_x;
388 bsdf->alpha_y = alpha_y;
389
390 fresnel->f0 = f0 * specular_tint;
391 fresnel->f90 = white;
392 fresnel->exponent = -eta;
393 fresnel->tint = {white, black};
394 fresnel->thin_film = {thinfilm_thickness, thinfilm_ior};
395
396 /* setup bsdf */
397 sd->runtime_flag |= bsdf_microfacet_ggx_setup(bsdf);
398 const bool is_multiggx = (distribution == CLOSURE_BSDF_MICROFACET_MULTI_GGX_GLASS_ID);
400 kg, bsdf, sd->wi, fresnel, is_multiggx);
401
402 /* Attenuate lower layers */
403 const Spectrum albedo = closure_layer_albedo(kg, sd, (ccl_private ShaderClosure *)bsdf);
404 weight = closure_layering_weight(albedo, weight);
405 }
406 }
407
408 /* Diffuse/Subsurface component */
409#ifdef __SUBSURFACE__
410 const float subsurface_weight = saturatef(stack_load(stack, data.subsurface_weight));
411 if (subsurface_weight > CLOSURE_WEIGHT_CUTOFF) {
412 const float anisotropy = stack_load(stack, data.subsurface_anisotropy);
413 const Spectrum closure_weight = clamped_base_color * subsurface_weight * weight;
414 if (thin_wall) {
415 const float diffuse_roughness = saturatef(stack_load(stack, data.diffuse_roughness));
417 sd, N, closure_weight, anisotropy, diffuse_roughness, clamped_base_color);
418 }
419 else {
420 const ClosureType subsurface_method = data.subsurface_method;
421 ccl_private Bssrdf *bssrdf = bssrdf_alloc(sd, closure_weight);
422 if (bssrdf) {
423 const float3 subsurface_radius = stack_load(stack, data.subsurface_radius);
424 const float subsurface_scale = stack_load(stack, data.subsurface_scale);
425
426 bssrdf->radius = rgb_to_spectrum(
427 max(subsurface_radius * subsurface_scale, zero_float3()));
428 bssrdf->albedo = clamped_base_color;
430 bssrdf->alpha = sqr(roughness);
431 /* IOR is clamped to [1.01..3.8] inside bssrdf_setup */
432 bssrdf->ior = eta;
433 /* Anisotropy is clamped to a valid range inside bssrdf_setup. */
434 bssrdf->anisotropy = anisotropy;
435 if (subsurface_method == CLOSURE_BSSRDF_RANDOM_WALK_SKIN_ID) {
436 bssrdf->ior = stack_load(stack, data.subsurface_ior);
437 }
438
439 /* setup bsdf */
440 sd->runtime_flag |= bssrdf_setup(sd, bssrdf, path_flag, subsurface_method);
441 }
442 }
443 }
444#else
445 const float subsurface_weight = 0.0f;
446#endif
447
448 const float diffuse_roughness = saturatef(stack_load(stack, data.diffuse_roughness));
449 const Spectrum diffuse_weight = base_color * (1.0f - subsurface_weight) * weight;
450 if (diffuse_roughness_is_almost_zero(diffuse_roughness)) {
451 bsdf_diffuse_setup(sd, N, diffuse_weight);
452 }
453 else {
454 bsdf_oren_nayar_setup(sd, N, diffuse_weight, diffuse_roughness, base_color);
455 }
456
457 break;
458 }
461 kg, &offset);
462 float3 N = stack_load_float3_default(stack, bsdf_data.normal_offset, sd->N);
463 N = safe_normalize_fallback(N, sd->N);
464
465 const Spectrum weight = closure_weight * mix_weight;
466 const float roughness = stack_load(stack, bsdf_data.roughness);
467 if (diffuse_roughness_is_almost_zero(roughness)) {
468 bsdf_diffuse_setup(sd, N, weight);
469 }
470 else {
471 const Spectrum color = saturate(rgb_to_spectrum(stack_load(stack, bsdf_data.color)));
472 bsdf_oren_nayar_setup(sd, N, weight, roughness, color);
473 }
474 break;
475 }
478 kg, &offset);
479 float3 N = stack_load_float3_default(stack, bsdf_data.normal_offset, sd->N);
480 N = safe_normalize_fallback(N, sd->N);
481
482 const Spectrum weight = closure_weight * mix_weight;
483 /* FIXME(weizhen): `maybe_ensure_valid_specular_reflection` should only be applied to glossy
484 * closures, applying to translucent closure seems to be a mistake. */
486 break;
487 }
490 const Spectrum weight = closure_weight * mix_weight;
491 bsdf_transparent_setup(sd, weight, path_flag);
492 break;
493 }
497 kg, &offset);
498
499#ifdef __CAUSTICS_TRICKS__
500 if (!kernel_data.integrator.caustics_reflective &&
501 (ray_visibility & PATH_RAY_VISIBILITY_DIFFUSE))
502 {
503 break;
504 }
505#endif
507 sd, sizeof(MicrofacetBsdf), rgb_to_spectrum(make_float3(mix_weight)));
508
509 if (bsdf != nullptr) {
510 float3 N = stack_load_float3_default(stack, cdata.normal_offset, sd->N);
511 N = safe_normalize_fallback(N, sd->N);
512 const float3 valid_reflection_N = maybe_ensure_valid_specular_reflection(sd, N);
513 const float anisotropy = saturatef(stack_load(stack, cdata.anisotropy));
514 const float roughness = saturatef(stack_load(stack, cdata.roughness));
515 bsdf->alpha_x = sqr(roughness);
516 bsdf->alpha_y = sqr(roughness);
517 if (anisotropy > 0.0f && stack_valid(cdata.tangent_offset)) {
518 bsdf->T = stack_load_float3(stack, cdata.tangent_offset);
519 const float aspect = sqrtf(1.0f - anisotropy * 0.9f);
520 bsdf->alpha_x /= aspect;
521 bsdf->alpha_y *= aspect;
522 const float anisotropic_rotation = stack_load(stack, cdata.rotation);
523 if (anisotropic_rotation != 0.0f) {
524 bsdf->T = rotate_around_axis(bsdf->T, N, anisotropic_rotation * M_2PI_F);
525 }
526 }
527 else {
528 bsdf->T = zero_float3();
529 }
530
531 bsdf->N = valid_reflection_N;
532 bsdf->ior = 1.0f;
533
534 const float thin_film_thickness = fmaxf(stack_load(stack, cdata.thin_film_thickness),
535 1e-5f);
536 const float thin_film_ior = fmaxf(stack_load(stack, cdata.thin_film_ior), 1e-5f);
537
538 const ClosureType distribution = cdata.distribution;
539 /* Setup BSDF */
540 if (distribution == CLOSURE_BSDF_MICROFACET_BECKMANN_ID) {
541 sd->runtime_flag |= bsdf_microfacet_beckmann_setup(bsdf);
542 }
543 else {
544 sd->runtime_flag |= bsdf_microfacet_ggx_setup(bsdf);
545 }
546
547 const bool is_multiggx = (distribution == CLOSURE_BSDF_MICROFACET_MULTI_GGX_ID);
548
552
553 if (!fresnel) {
554 break;
555 }
556
557 fresnel->thin_film.thickness = thin_film_thickness;
558 fresnel->thin_film.ior = thin_film_ior;
559
560 const float3 n = max(stack_load(stack, cdata.base_ior), zero_float3());
561 const float3 k = max(stack_load(stack, cdata.edge_tint_k), zero_float3());
562
563 fresnel->ior = {rgb_to_spectrum(n), rgb_to_spectrum(k)};
564 bsdf_microfacet_setup_fresnel_conductor(kg, bsdf, sd->wi, fresnel, is_multiggx);
565 }
566 else {
568 sd, sizeof(FresnelF82Tint));
569
570 if (!fresnel) {
571 break;
572 }
573
574 fresnel->thin_film.thickness = thin_film_thickness;
575 fresnel->thin_film.ior = thin_film_ior;
576
577 const float3 color = saturate(stack_load(stack, cdata.base_ior));
578 const float3 tint = saturate(stack_load(stack, cdata.edge_tint_k));
579
580 fresnel->f0 = rgb_to_spectrum(color);
581 const Spectrum f82 = rgb_to_spectrum(tint);
582 bsdf_microfacet_setup_fresnel_f82_tint(kg, bsdf, sd->wi, fresnel, f82, is_multiggx);
583 }
584 }
585 break;
586 }
588 const ccl_global SVMNodeRayPortalBsdfData &bsdf_data =
590 const Spectrum weight = closure_weight * mix_weight;
591 const float3 position = stack_load_float3_default(stack, bsdf_data.position_offset, sd->P);
592 const float3 direction = stack_load(stack, bsdf_data.direction);
593 bsdf_ray_portal_setup(sd, weight, position, direction);
594 break;
595 }
601 kg, &offset);
602
603#ifdef __CAUSTICS_TRICKS__
604 if (!kernel_data.integrator.caustics_reflective &&
605 (ray_visibility & PATH_RAY_VISIBILITY_DIFFUSE))
606 {
607 break;
608 }
609#endif
610 float3 N = stack_load_float3_default(stack, bsdf_data.normal_offset, sd->N);
611 N = safe_normalize_fallback(N, sd->N);
612
613 const Spectrum weight = closure_weight * mix_weight;
615 sd, sizeof(MicrofacetBsdf), weight);
616
617 if (!bsdf) {
618 break;
619 }
620
621 const float roughness = sqr(saturatef(stack_load(stack, bsdf_data.roughness)));
622
624 bsdf->ior = 1.0f;
625
626 /* compute roughness */
627 const float anisotropy = clamp(stack_load(stack, bsdf_data.anisotropy), -0.99f, 0.99f);
628 if (!stack_valid(bsdf_data.tangent_offset) || fabsf(anisotropy) <= 1e-4f) {
629 /* Isotropic case. */
630 bsdf->T = zero_float3();
631 bsdf->alpha_x = roughness;
632 bsdf->alpha_y = roughness;
633 }
634 else {
635 bsdf->T = stack_load_float3(stack, bsdf_data.tangent_offset);
636
637 /* Rotate tangent. */
638 const float rotation = stack_load(stack, bsdf_data.rotation);
639 if (rotation != 0.0f) {
640 bsdf->T = rotate_around_axis(bsdf->T, bsdf->N, rotation * M_2PI_F);
641 }
642
643 if (anisotropy < 0.0f) {
644 bsdf->alpha_x = roughness / (1.0f + anisotropy);
645 bsdf->alpha_y = roughness * (1.0f + anisotropy);
646 }
647 else {
648 bsdf->alpha_x = roughness * (1.0f - anisotropy);
649 bsdf->alpha_y = roughness / (1.0f - anisotropy);
650 }
651 }
652
653 /* setup bsdf */
655 sd->runtime_flag |= bsdf_microfacet_beckmann_setup(bsdf);
656 }
657 else if (type == CLOSURE_BSDF_ASHIKHMIN_SHIRLEY_ID) {
658 sd->runtime_flag |= bsdf_ashikhmin_shirley_setup(bsdf);
659 }
660 else {
661 sd->runtime_flag |= bsdf_microfacet_ggx_setup(bsdf);
663 const Spectrum color = max(rgb_to_spectrum(stack_load(stack, bsdf_data.color)), black);
664 bsdf_microfacet_setup_fresnel_constant(kg, bsdf, sd->wi, color);
665 }
666 }
667
668 break;
669 }
672 const ccl_global SVMNodeRefractionBsdfData &bsdf_data =
674
675#ifdef __CAUSTICS_TRICKS__
676 if (!kernel_data.integrator.caustics_refractive &&
677 (ray_visibility & PATH_RAY_VISIBILITY_DIFFUSE))
678 {
679 break;
680 }
681#endif
682 float3 N = stack_load_float3_default(stack, bsdf_data.normal_offset, sd->N);
683 N = safe_normalize_fallback(N, sd->N);
684
685 const Spectrum weight = closure_weight * mix_weight;
687 sd, sizeof(MicrofacetBsdf), weight);
688
689 if (bsdf) {
691 bsdf->T = zero_float3();
692
693 float eta = fmaxf(stack_load(stack, bsdf_data.ior), 1e-5f);
694 eta = (sd->runtime_flag & SR_BACKFACING) ? 1.0f / eta : eta;
695
696 /* setup bsdf */
697 const float roughness = sqr(stack_load(stack, bsdf_data.roughness));
698 bsdf->alpha_x = roughness;
699 bsdf->alpha_y = roughness;
700 bsdf->ior = eta;
701
703 sd->runtime_flag |= bsdf_microfacet_beckmann_refraction_setup(bsdf);
704 }
705 else {
706 sd->runtime_flag |= bsdf_microfacet_ggx_refraction_setup(bsdf);
707 }
708 }
709
710 break;
711 }
716 kg, &offset);
717
718#ifdef __CAUSTICS_TRICKS__
719 const bool reflective_caustics = (kernel_data.integrator.caustics_reflective ||
720 (ray_visibility & PATH_RAY_VISIBILITY_DIFFUSE) == 0);
721 const bool refractive_caustics = (kernel_data.integrator.caustics_refractive ||
722 (ray_visibility & PATH_RAY_VISIBILITY_DIFFUSE) == 0);
723 if (!(reflective_caustics || refractive_caustics)) {
724 break;
725 }
726#else
727 const bool reflective_caustics = true;
728 const bool refractive_caustics = true;
729#endif
730
731 float3 N = stack_load_float3_default(stack, bsdf_data.normal_offset, sd->N);
732 N = safe_normalize_fallback(N, sd->N);
733
734 const float thinfilm_thickness = stack_load(stack, bsdf_data.thin_film_thickness);
735 const float thinfilm_ior = fmaxf(stack_load(stack, bsdf_data.thin_film_ior), 1e-5f);
736
738 sd, sizeof(MicrofacetBsdf), make_spectrum(mix_weight));
741 sd, sizeof(FresnelGeneralizedSchlick)) :
742 nullptr;
743
744 if (bsdf && fresnel) {
746 const float anisotropy = clamp(stack_load(stack, bsdf_data.anisotropy), -0.99f, 0.99f);
747 const float roughness = sqr(saturatef(stack_load(stack, bsdf_data.roughness)));
748 if (!stack_valid(bsdf_data.tangent_offset) || fabsf(anisotropy) <= 1e-4f) {
749 /* Isotropic case. */
750 bsdf->T = zero_float3();
751 bsdf->alpha_x = bsdf->alpha_y = roughness;
752 }
753 else {
754 bsdf->T = stack_load_float3(stack, bsdf_data.tangent_offset);
755
756 /* Rotate tangent. */
757 const float rotation = stack_load(stack, bsdf_data.rotation);
758 if (rotation != 0.0f) {
759 bsdf->T = rotate_around_axis(bsdf->T, bsdf->N, rotation * M_2PI_F);
760 }
761
762 if (anisotropy < 0.0f) {
763 bsdf->alpha_x = roughness / (1.0f + anisotropy);
764 bsdf->alpha_y = roughness * (1.0f + anisotropy);
765 }
766 else {
767 bsdf->alpha_x = roughness * (1.0f - anisotropy);
768 bsdf->alpha_y = roughness / (1.0f - anisotropy);
769 }
770 }
771
772 const float ior = fmaxf(stack_load(stack, bsdf_data.ior), 1e-5f);
773 bsdf->ior = (sd->runtime_flag & SR_BACKFACING) ? 1.0f / ior : ior;
774
775 fresnel->f0 = make_float3(F0_from_ior(ior));
776 fresnel->f90 = white;
777 fresnel->exponent = -ior;
778 const Spectrum color = max(rgb_to_spectrum(stack_load(stack, bsdf_data.color)), black);
779 fresnel->tint = {float(reflective_caustics) * color, float(refractive_caustics) * color};
780 fresnel->thin_film.thickness = thinfilm_thickness;
781 fresnel->thin_film.ior = (sd->runtime_flag & SR_BACKFACING) ? thinfilm_ior / ior :
782 thinfilm_ior;
783 /* setup bsdf */
785 sd->runtime_flag |= bsdf_microfacet_beckmann_glass_setup(bsdf);
786 }
787 else {
788 sd->runtime_flag |= bsdf_microfacet_ggx_glass_setup(bsdf);
789 }
790 const bool is_multiggx = (type == CLOSURE_BSDF_MICROFACET_MULTI_GGX_GLASS_ID);
791 bsdf_microfacet_setup_fresnel_generalized_schlick(kg, bsdf, sd->wi, fresnel, is_multiggx);
792 }
793 break;
794 }
797 kg, &offset);
798 float3 N = stack_load_float3_default(stack, bsdf_data.normal_offset, sd->N);
799 N = safe_normalize_fallback(N, sd->N);
800
801 const Spectrum weight = closure_weight * mix_weight;
803 sd, sizeof(VelvetBsdf), weight);
804
805 if (bsdf) {
806 bsdf->N = N;
807
808 bsdf->sigma = saturatef(stack_load(stack, bsdf_data.param1));
809 sd->runtime_flag |= bsdf_ashikhmin_velvet_setup(bsdf);
810 }
811 break;
812 }
815 kg, &offset);
816 float3 N = stack_load_float3_default(stack, bsdf_data.normal_offset, sd->N);
817 N = safe_normalize_fallback(N, sd->N);
818
819 const Spectrum weight = closure_weight * mix_weight;
820 const float roughness = saturatef(stack_load(stack, bsdf_data.param1));
821 bsdf_sheen_setup(kg, sd, weight, N, roughness);
822 break;
823 }
827 &offset);
828
829#ifdef __CAUSTICS_TRICKS__
830 if (type == CLOSURE_BSDF_GLOSSY_TOON_ID && !kernel_data.integrator.caustics_reflective &&
831 (ray_visibility & PATH_RAY_VISIBILITY_DIFFUSE))
832 {
833 break;
834 }
835#endif
836 float3 N = stack_load_float3_default(stack, bsdf_data.normal_offset, sd->N);
837 N = safe_normalize_fallback(N, sd->N);
838
839 const Spectrum weight = closure_weight * mix_weight;
841 sd, sizeof(ToonBsdf), weight);
842
843 if (bsdf) {
844 bsdf->N = N;
845 bsdf->size = stack_load(stack, bsdf_data.size);
846 bsdf->smooth = stack_load(stack, bsdf_data.smooth);
847
848 if (type == CLOSURE_BSDF_DIFFUSE_TOON_ID) {
849 sd->runtime_flag |= bsdf_diffuse_toon_setup(bsdf);
850 }
851 else {
852 sd->runtime_flag |= bsdf_glossy_toon_setup(bsdf);
853 }
854 }
855 break;
856 }
861
862#if defined(__HAIR__) && defined(__PRINCIPLED_HAIR__)
863 const Spectrum weight = closure_weight * mix_weight;
864
865 const float alpha = stack_load(stack, hdata.offset);
866 const float ior = stack_load(stack, hdata.ior);
867
868 const AttributeDescriptor attr_descr_random = find_attribute(kg, sd, hdata.attr_random);
869 float random = 0.0f;
870 if (is_attribute_found(attr_descr_random)) {
871 random = primitive_surface_attribute<float>(kg, sd, attr_descr_random);
872 }
873 else {
874 random = stack_load(stack, hdata.random);
875 }
876
877 /* Random factors range: [-randomization/2, +randomization/2]. */
878 const float random_roughness = stack_load(stack, hdata.random_roughness);
879 const float factor_random_roughness = 1.0f + 2.0f * (random - 0.5f) * random_roughness;
880 const float roughness = stack_load(stack, hdata.roughness) * factor_random_roughness;
881 const float radial_roughness = (type == CLOSURE_BSDF_HAIR_CHIANG_ID) ?
882 stack_load(stack, hdata.radial_roughness) *
883 factor_random_roughness :
884 roughness;
885
886 Spectrum sigma;
887 switch (hdata.parametrization) {
889 const float3 absorption_coefficient = stack_load(stack, hdata.absorption_coefficient);
890 sigma = rgb_to_spectrum(absorption_coefficient);
891 break;
892 }
894 float melanin = stack_load(stack, hdata.melanin);
895 const float melanin_redness = stack_load(stack, hdata.melanin_redness);
896
897 /* Randomize melanin. */
898 float random_color = stack_load(stack, hdata.random_color);
899 random_color = clamp(random_color, 0.0f, 1.0f);
900 const float factor_random_color = 1.0f + 2.0f * (random - 0.5f) * random_color;
901 melanin *= factor_random_color;
902
903 /* Map melanin 0..inf from more perceptually linear 0..1. */
904 melanin = -logf(fmaxf(1.0f - melanin, 0.0001f));
905
906 /* Benedikt Bitterli's melanin ratio remapping. */
907 const float eumelanin = melanin * (1.0f - melanin_redness);
908 const float pheomelanin = melanin * melanin_redness;
910 eumelanin, pheomelanin);
911
912 /* Optional tint. */
913 const float3 tint = stack_load(stack, hdata.tint);
915 rgb_to_spectrum(tint), radial_roughness);
916
917 sigma = melanin_sigma + tint_sigma;
918 break;
919 }
921 const float3 color = stack_load(stack, hdata.color);
923 radial_roughness);
924 break;
925 }
926 default: {
927 /* Fallback to brownish hair, same as defaults for melanin. */
928 kernel_assert(!"Invalid Hair parametrization!");
929 sigma = bsdf_principled_hair_sigma_from_concentration(0.0f, 0.8054375f);
930 break;
931 }
932 }
933
934 if (type == CLOSURE_BSDF_HAIR_CHIANG_ID) {
936 sd, sizeof(ChiangHairBSDF), weight);
937 if (bsdf) {
938 /* Remap Coat value to [0, 100]% of Roughness. */
939 const float coat = stack_load(stack, hdata.coat);
940 const float m0_roughness = 1.0f - clamp(coat, 0.0f, 1.0f);
941
942 bsdf->v = roughness;
943 bsdf->s = radial_roughness;
944 bsdf->m0_roughness = m0_roughness;
945 bsdf->alpha = alpha;
946 bsdf->eta = ior;
947 bsdf->sigma = sigma;
948
949 sd->runtime_flag |= bsdf_hair_chiang_setup(sd, bsdf);
950 }
951 }
952 else {
954 const float R = stack_load(stack, hdata.R);
955 const float TT = stack_load(stack, hdata.TT);
956 const float TRT = stack_load(stack, hdata.TRT);
957 if (R <= 0.0f && TT <= 0.0f && TRT <= 0.0f) {
958 break;
959 }
960
962 sd, sizeof(HuangHairBSDF), weight);
963 if (bsdf) {
965 sd, sizeof(HuangHairExtra));
966
967 if (!extra) {
968 break;
969 }
970
971 bsdf->extra = extra;
972 bsdf->extra->R = fmaxf(0.0f, R);
973 bsdf->extra->TT = fmaxf(0.0f, TT);
974 bsdf->extra->TRT = fmaxf(0.0f, TRT);
975
976 bsdf->extra->pixel_coverage = 1.0f;
977
978 /* For camera ray, check if the hair covers more than one pixel, in which case a
979 * nearfield model is needed to prevent ribbon-like appearance. */
980 if ((ray_visibility & PATH_RAY_VISIBILITY_CAMERA) && (sd->type & PRIMITIVE_CURVE)) {
981 /* Interpolate radius between curve keys. */
982 const KernelCurve kcurve = kernel_data_fetch(curves, sd->prim);
983 const int k0 = kcurve.first_key + PRIMITIVE_UNPACK_SEGMENT(sd->type);
984 const int k1 = k0 + 1;
985 const int position_offset = kernel_data_fetch(objects, sd->object).position_offset;
986 const float radius = mix(kernel_data_fetch(curve_keys, position_offset + k0).w,
987 kernel_data_fetch(curve_keys, position_offset + k1).w,
988 sd->u);
989
990 bsdf->extra->pixel_coverage = 0.5f * sd->dP / radius;
991 }
992
993 bsdf->aspect_ratio = stack_load(stack, hdata.aspect_ratio);
994 if (bsdf->aspect_ratio != 1.0f) {
995 /* Align ellipse major axis with the curve normal direction. */
996 const AttributeDescriptor attr_descr_normal = find_attribute(
997 kg, sd, hdata.attr_normal);
998 bsdf->N = curve_attribute<float3>(kg, sd, attr_descr_normal);
999 }
1000
1001 bsdf->roughness = roughness;
1002 bsdf->tilt = alpha;
1003 bsdf->eta = ior;
1004 bsdf->sigma = sigma;
1005
1006 sd->runtime_flag |= bsdf_hair_huang_setup(sd, bsdf, path_flag);
1007 }
1008 }
1009#else
1010 (void)hdata;
1011#endif
1012 break;
1013 }
1017 &offset);
1018
1019#ifdef __HAIR__
1020 const Spectrum weight = closure_weight * mix_weight;
1021
1023 sd, sizeof(HairBsdf), weight);
1024
1025 if (bsdf) {
1026 bsdf->N = maybe_ensure_valid_specular_reflection(sd, sd->N);
1027 bsdf->roughness1 = stack_load(stack, bsdf_data.roughness1);
1028 bsdf->roughness2 = stack_load(stack, bsdf_data.roughness2);
1029 bsdf->offset = -stack_load(stack, bsdf_data.offset);
1030
1031 if (stack_valid(bsdf_data.tangent_offset)) {
1032 bsdf->T = normalize(stack_load_float3(stack, bsdf_data.tangent_offset));
1033 }
1034 else if (!(sd->type & PRIMITIVE_CURVE)) {
1035 bsdf->T = normalize(sd->dPdv);
1036 bsdf->offset = 0.0f;
1037 }
1038 else {
1039 bsdf->T = normalize(sd->dPdu);
1040 }
1041
1042 if (type == CLOSURE_BSDF_HAIR_REFLECTION_ID) {
1043 sd->runtime_flag |= bsdf_hair_reflection_setup(bsdf);
1044 }
1045 else {
1046 sd->runtime_flag |= bsdf_hair_transmission_setup(bsdf);
1047 }
1048 }
1049#else
1050 (void)bsdf_data;
1051#endif /* __HAIR__ */
1052
1053 break;
1054 }
1055
1060 const ccl_global SVMNodeBssrdfData &bsdf_data = svm_node_get<SVMNodeBssrdfData>(kg, &offset);
1061#ifdef __SUBSURFACE__
1062 float3 N = stack_load_float3_default(stack, bsdf_data.normal_offset, sd->N);
1063 N = safe_normalize_fallback(N, sd->N);
1064
1065 const Spectrum weight = closure_weight * mix_weight;
1066 ccl_private Bssrdf *bssrdf = bssrdf_alloc(sd, weight);
1067
1068 if (bssrdf) {
1069 const float scale = stack_load(stack, bsdf_data.scale);
1070 bssrdf->radius = max(rgb_to_spectrum(stack_load(stack, bsdf_data.radius) * scale), black);
1071 bssrdf->albedo = closure_weight;
1073 bssrdf->ior = stack_load(stack, bsdf_data.ior);
1074 bssrdf->alpha = saturatef(stack_load(stack, bsdf_data.roughness));
1075 bssrdf->anisotropy = stack_load(stack, bsdf_data.anisotropy);
1076
1077 sd->runtime_flag |= bssrdf_setup(sd, bssrdf, path_flag, type);
1078 }
1079#else
1080 (void)bsdf_data;
1081#endif
1082
1083 break;
1084 }
1085 default:
1086 /* Unknown closure type, skip the minimum data payload. */
1088 break;
1089 }
1090
1091 return offset;
1092}
1093
1095 ccl_private float *stack,
1096 Spectrum weight,
1097 const uint type,
1098 const SVMInputFloat param1,
1099 const SVMInputFloat param_extra)
1100{
1101 switch (type) {
1104 bsdf_alloc(sd, sizeof(HenyeyGreensteinVolume), weight);
1105 if (volume) {
1106 volume->g = stack_load(stack, param1);
1107 sd->runtime_flag |= volume_henyey_greenstein_setup(volume);
1108 }
1109 } break;
1112 sd, sizeof(FournierForandVolume), weight);
1113 if (volume) {
1114 const float IOR = stack_load(stack, param1);
1115 const float B = stack_load(stack, param_extra);
1116 sd->runtime_flag |= volume_fournier_forand_setup(volume, B, IOR);
1117 }
1118 } break;
1121 sd, sizeof(RayleighVolume), weight);
1122 if (volume) {
1123 sd->runtime_flag |= volume_rayleigh_setup(volume);
1124 }
1125 break;
1126 }
1129 sd, sizeof(DraineVolume), weight);
1130 if (volume) {
1131 volume->g = stack_load(stack, param1);
1132 volume->alpha = stack_load(stack, param_extra);
1133 sd->runtime_flag |= volume_draine_setup(volume);
1134 }
1135 } break;
1136 case CLOSURE_VOLUME_MIE_ID: {
1137 const float d = stack_load(stack, param1);
1138 float g_HG;
1139 float g_D;
1140 float alpha;
1141 float mixture;
1142 phase_mie_fitted_parameters(d, &g_HG, &g_D, &alpha, &mixture);
1144 sd, sizeof(HenyeyGreensteinVolume), weight * (1.0f - mixture));
1145 if (hg) {
1146 hg->g = g_HG;
1147 sd->runtime_flag |= volume_henyey_greenstein_setup(hg);
1148 }
1150 sd, sizeof(DraineVolume), weight * mixture);
1151 if (draine) {
1152 draine->g = g_D;
1153 draine->alpha = alpha;
1154 sd->runtime_flag |= volume_draine_setup(draine);
1155 }
1156 } break;
1157 default: {
1158 kernel_assert(0);
1159 break;
1160 }
1161 }
1162}
1163
1164template<ShaderType shader_type>
1166 KernelGlobals kg,
1167 ccl_private ShaderData *sd,
1168 ccl_private float *ccl_restrict stack,
1169 Spectrum closure_weight,
1171{
1172#ifdef __VOLUME__
1173 /* Only sum extinction for volumes, variable is shared with surface transparency. */
1174 if (shader_type != SHADER_TYPE_VOLUME) {
1175 return;
1176 }
1177
1178 const float mix_weight = stack_load_float_default(stack, node.mix_weight_offset, 1.0f);
1179 if (mix_weight == 0.0f) {
1180 return;
1181 }
1182
1183 float density = stack_load(stack, node.density);
1184 density = mix_weight * fmaxf(density, 0.0f) * object_volume_density(kg, sd->object);
1185
1186 /* Compute scattering coefficient. */
1187 Spectrum weight = closure_weight;
1188
1189 if (node.closure_type == CLOSURE_VOLUME_ABSORPTION_ID) {
1190 weight = one_spectrum() - weight;
1191 }
1192
1193 weight *= density;
1194
1195 /* Add closure for volume scattering. */
1196 if (CLOSURE_IS_VOLUME_SCATTER(node.closure_type)) {
1198 sd, stack, weight, node.closure_type, node.param1, node.param_extra);
1199 }
1200
1201 /* Sum total extinction weight. */
1202 volume_extinction_setup(sd, weight);
1203#endif
1204}
1205
1206template<ShaderType shader_type>
1208 KernelGlobals kg,
1209 ccl_private ShaderData *sd,
1210 ccl_private float *ccl_restrict stack,
1211 Spectrum scatter_coeffs,
1213 const PathRayVisibility path_visibility,
1214 const uint32_t path_flag)
1215{
1216#ifdef __VOLUME__
1217 /* Only sum extinction for volumes, variable is shared with surface transparency. */
1218 if (shader_type != SHADER_TYPE_VOLUME) {
1219 return;
1220 }
1221
1222 const float mix_weight = stack_load_float_default(stack, node.mix_weight_offset, 1.0f);
1223 if (mix_weight == 0.0f) {
1224 return;
1225 }
1226
1227 /* Compute scattering coefficient. */
1228 const float weight = mix_weight * object_volume_density(kg, sd->object);
1229
1230 /* Add closure for volume scattering. */
1231 if (!is_zero(scatter_coeffs) && CLOSURE_IS_VOLUME_SCATTER(node.closure_type)) {
1233 sd, stack, weight * scatter_coeffs, node.closure_type, node.param1, node.param_extra);
1234 }
1235
1236 const float3 absorption_coeffs = stack_load(stack, node.absorption_coeffs);
1237 volume_extinction_setup(sd, weight * (scatter_coeffs + absorption_coeffs));
1238
1239 /* Compute emission. */
1240 if ((path_visibility & PATH_RAY_VISIBILITY_SHADOW) || (path_flag & PATH_RAY_EXTINCTION)) {
1241 /* Don't need emission for shadows and extinction. */
1242 return;
1243 }
1244
1245 const float3 emission_coeffs = stack_load(stack, node.emission_coeffs);
1246 if (is_zero(emission_coeffs)) {
1247 return;
1248 }
1249 emission_setup(sd, weight * emission_coeffs);
1250
1251#endif
1252}
1253
1254template<ShaderType shader_type>
1256 KernelGlobals kg,
1257 ccl_private ShaderData *sd,
1258 ccl_private float *ccl_restrict stack,
1259 const Spectrum closure_weight,
1261 const PathRayVisibility path_visibility,
1262 const uint32_t path_flag)
1263{
1264#ifdef __VOLUME__
1265 /* Only sum extinction for volumes, variable is shared with surface transparency. */
1266 if (shader_type != SHADER_TYPE_VOLUME) {
1267 return;
1268 }
1269
1270 const float mix_weight = stack_load_float_default(stack, node.mix_weight_offset, 1.0f);
1271
1272 if (mix_weight == 0.0f) {
1273 return;
1274 }
1275
1276 /* Compute density. */
1277 const float weight = mix_weight * object_volume_density(kg, sd->object);
1278 float primitive_density = 1.0f;
1279 float density = stack_load(stack, node.density);
1280 density = weight * fmaxf(density, 0.0f);
1281
1282 if (density > 0.0f) {
1283 /* Density and color attribute lookup if available. */
1284 const AttributeDescriptor attr_density = find_attribute(kg, sd, node.attr_density);
1285 if (is_attribute_found(attr_density)) {
1286 primitive_density = primitive_volume_attribute<float>(kg, sd, attr_density, true);
1287 density = fmaxf(density * primitive_density, 0.0f);
1288 }
1289 }
1290
1291 if (density > 0.0f) {
1292 /* Compute scattering color. */
1293 Spectrum color = closure_weight;
1294
1295 const AttributeDescriptor attr_color = find_attribute(kg, sd, node.attr_color);
1296 if (is_attribute_found(attr_color)) {
1297 color *= rgb_to_spectrum(primitive_volume_attribute<float3>(kg, sd, attr_color, true));
1298 }
1299
1300 /* Add closure for volume scattering. */
1302 sd, sizeof(HenyeyGreensteinVolume), color * density);
1303 if (volume) {
1304 const float anisotropy = stack_load(stack, node.anisotropy);
1305 volume->g = anisotropy;
1306 sd->runtime_flag |= volume_henyey_greenstein_setup(volume);
1307 }
1308
1309 /* Add extinction weight. */
1310 const float3 absorption_color = max(sqrt(stack_load(stack, node.absorption_color)),
1311 zero_float3());
1312
1313 const Spectrum zero = zero_spectrum();
1314 const Spectrum one = one_spectrum();
1315 const Spectrum absorption = max(one - color, zero) *
1316 max(one - rgb_to_spectrum(absorption_color), zero);
1317 volume_extinction_setup(sd, (color + absorption) * density);
1318 }
1319
1320 /* Compute emission. */
1321 if ((path_visibility & PATH_RAY_VISIBILITY_SHADOW) || (path_flag & PATH_RAY_EXTINCTION)) {
1322 /* Don't need emission for shadows and extinction. */
1323 return;
1324 }
1325
1326 const float emission = stack_load(stack, node.emission);
1327 const float blackbody = stack_load(stack, node.blackbody);
1328
1329 if (emission > 0.0f) {
1330 const float3 emission_color = stack_load(stack, node.emission_color);
1331 emission_setup(sd, rgb_to_spectrum(emission * emission_color * weight));
1332 }
1333
1334 if (blackbody > 0.0f) {
1335 float T = stack_load(stack, node.temperature);
1336
1337 /* Add flame temperature from attribute if available. */
1338 const AttributeDescriptor attr_temperature = find_attribute(kg, sd, node.attr_temperature);
1339 if (is_attribute_found(attr_temperature)) {
1340 const float temperature = primitive_volume_attribute<float>(kg, sd, attr_temperature, true);
1341 T *= fmaxf(temperature, 0.0f);
1342 }
1343
1344 T = fmaxf(T, 0.0f);
1345
1346 /* Stefan-Boltzmann law. */
1347 const float T4 = sqr(sqr(T));
1348 const float sigma = 5.670373e-8f * 1e-6f / M_PI_F;
1349 const float intensity = sigma * mix(1.0f, T4, blackbody);
1350
1351 if (intensity > 0.0f) {
1352 const float3 blackbody_tint = stack_load(stack, node.blackbody_tint);
1353 const float3 bb = blackbody_tint * intensity *
1355 emission_setup(sd, rgb_to_spectrum(bb * weight));
1356 }
1357 }
1358#endif
1359}
1360
1362 KernelGlobals kg,
1363 ccl_private ShaderData *sd,
1364 ccl_private float *ccl_restrict stack,
1365 Spectrum closure_weight,
1367 const PathRayVisibility path_visibility,
1368 const uint32_t path_flag)
1369{
1370 Spectrum weight = closure_weight;
1371
1372 if (stack_valid(node.mix_weight_offset)) {
1373 const float mix_weight = stack_load_float(stack, node.mix_weight_offset);
1374
1375 if (mix_weight == 0.0f) {
1376 return;
1377 }
1378
1379 weight *= mix_weight;
1380 }
1381
1382 if (sd->runtime_flag & SR_IS_VOLUME_SHADER_EVAL) {
1383 if ((path_visibility & PATH_RAY_VISIBILITY_SHADOW) || (path_flag & PATH_RAY_EXTINCTION)) {
1384 /* Don't need emission for shadows and extinction. */
1385 return;
1386 }
1387 weight *= object_volume_density(kg, sd->object);
1388 }
1389
1390 emission_setup(sd, weight);
1391}
1392
1394 ccl_private ShaderData *sd,
1395 ccl_private float *ccl_restrict stack,
1396 Spectrum closure_weight,
1398{
1399 Spectrum weight = closure_weight;
1400
1401 if (stack_valid(node.mix_weight_offset)) {
1402 const float mix_weight = stack_load_float(stack, node.mix_weight_offset);
1403
1404 if (mix_weight == 0.0f) {
1405 return;
1406 }
1407
1408 weight *= mix_weight;
1409 }
1410
1411 background_setup(sd, weight);
1412}
1413
1415 ccl_private ShaderData *sd,
1416 ccl_private float *ccl_restrict stack,
1417 Spectrum closure_weight,
1419{
1420 if (stack_valid(node.mix_weight_offset)) {
1421 const float mix_weight = stack_load_float(stack, node.mix_weight_offset);
1422
1423 if (mix_weight == 0.0f) {
1424 return;
1425 }
1426
1427 closure_alloc(sd, sizeof(ShaderClosure), CLOSURE_HOLDOUT_ID, closure_weight * mix_weight);
1428 }
1429 else {
1430 closure_alloc(sd, sizeof(ShaderClosure), CLOSURE_HOLDOUT_ID, closure_weight);
1431 }
1432
1433 sd->runtime_flag |= SR_HOLDOUT;
1434}
1435
1436/* Closure Nodes */
1437
1440 node)
1441{
1442 *closure_weight = rgb_to_spectrum(node.rgb);
1443}
1444
1446 ccl_private Spectrum *closure_weight,
1448{
1449 *closure_weight = rgb_to_spectrum(stack_load_float3(stack, node.weight_offset));
1450}
1451
1453 ccl_private Spectrum *closure_weight,
1455{
1456 const float strength = stack_load(stack, node.strength);
1457 *closure_weight = rgb_to_spectrum(stack_load(stack, node.color)) * strength;
1458}
1459
1462{
1463 /* fetch weight from blend input, previous mix closures,
1464 * and write to stack to be used by closure nodes later */
1465 float weight = stack_load(stack, node.fac);
1466 weight = saturatef(weight);
1467
1468 const float in_weight = stack_load_float_default(stack, node.in_weight_offset, 1.0f);
1469
1470 if (stack_valid(node.weight1_offset)) {
1471 stack_store_float(stack, node.weight1_offset, in_weight * (1.0f - weight));
1472 }
1473 if (stack_valid(node.weight2_offset)) {
1474 stack_store_float(stack, node.weight2_offset, in_weight * weight);
1475 }
1476}
1477
1478/* (Bump) normal */
1479
1481 ccl_private float *ccl_restrict stack,
1483{
1484 const float3 normal = stack_load_float3(stack, node.direction_offset);
1485 sd->N = normal;
1486 stack_store_float3(stack, node.normal_offset, normal);
1487}
1488
CCL_NAMESPACE_BEGIN ccl_device ccl_private ShaderClosure * closure_alloc(ccl_private ShaderData *sd, const uint size, ClosureType type, Spectrum weight)
Definition alloc.h:11
ccl_device_inline ccl_private ShaderClosure * bsdf_alloc(ccl_private ShaderData *sd, const int size, Spectrum weight)
Definition alloc.h:76
ccl_device ccl_private void * closure_alloc_extra(ccl_private ShaderData *sd, const int size)
Definition alloc.h:34
ccl_device_inline Spectrum closure_layer_albedo(KernelGlobals kg, const ccl_private ShaderData *sd, const ccl_private ShaderClosure *sc)
Definition bsdf.h:743
CCL_NAMESPACE_BEGIN ccl_device int bsdf_ashikhmin_shirley_setup(ccl_private MicrofacetBsdf *bsdf)
ccl_device int bsdf_ashikhmin_velvet_setup(ccl_private VelvetBsdf *bsdf)
ccl_device void bsdf_translucent_setup(ccl_private ShaderData *sd, const float3 N, const Spectrum weight)
ccl_device void bsdf_diffuse_setup(ccl_private ShaderData *sd, const float3 N, const Spectrum weight)
ccl_device int bsdf_hair_reflection_setup(ccl_private HairBsdf *bsdf)
Definition bsdf_hair.h:27
ccl_device int bsdf_hair_transmission_setup(ccl_private HairBsdf *bsdf)
Definition bsdf_hair.h:35
ccl_device void bsdf_microfacet_setup_fresnel_conductor(KernelGlobals kg, ccl_private MicrofacetBsdf *bsdf, const float3 wi, ccl_private FresnelConductor *fresnel, const bool preserve_energy)
ccl_device void bsdf_microfacet_setup_fresnel_generalized_schlick(KernelGlobals kg, ccl_private MicrofacetBsdf *bsdf, const float3 wi, ccl_private FresnelGeneralizedSchlick *fresnel, const bool preserve_energy)
ccl_device Spectrum bsdf_coat_setup(KernelGlobals kg, ccl_private ShaderData *sd, const PathRayVisibility ray_visibility, ccl_private Coat &coat)
ccl_device int bsdf_microfacet_beckmann_glass_setup(ccl_private MicrofacetBsdf *bsdf)
ccl_device int bsdf_microfacet_beckmann_setup(ccl_private MicrofacetBsdf *bsdf)
ccl_device_forceinline FresnelGeneralizedSchlick generalized_schlick_setup(const float ior, const bool reflective_caustics, const bool refractive_caustics, const Spectrum reflection_tint, const Spectrum transmission_tint, FresnelThinFilm thinfilm)
ccl_device void bsdf_microfacet_setup_fresnel_f82_tint(KernelGlobals kg, ccl_private MicrofacetBsdf *bsdf, const float3 wi, ccl_private FresnelF82Tint *fresnel, const Spectrum f82_tint, const bool preserve_energy)
ccl_device void bsdf_microfacet_setup_fresnel_constant(KernelGlobals kg, ccl_private MicrofacetBsdf *bsdf, const float3 wi, const Spectrum color)
ccl_device FresnelCoeff bsdf_thin_glass_setup(KernelGlobals kg, ccl_private ShaderData *sd, const bool reflective, const bool refractive, const FresnelCoeff tint, const Spectrum weight, const float3 N, const float alpha, const float ior, const FresnelThinFilm thinfilm, const PathRayVisibility ray_visibility, const uint32_t path_flag)
ccl_device int bsdf_microfacet_beckmann_refraction_setup(ccl_private MicrofacetBsdf *bsdf)
ccl_device_inline float bsdf_glass_ior(ccl_private ShaderData *sd, float ior, const float inv_abbe)
ccl_device_inline void adjust_thin_film_ior_at_backface(ccl_private float &film_ior, const float inv_bulk_ior)
ccl_device int bsdf_microfacet_ggx_glass_setup(ccl_private MicrofacetBsdf *bsdf)
ccl_device int bsdf_microfacet_ggx_setup(ccl_private MicrofacetBsdf *bsdf)
ccl_device int bsdf_microfacet_ggx_refraction_setup(ccl_private MicrofacetBsdf *bsdf)
ccl_device void bsdf_oren_nayar_setup(ccl_private ShaderData *sd, const float3 N, const Spectrum weight, const float roughness, const Spectrum color)
ccl_device_forceinline bool diffuse_roughness_is_almost_zero(const float alpha)
ccl_device_inline void bsdf_thin_subsurface_setup(ccl_private ShaderData *sd, const float3 N, const Spectrum weight, const float anisotropy, const float roughness, const Spectrum color)
ccl_device void bsdf_ray_portal_setup(ccl_private ShaderData *sd, const Spectrum weight, const float3 position, float3 direction)
ccl_device Spectrum bsdf_sheen_setup(KernelGlobals kg, ccl_private ShaderData *sd, const Spectrum weight, const float3 N, const float roughness)
Definition bsdf_sheen.h:30
ccl_device int bsdf_diffuse_toon_setup(ccl_private ToonBsdf *bsdf)
Definition bsdf_toon.h:35
ccl_device int bsdf_glossy_toon_setup(ccl_private ToonBsdf *bsdf)
Definition bsdf_toon.h:119
CCL_NAMESPACE_BEGIN ccl_device void bsdf_transparent_setup(ccl_private ShaderData *sd, const Spectrum weight, const uint32_t path_flag)
ccl_device float3 maybe_ensure_valid_specular_reflection(ccl_private ShaderData *sd, const float3 N)
Definition bsdf_util.h:399
ccl_device_inline Spectrum bsdf_principled_hair_sigma_from_reflectance(const Spectrum color, const float azimuthal_roughness)
Definition bsdf_util.h:420
ccl_device_inline Spectrum bsdf_principled_hair_sigma_from_concentration(const float eumelanin, const float pheomelanin)
Definition bsdf_util.h:427
ccl_device_inline Spectrum closure_layering_weight(const Spectrum layer_albedo, const Spectrum weight)
Definition bsdf_util.h:440
ccl_device_inline ccl_private Bssrdf * bssrdf_alloc(ccl_private ShaderData *sd, Spectrum weight)
Definition bssrdf.h:270
ccl_device int bssrdf_setup(ccl_private ShaderData *sd, ccl_private Bssrdf *bssrdf, const uint32_t path_flag, ClosureType type)
Definition bssrdf.h:288
btScalar max
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
ccl_device_noinline void svm_node_mix_closure(ccl_private float *ccl_restrict stack, const ccl_global SVMNodeMixClosure &ccl_restrict node)
Definition closure.h:1460
ccl_device void svm_node_closure_weight(ccl_private float *ccl_restrict stack, ccl_private Spectrum *closure_weight, const ccl_global SVMNodeClosureWeight &ccl_restrict node)
Definition closure.h:1445
ccl_device_noinline void svm_node_volume_coefficients(KernelGlobals kg, ccl_private ShaderData *sd, ccl_private float *ccl_restrict stack, Spectrum scatter_coeffs, const ccl_global SVMNodeVolumeCoefficients &ccl_restrict node, const PathRayVisibility path_visibility, const uint32_t path_flag)
Definition closure.h:1207
ccl_device_noinline void svm_node_closure_background(ccl_private ShaderData *sd, ccl_private float *ccl_restrict stack, Spectrum closure_weight, const ccl_global SVMNodeClosureBackground &ccl_restrict node)
Definition closure.h:1393
ccl_device_noinline void svm_node_closure_emission(KernelGlobals kg, ccl_private ShaderData *sd, ccl_private float *ccl_restrict stack, Spectrum closure_weight, const ccl_global SVMNodeClosureEmission &ccl_restrict node, const PathRayVisibility path_visibility, const uint32_t path_flag)
Definition closure.h:1361
ccl_device_inline void svm_alloc_closure_volume_scatter(ccl_private ShaderData *sd, ccl_private float *stack, Spectrum weight, const uint type, const SVMInputFloat param1, const SVMInputFloat param_extra)
Definition closure.h:1094
ccl_device_noinline void svm_node_principled_volume(KernelGlobals kg, ccl_private ShaderData *sd, ccl_private float *ccl_restrict stack, const Spectrum closure_weight, const ccl_global SVMNodePrincipledVolume &ccl_restrict node, const PathRayVisibility path_visibility, const uint32_t path_flag)
Definition closure.h:1255
ccl_device_noinline void svm_node_closure_holdout(ccl_private ShaderData *sd, ccl_private float *ccl_restrict stack, Spectrum closure_weight, const ccl_global SVMNodeClosureHoldout &ccl_restrict node)
Definition closure.h:1414
ccl_device_inline Spectrum principled_bsdf_emission(KernelGlobals kg, ccl_private ShaderData *sd, ccl_private float *ccl_restrict stack, const ccl_global SVMNodePrincipledBsdfData &data, const float3 N, const PathRayVisibility ray_visibility, const uint32_t path_flag, const float mix_weight)
Definition closure.h:96
ccl_device_noinline void svm_node_closure_volume(KernelGlobals kg, ccl_private ShaderData *sd, ccl_private float *ccl_restrict stack, Spectrum closure_weight, const ccl_global SVMNodeClosureVolume &ccl_restrict node)
Definition closure.h:1165
ccl_device_noinline int svm_node_closure_bsdf(KernelGlobals kg, ccl_private ShaderData *sd, ccl_private float *ccl_restrict stack, Spectrum closure_weight, const ccl_global SVMNodeClosureBsdf &ccl_restrict node, const PathRayVisibility ray_visibility, const uint32_t path_flag, int offset)
Definition closure.h:165
CCL_NAMESPACE_BEGIN ccl_device_inline int svm_node_closure_bsdf_skip(int offset, const uint type)
Definition closure.h:29
ccl_device void svm_node_closure_set_weight(ccl_private Spectrum *closure_weight, const ccl_global SVMNodeClosureSetWeight &ccl_restrict node)
Definition closure.h:1438
ccl_device void svm_node_emission_weight(ccl_private float *ccl_restrict stack, ccl_private Spectrum *closure_weight, const ccl_global SVMNodeEmissionWeight &ccl_restrict node)
Definition closure.h:1452
ccl_device void svm_node_set_normal(ccl_private ShaderData *sd, ccl_private float *ccl_restrict stack, const ccl_global SVMNodeClosureSetNormal &ccl_restrict node)
Definition closure.h:1480
nullptr thinfilm_thickness
ccl_device_inline float3 stack_load_float3_default(const ccl_private float *stack, const uint a, const float3 value)
ccl_device_inline void stack_store_float(ccl_private float *stack, const uint a, const float f)
ccl_device_inline float3 stack_load_float3(const ccl_private float *stack, const uint a)
ccl_device_inline void stack_store_float3(ccl_private float *stack, const uint a, const float3 f)
CCL_NAMESPACE_BEGIN ccl_device_inline float stack_load_float(const ccl_private float *stack, const uint a)
ccl_device_inline const ccl_global T & svm_node_get(KernelGlobals kg, ccl_private int *const offset)
ccl_device_inline bool stack_valid(const uint a)
ccl_device_inline T stack_load(const ccl_private float *stack, const uint a)
ccl_device_inline float stack_load_float_default(const ccl_private float *stack, const uint a, const float value)
#define kernel_assert(cond)
#define CLOSURE_IS_VOLUME_SCATTER(type)
#define kernel_data
#define THINFILM_THICKNESS_CUTOFF
#define ccl_restrict
#define IF_KERNEL_NODES_FEATURE(feature)
#define kernel_data_fetch(name, index)
#define one_spectrum
#define PRIMITIVE_UNPACK_SEGMENT(type)
#define zero_spectrum
#define make_spectrum(f)
#define ccl_private
#define ccl_device_inline
#define ccl_global
#define ccl_device_noinline
#define logf(x)
#define CCL_NAMESPACE_END
#define saturatef(x)
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
#define CLOSURE_WEIGHT_CUTOFF
float F0_from_ior(float eta)
ccl_device void emission_setup(ccl_private ShaderData *sd, const Spectrum weight)
Definition emissive.h:29
CCL_NAMESPACE_BEGIN ccl_device void background_setup(ccl_private ShaderData *sd, const Spectrum weight)
Definition emissive.h:16
#define position
VecBase< float, D > normalize(VecOp< float, D >) RET
#define sqrt
constexpr T clamp(T, U, U) RET
float ior_from_F0(const float F0)
float3 rotate_around_axis(float3 p, float3 axis, float angle)
float3x3 scale(float3x3 mat, float2 scale)
bool is_zero(float3x3 a)
float safe_divide(float a, float b)
VecT safe_normalize(VecT vector)
CCL_NAMESPACE_BEGIN ccl_device void volume_extinction_setup(ccl_private ShaderData *sd, Spectrum weight)
CCL_NAMESPACE_BEGIN ccl_device_forceinline bool is_attribute_found(const ccl_private AttributeDescriptor &desc)
ccl_device_inline AttributeDescriptor find_attribute(const ccl_global AttributeMap *attributes_map, uint attr_offset, const int prim, const uint64_t id)
ccl_device_inline float object_volume_density(KernelGlobals kg, const int object)
ClosureType
@ CLOSURE_VOLUME_RAYLEIGH_ID
@ CLOSURE_BSDF_MICROFACET_GGX_REFRACTION_ID
@ CLOSURE_VOLUME_HENYEY_GREENSTEIN_ID
@ CLOSURE_VOLUME_MIE_ID
@ CLOSURE_BSDF_ASHIKHMIN_SHIRLEY_ID
@ CLOSURE_BSDF_MICROFACET_GGX_GLASS_ID
@ CLOSURE_BSDF_DIFFUSE_ID
@ CLOSURE_BSSRDF_BURLEY_ID
@ CLOSURE_BSDF_PRINCIPLED_ID
@ CLOSURE_BSDF_SHEEN_ID
@ CLOSURE_BSDF_TRANSPARENT_ID
@ CLOSURE_BSDF_DIFFUSE_TOON_ID
@ CLOSURE_VOLUME_DRAINE_ID
@ CLOSURE_BSDF_MICROFACET_GGX_ID
@ CLOSURE_BSDF_MICROFACET_BECKMANN_REFRACTION_ID
@ CLOSURE_BSDF_MICROFACET_MULTI_GGX_ID
@ CLOSURE_BSDF_HAIR_TRANSMISSION_ID
@ CLOSURE_BSDF_BURLEY_ID
@ CLOSURE_BSDF_MICROFACET_BECKMANN_GLASS_ID
@ CLOSURE_VOLUME_FOURNIER_FORAND_ID
@ CLOSURE_BSSRDF_RANDOM_WALK_ID
@ CLOSURE_BSDF_PHYSICAL_CONDUCTOR
@ CLOSURE_BSDF_HAIR_HUANG_ID
@ CLOSURE_BSDF_MICROFACET_BECKMANN_ID
@ CLOSURE_BSDF_MICROFACET_MULTI_GGX_GLASS_ID
@ CLOSURE_BSDF_F82_CONDUCTOR
@ CLOSURE_BSSRDF_RANDOM_WALK_LEGACY_ID
@ CLOSURE_BSDF_RAY_PORTAL_ID
@ CLOSURE_HOLDOUT_ID
@ CLOSURE_BSDF_OREN_NAYAR_ID
@ CLOSURE_BSDF_GLOSSY_TOON_ID
@ CLOSURE_BSDF_HAIR_CHIANG_ID
@ CLOSURE_VOLUME_ABSORPTION_ID
@ CLOSURE_BSDF_HAIR_REFLECTION_ID
@ CLOSURE_BSSRDF_RANDOM_WALK_SKIN_ID
@ CLOSURE_BSDF_TRANSLUCENT_ID
@ CLOSURE_BSDF_ASHIKHMIN_VELVET_ID
@ SHADER_TYPE_SURFACE
@ SHADER_TYPE_VOLUME
@ NODE_PRINCIPLED_HAIR_REFLECTANCE
@ NODE_PRINCIPLED_HAIR_DIRECT_ABSORPTION
@ NODE_PRINCIPLED_HAIR_PIGMENT_CONCENTRATION
uint32_t PathRayVisibility
@ SR_BACKFACING
@ SR_IS_VOLUME_SHADER_EVAL
@ SR_HOLDOUT
@ PRIMITIVE_CURVE
@ PATH_RAY_EXTINCTION
@ PATH_RAY_VISIBILITY_CAMERA
@ PATH_RAY_VISIBILITY_SHADOW
@ PATH_RAY_VISIBILITY_DIFFUSE
ccl_device float3 rec709_to_rgb(KernelGlobals kg, const float3 rec709)
ccl_device_inline Spectrum rgb_to_spectrum(const float3 rgb)
ccl_device_inline float3 safe_normalize_fallback(const float3 a, const float3 fallback)
CCL_NAMESPACE_BEGIN ccl_device_inline float3 zero_float3()
Definition math_float3.h:17
ccl_device float3 svm_math_blackbody_color_rec709(const float t)
Definition math_util.h:211
#define N
#define T
#define B
#define R
#define T4
Definition md5.cpp:23
CCL_NAMESPACE_BEGIN ccl_device_forceinline T primitive_surface_attribute(KernelGlobals kg, const ccl_private ShaderData *sd, const AttributeDescriptor desc)
Definition primitive.h:32
#define mix
#define sqr
#define fabsf
#define sqrtf
#define ccl_device
#define M_2PI_F
#define fmaxf
#define M_PI_F
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
#define saturate(a)
Definition smaa.cc:319
#define min(a, b)
Definition sort.cc:38
closure color absorption() BUILTIN
closure color bssrdf(string method, normal N, vector radius, color albedo) BUILTIN
closure color draine(float g, float alpha) BUILTIN
float3 Spectrum
ccl_device int volume_draine_setup(ccl_private DraineVolume *volume)
ccl_device int volume_fournier_forand_setup(ccl_private FournierForandVolume *volume, float B, float IOR)
ccl_device int volume_henyey_greenstein_setup(ccl_private HenyeyGreensteinVolume *volume)
ccl_device int volume_rayleigh_setup(ccl_private RayleighVolume *volume)
ccl_device void phase_mie_fitted_parameters(float d, ccl_private float *g_HG, ccl_private float *g_D, ccl_private float *alpha, ccl_private float *w)