// Copyright 2009-2020 Intel Corporation // SPDX-License-Identifier: Apache-2.0 #pragma once #include "subgrid.h" #include "quad_intersector_moeller.h" namespace embree { namespace isa { /* ----------------------------- */ /* -- single ray intersectors -- */ /* ----------------------------- */ template __forceinline void interpolateUV(MoellerTrumboreHitM &hit,const GridMesh::Grid &g, const SubGrid& subgrid) { /* correct U,V interpolation across the entire grid */ const vint sx((int)subgrid.x()); const vint sy((int)subgrid.y()); const vint sxM(sx + vint(0,1,1,0)); const vint syM(sy + vint(0,0,1,1)); 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)sxM * hit.absDen) * inv_resX; hit.V = (hit.V + (vfloat)syM * hit.absDen) * inv_resY; } template struct SubGridQuadMIntersector1MoellerTrumbore; template struct SubGridQuadMIntersector1MoellerTrumbore { __forceinline SubGridQuadMIntersector1MoellerTrumbore() {} __forceinline SubGridQuadMIntersector1MoellerTrumbore(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 { MoellerTrumboreHitM hit; MoellerTrumboreIntersector1 intersector(ray,nullptr); Intersect1EpilogMU epilog(ray,context,subgrid.geomID(),subgrid.primID()); /* intersect first triangle */ if (intersector.intersect(ray,v0,v1,v3,hit)) { interpolateUV(hit,g,subgrid); epilog(hit.valid,hit); } /* intersect second triangle */ if (intersector.intersect(ray,v2,v3,v1,hit)) { hit.U = hit.absDen - hit.U; hit.V = hit.absDen - hit.V; interpolateUV(hit,g,subgrid); 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 { MoellerTrumboreHitM hit; MoellerTrumboreIntersector1 intersector(ray,nullptr); Occluded1EpilogMU epilog(ray,context,subgrid.geomID(),subgrid.primID()); /* intersect first triangle */ if (intersector.intersect(ray,v0,v1,v3,hit)) { interpolateUV(hit,g,subgrid); if (epilog(hit.valid,hit)) return true; } /* intersect second triangle */ if (intersector.intersect(ray,v2,v3,v1,hit)) { hit.U = hit.absDen - hit.U; hit.V = hit.absDen - hit.V; interpolateUV(hit,g,subgrid); if (epilog(hit.valid,hit)) return true; } return false; } }; #if defined (__AVX__) /*! Intersects 4 quads with 1 ray using AVX */ template struct SubGridQuadMIntersector1MoellerTrumbore<4,filter> { __forceinline SubGridQuadMIntersector1MoellerTrumbore() {} __forceinline SubGridQuadMIntersector1MoellerTrumbore(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 MoellerTrumboreHitM<8> hit; MoellerTrumboreIntersector1<8> intersector(ray,nullptr); const vbool8 flags(0,0,0,0,1,1,1,1); if (unlikely(intersector.intersect(ray,vtx0,vtx1,vtx2,hit))) { vfloat8 U = hit.U, V = hit.V, absDen = hit.absDen; #if !defined(EMBREE_BACKFACE_CULLING) hit.U = select(flags,absDen-V,U); hit.V = select(flags,absDen-U,V); hit.vNg *= select(flags,vfloat8(-1.0f),vfloat8(1.0f)); #else hit.U = select(flags,absDen-U,U); hit.V = select(flags,absDen-V,V); #endif /* correct U,V interpolation across the entire grid */ const vint8 sx((int)subgrid.x()); const vint8 sy((int)subgrid.y()); const vint8 sx8(sx + vint8(0,1,1,0,0,1,1,0)); const vint8 sy8(sy + vint8(0,0,1,1,0,0,1,1)); const float inv_resX = rcp((float)((int)g.resX-1)); const float inv_resY = rcp((float)((int)g.resY-1)); hit.U = (hit.U + (vfloat8)sx8 * absDen) * inv_resX; hit.V = (hit.V + (vfloat8)sy8 * absDen) * inv_resY; 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 SubGridQuadHitK { __forceinline SubGridQuadHitK(const vfloat& U, const vfloat& V, const vfloat& T, const vfloat& absDen, const Vec3vf& Ng, const vbool& flags, const GridMesh::Grid &g, const SubGrid& subgrid, const unsigned int i) : U(U), V(V), T(T), absDen(absDen), flags(flags), tri_Ng(Ng), g(g), subgrid(subgrid), i(i) {} __forceinline std::tuple,vfloat,vfloat,Vec3vf> operator() () const { const vfloat rcpAbsDen = rcp(absDen); const vfloat t = T * rcpAbsDen; const vfloat u0 = min(U * rcpAbsDen,1.0f); const vfloat v0 = min(V * rcpAbsDen,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 T; const vfloat absDen; const vbool flags; const Vec3vf tri_Ng; const GridMesh::Grid &g; const SubGrid& subgrid; const size_t i; }; template struct SubGridQuadMIntersectorKMoellerTrumboreBase { __forceinline SubGridQuadMIntersectorKMoellerTrumboreBase(const vbool& valid, const RayK& ray) {} template __forceinline vbool intersectK(const vbool& valid0, RayK& ray, const Vec3vf& tri_v0, const Vec3vf& tri_e1, const Vec3vf& tri_e2, const Vec3vf& tri_Ng, const vbool& flags, const GridMesh::Grid &g, const SubGrid &subgrid, const unsigned int i, const Epilog& epilog) const { /* calculate denominator */ vbool valid = valid0; const Vec3vf C = tri_v0 - ray.org; const Vec3vf R = cross(C,ray.dir); const vfloat den = dot(tri_Ng,ray.dir); const vfloat absDen = abs(den); const vfloat sgnDen = signmsk(den); /* test against edge p2 p0 */ const vfloat U = dot(R,tri_e2) ^ sgnDen; valid &= U >= 0.0f; if (likely(none(valid))) return false; /* test against edge p0 p1 */ const vfloat V = dot(R,tri_e1) ^ sgnDen; valid &= V >= 0.0f; if (likely(none(valid))) return false; /* test against edge p1 p2 */ const vfloat W = absDen-U-V; valid &= W >= 0.0f; if (likely(none(valid))) return false; /* perform depth test */ const vfloat T = dot(tri_Ng,C) ^ sgnDen; valid &= (absDen*ray.tnear() < T) & (T <= absDen*ray.tfar); if (unlikely(none(valid))) return false; /* perform backface culling */ #if defined(EMBREE_BACKFACE_CULLING) valid &= den < vfloat(zero); if (unlikely(none(valid))) return false; #else valid &= den != vfloat(zero); if (unlikely(none(valid))) return false; #endif /* calculate hit information */ SubGridQuadHitK hit(U,V,T,absDen,tri_Ng,flags,g,subgrid,i); return epilog(valid,hit); } template __forceinline vbool intersectK(const vbool& valid0, RayK& ray, const Vec3vf& tri_v0, const Vec3vf& tri_v1, const Vec3vf& tri_v2, const vbool& flags, const GridMesh::Grid &g, const SubGrid &subgrid, const unsigned int i, const Epilog& epilog) const { const Vec3vf e1 = tri_v0-tri_v1; const Vec3vf e2 = tri_v2-tri_v0; const Vec3vf Ng = cross(e2,e1); return intersectK(valid0,ray,tri_v0,e1,e2,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_e1, const Vec3vf& tri_e2, const Vec3vf& tri_Ng, MoellerTrumboreHitM &hit) { /* calculate denominator */ const Vec3vf O = broadcast>(ray.org,k); const Vec3vf D = broadcast>(ray.dir,k); const Vec3vf C = Vec3vf(tri_v0) - O; const Vec3vf R = cross(C,D); const vfloat den = dot(Vec3vf(tri_Ng),D); const vfloat absDen = abs(den); const vfloat sgnDen = signmsk(den); /* perform edge tests */ const vfloat U = dot(R,Vec3vf(tri_e2)) ^ sgnDen; const vfloat V = dot(R,Vec3vf(tri_e1)) ^ sgnDen; /* perform backface culling */ #if defined(EMBREE_BACKFACE_CULLING) vbool valid = (den < vfloat(zero)) & (U >= 0.0f) & (V >= 0.0f) & (U+V<=absDen); #else vbool valid = (den != vfloat(zero)) & (U >= 0.0f) & (V >= 0.0f) & (U+V<=absDen); #endif if (likely(none(valid))) return false; /* perform depth test */ const vfloat T = dot(Vec3vf(tri_Ng),C) ^ sgnDen; valid &= (absDen*vfloat(ray.tnear()[k]) < T) & (T <= absDen*vfloat(ray.tfar[k])); if (likely(none(valid))) return false; /* calculate hit information */ new (&hit) MoellerTrumboreHitM(valid,U,V,T,absDen,tri_Ng); return true; } static __forceinline bool intersect1(RayK& ray, size_t k, const Vec3vf& v0, const Vec3vf& v1, const Vec3vf& v2, MoellerTrumboreHitM &hit) { const Vec3vf e1 = v0-v1; const Vec3vf e2 = v2-v0; const Vec3vf Ng = cross(e2,e1); return intersect1(ray,k,v0,e1,e2,Ng,hit); } }; template struct SubGridQuadMIntersectorKMoellerTrumbore : public SubGridQuadMIntersectorKMoellerTrumboreBase { __forceinline SubGridQuadMIntersectorKMoellerTrumbore(const vbool& valid, const RayK& ray) : SubGridQuadMIntersectorKMoellerTrumboreBase(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()); MoellerTrumboreHitM<4> hit; if (SubGridQuadMIntersectorKMoellerTrumboreBase<4,K,filter>::intersect1(ray,k,v0,v1,v3,hit)) { interpolateUV(hit,g,subgrid); epilog(hit.valid,hit); } if (SubGridQuadMIntersectorKMoellerTrumboreBase<4,K,filter>::intersect1(ray,k,v2,v3,v1,hit)) { hit.U = hit.absDen - hit.U; hit.V = hit.absDen - hit.V; interpolateUV(hit,g,subgrid); 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()); MoellerTrumboreHitM<4> hit; if (SubGridQuadMIntersectorKMoellerTrumboreBase<4,K,filter>::intersect1(ray,k,v0,v1,v3,hit)) { interpolateUV(hit,g,subgrid); if (epilog(hit.valid,hit)) return true; } if (SubGridQuadMIntersectorKMoellerTrumboreBase<4,K,filter>::intersect1(ray,k,v2,v3,v1,hit)) { hit.U = hit.absDen - hit.U; hit.V = hit.absDen - hit.V; interpolateUV(hit,g,subgrid); if (epilog(hit.valid,hit)) return true; } return false; } }; #if defined (__AVX__) /*! Intersects 4 quads with 1 ray using AVX */ template struct SubGridQuadMIntersectorKMoellerTrumbore<4,K,filter> : public SubGridQuadMIntersectorKMoellerTrumboreBase<4,K,filter> { __forceinline SubGridQuadMIntersectorKMoellerTrumbore(const vbool& valid, const RayK& ray) : SubGridQuadMIntersectorKMoellerTrumboreBase<4,K,filter>(valid,ray) {} template __forceinline bool intersect1(RayK& 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 const vbool8 flags(0,0,0,0,1,1,1,1); MoellerTrumboreHitM<8> hit; if (SubGridQuadMIntersectorKMoellerTrumboreBase<8,K,filter>::intersect1(ray,k,vtx0,vtx1,vtx2,hit)) { vfloat8 U = hit.U, V = hit.V, absDen = hit.absDen; #if !defined(EMBREE_BACKFACE_CULLING) hit.U = select(flags,absDen-V,U); hit.V = select(flags,absDen-U,V); hit.vNg *= select(flags,vfloat8(-1.0f),vfloat8(1.0f)); #else hit.U = select(flags,absDen-U,U); hit.V = select(flags,absDen-V,V); #endif /* correct U,V interpolation across the entire grid */ const vint8 sx((int)subgrid.x()); const vint8 sy((int)subgrid.y()); const vint8 sx8(sx + vint8(0,1,1,0,0,1,1,0)); const vint8 sy8(sy + vint8(0,0,1,1,0,0,1,1)); const float inv_resX = rcp((float)((int)g.resX-1)); const float inv_resY = rcp((float)((int)g.resY-1)); hit.U = (hit.U + (vfloat8)sx8 * absDen) * inv_resX; hit.V = (hit.V + (vfloat8)sy8 * absDen) * inv_resY; if (unlikely(epilog(hit.valid,hit))) return true; } return false; } __forceinline bool intersect1(RayHitK& 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& 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 } }