Blender V5.3
triangle_intersect.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2014-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5/* Triangle/Ray intersections.
6 *
7 * For BVH ray intersection we use a precomputed triangle storage to accelerate
8 * intersection at the cost of more memory usage.
9 */
10
11#pragma once
12
13#include "kernel/globals.h"
14
16#include "kernel/geom/object.h"
18
19#include "util/math_float3.h"
20#include "util/math_intersect.h"
21
23
26 const float3 P,
27 const float3 dir,
28 const float tmin,
29 const float tmax,
30 const uint visibility,
31 const int object,
32 const int prim,
33 const int prim_addr)
34{
35 const int position_offset = kernel_data_fetch(objects, object).position_offset;
36 const uint3 tri_vindex = kernel_data_fetch(tri_vindex, prim);
37 const float3 tri_a = kernel_data_fetch(tri_verts, position_offset + tri_vindex.x);
38 const float3 tri_b = kernel_data_fetch(tri_verts, position_offset + tri_vindex.y);
39 const float3 tri_c = kernel_data_fetch(tri_verts, position_offset + tri_vindex.z);
40
41 float t;
42 float u;
43 float v;
44 if (ray_triangle_intersect(P, dir, tmin, tmax, tri_a, tri_b, tri_c, &u, &v, &t)) {
45#if defined(__VISIBILITY_FLAG__) && !defined(__KERNEL_HIPRT__)
46 /* Visibility flag test. we do it here under the assumption that most triangles are culled by
47 * node flags.
48 * Note that HIP-RT performs visibility check on the instance level. */
49 if (kernel_data_fetch(prim_visibility, prim_addr) & visibility)
50#endif
51 {
52 isect->object = object;
53 isect->prim = prim;
54 isect->type = PRIMITIVE_TRIANGLE;
55 isect->u = u;
56 isect->v = v;
57 isect->t = t;
58 return true;
59 }
60 }
61 return false;
62}
63
64/* Special ray intersection routines for subsurface scattering. In that case we
65 * only want to intersect with primitives in the same object, and if case of
66 * multiple hits we pick a single random primitive as the intersection point.
67 * Returns whether traversal should be stopped.
68 */
69
70#ifdef __BVH_LOCAL__
71ccl_device_inline bool triangle_intersect_local(KernelGlobals kg,
72 ccl_private LocalIntersection *local_isect,
73 const float3 P,
74 const float3 dir,
75 const int object,
76 const int prim,
77 const float tmin,
78 const float tmax,
79 ccl_private uint *lcg_state,
80 const int max_hits)
81{
82 const int position_offset = kernel_data_fetch(objects, object).position_offset;
83 const uint3 tri_vindex = kernel_data_fetch(tri_vindex, prim);
84 const float3 tri_a = kernel_data_fetch(tri_verts, position_offset + tri_vindex.x);
85 const float3 tri_b = kernel_data_fetch(tri_verts, position_offset + tri_vindex.y);
86 const float3 tri_c = kernel_data_fetch(tri_verts, position_offset + tri_vindex.z);
87
88 float t;
89 float u;
90 float v;
91 if (!ray_triangle_intersect(P, dir, tmin, tmax, tri_a, tri_b, tri_c, &u, &v, &t)) {
92 return false;
93 }
94
95 /* If no actual hit information is requested, just return here. */
96 if (max_hits == 0) {
97 return true;
98 }
99
100 const int hit_index = local_intersect_get_record_index(local_isect, t, lcg_state, max_hits);
101 if (hit_index == -1) {
102 return false;
103 }
104
105 /* Record intersection. */
106 ccl_private Intersection *isect = &local_isect->hits[hit_index];
107 isect->prim = prim;
108 isect->object = object;
109 isect->type = PRIMITIVE_TRIANGLE;
110 isect->u = u;
111 isect->v = v;
112 isect->t = t;
113
114 /* Record geometric normal. */
115 local_isect->Ng[hit_index] = normalize(cross(tri_b - tri_a, tri_c - tri_a));
116
117 return false;
118}
119#endif /* __BVH_LOCAL__ */
120
125 ccl_private ShaderData *sd,
126 const int isect_prim,
127 const float u,
128 const float v)
129{
130 const int position_offset = kernel_data_fetch(objects, sd->object).position_offset;
131 const uint3 tri_vindex = kernel_data_fetch(tri_vindex, isect_prim);
132 const float3 tri_a = kernel_data_fetch(tri_verts, position_offset + tri_vindex.x);
133 const float3 tri_b = kernel_data_fetch(tri_verts, position_offset + tri_vindex.y);
134 const float3 tri_c = kernel_data_fetch(tri_verts, position_offset + tri_vindex.z);
135
136 /* This appears to give slightly better precision than interpolating with w = (1 - u - v). */
137 float3 P = tri_a + u * (tri_b - tri_a) + v * (tri_c - tri_a);
138
139 if (!(sd->object_flag & SD_OBJECT_TRANSFORM_APPLIED)) {
140 const Transform tfm = object_get_transform(kg, sd);
141 P = transform_point(&tfm, P);
142 }
143
144 return P;
145}
146
152 ccl_private ShaderData *sd,
153 const float u,
154 const float v,
155 const float3 verts[3])
156{
157 /* This appears to give slightly better precision than interpolating with w = (1 - u - v). */
158 float3 P = verts[0] + u * (verts[1] - verts[0]) + v * (verts[2] - verts[0]);
159
160 if (!(sd->object_flag & SD_OBJECT_TRANSFORM_APPLIED)) {
161 const Transform tfm = object_get_transform(kg, sd);
162 P = transform_point(&tfm, P);
163 }
164
165 return P;
166}
167
169{
170 sd->shader = kernel_data_fetch(tri_shader, sd->prim);
171
172 sd->P = triangle_point_from_uv(kg, sd, sd->prim, sd->u, sd->v);
173
174 /* Normals. */
175 const float3 Ng = triangle_normal(kg, sd);
176 sd->Ng = Ng;
177 sd->N = Ng;
178
179 /* Smooth normal. */
180 if (sd->shader & SHADER_SMOOTH_NORMAL) {
181 sd->N = triangle_smooth_normal(kg, Ng, sd->object, sd->object_flag, sd->prim, sd->u, sd->v);
182 }
183
184#ifdef __DPDU__
185 /* dPdu/dPdv */
186 triangle_dPdudv(kg, sd->object, sd->prim, &sd->dPdu, &sd->dPdv);
187#endif
188}
189
#define kernel_data_fetch(name, index)
#define ccl_private
#define ccl_device_inline
#define CCL_NAMESPACE_END
ccl_device_inline float3 triangle_smooth_normal(KernelGlobals kg, float3 Ng, int object, int object_flag, int prim, float u, float v)
ccl_device_inline float3 triangle_normal(KernelGlobals kg, ccl_private ShaderData *sd)
ccl_device_inline void triangle_dPdudv(KernelGlobals kg, const int object, const int prim, ccl_private float3 *dPdu, ccl_private float3 *dPdv)
VecBase< float, D > normalize(VecOp< float, D >) RET
VecBase< float, 3 > cross(VecOp< float, 3 >, VecOp< float, 3 >) RET
float3 transform_point(float3x3 mat, float3 point)
ccl_device_inline Transform object_get_transform(KernelGlobals kg, const ccl_private ShaderData *sd)
@ PRIMITIVE_TRIANGLE
@ SD_OBJECT_TRANSFORM_APPLIED
@ SHADER_SMOOTH_NORMAL
ccl_device_forceinline bool ray_triangle_intersect(const float3 ray_P, const float3 ray_D, const float ray_tmin, const float ray_tmax, const float3 tri_a, const float3 tri_b, const float3 tri_c, ccl_private float *isect_u, ccl_private float *isect_v, ccl_private float *isect_t)
ATTR_WARN_UNUSED_RESULT const BMVert * v
uint y
Definition types_uint3.h:13
uint z
Definition types_uint3.h:13
uint x
Definition types_uint3.h:13
ccl_device_inline float3 triangle_point_from_uv(KernelGlobals kg, ccl_private ShaderData *sd, const int isect_prim, const float u, const float v)
CCL_NAMESPACE_BEGIN ccl_device_inline bool triangle_intersect(KernelGlobals kg, ccl_private Intersection *isect, const float3 P, const float3 dir, const float tmin, const float tmax, const uint visibility, const int object, const int prim, const int prim_addr)
ccl_device_inline float3 triangle_point_from_uv_and_verts(KernelGlobals kg, ccl_private ShaderData *sd, const float u, const float v, const float3 verts[3])
ccl_device_inline void triangle_shader_setup(KernelGlobals kg, ccl_private ShaderData *sd)