Blender  V3.3
triangle_intersect.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0
2  * Copyright 2014-2022 Blender Foundation. */
3 
4 /* Triangle/Ray intersections.
5  *
6  * For BVH ray intersection we use a precomputed triangle storage to accelerate
7  * intersection at the cost of more memory usage.
8  */
9 
10 #pragma once
11 
12 #include "kernel/sample/lcg.h"
13 
15 
18  float3 P,
19  float3 dir,
20  float tmin,
21  float tmax,
22  uint visibility,
23  int object,
24  int prim,
25  int prim_addr)
26 {
27  const uint tri_vindex = kernel_data_fetch(tri_vindex, prim).w;
28  const float3 tri_a = kernel_data_fetch(tri_verts, tri_vindex + 0),
29  tri_b = kernel_data_fetch(tri_verts, tri_vindex + 1),
30  tri_c = kernel_data_fetch(tri_verts, tri_vindex + 2);
31  float t, u, v;
32  if (ray_triangle_intersect(P, dir, tmin, tmax, tri_a, tri_b, tri_c, &u, &v, &t)) {
33 #ifdef __VISIBILITY_FLAG__
34  /* Visibility flag test. we do it here under the assumption
35  * that most triangles are culled by node flags.
36  */
37  if (kernel_data_fetch(prim_visibility, prim_addr) & visibility)
38 #endif
39  {
40  isect->object = object;
41  isect->prim = prim;
42  isect->type = PRIMITIVE_TRIANGLE;
43  isect->u = u;
44  isect->v = v;
45  isect->t = t;
46  return true;
47  }
48  }
49  return false;
50 }
51 
52 /* Special ray intersection routines for subsurface scattering. In that case we
53  * only want to intersect with primitives in the same object, and if case of
54  * multiple hits we pick a single random primitive as the intersection point.
55  * Returns whether traversal should be stopped.
56  */
57 
58 #ifdef __BVH_LOCAL__
59 ccl_device_inline bool triangle_intersect_local(KernelGlobals kg,
60  ccl_private LocalIntersection *local_isect,
61  float3 P,
62  float3 dir,
63  int object,
64  int prim,
65  int prim_addr,
66  float tmin,
67  float tmax,
68  ccl_private uint *lcg_state,
69  int max_hits)
70 {
71  const uint tri_vindex = kernel_data_fetch(tri_vindex, prim).w;
72  const float3 tri_a = kernel_data_fetch(tri_verts, tri_vindex + 0),
73  tri_b = kernel_data_fetch(tri_verts, tri_vindex + 1),
74  tri_c = kernel_data_fetch(tri_verts, tri_vindex + 2);
75  float t, u, v;
76  if (!ray_triangle_intersect(P, dir, tmin, tmax, tri_a, tri_b, tri_c, &u, &v, &t)) {
77  return false;
78  }
79 
80  /* If no actual hit information is requested, just return here. */
81  if (max_hits == 0) {
82  return true;
83  }
84 
85  int hit;
86  if (lcg_state) {
87  /* Record up to max_hits intersections. */
88  for (int i = min(max_hits, local_isect->num_hits) - 1; i >= 0; --i) {
89  if (local_isect->hits[i].t == t) {
90  return false;
91  }
92  }
93 
94  local_isect->num_hits++;
95 
96  if (local_isect->num_hits <= max_hits) {
97  hit = local_isect->num_hits - 1;
98  }
99  else {
100  /* reservoir sampling: if we are at the maximum number of
101  * hits, randomly replace element or skip it */
102  hit = lcg_step_uint(lcg_state) % local_isect->num_hits;
103 
104  if (hit >= max_hits)
105  return false;
106  }
107  }
108  else {
109  /* Record closest intersection only. */
110  if (local_isect->num_hits && t > local_isect->hits[0].t) {
111  return false;
112  }
113 
114  hit = 0;
115  local_isect->num_hits = 1;
116  }
117 
118  /* Record intersection. */
119  ccl_private Intersection *isect = &local_isect->hits[hit];
120  isect->prim = prim;
121  isect->object = object;
122  isect->type = PRIMITIVE_TRIANGLE;
123  isect->u = u;
124  isect->v = v;
125  isect->t = t;
126 
127  /* Record geometric normal. */
128  local_isect->Ng[hit] = normalize(cross(tri_b - tri_a, tri_c - tri_a));
129 
130  return false;
131 }
132 #endif /* __BVH_LOCAL__ */
133 
139  const int isect_object,
140  const int isect_prim,
141  const float u,
142  const float v)
143 {
144  const uint tri_vindex = kernel_data_fetch(tri_vindex, isect_prim).w;
145  const packed_float3 tri_a = kernel_data_fetch(tri_verts, tri_vindex + 0),
146  tri_b = kernel_data_fetch(tri_verts, tri_vindex + 1),
147  tri_c = kernel_data_fetch(tri_verts, tri_vindex + 2);
148 
149  /* This appears to give slightly better precision than interpolating with w = (1 - u - v). */
150  float3 P = tri_a + u * (tri_b - tri_a) + v * (tri_c - tri_a);
151 
152  if (!(sd->object_flag & SD_OBJECT_TRANSFORM_APPLIED)) {
153  const Transform tfm = object_get_transform(kg, sd);
154  P = transform_point(&tfm, P);
155  }
156 
157  return P;
158 }
159 
unsigned int uint
Definition: BLI_sys_types.h:67
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble GLdouble nz _GL_VOID_RET _GL_VOID GLfloat GLfloat nz _GL_VOID_RET _GL_VOID GLint GLint nz _GL_VOID_RET _GL_VOID GLshort GLshort nz _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const GLfloat *values _GL_VOID_RET _GL_VOID GLsizei const GLushort *values _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID const GLuint const GLclampf *priorities _GL_VOID_RET _GL_VOID GLdouble y _GL_VOID_RET _GL_VOID GLfloat y _GL_VOID_RET _GL_VOID GLint y _GL_VOID_RET _GL_VOID GLshort y _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLfloat GLfloat z _GL_VOID_RET _GL_VOID GLint GLint z _GL_VOID_RET _GL_VOID GLshort GLshort z _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble w _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat w _GL_VOID_RET _GL_VOID GLint GLint GLint w _GL_VOID_RET _GL_VOID GLshort GLshort GLshort w _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble y2 _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat y2 _GL_VOID_RET _GL_VOID GLint GLint GLint y2 _GL_VOID_RET _GL_VOID GLshort GLshort GLshort y2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLuint *buffer _GL_VOID_RET _GL_VOID GLdouble t _GL_VOID_RET _GL_VOID GLfloat t _GL_VOID_RET _GL_VOID GLint t _GL_VOID_RET _GL_VOID GLshort t _GL_VOID_RET _GL_VOID GLdouble t
ATTR_WARN_UNUSED_RESULT const BMVert * v
#define ccl_private
Definition: cuda/compat.h:48
#define ccl_device_inline
Definition: cuda/compat.h:34
#define CCL_NAMESPACE_END
Definition: cuda/compat.h:9
const KernelGlobalsCPU *ccl_restrict KernelGlobals
#define kernel_data_fetch(name, index)
CCL_NAMESPACE_END CCL_NAMESPACE_BEGIN ccl_device_inline float3 transform_point(ccl_private const Transform *t, const float3 a)
ccl_device_inline Transform object_get_transform(KernelGlobals kg, ccl_private const ShaderData *sd)
@ PRIMITIVE_TRIANGLE
Definition: kernel/types.h:551
ShaderData
Definition: kernel/types.h:925
@ SD_OBJECT_TRANSFORM_APPLIED
Definition: kernel/types.h:808
CCL_NAMESPACE_BEGIN ccl_device uint lcg_step_uint(T rng)
Definition: lcg.h:11
static float P(float k)
Definition: math_interp.c:25
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)
vec_base< T, 3 > cross(const vec_base< T, 3 > &a, const vec_base< T, 3 > &b)
vec_base< T, Size > normalize(const vec_base< T, Size > &v)
#define min(a, b)
Definition: sort.c:35
ccl_device_inline float3 triangle_point_from_uv(KernelGlobals kg, ccl_private ShaderData *sd, const int isect_object, 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, float3 P, float3 dir, float tmin, float tmax, uint visibility, int object, int prim, int prim_addr)