Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/embree/kernels/common/scene_quad_mesh.h
9905 views
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
/*! Quad Mesh */
12
struct QuadMesh : public Geometry
13
{
14
/*! type of this geometry */
15
static const Geometry::GTypeMask geom_type = Geometry::MTY_QUAD_MESH;
16
17
/*! triangle indices */
18
struct Quad
19
{
20
Quad() {}
21
22
Quad (uint32_t v0, uint32_t v1, uint32_t v2, uint32_t v3) {
23
v[0] = v0; v[1] = v1; v[2] = v2; v[3] = v3;
24
}
25
26
/*! outputs triangle indices */
27
__forceinline friend embree_ostream operator<<(embree_ostream cout, const Quad& q) {
28
return cout << "Quad {" << q.v[0] << ", " << q.v[1] << ", " << q.v[2] << ", " << q.v[3] << " }";
29
}
30
31
uint32_t v[4];
32
};
33
34
public:
35
36
/*! quad mesh construction */
37
QuadMesh (Device* device);
38
39
/* geometry interface */
40
public:
41
void setMask(unsigned mask);
42
void setNumTimeSteps (unsigned int numTimeSteps);
43
void setVertexAttributeCount (unsigned int N);
44
void setBuffer(RTCBufferType type, unsigned int slot, RTCFormat format, const Ref<Buffer>& buffer, size_t offset, size_t stride, unsigned int num);
45
void* getBufferData(RTCBufferType type, unsigned int slot, BufferDataPointerType pointerType);
46
void updateBuffer(RTCBufferType type, unsigned int slot);
47
void commit();
48
bool verify();
49
void interpolate(const RTCInterpolateArguments* const args);
50
void addElementsToCount (GeometryCounts & counts) const;
51
size_t getGeometryDataDeviceByteSize() const;
52
void convertToDeviceRepresentation(size_t offset, char* data_host, char* data_device) const;
53
54
template<int N>
55
void interpolate_impl(const RTCInterpolateArguments* const args)
56
{
57
unsigned int primID = args->primID;
58
float u = args->u;
59
float v = args->v;
60
RTCBufferType bufferType = args->bufferType;
61
unsigned int bufferSlot = args->bufferSlot;
62
float* P = args->P;
63
float* dPdu = args->dPdu;
64
float* dPdv = args->dPdv;
65
float* ddPdudu = args->ddPdudu;
66
float* ddPdvdv = args->ddPdvdv;
67
float* ddPdudv = args->ddPdudv;
68
unsigned int valueCount = args->valueCount;
69
70
/* calculate base pointer and stride */
71
assert((bufferType == RTC_BUFFER_TYPE_VERTEX && bufferSlot < numTimeSteps) ||
72
(bufferType == RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE && bufferSlot <= vertexAttribs.size()));
73
const char* src = nullptr;
74
size_t stride = 0;
75
if (bufferType == RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE) {
76
src = vertexAttribs[bufferSlot].getPtr();
77
stride = vertexAttribs[bufferSlot].getStride();
78
} else {
79
src = vertices[bufferSlot].getPtr();
80
stride = vertices[bufferSlot].getStride();
81
}
82
83
for (unsigned int i=0; i<valueCount; i+=N)
84
{
85
const vbool<N> valid = vint<N>((int)i)+vint<N>(step) < vint<N>(int(valueCount));
86
const size_t ofs = i*sizeof(float);
87
const Quad& tri = quad(primID);
88
const vfloat<N> p0 = mem<vfloat<N>>::loadu(valid,(float*)&src[tri.v[0]*stride+ofs]);
89
const vfloat<N> p1 = mem<vfloat<N>>::loadu(valid,(float*)&src[tri.v[1]*stride+ofs]);
90
const vfloat<N> p2 = mem<vfloat<N>>::loadu(valid,(float*)&src[tri.v[2]*stride+ofs]);
91
const vfloat<N> p3 = mem<vfloat<N>>::loadu(valid,(float*)&src[tri.v[3]*stride+ofs]);
92
const vbool<N> left = u+v <= 1.0f;
93
const vfloat<N> Q0 = select(left,p0,p2);
94
const vfloat<N> Q1 = select(left,p1,p3);
95
const vfloat<N> Q2 = select(left,p3,p1);
96
const vfloat<N> U = select(left,u,vfloat<N>(1.0f)-u);
97
const vfloat<N> V = select(left,v,vfloat<N>(1.0f)-v);
98
const vfloat<N> W = 1.0f-U-V;
99
if (P) {
100
mem<vfloat<N>>::storeu(valid,P+i,madd(W,Q0,madd(U,Q1,V*Q2)));
101
}
102
if (dPdu) {
103
assert(dPdu); mem<vfloat<N>>::storeu(valid,dPdu+i,select(left,Q1-Q0,Q0-Q1));
104
assert(dPdv); mem<vfloat<N>>::storeu(valid,dPdv+i,select(left,Q2-Q0,Q0-Q2));
105
}
106
if (ddPdudu) {
107
assert(ddPdudu); mem<vfloat<N>>::storeu(valid,ddPdudu+i,vfloat<N>(zero));
108
assert(ddPdvdv); mem<vfloat<N>>::storeu(valid,ddPdvdv+i,vfloat<N>(zero));
109
assert(ddPdudv); mem<vfloat<N>>::storeu(valid,ddPdudv+i,vfloat<N>(zero));
110
}
111
}
112
}
113
114
public:
115
116
/*! returns number of vertices */
117
__forceinline size_t numVertices() const {
118
return vertices[0].size();
119
}
120
121
/*! returns i'th quad */
122
__forceinline const Quad& quad(size_t i) const {
123
return quads[i];
124
}
125
126
/*! returns i'th vertex of itime'th timestep */
127
__forceinline const Vec3fa vertex(size_t i) const {
128
return vertices0[i];
129
}
130
131
/*! returns i'th vertex of itime'th timestep */
132
__forceinline const char* vertexPtr(size_t i) const {
133
return vertices0.getPtr(i);
134
}
135
136
/*! returns i'th vertex of itime'th timestep */
137
__forceinline const Vec3fa vertex(size_t i, size_t itime) const {
138
return vertices[itime][i];
139
}
140
141
/*! returns i'th vertex of itime'th timestep */
142
__forceinline const char* vertexPtr(size_t i, size_t itime) const {
143
return vertices[itime].getPtr(i);
144
}
145
146
/*! returns i'th vertex of for specified time */
147
__forceinline Vec3fa vertex(size_t i, float time) const
148
{
149
float ftime;
150
const size_t itime = timeSegment(time, ftime);
151
const float t0 = 1.0f - ftime;
152
const float t1 = ftime;
153
Vec3fa v0 = vertex(i, itime+0);
154
Vec3fa v1 = vertex(i, itime+1);
155
return madd(Vec3fa(t0),v0,t1*v1);
156
}
157
158
/*! calculates the bounds of the i'th quad */
159
__forceinline BBox3fa bounds(size_t i) const
160
{
161
const Quad& q = quad(i);
162
const Vec3fa v0 = vertex(q.v[0]);
163
const Vec3fa v1 = vertex(q.v[1]);
164
const Vec3fa v2 = vertex(q.v[2]);
165
const Vec3fa v3 = vertex(q.v[3]);
166
return BBox3fa(min(v0,v1,v2,v3),max(v0,v1,v2,v3));
167
}
168
169
/*! calculates the bounds of the i'th quad at the itime'th timestep */
170
__forceinline BBox3fa bounds(size_t i, size_t itime) const
171
{
172
const Quad& q = quad(i);
173
const Vec3fa v0 = vertex(q.v[0],itime);
174
const Vec3fa v1 = vertex(q.v[1],itime);
175
const Vec3fa v2 = vertex(q.v[2],itime);
176
const Vec3fa v3 = vertex(q.v[3],itime);
177
return BBox3fa(min(v0,v1,v2,v3),max(v0,v1,v2,v3));
178
}
179
180
/*! check if the i'th primitive is valid at the itime'th timestep */
181
__forceinline bool valid(size_t i, size_t itime) const {
182
return valid(i, make_range(itime, itime));
183
}
184
185
/*! check if the i'th primitive is valid between the specified time range */
186
__forceinline bool valid(size_t i, const range<size_t>& itime_range) const
187
{
188
const Quad& q = quad(i);
189
if (unlikely(q.v[0] >= numVertices())) return false;
190
if (unlikely(q.v[1] >= numVertices())) return false;
191
if (unlikely(q.v[2] >= numVertices())) return false;
192
if (unlikely(q.v[3] >= numVertices())) return false;
193
194
for (size_t itime = itime_range.begin(); itime <= itime_range.end(); itime++)
195
{
196
if (!isvalid(vertex(q.v[0],itime))) return false;
197
if (!isvalid(vertex(q.v[1],itime))) return false;
198
if (!isvalid(vertex(q.v[2],itime))) return false;
199
if (!isvalid(vertex(q.v[3],itime))) return false;
200
}
201
202
return true;
203
}
204
205
/*! calculates the linear bounds of the i'th quad at the itimeGlobal'th time segment */
206
__forceinline LBBox3fa linearBounds(size_t i, size_t itime) const {
207
return LBBox3fa(bounds(i,itime+0),bounds(i,itime+1));
208
}
209
210
/*! calculates the build bounds of the i'th primitive, if it's valid */
211
__forceinline bool buildBounds(size_t i, BBox3fa* bbox = nullptr) const
212
{
213
const Quad& q = quad(i);
214
if (q.v[0] >= numVertices()) return false;
215
if (q.v[1] >= numVertices()) return false;
216
if (q.v[2] >= numVertices()) return false;
217
if (q.v[3] >= numVertices()) return false;
218
219
for (size_t t=0; t<numTimeSteps; t++)
220
{
221
const Vec3fa v0 = vertex(q.v[0],t);
222
const Vec3fa v1 = vertex(q.v[1],t);
223
const Vec3fa v2 = vertex(q.v[2],t);
224
const Vec3fa v3 = vertex(q.v[3],t);
225
226
if (unlikely(!isvalid(v0) || !isvalid(v1) || !isvalid(v2) || !isvalid(v3)))
227
return false;
228
}
229
230
if (bbox)
231
*bbox = bounds(i);
232
233
return true;
234
}
235
236
/*! calculates the build bounds of the i'th primitive at the itime'th time segment, if it's valid */
237
__forceinline bool buildBounds(size_t i, size_t itime, BBox3fa& bbox) const
238
{
239
const Quad& q = quad(i);
240
if (unlikely(q.v[0] >= numVertices())) return false;
241
if (unlikely(q.v[1] >= numVertices())) return false;
242
if (unlikely(q.v[2] >= numVertices())) return false;
243
if (unlikely(q.v[3] >= numVertices())) return false;
244
245
assert(itime+1 < numTimeSteps);
246
const Vec3fa a0 = vertex(q.v[0],itime+0); if (unlikely(!isvalid(a0))) return false;
247
const Vec3fa a1 = vertex(q.v[1],itime+0); if (unlikely(!isvalid(a1))) return false;
248
const Vec3fa a2 = vertex(q.v[2],itime+0); if (unlikely(!isvalid(a2))) return false;
249
const Vec3fa a3 = vertex(q.v[3],itime+0); if (unlikely(!isvalid(a3))) return false;
250
const Vec3fa b0 = vertex(q.v[0],itime+1); if (unlikely(!isvalid(b0))) return false;
251
const Vec3fa b1 = vertex(q.v[1],itime+1); if (unlikely(!isvalid(b1))) return false;
252
const Vec3fa b2 = vertex(q.v[2],itime+1); if (unlikely(!isvalid(b2))) return false;
253
const Vec3fa b3 = vertex(q.v[3],itime+1); if (unlikely(!isvalid(b3))) return false;
254
255
/* use bounds of first time step in builder */
256
bbox = BBox3fa(min(a0,a1,a2,a3),max(a0,a1,a2,a3));
257
return true;
258
}
259
260
/*! calculates the linear bounds of the i'th primitive for the specified time range */
261
__forceinline LBBox3fa linearBounds(size_t primID, const BBox1f& dt) const {
262
return LBBox3fa([&] (size_t itime) { return bounds(primID, itime); }, dt, time_range, fnumTimeSegments);
263
}
264
265
/*! calculates the linear bounds of the i'th primitive for the specified time range */
266
__forceinline bool linearBounds(size_t i, const BBox1f& dt, LBBox3fa& bbox) const
267
{
268
if (!valid(i, timeSegmentRange(dt))) return false;
269
bbox = linearBounds(i, dt);
270
return true;
271
}
272
273
/*! get fast access to first vertex buffer */
274
__forceinline float * getCompactVertexArray () const {
275
return (float*) vertices0.getPtr();
276
}
277
278
/* gets version info of topology */
279
unsigned int getTopologyVersion() const {
280
return quads.modCounter;
281
}
282
283
/* returns true if topology changed */
284
bool topologyChanged(unsigned int otherVersion) const {
285
return quads.isModified(otherVersion); // || numPrimitivesChanged;
286
}
287
288
/* returns the projected area */
289
__forceinline float projectedPrimitiveArea(const size_t i) const {
290
const Quad& q = quad(i);
291
const Vec3fa v0 = vertex(q.v[0]);
292
const Vec3fa v1 = vertex(q.v[1]);
293
const Vec3fa v2 = vertex(q.v[2]);
294
const Vec3fa v3 = vertex(q.v[3]);
295
return areaProjectedTriangle(v0,v1,v3) +
296
areaProjectedTriangle(v1,v2,v3);
297
}
298
299
public:
300
BufferView<Quad> quads; //!< array of quads
301
BufferView<Vec3fa> vertices0; //!< fast access to first vertex buffer
302
Device::vector<BufferView<Vec3fa>> vertices = device; //!< vertex array for each timestep
303
Device::vector<RawBufferView> vertexAttribs = device; //!< vertex attribute buffers
304
};
305
306
namespace isa
307
{
308
struct QuadMeshISA : public QuadMesh
309
{
310
QuadMeshISA (Device* device)
311
: QuadMesh(device) {}
312
313
LBBox3fa vlinearBounds(size_t primID, const BBox1f& time_range) const {
314
return linearBounds(primID,time_range);
315
}
316
317
PrimInfo createPrimRefArray(PrimRef* prims, const range<size_t>& r, size_t k, unsigned int geomID) const
318
{
319
PrimInfo pinfo(empty);
320
for (size_t j=r.begin(); j<r.end(); j++)
321
{
322
BBox3fa bounds = empty;
323
if (!buildBounds(j,&bounds)) continue;
324
const PrimRef prim(bounds,geomID,unsigned(j));
325
pinfo.add_center2(prim);
326
prims[k++] = prim;
327
}
328
return pinfo;
329
}
330
331
PrimInfo createPrimRefArrayMB(mvector<PrimRef>& prims, size_t itime, const range<size_t>& r, size_t k, unsigned int geomID) const
332
{
333
PrimInfo pinfo(empty);
334
for (size_t j=r.begin(); j<r.end(); j++)
335
{
336
BBox3fa bounds = empty;
337
if (!buildBounds(j,itime,bounds)) continue;
338
const PrimRef prim(bounds,geomID,unsigned(j));
339
pinfo.add_center2(prim);
340
prims[k++] = prim;
341
}
342
return pinfo;
343
}
344
345
PrimInfo createPrimRefArrayMB(PrimRef* prims, const BBox1f& time_range, const range<size_t>& r, size_t k, unsigned int geomID) const
346
{
347
PrimInfo pinfo(empty);
348
const BBox1f t0t1 = BBox1f::intersect(getTimeRange(), time_range);
349
if (t0t1.empty()) return pinfo;
350
351
for (size_t j = r.begin(); j < r.end(); j++) {
352
LBBox3fa lbounds = empty;
353
if (!linearBounds(j, t0t1, lbounds))
354
continue;
355
const PrimRef prim(lbounds.bounds(), geomID, unsigned(j));
356
pinfo.add_center2(prim);
357
prims[k++] = prim;
358
}
359
return pinfo;
360
}
361
362
PrimInfoMB createPrimRefMBArray(mvector<PrimRefMB>& prims, const BBox1f& t0t1, const range<size_t>& r, size_t k, unsigned int geomID) const
363
{
364
PrimInfoMB pinfo(empty);
365
for (size_t j=r.begin(); j<r.end(); j++)
366
{
367
if (!valid(j, timeSegmentRange(t0t1))) continue;
368
const PrimRefMB prim(linearBounds(j,t0t1),this->numTimeSegments(),this->time_range,this->numTimeSegments(),geomID,unsigned(j));
369
pinfo.add_primref(prim);
370
prims[k++] = prim;
371
}
372
return pinfo;
373
}
374
};
375
}
376
377
DECLARE_ISA_FUNCTION(QuadMesh*, createQuadMesh, Device*);
378
}
379
380