Path: blob/master/thirdparty/embree/kernels/common/hit.h
9905 views
// Copyright 2009-2021 Intel Corporation1// SPDX-License-Identifier: Apache-2.023#pragma once45#include "default.h"6#include "ray.h"7#include "instance_stack.h"89namespace embree10{11/* Hit structure for K hits */12template<int K>13struct HitK14{15/* Default construction does nothing */16__forceinline HitK() {}1718/* Constructs a hit */19__forceinline HitK(const RTCRayQueryContext* context, const vuint<K>& geomID, const vuint<K>& primID, const vfloat<K>& u, const vfloat<K>& v, const Vec3vf<K>& Ng)20: Ng(Ng), u(u), v(v), primID(primID), geomID(geomID)21{22for (unsigned l = 0; l < RTC_MAX_INSTANCE_LEVEL_COUNT; ++l) {23instID[l] = RTC_INVALID_GEOMETRY_ID;24#if defined(RTC_GEOMETRY_INSTANCE_ARRAY)25instPrimID[l] = RTC_INVALID_GEOMETRY_ID;26#endif27}2829instance_id_stack::copy_UV<K>(context->instID, instID);30#if defined(RTC_GEOMETRY_INSTANCE_ARRAY)31instance_id_stack::copy_UV<K>(context->instPrimID, instPrimID);32#endif33}3435/* Constructs a hit */36__forceinline HitK(const RTCRayQueryContext* context, const vuint<K>& geomID, const vuint<K>& primID, const Vec2vf<K>& uv, const Vec3vf<K>& Ng)37: HitK(context,geomID,primID,uv.x,uv.y,Ng) {}3839/* Returns the size of the hit */40static __forceinline size_t size() { return K; }4142public:43Vec3vf<K> Ng; // geometry normal44vfloat<K> u; // barycentric u coordinate of hit45vfloat<K> v; // barycentric v coordinate of hit46vuint<K> primID; // primitive ID47vuint<K> geomID; // geometry ID48vuint<K> instID[RTC_MAX_INSTANCE_LEVEL_COUNT]; // instance ID49#if defined(RTC_GEOMETRY_INSTANCE_ARRAY)50vuint<K> instPrimID[RTC_MAX_INSTANCE_LEVEL_COUNT]; // instance primitive ID51#endif52};5354/* Specialization for a single hit */55template<>56struct __aligned(16) HitK<1>57{58/* Default construction does nothing */59__forceinline HitK() {}6061/* Constructs a hit */62__forceinline HitK(const RTCRayQueryContext* context, unsigned int geomID, unsigned int primID, float u, float v, const Vec3fa& Ng)63: Ng(Ng.x,Ng.y,Ng.z), u(u), v(v), primID(primID), geomID(geomID)64{65instance_id_stack::copy_UU(context, context->instID, instID);66#if defined(RTC_GEOMETRY_INSTANCE_ARRAY)67instance_id_stack::copy_UU(context, context->instPrimID, instPrimID);68#endif69}7071/* Constructs a hit */72__forceinline HitK(const RTCRayQueryContext* context, unsigned int geomID, unsigned int primID, const Vec2f& uv, const Vec3fa& Ng)73: HitK<1>(context,geomID,primID,uv.x,uv.y,Ng) {}7475/* Returns the size of the hit */76static __forceinline size_t size() { return 1; }7778public:79Vec3<float> Ng; // geometry normal80float u; // barycentric u coordinate of hit81float v; // barycentric v coordinate of hit82unsigned int primID; // primitive ID83unsigned int geomID; // geometry ID84unsigned int instID[RTC_MAX_INSTANCE_LEVEL_COUNT]; // instance ID85#if defined(RTC_GEOMETRY_INSTANCE_ARRAY)86unsigned int instPrimID[RTC_MAX_INSTANCE_LEVEL_COUNT]; // instance primitive ID87#endif88};8990/* Shortcuts */91typedef HitK<1> Hit;92typedef HitK<4> Hit4;93typedef HitK<8> Hit8;94typedef HitK<16> Hit16;95typedef HitK<VSIZEX> Hitx;9697/* Outputs hit to stream */98template<int K>99__forceinline embree_ostream operator<<(embree_ostream cout, const HitK<K>& ray)100{101cout << "{ " << embree_endl102<< " Ng = " << ray.Ng << embree_endl103<< " u = " << ray.u << embree_endl104<< " v = " << ray.v << embree_endl105<< " primID = " << ray.primID << embree_endl106<< " geomID = " << ray.geomID << embree_endl107<< " instID =";108for (unsigned l = 0; l < RTC_MAX_INSTANCE_LEVEL_COUNT; ++l)109{110cout << " " << ray.instID[l];111}112#if defined(RTC_GEOMETRY_INSTANCE_ARRAY)113cout << " instPrimID =";114for (unsigned l = 0; l < RTC_MAX_INSTANCE_LEVEL_COUNT; ++l)115{116cout << " " << ray.instPrimID[l];117}118#endif119cout << embree_endl;120return cout << "}";121}122123template<typename Hit>124__forceinline void copyHitToRay(RayHit& ray, const Hit& hit)125{126ray.Ng = hit.Ng;127ray.u = hit.u;128ray.v = hit.v;129ray.primID = hit.primID;130ray.geomID = hit.geomID;131instance_id_stack::copy_UU(hit.instID, ray.instID);132#if defined(RTC_GEOMETRY_INSTANCE_ARRAY)133instance_id_stack::copy_UU(hit.instPrimID, ray.instPrimID);134#endif135}136137template<int K>138__forceinline void copyHitToRay(const vbool<K>& mask, RayHitK<K>& ray, const HitK<K>& hit)139{140vfloat<K>::storeu(mask,&ray.Ng.x, hit.Ng.x);141vfloat<K>::storeu(mask,&ray.Ng.y, hit.Ng.y);142vfloat<K>::storeu(mask,&ray.Ng.z, hit.Ng.z);143vfloat<K>::storeu(mask,&ray.u, hit.u);144vfloat<K>::storeu(mask,&ray.v, hit.v);145vuint<K>::storeu(mask,&ray.primID, hit.primID);146vuint<K>::storeu(mask,&ray.geomID, hit.geomID);147instance_id_stack::copy_VV<K>(hit.instID, ray.instID, mask);148#if defined(RTC_GEOMETRY_INSTANCE_ARRAY)149instance_id_stack::copy_VV<K>(hit.instPrimID, ray.instPrimID, mask);150#endif151}152}153154155