quadv_intersector.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 "quadv.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 QuadMvIntersector1Moeller
  27. {
  28. typedef QuadMv<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. pre.intersect(ray,context,quad.v0,quad.v1,quad.v2,quad.v3,quad.geomIDs,quad.primIDs);
  35. }
  36. /*! Test if the ray is occluded by one of M quads. */
  37. static __forceinline bool occluded(const Precalculations& pre, Ray& ray, IntersectContext* context, const Primitive& quad)
  38. {
  39. STAT3(shadow.trav_prims,1,1,1);
  40. return pre.occluded(ray,context, quad.v0,quad.v1,quad.v2,quad.v3,quad.geomIDs,quad.primIDs);
  41. }
  42. /*! Intersect an array of rays with an array of M primitives. */
  43. static __forceinline size_t intersect(Precalculations* pre, size_t valid, Ray** rays, IntersectContext* context, size_t ty, const Primitive* prim, size_t num)
  44. {
  45. size_t valid_isec = 0;
  46. do {
  47. const size_t i = __bscf(valid);
  48. const float old_far = rays[i]->tfar;
  49. for (size_t n=0; n<num; n++)
  50. intersect(pre[i],*rays[i],context,prim[n]);
  51. valid_isec |= (rays[i]->tfar < old_far) ? ((size_t)1 << i) : 0;
  52. } while(unlikely(valid));
  53. return valid_isec;
  54. }
  55. };
  56. /*! Intersects M triangles with K rays. */
  57. template<int M, int K, bool filter>
  58. struct QuadMvIntersectorKMoeller
  59. {
  60. typedef QuadMv<M> Primitive;
  61. typedef IntersectorKPrecalculations<K,QuadMIntersectorKMoellerTrumbore<M,K,filter>> Precalculations;
  62. /*! Intersects K rays with M triangles. */
  63. static __forceinline void intersect(const vbool<K>& valid_i, Precalculations& pre, RayK<K>& ray, IntersectContext* context, const QuadMv<M>& quad)
  64. {
  65. for (size_t i=0; i<QuadMv<M>::max_size(); i++)
  66. {
  67. if (!quad.valid(i)) break;
  68. STAT3(normal.trav_prims,1,popcnt(valid_i),K);
  69. const Vec3<vfloat<K>> p0 = broadcast<vfloat<K>>(quad.v0,i);
  70. const Vec3<vfloat<K>> p1 = broadcast<vfloat<K>>(quad.v1,i);
  71. const Vec3<vfloat<K>> p2 = broadcast<vfloat<K>>(quad.v2,i);
  72. const Vec3<vfloat<K>> p3 = broadcast<vfloat<K>>(quad.v3,i);
  73. pre.intersectK(valid_i,ray,p0,p1,p2,p3,IntersectKEpilogM<M,K,filter>(ray,context,quad.geomIDs,quad.primIDs,i));
  74. }
  75. }
  76. /*! Test for K rays if they are occluded by any of the M triangles. */
  77. static __forceinline vbool<K> occluded(const vbool<K>& valid_i, Precalculations& pre, RayK<K>& ray, IntersectContext* context, const QuadMv<M>& quad)
  78. {
  79. vbool<K> valid0 = valid_i;
  80. for (size_t i=0; i<QuadMv<M>::max_size(); i++)
  81. {
  82. if (!quad.valid(i)) break;
  83. STAT3(shadow.trav_prims,1,popcnt(valid0),K);
  84. const Vec3<vfloat<K>> p0 = broadcast<vfloat<K>>(quad.v0,i);
  85. const Vec3<vfloat<K>> p1 = broadcast<vfloat<K>>(quad.v1,i);
  86. const Vec3<vfloat<K>> p2 = broadcast<vfloat<K>>(quad.v2,i);
  87. const Vec3<vfloat<K>> p3 = broadcast<vfloat<K>>(quad.v3,i);
  88. if (pre.intersectK(valid0,ray,p0,p1,p2,p3,OccludedKEpilogM<M,K,filter>(valid0,ray,context,quad.geomIDs,quad.primIDs,i)))
  89. break;
  90. }
  91. return !valid0;
  92. }
  93. /*! Intersect a ray with M triangles and updates the hit. */
  94. static __forceinline void intersect(Precalculations& pre, RayK<K>& ray, size_t k, IntersectContext* context, const QuadMv<M>& quad)
  95. {
  96. STAT3(normal.trav_prims,1,1,1);
  97. pre.intersect1(ray,k,context,quad.v0,quad.v1,quad.v2,quad.v3,quad.geomIDs,quad.primIDs);
  98. }
  99. /*! Test if the ray is occluded by one of the M triangles. */
  100. static __forceinline bool occluded(Precalculations& pre, RayK<K>& ray, size_t k, IntersectContext* context, const QuadMv<M>& quad)
  101. {
  102. STAT3(shadow.trav_prims,1,1,1);
  103. return pre.occluded1(ray,k,context,quad.v0,quad.v1,quad.v2,quad.v3,quad.geomIDs,quad.primIDs);
  104. }
  105. };
  106. /*! Intersects M quads with 1 ray */
  107. template<int M, bool filter>
  108. struct QuadMvIntersector1Pluecker
  109. {
  110. typedef QuadMv<M> Primitive;
  111. typedef Intersector1Precalculations<QuadMIntersector1Pluecker<M,filter>> Precalculations;
  112. /*! Intersect a ray with the M quads and updates the hit. */
  113. static __forceinline void intersect(const Precalculations& pre, Ray& ray, IntersectContext* context, const Primitive& quad)
  114. {
  115. STAT3(normal.trav_prims,1,1,1);
  116. pre.intersect(ray,context,quad.v0,quad.v1,quad.v2,quad.v3,quad.geomIDs,quad.primIDs);
  117. }
  118. /*! Test if the ray is occluded by one of M quads. */
  119. static __forceinline bool occluded(const Precalculations& pre, Ray& ray, IntersectContext* context, const Primitive& quad)
  120. {
  121. STAT3(shadow.trav_prims,1,1,1);
  122. return pre.occluded(ray,context, quad.v0,quad.v1,quad.v2,quad.v3,quad.geomIDs,quad.primIDs);
  123. }
  124. /*! Intersect an array of rays with an array of M primitives. */
  125. static __forceinline size_t intersect(Precalculations* pre, size_t valid, Ray** rays, IntersectContext* context, size_t ty, const Primitive* prim, size_t num)
  126. {
  127. size_t valid_isec = 0;
  128. do {
  129. const size_t i = __bscf(valid);
  130. const float old_far = rays[i]->tfar;
  131. for (size_t n=0; n<num; n++)
  132. intersect(pre[i],*rays[i],context,prim[n]);
  133. valid_isec |= (rays[i]->tfar < old_far) ? ((size_t)1 << i) : 0;
  134. } while(unlikely(valid));
  135. return valid_isec;
  136. }
  137. };
  138. /*! Intersects M triangles with K rays. */
  139. template<int M, int K, bool filter>
  140. struct QuadMvIntersectorKPluecker
  141. {
  142. typedef QuadMv<M> Primitive;
  143. typedef IntersectorKPrecalculations<K,QuadMIntersectorKPluecker<M,K,filter>> Precalculations;
  144. /*! Intersects K rays with M triangles. */
  145. static __forceinline void intersect(const vbool<K>& valid_i, Precalculations& pre, RayK<K>& ray, IntersectContext* context, const QuadMv<M>& quad)
  146. {
  147. for (size_t i=0; i<QuadMv<M>::max_size(); i++)
  148. {
  149. if (!quad.valid(i)) break;
  150. STAT3(normal.trav_prims,1,popcnt(valid_i),K);
  151. const Vec3<vfloat<K>> p0 = broadcast<vfloat<K>>(quad.v0,i);
  152. const Vec3<vfloat<K>> p1 = broadcast<vfloat<K>>(quad.v1,i);
  153. const Vec3<vfloat<K>> p2 = broadcast<vfloat<K>>(quad.v2,i);
  154. const Vec3<vfloat<K>> p3 = broadcast<vfloat<K>>(quad.v3,i);
  155. pre.intersectK(valid_i,ray,p0,p1,p2,p3,IntersectKEpilogM<M,K,filter>(ray,context,quad.geomIDs,quad.primIDs,i));
  156. }
  157. }
  158. /*! Test for K rays if they are occluded by any of the M triangles. */
  159. static __forceinline vbool<K> occluded(const vbool<K>& valid_i, Precalculations& pre, RayK<K>& ray, IntersectContext* context, const QuadMv<M>& quad)
  160. {
  161. vbool<K> valid0 = valid_i;
  162. for (size_t i=0; i<QuadMv<M>::max_size(); i++)
  163. {
  164. if (!quad.valid(i)) break;
  165. STAT3(shadow.trav_prims,1,popcnt(valid0),K);
  166. const Vec3<vfloat<K>> p0 = broadcast<vfloat<K>>(quad.v0,i);
  167. const Vec3<vfloat<K>> p1 = broadcast<vfloat<K>>(quad.v1,i);
  168. const Vec3<vfloat<K>> p2 = broadcast<vfloat<K>>(quad.v2,i);
  169. const Vec3<vfloat<K>> p3 = broadcast<vfloat<K>>(quad.v3,i);
  170. if (pre.intersectK(valid0,ray,p0,p1,p2,p3,OccludedKEpilogM<M,K,filter>(valid0,ray,context,quad.geomIDs,quad.primIDs,i)))
  171. break;
  172. }
  173. return !valid0;
  174. }
  175. /*! Intersect a ray with M triangles and updates the hit. */
  176. static __forceinline void intersect(Precalculations& pre, RayK<K>& ray, size_t k, IntersectContext* context, const QuadMv<M>& quad)
  177. {
  178. STAT3(normal.trav_prims,1,1,1);
  179. pre.intersect1(ray,k,context,quad.v0,quad.v1,quad.v2,quad.v3,quad.geomIDs,quad.primIDs);
  180. }
  181. /*! Test if the ray is occluded by one of the M triangles. */
  182. static __forceinline bool occluded(Precalculations& pre, RayK<K>& ray, size_t k, IntersectContext* context, const QuadMv<M>& quad)
  183. {
  184. STAT3(shadow.trav_prims,1,1,1);
  185. return pre.occluded1(ray,k,context,quad.v0,quad.v1,quad.v2,quad.v3,quad.geomIDs,quad.primIDs);
  186. }
  187. };
  188. }
  189. }