Blender V5.3
state_util.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#pragma once
6
7#include "kernel/globals.h"
8
10
11#include "kernel/light/common.h"
12
13#include "kernel/sample/lcg.h"
14
16
17#if defined(__KERNEL_GPU__)
18# include "util/atomic.h"
19#endif
20
22
23/* Ray */
24
26 const ccl_private Ray *ccl_restrict ray)
27{
28#if defined(__INTEGRATOR_GPU_PACKED_STATE__) && defined(__KERNEL_GPU__)
29 packed_ray packed;
30 packed.P = ray->P;
31 packed.dP = ray->dP;
32 packed.D = ray->D;
33 packed.dD = ray->dD;
34 packed.tmin = ray->tmin;
35 packed.tmax = ray->tmax;
36 packed.time = ray->time;
37 INTEGRATOR_STATE_WRITE(state, ray, packed) = packed;
38#else
39 INTEGRATOR_STATE_WRITE(state, ray, P) = ray->P;
40 INTEGRATOR_STATE_WRITE(state, ray, D) = ray->D;
41 INTEGRATOR_STATE_WRITE(state, ray, tmin) = ray->tmin;
42 INTEGRATOR_STATE_WRITE(state, ray, tmax) = ray->tmax;
43 INTEGRATOR_STATE_WRITE(state, ray, time) = ray->time;
44 INTEGRATOR_STATE_WRITE(state, ray, dP) = ray->dP;
45 INTEGRATOR_STATE_WRITE(state, ray, dD) = ray->dD;
46#endif
47}
48
51{
52#if defined(__INTEGRATOR_GPU_PACKED_STATE__) && defined(__KERNEL_GPU__)
53 const packed_ray packed = INTEGRATOR_STATE(state, ray, packed);
54 ray->P = packed.P;
55 ray->dP = packed.dP;
56 ray->D = packed.D;
57 ray->dD = packed.dD;
58 ray->tmin = packed.tmin;
59 ray->tmax = packed.tmax;
60 ray->time = packed.time;
61#else
62 ray->P = INTEGRATOR_STATE(state, ray, P);
63 ray->D = INTEGRATOR_STATE(state, ray, D);
64 ray->tmin = INTEGRATOR_STATE(state, ray, tmin);
65 ray->tmax = INTEGRATOR_STATE(state, ray, tmax);
66 ray->time = INTEGRATOR_STATE(state, ray, time);
67 ray->dP = INTEGRATOR_STATE(state, ray, dP);
68 ray->dD = INTEGRATOR_STATE(state, ray, dD);
69#endif
70}
71
72/* Shadow Ray */
73
76{
77 INTEGRATOR_STATE_WRITE(state, shadow_ray, P) = ray->P;
78 INTEGRATOR_STATE_WRITE(state, shadow_ray, D) = ray->D;
79 INTEGRATOR_STATE_WRITE(state, shadow_ray, tmin) = ray->tmin;
80 INTEGRATOR_STATE_WRITE(state, shadow_ray, tmax) = ray->tmax;
81 INTEGRATOR_STATE_WRITE(state, shadow_ray, time) = ray->time;
82 INTEGRATOR_STATE_WRITE(state, shadow_ray, dP) = ray->dP;
83 INTEGRATOR_STATE_WRITE(state, shadow_ray, dD) = ray->dD;
84}
85
88{
89 ray->P = INTEGRATOR_STATE(state, shadow_ray, P);
90 ray->D = INTEGRATOR_STATE(state, shadow_ray, D);
91 ray->tmin = INTEGRATOR_STATE(state, shadow_ray, tmin);
92 ray->tmax = INTEGRATOR_STATE(state, shadow_ray, tmax);
93 ray->time = INTEGRATOR_STATE(state, shadow_ray, time);
94 ray->dP = INTEGRATOR_STATE(state, shadow_ray, dP);
95 ray->dD = INTEGRATOR_STATE(state, shadow_ray, dD);
96}
97
100{
101 /* There is a bit of implicit knowledge about the way how the kernels are invoked and what the
102 * state is actually storing. Special logic here is needed because the intersect_shadow kernel
103 * might be called multiple times. This happens when the total number of intersections by the
104 * ray (shadow_path.packed_num_hits) exceeds INTEGRATOR_SHADOW_ISECT_SIZE.
105 *
106 * Writing of the shadow_ray.self to the state happens only during the shadow ray setup, and
107 * the shadow_isect array gets overwritten by the intersect_shadow kernel. It is important to
108 * preserve the exact values of the light_object and light_prim for all invocations of the
109 * intersect_shadow kernel. Hence they are written to dedicated fields in the state.
110 *
111 * The self.object and self.prim are kept at the latest handled intersection: during shadow path
112 * branch-off it matches the main ray.self. For the consecutive calls of the intersect_shadow
113 * kernels it comes from the furthest intersection (the last element of the shadow_isect). So we
114 * use INTEGRATOR_SHADOW_ISECT_SIZE - 1 index for both writing and reading. This utilizes
115 * knowledge that intersect_shadow kernel is only called for either initial intersection, or when
116 * the number of ray intersections exceeds the shadow_isect size.
117 *
118 * This should help avoiding situations when the same intersection is recorded multiple times
119 * throughout separate invocations of the intersect_shadow kernel. However, it is still not
120 * fully reliable as there might be more than INTEGRATOR_SHADOW_ISECT_SIZE intersections at the
121 * same ray->t. There is no reliable way to deal with such situation, and offsetting ray from
122 * the shade_shadow kernel which will avoid potential false-positive detection of light being
123 * fully blocked at the expense of potentially ignoring some intersections. If the offset is
124 * used then preserving self.object and self.prim might not be as useful, but it definitely does
125 * not harm. */
126
128 state, shadow_isect, INTEGRATOR_SHADOW_ISECT_SIZE - 1, object) = ray->self.object;
130 state, shadow_isect, INTEGRATOR_SHADOW_ISECT_SIZE - 1, prim) = ray->self.prim;
131
132 INTEGRATOR_STATE_WRITE(state, shadow_ray, self_light_object) = ray->self.light_object;
133 INTEGRATOR_STATE_WRITE(state, shadow_ray, self_light_prim) = ray->self.light_prim;
134}
135
138{
139 ray->self.object = INTEGRATOR_STATE_ARRAY(
140 state, shadow_isect, INTEGRATOR_SHADOW_ISECT_SIZE - 1, object);
141 ray->self.prim = INTEGRATOR_STATE_ARRAY(
142 state, shadow_isect, INTEGRATOR_SHADOW_ISECT_SIZE - 1, prim);
143 ray->self.light_object = INTEGRATOR_STATE(state, shadow_ray, self_light_object);
144 ray->self.light_prim = INTEGRATOR_STATE(state, shadow_ray, self_light_prim);
145}
146
147/* Intersection */
148
151{
152#if defined(__INTEGRATOR_GPU_PACKED_STATE__) && defined(__KERNEL_GPU__)
153 INTEGRATOR_STATE_WRITE(state, isect, packed) = (ccl_private packed_isect &)*isect;
154
155 /* Ensure that we can correctly cast between Intersection and the generated packed_isect struct.
156 */
157 static_assert(offsetof(packed_isect, t) == offsetof(Intersection, t),
158 "Generated packed_isect struct is misaligned with Intersection struct");
159 static_assert(offsetof(packed_isect, u) == offsetof(Intersection, u),
160 "Generated packed_isect struct is misaligned with Intersection struct");
161 static_assert(offsetof(packed_isect, v) == offsetof(Intersection, v),
162 "Generated packed_isect struct is misaligned with Intersection struct");
163 static_assert(offsetof(packed_isect, object) == offsetof(Intersection, object),
164 "Generated packed_isect struct is misaligned with Intersection struct");
165 static_assert(offsetof(packed_isect, prim) == offsetof(Intersection, prim),
166 "Generated packed_isect struct is misaligned with Intersection struct");
167 static_assert(offsetof(packed_isect, type) == offsetof(Intersection, type),
168 "Generated packed_isect struct is misaligned with Intersection struct");
169#else
170 INTEGRATOR_STATE_WRITE(state, isect, t) = isect->t;
171 INTEGRATOR_STATE_WRITE(state, isect, u) = isect->u;
172 INTEGRATOR_STATE_WRITE(state, isect, v) = isect->v;
173 INTEGRATOR_STATE_WRITE(state, isect, object) = isect->object;
174 INTEGRATOR_STATE_WRITE(state, isect, prim) = isect->prim;
175 INTEGRATOR_STATE_WRITE(state, isect, type) = isect->type;
176#endif
177}
178
181{
182#if defined(__INTEGRATOR_GPU_PACKED_STATE__) && defined(__KERNEL_GPU__)
183 *((ccl_private packed_isect *)isect) = INTEGRATOR_STATE(state, isect, packed);
184#else
185 isect->prim = INTEGRATOR_STATE(state, isect, prim);
186 isect->object = INTEGRATOR_STATE(state, isect, object);
187 isect->type = INTEGRATOR_STATE(state, isect, type);
188 isect->u = INTEGRATOR_STATE(state, isect, u);
189 isect->v = INTEGRATOR_STATE(state, isect, v);
190 isect->t = INTEGRATOR_STATE(state, isect, t);
191#endif
192}
193
194#ifdef __VOLUME__
195ccl_device_forceinline VolumeStack integrator_state_read_volume_stack(ConstIntegratorState state,
196 const int i)
197{
198 VolumeStack entry = {INTEGRATOR_STATE_ARRAY(state, volume_stack, i, object),
199 INTEGRATOR_STATE_ARRAY(state, volume_stack, i, shader)};
200 return entry;
201}
202
203ccl_device_forceinline void integrator_state_write_volume_stack(IntegratorState state,
204 const int i,
205 VolumeStack entry)
206{
207 INTEGRATOR_STATE_ARRAY_WRITE(state, volume_stack, i, object) = entry.object;
208 INTEGRATOR_STATE_ARRAY_WRITE(state, volume_stack, i, shader) = entry.shader;
209}
210
211ccl_device_forceinline bool integrator_state_volume_stack_is_empty(KernelGlobals kg,
213{
214 return (kernel_data.kernel_features & KERNEL_FEATURE_VOLUME) ?
215 INTEGRATOR_STATE_ARRAY(state, volume_stack, 0, shader) == SHADER_NONE :
216 true;
217}
218
219ccl_device_forceinline void integrator_state_copy_volume_stack_to_shadow(
221{
222 if (kernel_data.kernel_features & KERNEL_FEATURE_VOLUME) {
223 int index = 0;
224 int shader;
225 do {
226 shader = INTEGRATOR_STATE_ARRAY(state, volume_stack, index, shader);
227
228 INTEGRATOR_STATE_ARRAY_WRITE(shadow_state, shadow_volume_stack, index, object) =
229 INTEGRATOR_STATE_ARRAY(state, volume_stack, index, object);
230 INTEGRATOR_STATE_ARRAY_WRITE(shadow_state, shadow_volume_stack, index, shader) = shader;
231
232 ++index;
233 } while (shader != SHADER_NONE);
234 }
235}
236
237ccl_device_forceinline void integrator_state_copy_volume_stack(KernelGlobals kg,
238 IntegratorState to_state,
240{
241 if (kernel_data.kernel_features & KERNEL_FEATURE_VOLUME) {
242 int index = 0;
243 int shader;
244 do {
245 shader = INTEGRATOR_STATE_ARRAY(state, volume_stack, index, shader);
246
247 INTEGRATOR_STATE_ARRAY_WRITE(to_state, volume_stack, index, object) = INTEGRATOR_STATE_ARRAY(
248 state, volume_stack, index, object);
249 INTEGRATOR_STATE_ARRAY_WRITE(to_state, volume_stack, index, shader) = shader;
250
251 ++index;
252 } while (shader != SHADER_NONE);
253 }
254}
255
256ccl_device_forceinline VolumeStack
257integrator_state_read_shadow_volume_stack(ConstIntegratorShadowState state, const int i)
258{
259 VolumeStack entry = {INTEGRATOR_STATE_ARRAY(state, shadow_volume_stack, i, object),
260 INTEGRATOR_STATE_ARRAY(state, shadow_volume_stack, i, shader)};
261 return entry;
262}
263
264ccl_device_forceinline bool integrator_state_shadow_volume_stack_is_empty(
266{
267 return (kernel_data.kernel_features & KERNEL_FEATURE_VOLUME) ?
268 INTEGRATOR_STATE_ARRAY(state, shadow_volume_stack, 0, shader) == SHADER_NONE :
269 true;
270}
271
272ccl_device_forceinline void integrator_state_write_shadow_volume_stack(IntegratorShadowState state,
273 const int i,
274 VolumeStack entry)
275{
276 INTEGRATOR_STATE_ARRAY_WRITE(state, shadow_volume_stack, i, object) = entry.object;
277 INTEGRATOR_STATE_ARRAY_WRITE(state, shadow_volume_stack, i, shader) = entry.shader;
278}
279
280#endif /* __VOLUME__ */
281
282/* Shadow Intersection */
283
287 const int index)
288{
289 INTEGRATOR_STATE_ARRAY_WRITE(state, shadow_isect, index, t) = isect->t;
290 INTEGRATOR_STATE_ARRAY_WRITE(state, shadow_isect, index, u) = isect->u;
291 INTEGRATOR_STATE_ARRAY_WRITE(state, shadow_isect, index, v) = isect->v;
292 INTEGRATOR_STATE_ARRAY_WRITE(state, shadow_isect, index, object) = isect->object;
293 INTEGRATOR_STATE_ARRAY_WRITE(state, shadow_isect, index, prim) = isect->prim;
294 INTEGRATOR_STATE_ARRAY_WRITE(state, shadow_isect, index, type) = isect->type;
295}
296
300 const int index)
301{
302 isect->prim = INTEGRATOR_STATE_ARRAY(state, shadow_isect, index, prim);
303 isect->object = INTEGRATOR_STATE_ARRAY(state, shadow_isect, index, object);
304 isect->type = INTEGRATOR_STATE_ARRAY(state, shadow_isect, index, type);
305 isect->u = INTEGRATOR_STATE_ARRAY(state, shadow_isect, index, u);
306 isect->v = INTEGRATOR_STATE_ARRAY(state, shadow_isect, index, v);
307 isect->t = INTEGRATOR_STATE_ARRAY(state, shadow_isect, index, t);
308}
309
310#ifdef __SHADOW_LINKING__
311
312/* Shadow linking state for shade_light_nee, reusing shadow state
313 * to avoid increasing state memory usage. */
314
315static_assert(INTEGRATOR_SHADOW_ISECT_SIZE > 1, "Light and self intersection must not overlap");
316
317ccl_device_forceinline void integrator_state_write_shadow_light_isect(
319{
321}
322
323ccl_device_forceinline void integrator_state_read_shadow_light_isect(
325{
327}
328
329#endif
330
331/* MNEE state.
332 *
333 * This is packed into the shadow_state to avoid increasing overall path state size. */
334
335#ifdef __MNEE__
336
338integrator_state_get_mnee_shadow_state(ConstIntegratorState state)
339{
340# ifdef __KERNEL_GPU__
341 return IntegratorShadowState(INTEGRATOR_STATE(state, path, mnee_shadow_state));
342# else
343 return &(((IntegratorStateCPU *)state)->shadow);
344# endif
345}
346
347# ifdef __KERNEL_GPU__
348/* The MNEE shadow slot stores a reference to its owning main path so shadow path
349 * sorting can maintain the correct index. */
350ccl_device_forceinline void integrator_state_write_mnee_shadow_owner(
352{
353 static_assert(INTEGRATOR_SHADOW_ISECT_SIZE >= 3);
354 INTEGRATOR_STATE_ARRAY_WRITE(shadow_state, shadow_isect, 2, prim) = (int)state;
355}
356
358integrator_state_read_mnee_shadow_owner(ConstIntegratorShadowState shadow_state)
359{
360 static_assert(INTEGRATOR_SHADOW_ISECT_SIZE >= 3);
361 return IntegratorState(INTEGRATOR_STATE_ARRAY(shadow_state, shadow_isect, 2, prim));
362}
363# endif
364
365ccl_device_forceinline void integrator_state_write_mnee(IntegratorState state,
366 IntegratorShadowState shadow_state,
367 const ccl_private LightSample *ls,
368 const ccl_private Ray *ray,
369 const int mnee_vertex_count,
370 const Spectrum mnee_throughput,
371 const float3 mnee_wo)
372{
373 static_assert(INTEGRATOR_SHADOW_ISECT_SIZE >= 3);
374
375# ifdef __KERNEL_GPU__
376 INTEGRATOR_STATE_WRITE(state, path, mnee_shadow_state) = (int)shadow_state;
377 integrator_state_write_mnee_shadow_owner(shadow_state, state);
378# endif
379
380 /* Light sample. */
381 INTEGRATOR_STATE_ARRAY_WRITE(shadow_state, shadow_isect, 0, t) = ls->P.x;
382 INTEGRATOR_STATE_ARRAY_WRITE(shadow_state, shadow_isect, 0, u) = ls->P.y;
383 INTEGRATOR_STATE_ARRAY_WRITE(shadow_state, shadow_isect, 0, v) = ls->P.z;
384 /* When the integrate_surface_direct_light() reads the MNEE state it should read mnee_wo as the
385 * light sample direction. */
386 INTEGRATOR_STATE_ARRAY_WRITE(shadow_state, shadow_isect, 1, t) = mnee_wo.x;
387 INTEGRATOR_STATE_ARRAY_WRITE(shadow_state, shadow_isect, 1, u) = mnee_wo.y;
388 INTEGRATOR_STATE_ARRAY_WRITE(shadow_state, shadow_isect, 1, v) = mnee_wo.z;
389 INTEGRATOR_STATE_WRITE(shadow_state, shadow_ray, tmin) = ls->t;
390 INTEGRATOR_STATE_WRITE(shadow_state, shadow_ray, tmax) = ls->pdf;
391 INTEGRATOR_STATE_ARRAY_WRITE(shadow_state, shadow_isect, 2, t) = ls->eval_fac;
392 INTEGRATOR_STATE_WRITE(shadow_state, shadow_ray, self_light_object) = ls->object;
393 INTEGRATOR_STATE_WRITE(shadow_state, shadow_ray, self_light_prim) = ls->prim;
394 INTEGRATOR_STATE_ARRAY_WRITE(shadow_state, shadow_isect, 0, object) = ls->shader;
395 INTEGRATOR_STATE_ARRAY_WRITE(shadow_state, shadow_isect, 0, prim) = ls->group + 1;
396 INTEGRATOR_STATE_ARRAY_WRITE(shadow_state, shadow_isect, 0, type) = (int)ls->type;
397
398 /* Ray. */
399 INTEGRATOR_STATE_ARRAY_WRITE(shadow_state, shadow_isect, 1, prim) = mnee_vertex_count;
400 INTEGRATOR_STATE_WRITE(shadow_state, shadow_path, throughput) = mnee_throughput;
401 /* The ray direction becomes the original light sample's direction for the shadow ray tracing. */
402 INTEGRATOR_STATE_WRITE(shadow_state, shadow_ray, D) = ls->D;
403 INTEGRATOR_STATE_WRITE(shadow_state, shadow_ray, P) = ray->P;
404 INTEGRATOR_STATE_WRITE(shadow_state, shadow_ray, dP) = ray->dP;
405 INTEGRATOR_STATE_WRITE(shadow_state, shadow_ray, dD) = ray->dD;
406 INTEGRATOR_STATE_ARRAY_WRITE(shadow_state, shadow_isect, 1, object) = ray->self.object;
407 INTEGRATOR_STATE_ARRAY_WRITE(shadow_state, shadow_isect, 1, type) = ray->self.prim;
408
410}
411
412ccl_device_forceinline void integrator_state_read_mnee(ConstIntegratorState state,
414 ccl_private int *mnee_vertex_count)
415{
416 static_assert(INTEGRATOR_SHADOW_ISECT_SIZE >= 3);
417
418 ConstIntegratorShadowState shadow_state = integrator_state_get_mnee_shadow_state(state);
419
420 ls->P = make_float3(INTEGRATOR_STATE_ARRAY(shadow_state, shadow_isect, 0, t),
421 INTEGRATOR_STATE_ARRAY(shadow_state, shadow_isect, 0, u),
422 INTEGRATOR_STATE_ARRAY(shadow_state, shadow_isect, 0, v));
423 ls->D = make_float3(INTEGRATOR_STATE_ARRAY(shadow_state, shadow_isect, 1, t),
424 INTEGRATOR_STATE_ARRAY(shadow_state, shadow_isect, 1, u),
425 INTEGRATOR_STATE_ARRAY(shadow_state, shadow_isect, 1, v));
426 ls->t = INTEGRATOR_STATE(shadow_state, shadow_ray, tmin);
427 ls->pdf = INTEGRATOR_STATE(shadow_state, shadow_ray, tmax);
428 ls->eval_fac = INTEGRATOR_STATE_ARRAY(shadow_state, shadow_isect, 2, t);
429 ls->object = INTEGRATOR_STATE(shadow_state, shadow_ray, self_light_object);
430 ls->prim = INTEGRATOR_STATE(shadow_state, shadow_ray, self_light_prim);
431 ls->shader = INTEGRATOR_STATE_ARRAY(shadow_state, shadow_isect, 0, object);
432 ls->group = INTEGRATOR_STATE_ARRAY(shadow_state, shadow_isect, 0, prim) - 1;
433 ls->type = (LightType)INTEGRATOR_STATE_ARRAY(shadow_state, shadow_isect, 0, type);
434 ls->pdf_selection = 0.0f;
435 ls->emitter_id = EMITTER_NONE;
436
437 *mnee_vertex_count = INTEGRATOR_STATE_ARRAY(shadow_state, shadow_isect, 1, prim);
438}
439
440ccl_device_forceinline void integrator_state_read_mnee_ray(ConstIntegratorState state,
441 const ccl_private LightSample *ls,
442 ccl_private Ray *ray)
443{
444 static_assert(INTEGRATOR_SHADOW_ISECT_SIZE >= 2);
445
446 ConstIntegratorShadowState shadow_state = integrator_state_get_mnee_shadow_state(state);
447
448 ray->P = INTEGRATOR_STATE(shadow_state, shadow_ray, P);
449 if (ls->t == FLT_MAX) {
450 /* Distant light. */
451 ray->D = INTEGRATOR_STATE(shadow_state, shadow_ray, D);
452 ray->tmax = ls->t;
453 }
454 else {
455 /* Other lights. */
456 ray->D = ls->P - ray->P;
457 ray->D = safe_normalize_len(ray->D, &ray->tmax);
458 }
459 ray->tmin = ((ls->shader & SHADER_CAST_SHADOW) == 0) ? FLT_MAX : 0.0f;
460 ray->time = INTEGRATOR_STATE(state, ray, time);
461 ray->dP = INTEGRATOR_STATE(shadow_state, shadow_ray, dP);
462 ray->dD = INTEGRATOR_STATE(shadow_state, shadow_ray, dD);
463 ray->self.object = INTEGRATOR_STATE_ARRAY(shadow_state, shadow_isect, 1, object);
464 ray->self.prim = INTEGRATOR_STATE_ARRAY(shadow_state, shadow_isect, 1, type);
465 ray->self.light_object = ls->object;
466 ray->self.light_prim = ls->prim;
467}
468
469ccl_device_forceinline Spectrum integrator_state_read_mnee_throughput(ConstIntegratorState state)
470{
471 ConstIntegratorShadowState shadow_state = integrator_state_get_mnee_shadow_state(state);
472 return INTEGRATOR_STATE(shadow_state, shadow_path, throughput);
473}
474
475#endif /* __MNEE__ */
476
477#if defined(__KERNEL_GPU__)
478ccl_device_inline void integrator_state_copy_only(KernelGlobals kg,
479 ConstIntegratorState to_state,
481{
482 int index;
483
484 /* Rely on the compiler to optimize out unused assignments and `while(false)`'s. */
485
486# define KERNEL_STRUCT_BEGIN(name) \
487 index = 0; \
488 do {
489
490# define KERNEL_STRUCT_MEMBER(parent_struct, type, name, feature) \
491 if (kernel_integrator_state.parent_struct.name != nullptr) { \
492 kernel_integrator_state.parent_struct.name[to_state] = \
493 kernel_integrator_state.parent_struct.name[state]; \
494 }
495
496# ifdef __INTEGRATOR_GPU_PACKED_STATE__
497# define KERNEL_STRUCT_BEGIN_PACKED(parent_struct, feature) \
498 KERNEL_STRUCT_BEGIN(parent_struct) \
499 KERNEL_STRUCT_MEMBER(parent_struct, packed_##parent_struct, packed, feature)
500# define KERNEL_STRUCT_MEMBER_PACKED(parent_struct, type, name, feature)
501# else
502# define KERNEL_STRUCT_MEMBER_PACKED KERNEL_STRUCT_MEMBER
503# define KERNEL_STRUCT_BEGIN_PACKED(parent_struct, feature) KERNEL_STRUCT_BEGIN(parent_struct)
504# endif
505
506# define KERNEL_STRUCT_ARRAY_MEMBER(parent_struct, type, name, feature) \
507 if (kernel_integrator_state.parent_struct[index].name != nullptr) { \
508 kernel_integrator_state.parent_struct[index].name[to_state] = \
509 kernel_integrator_state.parent_struct[index].name[state]; \
510 }
511
512# define KERNEL_STRUCT_END(name) \
513 } \
514 while (false) \
515 ;
516
517# define KERNEL_STRUCT_END_ARRAY(name, cpu_array_size, gpu_array_size) \
518 ++index; \
519 } \
520 while (index < gpu_array_size) \
521 ;
522
523# define KERNEL_STRUCT_VOLUME_STACK_SIZE kernel_data.volume_stack_size
524
526
527# undef KERNEL_STRUCT_BEGIN
528# undef KERNEL_STRUCT_BEGIN_PACKED
529# undef KERNEL_STRUCT_MEMBER
530# undef KERNEL_STRUCT_MEMBER_PACKED
531# undef KERNEL_STRUCT_ARRAY_MEMBER
532# undef KERNEL_STRUCT_END
533# undef KERNEL_STRUCT_END_ARRAY
534# undef KERNEL_STRUCT_VOLUME_STACK_SIZE
535}
536
537ccl_device_inline void integrator_state_move(KernelGlobals kg,
538 ConstIntegratorState to_state,
540{
541 integrator_state_copy_only(kg, to_state, state);
542
543 INTEGRATOR_STATE_WRITE(state, path, queued_kernel) = 0;
544
545# ifdef __MNEE__
546 if (INTEGRATOR_STATE(to_state, path, mnee) & PATH_MNEE_SAMPLED) {
547 const IntegratorShadowState slot = INTEGRATOR_STATE(to_state, path, mnee_shadow_state);
548 integrator_state_write_mnee_shadow_owner(slot, to_state);
549 }
550# endif
551}
552
553ccl_device_inline void integrator_shadow_state_copy_only(KernelGlobals kg,
556{
557 int index;
558
559 /* Rely on the compiler to optimize out unused assignments and `while(false)`'s. */
560
561# define KERNEL_STRUCT_BEGIN(name) \
562 index = 0; \
563 do {
564
565# define KERNEL_STRUCT_MEMBER(parent_struct, type, name, feature) \
566 if (kernel_integrator_state.parent_struct.name != nullptr) { \
567 kernel_integrator_state.parent_struct.name[to_state] = \
568 kernel_integrator_state.parent_struct.name[state]; \
569 }
570
571# ifdef __INTEGRATOR_GPU_PACKED_STATE__
572# define KERNEL_STRUCT_BEGIN_PACKED(parent_struct, feature) \
573 KERNEL_STRUCT_BEGIN(parent_struct) \
574 KERNEL_STRUCT_MEMBER(parent_struct, type, packed, feature)
575# define KERNEL_STRUCT_MEMBER_PACKED(parent_struct, type, name, feature)
576# else
577# define KERNEL_STRUCT_MEMBER_PACKED KERNEL_STRUCT_MEMBER
578# define KERNEL_STRUCT_BEGIN_PACKED(parent_struct, feature) KERNEL_STRUCT_BEGIN(parent_struct)
579# endif
580
581# define KERNEL_STRUCT_ARRAY_MEMBER(parent_struct, type, name, feature) \
582 if (kernel_integrator_state.parent_struct[index].name != nullptr) { \
583 kernel_integrator_state.parent_struct[index].name[to_state] = \
584 kernel_integrator_state.parent_struct[index].name[state]; \
585 }
586
587# define KERNEL_STRUCT_END(name) \
588 } \
589 while (false) \
590 ;
591
592# define KERNEL_STRUCT_END_ARRAY(name, cpu_array_size, gpu_array_size) \
593 ++index; \
594 } \
595 while (index < gpu_array_size) \
596 ;
597
598# define KERNEL_STRUCT_VOLUME_STACK_SIZE kernel_data.volume_stack_size
599
601
602# undef KERNEL_STRUCT_BEGIN
603# undef KERNEL_STRUCT_BEGIN_PACKED
604# undef KERNEL_STRUCT_MEMBER
605# undef KERNEL_STRUCT_MEMBER_PACKED
606# undef KERNEL_STRUCT_ARRAY_MEMBER
607# undef KERNEL_STRUCT_END
608# undef KERNEL_STRUCT_END_ARRAY
609# undef KERNEL_STRUCT_VOLUME_STACK_SIZE
610}
611
612ccl_device_inline void integrator_shadow_state_move(KernelGlobals kg,
613 ConstIntegratorState to_state,
615{
616 integrator_shadow_state_copy_only(kg, to_state, state);
617
618 INTEGRATOR_STATE_WRITE(state, shadow_path, queued_kernel) = 0;
619
620# ifdef __MNEE__
621 if (INTEGRATOR_STATE(to_state, shadow_path, queued_kernel) ==
623 {
624 const IntegratorState main_state = integrator_state_read_mnee_shadow_owner(to_state);
625 INTEGRATOR_STATE_WRITE(main_state, path, mnee_shadow_state) = (int)to_state;
626 }
627# endif
628}
629
630#endif
631
632/* NOTE: Leaves kernel scheduling information untouched. Use INIT semantic for one of the paths
633 * after this function. */
636{
637#if defined(__KERNEL_GPU__)
639 &kernel_integrator_state.next_main_path_index[0], 1);
640
641 integrator_state_copy_only(kg, to_state, state);
642#else
643 IntegratorStateCPU *ccl_restrict to_state = state + 1;
644
645 /* Only copy the required subset for performance. */
646 to_state->path = state->path;
647 to_state->ray = state->ray;
648 to_state->isect = state->isect;
649# ifdef __VOLUME__
650 integrator_state_copy_volume_stack(kg, to_state, state);
651# endif
652#endif
653
654 return to_state;
655}
656
658 const uint32_t /*path_flag*/)
659{
660 return INTEGRATOR_STATE(state, path, bounce);
661}
662
664 const uint32_t /*path_flag*/)
665{
666 return INTEGRATOR_STATE(state, shadow_path, bounce);
667}
668
670 const uint32_t /*path_flag*/)
671{
672 return INTEGRATOR_STATE(state, path, diffuse_bounce);
673}
674
676 const uint32_t /*path_flag*/)
677{
678 return INTEGRATOR_STATE(state, shadow_path, diffuse_bounce);
679}
680
682 const uint32_t /*path_flag*/)
683{
684 return INTEGRATOR_STATE(state, path, glossy_bounce);
685}
686
688 const uint32_t /*path_flag*/)
689{
690 return INTEGRATOR_STATE(state, shadow_path, glossy_bounce);
691}
692
694 const uint32_t /*path_flag*/)
695{
696 return INTEGRATOR_STATE(state, path, transmission_bounce);
697}
698
700 const uint32_t /*path_flag*/)
701{
702 return INTEGRATOR_STATE(state, shadow_path, transmission_bounce);
703}
704
706 const uint32_t /*path_flag*/)
707{
708 return INTEGRATOR_STATE(state, path, transparent_bounce);
709}
710
712 const uint32_t /*path_flag*/)
713{
714 return INTEGRATOR_STATE(state, shadow_path, transparent_bounce);
715}
716
719 const uint32_t /*path_flag*/)
720{
721 return (kernel_data.kernel_features & KERNEL_FEATURE_NODE_PORTAL) ?
722 INTEGRATOR_STATE(state, path, portal_bounce) :
723 0;
724}
725
728 const uint32_t /*path_flag*/)
729{
730 return (kernel_data.kernel_features & KERNEL_FEATURE_NODE_PORTAL) ?
731 INTEGRATOR_STATE(state, shadow_path, portal_bounce) :
732 0;
733}
734
736{
737 return lcg_state_init(INTEGRATOR_STATE(state, shadow_path, rng_pixel),
738 INTEGRATOR_STATE(state, shadow_path, rng_offset),
739 INTEGRATOR_STATE(state, shadow_path, sample),
740 hash);
741}
742
744{
745 return lcg_state_init(INTEGRATOR_STATE(state, path, rng_pixel),
746 INTEGRATOR_STATE(state, path, rng_offset),
748 hash);
749}
750
752 const uint /*hash*/)
753{
754 return 0;
755}
756
#define D
ATOMIC_INLINE uint32_t atomic_fetch_and_add_uint32(uint32_t *p, uint32_t x)
#define KERNEL_FEATURE_VOLUME
#define KERNEL_FEATURE_NODE_PORTAL
#define kernel_data
#define ccl_restrict
#define ccl_device_forceinline
#define SHADER_NONE
#define INTEGRATOR_SHADOW_ISECT_SIZE
#define ccl_private
#define ccl_device_inline
#define EMITTER_NONE
#define CCL_NAMESPACE_END
#define kernel_integrator_state
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
#define offsetof(t, d)
#define FLT_MAX
#define index(i)
const int state
@ PATH_MNEE_SAMPLED
@ SHADER_CAST_SHADOW
LightType
@ DEVICE_KERNEL_INTEGRATOR_SHADOW_PATH_MNEE_PENDING
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 float3 safe_normalize_len(const float3 a, ccl_private float *t)
#define hash
Definition noise_c.cc:156
IntegratorShadowStateCPU * IntegratorShadowState
Definition state.h:229
IntegratorBakeState ConstIntegratorBakeState
Definition state.h:232
#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
#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_inline int integrator_state_transmission_bounce(ConstIntegratorState state, const uint32_t)
Definition state_util.h:693
ccl_device_forceinline void integrator_state_read_shadow_ray(ConstIntegratorShadowState state, ccl_private Ray *ccl_restrict ray)
Definition state_util.h:86
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_inline int integrator_state_diffuse_bounce(ConstIntegratorState state, const uint32_t)
Definition state_util.h:669
ccl_device_forceinline void integrator_state_write_shadow_isect(IntegratorShadowState state, const ccl_private Intersection *ccl_restrict isect, const int index)
Definition state_util.h:284
ccl_device_inline int integrator_state_bounce(ConstIntegratorState state, const uint32_t)
Definition state_util.h:657
ccl_device_forceinline void integrator_state_read_ray(ConstIntegratorState state, ccl_private Ray *ccl_restrict ray)
Definition state_util.h:49
ccl_device_inline int integrator_state_portal_bounce(KernelGlobals kg, ConstIntegratorState state, const uint32_t)
Definition state_util.h:717
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_inline IntegratorState integrator_state_shadow_catcher_split(KernelGlobals kg, IntegratorState state)
Definition state_util.h:634
ccl_device_forceinline void integrator_state_read_shadow_isect(ConstIntegratorShadowState state, ccl_private Intersection *ccl_restrict isect, const int index)
Definition state_util.h:297
ccl_device_forceinline void integrator_state_read_isect(ConstIntegratorState state, ccl_private Intersection *ccl_restrict isect)
Definition state_util.h:179
ccl_device_inline uint integrator_state_lcg_init(ConstIntegratorShadowState state, const uint hash)
Definition state_util.h:735
ccl_device_inline int integrator_state_glossy_bounce(ConstIntegratorState state, const uint32_t)
Definition state_util.h:681
ccl_device_inline int integrator_state_transparent_bounce(ConstIntegratorState state, const uint32_t)
Definition state_util.h:705
CCL_NAMESPACE_BEGIN ccl_device_forceinline void integrator_state_write_ray(IntegratorState state, const ccl_private Ray *ccl_restrict ray)
Definition state_util.h:25
ccl_device_forceinline void integrator_state_write_isect(IntegratorState state, const ccl_private Intersection *ccl_restrict isect)
Definition state_util.h:149
ccl_device_forceinline void integrator_state_read_shadow_ray_self(ConstIntegratorShadowState state, ccl_private Ray *ccl_restrict ray)
Definition state_util.h:136
float z
Definition sky_math.h:136
float y
Definition sky_math.h:136
float x
Definition sky_math.h:136
float3 Spectrum