// Copyright 2009-2021 Intel Corporation // SPDX-License-Identifier: Apache-2.0 #pragma once #include "node_intersector.h" namespace embree { namespace isa { ////////////////////////////////////////////////////////////////////////////////////// // Ray structure used in single-ray traversal ////////////////////////////////////////////////////////////////////////////////////// template struct TravRayBase; /* Base (without tnear and tfar) */ template struct TravRayBase { __forceinline TravRayBase() {} __forceinline TravRayBase(const Vec3fa& ray_org, const Vec3fa& ray_dir) : org_xyz(ray_org), dir_xyz(ray_dir) { const Vec3fa ray_rdir = rcp_safe(ray_dir); org = Vec3vf(ray_org.x,ray_org.y,ray_org.z); dir = Vec3vf(ray_dir.x,ray_dir.y,ray_dir.z); rdir = Vec3vf(ray_rdir.x,ray_rdir.y,ray_rdir.z); #if defined(__AVX2__) || defined(__ARM_NEON) const Vec3fa ray_org_rdir = ray_org*ray_rdir; org_rdir = Vec3vf(ray_org_rdir.x,ray_org_rdir.y,ray_org_rdir.z); #endif nearX = ray_rdir.x >= 0.0f ? 0*sizeof(vfloat) : 1*sizeof(vfloat); nearY = ray_rdir.y >= 0.0f ? 2*sizeof(vfloat) : 3*sizeof(vfloat); nearZ = ray_rdir.z >= 0.0f ? 4*sizeof(vfloat) : 5*sizeof(vfloat); farX = nearX ^ sizeof(vfloat); farY = nearY ^ sizeof(vfloat); farZ = nearZ ^ sizeof(vfloat); } template __forceinline void init(size_t k, const Vec3vf& ray_org, const Vec3vf& ray_dir, const Vec3vf& ray_rdir, const Vec3vi& nearXYZ, size_t flip = sizeof(vfloat)) { org = Vec3vf(ray_org.x[k], ray_org.y[k], ray_org.z[k]); dir = Vec3vf(ray_dir.x[k], ray_dir.y[k], ray_dir.z[k]); rdir = Vec3vf(ray_rdir.x[k], ray_rdir.y[k], ray_rdir.z[k]); #if defined(__AVX2__) || defined(__ARM_NEON) org_rdir = org*rdir; #endif nearX = nearXYZ.x[k]; nearY = nearXYZ.y[k]; nearZ = nearXYZ.z[k]; farX = nearX ^ flip; farY = nearY ^ flip; farZ = nearZ ^ flip; } Vec3fa org_xyz, dir_xyz; Vec3vf org, dir, rdir; #if defined(__AVX2__) || defined(__ARM_NEON) Vec3vf org_rdir; #endif size_t nearX, nearY, nearZ; size_t farX, farY, farZ; }; /* Base (without tnear and tfar) */ template struct TravRayBase { __forceinline TravRayBase() {} __forceinline TravRayBase(const Vec3fa& ray_org, const Vec3fa& ray_dir) : org_xyz(ray_org), dir_xyz(ray_dir) { const float round_down = 1.0f-3.0f*float(ulp); const float round_up = 1.0f+3.0f*float(ulp); const Vec3fa ray_rdir = 1.0f/zero_fix(ray_dir); const Vec3fa ray_rdir_near = round_down*ray_rdir; const Vec3fa ray_rdir_far = round_up *ray_rdir; org = Vec3vf(ray_org.x,ray_org.y,ray_org.z); dir = Vec3vf(ray_dir.x,ray_dir.y,ray_dir.z); rdir_near = Vec3vf(ray_rdir_near.x,ray_rdir_near.y,ray_rdir_near.z); rdir_far = Vec3vf(ray_rdir_far .x,ray_rdir_far .y,ray_rdir_far .z); nearX = ray_rdir_near.x >= 0.0f ? 0*sizeof(vfloat) : 1*sizeof(vfloat); nearY = ray_rdir_near.y >= 0.0f ? 2*sizeof(vfloat) : 3*sizeof(vfloat); nearZ = ray_rdir_near.z >= 0.0f ? 4*sizeof(vfloat) : 5*sizeof(vfloat); farX = nearX ^ sizeof(vfloat); farY = nearY ^ sizeof(vfloat); farZ = nearZ ^ sizeof(vfloat); } template __forceinline void init(size_t k, const Vec3vf& ray_org, const Vec3vf& ray_dir, const Vec3vf& ray_rdir, const Vec3vi& nearXYZ, size_t flip = sizeof(vfloat)) { const vfloat round_down = 1.0f-3.0f*float(ulp); const vfloat round_up = 1.0f+3.0f*float(ulp); org = Vec3vf(ray_org.x[k], ray_org.y[k], ray_org.z[k]); dir = Vec3vf(ray_dir.x[k], ray_dir.y[k], ray_dir.z[k]); rdir_near = round_down*Vec3vf(ray_rdir.x[k], ray_rdir.y[k], ray_rdir.z[k]); rdir_far = round_up *Vec3vf(ray_rdir.x[k], ray_rdir.y[k], ray_rdir.z[k]); nearX = nearXYZ.x[k]; nearY = nearXYZ.y[k]; nearZ = nearXYZ.z[k]; farX = nearX ^ flip; farY = nearY ^ flip; farZ = nearZ ^ flip; } Vec3fa org_xyz, dir_xyz; Vec3vf org, dir, rdir_near, rdir_far; size_t nearX, nearY, nearZ; size_t farX, farY, farZ; }; /* Full (with tnear and tfar) */ template struct TravRay : TravRayBase { __forceinline TravRay() {} __forceinline TravRay(const Vec3fa& ray_org, const Vec3fa& ray_dir, float ray_tnear, float ray_tfar) : TravRayBase(ray_org, ray_dir), tnear(ray_tnear), tfar(ray_tfar) {} template __forceinline void init(size_t k, const Vec3vf& ray_org, const Vec3vf& ray_dir, const Vec3vf& ray_rdir, const Vec3vi& nearXYZ, float ray_tnear, float ray_tfar, size_t flip = sizeof(vfloat)) { TravRayBase::template init(k, ray_org, ray_dir, ray_rdir, nearXYZ, flip); tnear = ray_tnear; tfar = ray_tfar; } vfloat tnear; vfloat tfar; }; ////////////////////////////////////////////////////////////////////////////////////// // Point Query structure used in single-ray traversal ////////////////////////////////////////////////////////////////////////////////////// template struct TravPointQuery { __forceinline TravPointQuery() {} __forceinline TravPointQuery(const Vec3fa& query_org, const Vec3fa& query_rad) { org = Vec3vf(query_org.x, query_org.y, query_org.z); rad = Vec3vf(query_rad.x, query_rad.y, query_rad.z); } __forceinline vfloat const& tfar() const { return rad.x; } Vec3vf org, rad; }; ////////////////////////////////////////////////////////////////////////////////////// // point query ////////////////////////////////////////////////////////////////////////////////////// template __forceinline size_t pointQuerySphereDistAndMask( const TravPointQuery& query, vfloat& dist, vfloat const& minX, vfloat const& maxX, vfloat const& minY, vfloat const& maxY, vfloat const& minZ, vfloat const& maxZ) { const vfloat vX = min(max(query.org.x, minX), maxX) - query.org.x; const vfloat vY = min(max(query.org.y, minY), maxY) - query.org.y; const vfloat vZ = min(max(query.org.z, minZ), maxZ) - query.org.z; dist = vX * vX + vY * vY + vZ * vZ; const vbool vmask = dist <= query.tfar()*query.tfar(); const vbool valid = minX <= maxX; return movemask(vmask) & movemask(valid); } template __forceinline size_t pointQueryNodeSphere(const typename BVHN::AABBNode* node, const TravPointQuery& query, vfloat& dist) { const vfloat minX = vfloat::load((float*)((const char*)&node->lower_x)); const vfloat minY = vfloat::load((float*)((const char*)&node->lower_y)); const vfloat minZ = vfloat::load((float*)((const char*)&node->lower_z)); const vfloat maxX = vfloat::load((float*)((const char*)&node->upper_x)); const vfloat maxY = vfloat::load((float*)((const char*)&node->upper_y)); const vfloat maxZ = vfloat::load((float*)((const char*)&node->upper_z)); return pointQuerySphereDistAndMask(query, dist, minX, maxX, minY, maxY, minZ, maxZ); } template __forceinline size_t pointQueryNodeSphere(const typename BVHN::AABBNodeMB* node, const TravPointQuery& query, const float time, vfloat& dist) { const vfloat* pMinX = (const vfloat*)((const char*)&node->lower_x); const vfloat* pMinY = (const vfloat*)((const char*)&node->lower_y); const vfloat* pMinZ = (const vfloat*)((const char*)&node->lower_z); const vfloat* pMaxX = (const vfloat*)((const char*)&node->upper_x); const vfloat* pMaxY = (const vfloat*)((const char*)&node->upper_y); const vfloat* pMaxZ = (const vfloat*)((const char*)&node->upper_z); const vfloat minX = madd(time,pMinX[6],vfloat(pMinX[0])); const vfloat minY = madd(time,pMinY[6],vfloat(pMinY[0])); const vfloat minZ = madd(time,pMinZ[6],vfloat(pMinZ[0])); const vfloat maxX = madd(time,pMaxX[6],vfloat(pMaxX[0])); const vfloat maxY = madd(time,pMaxY[6],vfloat(pMaxY[0])); const vfloat maxZ = madd(time,pMaxZ[6],vfloat(pMaxZ[0])); return pointQuerySphereDistAndMask(query, dist, minX, maxX, minY, maxY, minZ, maxZ); } template __forceinline size_t pointQueryNodeSphereMB4D(const typename BVHN::NodeRef ref, const TravPointQuery& query, const float time, vfloat& dist) { const typename BVHN::AABBNodeMB* node = ref.getAABBNodeMB(); size_t mask = pointQueryNodeSphere(node, query, time, dist); if (unlikely(ref.isAABBNodeMB4D())) { const typename BVHN::AABBNodeMB4D* node1 = (const typename BVHN::AABBNodeMB4D*) node; const vbool vmask = (node1->lower_t <= time) & (time < node1->upper_t); mask &= movemask(vmask); } return mask; } template __forceinline size_t pointQueryNodeSphere(const typename BVHN::QuantizedBaseNode* node, const TravPointQuery& query, vfloat& dist) { const vfloat start_x(node->start.x); const vfloat scale_x(node->scale.x); const vfloat minX = madd(node->template dequantize((0*sizeof(vfloat)) >> 2),scale_x,start_x); const vfloat maxX = madd(node->template dequantize((1*sizeof(vfloat)) >> 2),scale_x,start_x); const vfloat start_y(node->start.y); const vfloat scale_y(node->scale.y); const vfloat minY = madd(node->template dequantize((2*sizeof(vfloat)) >> 2),scale_y,start_y); const vfloat maxY = madd(node->template dequantize((3*sizeof(vfloat)) >> 2),scale_y,start_y); const vfloat start_z(node->start.z); const vfloat scale_z(node->scale.z); const vfloat minZ = madd(node->template dequantize((4*sizeof(vfloat)) >> 2),scale_z,start_z); const vfloat maxZ = madd(node->template dequantize((5*sizeof(vfloat)) >> 2),scale_z,start_z); return pointQuerySphereDistAndMask(query, dist, minX, maxX, minY, maxY, minZ, maxZ) & movemask(node->validMask()); } template __forceinline size_t pointQueryNodeSphere(const typename BVHN::QuantizedBaseNodeMB* node, const TravPointQuery& query, const float time, vfloat& dist) { const vfloat minX = node->dequantizeLowerX(time); const vfloat maxX = node->dequantizeUpperX(time); const vfloat minY = node->dequantizeLowerY(time); const vfloat maxY = node->dequantizeUpperY(time); const vfloat minZ = node->dequantizeLowerZ(time); const vfloat maxZ = node->dequantizeUpperZ(time); return pointQuerySphereDistAndMask(query, dist, minX, maxX, minY, maxY, minZ, maxZ) & movemask(node->validMask()); } template __forceinline size_t pointQueryNodeSphere(const typename BVHN::OBBNode* node, const TravPointQuery& query, vfloat& dist) { // TODO: point query - implement const vbool vmask = vbool(true); const size_t mask = movemask(vmask) & ((1<(0.0f); return mask; } template __forceinline size_t pointQueryNodeSphere(const typename BVHN::OBBNodeMB* node, const TravPointQuery& query, const float time, vfloat& dist) { // TODO: point query - implement const vbool vmask = vbool(true); const size_t mask = movemask(vmask) & ((1<(0.0f); return mask; } template __forceinline size_t pointQueryAABBDistAndMask( const TravPointQuery& query, vfloat& dist, vfloat const& minX, vfloat const& maxX, vfloat const& minY, vfloat const& maxY, vfloat const& minZ, vfloat const& maxZ) { const vfloat vX = min(max(query.org.x, minX), maxX) - query.org.x; const vfloat vY = min(max(query.org.y, minY), maxY) - query.org.y; const vfloat vZ = min(max(query.org.z, minZ), maxZ) - query.org.z; dist = vX * vX + vY * vY + vZ * vZ; const vbool valid = minX <= maxX; const vbool vmask = !((maxX < query.org.x - query.rad.x) | (minX > query.org.x + query.rad.x) | (maxY < query.org.y - query.rad.y) | (minY > query.org.y + query.rad.y) | (maxZ < query.org.z - query.rad.z) | (minZ > query.org.z + query.rad.z)); return movemask(vmask) & movemask(valid); } template __forceinline size_t pointQueryNodeAABB(const typename BVHN::AABBNode* node, const TravPointQuery& query, vfloat& dist) { const vfloat minX = vfloat::load((float*)((const char*)&node->lower_x)); const vfloat minY = vfloat::load((float*)((const char*)&node->lower_y)); const vfloat minZ = vfloat::load((float*)((const char*)&node->lower_z)); const vfloat maxX = vfloat::load((float*)((const char*)&node->upper_x)); const vfloat maxY = vfloat::load((float*)((const char*)&node->upper_y)); const vfloat maxZ = vfloat::load((float*)((const char*)&node->upper_z)); return pointQueryAABBDistAndMask(query, dist, minX, maxX, minY, maxY, minZ, maxZ); } template __forceinline size_t pointQueryNodeAABB(const typename BVHN::AABBNodeMB* node, const TravPointQuery& query, const float time, vfloat& dist) { const vfloat* pMinX = (const vfloat*)((const char*)&node->lower_x); const vfloat* pMinY = (const vfloat*)((const char*)&node->lower_y); const vfloat* pMinZ = (const vfloat*)((const char*)&node->lower_z); const vfloat* pMaxX = (const vfloat*)((const char*)&node->upper_x); const vfloat* pMaxY = (const vfloat*)((const char*)&node->upper_y); const vfloat* pMaxZ = (const vfloat*)((const char*)&node->upper_z); const vfloat minX = madd(time,pMinX[6],vfloat(pMinX[0])); const vfloat minY = madd(time,pMinY[6],vfloat(pMinY[0])); const vfloat minZ = madd(time,pMinZ[6],vfloat(pMinZ[0])); const vfloat maxX = madd(time,pMaxX[6],vfloat(pMaxX[0])); const vfloat maxY = madd(time,pMaxY[6],vfloat(pMaxY[0])); const vfloat maxZ = madd(time,pMaxZ[6],vfloat(pMaxZ[0])); return pointQueryAABBDistAndMask(query, dist, minX, maxX, minY, maxY, minZ, maxZ); } template __forceinline size_t pointQueryNodeAABBMB4D(const typename BVHN::NodeRef ref, const TravPointQuery& query, const float time, vfloat& dist) { const typename BVHN::AABBNodeMB* node = ref.getAABBNodeMB(); size_t mask = pointQueryNodeAABB(node, query, time, dist); if (unlikely(ref.isAABBNodeMB4D())) { const typename BVHN::AABBNodeMB4D* node1 = (const typename BVHN::AABBNodeMB4D*) node; const vbool vmask = (node1->lower_t <= time) & (time < node1->upper_t); mask &= movemask(vmask); } return mask; } template __forceinline size_t pointQueryNodeAABB(const typename BVHN::QuantizedBaseNode* node, const TravPointQuery& query, vfloat& dist) { const size_t mvalid = movemask(node->validMask()); const vfloat start_x(node->start.x); const vfloat scale_x(node->scale.x); const vfloat minX = madd(node->template dequantize((0*sizeof(vfloat)) >> 2),scale_x,start_x); const vfloat maxX = madd(node->template dequantize((1*sizeof(vfloat)) >> 2),scale_x,start_x); const vfloat start_y(node->start.y); const vfloat scale_y(node->scale.y); const vfloat minY = madd(node->template dequantize((2*sizeof(vfloat)) >> 2),scale_y,start_y); const vfloat maxY = madd(node->template dequantize((3*sizeof(vfloat)) >> 2),scale_y,start_y); const vfloat start_z(node->start.z); const vfloat scale_z(node->scale.z); const vfloat minZ = madd(node->template dequantize((4*sizeof(vfloat)) >> 2),scale_z,start_z); const vfloat maxZ = madd(node->template dequantize((5*sizeof(vfloat)) >> 2),scale_z,start_z); return pointQueryAABBDistAndMask(query, dist, minX, maxX, minY, maxY, minZ, maxZ) & mvalid; } template __forceinline size_t pointQueryNodeAABB(const typename BVHN::QuantizedBaseNodeMB* node, const TravPointQuery& query, const float time, vfloat& dist) { const size_t mvalid = movemask(node->validMask()); const vfloat minX = node->dequantizeLowerX(time); const vfloat maxX = node->dequantizeUpperX(time); const vfloat minY = node->dequantizeLowerY(time); const vfloat maxY = node->dequantizeUpperY(time); const vfloat minZ = node->dequantizeLowerZ(time); const vfloat maxZ = node->dequantizeUpperZ(time); return pointQueryAABBDistAndMask(query, dist, minX, maxX, minY, maxY, minZ, maxZ) & mvalid; } template __forceinline size_t pointQueryNodeAABB(const typename BVHN::OBBNode* node, const TravPointQuery& query, vfloat& dist) { // TODO: point query - implement const vbool vmask = vbool(true); const size_t mask = movemask(vmask) & ((1<(0.0f); return mask; } template __forceinline size_t pointQueryNodeAABB(const typename BVHN::OBBNodeMB* node, const TravPointQuery& query, const float time, vfloat& dist) { // TODO: point query - implement const vbool vmask = vbool(true); const size_t mask = movemask(vmask) & ((1<(0.0f); return mask; } ////////////////////////////////////////////////////////////////////////////////////// // Fast AABBNode intersection ////////////////////////////////////////////////////////////////////////////////////// template __forceinline size_t intersectNode(const typename BVHN::AABBNode* node, const TravRay& ray, vfloat& dist); template<> __forceinline size_t intersectNode<4>(const typename BVH4::AABBNode* node, const TravRay<4,false>& ray, vfloat4& dist) { #if defined(__AVX2__) || defined(__ARM_NEON) const vfloat4 tNearX = msub(vfloat4::load((float*)((const char*)&node->lower_x+ray.nearX)), ray.rdir.x, ray.org_rdir.x); const vfloat4 tNearY = msub(vfloat4::load((float*)((const char*)&node->lower_x+ray.nearY)), ray.rdir.y, ray.org_rdir.y); const vfloat4 tNearZ = msub(vfloat4::load((float*)((const char*)&node->lower_x+ray.nearZ)), ray.rdir.z, ray.org_rdir.z); const vfloat4 tFarX = msub(vfloat4::load((float*)((const char*)&node->lower_x+ray.farX )), ray.rdir.x, ray.org_rdir.x); const vfloat4 tFarY = msub(vfloat4::load((float*)((const char*)&node->lower_x+ray.farY )), ray.rdir.y, ray.org_rdir.y); const vfloat4 tFarZ = msub(vfloat4::load((float*)((const char*)&node->lower_x+ray.farZ )), ray.rdir.z, ray.org_rdir.z); #else const vfloat4 tNearX = (vfloat4::load((float*)((const char*)&node->lower_x+ray.nearX)) - ray.org.x) * ray.rdir.x; const vfloat4 tNearY = (vfloat4::load((float*)((const char*)&node->lower_x+ray.nearY)) - ray.org.y) * ray.rdir.y; const vfloat4 tNearZ = (vfloat4::load((float*)((const char*)&node->lower_x+ray.nearZ)) - ray.org.z) * ray.rdir.z; const vfloat4 tFarX = (vfloat4::load((float*)((const char*)&node->lower_x+ray.farX )) - ray.org.x) * ray.rdir.x; const vfloat4 tFarY = (vfloat4::load((float*)((const char*)&node->lower_x+ray.farY )) - ray.org.y) * ray.rdir.y; const vfloat4 tFarZ = (vfloat4::load((float*)((const char*)&node->lower_x+ray.farZ )) - ray.org.z) * ray.rdir.z; #endif #if defined(__aarch64__) const vfloat4 tNear = maxi(tNearX, tNearY, tNearZ, ray.tnear); const vfloat4 tFar = mini(tFarX, tFarY, tFarZ, ray.tfar); const vbool4 vmask = asInt(tNear) <= asInt(tFar); const size_t mask = movemask(vmask); #elif defined(__SSE4_1__) && !defined(__AVX512F__) // up to HSW const vfloat4 tNear = maxi(tNearX,tNearY,tNearZ,ray.tnear); const vfloat4 tFar = mini(tFarX ,tFarY ,tFarZ ,ray.tfar); const vbool4 vmask = asInt(tNear) > asInt(tFar); const size_t mask = movemask(vmask) ^ ((1<<4)-1); #elif defined(__AVX512F__) // SKX const vfloat4 tNear = maxi(tNearX,tNearY,tNearZ,ray.tnear); const vfloat4 tFar = mini(tFarX ,tFarY ,tFarZ ,ray.tfar); const vbool4 vmask = asInt(tNear) <= asInt(tFar); const size_t mask = movemask(vmask); #else const vfloat4 tNear = max(tNearX,tNearY,tNearZ,ray.tnear); const vfloat4 tFar = min(tFarX ,tFarY ,tFarZ ,ray.tfar); const vbool4 vmask = tNear <= tFar; const size_t mask = movemask(vmask); #endif dist = tNear; return mask; } #if defined(__AVX__) template<> __forceinline size_t intersectNode<8>(const typename BVH8::AABBNode* node, const TravRay<8,false>& ray, vfloat8& dist) { #if defined(__AVX2__) || defined(__ARM_NEON) const vfloat8 tNearX = msub(vfloat8::load((float*)((const char*)&node->lower_x+ray.nearX)), ray.rdir.x, ray.org_rdir.x); const vfloat8 tNearY = msub(vfloat8::load((float*)((const char*)&node->lower_x+ray.nearY)), ray.rdir.y, ray.org_rdir.y); const vfloat8 tNearZ = msub(vfloat8::load((float*)((const char*)&node->lower_x+ray.nearZ)), ray.rdir.z, ray.org_rdir.z); const vfloat8 tFarX = msub(vfloat8::load((float*)((const char*)&node->lower_x+ray.farX )), ray.rdir.x, ray.org_rdir.x); const vfloat8 tFarY = msub(vfloat8::load((float*)((const char*)&node->lower_x+ray.farY )), ray.rdir.y, ray.org_rdir.y); const vfloat8 tFarZ = msub(vfloat8::load((float*)((const char*)&node->lower_x+ray.farZ )), ray.rdir.z, ray.org_rdir.z); #else const vfloat8 tNearX = (vfloat8::load((float*)((const char*)&node->lower_x+ray.nearX)) - ray.org.x) * ray.rdir.x; const vfloat8 tNearY = (vfloat8::load((float*)((const char*)&node->lower_x+ray.nearY)) - ray.org.y) * ray.rdir.y; const vfloat8 tNearZ = (vfloat8::load((float*)((const char*)&node->lower_x+ray.nearZ)) - ray.org.z) * ray.rdir.z; const vfloat8 tFarX = (vfloat8::load((float*)((const char*)&node->lower_x+ray.farX )) - ray.org.x) * ray.rdir.x; const vfloat8 tFarY = (vfloat8::load((float*)((const char*)&node->lower_x+ray.farY )) - ray.org.y) * ray.rdir.y; const vfloat8 tFarZ = (vfloat8::load((float*)((const char*)&node->lower_x+ray.farZ )) - ray.org.z) * ray.rdir.z; #endif #if defined(__AVX2__) && !defined(__AVX512F__) // HSW const vfloat8 tNear = maxi(tNearX,tNearY,tNearZ,ray.tnear); const vfloat8 tFar = mini(tFarX ,tFarY ,tFarZ ,ray.tfar); const vbool8 vmask = asInt(tNear) > asInt(tFar); const size_t mask = movemask(vmask) ^ ((1<<8)-1); #elif defined(__AVX512F__) // SKX const vfloat8 tNear = maxi(tNearX,tNearY,tNearZ,ray.tnear); const vfloat8 tFar = mini(tFarX ,tFarY ,tFarZ ,ray.tfar); const vbool8 vmask = asInt(tNear) <= asInt(tFar); const size_t mask = movemask(vmask); #else const vfloat8 tNear = max(tNearX,tNearY,tNearZ,ray.tnear); const vfloat8 tFar = min(tFarX ,tFarY ,tFarZ ,ray.tfar); const vbool8 vmask = tNear <= tFar; const size_t mask = movemask(vmask); #endif dist = tNear; return mask; } #endif ////////////////////////////////////////////////////////////////////////////////////// // Robust AABBNode intersection ////////////////////////////////////////////////////////////////////////////////////// template __forceinline size_t intersectNodeRobust(const typename BVHN::AABBNode* node, const TravRay& ray, vfloat& dist) { const vfloat tNearX = (vfloat::load((float*)((const char*)&node->lower_x+ray.nearX)) - ray.org.x) * ray.rdir_near.x; const vfloat tNearY = (vfloat::load((float*)((const char*)&node->lower_x+ray.nearY)) - ray.org.y) * ray.rdir_near.y; const vfloat tNearZ = (vfloat::load((float*)((const char*)&node->lower_x+ray.nearZ)) - ray.org.z) * ray.rdir_near.z; const vfloat tFarX = (vfloat::load((float*)((const char*)&node->lower_x+ray.farX )) - ray.org.x) * ray.rdir_far.x; const vfloat tFarY = (vfloat::load((float*)((const char*)&node->lower_x+ray.farY )) - ray.org.y) * ray.rdir_far.y; const vfloat tFarZ = (vfloat::load((float*)((const char*)&node->lower_x+ray.farZ )) - ray.org.z) * ray.rdir_far.z; const vfloat tNear = max(tNearX,tNearY,tNearZ,ray.tnear); const vfloat tFar = min(tFarX ,tFarY ,tFarZ ,ray.tfar); const vbool vmask = tNear <= tFar; const size_t mask = movemask(vmask); dist = tNear; return mask; } ////////////////////////////////////////////////////////////////////////////////////// // Fast AABBNodeMB intersection ////////////////////////////////////////////////////////////////////////////////////// template __forceinline size_t intersectNode(const typename BVHN::AABBNodeMB* node, const TravRay& ray, const float time, vfloat& dist) { const vfloat* pNearX = (const vfloat*)((const char*)&node->lower_x+ray.nearX); const vfloat* pNearY = (const vfloat*)((const char*)&node->lower_x+ray.nearY); const vfloat* pNearZ = (const vfloat*)((const char*)&node->lower_x+ray.nearZ); const vfloat* pFarX = (const vfloat*)((const char*)&node->lower_x+ray.farX); const vfloat* pFarY = (const vfloat*)((const char*)&node->lower_x+ray.farY); const vfloat* pFarZ = (const vfloat*)((const char*)&node->lower_x+ray.farZ); #if defined(__AVX2__) || defined(__ARM_NEON) const vfloat tNearX = msub(madd(time,pNearX[6],vfloat(pNearX[0])), ray.rdir.x, ray.org_rdir.x); const vfloat tNearY = msub(madd(time,pNearY[6],vfloat(pNearY[0])), ray.rdir.y, ray.org_rdir.y); const vfloat tNearZ = msub(madd(time,pNearZ[6],vfloat(pNearZ[0])), ray.rdir.z, ray.org_rdir.z); const vfloat tFarX = msub(madd(time,pFarX [6],vfloat(pFarX [0])), ray.rdir.x, ray.org_rdir.x); const vfloat tFarY = msub(madd(time,pFarY [6],vfloat(pFarY [0])), ray.rdir.y, ray.org_rdir.y); const vfloat tFarZ = msub(madd(time,pFarZ [6],vfloat(pFarZ [0])), ray.rdir.z, ray.org_rdir.z); #else const vfloat tNearX = (madd(time,pNearX[6],vfloat(pNearX[0])) - ray.org.x) * ray.rdir.x; const vfloat tNearY = (madd(time,pNearY[6],vfloat(pNearY[0])) - ray.org.y) * ray.rdir.y; const vfloat tNearZ = (madd(time,pNearZ[6],vfloat(pNearZ[0])) - ray.org.z) * ray.rdir.z; const vfloat tFarX = (madd(time,pFarX [6],vfloat(pFarX [0])) - ray.org.x) * ray.rdir.x; const vfloat tFarY = (madd(time,pFarY [6],vfloat(pFarY [0])) - ray.org.y) * ray.rdir.y; const vfloat tFarZ = (madd(time,pFarZ [6],vfloat(pFarZ [0])) - ray.org.z) * ray.rdir.z; #endif #if defined(__AVX2__) && !defined(__AVX512F__) // HSW const vfloat tNear = maxi(tNearX,tNearY,tNearZ,ray.tnear); const vfloat tFar = mini(tFarX ,tFarY ,tFarZ ,ray.tfar); const vbool vmask = asInt(tNear) > asInt(tFar); const size_t mask = movemask(vmask) ^ ((1< tNear = maxi(tNearX,tNearY,tNearZ,ray.tnear); const vfloat tFar = mini(tFarX ,tFarY ,tFarZ ,ray.tfar); const vbool vmask = asInt(tNear) <= asInt(tFar); const size_t mask = movemask(vmask); #else const vfloat tNear = max(ray.tnear,tNearX,tNearY,tNearZ); const vfloat tFar = min(ray.tfar, tFarX ,tFarY ,tFarZ ); const vbool vmask = tNear <= tFar; const size_t mask = movemask(vmask); #endif dist = tNear; return mask; } ////////////////////////////////////////////////////////////////////////////////////// // Robust AABBNodeMB intersection ////////////////////////////////////////////////////////////////////////////////////// template __forceinline size_t intersectNodeRobust(const typename BVHN::AABBNodeMB* node, const TravRay& ray, const float time, vfloat& dist) { const vfloat* pNearX = (const vfloat*)((const char*)&node->lower_x+ray.nearX); const vfloat* pNearY = (const vfloat*)((const char*)&node->lower_x+ray.nearY); const vfloat* pNearZ = (const vfloat*)((const char*)&node->lower_x+ray.nearZ); const vfloat tNearX = (madd(time,pNearX[6],vfloat(pNearX[0])) - ray.org.x) * ray.rdir_near.x; const vfloat tNearY = (madd(time,pNearY[6],vfloat(pNearY[0])) - ray.org.y) * ray.rdir_near.y; const vfloat tNearZ = (madd(time,pNearZ[6],vfloat(pNearZ[0])) - ray.org.z) * ray.rdir_near.z; const vfloat tNear = max(ray.tnear,tNearX,tNearY,tNearZ); const vfloat* pFarX = (const vfloat*)((const char*)&node->lower_x+ray.farX); const vfloat* pFarY = (const vfloat*)((const char*)&node->lower_x+ray.farY); const vfloat* pFarZ = (const vfloat*)((const char*)&node->lower_x+ray.farZ); const vfloat tFarX = (madd(time,pFarX[6],vfloat(pFarX[0])) - ray.org.x) * ray.rdir_far.x; const vfloat tFarY = (madd(time,pFarY[6],vfloat(pFarY[0])) - ray.org.y) * ray.rdir_far.y; const vfloat tFarZ = (madd(time,pFarZ[6],vfloat(pFarZ[0])) - ray.org.z) * ray.rdir_far.z; const vfloat tFar = min(ray.tfar,tFarX,tFarY,tFarZ); const size_t mask = movemask(tNear <= tFar); dist = tNear; return mask; } ////////////////////////////////////////////////////////////////////////////////////// // Fast AABBNodeMB4D intersection ////////////////////////////////////////////////////////////////////////////////////// template __forceinline size_t intersectNodeMB4D(const typename BVHN::NodeRef ref, const TravRay& ray, const float time, vfloat& dist) { const typename BVHN::AABBNodeMB* node = ref.getAABBNodeMB(); const vfloat* pNearX = (const vfloat*)((const char*)&node->lower_x+ray.nearX); const vfloat* pNearY = (const vfloat*)((const char*)&node->lower_x+ray.nearY); const vfloat* pNearZ = (const vfloat*)((const char*)&node->lower_x+ray.nearZ); const vfloat* pFarX = (const vfloat*)((const char*)&node->lower_x+ray.farX); const vfloat* pFarY = (const vfloat*)((const char*)&node->lower_x+ray.farY); const vfloat* pFarZ = (const vfloat*)((const char*)&node->lower_x+ray.farZ); #if defined (__AVX2__) || defined(__ARM_NEON) const vfloat tNearX = msub(madd(time,pNearX[6],vfloat(pNearX[0])), ray.rdir.x, ray.org_rdir.x); const vfloat tNearY = msub(madd(time,pNearY[6],vfloat(pNearY[0])), ray.rdir.y, ray.org_rdir.y); const vfloat tNearZ = msub(madd(time,pNearZ[6],vfloat(pNearZ[0])), ray.rdir.z, ray.org_rdir.z); const vfloat tFarX = msub(madd(time,pFarX [6],vfloat(pFarX [0])), ray.rdir.x, ray.org_rdir.x); const vfloat tFarY = msub(madd(time,pFarY [6],vfloat(pFarY [0])), ray.rdir.y, ray.org_rdir.y); const vfloat tFarZ = msub(madd(time,pFarZ [6],vfloat(pFarZ [0])), ray.rdir.z, ray.org_rdir.z); #else const vfloat tNearX = (madd(time,pNearX[6],vfloat(pNearX[0])) - ray.org.x) * ray.rdir.x; const vfloat tNearY = (madd(time,pNearY[6],vfloat(pNearY[0])) - ray.org.y) * ray.rdir.y; const vfloat tNearZ = (madd(time,pNearZ[6],vfloat(pNearZ[0])) - ray.org.z) * ray.rdir.z; const vfloat tFarX = (madd(time,pFarX [6],vfloat(pFarX [0])) - ray.org.x) * ray.rdir.x; const vfloat tFarY = (madd(time,pFarY [6],vfloat(pFarY [0])) - ray.org.y) * ray.rdir.y; const vfloat tFarZ = (madd(time,pFarZ [6],vfloat(pFarZ [0])) - ray.org.z) * ray.rdir.z; #endif #if defined(__AVX2__) && !defined(__AVX512F__) const vfloat tNear = maxi(maxi(tNearX,tNearY),maxi(tNearZ,ray.tnear)); const vfloat tFar = mini(mini(tFarX ,tFarY ),mini(tFarZ ,ray.tfar )); #else const vfloat tNear = max(ray.tnear,tNearX,tNearY,tNearZ); const vfloat tFar = min(ray.tfar, tFarX ,tFarY ,tFarZ ); #endif vbool vmask = tNear <= tFar; if (unlikely(ref.isAABBNodeMB4D())) { const typename BVHN::AABBNodeMB4D* node1 = (const typename BVHN::AABBNodeMB4D*) node; vmask &= (node1->lower_t <= time) & (time < node1->upper_t); } const size_t mask = movemask(vmask); dist = tNear; return mask; } ////////////////////////////////////////////////////////////////////////////////////// // Robust AABBNodeMB4D intersection ////////////////////////////////////////////////////////////////////////////////////// template __forceinline size_t intersectNodeMB4DRobust(const typename BVHN::NodeRef ref, const TravRay& ray, const float time, vfloat& dist) { const typename BVHN::AABBNodeMB* node = ref.getAABBNodeMB(); const vfloat* pNearX = (const vfloat*)((const char*)&node->lower_x+ray.nearX); const vfloat* pNearY = (const vfloat*)((const char*)&node->lower_x+ray.nearY); const vfloat* pNearZ = (const vfloat*)((const char*)&node->lower_x+ray.nearZ); const vfloat tNearX = (madd(time,pNearX[6],vfloat(pNearX[0])) - ray.org.x) * ray.rdir_near.x; const vfloat tNearY = (madd(time,pNearY[6],vfloat(pNearY[0])) - ray.org.y) * ray.rdir_near.y; const vfloat tNearZ = (madd(time,pNearZ[6],vfloat(pNearZ[0])) - ray.org.z) * ray.rdir_near.z; const vfloat tNear = max(ray.tnear,tNearX,tNearY,tNearZ); const vfloat* pFarX = (const vfloat*)((const char*)&node->lower_x+ray.farX); const vfloat* pFarY = (const vfloat*)((const char*)&node->lower_x+ray.farY); const vfloat* pFarZ = (const vfloat*)((const char*)&node->lower_x+ray.farZ); const vfloat tFarX = (madd(time,pFarX[6],vfloat(pFarX[0])) - ray.org.x) * ray.rdir_far.x; const vfloat tFarY = (madd(time,pFarY[6],vfloat(pFarY[0])) - ray.org.y) * ray.rdir_far.y; const vfloat tFarZ = (madd(time,pFarZ[6],vfloat(pFarZ[0])) - ray.org.z) * ray.rdir_far.z; const vfloat tFar = min(ray.tfar,tFarX,tFarY,tFarZ); vbool vmask = tNear <= tFar; if (unlikely(ref.isAABBNodeMB4D())) { const typename BVHN::AABBNodeMB4D* node1 = (const typename BVHN::AABBNodeMB4D*) node; vmask &= (node1->lower_t <= time) & (time < node1->upper_t); } const size_t mask = movemask(vmask); dist = tNear; return mask; } ////////////////////////////////////////////////////////////////////////////////////// // Fast QuantizedBaseNode intersection ////////////////////////////////////////////////////////////////////////////////////// template __forceinline size_t intersectNode(const typename BVHN::QuantizedBaseNode* node, const TravRay& ray, vfloat& dist); template<> __forceinline size_t intersectNode<4>(const typename BVH4::QuantizedBaseNode* node, const TravRay<4,false>& ray, vfloat4& dist) { const size_t mvalid = movemask(node->validMask()); const vfloat4 start_x(node->start.x); const vfloat4 scale_x(node->scale.x); const vfloat4 lower_x = madd(node->dequantize<4>(ray.nearX >> 2),scale_x,start_x); const vfloat4 upper_x = madd(node->dequantize<4>(ray.farX >> 2),scale_x,start_x); const vfloat4 start_y(node->start.y); const vfloat4 scale_y(node->scale.y); const vfloat4 lower_y = madd(node->dequantize<4>(ray.nearY >> 2),scale_y,start_y); const vfloat4 upper_y = madd(node->dequantize<4>(ray.farY >> 2),scale_y,start_y); const vfloat4 start_z(node->start.z); const vfloat4 scale_z(node->scale.z); const vfloat4 lower_z = madd(node->dequantize<4>(ray.nearZ >> 2),scale_z,start_z); const vfloat4 upper_z = madd(node->dequantize<4>(ray.farZ >> 2),scale_z,start_z); #if defined(__AVX2__) || defined(__ARM_NEON) const vfloat4 tNearX = msub(lower_x, ray.rdir.x, ray.org_rdir.x); const vfloat4 tNearY = msub(lower_y, ray.rdir.y, ray.org_rdir.y); const vfloat4 tNearZ = msub(lower_z, ray.rdir.z, ray.org_rdir.z); const vfloat4 tFarX = msub(upper_x, ray.rdir.x, ray.org_rdir.x); const vfloat4 tFarY = msub(upper_y, ray.rdir.y, ray.org_rdir.y); const vfloat4 tFarZ = msub(upper_z, ray.rdir.z, ray.org_rdir.z); #else const vfloat4 tNearX = (lower_x - ray.org.x) * ray.rdir.x; const vfloat4 tNearY = (lower_y - ray.org.y) * ray.rdir.y; const vfloat4 tNearZ = (lower_z - ray.org.z) * ray.rdir.z; const vfloat4 tFarX = (upper_x - ray.org.x) * ray.rdir.x; const vfloat4 tFarY = (upper_y - ray.org.y) * ray.rdir.y; const vfloat4 tFarZ = (upper_z - ray.org.z) * ray.rdir.z; #endif #if defined(__SSE4_1__) && !defined(__AVX512F__) // up to HSW const vfloat4 tNear = maxi(tNearX,tNearY,tNearZ,ray.tnear); const vfloat4 tFar = mini(tFarX ,tFarY ,tFarZ ,ray.tfar); const vbool4 vmask = asInt(tNear) > asInt(tFar); const size_t mask = movemask(vmask) ^ ((1<<4)-1); #elif defined(__AVX512F__) // SKX const vfloat4 tNear = maxi(tNearX,tNearY,tNearZ,ray.tnear); const vfloat4 tFar = mini(tFarX ,tFarY ,tFarZ ,ray.tfar); const vbool4 vmask = asInt(tNear) <= asInt(tFar); const size_t mask = movemask(vmask); #else const vfloat4 tNear = max(tNearX,tNearY,tNearZ,ray.tnear); const vfloat4 tFar = min(tFarX ,tFarY ,tFarZ ,ray.tfar); const vbool4 vmask = tNear <= tFar; const size_t mask = movemask(vmask); #endif dist = tNear; return mask & mvalid; } template<> __forceinline size_t intersectNode<4>(const typename BVH4::QuantizedBaseNode* node, const TravRay<4,true>& ray, vfloat4& dist) { const size_t mvalid = movemask(node->validMask()); const vfloat4 start_x(node->start.x); const vfloat4 scale_x(node->scale.x); const vfloat4 lower_x = madd(node->dequantize<4>(ray.nearX >> 2),scale_x,start_x); const vfloat4 upper_x = madd(node->dequantize<4>(ray.farX >> 2),scale_x,start_x); const vfloat4 start_y(node->start.y); const vfloat4 scale_y(node->scale.y); const vfloat4 lower_y = madd(node->dequantize<4>(ray.nearY >> 2),scale_y,start_y); const vfloat4 upper_y = madd(node->dequantize<4>(ray.farY >> 2),scale_y,start_y); const vfloat4 start_z(node->start.z); const vfloat4 scale_z(node->scale.z); const vfloat4 lower_z = madd(node->dequantize<4>(ray.nearZ >> 2),scale_z,start_z); const vfloat4 upper_z = madd(node->dequantize<4>(ray.farZ >> 2),scale_z,start_z); const vfloat4 tNearX = (lower_x - ray.org.x) * ray.rdir_near.x; const vfloat4 tNearY = (lower_y - ray.org.y) * ray.rdir_near.y; const vfloat4 tNearZ = (lower_z - ray.org.z) * ray.rdir_near.z; const vfloat4 tFarX = (upper_x - ray.org.x) * ray.rdir_far.x; const vfloat4 tFarY = (upper_y - ray.org.y) * ray.rdir_far.y; const vfloat4 tFarZ = (upper_z - ray.org.z) * ray.rdir_far.z; const vfloat4 tNear = max(tNearX,tNearY,tNearZ,ray.tnear); const vfloat4 tFar = min(tFarX ,tFarY ,tFarZ ,ray.tfar); const vbool4 vmask = tNear <= tFar; const size_t mask = movemask(vmask); dist = tNear; return mask & mvalid; } #if defined(__AVX__) template<> __forceinline size_t intersectNode<8>(const typename BVH8::QuantizedBaseNode* node, const TravRay<8,false>& ray, vfloat8& dist) { const size_t mvalid = movemask(node->validMask()); const vfloat8 start_x(node->start.x); const vfloat8 scale_x(node->scale.x); const vfloat8 lower_x = madd(node->dequantize<8>(ray.nearX >> 2),scale_x,start_x); const vfloat8 upper_x = madd(node->dequantize<8>(ray.farX >> 2),scale_x,start_x); const vfloat8 start_y(node->start.y); const vfloat8 scale_y(node->scale.y); const vfloat8 lower_y = madd(node->dequantize<8>(ray.nearY >> 2),scale_y,start_y); const vfloat8 upper_y = madd(node->dequantize<8>(ray.farY >> 2),scale_y,start_y); const vfloat8 start_z(node->start.z); const vfloat8 scale_z(node->scale.z); const vfloat8 lower_z = madd(node->dequantize<8>(ray.nearZ >> 2),scale_z,start_z); const vfloat8 upper_z = madd(node->dequantize<8>(ray.farZ >> 2),scale_z,start_z); #if defined(__AVX2__) || defined(__ARM_NEON) const vfloat8 tNearX = msub(lower_x, ray.rdir.x, ray.org_rdir.x); const vfloat8 tNearY = msub(lower_y, ray.rdir.y, ray.org_rdir.y); const vfloat8 tNearZ = msub(lower_z, ray.rdir.z, ray.org_rdir.z); const vfloat8 tFarX = msub(upper_x, ray.rdir.x, ray.org_rdir.x); const vfloat8 tFarY = msub(upper_y, ray.rdir.y, ray.org_rdir.y); const vfloat8 tFarZ = msub(upper_z, ray.rdir.z, ray.org_rdir.z); #else const vfloat8 tNearX = (lower_x - ray.org.x) * ray.rdir.x; const vfloat8 tNearY = (lower_y - ray.org.y) * ray.rdir.y; const vfloat8 tNearZ = (lower_z - ray.org.z) * ray.rdir.z; const vfloat8 tFarX = (upper_x - ray.org.x) * ray.rdir.x; const vfloat8 tFarY = (upper_y - ray.org.y) * ray.rdir.y; const vfloat8 tFarZ = (upper_z - ray.org.z) * ray.rdir.z; #endif #if defined(__AVX2__) && !defined(__AVX512F__) // HSW const vfloat8 tNear = maxi(tNearX,tNearY,tNearZ,ray.tnear); const vfloat8 tFar = mini(tFarX ,tFarY ,tFarZ ,ray.tfar); const vbool8 vmask = asInt(tNear) > asInt(tFar); const size_t mask = movemask(vmask) ^ ((1<<8)-1); #elif defined(__AVX512F__) // SKX const vfloat8 tNear = maxi(tNearX,tNearY,tNearZ,ray.tnear); const vfloat8 tFar = mini(tFarX ,tFarY ,tFarZ ,ray.tfar); const vbool8 vmask = asInt(tNear) <= asInt(tFar); const size_t mask = movemask(vmask); #else const vfloat8 tNear = max(tNearX,tNearY,tNearZ,ray.tnear); const vfloat8 tFar = min(tFarX ,tFarY ,tFarZ ,ray.tfar); const vbool8 vmask = tNear <= tFar; const size_t mask = movemask(vmask); #endif dist = tNear; return mask & mvalid; } template<> __forceinline size_t intersectNode<8>(const typename BVH8::QuantizedBaseNode* node, const TravRay<8,true>& ray, vfloat8& dist) { const size_t mvalid = movemask(node->validMask()); const vfloat8 start_x(node->start.x); const vfloat8 scale_x(node->scale.x); const vfloat8 lower_x = madd(node->dequantize<8>(ray.nearX >> 2),scale_x,start_x); const vfloat8 upper_x = madd(node->dequantize<8>(ray.farX >> 2),scale_x,start_x); const vfloat8 start_y(node->start.y); const vfloat8 scale_y(node->scale.y); const vfloat8 lower_y = madd(node->dequantize<8>(ray.nearY >> 2),scale_y,start_y); const vfloat8 upper_y = madd(node->dequantize<8>(ray.farY >> 2),scale_y,start_y); const vfloat8 start_z(node->start.z); const vfloat8 scale_z(node->scale.z); const vfloat8 lower_z = madd(node->dequantize<8>(ray.nearZ >> 2),scale_z,start_z); const vfloat8 upper_z = madd(node->dequantize<8>(ray.farZ >> 2),scale_z,start_z); const vfloat8 tNearX = (lower_x - ray.org.x) * ray.rdir_near.x; const vfloat8 tNearY = (lower_y - ray.org.y) * ray.rdir_near.y; const vfloat8 tNearZ = (lower_z - ray.org.z) * ray.rdir_near.z; const vfloat8 tFarX = (upper_x - ray.org.x) * ray.rdir_far.x; const vfloat8 tFarY = (upper_y - ray.org.y) * ray.rdir_far.y; const vfloat8 tFarZ = (upper_z - ray.org.z) * ray.rdir_far.z; const vfloat8 tNear = max(tNearX,tNearY,tNearZ,ray.tnear); const vfloat8 tFar = min(tFarX ,tFarY ,tFarZ ,ray.tfar); const vbool8 vmask = tNear <= tFar; const size_t mask = movemask(vmask); dist = tNear; return mask & mvalid; } #endif template __forceinline size_t intersectNode(const typename BVHN::QuantizedBaseNodeMB* node, const TravRay& ray, const float time, vfloat& dist) { const vboolf mvalid = node->validMask(); const vfloat lower_x = node->dequantizeLowerX(time); const vfloat upper_x = node->dequantizeUpperX(time); const vfloat lower_y = node->dequantizeLowerY(time); const vfloat upper_y = node->dequantizeUpperY(time); const vfloat lower_z = node->dequantizeLowerZ(time); const vfloat upper_z = node->dequantizeUpperZ(time); #if defined(__AVX2__) || defined(__ARM_NEON) const vfloat tNearX = msub(lower_x, ray.rdir.x, ray.org_rdir.x); const vfloat tNearY = msub(lower_y, ray.rdir.y, ray.org_rdir.y); const vfloat tNearZ = msub(lower_z, ray.rdir.z, ray.org_rdir.z); const vfloat tFarX = msub(upper_x, ray.rdir.x, ray.org_rdir.x); const vfloat tFarY = msub(upper_y, ray.rdir.y, ray.org_rdir.y); const vfloat tFarZ = msub(upper_z, ray.rdir.z, ray.org_rdir.z); #else const vfloat tNearX = (lower_x - ray.org.x) * ray.rdir.x; const vfloat tNearY = (lower_y - ray.org.y) * ray.rdir.y; const vfloat tNearZ = (lower_z - ray.org.z) * ray.rdir.z; const vfloat tFarX = (upper_x - ray.org.x) * ray.rdir.x; const vfloat tFarY = (upper_y - ray.org.y) * ray.rdir.y; const vfloat tFarZ = (upper_z - ray.org.z) * ray.rdir.z; #endif const vfloat tminX = mini(tNearX,tFarX); const vfloat tmaxX = maxi(tNearX,tFarX); const vfloat tminY = mini(tNearY,tFarY); const vfloat tmaxY = maxi(tNearY,tFarY); const vfloat tminZ = mini(tNearZ,tFarZ); const vfloat tmaxZ = maxi(tNearZ,tFarZ); const vfloat tNear = maxi(tminX,tminY,tminZ,ray.tnear); const vfloat tFar = mini(tmaxX,tmaxY,tmaxZ,ray.tfar); #if defined(__AVX512F__) // SKX const vbool vmask = le(mvalid,asInt(tNear),asInt(tFar)); #else const vbool vmask = (asInt(tNear) <= asInt(tFar)) & mvalid; #endif const size_t mask = movemask(vmask); dist = tNear; return mask; } template __forceinline size_t intersectNode(const typename BVHN::QuantizedBaseNodeMB* node, const TravRay& ray, const float time, vfloat& dist) { const vboolf mvalid = node->validMask(); const vfloat lower_x = node->dequantizeLowerX(time); const vfloat upper_x = node->dequantizeUpperX(time); const vfloat lower_y = node->dequantizeLowerY(time); const vfloat upper_y = node->dequantizeUpperY(time); const vfloat lower_z = node->dequantizeLowerZ(time); const vfloat upper_z = node->dequantizeUpperZ(time); const vfloat tNearX = (lower_x - ray.org.x) * ray.rdir_near.x; const vfloat tNearY = (lower_y - ray.org.y) * ray.rdir_near.y; const vfloat tNearZ = (lower_z - ray.org.z) * ray.rdir_near.z; const vfloat tFarX = (upper_x - ray.org.x) * ray.rdir_far.x; const vfloat tFarY = (upper_y - ray.org.y) * ray.rdir_far.y; const vfloat tFarZ = (upper_z - ray.org.z) * ray.rdir_far.z; const vfloat tminX = mini(tNearX,tFarX); const vfloat tmaxX = maxi(tNearX,tFarX); const vfloat tminY = mini(tNearY,tFarY); const vfloat tmaxY = maxi(tNearY,tFarY); const vfloat tminZ = mini(tNearZ,tFarZ); const vfloat tmaxZ = maxi(tNearZ,tFarZ); const vfloat tNear = maxi(tminX,tminY,tminZ,ray.tnear); const vfloat tFar = mini(tmaxX,tmaxY,tmaxZ,ray.tfar); #if defined(__AVX512F__) // SKX const vbool vmask = le(mvalid,asInt(tNear),asInt(tFar)); #else const vbool vmask = (asInt(tNear) <= asInt(tFar)) & mvalid; #endif const size_t mask = movemask(vmask); dist = tNear; return mask; } ////////////////////////////////////////////////////////////////////////////////////// // Fast OBBNode intersection ////////////////////////////////////////////////////////////////////////////////////// template __forceinline size_t intersectNode(const typename BVHN::OBBNode* node, const TravRay& ray, vfloat& dist) { const Vec3vf dir = xfmVector(node->naabb,ray.dir); //const Vec3vf nrdir = Vec3vf(vfloat(-1.0f))/dir; const Vec3vf nrdir = Vec3vf(vfloat(-1.0f))*rcp_safe(dir); const Vec3vf org = xfmPoint(node->naabb,ray.org); const Vec3vf tLowerXYZ = org * nrdir; // (Vec3fa(zero) - org) * rdir; const Vec3vf tUpperXYZ = tLowerXYZ - nrdir; // (Vec3fa(one ) - org) * rdir; const vfloat tNearX = mini(tLowerXYZ.x,tUpperXYZ.x); const vfloat tNearY = mini(tLowerXYZ.y,tUpperXYZ.y); const vfloat tNearZ = mini(tLowerXYZ.z,tUpperXYZ.z); const vfloat tFarX = maxi(tLowerXYZ.x,tUpperXYZ.x); const vfloat tFarY = maxi(tLowerXYZ.y,tUpperXYZ.y); const vfloat tFarZ = maxi(tLowerXYZ.z,tUpperXYZ.z); vfloat tNear = max(ray.tnear, tNearX,tNearY,tNearZ); vfloat tFar = min(ray.tfar, tFarX ,tFarY ,tFarZ ); if (robust) { tNear = tNear*vfloat(1.0f-3.0f*float(ulp)); tFar = tFar *vfloat(1.0f+3.0f*float(ulp)); } const vbool vmask = tNear <= tFar; dist = tNear; return movemask(vmask); } ////////////////////////////////////////////////////////////////////////////////////// // Fast OBBNodeMB intersection ////////////////////////////////////////////////////////////////////////////////////// template __forceinline size_t intersectNode(const typename BVHN::OBBNodeMB* node, const TravRay& ray, const float time, vfloat& dist) { const AffineSpace3vf xfm = node->space0; const Vec3vf b0_lower = zero; const Vec3vf b0_upper = one; const Vec3vf lower = lerp(b0_lower,node->b1.lower,vfloat(time)); const Vec3vf upper = lerp(b0_upper,node->b1.upper,vfloat(time)); const BBox3vf bounds(lower,upper); const Vec3vf dir = xfmVector(xfm,ray.dir); const Vec3vf rdir = rcp_safe(dir); const Vec3vf org = xfmPoint(xfm,ray.org); const Vec3vf tLowerXYZ = (bounds.lower - org) * rdir; const Vec3vf tUpperXYZ = (bounds.upper - org) * rdir; const vfloat tNearX = mini(tLowerXYZ.x,tUpperXYZ.x); const vfloat tNearY = mini(tLowerXYZ.y,tUpperXYZ.y); const vfloat tNearZ = mini(tLowerXYZ.z,tUpperXYZ.z); const vfloat tFarX = maxi(tLowerXYZ.x,tUpperXYZ.x); const vfloat tFarY = maxi(tLowerXYZ.y,tUpperXYZ.y); const vfloat tFarZ = maxi(tLowerXYZ.z,tUpperXYZ.z); vfloat tNear = max(ray.tnear, tNearX,tNearY,tNearZ); vfloat tFar = min(ray.tfar, tFarX ,tFarY ,tFarZ ); if (robust) { tNear = tNear*vfloat(1.0f-3.0f*float(ulp)); tFar = tFar *vfloat(1.0f+3.0f*float(ulp)); } const vbool vmask = tNear <= tFar; dist = tNear; return movemask(vmask); } ////////////////////////////////////////////////////////////////////////////////////// // Node intersectors used in point query raversal ////////////////////////////////////////////////////////////////////////////////////// /*! Computes traversal information for N nodes with 1 point query */ template struct BVHNNodePointQuerySphere1; template struct BVHNNodePointQuerySphere1 { static __forceinline bool pointQuery(const typename BVHN::NodeRef& node, const TravPointQuery& query, float time, vfloat& dist, size_t& mask) { if (unlikely(node.isLeaf())) return false; mask = pointQueryNodeSphere(node.getAABBNode(), query, dist); return true; } }; template struct BVHNNodePointQuerySphere1 { static __forceinline bool pointQuery(const typename BVHN::NodeRef& node, const TravPointQuery& query, float time, vfloat& dist, size_t& mask) { if (unlikely(node.isLeaf())) return false; mask = pointQueryNodeSphere(node.getAABBNodeMB(), query, time, dist); return true; } }; template struct BVHNNodePointQuerySphere1 { static __forceinline bool pointQuery(const typename BVHN::NodeRef& node, const TravPointQuery& query, float time, vfloat& dist, size_t& mask) { if (unlikely(node.isLeaf())) return false; mask = pointQueryNodeSphereMB4D(node, query, time, dist); return true; } }; template struct BVHNNodePointQuerySphere1 { static __forceinline bool pointQuery(const typename BVHN::NodeRef& node, const TravPointQuery& query, float time, vfloat& dist, size_t& mask) { if (likely(node.isAABBNode())) mask = pointQueryNodeSphere(node.getAABBNode(), query, dist); else if (unlikely(node.isOBBNode())) mask = pointQueryNodeSphere(node.ungetAABBNode(), query, dist); else return false; return true; } }; template struct BVHNNodePointQuerySphere1 { static __forceinline bool pointQuery(const typename BVHN::NodeRef& node, const TravPointQuery& query, float time, vfloat& dist, size_t& mask) { if (likely(node.isAABBNodeMB())) mask = pointQueryNodeSphere(node.getAABBNodeMB(), query, time, dist); else if (unlikely(node.isOBBNodeMB())) mask = pointQueryNodeSphere(node.ungetAABBNodeMB(), query, time, dist); else return false; return true; } }; template struct BVHNNodePointQuerySphere1 { static __forceinline bool pointQuery(const typename BVHN::NodeRef& node, const TravPointQuery& query, float time, vfloat& dist, size_t& mask) { if (unlikely(node.isLeaf())) return false; if (unlikely(node.isOBBNodeMB())) mask = pointQueryNodeSphere(node.ungetAABBNodeMB(), query, time, dist); else mask = pointQueryNodeSphereMB4D(node, query, time, dist); return true; } }; template struct BVHNNodePointQuerySphere1 { static __forceinline bool pointQuery(const typename BVHN::NodeRef& node, const TravPointQuery& query, float time, vfloat& dist, size_t& mask) { if (unlikely(node.isLeaf())) return false; mask = pointQueryNodeSphere((const typename BVHN::QuantizedNode*)node.quantizedNode(), query, dist); return true; } }; template struct BVHNQuantizedBaseNodePointQuerySphere1 { static __forceinline size_t pointQuery(const typename BVHN::QuantizedBaseNode* node, const TravPointQuery& query, vfloat& dist) { return pointQueryNodeSphere(node,query,dist); } static __forceinline size_t pointQuery(const typename BVHN::QuantizedBaseNodeMB* node, const TravPointQuery& query, const float time, vfloat& dist) { return pointQueryNodeSphere(node,query,time,dist); } }; /*! Computes traversal information for N nodes with 1 point query */ template struct BVHNNodePointQueryAABB1; template struct BVHNNodePointQueryAABB1 { static __forceinline bool pointQuery(const typename BVHN::NodeRef& node, const TravPointQuery& query, float time, vfloat& dist, size_t& mask) { if (unlikely(node.isLeaf())) return false; mask = pointQueryNodeAABB(node.getAABBNode(), query, dist); return true; } }; template struct BVHNNodePointQueryAABB1 { static __forceinline bool pointQuery(const typename BVHN::NodeRef& node, const TravPointQuery& query, float time, vfloat& dist, size_t& mask) { if (unlikely(node.isLeaf())) return false; mask = pointQueryNodeAABB(node.getAABBNodeMB(), query, time, dist); return true; } }; template struct BVHNNodePointQueryAABB1 { static __forceinline bool pointQuery(const typename BVHN::NodeRef& node, const TravPointQuery& query, float time, vfloat& dist, size_t& mask) { if (unlikely(node.isLeaf())) return false; mask = pointQueryNodeAABBMB4D(node, query, time, dist); return true; } }; template struct BVHNNodePointQueryAABB1 { static __forceinline bool pointQuery(const typename BVHN::NodeRef& node, const TravPointQuery& query, float time, vfloat& dist, size_t& mask) { if (likely(node.isAABBNode())) mask = pointQueryNodeAABB(node.getAABBNode(), query, dist); else if (unlikely(node.isOBBNode())) mask = pointQueryNodeAABB(node.ungetAABBNode(), query, dist); else return false; return true; } }; template struct BVHNNodePointQueryAABB1 { static __forceinline bool pointQuery(const typename BVHN::NodeRef& node, const TravPointQuery& query, float time, vfloat& dist, size_t& mask) { if (likely(node.isAABBNodeMB())) mask = pointQueryNodeAABB(node.getAABBNodeMB(), query, time, dist); else if (unlikely(node.isOBBNodeMB())) mask = pointQueryNodeAABB(node.ungetAABBNodeMB(), query, time, dist); else return false; return true; } }; template struct BVHNNodePointQueryAABB1 { static __forceinline bool pointQuery(const typename BVHN::NodeRef& node, const TravPointQuery& query, float time, vfloat& dist, size_t& mask) { if (unlikely(node.isLeaf())) return false; if (unlikely(node.isOBBNodeMB())) mask = pointQueryNodeAABB(node.ungetAABBNodeMB(), query, time, dist); else mask = pointQueryNodeAABBMB4D(node, query, time, dist); return true; } }; template struct BVHNNodePointQueryAABB1 { static __forceinline bool pointQuery(const typename BVHN::NodeRef& node, const TravPointQuery& query, float time, vfloat& dist, size_t& mask) { if (unlikely(node.isLeaf())) return false; mask = pointQueryNodeAABB((const typename BVHN::QuantizedNode*)node.quantizedNode(), query, dist); return true; } }; template struct BVHNQuantizedBaseNodePointQueryAABB1 { static __forceinline size_t pointQuery(const typename BVHN::QuantizedBaseNode* node, const TravPointQuery& query, vfloat& dist) { return pointQueryNodeAABB(node,query,dist); } static __forceinline size_t pointQuery(const typename BVHN::QuantizedBaseNodeMB* node, const TravPointQuery& query, const float time, vfloat& dist) { return pointQueryNodeAABB(node,query,time,dist); } }; ////////////////////////////////////////////////////////////////////////////////////// // Node intersectors used in ray traversal ////////////////////////////////////////////////////////////////////////////////////// /*! Intersects N nodes with 1 ray */ template struct BVHNNodeIntersector1; template struct BVHNNodeIntersector1 { static __forceinline bool intersect(const typename BVHN::NodeRef& node, const TravRay& ray, float time, vfloat& dist, size_t& mask) { if (unlikely(node.isLeaf())) return false; mask = intersectNode(node.getAABBNode(), ray, dist); return true; } }; template struct BVHNNodeIntersector1 { static __forceinline bool intersect(const typename BVHN::NodeRef& node, const TravRay& ray, float time, vfloat& dist, size_t& mask) { if (unlikely(node.isLeaf())) return false; mask = intersectNodeRobust(node.getAABBNode(), ray, dist); return true; } }; template struct BVHNNodeIntersector1 { static __forceinline bool intersect(const typename BVHN::NodeRef& node, const TravRay& ray, float time, vfloat& dist, size_t& mask) { if (unlikely(node.isLeaf())) return false; mask = intersectNode(node.getAABBNodeMB(), ray, time, dist); return true; } }; template struct BVHNNodeIntersector1 { static __forceinline bool intersect(const typename BVHN::NodeRef& node, const TravRay& ray, float time, vfloat& dist, size_t& mask) { if (unlikely(node.isLeaf())) return false; mask = intersectNodeRobust(node.getAABBNodeMB(), ray, time, dist); return true; } }; template struct BVHNNodeIntersector1 { static __forceinline bool intersect(const typename BVHN::NodeRef& node, const TravRay& ray, float time, vfloat& dist, size_t& mask) { if (unlikely(node.isLeaf())) return false; mask = intersectNodeMB4D(node, ray, time, dist); return true; } }; template struct BVHNNodeIntersector1 { static __forceinline bool intersect(const typename BVHN::NodeRef& node, const TravRay& ray, float time, vfloat& dist, size_t& mask) { if (unlikely(node.isLeaf())) return false; mask = intersectNodeMB4DRobust(node, ray, time, dist); return true; } }; template struct BVHNNodeIntersector1 { static __forceinline bool intersect(const typename BVHN::NodeRef& node, const TravRay& ray, float time, vfloat& dist, size_t& mask) { if (likely(node.isAABBNode())) mask = intersectNode(node.getAABBNode(), ray, dist); else if (unlikely(node.isOBBNode())) mask = intersectNode(node.ungetAABBNode(), ray, dist); else return false; return true; } }; template struct BVHNNodeIntersector1 { static __forceinline bool intersect(const typename BVHN::NodeRef& node, const TravRay& ray, float time, vfloat& dist, size_t& mask) { if (likely(node.isAABBNode())) mask = intersectNodeRobust(node.getAABBNode(), ray, dist); else if (unlikely(node.isOBBNode())) mask = intersectNode(node.ungetAABBNode(), ray, dist); else return false; return true; } }; template struct BVHNNodeIntersector1 { static __forceinline bool intersect(const typename BVHN::NodeRef& node, const TravRay& ray, float time, vfloat& dist, size_t& mask) { if (likely(node.isAABBNodeMB())) mask = intersectNode(node.getAABBNodeMB(), ray, time, dist); else if (unlikely(node.isOBBNodeMB())) mask = intersectNode(node.ungetAABBNodeMB(), ray, time, dist); else return false; return true; } }; template struct BVHNNodeIntersector1 { static __forceinline bool intersect(const typename BVHN::NodeRef& node, const TravRay& ray, float time, vfloat& dist, size_t& mask) { if (likely(node.isAABBNodeMB())) mask = intersectNodeRobust(node.getAABBNodeMB(), ray, time, dist); else if (unlikely(node.isOBBNodeMB())) mask = intersectNode(node.ungetAABBNodeMB(), ray, time, dist); else return false; return true; } }; template struct BVHNNodeIntersector1 { static __forceinline bool intersect(const typename BVHN::NodeRef& node, const TravRay& ray, float time, vfloat& dist, size_t& mask) { if (unlikely(node.isLeaf())) return false; if (unlikely(node.isOBBNodeMB())) mask = intersectNode(node.ungetAABBNodeMB(), ray, time, dist); else mask = intersectNodeMB4D(node, ray, time, dist); return true; } }; template struct BVHNNodeIntersector1 { static __forceinline bool intersect(const typename BVHN::NodeRef& node, const TravRay& ray, float time, vfloat& dist, size_t& mask) { if (unlikely(node.isLeaf())) return false; if (unlikely(node.isOBBNodeMB())) mask = intersectNode(node.ungetAABBNodeMB(), ray, time, dist); else mask = intersectNodeMB4DRobust(node, ray, time, dist); return true; } }; template struct BVHNNodeIntersector1 { static __forceinline bool intersect(const typename BVHN::NodeRef& node, const TravRay& ray, float time, vfloat& dist, size_t& mask) { if (unlikely(node.isLeaf())) return false; mask = intersectNode((const typename BVHN::QuantizedNode*)node.quantizedNode(), ray, dist); return true; } }; template struct BVHNNodeIntersector1 { static __forceinline bool intersect(const typename BVHN::NodeRef& node, const TravRay& ray, float time, vfloat& dist, size_t& mask) { if (unlikely(node.isLeaf())) return false; mask = intersectNodeRobust((const typename BVHN::QuantizedNode*)node.quantizedNode(), ray, dist); return true; } }; /*! Intersects N nodes with K rays */ template struct BVHNQuantizedBaseNodeIntersector1; template struct BVHNQuantizedBaseNodeIntersector1 { static __forceinline size_t intersect(const typename BVHN::QuantizedBaseNode* node, const TravRay& ray, vfloat& dist) { return intersectNode(node,ray,dist); } static __forceinline size_t intersect(const typename BVHN::QuantizedBaseNodeMB* node, const TravRay& ray, const float time, vfloat& dist) { return intersectNode(node,ray,time,dist); } }; template struct BVHNQuantizedBaseNodeIntersector1 { static __forceinline size_t intersect(const typename BVHN::QuantizedBaseNode* node, const TravRay& ray, vfloat& dist) { return intersectNode(node,ray,dist); } static __forceinline size_t intersect(const typename BVHN::QuantizedBaseNodeMB* node, const TravRay& ray, const float time, vfloat& dist) { return intersectNode(node,ray,time,dist); } }; } }