trianglev_intersector.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 "triangle.h"
  18. #include "triangle_intersector_pluecker.h"
  19. #include "triangle_intersector_moeller.h"
  20. namespace embree
  21. {
  22. namespace isa
  23. {
  24. /*! Intersects M triangles with 1 ray */
  25. template<int M, int Mx, bool filter>
  26. struct TriangleMvIntersector1Moeller
  27. {
  28. typedef TriangleMv<M> Primitive;
  29. typedef Intersector1Precalculations<MoellerTrumboreIntersector1<Mx>> Precalculations;
  30. /*! Intersect a ray with M triangles and updates the hit. */
  31. static __forceinline void intersect(Precalculations& pre, Ray& ray, IntersectContext* context, const Primitive& tri)
  32. {
  33. STAT3(normal.trav_prims,1,1,1);
  34. pre.intersect(ray,tri.v0,tri.v1,tri.v2,/*UVIdentity<Mx>(),*/Intersect1EpilogM<M,Mx,filter>(ray,context,tri.geomIDs,tri.primIDs));
  35. }
  36. /*! Test if the ray is occluded by one of the M triangles. */
  37. static __forceinline bool occluded(const Precalculations& pre, Ray& ray, IntersectContext* context, const Primitive& tri)
  38. {
  39. STAT3(shadow.trav_prims,1,1,1);
  40. return pre.intersect(ray,tri.v0,tri.v1,tri.v2,/*UVIdentity<Mx>(),*/Occluded1EpilogM<M,Mx,filter>(ray,context,tri.geomIDs,tri.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 Mx, int K, bool filter>
  58. struct TriangleMvIntersectorKMoeller
  59. {
  60. typedef TriangleMv<M> Primitive;
  61. typedef IntersectorKPrecalculations<K,MoellerTrumboreIntersectorK<Mx,K>> 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 Primitive& tri)
  64. {
  65. for (size_t i=0; i<M; i++)
  66. {
  67. if (!tri.valid(i)) break;
  68. STAT3(normal.trav_prims,1,popcnt(valid_i),K);
  69. const Vec3<vfloat<K>> v0 = broadcast<vfloat<K>>(tri.v0,i);
  70. const Vec3<vfloat<K>> v1 = broadcast<vfloat<K>>(tri.v1,i);
  71. const Vec3<vfloat<K>> v2 = broadcast<vfloat<K>>(tri.v2,i);
  72. pre.intersectK(valid_i,ray,v0,v1,v2,/*UVIdentity<K>(),*/IntersectKEpilogM<M,K,filter>(ray,context,tri.geomIDs,tri.primIDs,i));
  73. }
  74. }
  75. /*! Test for K rays if they are occluded by any of the M triangles. */
  76. static __forceinline vbool<K> occluded(const vbool<K>& valid_i, Precalculations& pre, RayK<K>& ray, IntersectContext* context, const Primitive& tri)
  77. {
  78. vbool<K> valid0 = valid_i;
  79. for (size_t i=0; i<M; i++)
  80. {
  81. if (!tri.valid(i)) break;
  82. STAT3(shadow.trav_prims,1,popcnt(valid_i),K);
  83. const Vec3<vfloat<K>> v0 = broadcast<vfloat<K>>(tri.v0,i);
  84. const Vec3<vfloat<K>> v1 = broadcast<vfloat<K>>(tri.v1,i);
  85. const Vec3<vfloat<K>> v2 = broadcast<vfloat<K>>(tri.v2,i);
  86. pre.intersectK(valid0,ray,v0,v1,v2,/*UVIdentity<K>(),*/OccludedKEpilogM<M,K,filter>(valid0,ray,context,tri.geomIDs,tri.primIDs,i));
  87. if (none(valid0)) break;
  88. }
  89. return !valid0;
  90. }
  91. /*! Intersect a ray with M triangles and updates the hit. */
  92. static __forceinline void intersect(Precalculations& pre, RayK<K>& ray, size_t k, IntersectContext* context, const Primitive& tri)
  93. {
  94. STAT3(normal.trav_prims,1,1,1);
  95. pre.intersect(ray,k,tri.v0,tri.v1,tri.v2,/*UVIdentity<Mx>(),*/Intersect1KEpilogM<M,Mx,K,filter>(ray,k,context,tri.geomIDs,tri.primIDs)); //FIXME: M,Mx
  96. }
  97. /*! Test if the ray is occluded by one of the M triangles. */
  98. static __forceinline bool occluded(Precalculations& pre, RayK<K>& ray, size_t k, IntersectContext* context, const Primitive& tri)
  99. {
  100. STAT3(shadow.trav_prims,1,1,1);
  101. return pre.intersect(ray,k,tri.v0,tri.v1,tri.v2,/*UVIdentity<Mx>(),*/Occluded1KEpilogM<M,Mx,K,filter>(ray,k,context,tri.geomIDs,tri.primIDs)); //FIXME: M,Mx
  102. }
  103. };
  104. /*! Intersects M triangles with 1 ray */
  105. template<int M, int Mx, bool filter>
  106. struct TriangleMvIntersector1Pluecker
  107. {
  108. typedef TriangleMv<M> Primitive;
  109. typedef Intersector1Precalculations<PlueckerIntersector1<Mx>> Precalculations;
  110. /*! Intersect a ray with M triangles and updates the hit. */
  111. static __forceinline void intersect(Precalculations& pre, Ray& ray, IntersectContext* context, const Primitive& tri)
  112. {
  113. STAT3(normal.trav_prims,1,1,1);
  114. pre.intersect(ray,tri.v0,tri.v1,tri.v2,UVIdentity<Mx>(),Intersect1EpilogM<M,Mx,filter>(ray,context,tri.geomIDs,tri.primIDs));
  115. }
  116. /*! Test if the ray is occluded by one of the M triangles. */
  117. static __forceinline bool occluded(const Precalculations& pre, Ray& ray, IntersectContext* context, const Primitive& tri)
  118. {
  119. STAT3(shadow.trav_prims,1,1,1);
  120. return pre.intersect(ray,tri.v0,tri.v1,tri.v2,UVIdentity<Mx>(),Occluded1EpilogM<M,Mx,filter>(ray,context,tri.geomIDs,tri.primIDs));
  121. }
  122. /*! Intersect an array of rays with an array of M primitives. */
  123. static __forceinline size_t intersect(Precalculations* pre, size_t valid, Ray** rays, IntersectContext* context, size_t ty, const Primitive* prim, size_t num)
  124. {
  125. size_t valid_isec = 0;
  126. do {
  127. const size_t i = __bscf(valid);
  128. const float old_far = rays[i]->tfar;
  129. for (size_t n=0; n<num; n++)
  130. intersect(pre[i],*rays[i],context,prim[n]);
  131. valid_isec |= (rays[i]->tfar < old_far) ? ((size_t)1 << i) : 0;
  132. } while(unlikely(valid));
  133. return valid_isec;
  134. }
  135. };
  136. /*! Intersects M triangles with K rays */
  137. template<int M, int Mx, int K, bool filter>
  138. struct TriangleMvIntersectorKPluecker
  139. {
  140. typedef TriangleMv<M> Primitive;
  141. typedef IntersectorKPrecalculations<K,PlueckerIntersectorK<Mx,K>> Precalculations;
  142. /*! Intersects K rays with M triangles. */
  143. static __forceinline void intersect(const vbool<K>& valid_i, Precalculations& pre, RayK<K>& ray, IntersectContext* context, const Primitive& tri)
  144. {
  145. for (size_t i=0; i<M; i++)
  146. {
  147. if (!tri.valid(i)) break;
  148. STAT3(normal.trav_prims,1,popcnt(valid_i),K);
  149. const Vec3<vfloat<K>> v0 = broadcast<vfloat<K>>(tri.v0,i);
  150. const Vec3<vfloat<K>> v1 = broadcast<vfloat<K>>(tri.v1,i);
  151. const Vec3<vfloat<K>> v2 = broadcast<vfloat<K>>(tri.v2,i);
  152. pre.intersectK(valid_i,ray,v0,v1,v2,UVIdentity<K>(),IntersectKEpilogM<M,K,filter>(ray,context,tri.geomIDs,tri.primIDs,i));
  153. }
  154. }
  155. /*! Test for K rays if they are occluded by any of the M triangles. */
  156. static __forceinline vbool<K> occluded(const vbool<K>& valid_i, Precalculations& pre, RayK<K>& ray, IntersectContext* context, const Primitive& tri)
  157. {
  158. vbool<K> valid0 = valid_i;
  159. for (size_t i=0; i<M; i++)
  160. {
  161. if (!tri.valid(i)) break;
  162. STAT3(shadow.trav_prims,1,popcnt(valid_i),K);
  163. const Vec3<vfloat<K>> v0 = broadcast<vfloat<K>>(tri.v0,i);
  164. const Vec3<vfloat<K>> v1 = broadcast<vfloat<K>>(tri.v1,i);
  165. const Vec3<vfloat<K>> v2 = broadcast<vfloat<K>>(tri.v2,i);
  166. pre.intersectK(valid0,ray,v0,v1,v2,UVIdentity<K>(),OccludedKEpilogM<M,K,filter>(valid0,ray,context,tri.geomIDs,tri.primIDs,i));
  167. if (none(valid0)) break;
  168. }
  169. return !valid0;
  170. }
  171. /*! Intersect a ray with M triangles and updates the hit. */
  172. static __forceinline void intersect(Precalculations& pre, RayK<K>& ray, size_t k, IntersectContext* context, const Primitive& tri)
  173. {
  174. STAT3(normal.trav_prims,1,1,1);
  175. pre.intersect(ray,k,tri.v0,tri.v1,tri.v2,UVIdentity<Mx>(),Intersect1KEpilogM<M,Mx,K,filter>(ray,k,context,tri.geomIDs,tri.primIDs)); //FIXME: M,Mx
  176. }
  177. /*! Test if the ray is occluded by one of the M triangles. */
  178. static __forceinline bool occluded(Precalculations& pre, RayK<K>& ray, size_t k, IntersectContext* context, const Primitive& tri)
  179. {
  180. STAT3(shadow.trav_prims,1,1,1);
  181. return pre.intersect(ray,k,tri.v0,tri.v1,tri.v2,UVIdentity<Mx>(),Occluded1KEpilogM<M,Mx,K,filter>(ray,k,context,tri.geomIDs,tri.primIDs)); //FIXME: M,Mx
  182. }
  183. };
  184. }
  185. }