quadi_intersector.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. // ======================================================================== //
  2. // Copyright 2009-2017 Intel Corporation //
  3. // //
  4. // Licensed under the Apache License, Version 2.0 (the "License"); //
  5. // you may not use this file except in compliance with the License. //
  6. // You may obtain a copy of the License at //
  7. // //
  8. // http://www.apache.org/licenses/LICENSE-2.0 //
  9. // //
  10. // Unless required by applicable law or agreed to in writing, software //
  11. // distributed under the License is distributed on an "AS IS" BASIS, //
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
  13. // See the License for the specific language governing permissions and //
  14. // limitations under the License. //
  15. // ======================================================================== //
  16. #pragma once
  17. #include "quadi.h"
  18. #include "quad_intersector_moeller.h"
  19. #include "quad_intersector_pluecker.h"
  20. namespace embree
  21. {
  22. namespace isa
  23. {
  24. /*! Intersects M quads with 1 ray */
  25. template<int M, bool filter>
  26. struct QuadMiIntersector1Moeller
  27. {
  28. typedef QuadMi<M> Primitive;
  29. typedef Intersector1Precalculations<QuadMIntersector1MoellerTrumbore<M,filter>> Precalculations;
  30. /*! Intersect a ray with the M quads and updates the hit. */
  31. static __forceinline void intersect(const Precalculations& pre, Ray& ray, IntersectContext* context, const Primitive& quad)
  32. {
  33. STAT3(normal.trav_prims,1,1,1);
  34. Vec3<vfloat<M>> v0,v1,v2,v3; quad.gather(v0,v1,v2,v3,context->scene);
  35. pre.intersect(ray,context,v0,v1,v2,v3,quad.geomIDs,quad.primIDs);
  36. }
  37. /*! Test if the ray is occluded by one of M quads. */
  38. static __forceinline bool occluded(const Precalculations& pre, Ray& ray, IntersectContext* context, const Primitive& quad)
  39. {
  40. STAT3(shadow.trav_prims,1,1,1);
  41. Vec3<vfloat<M>> v0,v1,v2,v3; quad.gather(v0,v1,v2,v3,context->scene);
  42. return pre.occluded(ray,context,v0,v1,v2,v3,quad.geomIDs,quad.primIDs);
  43. }
  44. /*! Intersect an array of rays with an array of M primitives. */
  45. static __forceinline size_t intersect(Precalculations* pre, size_t valid, Ray** rays, IntersectContext* context, size_t ty, const Primitive* prim, size_t num)
  46. {
  47. size_t valid_isec = 0;
  48. do {
  49. const size_t i = __bscf(valid);
  50. const float old_far = rays[i]->tfar;
  51. for (size_t n=0; n<num; n++)
  52. intersect(pre[i],*rays[i],context,prim[n]);
  53. valid_isec |= (rays[i]->tfar < old_far) ? ((size_t)1 << i) : 0;
  54. } while(unlikely(valid));
  55. return valid_isec;
  56. }
  57. };
  58. /*! Intersects M triangles with K rays. */
  59. template<int M, int K, bool filter>
  60. struct QuadMiIntersectorKMoeller
  61. {
  62. typedef QuadMi<M> Primitive;
  63. typedef IntersectorKPrecalculations<K,QuadMIntersectorKMoellerTrumbore<M,K,filter>> Precalculations;
  64. /*! Intersects K rays with M triangles. */
  65. static __forceinline void intersect(const vbool<K>& valid_i, Precalculations& pre, RayK<K>& ray, IntersectContext* context, const QuadMi<M>& quad)
  66. {
  67. Scene* scene = context->scene;
  68. for (size_t i=0; i<QuadMi<M>::max_size(); i++)
  69. {
  70. if (!quad.valid(i)) break;
  71. STAT3(normal.trav_prims,1,popcnt(valid_i),K);
  72. const Vec3fa & _p0 = quad.getVertex(quad.v0,i,scene);
  73. const Vec3fa & _p1 = quad.getVertex(quad.v1,i,scene);
  74. const Vec3fa & _p2 = quad.getVertex(quad.v2,i,scene);
  75. const Vec3fa & _p3 = quad.getVertex(quad.v3,i,scene);
  76. const Vec3<vfloat<K>> p0(_p0.x,_p0.y,_p0.z);
  77. const Vec3<vfloat<K>> p1(_p1.x,_p1.y,_p1.z);
  78. const Vec3<vfloat<K>> p2(_p2.x,_p2.y,_p2.z);
  79. const Vec3<vfloat<K>> p3(_p3.x,_p3.y,_p3.z);
  80. pre.intersectK(valid_i,ray,p0,p1,p2,p3,IntersectKEpilogM<M,K,filter>(ray,context,quad.geomIDs,quad.primIDs,i));
  81. }
  82. }
  83. /*! Test for K rays if they are occluded by any of the M triangles. */
  84. static __forceinline vbool<K> occluded(const vbool<K>& valid_i, Precalculations& pre, RayK<K>& ray, IntersectContext* context, const QuadMi<M>& quad)
  85. {
  86. Scene* scene = context->scene;
  87. vbool<K> valid0 = valid_i;
  88. for (size_t i=0; i<QuadMi<M>::max_size(); i++)
  89. {
  90. if (!quad.valid(i)) break;
  91. STAT3(shadow.trav_prims,1,popcnt(valid0),K);
  92. const Vec3fa & _p0 = quad.getVertex(quad.v0,i,scene);
  93. const Vec3fa & _p1 = quad.getVertex(quad.v1,i,scene);
  94. const Vec3fa & _p2 = quad.getVertex(quad.v2,i,scene);
  95. const Vec3fa & _p3 = quad.getVertex(quad.v3,i,scene);
  96. const Vec3<vfloat<K>> p0(_p0.x,_p0.y,_p0.z);
  97. const Vec3<vfloat<K>> p1(_p1.x,_p1.y,_p1.z);
  98. const Vec3<vfloat<K>> p2(_p2.x,_p2.y,_p2.z);
  99. const Vec3<vfloat<K>> p3(_p3.x,_p3.y,_p3.z);
  100. if (pre.intersectK(valid0,ray,p0,p1,p2,p3,OccludedKEpilogM<M,K,filter>(valid0,ray,context,quad.geomIDs,quad.primIDs,i)))
  101. break;
  102. }
  103. return !valid0;
  104. }
  105. /*! Intersect a ray with M triangles and updates the hit. */
  106. static __forceinline void intersect(Precalculations& pre, RayK<K>& ray, size_t k, IntersectContext* context, const QuadMi<M>& quad)
  107. {
  108. STAT3(normal.trav_prims,1,1,1);
  109. Vec3vf4 v0,v1,v2,v3; quad.gather(v0,v1,v2,v3,context->scene);
  110. pre.intersect1(ray,k,context,v0,v1,v2,v3,quad.geomIDs,quad.primIDs);
  111. }
  112. /*! Test if the ray is occluded by one of the M triangles. */
  113. static __forceinline bool occluded(Precalculations& pre, RayK<K>& ray, size_t k, IntersectContext* context, const QuadMi<M>& quad)
  114. {
  115. STAT3(shadow.trav_prims,1,1,1);
  116. Vec3vf4 v0,v1,v2,v3; quad.gather(v0,v1,v2,v3,context->scene);
  117. return pre.occluded1(ray,k,context,v0,v1,v2,v3,quad.geomIDs,quad.primIDs);
  118. }
  119. };
  120. /*! Intersects M quads with 1 ray */
  121. template<int M, bool filter>
  122. struct QuadMiIntersector1Pluecker
  123. {
  124. typedef QuadMi<M> Primitive;
  125. typedef Intersector1Precalculations<QuadMIntersector1Pluecker<M,filter>> Precalculations;
  126. /*! Intersect a ray with the M quads and updates the hit. */
  127. static __forceinline void intersect(const Precalculations& pre, Ray& ray, IntersectContext* context, const Primitive& quad)
  128. {
  129. STAT3(normal.trav_prims,1,1,1);
  130. Vec3<vfloat<M>> v0,v1,v2,v3; quad.gather(v0,v1,v2,v3,context->scene);
  131. pre.intersect(ray,context,v0,v1,v2,v3,quad.geomIDs,quad.primIDs);
  132. }
  133. /*! Test if the ray is occluded by one of M quads. */
  134. static __forceinline bool occluded(const Precalculations& pre, Ray& ray, IntersectContext* context, const Primitive& quad)
  135. {
  136. STAT3(shadow.trav_prims,1,1,1);
  137. Vec3<vfloat<M>> v0,v1,v2,v3; quad.gather(v0,v1,v2,v3,context->scene);
  138. return pre.occluded(ray,context,v0,v1,v2,v3,quad.geomIDs,quad.primIDs);
  139. }
  140. /*! Intersect an array of rays with an array of M primitives. */
  141. static __forceinline size_t intersect(Precalculations* pre, size_t valid, Ray** rays, IntersectContext* context, size_t ty, const Primitive* prim, size_t num)
  142. {
  143. size_t valid_isec = 0;
  144. do {
  145. const size_t i = __bscf(valid);
  146. const float old_far = rays[i]->tfar;
  147. for (size_t n=0; n<num; n++)
  148. intersect(pre[i],*rays[i],context,prim[n]);
  149. valid_isec |= (rays[i]->tfar < old_far) ? ((size_t)1 << i) : 0;
  150. } while(unlikely(valid));
  151. return valid_isec;
  152. }
  153. };
  154. /*! Intersects M triangles with K rays. */
  155. template<int M, int K, bool filter>
  156. struct QuadMiIntersectorKPluecker
  157. {
  158. typedef QuadMi<M> Primitive;
  159. typedef IntersectorKPrecalculations<K,QuadMIntersectorKPluecker<M,K,filter>> Precalculations;
  160. /*! Intersects K rays with M triangles. */
  161. static __forceinline void intersect(const vbool<K>& valid_i, Precalculations& pre, RayK<K>& ray, IntersectContext* context, const QuadMi<M>& quad)
  162. {
  163. Scene* scene = context->scene;
  164. for (size_t i=0; i<QuadMi<M>::max_size(); i++)
  165. {
  166. if (!quad.valid(i)) break;
  167. STAT3(normal.trav_prims,1,popcnt(valid_i),K);
  168. const Vec3fa & _p0 = quad.getVertex(quad.v0,i,scene);
  169. const Vec3fa & _p1 = quad.getVertex(quad.v1,i,scene);
  170. const Vec3fa & _p2 = quad.getVertex(quad.v2,i,scene);
  171. const Vec3fa & _p3 = quad.getVertex(quad.v3,i,scene);
  172. const Vec3<vfloat<K>> p0(_p0.x,_p0.y,_p0.z);
  173. const Vec3<vfloat<K>> p1(_p1.x,_p1.y,_p1.z);
  174. const Vec3<vfloat<K>> p2(_p2.x,_p2.y,_p2.z);
  175. const Vec3<vfloat<K>> p3(_p3.x,_p3.y,_p3.z);
  176. pre.intersectK(valid_i,ray,p0,p1,p2,p3,IntersectKEpilogM<M,K,filter>(ray,context,quad.geomIDs,quad.primIDs,i));
  177. }
  178. }
  179. /*! Test for K rays if they are occluded by any of the M triangles. */
  180. static __forceinline vbool<K> occluded(const vbool<K>& valid_i, Precalculations& pre, RayK<K>& ray, IntersectContext* context, const QuadMi<M>& quad)
  181. {
  182. Scene* scene = context->scene;
  183. vbool<K> valid0 = valid_i;
  184. for (size_t i=0; i<QuadMi<M>::max_size(); i++)
  185. {
  186. if (!quad.valid(i)) break;
  187. STAT3(shadow.trav_prims,1,popcnt(valid0),K);
  188. const Vec3fa & _p0 = quad.getVertex(quad.v0,i,scene);
  189. const Vec3fa & _p1 = quad.getVertex(quad.v1,i,scene);
  190. const Vec3fa & _p2 = quad.getVertex(quad.v2,i,scene);
  191. const Vec3fa & _p3 = quad.getVertex(quad.v3,i,scene);
  192. const Vec3<vfloat<K>> p0(_p0.x,_p0.y,_p0.z);
  193. const Vec3<vfloat<K>> p1(_p1.x,_p1.y,_p1.z);
  194. const Vec3<vfloat<K>> p2(_p2.x,_p2.y,_p2.z);
  195. const Vec3<vfloat<K>> p3(_p3.x,_p3.y,_p3.z);
  196. if (pre.intersectK(valid0,ray,p0,p1,p2,p3,OccludedKEpilogM<M,K,filter>(valid0,ray,context,quad.geomIDs,quad.primIDs,i)))
  197. break;
  198. }
  199. return !valid0;
  200. }
  201. /*! Intersect a ray with M triangles and updates the hit. */
  202. static __forceinline void intersect(Precalculations& pre, RayK<K>& ray, size_t k, IntersectContext* context, const QuadMi<M>& quad)
  203. {
  204. STAT3(normal.trav_prims,1,1,1);
  205. Vec3vf4 v0,v1,v2,v3; quad.gather(v0,v1,v2,v3,context->scene);
  206. pre.intersect1(ray,k,context,v0,v1,v2,v3,quad.geomIDs,quad.primIDs);
  207. }
  208. /*! Test if the ray is occluded by one of the M triangles. */
  209. static __forceinline bool occluded(Precalculations& pre, RayK<K>& ray, size_t k, IntersectContext* context, const QuadMi<M>& quad)
  210. {
  211. STAT3(shadow.trav_prims,1,1,1);
  212. Vec3vf4 v0,v1,v2,v3; quad.gather(v0,v1,v2,v3,context->scene);
  213. return pre.occluded1(ray,k,context,v0,v1,v2,v3,quad.geomIDs,quad.primIDs);
  214. }
  215. };
  216. }
  217. }