Path: blob/master/thirdparty/embree/kernels/common/scene_instance.h
9905 views
// Copyright 2009-2021 Intel Corporation1// SPDX-License-Identifier: Apache-2.023#pragma once45#include "geometry.h"6#include "accel.h"78namespace embree9{10struct MotionDerivativeCoefficients;1112/*! Instanced acceleration structure */13struct Instance : public Geometry14{15static const Geometry::GTypeMask geom_type = Geometry::MTY_INSTANCE;1617public:18Instance (Device* device, Accel* object = nullptr, unsigned int numTimeSteps = 1);19~Instance();2021private:22Instance (const Instance& other) DELETED; // do not implement23Instance& operator= (const Instance& other) DELETED; // do not implement2425private:26LBBox3fa nonlinearBounds(const BBox1f& time_range_in,27const BBox1f& geom_time_range,28float geom_time_segments) const;2930BBox3fa boundSegment(size_t itime,31BBox3fa const& obbox0, BBox3fa const& obbox1,32BBox3fa const& bbox0, BBox3fa const& bbox1,33float t_min, float t_max) const;3435/* calculates the (correct) interpolated bounds */36__forceinline BBox3fa bounds(size_t itime0, size_t itime1, float f) const37{38if (unlikely(gsubtype == GTY_SUBTYPE_INSTANCE_QUATERNION))39return xfmBounds(slerp(local2world[itime0], local2world[itime1], f),40lerp(getObjectBounds(itime0), getObjectBounds(itime1), f));41return xfmBounds(lerp(local2world[itime0], local2world[itime1], f),42lerp(getObjectBounds(itime0), getObjectBounds(itime1), f));43}4445public:46virtual void setNumTimeSteps (unsigned int numTimeSteps) override;47virtual void setInstancedScene(const Ref<Scene>& scene) override;48virtual void setTransform(const AffineSpace3fa& local2world, unsigned int timeStep) override;49virtual void setQuaternionDecomposition(const AffineSpace3ff& qd, unsigned int timeStep) override;50virtual AffineSpace3fa getTransform(float time) override;51virtual AffineSpace3fa getTransform(size_t, float time) override;52virtual void setMask (unsigned mask) override;53virtual void build() {}54virtual void addElementsToCount (GeometryCounts & counts) const override;55virtual void commit() override;56virtual size_t getGeometryDataDeviceByteSize() const override;57virtual void convertToDeviceRepresentation(size_t offset, char* data_host, char* data_device) const override;5859public:6061/*! calculates the bounds of instance */62__forceinline BBox3fa bounds(size_t i) const {63assert(i == 0);64if (unlikely(gsubtype == GTY_SUBTYPE_INSTANCE_QUATERNION))65return xfmBounds(quaternionDecompositionToAffineSpace(local2world[0]),object->bounds.bounds());66return xfmBounds(local2world[0],object->bounds.bounds());67}6869/*! gets the bounds of the instanced scene */70__forceinline BBox3fa getObjectBounds(size_t itime) const {71return object->getBounds(timeStep(itime));72}7374/*! calculates the bounds of instance */75__forceinline BBox3fa bounds(size_t i, size_t itime) const {76assert(i == 0);77if (unlikely(gsubtype == GTY_SUBTYPE_INSTANCE_QUATERNION))78return xfmBounds(quaternionDecompositionToAffineSpace(local2world[itime]),getObjectBounds(itime));79return xfmBounds(local2world[itime],getObjectBounds(itime));80}8182/*! calculates the linear bounds of the i'th primitive for the specified time range */83__forceinline LBBox3fa linearBounds(size_t i, const BBox1f& dt) const {84assert(i == 0);85LBBox3fa lbbox = nonlinearBounds(dt, time_range, fnumTimeSegments);86return lbbox;87}8889/*! calculates the build bounds of the i'th item, if it's valid */90__forceinline bool buildBounds(size_t i, BBox3fa* bbox = nullptr) const91{92assert(i==0);93const BBox3fa b = bounds(i);94if (bbox) *bbox = b;95return isvalid(b);96}9798/*! calculates the build bounds of the i'th item at the itime'th time segment, if it's valid */99__forceinline bool buildBounds(size_t i, size_t itime, BBox3fa& bbox) const100{101assert(i==0);102const LBBox3fa bounds = linearBounds(i,itime);103bbox = bounds.bounds ();104return isvalid(bounds);105}106107/* gets version info of topology */108unsigned int getTopologyVersion() const {109return numPrimitives;110}111112/* returns true if topology changed */113bool topologyChanged(unsigned int otherVersion) const {114return numPrimitives != otherVersion;115}116117/*! check if the i'th primitive is valid between the specified time range */118__forceinline bool valid(size_t i, const range<size_t>& itime_range) const119{120assert(i == 0);121for (size_t itime = itime_range.begin(); itime <= itime_range.end(); itime++)122if (!isvalid(bounds(i,itime))) return false;123124return true;125}126127__forceinline AffineSpace3fa getLocal2World() const128{129if (unlikely(gsubtype == GTY_SUBTYPE_INSTANCE_QUATERNION))130return quaternionDecompositionToAffineSpace(local2world[0]);131return local2world[0];132}133134__forceinline AffineSpace3fa getLocal2World(float t) const135{136if (numTimeSegments() > 0) {137float ftime; const unsigned int itime = timeSegment(t, ftime);138if (unlikely(gsubtype == GTY_SUBTYPE_INSTANCE_QUATERNION))139return slerp(local2world[itime+0],local2world[itime+1],ftime);140return lerp(local2world[itime+0],local2world[itime+1],ftime);141}142return getLocal2World();143}144145__forceinline AffineSpace3fa getWorld2Local() const {146return world2local0;147}148149__forceinline AffineSpace3fa getWorld2Local(float t) const {150if (numTimeSegments() > 0)151return rcp(getLocal2World(t));152return getWorld2Local();153}154155template<int K>156__forceinline AffineSpace3vf<K> getWorld2Local(const vbool<K>& valid, const vfloat<K>& t) const157{158if (unlikely(gsubtype == GTY_SUBTYPE_INSTANCE_QUATERNION))159return getWorld2LocalSlerp<K>(valid, t);160return getWorld2LocalLerp<K>(valid, t);161}162163__forceinline float projectedPrimitiveArea(const size_t i) const {164return area(bounds(i));165}166167private:168169template<int K>170__forceinline AffineSpace3vf<K> getWorld2LocalSlerp(const vbool<K>& valid, const vfloat<K>& t) const171{172vfloat<K> ftime;173const vint<K> itime_k = timeSegment<K>(t, ftime);174assert(any(valid));175const size_t index = bsf(movemask(valid));176const int itime = itime_k[index];177if (likely(all(valid, itime_k == vint<K>(itime)))) {178return rcp(slerp(AffineSpace3vff<K>(local2world[itime+0]),179AffineSpace3vff<K>(local2world[itime+1]),180ftime));181}182else {183AffineSpace3vff<K> space0,space1;184vbool<K> valid1 = valid;185while (any(valid1)) {186vbool<K> valid2;187const int itime = next_unique(valid1, itime_k, valid2);188space0 = select(valid2, AffineSpace3vff<K>(local2world[itime+0]), space0);189space1 = select(valid2, AffineSpace3vff<K>(local2world[itime+1]), space1);190}191return rcp(slerp(space0, space1, ftime));192}193}194195template<int K>196__forceinline AffineSpace3vf<K> getWorld2LocalLerp(const vbool<K>& valid, const vfloat<K>& t) const197{198vfloat<K> ftime;199const vint<K> itime_k = timeSegment<K>(t, ftime);200assert(any(valid));201const size_t index = bsf(movemask(valid));202const int itime = itime_k[index];203if (likely(all(valid, itime_k == vint<K>(itime)))) {204return rcp(lerp(AffineSpace3vf<K>((AffineSpace3fa)local2world[itime+0]),205AffineSpace3vf<K>((AffineSpace3fa)local2world[itime+1]),206ftime));207} else {208AffineSpace3vf<K> space0,space1;209vbool<K> valid1 = valid;210while (any(valid1)) {211vbool<K> valid2;212const int itime = next_unique(valid1, itime_k, valid2);213space0 = select(valid2, AffineSpace3vf<K>((AffineSpace3fa)local2world[itime+0]), space0);214space1 = select(valid2, AffineSpace3vf<K>((AffineSpace3fa)local2world[itime+1]), space1);215}216return rcp(lerp(space0, space1, ftime));217}218}219220public:221Accel* object; //!< pointer to instanced acceleration structure222AffineSpace3ff* local2world; //!< transformation from local space to world space for each timestep (either normal matrix or quaternion decomposition)223AffineSpace3fa world2local0; //!< transformation from world space to local space for timestep 0224};225226namespace isa227{228struct InstanceISA : public Instance229{230InstanceISA (Device* device)231: Instance(device) {}232233LBBox3fa vlinearBounds(size_t primID, const BBox1f& time_range) const {234return linearBounds(primID,time_range);235}236237PrimInfo createPrimRefArray(PrimRef* prims, const range<size_t>& r, size_t k, unsigned int geomID) const238{239assert(r.begin() == 0);240assert(r.end() == 1);241242PrimInfo pinfo(empty);243BBox3fa b = empty;244if (!buildBounds(0,&b)) return pinfo;245// const BBox3fa b = bounds(0);246// if (!isvalid(b)) return pinfo;247248const PrimRef prim(b,geomID,unsigned(0));249pinfo.add_center2(prim);250prims[k++] = prim;251return pinfo;252}253254PrimInfo createPrimRefArrayMB(mvector<PrimRef>& prims, size_t itime, const range<size_t>& r, size_t k, unsigned int geomID) const255{256assert(r.begin() == 0);257assert(r.end() == 1);258259PrimInfo pinfo(empty);260BBox3fa b = empty;261if (!buildBounds(0,&b)) return pinfo;262// if (!valid(0,range<size_t>(itime))) return pinfo;263// const PrimRef prim(linearBounds(0,itime).bounds(),geomID,unsigned(0));264const PrimRef prim(b,geomID,unsigned(0));265pinfo.add_center2(prim);266prims[k++] = prim;267return pinfo;268}269270PrimInfo createPrimRefArrayMB(PrimRef* prims, const BBox1f& time_range, const range<size_t>& r, size_t k, unsigned int geomID) const271{272assert(r.begin() == 0);273assert(r.end() == 1);274275PrimInfo pinfo(empty);276const BBox1f t0t1 = intersect(getTimeRange(), time_range);277if (t0t1.empty()) return pinfo;278279const BBox3fa bounds = linearBounds(0, t0t1).bounds();280const PrimRef prim(bounds, geomID, unsigned(0));281pinfo.add_center2(prim);282prims[k++] = prim;283return pinfo;284}285286PrimInfoMB createPrimRefMBArray(mvector<PrimRefMB>& prims, const BBox1f& t0t1, const range<size_t>& r, size_t k, unsigned int geomID) const287{288assert(r.begin() == 0);289assert(r.end() == 1);290291PrimInfoMB pinfo(empty);292if (!valid(0, timeSegmentRange(t0t1))) return pinfo;293const PrimRefMB prim(linearBounds(0,t0t1),this->numTimeSegments(),this->time_range,this->numTimeSegments(),geomID,unsigned(0));294pinfo.add_primref(prim);295prims[k++] = prim;296return pinfo;297}298};299}300301DECLARE_ISA_FUNCTION(Instance*, createInstance, Device*);302}303304305