summaryrefslogtreecommitdiff
path: root/thirdparty/embree/kernels/geometry/subgrid_intersector_pluecker.h
blob: 5ded56e1f7249c1fe45198fd66c601e4a6502b35 (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
// Copyright 2009-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include "subgrid.h"
#include "quad_intersector_moeller.h"
#include "quad_intersector_pluecker.h"

namespace embree
{
  namespace isa
  {

    template<int M>
    __forceinline void interpolateUV(PlueckerHitM<M,UVIdentity<M>> &hit,const GridMesh::Grid &g, const SubGrid& subgrid, const vint<M> &stepX, const vint<M> &stepY) 
    {
      /* correct U,V interpolation across the entire grid */
      const vint<M> sx((int)subgrid.x());
      const vint<M> sy((int)subgrid.y());
      const vint<M> sxM(sx + stepX);
      const vint<M> syM(sy + stepY);
      const float inv_resX = rcp((float)((int)g.resX-1));
      const float inv_resY = rcp((float)((int)g.resY-1));          
      hit.U = (hit.U + vfloat<M>(sxM) * hit.UVW) * inv_resX;
      hit.V = (hit.V + vfloat<M>(syM) * hit.UVW) * inv_resY;
    }
    
    template<int M, bool filter>
      struct SubGridQuadMIntersector1Pluecker;

    template<int M, bool filter>
      struct SubGridQuadMIntersector1Pluecker
      {
        __forceinline SubGridQuadMIntersector1Pluecker() {}

        __forceinline SubGridQuadMIntersector1Pluecker(const Ray& ray, const void* ptr) {}

        __forceinline void intersect(RayHit& ray, IntersectContext* context,
                                     const Vec3vf<M>& v0, const Vec3vf<M>& v1, const Vec3vf<M>& v2, const Vec3vf<M>& v3,
                                     const GridMesh::Grid &g, const SubGrid& subgrid) const
        {
          UVIdentity<M> mapUV;
          PlueckerHitM<M,UVIdentity<M>> hit(mapUV);
          PlueckerIntersector1<M> intersector(ray,nullptr);
	  
          Intersect1EpilogMU<M,filter> epilog(ray,context,subgrid.geomID(),subgrid.primID());

          /* intersect first triangle */
	  if (intersector.intersect(ray,v0,v1,v3,mapUV,hit)) 
          {
            interpolateUV<M>(hit,g,subgrid,vint<M>(0,1,1,0),vint<M>(0,0,1,1));
            epilog(hit.valid,hit);
          }

          /* intersect second triangle */
	  if (intersector.intersect(ray,v2,v3,v1,mapUV,hit)) 
          {
	    hit.U = hit.UVW - hit.U;
	    hit.V = hit.UVW - hit.V;
            interpolateUV<M>(hit,g,subgrid,vint<M>(0,1,1,0),vint<M>(0,0,1,1));
            epilog(hit.valid,hit);
          }
        }
      
        __forceinline bool occluded(Ray& ray, IntersectContext* context,
                                    const Vec3vf<M>& v0, const Vec3vf<M>& v1, const Vec3vf<M>& v2, const Vec3vf<M>& v3,
                                    const GridMesh::Grid &g, const SubGrid& subgrid) const
        {
          UVIdentity<M> mapUV;
          PlueckerHitM<M,UVIdentity<M>> hit(mapUV);
          PlueckerIntersector1<M> intersector(ray,nullptr);
          Occluded1EpilogMU<M,filter> epilog(ray,context,subgrid.geomID(),subgrid.primID());

          /* intersect first triangle */
	  if (intersector.intersect(ray,v0,v1,v3,mapUV,hit)) 
          {
            interpolateUV<M>(hit,g,subgrid,vint<M>(0,1,1,0),vint<M>(0,0,1,1));
            if (epilog(hit.valid,hit))
	      return true;
          }

          /* intersect second triangle */
	  if (intersector.intersect(ray,v2,v3,v1,mapUV,hit)) 
          {
	    hit.U = hit.UVW - hit.U;
	    hit.V = hit.UVW - hit.V;
            interpolateUV<M>(hit,g,subgrid,vint<M>(0,1,1,0),vint<M>(0,0,1,1));
            if (epilog(hit.valid,hit))
	      return true;
          }
          return false;
        }
      };

#if defined (__AVX__)

    /*! Intersects 4 quads with 1 ray using AVX */
    template<bool filter>
      struct SubGridQuadMIntersector1Pluecker<4,filter>
    {
      __forceinline SubGridQuadMIntersector1Pluecker() {}

      __forceinline SubGridQuadMIntersector1Pluecker(const Ray& ray, const void* ptr) {}
      
      template<typename Epilog>
        __forceinline bool intersect(Ray& ray, const Vec3vf4& v0, const Vec3vf4& v1, const Vec3vf4& v2, const Vec3vf4& v3, const GridMesh::Grid &g, const SubGrid& subgrid, const Epilog& epilog) const
      {
        const Vec3vf8 vtx0(vfloat8(v0.x,v2.x),vfloat8(v0.y,v2.y),vfloat8(v0.z,v2.z));
#if !defined(EMBREE_BACKFACE_CULLING)
        const Vec3vf8 vtx1(vfloat8(v1.x),vfloat8(v1.y),vfloat8(v1.z));
        const Vec3vf8 vtx2(vfloat8(v3.x),vfloat8(v3.y),vfloat8(v3.z));        
#else
        const Vec3vf8 vtx1(vfloat8(v1.x,v3.x),vfloat8(v1.y,v3.y),vfloat8(v1.z,v3.z));
        const Vec3vf8 vtx2(vfloat8(v3.x,v1.x),vfloat8(v3.y,v1.y),vfloat8(v3.z,v1.z));
#endif

        UVIdentity<8> mapUV;
        PlueckerHitM<8,UVIdentity<8>> hit(mapUV);
        PlueckerIntersector1<8> intersector(ray,nullptr);
        const vbool8 flags(0,0,0,0,1,1,1,1);
        if (unlikely(intersector.intersect(ray,vtx0,vtx1,vtx2,mapUV,hit)))
        {
	  /* correct U,V interpolation across the entire grid */
	  const vfloat8 U = select(flags,hit.UVW - hit.V,hit.U);	  
	  const vfloat8 V = select(flags,hit.UVW - hit.U,hit.V);
	  hit.U = U;
	  hit.V = V;	  
	  hit.vNg *= select(flags,vfloat8(-1.0f),vfloat8(1.0f)); 
          interpolateUV<8>(hit,g,subgrid,vint<8>(0,1,1,0,0,1,1,0),vint<8>(0,0,1,1,0,0,1,1));
          if (unlikely(epilog(hit.valid,hit)))
            return true;
        }
        return false;
      }
      
      __forceinline bool intersect(RayHit& ray, IntersectContext* context,
                                   const Vec3vf4& v0, const Vec3vf4& v1, const Vec3vf4& v2, const Vec3vf4& v3, 
                                   const GridMesh::Grid &g, const SubGrid& subgrid) const
      {
          return intersect(ray,v0,v1,v2,v3,g,subgrid,Intersect1EpilogMU<8,filter>(ray,context,subgrid.geomID(),subgrid.primID()));
      }
      
      __forceinline bool occluded(Ray& ray, IntersectContext* context,
                                  const Vec3vf4& v0, const Vec3vf4& v1, const Vec3vf4& v2, const Vec3vf4& v3, 
                                  const GridMesh::Grid &g, const SubGrid& subgrid) const
      {
          return intersect(ray,v0,v1,v2,v3,g,subgrid,Occluded1EpilogMU<8,filter>(ray,context,subgrid.geomID(),subgrid.primID()));
      }
    };

#endif


    /* ----------------------------- */
    /* -- ray packet intersectors -- */
    /* ----------------------------- */

    template<int K>
    __forceinline void interpolateUV(const vbool<K>& valid, PlueckerHitK<K,UVIdentity<K>> &hit,const GridMesh::Grid &g, const SubGrid& subgrid, const unsigned int i) 
    {
      /* correct U,V interpolation across the entire grid */
      const unsigned int sx = subgrid.x() + (unsigned int)(i % 2);
      const unsigned int sy = subgrid.y() + (unsigned int)(i >>1);
      const float inv_resX = rcp((float)(int)(g.resX-1));
      const float inv_resY = rcp((float)(int)(g.resY-1));      
      hit.U = select(valid,(hit.U + vfloat<K>((float)sx) * hit.UVW) * inv_resX,hit.U);
      hit.V = select(valid,(hit.V + vfloat<K>((float)sy) * hit.UVW) * inv_resY,hit.V);
    }
    
    template<int M, int K, bool filter>
      struct SubGridQuadMIntersectorKPlueckerBase
      {
        __forceinline SubGridQuadMIntersectorKPlueckerBase(const vbool<K>& valid, const RayK<K>& ray) {}

        template<typename Epilog>
        __forceinline bool intersectK(const vbool<K>& valid, 
                                      RayK<K>& ray,
                                      const Vec3vf<K>& v0,
                                      const Vec3vf<K>& v1,
                                      const Vec3vf<K>& v2,
                                      const Vec3vf<K>& v3,
                                      const GridMesh::Grid &g, 
                                      const SubGrid &subgrid,
                                      const unsigned int i,
                                      const Epilog& epilog) const
        {
	  UVIdentity<K> mapUV;
	  PlueckerHitK<K,UVIdentity<K>> hit(mapUV);
	  PlueckerIntersectorK<M,K> intersector;

          const vbool<K> valid0 = intersector.intersectK(valid,ray,v0,v1,v3,mapUV,hit);
	  if (any(valid0))
	    {
	      interpolateUV(valid0,hit,g,subgrid,i);
	      epilog(valid0,hit);
	    }
          const vbool<K> valid1 = intersector.intersectK(valid,ray,v2,v3,v1,mapUV,hit);
	  if (any(valid1))
	    {
	      hit.U = hit.UVW - hit.U;
	      hit.V = hit.UVW - hit.V;	      
	      interpolateUV(valid1,hit,g,subgrid,i);
	      epilog(valid1,hit);
	    }
	  return any(valid0|valid1);	  
        }

       template<typename Epilog>
        __forceinline bool occludedK(const vbool<K>& valid, 
				     RayK<K>& ray,
				     const Vec3vf<K>& v0,
				     const Vec3vf<K>& v1,
				     const Vec3vf<K>& v2,
				     const Vec3vf<K>& v3,
				     const GridMesh::Grid &g, 
				     const SubGrid &subgrid,
				     const unsigned int i,
				     const Epilog& epilog) const
        {
	  UVIdentity<K> mapUV;
	  PlueckerHitK<K,UVIdentity<K>> hit(mapUV);
	  PlueckerIntersectorK<M,K> intersector;

	  vbool<K> valid_final = valid;
          const vbool<K> valid0 = intersector.intersectK(valid,ray,v0,v1,v3,mapUV,hit);
	  if (any(valid0))
	    {
	      interpolateUV(valid0,hit,g,subgrid,i);
	      epilog(valid0,hit);
	      valid_final &= !valid0;
	    }
	  if (none(valid_final)) return true;	      	  
          const vbool<K> valid1 = intersector.intersectK(valid,ray,v2,v3,v1,mapUV,hit);
	  if (any(valid1))
	    {
	      hit.U = hit.UVW - hit.U;
	      hit.V = hit.UVW - hit.V;	      
	      interpolateUV(valid1,hit,g,subgrid,i);
	      epilog(valid1,hit);
	      valid_final &= !valid1;	      
	    }
	  return none(valid_final);
        }

	
      };


    

    template<int M, int K, bool filter>
      struct SubGridQuadMIntersectorKPluecker : public SubGridQuadMIntersectorKPlueckerBase<M,K,filter>
    {
      __forceinline SubGridQuadMIntersectorKPluecker(const vbool<K>& valid, const RayK<K>& ray)
        : SubGridQuadMIntersectorKPlueckerBase<M,K,filter>(valid,ray) {}

      __forceinline void intersect1(RayHitK<K>& ray, size_t k, IntersectContext* context,
                                    const Vec3vf<M>& v0, const Vec3vf<M>& v1, const Vec3vf<M>& v2, const Vec3vf<M>& v3, const GridMesh::Grid &g, const SubGrid &subgrid) const
      {
	UVIdentity<M> mapUV;
	PlueckerHitM<M,UVIdentity<M>> hit(mapUV);
        Intersect1KEpilogMU<M,K,filter> epilog(ray,k,context,subgrid.geomID(),subgrid.primID());
	PlueckerIntersectorK<M,K> intersector;
	
	/* intersect first triangle */
	if (intersector.intersect(ray,k,v0,v1,v3,mapUV,hit)) 
          {
            interpolateUV<M>(hit,g,subgrid,vint<M>(0,1,1,0),vint<M>(0,0,1,1));
            epilog(hit.valid,hit);
          }

	/* intersect second triangle */
	if (intersector.intersect(ray,k,v2,v3,v1,mapUV,hit)) 
          {
	    hit.U = hit.UVW - hit.U;
	    hit.V = hit.UVW - hit.V;
            interpolateUV<M>(hit,g,subgrid,vint<M>(0,1,1,0),vint<M>(0,0,1,1));
            epilog(hit.valid,hit);
          }
      }
      
      __forceinline bool occluded1(RayK<K>& ray, size_t k, IntersectContext* context,
                                   const Vec3vf<M>& v0, const Vec3vf<M>& v1, const Vec3vf<M>& v2, const Vec3vf<M>& v3, const GridMesh::Grid &g, const SubGrid &subgrid) const
      {
	UVIdentity<M> mapUV;
	PlueckerHitM<M,UVIdentity<M>> hit(mapUV);
        Occluded1KEpilogMU<M,K,filter> epilog(ray,k,context,subgrid.geomID(),subgrid.primID());	
	PlueckerIntersectorK<M,K> intersector;
	
	/* intersect first triangle */
	if (intersector.intersect(ray,k,v0,v1,v3,mapUV,hit)) 
        {
          interpolateUV<M>(hit,g,subgrid,vint<M>(0,1,1,0),vint<M>(0,0,1,1));
          if (epilog(hit.valid,hit)) return true;
        }

	/* intersect second triangle */
	if (intersector.intersect(ray,k,v2,v3,v1,mapUV,hit)) 
        {
	  hit.U = hit.UVW - hit.U;
	  hit.V = hit.UVW - hit.V;	  
          interpolateUV<M>(hit,g,subgrid,vint<M>(0,1,1,0),vint<M>(0,0,1,1));
          if (epilog(hit.valid,hit)) return true;
        }
        return false;
      }
    };


#if defined (__AVX__)

    /*! Intersects 4 quads with 1 ray using AVX */
    template<int K, bool filter>
      struct SubGridQuadMIntersectorKPluecker<4,K,filter> : public SubGridQuadMIntersectorKPlueckerBase<4,K,filter>
    {
      __forceinline SubGridQuadMIntersectorKPluecker(const vbool<K>& valid, const RayK<K>& ray)
        : SubGridQuadMIntersectorKPlueckerBase<4,K,filter>(valid,ray) {}
      
      template<typename Epilog>
        __forceinline bool intersect1(RayK<K>& ray, size_t k,const Vec3vf4& v0, const Vec3vf4& v1, const Vec3vf4& v2, const Vec3vf4& v3, 
                                      const GridMesh::Grid &g, const SubGrid &subgrid, const Epilog& epilog) const
      {
        const Vec3vf8 vtx0(vfloat8(v0.x,v2.x),vfloat8(v0.y,v2.y),vfloat8(v0.z,v2.z));
#if !defined(EMBREE_BACKFACE_CULLING)
        const Vec3vf8 vtx1(vfloat8(v1.x),vfloat8(v1.y),vfloat8(v1.z));
        const Vec3vf8 vtx2(vfloat8(v3.x),vfloat8(v3.y),vfloat8(v3.z));
#else
        const Vec3vf8 vtx1(vfloat8(v1.x,v3.x),vfloat8(v1.y,v3.y),vfloat8(v1.z,v3.z));
        const Vec3vf8 vtx2(vfloat8(v3.x,v1.x),vfloat8(v3.y,v1.y),vfloat8(v3.z,v1.z));
#endif
	UVIdentity<8> mapUV;
	PlueckerHitM<8,UVIdentity<8>> hit(mapUV);
	PlueckerIntersectorK<8,K> intersector;
	const vbool8 flags(0,0,0,0,1,1,1,1);
        if (unlikely(intersector.intersect(ray,k,vtx0,vtx1,vtx2,mapUV,hit)))	
        {
          /* correct U,V interpolation across the entire grid */
	  const vfloat8 U = select(flags,hit.UVW - hit.V,hit.U);	  
	  const vfloat8 V = select(flags,hit.UVW - hit.U,hit.V);
	  hit.U = U;
	  hit.V = V;	  
	  hit.vNg *= select(flags,vfloat8(-1.0f),vfloat8(1.0f)); 
          interpolateUV<8>(hit,g,subgrid,vint<8>(0,1,1,0,0,1,1,0),vint<8>(0,0,1,1,0,0,1,1));
          if (unlikely(epilog(hit.valid,hit)))
            return true;
        }
        return false;
      }
      
      __forceinline bool intersect1(RayHitK<K>& ray, size_t k, IntersectContext* context,
                                    const Vec3vf4& v0, const Vec3vf4& v1, const Vec3vf4& v2, const Vec3vf4& v3, const GridMesh::Grid &g, const SubGrid &subgrid) const
      {
        return intersect1(ray,k,v0,v1,v2,v3,g,subgrid,Intersect1KEpilogMU<8,K,filter>(ray,k,context,subgrid.geomID(),subgrid.primID()));
      }
      
      __forceinline bool occluded1(RayK<K>& ray, size_t k, IntersectContext* context,
                                   const Vec3vf4& v0, const Vec3vf4& v1, const Vec3vf4& v2, const Vec3vf4& v3, const GridMesh::Grid &g, const SubGrid &subgrid) const
      {
        return intersect1(ray,k,v0,v1,v2,v3,g,subgrid,Occluded1KEpilogMU<8,K,filter>(ray,k,context,subgrid.geomID(),subgrid.primID()));
      }
    };
#endif

    
  }
}