21#define GUIDING_FLT_LARGE 1.844e18f
22#define GUIDING_MAX_LIGHT_DISTANCE 1e6f
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)
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;
61 ris_sample->ris_target = 0.0f;
62 ris_sample->ris_pdf = 0.0f;
69#if defined(__PATH_GUIDING__)
70static pgl_vec3f guiding_vec3f(
const float3 v)
72 return {
v.x,
v.y,
v.z};
77 return {
v.x,
v.y,
v.z};
106 return kg->opgl_path_segment_storage->GetNumSegments() < kg->opgl_path_segment_storage_size;
111 const bool has_next_segment = guiding_has_next_segment(kg);
117 return has_next_segment ? kg->opgl_path_segment_storage->NextSegment() :
nullptr;
134#if defined(__PATH_GUIDING__) && PATH_GUIDING_LEVEL >= 1
141 const pgl_vec3f zero = guiding_vec3f(
zero_float3());
142 const pgl_vec3f one = guiding_vec3f(
one_float3());
144 state->guiding.path_segment = guiding_next_segment(kg);
146 if (
state->guiding.path_segment !=
nullptr) {
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);
172#if defined(__PATH_GUIDING__) && PATH_GUIDING_LEVEL >= 4
179 const float min_roughness =
safe_sqrtf(
fminf(roughness.x, roughness.y));
180 const bool is_delta = (min_roughness == 0.0f);
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);
206#if defined(__PATH_GUIDING__) && PATH_GUIDING_LEVEL >= 1
214 if (
state->guiding.path_segment !=
nullptr) {
217 openpgl::cpp::SetDirectContribution(
state->guiding.path_segment, guiding_vec3f(Le_rgb));
218 openpgl::cpp::SetMiWeight(
state->guiding.path_segment, mis_weight);
235#if defined(__PATH_GUIDING__) && PATH_GUIDING_LEVEL >= 1
242 const pgl_vec3f zero = guiding_vec3f(
zero_float3());
243 const pgl_vec3f one = guiding_vec3f(
one_float3());
245 state->guiding.path_segment = guiding_next_segment(kg);
247 if (
state->guiding.path_segment !=
nullptr) {
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);
269#if defined(__PATH_GUIDING__) && PATH_GUIDING_LEVEL >= 1
280 if (
state->guiding.path_segment !=
nullptr) {
281 openpgl::cpp::SetTransmittanceWeight(
state->guiding.path_segment,
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);
304#if defined(__PATH_GUIDING__) && PATH_GUIDING_LEVEL >= 1
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));
336#if defined(__PATH_GUIDING__) && PATH_GUIDING_LEVEL >= 1
343 const pgl_vec3f zero = guiding_vec3f(
zero_float3());
344 const pgl_vec3f one = guiding_vec3f(
one_float3());
346 state->guiding.path_segment = guiding_next_segment(kg);
348 if (
state->guiding.path_segment !=
nullptr) {
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);
371#if defined(__PATH_GUIDING__) && PATH_GUIDING_LEVEL >= 4
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);
403#if defined(__PATH_GUIDING__) && PATH_GUIDING_LEVEL >= 1
410 if (
state->guiding.path_segment) {
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])))
421 openpgl::cpp::SetTransmittanceWeight(
state->guiding.path_segment,
422 guiding_vec3f(transmittance_weight));
434#if defined(__PATH_GUIDING__) && PATH_GUIDING_LEVEL >= 1
441 if (
state->guiding.path_segment) {
444 openpgl::cpp::SetDirectContribution(
state->guiding.path_segment, guiding_vec3f(Le_rgb));
445 openpgl::cpp::SetMiWeight(
state->guiding.path_segment, 1.0f);
461#if defined(__PATH_GUIDING__) && PATH_GUIDING_LEVEL >= 1
468 const pgl_vec3f zero = guiding_vec3f(
zero_float3());
469 const pgl_vec3f one = guiding_vec3f(
one_float3());
472 const float3 P = ray_P + isect->t * ray_D;
474 state->guiding.path_segment = guiding_next_segment(kg);
476 if (
state->guiding.path_segment !=
nullptr) {
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);
503#if defined(__PATH_GUIDING__) && PATH_GUIDING_LEVEL >= 1
515 P = clamp_guiding_position(
P);
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);
535#if defined(__PATH_GUIDING__) && PATH_GUIDING_LEVEL >= 1
543 if (
state->shadow_path.path_segment) {
553 openpgl::cpp::AddScatteredContribution(
state->shadow_path.path_segment,
554 guiding_vec3f(Lo_rgb));
562 state->shadow_path.path_segment->scatteringWeight);
563 openpgl::cpp::AddScatteredContribution(
state->shadow_path.path_segment,
564 guiding_vec3f(scattering_weight * Lo_rgb));
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 :
577 if (!
kernel_data.integrator.use_guiding_mis_weights) {
579 state, shadow_path, guiding_light_linking_mis_weight);
580 pgl_sample.weight =
safe_divide(pgl_sample.weight, mis_weight);
587 kg->opgl_sample_data_storage->AddSample(pgl_sample);
600#if defined(__PATH_GUIDING__) && PATH_GUIDING_LEVEL >= 1
607 if (
state->guiding.path_segment) {
608 openpgl::cpp::SetRussianRouletteProbability(
state->guiding.path_segment,
609 continuation_probability);
624#if defined(__PATH_GUIDING__) && PATH_GUIDING_LEVEL >= 4
625# ifdef WITH_CYCLES_DEBUG
637 float guiding_prob =
state->guiding.surface_guiding_sampling_prob;
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];
651 sum_sample_weight += sc->sample_weight;
654 avg_roughness = avg_roughness > 0.0f ? avg_roughness / sum_sample_weight : 0.0f;
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));
687#if defined(__PATH_GUIDING__) && PATH_GUIDING_LEVEL >= 4
689 const pgl_point2f rand = {rand_bsdf.x, rand_bsdf.y};
690 const float pdf = guiding_ssd->SamplePDF(rand, pgl_wo);
701#if defined(__PATH_GUIDING__) && PATH_GUIDING_LEVEL >= 4
702 return guiding_ssd->PDF(guiding_vec3f(wo));
711#if defined(__PATH_GUIDING__) && PATH_GUIDING_LEVEL >= 4
712 return guiding_ssd->IncomingRadiancePDF(guiding_vec3f(wo));
726#if defined(__PATH_GUIDING__) && PATH_GUIDING_LEVEL >= 4
728 if (
fabsf(g) >= 0.99f) {
732 if (guiding_vsd->Init(guiding_guiding_field, guiding_point3f(
P), rand)) {
733 guiding_vsd->ApplySingleLobeHenyeyGreensteinProduct(guiding_vec3f(
D), g);
745#if defined(__PATH_GUIDING__) && PATH_GUIDING_LEVEL >= 4
747 const pgl_point2f rand = {rand_phase.x, rand_phase.y};
748 const float pdf = guiding_vsd->SamplePDF(rand, pgl_wo);
759#if defined(__PATH_GUIDING__) && PATH_GUIDING_LEVEL >= 4
760 return guiding_vsd->PDF(guiding_vec3f(wo));
CCL_NAMESPACE_BEGIN ccl_device_inline float bsdf_get_specular_roughness_squared(const ccl_private ShaderClosure *sc)
ccl_device_inline Spectrum safe_divide_color(Spectrum a, Spectrum b, const float fallback=0.0f)
#define kernel_assert(cond)
#define ccl_attr_maybe_unused
#define ccl_device_forceinline
#define CLOSURE_IS_BSDF_OR_BSSRDF(type)
#define CCL_NAMESPACE_END
constexpr T clamp(T, U, U) RET
float safe_divide(float a, float b)
float reduce_max(float2 a)
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_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)
ccl_device_inline bool isfinite_safe(const float f)
ccl_device_inline float3 one_float3()
CCL_NAMESPACE_BEGIN ccl_device_inline float3 zero_float3()
ATTR_WARN_UNUSED_RESULT const BMVert * v
KERNEL_FEATURE_LIGHT_PASSES unlit_throughput
IntegratorShadowStateCPU * IntegratorShadowState
#define INTEGRATOR_STATE(state, nested_struct, member)
IntegratorStateCPU * IntegratorState
float incoming_radiance_pdf
ccl_device_inline_method bool contains(T value) const
ccl_device_inline void film_write_pass_float(ccl_global float *ccl_restrict buffer, const float value)
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)