2
0

bvh_intersector1.cpp 12 KB

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