Blender V5.3
kernel/closure/volume.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
14
16
17/* VOLUME EXTINCTION */
18
20{
21 if (sd->runtime_flag & SR_EXTINCTION) {
22 sd->closure_transparent_extinction += weight;
23 }
24 else {
25 sd->runtime_flag |= SR_EXTINCTION;
26 sd->closure_transparent_extinction = weight;
27 }
28}
29
30/* VOLUME SCATTERING */
31
34 const float3 wo,
35 ccl_private float *pdf)
36{
37 switch (svc->type) {
39 return volume_fournier_forand_eval(sd, svc, wo, pdf);
41 return volume_rayleigh_eval(sd, wo, pdf);
43 return volume_draine_eval(sd, svc, wo, pdf);
45 return volume_henyey_greenstein_eval(sd, svc, wo, pdf);
46 default:
47 kernel_assert(false);
48 *pdf = 0.0f;
49 return zero_spectrum();
50 }
51}
52
55 const float2 rand,
58 ccl_private float *pdf)
59{
60 switch (svc->type) {
62 return volume_fournier_forand_sample(sd, svc, rand, eval, wo, pdf);
64 return volume_rayleigh_sample(sd, rand, eval, wo, pdf);
66 return volume_draine_sample(sd, svc, rand, eval, wo, pdf);
68 return volume_henyey_greenstein_sample(sd, svc, rand, eval, wo, pdf);
69 default:
70 kernel_assert(false);
71 *pdf = 0.0f;
72 return 0;
73 }
74}
75
76/* Widen the compact ray differential dD after a phase function scatter to
77 * match the lobe's angular spread. See bsdf_widen_dD for details. */
79 const float prev_dD,
80 const float sampled_roughness)
81{
82 return max(prev_dD, kernel_data.integrator.differential_widen_scale * sampled_roughness);
83}
84
85ccl_device bool volume_phase_equal(const ccl_private ShaderClosure *c1,
86 const ccl_private ShaderClosure *c2)
87{
88 if (c1->type != c2->type) {
89 return false;
90 }
91 switch (c1->type) {
95 return v1->c1 == v2->c1 && v1->c2 == v2->c2 && v1->c3 == v2->c3;
96 }
98 return true;
102 return v1->g == v2->g && v1->alpha == v2->alpha;
103 }
107 return v1->g == v2->g;
108 }
109 default:
110 return false;
111 }
112 return false;
113}
114
115/* Approximate phase functions as Henyey-Greenstein for volume guiding.
116 * TODO: This is not ideal, we should use RIS guiding for non-HG phase functions. */
118{
119 switch (svc->type) {
121 /* TODO */
122 return 1.0f;
124 /* Approximate as isotropic */
125 return 0.0f;
127 /* Approximate as HG, TODO */
128 return ((ccl_private DraineVolume *)svc)->g;
130 return ((ccl_private HenyeyGreensteinVolume *)svc)->g;
131 default:
132 return 0.0f;
133 }
134}
135
136/* Volume sampling utilities. */
137
138/* Ignore paths that have volume throughput below this value, to avoid unnecessary work
139 * and precision issues.
140 * TODO: this value could be tweaked or turned into a probability to avoid unnecessary work in
141 * volumes and subsurface scattering. */
142#define VOLUME_THROUGHPUT_EPSILON 1e-6f
143
145{
146 return exp(-sigma * t);
147}
148
149ccl_device float volume_channel_get(Spectrum value, const int channel)
150{
151 return GET_SPECTRUM_CHANNEL(value, channel);
152}
153
154/* Sample color channel proportional to throughput and single scattering albedo, to significantly
155 * reduce noise with many bounce, following:
156 *
157 * "Practical and Controllable Subsurface Scattering for Production Path Tracing".
158 * Matt Jen-Yuan Chiang, Peter Kutz, Brent Burley. SIGGRAPH 2016. */
160{
161 const Spectrum weights = fabs(throughput * albedo);
162 const float sum_weights = reduce_add(weights);
163
164 if ((1.0f - sum_weights) < 1.0f) {
165 /* The same as `sum_weights > 0.0f`, but avoids the case where `sum_weight` is denormal, which
166 * could produce `nan` after division. */
167 return weights / sum_weights;
168 }
169
170 return make_spectrum(1.0f / SPECTRUM_CHANNELS);
171}
172
174 Spectrum throughput,
175 ccl_private float *rand,
177{
178 *pdf = volume_sample_channel_pdf(albedo, throughput);
179
180 float pdf_sum = 0.0f;
182 const float channel_pdf = GET_SPECTRUM_CHANNEL(*pdf, i);
183 if (*rand < pdf_sum + channel_pdf) {
184 /* Rescale to reuse. */
185 *rand = (*rand - pdf_sum) / channel_pdf;
186 return i;
187 }
188 pdf_sum += channel_pdf;
189 }
190 return SPECTRUM_CHANNELS - 1;
191}
192
btScalar max
#define kernel_assert(cond)
#define kernel_data
#define ccl_device_forceinline
#define SPECTRUM_CHANNELS
#define zero_spectrum
#define make_spectrum(f)
#define ccl_private
#define ccl_device_inline
#define FOREACH_SPECTRUM_CHANNEL(counter)
#define GET_SPECTRUM_CHANNEL(v, i)
#define CCL_NAMESPACE_END
#define exp
float reduce_add(float2 a)
CCL_NAMESPACE_BEGIN ccl_device void volume_extinction_setup(ccl_private ShaderData *sd, Spectrum weight)
ccl_device_forceinline float volume_phase_widen_dD(const KernelGlobals kg, const float prev_dD, const float sampled_roughness)
ccl_device bool volume_phase_equal(const ccl_private ShaderClosure *c1, const ccl_private ShaderClosure *c2)
ccl_device Spectrum volume_color_transmittance(Spectrum sigma, const float t)
ccl_device int volume_sample_channel(Spectrum albedo, Spectrum throughput, ccl_private float *rand, ccl_private Spectrum *pdf)
ccl_device Spectrum volume_phase_eval(const ccl_private ShaderData *sd, const ccl_private ShaderVolumeClosure *svc, const float3 wo, ccl_private float *pdf)
ccl_device_inline Spectrum volume_sample_channel_pdf(Spectrum albedo, Spectrum throughput)
ccl_device float volume_phase_get_g(const ccl_private ShaderVolumeClosure *svc)
ccl_device float volume_channel_get(Spectrum value, const int channel)
ccl_device int volume_phase_sample(const ccl_private ShaderData *sd, const ccl_private ShaderVolumeClosure *svc, const float2 rand, ccl_private Spectrum *eval, ccl_private float3 *wo, ccl_private float *pdf)
@ CLOSURE_VOLUME_RAYLEIGH_ID
@ CLOSURE_VOLUME_HENYEY_GREENSTEIN_ID
@ CLOSURE_VOLUME_DRAINE_ID
@ CLOSURE_VOLUME_FOURNIER_FORAND_ID
@ SR_EXTINCTION
ccl_device_inline dual3 fabs(const ccl_private dual3 &a)
Definition math_dual.h:241
#define ccl_device
float3 Spectrum
ccl_device int volume_draine_sample(const ccl_private ShaderData *sd, const ccl_private ShaderVolumeClosure *svc, const float2 rand, ccl_private Spectrum *eval, ccl_private float3 *wo, ccl_private float *pdf)
ccl_device Spectrum volume_draine_eval(const ccl_private ShaderData *sd, const ccl_private ShaderVolumeClosure *svc, const float3 wo, ccl_private float *pdf)
ccl_device Spectrum volume_fournier_forand_eval(const ccl_private ShaderData *sd, const ccl_private ShaderVolumeClosure *svc, const float3 wo, ccl_private float *pdf)
ccl_device int volume_fournier_forand_sample(const ccl_private ShaderData *sd, const ccl_private ShaderVolumeClosure *svc, const float2 rand, ccl_private Spectrum *eval, ccl_private float3 *wo, ccl_private float *pdf)
ccl_device Spectrum volume_henyey_greenstein_eval(const ccl_private ShaderData *sd, const ccl_private ShaderVolumeClosure *svc, const float3 wo, ccl_private float *pdf)
ccl_device int volume_henyey_greenstein_sample(const ccl_private ShaderData *sd, const ccl_private ShaderVolumeClosure *svc, const float2 rand, ccl_private Spectrum *eval, ccl_private float3 *wo, ccl_private float *pdf)
ccl_device Spectrum volume_rayleigh_eval(const ccl_private ShaderData *sd, const float3 wo, ccl_private float *pdf)
ccl_device int volume_rayleigh_sample(const ccl_private ShaderData *sd, const float2 rand, ccl_private Spectrum *eval, ccl_private float3 *wo, ccl_private float *pdf)