Blender V5.3
surface_shader.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/* Functions to evaluate shaders. */
6
7#pragma once
8
11
13
16
17#ifdef __SVM__
18# include "kernel/svm/svm.h"
19#endif
20#ifdef __OSL__
21# include "kernel/osl/osl.h"
22#endif
23
25
26/* Guiding */
27
28#if defined(__PATH_GUIDING__)
29
30ccl_device float surface_shader_average_sample_weight_squared_roughness(
31 const ccl_private ShaderData *sd)
32{
33 float avg_squared_roughness = 0.0f;
34 float sum_sample_weight = 0.0f;
35 for (int i = 0; i < sd->num_closure; i++) {
36 const ccl_private ShaderClosure *sc = &sd->closure[i];
37
38 if (!CLOSURE_IS_BSDF_OR_BSSRDF(sc->type)) {
39 continue;
40 }
41 avg_squared_roughness += sc->sample_weight * bsdf_get_specular_roughness_squared(sc);
42 sum_sample_weight += sc->sample_weight;
43 }
44
45 avg_squared_roughness = avg_squared_roughness > 0.0f ?
46 avg_squared_roughness / sum_sample_weight :
47 0.0f;
48 return avg_squared_roughness;
49}
50
51ccl_device_inline void surface_shader_prepare_guiding(KernelGlobals kg,
53 ccl_private ShaderData *sd,
54 const ccl_private RNGState *rng_state)
55{
56 /* Have any BSDF to guide? */
57 if (!(kernel_data.integrator.use_surface_guiding && (sd->runtime_flag & SR_BSDF_HAS_EVAL))) {
58 INTEGRATOR_STATE_WRITE(state, guiding, use_surface_guiding) = false;
59 return;
60 }
61
62 const float surface_guiding_probability = kernel_data.integrator.surface_guiding_probability;
63 const int guiding_directional_sampling_type =
64 kernel_data.integrator.guiding_directional_sampling_type;
65 const float guiding_roughness_threshold = kernel_data.integrator.guiding_roughness_threshold;
66 float rand_bsdf_guiding = path_state_rng_1D(kg, rng_state, PRNG_SURFACE_BSDF_GUIDING);
67
68 /* Compute proportion of diffuse BSDF and BSSRDFs. */
69 float diffuse_sampling_fraction = 0.0f;
70 float bssrdf_sampling_fraction = 0.0f;
71 float bsdf_bssrdf_sampling_sum = 0.0f;
72
73 bool fully_opaque = true;
74
75 for (int i = 0; i < sd->num_closure; i++) {
76 ShaderClosure *sc = &sd->closure[i];
77 if (CLOSURE_IS_BSDF_OR_BSSRDF(sc->type)) {
78 const float sweight = sc->sample_weight;
79 kernel_assert(sweight >= 0.0f);
80
81 bsdf_bssrdf_sampling_sum += sweight;
82 if (CLOSURE_IS_BSDF_DIFFUSE(sc->type) && sc->type < CLOSURE_BSDF_TRANSLUCENT_ID) {
83 diffuse_sampling_fraction += sweight;
84 }
85 if (CLOSURE_IS_BSSRDF(sc->type)) {
86 bssrdf_sampling_fraction += sweight;
87 }
88
90 fully_opaque = false;
91 }
92 }
93 }
94
95 if (bsdf_bssrdf_sampling_sum > 0.0f) {
96 diffuse_sampling_fraction /= bsdf_bssrdf_sampling_sum;
97 bssrdf_sampling_fraction /= bsdf_bssrdf_sampling_sum;
98 }
99
100 /* Initial guiding */
101 /* The roughness because the function returns `alpha.x * alpha.y`.
102 * In addition alpha is squared again. */
103 float avg_roughness = surface_shader_average_sample_weight_squared_roughness(sd);
104 avg_roughness = safe_sqrtf(avg_roughness);
105 if (!fully_opaque || avg_roughness < guiding_roughness_threshold ||
106 ((guiding_directional_sampling_type == GUIDING_DIRECTIONAL_SAMPLING_TYPE_PRODUCT_MIS) &&
107 (diffuse_sampling_fraction <= 0.0f)) ||
108 !guiding_bsdf_init(kg, sd->P, sd->N, rand_bsdf_guiding))
109 {
110 INTEGRATOR_STATE_WRITE(state, guiding, use_surface_guiding) = false;
112 return;
113 }
114
115 INTEGRATOR_STATE_WRITE(state, guiding, use_surface_guiding) = true;
116 if (kernel_data.integrator.guiding_directional_sampling_type ==
118 {
120 state, guiding, surface_guiding_sampling_prob) = surface_guiding_probability *
121 diffuse_sampling_fraction;
122 }
123 else if (kernel_data.integrator.guiding_directional_sampling_type ==
125 {
127 state, guiding, surface_guiding_sampling_prob) = surface_guiding_probability;
128 }
129 else { // GUIDING_DIRECTIONAL_SAMPLING_TYPE_ROUGHNESS
131 state, guiding, surface_guiding_sampling_prob) = surface_guiding_probability *
132 avg_roughness;
133 }
134 INTEGRATOR_STATE_WRITE(state, guiding, bssrdf_sampling_prob) = bssrdf_sampling_fraction;
135 INTEGRATOR_STATE_WRITE(state, guiding, sample_surface_guiding_rand) = rand_bsdf_guiding;
136
139}
140#endif
141
144 ccl_private ShaderData *sd,
145 const PathRayVisibility path_visibility)
146{
147 /* Filter out closures. */
148 if (kernel_data.integrator.filter_closures) {
149 const int filter_closures = kernel_data.integrator.filter_closures;
150 if (filter_closures & FILTER_CLOSURE_EMISSION) {
151 sd->closure_emission_background = zero_spectrum();
152 }
153
154 if (path_visibility & PATH_RAY_VISIBILITY_CAMERA) {
155 if (filter_closures & FILTER_CLOSURE_DIRECT_LIGHT) {
156 sd->runtime_flag &= ~SR_BSDF_HAS_EVAL;
157 }
158 bool has_bsdf_closure = false;
159 for (int i = 0; i < sd->num_closure; i++) {
160 ccl_private ShaderClosure *sc = &sd->closure[i];
161
162 const bool filter_diffuse = (filter_closures & FILTER_CLOSURE_DIFFUSE);
163 const bool filter_glossy = (filter_closures & FILTER_CLOSURE_GLOSSY);
164 const bool filter_transmission = (filter_closures & FILTER_CLOSURE_TRANSMISSION);
165 const bool filter_glass = filter_glossy && filter_transmission;
166 if ((CLOSURE_IS_BSDF_DIFFUSE(sc->type) && filter_diffuse) ||
167 (CLOSURE_IS_BSDF_GLOSSY(sc->type) && filter_glossy) ||
168 (CLOSURE_IS_BSDF_TRANSMISSION(sc->type) && filter_transmission) ||
169 (CLOSURE_IS_GLASS(sc->type) && filter_glass))
170 {
171 sc->type = CLOSURE_NONE_ID;
172 sc->sample_weight = 0.0f;
173 }
174 else if ((CLOSURE_IS_BSDF_TRANSPARENT(sc->type) &&
175 (filter_closures & FILTER_CLOSURE_TRANSPARENT)))
176 {
177 sc->type = CLOSURE_HOLDOUT_ID;
178 sc->sample_weight = 0.0f;
179 sd->runtime_flag |= SR_HOLDOUT;
180 }
181 else if (CLOSURE_IS_BSDF(sc->type)) {
182 has_bsdf_closure = true;
183 }
184 }
185 if (!has_bsdf_closure) {
186 sd->runtime_flag &= ~SR_BSDF;
187 }
188 }
189 }
190
191 /* Filter glossy.
192 *
193 * Blurring of bsdf after bounces, for rays that have a small likelihood
194 * of following this particular path (diffuse, rough glossy) */
195 if (kernel_data.integrator.filter_glossy != FLT_MAX
196#ifdef __MNEE__
197 && !(INTEGRATOR_STATE(state, path, mnee) & PATH_MNEE_VALID)
198#endif
199 )
200 {
201 const float blur_pdf = kernel_data.integrator.filter_glossy *
202 INTEGRATOR_STATE(state, path, min_ray_pdf);
203
204 if (blur_pdf < 1.0f) {
205 const float blur_roughness = sqrtf(1.0f - blur_pdf) * 0.5f;
206
207 for (int i = 0; i < sd->num_closure; i++) {
208 ccl_private ShaderClosure *sc = &sd->closure[i];
209 if (CLOSURE_IS_BSDF(sc->type)) {
210 bsdf_blur(sc, blur_roughness);
211 }
212 }
213
214 /* NOTE: this is a sufficient condition. If `blur_roughness < THRESH < original_roughness`
215 * then the flag was already set. */
216 if (!alpha_is_almost_specular(blur_roughness, blur_roughness)) {
217 sd->runtime_flag |= SR_BSDF_HAS_EVAL;
218 }
219 }
220 }
221}
222
223/* BSDF */
224#ifdef WITH_CYCLES_DEBUG
225ccl_device_inline void surface_shader_validate_bsdf_sample(const KernelGlobals kg,
226 const ccl_private ShaderClosure *sc,
227 const float3 wo,
228 const int org_label,
229 const float2 org_roughness,
230 const float org_eta)
231{
232 /* Validate the #bsdf_label and #bsdf_roughness_eta functions
233 * by estimating the values after a BSDF sample. */
234 kernel_assert(org_label == bsdf_label(kg, sc, wo));
235 (void)kg;
236
237 float2 comp_roughness;
238 float comp_eta;
239 bsdf_roughness_eta(sc, wo, &comp_roughness, &comp_eta);
240 kernel_assert(org_eta == comp_eta);
241 kernel_assert(org_roughness.x == comp_roughness.x);
242 kernel_assert(org_roughness.y == comp_roughness.y);
243}
244#endif
245
247 const uint light_shader_flags)
248{
249 if (!(light_shader_flags & SHADER_EXCLUDE_ANY)) {
250 return false;
251 }
252 if (light_shader_flags & SHADER_EXCLUDE_DIFFUSE) {
253 if (CLOSURE_IS_BSDF_DIFFUSE(type)) {
254 return true;
255 }
256 }
257 if (light_shader_flags & SHADER_EXCLUDE_GLOSSY) {
258 if (CLOSURE_IS_BSDF_GLOSSY(type)) {
259 return true;
260 }
261 }
262 if (light_shader_flags & SHADER_EXCLUDE_TRANSMIT) {
264 return true;
265 }
266 }
267 /* Glass closures are both glossy and transmissive, so only exclude them if both are filtered. */
268 const uint exclude_glass = SHADER_EXCLUDE_TRANSMIT | SHADER_EXCLUDE_GLOSSY;
269 if ((light_shader_flags & exclude_glass) == exclude_glass) {
270 if (CLOSURE_IS_GLASS(type)) {
271 return true;
272 }
273 }
274 return false;
275}
276
278 ccl_private ShaderData *sd,
279 const float3 wo,
280 const ccl_private ShaderClosure *skip_sc,
281 ccl_private BsdfEval *result_eval,
282 float sum_pdf,
283 float sum_sample_weight,
284 const uint light_shader_flags,
285 float sum_pdf_roughness_squared,
286 ccl_private float &r_avg_roughness_squared)
287{
288 /* This is the veach one-sample model with balance heuristic,
289 * some PDF factors drop out when using balance heuristic weighting. */
290 for (int i = 0; i < sd->num_closure; i++) {
291 const ccl_private ShaderClosure *sc = &sd->closure[i];
292
293 if (sc == skip_sc) {
294 continue;
295 }
296
297 if (CLOSURE_IS_BSDF_OR_BSSRDF(sc->type)) {
298 if (CLOSURE_IS_BSDF(sc->type)) {
299 float bsdf_pdf = 0.0f;
300 const Spectrum eval = bsdf_eval(kg, sd, sc, wo, &bsdf_pdf);
301
302 if (bsdf_pdf != 0.0f) {
303 if (!_surface_shader_exclude(sc->type, light_shader_flags)) {
304 bsdf_eval_accum(result_eval, sc, wo, eval * sc->weight);
305 }
306
307 const float weight = bsdf_pdf * sc->sample_weight;
308 sum_pdf += weight;
309
310 /* See #bsdf_widen_dD for the motivation for this weighted average roughness. */
311 sum_pdf_roughness_squared += weight * bsdf_get_specular_roughness_squared(sc);
312 }
313 }
314
315 sum_sample_weight += sc->sample_weight;
316 }
317 }
318
319 r_avg_roughness_squared = (sum_pdf > 0.0f) ? sum_pdf_roughness_squared / sum_pdf : 0.0f;
320
321 return (sum_sample_weight > 0.0f) ? sum_pdf / sum_sample_weight : 0.0f;
322}
323
325 ccl_private ShaderData *sd,
326 const float3 wo,
327 ccl_private BsdfEval *result_eval,
328 ccl_private float *pdfs,
329 const uint light_shader_flags,
330 ccl_private float &r_avg_roughness_squared)
331{
332 /* This is the veach one-sample model with balance heuristic, some pdf
333 * factors drop out when using balance heuristic weighting. */
334 float sum_pdf = 0.0f;
335 float sum_sample_weight = 0.0f;
336 float sum_pdf_roughness_squared = 0.0f;
337 bsdf_eval_init(result_eval, zero_spectrum());
338 for (int i = 0; i < sd->num_closure; i++) {
339 const ccl_private ShaderClosure *sc = &sd->closure[i];
340
341 if (CLOSURE_IS_BSDF_OR_BSSRDF(sc->type)) {
342 if (CLOSURE_IS_BSDF(sc->type)) {
343 float bsdf_pdf = 0.0f;
344 const Spectrum eval = bsdf_eval(kg, sd, sc, wo, &bsdf_pdf);
345 kernel_assert(bsdf_pdf >= 0.0f);
346 if (bsdf_pdf != 0.0f) {
347 if (!_surface_shader_exclude(sc->type, light_shader_flags)) {
348 bsdf_eval_accum(result_eval, sc, wo, eval * sc->weight);
349 }
350
351 const float weight = bsdf_pdf * sc->sample_weight;
352 sum_pdf += weight;
353 kernel_assert(weight >= 0.0f);
354 pdfs[i] = weight;
355
356 /* See #bsdf_widen_dD for the motivation for this weighted average roughness. */
357 sum_pdf_roughness_squared += weight * bsdf_get_specular_roughness_squared(sc);
358 }
359 else {
360 pdfs[i] = 0.0f;
361 }
362 }
363 else {
364 pdfs[i] = 0.0f;
365 }
366
367 sum_sample_weight += sc->sample_weight;
368 }
369 else {
370 pdfs[i] = 0.0f;
371 }
372 }
373 if (sum_pdf > 0.0f) {
374 for (int i = 0; i < sd->num_closure; i++) {
375 pdfs[i] /= sum_pdf;
376 }
377 }
378
379 r_avg_roughness_squared = (sum_pdf > 0.0f) ? sum_pdf_roughness_squared / sum_pdf : 0.0f;
380
381 return (sum_sample_weight > 0.0f) ? sum_pdf / sum_sample_weight : 0.0f;
382}
383
384#ifndef __KERNEL_CUDA__
386#else
388#endif
391 ccl_private ShaderData *sd,
392 const float3 wo,
394 const uint light_shader_flags,
395 ccl_private float &r_avg_roughness_squared)
396{
398
399 float pdf = _surface_shader_bsdf_eval_mis(kg,
400 sd,
401 wo,
402 nullptr,
403 bsdf_eval,
404 0.0f,
405 0.0f,
406 light_shader_flags,
407 0.0f,
408 r_avg_roughness_squared);
409
410 /* If the light does not use MIS, then it is only sampled via NEE, so the probability of hitting
411 * the light using BSDF sampling is zero. */
412 if (!(light_shader_flags & SHADER_USE_MIS)) {
413 pdf = 0.0f;
414 }
415
416#if defined(__PATH_GUIDING__) && PATH_GUIDING_LEVEL >= 4
417 if ((kernel_data.kernel_features & KERNEL_FEATURE_PATH_GUIDING)) {
418 if (pdf > 0.0f && INTEGRATOR_STATE(state, guiding, use_surface_guiding)) {
419 const float guiding_sampling_prob = INTEGRATOR_STATE(
421 const float bssrdf_sampling_prob = INTEGRATOR_STATE(state, guiding, bssrdf_sampling_prob);
422 const float guide_pdf = guiding_bsdf_pdf(kg, wo);
423
424 if (kernel_data.integrator.guiding_directional_sampling_type ==
426 {
427 pdf = (0.5f * guide_pdf * (1.0f - bssrdf_sampling_prob)) + 0.5f * pdf;
428 }
429 else {
430 pdf = (guiding_sampling_prob * guide_pdf * (1.0f - bssrdf_sampling_prob)) +
431 (1.0f - guiding_sampling_prob) * pdf;
432 }
433 }
434 }
435#endif
436
437 return pdf;
438}
439
440/* Randomly sample a BSSRDF or BSDF proportional to ShaderClosure.sample_weight. */
442 const ccl_private ShaderData *ccl_restrict sd, ccl_private float3 *rand_bsdf)
443{
444 int sampled = 0;
445
446 if (sd->num_closure > 1) {
447 /* Pick a BSDF or based on sample weights. */
448 float sum = 0.0f;
449
450 for (int i = 0; i < sd->num_closure; i++) {
451 const ccl_private ShaderClosure *sc = &sd->closure[i];
452
453 if (CLOSURE_IS_BSDF_OR_BSSRDF(sc->type)) {
454 sum += sc->sample_weight;
455 }
456 }
457
458 const float r = (*rand_bsdf).z * sum;
459 float partial_sum = 0.0f;
460
461 for (int i = 0; i < sd->num_closure; i++) {
462 const ccl_private ShaderClosure *sc = &sd->closure[i];
463
464 if (CLOSURE_IS_BSDF_OR_BSSRDF(sc->type)) {
465 const float next_sum = partial_sum + sc->sample_weight;
466
467 if (r < next_sum) {
468 sampled = i;
469
470 /* Rescale to reuse for direction sample, to better preserve stratification. */
471 (*rand_bsdf).z = (r - partial_sum) / sc->sample_weight;
472 break;
473 }
474
475 partial_sum = next_sum;
476 }
477 }
478 }
479
480 return &sd->closure[sampled];
481}
482
483/* Return weight for picked BSSRDF. */
486 const ccl_private ShaderClosure *ccl_restrict bssrdf_sc)
487{
488 Spectrum weight = bssrdf_sc->weight;
489
490 if (sd->num_closure > 1) {
491 float sum = 0.0f;
492 for (int i = 0; i < sd->num_closure; i++) {
493 const ccl_private ShaderClosure *sc = &sd->closure[i];
494
495 if (CLOSURE_IS_BSDF_OR_BSSRDF(sc->type)) {
496 sum += sc->sample_weight;
497 }
498 }
499 weight *= sum / bssrdf_sc->sample_weight;
500 }
501
502 return weight;
503}
504
505#if defined(__PATH_GUIDING__)
506/* Sample direction for picked BSDF, and return evaluation and pdf for all
507 * BSDFs combined using MIS. */
508
509ccl_device int surface_shader_bsdf_guided_sample_closure_mis(
510 KernelGlobals kg,
512 ccl_private ShaderData *sd,
513 const ccl_private ShaderClosure *sc,
514 const float3 rand_bsdf,
517 ccl_private float *bsdf_pdf,
518 ccl_private float *unguided_bsdf_pdf,
519 ccl_private float2 *sampled_roughness,
520 ccl_private float *eta,
521 ccl_private float &r_avg_roughness_squared)
522{
523 /* BSSRDF should already have been handled elsewhere. */
525
526 r_avg_roughness_squared = 0.0f;
527
528 const bool use_surface_guiding = INTEGRATOR_STATE(state, guiding, use_surface_guiding);
529 const float guiding_sampling_prob = INTEGRATOR_STATE(
531 const float bssrdf_sampling_prob = INTEGRATOR_STATE(state, guiding, bssrdf_sampling_prob);
532
533 /* Decide between sampling guiding distribution and BSDF. */
534 bool sample_guiding = false;
535 float rand_bsdf_guiding = INTEGRATOR_STATE(state, guiding, sample_surface_guiding_rand);
536
537 if (use_surface_guiding && rand_bsdf_guiding < guiding_sampling_prob) {
538 sample_guiding = true;
539 rand_bsdf_guiding /= guiding_sampling_prob;
540 }
541 else {
542 rand_bsdf_guiding -= guiding_sampling_prob;
543 rand_bsdf_guiding /= (1.0f - guiding_sampling_prob);
544 }
545
546 /* Initialize to zero. */
547 int label = LABEL_NONE;
550
551 *unguided_bsdf_pdf = 0.0f;
552 float guide_pdf = 0.0f;
553
554 if (sample_guiding) {
555 /* Sample guiding distribution. */
556 guide_pdf = guiding_bsdf_sample(kg, make_float2(rand_bsdf), wo);
557 *bsdf_pdf = 0.0f;
558
559 if (guide_pdf != 0.0f) {
560 float unguided_bsdf_pdfs[MAX_CLOSURE];
561
562 *unguided_bsdf_pdf = surface_shader_bsdf_eval_pdfs(
563 kg, sd, *wo, bsdf_eval, unguided_bsdf_pdfs, 0, r_avg_roughness_squared);
564 *bsdf_pdf = (guiding_sampling_prob * guide_pdf * (1.0f - bssrdf_sampling_prob)) +
565 ((1.0f - guiding_sampling_prob) * (*unguided_bsdf_pdf));
566 float sum_pdfs = 0.0f;
567
568 if (*unguided_bsdf_pdf > 0.0f) {
569 int idx = -1;
570 for (int i = 0; i < sd->num_closure; i++) {
571 sum_pdfs += unguided_bsdf_pdfs[i];
572 if (rand_bsdf_guiding <= sum_pdfs) {
573 idx = i;
574 break;
575 }
576 }
577
578 kernel_assert(idx >= 0);
579 /* Set the default idx to the last in the list.
580 * in case of numerical problems and rand_bsdf_guiding is just >=1.0f and
581 * the sum of all unguided_bsdf_pdfs is just < 1.0f. */
582 idx = (rand_bsdf_guiding > sum_pdfs) ? sd->num_closure - 1 : idx;
583
584 label = bsdf_label(kg, &sd->closure[idx], *wo);
585 }
586 else {
587 *bsdf_pdf = 0.0f;
588 *unguided_bsdf_pdf = 0.0f;
589 }
590 }
591
593
594 *sampled_roughness = make_float2(1.0f, 1.0f);
595 *eta = 1.0f;
596 }
597 else {
598 /* Sample BSDF. */
599 *bsdf_pdf = 0.0f;
600 label = bsdf_sample(
601 kg, sd, sc, rand_bsdf, &eval, wo, unguided_bsdf_pdf, sampled_roughness, eta);
602# ifdef WITH_CYCLES_DEBUG
603 /* Code path to validate the estimation of the label, sampled roughness and eta. This should be
604 * activated from time to time when the BSDFs change to check if everything is still working
605 * correctly. */
606 if (*unguided_bsdf_pdf > 0.0f) {
607 surface_shader_validate_bsdf_sample(kg, sc, *wo, label, *sampled_roughness, *eta);
608 }
609# endif
610
611 if (*unguided_bsdf_pdf != 0.0f) {
612 bsdf_eval_init(bsdf_eval, sc, *wo, eval * sc->weight);
613
615
616 const float roughness_squared = bsdf_get_specular_roughness_squared(sc);
617 if (sd->num_closure > 1) {
618 const float sweight = sc->sample_weight;
619 const float weight = (*unguided_bsdf_pdf) * sweight;
620 *unguided_bsdf_pdf = _surface_shader_bsdf_eval_mis(kg,
621 sd,
622 *wo,
623 sc,
624 bsdf_eval,
625 weight,
626 sweight,
627 0,
628 weight * roughness_squared,
629 r_avg_roughness_squared);
631 }
632 else {
633 r_avg_roughness_squared = roughness_squared;
634 }
635 *bsdf_pdf = *unguided_bsdf_pdf;
636
637 if (use_surface_guiding) {
638 guide_pdf = guiding_bsdf_pdf(kg, *wo);
639 *bsdf_pdf *= 1.0f - guiding_sampling_prob;
640 *bsdf_pdf += guiding_sampling_prob * guide_pdf * (1.0f - bssrdf_sampling_prob);
641 }
642 }
643
645 }
646
647 return label;
648}
649
650ccl_device int surface_shader_bsdf_guided_sample_closure_ris(
651 KernelGlobals kg,
653 ccl_private ShaderData *sd,
654 const ccl_private ShaderClosure *sc,
655 const float3 rand_bsdf,
656 const ccl_private RNGState *rng_state,
659 ccl_private float *bsdf_pdf,
660 ccl_private float *mis_pdf,
661 ccl_private float *unguided_bsdf_pdf,
662 ccl_private float2 *sampled_roughness,
663 ccl_private float *eta,
664 ccl_private float &r_avg_roughness_squared)
665{
666 /* BSSRDF should already have been handled elsewhere. */
668
669 r_avg_roughness_squared = 0.0f;
670
671 const bool use_surface_guiding = INTEGRATOR_STATE(state, guiding, use_surface_guiding);
672 const float guiding_sampling_prob = INTEGRATOR_STATE(
674 const float bssrdf_sampling_prob = INTEGRATOR_STATE(state, guiding, bssrdf_sampling_prob);
675
676 /* Decide between sampling guiding distribution and BSDF. */
677 const float rand_bsdf_guiding = INTEGRATOR_STATE(state, guiding, sample_surface_guiding_rand);
678
679 /* Initialize to zero. */
680 int label = LABEL_NONE;
683
684 *unguided_bsdf_pdf = 0.0f;
685 float guide_pdf = 0.0f;
686
687 if (use_surface_guiding && guiding_sampling_prob > 0.0f) {
688 /* Performing guided sampling using RIS */
689
690 // selected RIS candidate
691 int ris_idx = 0;
692
693 // directional roughness for each of the two RIS candidates
694 float ris_avg_roughness_squared[2] = {0.0f, 0.0f};
695
696 // meta data for the two RIS candidates
697 GuidingRISSample ris_samples[2];
698 ris_samples[0].rand = rand_bsdf;
699 ris_samples[1].rand = path_state_rng_3D(kg, rng_state, PRNG_SURFACE_RIS_GUIDING_0);
700
701 // ----------------------------------------------------
702 // generate the first RIS candidate using a BSDF sample
703 // ----------------------------------------------------
704 ris_samples[0].label = bsdf_sample(kg,
705 sd,
706 sc,
707 ris_samples[0].rand,
708 &ris_samples[0].eval,
709 &ris_samples[0].wo,
710 &ris_samples[0].bsdf_pdf,
711 &ris_samples[0].sampled_roughness,
712 &ris_samples[0].eta);
713
715 &ris_samples[0].bsdf_eval, sc, ris_samples[0].wo, ris_samples[0].eval * sc->weight);
716 if (ris_samples[0].bsdf_pdf > 0.0f) {
717 const float roughness_squared = bsdf_get_specular_roughness_squared(sc);
718 if (sd->num_closure > 1) {
719 const float sweight = sc->sample_weight;
720 const float weight = ris_samples[0].bsdf_pdf * sweight;
721 ris_samples[0].bsdf_pdf = _surface_shader_bsdf_eval_mis(kg,
722 sd,
723 ris_samples[0].wo,
724 sc,
725 &ris_samples[0].bsdf_eval,
726 weight,
727 sweight,
728 0,
729 weight * roughness_squared,
730 ris_avg_roughness_squared[0]);
731 kernel_assert(reduce_min(bsdf_eval_sum(&ris_samples[0].bsdf_eval)) >= 0.0f);
732 }
733 else {
734 ris_avg_roughness_squared[0] = roughness_squared;
735 }
736 ris_samples[0].avg_bsdf_eval = average(ris_samples[0].bsdf_eval.sum);
737 ris_samples[0].guide_pdf = guiding_bsdf_pdf(kg, ris_samples[0].wo);
738 ris_samples[0].guide_pdf *= (1.0f - bssrdf_sampling_prob);
740 kg, ris_samples[0].wo);
741 ris_samples[0].bsdf_pdf = max(0.0f, ris_samples[0].bsdf_pdf);
742 }
743
744 // ------------------------------------------------------------------------------
745 // generate the second RIS candidate using a sample from the guiding distribution
746 // ------------------------------------------------------------------------------
747 float unguided_bsdf_pdfs[MAX_CLOSURE];
748 bsdf_eval_init(&ris_samples[1].bsdf_eval, eval);
749 ris_samples[1].guide_pdf = guiding_bsdf_sample(
750 kg, make_float2(ris_samples[1].rand), &ris_samples[1].wo);
751 ris_samples[1].guide_pdf *= (1.0f - bssrdf_sampling_prob);
753 kg, ris_samples[1].wo);
754 ris_samples[1].bsdf_pdf = surface_shader_bsdf_eval_pdfs(kg,
755 sd,
756 ris_samples[1].wo,
757 &ris_samples[1].bsdf_eval,
758 unguided_bsdf_pdfs,
759 0,
760 ris_avg_roughness_squared[1]);
761 ris_samples[1].label = ris_samples[0].label;
762 ris_samples[1].avg_bsdf_eval = average(ris_samples[1].bsdf_eval.sum);
763 ris_samples[1].bsdf_pdf = max(0.0f, ris_samples[1].bsdf_pdf);
764
765 // ------------------------------------------------------------------------------
766 // calculate the RIS target functions for each RIS candidate
767 // ------------------------------------------------------------------------------
768 int num_ris_candidates = 0;
769 float sum_ris_weights = 0.0f;
770 if (calculate_ris_target(&ris_samples[0], guiding_sampling_prob)) {
771 sum_ris_weights += ris_samples[0].ris_weight;
772 num_ris_candidates++;
773 }
774 kernel_assert(ris_samples[0].ris_weight >= 0.0f);
775 kernel_assert(sum_ris_weights >= 0.0f);
776
777 if (calculate_ris_target(&ris_samples[1], guiding_sampling_prob)) {
778 sum_ris_weights += ris_samples[1].ris_weight;
779 num_ris_candidates++;
780 }
781 kernel_assert(ris_samples[1].ris_weight >= 0.0f);
782 kernel_assert(sum_ris_weights >= 0.0f);
783
784 // ------------------------------------------------------------------------------
785 // Sample/Select a sample from the RIS candidates proportional to the target
786 // ------------------------------------------------------------------------------
787 if (num_ris_candidates == 0 || !(sum_ris_weights > 1e-10f)) {
788 *bsdf_pdf = 0.0f;
789 *mis_pdf = 0.0f;
790 return label;
791 }
792
793 const float rand_ris_select = rand_bsdf_guiding * sum_ris_weights;
794
795 float sum_ris = 0.0f;
796 for (int i = 0; i < 2; i++) {
797 sum_ris += ris_samples[i].ris_weight;
798 if (rand_ris_select <= sum_ris) {
799 ris_idx = i;
800 break;
801 }
802 }
803
804 kernel_assert(sum_ris >= 0.0f);
805 kernel_assert(ris_idx < 2);
806
807 // ------------------------------------------------------------------------------
808 // Fill in the sample data for the selected RIS candidate
809 // ------------------------------------------------------------------------------
810 guide_pdf = ris_samples[ris_idx].ris_target * (2.0f / sum_ris_weights);
811 *unguided_bsdf_pdf = ris_samples[ris_idx].bsdf_pdf;
812 *mis_pdf = 0.5f * (ris_samples[ris_idx].bsdf_pdf + ris_samples[ris_idx].guide_pdf);
813 *bsdf_pdf = guide_pdf;
814
815 *wo = ris_samples[ris_idx].wo;
816 label = ris_samples[ris_idx].label;
817
818 *sampled_roughness = ris_samples[ris_idx].sampled_roughness;
819 *eta = ris_samples[ris_idx].eta;
820 *bsdf_eval = ris_samples[ris_idx].bsdf_eval;
821 r_avg_roughness_squared = ris_avg_roughness_squared[ris_idx];
822
823 kernel_assert(isfinite_safe(guide_pdf));
824 kernel_assert(isfinite_safe(*bsdf_pdf));
825
826 if (!(*bsdf_pdf > 1e-10f)) {
827 *bsdf_pdf = 0.0f;
828 *mis_pdf = 0.0f;
829 return label;
830 }
831
832 kernel_assert(*bsdf_pdf > 0.0f);
833 kernel_assert(*bsdf_pdf >= 1e-20f);
834 kernel_assert(guide_pdf >= 0.0f);
835
837 if (ris_idx == 1 && ris_samples[1].bsdf_pdf > 0.0f) {
838
839 const float rnd = path_state_rng_1D(kg, rng_state, PRNG_SURFACE_RIS_GUIDING_1);
840
841 float sum_pdfs = 0.0f;
842 int idx = -1;
843 for (int i = 0; i < sd->num_closure; i++) {
844 sum_pdfs += unguided_bsdf_pdfs[i];
845 if (rnd <= sum_pdfs) {
846 idx = i;
847 break;
848 }
849 }
850 // kernel_assert(idx >= 0);
851 /* Set the default idx to the last in the list.
852 * in case of numerical problems and rand_bsdf_guiding is just >=1.0f and
853 * the sum of all unguided_bsdf_pdfs is just < 1.0f. */
854 idx = (rnd > sum_pdfs) ? sd->num_closure - 1 : idx;
855
856 label = bsdf_label(kg, &sd->closure[idx], *wo);
857 bsdf_roughness_eta(&sd->closure[idx], *wo, sampled_roughness, eta);
858 }
859
860 kernel_assert(isfinite_safe(*bsdf_pdf));
861 kernel_assert(*bsdf_pdf >= 0.0f);
863 }
864 else {
865 /* Sample BSDF. */
866 *bsdf_pdf = 0.0f;
867 label = bsdf_sample(
868 kg, sd, sc, rand_bsdf, &eval, wo, unguided_bsdf_pdf, sampled_roughness, eta);
869# ifdef WITH_CYCLES_DEBUG
870 // Code path to validate the estimation of the label, sampled roughness and eta
871 // This should be activated from time to time when the BSDFs change to check if everything
872 // is still working correctly.
873 if (*unguided_bsdf_pdf > 0.0f) {
874 surface_shader_validate_bsdf_sample(kg, sc, *wo, label, *sampled_roughness, *eta);
875 }
876# endif
877
878 if (*unguided_bsdf_pdf != 0.0f) {
879 bsdf_eval_init(bsdf_eval, sc, *wo, eval * sc->weight);
880
882
883 const float roughness_squared = bsdf_get_specular_roughness_squared(sc);
884 if (sd->num_closure > 1) {
885 const float sweight = sc->sample_weight;
886 const float weight = (*unguided_bsdf_pdf) * sweight;
887 *unguided_bsdf_pdf = _surface_shader_bsdf_eval_mis(kg,
888 sd,
889 *wo,
890 sc,
891 bsdf_eval,
892 weight,
893 sweight,
894 0,
895 weight * roughness_squared,
896 r_avg_roughness_squared);
898 }
899 else {
900 r_avg_roughness_squared = roughness_squared;
901 }
902 *bsdf_pdf = *unguided_bsdf_pdf;
903 *mis_pdf = *bsdf_pdf;
904 }
905
907 }
908
909 return label;
910}
911
912ccl_device int surface_shader_bsdf_guided_sample_closure(
913 KernelGlobals kg,
915 ccl_private ShaderData *sd,
916 const ccl_private ShaderClosure *sc,
917 const float3 rand_bsdf,
920 ccl_private float *bsdf_pdf,
921 ccl_private float *mis_pdf,
922 ccl_private float *unguided_bsdf_pdf,
923 ccl_private float2 *sampled_roughness,
924 ccl_private float *eta,
925 const ccl_private RNGState *rng_state,
926 ccl_private float &r_avg_roughness_squared)
927{
928 int label = LABEL_NONE;
929 r_avg_roughness_squared = 0.0f;
930 if (kernel_data.integrator.guiding_directional_sampling_type ==
932 kernel_data.integrator.guiding_directional_sampling_type ==
934 {
935 label = surface_shader_bsdf_guided_sample_closure_mis(kg,
936 state,
937 sd,
938 sc,
939 rand_bsdf,
940 bsdf_eval,
941 wo,
942 bsdf_pdf,
943 unguided_bsdf_pdf,
944 sampled_roughness,
945 eta,
946 r_avg_roughness_squared);
947 *mis_pdf = (*unguided_bsdf_pdf > 0.0f) ? *bsdf_pdf : 0.0f;
948 }
949 else if (kernel_data.integrator.guiding_directional_sampling_type ==
951 {
952 label = surface_shader_bsdf_guided_sample_closure_ris(kg,
953 state,
954 sd,
955 sc,
956 rand_bsdf,
957 rng_state,
958 bsdf_eval,
959 wo,
960 bsdf_pdf,
961 mis_pdf,
962 unguided_bsdf_pdf,
963 sampled_roughness,
964 eta,
965 r_avg_roughness_squared);
966 }
967 if (!(*unguided_bsdf_pdf > 0.0f)) {
968 *bsdf_pdf = 0.0f;
969 *mis_pdf = 0.0f;
970 }
971 return label;
972}
973
974#endif
975
976/* Sample direction for picked BSDF, and return evaluation and pdf for all
977 * BSDFs combined using MIS. */
979 ccl_private ShaderData *sd,
980 const ccl_private ShaderClosure *sc,
981 const float3 rand_bsdf,
984 ccl_private float *pdf,
985 ccl_private float2 *sampled_roughness,
986 ccl_private float *eta,
987 ccl_private float &r_avg_roughness_squared)
988{
989 /* BSSRDF should already have been handled elsewhere. */
991
992 int label;
993 Spectrum eval = zero_spectrum();
994
995 *pdf = 0.0f;
996 label = bsdf_sample(kg, sd, sc, rand_bsdf, &eval, wo, pdf, sampled_roughness, eta);
997
998 if (*pdf != 0.0f) {
999 bsdf_eval_init(bsdf_eval, sc, *wo, eval * sc->weight);
1000
1001 const float sweight = sc->sample_weight;
1002 const float roughness_squared = bsdf_get_specular_roughness_squared(sc);
1003
1004 if (sd->num_closure > 1) {
1005 const float weight = *pdf * sweight;
1007 sd,
1008 *wo,
1009 sc,
1010 bsdf_eval,
1011 weight,
1012 sweight,
1013 0,
1014 weight * roughness_squared,
1015 r_avg_roughness_squared);
1016 }
1017 else {
1018 r_avg_roughness_squared = roughness_squared;
1019 }
1020 }
1021 else {
1023 r_avg_roughness_squared = 0.0f;
1024 }
1025
1026 return label;
1027}
1028
1030{
1031 float roughness = 0.0f;
1032 float sum_weight = 0.0f;
1033
1034 for (int i = 0; i < sd->num_closure; i++) {
1035 const ccl_private ShaderClosure *sc = &sd->closure[i];
1036
1037 if (CLOSURE_IS_BSDF(sc->type)) {
1038 /* `sqrt()` once to undo the squaring from multiplying roughness on the
1039 * two axes, and once for the squared roughness convention. */
1040 const float value = bsdf_get_roughness_pass_squared(sc);
1041 if (value >= 0.0f) {
1042 const float weight = fabsf(average(sc->weight));
1043 roughness += weight * sqrtf(sqrtf(value));
1044 sum_weight += weight;
1045 }
1046 }
1047 }
1048
1049 return (sum_weight > 0.0f) ? roughness / sum_weight : 1.0f;
1050}
1051
1053{
1054 if (sd->shader_flag & SD_HAS_ONLY_VOLUME) {
1055 return one_spectrum();
1056 }
1057 if (sd->runtime_flag & (SR_TRANSPARENT | SR_RAY_PORTAL)) {
1058 return sd->closure_transparent_extinction;
1059 }
1060 return zero_spectrum();
1061}
1062
1064{
1066
1067 alpha = saturate(alpha);
1068
1069 return alpha;
1070}
1071
1073{
1074 Spectrum eval = zero_spectrum();
1075
1076 for (int i = 0; i < sd->num_closure; i++) {
1077 const ccl_private ShaderClosure *sc = &sd->closure[i];
1078
1079 if (CLOSURE_IS_BSDF_DIFFUSE(sc->type) || CLOSURE_IS_BSSRDF(sc->type)) {
1080 eval += closure_albedo(kg, sd, sc, true, true);
1081 }
1082 }
1083
1084 return eval;
1085}
1086
1088{
1089 Spectrum eval = zero_spectrum();
1090
1091 for (int i = 0; i < sd->num_closure; i++) {
1092 const ccl_private ShaderClosure *sc = &sd->closure[i];
1093
1094 if (CLOSURE_IS_BSDF_GLOSSY(sc->type) || CLOSURE_IS_GLASS(sc->type)) {
1095 eval += closure_albedo(kg, sd, sc, true, false);
1096 }
1097 }
1098
1099 return eval;
1100}
1101
1103{
1104 Spectrum eval = zero_spectrum();
1105
1106 for (int i = 0; i < sd->num_closure; i++) {
1107 const ccl_private ShaderClosure *sc = &sd->closure[i];
1108
1109 if (CLOSURE_IS_BSDF_TRANSMISSION(sc->type) || CLOSURE_IS_GLASS(sc->type)) {
1110 eval += closure_albedo(kg, sd, sc, false, true);
1111 }
1112 }
1113
1114 return eval;
1115}
1116
1118{
1119 float3 N = zero_float3();
1120
1121 for (int i = 0; i < sd->num_closure; i++) {
1122 const ccl_private ShaderClosure *sc = &sd->closure[i];
1123 if (CLOSURE_IS_BSDF_OR_BSSRDF(sc->type)) {
1124 N += sc->N * fabsf(average(sc->weight));
1125 }
1126 }
1127
1128 return (is_zero(N)) ? sd->N : normalize(N);
1129}
1130
1132 const float ao_factor,
1134{
1135 Spectrum eval = zero_spectrum();
1136 float3 N = zero_float3();
1137
1138 for (int i = 0; i < sd->num_closure; i++) {
1139 const ccl_private ShaderClosure *sc = &sd->closure[i];
1140
1141 if (CLOSURE_IS_BSDF_DIFFUSE(sc->type)) {
1142 const ccl_private DiffuseBsdf *bsdf = (const ccl_private DiffuseBsdf *)sc;
1143 eval += sc->weight * ao_factor;
1144 N += bsdf->N * fabsf(average(sc->weight));
1145 }
1146 }
1147
1148 *N_ = (is_zero(N)) ? sd->N : normalize(N);
1149 return eval;
1150}
1151
1152#ifdef __SUBSURFACE__
1153ccl_device float3 surface_shader_bssrdf_normal(const ccl_private ShaderData *sd)
1154{
1155 float3 N = zero_float3();
1156
1157 for (int i = 0; i < sd->num_closure; i++) {
1158 const ccl_private ShaderClosure *sc = &sd->closure[i];
1159
1160 if (CLOSURE_IS_BSSRDF(sc->type)) {
1161 const ccl_private Bssrdf *bssrdf = (const ccl_private Bssrdf *)sc;
1162 const float avg_weight = fabsf(average(sc->weight));
1163
1164 N += bssrdf->N * avg_weight;
1165 }
1166 }
1167
1168 return (is_zero(N)) ? sd->N : normalize(N);
1169}
1170#endif /* __SUBSURFACE__ */
1171
1172/* Constant emission optimization */
1173
1175 const int shader,
1176 ccl_private Spectrum *eval)
1177{
1178 const int shader_index = shader & SHADER_MASK;
1179 const int shader_flag = kernel_data_fetch(shaders, shader_index).flags;
1180
1181 if (shader_flag & SD_HAS_CONSTANT_EMISSION) {
1182 const float3 emission_rgb = make_float3(
1183 kernel_data_fetch(shaders, shader_index).constant_emission[0],
1184 kernel_data_fetch(shaders, shader_index).constant_emission[1],
1185 kernel_data_fetch(shaders, shader_index).constant_emission[2]);
1186 *eval = rgb_to_spectrum(emission_rgb);
1187
1188 return true;
1189 }
1190
1191 return false;
1192}
1193
1194/* Background */
1195
1197{
1198 if (sd->runtime_flag & SR_EMISSION) {
1199 return sd->closure_emission_background;
1200 }
1201 return zero_spectrum();
1202}
1203
1204/* Emission */
1205
1207{
1208 if (sd->runtime_flag & SR_EMISSION) {
1209 return emissive_simple_eval(sd->Ng, sd->wi) * sd->closure_emission_background;
1210 }
1211 return zero_spectrum();
1212}
1213
1214/* Holdout */
1215
1217{
1218 Spectrum weight = zero_spectrum();
1219
1220 /* For objects marked as holdout, preserve transparency and remove all other
1221 * closures, replacing them with a holdout weight. */
1222 if (sd->object_flag & SD_OBJECT_HOLDOUT_MASK) {
1223 if ((sd->runtime_flag & SR_TRANSPARENT) && !(sd->shader_flag & SD_HAS_ONLY_VOLUME)) {
1224 weight = one_spectrum() - sd->closure_transparent_extinction;
1225
1226 for (int i = 0; i < sd->num_closure; i++) {
1227 ccl_private ShaderClosure *sc = &sd->closure[i];
1228 if (!CLOSURE_IS_BSDF_TRANSPARENT(sc->type)) {
1229 sc->type = NBUILTIN_CLOSURES;
1230 }
1231 }
1232
1233 sd->runtime_flag &= (~SR_CLOSURE_FLAG | SR_TRANSPARENT | SR_BSDF);
1234 }
1235 else {
1236 weight = one_spectrum();
1237 }
1238 }
1239 else {
1240 for (int i = 0; i < sd->num_closure; i++) {
1241 const ccl_private ShaderClosure *sc = &sd->closure[i];
1242 if (CLOSURE_IS_HOLDOUT(sc->type)) {
1243 weight += sc->weight;
1244 }
1245 }
1246 }
1247
1248 return weight;
1249}
1250
1251/* Surface Evaluation */
1252
1253template<uint64_t node_feature_mask, typename ConstIntegratorGenericState>
1255 ConstIntegratorGenericState state,
1256 ccl_private ShaderData *ccl_restrict sd,
1257 ccl_global float *ccl_restrict buffer,
1258 const PathRayVisibility path_visibility,
1259 const uint32_t path_flag,
1260 bool use_caustics_storage = false)
1261{
1262 /* Initialize additional RNG for BSDFs and image textures. */
1263 sd->lcg_state = integrator_state_lcg_init(state, 0xb4bc3953);
1264
1265 /* If path is being terminated, we are tracing a shadow ray or evaluating
1266 * emission, then we don't need to store closures. The emission and shadow
1267 * shader data also do not have a closure array to save GPU memory. */
1268 int max_closures;
1269 if ((path_visibility & PATH_RAY_VISIBILITY_SHADOW) ||
1270 (path_flag & (PATH_RAY_TERMINATE | PATH_RAY_EMISSION)))
1271 {
1272 max_closures = 0;
1273 }
1274 else {
1275 max_closures = use_caustics_storage ? CAUSTICS_MAX_CLOSURE : kernel_data.max_closures;
1276 }
1277
1278 sd->num_closure = 0;
1279 sd->num_closure_left = max_closures;
1280 sd->closure_transparent_extinction = zero_spectrum();
1281
1282#ifdef __OSL__
1283 if (kernel_data.kernel_features & KERNEL_FEATURE_OSL_SHADING) {
1284 osl_eval_nodes<SHADER_TYPE_SURFACE>(kg, state, sd, path_visibility, path_flag);
1285 }
1286 else
1287#endif
1288 {
1289#ifdef __SVM__
1291 kg, state, sd, buffer, path_visibility, path_flag);
1292#else
1293 if (sd->object == OBJECT_NONE) {
1294 sd->closure_emission_background = make_spectrum(0.8f);
1295 sd->runtime_flag |= SR_EMISSION;
1296 }
1297 else {
1298 bsdf_diffuse_setup(sd, sd->N, make_spectrum(0.8f));
1299 }
1300#endif
1301 }
1302}
1303
CCL_NAMESPACE_BEGIN ccl_device_inline float bsdf_get_specular_roughness_squared(const ccl_private ShaderClosure *sc)
Definition bsdf.h:29
ccl_device Spectrum bsdf_eval(KernelGlobals kg, ccl_private ShaderData *sd, const ccl_private ShaderClosure *sc, const float3 wo, ccl_private float *pdf)
Definition bsdf.h:554
ccl_device_inline void bsdf_roughness_eta(const ccl_private ShaderClosure *sc, const float3 wo, ccl_private float2 *roughness, ccl_private float *eta)
Definition bsdf.h:333
ccl_device_inline float bsdf_get_roughness_pass_squared(const ccl_private ShaderClosure *sc)
Definition bsdf.h:43
ccl_device_inline int bsdf_label(const KernelGlobals kg, const ccl_private ShaderClosure *sc, const float3 wo)
Definition bsdf.h:442
ccl_device_inline int bsdf_sample(KernelGlobals kg, ccl_private ShaderData *sd, const ccl_private ShaderClosure *sc, const float3 rand, ccl_private Spectrum *eval, ccl_private float3 *wo, ccl_private float *pdf, ccl_private float2 *sampled_roughness, ccl_private float *eta)
Definition bsdf.h:173
ccl_device_inline Spectrum closure_albedo(KernelGlobals kg, const ccl_private ShaderData *sd, const ccl_private ShaderClosure *sc, const bool reflection, const bool transmission)
Definition bsdf.h:732
ccl_device void bsdf_blur(ccl_private ShaderClosure *sc, const float roughness)
Definition bsdf.h:665
ccl_device void bsdf_diffuse_setup(ccl_private ShaderData *sd, const float3 N, const Spectrum weight)
ccl_device_forceinline bool alpha_is_almost_specular(const float alpha_x, const float alpha_y)
btScalar max
#define kernel_assert(cond)
#define CLOSURE_IS_GLASS(type)
#define kernel_data
#define ccl_attr_maybe_unused
#define ccl_restrict
#define ccl_device_forceinline
#define KERNEL_FEATURE_PATH_GUIDING
#define kernel_data_fetch(name, index)
#define CLOSURE_IS_HOLDOUT(type)
#define one_spectrum
#define CLOSURE_IS_BSDF_TRANSPARENT(type)
#define CLOSURE_IS_BSDF_TRANSMISSION(type)
#define CLOSURE_IS_BSDF(type)
#define zero_spectrum
#define OBJECT_NONE
#define KERNEL_FEATURE_OSL_SHADING
#define make_spectrum(f)
#define CLOSURE_IS_BSDF_GLOSSY(type)
#define ccl_private
#define ccl_device_inline
#define CLOSURE_IS_BSDF_DIFFUSE(type)
#define MAX_CLOSURE
#define ccl_global
#define CLOSURE_IS_BSDF_OR_BSSRDF(type)
#define __MNEE__
#define CLOSURE_IS_BSSRDF(type)
#define CAUSTICS_MAX_CLOSURE
#define CCL_NAMESPACE_END
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
ccl_device Spectrum emissive_simple_eval(const float3 Ng, const float3 wi)
Definition emissive.h:58
#define FLT_MAX
VecBase< float, D > normalize(VecOp< float, D >) RET
bool is_zero(float3x3 a)
float reduce_min(float2 a)
float average(float2 a)
const int state
ccl_device_forceinline float guiding_surface_incoming_radiance_pdf(ccl_attr_maybe_unused KernelGlobals kg, ccl_attr_maybe_unused const float3 wo)
ccl_device_forceinline bool guiding_bsdf_init(ccl_attr_maybe_unused KernelGlobals kg, ccl_attr_maybe_unused const float3 P, ccl_attr_maybe_unused const float3 N, ccl_attr_maybe_unused ccl_private float &rand)
ccl_device_forceinline float guiding_bsdf_sample(ccl_attr_maybe_unused KernelGlobals kg, ccl_attr_maybe_unused const float2 rand_bsdf, ccl_attr_maybe_unused ccl_private float3 *wo)
ccl_device_forceinline bool calculate_ris_target(ccl_attr_maybe_unused ccl_private GuidingRISSample *ris_sample, ccl_attr_maybe_unused const ccl_private float guiding_sampling_prob)
ccl_device_forceinline float guiding_bsdf_pdf(ccl_attr_maybe_unused KernelGlobals kg, ccl_attr_maybe_unused const float3 wo)
void osl_eval_nodes(const ThreadKernelGlobalsCPU *kg, ConstIntegratorGenericState state, ShaderData *sd, PathRayVisibility path_visibility, uint32_t path_flag)
ccl_device void svm_eval_nodes(KernelGlobals kg, ConstIntegratorGenericState state, ccl_private ShaderData *sd, ccl_global float *render_buffer, const PathRayVisibility path_visibility, const uint32_t path_flag)
ClosureType
@ CLOSURE_NONE_ID
@ CLOSURE_HOLDOUT_ID
@ NBUILTIN_CLOSURES
@ CLOSURE_BSDF_TRANSLUCENT_ID
uint32_t PathRayVisibility
@ PATH_MNEE_VALID
@ SR_RAY_PORTAL
@ SR_CLOSURE_FLAG
@ SR_TRANSPARENT
@ SR_BSDF_HAS_EVAL
@ SR_EMISSION
@ SR_HOLDOUT
@ SR_BSDF
@ FILTER_CLOSURE_EMISSION
@ FILTER_CLOSURE_GLOSSY
@ FILTER_CLOSURE_DIFFUSE
@ FILTER_CLOSURE_TRANSPARENT
@ FILTER_CLOSURE_DIRECT_LIGHT
@ FILTER_CLOSURE_TRANSMISSION
@ SD_HAS_CONSTANT_EMISSION
@ SD_HAS_ONLY_VOLUME
@ PRNG_SURFACE_BSDF_GUIDING
@ PRNG_SURFACE_RIS_GUIDING_0
@ PRNG_SURFACE_RIS_GUIDING_1
@ PATH_RAY_TERMINATE
@ PATH_RAY_EMISSION
@ PATH_RAY_VISIBILITY_CAMERA
@ PATH_RAY_VISIBILITY_SHADOW
@ SD_OBJECT_HOLDOUT_MASK
@ SHADER_EXCLUDE_DIFFUSE
@ SHADER_USE_MIS
@ SHADER_EXCLUDE_ANY
@ SHADER_EXCLUDE_GLOSSY
@ SHADER_EXCLUDE_TRANSMIT
@ SHADER_MASK
@ GUIDING_DIRECTIONAL_SAMPLING_TYPE_PRODUCT_MIS
@ GUIDING_DIRECTIONAL_SAMPLING_TYPE_RIS
@ GUIDING_DIRECTIONAL_SAMPLING_TYPE_ROUGHNESS
@ LABEL_NONE
ccl_device_inline Spectrum rgb_to_spectrum(const float3 rgb)
CCL_NAMESPACE_BEGIN ccl_device_inline void bsdf_eval_init(ccl_private BsdfEval *eval, const ccl_private ShaderClosure *sc, const float3 wo, Spectrum value)
ccl_device_inline void bsdf_eval_accum(ccl_private BsdfEval *eval, const ccl_private ShaderClosure *sc, const float3 wo, Spectrum value)
ccl_device_inline Spectrum bsdf_eval_sum(const ccl_private BsdfEval *eval)
ccl_device_inline float safe_sqrtf(const float f)
Definition math_base.h:571
ccl_device_inline bool isfinite_safe(const float f)
Definition math_base.h:362
CCL_NAMESPACE_BEGIN ccl_device_inline float3 zero_float3()
Definition math_float3.h:17
#define N
ResultT eval(const Uniform &uni, const ViewMatrices view, ScreenThicknessParameters thickness_params, sampler2D hiz_tx, sampler2D screen_radiance_tx, sampler2D screen_normal_tx, float3 vP, float3 vN, float4 noise, float2 pixel_size, float search_distance, float angle_bias, const int slice_count, const int sample_count, const bool reversed, const bool ao_only)
ccl_device_inline float path_state_rng_1D(KernelGlobals kg, const ccl_private RNGState *rng_state, const int dimension)
Definition path_state.h:361
ccl_device_inline float3 path_state_rng_3D(KernelGlobals kg, const ccl_private RNGState *rng_state, const int dimension)
Definition path_state.h:377
#define fabsf
#define sqrtf
#define ccl_device
#define make_float2
#define saturate(a)
Definition smaa.cc:319
#define INTEGRATOR_STATE_WRITE(state, nested_struct, member)
Definition state.h:235
#define INTEGRATOR_STATE(state, nested_struct, member)
Definition state.h:234
IntegratorStateCPU * IntegratorState
Definition state.h:227
const IntegratorStateCPU * ConstIntegratorState
Definition state.h:228
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_DENOISING KERNEL_STRUCT_VOLUME_STACK_SIZE surface_guiding_sampling_prob
ccl_device_inline uint integrator_state_lcg_init(ConstIntegratorShadowState state, const uint hash)
Definition state_util.h:735
closure color bssrdf(string method, normal N, vector radius, color albedo) BUILTIN
float x
float y
const ccl_device_inline ccl_private ShaderClosure * surface_shader_bsdf_bssrdf_pick(const ccl_private ShaderData *ccl_restrict sd, ccl_private float3 *rand_bsdf)
ccl_device int surface_shader_bsdf_sample_closure(KernelGlobals kg, ccl_private ShaderData *sd, const ccl_private ShaderClosure *sc, const float3 rand_bsdf, ccl_private BsdfEval *bsdf_eval, ccl_private float3 *wo, ccl_private float *pdf, ccl_private float2 *sampled_roughness, ccl_private float *eta, ccl_private float &r_avg_roughness_squared)
ccl_device float surface_shader_average_roughness(const ccl_private ShaderData *sd)
ccl_device Spectrum surface_shader_transmission(KernelGlobals kg, const ccl_private ShaderData *sd)
ccl_device void surface_shader_eval(KernelGlobals kg, ConstIntegratorGenericState state, ccl_private ShaderData *ccl_restrict sd, ccl_global float *ccl_restrict buffer, const PathRayVisibility path_visibility, const uint32_t path_flag, bool use_caustics_storage=false)
ccl_device_inline float surface_shader_bsdf_eval_pdfs(const KernelGlobals kg, ccl_private ShaderData *sd, const float3 wo, ccl_private BsdfEval *result_eval, ccl_private float *pdfs, const uint light_shader_flags, ccl_private float &r_avg_roughness_squared)
ccl_device_forceinline bool _surface_shader_exclude(ClosureType type, const uint light_shader_flags)
ccl_device float3 surface_shader_average_normal(const ccl_private ShaderData *sd)
ccl_device_inline float _surface_shader_bsdf_eval_mis(KernelGlobals kg, ccl_private ShaderData *sd, const float3 wo, const ccl_private ShaderClosure *skip_sc, ccl_private BsdfEval *result_eval, float sum_pdf, float sum_sample_weight, const uint light_shader_flags, float sum_pdf_roughness_squared, ccl_private float &r_avg_roughness_squared)
ccl_device Spectrum surface_shader_ao(const ccl_private ShaderData *sd, const float ao_factor, ccl_private float3 *N_)
ccl_device Spectrum surface_shader_transparency(const ccl_private ShaderData *sd)
ccl_device Spectrum surface_shader_background(const ccl_private ShaderData *sd)
ccl_device Spectrum surface_shader_glossy(KernelGlobals kg, const ccl_private ShaderData *sd)
ccl_device float surface_shader_bsdf_eval(KernelGlobals kg, ccl_attr_maybe_unused IntegratorState state, ccl_private ShaderData *sd, const float3 wo, ccl_private BsdfEval *bsdf_eval, const uint light_shader_flags, ccl_private float &r_avg_roughness_squared)
ccl_device Spectrum surface_shader_alpha(const ccl_private ShaderData *sd)
ccl_device Spectrum surface_shader_apply_holdout(ccl_private ShaderData *sd)
ccl_device Spectrum surface_shader_diffuse(KernelGlobals kg, const ccl_private ShaderData *sd)
ccl_device_inline Spectrum surface_shader_bssrdf_sample_weight(const ccl_private ShaderData *ccl_restrict sd, const ccl_private ShaderClosure *ccl_restrict bssrdf_sc)
ccl_device Spectrum surface_shader_emission(const ccl_private ShaderData *sd)
ccl_device bool surface_shader_constant_emission(KernelGlobals kg, const int shader, ccl_private Spectrum *eval)
CCL_NAMESPACE_BEGIN ccl_device_inline void surface_shader_prepare_closures(KernelGlobals kg, ConstIntegratorState state, ccl_private ShaderData *sd, const PathRayVisibility path_visibility)
float3 Spectrum
#define N_(msgid)