Blender V5.3
geom_intersect.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5/* Common utilities for various geometry type intersections. */
6
7#pragma once
8
9#include "kernel/globals.h"
10#include "kernel/sample/lcg.h"
11
13
14/* For an intersection with the given distance isect_t from the ray origin, increase the number
15 * of hits (when needed) and return an index within local_isect->hits where the intersection is to
16 * be stored. If the return value -1, then the intersection is to be ignored (nothing is to be
17 * written to the local_isect->hits, and the intersection test function is to return false.
18 *
19 * The LocalIntersection is a templated type, allowing different types that implement similar data
20 * layout to be passed here. This is needed for the MetalRT, where it is not possible to access the
21 * pointer to actual LocalIntersection from the git function. */
22#ifdef __BVH_LOCAL__
23template<class LocalIntersection>
24ccl_device_forceinline int local_intersect_get_record_index(
25 ccl_private LocalIntersection *local_isect,
26 const float isect_t,
27 ccl_private uint *lcg_state,
28 const int max_hits)
29{
30 if (lcg_state) {
31 /* Record up to max_hits intersections. */
32 for (int i = min(max_hits, int(local_isect->num_hits)) - 1; i >= 0; --i) {
33 if (local_isect->hits[i].t == isect_t) {
34 return -1;
35 }
36 }
37
38 local_isect->num_hits++;
39
40 int hit;
41 if (local_isect->num_hits <= max_hits) {
42 hit = local_isect->num_hits - 1;
43 }
44 else {
45 /* Reservoir sampling: if we are at the maximum number of hits, randomly replace element or
46 * skip it. */
47 hit = lcg_step_uint(lcg_state) % local_isect->num_hits;
48 if (hit >= max_hits) {
49 return -1;
50 }
51 }
52 return hit;
53 }
54
55 /* Record closest intersection only. */
56 if (local_isect->num_hits && isect_t > local_isect->hits[0].t) {
57 return -1;
58 }
59 local_isect->num_hits = 1;
60 return 0;
61}
62#endif /* __BVH_LOCAL__ */
63
#define ccl_device_forceinline
#define ccl_private
#define CCL_NAMESPACE_END
CCL_NAMESPACE_BEGIN ccl_device uint lcg_step_uint(T rng)
Definition lcg.h:14
#define min(a, b)
Definition sort.cc:38