// Copyright 2009-2020 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 struct SubGridQuadHitPlueckerM { __forceinline SubGridQuadHitPlueckerM() {} __forceinline SubGridQuadHitPlueckerM(const vbool& valid, const vfloat& U, const vfloat& V, const vfloat& UVW, const vfloat& t, const Vec3vf& Ng, const vbool& flags) : valid(valid), vt(t) { const vbool invalid = abs(UVW) < min_rcp_input; const vfloat rcpUVW = select(invalid,vfloat(0.0f),rcp(UVW)); const vfloat u = min(U * rcpUVW,1.0f); const vfloat v = min(V * rcpUVW,1.0f); const vfloat u1 = vfloat(1.0f) - u; const vfloat v1 = vfloat(1.0f) - v; #if !defined(__AVX__) || defined(EMBREE_BACKFACE_CULLING) vu = select(flags,u1,u); vv = select(flags,v1,v); vNg = Vec3vf(Ng.x,Ng.y,Ng.z); #else const vfloat flip = select(flags,vfloat(-1.0f),vfloat(1.0f)); vv = select(flags,u1,v); vu = select(flags,v1,u); vNg = Vec3vf(flip*Ng.x,flip*Ng.y,flip*Ng.z); #endif } __forceinline void finalize() { } __forceinline Vec2f uv(const size_t i) { const float u = vu[i]; const float v = vv[i]; return Vec2f(u,v); } __forceinline float t(const size_t i) { return vt[i]; } __forceinline Vec3fa Ng(const size_t i) { return Vec3fa(vNg.x[i],vNg.y[i],vNg.z[i]); } public: vbool valid; vfloat vu; vfloat vv; vfloat vt; Vec3vf vNg; }; template __forceinline void interpolateUV(SubGridQuadHitPlueckerM &hit,const GridMesh::Grid &g, const SubGrid& subgrid, const vint &stepX, const vint &stepY) { /* correct U,V interpolation across the entire grid */ const vint sx((int)subgrid.x()); const vint sy((int)subgrid.y()); const vint sxM(sx + stepX); const vint syM(sy + stepY); const float inv_resX = rcp((float)((int)g.resX-1)); const float inv_resY = rcp((float)((int)g.resY-1)); hit.vu = (hit.vu + vfloat(sxM)) * inv_resX; hit.vv = (hit.vv + vfloat(syM)) * inv_resY; } template __forceinline static bool intersectPluecker(Ray& ray, const Vec3vf& tri_v0, const Vec3vf& tri_v1, const Vec3vf& tri_v2, const vbool& flags, SubGridQuadHitPlueckerM &hit) { /* calculate vertices relative to ray origin */ const Vec3vf O = Vec3vf((Vec3fa)ray.org); const Vec3vf D = Vec3vf((Vec3fa)ray.dir); const Vec3vf v0 = tri_v0-O; const Vec3vf v1 = tri_v1-O; const Vec3vf v2 = tri_v2-O; /* calculate triangle edges */ const Vec3vf e0 = v2-v0; const Vec3vf e1 = v0-v1; const Vec3vf e2 = v1-v2; /* perform edge tests */ const vfloat U = dot(cross(e0,v2+v0),D); const vfloat V = dot(cross(e1,v0+v1),D); const vfloat W = dot(cross(e2,v1+v2),D); const vfloat UVW = U+V+W; const vfloat eps = float(ulp)*abs(UVW); #if defined(EMBREE_BACKFACE_CULLING) vbool valid = max(U,V,W) <= eps; #else vbool valid = (min(U,V,W) >= -eps) | (max(U,V,W) <= eps); #endif if (unlikely(none(valid))) return false; /* calculate geometry normal and denominator */ const Vec3vf Ng = stable_triangle_normal(e0,e1,e2); const vfloat den = twice(dot(Ng,D)); /* perform depth test */ const vfloat T = twice(dot(v0,Ng)); const vfloat t = rcp(den)*T; valid &= vfloat(ray.tnear()) <= t & t <= vfloat(ray.tfar); valid &= den != vfloat(zero); if (unlikely(none(valid))) return false; /* update hit information */ new (&hit) SubGridQuadHitPlueckerM(valid,U,V,UVW,t,Ng,flags); return true; } template struct SubGridQuadMIntersector1Pluecker; template struct SubGridQuadMIntersector1Pluecker { __forceinline SubGridQuadMIntersector1Pluecker() {} __forceinline SubGridQuadMIntersector1Pluecker(const Ray& ray, const void* ptr) {} __forceinline void intersect(RayHit& ray, IntersectContext* context, const Vec3vf& v0, const Vec3vf& v1, const Vec3vf& v2, const Vec3vf& v3, const GridMesh::Grid &g, const SubGrid& subgrid) const { SubGridQuadHitPlueckerM hit; Intersect1EpilogMU epilog(ray,context,subgrid.geomID(),subgrid.primID()); /* intersect first triangle */ if (intersectPluecker(ray,v0,v1,v3,vbool(false),hit)) { interpolateUV(hit,g,subgrid,vint(0,1,1,0),vint(0,0,1,1)); epilog(hit.valid,hit); } /* intersect second triangle */ if (intersectPluecker(ray,v2,v3,v1,vbool(true),hit)) { interpolateUV(hit,g,subgrid,vint(0,1,1,0),vint(0,0,1,1)); epilog(hit.valid,hit); } } __forceinline bool occluded(Ray& ray, IntersectContext* context, const Vec3vf& v0, const Vec3vf& v1, const Vec3vf& v2, const Vec3vf& v3, const GridMesh::Grid &g, const SubGrid& subgrid) const { SubGridQuadHitPlueckerM hit; Occluded1EpilogMU epilog(ray,context,subgrid.geomID(),subgrid.primID()); /* intersect first triangle */ if (intersectPluecker(ray,v0,v1,v3,vbool(false),hit)) { interpolateUV(hit,g,subgrid,vint(0,1,1,0),vint(0,0,1,1)); if (epilog(hit.valid,hit)) return true; } /* intersect second triangle */ if (intersectPluecker(ray,v2,v3,v1,vbool(true),hit)) { interpolateUV(hit,g,subgrid,vint(0,1,1,0),vint(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 struct SubGridQuadMIntersector1Pluecker<4,filter> { __forceinline SubGridQuadMIntersector1Pluecker() {} __forceinline SubGridQuadMIntersector1Pluecker(const Ray& ray, const void* ptr) {} template __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 SubGridQuadHitPlueckerM<8> hit; const vbool8 flags(0,0,0,0,1,1,1,1); if (unlikely(intersectPluecker(ray,vtx0,vtx1,vtx2,flags,hit))) { /* correct U,V interpolation across the entire grid */ 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 struct SubGridQuadHitPlueckerK { __forceinline SubGridQuadHitPlueckerK(const vfloat& U, const vfloat& V, const vfloat& UVW, const vfloat& t, const Vec3vf& Ng, const vbool& flags, const GridMesh::Grid &g, const SubGrid& subgrid, const unsigned int i) : U(U), V(V), UVW(UVW), t(t), flags(flags), tri_Ng(Ng), g(g), subgrid(subgrid), i(i) {} __forceinline std::tuple,vfloat,vfloat,Vec3vf> operator() () const { const vbool invalid = abs(UVW) < min_rcp_input; const vfloat rcpUVW = select(invalid,vfloat(0.0f),rcp(UVW)); const vfloat u0 = min(U * rcpUVW,1.0f); const vfloat v0 = min(V * rcpUVW,1.0f); const vfloat u1 = vfloat(1.0f) - u0; const vfloat v1 = vfloat(1.0f) - v0; const vfloat uu = select(flags,u1,u0); const vfloat vv = select(flags,v1,v0); 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)); const vfloat u = (uu + (float)(int)sx) * inv_resX; const vfloat v = (vv + (float)(int)sy) * inv_resY; const Vec3vf Ng(tri_Ng.x,tri_Ng.y,tri_Ng.z); return std::make_tuple(u,v,t,Ng); } private: const vfloat U; const vfloat V; const vfloat UVW; const vfloat t; const vfloat absDen; const vbool flags; const Vec3vf tri_Ng; const GridMesh::Grid &g; const SubGrid& subgrid; const size_t i; }; template struct SubGridQuadMIntersectorKPlueckerBase { __forceinline SubGridQuadMIntersectorKPlueckerBase(const vbool& valid, const RayK& ray) {} template __forceinline vbool intersectK(const vbool& valid0, RayK& ray, const Vec3vf& tri_v0, const Vec3vf& tri_v1, const Vec3vf& tri_v2, const Vec3vf& tri_Ng, const vbool& flags, const GridMesh::Grid &g, const SubGrid &subgrid, const unsigned int i, const Epilog& epilog) const { /* calculate denominator */ /* calculate vertices relative to ray origin */ vbool valid = valid0; const Vec3vf O = ray.org; const Vec3vf D = ray.dir; const Vec3vf v0 = tri_v0-O; const Vec3vf v1 = tri_v1-O; const Vec3vf v2 = tri_v2-O; /* calculate triangle edges */ const Vec3vf e0 = v2-v0; const Vec3vf e1 = v0-v1; const Vec3vf e2 = v1-v2; /* perform edge tests */ const vfloat U = dot(Vec3vf(cross(e0,v2+v0)),D); const vfloat V = dot(Vec3vf(cross(e1,v0+v1)),D); const vfloat W = dot(Vec3vf(cross(e2,v1+v2)),D); const vfloat UVW = U+V+W; const vfloat eps = float(ulp)*abs(UVW); #if defined(EMBREE_BACKFACE_CULLING) valid &= max(U,V,W) <= eps; #else valid &= (min(U,V,W) >= -eps) | (max(U,V,W) <= eps); #endif if (unlikely(none(valid))) return false; /* calculate geometry normal and denominator */ const Vec3vf Ng = stable_triangle_normal(e0,e1,e2); const vfloat den = twice(dot(Vec3vf(Ng),D)); /* perform depth test */ const vfloat T = twice(dot(v0,Vec3vf(Ng))); const vfloat t = rcp(den)*T; valid &= ray.tnear() <= t & t <= ray.tfar; valid &= den != vfloat(zero); if (unlikely(none(valid))) return false; /* calculate hit information */ SubGridQuadHitPlueckerK hit(U,V,UVW,t,tri_Ng,flags,g,subgrid,i); return epilog(valid,hit); } template __forceinline vbool intersectK(const vbool& valid0, RayK& ray, const Vec3vf& v0, const Vec3vf& v1, const Vec3vf& v2, const vbool& flags, const GridMesh::Grid &g, const SubGrid &subgrid, const unsigned int i, const Epilog& epilog) const { const Vec3vf e1 = v0-v1; const Vec3vf e2 = v2-v0; const Vec3vf Ng = cross(e2,e1); return intersectK(valid0,ray,v0,v1,v2,Ng,flags,g,subgrid,i,epilog); } template __forceinline bool intersectK(const vbool& valid0, RayK& ray, const Vec3vf& v0, const Vec3vf& v1, const Vec3vf& v2, const Vec3vf& v3, const GridMesh::Grid &g, const SubGrid &subgrid, const unsigned int i, const Epilog& epilog) const { intersectK(valid0,ray,v0,v1,v3,vbool(false),g,subgrid,i,epilog); if (none(valid0)) return true; intersectK(valid0,ray,v2,v3,v1,vbool(true ),g,subgrid,i,epilog); return none(valid0); } static __forceinline bool intersect1(RayK& ray, size_t k, const Vec3vf& tri_v0, const Vec3vf& tri_v1, const Vec3vf& tri_v2, const Vec3vf& tri_Ng, const vbool& flags, SubGridQuadHitPlueckerM &hit) { /* calculate vertices relative to ray origin */ const Vec3vf O = broadcast>(ray.org,k); const Vec3vf D = broadcast>(ray.dir,k); const Vec3vf v0 = tri_v0-O; const Vec3vf v1 = tri_v1-O; const Vec3vf v2 = tri_v2-O; /* calculate triangle edges */ const Vec3vf e0 = v2-v0; const Vec3vf e1 = v0-v1; const Vec3vf e2 = v1-v2; /* perform edge tests */ const vfloat U = dot(cross(e0,v2+v0),D); const vfloat V = dot(cross(e1,v0+v1),D); const vfloat W = dot(cross(e2,v1+v2),D); const vfloat UVW = U+V+W; const vfloat eps = float(ulp)*abs(UVW); #if defined(EMBREE_BACKFACE_CULLING) vbool valid = max(U,V,W) <= eps ; #else vbool valid = (min(U,V,W) >= -eps) | (max(U,V,W) <= eps); #endif if (unlikely(none(valid))) return false; /* calculate geometry normal and denominator */ const Vec3vf Ng = stable_triangle_normal(e0,e1,e2); const vfloat den = twice(dot(Ng,D)); /* perform depth test */ const vfloat T = twice(dot(v0,Ng)); const vfloat t = rcp(den)*T; valid &= vfloat(ray.tnear()[k]) <= t & t <= vfloat(ray.tfar[k]); if (unlikely(none(valid))) return false; /* avoid division by 0 */ valid &= den != vfloat(zero); if (unlikely(none(valid))) return false; /* update hit information */ new (&hit) SubGridQuadHitPlueckerM(valid,U,V,UVW,t,tri_Ng,flags); return true; } static __forceinline bool intersect1(RayK& ray, size_t k, const Vec3vf& v0, const Vec3vf& v1, const Vec3vf& v2, const vbool& flags, SubGridQuadHitPlueckerM &hit) { const Vec3vf e1 = v0-v1; const Vec3vf e2 = v2-v0; const Vec3vf Ng = cross(e2,e1); // FIXME: optimize!!! return intersect1(ray,k,v0,v1,v2,Ng,flags,hit); } }; template struct SubGridQuadMIntersectorKPluecker : public SubGridQuadMIntersectorKPlueckerBase { __forceinline SubGridQuadMIntersectorKPluecker(const vbool& valid, const RayK& ray) : SubGridQuadMIntersectorKPlueckerBase(valid,ray) {} __forceinline void intersect1(RayHitK& ray, size_t k, IntersectContext* context, const Vec3vf& v0, const Vec3vf& v1, const Vec3vf& v2, const Vec3vf& v3, const GridMesh::Grid &g, const SubGrid &subgrid) const { Intersect1KEpilogMU epilog(ray,k,context,subgrid.geomID(),subgrid.primID()); SubGridQuadHitPlueckerM<4> hit; if (SubGridQuadMIntersectorKPlueckerBase<4,K,filter>::intersect1(ray,k,v0,v1,v3,vboolf4(false),hit)) { interpolateUV(hit,g,subgrid,vint(0,1,1,0),vint(0,0,1,1)); epilog(hit.valid,hit); } if (SubGridQuadMIntersectorKPlueckerBase<4,K,filter>::intersect1(ray,k,v2,v3,v1,vboolf4(true),hit)) { interpolateUV(hit,g,subgrid,vint(0,1,1,0),vint(0,0,1,1)); epilog(hit.valid,hit); } } __forceinline bool occluded1(RayK& ray, size_t k, IntersectContext* context, const Vec3vf& v0, const Vec3vf& v1, const Vec3vf& v2, const Vec3vf& v3, const GridMesh::Grid &g, const SubGrid &subgrid) const { Occluded1KEpilogMU epilog(ray,k,context,subgrid.geomID(),subgrid.primID()); SubGridQuadHitPlueckerM<4> hit; if (SubGridQuadMIntersectorKPlueckerBase<4,K,filter>::intersect1(ray,k,v0,v1,v3,vboolf4(false),hit)) { interpolateUV(hit,g,subgrid,vint(0,1,1,0),vint(0,0,1,1)); if (epilog(hit.valid,hit)) return true; } if (SubGridQuadMIntersectorKPlueckerBase<4,K,filter>::intersect1(ray,k,v2,v3,v1,vboolf4(true),hit)) { interpolateUV(hit,g,subgrid,vint(0,1,1,0),vint(0,0,1,1)); if (epilog(hit.valid,hit)) return true; } return false; } }; } }