Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/embree/kernels/common/scene_instance_array.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 "accel.h"
8
9
namespace embree
10
{
11
struct MotionDerivativeCoefficients;
12
13
/*! Instanced acceleration structure */
14
struct InstanceArray : public Geometry
15
{
16
static const Geometry::GTypeMask geom_type = Geometry::MTY_INSTANCE_ARRAY;
17
18
public:
19
InstanceArray (Device* device, unsigned int numTimeSteps = 1);
20
~InstanceArray();
21
22
private:
23
InstanceArray (const InstanceArray& other) DELETED; // do not implement
24
InstanceArray& operator= (const InstanceArray& other) DELETED; // do not implement
25
26
private:
27
LBBox3fa nonlinearBounds(size_t i,
28
const BBox1f& time_range_in,
29
const BBox1f& geom_time_range,
30
float geom_time_segments) const;
31
32
BBox3fa boundSegment(size_t i, size_t itime,
33
BBox3fa const& obbox0, BBox3fa const& obbox1,
34
BBox3fa const& bbox0, BBox3fa const& bbox1,
35
float t_min, float t_max) const;
36
37
/* calculates the (correct) interpolated bounds */
38
__forceinline BBox3fa bounds(size_t i, size_t itime0, size_t itime1, float f) const
39
{
40
if (unlikely(gsubtype == GTY_SUBTYPE_INSTANCE_QUATERNION))
41
return xfmBounds(slerp(l2w(i, itime0), l2w(i, itime1), f),
42
lerp(getObjectBounds(i, itime0), getObjectBounds(i, itime1), f));
43
return xfmBounds(lerp(l2w(i, itime0), l2w(i, itime1), f),
44
lerp(getObjectBounds(i, itime0), getObjectBounds(i, itime1), f));
45
}
46
47
public:
48
49
virtual void setBuffer(RTCBufferType type, unsigned int slot, RTCFormat format, const Ref<Buffer>& buffer, size_t offset, size_t stride, unsigned int num) override;
50
virtual void* getBufferData(RTCBufferType type, unsigned int slot, BufferDataPointerType pointerType) override;
51
virtual void updateBuffer(RTCBufferType type, unsigned int slot) override;
52
53
virtual void setNumTimeSteps (unsigned int numTimeSteps) override;
54
virtual void setInstancedScene(const Ref<Scene>& scene) override;
55
virtual void setInstancedScenes(const RTCScene* scenes, size_t numScenes) override;
56
virtual AffineSpace3fa getTransform(size_t, float time) override;
57
virtual void setMask (unsigned mask) override;
58
virtual void build() {}
59
virtual void addElementsToCount (GeometryCounts & counts) const override;
60
virtual void commit() override;
61
size_t getGeometryDataDeviceByteSize() const override;
62
void convertToDeviceRepresentation(size_t offset, char* data_host, char* data_device) const override;
63
64
public:
65
66
/*! calculates the bounds of instance */
67
__forceinline BBox3fa bounds(size_t i) const {
68
if (!valid(i))
69
return BBox3fa();
70
71
if (unlikely(gsubtype == GTY_SUBTYPE_INSTANCE_QUATERNION))
72
return xfmBounds(quaternionDecompositionToAffineSpace(l2w(i, 0)),getObject(i)->bounds.bounds());
73
return xfmBounds(l2w(i, 0),getObject(i)->bounds.bounds());
74
}
75
76
/*! gets the bounds of the instanced scene */
77
__forceinline BBox3fa getObjectBounds(size_t i, size_t itime) const {
78
if (!valid(i))
79
return BBox3fa();
80
81
return getObject(i)->getBounds(timeStep(itime));
82
}
83
84
/*! calculates the bounds of instance */
85
__forceinline BBox3fa bounds(size_t i, size_t itime) const {
86
if (!valid(i))
87
return BBox3fa();
88
89
if (unlikely(gsubtype == GTY_SUBTYPE_INSTANCE_QUATERNION))
90
return xfmBounds(quaternionDecompositionToAffineSpace(l2w(i, itime)),getObjectBounds(i, itime));
91
return xfmBounds(l2w(i, itime),getObjectBounds(i, itime));
92
}
93
94
/*! calculates the linear bounds of the i'th primitive for the specified time range */
95
__forceinline LBBox3fa linearBounds(size_t i, const BBox1f& dt) const {
96
if (!valid(i))
97
return LBBox3fa();
98
99
LBBox3fa lbbox = nonlinearBounds(i, dt, time_range, fnumTimeSegments);
100
return lbbox;
101
}
102
103
/*! calculates the build bounds of the i'th item, if it's valid */
104
__forceinline bool buildBounds(size_t i, BBox3fa* bbox = nullptr) const
105
{
106
if (!valid(i))
107
return false;
108
109
const BBox3fa b = bounds(i);
110
if (bbox) *bbox = b;
111
return isvalid(b);
112
}
113
114
/*! calculates the build bounds of the i'th item at the itime'th time segment, if it's valid */
115
__forceinline bool buildBounds(size_t i, size_t itime, BBox3fa& bbox) const
116
{
117
if (!valid(i))
118
return false;
119
120
const LBBox3fa bounds = linearBounds(i,itime);
121
bbox = bounds.bounds ();
122
return isvalid(bounds);
123
}
124
125
/* gets version info of topology */
126
unsigned int getTopologyVersion() const {
127
return numPrimitives;
128
}
129
130
/* returns true if topology changed */
131
bool topologyChanged(unsigned int otherVersion) const {
132
return numPrimitives != otherVersion;
133
}
134
135
/*! check if the i'th primitive is valid between the specified time range */
136
__forceinline bool valid(size_t i) const
137
{
138
if (object) return true;
139
return (object_ids[i] != (unsigned int)(-1));
140
}
141
142
/*! check if the i'th primitive is valid between the specified time range */
143
__forceinline bool valid(size_t i, const range<size_t>& itime_range) const
144
{
145
for (size_t itime = itime_range.begin(); itime <= itime_range.end(); itime++)
146
if (!isvalid(bounds(i,itime))) return false;
147
148
return true;
149
}
150
151
__forceinline AffineSpace3fa getLocal2World(size_t i) const
152
{
153
if (unlikely(gsubtype == GTY_SUBTYPE_INSTANCE_QUATERNION))
154
return quaternionDecompositionToAffineSpace(l2w(i,0));
155
return l2w(i, 0);
156
}
157
158
__forceinline AffineSpace3fa getLocal2World(size_t i, float t) const
159
{
160
if (numTimeSegments() > 0) {
161
float ftime; const unsigned int itime = timeSegment(t, ftime);
162
if (unlikely(gsubtype == GTY_SUBTYPE_INSTANCE_QUATERNION))
163
return slerp(l2w(i, itime+0),l2w(i, itime+1),ftime);
164
return lerp(l2w(i, itime+0),l2w(i, itime+1),ftime);
165
}
166
return getLocal2World(i);
167
}
168
169
__forceinline AffineSpace3fa getWorld2Local(size_t i) const {
170
return rcp(getLocal2World(i));
171
}
172
173
__forceinline AffineSpace3fa getWorld2Local(size_t i, float t) const {
174
return rcp(getLocal2World(i, t));
175
}
176
177
template<int K>
178
__forceinline AffineSpace3vf<K> getWorld2Local(size_t i, const vbool<K>& valid, const vfloat<K>& t) const
179
{
180
if (unlikely(gsubtype == GTY_SUBTYPE_INSTANCE_QUATERNION))
181
return getWorld2LocalSlerp<K>(i, valid, t);
182
return getWorld2LocalLerp<K>(i, valid, t);
183
}
184
185
__forceinline float projectedPrimitiveArea(const size_t i) const {
186
return area(bounds(i));
187
}
188
189
inline Accel* getObject(size_t i) const {
190
if (object) {
191
return object;
192
}
193
194
assert(objects);
195
assert(i < numPrimitives);
196
if (object_ids[i] == (unsigned int)(-1))
197
return nullptr;
198
199
assert(object_ids[i] < numObjects);
200
return objects[object_ids[i]];
201
}
202
203
private:
204
205
template<int K>
206
__forceinline AffineSpace3vf<K> getWorld2LocalSlerp(size_t i, const vbool<K>& valid, const vfloat<K>& t) const
207
{
208
vfloat<K> ftime;
209
const vint<K> itime_k = timeSegment<K>(t, ftime);
210
assert(any(valid));
211
const size_t index = bsf(movemask(valid));
212
const int itime = itime_k[index];
213
if (likely(all(valid, itime_k == vint<K>(itime)))) {
214
return rcp(slerp(AffineSpace3vff<K>(l2w(i, itime+0)),
215
AffineSpace3vff<K>(l2w(i, itime+1)),
216
ftime));
217
}
218
else {
219
AffineSpace3vff<K> space0,space1;
220
vbool<K> valid1 = valid;
221
while (any(valid1)) {
222
vbool<K> valid2;
223
const int itime = next_unique(valid1, itime_k, valid2);
224
space0 = select(valid2, AffineSpace3vff<K>(l2w(i, itime+0)), space0);
225
space1 = select(valid2, AffineSpace3vff<K>(l2w(i, itime+1)), space1);
226
}
227
return rcp(slerp(space0, space1, ftime));
228
}
229
}
230
231
template<int K>
232
__forceinline AffineSpace3vf<K> getWorld2LocalLerp(size_t i, const vbool<K>& valid, const vfloat<K>& t) const
233
{
234
vfloat<K> ftime;
235
const vint<K> itime_k = timeSegment<K>(t, ftime);
236
assert(any(valid));
237
const size_t index = bsf(movemask(valid));
238
const int itime = itime_k[index];
239
if (likely(all(valid, itime_k == vint<K>(itime)))) {
240
return rcp(lerp(AffineSpace3vf<K>((AffineSpace3fa)l2w(i, itime+0)),
241
AffineSpace3vf<K>((AffineSpace3fa)l2w(i, itime+1)),
242
ftime));
243
} else {
244
AffineSpace3vf<K> space0,space1;
245
vbool<K> valid1 = valid;
246
while (any(valid1)) {
247
vbool<K> valid2;
248
const int itime = next_unique(valid1, itime_k, valid2);
249
space0 = select(valid2, AffineSpace3vf<K>((AffineSpace3fa)l2w(i, itime+0)), space0);
250
space1 = select(valid2, AffineSpace3vf<K>((AffineSpace3fa)l2w(i, itime+1)), space1);
251
}
252
return rcp(lerp(space0, space1, ftime));
253
}
254
}
255
256
private:
257
258
__forceinline AffineSpace3ff l2w(size_t i, size_t itime) const {
259
if (l2w_buf[itime].getFormat() == RTC_FORMAT_FLOAT4X4_COLUMN_MAJOR) {
260
return *(AffineSpace3ff*)(l2w_buf[itime].getPtr(i));
261
}
262
else if(l2w_buf[itime].getFormat() == RTC_FORMAT_QUATERNION_DECOMPOSITION) {
263
AffineSpace3ff transform;
264
QuaternionDecomposition* qd = (QuaternionDecomposition*)l2w_buf[itime].getPtr(i);
265
transform.l.vx.x = qd->scale_x;
266
transform.l.vy.y = qd->scale_y;
267
transform.l.vz.z = qd->scale_z;
268
transform.l.vy.x = qd->skew_xy;
269
transform.l.vz.x = qd->skew_xz;
270
transform.l.vz.y = qd->skew_yz;
271
transform.l.vx.y = qd->translation_x;
272
transform.l.vx.z = qd->translation_y;
273
transform.l.vy.z = qd->translation_z;
274
transform.p.x = qd->shift_x;
275
transform.p.y = qd->shift_y;
276
transform.p.z = qd->shift_z;
277
// normalize quaternion
278
Quaternion3f q(qd->quaternion_r, qd->quaternion_i, qd->quaternion_j, qd->quaternion_k);
279
q = normalize(q);
280
transform.l.vx.w = q.i;
281
transform.l.vy.w = q.j;
282
transform.l.vz.w = q.k;
283
transform.p.w = q.r;
284
return transform;
285
}
286
else if (l2w_buf[itime].getFormat() == RTC_FORMAT_FLOAT3X4_COLUMN_MAJOR) {
287
AffineSpace3f* l2w = reinterpret_cast<AffineSpace3f*>(l2w_buf[itime].getPtr(i));
288
return AffineSpace3ff(*l2w);
289
}
290
else if (l2w_buf[itime].getFormat() == RTC_FORMAT_FLOAT3X4_ROW_MAJOR) {
291
float* data = reinterpret_cast<float*>(l2w_buf[itime].getPtr(i));
292
AffineSpace3f l2w;
293
l2w.l.vx.x = data[0]; l2w.l.vy.x = data[1]; l2w.l.vz.x = data[2]; l2w.p.x = data[3];
294
l2w.l.vx.y = data[4]; l2w.l.vy.y = data[5]; l2w.l.vz.y = data[6]; l2w.p.y = data[7];
295
l2w.l.vx.z = data[8]; l2w.l.vy.z = data[9]; l2w.l.vz.z = data[10]; l2w.p.z = data[11];
296
return l2w;
297
}
298
assert(false);
299
return AffineSpace3ff();
300
}
301
302
inline AffineSpace3ff l2w(size_t i) const {
303
return l2w(i, 0);
304
}
305
306
private:
307
Accel* object; //!< fast path if only one scene is instanced
308
Accel** objects;
309
uint32_t numObjects;
310
Device::vector<RawBufferView> l2w_buf = device; //!< transformation from local space to world space for each timestep (either normal matrix or quaternion decomposition)
311
BufferView<uint32_t> object_ids; //!< array of scene ids per instance array primitive
312
};
313
314
namespace isa
315
{
316
struct InstanceArrayISA : public InstanceArray
317
{
318
InstanceArrayISA (Device* device)
319
: InstanceArray(device) {}
320
321
LBBox3fa vlinearBounds(size_t primID, const BBox1f& time_range) const {
322
return linearBounds(primID,time_range);
323
}
324
325
PrimInfo createPrimRefArray(PrimRef* prims, const range<size_t>& r, size_t k, unsigned int geomID) const
326
{
327
PrimInfo pinfo(empty);
328
for (size_t j = r.begin(); j < r.end(); j++) {
329
BBox3fa bounds = empty;
330
if (!buildBounds(j, &bounds) || !valid(j))
331
continue;
332
const PrimRef prim(bounds, geomID, unsigned(j));
333
pinfo.add_center2(prim);
334
prims[k++] = prim;
335
}
336
return pinfo;
337
}
338
339
PrimInfo createPrimRefArrayMB(mvector<PrimRef>& prims, size_t itime, const range<size_t>& r, size_t k, unsigned int geomID) const
340
{
341
PrimInfo pinfo(empty);
342
for (size_t j = r.begin(); j < r.end(); j++) {
343
BBox3fa bounds = empty;
344
if (!buildBounds(j, itime, bounds))
345
continue;
346
const PrimRef prim(bounds, geomID, unsigned(j));
347
pinfo.add_center2(prim);
348
prims[k++] = prim;
349
}
350
return pinfo;
351
}
352
353
PrimInfo createPrimRefArrayMB(PrimRef* prims, const BBox1f& time_range, const range<size_t>& r, size_t k, unsigned int geomID) const
354
{
355
PrimInfo pinfo(empty);
356
const BBox1f t0t1 = BBox1f::intersect(getTimeRange(), time_range);
357
if (t0t1.empty()) return pinfo;
358
359
for (size_t j = r.begin(); j < r.end(); j++) {
360
LBBox3fa lbounds = linearBounds(j, t0t1);
361
if (!isvalid(lbounds.bounds()))
362
continue;
363
const PrimRef prim(lbounds.bounds(), geomID, unsigned(j));
364
pinfo.add_center2(prim);
365
prims[k++] = prim;
366
}
367
return pinfo;
368
}
369
370
PrimInfoMB createPrimRefMBArray(mvector<PrimRefMB>& prims, const BBox1f& t0t1, const range<size_t>& r, size_t k, unsigned int geomID) const
371
{
372
PrimInfoMB pinfo(empty);
373
for (size_t j = r.begin(); j < r.end(); j++) {
374
if (!valid(j, timeSegmentRange(t0t1)))
375
continue;
376
const PrimRefMB prim(linearBounds(j, t0t1), this->numTimeSegments(), this->time_range, this->numTimeSegments(), geomID, unsigned(j));
377
pinfo.add_primref(prim);
378
prims[k++] = prim;
379
}
380
return pinfo;
381
}
382
};
383
}
384
385
DECLARE_ISA_FUNCTION(InstanceArray*, createInstanceArray, Device*);
386
}
387
388