Blender V5.3
kernel/integrator/guiding.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
7#include "kernel/globals.h"
8#include "kernel/types.h"
9
12
13/* FIXME: The below include could be guarded behind `ifdef WITH_CYCLES_DEBUG`, but Metal
14 * pre-processing is not expanding guarded include files properly. */
15#include "kernel/closure/bsdf.h"
16
17#include "util/color.h"
18
20
21#define GUIDING_FLT_LARGE 1.844e18f
22#define GUIDING_MAX_LIGHT_DISTANCE 1e6f
23
24/* Utilities. */
25
29 /* The relative IOR of the outgoing media and the incoming media. */
30 float eta{1.0f};
31 int label;
33 float bsdf_pdf{0.0f};
34 float guide_pdf{0.0f};
35 float ris_target{0.0f};
36 float ris_pdf{0.0f};
37 float ris_weight{0.0f};
38
41 float avg_bsdf_eval{0.0f};
43};
44
47 ccl_attr_maybe_unused const ccl_private float guiding_sampling_prob)
48{
49#if defined(__PATH_GUIDING__)
50 const float pi_factor = 2.0f;
51 if (ris_sample->avg_bsdf_eval > 0.0f && ris_sample->bsdf_pdf > 1e-10f &&
52 ris_sample->guide_pdf > 0.0f)
53 {
54 ris_sample->ris_target = (ris_sample->avg_bsdf_eval *
55 ((((1.0f - guiding_sampling_prob) * (1.0f / (pi_factor * M_PI_F))) +
56 (guiding_sampling_prob * ris_sample->incoming_radiance_pdf))));
57 ris_sample->ris_pdf = (0.5f * (ris_sample->bsdf_pdf + ris_sample->guide_pdf));
58 ris_sample->ris_weight = ris_sample->ris_target / ris_sample->ris_pdf;
59 return true;
60 }
61 ris_sample->ris_target = 0.0f;
62 ris_sample->ris_pdf = 0.0f;
63 return false;
64#else
65 return false;
66#endif
67}
68
69#if defined(__PATH_GUIDING__)
70static pgl_vec3f guiding_vec3f(const float3 v)
71{
72 return {v.x, v.y, v.z};
73}
74
75ccl_device_forceinline pgl_point3f guiding_point3f(const float3 v)
76{
77 return {v.x, v.y, v.z};
78}
79
81{
82 return make_float3(v.x, v.y, v.z);
83}
84
85ccl_device_forceinline bool is_guiding_valid(const float3 v)
86{
88 bool valid = true;
89 valid &= isfinite_safe(v);
90 valid &= interval.contains(v.x);
91 valid &= interval.contains(v.y);
92 valid &= interval.contains(v.z);
93
94 return valid;
95}
96ccl_device_forceinline float3 clamp_guiding_position(const float3 p)
97{
98 /* Clamping to the range of +/- GUIDING_FLT_LARGE / 5.0 to avoid potential numerical problems on
99 * the OpenPGL side. NOTE: The clamping is mainly a robustness fallback and might not be needed
100 * at all. */
102}
103
104ccl_device_forceinline bool guiding_has_next_segment(KernelGlobals kg)
105{
106 return kg->opgl_path_segment_storage->GetNumSegments() < kg->opgl_path_segment_storage_size;
107}
108
109ccl_device_forceinline openpgl::cpp::PathSegment *guiding_next_segment(KernelGlobals kg)
110{
111 const bool has_next_segment = guiding_has_next_segment(kg);
112 /* Ideally we should always have a next segment, if not we might need to investigate our
113 * training data generation procedure.
114 * NOTE: Currently SSS events generate one additional event that is not counted as actual bounce.
115 */
116 kernel_assert(has_next_segment);
117 return has_next_segment ? kg->opgl_path_segment_storage->NextSegment() : nullptr;
118}
119
120#endif
121
122/* Path recording for guiding. */
123
124/* Record Surface Interactions */
125
126/* Records/Adds a new path segment with the current path vertex on a surface.
127 * If the path is not terminated this call is usually followed by a call of
128 * guiding_record_surface_bounce. */
132 ccl_attr_maybe_unused const ccl_private ShaderData *sd)
133{
134#if defined(__PATH_GUIDING__) && PATH_GUIDING_LEVEL >= 1
135 if (!kernel_data.integrator.train_guiding) {
136 return;
137 }
138
140
141 const pgl_vec3f zero = guiding_vec3f(zero_float3());
142 const pgl_vec3f one = guiding_vec3f(one_float3());
143
144 state->guiding.path_segment = guiding_next_segment(kg);
145
146 if (state->guiding.path_segment != nullptr) {
147 float3 p = sd->P;
148 kernel_assert(is_guiding_valid(p));
149 p = clamp_guiding_position(p);
150 openpgl::cpp::SetPosition(state->guiding.path_segment, guiding_point3f(p));
151 openpgl::cpp::SetDirectionOut(state->guiding.path_segment, guiding_vec3f(sd->wi));
152 openpgl::cpp::SetVolumeScatter(state->guiding.path_segment, false);
153 openpgl::cpp::SetScatteredContribution(state->guiding.path_segment, zero);
154 openpgl::cpp::SetDirectContribution(state->guiding.path_segment, zero);
155 openpgl::cpp::SetTransmittanceWeight(state->guiding.path_segment, one);
156 openpgl::cpp::SetEta(state->guiding.path_segment, 1.0);
157 }
158#endif
159}
160
161/* Records the surface scattering event at the current vertex position of the segment. */
165 ccl_attr_maybe_unused const Spectrum weight,
166 ccl_attr_maybe_unused const float pdf,
169 ccl_attr_maybe_unused const float2 roughness,
170 ccl_attr_maybe_unused const float eta)
171{
172#if defined(__PATH_GUIDING__) && PATH_GUIDING_LEVEL >= 4
173 if (!kernel_data.integrator.train_guiding) {
174 return;
175 }
176
178
179 const float min_roughness = safe_sqrtf(fminf(roughness.x, roughness.y));
180 const bool is_delta = (min_roughness == 0.0f);
181 const float3 weight_rgb = spectrum_to_rgb(weight);
182 const float3 normal = clamp(N, -one_float3(), one_float3());
183
184 kernel_assert(state->guiding.path_segment != nullptr);
185 if (state->guiding.path_segment != nullptr) {
186 openpgl::cpp::SetTransmittanceWeight(state->guiding.path_segment, guiding_vec3f(one_float3()));
187 openpgl::cpp::SetVolumeScatter(state->guiding.path_segment, false);
188 openpgl::cpp::SetNormal(state->guiding.path_segment, guiding_vec3f(normal));
189 openpgl::cpp::SetDirectionIn(state->guiding.path_segment, guiding_vec3f(wo));
190 openpgl::cpp::SetPDFDirectionIn(state->guiding.path_segment, pdf);
191 openpgl::cpp::SetScatteringWeight(state->guiding.path_segment, guiding_vec3f(weight_rgb));
192 openpgl::cpp::SetIsDelta(state->guiding.path_segment, is_delta);
193 openpgl::cpp::SetEta(state->guiding.path_segment, eta);
194 openpgl::cpp::SetRoughness(state->guiding.path_segment, min_roughness);
195 }
196#endif
197}
198
199/* Records the emission at the current surface intersection (physical or virtual) */
204 ccl_attr_maybe_unused const float mis_weight)
205{
206#if defined(__PATH_GUIDING__) && PATH_GUIDING_LEVEL >= 1
207 if (!kernel_data.integrator.train_guiding) {
208 return;
209 }
210
212
213 kernel_assert(state->guiding.path_segment != nullptr);
214 if (state->guiding.path_segment != nullptr) {
215 const float3 Le_rgb = spectrum_to_rgb(Le);
216
217 openpgl::cpp::SetDirectContribution(state->guiding.path_segment, guiding_vec3f(Le_rgb));
218 openpgl::cpp::SetMiWeight(state->guiding.path_segment, mis_weight);
219 }
220#endif
221}
222
223/* Record BSSRDF Interactions */
224
225/* Records/Adds a new path segment where the vertex position is the point of entry
226 * of the sub surface scattering boundary.
227 * If the path is not terminated this call is usually followed by a call of
228 * guiding_record_bssrdf_weight and guiding_record_bssrdf_bounce. */
231 state,
234{
235#if defined(__PATH_GUIDING__) && PATH_GUIDING_LEVEL >= 1
236 if (!kernel_data.integrator.train_guiding) {
237 return;
238 }
239
241
242 const pgl_vec3f zero = guiding_vec3f(zero_float3());
243 const pgl_vec3f one = guiding_vec3f(one_float3());
244
245 state->guiding.path_segment = guiding_next_segment(kg);
246
247 if (state->guiding.path_segment != nullptr) {
248 kernel_assert(is_guiding_valid(P));
249 float3 p = clamp_guiding_position(P);
250 openpgl::cpp::SetPosition(state->guiding.path_segment, guiding_point3f(p));
251 openpgl::cpp::SetDirectionOut(state->guiding.path_segment, guiding_vec3f(wi));
252 openpgl::cpp::SetVolumeScatter(state->guiding.path_segment, true);
253 openpgl::cpp::SetScatteredContribution(state->guiding.path_segment, zero);
254 openpgl::cpp::SetDirectContribution(state->guiding.path_segment, zero);
255 openpgl::cpp::SetTransmittanceWeight(state->guiding.path_segment, one);
256 openpgl::cpp::SetEta(state->guiding.path_segment, 1.0);
257 }
258#endif
259}
260
261/* Records the transmission of the path at the point of entry while passing
262 * the surface boundary. */
266 ccl_attr_maybe_unused const Spectrum weight,
267 ccl_attr_maybe_unused const Spectrum albedo)
268{
269#if defined(__PATH_GUIDING__) && PATH_GUIDING_LEVEL >= 1
270 if (!kernel_data.integrator.train_guiding) {
271 return;
272 }
273
275
276 /* NOTE: Albedo left out here, will be included in guiding_record_bssrdf_bounce. */
277 const float3 weight_rgb = spectrum_to_rgb(safe_divide_color(weight, albedo));
278
279 kernel_assert(state->guiding.path_segment != nullptr);
280 if (state->guiding.path_segment != nullptr) {
281 openpgl::cpp::SetTransmittanceWeight(state->guiding.path_segment,
282 guiding_vec3f(zero_float3()));
283 openpgl::cpp::SetScatteringWeight(state->guiding.path_segment, guiding_vec3f(weight_rgb));
284 openpgl::cpp::SetIsDelta(state->guiding.path_segment, false);
285 openpgl::cpp::SetEta(state->guiding.path_segment, 1.0f);
286 openpgl::cpp::SetRoughness(state->guiding.path_segment, 1.0f);
287 }
288#endif
289}
290
291/* Records the direction at the point of entry the path takes when sampling the SSS contribution.
292 * If not terminated this function is usually followed by a call of
293 * guiding_record_volume_transmission to record the transmittance between the point of entry and
294 * the point of exit. */
298 ccl_attr_maybe_unused const float pdf,
301 ccl_attr_maybe_unused const Spectrum weight,
302 ccl_attr_maybe_unused const Spectrum albedo)
303{
304#if defined(__PATH_GUIDING__) && PATH_GUIDING_LEVEL >= 1
305 if (!kernel_data.integrator.train_guiding) {
306 return;
307 }
308
310
311 const float3 normal = clamp(N, -one_float3(), one_float3());
312 const float3 weight_rgb = spectrum_to_rgb(weight * albedo);
313
314 kernel_assert(state->guiding.path_segment != nullptr);
315 if (state->guiding.path_segment != nullptr) {
316 openpgl::cpp::SetVolumeScatter(state->guiding.path_segment, false);
317 openpgl::cpp::SetNormal(state->guiding.path_segment, guiding_vec3f(normal));
318 openpgl::cpp::SetDirectionIn(state->guiding.path_segment, guiding_vec3f(wo));
319 openpgl::cpp::SetPDFDirectionIn(state->guiding.path_segment, pdf);
320 openpgl::cpp::SetTransmittanceWeight(state->guiding.path_segment, guiding_vec3f(weight_rgb));
321 }
322#endif
323}
324
325/* Record Volume Interactions */
326
327/* Records/Adds a new path segment with the current path vertex being inside a volume.
328 * If the path is not terminated this call is usually followed by a call of
329 * guiding_record_volume_bounce. */
332 state,
335{
336#if defined(__PATH_GUIDING__) && PATH_GUIDING_LEVEL >= 1
337 if (!kernel_data.integrator.train_guiding) {
338 return;
339 }
340
342
343 const pgl_vec3f zero = guiding_vec3f(zero_float3());
344 const pgl_vec3f one = guiding_vec3f(one_float3());
345
346 state->guiding.path_segment = guiding_next_segment(kg);
347
348 if (state->guiding.path_segment != nullptr) {
349 kernel_assert(is_guiding_valid(P));
350 float3 p = clamp_guiding_position(P);
351 openpgl::cpp::SetPosition(state->guiding.path_segment, guiding_point3f(p));
352 openpgl::cpp::SetDirectionOut(state->guiding.path_segment, guiding_vec3f(I));
353 openpgl::cpp::SetVolumeScatter(state->guiding.path_segment, true);
354 openpgl::cpp::SetScatteredContribution(state->guiding.path_segment, zero);
355 openpgl::cpp::SetDirectContribution(state->guiding.path_segment, zero);
356 openpgl::cpp::SetTransmittanceWeight(state->guiding.path_segment, one);
357 openpgl::cpp::SetEta(state->guiding.path_segment, 1.0);
358 }
359#endif
360}
361
362/* Records the volume scattering event at the current vertex position of the segment. */
366 ccl_attr_maybe_unused const Spectrum weight,
367 ccl_attr_maybe_unused const float pdf,
369 ccl_attr_maybe_unused const float roughness)
370{
371#if defined(__PATH_GUIDING__) && PATH_GUIDING_LEVEL >= 4
372 if (!kernel_data.integrator.train_guiding) {
373 return;
374 }
375
377
378 const float3 weight_rgb = spectrum_to_rgb(weight);
379 const float3 normal = make_float3(0.0f, 0.0f, 1.0f);
380
381 kernel_assert(state->guiding.path_segment != nullptr);
382 if (state->guiding.path_segment != nullptr) {
383 openpgl::cpp::SetVolumeScatter(state->guiding.path_segment, true);
384 openpgl::cpp::SetTransmittanceWeight(state->guiding.path_segment, guiding_vec3f(one_float3()));
385 openpgl::cpp::SetNormal(state->guiding.path_segment, guiding_vec3f(normal));
386 openpgl::cpp::SetDirectionIn(state->guiding.path_segment, guiding_vec3f(wo));
387 openpgl::cpp::SetPDFDirectionIn(state->guiding.path_segment, pdf);
388 openpgl::cpp::SetScatteringWeight(state->guiding.path_segment, guiding_vec3f(weight_rgb));
389 openpgl::cpp::SetIsDelta(state->guiding.path_segment, false);
390 openpgl::cpp::SetEta(state->guiding.path_segment, 1.0f);
391 openpgl::cpp::SetRoughness(state->guiding.path_segment, roughness);
392 }
393#endif
394}
395
396/* Records the transmission (a.k.a. transmittance weight) between the current path segment
397 * and the next one, when the path is inside or passes a volume. */
401 ccl_attr_maybe_unused const float3 transmittance_weight)
402{
403#if defined(__PATH_GUIDING__) && PATH_GUIDING_LEVEL >= 1
404 if (!kernel_data.integrator.train_guiding) {
405 return;
406 }
407
409
410 if (state->guiding.path_segment) {
411 // TODO (sherholz): need to find a better way to avoid this check
412 if ((transmittance_weight[0] < 0.0f || !std::isfinite(transmittance_weight[0]) ||
413 std::isnan(transmittance_weight[0])) ||
414 (transmittance_weight[1] < 0.0f || !std::isfinite(transmittance_weight[1]) ||
415 std::isnan(transmittance_weight[1])) ||
416 (transmittance_weight[2] < 0.0f || !std::isfinite(transmittance_weight[2]) ||
417 std::isnan(transmittance_weight[2])))
418 {
419 }
420 else {
421 openpgl::cpp::SetTransmittanceWeight(state->guiding.path_segment,
422 guiding_vec3f(transmittance_weight));
423 }
424 }
425#endif
426}
427
428/* Records the emission of a volume at the vertex of the current path segment. */
431 state,
433{
434#if defined(__PATH_GUIDING__) && PATH_GUIDING_LEVEL >= 1
435 if (!kernel_data.integrator.train_guiding) {
436 return;
437 }
438
440
441 if (state->guiding.path_segment) {
442 const float3 Le_rgb = spectrum_to_rgb(Le);
443
444 openpgl::cpp::SetDirectContribution(state->guiding.path_segment, guiding_vec3f(Le_rgb));
445 openpgl::cpp::SetMiWeight(state->guiding.path_segment, 1.0f);
446 }
447#endif
448}
449
450/* Record Light Interactions */
451
452/* Adds a pseudo path vertex/segment when intersecting a virtual light source.
453 * (e.g., area, sphere, or disk light). This call is often followed
454 * a call of guiding_record_surface_emission, if the intersected light source
455 * emits light in the direction of the path. */
460{
461#if defined(__PATH_GUIDING__) && PATH_GUIDING_LEVEL >= 1
462 if (!kernel_data.integrator.train_guiding) {
463 return;
464 }
465
467
468 const pgl_vec3f zero = guiding_vec3f(zero_float3());
469 const pgl_vec3f one = guiding_vec3f(one_float3());
470 const float3 ray_P = INTEGRATOR_STATE(state, ray, P);
471 const float3 ray_D = INTEGRATOR_STATE(state, ray, D);
472 const float3 P = ray_P + isect->t * ray_D;
473
474 state->guiding.path_segment = guiding_next_segment(kg);
475
476 if (state->guiding.path_segment != nullptr) {
477 kernel_assert(is_guiding_valid(P));
478 float3 p = clamp_guiding_position(P);
479 openpgl::cpp::SetPosition(state->guiding.path_segment, guiding_point3f(p));
480 openpgl::cpp::SetDirectionOut(state->guiding.path_segment, guiding_vec3f(-ray_D));
481 openpgl::cpp::SetNormal(state->guiding.path_segment, guiding_vec3f(-ray_D));
482 openpgl::cpp::SetDirectionIn(state->guiding.path_segment, guiding_vec3f(ray_D));
483 openpgl::cpp::SetPDFDirectionIn(state->guiding.path_segment, 1.0f);
484 openpgl::cpp::SetVolumeScatter(state->guiding.path_segment, false);
485 openpgl::cpp::SetScatteredContribution(state->guiding.path_segment, zero);
486 openpgl::cpp::SetDirectContribution(state->guiding.path_segment, zero);
487 openpgl::cpp::SetTransmittanceWeight(state->guiding.path_segment, one);
488 openpgl::cpp::SetScatteringWeight(state->guiding.path_segment, one);
489 openpgl::cpp::SetEta(state->guiding.path_segment, 1.0f);
490 }
491#endif
492}
493
494/* Records/Adds a final path segment when the path leaves the scene and
495 * intersects with a background light (e.g., background color,
496 * sun light, or env map). The vertex for this segment is placed along
497 * the current ray far out the scene. */
501 ccl_attr_maybe_unused const float mis_weight)
502{
503#if defined(__PATH_GUIDING__) && PATH_GUIDING_LEVEL >= 1
504 if (!kernel_data.integrator.train_guiding) {
505 return;
506 }
507
509
510 const float3 L_rgb = spectrum_to_rgb(L);
511 const float3 ray_P = INTEGRATOR_STATE(state, ray, P);
512 const float3 ray_D = INTEGRATOR_STATE(state, ray, D);
513 float3 P = ray_P + GUIDING_MAX_LIGHT_DISTANCE * ray_D;
514 kernel_assert(is_guiding_valid(P));
515 P = clamp_guiding_position(P);
516 const float3 normal = make_float3(0.0f, 0.0f, 1.0f);
517
518 openpgl::cpp::PathSegment background_segment;
519 openpgl::cpp::SetPosition(&background_segment, guiding_vec3f(P));
520 openpgl::cpp::SetNormal(&background_segment, guiding_vec3f(normal));
521 openpgl::cpp::SetDirectionOut(&background_segment, guiding_vec3f(-ray_D));
522 openpgl::cpp::SetDirectContribution(&background_segment, guiding_vec3f(L_rgb));
523 openpgl::cpp::SetMiWeight(&background_segment, mis_weight);
524 if (guiding_has_next_segment(kg)) {
525 kg->opgl_path_segment_storage->AddSegment(background_segment);
526 }
527#endif
528}
529
530/* Records direct lighting from either next event estimation or a dedicated BSDF
531 * sampled shadow ray. */
534{
535#if defined(__PATH_GUIDING__) && PATH_GUIDING_LEVEL >= 1
536 if (!kernel_data.integrator.train_guiding) {
537 return;
538 }
539 const uint32_t path_flag = INTEGRATOR_STATE(state, shadow_path, flag);
540 if (path_flag & PATH_RAY_SHADOW_FOR_AO) {
541 return;
542 }
543 if (state->shadow_path.path_segment) {
544 /* Estimating the out-scattered radiance at the current path segment.
545 * NOTE: in the light linking case this estimate is the incoming radiance.*/
546 const Spectrum Lo = safe_divide_color(INTEGRATOR_STATE(state, shadow_path, throughput),
548 const float3 Lo_rgb = spectrum_to_rgb(Lo);
549
550 if (!(path_flag & PATH_RAY_SHADOW_FOR_LIGHT_LINKING)) {
551 /* Scattered contribution of a next event estimation (i.e., a direct light estimate
552 * scattered at the current path vertex towards the previous vertex). */
553 openpgl::cpp::AddScatteredContribution(state->shadow_path.path_segment,
554 guiding_vec3f(Lo_rgb));
555 }
556 else {
557 /* The contribution comes from a light linking forward ray. We need to record this
558 * contribution as scattered contribution at the current path segment. To be able to guide
559 * towards this light source we add a directional sample directly to the guiding
560 * training data storage. */
561 const float3 scattering_weight = make_float3(
562 state->shadow_path.path_segment->scatteringWeight);
563 openpgl::cpp::AddScatteredContribution(state->shadow_path.path_segment,
564 guiding_vec3f(scattering_weight * Lo_rgb));
565
566 /* Adding an additional training sample for the guiding cache in the direction of the linked
567 * light source. */
568 float dist = INTEGRATOR_STATE(state, shadow_ray, tmax);
569 openpgl::cpp::SampleData pgl_sample;
570 pgl_sample.direction = state->shadow_path.path_segment->directionIn;
571 pgl_sample.pdf = state->shadow_path.path_segment->pdfDirectionIn;
572 pgl_sample.position = state->shadow_path.path_segment->position;
573 pgl_sample.flags = state->shadow_path.path_segment->volumeScatter ?
574 openpgl::cpp::SampleData::EInsideVolume :
575 0;
576 pgl_sample.weight = safe_divide(reduce_max(Lo_rgb), pgl_sample.pdf);
577 if (!kernel_data.integrator.use_guiding_mis_weights) {
578 const float mis_weight = INTEGRATOR_STATE(
579 state, shadow_path, guiding_light_linking_mis_weight);
580 pgl_sample.weight = safe_divide(pgl_sample.weight, mis_weight);
581 }
582
583 /* Checking if the light source is an infinite one (e.g., background, sun). If so the
584 * distance is set to GUIDING_MAX_LIGHT_DISTANCE.
585 * NOTE: checking for FLT_MAX is not working.*/
586 pgl_sample.distance = dist > GUIDING_FLT_LARGE ? GUIDING_MAX_LIGHT_DISTANCE : dist;
587 kg->opgl_sample_data_storage->AddSample(pgl_sample);
588 }
589 }
590#endif
591}
592
593/* Record Russian Roulette */
594/* Records the probability of continuing the path at the current path segment. */
598 ccl_attr_maybe_unused const float continuation_probability)
599{
600#if defined(__PATH_GUIDING__) && PATH_GUIDING_LEVEL >= 1
601 if (!kernel_data.integrator.train_guiding) {
602 return;
603 }
604
606
607 if (state->guiding.path_segment) {
608 openpgl::cpp::SetRussianRouletteProbability(state->guiding.path_segment,
609 continuation_probability);
610 }
611#endif
612}
613
614/* Path guiding debug render passes. */
615
616/* Write a set of path guiding related debug information (e.g., guiding probability at first
617 * bounce) into separate rendering passes. */
621 ccl_attr_maybe_unused const ccl_private ShaderData *sd,
623{
624#if defined(__PATH_GUIDING__) && PATH_GUIDING_LEVEL >= 4
625# ifdef WITH_CYCLES_DEBUG
626 if (!kernel_data.integrator.train_guiding) {
627 return;
628 }
629
630 if (INTEGRATOR_STATE(state, path, bounce) != 0) {
631 return;
632 }
633
635
636 if (kernel_data.film.pass_guiding_probability != PASS_UNUSED) {
637 float guiding_prob = state->guiding.surface_guiding_sampling_prob;
638 film_write_pass_float(buffer + kernel_data.film.pass_guiding_probability, guiding_prob);
639 }
640
641 if (kernel_data.film.pass_guiding_avg_roughness != PASS_UNUSED) {
642 float avg_roughness = 0.0f;
643 float sum_sample_weight = 0.0f;
644 for (int i = 0; i < sd->num_closure; i++) {
645 const ccl_private ShaderClosure *sc = &sd->closure[i];
646
647 if (!CLOSURE_IS_BSDF_OR_BSSRDF(sc->type)) {
648 continue;
649 }
650 avg_roughness += sc->sample_weight * bsdf_get_specular_roughness_squared(sc);
651 sum_sample_weight += sc->sample_weight;
652 }
653
654 avg_roughness = avg_roughness > 0.0f ? avg_roughness / sum_sample_weight : 0.0f;
655
656 film_write_pass_float(buffer + kernel_data.film.pass_guiding_avg_roughness, avg_roughness);
657 }
658# else
659 (void)kg;
660 (void)state;
661 (void)sd;
662 (void)render_buffer;
663# endif
664#endif
665}
666
667/* Guided BSDFs */
668
673{
674#if defined(__PATH_GUIDING__) && PATH_GUIDING_LEVEL >= 4
675 if (guiding_ssd->Init(guiding_guiding_field, guiding_point3f(P), rand)) {
676 guiding_ssd->ApplyCosineProduct(guiding_point3f(N));
677 return true;
678 }
679#endif
680 return false;
681}
682
684 ccl_attr_maybe_unused const float2 rand_bsdf,
686{
687#if defined(__PATH_GUIDING__) && PATH_GUIDING_LEVEL >= 4
688 pgl_vec3f pgl_wo;
689 const pgl_point2f rand = {rand_bsdf.x, rand_bsdf.y};
690 const float pdf = guiding_ssd->SamplePDF(rand, pgl_wo);
691 *wo = make_float3(pgl_wo.x, pgl_wo.y, pgl_wo.z);
692 return pdf;
693#else
694 return 0.0f;
695#endif
696}
697
700{
701#if defined(__PATH_GUIDING__) && PATH_GUIDING_LEVEL >= 4
702 return guiding_ssd->PDF(guiding_vec3f(wo));
703#else
704 return 0.0f;
705#endif
706}
707
710{
711#if defined(__PATH_GUIDING__) && PATH_GUIDING_LEVEL >= 4
712 return guiding_ssd->IncomingRadiancePDF(guiding_vec3f(wo));
713#else
714 return 0.0f;
715#endif
716}
717
718/* Guided Volume Phases */
719
723 ccl_attr_maybe_unused const float g,
725{
726#if defined(__PATH_GUIDING__) && PATH_GUIDING_LEVEL >= 4
727 /* we do not need to guide almost delta phase functions */
728 if (fabsf(g) >= 0.99f) {
729 return false;
730 }
731
732 if (guiding_vsd->Init(guiding_guiding_field, guiding_point3f(P), rand)) {
733 guiding_vsd->ApplySingleLobeHenyeyGreensteinProduct(guiding_vec3f(D), g);
734 return true;
735 }
736#endif
737
738 return false;
739}
740
742 ccl_attr_maybe_unused const float2 rand_phase,
744{
745#if defined(__PATH_GUIDING__) && PATH_GUIDING_LEVEL >= 4
746 pgl_vec3f pgl_wo;
747 const pgl_point2f rand = {rand_phase.x, rand_phase.y};
748 const float pdf = guiding_vsd->SamplePDF(rand, pgl_wo);
749 *wo = make_float3(pgl_wo.x, pgl_wo.y, pgl_wo.z);
750 return pdf;
751#else
752 return 0.0f;
753#endif
754}
755
758{
759#if defined(__PATH_GUIDING__) && PATH_GUIDING_LEVEL >= 4
760 return guiding_vsd->PDF(guiding_vec3f(wo));
761#else
762 return 0.0f;
763#endif
764}
765
#define D
CCL_NAMESPACE_BEGIN ccl_device_inline float bsdf_get_specular_roughness_squared(const ccl_private ShaderClosure *sc)
Definition bsdf.h:29
ccl_device_inline Spectrum safe_divide_color(Spectrum a, Spectrum b, const float fallback=0.0f)
Definition color.h:376
#define kernel_assert(cond)
#define kernel_data
#define ccl_attr_maybe_unused
#define PASS_UNUSED
#define ccl_restrict
#define ccl_device_forceinline
#define zero_spectrum
#define ccl_private
#define ccl_global
#define CLOSURE_IS_BSDF_OR_BSSRDF(type)
#define CCL_NAMESPACE_END
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
constexpr T clamp(T, U, U) RET
float safe_divide(float a, float b)
float reduce_max(float2 a)
const int state
ccl_gpu_kernel_postfix ccl_global KernelWorkTile const int ccl_global float * render_buffer
ccl_device_forceinline void guiding_record_surface_segment(ccl_attr_maybe_unused KernelGlobals kg, ccl_attr_maybe_unused IntegratorState state, ccl_attr_maybe_unused const ccl_private ShaderData *sd)
ccl_device_forceinline void guiding_record_direct_light(ccl_attr_maybe_unused KernelGlobals kg, ccl_attr_maybe_unused IntegratorShadowState 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 void guiding_record_bssrdf_bounce(ccl_attr_maybe_unused KernelGlobals kg, ccl_attr_maybe_unused IntegratorState state, ccl_attr_maybe_unused const float pdf, ccl_attr_maybe_unused const float3 N, ccl_attr_maybe_unused const float3 wo, ccl_attr_maybe_unused const Spectrum weight, ccl_attr_maybe_unused const Spectrum albedo)
#define GUIDING_FLT_LARGE
ccl_device_forceinline void guiding_record_volume_segment(ccl_attr_maybe_unused KernelGlobals kg, ccl_attr_maybe_unused IntegratorState state, ccl_attr_maybe_unused const float3 P, ccl_attr_maybe_unused const float3 I)
ccl_device_forceinline float guiding_phase_sample(ccl_attr_maybe_unused KernelGlobals kg, ccl_attr_maybe_unused const float2 rand_phase, ccl_attr_maybe_unused ccl_private float3 *wo)
#define GUIDING_MAX_LIGHT_DISTANCE
ccl_device_forceinline void guiding_record_surface_emission(ccl_attr_maybe_unused KernelGlobals kg, ccl_attr_maybe_unused IntegratorState state, ccl_attr_maybe_unused const Spectrum Le, ccl_attr_maybe_unused const float mis_weight)
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 void guiding_record_bssrdf_segment(ccl_attr_maybe_unused KernelGlobals kg, ccl_attr_maybe_unused IntegratorState state, ccl_attr_maybe_unused const float3 P, ccl_attr_maybe_unused const float3 wi)
ccl_device_forceinline void guiding_record_continuation_probability(ccl_attr_maybe_unused KernelGlobals kg, ccl_attr_maybe_unused IntegratorState state, ccl_attr_maybe_unused const float continuation_probability)
ccl_device_forceinline void guiding_record_volume_bounce(ccl_attr_maybe_unused KernelGlobals kg, ccl_attr_maybe_unused IntegratorState state, ccl_attr_maybe_unused const Spectrum weight, ccl_attr_maybe_unused const float pdf, ccl_attr_maybe_unused const float3 wo, ccl_attr_maybe_unused const float roughness)
ccl_device_forceinline void guiding_write_debug_passes(ccl_attr_maybe_unused KernelGlobals kg, ccl_attr_maybe_unused IntegratorState state, ccl_attr_maybe_unused const ccl_private ShaderData *sd, ccl_attr_maybe_unused ccl_global float *ccl_restrict render_buffer)
ccl_device_forceinline void guiding_record_light_surface_segment(ccl_attr_maybe_unused KernelGlobals kg, ccl_attr_maybe_unused IntegratorState state, ccl_attr_maybe_unused const ccl_private Intersection *ccl_restrict isect)
ccl_device_forceinline void guiding_record_volume_transmission(ccl_attr_maybe_unused KernelGlobals kg, ccl_attr_maybe_unused IntegratorState state, ccl_attr_maybe_unused const float3 transmittance_weight)
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 void guiding_record_bssrdf_weight(ccl_attr_maybe_unused KernelGlobals kg, ccl_attr_maybe_unused IntegratorState state, ccl_attr_maybe_unused const Spectrum weight, ccl_attr_maybe_unused const Spectrum albedo)
ccl_device_forceinline void guiding_record_background(ccl_attr_maybe_unused KernelGlobals kg, ccl_attr_maybe_unused IntegratorState state, ccl_attr_maybe_unused const Spectrum L, ccl_attr_maybe_unused const float mis_weight)
ccl_device_forceinline bool guiding_phase_init(ccl_attr_maybe_unused KernelGlobals kg, ccl_attr_maybe_unused const float3 P, ccl_attr_maybe_unused const float3 D, ccl_attr_maybe_unused const float g, ccl_attr_maybe_unused ccl_private float &rand)
ccl_device_forceinline void guiding_record_volume_emission(ccl_attr_maybe_unused KernelGlobals kg, ccl_attr_maybe_unused IntegratorState state, ccl_attr_maybe_unused const Spectrum Le)
ccl_device_forceinline void guiding_record_surface_bounce(ccl_attr_maybe_unused KernelGlobals kg, ccl_attr_maybe_unused IntegratorState state, ccl_attr_maybe_unused const Spectrum weight, ccl_attr_maybe_unused const float pdf, ccl_attr_maybe_unused const float3 N, ccl_attr_maybe_unused const float3 wo, ccl_attr_maybe_unused const float2 roughness, ccl_attr_maybe_unused const float eta)
ccl_device_forceinline float guiding_phase_pdf(ccl_attr_maybe_unused KernelGlobals kg, ccl_attr_maybe_unused const float3 wo)
ccl_device_forceinline float guiding_bsdf_pdf(ccl_attr_maybe_unused KernelGlobals kg, ccl_attr_maybe_unused const float3 wo)
@ PATH_RAY_SHADOW_FOR_AO
@ PATH_RAY_SHADOW_CATCHER_PASS
@ PATH_RAY_SHADOW_FOR_LIGHT_LINKING
ccl_device_inline float3 spectrum_to_rgb(Spectrum s)
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_device_inline float3 one_float3()
Definition math_float3.h:26
CCL_NAMESPACE_BEGIN ccl_device_inline float3 zero_float3()
Definition math_float3.h:17
#define N
#define L
ATTR_WARN_UNUSED_RESULT const BMVert * v
#define I
#define fabsf
#define fminf
#define M_PI_F
KERNEL_FEATURE_LIGHT_PASSES unlit_throughput
IntegratorShadowStateCPU * IntegratorShadowState
Definition state.h:229
#define INTEGRATOR_STATE(state, nested_struct, member)
Definition state.h:234
IntegratorStateCPU * IntegratorState
Definition state.h:227
ccl_device_inline_method bool contains(T value) const
Definition math_base.h:926
float3 Spectrum
ccl_device_inline void film_write_pass_float(ccl_global float *ccl_restrict buffer, const float value)
Definition write.h:58
CCL_NAMESPACE_BEGIN ccl_device_forceinline ccl_global float * film_pass_pixel_render_buffer(KernelGlobals kg, ConstIntegratorState state, ccl_global float *ccl_restrict render_buffer)
Definition write.h:24