subgrid_intersector_moeller.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. // Copyright 2009-2021 Intel Corporation
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #include "subgrid.h"
  5. #include "quad_intersector_moeller.h"
  6. namespace embree
  7. {
  8. namespace isa
  9. {
  10. /* ----------------------------- */
  11. /* -- single ray intersectors -- */
  12. /* ----------------------------- */
  13. template<int M>
  14. __forceinline void interpolateUV(MoellerTrumboreHitM<M,UVIdentity<M>> &hit,const GridMesh::Grid &g, const SubGrid& subgrid, const vint<M> &stepX, const vint<M> &stepY)
  15. {
  16. /* correct U,V interpolation across the entire grid */
  17. const vint<M> sx((int)subgrid.x());
  18. const vint<M> sy((int)subgrid.y());
  19. const vint<M> sxM(sx + stepX);
  20. const vint<M> syM(sy + stepY);
  21. const float inv_resX = rcp((float)((int)g.resX-1));
  22. const float inv_resY = rcp((float)((int)g.resY-1));
  23. hit.U = (hit.U + (vfloat<M>)sxM * hit.absDen) * inv_resX;
  24. hit.V = (hit.V + (vfloat<M>)syM * hit.absDen) * inv_resY;
  25. }
  26. template<int M, bool filter>
  27. struct SubGridQuadMIntersector1MoellerTrumbore;
  28. template<int M, bool filter>
  29. struct SubGridQuadMIntersector1MoellerTrumbore
  30. {
  31. __forceinline SubGridQuadMIntersector1MoellerTrumbore() {}
  32. __forceinline SubGridQuadMIntersector1MoellerTrumbore(const Ray& ray, const void* ptr) {}
  33. __forceinline void intersect(RayHit& ray, RayQueryContext* context,
  34. const Vec3vf<M>& v0, const Vec3vf<M>& v1, const Vec3vf<M>& v2, const Vec3vf<M>& v3,
  35. const GridMesh::Grid &g, const SubGrid& subgrid) const
  36. {
  37. UVIdentity<M> mapUV;
  38. MoellerTrumboreHitM<M,UVIdentity<M>> hit(mapUV);
  39. MoellerTrumboreIntersector1<M> intersector(ray,nullptr);
  40. Intersect1EpilogMU<M,filter> epilog(ray,context,subgrid.geomID(),subgrid.primID());
  41. /* intersect first triangle */
  42. if (intersector.intersect(ray,v0,v1,v3,mapUV,hit))
  43. {
  44. interpolateUV<M>(hit,g,subgrid,vint<M>(0,1,1,0),vint<M>(0,0,1,1));
  45. epilog(hit.valid,hit);
  46. }
  47. /* intersect second triangle */
  48. if (intersector.intersect(ray,v2,v3,v1,mapUV,hit))
  49. {
  50. hit.U = hit.absDen - hit.U;
  51. hit.V = hit.absDen - hit.V;
  52. interpolateUV<M>(hit,g,subgrid,vint<M>(0,1,1,0),vint<M>(0,0,1,1));
  53. epilog(hit.valid,hit);
  54. }
  55. }
  56. __forceinline bool occluded(Ray& ray, RayQueryContext* context,
  57. const Vec3vf<M>& v0, const Vec3vf<M>& v1, const Vec3vf<M>& v2, const Vec3vf<M>& v3,
  58. const GridMesh::Grid &g, const SubGrid& subgrid) const
  59. {
  60. UVIdentity<M> mapUV;
  61. MoellerTrumboreHitM<M,UVIdentity<M>> hit(mapUV);
  62. MoellerTrumboreIntersector1<M> intersector(ray,nullptr);
  63. Occluded1EpilogMU<M,filter> epilog(ray,context,subgrid.geomID(),subgrid.primID());
  64. /* intersect first triangle */
  65. if (intersector.intersect(ray,v0,v1,v3,mapUV,hit))
  66. {
  67. interpolateUV<M>(hit,g,subgrid,vint<M>(0,1,1,0),vint<M>(0,0,1,1));
  68. if (epilog(hit.valid,hit))
  69. return true;
  70. }
  71. /* intersect second triangle */
  72. if (intersector.intersect(ray,v2,v3,v1,mapUV,hit))
  73. {
  74. hit.U = hit.absDen - hit.U;
  75. hit.V = hit.absDen - hit.V;
  76. interpolateUV<M>(hit,g,subgrid,vint<M>(0,1,1,0),vint<M>(0,0,1,1));
  77. if (epilog(hit.valid,hit))
  78. return true;
  79. }
  80. return false;
  81. }
  82. };
  83. #if defined (__AVX__)
  84. /*! Intersects 4 quads with 1 ray using AVX */
  85. template<bool filter>
  86. struct SubGridQuadMIntersector1MoellerTrumbore<4,filter>
  87. {
  88. __forceinline SubGridQuadMIntersector1MoellerTrumbore() {}
  89. __forceinline SubGridQuadMIntersector1MoellerTrumbore(const Ray& ray, const void* ptr) {}
  90. template<typename Epilog>
  91. __forceinline bool intersect(Ray& ray, const Vec3vf4& v0, const Vec3vf4& v1, const Vec3vf4& v2, const Vec3vf4& v3, const GridMesh::Grid &g, const SubGrid& subgrid, const Epilog& epilog) const
  92. {
  93. const Vec3vf8 vtx0(vfloat8(v0.x,v2.x),vfloat8(v0.y,v2.y),vfloat8(v0.z,v2.z));
  94. #if !defined(EMBREE_BACKFACE_CULLING)
  95. const Vec3vf8 vtx1(vfloat8(v1.x),vfloat8(v1.y),vfloat8(v1.z));
  96. const Vec3vf8 vtx2(vfloat8(v3.x),vfloat8(v3.y),vfloat8(v3.z));
  97. #else
  98. const Vec3vf8 vtx1(vfloat8(v1.x,v3.x),vfloat8(v1.y,v3.y),vfloat8(v1.z,v3.z));
  99. const Vec3vf8 vtx2(vfloat8(v3.x,v1.x),vfloat8(v3.y,v1.y),vfloat8(v3.z,v1.z));
  100. #endif
  101. UVIdentity<8> mapUV;
  102. MoellerTrumboreHitM<8,UVIdentity<8>> hit(mapUV);
  103. MoellerTrumboreIntersector1<8> intersector(ray,nullptr);
  104. const vbool8 flags(0,0,0,0,1,1,1,1);
  105. if (unlikely(intersector.intersect(ray,vtx0,vtx1,vtx2,mapUV,hit)))
  106. {
  107. /* correct U,V interpolation across the entire grid */
  108. const vfloat8 U = select(flags,hit.absDen - hit.V,hit.U);
  109. const vfloat8 V = select(flags,hit.absDen - hit.U,hit.V);
  110. hit.U = U;
  111. hit.V = V;
  112. hit.vNg *= select(flags,vfloat8(-1.0f),vfloat8(1.0f));
  113. interpolateUV<8>(hit,g,subgrid,vint<8>(0,1,1,0,0,1,1,0),vint<8>(0,0,1,1,0,0,1,1));
  114. if (unlikely(epilog(hit.valid,hit)))
  115. return true;
  116. }
  117. return false;
  118. }
  119. __forceinline bool intersect(RayHit& ray, RayQueryContext* context,
  120. const Vec3vf4& v0, const Vec3vf4& v1, const Vec3vf4& v2, const Vec3vf4& v3,
  121. const GridMesh::Grid &g, const SubGrid& subgrid) const
  122. {
  123. return intersect(ray,v0,v1,v2,v3,g,subgrid,Intersect1EpilogMU<8,filter>(ray,context,subgrid.geomID(),subgrid.primID()));
  124. }
  125. __forceinline bool occluded(Ray& ray, RayQueryContext* context,
  126. const Vec3vf4& v0, const Vec3vf4& v1, const Vec3vf4& v2, const Vec3vf4& v3,
  127. const GridMesh::Grid &g, const SubGrid& subgrid) const
  128. {
  129. return intersect(ray,v0,v1,v2,v3,g,subgrid,Occluded1EpilogMU<8,filter>(ray,context,subgrid.geomID(),subgrid.primID()));
  130. }
  131. };
  132. #endif
  133. // ============================================================================================================================
  134. // ============================================================================================================================
  135. // ============================================================================================================================
  136. /* ----------------------------- */
  137. /* -- ray packet intersectors -- */
  138. /* ----------------------------- */
  139. template<int K>
  140. __forceinline void interpolateUV(const vbool<K>& valid, MoellerTrumboreHitK<K,UVIdentity<K>> &hit,const GridMesh::Grid &g, const SubGrid& subgrid, const unsigned int i)
  141. {
  142. /* correct U,V interpolation across the entire grid */
  143. const unsigned int sx = subgrid.x() + (unsigned int)(i % 2);
  144. const unsigned int sy = subgrid.y() + (unsigned int)(i >>1);
  145. const float inv_resX = rcp((float)(int)(g.resX-1));
  146. const float inv_resY = rcp((float)(int)(g.resY-1));
  147. hit.U = select(valid,(hit.U + vfloat<K>((float)sx) * hit.absDen) * inv_resX,hit.U);
  148. hit.V = select(valid,(hit.V + vfloat<K>((float)sy) * hit.absDen) * inv_resY,hit.V);
  149. }
  150. template<int M, int K, bool filter>
  151. struct SubGridQuadMIntersectorKMoellerTrumboreBase
  152. {
  153. __forceinline SubGridQuadMIntersectorKMoellerTrumboreBase(const vbool<K>& valid, const RayK<K>& ray) {}
  154. template<typename Epilog>
  155. __forceinline bool intersectK(const vbool<K>& valid,
  156. RayK<K>& ray,
  157. const Vec3vf<K>& v0,
  158. const Vec3vf<K>& v1,
  159. const Vec3vf<K>& v2,
  160. const Vec3vf<K>& v3,
  161. const GridMesh::Grid &g,
  162. const SubGrid &subgrid,
  163. const unsigned int i,
  164. const Epilog& epilog) const
  165. {
  166. UVIdentity<K> mapUV;
  167. MoellerTrumboreHitK<K,UVIdentity<K>> hit(mapUV);
  168. MoellerTrumboreIntersectorK<M,K> intersector;
  169. const vbool<K> valid0 = intersector.intersectK(valid,ray,v0,v1,v3,mapUV,hit);
  170. if (any(valid0))
  171. {
  172. interpolateUV(valid0,hit,g,subgrid,i);
  173. epilog(valid0,hit);
  174. }
  175. const vbool<K> valid1 = intersector.intersectK(valid,ray,v2,v3,v1,mapUV,hit);
  176. if (any(valid1))
  177. {
  178. hit.U = hit.absDen - hit.U;
  179. hit.V = hit.absDen - hit.V;
  180. interpolateUV(valid1,hit,g,subgrid,i);
  181. epilog(valid1,hit);
  182. }
  183. return any(valid0|valid1);
  184. }
  185. template<typename Epilog>
  186. __forceinline bool occludedK(const vbool<K>& valid,
  187. RayK<K>& ray,
  188. const Vec3vf<K>& v0,
  189. const Vec3vf<K>& v1,
  190. const Vec3vf<K>& v2,
  191. const Vec3vf<K>& v3,
  192. const GridMesh::Grid &g,
  193. const SubGrid &subgrid,
  194. const unsigned int i,
  195. const Epilog& epilog) const
  196. {
  197. UVIdentity<K> mapUV;
  198. MoellerTrumboreHitK<K,UVIdentity<K>> hit(mapUV);
  199. MoellerTrumboreIntersectorK<M,K> intersector;
  200. vbool<K> valid_final = valid;
  201. const vbool<K> valid0 = intersector.intersectK(valid,ray,v0,v1,v3,mapUV,hit);
  202. if (any(valid0))
  203. {
  204. interpolateUV(valid0,hit,g,subgrid,i);
  205. epilog(valid0,hit);
  206. valid_final &= !valid0;
  207. }
  208. if (none(valid_final)) return true;
  209. const vbool<K> valid1 = intersector.intersectK(valid,ray,v2,v3,v1,mapUV,hit);
  210. if (any(valid1))
  211. {
  212. hit.U = hit.absDen - hit.U;
  213. hit.V = hit.absDen - hit.V;
  214. interpolateUV(valid1,hit,g,subgrid,i);
  215. epilog(valid1,hit);
  216. valid_final &= !valid1;
  217. }
  218. return none(valid_final);
  219. }
  220. static __forceinline bool intersect1(RayK<K>& ray,
  221. size_t k,
  222. const Vec3vf<M>& v0,
  223. const Vec3vf<M>& v1,
  224. const Vec3vf<M>& v2,
  225. MoellerTrumboreHitM<M,UVIdentity<M>> &hit)
  226. {
  227. const Vec3vf<M> e1 = v0-v1;
  228. const Vec3vf<M> e2 = v2-v0;
  229. MoellerTrumboreIntersectorK<8,K> intersector;
  230. UVIdentity<M> mapUV;
  231. return intersector.intersectEdge(ray,k,v0,e1,e2,mapUV,hit);
  232. }
  233. };
  234. template<int M, int K, bool filter>
  235. struct SubGridQuadMIntersectorKMoellerTrumbore : public SubGridQuadMIntersectorKMoellerTrumboreBase<M,K,filter>
  236. {
  237. __forceinline SubGridQuadMIntersectorKMoellerTrumbore(const vbool<K>& valid, const RayK<K>& ray)
  238. : SubGridQuadMIntersectorKMoellerTrumboreBase<M,K,filter>(valid,ray) {}
  239. __forceinline void intersect1(RayHitK<K>& ray, size_t k, RayQueryContext* context,
  240. const Vec3vf<M>& v0, const Vec3vf<M>& v1, const Vec3vf<M>& v2, const Vec3vf<M>& v3, const GridMesh::Grid &g, const SubGrid &subgrid) const
  241. {
  242. UVIdentity<M> mapUV;
  243. MoellerTrumboreHitM<M,UVIdentity<M>> hit(mapUV);
  244. Intersect1KEpilogMU<M,K,filter> epilog(ray,k,context,subgrid.geomID(),subgrid.primID());
  245. MoellerTrumboreIntersectorK<M,K> intersector;
  246. /* intersect first triangle */
  247. if (intersector.intersect(ray,k,v0,v1,v3,mapUV,hit))
  248. {
  249. interpolateUV<M>(hit,g,subgrid,vint<M>(0,1,1,0),vint<M>(0,0,1,1));
  250. epilog(hit.valid,hit);
  251. }
  252. /* intersect second triangle */
  253. if (intersector.intersect(ray,k,v2,v3,v1,mapUV,hit))
  254. {
  255. hit.U = hit.absDen - hit.U;
  256. hit.V = hit.absDen - hit.V;
  257. interpolateUV<M>(hit,g,subgrid,vint<M>(0,1,1,0),vint<M>(0,0,1,1));
  258. epilog(hit.valid,hit);
  259. }
  260. }
  261. __forceinline bool occluded1(RayK<K>& ray, size_t k, RayQueryContext* context,
  262. const Vec3vf<M>& v0, const Vec3vf<M>& v1, const Vec3vf<M>& v2, const Vec3vf<M>& v3, const GridMesh::Grid &g, const SubGrid &subgrid) const
  263. {
  264. UVIdentity<M> mapUV;
  265. MoellerTrumboreHitM<M,UVIdentity<M>> hit(mapUV);
  266. Occluded1KEpilogMU<M,K,filter> epilog(ray,k,context,subgrid.geomID(),subgrid.primID());
  267. MoellerTrumboreIntersectorK<M,K> intersector;
  268. /* intersect first triangle */
  269. if (intersector.intersect(ray,k,v0,v1,v3,mapUV,hit))
  270. {
  271. interpolateUV<M>(hit,g,subgrid,vint<M>(0,1,1,0),vint<M>(0,0,1,1));
  272. if (epilog(hit.valid,hit)) return true;
  273. }
  274. /* intersect second triangle */
  275. if (intersector.intersect(ray,k,v2,v3,v1,mapUV,hit))
  276. {
  277. hit.U = hit.absDen - hit.U;
  278. hit.V = hit.absDen - hit.V;
  279. interpolateUV<M>(hit,g,subgrid,vint<M>(0,1,1,0),vint<M>(0,0,1,1));
  280. if (epilog(hit.valid,hit)) return true;
  281. }
  282. return false;
  283. }
  284. };
  285. #if defined (__AVX__)
  286. /*! Intersects 4 quads with 1 ray using AVX */
  287. template<int K, bool filter>
  288. struct SubGridQuadMIntersectorKMoellerTrumbore<4,K,filter> : public SubGridQuadMIntersectorKMoellerTrumboreBase<4,K,filter>
  289. {
  290. __forceinline SubGridQuadMIntersectorKMoellerTrumbore(const vbool<K>& valid, const RayK<K>& ray)
  291. : SubGridQuadMIntersectorKMoellerTrumboreBase<4,K,filter>(valid,ray) {}
  292. template<typename Epilog>
  293. __forceinline bool intersect1(RayK<K>& ray, size_t k,const Vec3vf4& v0, const Vec3vf4& v1, const Vec3vf4& v2, const Vec3vf4& v3,
  294. const GridMesh::Grid &g, const SubGrid &subgrid, const Epilog& epilog) const
  295. {
  296. const Vec3vf8 vtx0(vfloat8(v0.x,v2.x),vfloat8(v0.y,v2.y),vfloat8(v0.z,v2.z));
  297. #if !defined(EMBREE_BACKFACE_CULLING)
  298. const Vec3vf8 vtx1(vfloat8(v1.x),vfloat8(v1.y),vfloat8(v1.z));
  299. const Vec3vf8 vtx2(vfloat8(v3.x),vfloat8(v3.y),vfloat8(v3.z));
  300. #else
  301. const Vec3vf8 vtx1(vfloat8(v1.x,v3.x),vfloat8(v1.y,v3.y),vfloat8(v1.z,v3.z));
  302. const Vec3vf8 vtx2(vfloat8(v3.x,v1.x),vfloat8(v3.y,v1.y),vfloat8(v3.z,v1.z));
  303. #endif
  304. const vbool8 flags(0,0,0,0,1,1,1,1);
  305. UVIdentity<8> mapUV;
  306. MoellerTrumboreHitM<8,UVIdentity<8>> hit(mapUV);
  307. if (SubGridQuadMIntersectorKMoellerTrumboreBase<8,K,filter>::intersect1(ray,k,vtx0,vtx1,vtx2,hit))
  308. {
  309. const vfloat8 U = select(flags,hit.absDen - hit.V,hit.U);
  310. const vfloat8 V = select(flags,hit.absDen - hit.U,hit.V);
  311. hit.U = U;
  312. hit.V = V;
  313. hit.vNg *= select(flags,vfloat8(-1.0f),vfloat8(1.0f));
  314. interpolateUV<8>(hit,g,subgrid,vint<8>(0,1,1,0,0,1,1,0),vint<8>(0,0,1,1,0,0,1,1));
  315. if (unlikely(epilog(hit.valid,hit)))
  316. return true;
  317. }
  318. return false;
  319. }
  320. __forceinline bool intersect1(RayHitK<K>& ray, size_t k, RayQueryContext* context,
  321. const Vec3vf4& v0, const Vec3vf4& v1, const Vec3vf4& v2, const Vec3vf4& v3, const GridMesh::Grid &g, const SubGrid &subgrid) const
  322. {
  323. return intersect1(ray,k,v0,v1,v2,v3,g,subgrid,Intersect1KEpilogMU<8,K,filter>(ray,k,context,subgrid.geomID(),subgrid.primID()));
  324. }
  325. __forceinline bool occluded1(RayK<K>& ray, size_t k, RayQueryContext* context,
  326. const Vec3vf4& v0, const Vec3vf4& v1, const Vec3vf4& v2, const Vec3vf4& v3, const GridMesh::Grid &g, const SubGrid &subgrid) const
  327. {
  328. return intersect1(ray,k,v0,v1,v2,v3,g,subgrid,Occluded1KEpilogMU<8,K,filter>(ray,k,context,subgrid.geomID(),subgrid.primID()));
  329. }
  330. };
  331. #endif
  332. }
  333. }