| 1 | // Copyright 2009-2021 Intel Corporation |
| 2 | // SPDX-License-Identifier: Apache-2.0 |
| 3 | |
| 4 | #pragma once |
| 5 | |
| 6 | #include "primitive.h" |
| 7 | |
| 8 | namespace embree |
| 9 | { |
| 10 | /* Stores the vertices of M triangles in struct of array layout */ |
| 11 | template <int M> |
| 12 | struct TriangleMv |
| 13 | { |
| 14 | public: |
| 15 | struct Type : public PrimitiveType |
| 16 | { |
| 17 | const char* name() const; |
| 18 | size_t sizeActive(const char* This) const; |
| 19 | size_t sizeTotal(const char* This) const; |
| 20 | size_t getBytes(const char* This) const; |
| 21 | }; |
| 22 | static Type type; |
| 23 | |
| 24 | public: |
| 25 | |
| 26 | /* Returns maximum number of stored triangles */ |
| 27 | static __forceinline size_t max_size() { return M; } |
| 28 | |
| 29 | /* Returns required number of primitive blocks for N primitives */ |
| 30 | static __forceinline size_t blocks(size_t N) { return (N+max_size()-1)/max_size(); } |
| 31 | |
| 32 | public: |
| 33 | |
| 34 | /* Default constructor */ |
| 35 | __forceinline TriangleMv() {} |
| 36 | |
| 37 | /* Construction from vertices and IDs */ |
| 38 | __forceinline TriangleMv(const Vec3vf<M>& v0, const Vec3vf<M>& v1, const Vec3vf<M>& v2, const vuint<M>& geomIDs, const vuint<M>& primIDs) |
| 39 | : v0(v0), v1(v1), v2(v2), geomIDs(geomIDs), primIDs(primIDs) {} |
| 40 | |
| 41 | /* Returns a mask that tells which triangles are valid */ |
| 42 | __forceinline vbool<M> valid() const { return geomIDs != vuint<M>(-1); } |
| 43 | |
| 44 | /* Returns true if the specified triangle is valid */ |
| 45 | __forceinline bool valid(const size_t i) const { assert(i<M); return geomIDs[i] != -1; } |
| 46 | |
| 47 | /* Returns the number of stored triangles */ |
| 48 | __forceinline size_t size() const { return bsf(~movemask(valid())); } |
| 49 | |
| 50 | /* Returns the geometry IDs */ |
| 51 | __forceinline vuint<M>& geomID() { return geomIDs; } |
| 52 | __forceinline const vuint<M>& geomID() const { return geomIDs; } |
| 53 | __forceinline unsigned int geomID(const size_t i) const { assert(i<M); return geomIDs[i]; } |
| 54 | |
| 55 | /* Returns the primitive IDs */ |
| 56 | __forceinline vuint<M>& primID() { return primIDs; } |
| 57 | __forceinline const vuint<M>& primID() const { return primIDs; } |
| 58 | __forceinline unsigned int primID(const size_t i) const { assert(i<M); return primIDs[i]; } |
| 59 | |
| 60 | /* Calculate the bounds of the triangles */ |
| 61 | __forceinline BBox3fa bounds() const |
| 62 | { |
| 63 | Vec3vf<M> lower = min(v0,v1,v2); |
| 64 | Vec3vf<M> upper = max(v0,v1,v2); |
| 65 | vbool<M> mask = valid(); |
| 66 | lower.x = select(mask,lower.x,vfloat<M>(pos_inf)); |
| 67 | lower.y = select(mask,lower.y,vfloat<M>(pos_inf)); |
| 68 | lower.z = select(mask,lower.z,vfloat<M>(pos_inf)); |
| 69 | upper.x = select(mask,upper.x,vfloat<M>(neg_inf)); |
| 70 | upper.y = select(mask,upper.y,vfloat<M>(neg_inf)); |
| 71 | upper.z = select(mask,upper.z,vfloat<M>(neg_inf)); |
| 72 | return BBox3fa(Vec3fa(reduce_min(lower.x),reduce_min(lower.y),reduce_min(lower.z)), |
| 73 | Vec3fa(reduce_max(upper.x),reduce_max(upper.y),reduce_max(upper.z))); |
| 74 | } |
| 75 | |
| 76 | /* Non temporal store */ |
| 77 | __forceinline static void store_nt(TriangleMv* dst, const TriangleMv& src) |
| 78 | { |
| 79 | vfloat<M>::store_nt(&dst->v0.x,src.v0.x); |
| 80 | vfloat<M>::store_nt(&dst->v0.y,src.v0.y); |
| 81 | vfloat<M>::store_nt(&dst->v0.z,src.v0.z); |
| 82 | vfloat<M>::store_nt(&dst->v1.x,src.v1.x); |
| 83 | vfloat<M>::store_nt(&dst->v1.y,src.v1.y); |
| 84 | vfloat<M>::store_nt(&dst->v1.z,src.v1.z); |
| 85 | vfloat<M>::store_nt(&dst->v2.x,src.v2.x); |
| 86 | vfloat<M>::store_nt(&dst->v2.y,src.v2.y); |
| 87 | vfloat<M>::store_nt(&dst->v2.z,src.v2.z); |
| 88 | vuint<M>::store_nt(&dst->geomIDs,src.geomIDs); |
| 89 | vuint<M>::store_nt(&dst->primIDs,src.primIDs); |
| 90 | } |
| 91 | |
| 92 | /* Fill triangle from triangle list */ |
| 93 | __forceinline void fill(const PrimRef* prims, size_t& begin, size_t end, Scene* scene) |
| 94 | { |
| 95 | vuint<M> vgeomID = -1, vprimID = -1; |
| 96 | Vec3vf<M> v0 = zero, v1 = zero, v2 = zero; |
| 97 | |
| 98 | for (size_t i=0; i<M && begin<end; i++, begin++) |
| 99 | { |
| 100 | const PrimRef& prim = prims[begin]; |
| 101 | const unsigned geomID = prim.geomID(); |
| 102 | const unsigned primID = prim.primID(); |
| 103 | const TriangleMesh* __restrict__ const mesh = scene->get<TriangleMesh>(geomID); |
| 104 | const TriangleMesh::Triangle& tri = mesh->triangle(primID); |
| 105 | const Vec3fa& p0 = mesh->vertex(tri.v[0]); |
| 106 | const Vec3fa& p1 = mesh->vertex(tri.v[1]); |
| 107 | const Vec3fa& p2 = mesh->vertex(tri.v[2]); |
| 108 | vgeomID [i] = geomID; |
| 109 | vprimID [i] = primID; |
| 110 | v0.x[i] = p0.x; v0.y[i] = p0.y; v0.z[i] = p0.z; |
| 111 | v1.x[i] = p1.x; v1.y[i] = p1.y; v1.z[i] = p1.z; |
| 112 | v2.x[i] = p2.x; v2.y[i] = p2.y; v2.z[i] = p2.z; |
| 113 | } |
| 114 | TriangleMv::store_nt(this,TriangleMv(v0,v1,v2,vgeomID,vprimID)); |
| 115 | } |
| 116 | |
| 117 | /* Updates the primitive */ |
| 118 | __forceinline BBox3fa update(TriangleMesh* mesh) |
| 119 | { |
| 120 | BBox3fa bounds = empty; |
| 121 | vuint<M> vgeomID = -1, vprimID = -1; |
| 122 | Vec3vf<M> v0 = zero, v1 = zero, v2 = zero; |
| 123 | |
| 124 | for (size_t i=0; i<M; i++) |
| 125 | { |
| 126 | if (primID(i) == -1) break; |
| 127 | const unsigned geomId = geomID(i); |
| 128 | const unsigned primId = primID(i); |
| 129 | const TriangleMesh::Triangle& tri = mesh->triangle(primId); |
| 130 | const Vec3fa p0 = mesh->vertex(tri.v[0]); |
| 131 | const Vec3fa p1 = mesh->vertex(tri.v[1]); |
| 132 | const Vec3fa p2 = mesh->vertex(tri.v[2]); |
| 133 | bounds.extend(merge(BBox3fa(p0),BBox3fa(p1),BBox3fa(p2))); |
| 134 | vgeomID [i] = geomId; |
| 135 | vprimID [i] = primId; |
| 136 | v0.x[i] = p0.x; v0.y[i] = p0.y; v0.z[i] = p0.z; |
| 137 | v1.x[i] = p1.x; v1.y[i] = p1.y; v1.z[i] = p1.z; |
| 138 | v2.x[i] = p2.x; v2.y[i] = p2.y; v2.z[i] = p2.z; |
| 139 | } |
| 140 | new (this) TriangleMv(v0,v1,v2,vgeomID,vprimID); |
| 141 | return bounds; |
| 142 | } |
| 143 | |
| 144 | public: |
| 145 | Vec3vf<M> v0; // 1st vertex of the triangles |
| 146 | Vec3vf<M> v1; // 2nd vertex of the triangles |
| 147 | Vec3vf<M> v2; // 3rd vertex of the triangles |
| 148 | private: |
| 149 | vuint<M> geomIDs; // geometry ID |
| 150 | vuint<M> primIDs; // primitive ID |
| 151 | }; |
| 152 | |
| 153 | template<int M> |
| 154 | typename TriangleMv<M>::Type TriangleMv<M>::type; |
| 155 | |
| 156 | typedef TriangleMv<4> Triangle4v; |
| 157 | } |
| 158 | |