Path: blob/master/thirdparty/embree/kernels/geometry/curveNi_mb.h
9905 views
// Copyright 2009-2021 Intel Corporation1// SPDX-License-Identifier: Apache-2.023#pragma once45#include "primitive.h"6#include "curve_intersector_precalculations.h"78namespace embree9{10template<int M>11struct CurveNiMB12{13struct Type : public PrimitiveType {14const char* name() const;15size_t sizeActive(const char* This) const;16size_t sizeTotal(const char* This) const;17size_t getBytes(const char* This) const;18};19static Type type;2021public:2223/* Returns maximum number of stored primitives */24static __forceinline size_t max_size() { return M; }2526/* Returns required number of primitive blocks for N primitives */27static __forceinline size_t blocks(size_t N) { return (N+M-1)/M; }2829static __forceinline size_t bytes(size_t N)30{31const size_t f = N/M, r = N%M;32static_assert(sizeof(CurveNiMB) == 6+37*M+24, "internal data layout issue");33return f*sizeof(CurveNiMB) + (r!=0)*(6+37*r+24);34}3536public:3738/*! Default constructor. */39__forceinline CurveNiMB () {}4041/*! fill curve from curve list */42__forceinline LBBox3fa fillMB(const PrimRefMB* prims, size_t& begin, size_t _end, Scene* scene, const BBox1f time_range)43{44size_t end = min(begin+M,_end);45N = (unsigned char)(end-begin);46const unsigned int geomID0 = prims[begin].geomID();47this->geomID(N) = geomID0;48ty = (unsigned char) scene->get(geomID0)->getType();4950/* encode all primitives */51LBBox3fa lbounds = empty;52for (size_t i=0; i<N; i++)53{54const PrimRefMB& prim = prims[begin+i];55const unsigned int geomID = prim.geomID(); assert(geomID == geomID0);56const unsigned int primID = prim.primID();57lbounds.extend(scene->get(geomID)->vlinearBounds(primID,time_range));58}59BBox3fa bounds = lbounds.bounds();6061/* calculate offset and scale */62Vec3fa loffset = bounds.lower;63float lscale = reduce_min(256.0f/(bounds.size()*sqrt(3.0f)));64if (bounds.size() == Vec3fa(zero)) lscale = 0.0f;65*this->offset(N) = loffset;66*this->scale(N) = lscale;67this->time_offset(N) = time_range.lower;68this->time_scale(N) = 1.0f/time_range.size();6970/* encode all primitives */71for (size_t i=0; i<M && begin<end; i++, begin++)72{73const PrimRefMB& prim = prims[begin];74const unsigned int geomID = prim.geomID();75const unsigned int primID = prim.primID();76const LinearSpace3fa space2 = scene->get(geomID)->computeAlignedSpaceMB(primID,time_range);7778const LinearSpace3fa space3(trunc(126.0f*space2.vx),trunc(126.0f*space2.vy),trunc(126.0f*space2.vz));79const LBBox3fa bounds = scene->get(geomID)->vlinearBounds(loffset,lscale,max(length(space3.vx),length(space3.vy),length(space3.vz)),space3.transposed(),primID,time_range);8081// NOTE: this weird (char) (short) cast works around VS2015 Win32 compiler bug82bounds_vx_x(N)[i] = (char) (short) space3.vx.x;83bounds_vx_y(N)[i] = (char) (short) space3.vx.y;84bounds_vx_z(N)[i] = (char) (short) space3.vx.z;85bounds_vx_lower0(N)[i] = (short) clamp(floor(bounds.bounds0.lower.x),-32767.0f,32767.0f);86bounds_vx_upper0(N)[i] = (short) clamp(ceil (bounds.bounds0.upper.x),-32767.0f,32767.0f);87bounds_vx_lower1(N)[i] = (short) clamp(floor(bounds.bounds1.lower.x),-32767.0f,32767.0f);88bounds_vx_upper1(N)[i] = (short) clamp(ceil (bounds.bounds1.upper.x),-32767.0f,32767.0f);89assert(-32767.0f <= floor(bounds.bounds0.lower.x) && floor(bounds.bounds0.lower.x) <= 32767.0f);90assert(-32767.0f <= ceil (bounds.bounds0.upper.x) && ceil (bounds.bounds0.upper.x) <= 32767.0f);91assert(-32767.0f <= floor(bounds.bounds1.lower.x) && floor(bounds.bounds1.lower.x) <= 32767.0f);92assert(-32767.0f <= ceil (bounds.bounds1.upper.x) && ceil (bounds.bounds1.upper.x) <= 32767.0f);9394bounds_vy_x(N)[i] = (char) (short) space3.vy.x;95bounds_vy_y(N)[i] = (char) (short) space3.vy.y;96bounds_vy_z(N)[i] = (char) (short) space3.vy.z;97bounds_vy_lower0(N)[i] = (short) clamp(floor(bounds.bounds0.lower.y),-32767.0f,32767.0f);98bounds_vy_upper0(N)[i] = (short) clamp(ceil (bounds.bounds0.upper.y),-32767.0f,32767.0f);99bounds_vy_lower1(N)[i] = (short) clamp(floor(bounds.bounds1.lower.y),-32767.0f,32767.0f);100bounds_vy_upper1(N)[i] = (short) clamp(ceil (bounds.bounds1.upper.y),-32767.0f,32767.0f);101assert(-32767.0f <= floor(bounds.bounds0.lower.y) && floor(bounds.bounds0.lower.y) <= 32767.0f);102assert(-32767.0f <= ceil (bounds.bounds0.upper.y) && ceil (bounds.bounds0.upper.y) <= 32767.0f);103assert(-32767.0f <= floor(bounds.bounds1.lower.y) && floor(bounds.bounds1.lower.y) <= 32767.0f);104assert(-32767.0f <= ceil (bounds.bounds1.upper.y) && ceil (bounds.bounds1.upper.y) <= 32767.0f);105106bounds_vz_x(N)[i] = (char) (short) space3.vz.x;107bounds_vz_y(N)[i] = (char) (short) space3.vz.y;108bounds_vz_z(N)[i] = (char) (short) space3.vz.z;109bounds_vz_lower0(N)[i] = (short) clamp(floor(bounds.bounds0.lower.z),-32767.0f,32767.0f);110bounds_vz_upper0(N)[i] = (short) clamp(ceil (bounds.bounds0.upper.z),-32767.0f,32767.0f);111bounds_vz_lower1(N)[i] = (short) clamp(floor(bounds.bounds1.lower.z),-32767.0f,32767.0f);112bounds_vz_upper1(N)[i] = (short) clamp(ceil (bounds.bounds1.upper.z),-32767.0f,32767.0f);113assert(-32767.0f <= floor(bounds.bounds0.lower.z) && floor(bounds.bounds0.lower.z) <= 32767.0f);114assert(-32767.0f <= ceil (bounds.bounds0.upper.z) && ceil (bounds.bounds0.upper.z) <= 32767.0f);115assert(-32767.0f <= floor(bounds.bounds1.lower.z) && floor(bounds.bounds1.lower.z) <= 32767.0f);116assert(-32767.0f <= ceil (bounds.bounds1.upper.z) && ceil (bounds.bounds1.upper.z) <= 32767.0f);117118this->primID(N)[i] = primID;119}120121return lbounds;122}123124template<typename BVH, typename SetMB, typename Allocator>125__forceinline static typename BVH::NodeRecordMB4D createLeafMB(BVH* bvh, const SetMB& prims, const Allocator& alloc)126{127size_t start = prims.begin();128size_t end = prims.end();129size_t items = CurveNiMB::blocks(prims.size());130size_t numbytes = CurveNiMB::bytes(prims.size());131CurveNiMB* accel = (CurveNiMB*) alloc.malloc1(numbytes,BVH::byteAlignment);132const typename BVH::NodeRef node = bvh->encodeLeaf((char*)accel,items);133134LBBox3fa bounds = empty;135for (size_t i=0; i<items; i++)136bounds.extend(accel[i].fillMB(prims.prims->data(),start,end,bvh->scene,prims.time_range));137138return typename BVH::NodeRecordMB4D(node,bounds,prims.time_range);139};140141142public:143144// 27.6 - 46 bytes per primitive145unsigned char ty;146unsigned char N;147unsigned char data[4+37*M+24];148149/*150struct Layout151{152unsigned int geomID;153unsigned int primID[N];154155char bounds_vx_x[N];156char bounds_vx_y[N];157char bounds_vx_z[N];158short bounds_vx_lower0[N];159short bounds_vx_upper0[N];160short bounds_vx_lower1[N];161short bounds_vx_upper1[N];162163char bounds_vy_x[N];164char bounds_vy_y[N];165char bounds_vy_z[N];166short bounds_vy_lower0[N];167short bounds_vy_upper0[N];168short bounds_vy_lower1[N];169short bounds_vy_upper1[N];170171char bounds_vz_x[N];172char bounds_vz_y[N];173char bounds_vz_z[N];174short bounds_vz_lower0[N];175short bounds_vz_upper0[N];176short bounds_vz_lower1[N];177short bounds_vz_upper1[N];178179Vec3f offset;180float scale;181182float time_offset;183float time_scale;184};185*/186187__forceinline unsigned int& geomID(size_t N) { return *(unsigned int*)((char*)this+2); }188__forceinline const unsigned int& geomID(size_t N) const { return *(unsigned int*)((char*)this+2); }189190__forceinline unsigned int* primID(size_t N) { return (unsigned int*)((char*)this+6); }191__forceinline const unsigned int* primID(size_t N) const { return (unsigned int*)((char*)this+6); }192193__forceinline char* bounds_vx_x(size_t N) { return (char*)((char*)this+6+4*N); }194__forceinline const char* bounds_vx_x(size_t N) const { return (char*)((char*)this+6+4*N); }195196__forceinline char* bounds_vx_y(size_t N) { return (char*)((char*)this+6+5*N); }197__forceinline const char* bounds_vx_y(size_t N) const { return (char*)((char*)this+6+5*N); }198199__forceinline char* bounds_vx_z(size_t N) { return (char*)((char*)this+6+6*N); }200__forceinline const char* bounds_vx_z(size_t N) const { return (char*)((char*)this+6+6*N); }201202__forceinline short* bounds_vx_lower0(size_t N) { return (short*)((char*)this+6+7*N); }203__forceinline const short* bounds_vx_lower0(size_t N) const { return (short*)((char*)this+6+7*N); }204205__forceinline short* bounds_vx_upper0(size_t N) { return (short*)((char*)this+6+9*N); }206__forceinline const short* bounds_vx_upper0(size_t N) const { return (short*)((char*)this+6+9*N); }207208__forceinline short* bounds_vx_lower1(size_t N) { return (short*)((char*)this+6+11*N); }209__forceinline const short* bounds_vx_lower1(size_t N) const { return (short*)((char*)this+6+11*N); }210211__forceinline short* bounds_vx_upper1(size_t N) { return (short*)((char*)this+6+13*N); }212__forceinline const short* bounds_vx_upper1(size_t N) const { return (short*)((char*)this+6+13*N); }213214__forceinline char* bounds_vy_x(size_t N) { return (char*)((char*)this+6+15*N); }215__forceinline const char* bounds_vy_x(size_t N) const { return (char*)((char*)this+6+15*N); }216217__forceinline char* bounds_vy_y(size_t N) { return (char*)((char*)this+6+16*N); }218__forceinline const char* bounds_vy_y(size_t N) const { return (char*)((char*)this+6+16*N); }219220__forceinline char* bounds_vy_z(size_t N) { return (char*)((char*)this+6+17*N); }221__forceinline const char* bounds_vy_z(size_t N) const { return (char*)((char*)this+6+17*N); }222223__forceinline short* bounds_vy_lower0(size_t N) { return (short*)((char*)this+6+18*N); }224__forceinline const short* bounds_vy_lower0(size_t N) const { return (short*)((char*)this+6+18*N); }225226__forceinline short* bounds_vy_upper0(size_t N) { return (short*)((char*)this+6+20*N); }227__forceinline const short* bounds_vy_upper0(size_t N) const { return (short*)((char*)this+6+20*N); }228229__forceinline short* bounds_vy_lower1(size_t N) { return (short*)((char*)this+6+22*N); }230__forceinline const short* bounds_vy_lower1(size_t N) const { return (short*)((char*)this+6+22*N); }231232__forceinline short* bounds_vy_upper1(size_t N) { return (short*)((char*)this+6+24*N); }233__forceinline const short* bounds_vy_upper1(size_t N) const { return (short*)((char*)this+6+24*N); }234235__forceinline char* bounds_vz_x(size_t N) { return (char*)((char*)this+6+26*N); }236__forceinline const char* bounds_vz_x(size_t N) const { return (char*)((char*)this+6+26*N); }237238__forceinline char* bounds_vz_y(size_t N) { return (char*)((char*)this+6+27*N); }239__forceinline const char* bounds_vz_y(size_t N) const { return (char*)((char*)this+6+27*N); }240241__forceinline char* bounds_vz_z(size_t N) { return (char*)((char*)this+6+28*N); }242__forceinline const char* bounds_vz_z(size_t N) const { return (char*)((char*)this+6+28*N); }243244__forceinline short* bounds_vz_lower0(size_t N) { return (short*)((char*)this+6+29*N); }245__forceinline const short* bounds_vz_lower0(size_t N) const { return (short*)((char*)this+6+29*N); }246247__forceinline short* bounds_vz_upper0(size_t N) { return (short*)((char*)this+6+31*N); }248__forceinline const short* bounds_vz_upper0(size_t N) const { return (short*)((char*)this+6+31*N); }249250__forceinline short* bounds_vz_lower1(size_t N) { return (short*)((char*)this+6+33*N); }251__forceinline const short* bounds_vz_lower1(size_t N) const { return (short*)((char*)this+6+33*N); }252253__forceinline short* bounds_vz_upper1(size_t N) { return (short*)((char*)this+6+35*N); }254__forceinline const short* bounds_vz_upper1(size_t N) const { return (short*)((char*)this+6+35*N); }255256__forceinline Vec3f* offset(size_t N) { return (Vec3f*)((char*)this+6+37*N); }257__forceinline const Vec3f* offset(size_t N) const { return (Vec3f*)((char*)this+6+37*N); }258259__forceinline float* scale(size_t N) { return (float*)((char*)this+6+37*N+12); }260__forceinline const float* scale(size_t N) const { return (float*)((char*)this+6+37*N+12); }261262__forceinline float& time_offset(size_t N) { return *(float*)((char*)this+6+37*N+16); }263__forceinline const float& time_offset(size_t N) const { return *(float*)((char*)this+6+37*N+16); }264265__forceinline float& time_scale(size_t N) { return *(float*)((char*)this+6+37*N+20); }266__forceinline const float& time_scale(size_t N) const { return *(float*)((char*)this+6+37*N+20); }267268__forceinline char* end(size_t N) { return (char*)this+6+37*N+24; }269__forceinline const char* end(size_t N) const { return (char*)this+6+37*N+24; }270};271272template<int M>273typename CurveNiMB<M>::Type CurveNiMB<M>::type;274275typedef CurveNiMB<4> Curve4iMB;276typedef CurveNiMB<8> Curve8iMB;277}278279280