triangle_intersector_woop.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. // Copyright 2009-2021 Intel Corporation
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #include "triangle.h"
  5. #include "intersector_epilog.h"
  6. /*! This intersector implements a modified version of the Woop's ray-triangle intersection test */
  7. namespace embree
  8. {
  9. namespace isa
  10. {
  11. template<int M>
  12. struct WoopHitM
  13. {
  14. __forceinline WoopHitM() {}
  15. __forceinline WoopHitM(const vbool<M>& valid,
  16. const vfloat<M>& U,
  17. const vfloat<M>& V,
  18. const vfloat<M>& T,
  19. const vfloat<M>& inv_det,
  20. const Vec3vf<M>& Ng)
  21. : U(U), V(V), T(T), inv_det(inv_det), valid(valid), vNg(Ng) {}
  22. __forceinline void finalize()
  23. {
  24. vt = T;
  25. vu = U*inv_det;
  26. vv = V*inv_det;
  27. }
  28. __forceinline Vec2f uv (const size_t i) const { return Vec2f(vu[i],vv[i]); }
  29. __forceinline float t (const size_t i) const { return vt[i]; }
  30. __forceinline Vec3fa Ng(const size_t i) const { return Vec3fa(vNg.x[i],vNg.y[i],vNg.z[i]); }
  31. private:
  32. const vfloat<M> U;
  33. const vfloat<M> V;
  34. const vfloat<M> T;
  35. const vfloat<M> inv_det;
  36. public:
  37. const vbool<M> valid;
  38. vfloat<M> vu;
  39. vfloat<M> vv;
  40. vfloat<M> vt;
  41. Vec3vf<M> vNg;
  42. };
  43. template<int M>
  44. struct WoopPrecalculations1
  45. {
  46. unsigned int kx,ky,kz;
  47. Vec3vf<M> org;
  48. Vec3fa S;
  49. __forceinline WoopPrecalculations1() {}
  50. __forceinline WoopPrecalculations1(const Ray& ray, const void* ptr)
  51. {
  52. kz = maxDim(abs(ray.dir));
  53. kx = (kz+1) % 3;
  54. ky = (kx+1) % 3;
  55. const float inv_dir_kz = rcp(ray.dir[kz]);
  56. if (ray.dir[kz] < 0.0f) std::swap(kx,ky);
  57. S.x = ray.dir[kx] * inv_dir_kz;
  58. S.y = ray.dir[ky] * inv_dir_kz;
  59. S.z = inv_dir_kz;
  60. org = Vec3vf<M>(ray.org[kx],ray.org[ky],ray.org[kz]);
  61. }
  62. };
  63. template<int M>
  64. struct WoopIntersector1
  65. {
  66. typedef WoopPrecalculations1<M> Precalculations;
  67. __forceinline WoopIntersector1() {}
  68. __forceinline WoopIntersector1(const Ray& ray, const void* ptr) {}
  69. static __forceinline bool intersect(const vbool<M>& valid0,
  70. Ray& ray,
  71. const Precalculations& pre,
  72. const Vec3vf<M>& tri_v0,
  73. const Vec3vf<M>& tri_v1,
  74. const Vec3vf<M>& tri_v2,
  75. WoopHitM<M>& hit)
  76. {
  77. vbool<M> valid = valid0;
  78. /* vertices relative to ray origin */
  79. const Vec3vf<M> org = Vec3vf<M>(pre.org.x,pre.org.y,pre.org.z);
  80. const Vec3vf<M> A = Vec3vf<M>(tri_v0[pre.kx],tri_v0[pre.ky],tri_v0[pre.kz]) - org;
  81. const Vec3vf<M> B = Vec3vf<M>(tri_v1[pre.kx],tri_v1[pre.ky],tri_v1[pre.kz]) - org;
  82. const Vec3vf<M> C = Vec3vf<M>(tri_v2[pre.kx],tri_v2[pre.ky],tri_v2[pre.kz]) - org;
  83. /* shear and scale vertices */
  84. const vfloat<M> Ax = nmadd(A.z,pre.S.x,A.x);
  85. const vfloat<M> Ay = nmadd(A.z,pre.S.y,A.y);
  86. const vfloat<M> Bx = nmadd(B.z,pre.S.x,B.x);
  87. const vfloat<M> By = nmadd(B.z,pre.S.y,B.y);
  88. const vfloat<M> Cx = nmadd(C.z,pre.S.x,C.x);
  89. const vfloat<M> Cy = nmadd(C.z,pre.S.y,C.y);
  90. /* scaled barycentric */
  91. const vfloat<M> U0 = Cx*By;
  92. const vfloat<M> U1 = Cy*Bx;
  93. const vfloat<M> V0 = Ax*Cy;
  94. const vfloat<M> V1 = Ay*Cx;
  95. const vfloat<M> W0 = Bx*Ay;
  96. const vfloat<M> W1 = By*Ax;
  97. #if !defined(__AVX512F__)
  98. valid &= (U0 >= U1) & (V0 >= V1) & (W0 >= W1) |
  99. (U0 <= U1) & (V0 <= V1) & (W0 <= W1);
  100. #else
  101. valid &= ge(ge(U0 >= U1,V0,V1),W0,W1) | le(le(U0 <= U1,V0,V1),W0,W1);
  102. #endif
  103. if (likely(none(valid))) return false;
  104. const vfloat<M> U = U0-U1;
  105. const vfloat<M> V = V0-V1;
  106. const vfloat<M> W = W0-W1;
  107. const vfloat<M> det = U+V+W;
  108. valid &= det != 0.0f;
  109. const vfloat<M> inv_det = rcp(det);
  110. const vfloat<M> Az = pre.S.z * A.z;
  111. const vfloat<M> Bz = pre.S.z * B.z;
  112. const vfloat<M> Cz = pre.S.z * C.z;
  113. const vfloat<M> T = madd(U,Az,madd(V,Bz,W*Cz));
  114. const vfloat<M> t = T * inv_det;
  115. /* perform depth test */
  116. valid &= (vfloat<M>(ray.tnear()) < t) & (t <= vfloat<M>(ray.tfar));
  117. if (likely(none(valid))) return false;
  118. const Vec3vf<M> tri_Ng = cross(tri_v2-tri_v0,tri_v0-tri_v1);
  119. /* update hit information */
  120. new (&hit) WoopHitM<M>(valid,U,V,t,inv_det,tri_Ng);
  121. return true;
  122. }
  123. static __forceinline bool intersect(Ray& ray,
  124. const Precalculations& pre,
  125. const Vec3vf<M>& v0,
  126. const Vec3vf<M>& v1,
  127. const Vec3vf<M>& v2,
  128. WoopHitM<M>& hit)
  129. {
  130. vbool<M> valid = true;
  131. return intersect(valid,ray,pre,v0,v1,v2,hit);
  132. }
  133. template<typename Epilog>
  134. static __forceinline bool intersect(Ray& ray,
  135. const Precalculations& pre,
  136. const Vec3vf<M>& v0,
  137. const Vec3vf<M>& v1,
  138. const Vec3vf<M>& v2,
  139. const Epilog& epilog)
  140. {
  141. WoopHitM<M> hit;
  142. if (likely(intersect(ray,pre,v0,v1,v2,hit))) return epilog(hit.valid,hit);
  143. return false;
  144. }
  145. template<typename Epilog>
  146. static __forceinline bool intersect(const vbool<M>& valid,
  147. Ray& ray,
  148. const Precalculations& pre,
  149. const Vec3vf<M>& v0,
  150. const Vec3vf<M>& v1,
  151. const Vec3vf<M>& v2,
  152. const Epilog& epilog)
  153. {
  154. WoopHitM<M> hit;
  155. if (likely(intersect(valid,ray,pre,v0,v1,v2,hit))) return epilog(hit.valid,hit);
  156. return false;
  157. }
  158. };
  159. #if 0
  160. template<int K>
  161. struct WoopHitK
  162. {
  163. __forceinline WoopHitK(const vfloat<K>& U, const vfloat<K>& V, const vfloat<K>& T, const vfloat<K>& absDen, const Vec3vf<K>& Ng)
  164. : U(U), V(V), T(T), absDen(absDen), Ng(Ng) {}
  165. __forceinline std::tuple<vfloat<K>,vfloat<K>,vfloat<K>,Vec3vf<K>> operator() () const
  166. {
  167. const vfloat<K> rcpAbsDen = rcp(absDen);
  168. const vfloat<K> t = T * rcpAbsDen;
  169. const vfloat<K> u = U * rcpAbsDen;
  170. const vfloat<K> v = V * rcpAbsDen;
  171. return std::make_tuple(u,v,t,Ng);
  172. }
  173. private:
  174. const vfloat<K> U;
  175. const vfloat<K> V;
  176. const vfloat<K> T;
  177. const vfloat<K> absDen;
  178. const Vec3vf<K> Ng;
  179. };
  180. template<int M, int K>
  181. struct WoopIntersectorK
  182. {
  183. __forceinline WoopIntersectorK(const vbool<K>& valid, const RayK<K>& ray) {}
  184. /*! Intersects K rays with one of M triangles. */
  185. template<typename Epilog>
  186. __forceinline vbool<K> intersectK(const vbool<K>& valid0,
  187. //RayK<K>& ray,
  188. const Vec3vf<K>& ray_org,
  189. const Vec3vf<K>& ray_dir,
  190. const vfloat<K>& ray_tnear,
  191. const vfloat<K>& ray_tfar,
  192. const Vec3vf<K>& tri_v0,
  193. const Vec3vf<K>& tri_e1,
  194. const Vec3vf<K>& tri_e2,
  195. const Vec3vf<K>& tri_Ng,
  196. const Epilog& epilog) const
  197. {
  198. /* calculate denominator */
  199. vbool<K> valid = valid0;
  200. const Vec3vf<K> C = tri_v0 - ray_org;
  201. const Vec3vf<K> R = cross(C,ray_dir);
  202. const vfloat<K> den = dot(tri_Ng,ray_dir);
  203. const vfloat<K> absDen = abs(den);
  204. const vfloat<K> sgnDen = signmsk(den);
  205. /* test against edge p2 p0 */
  206. const vfloat<K> U = dot(tri_e2,R) ^ sgnDen;
  207. valid &= U >= 0.0f;
  208. if (likely(none(valid))) return false;
  209. /* test against edge p0 p1 */
  210. const vfloat<K> V = dot(tri_e1,R) ^ sgnDen;
  211. valid &= V >= 0.0f;
  212. if (likely(none(valid))) return false;
  213. /* test against edge p1 p2 */
  214. const vfloat<K> W = absDen-U-V;
  215. valid &= W >= 0.0f;
  216. if (likely(none(valid))) return false;
  217. /* perform depth test */
  218. const vfloat<K> T = dot(tri_Ng,C) ^ sgnDen;
  219. valid &= (absDen*ray_tnear < T) & (T <= absDen*ray_tfar);
  220. if (unlikely(none(valid))) return false;
  221. /* perform backface culling */
  222. #if defined(EMBREE_BACKFACE_CULLING)
  223. valid &= den < vfloat<K>(zero);
  224. if (unlikely(none(valid))) return false;
  225. #else
  226. valid &= den != vfloat<K>(zero);
  227. if (unlikely(none(valid))) return false;
  228. #endif
  229. /* calculate hit information */
  230. WoopHitK<K> hit(U,V,T,absDen,tri_Ng);
  231. return epilog(valid,hit);
  232. }
  233. /*! Intersects K rays with one of M triangles. */
  234. template<typename Epilog>
  235. __forceinline vbool<K> intersectK(const vbool<K>& valid0,
  236. RayK<K>& ray,
  237. const Vec3vf<K>& tri_v0,
  238. const Vec3vf<K>& tri_v1,
  239. const Vec3vf<K>& tri_v2,
  240. const Epilog& epilog) const
  241. {
  242. const Vec3vf<K> e1 = tri_v0-tri_v1;
  243. const Vec3vf<K> e2 = tri_v2-tri_v0;
  244. const Vec3vf<K> Ng = cross(e2,e1);
  245. return intersectK(valid0,ray.org,ray.dir,ray.tnear(),ray.tfar,tri_v0,e1,e2,Ng,epilog);
  246. }
  247. /*! Intersects K rays with one of M triangles. */
  248. template<typename Epilog>
  249. __forceinline vbool<K> intersectEdgeK(const vbool<K>& valid0,
  250. RayK<K>& ray,
  251. const Vec3vf<K>& tri_v0,
  252. const Vec3vf<K>& tri_e1,
  253. const Vec3vf<K>& tri_e2,
  254. const Epilog& epilog) const
  255. {
  256. const Vec3vf<K> tri_Ng = cross(tri_e2,tri_e1);
  257. return intersectK(valid0,ray.org,ray.dir,ray.tnear(),ray.tfar,tri_v0,tri_e1,tri_e2,tri_Ng,epilog);
  258. }
  259. /*! Intersect k'th ray from ray packet of size K with M triangles. */
  260. __forceinline bool intersectEdge(RayK<K>& ray,
  261. size_t k,
  262. const Vec3vf<M>& tri_v0,
  263. const Vec3vf<M>& tri_e1,
  264. const Vec3vf<M>& tri_e2,
  265. WoopHitM<M>& hit) const
  266. {
  267. /* calculate denominator */
  268. typedef Vec3vf<M> Vec3vfM;
  269. const Vec3vf<M> tri_Ng = cross(tri_e2,tri_e1);
  270. const Vec3vfM O = broadcast<vfloat<M>>(ray.org,k);
  271. const Vec3vfM D = broadcast<vfloat<M>>(ray.dir,k);
  272. const Vec3vfM C = Vec3vfM(tri_v0) - O;
  273. const Vec3vfM R = cross(C,D);
  274. const vfloat<M> den = dot(Vec3vfM(tri_Ng),D);
  275. const vfloat<M> absDen = abs(den);
  276. const vfloat<M> sgnDen = signmsk(den);
  277. /* perform edge tests */
  278. const vfloat<M> U = dot(Vec3vf<M>(tri_e2),R) ^ sgnDen;
  279. const vfloat<M> V = dot(Vec3vf<M>(tri_e1),R) ^ sgnDen;
  280. /* perform backface culling */
  281. #if defined(EMBREE_BACKFACE_CULLING)
  282. vbool<M> valid = (den < vfloat<M>(zero)) & (U >= 0.0f) & (V >= 0.0f) & (U+V<=absDen);
  283. #else
  284. vbool<M> valid = (den != vfloat<M>(zero)) & (U >= 0.0f) & (V >= 0.0f) & (U+V<=absDen);
  285. #endif
  286. if (likely(none(valid))) return false;
  287. /* perform depth test */
  288. const vfloat<M> T = dot(Vec3vf<M>(tri_Ng),C) ^ sgnDen;
  289. valid &= (absDen*vfloat<M>(ray.tnear()[k]) < T) & (T <= absDen*vfloat<M>(ray.tfar[k]));
  290. if (likely(none(valid))) return false;
  291. /* calculate hit information */
  292. new (&hit) WoopHitM<M>(valid,U,V,T,absDen,tri_Ng);
  293. return true;
  294. }
  295. __forceinline bool intersectEdge(RayK<K>& ray,
  296. size_t k,
  297. const BBox<vfloat<M>>& time_range,
  298. const Vec3vf<M>& tri_v0,
  299. const Vec3vf<M>& tri_e1,
  300. const Vec3vf<M>& tri_e2,
  301. WoopHitM<M>& hit) const
  302. {
  303. if (likely(intersect(ray,k,tri_v0,tri_e1,tri_e2,hit)))
  304. {
  305. hit.valid &= time_range.lower <= vfloat<M>(ray.time[k]);
  306. hit.valid &= vfloat<M>(ray.time[k]) < time_range.upper;
  307. return any(hit.valid);
  308. }
  309. return false;
  310. }
  311. template<typename Epilog>
  312. __forceinline bool intersectEdge(RayK<K>& ray,
  313. size_t k,
  314. const Vec3vf<M>& tri_v0,
  315. const Vec3vf<M>& tri_e1,
  316. const Vec3vf<M>& tri_e2,
  317. const Epilog& epilog) const
  318. {
  319. WoopHitM<M> hit;
  320. if (likely(intersectEdge(ray,k,tri_v0,tri_e1,tri_e2,hit))) return epilog(hit.valid,hit);
  321. return false;
  322. }
  323. template<typename Epilog>
  324. __forceinline bool intersectEdge(RayK<K>& ray,
  325. size_t k,
  326. const BBox<vfloat<M>>& time_range,
  327. const Vec3vf<M>& tri_v0,
  328. const Vec3vf<M>& tri_e1,
  329. const Vec3vf<M>& tri_e2,
  330. const Epilog& epilog) const
  331. {
  332. WoopHitM<M> hit;
  333. if (likely(intersectEdge(ray,k,time_range,tri_v0,tri_e1,tri_e2,hit))) return epilog(hit.valid,hit);
  334. return false;
  335. }
  336. template<typename Epilog>
  337. __forceinline bool intersect(RayK<K>& ray,
  338. size_t k,
  339. const Vec3vf<M>& v0,
  340. const Vec3vf<M>& v1,
  341. const Vec3vf<M>& v2,
  342. const Epilog& epilog) const
  343. {
  344. const Vec3vf<M> e1 = v0-v1;
  345. const Vec3vf<M> e2 = v2-v0;
  346. return intersectEdge(ray,k,v0,e1,e2,epilog);
  347. }
  348. template<typename Epilog>
  349. __forceinline bool intersect(RayK<K>& ray,
  350. size_t k,
  351. const BBox<vfloat<M>>& time_range,
  352. const Vec3vf<M>& v0,
  353. const Vec3vf<M>& v1,
  354. const Vec3vf<M>& v2,
  355. const Epilog& epilog) const
  356. {
  357. const Vec3vf<M> e1 = v0-v1;
  358. const Vec3vf<M> e2 = v2-v0;
  359. return intersectEdge(ray,k,time_range,v0,e1,e2,epilog);
  360. }
  361. };
  362. #endif
  363. }
  364. }