summaryrefslogtreecommitdiff
path: root/thirdparty/embree/kernels/bvh/bvh_intersector1.cpp
blob: 9594f402c3ff50db551ae9fcc54052d873f0a1e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// Copyright 2009-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0

#include "bvh_intersector1.h"
#include "node_intersector1.h"
#include "bvh_traverser1.h"

#include "../geometry/intersector_iterators.h"
#include "../geometry/triangle_intersector.h"
#include "../geometry/trianglev_intersector.h"
#include "../geometry/trianglev_mb_intersector.h"
#include "../geometry/trianglei_intersector.h"
#include "../geometry/quadv_intersector.h"
#include "../geometry/quadi_intersector.h"
#include "../geometry/curveNv_intersector.h"
#include "../geometry/curveNi_intersector.h"
#include "../geometry/curveNi_mb_intersector.h"
#include "../geometry/linei_intersector.h"
#include "../geometry/subdivpatch1_intersector.h"
#include "../geometry/object_intersector.h"
#include "../geometry/instance_intersector.h"
#include "../geometry/subgrid_intersector.h"
#include "../geometry/subgrid_mb_intersector.h"
#include "../geometry/curve_intersector_virtual.h"

namespace embree
{
  namespace isa
  {
    template<int N, int types, bool robust, typename PrimitiveIntersector1>
    void BVHNIntersector1<N, types, robust, PrimitiveIntersector1>::intersect(const Accel::Intersectors* __restrict__ This,
                                                                              RayHit& __restrict__ ray,
                                                                              IntersectContext* __restrict__ context)
    {
      const BVH* __restrict__ bvh = (const BVH*)This->ptr;
      
      /* we may traverse an empty BVH in case all geometry was invalid */
      if (bvh->root == BVH::emptyNode)
        return;
      
      /* perform per ray precalculations required by the primitive intersector */
      Precalculations pre(ray, bvh);

      /* stack state */
      StackItemT<NodeRef> stack[stackSize];    // stack of nodes
      StackItemT<NodeRef>* stackPtr = stack+1; // current stack pointer
      StackItemT<NodeRef>* stackEnd = stack+stackSize;
      stack[0].ptr  = bvh->root;
      stack[0].dist = neg_inf;
      
      if (bvh->root == BVH::emptyNode)
        return;
      
      /* filter out invalid rays */
#if defined(EMBREE_IGNORE_INVALID_RAYS)
      if (!ray.valid()) return;
#endif
      /* verify correct input */
      assert(ray.valid());
      assert(ray.tnear() >= 0.0f);
      assert(!(types & BVH_MB) || (ray.time() >= 0.0f && ray.time() <= 1.0f));

      /* load the ray into SIMD registers */
      TravRay<N,robust> tray(ray.org, ray.dir, max(ray.tnear(), 0.0f), max(ray.tfar, 0.0f));

      /* initialize the node traverser */
      BVHNNodeTraverser1Hit<N, types> nodeTraverser;

      /* pop loop */
      while (true) pop:
      {
        /* pop next node */
        if (unlikely(stackPtr == stack)) break;
        stackPtr--;
        NodeRef cur = NodeRef(stackPtr->ptr);

        /* if popped node is too far, pop next one */
        if (unlikely(*(float*)&stackPtr->dist > ray.tfar))
          continue;

        /* downtraversal loop */
        while (true)
        {
          /* intersect node */
          size_t mask; vfloat<N> tNear;
          STAT3(normal.trav_nodes,1,1,1);
          bool nodeIntersected = BVHNNodeIntersector1<N, types, robust>::intersect(cur, tray, ray.time(), tNear, mask);
          if (unlikely(!nodeIntersected)) { STAT3(normal.trav_nodes,-1,-1,-1); break; }

          /* if no child is hit, pop next node */
          if (unlikely(mask == 0))
            goto pop;

          /* select next child and push other children */
          nodeTraverser.traverseClosestHit(cur, mask, tNear, stackPtr, stackEnd);
        }

        /* this is a leaf node */
        assert(cur != BVH::emptyNode);
        STAT3(normal.trav_leaves,1,1,1);
        size_t num; Primitive* prim = (Primitive*)cur.leaf(num);
        size_t lazy_node = 0;
        PrimitiveIntersector1::intersect(This, pre, ray, context, prim, num, tray, lazy_node);
        tray.tfar = ray.tfar;

        /* push lazy node onto stack */
        if (unlikely(lazy_node)) {
          stackPtr->ptr = lazy_node;
          stackPtr->dist = neg_inf;
          stackPtr++;
        }
      }
    }

