Blender V4.5
volume_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/* Volume shader evaluation and sampling. */
6
7#pragma once
8
10
13
14#ifdef __SVM__
15# include "kernel/svm/svm.h"
16#endif
17#ifdef __OSL__
18# include "kernel/osl/osl.h"
19#endif
20
22
24
26
27#ifdef __VOLUME__
28
29/* Merging */
30
31ccl_device_inline void volume_shader_merge_closures(ccl_private ShaderData *sd)
32{
33 /* Merge identical closures to save closure space with stacked volumes. */
34 for (int i = 0; i < sd->num_closure; i++) {
35 ccl_private ShaderClosure *sci = &sd->closure[i];
36
37 if (!CLOSURE_IS_VOLUME_SCATTER(sci->type)) {
38 continue;
39 }
40
41 for (int j = i + 1; j < sd->num_closure; j++) {
42 ccl_private ShaderClosure *scj = &sd->closure[j];
43 if (!volume_phase_equal(sci, scj)) {
44 continue;
45 }
46
47 sci->weight += scj->weight;
48 sci->sample_weight += scj->sample_weight;
49
50 const int size = sd->num_closure - (j + 1);
51 if (size > 0) {
52 for (int k = 0; k < size; k++) {
53 scj[k] = scj[k + 1];
54 }
55 }
56
57 sd->num_closure--;
58 kernel_assert(sd->num_closure >= 0);
59 j--;
60 }
61 }
62}
63
65 phases,
66 const ccl_private ShaderData *ccl_restrict sd)
67{
68 phases->num_closure = 0;
69
70 for (int i = 0; i < sd->num_closure; i++) {
71 const ccl_private ShaderClosure *from_sc = &sd->closure[i];
72 if (CLOSURE_IS_VOLUME_SCATTER(from_sc->type)) {
73 /* ShaderVolumeClosure is a subset of ShaderClosure, so this is fine for all volume scatter
74 * closures. */
75 phases->closure[phases->num_closure++] = *((const ccl_private ShaderVolumeClosure *)from_sc);
76 if (phases->num_closure >= MAX_VOLUME_CLOSURE) {
77 break;
78 }
79 }
80 }
81}
82
83/* Guiding */
84
85# ifdef __PATH_GUIDING__
86ccl_device_inline void volume_shader_prepare_guiding(KernelGlobals kg,
88 ccl_private ShaderData *sd,
89 float rand_phase_guiding,
90 const float3 P,
91 const float3 D,
93{
94 /* Have any phase functions to guide? */
95 const int num_phases = phases->num_closure;
96 if (!kernel_data.integrator.use_volume_guiding || num_phases == 0) {
97 state->guiding.use_volume_guiding = false;
98 return;
99 }
100
101 const float volume_guiding_probability = kernel_data.integrator.volume_guiding_probability;
102
103 /* If we have more than one phase function we select one random based on its
104 * sample weight to calculate the product distribution for guiding. */
105 int phase_id = 0;
106 float phase_weight = 1.0f;
107
108 if (num_phases > 1) {
109 /* Pick a phase closure based on sample weights. */
110 float sum = 0.0f;
111
112 for (phase_id = 0; phase_id < num_phases; phase_id++) {
113 const ccl_private ShaderVolumeClosure *svc = &phases->closure[phase_id];
114 sum += svc->sample_weight;
115 }
116
117 const float r = rand_phase_guiding * sum;
118 float partial_sum = 0.0f;
119
120 for (phase_id = 0; phase_id < num_phases; phase_id++) {
121 const ccl_private ShaderVolumeClosure *svc = &phases->closure[phase_id];
122 const float next_sum = partial_sum + svc->sample_weight;
123
124 if (r <= next_sum) {
125 /* Rescale to reuse. */
126 rand_phase_guiding = (r - partial_sum) / svc->sample_weight;
127 phase_weight = svc->sample_weight / sum;
128 break;
129 }
130
131 partial_sum = next_sum;
132 }
133
134 /* Adjust the sample weight of the component used for guiding. */
135 phases->closure[phase_id].sample_weight *= volume_guiding_probability;
136 }
137
138 /* Init guiding for selected phase function. */
139 const ccl_private ShaderVolumeClosure *svc = &phases->closure[phase_id];
140 const float phase_g = volume_phase_get_g(svc);
141 if (!guiding_phase_init(kg, state, P, D, phase_g, rand_phase_guiding)) {
142 state->guiding.use_volume_guiding = false;
143 return;
144 }
145
146 state->guiding.use_volume_guiding = true;
147 state->guiding.sample_volume_guiding_rand = rand_phase_guiding;
148 state->guiding.volume_guiding_sampling_prob = volume_guiding_probability * phase_weight;
149
150 kernel_assert(state->guiding.volume_guiding_sampling_prob > 0.0f &&
151 state->guiding.volume_guiding_sampling_prob <= 1.0f);
152}
153# endif
154
155/* Phase Evaluation & Sampling */
156
157/* Randomly sample a volume phase function proportional to ShaderClosure.sample_weight. */
158/* TODO: this isn't quite correct, we don't weight anisotropy properly depending on color channels,
159 * even if this is perhaps not a common case */
160const ccl_device_inline ccl_private ShaderVolumeClosure *volume_shader_phase_pick(
161 const ccl_private ShaderVolumePhases *phases, ccl_private float2 *rand_phase)
162{
163 int sampled = 0;
164
165 if (phases->num_closure > 1) {
166 /* Pick a phase closure based on sample weights. */
167 /* For reservoir sampling, always accept the first in the stream. */
168 float sum = phases->closure[0].sample_weight;
169
170 for (int i = 1; i < phases->num_closure; i++) {
171 const float sample_weight = phases->closure[i].sample_weight;
172 sum += sample_weight;
173 const float thresh = sample_weight / sum;
174
175 /* Rescale random number to reuse for volume phase direction sample. */
176 if (rand_phase->x < thresh) {
177 sampled = i;
178 rand_phase->x /= thresh;
179 }
180 else {
181 rand_phase->x = (rand_phase->x - thresh) / (1.0f - thresh);
182 }
183 }
184 }
185
186 return &phases->closure[sampled];
187}
188
189ccl_device_inline float _volume_shader_phase_eval_mis(const ccl_private ShaderData *sd,
190 const ccl_private ShaderVolumePhases *phases,
191 const float3 wo,
192 ccl_private BsdfEval *result_eval,
193 float sum_pdf,
194 float sum_sample_weight)
195{
196 for (int i = 0; i < phases->num_closure; i++) {
197 const ccl_private ShaderVolumeClosure *svc = &phases->closure[i];
198 float phase_pdf = 0.0f;
199 const Spectrum eval = volume_phase_eval(sd, svc, wo, &phase_pdf);
200
201 if (phase_pdf != 0.0f) {
202 bsdf_eval_accum(result_eval, eval * svc->sample_weight);
203 sum_pdf += phase_pdf * svc->sample_weight;
204 }
205
206 sum_sample_weight += svc->sample_weight;
207 }
208
209 bsdf_eval_mul(result_eval, 1.0f / sum_sample_weight);
210 return (sum_sample_weight > 0.0f) ? sum_pdf / sum_sample_weight : 0.0f;
211}
212
213ccl_device float volume_shader_phase_eval(KernelGlobals kg,
214 const ccl_private ShaderData *sd,
216 const float3 wo,
217 ccl_private BsdfEval *phase_eval)
218{
219 float phase_pdf = 0.0f;
220 const Spectrum eval = volume_phase_eval(sd, svc, wo, &phase_pdf);
221
222 if (phase_pdf != 0.0f) {
223 bsdf_eval_accum(phase_eval, eval);
224 }
225
226 return phase_pdf;
227}
228
229ccl_device float volume_shader_phase_eval(KernelGlobals kg,
231 const ccl_private ShaderData *sd,
232 const ccl_private ShaderVolumePhases *phases,
233 const float3 wo,
234 ccl_private BsdfEval *phase_eval,
235 const uint light_shader_flags)
236{
237 bsdf_eval_init(phase_eval, zero_spectrum());
238
239 float pdf = _volume_shader_phase_eval_mis(sd, phases, wo, phase_eval, 0.0f, 0.0f);
240
241# if defined(__PATH_GUIDING__) && PATH_GUIDING_LEVEL >= 4
242 if (state->guiding.use_volume_guiding) {
243 const float guiding_sampling_prob = state->guiding.volume_guiding_sampling_prob;
244 const float guide_pdf = guiding_phase_pdf(kg, state, wo);
245 pdf = (guiding_sampling_prob * guide_pdf) + (1.0f - guiding_sampling_prob) * pdf;
246 }
247# endif
248
249 /* If the light does not use MIS, then it is only sampled via NEE, so the probability of hitting
250 * the light using BSDF sampling is zero. */
251 if (!(light_shader_flags & SHADER_USE_MIS)) {
252 pdf = 0.0f;
253 }
254
255 return pdf;
256}
257
258# ifdef __PATH_GUIDING__
259ccl_device int volume_shader_phase_guided_sample(KernelGlobals kg,
261 const ccl_private ShaderData *sd,
263 const float2 rand_phase,
264 ccl_private BsdfEval *phase_eval,
266 ccl_private float *phase_pdf,
267 ccl_private float *unguided_phase_pdf,
268 ccl_private float *sampled_roughness)
269{
270 const bool use_volume_guiding = state->guiding.use_volume_guiding;
271 const float guiding_sampling_prob = state->guiding.volume_guiding_sampling_prob;
272
273 /* Decide between sampling guiding distribution and phase. */
274 float rand_phase_guiding = state->guiding.sample_volume_guiding_rand;
275 bool sample_guiding = false;
276 if (use_volume_guiding && rand_phase_guiding < guiding_sampling_prob) {
277 sample_guiding = true;
278 rand_phase_guiding /= guiding_sampling_prob;
279 }
280 else {
281 rand_phase_guiding -= guiding_sampling_prob;
282 rand_phase_guiding /= (1.0f - guiding_sampling_prob);
283 }
284
285 /* Initialize to zero. */
286 int label = LABEL_NONE;
287 Spectrum eval = zero_spectrum();
288
289 *unguided_phase_pdf = 0.0f;
290 float guide_pdf = 0.0f;
291 *sampled_roughness = 1.0f - fabsf(volume_phase_get_g(svc));
292
293 bsdf_eval_init(phase_eval, zero_spectrum());
294
295 if (sample_guiding) {
296 /* Sample guiding distribution. */
297 guide_pdf = guiding_phase_sample(kg, state, rand_phase, wo);
298 *phase_pdf = 0.0f;
299
300 if (guide_pdf != 0.0f) {
301 *unguided_phase_pdf = volume_shader_phase_eval(kg, sd, svc, *wo, phase_eval);
302 *phase_pdf = (guiding_sampling_prob * guide_pdf) +
303 ((1.0f - guiding_sampling_prob) * (*unguided_phase_pdf));
304 label = LABEL_VOLUME_SCATTER;
305 }
306 }
307 else {
308 /* Sample phase. */
309 *phase_pdf = 0.0f;
310 label = volume_phase_sample(sd, svc, rand_phase, &eval, wo, unguided_phase_pdf);
311
312 if (*unguided_phase_pdf != 0.0f) {
313 bsdf_eval_init(phase_eval, eval);
314
315 *phase_pdf = *unguided_phase_pdf;
316 if (use_volume_guiding) {
317 guide_pdf = guiding_phase_pdf(kg, state, *wo);
318 *phase_pdf *= 1.0f - guiding_sampling_prob;
319 *phase_pdf += guiding_sampling_prob * guide_pdf;
320 }
321
322 kernel_assert(reduce_min(bsdf_eval_sum(phase_eval)) >= 0.0f);
323 }
324 else {
325 bsdf_eval_init(phase_eval, zero_spectrum());
326 }
327
328 kernel_assert(reduce_min(bsdf_eval_sum(phase_eval)) >= 0.0f);
329 }
330
331 return label;
332}
333# endif
334
335ccl_device int volume_shader_phase_sample(KernelGlobals kg,
336 const ccl_private ShaderData *sd,
337 const ccl_private ShaderVolumePhases *phases,
339 const float2 rand_phase,
340 ccl_private BsdfEval *phase_eval,
342 ccl_private float *pdf,
343 ccl_private float *sampled_roughness)
344{
345 *sampled_roughness = 1.0f - fabsf(volume_phase_get_g(svc));
346 Spectrum eval = zero_spectrum();
347
348 *pdf = 0.0f;
349 const int label = volume_phase_sample(sd, svc, rand_phase, &eval, wo, pdf);
350
351 if (*pdf != 0.0f) {
352 bsdf_eval_init(phase_eval, eval);
353 }
354
355 return label;
356}
357
358/* Motion Blur */
359
360# ifdef __OBJECT_MOTION__
361ccl_device_inline void volume_shader_motion_blur(KernelGlobals kg,
362 ccl_private ShaderData *ccl_restrict sd)
363{
364 if ((sd->object_flag & SD_OBJECT_HAS_VOLUME_MOTION) == 0) {
365 return;
366 }
367
370
371 const float3 P = sd->P;
372 const float velocity_scale = kernel_data_fetch(objects, sd->object).velocity_scale;
373 const float time_offset = kernel_data.cam.motion_position == MOTION_POSITION_CENTER ? 0.5f :
374 0.0f;
375 const float time = kernel_data.cam.motion_position == MOTION_POSITION_END ?
376 (1.0f - kernel_data.cam.shuttertime) + sd->time :
377 sd->time;
378
379 /* Use a 1st order semi-lagrangian advection scheme to estimate what volume quantity
380 * existed, or will exist, at the given time:
381 *
382 * `phi(x, T) = phi(x - (T - t) * u(x, T), t)`
383 *
384 * where
385 *
386 * x : position
387 * T : super-sampled time (or ray time)
388 * t : current time of the simulation (in rendering we assume this is center frame with
389 * relative time = 0)
390 * phi : the volume quantity
391 * u : the velocity field
392 *
393 * But first we need to determine the velocity field `u(x, T)`, which we can estimate also
394 * using semi-lagrangian advection.
395 *
396 * `u(x, T) = u(x - (T - t) * u(x, T), t)`
397 *
398 * This is the typical way to model self-advection in fluid dynamics, however, we do not
399 * account for other forces affecting the velocity during simulation (pressure, buoyancy,
400 * etc.): this gives a linear interpolation when fluid are mostly "curvy". For better
401 * results, a higher order interpolation scheme can be used (at the cost of more lookups),
402 * or an interpolation of the velocity fields for the previous and next frames could also
403 * be used to estimate `u(x, T)` (which will cost more memory and lookups).
404 *
405 * References:
406 * "Eulerian Motion Blur", Kim and Ko, 2007
407 * "Production Volume Rendering", Wreninge et al., 2012
408 */
409
410 /* Find velocity. */
411 float3 velocity = primitive_volume_attribute<float3>(kg, sd, v_desc);
412 object_dir_transform(kg, sd, &velocity);
413
414 /* Find advected P. */
415 sd->P = P - (time - time_offset) * velocity_scale * velocity;
416
417 /* Find advected velocity. */
418 velocity = primitive_volume_attribute<float3>(kg, sd, v_desc);
419 object_dir_transform(kg, sd, &velocity);
420
421 /* Find advected P. */
422 sd->P = P - (time - time_offset) * velocity_scale * velocity;
423}
424# endif
425
426/* Volume Evaluation */
427
428template<const bool shadow, const uint node_feature_mask, typename ConstIntegratorGenericState>
429ccl_device_inline bool volume_shader_eval_entry(KernelGlobals kg,
430 ConstIntegratorGenericState state,
431 ccl_private ShaderData *ccl_restrict sd,
432 const ccl_private VolumeStack &entry,
433 const uint32_t path_flag)
434{
435 if (entry.shader == SHADER_NONE) {
436 return false;
437 }
438
439 /* Setup shader-data from stack. It's mostly setup already in shader_setup_from_volume, this
440 * switching should be quick. */
441 sd->object = entry.object;
442 sd->shader = entry.shader;
443
444 sd->flag &= ~SD_SHADER_FLAGS;
445 sd->flag |= kernel_data_fetch(shaders, (sd->shader & SHADER_MASK)).flags;
446 sd->object_flag &= ~SD_OBJECT_FLAGS;
447
448 if (sd->object != OBJECT_NONE) {
449 sd->object_flag |= kernel_data_fetch(object_flag, sd->object);
450
451 if (shadow && !(kernel_data_fetch(objects, sd->object).visibility &
452 (path_flag & PATH_RAY_ALL_VISIBILITY)))
453 {
454 /* If volume is invisible to shadow ray, the hit is not registered, but the volume is still
455 * in the stack. Skip the volume in such cases. */
456 /* NOTE: `SHADOW_CATCHER_PATH_VISIBILITY()` is omitted because `path_flag` is just
457 * `PATH_RAY_SHADOW` when evaluating shadows. */
458 return true;
459 }
460
461# ifdef __OBJECT_MOTION__
462 /* TODO: this is inefficient for motion blur, we should be caching matrices instead of
463 * recomputing them each step. */
464 shader_setup_object_transforms(kg, sd, sd->time);
465
466 volume_shader_motion_blur(kg, sd);
467# endif
468 }
469
470 /* Evaluate shader. */
471# ifdef __OSL__
472 if (kernel_data.kernel_features & KERNEL_FEATURE_OSL_SHADING) {
473 osl_eval_nodes<SHADER_TYPE_VOLUME>(kg, state, sd, path_flag);
474 }
475 else
476# endif
477 {
478# ifdef __SVM__
480# endif
481 }
482
483 return true;
484}
485
486template<const bool shadow, typename StackReadOp, typename ConstIntegratorGenericState>
487ccl_device_inline void volume_shader_eval(KernelGlobals kg,
488 ConstIntegratorGenericState state,
489 ccl_private ShaderData *ccl_restrict sd,
490 const uint32_t path_flag,
491 StackReadOp stack_read)
492{
493 /* If path is being terminated, we are tracing a shadow ray or evaluating
494 * emission, then we don't need to store closures. The emission and shadow
495 * shader data also do not have a closure array to save GPU memory. */
496 int max_closures;
498 max_closures = 0;
499 }
500 else {
501 max_closures = kernel_data.max_closures;
502 }
503
504 /* reset closures once at the start, we will be accumulating the closures
505 * for all volumes in the stack into a single array of closures */
506 sd->num_closure = 0;
507 sd->num_closure_left = max_closures;
508 sd->flag = SD_IS_VOLUME_SHADER_EVAL;
509 sd->object_flag = 0;
510
511 for (int i = 0;; i++) {
512 const VolumeStack entry = stack_read(i);
513 if (!volume_shader_eval_entry<shadow, KERNEL_FEATURE_NODE_MASK_VOLUME>(
514 kg, state, sd, entry, path_flag))
515 {
516 /* Stack fully processed. */
517 return;
518 }
519
520 /* Merge closures to avoid exceeding number of closures limit. */
521 if (!shadow) {
522 if (i > 0) {
523 volume_shader_merge_closures(sd);
524 }
525 }
526 }
527}
528
529#endif /* __VOLUME__ */
530
#define D
unsigned int uint
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
static T sum(const btAlignedObjectArray< T > &items)
void osl_eval_nodes< SHADER_TYPE_VOLUME >(const ThreadKernelGlobalsCPU *kg, const void *state, ShaderData *sd, const uint32_t path_flag)
Definition closures.cpp:200
#define kernel_assert(cond)
#define CLOSURE_IS_VOLUME_SCATTER(type)
#define kernel_data
#define ccl_restrict
#define MAX_VOLUME_CLOSURE
#define kernel_data_fetch(name, index)
#define SHADER_NONE
#define ccl_device
#define zero_spectrum
#define OBJECT_NONE
#define KERNEL_FEATURE_OSL_SHADING
#define ccl_private
const ThreadKernelGlobalsCPU * KernelGlobals
#define ccl_device_inline
#define CCL_NAMESPACE_END
#define fabsf(x)
ccl_device bool volume_phase_equal(const ccl_private ShaderClosure *c1, const ccl_private ShaderClosure *c2)
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 float volume_phase_get_g(const ccl_private ShaderVolumeClosure *svc)
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)
ccl_device_inline void object_dir_transform(KernelGlobals kg, const ccl_private ShaderData *sd, ccl_private float3 *D)
ccl_device_forceinline float guiding_phase_sample(KernelGlobals kg, IntegratorState state, const float2 rand_phase, ccl_private float3 *wo)
ccl_device_forceinline float guiding_phase_pdf(KernelGlobals kg, IntegratorState state, const float3 wo)
ccl_device_forceinline bool guiding_phase_init(KernelGlobals kg, IntegratorState state, const float3 P, const float3 D, const float g, ccl_private float &rand)
ccl_device void svm_eval_nodes(KernelGlobals kg, ConstIntegratorGenericState state, ccl_private ShaderData *sd, ccl_global float *render_buffer, const uint32_t path_flag)
@ SD_SHADER_FLAGS
@ SD_IS_VOLUME_SHADER_EVAL
@ ATTR_STD_NOT_FOUND
@ ATTR_STD_VOLUME_VELOCITY
@ PATH_RAY_SHADOW
@ PATH_RAY_TERMINATE
@ PATH_RAY_EMISSION
@ PATH_RAY_ALL_VISIBILITY
@ SHADER_USE_MIS
@ SHADER_MASK
@ MOTION_POSITION_END
@ MOTION_POSITION_CENTER
@ SD_OBJECT_HAS_VOLUME_MOTION
@ SD_OBJECT_FLAGS
@ LABEL_VOLUME_SCATTER
@ LABEL_NONE
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 void bsdf_eval_mul(ccl_private BsdfEval *eval, const float value)
ccl_device_inline Spectrum bsdf_eval_sum(const ccl_private BsdfEval *eval)
ccl_device_inline float reduce_min(const float2 a)
static ulong state[N]
CCL_NAMESPACE_BEGIN ccl_device void shader_setup_object_transforms(KernelGlobals kg, ccl_private ShaderData *ccl_restrict sd, const float time)
Definition shader_data.h:25
IntegratorStateCPU * IntegratorState
Definition state.h:228
static bool find_attribute(const std::string &attributes, const char *search_attribute)
i
Definition text_draw.cc:230
float3 Spectrum