Blender V5.3
path_state.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
10
12
14
15/* Initialize queues, so that this path is considered terminated.
16 * Used for early outputs in the camera ray initialization, as well as initialization of split
17 * states for shadow catcher. */
19{
20 INTEGRATOR_STATE_WRITE(state, path, queued_kernel) = 0;
21#ifndef __KERNEL_GPU__
22 INTEGRATOR_STATE_WRITE(&state->shadow, shadow_path, queued_kernel) = 0;
23 INTEGRATOR_STATE_WRITE(&state->ao, shadow_path, queued_kernel) = 0;
24#endif
25}
26
27/* Minimalistic initialization of the path state, which is needed for early outputs in the
28 * integrator initialization to work. */
31 const int x,
32 const int y)
33{
34 const uint render_pixel_index = (uint)tile->offset + x + y * tile->stride;
35
36 INTEGRATOR_STATE_WRITE(state, path, render_pixel_index) = render_pixel_index;
37
39}
40
41/* Initialize the rest of the path state needed to continue the path integration. */
44 const int sample,
45 const uint rng_pixel,
46 const Spectrum throughput)
47{
49 INTEGRATOR_STATE_WRITE(state, path, bounce) = 0;
50 INTEGRATOR_STATE_WRITE(state, path, diffuse_bounce) = 0;
51 INTEGRATOR_STATE_WRITE(state, path, glossy_bounce) = 0;
52 INTEGRATOR_STATE_WRITE(state, path, transmission_bounce) = 0;
53 INTEGRATOR_STATE_WRITE(state, path, transparent_bounce) = 0;
54 INTEGRATOR_STATE_WRITE(state, path, volume_bounce) = 0;
55 INTEGRATOR_STATE_WRITE(state, path, volume_bounds_bounce) = 0;
56 if ((kernel_data.kernel_features & KERNEL_FEATURE_NODE_PORTAL)) {
57 INTEGRATOR_STATE_WRITE(state, path, portal_bounce) = 0;
58 }
59 INTEGRATOR_STATE_WRITE(state, path, rng_pixel) = rng_pixel;
60 INTEGRATOR_STATE_WRITE(state, path, rng_offset) = PRNG_BOUNCE_NUM;
63 INTEGRATOR_STATE_WRITE(state, path, mis_ray_pdf) = 0.0f;
64 INTEGRATOR_STATE_WRITE(state, path, min_ray_pdf) = FLT_MAX;
65 INTEGRATOR_STATE_WRITE(state, path, continuation_probability) = 1.0f;
66 INTEGRATOR_STATE_WRITE(state, path, throughput) = throughput;
67 INTEGRATOR_STATE_WRITE(state, path, optical_depth) = 0.0f;
68#if defined(__PATH_GUIDING__)
69 if ((kernel_data.kernel_features & KERNEL_FEATURE_PATH_GUIDING)) {
70 INTEGRATOR_STATE_WRITE(state, path, unguided_throughput) = 1.0f;
71 INTEGRATOR_STATE_WRITE(state, guiding, path_segment) = nullptr;
72 INTEGRATOR_STATE_WRITE(state, guiding, use_surface_guiding) = false;
73 INTEGRATOR_STATE_WRITE(state, guiding, sample_surface_guiding_rand) = 0.5f;
75 INTEGRATOR_STATE_WRITE(state, guiding, bssrdf_sampling_prob) = 0.0f;
76 INTEGRATOR_STATE_WRITE(state, guiding, use_volume_guiding) = false;
78 INTEGRATOR_STATE_WRITE(state, guiding, volume_guiding_sampling_prob) = 0.0f;
79 }
80#endif
81#ifdef __MNEE__
82 INTEGRATOR_STATE_WRITE(state, path, mnee) = 0;
83#endif
84
88
89 if (kernel_data.kernel_features & KERNEL_FEATURE_VOLUME) {
91 state, volume_stack, 0, object) = kernel_data.background.object_index;
93 state, volume_stack, 0, shader) = kernel_data.background.volume_shader;
94 INTEGRATOR_STATE_ARRAY_WRITE(state, volume_stack, 1, object) = OBJECT_NONE;
95 INTEGRATOR_STATE_ARRAY_WRITE(state, volume_stack, 1, shader) = SHADER_NONE;
96 }
97
98#ifdef __DENOISING_FEATURES__
99 if (kernel_data.kernel_features & KERNEL_FEATURE_DENOISING) {
102 }
103#endif
104
105#ifdef __LIGHT_LINKING__
106 if (kernel_data.kernel_features & KERNEL_FEATURE_LIGHT_LINKING) {
107 INTEGRATOR_STATE_WRITE(state, path, mis_ray_object) = kernel_data.background.object_index;
108 }
109#endif
110}
111
114 const int label,
115 const int runtime_flag)
116{
117 PathRayVisibility visibility = INTEGRATOR_STATE(state, path, visibility);
118 uint32_t flag = INTEGRATOR_STATE(state, path, flag);
119
120 /* ray through transparent keeps same flags from previous ray and is
121 * not counted as a regular bounce, transparent has separate max */
122 if (label & (LABEL_TRANSPARENT | LABEL_RAY_PORTAL)) {
123 const int transparent_bounce = INTEGRATOR_STATE(state, path, transparent_bounce) + 1;
124
125 flag |= PATH_RAY_TRANSPARENT;
126 if (transparent_bounce >= kernel_data.integrator.transparent_max_bounce) {
127 /* FIXME: `transparent_max_bounce` could be 0, but `transparent_bounce` is at least 1 when we
128 * enter this path. */
130 }
131
132 if (runtime_flag & SR_RAY_PORTAL) {
133 flag |= PATH_RAY_MIS_SKIP;
135 state, path, portal_bounce) = INTEGRATOR_STATE(state, path, portal_bounce) + 1;
136 }
137
138 INTEGRATOR_STATE_WRITE(state, path, flag) = flag;
139 INTEGRATOR_STATE_WRITE(state, path, transparent_bounce) = transparent_bounce;
140 /* Random number generator next bounce. */
141 INTEGRATOR_STATE_WRITE(state, path, rng_offset) += PRNG_BOUNCE_NUM;
142 return;
143 }
144
145 const int bounce = INTEGRATOR_STATE(state, path, bounce) + 1;
146 if (bounce >= kernel_data.integrator.max_bounce) {
148 }
149
150 visibility = PATH_RAY_VISIBILITY_NONE;
153
154#ifdef __VOLUME__
155 if (label & LABEL_VOLUME_SCATTER) {
156 /* volume scatter */
160 if (!(flag & PATH_RAY_ANY_PASS)) {
161 flag |= PATH_RAY_VOLUME_PASS;
162 }
163
164 const int volume_bounce = INTEGRATOR_STATE(state, path, volume_bounce) + 1;
165 INTEGRATOR_STATE_WRITE(state, path, volume_bounce) = volume_bounce;
166 if (volume_bounce >= kernel_data.integrator.max_volume_bounce) {
168 }
169
170 if (bounce == 1) {
172 }
173 }
174 else
175#endif
176 {
177 /* surface reflection/transmission */
178 if (label & LABEL_REFLECT) {
179 flag |= PATH_RAY_REFLECT;
181
182 if (label & LABEL_DIFFUSE) {
183 const int diffuse_bounce = INTEGRATOR_STATE(state, path, diffuse_bounce) + 1;
184 INTEGRATOR_STATE_WRITE(state, path, diffuse_bounce) = diffuse_bounce;
185 if (diffuse_bounce >= kernel_data.integrator.max_diffuse_bounce) {
187 }
188 }
189 else {
190 const int glossy_bounce = INTEGRATOR_STATE(state, path, glossy_bounce) + 1;
191 INTEGRATOR_STATE_WRITE(state, path, glossy_bounce) = glossy_bounce;
192 if (glossy_bounce >= kernel_data.integrator.max_glossy_bounce) {
194 }
195 }
196 }
197 else {
199
200 visibility |= PATH_RAY_VISIBILITY_TRANSMIT;
201
202 if (!(label & LABEL_TRANSMIT_TRANSPARENT)) {
204 }
205
206 const int transmission_bounce = INTEGRATOR_STATE(state, path, transmission_bounce) + 1;
207 INTEGRATOR_STATE_WRITE(state, path, transmission_bounce) = transmission_bounce;
208 if (transmission_bounce >= kernel_data.integrator.max_transmission_bounce) {
210 }
211 }
212
213 /* diffuse/glossy/singular */
214 if (label & LABEL_DIFFUSE) {
215 visibility |= PATH_RAY_VISIBILITY_DIFFUSE;
217 }
218 else if (label & LABEL_GLOSSY) {
219 visibility |= PATH_RAY_VISIBILITY_GLOSSY;
220 }
221 else {
223 visibility |= PATH_RAY_VISIBILITY_GLOSSY;
225 }
226
227 /* Flag for consistent MIS weights with light tree. */
228 if (runtime_flag & SR_BSDF_HAS_TRANSMISSION) {
230 }
231
232 /* Render pass categories. */
233 if (!(flag & PATH_RAY_ANY_PASS) && !(flag & PATH_RAY_TRANSPARENT_BACKGROUND)) {
234 flag |= PATH_RAY_SURFACE_PASS;
235 }
236 }
237
238 INTEGRATOR_STATE_WRITE(state, path, visibility) = visibility;
239 INTEGRATOR_STATE_WRITE(state, path, flag) = flag;
240 INTEGRATOR_STATE_WRITE(state, path, bounce) = bounce;
241
242 /* Random number generator next bounce. */
243 INTEGRATOR_STATE_WRITE(state, path, rng_offset) += PRNG_BOUNCE_NUM;
244}
245
246#ifdef __VOLUME__
247ccl_device_inline bool path_state_volume_next(IntegratorState state)
248{
249 /* For volume bounding meshes we pass through without counting transparent
250 * bounces, only sanity check in case self intersection gets us stuck. */
251 const uint32_t volume_bounds_bounce = INTEGRATOR_STATE(state, path, volume_bounds_bounce) + 1;
252 INTEGRATOR_STATE_WRITE(state, path, volume_bounds_bounce) = volume_bounds_bounce;
253 if (volume_bounds_bounce > VOLUME_BOUNDS_MAX) {
254 return false;
255 }
256
257 /* Random number generator next bounce. */
258 INTEGRATOR_STATE_WRITE(state, path, rng_offset) += PRNG_BOUNCE_NUM;
259
260 return true;
261}
262#endif
263
265{
266 PathRayVisibility visibility = INTEGRATOR_STATE(state, path, visibility);
267
268 /* For visibility, diffuse/glossy are for reflection only. */
269 if (visibility & PATH_RAY_VISIBILITY_TRANSMIT) {
271 }
272
273 const uint32_t path_flag = INTEGRATOR_STATE(state, path, flag);
274 visibility = SHADOW_CATCHER_PATH_VISIBILITY(path_flag, visibility);
275
276 return visibility;
277}
278
281 const uint32_t path_flag)
282{
283 if (path_flag & PATH_RAY_TRANSPARENT) {
284 const int transparent_bounce = INTEGRATOR_STATE(state, path, transparent_bounce);
285 /* Do at least specified number of bounces without RR. */
286 if (transparent_bounce <= kernel_data.integrator.transparent_min_bounce) {
287 return 1.0f;
288 }
289 }
290 else {
291 const int bounce = INTEGRATOR_STATE(state, path, bounce);
292 /* Do at least specified number of bounces without RR. */
293 if (bounce <= kernel_data.integrator.min_bounce) {
294 return 1.0f;
295 }
296 }
297
298 /* Probabilistic termination: use `sqrt()` to roughly match typical view
299 * transform and do path termination a bit later on average. */
300 Spectrum throughput = INTEGRATOR_STATE(state, path, throughput);
301#if defined(__PATH_GUIDING__) && PATH_GUIDING_LEVEL >= 4
302 if ((kernel_data.kernel_features & KERNEL_FEATURE_PATH_GUIDING)) {
303 throughput *= INTEGRATOR_STATE(state, path, unguided_throughput);
304 }
305#endif
306 return min(sqrtf(reduce_max(fabs(throughput))), 1.0f);
307}
308
310{
311 if (!kernel_data.integrator.ao_bounces) {
312 return false;
313 }
314
315 const int bounce = INTEGRATOR_STATE(state, path, bounce) -
316 INTEGRATOR_STATE(state, path, transmission_bounce) -
317 (INTEGRATOR_STATE(state, path, glossy_bounce) > 0) + 1;
318 return (bounce > kernel_data.integrator.ao_bounces);
319}
320
321/* Random Number Sampling Utility Functions
322 *
323 * For each random number in each step of the path we must have a unique
324 * dimension to avoid using the same sequence twice.
325 *
326 * For branches in the path we must be careful not to reuse the same number
327 * in a sequence and offset accordingly.
328 */
329
330/* RNG State loaded onto stack. */
331struct RNGState {
335};
336
338 ccl_private RNGState *rng_state)
339{
340 rng_state->rng_pixel = INTEGRATOR_STATE(state, path, rng_pixel);
341 rng_state->rng_offset = INTEGRATOR_STATE(state, path, rng_offset);
342 rng_state->sample = INTEGRATOR_STATE(state, path, sample);
343}
344
346 ccl_private RNGState *rng_state)
347{
348 rng_state->rng_pixel = INTEGRATOR_STATE(state, shadow_path, rng_pixel);
349 rng_state->rng_offset = INTEGRATOR_STATE(state, shadow_path, rng_offset);
350 rng_state->sample = INTEGRATOR_STATE(state, shadow_path, sample);
351}
352
354{
355 /* To get an uncorrelated sequence of samples (e.g. for subsurface random walk), just change
356 * the dimension offset since all implemented samplers can generate unlimited numbers of
357 * dimensions anyway. The only thing to ensure is that the offset is divisible by 4. */
358 rng_state->rng_offset = hash_hp_seeded_uint(rng_state->rng_offset, seed) & ~0x3;
359}
360
362 const ccl_private RNGState *rng_state,
363 const int dimension)
364{
365 return path_rng_1D(
366 kg, rng_state->rng_pixel, rng_state->sample, rng_state->rng_offset + dimension);
367}
368
370 const ccl_private RNGState *rng_state,
371 const int dimension)
372{
373 return path_rng_2D(
374 kg, rng_state->rng_pixel, rng_state->sample, rng_state->rng_offset + dimension);
375}
376
378 const ccl_private RNGState *rng_state,
379 const int dimension)
380{
381 return path_rng_3D(
382 kg, rng_state->rng_pixel, rng_state->sample, rng_state->rng_offset + dimension);
383}
384
386 const ccl_private RNGState *rng_state,
387 const int branch,
388 const int num_branches,
389 const int dimension)
390{
391 return path_rng_1D(kg,
392 rng_state->rng_pixel,
393 rng_state->sample * num_branches + branch,
394 rng_state->rng_offset + dimension);
395}
396
398 const ccl_private RNGState *rng_state,
399 const int branch,
400 const int num_branches,
401 const int dimension)
402{
403 return path_rng_2D(kg,
404 rng_state->rng_pixel,
405 rng_state->sample * num_branches + branch,
406 rng_state->rng_offset + dimension);
407}
408
410 const ccl_private RNGState *rng_state,
411 const int branch,
412 const int num_branches,
413 const int dimension)
414{
415 return path_rng_3D(kg,
416 rng_state->rng_pixel,
417 rng_state->sample * num_branches + branch,
418 rng_state->rng_offset + dimension);
419}
420
421/* Utility functions to get light termination value,
422 * since it might not be needed in many cases.
423 */
426{
427 if (kernel_data.integrator.light_inv_rr_threshold > 0.0f) {
429 }
430 return 0.0f;
431}
432
433#ifdef __SPECTRAL__
434ccl_device_inline Spectrum dispersion_throughput_weight(KernelGlobals kg, const float rand)
435{
436 float prob;
437 const float wavelength = sample_wavelength(rand, &prob);
438 return wavelength_to_rgb_d65(kg, wavelength) / prob;
439}
440
441ccl_device_inline void update_path_throughput_for_dispersion(KernelGlobals kg,
443 const float rand)
444{
445 const uint32_t path_flag = INTEGRATOR_STATE(state, path, flag);
446 if (path_flag & PATH_RAY_SPECTRAL) {
447 /* Throughput is already updated. */
448 return;
449 }
450
451 /* Update throughput since the path is now monochromatic. */
452 INTEGRATOR_STATE_WRITE(state, path, throughput) *= dispersion_throughput_weight(kg, rand);
454}
455#endif
456
#define kernel_assert(cond)
#define KERNEL_FEATURE_VOLUME
#define KERNEL_FEATURE_NODE_PORTAL
#define kernel_data
#define ccl_restrict
#define VOLUME_BOUNDS_MAX
#define KERNEL_FEATURE_PATH_GUIDING
#define SHADER_NONE
#define one_spectrum
#define PRIM_NONE
#define OBJECT_NONE
#define ccl_private
#define ccl_device_inline
#define KERNEL_FEATURE_DENOISING
#define KERNEL_FEATURE_LIGHT_LINKING
#define ccl_global
#define SHADOW_CATCHER_PATH_VISIBILITY(path_flag, visibility)
#define CCL_NAMESPACE_END
#define FLT_MAX
float reduce_max(float2 a)
ccl_device_inline uint hash_hp_seeded_uint(const uint i, uint seed)
Definition hash.h:526
const int state
const ccl_global KernelWorkTile * tile
uint32_t PathRayVisibility
@ SR_RAY_PORTAL
@ SR_BSDF_HAS_TRANSMISSION
@ PRIMITIVE_NONE
@ PRNG_BOUNCE_NUM
@ PRNG_LIGHT_TERMINATE
@ PATH_RAY_VOLUME_PASS
@ PATH_RAY_TERMINATE_AFTER_TRANSPARENT
@ PATH_RAY_SINGULAR
@ PATH_RAY_REFLECT
@ PATH_RAY_IMPORTANCE_BAKE
@ PATH_RAY_MIS_HAD_TRANSMISSION
@ PATH_RAY_TRANSPARENT
@ PATH_RAY_MIS_SKIP
@ PATH_RAY_DENOISING_FEATURES
@ PATH_RAY_SURFACE_PASS
@ PATH_RAY_TRANSPARENT_BACKGROUND
@ PATH_RAY_ANY_PASS
@ PATH_RAY_TERMINATE_ON_NEXT_SURFACE
@ PATH_RAY_VOLUME_PRIMARY_TRANSMIT
@ PATH_RAY_DIFFUSE_ANCESTOR
@ PATH_RAY_SPECTRAL
@ PATH_RAY_VISIBILITY_VOLUME_SCATTER
@ PATH_RAY_VISIBILITY_CAMERA
@ PATH_RAY_VISIBILITY_TRANSMIT
@ PATH_RAY_VISIBILITY_DIFFUSE
@ PATH_RAY_VISIBILITY_NONE
@ PATH_RAY_VISIBILITY_GLOSSY
@ LABEL_TRANSMIT
@ LABEL_RAY_PORTAL
@ LABEL_TRANSMIT_TRANSPARENT
@ LABEL_VOLUME_SCATTER
@ LABEL_DIFFUSE
@ LABEL_SINGULAR
@ LABEL_GLOSSY
@ LABEL_REFLECT
@ LABEL_TRANSPARENT
ccl_device_inline dual3 fabs(const ccl_private dual3 &a)
Definition math_dual.h:241
unsigned int uint32_t
Definition BKE_text.h:81
ccl_device_inline void path_state_init_integrator(KernelGlobals kg, IntegratorState state, const int sample, const uint rng_pixel, const Spectrum throughput)
Definition path_state.h:42
ccl_device_inline float path_state_continuation_probability(KernelGlobals kg, ConstIntegratorState state, const uint32_t path_flag)
Definition path_state.h:279
ccl_device_inline bool path_state_ao_bounce(KernelGlobals kg, ConstIntegratorState state)
Definition path_state.h:309
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_branched_rng_1D(KernelGlobals kg, const ccl_private RNGState *rng_state, const int branch, const int num_branches, const int dimension)
Definition path_state.h:385
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 float2 path_branched_rng_2D(KernelGlobals kg, const ccl_private RNGState *rng_state, const int branch, const int num_branches, const int dimension)
Definition path_state.h:397
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 float3 path_branched_rng_3D(KernelGlobals kg, const ccl_private RNGState *rng_state, const int branch, const int num_branches, const int dimension)
Definition path_state.h:409
CCL_NAMESPACE_BEGIN ccl_device_inline void path_state_init_queues(IntegratorState state)
Definition path_state.h:18
ccl_device_inline PathRayVisibility path_state_ray_visibility(ConstIntegratorState state)
Definition path_state.h:264
ccl_device_inline void path_state_rng_scramble(ccl_private RNGState *rng_state, const int seed)
Definition path_state.h:353
ccl_device_inline void path_state_init(IntegratorState state, const ccl_global KernelWorkTile *ccl_restrict tile, const int x, const int y)
Definition path_state.h:29
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 float2 path_rng_2D(KernelGlobals kg, const uint rng_pixel, const int sample, const int dimension)
Definition pattern.h:83
ccl_device_forceinline float3 path_rng_3D(KernelGlobals kg, const uint rng_pixel, const int sample, const int dimension)
Definition pattern.h:100
ccl_device_forceinline float path_rng_1D(KernelGlobals kg, const uint rng_pixel, const uint sample, const int dimension)
Definition pattern.h:66
#define sqrtf
#define min(a, b)
Definition sort.cc:38
#define INTEGRATOR_STATE_ARRAY_WRITE(state, nested_struct, array_index, member)
Definition state.h:239
#define INTEGRATOR_STATE_WRITE(state, nested_struct, member)
Definition state.h:235
#define INTEGRATOR_STATE(state, nested_struct, member)
Definition state.h:234
const IntegratorShadowStateCPU * ConstIntegratorShadowState
Definition state.h:230
IntegratorStateCPU * IntegratorState
Definition state.h:227
const IntegratorStateCPU * ConstIntegratorState
Definition state.h:228
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_DENOISING KERNEL_STRUCT_VOLUME_STACK_SIZE KERNEL_FEATURE_PATH_GUIDING sample_volume_guiding_rand
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_DENOISING KERNEL_STRUCT_VOLUME_STACK_SIZE surface_guiding_sampling_prob
KERNEL_FEATURE_LIGHT_PASSES denoising_feature_throughput
uint rng_pixel
Definition path_state.h:332
uint rng_offset
Definition path_state.h:333
float3 Spectrum