Blender V5.3
cycles/kernel/bvh/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"
9#include "kernel/types.h"
10#include "util/types_rgbe.h"
11
13
15{
16 /* NOTE: Due to some vectorization code non-finite origin point might
17 * cause lots of false-positive intersections which will overflow traversal
18 * stack.
19 * This code is a quick way to perform early output, to avoid crashes in
20 * such cases.
21 * From production scenes so far it seems it's enough to test first element
22 * only.
23 * Scene intersection may also called with empty rays for conditional trace
24 * calls that evaluate to false, so filter those out.
25 */
26 return isfinite_safe(ray->P.x) && isfinite_safe(ray->D.x) && len_squared(ray->D) != 0.0f &&
27 ray->tmin < FLT_MAX;
28}
29
30/* Offset intersection distance by the smallest possible amount, to skip
31 * intersections at this distance. This works in cases where the ray start
32 * position is unchanged and only tmin is updated, since for self
33 * intersection we'll be comparing against the exact same distances.
34 *
35 * Always returns normalized floating point value. */
37{
38 /* This is a simplified version of `nextafterf(t, FLT_MAX)`, only dealing with
39 * non-negative and finite t. */
40 kernel_assert(t >= 0.0f && isfinite_safe(t));
41
42 /* Special handling of zero, which also includes handling of denormal values:
43 * always return smallest normalized value. If a denormalized zero is returned
44 * it will cause false-positive intersection detection with a distance of 0.
45 *
46 * The check relies on the fact that comparison of denormal values with zero
47 * returns true. */
48 if (t == 0.0f) {
49 /* The exact bit value of this should be 0x1p-126, but hex floating point values notation is
50 * not available in CUDA/OptiX. */
51 return FLT_MIN;
52 }
53
54 const uint32_t bits = __float_as_uint(t) + 1;
55 const float result = __uint_as_float(bits);
56
57 /* Assert that the calculated value is indeed considered to be offset from the
58 * original value. */
59 kernel_assert(result > t);
60
61 return result;
62}
63
64/* Ray offset to avoid self intersection.
65 *
66 * This function can be used to compute a modified ray start position for rays
67 * leaving from a surface. This is from:
68 * "A Fast and Robust Method for Avoiding Self-Intersection"
69 * Ray Tracing Gems, chapter 6.
70 */
72{
73 const float int_scale = 256.0f;
74 const int3 of_i = make_int3(
75 (int)(int_scale * Ng.x), (int)(int_scale * Ng.y), (int)(int_scale * Ng.z));
76
77 const float3 p_i = make_float3(
78 __int_as_float(__float_as_int(P.x) + ((P.x < 0) ? -of_i.x : of_i.x)),
79 __int_as_float(__float_as_int(P.y) + ((P.y < 0) ? -of_i.y : of_i.y)),
80 __int_as_float(__float_as_int(P.z) + ((P.z < 0) ? -of_i.z : of_i.z)));
81 const float origin = 1.0f / 32.0f;
82 const float float_scale = 1.0f / 65536.0f;
83 return make_float3(fabsf(P.x) < origin ? P.x + float_scale * Ng.x : p_i.x,
84 fabsf(P.y) < origin ? P.y + float_scale * Ng.y : p_i.y,
85 fabsf(P.z) < origin ? P.z + float_scale * Ng.z : p_i.z);
86}
87
88#ifndef __KERNEL_GPU__
89ccl_device int intersections_compare(const void *a, const void *b)
90{
91 const Intersection *isect_a = (const Intersection *)a;
92 const Intersection *isect_b = (const Intersection *)b;
93
94 if (isect_a->t < isect_b->t) {
95 return -1;
96 }
97 if (isect_a->t > isect_b->t) {
98 return 1;
99 }
100 return 0;
101}
102#endif
103
104/* For subsurface scattering, only sorting a small amount of intersections
105 * so bubble sort is fine for CPU and GPU. */
108 uint num_hits)
109{
110 bool swapped;
111 do {
112 swapped = false;
113 for (uint j = 0; j < num_hits - 1; ++j) {
114 if (hits[j].t > hits[j + 1].t) {
115 Intersection tmp_hit = hits[j];
116 float3 tmp_Ng = Ng[j];
117 hits[j] = hits[j + 1];
118 Ng[j] = Ng[j + 1];
119 hits[j + 1] = tmp_hit;
120 Ng[j + 1] = tmp_Ng;
121 swapped = true;
122 }
123 }
124 --num_hits;
125 } while (swapped);
126}
127
128/* Utility to quickly get flags from an intersection. */
129
131 const int prim,
132 const int type)
133{
134 int shader = 0;
135
136 if (type & PRIMITIVE_TRIANGLE) {
137 shader = kernel_data_fetch(tri_shader, prim);
138 }
139#ifdef __POINTCLOUD__
140 else if (type & PRIMITIVE_ANY_POINT) {
141 shader = kernel_data_fetch(points_shader, prim);
142 }
143#endif
144#ifdef __HAIR__
145 else if (type & PRIMITIVE_CURVE) {
146 shader = kernel_data_fetch(curves, prim).shader_id;
147 }
148#endif
149
150 return kernel_data_fetch(shaders, (shader & SHADER_MASK)).flags;
151}
152
154 const int prim,
155 const int isect_type)
156{
157 int shader = 0;
158
159 if (isect_type & PRIMITIVE_TRIANGLE) {
160 shader = kernel_data_fetch(tri_shader, prim);
161 }
162#ifdef __POINTCLOUD__
163 else if (isect_type & PRIMITIVE_ANY_POINT) {
164 shader = kernel_data_fetch(points_shader, prim);
165 }
166#endif
167#ifdef __HAIR__
168 else if (isect_type & PRIMITIVE_CURVE) {
169 shader = kernel_data_fetch(curves, prim).shader_id;
170 }
171#endif
172
173 return shader & SHADER_MASK;
174}
175
181
184{
185 return kernel_data_fetch(object_flag, isect->object);
186}
187
188/* TODO: find a better (faster) solution for this. Maybe store offset per object for
189 * attributes needed in intersection? */
191 const int object,
192 const uint id)
193{
194 uint attr_offset = kernel_data_fetch(objects, object).attribute_map_offset;
195 AttributeMap attr_map = kernel_data_fetch(attributes_map, attr_offset);
196
197 while (attr_map.id != id) {
198 if (UNLIKELY(attr_map.id == ATTR_STD_NONE)) {
199 if (UNLIKELY(attr_map.element == 0)) {
200 return (int)ATTR_STD_NOT_FOUND;
201 }
202 /* Chain jump to a different part of the table. */
203 attr_offset = attr_map.offset;
204 }
205 else {
206 attr_offset += ATTR_PRIM_TYPES;
207 }
208 attr_map = kernel_data_fetch(attributes_map, attr_offset);
209 }
210
211 /* return result */
212 return (attr_map.element == ATTR_ELEMENT_NONE) ? (int)ATTR_STD_NOT_FOUND : attr_map.offset;
213}
214
215/* Transparent Shadows */
216
217/* Cut-off value to stop transparent shadow tracing when practically opaque. */
218#define CURVE_SHADOW_TRANSPARENCY_CUTOFF 0.001f
219
221 KernelGlobals kg, const int object, const int prim, const int type, const float u)
222{
223 /* Find attribute. */
224 const int offset = intersection_find_attribute(kg, object, ATTR_STD_SHADOW_TRANSPARENCY);
225 if (offset == ATTR_STD_NOT_FOUND) {
226 /* If no shadow transparency attribute, assume opaque. */
227 return zero_float3();
228 }
229
230 /* Interpolate transparency between curve keys. */
231 const KernelCurve kcurve = kernel_data_fetch(curves, prim);
232 const int k0 = kcurve.first_key + PRIMITIVE_UNPACK_SEGMENT(type);
233 const int k1 = k0 + 1;
234
235 const float f0_encoded = kernel_data_fetch(attributes_float, offset + k0);
236 const float f1_encoded = kernel_data_fetch(attributes_float, offset + k1);
237
238 const float3 f0 = rgbe_to_rgb(RGBE(f0_encoded));
239 const float3 f1 = rgbe_to_rgb(RGBE(f1_encoded));
240
241 return (1.0f - u) * f0 + u * f1;
242}
243
245 const int object,
246 const int prim)
247{
248 return (self.prim == prim) && (self.object == object);
249}
250
252 const int object,
253 const int prim)
254{
255 return ((self.prim == prim) && (self.object == object)) ||
256 ((self.light_prim == prim) && (self.light_object == object));
257}
258
260 const int prim)
261{
262 return (self.prim == prim);
263}
264
265#ifdef __SHADOW_LINKING__
267ray_get_shadow_set_membership(KernelGlobals kg, const ccl_ray_data RaySelfPrimitives &self)
268{
269 if (self.light_object != OBJECT_NONE) {
270 return kernel_data_fetch(objects, self.light_object).shadow_set_membership;
271 }
272
273 return LIGHT_LINK_MASK_ALL;
274}
275#endif
276
279 const int isect_object)
280{
281#ifdef __SHADOW_LINKING__
282 if (!(kernel_data.kernel_features & KERNEL_FEATURE_SHADOW_LINKING)) {
283 return false;
284 }
285
286 const uint64_t set_membership = ray_get_shadow_set_membership(kg, self);
287 if (set_membership == LIGHT_LINK_MASK_ALL) {
288 return false;
289 }
290
291 const uint blocker_set = kernel_data_fetch(objects, isect_object).blocker_shadow_set;
292 return ((uint64_t(1) << uint64_t(blocker_set)) & set_membership) == 0;
293#else
294 return false;
295#endif
296}
297
298/* Check whether an intersection denoted by its object and primitive is to be skipped due to it
299 * being already recoded.
300 * The situation when primitive is already recoded happens when BVH spatial splits are used. */
302 const int object,
303 const int prim,
304 const uint num_hits)
305{
306 const uint num_recorded_hits = min(num_hits, INTEGRATOR_SHADOW_ISECT_SIZE);
307 for (uint i = 0; i < num_recorded_hits; ++i) {
308 const int isect_object = INTEGRATOR_STATE_ARRAY(state, shadow_isect, i, object);
309 const int isect_prim = INTEGRATOR_STATE_ARRAY(state, shadow_isect, i, prim);
310 if (object == isect_object && prim == isect_prim) {
311 return true;
312 }
313 }
314 return false;
315}
316
#define UNLIKELY(x)
unsigned long long int uint64_t
ccl_device_forceinline int intersection_get_shader_flags(KernelGlobals kg, const int prim, const int type)
ccl_device_forceinline uint intersection_get_object_flags(KernelGlobals kg, const ccl_private Intersection *ccl_restrict isect)
ccl_device_inline bool intersection_skip_shadow_link(KernelGlobals kg, const ccl_ray_data RaySelfPrimitives &self, const int isect_object)
ccl_device_inline void sort_intersections_and_normals(ccl_private Intersection *hits, ccl_private float3 *Ng, uint num_hits)
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(const ccl_ray_data RaySelfPrimitives &self, const int object, const int prim)
CCL_NAMESPACE_BEGIN ccl_device_inline bool intersection_ray_valid(const ccl_private Ray *ray)
ccl_device_forceinline float intersection_t_offset(const float t)
ccl_device int intersections_compare(const void *a, const void *b)
ccl_device_inline int intersection_find_attribute(KernelGlobals kg, const int object, const uint id)
ccl_device_forceinline int intersection_get_shader(KernelGlobals kg, const ccl_private Intersection *ccl_restrict isect)
ccl_device_inline float3 ray_offset(const float3 P, const float3 Ng)
ccl_device_forceinline bool intersection_skip_shadow_already_recoded(IntegratorShadowState state, const int object, const int prim, const uint num_hits)
ccl_device_inline float3 intersection_curve_shadow_transparency(KernelGlobals kg, const int object, const int prim, const int type, const float u)
ccl_device_forceinline int intersection_get_shader_from_isect_prim(KernelGlobals kg, const int prim, const int isect_type)
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_ray_data
#define ccl_restrict
#define ccl_device_forceinline
#define kernel_data_fetch(name, index)
#define PRIMITIVE_UNPACK_SEGMENT(type)
#define INTEGRATOR_SHADOW_ISECT_SIZE
#define OBJECT_NONE
#define ccl_private
#define ccl_device_inline
#define KERNEL_FEATURE_SHADOW_LINKING
#define LIGHT_LINK_MASK_ALL
#define CCL_NAMESPACE_END
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
#define __int_as_float(x)
ccl_device_forceinline int3 make_int3(const int x, const int y, const int z)
#define __float_as_int(x)
#define __float_as_uint(x)
#define __uint_as_float(x)
#define FLT_MAX
#define FLT_MIN
const int state
@ PRIMITIVE_CURVE
@ PRIMITIVE_ANY_POINT
@ PRIMITIVE_TRIANGLE
@ SHADER_MASK
@ ATTR_ELEMENT_NONE
@ ATTR_PRIM_TYPES
@ ATTR_STD_NOT_FOUND
@ ATTR_STD_NONE
@ ATTR_STD_SHADOW_TRANSPARENCY
ccl_device_inline bool isfinite_safe(const float f)
Definition math_base.h:362
ccl_device_inline dual1 len_squared(const ccl_private dual< T > &a)
Definition math_dual.h:273
CCL_NAMESPACE_BEGIN ccl_device_inline float3 zero_float3()
Definition math_float3.h:17
unsigned int uint
Definition BKE_text.h:55
#define fabsf
#define ccl_device
#define min(a, b)
Definition sort.cc:38
IntegratorShadowStateCPU * IntegratorShadowState
Definition state.h:229
#define INTEGRATOR_STATE_ARRAY(state, nested_struct, array_index, member)
Definition state.h:237
uint16_t element
RGBE()=default
float z
Definition sky_math.h:136
float y
Definition sky_math.h:136
float x
Definition sky_math.h:136
ccl_device_inline float3 rgbe_to_rgb(const RGBE rgbe)
Definition types_rgbe.h:92