1 | // Copyright 2009-2021 Intel Corporation |
2 | // SPDX-License-Identifier: Apache-2.0 |
3 | |
4 | #pragma once |
5 | |
6 | #include "geometry.h" |
7 | #include "buffer.h" |
8 | |
9 | namespace embree |
10 | { |
11 | /*! Triangle Mesh */ |
12 | struct TriangleMesh : public Geometry |
13 | { |
14 | /*! type of this geometry */ |
15 | static const Geometry::GTypeMask geom_type = Geometry::MTY_TRIANGLE_MESH; |
16 | |
17 | /*! triangle indices */ |
18 | struct Triangle |
19 | { |
20 | uint32_t v[3]; |
21 | |
22 | /*! outputs triangle indices */ |
23 | __forceinline friend embree_ostream operator<<(embree_ostream cout, const Triangle& t) { |
24 | return cout << "Triangle { " << t.v[0] << ", " << t.v[1] << ", " << t.v[2] << " }" ; |
25 | } |
26 | }; |
27 | |
28 | public: |
29 | |
30 | /*! triangle mesh construction */ |
31 | TriangleMesh (Device* device); |
32 | |
33 | /* geometry interface */ |
34 | public: |
35 | void setMask(unsigned mask); |
36 | void setNumTimeSteps (unsigned int numTimeSteps); |
37 | void setVertexAttributeCount (unsigned int N); |
38 | void setBuffer(RTCBufferType type, unsigned int slot, RTCFormat format, const Ref<Buffer>& buffer, size_t offset, size_t stride, unsigned int num); |
39 | void* getBuffer(RTCBufferType type, unsigned int slot); |
40 | void updateBuffer(RTCBufferType type, unsigned int slot); |
41 | void commit(); |
42 | bool verify(); |
43 | void interpolate(const RTCInterpolateArguments* const args); |
44 | void addElementsToCount (GeometryCounts & counts) const; |
45 | |
46 | template<int N> |
47 | void interpolate_impl(const RTCInterpolateArguments* const args) |
48 | { |
49 | unsigned int primID = args->primID; |
50 | float u = args->u; |
51 | float v = args->v; |
52 | RTCBufferType bufferType = args->bufferType; |
53 | unsigned int bufferSlot = args->bufferSlot; |
54 | float* P = args->P; |
55 | float* dPdu = args->dPdu; |
56 | float* dPdv = args->dPdv; |
57 | float* ddPdudu = args->ddPdudu; |
58 | float* ddPdvdv = args->ddPdvdv; |
59 | float* ddPdudv = args->ddPdudv; |
60 | unsigned int valueCount = args->valueCount; |
61 | |
62 | /* calculate base pointer and stride */ |
63 | assert((bufferType == RTC_BUFFER_TYPE_VERTEX && bufferSlot < numTimeSteps) || |
64 | (bufferType == RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE && bufferSlot <= vertexAttribs.size())); |
65 | const char* src = nullptr; |
66 | size_t stride = 0; |
67 | if (bufferType == RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE) { |
68 | src = vertexAttribs[bufferSlot].getPtr(); |
69 | stride = vertexAttribs[bufferSlot].getStride(); |
70 | } else { |
71 | src = vertices[bufferSlot].getPtr(); |
72 | stride = vertices[bufferSlot].getStride(); |
73 | } |
74 | |
75 | for (unsigned int i=0; i<valueCount; i+=N) |
76 | { |
77 | size_t ofs = i*sizeof(float); |
78 | const float w = 1.0f-u-v; |
79 | const Triangle& tri = triangle(i: primID); |
80 | const vbool<N> valid = vint<N>((int)i)+vint<N>(step) < vint<N>(int(valueCount)); |
81 | const vfloat<N> p0 = mem<vfloat<N>>::loadu(valid,(float*)&src[tri.v[0]*stride+ofs]); |
82 | const vfloat<N> p1 = mem<vfloat<N>>::loadu(valid,(float*)&src[tri.v[1]*stride+ofs]); |
83 | const vfloat<N> p2 = mem<vfloat<N>>::loadu(valid,(float*)&src[tri.v[2]*stride+ofs]); |
84 | |
85 | if (P) { |
86 | mem<vfloat<N>>::storeu(valid,P+i,madd(w,p0,madd(u,p1,v*p2))); |
87 | } |
88 | if (dPdu) { |
89 | assert(dPdu); mem<vfloat<N>>::storeu(valid,dPdu+i,p1-p0); |
90 | assert(dPdv); mem<vfloat<N>>::storeu(valid,dPdv+i,p2-p0); |
91 | } |
92 | if (ddPdudu) { |
93 | assert(ddPdudu); mem<vfloat<N>>::storeu(valid,ddPdudu+i,vfloat<N>(zero)); |
94 | assert(ddPdvdv); mem<vfloat<N>>::storeu(valid,ddPdvdv+i,vfloat<N>(zero)); |
95 | assert(ddPdudv); mem<vfloat<N>>::storeu(valid,ddPdudv+i,vfloat<N>(zero)); |
96 | } |
97 | } |
98 | } |
99 | |
100 | public: |
101 | |
102 | /*! returns number of vertices */ |
103 | __forceinline size_t numVertices() const { |
104 | return vertices[0].size(); |
105 | } |
106 | |
107 | /*! returns i'th triangle*/ |
108 | __forceinline const Triangle& triangle(size_t i) const { |
109 | return triangles[i]; |
110 | } |
111 | |
112 | /*! returns i'th vertex of the first time step */ |
113 | __forceinline const Vec3fa vertex(size_t i) const { |
114 | return vertices0[i]; |
115 | } |
116 | |
117 | /*! returns i'th vertex of the first time step */ |
118 | __forceinline const char* vertexPtr(size_t i) const { |
119 | return vertices0.getPtr(i); |
120 | } |
121 | |
122 | /*! returns i'th vertex of itime'th timestep */ |
123 | __forceinline const Vec3fa vertex(size_t i, size_t itime) const { |
124 | return vertices[itime][i]; |
125 | } |
126 | |
127 | /*! returns i'th vertex of itime'th timestep */ |
128 | __forceinline const char* vertexPtr(size_t i, size_t itime) const { |
129 | return vertices[itime].getPtr(i); |
130 | } |
131 | |
132 | /*! calculates the bounds of the i'th triangle */ |
133 | __forceinline BBox3fa bounds(size_t i) const |
134 | { |
135 | const Triangle& tri = triangle(i); |
136 | const Vec3fa v0 = vertex(i: tri.v[0]); |
137 | const Vec3fa v1 = vertex(i: tri.v[1]); |
138 | const Vec3fa v2 = vertex(i: tri.v[2]); |
139 | return BBox3fa(min(a: v0,b: v1,c: v2),max(a: v0,b: v1,c: v2)); |
140 | } |
141 | |
142 | /*! calculates the bounds of the i'th triangle at the itime'th timestep */ |
143 | __forceinline BBox3fa bounds(size_t i, size_t itime) const |
144 | { |
145 | const Triangle& tri = triangle(i); |
146 | const Vec3fa v0 = vertex(i: tri.v[0],itime); |
147 | const Vec3fa v1 = vertex(i: tri.v[1],itime); |
148 | const Vec3fa v2 = vertex(i: tri.v[2],itime); |
149 | return BBox3fa(min(a: v0,b: v1,c: v2),max(a: v0,b: v1,c: v2)); |
150 | } |
151 | |
152 | /*! check if the i'th primitive is valid at the itime'th timestep */ |
153 | __forceinline bool valid(size_t i, size_t itime) const { |
154 | return valid(i, itime_range: make_range(begin: itime, end: itime)); |
155 | } |
156 | |
157 | /*! check if the i'th primitive is valid between the specified time range */ |
158 | __forceinline bool valid(size_t i, const range<size_t>& itime_range) const |
159 | { |
160 | const Triangle& tri = triangle(i); |
161 | if (unlikely(tri.v[0] >= numVertices())) return false; |
162 | if (unlikely(tri.v[1] >= numVertices())) return false; |
163 | if (unlikely(tri.v[2] >= numVertices())) return false; |
164 | |
165 | for (size_t itime = itime_range.begin(); itime <= itime_range.end(); itime++) |
166 | { |
167 | if (!isvalid(v: vertex(i: tri.v[0],itime))) return false; |
168 | if (!isvalid(v: vertex(i: tri.v[1],itime))) return false; |
169 | if (!isvalid(v: vertex(i: tri.v[2],itime))) return false; |
170 | } |
171 | |
172 | return true; |
173 | } |
174 | |
175 | /*! calculates the linear bounds of the i'th primitive at the itimeGlobal'th time segment */ |
176 | __forceinline LBBox3fa linearBounds(size_t i, size_t itime) const { |
177 | return LBBox3fa(bounds(i,itime: itime+0),bounds(i,itime: itime+1)); |
178 | } |
179 | |
180 | /*! calculates the build bounds of the i'th primitive, if it's valid */ |
181 | __forceinline bool buildBounds(size_t i, BBox3fa* bbox = nullptr) const |
182 | { |
183 | const Triangle& tri = triangle(i); |
184 | if (unlikely(tri.v[0] >= numVertices())) return false; |
185 | if (unlikely(tri.v[1] >= numVertices())) return false; |
186 | if (unlikely(tri.v[2] >= numVertices())) return false; |
187 | |
188 | for (size_t t=0; t<numTimeSteps; t++) |
189 | { |
190 | const Vec3fa v0 = vertex(i: tri.v[0],itime: t); |
191 | const Vec3fa v1 = vertex(i: tri.v[1],itime: t); |
192 | const Vec3fa v2 = vertex(i: tri.v[2],itime: t); |
193 | if (unlikely(!isvalid(v0) || !isvalid(v1) || !isvalid(v2))) |
194 | return false; |
195 | } |
196 | |
197 | if (likely(bbox)) |
198 | *bbox = bounds(i); |
199 | |
200 | return true; |
201 | } |
202 | |
203 | /*! calculates the build bounds of the i'th primitive at the itime'th time segment, if it's valid */ |
204 | __forceinline bool buildBounds(size_t i, size_t itime, BBox3fa& bbox) const |
205 | { |
206 | const Triangle& tri = triangle(i); |
207 | if (unlikely(tri.v[0] >= numVertices())) return false; |
208 | if (unlikely(tri.v[1] >= numVertices())) return false; |
209 | if (unlikely(tri.v[2] >= numVertices())) return false; |
210 | |
211 | assert(itime+1 < numTimeSteps); |
212 | const Vec3fa a0 = vertex(i: tri.v[0],itime: itime+0); if (unlikely(!isvalid(a0))) return false; |
213 | const Vec3fa a1 = vertex(i: tri.v[1],itime: itime+0); if (unlikely(!isvalid(a1))) return false; |
214 | const Vec3fa a2 = vertex(i: tri.v[2],itime: itime+0); if (unlikely(!isvalid(a2))) return false; |
215 | const Vec3fa b0 = vertex(i: tri.v[0],itime: itime+1); if (unlikely(!isvalid(b0))) return false; |
216 | const Vec3fa b1 = vertex(i: tri.v[1],itime: itime+1); if (unlikely(!isvalid(b1))) return false; |
217 | const Vec3fa b2 = vertex(i: tri.v[2],itime: itime+1); if (unlikely(!isvalid(b2))) return false; |
218 | |
219 | /* use bounds of first time step in builder */ |
220 | bbox = BBox3fa(min(a: a0,b: a1,c: a2),max(a: a0,b: a1,c: a2)); |
221 | return true; |
222 | } |
223 | |
224 | /*! calculates the linear bounds of the i'th primitive for the specified time range */ |
225 | __forceinline LBBox3fa linearBounds(size_t primID, const BBox1f& dt) const { |
226 | return LBBox3fa([&] (size_t itime) { return bounds(i: primID, itime); }, dt, time_range, fnumTimeSegments); |
227 | } |
228 | |
229 | /*! calculates the linear bounds of the i'th primitive for the specified time range */ |
230 | __forceinline bool linearBounds(size_t i, const BBox1f& dt, LBBox3fa& bbox) const { |
231 | if (!valid(i, itime_range: timeSegmentRange(range: dt))) return false; |
232 | bbox = linearBounds(primID: i, dt); |
233 | return true; |
234 | } |
235 | |
236 | /*! get fast access to first vertex buffer */ |
237 | __forceinline float * getCompactVertexArray () const { |
238 | return (float*) vertices0.getPtr(); |
239 | } |
240 | |
241 | /* gets version info of topology */ |
242 | unsigned int getTopologyVersion() const { |
243 | return triangles.modCounter; |
244 | } |
245 | |
246 | /* returns true if topology changed */ |
247 | bool topologyChanged(unsigned int otherVersion) const { |
248 | return triangles.isModified(otherModCounter: otherVersion); // || numPrimitivesChanged; |
249 | } |
250 | |
251 | /* returns the projected area */ |
252 | __forceinline float projectedPrimitiveArea(const size_t i) const { |
253 | const Triangle& tri = triangle(i); |
254 | const Vec3fa v0 = vertex(i: tri.v[0]); |
255 | const Vec3fa v1 = vertex(i: tri.v[1]); |
256 | const Vec3fa v2 = vertex(i: tri.v[2]); |
257 | return areaProjectedTriangle(v0,v1,v2); |
258 | } |
259 | |
260 | public: |
261 | BufferView<Triangle> triangles; //!< array of triangles |
262 | BufferView<Vec3fa> vertices0; //!< fast access to first vertex buffer |
263 | vector<BufferView<Vec3fa>> vertices; //!< vertex array for each timestep |
264 | vector<RawBufferView> vertexAttribs; //!< vertex attributes |
265 | }; |
266 | |
267 | namespace isa |
268 | { |
269 | struct TriangleMeshISA : public TriangleMesh |
270 | { |
271 | TriangleMeshISA (Device* device) |
272 | : TriangleMesh(device) {} |
273 | |
274 | PrimInfo createPrimRefArray(mvector<PrimRef>& prims, const range<size_t>& r, size_t k, unsigned int geomID) const |
275 | { |
276 | PrimInfo pinfo(empty); |
277 | for (size_t j=r.begin(); j<r.end(); j++) |
278 | { |
279 | BBox3fa bounds = empty; |
280 | if (!buildBounds(i: j,bbox: &bounds)) continue; |
281 | const PrimRef prim(bounds,geomID,unsigned(j)); |
282 | pinfo.add_center2(prim); |
283 | prims[k++] = prim; |
284 | } |
285 | return pinfo; |
286 | } |
287 | |
288 | PrimInfo createPrimRefArrayMB(mvector<PrimRef>& prims, size_t itime, const range<size_t>& r, size_t k, unsigned int geomID) const |
289 | { |
290 | PrimInfo pinfo(empty); |
291 | for (size_t j=r.begin(); j<r.end(); j++) |
292 | { |
293 | BBox3fa bounds = empty; |
294 | if (!buildBounds(i: j,itime,bbox&: bounds)) continue; |
295 | const PrimRef prim(bounds,geomID,unsigned(j)); |
296 | pinfo.add_center2(prim); |
297 | prims[k++] = prim; |
298 | } |
299 | return pinfo; |
300 | } |
301 | |
302 | PrimInfoMB createPrimRefMBArray(mvector<PrimRefMB>& prims, const BBox1f& t0t1, const range<size_t>& r, size_t k, unsigned int geomID) const |
303 | { |
304 | PrimInfoMB pinfo(empty); |
305 | for (size_t j=r.begin(); j<r.end(); j++) |
306 | { |
307 | if (!valid(i: j, itime_range: timeSegmentRange(range: t0t1))) continue; |
308 | const PrimRefMB prim(linearBounds(primID: j,dt: t0t1),this->numTimeSegments(),this->time_range,this->numTimeSegments(),geomID,unsigned(j)); |
309 | pinfo.add_primref(prim); |
310 | prims[k++] = prim; |
311 | } |
312 | return pinfo; |
313 | } |
314 | }; |
315 | } |
316 | |
317 | DECLARE_ISA_FUNCTION(TriangleMesh*, createTriangleMesh, Device*); |
318 | } |
319 | |