1// Copyright 2009-2021 Intel Corporation
2// SPDX-License-Identifier: Apache-2.0
3
4#pragma once
5
6#include "quadv.h"
7#include "triangle_intersector_moeller.h"
8
9namespace embree
10{
11 namespace isa
12 {
13 template<int M>
14 struct QuadHitM
15 {
16 __forceinline QuadHitM() {}
17
18 __forceinline QuadHitM(const vbool<M>& valid,
19 const vfloat<M>& U,
20 const vfloat<M>& V,
21 const vfloat<M>& T,
22 const vfloat<M>& absDen,
23 const Vec3vf<M>& Ng,
24 const vbool<M>& flags)
25 : U(U), V(V), T(T), absDen(absDen), tri_Ng(Ng), valid(valid), flags(flags) {}
26
27 __forceinline void finalize()
28 {
29 const vfloat<M> rcpAbsDen = rcp(absDen);
30 vt = T * rcpAbsDen;
31 const vfloat<M> u = min(U * rcpAbsDen,1.0f);
32 const vfloat<M> v = min(V * rcpAbsDen,1.0f);
33 const vfloat<M> u1 = vfloat<M>(1.0f) - u;
34 const vfloat<M> v1 = vfloat<M>(1.0f) - v;
35#if !defined(__AVX__) || defined(EMBREE_BACKFACE_CULLING)
36 vu = select(flags,u1,u);
37 vv = select(flags,v1,v);
38 vNg = Vec3vf<M>(tri_Ng.x,tri_Ng.y,tri_Ng.z);
39#else
40 const vfloat<M> flip = select(flags,vfloat<M>(-1.0f),vfloat<M>(1.0f));
41 vv = select(flags,u1,v);
42 vu = select(flags,v1,u);
43 vNg = Vec3vf<M>(flip*tri_Ng.x,flip*tri_Ng.y,flip*tri_Ng.z);
44#endif
45 }
46
47 __forceinline Vec2f uv(const size_t i)
48 {
49 const float u = vu[i];
50 const float v = vv[i];
51 return Vec2f(u,v);
52 }
53
54 __forceinline float t(const size_t i) { return vt[i]; }
55 __forceinline Vec3fa Ng(const size_t i) { return Vec3fa(vNg.x[i],vNg.y[i],vNg.z[i]); }
56
57 private:
58 vfloat<M> U;
59 vfloat<M> V;
60 vfloat<M> T;
61 vfloat<M> absDen;
62 Vec3vf<M> tri_Ng;
63
64 public:
65 vbool<M> valid;
66 vfloat<M> vu;
67 vfloat<M> vv;
68 vfloat<M> vt;
69 Vec3vf<M> vNg;
70
71 public:
72 const vbool<M> flags;
73 };
74
75 template<int K>
76 struct QuadHitK
77 {
78 __forceinline QuadHitK(const vfloat<K>& U,
79 const vfloat<K>& V,
80 const vfloat<K>& T,
81 const vfloat<K>& absDen,
82 const Vec3vf<K>& Ng,
83 const vbool<K>& flags)
84 : U(U), V(V), T(T), absDen(absDen), flags(flags), tri_Ng(Ng) {}
85
86 __forceinline std::tuple<vfloat<K>,vfloat<K>,vfloat<K>,Vec3vf<K>> operator() () const
87 {
88 const vfloat<K> rcpAbsDen = rcp(absDen);
89 const vfloat<K> t = T * rcpAbsDen;
90 const vfloat<K> u0 = min(U * rcpAbsDen,1.0f);
91 const vfloat<K> v0 = min(V * rcpAbsDen,1.0f);
92 const vfloat<K> u1 = vfloat<K>(1.0f) - u0;
93 const vfloat<K> v1 = vfloat<K>(1.0f) - v0;
94 const vfloat<K> u = select(flags,u1,u0);
95 const vfloat<K> v = select(flags,v1,v0);
96 const Vec3vf<K> Ng(tri_Ng.x,tri_Ng.y,tri_Ng.z);
97 return std::make_tuple(u,v,t,Ng);
98 }
99
100 private:
101 const vfloat<K> U;
102 const vfloat<K> V;
103 const vfloat<K> T;
104 const vfloat<K> absDen;
105 const vbool<K> flags;
106 const Vec3vf<K> tri_Ng;
107 };
108
109 /* ----------------------------- */
110 /* -- single ray intersectors -- */
111 /* ----------------------------- */
112
113
114 template<int M, bool filter>
115 struct QuadMIntersector1MoellerTrumbore;
116
117 /*! Intersects M quads with 1 ray */
118 template<int M, bool filter>
119 struct QuadMIntersector1MoellerTrumbore
120 {
121 __forceinline QuadMIntersector1MoellerTrumbore() {}
122
123 __forceinline QuadMIntersector1MoellerTrumbore(const Ray& ray, const void* ptr) {}
124
125 __forceinline void intersect(RayHit& ray, IntersectContext* context,
126 const Vec3vf<M>& v0, const Vec3vf<M>& v1, const Vec3vf<M>& v2, const Vec3vf<M>& v3,
127 const vuint<M>& geomID, const vuint<M>& primID) const
128 {
129 UVIdentity<M> mapUV;
130 MoellerTrumboreHitM<M,UVIdentity<M>> hit(mapUV);
131 MoellerTrumboreIntersector1<M> intersector(ray,nullptr);
132 Intersect1EpilogM<M,filter> epilog(ray,context,geomID,primID);
133
134 /* intersect first triangle */
135 if (intersector.intersect(ray,v0,v1,v3,mapUV,hit))
136 epilog(hit.valid,hit);
137
138 /* intersect second triangle */
139 if (intersector.intersect(ray,v2,v3,v1,mapUV,hit))
140 {
141 hit.U = hit.absDen - hit.U;
142 hit.V = hit.absDen - hit.V;
143 epilog(hit.valid,hit);
144 }
145 }
146
147 __forceinline bool occluded(Ray& ray, IntersectContext* context,
148 const Vec3vf<M>& v0, const Vec3vf<M>& v1, const Vec3vf<M>& v2, const Vec3vf<M>& v3,
149 const vuint<M>& geomID, const vuint<M>& primID) const
150 {
151 UVIdentity<M> mapUV;
152 MoellerTrumboreHitM<M,UVIdentity<M>> hit(mapUV);
153 MoellerTrumboreIntersector1<M> intersector(ray,nullptr);
154 Occluded1EpilogM<M,filter> epilog(ray,context,geomID,primID);
155
156 /* intersect first triangle */
157 if (intersector.intersect(ray,v0,v1,v3,mapUV,hit))
158 {
159 if (epilog(hit.valid,hit))
160 return true;
161 }
162
163 /* intersect second triangle */
164 if (intersector.intersect(ray,v2,v3,v1,mapUV,hit))
165 {
166 hit.U = hit.absDen - hit.U;
167 hit.V = hit.absDen - hit.V;
168 if (epilog(hit.valid,hit))
169 return true;
170 }
171 return false;
172 }
173 };
174
175#if defined(__AVX__)
176
177 /*! Intersects 4 quads with 1 ray using AVX */
178 template<bool filter>
179 struct QuadMIntersector1MoellerTrumbore<4,filter>
180 {
181 __forceinline QuadMIntersector1MoellerTrumbore() {}
182
183 __forceinline QuadMIntersector1MoellerTrumbore(const Ray& ray, const void* ptr) {}
184
185 template<typename Epilog>
186 __forceinline bool intersect(Ray& ray, const Vec3vf4& v0, const Vec3vf4& v1, const Vec3vf4& v2, const Vec3vf4& v3, const Epilog& epilog) const
187 {
188 const Vec3vf8 vtx0(vfloat8(v0.x,v2.x),vfloat8(v0.y,v2.y),vfloat8(v0.z,v2.z));
189#if !defined(EMBREE_BACKFACE_CULLING)
190 const Vec3vf8 vtx1(vfloat8(v1.x),vfloat8(v1.y),vfloat8(v1.z));
191 const Vec3vf8 vtx2(vfloat8(v3.x),vfloat8(v3.y),vfloat8(v3.z));
192#else
193 const Vec3vf8 vtx1(vfloat8(v1.x,v3.x),vfloat8(v1.y,v3.y),vfloat8(v1.z,v3.z));
194 const Vec3vf8 vtx2(vfloat8(v3.x,v1.x),vfloat8(v3.y,v1.y),vfloat8(v3.z,v1.z));
195#endif
196 UVIdentity<8> mapUV;
197 MoellerTrumboreHitM<8,UVIdentity<8>> hit(mapUV);
198 MoellerTrumboreIntersector1<8> intersector(ray,nullptr);
199 const vbool8 flags(0,0,0,0,1,1,1,1);
200 if (unlikely(intersector.intersect(ray,vtx0,vtx1,vtx2,mapUV,hit)))
201 {
202 vfloat8 U = hit.U, V = hit.V, absDen = hit.absDen;
203
204#if !defined(EMBREE_BACKFACE_CULLING)
205 hit.U = select(flags,absDen-V,U);
206 hit.V = select(flags,absDen-U,V);
207 hit.vNg *= select(flags,vfloat8(-1.0f),vfloat8(1.0f)); // FIXME: use XOR
208#else
209 hit.U = select(flags,absDen-U,U);
210 hit.V = select(flags,absDen-V,V);
211#endif
212 if (unlikely(epilog(hit.valid,hit)))
213 return true;
214 }
215 return false;
216 }
217
218 __forceinline bool intersect(RayHit& ray, IntersectContext* context,
219 const Vec3vf4& v0, const Vec3vf4& v1, const Vec3vf4& v2, const Vec3vf4& v3,
220 const vuint4& geomID, const vuint4& primID) const
221 {
222 return intersect(ray,v0,v1,v2,v3,Intersect1EpilogM<8,filter>(ray,context,vuint8(geomID),vuint8(primID)));
223 }
224
225 __forceinline bool occluded(Ray& ray, IntersectContext* context,
226 const Vec3vf4& v0, const Vec3vf4& v1, const Vec3vf4& v2, const Vec3vf4& v3,
227 const vuint4& geomID, const vuint4& primID) const
228 {
229 return intersect(ray,v0,v1,v2,v3,Occluded1EpilogM<8,filter>(ray,context,vuint8(geomID),vuint8(primID)));
230 }
231 };
232
233#endif
234
235 /* ----------------------------- */
236 /* -- ray packet intersectors -- */
237 /* ----------------------------- */
238
239
240 struct MoellerTrumboreIntersector1KTriangleM
241 {
242 /*! Intersect k'th ray from ray packet of size K with M triangles. */
243 template<int M, int K, typename Epilog>
244 static __forceinline bool intersect(RayK<K>& ray,
245 size_t k,
246 const Vec3vf<M>& tri_v0,
247 const Vec3vf<M>& tri_e1,
248 const Vec3vf<M>& tri_e2,
249 const Vec3vf<M>& tri_Ng,
250 const vbool<M>& flags,
251 const Epilog& epilog)
252 {
253 /* calculate denominator */
254 const Vec3vf<M> O = broadcast<vfloat<M>>(ray.org,k);
255 const Vec3vf<M> D = broadcast<vfloat<M>>(ray.dir,k);
256 const Vec3vf<M> C = Vec3vf<M>(tri_v0) - O;
257 const Vec3vf<M> R = cross(C,D);
258 const vfloat<M> den = dot(Vec3vf<M>(tri_Ng),D);
259 const vfloat<M> absDen = abs(den);
260 const vfloat<M> sgnDen = signmsk(den);
261
262 /* perform edge tests */
263 const vfloat<M> U = dot(R,Vec3vf<M>(tri_e2)) ^ sgnDen;
264 const vfloat<M> V = dot(R,Vec3vf<M>(tri_e1)) ^ sgnDen;
265
266 /* perform backface culling */
267#if defined(EMBREE_BACKFACE_CULLING)
268 vbool<M> valid = (den < vfloat<M>(zero)) & (U >= 0.0f) & (V >= 0.0f) & (U+V<=absDen);
269#else
270 vbool<M> valid = (den != vfloat<M>(zero)) & (U >= 0.0f) & (V >= 0.0f) & (U+V<=absDen);
271#endif
272 if (likely(none(valid))) return false;
273
274 /* perform depth test */
275 const vfloat<M> T = dot(Vec3vf<M>(tri_Ng),C) ^ sgnDen;
276 valid &= (absDen*vfloat<M>(ray.tnear()[k]) < T) & (T <= absDen*vfloat<M>(ray.tfar[k]));
277 if (likely(none(valid))) return false;
278
279 /* calculate hit information */
280 QuadHitM<M> hit(valid,U,V,T,absDen,tri_Ng,flags);
281 return epilog(valid,hit);
282 }
283
284 template<int M, int K, typename Epilog>
285 static __forceinline bool intersect1(RayK<K>& ray,
286 size_t k,
287 const Vec3vf<M>& v0,
288 const Vec3vf<M>& v1,
289 const Vec3vf<M>& v2,
290 const vbool<M>& flags,
291 const Epilog& epilog)
292 {
293 const Vec3vf<M> e1 = v0-v1;
294 const Vec3vf<M> e2 = v2-v0;
295 const Vec3vf<M> Ng = cross(e2,e1);
296 return intersect<M,K>(ray,k,v0,e1,e2,Ng,flags,epilog);
297 }
298 };
299
300 template<int M, int K, bool filter>
301 struct QuadMIntersectorKMoellerTrumboreBase
302 {
303 __forceinline QuadMIntersectorKMoellerTrumboreBase(const vbool<K>& valid, const RayK<K>& ray) {}
304
305 /*! Intersects K rays with one of M triangles. */
306 template<typename Epilog>
307 __forceinline vbool<K> intersectK(const vbool<K>& valid0,
308 RayK<K>& ray,
309 const Vec3vf<K>& tri_v0,
310 const Vec3vf<K>& tri_e1,
311 const Vec3vf<K>& tri_e2,
312 const Vec3vf<K>& tri_Ng,
313 const vbool<K>& flags,
314 const Epilog& epilog) const
315 {
316 /* calculate denominator */
317 vbool<K> valid = valid0;
318 const Vec3vf<K> C = tri_v0 - ray.org;
319 const Vec3vf<K> R = cross(C,ray.dir);
320 const vfloat<K> den = dot(tri_Ng,ray.dir);
321 const vfloat<K> absDen = abs(den);
322 const vfloat<K> sgnDen = signmsk(den);
323
324 /* test against edge p2 p0 */
325 const vfloat<K> U = dot(R,tri_e2) ^ sgnDen;
326 valid &= U >= 0.0f;
327 if (likely(none(valid))) return false;
328
329 /* test against edge p0 p1 */
330 const vfloat<K> V = dot(R,tri_e1) ^ sgnDen;
331 valid &= V >= 0.0f;
332 if (likely(none(valid))) return false;
333
334 /* test against edge p1 p2 */
335 const vfloat<K> W = absDen-U-V;
336 valid &= W >= 0.0f;
337 if (likely(none(valid))) return false;
338
339 /* perform depth test */
340 const vfloat<K> T = dot(tri_Ng,C) ^ sgnDen;
341 valid &= (absDen*ray.tnear() < T) & (T <= absDen*ray.tfar);
342 if (unlikely(none(valid))) return false;
343
344 /* perform backface culling */
345#if defined(EMBREE_BACKFACE_CULLING)
346 valid &= den < vfloat<K>(zero);
347 if (unlikely(none(valid))) return false;
348#else
349 valid &= den != vfloat<K>(zero);
350 if (unlikely(none(valid))) return false;
351#endif
352
353 /* calculate hit information */
354 QuadHitK<K> hit(U,V,T,absDen,tri_Ng,flags);
355 return epilog(valid,hit);
356 }
357
358 /*! Intersects K rays with one of M quads. */
359 template<typename Epilog>
360 __forceinline vbool<K> intersectK(const vbool<K>& valid0,
361 RayK<K>& ray,
362 const Vec3vf<K>& tri_v0,
363 const Vec3vf<K>& tri_v1,
364 const Vec3vf<K>& tri_v2,
365 const vbool<K>& flags,
366 const Epilog& epilog) const
367 {
368 const Vec3vf<K> e1 = tri_v0-tri_v1;
369 const Vec3vf<K> e2 = tri_v2-tri_v0;
370 const Vec3vf<K> Ng = cross(e2,e1);
371 return intersectK(valid0,ray,tri_v0,e1,e2,Ng,flags,epilog);
372 }
373
374 /*! Intersects K rays with one of M quads. */
375 template<typename Epilog>
376 __forceinline bool intersectK(const vbool<K>& valid0,
377 RayK<K>& ray,
378 const Vec3vf<K>& v0,
379 const Vec3vf<K>& v1,
380 const Vec3vf<K>& v2,
381 const Vec3vf<K>& v3,
382 const Epilog& epilog) const
383 {
384 intersectK(valid0,ray,v0,v1,v3,vbool<K>(false),epilog);
385 if (none(valid0)) return true;
386 intersectK(valid0,ray,v2,v3,v1,vbool<K>(true ),epilog);
387 return none(valid0);
388 }
389 };
390
391 template<int M, int K, bool filter>
392 struct QuadMIntersectorKMoellerTrumbore : public QuadMIntersectorKMoellerTrumboreBase<M,K,filter>
393 {
394 __forceinline QuadMIntersectorKMoellerTrumbore(const vbool<K>& valid, const RayK<K>& ray)
395 : QuadMIntersectorKMoellerTrumboreBase<M,K,filter>(valid,ray) {}
396
397 __forceinline void intersect1(RayHitK<K>& ray, size_t k, IntersectContext* context,
398 const Vec3vf<M>& v0, const Vec3vf<M>& v1, const Vec3vf<M>& v2, const Vec3vf<M>& v3,
399 const vuint<M>& geomID, const vuint<M>& primID) const
400 {
401 Intersect1KEpilogM<M,K,filter> epilog(ray,k,context,geomID,primID);
402 MoellerTrumboreIntersector1KTriangleM::intersect1<M,K>(ray,k,v0,v1,v3,vbool<M>(false),epilog);
403 MoellerTrumboreIntersector1KTriangleM::intersect1<M,K>(ray,k,v2,v3,v1,vbool<M>(true ),epilog);
404 }
405
406 __forceinline bool occluded1(RayK<K>& ray, size_t k, IntersectContext* context,
407 const Vec3vf<M>& v0, const Vec3vf<M>& v1, const Vec3vf<M>& v2, const Vec3vf<M>& v3,
408 const vuint<M>& geomID, const vuint<M>& primID) const
409 {
410 Occluded1KEpilogM<M,K,filter> epilog(ray,k,context,geomID,primID);
411 if (MoellerTrumboreIntersector1KTriangleM::intersect1<M,K>(ray,k,v0,v1,v3,vbool<M>(false),epilog)) return true;
412 if (MoellerTrumboreIntersector1KTriangleM::intersect1<M,K>(ray,k,v2,v3,v1,vbool<M>(true ),epilog)) return true;
413 return false;
414 }
415 };
416
417
418#if defined(__AVX__)
419
420 /*! Intersects 4 quads with 1 ray using AVX */
421 template<int K, bool filter>
422 struct QuadMIntersectorKMoellerTrumbore<4,K,filter> : public QuadMIntersectorKMoellerTrumboreBase<4,K,filter>
423 {
424 __forceinline QuadMIntersectorKMoellerTrumbore(const vbool<K>& valid, const RayK<K>& ray)
425 : QuadMIntersectorKMoellerTrumboreBase<4,K,filter>(valid,ray) {}
426
427 template<typename Epilog>
428 __forceinline bool intersect1(RayK<K>& ray, size_t k,
429 const Vec3vf4& v0, const Vec3vf4& v1, const Vec3vf4& v2, const Vec3vf4& v3, const Epilog& epilog) const
430 {
431 const Vec3vf8 vtx0(vfloat8(v0.x,v2.x),vfloat8(v0.y,v2.y),vfloat8(v0.z,v2.z));
432#if !defined(EMBREE_BACKFACE_CULLING)
433 const Vec3vf8 vtx1(vfloat8(v1.x),vfloat8(v1.y),vfloat8(v1.z));
434 const Vec3vf8 vtx2(vfloat8(v3.x),vfloat8(v3.y),vfloat8(v3.z));
435#else
436 const Vec3vf8 vtx1(vfloat8(v1.x,v3.x),vfloat8(v1.y,v3.y),vfloat8(v1.z,v3.z));
437 const Vec3vf8 vtx2(vfloat8(v3.x,v1.x),vfloat8(v3.y,v1.y),vfloat8(v3.z,v1.z));
438#endif
439 const vbool8 flags(0,0,0,0,1,1,1,1);
440 return MoellerTrumboreIntersector1KTriangleM::intersect1<8,K>(ray,k,vtx0,vtx1,vtx2,flags,epilog);
441 }
442
443 __forceinline bool intersect1(RayHitK<K>& ray, size_t k, IntersectContext* context,
444 const Vec3vf4& v0, const Vec3vf4& v1, const Vec3vf4& v2, const Vec3vf4& v3,
445 const vuint4& geomID, const vuint4& primID) const
446 {
447 return intersect1(ray,k,v0,v1,v2,v3,Intersect1KEpilogM<8,K,filter>(ray,k,context,vuint8(geomID),vuint8(primID)));
448 }
449
450 __forceinline bool occluded1(RayK<K>& ray, size_t k, IntersectContext* context,
451 const Vec3vf4& v0, const Vec3vf4& v1, const Vec3vf4& v2, const Vec3vf4& v3,
452 const vuint4& geomID, const vuint4& primID) const
453 {
454 return intersect1(ray,k,v0,v1,v2,v3,Occluded1KEpilogM<8,K,filter>(ray,k,context,vuint8(geomID),vuint8(primID)));
455 }
456 };
457
458#endif
459 }
460}
461

source code of qtquick3d/src/3rdparty/embree/kernels/geometry/quad_intersector_moeller.h