Blender V5.3
shade_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
8
11
20
21#include "kernel/light/light.h"
22#include "kernel/light/sample.h"
23
25
26#include "kernel/sample/lcg.h"
27
29
30/* Events for probabilistic scattering. */
31
38
39#ifdef __VOLUME__
40
41struct VolumeIntegrateResult {
42 /* Throughput and offset for direct light scattering. */
43 bool direct_scatter;
44 Spectrum direct_throughput;
45 float direct_t;
46 ShaderVolumePhases direct_phases;
47# if defined(__PATH_GUIDING__)
48 VolumeSampleMethod direct_sample_method;
49# endif
50
51 /* Throughput and offset for indirect light scattering. */
52 bool indirect_scatter;
53 Spectrum indirect_throughput;
54 float indirect_t;
55 ShaderVolumePhases indirect_phases;
56};
57
58/* We use both volume octree and volume stack, sometimes they disagree on whether a point is inside
59 * a volume or not. We accept small numerical precision issues, above this threshold the volume
60 * stack shall prevail. */
61/* TODO(weizhen): tweak this value. */
62# define OVERLAP_EXP 5e-4f
63/* Restrict the number of steps in case of numerical problems */
64# define VOLUME_MAX_STEPS 1024
65/* Number of mantissa bits of floating-point numbers. */
66# define MANTISSA_BITS 23
67
68/* Volume shader properties
69 *
70 * extinction coefficient = absorption coefficient + scattering coefficient
71 * sigma_t = sigma_a + sigma_s */
72
73struct VolumeShaderCoefficients {
74 Spectrum sigma_t;
75 Spectrum sigma_s;
76 Spectrum emission;
77};
78
79struct EquiangularCoefficients {
80 float3 P;
81 Interval<float> t_range;
82};
83
84/* Evaluate extinction coefficient at `sd->P`. */
85template<const bool shadow, typename IntegratorGenericState>
86ccl_device_inline Spectrum volume_shader_eval_extinction(KernelGlobals kg,
87 const IntegratorGenericState state,
88 ccl_private ShaderData *ccl_restrict sd,
89 const PathRayVisibility path_visibility,
90 uint32_t path_flag)
91{
92 /* Use extinction flag to avoid storing phase function and evaluating emission. */
93 path_flag |= PATH_RAY_EXTINCTION;
94
95 volume_shader_eval<shadow>(kg, state, sd, path_visibility, path_flag);
96
97 return (sd->runtime_flag & SR_EXTINCTION) ? sd->closure_transparent_extinction : zero_spectrum();
98}
99
100/* Evaluate shader to get absorption, scattering and emission at P. */
101ccl_device_inline bool volume_shader_sample(KernelGlobals kg,
103 ccl_private ShaderData *ccl_restrict sd,
104 ccl_private VolumeShaderCoefficients *coeff)
105{
106 const PathRayVisibility path_visibility = INTEGRATOR_STATE(state, path, visibility);
107 const uint32_t path_flag = INTEGRATOR_STATE(state, path, flag);
108 volume_shader_eval<false>(kg, state, sd, path_visibility, path_flag);
109
110 if (!(sd->runtime_flag & (SR_EXTINCTION | SR_SCATTER | SR_EMISSION))) {
111 return false;
112 }
113
114 coeff->sigma_s = zero_spectrum();
115 coeff->sigma_t = (sd->runtime_flag & SR_EXTINCTION) ? sd->closure_transparent_extinction :
117 coeff->emission = (sd->runtime_flag & SR_EMISSION) ? sd->closure_emission_background :
119
120 if (sd->runtime_flag & SR_SCATTER) {
121 for (int i = 0; i < sd->num_closure; i++) {
122 const ccl_private ShaderClosure *sc = &sd->closure[i];
123
124 if (CLOSURE_IS_VOLUME(sc->type)) {
125 coeff->sigma_s += sc->weight;
126 }
127 }
128 }
129
130 return true;
131}
132
133/* -------------------------------------------------------------------- */
149
150struct OctreeTracing {
151 /* Current active leaf node. */
152 ccl_global const KernelOctreeNode *node = nullptr;
153
154 /* Current active ray segment, typically spans from the front face to the back face of the
155 * current leaf node. */
156 Interval<float> t;
157
158 /* Ray origin in octree coordinate space. */
159 packed_float3 ray_P;
160
161 /* Ray direction in octree coordinate space. */
162 packed_float3 ray_D;
163
164 /* Current active position in octree coordinate space. */
165 uint3 current_P;
166
167 /* Object and shader which the octree represents. */
168 VolumeStack entry = {OBJECT_NONE, SHADER_NONE};
169
170 /* Scale of the current active leaf node, relative to the smallest possible size representable by
171 * float. Initialize to the number of float mantissa bits. */
172 uint8_t scale = MANTISSA_BITS;
173 uint8_t next_scale;
174 /* Mark the dimension (x,y,z) to negate the ray so that we find the correct octant. */
175 uint8_t octant_mask;
176
177 /* Whether multiple volumes overlap in the ray segment. */
178 bool no_overlap = false;
179
180 /* Maximum and minimum of the densities in the current segment. */
181 Extrema<float> sigma = 0.0f;
182
183 ccl_device_inline_method OctreeTracing(const float tmin)
184 {
185 /* Initialize t.max to FLT_MAX so that any intersection with the node face is smaller. */
186 t = {tmin, FLT_MAX};
187 }
188
189 enum Dimension { DIM_X = 1U << 0U, DIM_Y = 1U << 1U, DIM_Z = 1U << 2U };
190
191 /* Given ray origin `P` and direction `D` in object space, convert them into octree space
192 * [1.0, 2.0).
193 * Returns false if ray is leaving the octree or octree has degenerate shape. */
194 ccl_device_inline_method bool to_octree_space(ccl_private const float3 &P,
195 ccl_private const float3 &D,
196 const float3 scale,
197 const float3 translation)
198 {
199 if (!isfinite_safe(scale)) {
200 /* Octree with a degenerate shape. */
201 return false;
202 }
203
204 /* Starting point of octree tracing. */
205 float3 local_P = (P + D * t.min) * scale + translation;
206 ray_D = D * scale;
207
208 /* Select octant mask to mirror the coordinate system so that ray direction is negative along
209 * each axis, and adjust `local_P` accordingly. */
210 const auto positive = ray_D > 0.0f;
211 octant_mask = (!!positive.x * DIM_X) | (!!positive.y * DIM_Y) | (!!positive.z * DIM_Z);
212 local_P = select(positive, 3.0f - local_P, local_P);
213
214 /* Clamp to the largest floating-point number smaller than 2.0f, for numerical stability. */
215 local_P = min(local_P, make_float3(1.9999999f));
216 current_P = float3_as_uint3(local_P);
217
218 ray_D = -fabs(ray_D);
219
220 /* Ray origin. */
221 ray_P = local_P - ray_D * t.min;
222
223 /* Returns false if point lies outside of the octree and the ray is leaving the octree. */
224 return all(local_P > 1.0f);
225 }
226
227 /* Find the bounding box min of the node that `current_P` lies in within the current scale. */
228 ccl_device_inline_method float3 floor_pos() const
229 {
230 /* Erase bits lower than scale. */
231 const uint mask = ~0u << scale;
232 return make_float3(__uint_as_float(current_P.x & mask),
233 __uint_as_float(current_P.y & mask),
234 __uint_as_float(current_P.z & mask));
235 }
236
237 /* Find arbitrary position inside the next node.
238 * We use the end of the current segment offsetted by half of the minimal node size in the normal
239 * direction of the last face intersection. */
240 ccl_device_inline_method void find_next_pos(const float3 bbox_min,
241 const float3 t,
242 const float tmax)
243 {
244 constexpr float half_size = 1.0f / (2 << VOLUME_OCTREE_MAX_DEPTH);
245 const uint3 next_P = float3_as_uint3(
246 select(t == tmax, bbox_min - half_size, ray_D * tmax + ray_P));
247
248 /* Find the nearest common ancestor of two positions by checking the shared higher bits. */
249 const uint diff = (current_P.x ^ next_P.x) | (current_P.y ^ next_P.y) |
250 (current_P.z ^ next_P.z);
251
252 current_P = next_P;
253 next_scale = 32u - count_leading_zeros(diff);
254 }
255
256 /* See `ray_aabb_intersect()`. We only need to intersect the 3 back sides because the ray
257 * direction is all negative. */
258 ccl_device_inline_method float ray_voxel_intersect(const float ray_tmax)
259 {
260 const float3 bbox_min = floor_pos();
261
262 /* Distances to the three surfaces. */
263 float3 intersect_t = (bbox_min - ray_P) / ray_D;
264
265 /* Select the smallest element that is larger than `t.min`, to avoid self intersection. */
266 intersect_t = select(intersect_t > t.min, intersect_t, make_float3(FLT_MAX));
267
268 /* The first intersection is given by the smallest t. */
269 const float tmax = reduce_min(intersect_t);
270
271 find_next_pos(bbox_min, intersect_t, tmax);
272
273 return fminf(tmax, ray_tmax);
274 }
275
276 /* Returns the octant of `current_P` in the node at given scale. */
277 ccl_device_inline_method int get_octant() const
278 {
279 const uint8_t x = (current_P.x >> scale) & 1u;
280 const uint8_t y = ((current_P.y >> scale) & 1u) << 1u;
281 const uint8_t z = ((current_P.z >> scale) & 1u) << 2u;
282 return (x | y | z) ^ octant_mask;
283 }
284};
285
286/* Check if an octree node is leaf node. */
287ccl_device_inline bool volume_node_is_leaf(const ccl_global KernelOctreeNode *knode)
288{
289 return knode->first_child == -1;
290}
291
292/* Find the leaf node of the current position, and replace `octree.node` with that node. */
293ccl_device void volume_voxel_get(KernelGlobals kg, ccl_private OctreeTracing &octree)
294{
295 while (!volume_node_is_leaf(octree.node)) {
296 octree.scale -= 1;
297 const int child_index = octree.node->first_child + octree.get_octant();
298 octree.node = &kernel_data_fetch(volume_tree_nodes, child_index);
299 }
300}
301
302/* If there exists a Light Path Node, it could affect the density evaluation at runtime.
303 * Randomly sample a few points on the ray to estimate the extrema. */
304template<const bool shadow, typename IntegratorGenericState>
305ccl_device_noinline Extrema<float> volume_estimate_extrema(KernelGlobals kg,
306 const ccl_private Ray *ccl_restrict ray,
307 ccl_private ShaderData *ccl_restrict sd,
308 const IntegratorGenericState state,
309 const ccl_private RNGState *rng_state,
310 const PathRayVisibility path_visibility,
311 const uint32_t path_flag,
312/* Work around apparent HIP compiler bug. */
313# ifdef __KERNEL_HIP__
314 const ccl_private OctreeTracing &octree
315# else
316 const Interval<float> t,
317 const VolumeStack entry
318# endif
319)
320{
321# ifdef __KERNEL_HIP__
322 const ccl_private Interval<float> &t = octree.t;
323 const ccl_private VolumeStack &entry = octree.entry;
324# endif
325 const bool homogeneous = volume_is_homogeneous(kg, entry);
326 const int samples = homogeneous ? 1 : 4;
327 const float shade_offset = homogeneous ?
328 0.5f :
330 const float step_size = t.length() / float(samples);
331
332 /* Do not allocate closures. */
333 sd->num_closure_left = 0;
334
335 Extrema<float> extrema = {FLT_MAX, -FLT_MAX};
336 for (int i = 0; i < samples; i++) {
337 const float shade_t = t.min + (shade_offset + i) * step_size;
338 sd->P = ray->P + ray->D * shade_t;
339
340 sd->closure_transparent_extinction = zero_float3();
341 sd->closure_emission_background = zero_float3();
342
343 volume_shader_eval_entry<shadow, KERNEL_FEATURE_NODE_MASK_VOLUME>(
344 kg, state, sd, entry, path_visibility, path_flag);
345
346 const float sigma = reduce_max(sd->closure_transparent_extinction);
347 const float emission = reduce_max(sd->closure_emission_background);
348
349 extrema = merge(extrema, fmaxf(sigma, emission));
350 }
351
352 if (!homogeneous) {
353 /* Slightly increase the majorant in case the estimation is not accurate. */
354 extrema.max = fmaxf(0.5f, extrema.max * 1.5f);
355 }
356
357 return extrema;
358}
359
360/* Given an octree node, compute it's extrema.
361 * In most common cases, the extrema are already stored in the node, but if the shader contains
362 * a light path node, we need to evaluate the densities on the fly. */
363template<const bool shadow, typename IntegratorGenericState>
364ccl_device_inline Extrema<float> volume_object_get_extrema(KernelGlobals kg,
365 const ccl_private Ray *ccl_restrict ray,
366 ccl_private ShaderData *ccl_restrict sd,
367 const IntegratorGenericState state,
368 const ccl_private OctreeTracing &octree,
369 const ccl_private RNGState *rng_state,
370 const PathRayVisibility path_visibility,
371 const uint32_t path_flag)
372{
373 const int shader_flag = kernel_data_fetch(shaders, (octree.entry.shader & SHADER_MASK)).flags;
374 if ((path_visibility & PATH_RAY_VISIBILITY_CAMERA) || !(shader_flag & SD_HAS_LIGHT_PATH_NODE)) {
375 /* Use the baked volume density extrema. */
376 return octree.node->sigma * object_volume_density(kg, octree.entry.object);
377 }
378
379# ifdef __KERNEL_HIP__
380 return volume_estimate_extrema<shadow>(
381 kg, ray, sd, state, rng_state, path_visibility, path_flag, octree);
382# else
383 return volume_estimate_extrema<shadow>(
384 kg, ray, sd, state, rng_state, path_visibility, path_flag, octree.t, octree.entry);
385# endif
386}
387
388/* Find the octree root node in the kernel array that corresponds to the volume stack entry. */
389ccl_device_inline const ccl_global KernelOctreeRoot *volume_find_octree_root(
390 KernelGlobals kg, const VolumeStack entry)
391{
392 int root = kernel_data_fetch(volume_tree_root_ids, entry.object);
393 const ccl_global KernelOctreeRoot *kroot = &kernel_data_fetch(volume_tree_roots, root);
394 while ((entry.shader & SHADER_MASK) != kroot->shader) {
395 /* If one object has multiple shaders, we store the index of the last shader, and search
396 * backwards for the octree with the corresponding shader. */
397 kroot = &kernel_data_fetch(volume_tree_roots, --root);
398 }
399 return kroot;
400}
401
402template<const bool shadow, typename IntegratorGenericState>
403ccl_device bool volume_octree_setup_single(KernelGlobals kg,
404 const ccl_private Ray *ccl_restrict ray,
405 ccl_private ShaderData *ccl_restrict sd,
406 const IntegratorGenericState state,
407 const ccl_private RNGState *rng_state,
408 const PathRayVisibility path_visibility,
409 const uint32_t path_flag,
410 ccl_private OctreeTracing &local,
411 const VolumeStack skip,
412 const int index)
413{
414 const VolumeStack entry = volume_stack_read<shadow>(state, index);
415
416 if (entry.shader == SHADER_NONE) {
417 return false;
418 }
419
420 if (entry.object == skip.object && entry.shader == skip.shader) {
421 return true;
422 }
423
424 const ccl_global KernelOctreeRoot *kroot = volume_find_octree_root(kg, entry);
425
426 local.node = &kernel_data_fetch(volume_tree_nodes, kroot->id);
427 local.entry = entry;
428
429 /* Convert to object space. */
430 float3 local_P = ray->P, local_D = ray->D;
431 if (!(kernel_data_fetch(object_flag, entry.object) & SD_OBJECT_TRANSFORM_APPLIED)) {
432 const Transform itfm = object_fetch_transform(kg, entry.object, OBJECT_INVERSE_TRANSFORM);
433 local_P = transform_point(&itfm, ray->P);
434 local_D = transform_direction(&itfm, ray->D);
435 }
436
437 /* Convert to octree space. */
438 if (local.to_octree_space(local_P, local_D, kroot->scale, kroot->translation)) {
439 volume_voxel_get(kg, local);
440 local.t.max = local.ray_voxel_intersect(ray->tmax);
441 }
442 else {
443 /* Current ray segment lies outside of the octree, usually happens with implicit volume, i.e.
444 * everything behind a surface is considered as volume. */
445 local.t.max = ray->tmax;
446 }
447
448 local.sigma = volume_object_get_extrema<shadow>(
449 kg, ray, sd, state, local, rng_state, path_visibility, path_flag);
450 return true;
451}
452
453/* Find the current active ray segment.
454 * We might have multiple overlapping octrees, so find the smallest `tmax` of all and store the
455 * information of that octree in `OctreeTracing`.
456 * Meanwhile, accumulate the density of all the leaf nodes that overlap with the active segment. */
457template<const bool shadow, typename IntegratorGenericState>
458ccl_device bool volume_octree_setup(KernelGlobals kg,
459 const ccl_private Ray *ccl_restrict ray,
460 ccl_private ShaderData *ccl_restrict sd,
461 const IntegratorGenericState state,
462 const ccl_private RNGState *rng_state,
463 const PathRayVisibility path_visibility,
464 const uint32_t path_flag,
465 ccl_private OctreeTracing &global)
466{
467 if (global.no_overlap) {
468 /* If the current active octree is already set up. */
469 return !global.t.is_empty();
470 }
471
472 const VolumeStack skip = global.entry;
473
474 int i = 0;
475 for (;; i++) {
476 /* Loop through all the object in the volume stack and find their octrees. */
477 OctreeTracing local(global.t.min);
478 if (!volume_octree_setup_single<shadow>(
479 kg, ray, sd, state, rng_state, path_visibility, path_flag, local, skip, i))
480 {
481 break;
482 }
483
484 global.sigma += local.sigma;
485 if (local.t.max <= global.t.max) {
486 /* Replace the current active octree with the one that has the smallest `tmax`. */
487 local.sigma = global.sigma;
488 global = local;
489 }
490 }
491
492 if (i == 1) {
493 global.no_overlap = true;
494 }
495
496 return global.node && !global.t.is_empty();
497}
498
499/* Advance to the next adjacent leaf node and update the active interval. */
500template<const bool shadow, typename IntegratorGenericState>
501ccl_device_inline bool volume_octree_advance(KernelGlobals kg,
502 const ccl_private Ray *ccl_restrict ray,
503 ccl_private ShaderData *ccl_restrict sd,
504 const IntegratorGenericState state,
505 const ccl_private RNGState *rng_state,
506 const PathRayVisibility path_visibility,
507 const uint32_t path_flag,
508 ccl_private OctreeTracing &octree)
509{
510 if (octree.t.max >= ray->tmax) {
511 /* Reached the last segment. */
512 return false;
513 }
514
515 if (octree.next_scale > MANTISSA_BITS) {
516 if (fabsf(octree.t.max - ray->tmax) <= OVERLAP_EXP) {
517 /* This could happen due to numerical issues, when the bounding box overlaps with a
518 * primitive, but different intersections are registered for octree and ray intersection. */
519 return false;
520 }
521
522 /* Outside of the root node, continue tracing using the extrema of the root node. */
523 octree.t = {octree.t.max, ray->tmax};
524 octree.node = &kernel_data_fetch(volume_tree_nodes,
525 volume_find_octree_root(kg, octree.entry)->id);
526 }
527 else {
528 kernel_assert(octree.next_scale > octree.scale);
529
530 /* Fetch the common ancestor of the current and the next leaf nodes. */
531 for (; octree.scale < octree.next_scale; octree.scale++) {
532 kernel_assert(octree.node->parent != -1);
533 octree.node = &kernel_data_fetch(volume_tree_nodes, octree.node->parent);
534 }
535
536 /* Find the current active leaf node. */
537 volume_voxel_get(kg, octree);
538
539 /* Advance to the next segment. */
540 octree.t.min = octree.t.max;
541 octree.t.max = octree.ray_voxel_intersect(ray->tmax);
542 }
543
544 octree.sigma = volume_object_get_extrema<shadow>(
545 kg, ray, sd, state, octree, rng_state, path_visibility, path_flag);
546 return volume_octree_setup<shadow>(
547 kg, ray, sd, state, rng_state, path_visibility, path_flag, octree);
548}
549
551
552/* Volume Shadows
553 *
554 * These functions are used to attenuate shadow rays to lights. Both absorption
555 * and scattering will block light, represented by the extinction coefficient. */
556
557/* Advance until the majorant optical depth is at least one, or we have reached the end of the
558 * volume. Because telescoping has to take at least one sample per segment, having a larger
559 * segment helps to take less samples. */
560ccl_device_inline bool volume_octree_advance_shadow(KernelGlobals kg,
561 const ccl_private Ray *ccl_restrict ray,
562 ccl_private ShaderData *ccl_restrict sd,
564 ccl_private RNGState *rng_state,
565 const PathRayVisibility path_visibility,
566 const uint32_t path_flag,
567 ccl_private OctreeTracing &octree)
568{
569 /* Advance random number offset. */
570 rng_state->rng_offset += PRNG_BOUNCE_NUM;
571
572 Extrema<float> sigma = octree.t.is_empty() ? Extrema<float>(FLT_MAX, -FLT_MAX) : octree.sigma;
573 const float tmin = octree.t.min;
574
575 while (octree.t.is_empty() || sigma.range() * octree.t.length() < 1.0f) {
576 if (!volume_octree_advance<true>(
577 kg, ray, sd, state, rng_state, path_visibility, path_flag, octree))
578 {
579 return !octree.t.is_empty();
580 }
581
582 octree.sigma = sigma = merge(sigma, octree.sigma);
583 octree.t.min = tmin;
584 }
585
586 return true;
587}
588
605template<const bool shadow, typename IntegratorGenericState>
606ccl_device Spectrum volume_transmittance(KernelGlobals kg,
607 IntegratorGenericState state,
608 const ccl_private Ray *ray,
609 ccl_private ShaderData *ccl_restrict sd,
610 const float sigma_c,
611 const Interval<float> t,
612 const ccl_private RNGState *rng_state,
613 const PathRayVisibility path_visibility,
614 const uint32_t path_flag)
615{
616 constexpr float r = 0.9f;
617 const float ray_length = t.length();
618
619 /* Expected number of steps with residual ratio tracking. */
620 const float expected_steps = sigma_c * ray_length;
621 /* Number of samples for the biased estimator. */
622 const int k = clamp(int(roundf(expected_steps)), 1, VOLUME_MAX_STEPS);
623
624 /* Sample the evaluation order of the debiasing term. */
625 /* Use the same random number for all pixels to sync the workload on GPU. */
626 /* TODO(weizhen): need to check if such correlation introduces artefacts. */
627 const float rand = path_rng_1D(
628 kg, 0, rng_state->sample, rng_state->rng_offset + PRNG_VOLUME_EXPANSION_ORDER);
629 /* A hard cut-off to prevent taking too many samples on the GPU. The probability of going beyond
630 * this order is 1e-5f. */
631 constexpr int cut_off = 4;
632 float pmf;
633 /* Number of independent estimators of T_k. */
634 const int N = (sigma_c == 0.0f) ?
635 1 :
636 power_of_2(sample_geometric_distribution(rand, r, pmf, cut_off));
637
638 /* Total number of density evaluations. */
639 const int samples = N * k;
640
641 const float shade_offset = path_state_rng_1D(kg, rng_state, PRNG_VOLUME_SHADE_OFFSET);
642 const float step_size = ray_length / float(samples);
643
644 if (N == 1) {
645 /* Only compute the biased estimator. */
646 Spectrum tau_k = zero_spectrum();
647 for (int i = 0; i < k; i++) {
648 const float shade_t = min(t.max, t.min + (shade_offset + i) * step_size);
649 sd->P = ray->P + ray->D * shade_t;
650 tau_k += volume_shader_eval_extinction<shadow>(kg, state, sd, path_visibility, path_flag);
651 }
652 /* OneAPI has some problem with exp(-0 * FLT_MAX). */
653 return is_zero(tau_k) ? one_spectrum() : exp(-tau_k * step_size);
654 }
655
656 /* Estimations of optical thickness. */
657 Spectrum tau_j[2];
658 tau_j[0] = zero_spectrum();
659 tau_j[1] = zero_spectrum();
660 Spectrum tau_j_1 = zero_spectrum();
661
662 Spectrum T_k = zero_spectrum();
663 for (int n = 0; n < N; n++) {
664 Spectrum tau_k = zero_spectrum();
665 for (int i = 0; i < k; i++) {
666 const int step = i * N + n;
667 const float shade_t = min(t.max, t.min + (shade_offset + step) * step_size);
668 sd->P = ray->P + ray->D * shade_t;
669
670 const Spectrum tau = step_size * volume_shader_eval_extinction<shadow>(
671 kg, state, sd, path_visibility, path_flag);
672
673 tau_k += tau * N;
674 tau_j[step % 2] += tau * 2.0f;
675 tau_j_1 += tau;
676 }
677 T_k += exp(-tau_k);
678 }
679
680 const Spectrum T_j_1 = exp(-tau_j_1);
681
682 /* Eq (16). This is the secondary estimator which averages a few independent estimations. */
683 T_k /= float(N);
684 const Spectrum T_j = 0.5f * (exp(-tau_j[0]) + exp(-tau_j[1]));
685
686 /* Eq (14), single-term primary estimator. */
687 return T_k + (T_j_1 - T_j) / pmf;
688}
689
690/* Compute the volumetric transmittance of the segment [ray->tmin, ray->tmax],
691 * used for the shadow ray throughput. */
692ccl_device void volume_shadow_null_scattering(KernelGlobals kg,
695 ccl_private ShaderData *ccl_restrict sd,
697{
698 if (volume_is_homogeneous<true>(kg, state)) {
699 const Spectrum extinction = volume_shader_eval_extinction<true>(
701 if (!is_zero(extinction)) {
702 *throughput *= volume_color_transmittance(extinction, ray->tmax - ray->tmin);
703 }
704 return;
705 }
706
707 /* Load random number state. */
708 RNGState rng_state;
710
711 /* For stochastic texture sampling. */
712 sd->lcg_state = lcg_state_init(
713 rng_state.rng_pixel, rng_state.rng_offset, rng_state.sample, 0xd9111870);
714
715 path_state_rng_scramble(&rng_state, 0x8647ace4);
716
717 OctreeTracing octree(ray->tmin);
718 const PathRayVisibility path_visibility = PATH_RAY_VISIBILITY_SHADOW;
719 const uint32_t path_flag = PATH_RAY_FLAG_NONE;
720 if (!volume_octree_setup<true>(
721 kg, ray, sd, state, &rng_state, path_visibility, path_flag, octree))
722 {
723 return;
724 }
725
726 while (volume_octree_advance_shadow(
727 kg, ray, sd, state, &rng_state, path_visibility, path_flag, octree))
728 {
729 const float sigma = octree.sigma.range();
730 *throughput *= volume_transmittance<true>(
731 kg, state, ray, sd, sigma, octree.t, &rng_state, path_visibility, path_flag);
732
733 if (reduce_max(fabs(*throughput)) < VOLUME_THROUGHPUT_EPSILON) {
734 return;
735 }
736 octree.t.min = octree.t.max;
737 }
738}
739
740/* Equiangular sampling as in:
741 * "Importance Sampling Techniques for Path Tracing in Participating Media" */
742
743/* Below this pdf we ignore samples, as they tend to lead to very long distances.
744 * This can cause performance issues with BVH traversal in OptiX, leading it to
745 * traverse many nodes. Since these contribute very little to the image, just ignore
746 * those samples. */
747# define VOLUME_SAMPLE_PDF_CUTOFF 1e-8f
748
749ccl_device float volume_equiangular_sample(const ccl_private Ray *ccl_restrict ray,
750 const ccl_private EquiangularCoefficients &coeffs,
751 const float xi,
752 ccl_private float *pdf)
753{
754 const float delta = dot((coeffs.P - ray->P), ray->D);
755 const float D = len(coeffs.P - ray->P - ray->D * delta);
756 if (UNLIKELY(D == 0.0f)) {
757 *pdf = 0.0f;
758 return 0.0f;
759 }
760 const float tmin = coeffs.t_range.min;
761 const float tmax = coeffs.t_range.max;
762
763 const float theta_a = atan2f(tmin - delta, D);
764 const float theta_b = atan2f(tmax - delta, D);
765 const float theta_d = theta_b - theta_a;
766 if (UNLIKELY(theta_d < 1e-6f)) {
767 /* Use uniform sampling when `theta_d` is too small. */
768 *pdf = safe_divide(1.0f, tmax - tmin);
769 return mix(tmin, tmax, xi);
770 }
771
772 const float t_ = D * tanf((xi * theta_b) + (1 - xi) * theta_a);
773 *pdf = D / (theta_d * (D * D + t_ * t_));
774
775 return clamp(delta + t_, tmin, tmax); /* clamp is only for float precision errors */
776}
777
778ccl_device float volume_equiangular_pdf(const ccl_private Ray *ccl_restrict ray,
779 const ccl_private EquiangularCoefficients &coeffs,
780 const float sample_t)
781{
782 const float delta = dot((coeffs.P - ray->P), ray->D);
783 const float D = len(coeffs.P - ray->P - ray->D * delta);
784 if (UNLIKELY(D == 0.0f)) {
785 return 0.0f;
786 }
787
788 const float tmin = coeffs.t_range.min;
789 const float tmax = coeffs.t_range.max;
790
791 const float theta_a = atan2f(tmin - delta, D);
792 const float theta_b = atan2f(tmax - delta, D);
793 const float theta_d = theta_b - theta_a;
794 if (UNLIKELY(theta_d < 1e-6f)) {
795 return safe_divide(1.0f, tmax - tmin);
796 }
797
798 const float t_ = sample_t - delta;
799 const float pdf = D / (theta_d * (D * D + t_ * t_));
800
801 return pdf;
802}
803
804/* Compute ray segment directly visible to the sampled light. */
805ccl_device_inline bool volume_valid_direct_ray_segment(KernelGlobals kg,
806 const float3 ray_P,
807 const float3 ray_D,
809 const ccl_private LightSample *ls)
810{
811 if (ls->type == LIGHT_SPOT) {
812 const ccl_global KernelLight *klight = &kernel_data_fetch(lights, ls->prim);
813 return spot_light_valid_ray_segment(kg, klight, ray_P, ray_D, t_range);
814 }
815 if (ls->type == LIGHT_AREA) {
816 const ccl_global KernelLight *klight = &kernel_data_fetch(lights, ls->prim);
817 return area_light_valid_ray_segment(&klight->area, ray_P - klight->co, ray_D, t_range);
818 }
819 if (ls->type == LIGHT_TRIANGLE) {
820 return triangle_light_valid_ray_segment(kg, ray_P - ls->P, ray_D, t_range, ls);
821 }
822
823 /* Point light or distant light, the whole range of the ray is visible. */
824 kernel_assert(ls->type == LIGHT_POINT || ls->t == FLT_MAX);
825 return !t_range->is_empty();
826}
827
828/* Emission */
829
830ccl_device Spectrum volume_emission_integrate(ccl_private VolumeShaderCoefficients *coeff,
831 const int runtime_flag,
832 const float t)
833{
834 /* integral E * exp(-sigma_t * t) from 0 to t = E * (1 - exp(-sigma_t * t))/sigma_t
835 * this goes to E * t as sigma_t goes to zero. */
836 Spectrum emission = coeff->emission;
837
838 if (runtime_flag & SR_EXTINCTION) {
839 const Spectrum optical_depth = coeff->sigma_t * t;
840 emission *= select(optical_depth > 1e-5f,
841 (1.0f - exp(-optical_depth)) / coeff->sigma_t,
842 /* Second order Taylor expansion to avoid precision issue. */
843 t * (1.0f - 0.5f * optical_depth));
844 }
845 else {
846 emission *= t;
847 }
848
849 return emission;
850}
851
852/* Volume Integration */
853
854struct VolumeIntegrateState {
855 /* Random number. */
856 float rscatter;
857
858 /* Method used for sampling direct scatter position. */
859 VolumeSampleMethod direct_sample_method;
860 /* Probability of sampling the scatter position using null scattering. */
861 float distance_pdf;
862 /* Probability of sampling the scatter position using equiangular sampling. */
863 float equiangular_pdf;
864 /* Majorant density at the equiangular scatter position. Used to compute the pdf. */
865 float sigma_max;
866
867 /* Ratio tracking estimator of the volume transmittance, with MIS applied. */
868 float transmittance;
869 /* Current sample position. */
870 float t;
871 /* Majorant optical depth until now. */
872 float optical_depth;
873 /* Steps taken while tracking. Should not exceed `VOLUME_MAX_STEPS`. */
875 /* Multiple importance sampling. */
876 bool use_mis;
877
878 /* Volume scattering probability guiding. */
879 bool vspg;
880 /* The guided probability that the ray is scattered in the volume. `P_vol` in the paper. */
881 float scatter_prob;
882 /* Minimal scale of majorant for achieving the desired scatter probability. */
883 float majorant_scale;
884 /* Scale to apply after direct throughput due to Russian Roulette. */
885 float direct_rr_scale;
886
887 /* Extra fields for path guiding and denoising. */
888 PackedSpectrum emission;
889# ifdef __DENOISING_FEATURES__
890 PackedSpectrum albedo;
891# endif
892
893 /* The distance between the current and the last sample position. */
894 float dt;
895 /* `dt` at equiangular scatter position. Used to compute the pdf. */
896 float sample_dt;
897};
898
899/* Accumulate transmittance for equiangular distance sampling without MIS. Using telescoping to
900 * reduce noise. */
901ccl_device_inline void volume_equiangular_transmittance(
902 KernelGlobals kg,
904 const ccl_private Ray *ccl_restrict ray,
905 const ccl_private Extrema<float> &sigma,
906 const ccl_private Interval<float> &interval,
907 ccl_private ShaderData *ccl_restrict sd,
908 const ccl_private RNGState *rng_state,
909 const ccl_private VolumeIntegrateState &ccl_restrict vstate,
910 ccl_private VolumeIntegrateResult &ccl_restrict result)
911{
912 if (vstate.direct_sample_method != VOLUME_SAMPLE_EQUIANGULAR || vstate.use_mis ||
913 result.direct_scatter)
914 {
915 return;
916 }
917
919 if (interval.contains(result.direct_t)) {
920 /* Compute transmittance until the direct scatter position. */
921 t = {interval.min, result.direct_t};
922 result.direct_scatter = true;
923 }
924 else {
925 /* Compute transmittance of the whole segment. */
926 t = interval;
927 }
928
929 const PathRayVisibility path_visibility = INTEGRATOR_STATE(state, path, visibility);
930 const uint32_t path_flag = INTEGRATOR_STATE(state, path, flag);
931 result.direct_throughput *= volume_transmittance<false>(
932 kg, state, ray, sd, sigma.range(), t, rng_state, path_visibility, path_flag);
933}
934
935/* Sample the next candidate indirect scatter position following exponential distribution,
936 * and compute the direct throughput for equiangular sampling if using MIS.
937 * Returns true if should continue advancing. */
938ccl_device_inline bool volume_indirect_scatter_advance(const ccl_private OctreeTracing &octree,
939 const bool equiangular,
940 ccl_private float &residual_optical_depth,
941 ccl_private VolumeIntegrateState &vstate,
942 ccl_private VolumeIntegrateResult &result)
943{
944 const float sigma_max = octree.sigma.max * vstate.majorant_scale;
945 residual_optical_depth = (octree.t.max - vstate.t) * sigma_max;
946 if (sigma_max == 0.0f) {
947 return true;
948 }
949
950 vstate.dt = sample_exponential_distribution(vstate.rscatter, sigma_max);
951 vstate.t += vstate.dt;
952
953 const bool segment_has_equiangular = equiangular && octree.t.contains(result.direct_t);
954 if (segment_has_equiangular && vstate.t > result.direct_t && !result.direct_scatter) {
955 /* Stepped beyond the equiangular scatter position, compute direct throughput. */
956 result.direct_scatter = true;
957 result.direct_throughput = result.indirect_throughput * vstate.transmittance *
958 vstate.direct_rr_scale;
959 vstate.sample_dt = result.direct_t - vstate.t + vstate.dt;
960 vstate.distance_pdf = vstate.transmittance * sigma_max;
961 vstate.sigma_max = sigma_max;
962 }
963
964 /* Sampled a position outside the current voxel. */
965 return vstate.t > octree.t.max;
966}
967
969ccl_device_inline bool volume_integrate_advance(KernelGlobals kg,
970 const ccl_private Ray *ccl_restrict ray,
971 ccl_private ShaderData *ccl_restrict sd,
973 ccl_private RNGState *rng_state,
974 const PathRayVisibility path_visibility,
975 const uint32_t path_flag,
976 ccl_private OctreeTracing &octree,
977 ccl_private VolumeIntegrateState &vstate,
978 ccl_private VolumeIntegrateResult &result)
979{
980 if (vstate.step++ > VOLUME_MAX_STEPS) {
981 /* Exceeds maximal steps. */
982 return false;
983 }
984
985 float residual_optical_depth;
986 vstate.rscatter = path_state_rng_1D(kg, rng_state, PRNG_VOLUME_SCATTER_DISTANCE);
987 const bool equiangular = (vstate.direct_sample_method == VOLUME_SAMPLE_EQUIANGULAR) &&
988 vstate.use_mis;
989
990 while (
991 volume_indirect_scatter_advance(octree, equiangular, residual_optical_depth, vstate, result))
992 {
993 /* Advance to the next voxel if the sampled distance is beyond the current voxel. */
994 if (!volume_octree_advance<false>(
995 kg, ray, sd, state, rng_state, path_visibility, path_flag, octree))
996 {
997 return false;
998 }
999
1000 vstate.optical_depth += octree.sigma.max * octree.t.length();
1001 vstate.t = octree.t.min;
1002 volume_equiangular_transmittance(
1003 kg, state, ray, octree.sigma, octree.t, sd, rng_state, vstate, result);
1004
1005 /* Scale the random number by the residual depth for reusing. */
1006 vstate.rscatter = saturatef(1.0f - (1.0f - vstate.rscatter) * expf(residual_optical_depth));
1007 }
1008
1009 /* Advance random number offset. */
1010 rng_state->rng_offset += PRNG_BOUNCE_NUM;
1011
1012 return true;
1013}
1014
1015/* -------------------------------------------------------------------- */
1026
1027/* Candidate scatter position for VSPG. */
1028struct VolumeSampleCandidate {
1029 PackedSpectrum emission;
1030 float t;
1031 PackedSpectrum throughput;
1032 float distance_pdf;
1033# ifdef __DENOISING_FEATURES__
1034 PackedSpectrum albedo;
1035# endif
1036 /* Remember the random number so that we sample the sample point for stochastic evaluation. */
1037 uint lcg_state;
1038};
1039
1040/* Sample reservoir for VSPG. */
1041struct VolumeSampleReservoir {
1042 float total_weight = 0.0f;
1043 float rand;
1044 VolumeSampleCandidate candidate;
1045
1046 ccl_device_inline_method VolumeSampleReservoir(const float rand_) : rand(rand_) {}
1047
1048 /* Stream the candidate samples through the reservoir. */
1049 ccl_device_inline_method void add_sample(const float weight,
1050 const VolumeSampleCandidate new_candidate)
1051 {
1052 if (!(weight > 0.0f)) {
1053 return;
1054 }
1055
1056 total_weight += weight;
1057 const float thresh = weight / total_weight;
1058
1059 if ((rand <= thresh) || (total_weight == weight)) {
1060 /* Explicitly select the first candidate in case of numerical issues. */
1061 candidate = new_candidate;
1062 rand /= thresh;
1063 }
1064 else {
1065 rand = (rand - thresh) / (1.0f - thresh);
1066 }
1067
1068 /* Ensure the `rand` is always within 0..1 range, which could be violated above when
1069 * `-ffast-math` is used. */
1070 rand = saturatef(rand);
1071 }
1072
1073 ccl_device_inline_method bool is_empty() const
1074 {
1075 return total_weight == 0.0f;
1076 }
1077};
1078
1079/* Estimate volume majorant optical depth `\sum\sigma_{max}t` along the ray, by accumulating the
1080 * result from previous samples in a render buffer. */
1081ccl_device_inline float volume_majorant_optical_depth(KernelGlobals kg,
1082 const ccl_global float *buffer)
1083{
1084 kernel_assert(kernel_data.film.pass_volume_majorant != PASS_UNUSED);
1085 kernel_assert(kernel_data.film.pass_volume_majorant_sample_count != PASS_UNUSED);
1086
1087 const ccl_global float *accumulated_optical_depth = buffer +
1088 kernel_data.film.pass_volume_majorant;
1089 const ccl_global float *count = buffer + kernel_data.film.pass_volume_majorant_sample_count;
1090
1091 /* Assume `FLT_MAX` when we have no information of the optical depth. */
1092 return (*count == 0.0f) ? FLT_MAX : *accumulated_optical_depth / *count;
1093}
1094
1095/* Compute guided volume scatter probability and the majorant scale needed for achieving the
1096 * scatter probability, for heterogeneous volume. */
1097ccl_device_inline void volume_scatter_probability_heterogeneous(
1098 KernelGlobals kg,
1099 const IntegratorState state,
1101 ccl_private VolumeIntegrateState &vstate)
1102{
1103 if (!vstate.vspg) {
1104 return;
1105 }
1106
1107 const ccl_global float *buffer = film_pass_pixel_render_buffer(kg, state, render_buffer);
1108
1109 kernel_assert(kernel_data.film.pass_volume_scatter_denoised != PASS_UNUSED);
1110 kernel_assert(kernel_data.film.pass_volume_transmit_denoised != PASS_UNUSED);
1111
1112 /* Contribution based criterion, see Eq. (15). */
1113 const float L_scattered = reduce_add(
1114 kernel_read_pass_rgbe(buffer + kernel_data.film.pass_volume_scatter_denoised));
1115 const float L_transmitted = reduce_add(
1116 kernel_read_pass_rgbe(buffer + kernel_data.film.pass_volume_transmit_denoised));
1117 const float L_volume = L_transmitted + L_scattered;
1118
1119 /* Compute guided scattering probability. */
1120 if (L_volume == 0.0f) {
1121 /* Equal probability if no information gathered yet. */
1122 vstate.scatter_prob = 0.5f;
1123 }
1124 else {
1125 /* Exponential distribution has non-zero probability beyond the boundary, so the scatter
1126 * probability can never reach 1. Clamp to avoid scaling the majorant to infinity. */
1127 vstate.scatter_prob = fminf(L_scattered / L_volume, 0.9999f);
1128 }
1129
1130 const float optical_depth = volume_majorant_optical_depth(kg, buffer);
1131
1132 /* There is a non-zero probability of sampling no scatter events in the volume segment. In order
1133 * to reach the desired scattering probability, we might need to upscale the majorant and/or the
1134 * guiding scattering probability. See Eq (25,26). */
1135 vstate.majorant_scale = (optical_depth == 0.0f) ?
1136 1.0f :
1137 -fast_logf(1.0f - vstate.scatter_prob) / optical_depth;
1138 if (vstate.majorant_scale < 1.0f) {
1139 vstate.majorant_scale = 1.0f;
1140 vstate.scatter_prob = safe_divide(vstate.scatter_prob, 1.0f - fast_expf(-optical_depth));
1141 }
1142 else {
1143 vstate.scatter_prob = 1.0f;
1144 }
1145}
1146
1147/* Final guiding decision on sampling scatter or transmit event. */
1148ccl_device_inline void volume_distance_sampling_finalize(
1149 KernelGlobals kg,
1150 const IntegratorState state,
1151 const ccl_private Ray *ccl_restrict ray,
1152 ccl_private ShaderData *ccl_restrict sd,
1153 ccl_private VolumeIntegrateState &ccl_restrict vstate,
1154 ccl_private VolumeIntegrateResult &ccl_restrict result,
1155 ccl_private VolumeSampleReservoir &reservoir)
1156{
1157 if (reservoir.is_empty()) {
1158 return;
1159 }
1160
1161 const bool sample_distance = !(INTEGRATOR_STATE(state, path, flag) & PATH_RAY_TERMINATE) &&
1162 (vstate.direct_sample_method == VOLUME_SAMPLE_DISTANCE);
1163
1164 if (!vstate.vspg) {
1165 result.indirect_throughput = reservoir.candidate.throughput;
1166 vstate.emission = reservoir.candidate.emission;
1167# ifdef __DENOISING_FEATURES__
1168 vstate.albedo = reservoir.candidate.albedo;
1169# endif
1170 result.indirect_t = reservoir.candidate.t;
1171
1172 if (sample_distance) {
1173 /* If using distance sampling for direct light, just copy parameters of indirect light
1174 * since we scatter at the same point. */
1175 result.direct_scatter = true;
1176 result.direct_t = result.indirect_t;
1177 result.direct_throughput = result.indirect_throughput;
1178 if (vstate.use_mis) {
1179 vstate.distance_pdf = reservoir.candidate.distance_pdf;
1180 }
1181 }
1182 return;
1183 }
1184
1185 const uint lcg_state = reservoir.candidate.lcg_state;
1186
1187 if (sample_distance) {
1188 /* Always sample direct scatter, regardless of indirect scatter guiding decision. */
1189 result.direct_throughput = reservoir.candidate.throughput * reservoir.total_weight;
1190 vstate.distance_pdf = reservoir.candidate.distance_pdf;
1191 }
1192
1193 /* We only guide scatter decisions, no need to apply on emission and albedo. */
1194 vstate.emission = mix(vstate.emission, reservoir.candidate.emission, reservoir.total_weight);
1195# ifdef __DENOISING_FEATURES__
1196 vstate.albedo = mix(vstate.albedo, reservoir.candidate.albedo, reservoir.total_weight);
1197# endif
1198
1199 const float unguided_scatter_prob = reservoir.total_weight;
1200 float guided_scatter_prob;
1201 if (is_zero(result.indirect_throughput)) {
1202 /* Always sample scatter event if the contribution of transmitted event is zero. */
1203 guided_scatter_prob = 1.0f;
1204 }
1205 else {
1206 /* Defensive resampling. */
1207 const float alpha = 0.75f;
1208 reservoir.total_weight = mix(reservoir.total_weight, vstate.scatter_prob, alpha);
1209 guided_scatter_prob = reservoir.total_weight;
1210
1211 /* Add transmitted candidate. */
1212 reservoir.add_sample(
1213 1.0f - guided_scatter_prob,
1215 { vstate.emission, reservoir.candidate.t, result.indirect_throughput, 0.0f, vstate.albedo }
1216# else
1217 {vstate.emission, reservoir.candidate.t, result.indirect_throughput, 0.0f}
1218# endif
1219 );
1220 }
1221
1222 const bool scatter = (reservoir.candidate.distance_pdf > 0.0f);
1223 const float scale = scatter ? unguided_scatter_prob / guided_scatter_prob :
1224 (1.0f - unguided_scatter_prob) / (1.0f - guided_scatter_prob);
1225 result.indirect_throughput = reservoir.candidate.throughput * scale;
1226
1227 if (!scatter && !sample_distance) {
1228 /* No scatter event sampled. */
1229 return;
1230 }
1231
1232 /* Recover the volume coefficients at the scatter position. */
1233 sd->P = ray->P + ray->D * reservoir.candidate.t;
1234 sd->lcg_state = lcg_state;
1235 VolumeShaderCoefficients coeff ccl_optional_struct_init;
1236 if (!volume_shader_sample(kg, state, sd, &coeff)) {
1237 kernel_assert(false);
1238 return;
1239 }
1240
1241 kernel_assert(sd->runtime_flag & SR_SCATTER);
1242 if (sample_distance) {
1243 /* Direct scatter. */
1244 result.direct_scatter = true;
1245 result.direct_t = reservoir.candidate.t;
1246 volume_shader_copy_phases(&result.direct_phases, sd);
1247 }
1248
1249 if (scatter) {
1250 /* Indirect scatter. */
1251 result.indirect_scatter = true;
1252 result.indirect_t = reservoir.candidate.t;
1253 volume_shader_copy_phases(&result.indirect_phases, sd);
1254 }
1255}
1256
1258
1259ccl_device bool volume_integrate_should_stop(const ccl_private VolumeIntegrateResult &result)
1260{
1261 if (is_zero(result.indirect_throughput) && is_zero(result.direct_throughput)) {
1262 /* Stopped during Russian Roulette. */
1263 return true;
1264 }
1265
1266 /* If we have scattering data for both direct and indirect, we're done. */
1267 return (result.direct_scatter && result.indirect_scatter);
1268}
1269
1270/* Perform Russian Roulette termination to avoid drawing too many samples for indirect scatter, but
1271 * only if both direct and indirect scatter positions are available, or if no scattering is needed.
1272 */
1273ccl_device_inline bool volume_russian_roulette_termination(
1274 const IntegratorState state,
1275 ccl_private VolumeSampleReservoir &reservoir,
1276 ccl_private VolumeIntegrateResult &ccl_restrict result,
1277 ccl_private VolumeIntegrateState &ccl_restrict vstate)
1278{
1279 if (result.direct_scatter && result.indirect_scatter) {
1280 return true;
1281 }
1282
1283 const float thresh = reduce_max(fabs(result.indirect_throughput));
1284 if (thresh > 0.05f) {
1285 /* Only stop if contribution is low enough. */
1286 return false;
1287 }
1288
1289 /* Whether equiangular estimator of the direct throughput depends on the indirect throughput. */
1290 const bool equiangular = (vstate.direct_sample_method == VOLUME_SAMPLE_EQUIANGULAR) &&
1291 vstate.use_mis && !result.direct_scatter;
1292 /* Whether both indirect and direct scatter are possible. */
1293 const bool has_scatter_samples = !reservoir.is_empty() && !equiangular;
1294 /* The path is to be terminated, no scatter position is needed along the ray. */
1295 const bool absorption_only = INTEGRATOR_STATE(state, path, flag) & PATH_RAY_TERMINATE;
1296
1297 /* Randomly stop indirect scatter. */
1298 if (absorption_only || has_scatter_samples) {
1299 if (reservoir.rand > thresh) {
1300 result.indirect_throughput = zero_spectrum();
1301 if (equiangular || (vstate.direct_sample_method == VOLUME_SAMPLE_DISTANCE)) {
1302 /* Direct throughput depends on the indirect throughput, set to 0 for early termination. */
1303 result.direct_throughput = zero_spectrum();
1304 }
1305 return true;
1306 }
1307
1308 reservoir.rand = saturatef(reservoir.rand / thresh);
1309 result.indirect_throughput /= thresh;
1310 }
1311
1312 /* Randomly stop direct scatter. */
1313 if (equiangular) {
1314 if (reservoir.rand > thresh) {
1315 result.direct_scatter = true;
1316 result.direct_throughput = zero_spectrum();
1317 reservoir.rand = (reservoir.rand - thresh) / (1.0f - thresh);
1318 }
1319 else {
1320 reservoir.rand /= thresh;
1321 vstate.direct_rr_scale /= thresh;
1322 }
1323 reservoir.rand = saturatef(reservoir.rand);
1324 }
1325
1326 return false;
1327}
1328
1329/* -------------------------------------------------------------------- */
1332
1333/* In a null-scattering framework, we fill the volume with fictitious particles, so that the
1334 * density is `majorant` everywhere. The null-scattering coefficients `sigma_n` is then defined by
1335 * the density of such particles. */
1336ccl_device_inline Spectrum volume_null_event_coefficients(const Spectrum sigma_t,
1337 const float sigma_max,
1338 ccl_private float &majorant)
1339{
1340 majorant = fmaxf(reduce_max(sigma_t), sigma_max);
1341 return make_spectrum(majorant) - sigma_t;
1342}
1343
1344/* The probability of sampling real scattering event at each candidate point of delta tracking. */
1345ccl_device_inline float volume_scatter_probability(
1346 const ccl_private VolumeShaderCoefficients &coeff,
1347 const Spectrum sigma_n,
1348 const Spectrum throughput)
1349{
1350 /* We use `sigma_s` instead of `sigma_t` to skip sampling the absorption event, because it
1351 * always returns zero and has high variance. */
1352 const Spectrum sigma_c = coeff.sigma_s + sigma_n;
1353
1354 /* Set `albedo` to 1 for the channel where extinction coefficient `sigma_t` is zero, to make
1355 * sure that we sample a distance outside the current segment when that channel is picked,
1356 * meaning light passes through without attenuation. */
1357 const Spectrum albedo = safe_divide_color(coeff.sigma_s, coeff.sigma_t, 1.0f);
1358
1359 /* Assign weights per channel to pick scattering event based on throughput and single
1360 * scattering albedo. */
1361 /* TODO(weizhen): currently the sample distance is the same for each color channel, revisit
1362 * the MIS weight when we use Spectral Majorant. */
1363 const Spectrum channel_pdf = volume_sample_channel_pdf(albedo, throughput);
1364
1365 return dot(coeff.sigma_s / sigma_c, channel_pdf);
1366}
1367
1368/* Decide between real and null scatter events at the current position. */
1369ccl_device_inline void volume_sample_indirect_scatter(
1370 const float sigma_max,
1371 const float prob_s,
1372 const Spectrum sigma_s,
1373 ccl_private ShaderData *ccl_restrict sd,
1374 ccl_private VolumeIntegrateState &ccl_restrict vstate,
1375 ccl_private VolumeIntegrateResult &ccl_restrict result,
1376 const uint lcg_state,
1377 ccl_private VolumeSampleReservoir &reservoir)
1378{
1379 const float weight = vstate.transmittance * prob_s;
1380 const Spectrum throughput = result.indirect_throughput * sigma_s / prob_s;
1381
1382 if (vstate.vspg) {
1383 /* If we guide the scatter probability, simply put the candidate in the reservoir. */
1384 reservoir.add_sample(
1386 weight,
1387 {vstate.emission, vstate.t, throughput, weight * sigma_max, vstate.albedo, lcg_state}
1388# else
1389 weight, {vstate.emission, vstate.t, throughput, weight * sigma_max, lcg_state}
1390# endif
1391 );
1392 }
1393 else if (!result.indirect_scatter) {
1394 /* If no guiding and indirect scatter position has not been found, decide between real and null
1395 * scatter events. */
1396 if (reservoir.rand <= prob_s) {
1397 /* Rescale random number for reusing. */
1398 reservoir.rand /= prob_s;
1399
1400 /* Sampled scatter event. */
1401 result.indirect_scatter = true;
1402 volume_shader_copy_phases(&result.indirect_phases, sd);
1403 reservoir.add_sample(
1405 weight,
1406 {vstate.emission, vstate.t, throughput, weight * sigma_max, vstate.albedo, lcg_state}
1407# else
1408 weight, {vstate.emission, vstate.t, throughput, weight * sigma_max, lcg_state}
1409# endif
1410 );
1411
1412 if (vstate.direct_sample_method == VOLUME_SAMPLE_DISTANCE) {
1413 result.direct_scatter = true;
1414 volume_shader_copy_phases(&result.direct_phases, sd);
1415 }
1416 }
1417 else {
1418 /* Rescale random number for reusing. */
1419 reservoir.rand = (reservoir.rand - prob_s) / (1.0f - prob_s);
1420 }
1421 reservoir.rand = saturatef(reservoir.rand);
1422 }
1423}
1424
1441ccl_device void volume_integrate_step_scattering(
1442 KernelGlobals kg,
1443 const IntegratorState state,
1444 const ccl_private Ray *ccl_restrict ray,
1445 const float sigma_max,
1446 ccl_private ShaderData *ccl_restrict sd,
1447 ccl_private VolumeIntegrateState &ccl_restrict vstate,
1448 ccl_private VolumeIntegrateResult &ccl_restrict result,
1449 ccl_private VolumeSampleReservoir &reservoir)
1450{
1451 if (volume_russian_roulette_termination(state, reservoir, result, vstate)) {
1452 return;
1453 }
1454
1455 sd->P = ray->P + ray->D * vstate.t;
1456 VolumeShaderCoefficients coeff ccl_optional_struct_init;
1457 const uint lcg_state = sd->lcg_state;
1458 if (!volume_shader_sample(kg, state, sd, &coeff)) {
1459 return;
1460 }
1461
1462 kernel_assert(sigma_max != 0.0f);
1463
1464 /* Null scattering coefficients. */
1465 float majorant;
1466 const Spectrum sigma_n = volume_null_event_coefficients(coeff.sigma_t, sigma_max, majorant);
1467 if (majorant != sigma_max) {
1468 /* Standard null scattering uses the majorant as the rate parameter for distance sampling, thus
1469 * the MC estimator is
1470 * <L> = T(t) / p(t) * (L_e + sigma_s * L_s + sigma_n * L)
1471 * = 1 / majorant * (L_e + sigma_s * L_s + sigma_n * L).
1472 * If we use another rate parameter sigma for distance sampling, the equation becomes
1473 * <L> = T(t) / p(t) * (L_e + sigma_s * L_s + sigma_n * L)
1474 * = exp(-majorant * t) / sigma * exp(-sigma * t ) * (L_e + sigma_s * L_s + sigma_n *L),
1475 * there is a scaling of majorant / sigma * exp(-(majorant - sigma) * t).
1476 * NOTE: this is not really unbiased, because the scaling is only applied when we sample an
1477 * event inside the segment, but in practice, if the majorant is reasonable, this doesn't
1478 * happen too often and shouldn't affect the result much. */
1479 result.indirect_throughput *= expf((sigma_max - majorant) * vstate.dt) / sigma_max;
1480 }
1481 else {
1482 result.indirect_throughput /= majorant;
1483 }
1484
1485 /* Emission. */
1486 if (sd->runtime_flag & SR_EMISSION) {
1487 /* Emission = inv_sigma * (L_e + sigma_n * (inv_sigma * (L_e + sigma_n * ···))). */
1488 vstate.emission += result.indirect_throughput * coeff.emission;
1489 if (!result.indirect_scatter) {
1490 /* Record emission until scatter position. */
1491 guiding_record_volume_emission(kg, state, coeff.emission);
1492 }
1493 }
1494
1495 if (reduce_add(coeff.sigma_s) == 0.0f) {
1496 /* Absorption only. Deterministically choose null scattering and estimate the transmittance
1497 * of the current ray segment. */
1498 result.indirect_throughput *= sigma_n;
1499 return;
1500 }
1501
1502# ifdef __DENOISING_FEATURES__
1504 /* Albedo = inv_sigma * (sigma_s + sigma_n * (inv_sigma * (sigma_s + sigma_n * ···))). */
1505 vstate.albedo += result.indirect_throughput * coeff.sigma_s;
1506 }
1507# endif
1508
1509 /* Indirect scatter. */
1510 const float prob_s = volume_scatter_probability(coeff, sigma_n, result.indirect_throughput);
1511 volume_sample_indirect_scatter(
1512 sigma_max, prob_s, coeff.sigma_s, sd, vstate, result, lcg_state, reservoir);
1513
1514 /* Null scattering. Accumulate weight and continue. */
1515 const float prob_n = 1.0f - prob_s;
1516 result.indirect_throughput *= safe_divide(sigma_n, prob_n);
1517 vstate.transmittance *= prob_n;
1518}
1519
1520/* Evaluate coefficients at the equiangular scatter position, and update the direct throughput. */
1521ccl_device_inline void volume_equiangular_direct_scatter(
1522 KernelGlobals kg,
1523 const IntegratorState state,
1524 const ccl_private Ray *ccl_restrict ray,
1525 ccl_private ShaderData *ccl_restrict sd,
1526 ccl_private VolumeIntegrateState &vstate,
1527 ccl_private VolumeIntegrateResult &ccl_restrict result)
1528{
1529 if (vstate.direct_sample_method != VOLUME_SAMPLE_EQUIANGULAR || !result.direct_scatter) {
1530 return;
1531 }
1532
1533 sd->P = ray->P + ray->D * result.direct_t;
1534 VolumeShaderCoefficients coeff ccl_optional_struct_init;
1535 if (volume_shader_sample(kg, state, sd, &coeff) && (sd->runtime_flag & SR_SCATTER)) {
1536 volume_shader_copy_phases(&result.direct_phases, sd);
1537
1538 if (vstate.use_mis) {
1539 /* Compute distance pdf for multiple importance sampling. */
1540 float majorant;
1541 const Spectrum sigma_n = volume_null_event_coefficients(
1542 coeff.sigma_t, vstate.sigma_max, majorant);
1543 if ((vstate.sample_dt != FLT_MAX) && (majorant != vstate.sigma_max)) {
1544 result.direct_throughput *= expf((vstate.sigma_max - majorant) * vstate.sample_dt);
1545 }
1546 vstate.distance_pdf *= volume_scatter_probability(coeff, sigma_n, result.direct_throughput);
1547 }
1548
1549 result.direct_throughput *= safe_divide(coeff.sigma_s, vstate.equiangular_pdf);
1550 }
1551 else {
1552 /* Scattering coefficient is zero at the sampled position. */
1553 result.direct_scatter = false;
1554 }
1555}
1556
1557/* Multiple Importance Sampling between equiangular sampling and distance sampling.
1558 *
1559 * According to [A null-scattering path integral formulation of light transport]
1560 * (https://cs.dartmouth.edu/~wjarosz/publications/miller19null.html), the pdf of sampling a
1561 * scattering event at point P using distance sampling is the probability of sampling a series of
1562 * null events, and then a scatter event at P, i.e.
1563 *
1564 * distance_pdf = (∏p_dist * p_null) * p_dist * p_scatter,
1565 *
1566 * where `p_dist = sigma_max * exp(-sigma_max * dt)` is the probability of sampling an incremental
1567 * distance `dt` following exponential distribution, and `p_null = sigma_n / sigma_c` is the
1568 * probability of sampling a null event at a certain point, `p_scatter = sigma_s / sigma_c` the
1569 * probability of sampling a scatter event.
1570 *
1571 * The pdf of sampling a scattering event at point P using equiangular sampling is the probability
1572 * of sampling a series of null events deterministically, and then a scatter event at the point of
1573 * equiangular sampling, i.e.
1574 *
1575 * equiangular_pdf = (∏p_dist * 1) * T * p_equi,
1576 *
1577 * where `T = exp(-sigma_max * dt)` is the probability of sampling a distance beyond `dt` following
1578 * exponential distribution, `p_equi` is the equiangular pdf. Since the null events are sampled
1579 * deterministically, the pdf is 1 instead of `p_null`.
1580 *
1581 * When performing MIS between distance and equiangular sampling, since we use single-channel
1582 * majorant, `p_dist` is shared in both pdfs, therefore we can write
1583 *
1584 * distance_pdf / equiangular_pdf = (∏p_null) * sigma_max * p_scatter / p_equi.
1585 *
1586 * If we want to use multi-channel majorants in the future, the components do not cancel, but we
1587 * can divide by the `p_dist` of the hero channel to alleviate numerical issues.
1588 */
1589ccl_device_inline void volume_direct_scatter_mis(
1590 const ccl_private Ray *ccl_restrict ray,
1591 const ccl_private VolumeIntegrateState &ccl_restrict vstate,
1592 const ccl_private EquiangularCoefficients &equiangular_coeffs,
1593 ccl_private VolumeIntegrateResult &ccl_restrict result)
1594{
1595 if (!vstate.use_mis || !result.direct_scatter) {
1596 return;
1597 }
1598
1599 float mis_weight;
1600 if (vstate.direct_sample_method == VOLUME_SAMPLE_DISTANCE) {
1601 const float equiangular_pdf = volume_equiangular_pdf(ray, equiangular_coeffs, result.direct_t);
1602 mis_weight = power_heuristic(vstate.distance_pdf, equiangular_pdf);
1603 }
1604 else {
1605 kernel_assert(vstate.direct_sample_method == VOLUME_SAMPLE_EQUIANGULAR);
1606 mis_weight = power_heuristic(vstate.equiangular_pdf, vstate.distance_pdf);
1607 }
1608
1609 result.direct_throughput *= 2.0f * mis_weight;
1610}
1611
1613
1614ccl_device_inline void volume_integrate_state_init(KernelGlobals kg,
1615 const IntegratorState state,
1616 const VolumeSampleMethod direct_sample_method,
1617 const ccl_private RNGState *rng_state,
1618 const float tmin,
1619 ccl_private VolumeIntegrateState &vstate)
1620{
1621 vstate.rscatter = path_state_rng_1D(kg, rng_state, PRNG_VOLUME_SCATTER_DISTANCE);
1622
1623 /* Multiple importance sampling: pick between equiangular and distance sampling strategy. */
1624 vstate.direct_sample_method = direct_sample_method;
1625 vstate.use_mis = (direct_sample_method == VOLUME_SAMPLE_MIS);
1626 if (vstate.use_mis) {
1627 if (vstate.rscatter < 0.5f) {
1628 vstate.direct_sample_method = VOLUME_SAMPLE_DISTANCE;
1629 vstate.rscatter *= 2.0f;
1630 }
1631 else {
1632 /* Rescale for equiangular distance sampling. */
1633 vstate.rscatter = (vstate.rscatter - 0.5f) * 2.0f;
1634 vstate.direct_sample_method = VOLUME_SAMPLE_EQUIANGULAR;
1635 }
1636 }
1637
1638 vstate.distance_pdf = 0.0f;
1639 vstate.equiangular_pdf = 0.0f;
1640 vstate.sigma_max = 0.0f;
1641 vstate.transmittance = 1.0f;
1642 vstate.t = tmin;
1643 vstate.optical_depth = 0.0f;
1644 vstate.step = 0;
1645 /* Only guide primary rays. */
1646 vstate.vspg = (INTEGRATOR_STATE(state, path, bounce) == 0);
1647 vstate.scatter_prob = 1.0f;
1648 vstate.majorant_scale = 1.0f;
1649 vstate.direct_rr_scale = 1.0f;
1650 vstate.emission = zero_spectrum();
1651# ifdef __DENOISING_FEATURES__
1652 vstate.albedo = zero_spectrum();
1653# endif
1654}
1655
1656ccl_device_inline void volume_integrate_result_init(
1658 const ccl_private Ray *ccl_restrict ray,
1659 ccl_private VolumeIntegrateState &vstate,
1660 const ccl_private EquiangularCoefficients &equiangular_coeffs,
1661 ccl_private VolumeIntegrateResult &result)
1662{
1663 const Spectrum throughput = INTEGRATOR_STATE(state, path, throughput);
1664 result.direct_throughput = (vstate.direct_sample_method == VOLUME_SAMPLE_NONE) ?
1665 zero_spectrum() :
1666 throughput;
1667 result.indirect_throughput = throughput;
1668
1669 /* Equiangular sampling: compute distance and PDF in advance. */
1670 if (vstate.direct_sample_method == VOLUME_SAMPLE_EQUIANGULAR) {
1671 result.direct_t = volume_equiangular_sample(
1672 ray, equiangular_coeffs, vstate.rscatter, &vstate.equiangular_pdf);
1673 }
1674
1675# if defined(__PATH_GUIDING__)
1676 result.direct_sample_method = vstate.direct_sample_method;
1677# endif
1678}
1679
1680/* Compute guided volume scatter probability and the majorant scale needed for achieving the
1681 * scatter probability, for homogeneous volume. */
1683volume_scatter_probability_homogeneous(KernelGlobals kg,
1684 const IntegratorState state,
1686 const float ray_length,
1687 const ccl_private VolumeShaderCoefficients &coeff,
1688 ccl_private VolumeIntegrateState &vstate)
1689{
1690 const bool attenuation_only = (INTEGRATOR_STATE(state, path, flag) & PATH_RAY_TERMINATE) ||
1691 is_zero(coeff.sigma_s);
1692 if (attenuation_only) {
1693 return zero_spectrum();
1694 }
1695
1696 const Spectrum attenuation = 1.0f - volume_color_transmittance(coeff.sigma_t, ray_length);
1697 if (!vstate.vspg) {
1698 return attenuation;
1699 }
1700
1701 const ccl_global float *buffer = film_pass_pixel_render_buffer(kg, state, render_buffer);
1702
1703 kernel_assert(kernel_data.film.pass_volume_scatter_denoised != PASS_UNUSED);
1704 kernel_assert(kernel_data.film.pass_volume_transmit_denoised != PASS_UNUSED);
1705
1706 /* Contribution based criterion, see Eq. (15). */
1707 const Spectrum L_scattered = kernel_read_pass_rgbe(
1708 buffer + kernel_data.film.pass_volume_scatter_denoised);
1709 const Spectrum L_transmitted = kernel_read_pass_rgbe(
1710 buffer + kernel_data.film.pass_volume_transmit_denoised);
1711 const Spectrum L_volume = L_transmitted + L_scattered;
1712
1713 Spectrum guided_scatter_prob;
1714 if (is_zero(L_volume)) {
1715 /* Equal probability if no information gathered yet. */
1716 guided_scatter_prob = select(coeff.sigma_t > 0.0f, make_spectrum(0.5f), zero_spectrum());
1717 }
1718 else {
1719 /* VSPG guide the scattering probability along the primary ray, but not necessarily in the
1720 * current segment. Scale the probability based on the relative majorant transmittance. */
1721 /* TODO(weizhen): spectrum optical depth. */
1722 const float optical_depth = volume_majorant_optical_depth(kg, buffer);
1723 const float scale = reduce_max(attenuation) / (1.0f - expf(-optical_depth));
1724
1725 guided_scatter_prob = clamp(
1726 safe_divide(L_scattered, L_volume) * scale, zero_spectrum(), one_spectrum());
1727 }
1728
1729 /* Defensive sampling. */
1730 return mix(attenuation, guided_scatter_prob, 0.75f);
1731}
1732
1733/* Homogeneous volume distance sampling, using analytic solution to avoid drawing multiple samples
1734 * with the reservoir.
1735 * Decide the indirect scatter probability, and sample an indirect scatter position inside the
1736 * volume or transmit through the volume.
1737 * Direct scatter is always sampled, if possible. */
1738ccl_device_forceinline void volume_integrate_homogeneous(KernelGlobals kg,
1739 const IntegratorState state,
1740 const ccl_private Ray *ccl_restrict ray,
1741 ccl_private ShaderData *ccl_restrict sd,
1742 const ccl_private RNGState *rng_state,
1745 ccl_private VolumeIntegrateState &vstate,
1746 const Interval<float> interval,
1747 ccl_private VolumeIntegrateResult &result)
1748{
1749 sd->P = ray->P + ray->D * ray->tmin;
1750 VolumeShaderCoefficients coeff ccl_optional_struct_init;
1751 if (!volume_shader_sample(kg, state, sd, &coeff)) {
1752 return;
1753 }
1754
1755 const float ray_length = ray->tmax - ray->tmin;
1756 vstate.optical_depth = reduce_max(coeff.sigma_t) * ray_length;
1757
1758 /* Emission. */
1759 const Spectrum throughput = INTEGRATOR_STATE(state, path, throughput);
1760 if (sd->runtime_flag & SR_EMISSION) {
1761 const Spectrum emission = volume_emission_integrate(&coeff, sd->runtime_flag, ray_length);
1762 vstate.emission = throughput * emission;
1764 }
1765
1766 /* Transmittance of the complete ray segment. */
1767 const Spectrum transmittance = volume_color_transmittance(coeff.sigma_t, ray_length);
1768 if ((INTEGRATOR_STATE(state, path, flag) & PATH_RAY_TERMINATE) || is_zero(coeff.sigma_s)) {
1769 /* Attenuation only. */
1770 result.indirect_throughput *= transmittance;
1771 return;
1772 }
1773
1774 float rchannel = path_state_rng_1D(kg, rng_state, PRNG_VOLUME_RESERVOIR);
1775 /* Single scattering albedo. */
1776 const Spectrum albedo = safe_divide_color(coeff.sigma_s, coeff.sigma_t);
1777 /* Multiple scattering albedo. */
1778 vstate.albedo = albedo * (1.0f - transmittance) * throughput;
1779
1780 /* Indirect scatter. */
1781 {
1782 /* Consider the contribution of both scattering and transmission when sampling indirect
1783 * scatter. */
1784 Spectrum channel_pdf;
1785 const int channel = volume_sample_channel(
1786 vstate.albedo + transmittance, throughput, &rchannel, &channel_pdf);
1787
1788 const Spectrum scatter_prob = volume_scatter_probability_homogeneous(
1789 kg, state, render_buffer, ray_length, coeff, vstate);
1790 const float scatter_pdf_channel = volume_channel_get(scatter_prob, channel);
1791
1792 if (vstate.rscatter < scatter_pdf_channel) {
1793 /* Sampled scatter event. */
1794 vstate.rscatter /= scatter_pdf_channel;
1795 const Interval<float> t_range = {0.0f, ray_length};
1796 result.indirect_scatter = !t_range.is_empty();
1797 const float sigma = volume_channel_get(coeff.sigma_t, channel);
1798 const float dt = sample_exponential_distribution(vstate.rscatter, sigma, t_range);
1799 result.indirect_t = ray->tmin + dt;
1800 const Spectrum distance_pdf = pdf_exponential_distribution(dt, coeff.sigma_t, t_range);
1801 const float indirect_distance_pdf = dot(distance_pdf * scatter_prob, channel_pdf);
1802 const Spectrum transmittance = volume_color_transmittance(coeff.sigma_t, dt);
1803 result.indirect_throughput *= safe_divide(coeff.sigma_s * transmittance,
1804 indirect_distance_pdf);
1805 volume_shader_copy_phases(&result.indirect_phases, sd);
1806 }
1807 else {
1808 /* Sampled transmit event. */
1809 const float indirect_distance_pdf = dot((1.0f - scatter_prob), channel_pdf);
1810 result.indirect_throughput *= safe_divide(transmittance, indirect_distance_pdf);
1811 vstate.rscatter = (vstate.rscatter - scatter_pdf_channel) / (1.0f - scatter_pdf_channel);
1812 }
1813 }
1814
1815 /* Direct scatter. */
1816 if (vstate.direct_sample_method == VOLUME_SAMPLE_NONE) {
1817 return;
1818 }
1819
1820 /* Sample inside the valid ray segment. */
1821 const Interval<float> t_range = {interval.min - ray->tmin, interval.max - ray->tmin};
1822 result.direct_scatter = !t_range.is_empty();
1823 volume_shader_copy_phases(&result.direct_phases, sd);
1824
1825 Spectrum channel_pdf;
1826 const int channel = volume_sample_channel(vstate.albedo, throughput, &rchannel, &channel_pdf);
1827
1828 if (vstate.direct_sample_method == VOLUME_SAMPLE_DISTANCE) {
1829 const float sigma = volume_channel_get(coeff.sigma_t, channel);
1830 const float dt = sample_exponential_distribution(vstate.rscatter, sigma, t_range);
1831 result.direct_t = ray->tmin + dt;
1832 const Spectrum distance_pdf = pdf_exponential_distribution(dt, coeff.sigma_t, t_range);
1833 vstate.distance_pdf = dot(distance_pdf, channel_pdf);
1834 const Spectrum transmittance = volume_color_transmittance(coeff.sigma_t, dt);
1835 result.direct_throughput *= safe_divide(coeff.sigma_s * transmittance, vstate.distance_pdf);
1836 }
1837 else {
1838 kernel_assert(vstate.direct_sample_method == VOLUME_SAMPLE_EQUIANGULAR);
1839 const float dt = result.direct_t - ray->tmin;
1840 const Spectrum transmittance = volume_color_transmittance(coeff.sigma_t, dt);
1841 result.direct_throughput *= safe_divide(coeff.sigma_s * transmittance, vstate.equiangular_pdf);
1842 if (vstate.use_mis) {
1843 vstate.distance_pdf = dot(pdf_exponential_distribution(dt, coeff.sigma_t, t_range),
1844 channel_pdf);
1845 }
1846 }
1847}
1848
1849/* heterogeneous volume distance sampling: integrate stepping through the
1850 * volume until we reach the end, get absorbed entirely, or run out of
1851 * iterations. this does probabilistically scatter or get transmitted through
1852 * for path tracing where we don't want to branch. */
1853ccl_device_forceinline void volume_integrate_heterogeneous(
1854 KernelGlobals kg,
1855 const IntegratorState state,
1856 const ccl_private Ray *ccl_restrict ray,
1857 ccl_private ShaderData *ccl_restrict sd,
1858 RNGState rng_state,
1860 ccl_private VolumeIntegrateState &vstate,
1861 ccl_private VolumeIntegrateResult &result)
1862{
1863 OctreeTracing octree(ray->tmin);
1864 const PathRayVisibility path_visibility = INTEGRATOR_STATE(state, path, visibility);
1865 const uint32_t path_flag = INTEGRATOR_STATE(state, path, flag);
1866 if (!volume_octree_setup<false>(
1867 kg, ray, sd, state, &rng_state, path_visibility, path_flag, octree))
1868 {
1869 return;
1870 }
1871
1872 /* Prepare struct for guiding. */
1873 vstate.optical_depth = octree.sigma.max * octree.t.length();
1874 volume_scatter_probability_heterogeneous(kg, state, render_buffer, vstate);
1875
1876 /* Initialize reservoir for sampling scatter position. */
1877 VolumeSampleReservoir reservoir = path_state_rng_1D(kg, &rng_state, PRNG_VOLUME_RESERVOIR);
1878
1879 /* Scramble for stepping through volume. */
1880 path_state_rng_scramble(&rng_state, 0xe35fad82);
1881
1882 volume_equiangular_transmittance(
1883 kg, state, ray, octree.sigma, octree.t, sd, &rng_state, vstate, result);
1884
1885 while (volume_integrate_advance(
1886 kg, ray, sd, state, &rng_state, path_visibility, path_flag, octree, vstate, result))
1887 {
1888 const float sigma_max = octree.sigma.max * vstate.majorant_scale;
1889 volume_integrate_step_scattering(kg, state, ray, sigma_max, sd, vstate, result, reservoir);
1890
1891 if (volume_integrate_should_stop(result)) {
1892 break;
1893 }
1894 }
1895
1896 volume_distance_sampling_finalize(kg, state, ray, sd, vstate, result, reservoir);
1897 volume_equiangular_direct_scatter(kg, state, ray, sd, vstate, result);
1898}
1899
1900/* Path tracing: sample point on light using equiangular sampling. */
1901ccl_device_forceinline bool integrate_volume_sample_direct_light(
1902 KernelGlobals kg,
1903 const IntegratorState state,
1904 const ccl_private Ray *ccl_restrict ray,
1905 const ccl_private ShaderData *ccl_restrict sd,
1906 const ccl_private RNGState *ccl_restrict rng_state,
1907 ccl_private EquiangularCoefficients *ccl_restrict equiangular_coeffs,
1909{
1910 /* Test if there is a light or BSDF that needs direct light. */
1911 if (!kernel_data.integrator.use_direct_light) {
1912 return false;
1913 }
1914
1915 /* Sample position on a light. */
1916 const uint32_t path_flag = INTEGRATOR_STATE(state, path, flag);
1917 const uint bounce = INTEGRATOR_STATE(state, path, bounce);
1918 const float3 rand_light = path_state_rng_3D(kg, rng_state, PRNG_LIGHT);
1919
1921 rand_light,
1922 sd->time,
1923 sd->P,
1924 ray->D,
1925 ray->tmax - ray->tmin,
1927 bounce,
1928 path_flag,
1929 ls))
1930 {
1931 ls->emitter_id = EMITTER_NONE;
1932 return false;
1933 }
1934
1935 if (ls->shader & SHADER_EXCLUDE_SCATTER) {
1936 ls->emitter_id = EMITTER_NONE;
1937 return false;
1938 }
1939
1940 equiangular_coeffs->P = ls->P;
1941
1942 return volume_valid_direct_ray_segment(kg, ray->P, ray->D, &equiangular_coeffs->t_range, ls);
1943}
1944
1945/* Determine the method to sample direct light, based on the volume property and settings. */
1946ccl_device_forceinline VolumeSampleMethod
1947volume_direct_sample_method(KernelGlobals kg,
1948 const IntegratorState state,
1949 const ccl_private Ray *ccl_restrict ray,
1950 const ccl_private ShaderData *ccl_restrict sd,
1951 const ccl_private RNGState *ccl_restrict rng_state,
1952 ccl_private EquiangularCoefficients *coeffs,
1954{
1955 if (INTEGRATOR_STATE(state, path, flag) & PATH_RAY_TERMINATE) {
1956 return VOLUME_SAMPLE_NONE;
1957 }
1958
1959 if (!integrate_volume_sample_direct_light(kg, state, ray, sd, rng_state, coeffs, ls)) {
1960 return VOLUME_SAMPLE_NONE;
1961 }
1962
1963 /* Sample the scatter position with distance sampling for distant light. */
1964 const bool has_equiangular_sample = (ls->t != FLT_MAX);
1965 return has_equiangular_sample ? volume_stack_sample_method(kg, state) : VOLUME_SAMPLE_DISTANCE;
1966}
1967
1968/* Shared function of integrating homogeneous and heterogeneous volume. */
1969ccl_device void volume_integrate_null_scattering(KernelGlobals kg,
1970 const IntegratorState state,
1971 const ccl_private Ray *ccl_restrict ray,
1972 ccl_private ShaderData *ccl_restrict sd,
1973 const ccl_private RNGState *rng_state,
1976 ccl_private VolumeIntegrateResult &result)
1977{
1979
1980 EquiangularCoefficients equiangular_coeffs = {zero_float3(), {ray->tmin, ray->tmax}};
1981 const VolumeSampleMethod direct_sample_method = volume_direct_sample_method(
1982 kg, state, ray, sd, rng_state, &equiangular_coeffs, ls);
1983
1984 /* Initialize volume integration state. */
1985 VolumeIntegrateState vstate ccl_optional_struct_init;
1986 volume_integrate_state_init(kg, state, direct_sample_method, rng_state, ray->tmin, vstate);
1987
1988 /* Initialize volume integration result. */
1989 volume_integrate_result_init(state, ray, vstate, equiangular_coeffs, result);
1990
1991 if (volume_is_homogeneous<false>(kg, state)) {
1992 volume_integrate_homogeneous(
1993 kg, state, ray, sd, rng_state, render_buffer, vstate, equiangular_coeffs.t_range, result);
1994 }
1995 else {
1996 volume_integrate_heterogeneous(kg, state, ray, sd, *rng_state, render_buffer, vstate, result);
1997 }
1998
1999 volume_direct_scatter_mis(ray, vstate, equiangular_coeffs, result);
2000
2001 /* Write accumulated emission. */
2002 if (!is_zero(vstate.emission)) {
2003 if (light_link_object_match(kg, light_link_receiver_forward(kg, state), sd->object)) {
2005 kg, state, vstate.emission, render_buffer, object_lightgroup(kg, sd->object));
2006 }
2007 }
2008
2009# ifdef __DENOISING_FEATURES__
2010 /* Write denoising features. */
2012 film_write_denoising_features_volume(
2013 kg, state, vstate.albedo, result.indirect_scatter, render_buffer);
2014 }
2015# endif /* __DENOISING_FEATURES__ */
2016
2017 if (INTEGRATOR_STATE(state, path, bounce) == 0) {
2018 INTEGRATOR_STATE_WRITE(state, path, optical_depth) += vstate.optical_depth;
2019 }
2020}
2021
2022/* -------------------------------------------------------------------- */
2025
2026/* Determines the next shading position. */
2027struct VolumeStep {
2028 /* Shift starting point of all segments by a random amount to avoid banding artifacts due to
2029 * biased ray marching with insufficient step size. */
2030 float offset;
2031
2032 /* Step size taken at each marching step. */
2033 float size;
2034
2035 /* Perform shading at this offset within a step, to integrate over the entire step segment. */
2036 float shade_offset;
2037
2038 /* Maximal steps allowed between `ray->tmin` and `ray->tmax`. */
2039 int max_steps;
2040
2041 /* Current active segment. */
2042 Interval<float> t;
2043};
2044
2045template<const bool shadow>
2046ccl_device_forceinline void volume_step_init(KernelGlobals kg,
2047 const ccl_private RNGState *rng_state,
2048 const float object_step_size,
2049 const float tmin,
2050 const float tmax,
2051 ccl_private VolumeStep *vstep)
2052{
2053 vstep->t.min = vstep->t.max = tmin;
2054
2055 if (object_step_size == FLT_MAX) {
2056 /* Homogeneous volume. */
2057 vstep->size = tmax - tmin;
2058 vstep->shade_offset = 0.0f;
2059 vstep->offset = 1.0f;
2060 vstep->max_steps = 1;
2061 }
2062 else {
2063 /* Heterogeneous volume. */
2064 vstep->max_steps = kernel_data.integrator.volume_max_steps;
2065 const float t = tmax - tmin;
2066 float step_size = min(object_step_size, t);
2067
2068 if (t > vstep->max_steps * step_size) {
2069 /* Increase step size to cover the whole ray segment. */
2070 step_size = t / (float)vstep->max_steps;
2071 }
2072
2073 vstep->size = step_size;
2074 vstep->shade_offset = path_state_rng_1D(kg, rng_state, PRNG_VOLUME_SHADE_OFFSET);
2075
2076 if (shadow) {
2077 /* For shadows we do not offset all segments, since the starting point is already a random
2078 * distance inside the volume. It also appears to create banding artifacts for unknown
2079 * reasons. */
2080 vstep->offset = 1.0f;
2081 }
2082 else {
2083 vstep->offset = path_state_rng_1D(kg, rng_state, PRNG_VOLUME_OFFSET);
2084 }
2085 }
2086}
2087
2088ccl_device_inline bool volume_ray_marching_advance(const int step,
2089 const ccl_private Ray *ccl_restrict ray,
2090 ccl_private float3 *shade_P,
2091 ccl_private VolumeStep &vstep)
2092{
2093 if (vstep.t.max == ray->tmax) {
2094 /* Reached the last segment. */
2095 return false;
2096 }
2097
2098 /* Advance to new position. */
2099 vstep.t.min = vstep.t.max;
2100 vstep.t.max = min(ray->tmax, ray->tmin + (step + vstep.offset) * vstep.size);
2101 const float shade_t = mix(vstep.t.min, vstep.t.max, vstep.shade_offset);
2102 *shade_P = ray->P + ray->D * shade_t;
2103
2104 return step < vstep.max_steps;
2105}
2106
2107ccl_device void volume_shadow_ray_marching(KernelGlobals kg,
2110 ccl_private ShaderData *ccl_restrict sd,
2111 ccl_private Spectrum *ccl_restrict throughput,
2112 const float object_step_size)
2113{
2114 /* Load random number state. */
2115 RNGState rng_state;
2116 shadow_path_state_rng_load(state, &rng_state);
2117
2118 /* For stochastic texture sampling. */
2119 sd->lcg_state = lcg_state_init(
2120 rng_state.rng_pixel, rng_state.rng_offset, rng_state.sample, 0xd9111870);
2121
2122 Spectrum tp = *throughput;
2123
2124 /* Prepare for stepping. */
2125 VolumeStep vstep;
2126 volume_step_init<true>(kg, &rng_state, object_step_size, ray->tmin, ray->tmax, &vstep);
2127
2128 /* compute extinction at the start */
2129 Spectrum sum = zero_spectrum();
2130 for (int step = 0; volume_ray_marching_advance(step, ray, &sd->P, vstep); step++) {
2131 /* compute attenuation over segment */
2132 const Spectrum sigma_t = volume_shader_eval_extinction<true>(
2134 /* Compute `expf()` only for every Nth step, to save some calculations
2135 * because `exp(a)*exp(b) = exp(a+b)`, also do a quick #VOLUME_THROUGHPUT_EPSILON
2136 * check then. */
2137 sum += (-sigma_t * vstep.t.length());
2138 if ((step & 0x07) == 0) { /* TODO: Other interval? */
2139 tp = *throughput * exp(sum);
2140
2141 /* stop if nearly all light is blocked */
2143 break;
2144 }
2145 }
2146 }
2147
2148 if (vstep.t.max == ray->tmax) {
2149 /* Update throughput in case we haven't done it above. */
2150 tp = *throughput * exp(sum);
2151 }
2152
2153 *throughput = tp;
2154}
2155
2156struct VolumeRayMarchingState {
2157 /* Random numbers for scattering. */
2158 float rscatter;
2159 float rchannel;
2160
2161 /* Multiple importance sampling. */
2162 VolumeSampleMethod direct_sample_method;
2163 bool use_mis;
2164 float distance_pdf;
2165 float equiangular_pdf;
2166};
2167
2168ccl_device_inline void volume_ray_marching_state_init(
2169 KernelGlobals kg,
2170 const ccl_private RNGState *rng_state,
2171 const VolumeSampleMethod direct_sample_method,
2172 ccl_private VolumeRayMarchingState &vstate)
2173{
2174 vstate.rscatter = path_state_rng_1D(kg, rng_state, PRNG_VOLUME_SCATTER_DISTANCE);
2175 vstate.rchannel = path_state_rng_1D(kg, rng_state, PRNG_VOLUME_COLOR_CHANNEL);
2176
2177 /* Multiple importance sampling: pick between equiangular and distance sampling strategy. */
2178 vstate.direct_sample_method = direct_sample_method;
2179 vstate.use_mis = (direct_sample_method == VOLUME_SAMPLE_MIS);
2180 if (vstate.use_mis) {
2181 if (vstate.rscatter < 0.5f) {
2182 vstate.rscatter *= 2.0f;
2183 vstate.direct_sample_method = VOLUME_SAMPLE_DISTANCE;
2184 }
2185 else {
2186 vstate.rscatter = (vstate.rscatter - 0.5f) * 2.0f;
2187 vstate.direct_sample_method = VOLUME_SAMPLE_EQUIANGULAR;
2188 }
2189 }
2190 vstate.equiangular_pdf = 0.0f;
2191 vstate.distance_pdf = 1.0f;
2192}
2193
2194/* Returns true if we found the indirect scatter position within the current active ray segment. */
2195ccl_device bool volume_sample_indirect_scatter_ray_marching(
2196 const Spectrum transmittance,
2197 const Spectrum channel_pdf,
2198 const int channel,
2199 const ccl_private ShaderData *ccl_restrict sd,
2200 const ccl_private VolumeShaderCoefficients &ccl_restrict coeff,
2202 ccl_private VolumeRayMarchingState &ccl_restrict vstate,
2203 ccl_private VolumeIntegrateResult &ccl_restrict result)
2204{
2205 if (result.indirect_scatter) {
2206 /* Already sampled indirect scatter position. */
2207 return false;
2208 }
2209
2210 /* If sampled distance does not go beyond the current segment, we have found the scatter
2211 * position. Otherwise continue searching and accumulate the transmittance along the ray. */
2212 const float sample_transmittance = volume_channel_get(transmittance, channel);
2213 if (1.0f - vstate.rscatter >= sample_transmittance) {
2214 /* Pick `sigma_t` from a random channel. */
2215 const float sample_sigma_t = volume_channel_get(coeff.sigma_t, channel);
2216
2217 /* Generate the next distance using random walk, following exponential distribution
2218 * p(dt) = sigma_t * exp(-sigma_t * dt). */
2219 const float new_dt = -logf(1.0f - vstate.rscatter) / sample_sigma_t;
2220 const float new_t = t.min + new_dt;
2221
2222 const Spectrum new_transmittance = volume_color_transmittance(coeff.sigma_t, new_dt);
2223 /* PDF for density-based distance sampling is handled implicitly via
2224 * transmittance / pdf = exp(-sigma_t * dt) / (sigma_t * exp(-sigma_t * dt)) = 1 / sigma_t. */
2225 const float distance_pdf = dot(channel_pdf, coeff.sigma_t * new_transmittance);
2226
2227 if (vstate.distance_pdf * distance_pdf > VOLUME_SAMPLE_PDF_CUTOFF) {
2228 /* Update throughput. */
2229 result.indirect_scatter = true;
2230 result.indirect_t = new_t;
2231 result.indirect_throughput *= coeff.sigma_s * new_transmittance / distance_pdf;
2232 if (vstate.direct_sample_method == VOLUME_SAMPLE_DISTANCE) {
2233 vstate.distance_pdf *= distance_pdf;
2234 }
2235
2236 volume_shader_copy_phases(&result.indirect_phases, sd);
2237
2238 return true;
2239 }
2240 }
2241 else {
2242 /* Update throughput. */
2243 const float distance_pdf = dot(channel_pdf, transmittance);
2244 result.indirect_throughput *= transmittance / distance_pdf;
2245 if (vstate.direct_sample_method == VOLUME_SAMPLE_DISTANCE) {
2246 vstate.distance_pdf *= distance_pdf;
2247 }
2248
2249 /* Remap rscatter so we can reuse it and keep thing stratified. */
2250 vstate.rscatter = 1.0f - (1.0f - vstate.rscatter) / sample_transmittance;
2251 }
2252
2253 return false;
2254}
2255
2256/* Find direct and indirect scatter positions. */
2257ccl_device_forceinline void volume_ray_marching_step_scattering(
2258 const ccl_private ShaderData *sd,
2259 const ccl_private Ray *ray,
2260 const ccl_private EquiangularCoefficients &equiangular_coeffs,
2261 const ccl_private VolumeShaderCoefficients &ccl_restrict coeff,
2262 const Spectrum transmittance,
2264 ccl_private VolumeRayMarchingState &ccl_restrict vstate,
2265 ccl_private VolumeIntegrateResult &ccl_restrict result)
2266{
2267 /* Pick random color channel for sampling the scatter distance. We use the Veach one-sample model
2268 * with balance heuristic for the channels.
2269 * Set `albedo` to 1 for the channel where extinction coefficient `sigma_t` is zero, to make sure
2270 * that we sample a distance outside the current segment when that channel is picked, meaning
2271 * light passes through without attenuation. */
2272 const Spectrum albedo = safe_divide_color(coeff.sigma_s, coeff.sigma_t, 1.0f);
2273 Spectrum channel_pdf;
2274 const int channel = volume_sample_channel(
2275 albedo, result.indirect_throughput, &vstate.rchannel, &channel_pdf);
2276
2277 /* Equiangular sampling for direct lighting. */
2278 if (vstate.direct_sample_method == VOLUME_SAMPLE_EQUIANGULAR && !result.direct_scatter) {
2279 if (t.contains(result.direct_t) && vstate.equiangular_pdf > VOLUME_SAMPLE_PDF_CUTOFF) {
2280 const float new_dt = result.direct_t - t.min;
2281 const Spectrum new_transmittance = volume_color_transmittance(coeff.sigma_t, new_dt);
2282
2283 result.direct_scatter = true;
2284 result.direct_throughput *= safe_divide(coeff.sigma_s * new_transmittance,
2285 vstate.equiangular_pdf);
2286 volume_shader_copy_phases(&result.direct_phases, sd);
2287
2288 /* Multiple importance sampling. */
2289 if (vstate.use_mis) {
2290 const float distance_pdf = vstate.distance_pdf *
2291 dot(channel_pdf, coeff.sigma_t * new_transmittance);
2292 const float mis_weight = 2.0f * power_heuristic(vstate.equiangular_pdf, distance_pdf);
2293 result.direct_throughput *= mis_weight;
2294 }
2295 }
2296 else {
2297 result.direct_throughput *= transmittance;
2298 vstate.distance_pdf *= dot(channel_pdf, transmittance);
2299 }
2300 }
2301
2302 /* Distance sampling for indirect and optional direct lighting. */
2303 if (volume_sample_indirect_scatter_ray_marching(
2304 transmittance, channel_pdf, channel, sd, coeff, t, vstate, result))
2305 {
2306 if (vstate.direct_sample_method == VOLUME_SAMPLE_DISTANCE) {
2307 /* If using distance sampling for direct light, just copy parameters of indirect light
2308 * since we scatter at the same point. */
2309 result.direct_scatter = true;
2310 result.direct_t = result.indirect_t;
2311 result.direct_throughput = result.indirect_throughput;
2312 volume_shader_copy_phases(&result.direct_phases, sd);
2313
2314 /* Multiple importance sampling. */
2315 if (vstate.use_mis) {
2316 const float equiangular_pdf = volume_equiangular_pdf(
2317 ray, equiangular_coeffs, result.indirect_t);
2318 const float mis_weight = power_heuristic(vstate.distance_pdf, equiangular_pdf);
2319 result.direct_throughput *= 2.0f * mis_weight;
2320 }
2321 }
2322 }
2323}
2324
2325/* heterogeneous volume distance sampling: integrate stepping through the
2326 * volume until we reach the end, get absorbed entirely, or run out of
2327 * iterations. this does probabilistically scatter or get transmitted through
2328 * for path tracing where we don't want to branch. */
2329ccl_device_forceinline void volume_integrate_ray_marching(
2330 KernelGlobals kg,
2331 const IntegratorState state,
2332 const ccl_private Ray *ccl_restrict ray,
2333 ccl_private ShaderData *ccl_restrict sd,
2334 const ccl_private RNGState *ccl_restrict rng_state,
2336 const float object_step_size,
2338 ccl_private VolumeIntegrateResult &result)
2339{
2341
2342 EquiangularCoefficients equiangular_coeffs = {zero_float3(), {ray->tmin, ray->tmax}};
2343 const VolumeSampleMethod direct_sample_method = volume_direct_sample_method(
2344 kg, state, ray, sd, rng_state, &equiangular_coeffs, ls);
2345
2346 /* Prepare for stepping. */
2347 VolumeStep vstep;
2348 volume_step_init<false>(kg, rng_state, object_step_size, ray->tmin, ray->tmax, &vstep);
2349
2350 /* Initialize volume integration state. */
2351 VolumeRayMarchingState vstate ccl_optional_struct_init;
2352 volume_ray_marching_state_init(kg, rng_state, direct_sample_method, vstate);
2353
2354 /* Initialize volume integration result. */
2355 const Spectrum throughput = INTEGRATOR_STATE(state, path, throughput);
2356 result.direct_throughput = (vstate.direct_sample_method == VOLUME_SAMPLE_NONE) ?
2357 zero_spectrum() :
2358 throughput;
2359 result.indirect_throughput = throughput;
2360
2361 /* Equiangular sampling: compute distance and PDF in advance. */
2362 if (vstate.direct_sample_method == VOLUME_SAMPLE_EQUIANGULAR) {
2363 result.direct_t = volume_equiangular_sample(
2364 ray, equiangular_coeffs, vstate.rscatter, &vstate.equiangular_pdf);
2365 }
2366# if defined(__PATH_GUIDING__)
2367 result.direct_sample_method = vstate.direct_sample_method;
2368# endif
2369
2370# ifdef __DENOISING_FEATURES__
2371 const bool write_denoising_features = (INTEGRATOR_STATE(state, path, flag) &
2373 Spectrum accum_albedo = zero_spectrum();
2374# endif
2375 Spectrum accum_emission = zero_spectrum();
2376
2377 for (int step = 0; volume_ray_marching_advance(step, ray, &sd->P, vstep); step++) {
2378 /* compute segment */
2379 VolumeShaderCoefficients coeff ccl_optional_struct_init;
2380 if (volume_shader_sample(kg, state, sd, &coeff)) {
2381 const int runtime_flag = sd->runtime_flag;
2382
2383 /* Evaluate transmittance over segment. */
2384 const float dt = vstep.t.length();
2385 const Spectrum transmittance = (runtime_flag & SR_EXTINCTION) ?
2386 volume_color_transmittance(coeff.sigma_t, dt) :
2387 one_spectrum();
2388
2389 /* Emission. */
2390 if (runtime_flag & SR_EMISSION) {
2391 /* Only write emission before indirect light scatter position, since we terminate
2392 * stepping at that point if we have already found a direct light scatter position. */
2393 if (!result.indirect_scatter) {
2394 const Spectrum emission = volume_emission_integrate(&coeff, runtime_flag, dt);
2395 accum_emission += result.indirect_throughput * emission;
2397 }
2398 }
2399
2400 if (runtime_flag & SR_SCATTER) {
2401# ifdef __DENOISING_FEATURES__
2402 /* Accumulate albedo for denoising features. */
2403 if (write_denoising_features && (runtime_flag & SR_SCATTER)) {
2404 const Spectrum albedo = safe_divide_color(coeff.sigma_s, coeff.sigma_t);
2405 accum_albedo += result.indirect_throughput * albedo * (one_spectrum() - transmittance);
2406 }
2407# endif
2408
2409 /* Scattering and absorption. */
2410 volume_ray_marching_step_scattering(
2411 sd, ray, equiangular_coeffs, coeff, transmittance, vstep.t, vstate, result);
2412 }
2413 else if (runtime_flag & SR_EXTINCTION) {
2414 /* Absorption only. */
2415 result.indirect_throughput *= transmittance;
2416 result.direct_throughput *= transmittance;
2417 }
2418
2419 if (volume_integrate_should_stop(result)) {
2420 break;
2421 }
2422 }
2423 }
2424
2425 /* Write accumulated emission. */
2426 if (!is_zero(accum_emission)) {
2427 if (light_link_object_match(kg, light_link_receiver_forward(kg, state), sd->object)) {
2429 kg, state, accum_emission, render_buffer, object_lightgroup(kg, sd->object));
2430 }
2431 }
2432
2433# ifdef __DENOISING_FEATURES__
2434 /* Write denoising features. */
2435 if (write_denoising_features) {
2436 film_write_denoising_features_volume(
2437 kg, state, accum_albedo, result.indirect_scatter, render_buffer);
2438 }
2439# endif /* __DENOISING_FEATURES__ */
2440}
2441
2443
2444/* Path tracing: sample point on light and evaluate light shader, then
2445 * queue shadow ray to be traced. */
2446ccl_device_forceinline void integrate_volume_direct_light(
2447 KernelGlobals kg,
2449 const ccl_private ShaderData *ccl_restrict sd,
2450 const ccl_private RNGState *ccl_restrict rng_state,
2451 const float3 P,
2453# if defined(__PATH_GUIDING__)
2455# endif
2456 const ccl_private Spectrum throughput,
2458{
2460
2461 if (!kernel_data.integrator.use_direct_light || ls.emitter_id == EMITTER_NONE) {
2462 return;
2463 }
2464
2465 /* Sample position on the same light again, now from the shading point where we scattered. */
2466 {
2467 const uint32_t path_flag = INTEGRATOR_STATE(state, path, flag);
2468 const uint bounce = INTEGRATOR_STATE(state, path, bounce);
2469 const float3 rand_light = path_state_rng_3D(kg, rng_state, PRNG_LIGHT);
2470 const float3 N = zero_float3();
2471 const int object_receiver = light_link_receiver_nee(kg, sd);
2472 const int runtime_flags = SR_BSDF_HAS_TRANSMISSION;
2473
2474 if (!light_sample<false>(kg,
2475 rand_light,
2476 sd->time,
2477 P,
2478 N,
2479 object_receiver,
2480 runtime_flags,
2481 bounce,
2482 path_flag,
2483 &ls))
2484 {
2485 return;
2486 }
2487 }
2488
2489 if (ls.shader & SHADER_EXCLUDE_SCATTER) {
2490 return;
2491 }
2492
2493 /* Evaluate constant part of light shader, rest will optionally be done in another kernel. */
2494 Spectrum light_shader_eval ccl_optional_struct_init;
2495 const bool is_constant_light_shader = light_sample_shader_eval_nee_constant(
2496 kg, ls.shader, ls.prim, ls.type != LIGHT_TRIANGLE, light_shader_eval);
2497
2498 /* Evaluate BSDF. */
2500 const float phase_pdf = volume_shader_phase_eval(
2501 kg, state, sd, phases, ls.D, &phase_eval, ls.shader);
2502 const float mis_weight = light_sample_mis_weight_nee(kg, ls.pdf, phase_pdf);
2503 bsdf_eval_mul(&phase_eval, light_shader_eval * ls.eval_fac / ls.pdf * mis_weight);
2504
2505 /* Path termination for constant light shader. */
2506 if (is_constant_light_shader && !(kernel_data.kernel_features & KERNEL_FEATURE_LIGHT_TREE)) {
2507 const float terminate = path_state_rng_light_termination(kg, rng_state);
2508 if (light_sample_terminate(kg, &phase_eval, terminate)) {
2509 return;
2510 }
2511 }
2512 /* For non-constant light shader, probabilistic termination happens in
2513 * SHADE_LIGHT_NEE when the full contribution is known. */
2514 else if (bsdf_eval_is_zero(&phase_eval)) {
2515 return;
2516 }
2517
2518 /* Create shadow ray. */
2520 light_sample_to_volume_shadow_ray(sd, &ls, P, &ray);
2521
2522 /* Branch off shadow kernel. */
2524 kg,
2525 state,
2526 (is_constant_light_shader) ? DEVICE_KERNEL_INTEGRATOR_INTERSECT_SHADOW :
2528 false);
2529
2530 /* Write shadow ray and associated state to global memory. */
2531 integrator_state_write_shadow_ray(shadow_state, &ray);
2532 integrator_state_write_shadow_ray_self(shadow_state, &ray);
2533
2534 /* Copy state from main path to shadow path. */
2535 const uint16_t bounce = INTEGRATOR_STATE(state, path, bounce);
2536 const uint16_t transparent_bounce = INTEGRATOR_STATE(state, path, transparent_bounce);
2537 PathRayVisibility path_visibility = INTEGRATOR_STATE(state, path, visibility);
2538 uint32_t shadow_flag = INTEGRATOR_STATE(state, path, flag);
2539 const Spectrum phase_sum = bsdf_eval_sum(&phase_eval);
2540 const Spectrum throughput_phase = throughput * phase_sum;
2541
2542 if (!(kernel_data.kernel_features & KERNEL_FEATURE_LIGHT_TREE)) {
2543 INTEGRATOR_STATE_WRITE(shadow_state, shadow_path, bsdf_eval_average) = average(phase_sum);
2544 }
2545
2546 if (kernel_data.kernel_features & KERNEL_FEATURE_LIGHT_PASSES) {
2548 PackedSpectrum pass_glossy_weight;
2549
2550 if (shadow_flag & PATH_RAY_ANY_PASS) {
2551 /* Indirect bounce, use weights from earlier surface or volume bounce. */
2553 pass_glossy_weight = INTEGRATOR_STATE(state, path, pass_glossy_weight);
2554 }
2555 else {
2556 /* Direct light, no diffuse/glossy distinction needed for volumes. */
2557 shadow_flag |= PATH_RAY_VOLUME_PASS;
2559 pass_glossy_weight = zero_spectrum();
2560 }
2561
2563 INTEGRATOR_STATE_WRITE(shadow_state, shadow_path, pass_glossy_weight) = pass_glossy_weight;
2564 }
2565
2566 if (bounce == 0) {
2567 path_visibility |= PATH_RAY_VISIBILITY_VOLUME_SCATTER;
2568 shadow_flag &= ~PATH_RAY_VOLUME_PRIMARY_TRANSMIT;
2569 }
2570
2571 INTEGRATOR_STATE_WRITE(shadow_state, shadow_path, render_pixel_index) = INTEGRATOR_STATE(
2572 state, path, render_pixel_index);
2573 INTEGRATOR_STATE_WRITE(shadow_state, shadow_path, rng_offset) = INTEGRATOR_STATE(
2574 state, path, rng_offset);
2575 INTEGRATOR_STATE_WRITE(shadow_state, shadow_path, rng_pixel) = INTEGRATOR_STATE(
2576 state, path, rng_pixel);
2577 INTEGRATOR_STATE_WRITE(shadow_state, shadow_path, sample) = INTEGRATOR_STATE(
2578 state, path, sample);
2579 INTEGRATOR_STATE_WRITE(shadow_state, shadow_path, visibility) = path_visibility;
2580 INTEGRATOR_STATE_WRITE(shadow_state, shadow_path, flag) = shadow_flag;
2581 INTEGRATOR_STATE_WRITE(shadow_state, shadow_path, bounce) = bounce;
2582 INTEGRATOR_STATE_WRITE(shadow_state, shadow_path, transparent_bounce) = transparent_bounce;
2583 INTEGRATOR_STATE_WRITE(shadow_state, shadow_path, diffuse_bounce) = INTEGRATOR_STATE(
2584 state, path, diffuse_bounce);
2585 INTEGRATOR_STATE_WRITE(shadow_state, shadow_path, glossy_bounce) = INTEGRATOR_STATE(
2586 state, path, glossy_bounce);
2587 INTEGRATOR_STATE_WRITE(shadow_state, shadow_path, transmission_bounce) = INTEGRATOR_STATE(
2588 state, path, transmission_bounce);
2589 INTEGRATOR_STATE_WRITE(shadow_state, shadow_path, volume_bounds_bounce) = INTEGRATOR_STATE(
2590 state, path, volume_bounds_bounce);
2591 INTEGRATOR_STATE_WRITE(shadow_state, shadow_path, throughput) = throughput_phase;
2592
2593 /* Write Light-group, +1 as light-group is int but we need to encode into a uint8_t. */
2594 INTEGRATOR_STATE_WRITE(shadow_state, shadow_path, lightgroup) = ls.group + 1;
2595
2596# if defined(__PATH_GUIDING__)
2597 if ((kernel_data.kernel_features & KERNEL_FEATURE_PATH_GUIDING)) {
2598 INTEGRATOR_STATE_WRITE(shadow_state, shadow_path, unlit_throughput) = unlit_throughput;
2599 INTEGRATOR_STATE_WRITE(shadow_state, shadow_path, path_segment) = INTEGRATOR_STATE(
2600 state, guiding, path_segment);
2601 INTEGRATOR_STATE(shadow_state, shadow_path, guiding_light_linking_mis_weight) = 0.0f;
2602 }
2603# endif
2604
2605 integrator_state_copy_volume_stack_to_shadow(kg, shadow_state, state);
2606}
2607
2608/* Path tracing: scatter in new direction using phase function */
2609ccl_device_forceinline bool integrate_volume_phase_scatter(
2610 KernelGlobals kg,
2612 ccl_private ShaderData *sd,
2613 const ccl_private Ray *ray,
2614 const ccl_private RNGState *rng_state,
2615 const ccl_private ShaderVolumePhases *phases)
2616{
2618
2619 float2 rand_phase = path_state_rng_2D(kg, rng_state, PRNG_VOLUME_PHASE);
2620
2621 const ccl_private ShaderVolumeClosure *svc = volume_shader_phase_pick(phases, &rand_phase);
2622
2623 /* Phase closure, sample direction. */
2624 float phase_pdf = 0.0f;
2625 float unguided_phase_pdf = 0.0f;
2628 float sampled_roughness = 1.0f;
2629 int label;
2630
2631# if defined(__PATH_GUIDING__) && PATH_GUIDING_LEVEL >= 4
2632 if (kernel_data.integrator.use_guiding &&
2633 (kernel_data.kernel_features & KERNEL_FEATURE_PATH_GUIDING))
2634 {
2635 label = volume_shader_phase_guided_sample(kg,
2636 state,
2637 sd,
2638 phases,
2639 svc,
2640 rand_phase,
2641 &phase_eval,
2642 &phase_wo,
2643 &phase_pdf,
2644 &unguided_phase_pdf,
2645 &sampled_roughness);
2646
2647 if (phase_pdf == 0.0f || bsdf_eval_is_zero(&phase_eval)) {
2648 return false;
2649 }
2650
2651 INTEGRATOR_STATE_WRITE(state, path, unguided_throughput) *= phase_pdf / unguided_phase_pdf;
2652 }
2653 else
2654# endif
2655 {
2656 label = volume_shader_phase_sample(
2657 sd, phases, svc, rand_phase, &phase_eval, &phase_wo, &phase_pdf, &sampled_roughness);
2658
2659 if (phase_pdf == 0.0f || bsdf_eval_is_zero(&phase_eval)) {
2660 return false;
2661 }
2662
2663 unguided_phase_pdf = phase_pdf;
2664 }
2665
2666 /* Setup ray. */
2667 INTEGRATOR_STATE_WRITE(state, ray, P) = sd->P;
2668 INTEGRATOR_STATE_WRITE(state, ray, D) = normalize(phase_wo);
2669 INTEGRATOR_STATE_WRITE(state, ray, tmin) = 0.0f;
2670# ifdef __LIGHT_TREE__
2671 if (kernel_data.integrator.use_light_tree) {
2672 INTEGRATOR_STATE_WRITE(state, ray, previous_dt) = ray->tmax - ray->tmin;
2673 }
2674# endif
2675 INTEGRATOR_STATE_WRITE(state, ray, tmax) = FLT_MAX;
2676# ifdef __RAY_DIFFERENTIALS__
2679 kg, INTEGRATOR_STATE(state, ray, dD), sampled_roughness);
2680# endif
2681 // Save memory by storing last hit prim and object in isect
2682 INTEGRATOR_STATE_WRITE(state, isect, prim) = sd->prim;
2683 INTEGRATOR_STATE_WRITE(state, isect, object) = sd->object;
2684
2685 const Spectrum phase_weight = bsdf_eval_sum(&phase_eval) / phase_pdf;
2686
2687 /* Add phase function sampling data to the path segment. */
2689 kg, state, phase_weight, phase_pdf, normalize(phase_wo), sampled_roughness);
2690
2691 /* Update throughput. */
2692 const Spectrum throughput = INTEGRATOR_STATE(state, path, throughput);
2693 const Spectrum throughput_phase = throughput * phase_weight;
2694 INTEGRATOR_STATE_WRITE(state, path, throughput) = throughput_phase;
2695
2696 if (kernel_data.kernel_features & KERNEL_FEATURE_LIGHT_PASSES) {
2697 if (INTEGRATOR_STATE(state, path, bounce) == 0) {
2699 INTEGRATOR_STATE_WRITE(state, path, pass_glossy_weight) = zero_spectrum();
2700 }
2701 }
2702
2703 /* Update path state */
2704 INTEGRATOR_STATE_WRITE(state, path, mis_ray_pdf) = phase_pdf;
2705 const float3 previous_P = ray->P + ray->D * ray->tmin;
2706 INTEGRATOR_STATE_WRITE(state, path, mis_origin_n) = sd->P - previous_P;
2707 INTEGRATOR_STATE_WRITE(state, path, min_ray_pdf) = fminf(
2708 unguided_phase_pdf, INTEGRATOR_STATE(state, path, min_ray_pdf));
2709
2710# ifdef __LIGHT_LINKING__
2711 if (kernel_data.kernel_features & KERNEL_FEATURE_LIGHT_LINKING) {
2712 INTEGRATOR_STATE_WRITE(state, path, mis_ray_object) = sd->object;
2713 }
2714# endif
2715
2716 path_state_next(kg, state, label, sd->runtime_flag);
2717 return true;
2718}
2719
2721volume_integrate_event(KernelGlobals kg,
2723 const ccl_private Ray *ccl_restrict ray,
2724 ccl_private ShaderData *sd,
2725 const ccl_private RNGState *rng_state,
2727 ccl_private VolumeIntegrateResult &result)
2728{
2729# if defined(__PATH_GUIDING__) && PATH_GUIDING_LEVEL >= 1
2730 /* The current path throughput which is used later to calculate per-segment throughput. */
2731 const float3 initial_throughput = INTEGRATOR_STATE(state, path, throughput);
2732 /* The path throughput used to calculate the throughput for direct light. */
2733 float3 unlit_throughput = initial_throughput;
2734 /* If a new path segment is generated at the direct scatter position. */
2735 bool guiding_generated_new_segment = false;
2736 float rand_phase_guiding = 0.5f;
2737# endif
2738
2739 /* Perform path termination. The intersect_closest will have already marked this path
2740 * to be terminated. That will shading evaluating to leave out any scattering closures,
2741 * but emission and absorption are still handled for multiple importance sampling. */
2742 const uint32_t path_flag = INTEGRATOR_STATE(state, path, flag);
2743 const float continuation_probability = (path_flag & PATH_RAY_TERMINATE_IN_NEXT_VOLUME) ?
2744 0.0f :
2746 state, path, continuation_probability));
2747 if (continuation_probability == 0.0f) {
2748 return VOLUME_PATH_MISSED;
2749 }
2750
2751 /* Direct light. */
2752 if (result.direct_scatter) {
2753 const float3 direct_P = ray->P + result.direct_t * ray->D;
2754
2755# if defined(__PATH_GUIDING__)
2756 if (kernel_data.integrator.use_guiding) {
2757# if PATH_GUIDING_LEVEL >= 1
2758 if (result.indirect_scatter && result.direct_t == result.indirect_t) {
2759 /* If the direct and indirect distances are the same the path will continue at the
2760 * position of the direct event and we generate a new path segment at this position. */
2761 const float3 transmittance_weight = spectrum_to_rgb(
2762 safe_divide_color(result.indirect_throughput, initial_throughput));
2763 guiding_record_volume_transmission(kg, state, transmittance_weight);
2764 guiding_record_volume_segment(kg, state, direct_P, sd->wi);
2765 guiding_generated_new_segment = true;
2766 unlit_throughput = result.indirect_throughput / continuation_probability;
2767 rand_phase_guiding = path_state_rng_1D(kg, rng_state, PRNG_VOLUME_PHASE_GUIDING_DISTANCE);
2768 }
2769 else {
2770 /* If the direct scattering event is not at the same distance as the indirect one, the
2771 * direct light event will contribute to the out-scattered contribution of the current path
2772 * segment and not to the incoming radiance from the segment direction. We therefore need
2773 * to adjust the unlit_throughput to include the scattering at the previous path vertex.
2774 */
2775 float3 scatterEval = one_float3();
2776 if (INTEGRATOR_STATE(state, guiding, path_segment)) {
2777 const pgl_vec3f scatteringWeight =
2778 INTEGRATOR_STATE(state, guiding, path_segment)->scatteringWeight;
2779 scatterEval = make_float3(scatteringWeight.x, scatteringWeight.y, scatteringWeight.z);
2780 unlit_throughput = safe_divide(unlit_throughput, scatterEval);
2781 }
2782 unlit_throughput *= continuation_probability;
2783 rand_phase_guiding = path_state_rng_1D(
2784 kg, rng_state, PRNG_VOLUME_PHASE_GUIDING_EQUIANGULAR);
2785 }
2786# endif
2787# if PATH_GUIDING_LEVEL >= 4
2788 if ((kernel_data.kernel_features & KERNEL_FEATURE_PATH_GUIDING)) {
2789 /* Initialize the guiding distribution at the direct scattering event position. */
2790 volume_shader_prepare_guiding(
2791 kg, state, rand_phase_guiding, direct_P, ray->D, &result.direct_phases);
2792 }
2793# endif
2794 }
2795# endif
2796
2797 result.direct_throughput /= continuation_probability;
2798 integrate_volume_direct_light(kg,
2799 state,
2800 sd,
2801 rng_state,
2802 direct_P,
2803 &result.direct_phases,
2804# if defined(__PATH_GUIDING__)
2806# endif
2807 result.direct_throughput,
2808 ls);
2809 }
2810
2811 /* Indirect light.
2812 *
2813 * Only divide throughput by continuation_probability if we scatter. For the attenuation
2814 * case the next surface will already do this division. */
2815 if (result.indirect_scatter) {
2816# if defined(__PATH_GUIDING__) && PATH_GUIDING_LEVEL >= 1
2817 if (!guiding_generated_new_segment) {
2818 const float3 transmittance_weight = spectrum_to_rgb(
2819 safe_divide_color(result.indirect_throughput, initial_throughput));
2820 guiding_record_volume_transmission(kg, state, transmittance_weight);
2821 }
2822# endif
2823 result.indirect_throughput /= continuation_probability;
2824 }
2825 INTEGRATOR_STATE_WRITE(state, path, throughput) = result.indirect_throughput;
2826
2827 if (result.indirect_scatter) {
2828 sd->P = ray->P + result.indirect_t * ray->D;
2829
2830# if defined(__PATH_GUIDING__)
2831 if ((kernel_data.kernel_features & KERNEL_FEATURE_PATH_GUIDING)) {
2832# if PATH_GUIDING_LEVEL >= 1
2833 if (!guiding_generated_new_segment) {
2834 guiding_record_volume_segment(kg, state, sd->P, sd->wi);
2835 }
2836# endif
2837# if PATH_GUIDING_LEVEL >= 4
2838 /* If the direct and indirect event do not have the same distance the guiding
2839 * distribution needs to be initialized at the indirect scattering position. */
2840 if (!result.direct_scatter || result.direct_t != result.indirect_t) {
2841 rand_phase_guiding = path_state_rng_1D(kg, rng_state, PRNG_VOLUME_PHASE_GUIDING_DISTANCE);
2842 volume_shader_prepare_guiding(
2843 kg, state, rand_phase_guiding, sd->P, ray->D, &result.indirect_phases);
2844 }
2845# endif
2846 }
2847# endif
2848
2849 if (integrate_volume_phase_scatter(kg, state, sd, ray, rng_state, &result.indirect_phases)) {
2850 return VOLUME_PATH_SCATTERED;
2851 }
2852 return VOLUME_PATH_MISSED;
2853 }
2854# if defined(__PATH_GUIDING__)
2855 /* No guiding if we don't scatter. */
2856 if ((kernel_data.kernel_features & KERNEL_FEATURE_PATH_GUIDING)) {
2857 INTEGRATOR_STATE_WRITE(state, guiding, use_volume_guiding) = false;
2858 }
2859# endif
2861}
2862
2863/* get the volume attenuation and emission over line segment defined by
2864 * ray, with the assumption that there are no surfaces blocking light
2865 * between the endpoints. distance sampling is used to decide if we will
2866 * scatter or not. */
2871{
2872 kernel_assert(!kernel_data.integrator.volume_ray_marching);
2873
2874 if (integrator_state_volume_stack_is_empty(kg, state)) {
2876 }
2877
2878 ShaderData sd;
2879 /* FIXME: `object` is used for light linking. We read the bottom of the stack for simplicity, but
2880 * this does not work for overlapping volumes. */
2881 shader_setup_from_volume(&sd, ray, INTEGRATOR_STATE_ARRAY(state, volume_stack, 0, object));
2882
2883 /* Load random number state. */
2884 RNGState rng_state;
2885 path_state_rng_load(state, &rng_state);
2886
2887 /* For stochastic texture sampling. */
2888 sd.lcg_state = lcg_state_init(
2889 rng_state.rng_pixel, rng_state.rng_offset, rng_state.sample, 0x15b4f88d);
2890
2892
2893 /* TODO: expensive to zero closures? */
2894 VolumeIntegrateResult result = {};
2895 volume_integrate_null_scattering(kg, state, ray, &sd, &rng_state, render_buffer, &ls, result);
2896
2897 if (sd.runtime_flag & SR_CACHE_MISS) {
2899 }
2900
2901 return volume_integrate_event(kg, state, ray, &sd, &rng_state, ls, result);
2902}
2903
2905volume_integrate_ray_marching(KernelGlobals kg,
2909{
2910 kernel_assert(kernel_data.integrator.volume_ray_marching);
2911
2912 if (integrator_state_volume_stack_is_empty(kg, state)) {
2914 }
2915
2916 ShaderData sd;
2917 /* FIXME: `object` is used for light linking. We read the bottom of the stack for simplicity, but
2918 * this does not work for overlapping volumes. */
2919 shader_setup_from_volume(&sd, ray, INTEGRATOR_STATE_ARRAY(state, volume_stack, 0, object));
2920
2921 /* Load random number state. */
2922 RNGState rng_state;
2923 path_state_rng_load(state, &rng_state);
2924
2925 /* For stochastic texture sampling. */
2926 sd.lcg_state = lcg_state_init(
2927 rng_state.rng_pixel, rng_state.rng_offset, rng_state.sample, 0x15b4f88d);
2928
2930
2931 /* TODO: expensive to zero closures? */
2932 VolumeIntegrateResult result = {};
2933
2934 const float step_size = volume_stack_step_size<false>(kg, state);
2935 volume_integrate_ray_marching(
2936 kg, state, ray, &sd, &rng_state, render_buffer, step_size, &ls, result);
2937
2938 if (sd.runtime_flag & SR_CACHE_MISS) {
2940 }
2941
2942 return volume_integrate_event(kg, state, ray, &sd, &rng_state, ls, result);
2943}
2944
2945#endif
2946
2947#ifdef __VOLUME__
2948ccl_device_inline void integrator_shade_volume_setup(KernelGlobals kg,
2952{
2954
2955 /* Setup shader data. */
2958
2959 /* Set ray length to current segment. */
2960 ray->tmax = (isect->prim != PRIM_NONE) ? isect->t : FLT_MAX;
2961
2962 /* Clean volume stack for background rays. */
2963 if (isect->prim == PRIM_NONE) {
2964 volume_stack_clean(kg, state);
2965 }
2966
2967 /* Assign flag to transmitted volume rays for scattering probability guiding. */
2968 if (INTEGRATOR_STATE(state, path, bounce) == 0) {
2970 }
2971}
2972#endif
2973
2974template<DeviceKernel volume_kernel>
2976 KernelGlobals kg,
2977 const IntegratorState state,
2980 const VolumeIntegrateEvent event)
2981{
2982 if (event == VOLUME_PATH_MISSED) {
2983 /* End path. */
2984 integrator_path_terminate(kg, state, render_buffer, volume_kernel);
2985 return;
2986 }
2987
2988 if (event == VOLUME_PATH_ATTENUATED) {
2989 /* Continue to background, light or surface. */
2991 return;
2992 }
2993
2994#ifdef __SHADOW_LINKING__
2995 if (shadow_linking_schedule_intersection_kernel<volume_kernel>(kg, state)) {
2996 return;
2997 }
2998#endif /* __SHADOW_LINKING__ */
2999
3001
3002 /* Queue intersect_closest kernel. */
3004}
3005
3009{
3010#ifdef __VOLUME__
3013 integrator_shade_volume_setup(kg, state, &ray, &isect);
3014
3015 const VolumeIntegrateEvent event = volume_integrate(kg, state, &ray, render_buffer);
3016 if (event == VOLUME_PATH_CACHE_MISS) {
3018 return;
3019 }
3021 kg, state, render_buffer, &isect, event);
3022
3023#endif /* __VOLUME__ */
3024}
3025
3029{
3030#ifdef __VOLUME__
3033 integrator_shade_volume_setup(kg, state, &ray, &isect);
3034
3035 const VolumeIntegrateEvent event = volume_integrate_ray_marching(kg, state, &ray, render_buffer);
3036 if (event == VOLUME_PATH_CACHE_MISS) {
3038 return;
3039 }
3041 kg, state, render_buffer, &isect, event);
3042
3043#endif /* __VOLUME__ */
3044}
#define D
blender::float3 packed_float3
ccl_device_inline bool area_light_valid_ray_segment(const ccl_global KernelAreaLight *light, float3 P, float3 D, ccl_private Interval< float > *t_range)
Definition area.h:472
#define U
#define UNLIKELY(x)
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
ccl_device_inline Spectrum safe_divide_color(Spectrum a, Spectrum b, const float fallback=0.0f)
Definition color.h:376
ccl_device_inline bool light_sample_terminate(KernelGlobals kg, ccl_private BsdfEval *ccl_restrict eval, const float rand_terminate)
ccl_device_inline void light_sample_to_volume_shadow_ray(const ccl_private ShaderData *ccl_restrict sd, const ccl_private LightSample *ccl_restrict ls, const float3 P, ccl_private Ray *ray)
ccl_device_inline float light_sample_mis_weight_nee(KernelGlobals kg, const float nee_pdf, const float forward_pdf)
CCL_NAMESPACE_BEGIN ccl_device bool light_sample_shader_eval_nee_constant(KernelGlobals kg, const int shader_id, const int prim, const bool is_light, ccl_private Spectrum &eval)
ccl_device_inline bool light_sample_from_volume_segment(KernelGlobals kg, const float3 rand, const float time, const float3 P, const float3 D, const float t, const int object_receiver, const int bounce, const uint32_t path_flag, ccl_private LightSample *ls)
#define kernel_assert(cond)
#define KERNEL_FEATURE_LIGHT_TREE
#define CLOSURE_IS_VOLUME(type)
#define kernel_data
#define PASS_UNUSED
#define ccl_restrict
#define ccl_device_forceinline
#define KERNEL_FEATURE_PATH_GUIDING
#define ccl_optional_struct_init
#define kernel_data_fetch(name, index)
#define SHADER_NONE
#define one_spectrum
#define PRIM_NONE
#define zero_spectrum
#define OBJECT_NONE
#define make_spectrum(f)
#define ccl_private
#define ccl_device_inline
#define EMITTER_NONE
#define KERNEL_FEATURE_LIGHT_PASSES
#define __DENOISING_FEATURES__
#define KERNEL_FEATURE_LIGHT_LINKING
#define ccl_global
#define VOLUME_OCTREE_MAX_DEPTH
#define ccl_device_noinline
#define ccl_device_inline_method
#define logf(x)
#define expf(x)
#define CCL_NAMESPACE_END
#define __KERNEL_HIP__
#define saturatef(x)
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
#define roundf(x)
#define __uint_as_float(x)
ccl_device_forceinline float differential_make_compact(const float dD)
IMETHOD Vector diff(const Vector &a, const Vector &b, double dt)
Definition frames.inl:1166
#define FLT_MAX
#define index(i)
#define exp
VecBase< float, D > normalize(VecOp< float, D >) RET
float dot(VecOp< float, D >, VecOp< float, D >) RET
#define select(A, B, C)
bool all(VecOp< bool, D >) RET
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
constexpr T clamp(T, U, U) RET
VecBase< float, 3 > float3
VecBase< uint, 3 > uint3
float3x3 scale(float3x3 mat, float2 scale)
bool is_zero(float3x3 a)
float3 transform_direction(float3x3 mat, float3 direction)
float3 transform_point(float3x3 mat, float3 point)
float safe_divide(float a, float b)
float reduce_add(float2 a)
float reduce_min(float2 a)
float average(float2 a)
float reduce_max(float2 a)
ccl_device_forceinline void integrator_intersect_next_kernel_after_volume(KernelGlobals kg, IntegratorState state, const ccl_private Intersection *ccl_restrict isect, ccl_global float *ccl_restrict render_buffer)
ccl_device_forceinline float volume_phase_widen_dD(const KernelGlobals kg, const float prev_dD, const float sampled_roughness)
#define VOLUME_THROUGHPUT_EPSILON
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_inline Spectrum volume_sample_channel_pdf(Spectrum albedo, Spectrum throughput)
ccl_device float volume_channel_get(Spectrum value, const int channel)
const int state
ccl_gpu_kernel_postfix ccl_global KernelWorkTile const int ccl_global float * render_buffer
@ OBJECT_INVERSE_TRANSFORM
ccl_device_inline float object_volume_density(KernelGlobals kg, const int object)
ccl_device_inline int object_lightgroup(KernelGlobals kg, const int object)
ccl_device_inline Transform object_fetch_transform(KernelGlobals kg, const int object, enum ObjectTransform type)
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 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_record_volume_emission(ccl_attr_maybe_unused KernelGlobals kg, ccl_attr_maybe_unused IntegratorState state, ccl_attr_maybe_unused const Spectrum Le)
ccl_device_inline bool light_sample(KernelGlobals kg, const int lamp, const float2 rand, const float3 P, const float3 N, const int runtime_flags, const uint32_t path_flag, ccl_private LightSample *ls)
ccl_device_inline bool light_link_object_match(KernelGlobals kg, const int receiver, const int emitter)
ccl_device_inline int light_link_receiver_nee(KernelGlobals kg, const ccl_private ShaderData *sd)
ccl_device_inline int light_link_receiver_forward(KernelGlobals kg, IntegratorState state)
uint32_t PathRayVisibility
@ SR_BSDF_HAS_TRANSMISSION
@ SR_EMISSION
@ SR_CACHE_MISS
@ SR_EXTINCTION
@ SR_SCATTER
@ SD_HAS_LIGHT_PATH_NODE
@ PRNG_BOUNCE_NUM
@ PRNG_VOLUME_PHASE
@ PRNG_LIGHT
@ PRNG_VOLUME_OFFSET
@ PRNG_VOLUME_EXPANSION_ORDER
@ PRNG_VOLUME_SHADE_OFFSET
@ PRNG_VOLUME_RESERVOIR
@ PRNG_VOLUME_PHASE_GUIDING_DISTANCE
@ PRNG_VOLUME_COLOR_CHANNEL
@ PRNG_VOLUME_SCATTER_DISTANCE
@ PATH_RAY_VOLUME_PASS
@ PATH_RAY_TERMINATE
@ PATH_RAY_FLAG_NONE
@ PATH_RAY_TERMINATE_IN_NEXT_VOLUME
@ PATH_RAY_DENOISING_FEATURES
@ PATH_RAY_ANY_PASS
@ PATH_RAY_EXTINCTION
@ PATH_RAY_VOLUME_PRIMARY_TRANSMIT
@ PATH_RAY_VISIBILITY_VOLUME_SCATTER
@ PATH_RAY_VISIBILITY_CAMERA
@ PATH_RAY_VISIBILITY_SHADOW
@ SD_OBJECT_TRANSFORM_APPLIED
@ SHADER_EXCLUDE_SCATTER
@ SHADER_MASK
@ LIGHT_AREA
@ LIGHT_SPOT
@ LIGHT_TRIANGLE
@ LIGHT_POINT
@ DEVICE_KERNEL_INTEGRATOR_SHADE_VOLUME_RAY_MARCHING
@ DEVICE_KERNEL_INTEGRATOR_SHADE_LIGHT_NEE
@ DEVICE_KERNEL_INTEGRATOR_SHADE_VOLUME
@ DEVICE_KERNEL_INTEGRATOR_INTERSECT_SHADOW
@ DEVICE_KERNEL_INTEGRATOR_INTERSECT_CLOSEST
ccl_device_inline uint lcg_state_init(const uint rng_hash, const uint rng_offset, const uint sample, const uint scramble)
Definition lcg.h:45
ccl_device_inline bool triangle_light_valid_ray_segment(KernelGlobals kg, const float3 P, const float3 D, ccl_private Interval< float > *t_range, const ccl_private LightSample *ls)
ccl_device_inline bool bsdf_eval_is_zero(ccl_private BsdfEval *eval)
ccl_device_inline void film_write_volume_emission(KernelGlobals kg, ConstIntegratorState state, const Spectrum L, ccl_global float *ccl_restrict render_buffer, const int lightgroup=LIGHTGROUP_NONE)
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)
OrientationBounds merge(const OrientationBounds &cone_a, const OrientationBounds &cone_b)
ccl_device_inline uint count_leading_zeros(const uint x)
Definition math_base.h:753
ccl_device_inline int power_of_2(const int a)
Definition math_base.h:679
ccl_device_inline bool isfinite_safe(const float f)
Definition math_base.h:362
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
ccl_device_inline dual3 fabs(const ccl_private dual3 &a)
Definition math_dual.h:241
ccl_device_inline float fast_logf(const float x)
Definition math_fast.h:379
ccl_device_inline float2 mask(const MaskType mask, const float2 a)
CCL_NAMESPACE_BEGIN ccl_device_inline float3 zero_float3()
Definition math_float3.h:17
ccl_device_inline uint3 float3_as_uint3(const float3 f)
#define N
#define PROFILING_INIT(kg, event)
ccl_device float power_heuristic(const float a, const float b)
Definition mis.h:26
int size(const AssetLibraryReference *library_reference)
unsigned int uint
Definition BKE_text.h:55
unsigned short uint16_t
Definition BKE_text.h:80
unsigned int uint32_t
Definition BKE_text.h:81
unsigned char uint8_t
Definition BKE_text.h:79
closure color scatter(string phase, float Anisotropy, float IOR, float Backscatter, float Alpha, float Diameter)
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 void shadow_path_state_rng_load(ConstIntegratorShadowState state, ccl_private RNGState *rng_state)
Definition path_state.h:345
ccl_device_inline float path_state_rng_light_termination(KernelGlobals kg, const ccl_private RNGState *state)
Definition path_state.h:424
ccl_device_inline void path_state_rng_load(ConstIntegratorState state, ccl_private RNGState *rng_state)
Definition path_state.h:337
ccl_device_inline void path_state_next(KernelGlobals kg, IntegratorState state, const int label, const int runtime_flag)
Definition path_state.h:112
ccl_device_inline void path_state_rng_scramble(ccl_private RNGState *rng_state, const int seed)
Definition path_state.h:353
ccl_device_inline float2 path_state_rng_2D(KernelGlobals kg, const ccl_private RNGState *rng_state, const int dimension)
Definition path_state.h:369
ccl_device_inline float3 path_state_rng_3D(KernelGlobals kg, const ccl_private RNGState *rng_state, const int dimension)
Definition path_state.h:377
ccl_device_forceinline float path_rng_1D(KernelGlobals kg, const uint rng_pixel, const uint sample, const int dimension)
Definition pattern.h:66
@ PROFILING_SHADE_VOLUME_INTEGRATE
Definition profiling.h:34
@ PROFILING_SHADE_VOLUME_SETUP
Definition profiling.h:33
@ PROFILING_SHADE_VOLUME_DIRECT_LIGHT
Definition profiling.h:35
@ PROFILING_SHADE_VOLUME_INDIRECT_LIGHT
Definition profiling.h:36
#define mix
#define fabsf
#define ccl_device
#define fmaxf
#define tanf
#define fminf
#define atan2f
ccl_device_inline float sample_exponential_distribution(const float rand, const float lambda)
ccl_device_inline int sample_geometric_distribution(const float rand, const float r, ccl_private float &pmf, const int cut_off=INT_MAX)
ccl_device_inline Spectrum pdf_exponential_distribution(const float x, const Spectrum lambda, const Interval< float > t)
ccl_device void integrator_shade_volume_ray_marching(KernelGlobals kg, IntegratorState state, ccl_global float *ccl_restrict render_buffer)
ccl_device void integrator_shade_volume(KernelGlobals kg, IntegratorState state, ccl_global float *ccl_restrict render_buffer)
VolumeIntegrateEvent
@ VOLUME_PATH_CACHE_MISS
@ VOLUME_PATH_MISSED
@ VOLUME_PATH_SCATTERED
@ VOLUME_PATH_ATTENUATED
ccl_device_inline void integrator_next_kernel_after_shade_volume(KernelGlobals kg, const IntegratorState state, ccl_global float *ccl_restrict render_buffer, const ccl_private Intersection *ccl_restrict isect, const VolumeIntegrateEvent event)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING bsdf_eval_average
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
pass_diffuse_weight
KERNEL_FEATURE_LIGHT_PASSES unlit_throughput
#define min(a, b)
Definition sort.cc:38
ccl_device_inline bool spot_light_valid_ray_segment(KernelGlobals kg, const ccl_global KernelLight *klight, const float3 P, const float3 D, ccl_private Interval< float > *t_range)
Definition spot.h:252
IntegratorShadowStateCPU * IntegratorShadowState
Definition state.h:229
#define INTEGRATOR_STATE_WRITE(state, nested_struct, member)
Definition state.h:235
#define INTEGRATOR_STATE(state, nested_struct, member)
Definition state.h:234
#define INTEGRATOR_STATE_ARRAY(state, nested_struct, array_index, member)
Definition state.h:237
IntegratorStateCPU * IntegratorState
Definition state.h:227
const IntegratorStateCPU * ConstIntegratorState
Definition state.h:228
ccl_device_forceinline void integrator_path_cache_miss(IntegratorState, const DeviceKernel)
Definition state_flow.h:238
ccl_device_forceinline void integrator_path_next(IntegratorState state, const DeviceKernel current_kernel, const DeviceKernel next_kernel)
Definition state_flow.h:219
ccl_device_forceinline void integrator_path_terminate(KernelGlobals kg, IntegratorState state, ccl_global float *ccl_restrict render_buffer, const DeviceKernel current_kernel)
Definition state_flow.h:227
ccl_device_forceinline IntegratorShadowState integrator_shadow_path_init(ccl_attr_maybe_unused KernelGlobals kg, IntegratorState state, const DeviceKernel next_kernel, const bool is_ao)
Definition state_flow.h:262
ccl_device_forceinline void integrator_state_write_shadow_ray_self(IntegratorShadowState state, const ccl_private Ray *ccl_restrict ray)
Definition state_util.h:98
ccl_device_forceinline void integrator_state_read_ray(ConstIntegratorState state, ccl_private Ray *ccl_restrict ray)
Definition state_util.h:49
ccl_device_forceinline void integrator_state_write_shadow_ray(IntegratorShadowState state, const ccl_private Ray *ccl_restrict ray)
Definition state_util.h:74
ccl_device_forceinline void integrator_state_read_isect(ConstIntegratorState state, ccl_private Intersection *ccl_restrict isect)
Definition state_util.h:179
ccl_device_inline_method T range() const
Definition math_base.h:961
ccl_device_inline_method T length() const
Definition math_base.h:931
ccl_device_inline_method bool is_empty() const
Definition math_base.h:920
uint rng_pixel
Definition path_state.h:332
uint rng_offset
Definition path_state.h:333
float y
uint y
Definition types_uint3.h:13
uint z
Definition types_uint3.h:13
uint x
Definition types_uint3.h:13
float3 Spectrum
packed_float3 PackedSpectrum
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
ccl_device_inline float3 kernel_read_pass_rgbe(const ccl_global float *ccl_restrict buffer)
Definition write.h:153