Path: blob/master/thirdparty/embree/kernels/geometry/quadv.h
9905 views
// Copyright 2009-2021 Intel Corporation1// SPDX-License-Identifier: Apache-2.023#pragma once45#include "primitive.h"67namespace embree8{9/* Stores the vertices of M quads in struct of array layout */10template <int M>11struct QuadMv12{13public:14struct Type : public PrimitiveType15{16const char* name() const;17size_t sizeActive(const char* This) const;18size_t sizeTotal(const char* This) const;19size_t getBytes(const char* This) const;20};21static Type type;2223public:2425/* Returns maximum number of stored quads */26static __forceinline size_t max_size() { return M; }2728/* Returns required number of primitive blocks for N primitives */29static __forceinline size_t blocks(size_t N) { return (N+max_size()-1)/max_size(); }3031public:3233/* Default constructor */34__forceinline QuadMv() {}3536/* Construction from vertices and IDs */37__forceinline QuadMv(const Vec3vf<M>& v0, const Vec3vf<M>& v1, const Vec3vf<M>& v2, const Vec3vf<M>& v3, const vuint<M>& geomIDs, const vuint<M>& primIDs)38: v0(v0), v1(v1), v2(v2), v3(v3), geomIDs(geomIDs), primIDs(primIDs) {}3940/* Returns a mask that tells which quads are valid */41__forceinline vbool<M> valid() const { return geomIDs != vuint<M>(-1); }4243/* Returns true if the specified quad is valid */44__forceinline bool valid(const size_t i) const { assert(i<M); return geomIDs[i] != -1; }4546/* Returns the number of stored quads */47__forceinline size_t size() const { return bsf(~movemask(valid())); }4849/* Returns the geometry IDs */50__forceinline vuint<M>& geomID() { return geomIDs; }51__forceinline const vuint<M>& geomID() const { return geomIDs; }52__forceinline unsigned int geomID(const size_t i) const { assert(i<M); return geomIDs[i]; }5354/* Returns the primitive IDs */55__forceinline vuint<M> primID() { return primIDs; }56__forceinline const vuint<M> primID() const { return primIDs; }57__forceinline unsigned int primID(const size_t i) const { assert(i<M); return primIDs[i]; }5859/* Calculate the bounds of the quads */60__forceinline BBox3fa bounds() const61{62Vec3vf<M> lower = min(v0,v1,v2,v3);63Vec3vf<M> upper = max(v0,v1,v2,v3);64vbool<M> mask = valid();65lower.x = select(mask,lower.x,vfloat<M>(pos_inf));66lower.y = select(mask,lower.y,vfloat<M>(pos_inf));67lower.z = select(mask,lower.z,vfloat<M>(pos_inf));68upper.x = select(mask,upper.x,vfloat<M>(neg_inf));69upper.y = select(mask,upper.y,vfloat<M>(neg_inf));70upper.z = select(mask,upper.z,vfloat<M>(neg_inf));71return BBox3fa(Vec3fa(reduce_min(lower.x),reduce_min(lower.y),reduce_min(lower.z)),72Vec3fa(reduce_max(upper.x),reduce_max(upper.y),reduce_max(upper.z)));73}7475/* Non temporal store */76__forceinline static void store_nt(QuadMv* dst, const QuadMv& src)77{78vfloat<M>::store_nt(&dst->v0.x,src.v0.x);79vfloat<M>::store_nt(&dst->v0.y,src.v0.y);80vfloat<M>::store_nt(&dst->v0.z,src.v0.z);81vfloat<M>::store_nt(&dst->v1.x,src.v1.x);82vfloat<M>::store_nt(&dst->v1.y,src.v1.y);83vfloat<M>::store_nt(&dst->v1.z,src.v1.z);84vfloat<M>::store_nt(&dst->v2.x,src.v2.x);85vfloat<M>::store_nt(&dst->v2.y,src.v2.y);86vfloat<M>::store_nt(&dst->v2.z,src.v2.z);87vfloat<M>::store_nt(&dst->v3.x,src.v3.x);88vfloat<M>::store_nt(&dst->v3.y,src.v3.y);89vfloat<M>::store_nt(&dst->v3.z,src.v3.z);90vuint<M>::store_nt(&dst->geomIDs,src.geomIDs);91vuint<M>::store_nt(&dst->primIDs,src.primIDs);92}9394/* Fill quad from quad list */95__forceinline void fill(const PrimRef* prims, size_t& begin, size_t end, Scene* scene)96{97vuint<M> vgeomID = -1, vprimID = -1;98Vec3vf<M> v0 = zero, v1 = zero, v2 = zero, v3 = zero;99100for (size_t i=0; i<M && begin<end; i++, begin++)101{102const PrimRef& prim = prims[begin];103const unsigned geomID = prim.geomID();104const unsigned primID = prim.primID();105const QuadMesh* __restrict__ const mesh = scene->get<QuadMesh>(geomID);106const QuadMesh::Quad& quad = mesh->quad(primID);107const Vec3fa& p0 = mesh->vertex(quad.v[0]);108const Vec3fa& p1 = mesh->vertex(quad.v[1]);109const Vec3fa& p2 = mesh->vertex(quad.v[2]);110const Vec3fa& p3 = mesh->vertex(quad.v[3]);111vgeomID [i] = geomID;112vprimID [i] = primID;113v0.x[i] = p0.x; v0.y[i] = p0.y; v0.z[i] = p0.z;114v1.x[i] = p1.x; v1.y[i] = p1.y; v1.z[i] = p1.z;115v2.x[i] = p2.x; v2.y[i] = p2.y; v2.z[i] = p2.z;116v3.x[i] = p3.x; v3.y[i] = p3.y; v3.z[i] = p3.z;117}118QuadMv::store_nt(this,QuadMv(v0,v1,v2,v3,vgeomID,vprimID));119}120121/* Updates the primitive */122__forceinline BBox3fa update(QuadMesh* mesh)123{124BBox3fa bounds = empty;125vuint<M> vgeomID = -1, vprimID = -1;126Vec3vf<M> v0 = zero, v1 = zero, v2 = zero;127128for (size_t i=0; i<M; i++)129{130if (primID(i) == -1) break;131const unsigned geomId = geomID(i);132const unsigned primId = primID(i);133const QuadMesh::Quad& quad = mesh->quad(primId);134const Vec3fa p0 = mesh->vertex(quad.v[0]);135const Vec3fa p1 = mesh->vertex(quad.v[1]);136const Vec3fa p2 = mesh->vertex(quad.v[2]);137const Vec3fa p3 = mesh->vertex(quad.v[3]);138bounds.extend(merge(BBox3fa(p0),BBox3fa(p1),BBox3fa(p2),BBox3fa(p3)));139vgeomID [i] = geomId;140vprimID [i] = primId;141v0.x[i] = p0.x; v0.y[i] = p0.y; v0.z[i] = p0.z;142v1.x[i] = p1.x; v1.y[i] = p1.y; v1.z[i] = p1.z;143v2.x[i] = p2.x; v2.y[i] = p2.y; v2.z[i] = p2.z;144v3.x[i] = p3.x; v3.y[i] = p3.y; v3.z[i] = p3.z;145}146new (this) QuadMv(v0,v1,v2,v3,vgeomID,vprimID);147return bounds;148}149150public:151Vec3vf<M> v0; // 1st vertex of the quads152Vec3vf<M> v1; // 2nd vertex of the quads153Vec3vf<M> v2; // 3rd vertex of the quads154Vec3vf<M> v3; // 4th vertex of the quads155private:156vuint<M> geomIDs; // geometry ID157vuint<M> primIDs; // primitive ID158};159160template<int M>161typename QuadMv<M>::Type QuadMv<M>::type;162163typedef QuadMv<4> Quad4v;164}165166167