    template<int N, int types, bool robust, typename PrimitiveIntersector1>
    void BVHNIntersector1<N, types, robust, PrimitiveIntersector1>::occluded(const Accel::Intersectors* __restrict__ This,
                                                                             Ray& __restrict__ ray,
                                                                             IntersectContext* __restrict__ context)
    {
      const BVH* __restrict__ bvh = (const BVH*)This->ptr;
      
      /* we may traverse an empty BVH in case all geometry was invalid */
      if (bvh->root == BVH::emptyNode)
        return;
       
      /* early out for already occluded rays */
      if (unlikely(ray.tfar < 0.0f))
        return;

      /* perform per ray precalculations required by the primitive intersector */
      Precalculations pre(ray, bvh);

      /* stack state */
      NodeRef stack[stackSize];    // stack of nodes that still need to get traversed
      NodeRef* stackPtr = stack+1; // current stack pointer
      NodeRef* stackEnd = stack+stackSize;
      stack[0] = bvh->root;

      /* filter out invalid rays */
#if defined(EMBREE_IGNORE_INVALID_RAYS)
      if (!ray.valid()) return;
#endif

      /* verify correct input */
      assert(ray.valid());
      assert(ray.tnear() >= 0.0f);
      assert(!(types & BVH_MB) || (ray.time() >= 0.0f && ray.time() <= 1.0f));

      /* load the ray into SIMD registers */
      TravRay<N,robust> tray(ray.org, ray.dir, max(ray.tnear(), 0.0f), max(ray.tfar, 0.0f));

      /* initialize the node traverser */
      BVHNNodeTraverser1Hit<N, types> nodeTraverser;

      /* pop loop */
      while (true) pop:
      {
        /* pop next node */
        if (unlikely(stackPtr == stack)) break;
        stackPtr--;
        NodeRef cur = (NodeRef)*stackPtr;

        /* downtraversal loop */
        while (true)
        {
          /* intersect node */
          size_t mask; vfloat<N> tNear;
          STAT3(shadow.trav_nodes,1,1,1);
          bool nodeIntersected = BVHNNodeIntersector1<N, types, robust>::intersect(cur, tray, ray.time(), tNear, mask);
          if (unlikely(!nodeIntersected)) { STAT3(shadow.trav_nodes,-1,-1,-1); break; }

          /* if no child is hit, pop next node */
          if (unlikely(mask == 0))
            goto pop;

          /* select next child and push other children */
          nodeTraverser.traverseAnyHit(cur, mask, tNear, stackPtr, stackEnd);
        }

        /* this is a leaf node */
        assert(cur != BVH::emptyNode);
        STAT3(shadow.trav_leaves,1,1,1);
        size_t num; Primitive* prim = (Primitive*)cur.leaf(num);
        size_t lazy_node = 0;
        if (PrimitiveIntersector1::occluded(This, pre, ray, context, prim, num, tray, lazy_node)) {
          ray.tfar = neg_inf;
          break;
        }

        /* push lazy node onto stack */
        if (unlikely(lazy_node)) {
          *stackPtr = (NodeRef)lazy_node;
          stackPtr++;
        }
      }
    }

    template<int N, int types, bool robust, typename PrimitiveIntersector1>
    struct PointQueryDispatch
    {
      typedef typename PrimitiveIntersector1::Precalculations Precalculations;
      typedef typename PrimitiveIntersector1::Primitive Primitive;
      typedef BVHN<N> BVH;
      typedef typename BVH::NodeRef NodeRef;
      typedef typename BVH::AABBNode AABBNode;
      typedef typename BVH::AABBNodeMB4D AABBNodeMB4D;

      static const size_t stackSize = 1+(N-1)*BVH::maxDepth+3; // +3 due to 16-wide store

