Blender V5.0
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 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 intersection test function is to return false. */
18#ifdef __BVH_LOCAL__
19ccl_device_forceinline int local_intersect_get_record_index(
20 ccl_private LocalIntersection *local_isect,
21 const float isect_t,
22 ccl_private uint *lcg_state,
23 const int max_hits)
24{
25 if (lcg_state) {
26 /* Record up to max_hits intersections. */
27 for (int i = min(max_hits, local_isect->num_hits) - 1; i >= 0; --i) {
28 if (local_isect->hits[i].t == isect_t) {
29 return -1;
30 }
31 }
32
33 local_isect->num_hits++;
34
35 int hit;
36 if (local_isect->num_hits <= max_hits) {
37 hit = local_isect->num_hits - 1;
38 }
39 else {
40 /* Reservoir sampling: if we are at the maximum number of hits, randomly replace element or
41 * skip it. */
42 hit = lcg_step_uint(lcg_state) % local_isect->num_hits;
43 if (hit >= max_hits) {
44 return -1;
45 }
46 }
47 return hit;
48 }
49
50 /* Record closest intersection only. */
51 if (local_isect->num_hits && isect_t > local_isect->hits[0].t) {
52 return -1;
53 }
54 local_isect->num_hits = 1;
55 return 0;
56}
57#endif /* __BVH_LOCAL__ */
58
unsigned int uint
#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:36
i
Definition text_draw.cc:230