1// Copyright 2009-2021 Intel Corporation
2// SPDX-License-Identifier: Apache-2.0
3
4#pragma once
5
6#include "primitive.h"
7
8namespace embree
9{
10 /* Stores the vertices of M quads in struct of array layout */
11 template <int M>
12 struct QuadMv
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 quads */
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 QuadMv() {}
36
37 /* Construction from vertices and IDs */
38 __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)
39 : v0(v0), v1(v1), v2(v2), v3(v3), geomIDs(geomIDs), primIDs(primIDs) {}
40
41 /* Returns a mask that tells which quads are valid */
42 __forceinline vbool<M> valid() const { return geomIDs != vuint<M>(-1); }
43
44 /* Returns true if the specified quad 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 quads */
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 quads */
61 __forceinline BBox3fa bounds() const
62 {
63 Vec3vf<M> lower = min(v0,v1,v2,v3);
64 Vec3vf<M> upper = max(v0,v1,v2,v3);
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(QuadMv* dst, const QuadMv& 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 vfloat<M>::store_nt(&dst->v3.x,src.v3.x);
89 vfloat<M>::store_nt(&dst->v3.y,src.v3.y);
90 vfloat<M>::store_nt(&dst->v3.z,src.v3.z);
91 vuint<M>::store_nt(&dst->geomIDs,src.geomIDs);
92 vuint<M>::store_nt(&dst->primIDs,src.primIDs);
93 }
94
95 /* Fill quad from quad list */
96 __forceinline void fill(const PrimRef* prims, size_t& begin, size_t end, Scene* scene)
97 {
98 vuint<M> vgeomID = -1, vprimID = -1;
99 Vec3vf<M> v0 = zero, v1 = zero, v2 = zero, v3 = zero;
100
101 for (size_t i=0; i<M && begin<end; i++, begin++)
102 {
103 const PrimRef& prim = prims[begin];
104 const unsigned geomID = prim.geomID();
105 const unsigned primID = prim.primID();
106 const QuadMesh* __restrict__ const mesh = scene->get<QuadMesh>(geomID);
107 const QuadMesh::Quad& quad = mesh->quad(primID);
108 const Vec3fa& p0 = mesh->vertex(quad.v[0]);
109 const Vec3fa& p1 = mesh->vertex(quad.v[1]);
110 const Vec3fa& p2 = mesh->vertex(quad.v[2]);
111 const Vec3fa& p3 = mesh->vertex(quad.v[3]);
112 vgeomID [i] = geomID;
113 vprimID [i] = primID;
114 v0.x[i] = p0.x; v0.y[i] = p0.y; v0.z[i] = p0.z;
115 v1.x[i] = p1.x; v1.y[i] = p1.y; v1.z[i] = p1.z;
116 v2.x[i] = p2.x; v2.y[i] = p2.y; v2.z[i] = p2.z;
117 v3.x[i] = p3.x; v3.y[i] = p3.y; v3.z[i] = p3.z;
118 }
119 QuadMv::store_nt(this,QuadMv(v0,v1,v2,v3,vgeomID,vprimID));
120 }
121
122 /* Updates the primitive */
123 __forceinline BBox3fa update(QuadMesh* mesh)
124 {
125 BBox3fa bounds = empty;
126 vuint<M> vgeomID = -1, vprimID = -1;
127 Vec3vf<M> v0 = zero, v1 = zero, v2 = zero;
128
129 for (size_t i=0; i<M; i++)
130 {
131 if (primID(i) == -1) break;
132 const unsigned geomId = geomID(i);
133 const unsigned primId = primID(i);
134 const QuadMesh::Quad& quad = mesh->quad(primId);
135 const Vec3fa p0 = mesh->vertex(quad.v[0]);
136 const Vec3fa p1 = mesh->vertex(quad.v[1]);
137 const Vec3fa p2 = mesh->vertex(quad.v[2]);
138 const Vec3fa p3 = mesh->vertex(quad.v[3]);
139 bounds.extend(merge(BBox3fa(p0),BBox3fa(p1),BBox3fa(p2),BBox3fa(p3)));
140 vgeomID [i] = geomId;
141 vprimID [i] = primId;
142 v0.x[i] = p0.x; v0.y[i] = p0.y; v0.z[i] = p0.z;
143 v1.x[i] = p1.x; v1.y[i] = p1.y; v1.z[i] = p1.z;
144 v2.x[i] = p2.x; v2.y[i] = p2.y; v2.z[i] = p2.z;
145 v3.x[i] = p3.x; v3.y[i] = p3.y; v3.z[i] = p3.z;
146 }
147 new (this) QuadMv(v0,v1,v2,v3,vgeomID,vprimID);
148 return bounds;
149 }
150
151 public:
152 Vec3vf<M> v0; // 1st vertex of the quads
153 Vec3vf<M> v1; // 2nd vertex of the quads
154 Vec3vf<M> v2; // 3rd vertex of the quads
155 Vec3vf<M> v3; // 4th vertex of the quads
156 private:
157 vuint<M> geomIDs; // geometry ID
158 vuint<M> primIDs; // primitive ID
159 };
160
161 template<int M>
162 typename QuadMv<M>::Type QuadMv<M>::type;
163
164 typedef QuadMv<4> Quad4v;
165}
166