      static __forceinline bool pointQuery(const Accel::Intersectors* This, PointQuery* query, PointQueryContext* context)
      {
        const BVH* __restrict__ bvh = (const BVH*)This->ptr;
        
        /* we may traverse an empty BVH in case all geometry was invalid */
        if (bvh->root == BVH::emptyNode)
          return false;
        
        /* stack state */
        StackItemT<NodeRef> stack[stackSize];    // stack of nodes
        StackItemT<NodeRef>* stackPtr = stack+1; // current stack pointer
        StackItemT<NodeRef>* stackEnd = stack+stackSize;
        stack[0].ptr  = bvh->root;
        stack[0].dist = neg_inf;
        
        /* verify correct input */
        assert(!(types & BVH_MB) || (query->time >= 0.0f && query->time <= 1.0f));

        /* load the point query into SIMD registers */
        TravPointQuery<N> tquery(query->p, context->query_radius);

        /* initialize the node traverser */
        BVHNNodeTraverser1Hit<N,types> nodeTraverser;

        bool changed = false;
        float cull_radius = context->query_type == POINT_QUERY_TYPE_SPHERE
                          ? query->radius * query->radius
                          : dot(context->query_radius, context->query_radius);

        /* pop loop */
        while (true) pop:
        {
          /* pop next node */
          if (unlikely(stackPtr == stack)) break;
          stackPtr--;
          NodeRef cur = NodeRef(stackPtr->ptr);

          /* if popped node is too far, pop next one */
          if (unlikely(*(float*)&stackPtr->dist > cull_radius))
            continue;

          /* downtraversal loop */
          while (true)
          {
            /* intersect node */
            size_t mask; vfloat<N> tNear;
            STAT3(point_query.trav_nodes,1,1,1);
            bool nodeIntersected;
            if (likely(context->query_type == POINT_QUERY_TYPE_SPHERE)) {
              nodeIntersected = BVHNNodePointQuerySphere1<N, types>::pointQuery(cur, tquery, query->time, tNear, mask);
            } else {
              nodeIntersected = BVHNNodePointQueryAABB1  <N, types>::pointQuery(cur, tquery, query->time, tNear, mask);
            }
            if (unlikely(!nodeIntersected)) { STAT3(point_query.trav_nodes,-1,-1,-1); break; }

            /* if no child is hit, pop next node */
            if (unlikely(mask == 0))
              goto pop;

            /* select next child and push other children */
            nodeTraverser.traverseClosestHit(cur, mask, tNear, stackPtr, stackEnd);
          }

          /* this is a leaf node */
          assert(cur != BVH::emptyNode);
          STAT3(point_query.trav_leaves,1,1,1);
          size_t num; Primitive* prim = (Primitive*)cur.leaf(num);
          size_t lazy_node = 0;
          if (PrimitiveIntersector1::pointQuery(This, query, context, prim, num, tquery, lazy_node))
          {
            changed = true;
            tquery.rad = context->query_radius;
            cull_radius = context->query_type == POINT_QUERY_TYPE_SPHERE
                        ? query->radius * query->radius
                        : dot(context->query_radius, context->query_radius);
          }

          /* push lazy node onto stack */
          if (unlikely(lazy_node)) {
            stackPtr->ptr = lazy_node;
            stackPtr->dist = neg_inf;
            stackPtr++;
          }
        }
        return changed;
      }
    };

    /* disable point queries for not yet supported geometry types */
    template<int N, int types, bool robust>
    struct PointQueryDispatch<N, types, robust, VirtualCurveIntersector1> {
      static __forceinline bool pointQuery(const Accel::Intersectors* This, PointQuery* query, PointQueryContext* context) { return false; }
    };
    
    template<int N, int types, bool robust>
    struct PointQueryDispatch<N, types, robust, SubdivPatch1Intersector1> {
      static __forceinline bool pointQuery(const Accel::Intersectors* This, PointQuery* query, PointQueryContext* context) { return false; }
    };
    
    template<int N, int types, bool robust>
    struct PointQueryDispatch<N, types, robust, SubdivPatch1MBIntersector1> {
      static __forceinline bool pointQuery(const Accel::Intersectors* This, PointQuery* query, PointQueryContext* context) { return false; }
    };

    template<int N, int types, bool robust, typename PrimitiveIntersector1>
    bool BVHNIntersector1<N, types, robust, PrimitiveIntersector1>::pointQuery(
      const Accel::Intersectors* This, PointQuery* query, PointQueryContext* context)
    {
      return PointQueryDispatch<N, types, robust, PrimitiveIntersector1>::pointQuery(This, query, context);
    }
  }
}