Blender V5.3
kernel/device/cpu/bvh.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2021-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5/* CPU Embree implementation of ray-scene intersection. */
6
7#pragma once
8
9#include <embree4/rtcore_geometry.h>
10#include <embree4/rtcore_ray.h>
11#include <embree4/rtcore_scene.h>
12
13#ifdef __KERNEL_ONEAPI__
16#else
19#endif
20
22#include "kernel/bvh/types.h"
23#include "kernel/bvh/util.h"
25#include "kernel/geom/object.h"
28#include "kernel/sample/lcg.h"
29
31
32#ifdef __KERNEL_ONEAPI__
33using numhit_t = uint16_t;
34#else
35using numhit_t = uint32_t;
36#endif
37
38/* Before Embree 4.4, the so-called Traversable functionality was exposed through Scene API.
39 * So, in order to simplify code between different versions, we are defining the traversable class
40 * and calls for older Embree versions as well. */
41#if RTC_VERSION < 40400
42# define RTCTraversable RTCScene
43# define rtcGetGeometryUserDataFromTraversable rtcGetGeometryUserDataFromScene
44# define rtcTraversableIntersect1 rtcIntersect1
45# define rtcTraversableOccluded1 rtcOccluded1
46#endif
47
48#ifdef __KERNEL_ONEAPI__
49# define CYCLES_EMBREE_USED_FEATURES \
50 (kernel_handler.get_specialization_constant<oneapi_embree_features>())
51#else
52# define CYCLES_EMBREE_USED_FEATURES \
53 (RTCFeatureFlags)(RTC_FEATURE_FLAG_TRIANGLE | RTC_FEATURE_FLAG_INSTANCE | \
54 RTC_FEATURE_FLAG_FILTER_FUNCTION_IN_ARGUMENTS | RTC_FEATURE_FLAG_POINT | \
55 RTC_FEATURE_FLAG_MOTION_BLUR | RTC_FEATURE_FLAG_ROUND_CATMULL_ROM_CURVE | \
56 RTC_FEATURE_FLAG_FLAT_CATMULL_ROM_CURVE | \
57 RTC_FEATURE_FLAG_ROUND_LINEAR_CURVE | \
58 RTC_FEATURE_FLAG_USER_GEOMETRY_CALLBACK_IN_ARGUMENTS)
59#endif
60
61#define EMBREE_IS_HAIR(x) (x & 1)
62
63/* Intersection context. */
64
65struct CCLFirstHitContext : public RTCRayQueryContext {
67 /* For avoiding self intersections */
68 const Ray *ray;
69};
70
71struct CCLShadowContext : public RTCRayQueryContext {
72#if defined(__KERNEL_ONEAPI__)
73 ONEAPIKernelContext *oneapi_kernel_context;
74#else
76#endif
77
78 BVHShadowAllPayload *payload;
79};
80
90
91struct CCLVolumeContext : public RTCRayQueryContext {
93 const Ray *ray;
94#ifdef __VOLUME_RECORD_ALL__
95 numhit_t max_hits;
96#endif
99};
100
101/* Utilities. */
102
104 RTCRay &rtc_ray,
105 const uint visibility)
106{
107 rtc_ray.org_x = ray.P.x;
108 rtc_ray.org_y = ray.P.y;
109 rtc_ray.org_z = ray.P.z;
110 rtc_ray.dir_x = ray.D.x;
111 rtc_ray.dir_y = ray.D.y;
112 rtc_ray.dir_z = ray.D.z;
113 rtc_ray.tnear = ray.tmin;
114 rtc_ray.tfar = ray.tmax;
115 rtc_ray.time = ray.time;
116 rtc_ray.mask = visibility;
117}
118
120 RTCRayHit &rayhit,
121 const uint visibility)
122{
123 kernel_embree_setup_ray(ray, rayhit.ray, visibility);
124 rayhit.hit.geomID = RTC_INVALID_GEOMETRY_ID;
125 rayhit.hit.instID[0] = RTC_INVALID_GEOMETRY_ID;
126}
127
129{
130 return (hit->instID[0] != RTC_INVALID_GEOMETRY_ID ? hit->instID[0] : hit->geomID) / 2;
131}
132
134 const RTCHit *hit,
135 const Ray *ray)
136{
137 const int object = kernel_embree_get_hit_object(hit);
138
139 int prim;
140 if ((ray->self.object == object) || (ray->self.light_object == object)) {
141 const int prim_offset = kernel_data_fetch(object_prim_offset, object);
142 prim = hit->primID + prim_offset;
143 }
144 else {
145 return false;
146 }
147
148 const bool is_hair = hit->geomID & 1;
149 if (is_hair) {
150 prim = kernel_data_fetch(curve_segments, prim).prim;
151 }
152
153 return intersection_skip_self_shadow(ray->self, object, prim);
154}
155
157 const RTCRay *ray,
158 const RTCHit *hit,
159 Intersection *isect)
160{
161 const int object = kernel_embree_get_hit_object(hit);
162 const int prim_offset = kernel_data_fetch(object_prim_offset, object);
163
164 isect->t = ray->tfar;
165 isect->prim = hit->primID + prim_offset;
166 isect->object = object;
167
168 const bool is_hair = hit->geomID & 1;
169 if (is_hair) {
170 const KernelCurveSegment segment = kernel_data_fetch(curve_segments, isect->prim);
171 isect->type = segment.type;
172 isect->prim = segment.prim;
173 isect->u = hit->u;
174 isect->v = hit->v;
175 }
176 else {
177 isect->type = kernel_data_fetch(objects, isect->object).primitive_type;
178 isect->u = hit->u;
179 isect->v = hit->v;
180 }
181}
182
184 KernelGlobals kg, const RTCRay *ray, const RTCHit *hit, Intersection *isect, const int object)
185{
186 const int prim_offset = kernel_data_fetch(object_prim_offset, object);
187
188 isect->u = hit->u;
189 isect->v = hit->v;
190 isect->t = ray->tfar;
191 isect->prim = hit->primID + prim_offset;
192 isect->object = object;
193 isect->type = kernel_data_fetch(objects, object).primitive_type;
194}
195
196/* Ray filter functions. */
197
198/* This gets called by Embree at every valid ray/object intersection.
199 * Things like recording subsurface or shadow hits for later evaluation
200 * as well as filtering for volume objects happen here.
201 * Cycles' own BVH does that directly inside the traversal calls. */
203 const RTCFilterFunctionNArguments *args)
204{
205 /* Current implementation in Cycles assumes only single-ray intersection queries. */
206 kernel_assert(args->N == 1);
207
208 RTCHit *hit = (RTCHit *)args->hit;
209 CCLFirstHitContext *ctx = (CCLFirstHitContext *)(args->context);
210#ifdef __KERNEL_ONEAPI__
211 KernelGlobalsGPU *kg = nullptr;
212#else
213 const ThreadKernelGlobalsCPU *kg = ctx->kg;
214#endif
215 const Ray *cray = ctx->ray;
216
217 if (kernel_embree_is_self_intersection(kg, hit, cray)) {
218 *args->valid = 0;
219 return;
220 }
221
222#ifdef __SHADOW_LINKING__
224 *args->valid = 0;
225 return;
226 }
227#endif
228}
229
230ccl_device_forceinline int custom_intersect_get_object(const RTCRayQueryContext &ctx,
231 unsigned int geom_id)
232{
233 return ((ctx.instID[0] != RTC_INVALID_GEOMETRY_ID) ? ctx.instID[0] : geom_id) / 2;
234}
235
237 const RTCIntersectFunctionNArguments *args)
238{
239 kernel_assert(args->N == 1);
240 static_assert(RTC_MAX_INSTANCE_LEVEL_COUNT == 1);
241
242#if defined(__GSPLATS__)
243 CCLFirstHitContext *ctx = (CCLFirstHitContext *)(args->context);
244 RTCRayHit *ray_hit = (RTCRayHit *)args->rayhit;
245
246# ifdef __KERNEL_ONEAPI__
247 KernelGlobalsGPU *kg = nullptr;
248# else
249 const ThreadKernelGlobalsCPU *kg = ctx->kg;
250# endif
251
252 if (!args->valid[0]) {
253 return;
254 }
255
256 const float3 ray_P = make_float3(ray_hit->ray.org_x, ray_hit->ray.org_y, ray_hit->ray.org_z);
257 const float3 ray_D = make_float3(ray_hit->ray.dir_x, ray_hit->ray.dir_y, ray_hit->ray.dir_z);
258
259 const uint object = custom_intersect_get_object(*ctx, args->geomID);
260 const int prim_offset = kernel_data_fetch(object_prim_offset, object);
261 const int prim = args->primID + prim_offset;
262
263 /* Currently only gaussian splats are handled via the custom geometry. */
264 const int primitive_type = kernel_data_fetch(objects, object).primitive_type;
265 kernel_assert(primitive_type & PRIMITIVE_GSPLAT);
266
267 float isect_t;
268 if (!gsplat_intersect_test(kg,
269 ray_P,
270 ray_D,
271 ray_hit->ray.tnear,
272 ray_hit->ray.tfar,
273 object,
274 prim,
275 ray_hit->ray.time,
276 primitive_type,
277 isect_t))
278 {
279 args->valid[0] = 0;
280 return;
281 }
282
283 RTCHit potential_hit;
284 potential_hit.u = 0.0f;
285 potential_hit.v = 0.0f;
286 potential_hit.primID = args->primID;
287 potential_hit.geomID = args->geomID;
288 potential_hit.instID[0] = ctx->instID[0];
289
290 const float old_tfar = ray_hit->ray.tfar;
291 ray_hit->ray.tfar = isect_t;
292
293 RTCFilterFunctionNArguments filter_args;
294 int valid = -1;
295 filter_args.valid = &valid;
296 filter_args.geometryUserPtr = args->geometryUserPtr;
297 filter_args.context = args->context;
298 filter_args.ray = (RTCRayN *)&ray_hit->ray;
299 filter_args.hit = (RTCHitN *)&potential_hit;
300 filter_args.N = 1;
302 if (!valid) {
303 ray_hit->ray.tfar = old_tfar;
304 args->valid[0] = 0;
305 return;
306 }
307
308 ray_hit->hit = potential_hit;
309 args->valid[0] = -1;
310#else
311 args->valid[0] = 0;
312#endif
313}
314
316 const RTCOccludedFunctionNArguments *args)
317{
318 kernel_assert(args->N == 1);
319 static_assert(RTC_MAX_INSTANCE_LEVEL_COUNT == 1);
320
321#if defined(__GSPLATS__)
322 if (!args->valid[0]) {
323 return;
324 }
325
326 CCLShadowContext *ctx = (CCLShadowContext *)(args->context);
327 RTCRay *ray = (RTCRay *)args->ray;
328 BVHShadowAllPayload &payload = *ctx->payload;
329
330# ifdef __KERNEL_ONEAPI__
331 KernelGlobalsGPU *kg = nullptr;
332# else
333 const ThreadKernelGlobalsCPU *kg = ctx->kg;
334# endif
335
336 const float3 ray_P = make_float3(ray->org_x, ray->org_y, ray->org_z);
337 const float3 ray_D = make_float3(ray->dir_x, ray->dir_y, ray->dir_z);
338
339 const uint object = custom_intersect_get_object(*ctx, args->geomID);
340 const int prim_offset = kernel_data_fetch(object_prim_offset, object);
341 const int prim = args->primID + prim_offset;
342
343 /* Currently only gaussian splats are handled via the custom geometry. */
344 const int primitive_type = kernel_data_fetch(objects, object).primitive_type;
345 kernel_assert(primitive_type & PRIMITIVE_GSPLAT);
346
347 Intersection isect;
348 if (!gsplat_intersect(kg,
349 &isect,
350 ray_P,
351 ray_D,
352 ray->tnear,
353 ray->tfar,
354 object,
355 prim,
356 ray->time,
357 primitive_type))
358 {
359 args->valid[0] = 0;
360 return;
361 }
362
364 kg, payload.state, payload, payload.base.ray_self, 0, isect))
365 {
366 args->valid[0] = 0;
367 return;
368 }
369
370 payload.throughput = zero_float3();
371
372 ray->tfar = -FLT_MAX;
373 args->valid[0] = -1;
374#else
375 args->valid[0] = 0;
376#endif
377}
378
380 const RTCOccludedFunctionNArguments *args)
381{
382 kernel_assert(args->N == 1);
383
384 /* Ignore intersections with custom primitives.
385 * Currently the only custom primitive is the Gaussian splat, which requires extra design work
386 * to integrate into local intersection test. */
387 args->valid[0] = 0;
388}
389
391 const RTCOccludedFunctionNArguments *args)
392{
393 kernel_assert(args->N == 1);
394
395 /* Ignore intersections with custom primitives.
396 * Currently the only custom primitive is the Gaussian splat, which does not support volume
397 * shaders. */
398 args->valid[0] = 0;
399}
400
401/* This gets called by Embree at every valid ray/object intersection.
402 * Things like recording subsurface or shadow hits for later evaluation
403 * as well as filtering for volume objects happen here.
404 * Cycles' own BVH does that directly inside the traversal calls.
405 */
407 const RTCFilterFunctionNArguments *args)
408{
409 /* Current implementation in Cycles assumes only single-ray intersection queries. */
410 kernel_assert(args->N == 1);
411
412 const RTCRay *ray = (RTCRay *)args->ray;
413 const RTCHit *hit = (RTCHit *)args->hit;
414
415 CCLShadowContext *ctx = (CCLShadowContext *)(args->context);
416 BVHShadowAllPayload &payload = *ctx->payload;
417
418#ifdef __KERNEL_ONEAPI__
419 KernelGlobalsGPU *kg = nullptr;
420#else
421 const ThreadKernelGlobalsCPU *kg = ctx->kg;
422#endif
423
424 Intersection isect;
425 kernel_embree_convert_hit(kg, ray, hit, &isect);
426
428 kg, payload.state, payload, payload.base.ray_self, 0, isect))
429 {
430 return;
431 }
432
433 *args->valid = 0;
434}
435
437 const RTCFilterFunctionNArguments *args)
438{
439 /* Current implementation in Cycles assumes only single-ray intersection queries. */
440 kernel_assert(args->N == 1);
441
442 const RTCRay *ray = (RTCRay *)args->ray;
443 RTCHit *hit = (RTCHit *)args->hit;
444 CCLLocalContext *ctx = (CCLLocalContext *)(args->context);
445#ifdef __KERNEL_ONEAPI__
446 KernelGlobalsGPU *kg = nullptr;
447#else
448 const ThreadKernelGlobalsCPU *kg = ctx->kg;
449#endif
450 const Ray *cray = ctx->ray;
451
452 /* Check if it's hitting the correct object. */
453 Intersection current_isect;
454 if (ctx->is_sss) {
455 kernel_embree_convert_sss_hit(kg, ray, hit, &current_isect, ctx->local_object_id);
456 }
457 else {
458 kernel_embree_convert_hit(kg, ray, hit, &current_isect);
459 if (ctx->local_object_id != current_isect.object) {
460 /* This tells Embree to continue tracing. */
461 *args->valid = 0;
462 return;
463 }
464 }
465 if (intersection_skip_self_local(cray->self, current_isect.prim)) {
466 *args->valid = 0;
467 return;
468 }
469
470 /* No intersection information requested, just return a hit. */
471 if (ctx->max_hits == 0) {
472 return;
473 }
474
475 /* Ignore curves. */
476 if (EMBREE_IS_HAIR(hit->geomID)) {
477 /* This tells Embree to continue tracing. */
478 *args->valid = 0;
479 return;
480 }
481
482 LocalIntersection *local_isect = ctx->local_isect;
483 int hit_idx = 0;
484
485 if (ctx->lcg_state) {
486 /* See triangle_intersect_subsurface() for the native equivalent. */
487 for (int i = min((int)ctx->max_hits, local_isect->num_hits) - 1; i >= 0; --i) {
488 if (local_isect->hits[i].t == ray->tfar) {
489 /* This tells Embree to continue tracing. */
490 *args->valid = 0;
491 return;
492 }
493 }
494
495 local_isect->num_hits++;
496
497 if (local_isect->num_hits <= ctx->max_hits) {
498 hit_idx = local_isect->num_hits - 1;
499 }
500 else {
501 /* reservoir sampling: if we are at the maximum number of
502 * hits, randomly replace element or skip it */
503 hit_idx = lcg_step_uint(ctx->lcg_state) % local_isect->num_hits;
504
505 if (hit_idx >= ctx->max_hits) {
506 /* This tells Embree to continue tracing. */
507 *args->valid = 0;
508 return;
509 }
510 }
511 }
512 else {
513 /* Record closest intersection only. */
514 if (local_isect->num_hits && current_isect.t > local_isect->hits[0].t) {
515 *args->valid = 0;
516 return;
517 }
518
519 local_isect->num_hits = 1;
520 }
521
522 /* record intersection */
523 local_isect->hits[hit_idx] = current_isect;
524 local_isect->Ng[hit_idx] = normalize(make_float3(hit->Ng_x, hit->Ng_y, hit->Ng_z));
525 /* This tells Embree to continue tracing. */
526 *args->valid = 0;
527}
528
530 const RTCFilterFunctionNArguments *args)
531{
532 /* Current implementation in Cycles assumes only single-ray intersection queries. */
533 kernel_assert(args->N == 1);
534
535 const RTCRay *ray = (RTCRay *)args->ray;
536 RTCHit *hit = (RTCHit *)args->hit;
537 CCLVolumeContext *ctx = (CCLVolumeContext *)(args->context);
538#ifdef __KERNEL_ONEAPI__
539 KernelGlobalsGPU *kg = nullptr;
540#else
541 const ThreadKernelGlobalsCPU *kg = ctx->kg;
542#endif
543 const Ray *cray = ctx->ray;
544
545#ifdef __VOLUME_RECORD_ALL__
546 /* Append the intersection to the end of the array. */
547 if (ctx->num_hits < ctx->max_hits) {
548#endif
549 Intersection current_isect;
550 kernel_embree_convert_hit(kg, ray, hit, &current_isect);
551
553 kg, current_isect.object, current_isect.prim, cray->self, 0))
554 {
555 *args->valid = 0;
556 return;
557 }
558
559 Intersection *isect = &ctx->vol_isect[ctx->num_hits];
560 ++ctx->num_hits;
561 *isect = current_isect;
562#ifdef __VOLUME_RECORD_ALL__
563 /* This tells Embree to continue tracing. */
564 *args->valid = 0;
565 }
566#endif
567}
568
569#ifdef __KERNEL_ONEAPI__
570/* Static wrappers so we can call the callbacks from out side the ONEAPIKernelContext class */
571RTC_SYCL_INDIRECTLY_CALLABLE static void ccl_always_inline
572kernel_embree_filter_intersection_func_static(const RTCFilterFunctionNArguments *args)
573{
574 RTCHit *hit = (RTCHit *)args->hit;
575 CCLFirstHitContext *ctx = (CCLFirstHitContext *)(args->context);
576 ONEAPIKernelContext *context = static_cast<ONEAPIKernelContext *>(ctx->kg);
577 context->kernel_embree_filter_intersection_func_impl(args);
578}
579
580RTC_SYCL_INDIRECTLY_CALLABLE static void ccl_always_inline
581kernel_embree_filter_occluded_shadow_all_func_static(const RTCFilterFunctionNArguments *args)
582{
583 RTCHit *hit = (RTCHit *)args->hit;
584 CCLShadowContext *ctx = (CCLShadowContext *)(args->context);
585 ONEAPIKernelContext *context = ctx->oneapi_kernel_context;
586 context->kernel_embree_filter_occluded_shadow_all_func_impl(args);
587}
588
589RTC_SYCL_INDIRECTLY_CALLABLE static void ccl_always_inline
590kernel_embree_filter_occluded_local_func_static(const RTCFilterFunctionNArguments *args)
591{
592 RTCHit *hit = (RTCHit *)args->hit;
593 CCLLocalContext *ctx = (CCLLocalContext *)(args->context);
594 ONEAPIKernelContext *context = static_cast<ONEAPIKernelContext *>(ctx->kg);
595 context->kernel_embree_filter_occluded_local_func_impl(args);
596}
597
598RTC_SYCL_INDIRECTLY_CALLABLE static void ccl_always_inline
599kernel_embree_filter_occluded_volume_all_func_static(const RTCFilterFunctionNArguments *args)
600{
601 RTCHit *hit = (RTCHit *)args->hit;
602 CCLVolumeContext *ctx = (CCLVolumeContext *)(args->context);
603 ONEAPIKernelContext *context = static_cast<ONEAPIKernelContext *>(ctx->kg);
604 context->kernel_embree_filter_occluded_volume_all_func_impl(args);
605}
606
607RTC_SYCL_INDIRECTLY_CALLABLE static void ccl_always_inline
608kernel_embree_custom_intersection_func_static(const RTCIntersectFunctionNArguments *args)
609{
610 CCLFirstHitContext *ctx = (CCLFirstHitContext *)(args->context);
611 ONEAPIKernelContext *context = static_cast<ONEAPIKernelContext *>(ctx->kg);
612 context->kernel_embree_custom_intersection_func_impl(args);
613}
614
615RTC_SYCL_INDIRECTLY_CALLABLE static void ccl_always_inline
616kernel_embree_custom_occluded_func_static(const RTCOccludedFunctionNArguments *args)
617{
618 CCLShadowContext *ctx = (CCLShadowContext *)(args->context);
619 ONEAPIKernelContext *context = ctx->oneapi_kernel_context;
620 context->kernel_embree_custom_occluded_func_impl(args);
621}
622
623RTC_SYCL_INDIRECTLY_CALLABLE static void ccl_always_inline
624kernel_embree_custom_occluded_local_func_static(const RTCOccludedFunctionNArguments *args)
625{
626 CCLLocalContext *ctx = (CCLLocalContext *)(args->context);
627 ONEAPIKernelContext *context = static_cast<ONEAPIKernelContext *>(ctx->kg);
628 context->kernel_embree_custom_occluded_local_func_impl(args);
629}
630
631RTC_SYCL_INDIRECTLY_CALLABLE static void ccl_always_inline
632kernel_embree_custom_occluded_volume_func_static(const RTCOccludedFunctionNArguments *args)
633{
634 CCLVolumeContext *ctx = (CCLVolumeContext *)(args->context);
635 ONEAPIKernelContext *context = static_cast<ONEAPIKernelContext *>(ctx->kg);
636 context->kernel_embree_custom_occluded_volume_func_impl(args);
637}
638
639# define kernel_embree_filter_intersection_func \
640 ONEAPIKernelContext::kernel_embree_filter_intersection_func_static
641# define kernel_embree_filter_occluded_shadow_all_func \
642 ONEAPIKernelContext::kernel_embree_filter_occluded_shadow_all_func_static
643# define kernel_embree_filter_occluded_local_func \
644 ONEAPIKernelContext::kernel_embree_filter_occluded_local_func_static
645# define kernel_embree_filter_occluded_volume_all_func \
646 ONEAPIKernelContext::kernel_embree_filter_occluded_volume_all_func_static
647# define kernel_embree_custom_intersection_func kernel_embree_custom_intersection_func_static
648# define kernel_embree_custom_occluded_func kernel_embree_custom_occluded_func_static
649# define kernel_embree_custom_occluded_local_func kernel_embree_custom_occluded_local_func_static
650# define kernel_embree_custom_occluded_volume_func \
651 kernel_embree_custom_occluded_volume_func_static
652#else
653# define kernel_embree_filter_intersection_func kernel_embree_filter_intersection_func_impl
654# define kernel_embree_filter_occluded_shadow_all_func \
655 kernel_embree_filter_occluded_shadow_all_func_impl
656# define kernel_embree_filter_occluded_local_func kernel_embree_filter_occluded_local_func_impl
657# define kernel_embree_filter_occluded_volume_all_func \
658 kernel_embree_filter_occluded_volume_all_func_impl
659# define kernel_embree_custom_intersection_func kernel_embree_custom_intersection_func_impl
660# define kernel_embree_custom_occluded_func kernel_embree_custom_occluded_func_impl
661# define kernel_embree_custom_occluded_local_func kernel_embree_custom_occluded_local_func_impl
662# define kernel_embree_custom_occluded_volume_func kernel_embree_custom_occluded_volume_func_impl
663#endif
664
665/* Scene intersection. */
666
668 const ccl_private Ray *ray,
669 const uint visibility,
671{
672 isect->t = ray->tmax;
674 rtcInitRayQueryContext(&ctx);
675#ifdef __KERNEL_ONEAPI__
676 /* NOTE(sirgienko): Cycles GPU back-ends passes nullptr to KernelGlobals and
677 * uses global device allocation (CUDA, Optix, HIP) or passes all needed data
678 * as a class context (Metal, oneAPI). So we need to pass this context here
679 * in order to have an access to it later in Embree filter functions on GPU. */
680 ctx.kg = (KernelGlobals)this;
681#else
682 ctx.kg = kg;
683#endif
684
685 RTCRayHit ray_hit;
686 ctx.ray = ray;
687 kernel_embree_setup_rayhit(*ray, ray_hit, visibility);
688
689 RTCIntersectArguments args;
690 rtcInitIntersectArguments(&args);
691 args.filter = reinterpret_cast<RTCFilterFunctionN>(kernel_embree_filter_intersection_func);
692 args.intersect = reinterpret_cast<RTCIntersectFunctionN>(kernel_embree_custom_intersection_func);
693 args.feature_mask = CYCLES_EMBREE_USED_FEATURES;
694 args.context = &ctx;
695 rtcTraversableIntersect1(kernel_data.device_bvh, &ray_hit, &args);
696 if (ray_hit.hit.geomID == RTC_INVALID_GEOMETRY_ID ||
697 ray_hit.hit.primID == RTC_INVALID_GEOMETRY_ID)
698 {
699 return false;
700 }
701
702 kernel_embree_convert_hit(kg, &ray_hit.ray, &ray_hit.hit, isect);
703 return true;
704}
705
706#ifdef __BVH_LOCAL__
707ccl_device_intersect bool kernel_embree_intersect_local(KernelGlobals kg,
708 const ccl_private Ray *ray,
709 ccl_private LocalIntersection *local_isect,
710 const int local_object,
711 ccl_private uint *lcg_state,
712 const int max_hits)
713{
714 const bool has_bvh = !(kernel_data_fetch(object_flag, local_object) &
716 CCLLocalContext ctx;
717 rtcInitRayQueryContext(&ctx);
718# ifdef __KERNEL_ONEAPI__
719 /* NOTE(sirgienko): Cycles GPU back-ends passes nullptr to KernelGlobals and
720 * uses global device allocation (CUDA, Optix, HIP) or passes all needed data
721 * as a class context (Metal, oneAPI). So we need to pass this context here
722 * in order to have an access to it later in Embree filter functions on GPU. */
723 ctx.kg = (KernelGlobals)this;
724# else
725 ctx.kg = kg;
726# endif
727 ctx.is_sss = has_bvh;
728 ctx.lcg_state = lcg_state;
729 ctx.max_hits = max_hits;
730 ctx.ray = ray;
731 ctx.local_isect = local_isect;
732 if (local_isect) {
733 local_isect->num_hits = 0;
734 }
735 ctx.local_object_id = local_object;
736 RTCRay rtc_ray;
738
739 RTCOccludedArguments args;
740 rtcInitOccludedArguments(&args);
741 args.occluded = reinterpret_cast<RTCOccludedFunctionN>(kernel_embree_custom_occluded_local_func);
742 args.filter = reinterpret_cast<RTCFilterFunctionN>(kernel_embree_filter_occluded_local_func);
743 args.feature_mask = CYCLES_EMBREE_USED_FEATURES;
744 args.context = &ctx;
745
746 /* If this object has its own BVH, use it. */
747 if (has_bvh) {
748 float3 P = ray->P;
749 float3 dir = ray->D;
750 float3 idir = ray->D;
751# ifdef __OBJECT_MOTION__
752 bvh_instance_motion_push(kg, local_object, ray, &P, &dir, &idir);
753# else
754 bvh_instance_push(kg, local_object, ray, &P, &dir, &idir);
755# endif
756
757 rtc_ray.org_x = P.x;
758 rtc_ray.org_y = P.y;
759 rtc_ray.org_z = P.z;
760 rtc_ray.dir_x = dir.x;
761 rtc_ray.dir_y = dir.y;
762 rtc_ray.dir_z = dir.z;
763 rtc_ray.tnear = ray->tmin;
764 rtc_ray.tfar = ray->tmax;
766 kernel_data.device_bvh, local_object * 2);
767 kernel_assert(scene);
768 if (scene) {
769 rtcTraversableOccluded1(scene, &rtc_ray, &args);
770 }
771 }
772 else {
773 rtcTraversableOccluded1(kernel_data.device_bvh, &rtc_ray, &args);
774 }
775
776 /* rtcOccluded1 sets tfar to -inf if a hit was found. */
777 return (local_isect && local_isect->num_hits > 0) || (rtc_ray.tfar < 0);
778}
779#endif
780
781#ifdef __TRANSPARENT_SHADOWS__
782ccl_device_intersect void kernel_embree_intersect_shadow_all(KernelGlobals kg,
783 const ccl_private Ray *ray,
784 BVHShadowAllPayload &payload)
785{
787 rtcInitRayQueryContext(&ctx);
788# if defined(__KERNEL_ONEAPI__)
789 ctx.oneapi_kernel_context = this;
790# else
791 ctx.kg = kg;
792# endif
793 ctx.payload = &payload;
794
795 RTCRay rtc_ray;
796 kernel_embree_setup_ray(*ray, rtc_ray, payload.base.ray_visibility);
797
798 RTCOccludedArguments args;
799 rtcInitOccludedArguments(&args);
800 args.filter = reinterpret_cast<RTCFilterFunctionN>(
802 args.occluded = reinterpret_cast<RTCOccludedFunctionN>(kernel_embree_custom_occluded_func);
803 args.feature_mask = CYCLES_EMBREE_USED_FEATURES;
804 args.context = &ctx;
805
806 rtcTraversableOccluded1(kernel_data.device_bvh, &rtc_ray, &args);
807}
808#endif
809
810#ifdef __VOLUME__
811
812ccl_device_intersect uint kernel_embree_intersect_volume(KernelGlobals kg,
813 const ccl_private Ray *ray,
816 const uint max_hits,
817# endif
818 const uint visibility)
819{
821 rtcInitRayQueryContext(&ctx);
822# ifdef __KERNEL_ONEAPI__
823 /* NOTE(sirgienko) Cycles GPU back-ends passes nullptr to KernelGlobals and
824 * uses global device allocation (CUDA, Optix, HIP) or passes all needed data
825 * as a class context (Metal, oneAPI). So we need to pass this context here
826 * in order to have an access to it later in Embree filter functions on GPU. */
827 ctx.kg = (KernelGlobals)this;
828# else
829 ctx.kg = kg;
830# endif
831 ctx.vol_isect = isect;
832# ifdef __VOLUME_RECORD_ALL__
833 ctx.max_hits = numhit_t(max_hits);
834# endif
835 ctx.num_hits = numhit_t(0);
836 ctx.ray = ray;
837 RTCRay rtc_ray;
838 kernel_embree_setup_ray(*ray, rtc_ray, visibility);
839 RTCOccludedArguments args;
840 rtcInitOccludedArguments(&args);
841 args.occluded = reinterpret_cast<RTCOccludedFunctionN>(
843 args.filter = reinterpret_cast<RTCFilterFunctionN>(
845 args.feature_mask = CYCLES_EMBREE_USED_FEATURES;
846 args.context = &ctx;
847 rtcTraversableOccluded1(kernel_data.device_bvh, &rtc_ray, &args);
848 return ctx.num_hits;
849}
850#endif
851
ccl_device_inline bool intersection_skip_shadow_link(KernelGlobals kg, const ccl_ray_data RaySelfPrimitives &self, const int isect_object)
ccl_device_inline bool intersection_skip_self_shadow(const ccl_ray_data RaySelfPrimitives &self, const int object, const int prim)
ccl_device_inline bool intersection_skip_self_local(const ccl_ray_data RaySelfPrimitives &self, const int prim)
#define kernel_assert(cond)
#define kernel_data
#define ccl_device_forceinline
#define kernel_data_fetch(name, index)
#define ccl_always_inline
#define __VOLUME_RECORD_ALL__
#define ccl_private
#define ccl_device_inline
#define CCL_NAMESPACE_END
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
#define FLT_MAX
VecBase< float, D > normalize(VecOp< float, D >) RET
ccl_device_forceinline bool bvh_shadow_all_anyhit_filter(KernelGlobals kg, IntegratorShadowState state, ccl_ray_data BVHShadowAllPayload &ccl_restrict payload, const ccl_ray_data RaySelfPrimitives &ccl_restrict ray_self, const uint ray_visibility, const Intersection isect)
ccl_device_forceinline bool bvh_volume_anyhit_triangle_filter(KernelGlobals kg, const int object, const int prim, const ccl_ray_data RaySelfPrimitives &ccl_restrict ray_self, const uint ray_visibility)
#define ccl_device_intersect
#define rtcTraversableOccluded1
uint32_t numhit_t
ccl_device_forceinline void kernel_embree_filter_occluded_local_func_impl(const RTCFilterFunctionNArguments *args)
#define CYCLES_EMBREE_USED_FEATURES
ccl_device_forceinline void kernel_embree_filter_occluded_volume_all_func_impl(const RTCFilterFunctionNArguments *args)
ccl_device_forceinline void kernel_embree_filter_occluded_shadow_all_func_impl(const RTCFilterFunctionNArguments *args)
ccl_device_forceinline void kernel_embree_custom_occluded_func_impl(const RTCOccludedFunctionNArguments *args)
ccl_device_intersect bool kernel_embree_intersect(KernelGlobals kg, const ccl_private Ray *ray, const uint visibility, ccl_private Intersection *isect)
ccl_device_inline void kernel_embree_setup_rayhit(const Ray &ray, RTCRayHit &rayhit, const uint visibility)
#define kernel_embree_filter_occluded_volume_all_func
ccl_device_inline bool kernel_embree_is_self_intersection(const KernelGlobals kg, const RTCHit *hit, const Ray *ray)
ccl_device_forceinline int custom_intersect_get_object(const RTCRayQueryContext &ctx, unsigned int geom_id)
ccl_device_forceinline void kernel_embree_custom_intersection_func_impl(const RTCIntersectFunctionNArguments *args)
#define kernel_embree_custom_occluded_func
#define kernel_embree_filter_intersection_func
#define rtcGetGeometryUserDataFromTraversable
ccl_device_forceinline void kernel_embree_custom_occluded_volume_func_impl(const RTCOccludedFunctionNArguments *args)
#define kernel_embree_custom_occluded_volume_func
#define kernel_embree_custom_intersection_func
ccl_device_inline int kernel_embree_get_hit_object(const RTCHit *hit)
#define EMBREE_IS_HAIR(x)
#define rtcTraversableIntersect1
#define kernel_embree_filter_occluded_local_func
ccl_device_forceinline void kernel_embree_custom_occluded_local_func_impl(const RTCOccludedFunctionNArguments *args)
ccl_device_inline void kernel_embree_convert_sss_hit(KernelGlobals kg, const RTCRay *ray, const RTCHit *hit, Intersection *isect, const int object)
#define kernel_embree_custom_occluded_local_func
ccl_device_forceinline void kernel_embree_filter_intersection_func_impl(const RTCFilterFunctionNArguments *args)
#define RTCTraversable
ccl_device_inline void kernel_embree_convert_hit(KernelGlobals kg, const RTCRay *ray, const RTCHit *hit, Intersection *isect)
#define kernel_embree_filter_occluded_shadow_all_func
ccl_device_inline void kernel_embree_setup_ray(const Ray &ray, RTCRay &rtc_ray, const uint visibility)
ccl_device_inline void bvh_instance_push(KernelGlobals kg, const int object, const ccl_private Ray *ray, ccl_private float3 *P, ccl_private float3 *dir, ccl_private float3 *idir)
@ PRIMITIVE_GSPLAT
@ PATH_RAY_VISIBILITY_OBJECT_ALL
@ SD_OBJECT_TRANSFORM_APPLIED
CCL_NAMESPACE_BEGIN ccl_device uint lcg_step_uint(T rng)
Definition lcg.h:14
CCL_NAMESPACE_BEGIN ccl_device_inline float3 zero_float3()
Definition math_float3.h:17
int context(const bContext *C, const char *member, bContextDataResult *result)
unsigned int uint
Definition BKE_text.h:55
unsigned short uint16_t
Definition BKE_text.h:80
#define min(a, b)
Definition sort.cc:38
LocalIntersection * local_isect
BVHShadowAllPayload * payload
Intersection hits[LOCAL_MAX_HITS]
float3 Ng[LOCAL_MAX_HITS]
float tmax
float tmin
float3 P
float time
RaySelfPrimitives self
float3 D
float z
Definition sky_math.h:136
float y
Definition sky_math.h:136
float x
Definition sky_math.h:136