quad_intersector_moeller.h 18 KB

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