Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/embree/kernels/common/scene_instance.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 Instance : public Geometry
15
{
16
static const Geometry::GTypeMask geom_type = Geometry::MTY_INSTANCE;
17
18
public:
19
Instance (Device* device, Accel* object = nullptr, unsigned int numTimeSteps = 1);
20
~Instance();
21
22
private:
23
Instance (const Instance& other) DELETED; // do not implement
24
Instance& operator= (const Instance& other) DELETED; // do not implement
25
26
private:
27
LBBox3fa nonlinearBounds(const BBox1f& time_range_in,
28
const BBox1f& geom_time_range,
29
float geom_time_segments) const;
30
31
BBox3fa boundSegment(size_t itime,
32
BBox3fa const& obbox0, BBox3fa const& obbox1,
33
BBox3fa const& bbox0, BBox3fa const& bbox1,
34
float t_min, float t_max) const;
35
36
/* calculates the (correct) interpolated bounds */
37
__forceinline BBox3fa bounds(size_t itime0, size_t itime1, float f) const
38
{
39
if (unlikely(gsubtype == GTY_SUBTYPE_INSTANCE_QUATERNION))
40
return xfmBounds(slerp(local2world[itime0], local2world[itime1], f),
41
lerp(getObjectBounds(itime0), getObjectBounds(itime1), f));
42
return xfmBounds(lerp(local2world[itime0], local2world[itime1], f),
43
lerp(getObjectBounds(itime0), getObjectBounds(itime1), f));
44
}
45
46
public:
47
virtual void setNumTimeSteps (unsigned int numTimeSteps) override;
48
virtual void setInstancedScene(const Ref<Scene>& scene) override;
49
virtual void setTransform(const AffineSpace3fa& local2world, unsigned int timeStep) override;
50
virtual void setQuaternionDecomposition(const AffineSpace3ff& qd, unsigned int timeStep) override;
51
virtual AffineSpace3fa getTransform(float time) override;
52
virtual AffineSpace3fa getTransform(size_t, float time) override;
53
virtual void setMask (unsigned mask) override;
54
virtual void build() {}
55
virtual void addElementsToCount (GeometryCounts & counts) const override;
56
virtual void commit() override;
57
virtual size_t getGeometryDataDeviceByteSize() const override;
58
virtual void convertToDeviceRepresentation(size_t offset, char* data_host, char* data_device) const override;
59
60
public:
61
62
/*! calculates the bounds of instance */
63
__forceinline BBox3fa bounds(size_t i) const {
64
assert(i == 0);
65
if (unlikely(gsubtype == GTY_SUBTYPE_INSTANCE_QUATERNION))
66
return xfmBounds(quaternionDecompositionToAffineSpace(local2world[0]),object->bounds.bounds());
67
return xfmBounds(local2world[0],object->bounds.bounds());
68
}
69
70
/*! gets the bounds of the instanced scene */
71
__forceinline BBox3fa getObjectBounds(size_t itime) const {
72
return object->getBounds(timeStep(itime));
73
}
74
75
/*! calculates the bounds of instance */
76
__forceinline BBox3fa bounds(size_t i, size_t itime) const {
77
assert(i == 0);
78
if (unlikely(gsubtype == GTY_SUBTYPE_INSTANCE_QUATERNION))
79
return xfmBounds(quaternionDecompositionToAffineSpace(local2world[itime]),getObjectBounds(itime));
80
return xfmBounds(local2world[itime],getObjectBounds(itime));
81
}
82
83
/*! calculates the linear bounds of the i'th primitive for the specified time range */
84
__forceinline LBBox3fa linearBounds(size_t i, const BBox1f& dt) const {
85
assert(i == 0);
86
LBBox3fa lbbox = nonlinearBounds(dt, time_range, fnumTimeSegments);
87
return lbbox;
88
}
89
90
/*! calculates the build bounds of the i'th item, if it's valid */
91
__forceinline bool buildBounds(size_t i, BBox3fa* bbox = nullptr) const
92
{
93
assert(i==0);
94
const BBox3fa b = bounds(i);
95
if (bbox) *bbox = b;
96
return isvalid(b);
97
}
98
99
/*! calculates the build bounds of the i'th item at the itime'th time segment, if it's valid */
100
__forceinline bool buildBounds(size_t i, size_t itime, BBox3fa& bbox) const
101
{
102
assert(i==0);
103
const LBBox3fa bounds = linearBounds(i,itime);
104
bbox = bounds.bounds ();
105
return isvalid(bounds);
106
}
107
108
/* gets version info of topology */
109
unsigned int getTopologyVersion() const {
110
return numPrimitives;
111
}
112
113
/* returns true if topology changed */
114
bool topologyChanged(unsigned int otherVersion) const {
115
return numPrimitives != otherVersion;
116
}
117
118
/*! check if the i'th primitive is valid between the specified time range */
119
__forceinline bool valid(size_t i, const range<size_t>& itime_range) const
120
{
121
assert(i == 0);
122
for (size_t itime = itime_range.begin(); itime <= itime_range.end(); itime++)
123
if (!isvalid(bounds(i,itime))) return false;
124
125
return true;
126
}
127
128
__forceinline AffineSpace3fa getLocal2World() const
129
{
130
if (unlikely(gsubtype == GTY_SUBTYPE_INSTANCE_QUATERNION))
131
return quaternionDecompositionToAffineSpace(local2world[0]);
132
return local2world[0];
133
}
134
135
__forceinline AffineSpace3fa getLocal2World(float t) const
136
{
137
if (numTimeSegments() > 0) {
138
float ftime; const unsigned int itime = timeSegment(t, ftime);
139
if (unlikely(gsubtype == GTY_SUBTYPE_INSTANCE_QUATERNION))
140
return slerp(local2world[itime+0],local2world[itime+1],ftime);
141
return lerp(local2world[itime+0],local2world[itime+1],ftime);
142
}
143
return getLocal2World();
144
}
145
146
__forceinline AffineSpace3fa getWorld2Local() const {
147
return world2local0;
148
}
149
150
__forceinline AffineSpace3fa getWorld2Local(float t) const {
151
if (numTimeSegments() > 0)
152
return rcp(getLocal2World(t));
153
return getWorld2Local();
154
}
155
156
template<int K>
157
__forceinline AffineSpace3vf<K> getWorld2Local(const vbool<K>& valid, const vfloat<K>& t) const
158
{
159
if (unlikely(gsubtype == GTY_SUBTYPE_INSTANCE_QUATERNION))
160
return getWorld2LocalSlerp<K>(valid, t);
161
return getWorld2LocalLerp<K>(valid, t);
162
}
163
164
__forceinline float projectedPrimitiveArea(const size_t i) const {
165
return area(bounds(i));
166
}
167
168
private:
169
170
template<int K>
171
__forceinline AffineSpace3vf<K> getWorld2LocalSlerp(const vbool<K>& valid, const vfloat<K>& t) const
172
{
173
vfloat<K> ftime;
174
const vint<K> itime_k = timeSegment<K>(t, ftime);
175
assert(any(valid));
176
const size_t index = bsf(movemask(valid));
177
const int itime = itime_k[index];
178
if (likely(all(valid, itime_k == vint<K>(itime)))) {
179
return rcp(slerp(AffineSpace3vff<K>(local2world[itime+0]),
180
AffineSpace3vff<K>(local2world[itime+1]),
181
ftime));
182
}
183
else {
184
AffineSpace3vff<K> space0,space1;
185
vbool<K> valid1 = valid;
186
while (any(valid1)) {
187
vbool<K> valid2;
188
const int itime = next_unique(valid1, itime_k, valid2);
189
space0 = select(valid2, AffineSpace3vff<K>(local2world[itime+0]), space0);
190
space1 = select(valid2, AffineSpace3vff<K>(local2world[itime+1]), space1);
191
}
192
return rcp(slerp(space0, space1, ftime));
193
}
194
}
195
196
template<int K>
197
__forceinline AffineSpace3vf<K> getWorld2LocalLerp(const vbool<K>& valid, const vfloat<K>& t) const
198
{
199
vfloat<K> ftime;
200
const vint<K> itime_k = timeSegment<K>(t, ftime);
201
assert(any(valid));
202
const size_t index = bsf(movemask(valid));
203
const int itime = itime_k[index];
204
if (likely(all(valid, itime_k == vint<K>(itime)))) {
205
return rcp(lerp(AffineSpace3vf<K>((AffineSpace3fa)local2world[itime+0]),
206
AffineSpace3vf<K>((AffineSpace3fa)local2world[itime+1]),
207
ftime));
208
} else {
209
AffineSpace3vf<K> space0,space1;
210
vbool<K> valid1 = valid;
211
while (any(valid1)) {
212
vbool<K> valid2;
213
const int itime = next_unique(valid1, itime_k, valid2);
214
space0 = select(valid2, AffineSpace3vf<K>((AffineSpace3fa)local2world[itime+0]), space0);
215
space1 = select(valid2, AffineSpace3vf<K>((AffineSpace3fa)local2world[itime+1]), space1);
216
}
217
return rcp(lerp(space0, space1, ftime));
218
}
219
}
220
221
public:
222
Accel* object; //!< pointer to instanced acceleration structure
223
AffineSpace3ff* local2world; //!< transformation from local space to world space for each timestep (either normal matrix or quaternion decomposition)
224
AffineSpace3fa world2local0; //!< transformation from world space to local space for timestep 0
225
};
226
227
namespace isa
228
{
229
struct InstanceISA : public Instance
230
{
231
InstanceISA (Device* device)
232
: Instance(device) {}
233
234
LBBox3fa vlinearBounds(size_t primID, const BBox1f& time_range) const {
235
return linearBounds(primID,time_range);
236
}
237
238
PrimInfo createPrimRefArray(PrimRef* prims, const range<size_t>& r, size_t k, unsigned int geomID) const
239
{
240
assert(r.begin() == 0);
241
assert(r.end() == 1);
242
243
PrimInfo pinfo(empty);
244
BBox3fa b = empty;
245
if (!buildBounds(0,&b)) return pinfo;
246
// const BBox3fa b = bounds(0);
247
// if (!isvalid(b)) return pinfo;
248
249
const PrimRef prim(b,geomID,unsigned(0));
250
pinfo.add_center2(prim);
251
prims[k++] = prim;
252
return pinfo;
253
}
254
255
PrimInfo createPrimRefArrayMB(mvector<PrimRef>& prims, size_t itime, const range<size_t>& r, size_t k, unsigned int geomID) const
256
{
257
assert(r.begin() == 0);
258
assert(r.end() == 1);
259
260
PrimInfo pinfo(empty);
261
BBox3fa b = empty;
262
if (!buildBounds(0,&b)) return pinfo;
263
// if (!valid(0,range<size_t>(itime))) return pinfo;
264
// const PrimRef prim(linearBounds(0,itime).bounds(),geomID,unsigned(0));
265
const PrimRef prim(b,geomID,unsigned(0));
266
pinfo.add_center2(prim);
267
prims[k++] = prim;
268
return pinfo;
269
}
270
271
PrimInfo createPrimRefArrayMB(PrimRef* prims, const BBox1f& time_range, const range<size_t>& r, size_t k, unsigned int geomID) const
272
{
273
assert(r.begin() == 0);
274
assert(r.end() == 1);
275
276
PrimInfo pinfo(empty);
277
const BBox1f t0t1 = intersect(getTimeRange(), time_range);
278
if (t0t1.empty()) return pinfo;
279
280
const BBox3fa bounds = linearBounds(0, t0t1).bounds();
281
const PrimRef prim(bounds, geomID, unsigned(0));
282
pinfo.add_center2(prim);
283
prims[k++] = prim;
284
return pinfo;
285
}
286
287
PrimInfoMB createPrimRefMBArray(mvector<PrimRefMB>& prims, const BBox1f& t0t1, const range<size_t>& r, size_t k, unsigned int geomID) const
288
{
289
assert(r.begin() == 0);
290
assert(r.end() == 1);
291
292
PrimInfoMB pinfo(empty);
293
if (!valid(0, timeSegmentRange(t0t1))) return pinfo;
294
const PrimRefMB prim(linearBounds(0,t0t1),this->numTimeSegments(),this->time_range,this->numTimeSegments(),geomID,unsigned(0));
295
pinfo.add_primref(prim);
296
prims[k++] = prim;
297
return pinfo;
298
}
299
};
300
}
301
302
DECLARE_ISA_FUNCTION(Instance*, createInstance, Device*);
303
}
304
305