subgrid_mb_intersector.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. // Copyright 2009-2021 Intel Corporation
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #include "subgrid_intersector.h"
  5. namespace embree
  6. {
  7. namespace isa
  8. {
  9. template<int N, bool filter>
  10. struct SubGridMBIntersector1Pluecker
  11. {
  12. typedef SubGridMBQBVHN<N> Primitive;
  13. typedef SubGridQuadMIntersector1Pluecker<4,filter> Precalculations;
  14. static __forceinline void intersect(const Precalculations& pre, RayHit& ray, IntersectContext* context, const SubGrid& subgrid)
  15. {
  16. STAT3(normal.trav_prims,1,1,1);
  17. const GridMesh* mesh = context->scene->get<GridMesh>(subgrid.geomID());
  18. const GridMesh::Grid &g = mesh->grid(subgrid.primID());
  19. float ftime;
  20. const int itime = mesh->timeSegment(ray.time(), ftime);
  21. Vec3vf4 v0,v1,v2,v3; subgrid.gatherMB(v0,v1,v2,v3,context->scene,itime,ftime);
  22. pre.intersect(ray,context,v0,v1,v2,v3,g,subgrid);
  23. }
  24. static __forceinline bool occluded(const Precalculations& pre, Ray& ray, IntersectContext* context, const SubGrid& subgrid)
  25. {
  26. STAT3(shadow.trav_prims,1,1,1);
  27. const GridMesh* mesh = context->scene->get<GridMesh>(subgrid.geomID());
  28. const GridMesh::Grid &g = mesh->grid(subgrid.primID());
  29. float ftime;
  30. const int itime = mesh->timeSegment(ray.time(), ftime);
  31. Vec3vf4 v0,v1,v2,v3; subgrid.gatherMB(v0,v1,v2,v3,context->scene,itime,ftime);
  32. return pre.occluded(ray,context,v0,v1,v2,v3,g,subgrid);
  33. }
  34. static __forceinline bool pointQuery(PointQuery* query, PointQueryContext* context, const SubGrid& subgrid)
  35. {
  36. return PrimitivePointQuery1<Primitive>::pointQuery(query, context, subgrid);
  37. }
  38. template<bool robust>
  39. static __forceinline void intersect(const Accel::Intersectors* This, Precalculations& pre, RayHit& ray, IntersectContext* context, const Primitive* prim, size_t num, const TravRay<N,robust> &tray, size_t& lazy_node)
  40. {
  41. BVHNQuantizedBaseNodeIntersector1<N,robust> isec1;
  42. for (size_t i=0;i<num;i++)
  43. {
  44. vfloat<N> dist;
  45. const float time = prim[i].adjustTime(ray.time());
  46. assert(time <= 1.0f);
  47. size_t mask = isec1.intersect(&prim[i].qnode,tray,time,dist);
  48. #if defined(__AVX__)
  49. STAT3(normal.trav_hit_boxes[popcnt(mask)],1,1,1);
  50. #endif
  51. while(mask != 0)
  52. {
  53. const size_t ID = bscf(mask);
  54. if (unlikely(dist[ID] > ray.tfar)) continue;
  55. intersect(pre,ray,context,prim[i].subgrid(ID));
  56. }
  57. }
  58. }
  59. template<bool robust>
  60. static __forceinline bool occluded(const Accel::Intersectors* This, Precalculations& pre, Ray& ray, IntersectContext* context, const Primitive* prim, size_t num, const TravRay<N,robust> &tray, size_t& lazy_node)
  61. {
  62. BVHNQuantizedBaseNodeIntersector1<N,robust> isec1;
  63. for (size_t i=0;i<num;i++)
  64. {
  65. const float time = prim[i].adjustTime(ray.time());
  66. assert(time <= 1.0f);
  67. vfloat<N> dist;
  68. size_t mask = isec1.intersect(&prim[i].qnode,tray,time,dist);
  69. while(mask != 0)
  70. {
  71. const size_t ID = bscf(mask);
  72. if (occluded(pre,ray,context,prim[i].subgrid(ID)))
  73. return true;
  74. }
  75. }
  76. return false;
  77. }
  78. static __forceinline bool pointQuery(const Accel::Intersectors* This, PointQuery* query, PointQueryContext* context, const Primitive* prim, size_t num, const TravPointQuery<N> &tquery, size_t& lazy_node)
  79. {
  80. assert(false && "not implemented");
  81. return false;
  82. }
  83. };
  84. template<int N, int K, bool filter>
  85. struct SubGridMBIntersectorKPluecker
  86. {
  87. typedef SubGridMBQBVHN<N> Primitive;
  88. typedef SubGridQuadMIntersectorKPluecker<4,K,filter> Precalculations;
  89. static __forceinline void intersect(const vbool<K>& valid_i, Precalculations& pre, RayHitK<K>& ray, IntersectContext* context, const SubGrid& subgrid)
  90. {
  91. size_t m_valid = movemask(valid_i);
  92. while(m_valid)
  93. {
  94. size_t ID = bscf(m_valid);
  95. intersect(pre,ray,ID,context,subgrid);
  96. }
  97. }
  98. static __forceinline vbool<K> occluded(const vbool<K>& valid_i, Precalculations& pre, RayK<K>& ray, IntersectContext* context, const SubGrid& subgrid)
  99. {
  100. vbool<K> valid0 = valid_i;
  101. size_t m_valid = movemask(valid_i);
  102. while(m_valid)
  103. {
  104. size_t ID = bscf(m_valid);
  105. if (occluded(pre,ray,ID,context,subgrid))
  106. clear(valid0,ID);
  107. }
  108. return !valid0;
  109. }
  110. static __forceinline void intersect(Precalculations& pre, RayHitK<K>& ray, size_t k, IntersectContext* context, const SubGrid& subgrid)
  111. {
  112. STAT3(normal.trav_prims,1,1,1);
  113. const GridMesh* mesh = context->scene->get<GridMesh>(subgrid.geomID());
  114. const GridMesh::Grid &g = mesh->grid(subgrid.primID());
  115. vfloat<K> ftime;
  116. const vint<K> itime = mesh->timeSegment<K>(ray.time(), ftime);
  117. Vec3vf4 v0,v1,v2,v3; subgrid.gatherMB(v0,v1,v2,v3,context->scene,itime[k],ftime[k]);
  118. pre.intersect1(ray,k,context,v0,v1,v2,v3,g,subgrid);
  119. }
  120. static __forceinline bool occluded(Precalculations& pre, RayK<K>& ray, size_t k, IntersectContext* context, const SubGrid& subgrid)
  121. {
  122. STAT3(shadow.trav_prims,1,1,1);
  123. const GridMesh* mesh = context->scene->get<GridMesh>(subgrid.geomID());
  124. const GridMesh::Grid &g = mesh->grid(subgrid.primID());
  125. vfloat<K> ftime;
  126. const vint<K> itime = mesh->timeSegment<K>(ray.time(), ftime);
  127. Vec3vf4 v0,v1,v2,v3; subgrid.gatherMB(v0,v1,v2,v3,context->scene,itime[k],ftime[k]);
  128. return pre.occluded1(ray,k,context,v0,v1,v2,v3,g,subgrid);
  129. }
  130. template<bool robust>
  131. static __forceinline void intersect(const vbool<K>& valid, const Accel::Intersectors* This, Precalculations& pre, RayHitK<K>& ray, IntersectContext* context, const Primitive* prim, size_t num, const TravRayK<K, robust> &tray, size_t& lazy_node)
  132. {
  133. BVHNQuantizedBaseNodeIntersectorK<N,K,robust> isecK;
  134. for (size_t j=0;j<num;j++)
  135. {
  136. size_t m_valid = movemask(prim[j].qnode.validMask());
  137. const vfloat<K> time = prim[j].template adjustTime<K>(ray.time());
  138. vfloat<K> dist;
  139. while(m_valid)
  140. {
  141. const size_t i = bscf(m_valid);
  142. if (none(valid & isecK.intersectK(&prim[j].qnode,i,tray,time,dist))) continue;
  143. intersect(valid,pre,ray,context,prim[j].subgrid(i));
  144. }
  145. }
  146. }
  147. template<bool robust>
  148. static __forceinline vbool<K> occluded(const vbool<K>& valid, const Accel::Intersectors* This, Precalculations& pre, RayK<K>& ray, IntersectContext* context, const Primitive* prim, size_t num, const TravRayK<K, robust> &tray, size_t& lazy_node)
  149. {
  150. BVHNQuantizedBaseNodeIntersectorK<N,K,robust> isecK;
  151. vbool<K> valid0 = valid;
  152. for (size_t j=0;j<num;j++)
  153. {
  154. size_t m_valid = movemask(prim[j].qnode.validMask());
  155. const vfloat<K> time = prim[j].template adjustTime<K>(ray.time());
  156. vfloat<K> dist;
  157. while(m_valid)
  158. {
  159. const size_t i = bscf(m_valid);
  160. if (none(valid0 & isecK.intersectK(&prim[j].qnode,i,tray,time,dist))) continue;
  161. valid0 &= !occluded(valid0,pre,ray,context,prim[j].subgrid(i));
  162. if (none(valid0)) break;
  163. }
  164. }
  165. return !valid0;
  166. }
  167. template<bool robust>
  168. static __forceinline void intersect(const Accel::Intersectors* This, Precalculations& pre, RayHitK<K>& ray, size_t k, IntersectContext* context, const Primitive* prim, size_t num, const TravRay<N,robust> &tray, size_t& lazy_node)
  169. {
  170. BVHNQuantizedBaseNodeIntersector1<N,robust> isec1;
  171. for (size_t i=0;i<num;i++)
  172. {
  173. vfloat<N> dist;
  174. const float time = prim[i].adjustTime(ray.time()[k]);
  175. assert(time <= 1.0f);
  176. size_t mask = isec1.intersect(&prim[i].qnode,tray,time,dist);
  177. while(mask != 0)
  178. {
  179. const size_t ID = bscf(mask);
  180. if (unlikely(dist[ID] > ray.tfar[k])) continue;
  181. intersect(pre,ray,k,context,prim[i].subgrid(ID));
  182. }
  183. }
  184. }
  185. template<bool robust>
  186. static __forceinline bool occluded(const Accel::Intersectors* This, Precalculations& pre, RayK<K>& ray, size_t k, IntersectContext* context, const Primitive* prim, size_t num, const TravRay<N,robust> &tray, size_t& lazy_node)
  187. {
  188. BVHNQuantizedBaseNodeIntersector1<N,robust> isec1;
  189. for (size_t i=0;i<num;i++)
  190. {
  191. vfloat<N> dist;
  192. const float time = prim[i].adjustTime(ray.time()[k]);
  193. assert(time <= 1.0f);
  194. size_t mask = isec1.intersect(&prim[i].qnode,tray,time,dist);
  195. while(mask != 0)
  196. {
  197. const size_t ID = bscf(mask);
  198. if (occluded(pre,ray,k,context,prim[i].subgrid(ID)))
  199. return true;
  200. }
  201. }
  202. return false;
  203. }
  204. };
  205. }
  206. }