// Copyright 2009-2021 Intel Corporation // SPDX-License-Identifier: Apache-2.0 #pragma once #include "triangle.h" #include "intersector_epilog.h" /*! This intersector implements a modified version of the Woop's ray-triangle intersection test */ namespace embree { namespace isa { template struct WoopHitM { __forceinline WoopHitM() {} __forceinline WoopHitM(const vbool& valid, const vfloat& U, const vfloat& V, const vfloat& T, const vfloat& inv_det, const Vec3vf& Ng) : U(U), V(V), T(T), inv_det(inv_det), valid(valid), vNg(Ng) {} __forceinline void finalize() { vt = T; vu = U*inv_det; vv = V*inv_det; } __forceinline Vec2f uv (const size_t i) const { return Vec2f(vu[i],vv[i]); } __forceinline float t (const size_t i) const { return vt[i]; } __forceinline Vec3fa Ng(const size_t i) const { return Vec3fa(vNg.x[i],vNg.y[i],vNg.z[i]); } private: const vfloat U; const vfloat V; const vfloat T; const vfloat inv_det; public: const vbool valid; vfloat vu; vfloat vv; vfloat vt; Vec3vf vNg; }; template struct WoopPrecalculations1 { unsigned int kx,ky,kz; Vec3vf org; Vec3fa S; __forceinline WoopPrecalculations1() {} __forceinline WoopPrecalculations1(const Ray& ray, const void* ptr) { kz = maxDim(abs(ray.dir)); kx = (kz+1) % 3; ky = (kx+1) % 3; const float inv_dir_kz = rcp(ray.dir[kz]); if (ray.dir[kz] < 0.0f) std::swap(kx,ky); S.x = ray.dir[kx] * inv_dir_kz; S.y = ray.dir[ky] * inv_dir_kz; S.z = inv_dir_kz; org = Vec3vf(ray.org[kx],ray.org[ky],ray.org[kz]); } }; template struct WoopIntersector1 { typedef WoopPrecalculations1 Precalculations; __forceinline WoopIntersector1() {} __forceinline WoopIntersector1(const Ray& ray, const void* ptr) {} static __forceinline bool intersect(const vbool& valid0, Ray& ray, const Precalculations& pre, const Vec3vf& tri_v0, const Vec3vf& tri_v1, const Vec3vf& tri_v2, WoopHitM& hit) { vbool valid = valid0; /* vertices relative to ray origin */ const Vec3vf org = Vec3vf(pre.org.x,pre.org.y,pre.org.z); const Vec3vf A = Vec3vf(tri_v0[pre.kx],tri_v0[pre.ky],tri_v0[pre.kz]) - org; const Vec3vf B = Vec3vf(tri_v1[pre.kx],tri_v1[pre.ky],tri_v1[pre.kz]) - org; const Vec3vf C = Vec3vf(tri_v2[pre.kx],tri_v2[pre.ky],tri_v2[pre.kz]) - org; /* shear and scale vertices */ const vfloat Ax = nmadd(A.z,pre.S.x,A.x); const vfloat Ay = nmadd(A.z,pre.S.y,A.y); const vfloat Bx = nmadd(B.z,pre.S.x,B.x); const vfloat By = nmadd(B.z,pre.S.y,B.y); const vfloat Cx = nmadd(C.z,pre.S.x,C.x); const vfloat Cy = nmadd(C.z,pre.S.y,C.y); /* scaled barycentric */ const vfloat U0 = Cx*By; const vfloat U1 = Cy*Bx; const vfloat V0 = Ax*Cy; const vfloat V1 = Ay*Cx; const vfloat W0 = Bx*Ay; const vfloat W1 = By*Ax; #if !defined(__AVX512F__) valid &= (U0 >= U1) & (V0 >= V1) & (W0 >= W1) | (U0 <= U1) & (V0 <= V1) & (W0 <= W1); #else valid &= ge(ge(U0 >= U1,V0,V1),W0,W1) | le(le(U0 <= U1,V0,V1),W0,W1); #endif if (likely(none(valid))) return false; const vfloat U = U0-U1; const vfloat V = V0-V1; const vfloat W = W0-W1; const vfloat det = U+V+W; valid &= det != 0.0f; const vfloat inv_det = rcp(det); const vfloat Az = pre.S.z * A.z; const vfloat Bz = pre.S.z * B.z; const vfloat Cz = pre.S.z * C.z; const vfloat T = madd(U,Az,madd(V,Bz,W*Cz)); const vfloat t = T * inv_det; /* perform depth test */ valid &= (vfloat(ray.tnear()) < t) & (t <= vfloat(ray.tfar)); if (likely(none(valid))) return false; const Vec3vf tri_Ng = cross(tri_v2-tri_v0,tri_v0-tri_v1); /* update hit information */ new (&hit) WoopHitM(valid,U,V,t,inv_det,tri_Ng); return true; } static __forceinline bool intersect(Ray& ray, const Precalculations& pre, const Vec3vf& v0, const Vec3vf& v1, const Vec3vf& v2, WoopHitM& hit) { vbool valid = true; return intersect(valid,ray,pre,v0,v1,v2,hit); } template static __forceinline bool intersect(Ray& ray, const Precalculations& pre, const Vec3vf& v0, const Vec3vf& v1, const Vec3vf& v2, const Epilog& epilog) { WoopHitM hit; if (likely(intersect(ray,pre,v0,v1,v2,hit))) return epilog(hit.valid,hit); return false; } template static __forceinline bool intersect(const vbool& valid, Ray& ray, const Precalculations& pre, const Vec3vf& v0, const Vec3vf& v1, const Vec3vf& v2, const Epilog& epilog) { WoopHitM hit; if (likely(intersect(valid,ray,pre,v0,v1,v2,hit))) return epilog(hit.valid,hit); return false; } }; #if 0 template struct WoopHitK { __forceinline WoopHitK(const vfloat& U, const vfloat& V, const vfloat& T, const vfloat& absDen, const Vec3vf& Ng) : U(U), V(V), T(T), absDen(absDen), Ng(Ng) {} __forceinline std::tuple,vfloat,vfloat,Vec3vf> operator() () const { const vfloat rcpAbsDen = rcp(absDen); const vfloat t = T * rcpAbsDen; const vfloat u = U * rcpAbsDen; const vfloat v = V * rcpAbsDen; return std::make_tuple(u,v,t,Ng); } private: const vfloat U; const vfloat V; const vfloat T; const vfloat absDen; const Vec3vf Ng; }; template struct WoopIntersectorK { __forceinline WoopIntersectorK(const vbool& valid, const RayK& ray) {} /*! Intersects K rays with one of M triangles. */ template __forceinline vbool intersectK(const vbool& valid0, //RayK& ray, const Vec3vf& ray_org, const Vec3vf& ray_dir, const vfloat& ray_tnear, const vfloat& ray_tfar, const Vec3vf& tri_v0, const Vec3vf& tri_e1, const Vec3vf& tri_e2, const Vec3vf& tri_Ng, 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(tri_e2,R) ^ sgnDen; valid &= U >= 0.0f; if (likely(none(valid))) return false; /* test against edge p0 p1 */ const vfloat V = dot(tri_e1,R) ^ 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 */ WoopHitK hit(U,V,T,absDen,tri_Ng); return epilog(valid,hit); } /*! Intersects K rays with one of M triangles. */ template __forceinline vbool intersectK(const vbool& valid0, RayK& ray, const Vec3vf& tri_v0, const Vec3vf& tri_v1, const Vec3vf& tri_v2, 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.org,ray.dir,ray.tnear(),ray.tfar,tri_v0,e1,e2,Ng,epilog); } /*! Intersects K rays with one of M triangles. */ template __forceinline vbool intersectEdgeK(const vbool& valid0, RayK& ray, const Vec3vf& tri_v0, const Vec3vf& tri_e1, const Vec3vf& tri_e2, const Epilog& epilog) const { const Vec3vf tri_Ng = cross(tri_e2,tri_e1); return intersectK(valid0,ray.org,ray.dir,ray.tnear(),ray.tfar,tri_v0,tri_e1,tri_e2,tri_Ng,epilog); } /*! Intersect k'th ray from ray packet of size K with M triangles. */ __forceinline bool intersectEdge(RayK& ray, size_t k, const Vec3vf& tri_v0, const Vec3vf& tri_e1, const Vec3vf& tri_e2, WoopHitM& hit) const { /* calculate denominator */ typedef Vec3vf Vec3vfM; const Vec3vf tri_Ng = cross(tri_e2,tri_e1); const Vec3vfM O = broadcast>(ray.org,k); const Vec3vfM D = broadcast>(ray.dir,k); const Vec3vfM C = Vec3vfM(tri_v0) - O; const Vec3vfM R = cross(C,D); const vfloat den = dot(Vec3vfM(tri_Ng),D); const vfloat absDen = abs(den); const vfloat sgnDen = signmsk(den); /* perform edge tests */ const vfloat U = dot(Vec3vf(tri_e2),R) ^ sgnDen; const vfloat V = dot(Vec3vf(tri_e1),R) ^ 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) WoopHitM(valid,U,V,T,absDen,tri_Ng); return true; } __forceinline bool intersectEdge(RayK& ray, size_t k, const BBox>& time_range, const Vec3vf& tri_v0, const Vec3vf& tri_e1, const Vec3vf& tri_e2, WoopHitM& hit) const { if (likely(intersect(ray,k,tri_v0,tri_e1,tri_e2,hit))) { hit.valid &= time_range.lower <= vfloat(ray.time[k]); hit.valid &= vfloat(ray.time[k]) < time_range.upper; return any(hit.valid); } return false; } template __forceinline bool intersectEdge(RayK& ray, size_t k, const Vec3vf& tri_v0, const Vec3vf& tri_e1, const Vec3vf& tri_e2, const Epilog& epilog) const { WoopHitM hit; if (likely(intersectEdge(ray,k,tri_v0,tri_e1,tri_e2,hit))) return epilog(hit.valid,hit); return false; } template __forceinline bool intersectEdge(RayK& ray, size_t k, const BBox>& time_range, const Vec3vf& tri_v0, const Vec3vf& tri_e1, const Vec3vf& tri_e2, const Epilog& epilog) const { WoopHitM hit; if (likely(intersectEdge(ray,k,time_range,tri_v0,tri_e1,tri_e2,hit))) return epilog(hit.valid,hit); return false; } template __forceinline bool intersect(RayK& ray, size_t k, const Vec3vf& v0, const Vec3vf& v1, const Vec3vf& v2, const Epilog& epilog) const { const Vec3vf e1 = v0-v1; const Vec3vf e2 = v2-v0; return intersectEdge(ray,k,v0,e1,e2,epilog); } template __forceinline bool intersect(RayK& ray, size_t k, const BBox>& time_range, const Vec3vf& v0, const Vec3vf& v1, const Vec3vf& v2, const Epilog& epilog) const { const Vec3vf e1 = v0-v1; const Vec3vf e2 = v2-v0; return intersectEdge(ray,k,time_range,v0,e1,e2,epilog); } }; #endif } }