summaryrefslogtreecommitdiff
path: root/thirdparty/embree/kernels/bvh/bvh_traverser1.h
blob: 8ce01b57f566ecb2e51aea272bb6e1ae473a3ab1 (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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
// Copyright 2009-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include "bvh.h"
#include "node_intersector1.h"
#include "../common/stack_item.h"

#define NEW_SORTING_CODE 1

namespace embree
{
  namespace isa
  {
    /*! BVH regular node traversal for single rays. */
    template<int N, int types>
    class BVHNNodeTraverser1Hit;

#if defined(__AVX512VL__) // SKX

    template<int N>
    __forceinline void isort_update(vint<N> &dist, const vint<N> &d)
    {
      const vint<N> dist_shift = align_shift_right<N-1>(dist,dist);
      const vboolf<N> m_geq = d >= dist;
      const vboolf<N> m_geq_shift = m_geq << 1;
      dist = select(m_geq,d,dist);
      dist = select(m_geq_shift,dist_shift,dist);
    }

    template<int N>
    __forceinline void isort_quick_update(vint<N> &dist, const vint<N> &d) {
      dist = align_shift_right<N-1>(dist,permute(d,vint<N>(zero)));
    }

    __forceinline size_t permuteExtract(const vint8& index, const vllong4& n0, const vllong4& n1) {
      return toScalar(permutex2var((__m256i)index,n0,n1));
    }

    __forceinline float permuteExtract(const vint8& index, const vfloat8& n) {
      return toScalar(permute(n,index));
    }

#endif

    /* Specialization for BVH4. */
    template<int types>
    class BVHNNodeTraverser1Hit<4, types>
    {
      typedef BVH4 BVH;
      typedef BVH4::NodeRef NodeRef;
      typedef BVH4::BaseNode BaseNode;


    public:
      /* Traverses a node with at least one hit child. Optimized for finding the closest hit (intersection). */
      static __forceinline void traverseClosestHit(NodeRef& cur,
                                                   size_t mask,
                                                   const vfloat4& tNear,
                                                   StackItemT<NodeRef>*& stackPtr,
                                                   StackItemT<NodeRef>* stackEnd)
      {
        assert(mask != 0);
        const BaseNode* node = cur.baseNode();

        /*! one child is hit, continue with that child */
        size_t r = bscf(mask);
        cur = node->child(r);
        BVH::prefetch(cur,types);
        if (likely(mask == 0)) {
          assert(cur != BVH::emptyNode);
          return;
        }

        /*! two children are hit, push far child, and continue with closer child */
        NodeRef c0 = cur;
        const unsigned int d0 = ((unsigned int*)&tNear)[r];
        r = bscf(mask);
        NodeRef c1 = node->child(r);
        BVH::prefetch(c1,types);
        const unsigned int d1 = ((unsigned int*)&tNear)[r];
        assert(c0 != BVH::emptyNode);
        assert(c1 != BVH::emptyNode);
        if (likely(mask == 0)) {
          assert(stackPtr < stackEnd);
          if (d0 < d1) { stackPtr->ptr = c1; stackPtr->dist = d1; stackPtr++; cur = c0; return; }
          else         { stackPtr->ptr = c0; stackPtr->dist = d0; stackPtr++; cur = c1; return; }
        }

#if NEW_SORTING_CODE == 1
        vint4 s0((size_t)c0,(size_t)d0);
        vint4 s1((size_t)c1,(size_t)d1);
        r = bscf(mask);
        NodeRef c2 = node->child(r); BVH::prefetch(c2,types); unsigned int d2 = ((unsigned int*)&tNear)[r]; 
        vint4 s2((size_t)c2,(size_t)d2);
        /* 3 hits */
        if (likely(mask == 0)) {
          StackItemT<NodeRef>::sort3(s0,s1,s2);
          *(vint4*)&stackPtr[0] = s0; *(vint4*)&stackPtr[1] = s1;
          cur = toSizeT(s2);
          stackPtr+=2;
          return;
        }
        r = bscf(mask);
        NodeRef c3 = node->child(r); BVH::prefetch(c3,types); unsigned int d3 = ((unsigned int*)&tNear)[r]; 
        vint4 s3((size_t)c3,(size_t)d3);
        /* 4 hits */
        StackItemT<NodeRef>::sort4(s0,s1,s2,s3);
        *(vint4*)&stackPtr[0] = s0; *(vint4*)&stackPtr[1] = s1; *(vint4*)&stackPtr[2] = s2;
        cur = toSizeT(s3);
        stackPtr+=3;
#else
        /*! Here starts the slow path for 3 or 4 hit children. We push
         *  all nodes onto the stack to sort them there. */
        assert(stackPtr < stackEnd);
        stackPtr->ptr = c0; stackPtr->dist = d0; stackPtr++;
        assert(stackPtr < stackEnd);
        stackPtr->ptr = c1; stackPtr->dist = d1; stackPtr++;

        /*! three children are hit, push all onto stack and sort 3 stack items, continue with closest child */
        assert(stackPtr < stackEnd);
        r = bscf(mask);
        NodeRef c = node->child(r); BVH::prefetch(c,types); unsigned int d = ((unsigned int*)&tNear)[r]; stackPtr->ptr = c; stackPtr->dist = d; stackPtr++;
        assert(c != BVH::emptyNode);
        if (likely(mask == 0)) {
          sort(stackPtr[-1],stackPtr[-2],stackPtr[-3]);
          cur = (NodeRef) stackPtr[-1].ptr; stackPtr--;
          return;
        }

        /*! four children are hit, push all onto stack and sort 4 stack items, continue with closest child */
        assert(stackPtr < stackEnd);
        r = bscf(mask);
        c = node->child(r); BVH::prefetch(c,types); d = *(unsigned int*)&tNear[r]; stackPtr->ptr = c; stackPtr->dist = d; stackPtr++;
        assert(c != BVH::emptyNode);
        sort(stackPtr[-1],stackPtr[-2],stackPtr[-3],stackPtr[-4]);
        cur = (NodeRef) stackPtr[-1].ptr; stackPtr--;
#endif
      }

      /* Traverses a node with at least one hit child. Optimized for finding any hit (occlusion). */
      static __forceinline void traverseAnyHit(NodeRef& cur,
                                               size_t mask,
                                               const vfloat4& tNear,
                                               NodeRef*& stackPtr,
                                               NodeRef* stackEnd)
      {
        const BaseNode* node = cur.baseNode();

        /*! one child is hit, continue with that child */
        size_t r = bscf(mask);
        cur = node->child(r); 
        BVH::prefetch(cur,types);

        /* simpler in sequence traversal order */
        assert(cur != BVH::emptyNode);
        if (likely(mask == 0)) return;
        assert(stackPtr < stackEnd);
        *stackPtr = cur; stackPtr++;

        for (; ;)
        {
          r = bscf(mask);
          cur = node->child(r); BVH::prefetch(cur,types);
          assert(cur != BVH::emptyNode);
          if (likely(mask == 0)) return;
          assert(stackPtr < stackEnd);
          *stackPtr = cur; stackPtr++;
        }
      }
    };

    /* Specialization for BVH8. */
    template<int types>
    class BVHNNodeTraverser1Hit<8, types>
    {
      typedef BVH8 BVH;
      typedef BVH8::NodeRef NodeRef;
      typedef BVH8::BaseNode BaseNode;
      
#if defined(__AVX512VL__)
      template<class NodeRef, class BaseNode>
        static __forceinline void traverseClosestHitAVX512VL8(NodeRef& cur,
                                                              size_t mask,
                                                              const vfloat8& tNear,
                                                              StackItemT<NodeRef>*& stackPtr,
                                                              StackItemT<NodeRef>* stackEnd)
      {
        assert(mask != 0);
        const BaseNode* node = cur.baseNode();
        const vllong4 n0 = vllong4::loadu((vllong4*)&node->children[0]);
        const vllong4 n1 = vllong4::loadu((vllong4*)&node->children[4]);
        vint8 distance_i = (asInt(tNear) & 0xfffffff8) | vint8(step);
        distance_i = vint8::compact((int)mask,distance_i,distance_i);
        cur = permuteExtract(distance_i,n0,n1);
        BVH::prefetch(cur,types);

        mask &= mask-1;
        if (likely(mask == 0)) return;

        /* 2 hits: order A0 B0 */
        const vint8 d0(distance_i);
        const vint8 d1(shuffle<1>(distance_i));
        cur = permuteExtract(d1,n0,n1);
        BVH::prefetch(cur,types);

        const vint8 dist_A0 = min(d0, d1);
        const vint8 dist_B0 = max(d0, d1);
        assert(dist_A0[0] < dist_B0[0]);

        mask &= mask-1;
        if (likely(mask == 0)) {
          cur                        = permuteExtract(dist_A0,n0,n1);
          stackPtr[0].ptr            = permuteExtract(dist_B0,n0,n1);
          *(float*)&stackPtr[0].dist = permuteExtract(dist_B0,tNear);
          stackPtr++;
          return;
        }

        /* 3 hits: order A1 B1 C1 */

        const vint8 d2(shuffle<2>(distance_i));
        cur = permuteExtract(d2,n0,n1);
        BVH::prefetch(cur,types);

        const vint8 dist_A1     = min(dist_A0,d2);
        const vint8 dist_tmp_B1 = max(dist_A0,d2);
        const vint8 dist_B1     = min(dist_B0,dist_tmp_B1);
        const vint8 dist_C1     = max(dist_B0,dist_tmp_B1);
        assert(dist_A1[0] < dist_B1[0]);
        assert(dist_B1[0] < dist_C1[0]);

        mask &= mask-1;
        if (likely(mask == 0)) {
          cur                        = permuteExtract(dist_A1,n0,n1);
          stackPtr[0].ptr            = permuteExtract(dist_C1,n0,n1);
          *(float*)&stackPtr[0].dist = permuteExtract(dist_C1,tNear);
          stackPtr[1].ptr            = permuteExtract(dist_B1,n0,n1);
          *(float*)&stackPtr[1].dist = permuteExtract(dist_B1,tNear);
          stackPtr+=2;
          return;
        }

        /* 4 hits: order A2 B2 C2 D2 */

        const vint8 d3(shuffle<3>(distance_i));
        cur = permuteExtract(d3,n0,n1);
        BVH::prefetch(cur,types);

        const vint8 dist_A2     = min(dist_A1,d3);
        const vint8 dist_tmp_B2 = max(dist_A1,d3);
        const vint8 dist_B2     = min(dist_B1,dist_tmp_B2);
        const vint8 dist_tmp_C2 = max(dist_B1,dist_tmp_B2);
        const vint8 dist_C2     = min(dist_C1,dist_tmp_C2);
        const vint8 dist_D2     = max(dist_C1,dist_tmp_C2);
        assert(dist_A2[0] < dist_B2[0]);
        assert(dist_B2[0] < dist_C2[0]);
        assert(dist_C2[0] < dist_D2[0]);
        
        mask &= mask-1;
        if (likely(mask == 0)) {
          cur                        = permuteExtract(dist_A2,n0,n1);
          stackPtr[0].ptr            = permuteExtract(dist_D2,n0,n1);
          *(float*)&stackPtr[0].dist = permuteExtract(dist_D2,tNear);
          stackPtr[1].ptr            = permuteExtract(dist_C2,n0,n1);
          *(float*)&stackPtr[1].dist = permuteExtract(dist_C2,tNear);
          stackPtr[2].ptr            = permuteExtract(dist_B2,n0,n1);
          *(float*)&stackPtr[2].dist = permuteExtract(dist_B2,tNear);
          stackPtr+=3;
          return;
        }

        /* >=5 hits: reverse to descending order for writing to stack */

        distance_i = align_shift_right<3>(distance_i,distance_i);
        const size_t hits = 4 + popcnt(mask);
        vint8 dist(INT_MIN); // this will work with -0.0f (0x80000000) as distance, isort_update uses >= to insert
	
        isort_quick_update<8>(dist,dist_A2);
        isort_quick_update<8>(dist,dist_B2);
        isort_quick_update<8>(dist,dist_C2);
        isort_quick_update<8>(dist,dist_D2);

        do {

          distance_i = align_shift_right<1>(distance_i,distance_i);
          cur = permuteExtract(distance_i,n0,n1);
          BVH::prefetch(cur,types);
          const vint8 new_dist(permute(distance_i,vint8(zero)));
          mask &= mask-1;
          isort_update<8>(dist,new_dist);

        } while(mask);

        for (size_t i=0; i<7; i++)
          assert(dist[i+0]>=dist[i+1]);

        for (size_t i=0;i<hits-1;i++)
        {
          stackPtr->ptr            = permuteExtract(dist,n0,n1);
          *(float*)&stackPtr->dist = permuteExtract(dist,tNear);
          dist = align_shift_right<1>(dist,dist);
          stackPtr++;
        }
        cur = permuteExtract(dist,n0,n1);
      }
#endif

    public:
      static __forceinline void traverseClosestHit(NodeRef& cur,
                                                   size_t mask,
                                                   const vfloat8& tNear,
                                                   StackItemT<NodeRef>*& stackPtr,
                                                   StackItemT<NodeRef>* stackEnd)
      {
        assert(mask != 0);
#if defined(__AVX512VL__)
        traverseClosestHitAVX512VL8<NodeRef,BaseNode>(cur,mask,tNear,stackPtr,stackEnd);
#else

        const BaseNode* node = cur.baseNode();

        /*! one child is hit, continue with that child */
        size_t r = bscf(mask);
        cur = node->child(r);
        BVH::prefetch(cur,types);
        if (likely(mask == 0)) {
          assert(cur != BVH::emptyNode);
          return;
        }

        /*! two children are hit, push far child, and continue with closer child */
        NodeRef c0 = cur;
        const unsigned int d0 = ((unsigned int*)&tNear)[r];
        r = bscf(mask);
        NodeRef c1 = node->child(r);
        BVH::prefetch(c1,types);
        const unsigned int d1 = ((unsigned int*)&tNear)[r];

        assert(c0 != BVH::emptyNode);
        assert(c1 != BVH::emptyNode);
        if (likely(mask == 0)) {
          assert(stackPtr < stackEnd);
          if (d0 < d1) { stackPtr->ptr = c1; stackPtr->dist = d1; stackPtr++; cur = c0; return; }
          else         { stackPtr->ptr = c0; stackPtr->dist = d0; stackPtr++; cur = c1; return; }
        }
#if NEW_SORTING_CODE == 1
        vint4 s0((size_t)c0,(size_t)d0);
        vint4 s1((size_t)c1,(size_t)d1);

        r = bscf(mask);
        NodeRef c2 = node->child(r); BVH::prefetch(c2,types); unsigned int d2 = ((unsigned int*)&tNear)[r]; 
        vint4 s2((size_t)c2,(size_t)d2);
        /* 3 hits */
        if (likely(mask == 0)) {
          StackItemT<NodeRef>::sort3(s0,s1,s2);
          *(vint4*)&stackPtr[0] = s0; *(vint4*)&stackPtr[1] = s1;
          cur = toSizeT(s2);
          stackPtr+=2;
          return;
        }
        r = bscf(mask);
        NodeRef c3 = node->child(r); BVH::prefetch(c3,types); unsigned int d3 = ((unsigned int*)&tNear)[r]; 
        vint4 s3((size_t)c3,(size_t)d3);
        /* 4 hits */
        if (likely(mask == 0)) {
          StackItemT<NodeRef>::sort4(s0,s1,s2,s3);
          *(vint4*)&stackPtr[0] = s0; *(vint4*)&stackPtr[1] = s1; *(vint4*)&stackPtr[2] = s2;
          cur = toSizeT(s3);
          stackPtr+=3;
          return;
        }
        *(vint4*)&stackPtr[0] = s0; *(vint4*)&stackPtr[1] = s1; *(vint4*)&stackPtr[2] = s2; *(vint4*)&stackPtr[3] = s3;
        /*! fallback case if more than 4 children are hit */
        StackItemT<NodeRef>* stackFirst = stackPtr;
        stackPtr+=4;      
        while (1)
        {
          assert(stackPtr < stackEnd);
          r = bscf(mask);
          NodeRef c = node->child(r); BVH::prefetch(c,types); unsigned int d = *(unsigned int*)&tNear[r]; 
          const vint4 s((size_t)c,(size_t)d);
          *(vint4*)stackPtr++ = s;
          assert(c != BVH::emptyNode);
          if (unlikely(mask == 0)) break;
        }
        sort(stackFirst,stackPtr);
        cur = (NodeRef) stackPtr[-1].ptr; stackPtr--;
#else
        /*! Here starts the slow path for 3 or 4 hit children. We push
         *  all nodes onto the stack to sort them there. */
        assert(stackPtr < stackEnd);
        stackPtr->ptr = c0; stackPtr->dist = d0; stackPtr++;
        assert(stackPtr < stackEnd);
        stackPtr->ptr = c1; stackPtr->dist = d1; stackPtr++;

        /*! three children are hit, push all onto stack and sort 3 stack items, continue with closest child */
        assert(stackPtr < stackEnd);
        r = bscf(mask);
        NodeRef c = node->child(r); BVH::prefetch(c,types); unsigned int d = ((unsigned int*)&tNear)[r]; stackPtr->ptr = c; stackPtr->dist = d; stackPtr++;
        assert(c != BVH::emptyNode);
        if (likely(mask == 0)) {
          sort(stackPtr[-1],stackPtr[-2],stackPtr[-3]);
          cur = (NodeRef) stackPtr[-1].ptr; stackPtr--;
          return;
        }

        /*! four children are hit, push all onto stack and sort 4 stack items, continue with closest child */
        assert(stackPtr < stackEnd);
        r = bscf(mask);
        c = node->child(r); BVH::prefetch(c,types); d = *(unsigned int*)&tNear[r]; stackPtr->ptr = c; stackPtr->dist = d; stackPtr++;
        assert(c != BVH::emptyNode);
        if (likely(mask == 0)) {
          sort(stackPtr[-1],stackPtr[-2],stackPtr[-3],stackPtr[-4]);
          cur = (NodeRef) stackPtr[-1].ptr; stackPtr--;
          return;
        }
        /*! fallback case if more than 4 children are hit */
        StackItemT<NodeRef>* stackFirst = stackPtr-4;
        while (1)
        {
          assert(stackPtr < stackEnd);
          r = bscf(mask);
          c = node->child(r); BVH::prefetch(c,types); d = *(unsigned int*)&tNear[r]; stackPtr->ptr = c; stackPtr->dist = d; stackPtr++;
          assert(c != BVH::emptyNode);
          if (unlikely(mask == 0)) break;
        }
        sort(stackFirst,stackPtr);
        cur = (NodeRef) stackPtr[-1].ptr; stackPtr--;
#endif
#endif
      }

      static __forceinline void traverseAnyHit(NodeRef& cur,
                                               size_t mask,
                                               const vfloat8& tNear,
                                               NodeRef*& stackPtr,
                                               NodeRef* stackEnd)
      {
        const BaseNode* node = cur.baseNode();

        /*! one child is hit, continue with that child */
        size_t r = bscf(mask);
        cur = node->child(r);
        BVH::prefetch(cur,types);

        /* simpler in sequence traversal order */
        assert(cur != BVH::emptyNode);
        if (likely(mask == 0)) return;
        assert(stackPtr < stackEnd);
        *stackPtr = cur; stackPtr++;

        for (; ;)
        {
          r = bscf(mask);
          cur = node->child(r); BVH::prefetch(cur,types);
          assert(cur != BVH::emptyNode);
          if (likely(mask == 0)) return;
          assert(stackPtr < stackEnd);
          *stackPtr = cur; stackPtr++;
        }
      }
    };
  }
}