bvh_intersector1.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. // Copyright 2009-2021 Intel Corporation
  2. // SPDX-License-Identifier: Apache-2.0
  3. #include "bvh_intersector1.h"
  4. #include "node_intersector1.h"
  5. #include "bvh_traverser1.h"
  6. #include "../geometry/intersector_iterators.h"
  7. #include "../geometry/triangle_intersector.h"
  8. #include "../geometry/trianglev_intersector.h"
  9. #include "../geometry/trianglev_mb_intersector.h"
  10. #include "../geometry/trianglei_intersector.h"
  11. #include "../geometry/quadv_intersector.h"
  12. #include "../geometry/quadi_intersector.h"
  13. #include "../geometry/curveNv_intersector.h"
  14. #include "../geometry/curveNi_intersector.h"
  15. #include "../geometry/curveNi_mb_intersector.h"
  16. #include "../geometry/linei_intersector.h"
  17. #include "../geometry/subdivpatch1_intersector.h"
  18. #include "../geometry/object_intersector.h"
  19. #include "../geometry/instance_intersector.h"
  20. #include "../geometry/instance_array_intersector.h"
  21. #include "../geometry/subgrid_intersector.h"
  22. #include "../geometry/subgrid_mb_intersector.h"
  23. #include "../geometry/curve_intersector_virtual.h"
  24. namespace embree
  25. {
  26. namespace isa
  27. {
  28. template<int N, int types, bool robust, typename PrimitiveIntersector1>
  29. void BVHNIntersector1<N, types, robust, PrimitiveIntersector1>::intersect(const Accel::Intersectors* __restrict__ This,
  30. RayHit& __restrict__ ray,
  31. RayQueryContext* __restrict__ context)
  32. {
  33. const BVH* __restrict__ bvh = (const BVH*)This->ptr;
  34. /* we may traverse an empty BVH in case all geometry was invalid */
  35. if (bvh->root == BVH::emptyNode)
  36. return;
  37. /* perform per ray precalculations required by the primitive intersector */
  38. Precalculations pre(ray, bvh);
  39. /* stack state */
  40. StackItemT<NodeRef> stack[stackSize]; // stack of nodes
  41. StackItemT<NodeRef>* stackPtr = stack+1; // current stack pointer
  42. StackItemT<NodeRef>* stackEnd = stack+stackSize;
  43. stack[0].ptr = bvh->root;
  44. stack[0].dist = neg_inf;
  45. if (bvh->root == BVH::emptyNode)
  46. return;
  47. /* filter out invalid rays */
  48. #if defined(EMBREE_IGNORE_INVALID_RAYS)
  49. if (!ray.valid()) return;
  50. #endif
  51. /* verify correct input */
  52. assert(ray.valid());
  53. assert(ray.tnear() >= 0.0f);
  54. assert(!(types & BVH_MB) || (ray.time() >= 0.0f && ray.time() <= 1.0f));
  55. /* load the ray into SIMD registers */
  56. TravRay<N,robust> tray(ray.org, ray.dir, max(ray.tnear(), 0.0f), max(ray.tfar, 0.0f));
  57. /* initialize the node traverser */
  58. BVHNNodeTraverser1Hit<N, types> nodeTraverser;
  59. /* pop loop */
  60. while (true) pop:
  61. {
  62. /* pop next node */
  63. if (unlikely(stackPtr == stack)) break;
  64. stackPtr--;
  65. NodeRef cur = NodeRef(stackPtr->ptr);
  66. /* if popped node is too far, pop next one */
  67. if (unlikely(*(float*)&stackPtr->dist > ray.tfar))
  68. continue;
  69. /* downtraversal loop */
  70. while (true)
  71. {
  72. /* intersect node */
  73. size_t mask; vfloat<N> tNear;
  74. STAT3(normal.trav_nodes,1,1,1);
  75. bool nodeIntersected = BVHNNodeIntersector1<N, types, robust>::intersect(cur, tray, ray.time(), tNear, mask);
  76. if (unlikely(!nodeIntersected)) { STAT3(normal.trav_nodes,-1,-1,-1); break; }
  77. /* if no child is hit, pop next node */
  78. if (unlikely(mask == 0))
  79. goto pop;
  80. /* select next child and push other children */
  81. nodeTraverser.traverseClosestHit(cur, mask, tNear, stackPtr, stackEnd);
  82. }
  83. /* this is a leaf node */
  84. assert(cur != BVH::emptyNode);
  85. STAT3(normal.trav_leaves,1,1,1);
  86. size_t num; Primitive* prim = (Primitive*)cur.leaf(num);
  87. size_t lazy_node = 0;
  88. PrimitiveIntersector1::intersect(This, pre, ray, context, prim, num, tray, lazy_node);
  89. tray.tfar = ray.tfar;
  90. /* push lazy node onto stack */
  91. if (unlikely(lazy_node)) {
  92. stackPtr->ptr = lazy_node;
  93. stackPtr->dist = neg_inf;
  94. stackPtr++;
  95. }
  96. }
  97. }
  98. template<int N, int types, bool robust, typename PrimitiveIntersector1>
  99. void BVHNIntersector1<N, types, robust, PrimitiveIntersector1>::occluded(const Accel::Intersectors* __restrict__ This,
  100. Ray& __restrict__ ray,
  101. RayQueryContext* __restrict__ context)
  102. {
  103. const BVH* __restrict__ bvh = (const BVH*)This->ptr;
  104. /* we may traverse an empty BVH in case all geometry was invalid */
  105. if (bvh->root == BVH::emptyNode)
  106. return;
  107. /* early out for already occluded rays */
  108. if (unlikely(ray.tfar < 0.0f))
  109. return;
  110. /* perform per ray precalculations required by the primitive intersector */
  111. Precalculations pre(ray, bvh);
  112. /* stack state */
  113. NodeRef stack[stackSize]; // stack of nodes that still need to get traversed
  114. NodeRef* stackPtr = stack+1; // current stack pointer
  115. NodeRef* stackEnd = stack+stackSize;
  116. stack[0] = bvh->root;
  117. /* filter out invalid rays */
  118. #if defined(EMBREE_IGNORE_INVALID_RAYS)
  119. if (!ray.valid()) return;
  120. #endif
  121. /* verify correct input */
  122. assert(ray.valid());
  123. assert(ray.tnear() >= 0.0f);
  124. assert(!(types & BVH_MB) || (ray.time() >= 0.0f && ray.time() <= 1.0f));
  125. /* load the ray into SIMD registers */
  126. TravRay<N,robust> tray(ray.org, ray.dir, max(ray.tnear(), 0.0f), max(ray.tfar, 0.0f));
  127. /* initialize the node traverser */
  128. BVHNNodeTraverser1Hit<N, types> nodeTraverser;
  129. /* pop loop */
  130. while (true) pop:
  131. {
  132. /* pop next node */
  133. if (unlikely(stackPtr == stack)) break;
  134. stackPtr--;
  135. NodeRef cur = (NodeRef)*stackPtr;
  136. /* downtraversal loop */
  137. while (true)
  138. {
  139. /* intersect node */
  140. size_t mask; vfloat<N> tNear;
  141. STAT3(shadow.trav_nodes,1,1,1);
  142. bool nodeIntersected = BVHNNodeIntersector1<N, types, robust>::intersect(cur, tray, ray.time(), tNear, mask);
  143. if (unlikely(!nodeIntersected)) { STAT3(shadow.trav_nodes,-1,-1,-1); break; }
  144. /* if no child is hit, pop next node */
  145. if (unlikely(mask == 0))
  146. goto pop;
  147. /* select next child and push other children */
  148. nodeTraverser.traverseAnyHit(cur, mask, tNear, stackPtr, stackEnd);
  149. }
  150. /* this is a leaf node */
  151. assert(cur != BVH::emptyNode);
  152. STAT3(shadow.trav_leaves,1,1,1);
  153. size_t num; Primitive* prim = (Primitive*)cur.leaf(num);
  154. size_t lazy_node = 0;
  155. if (PrimitiveIntersector1::occluded(This, pre, ray, context, prim, num, tray, lazy_node)) {
  156. ray.tfar = neg_inf;
  157. break;
  158. }
  159. /* push lazy node onto stack */
  160. if (unlikely(lazy_node)) {
  161. *stackPtr = (NodeRef)lazy_node;
  162. stackPtr++;
  163. }
  164. }
  165. }
  166. template<int N, int types, bool robust, typename PrimitiveIntersector1>
  167. struct PointQueryDispatch
  168. {
  169. typedef typename PrimitiveIntersector1::Precalculations Precalculations;
  170. typedef typename PrimitiveIntersector1::Primitive Primitive;
  171. typedef BVHN<N> BVH;
  172. typedef typename BVH::NodeRef NodeRef;
  173. typedef typename BVH::AABBNode AABBNode;
  174. typedef typename BVH::AABBNodeMB4D AABBNodeMB4D;
  175. static const size_t stackSize = 1+(N-1)*BVH::maxDepth+3; // +3 due to 16-wide store
  176. static __forceinline bool pointQuery(const Accel::Intersectors* This, PointQuery* query, PointQueryContext* context)
  177. {
  178. const BVH* __restrict__ bvh = (const BVH*)This->ptr;
  179. /* we may traverse an empty BVH in case all geometry was invalid */
  180. if (bvh->root == BVH::emptyNode)
  181. return false;
  182. /* stack state */
  183. StackItemT<NodeRef> stack[stackSize]; // stack of nodes
  184. StackItemT<NodeRef>* stackPtr = stack+1; // current stack pointer
  185. StackItemT<NodeRef>* stackEnd = stack+stackSize;
  186. stack[0].ptr = bvh->root;
  187. stack[0].dist = neg_inf;
  188. /* verify correct input */
  189. assert(!(types & BVH_MB) || (query->time >= 0.0f && query->time <= 1.0f));
  190. /* load the point query into SIMD registers */
  191. TravPointQuery<N> tquery(query->p, context->query_radius);
  192. /* initialize the node traverser */
  193. BVHNNodeTraverser1Hit<N,types> nodeTraverser;
  194. bool changed = false;
  195. float cull_radius = context->query_type == POINT_QUERY_TYPE_SPHERE
  196. ? query->radius * query->radius
  197. : dot(context->query_radius, context->query_radius);
  198. /* pop loop */
  199. while (true) pop:
  200. {
  201. /* pop next node */
  202. if (unlikely(stackPtr == stack)) break;
  203. stackPtr--;
  204. NodeRef cur = NodeRef(stackPtr->ptr);
  205. /* if popped node is too far, pop next one */
  206. if (unlikely(*(float*)&stackPtr->dist > cull_radius))
  207. continue;
  208. /* downtraversal loop */
  209. while (true)
  210. {
  211. /* intersect node */
  212. size_t mask; vfloat<N> tNear;
  213. STAT3(point_query.trav_nodes,1,1,1);
  214. bool nodeIntersected;
  215. if (likely(context->query_type == POINT_QUERY_TYPE_SPHERE)) {
  216. nodeIntersected = BVHNNodePointQuerySphere1<N, types>::pointQuery(cur, tquery, query->time, tNear, mask);
  217. } else {
  218. nodeIntersected = BVHNNodePointQueryAABB1 <N, types>::pointQuery(cur, tquery, query->time, tNear, mask);
  219. }
  220. if (unlikely(!nodeIntersected)) { STAT3(point_query.trav_nodes,-1,-1,-1); break; }
  221. /* if no child is hit, pop next node */
  222. if (unlikely(mask == 0))
  223. goto pop;
  224. /* select next child and push other children */
  225. nodeTraverser.traverseClosestHit(cur, mask, tNear, stackPtr, stackEnd);
  226. }
  227. /* this is a leaf node */
  228. assert(cur != BVH::emptyNode);
  229. STAT3(point_query.trav_leaves,1,1,1);
  230. size_t num; Primitive* prim = (Primitive*)cur.leaf(num);
  231. size_t lazy_node = 0;
  232. if (PrimitiveIntersector1::pointQuery(This, query, context, prim, num, tquery, lazy_node))
  233. {
  234. changed = true;
  235. tquery.rad = context->query_radius;
  236. cull_radius = context->query_type == POINT_QUERY_TYPE_SPHERE
  237. ? query->radius * query->radius
  238. : dot(context->query_radius, context->query_radius);
  239. }
  240. /* push lazy node onto stack */
  241. if (unlikely(lazy_node)) {
  242. stackPtr->ptr = lazy_node;
  243. stackPtr->dist = neg_inf;
  244. stackPtr++;
  245. }
  246. }
  247. return changed;
  248. }
  249. };
  250. /* disable point queries for not yet supported geometry types */
  251. template<int N, int types, bool robust>
  252. struct PointQueryDispatch<N, types, robust, VirtualCurveIntersector1> {
  253. static __forceinline bool pointQuery(const Accel::Intersectors* This, PointQuery* query, PointQueryContext* context) { return false; }
  254. };
  255. template<int N, int types, bool robust>
  256. struct PointQueryDispatch<N, types, robust, SubdivPatch1Intersector1> {
  257. static __forceinline bool pointQuery(const Accel::Intersectors* This, PointQuery* query, PointQueryContext* context) { return false; }
  258. };
  259. template<int N, int types, bool robust>
  260. struct PointQueryDispatch<N, types, robust, SubdivPatch1MBIntersector1> {
  261. static __forceinline bool pointQuery(const Accel::Intersectors* This, PointQuery* query, PointQueryContext* context) { return false; }
  262. };
  263. template<int N, int types, bool robust, typename PrimitiveIntersector1>
  264. bool BVHNIntersector1<N, types, robust, PrimitiveIntersector1>::pointQuery(
  265. const Accel::Intersectors* This, PointQuery* query, PointQueryContext* context)
  266. {
  267. return PointQueryDispatch<N, types, robust, PrimitiveIntersector1>::pointQuery(This, query, context);
  268. }
  269. }
  270. }