Path: blob/master/thirdparty/embree/kernels/geometry/quad_intersector_moeller.h
9905 views
// Copyright 2009-2021 Intel Corporation1// SPDX-License-Identifier: Apache-2.023#pragma once45#include "quadv.h"6#include "triangle_intersector_moeller.h"78namespace embree9{10namespace isa11{12template<int M>13struct QuadHitM14{15__forceinline QuadHitM() {}1617__forceinline QuadHitM(const vbool<M>& valid,18const vfloat<M>& U,19const vfloat<M>& V,20const vfloat<M>& T,21const vfloat<M>& absDen,22const Vec3vf<M>& Ng,23const vbool<M>& flags)24: U(U), V(V), T(T), absDen(absDen), tri_Ng(Ng), valid(valid), flags(flags) {}2526__forceinline void finalize()27{28const vfloat<M> rcpAbsDen = rcp(absDen);29vt = T * rcpAbsDen;30const vfloat<M> u = min(U * rcpAbsDen,1.0f);31const vfloat<M> v = min(V * rcpAbsDen,1.0f);32const vfloat<M> u1 = vfloat<M>(1.0f) - u;33const vfloat<M> v1 = vfloat<M>(1.0f) - v;34#if !defined(__AVX__) || defined(EMBREE_BACKFACE_CULLING)35vu = select(flags,u1,u);36vv = select(flags,v1,v);37vNg = Vec3vf<M>(tri_Ng.x,tri_Ng.y,tri_Ng.z);38#else39const vfloat<M> flip = select(flags,vfloat<M>(-1.0f),vfloat<M>(1.0f));40vv = select(flags,u1,v);41vu = select(flags,v1,u);42vNg = Vec3vf<M>(flip*tri_Ng.x,flip*tri_Ng.y,flip*tri_Ng.z);43#endif44}4546__forceinline Vec2f uv(const size_t i)47{48const float u = vu[i];49const float v = vv[i];50return Vec2f(u,v);51}5253__forceinline float t(const size_t i) { return vt[i]; }54__forceinline Vec3fa Ng(const size_t i) { return Vec3fa(vNg.x[i],vNg.y[i],vNg.z[i]); }5556private:57vfloat<M> U;58vfloat<M> V;59vfloat<M> T;60vfloat<M> absDen;61Vec3vf<M> tri_Ng;6263public:64vbool<M> valid;65vfloat<M> vu;66vfloat<M> vv;67vfloat<M> vt;68Vec3vf<M> vNg;6970public:71const vbool<M> flags;72};7374template<int K>75struct QuadHitK76{77__forceinline QuadHitK(const vfloat<K>& U,78const vfloat<K>& V,79const vfloat<K>& T,80const vfloat<K>& absDen,81const Vec3vf<K>& Ng,82const vbool<K>& flags)83: U(U), V(V), T(T), absDen(absDen), flags(flags), tri_Ng(Ng) {}8485__forceinline std::tuple<vfloat<K>,vfloat<K>,vfloat<K>,Vec3vf<K>> operator() () const86{87const vfloat<K> rcpAbsDen = rcp(absDen);88const vfloat<K> t = T * rcpAbsDen;89const vfloat<K> u0 = min(U * rcpAbsDen,1.0f);90const vfloat<K> v0 = min(V * rcpAbsDen,1.0f);91const vfloat<K> u1 = vfloat<K>(1.0f) - u0;92const vfloat<K> v1 = vfloat<K>(1.0f) - v0;93const vfloat<K> u = select(flags,u1,u0);94const vfloat<K> v = select(flags,v1,v0);95const Vec3vf<K> Ng(tri_Ng.x,tri_Ng.y,tri_Ng.z);96return std::make_tuple(u,v,t,Ng);97}9899private:100const vfloat<K> U;101const vfloat<K> V;102const vfloat<K> T;103const vfloat<K> absDen;104const vbool<K> flags;105const Vec3vf<K> tri_Ng;106};107108/* ----------------------------- */109/* -- single ray intersectors -- */110/* ----------------------------- */111112113template<int M, bool filter>114struct QuadMIntersector1MoellerTrumbore;115116/*! Intersects M quads with 1 ray */117template<int M, bool filter>118struct QuadMIntersector1MoellerTrumbore119{120__forceinline QuadMIntersector1MoellerTrumbore() {}121122__forceinline QuadMIntersector1MoellerTrumbore(const Ray& ray, const void* ptr) {}123124__forceinline void intersect(RayHit& ray, RayQueryContext* context,125const Vec3vf<M>& v0, const Vec3vf<M>& v1, const Vec3vf<M>& v2, const Vec3vf<M>& v3,126const vuint<M>& geomID, const vuint<M>& primID) const127{128UVIdentity<M> mapUV;129MoellerTrumboreHitM<M,UVIdentity<M>> hit(mapUV);130MoellerTrumboreIntersector1<M> intersector(ray,nullptr);131Intersect1EpilogM<M,filter> epilog(ray,context,geomID,primID);132133/* intersect first triangle */134if (intersector.intersect(ray,v0,v1,v3,mapUV,hit))135epilog(hit.valid,hit);136137/* intersect second triangle */138if (intersector.intersect(ray,v2,v3,v1,mapUV,hit))139{140hit.U = hit.absDen - hit.U;141hit.V = hit.absDen - hit.V;142epilog(hit.valid,hit);143}144}145146__forceinline bool occluded(Ray& ray, RayQueryContext* context,147const Vec3vf<M>& v0, const Vec3vf<M>& v1, const Vec3vf<M>& v2, const Vec3vf<M>& v3,148const vuint<M>& geomID, const vuint<M>& primID) const149{150UVIdentity<M> mapUV;151MoellerTrumboreHitM<M,UVIdentity<M>> hit(mapUV);152MoellerTrumboreIntersector1<M> intersector(ray,nullptr);153Occluded1EpilogM<M,filter> epilog(ray,context,geomID,primID);154155/* intersect first triangle */156if (intersector.intersect(ray,v0,v1,v3,mapUV,hit))157{158if (epilog(hit.valid,hit))159return true;160}161162/* intersect second triangle */163if (intersector.intersect(ray,v2,v3,v1,mapUV,hit))164{165hit.U = hit.absDen - hit.U;166hit.V = hit.absDen - hit.V;167if (epilog(hit.valid,hit))168return true;169}170return false;171}172};173174#if defined(__AVX__)175176/*! Intersects 4 quads with 1 ray using AVX */177template<bool filter>178struct QuadMIntersector1MoellerTrumbore<4,filter>179{180__forceinline QuadMIntersector1MoellerTrumbore() {}181182__forceinline QuadMIntersector1MoellerTrumbore(const Ray& ray, const void* ptr) {}183184template<typename Epilog>185__forceinline bool intersect(Ray& ray, const Vec3vf4& v0, const Vec3vf4& v1, const Vec3vf4& v2, const Vec3vf4& v3, const Epilog& epilog) const186{187const Vec3vf8 vtx0(vfloat8(v0.x,v2.x),vfloat8(v0.y,v2.y),vfloat8(v0.z,v2.z));188#if !defined(EMBREE_BACKFACE_CULLING)189const Vec3vf8 vtx1(vfloat8(v1.x),vfloat8(v1.y),vfloat8(v1.z));190const Vec3vf8 vtx2(vfloat8(v3.x),vfloat8(v3.y),vfloat8(v3.z));191#else192const Vec3vf8 vtx1(vfloat8(v1.x,v3.x),vfloat8(v1.y,v3.y),vfloat8(v1.z,v3.z));193const Vec3vf8 vtx2(vfloat8(v3.x,v1.x),vfloat8(v3.y,v1.y),vfloat8(v3.z,v1.z));194#endif195UVIdentity<8> mapUV;196MoellerTrumboreHitM<8,UVIdentity<8>> hit(mapUV);197MoellerTrumboreIntersector1<8> intersector(ray,nullptr);198const vbool8 flags(0,0,0,0,1,1,1,1);199if (unlikely(intersector.intersect(ray,vtx0,vtx1,vtx2,mapUV,hit)))200{201vfloat8 U = hit.U, V = hit.V, absDen = hit.absDen;202203#if !defined(EMBREE_BACKFACE_CULLING)204hit.U = select(flags,absDen-V,U);205hit.V = select(flags,absDen-U,V);206hit.vNg *= select(flags,vfloat8(-1.0f),vfloat8(1.0f)); // FIXME: use XOR207#else208hit.U = select(flags,absDen-U,U);209hit.V = select(flags,absDen-V,V);210#endif211if (unlikely(epilog(hit.valid,hit)))212return true;213}214return false;215}216217__forceinline bool intersect(RayHit& ray, RayQueryContext* context,218const Vec3vf4& v0, const Vec3vf4& v1, const Vec3vf4& v2, const Vec3vf4& v3,219const vuint4& geomID, const vuint4& primID) const220{221return intersect(ray,v0,v1,v2,v3,Intersect1EpilogM<8,filter>(ray,context,vuint8(geomID),vuint8(primID)));222}223224__forceinline bool occluded(Ray& ray, RayQueryContext* context,225const Vec3vf4& v0, const Vec3vf4& v1, const Vec3vf4& v2, const Vec3vf4& v3,226const vuint4& geomID, const vuint4& primID) const227{228return intersect(ray,v0,v1,v2,v3,Occluded1EpilogM<8,filter>(ray,context,vuint8(geomID),vuint8(primID)));229}230};231232#endif233234/* ----------------------------- */235/* -- ray packet intersectors -- */236/* ----------------------------- */237238239struct MoellerTrumboreIntersector1KTriangleM240{241/*! Intersect k'th ray from ray packet of size K with M triangles. */242template<int M, int K, typename Epilog>243static __forceinline bool intersect(RayK<K>& ray,244size_t k,245const Vec3vf<M>& tri_v0,246const Vec3vf<M>& tri_e1,247const Vec3vf<M>& tri_e2,248const Vec3vf<M>& tri_Ng,249const vbool<M>& flags,250const Epilog& epilog)251{252/* calculate denominator */253const Vec3vf<M> O = broadcast<vfloat<M>>(ray.org,k);254const Vec3vf<M> D = broadcast<vfloat<M>>(ray.dir,k);255const Vec3vf<M> C = Vec3vf<M>(tri_v0) - O;256const Vec3vf<M> R = cross(C,D);257const vfloat<M> den = dot(Vec3vf<M>(tri_Ng),D);258const vfloat<M> absDen = abs(den);259const vfloat<M> sgnDen = signmsk(den);260261/* perform edge tests */262const vfloat<M> U = dot(R,Vec3vf<M>(tri_e2)) ^ sgnDen;263const vfloat<M> V = dot(R,Vec3vf<M>(tri_e1)) ^ sgnDen;264265/* perform backface culling */266#if defined(EMBREE_BACKFACE_CULLING)267vbool<M> valid = (den < vfloat<M>(zero)) & (U >= 0.0f) & (V >= 0.0f) & (U+V<=absDen);268#else269vbool<M> valid = (den != vfloat<M>(zero)) & (U >= 0.0f) & (V >= 0.0f) & (U+V<=absDen);270#endif271if (likely(none(valid))) return false;272273/* perform depth test */274const vfloat<M> T = dot(Vec3vf<M>(tri_Ng),C) ^ sgnDen;275valid &= (absDen*vfloat<M>(ray.tnear()[k]) < T) & (T <= absDen*vfloat<M>(ray.tfar[k]));276if (likely(none(valid))) return false;277278/* calculate hit information */279QuadHitM<M> hit(valid,U,V,T,absDen,tri_Ng,flags);280return epilog(valid,hit);281}282283template<int M, int K, typename Epilog>284static __forceinline bool intersect1(RayK<K>& ray,285size_t k,286const Vec3vf<M>& v0,287const Vec3vf<M>& v1,288const Vec3vf<M>& v2,289const vbool<M>& flags,290const Epilog& epilog)291{292const Vec3vf<M> e1 = v0-v1;293const Vec3vf<M> e2 = v2-v0;294const Vec3vf<M> Ng = cross(e2,e1);295return intersect<M,K>(ray,k,v0,e1,e2,Ng,flags,epilog);296}297};298299template<int M, int K, bool filter>300struct QuadMIntersectorKMoellerTrumboreBase301{302__forceinline QuadMIntersectorKMoellerTrumboreBase(const vbool<K>& valid, const RayK<K>& ray) {}303304/*! Intersects K rays with one of M triangles. */305template<typename Epilog>306__forceinline vbool<K> intersectK(const vbool<K>& valid0,307RayK<K>& ray,308const Vec3vf<K>& tri_v0,309const Vec3vf<K>& tri_e1,310const Vec3vf<K>& tri_e2,311const Vec3vf<K>& tri_Ng,312const vbool<K>& flags,313const Epilog& epilog) const314{315/* calculate denominator */316vbool<K> valid = valid0;317const Vec3vf<K> C = tri_v0 - ray.org;318const Vec3vf<K> R = cross(C,ray.dir);319const vfloat<K> den = dot(tri_Ng,ray.dir);320const vfloat<K> absDen = abs(den);321const vfloat<K> sgnDen = signmsk(den);322323/* test against edge p2 p0 */324const vfloat<K> U = dot(R,tri_e2) ^ sgnDen;325valid &= U >= 0.0f;326if (likely(none(valid))) return false;327328/* test against edge p0 p1 */329const vfloat<K> V = dot(R,tri_e1) ^ sgnDen;330valid &= V >= 0.0f;331if (likely(none(valid))) return false;332333/* test against edge p1 p2 */334const vfloat<K> W = absDen-U-V;335valid &= W >= 0.0f;336if (likely(none(valid))) return false;337338/* perform depth test */339const vfloat<K> T = dot(tri_Ng,C) ^ sgnDen;340valid &= (absDen*ray.tnear() < T) & (T <= absDen*ray.tfar);341if (unlikely(none(valid))) return false;342343/* perform backface culling */344#if defined(EMBREE_BACKFACE_CULLING)345valid &= den < vfloat<K>(zero);346if (unlikely(none(valid))) return false;347#else348valid &= den != vfloat<K>(zero);349if (unlikely(none(valid))) return false;350#endif351352/* calculate hit information */353QuadHitK<K> hit(U,V,T,absDen,tri_Ng,flags);354return epilog(valid,hit);355}356357/*! Intersects K rays with one of M quads. */358template<typename Epilog>359__forceinline vbool<K> intersectK(const vbool<K>& valid0,360RayK<K>& ray,361const Vec3vf<K>& tri_v0,362const Vec3vf<K>& tri_v1,363const Vec3vf<K>& tri_v2,364const vbool<K>& flags,365const Epilog& epilog) const366{367const Vec3vf<K> e1 = tri_v0-tri_v1;368const Vec3vf<K> e2 = tri_v2-tri_v0;369const Vec3vf<K> Ng = cross(e2,e1);370return intersectK(valid0,ray,tri_v0,e1,e2,Ng,flags,epilog);371}372373/*! Intersects K rays with one of M quads. */374template<typename Epilog>375__forceinline bool intersectK(const vbool<K>& valid0,376RayK<K>& ray,377const Vec3vf<K>& v0,378const Vec3vf<K>& v1,379const Vec3vf<K>& v2,380const Vec3vf<K>& v3,381const Epilog& epilog) const382{383intersectK(valid0,ray,v0,v1,v3,vbool<K>(false),epilog);384if (none(valid0)) return true;385intersectK(valid0,ray,v2,v3,v1,vbool<K>(true ),epilog);386return none(valid0);387}388};389390template<int M, int K, bool filter>391struct QuadMIntersectorKMoellerTrumbore : public QuadMIntersectorKMoellerTrumboreBase<M,K,filter>392{393__forceinline QuadMIntersectorKMoellerTrumbore(const vbool<K>& valid, const RayK<K>& ray)394: QuadMIntersectorKMoellerTrumboreBase<M,K,filter>(valid,ray) {}395396__forceinline void intersect1(RayHitK<K>& ray, size_t k, RayQueryContext* context,397const Vec3vf<M>& v0, const Vec3vf<M>& v1, const Vec3vf<M>& v2, const Vec3vf<M>& v3,398const vuint<M>& geomID, const vuint<M>& primID) const399{400Intersect1KEpilogM<M,K,filter> epilog(ray,k,context,geomID,primID);401MoellerTrumboreIntersector1KTriangleM::intersect1<M,K>(ray,k,v0,v1,v3,vbool<M>(false),epilog);402MoellerTrumboreIntersector1KTriangleM::intersect1<M,K>(ray,k,v2,v3,v1,vbool<M>(true ),epilog);403}404405__forceinline bool occluded1(RayK<K>& ray, size_t k, RayQueryContext* context,406const Vec3vf<M>& v0, const Vec3vf<M>& v1, const Vec3vf<M>& v2, const Vec3vf<M>& v3,407const vuint<M>& geomID, const vuint<M>& primID) const408{409Occluded1KEpilogM<M,K,filter> epilog(ray,k,context,geomID,primID);410if (MoellerTrumboreIntersector1KTriangleM::intersect1<M,K>(ray,k,v0,v1,v3,vbool<M>(false),epilog)) return true;411if (MoellerTrumboreIntersector1KTriangleM::intersect1<M,K>(ray,k,v2,v3,v1,vbool<M>(true ),epilog)) return true;412return false;413}414};415416417#if defined(__AVX__)418419/*! Intersects 4 quads with 1 ray using AVX */420template<int K, bool filter>421struct QuadMIntersectorKMoellerTrumbore<4,K,filter> : public QuadMIntersectorKMoellerTrumboreBase<4,K,filter>422{423__forceinline QuadMIntersectorKMoellerTrumbore(const vbool<K>& valid, const RayK<K>& ray)424: QuadMIntersectorKMoellerTrumboreBase<4,K,filter>(valid,ray) {}425426template<typename Epilog>427__forceinline bool intersect1(RayK<K>& ray, size_t k,428const Vec3vf4& v0, const Vec3vf4& v1, const Vec3vf4& v2, const Vec3vf4& v3, const Epilog& epilog) const429{430const Vec3vf8 vtx0(vfloat8(v0.x,v2.x),vfloat8(v0.y,v2.y),vfloat8(v0.z,v2.z));431#if !defined(EMBREE_BACKFACE_CULLING)432const Vec3vf8 vtx1(vfloat8(v1.x),vfloat8(v1.y),vfloat8(v1.z));433const Vec3vf8 vtx2(vfloat8(v3.x),vfloat8(v3.y),vfloat8(v3.z));434#else435const Vec3vf8 vtx1(vfloat8(v1.x,v3.x),vfloat8(v1.y,v3.y),vfloat8(v1.z,v3.z));436const Vec3vf8 vtx2(vfloat8(v3.x,v1.x),vfloat8(v3.y,v1.y),vfloat8(v3.z,v1.z));437#endif438const vbool8 flags(0,0,0,0,1,1,1,1);439return MoellerTrumboreIntersector1KTriangleM::intersect1<8,K>(ray,k,vtx0,vtx1,vtx2,flags,epilog);440}441442__forceinline bool intersect1(RayHitK<K>& ray, size_t k, RayQueryContext* context,443const Vec3vf4& v0, const Vec3vf4& v1, const Vec3vf4& v2, const Vec3vf4& v3,444const vuint4& geomID, const vuint4& primID) const445{446return intersect1(ray,k,v0,v1,v2,v3,Intersect1KEpilogM<8,K,filter>(ray,k,context,vuint8(geomID),vuint8(primID)));447}448449__forceinline bool occluded1(RayK<K>& ray, size_t k, RayQueryContext* context,450const Vec3vf4& v0, const Vec3vf4& v1, const Vec3vf4& v2, const Vec3vf4& v3,451const vuint4& geomID, const vuint4& primID) const452{453return intersect1(ray,k,v0,v1,v2,v3,Occluded1KEpilogM<8,K,filter>(ray,k,context,vuint8(geomID),vuint8(primID)));454}455};456457#endif458}459}460461462