Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/embree/kernels/common/scene_curves.h
9905 views
1
// Copyright 2009-2021 Intel Corporation
2
// SPDX-License-Identifier: Apache-2.0
3
4
#pragma once
5
6
#include "default.h"
7
#include "geometry.h"
8
#include "buffer.h"
9
10
#include "../subdiv/bezier_curve.h"
11
#include "../subdiv/hermite_curve.h"
12
#include "../subdiv/bspline_curve.h"
13
#include "../subdiv/catmullrom_curve.h"
14
#include "../subdiv/linear_bezier_patch.h"
15
16
namespace embree
17
{
18
/*! represents an array of bicubic bezier curves */
19
struct CurveGeometry : public Geometry
20
{
21
/*! type of this geometry */
22
static const Geometry::GTypeMask geom_type = Geometry::MTY_CURVE4;
23
24
public:
25
26
/*! bezier curve construction */
27
CurveGeometry (Device* device, Geometry::GType gtype);
28
29
public:
30
void setMask(unsigned mask);
31
void setNumTimeSteps (unsigned int numTimeSteps);
32
void setVertexAttributeCount (unsigned int N);
33
void setBuffer(RTCBufferType type, unsigned int slot, RTCFormat format, const Ref<Buffer>& buffer, size_t offset, size_t stride, unsigned int num);
34
void* getBufferData(RTCBufferType type, unsigned int slot, BufferDataPointerType pointerType);
35
void updateBuffer(RTCBufferType type, unsigned int slot);
36
void commit();
37
bool verify();
38
void setTessellationRate(float N);
39
void setMaxRadiusScale(float s);
40
void addElementsToCount (GeometryCounts & counts) const;
41
size_t getGeometryDataDeviceByteSize() const;
42
void convertToDeviceRepresentation(size_t offset, char* data_host, char* data_device) const;
43
44
public:
45
46
/*! returns the number of vertices */
47
__forceinline size_t numVertices() const {
48
return vertices[0].size();
49
}
50
51
/*! returns the i'th curve */
52
__forceinline const unsigned int& curve(size_t i) const {
53
return curves[i];
54
}
55
56
/*! returns i'th vertex of the first time step */
57
__forceinline Vec3ff vertex(size_t i) const {
58
return vertices0[i];
59
}
60
61
/*! returns i'th normal of the first time step */
62
__forceinline Vec3fa normal(size_t i) const {
63
return normals0[i];
64
}
65
66
/*! returns i'th tangent of the first time step */
67
__forceinline Vec3ff tangent(size_t i) const {
68
return tangents0[i];
69
}
70
71
/*! returns i'th normal derivative of the first time step */
72
__forceinline Vec3fa dnormal(size_t i) const {
73
return dnormals0[i];
74
}
75
76
/*! returns i'th radius of the first time step */
77
__forceinline float radius(size_t i) const {
78
return vertices0[i].w;
79
}
80
81
/*! returns i'th vertex of itime'th timestep */
82
__forceinline Vec3ff vertex(size_t i, size_t itime) const {
83
return vertices[itime][i];
84
}
85
86
/*! returns i'th normal of itime'th timestep */
87
__forceinline Vec3fa normal(size_t i, size_t itime) const {
88
return normals[itime][i];
89
}
90
91
/*! returns i'th tangent of itime'th timestep */
92
__forceinline Vec3ff tangent(size_t i, size_t itime) const {
93
return tangents[itime][i];
94
}
95
96
/*! returns i'th normal derivative of itime'th timestep */
97
__forceinline Vec3fa dnormal(size_t i, size_t itime) const {
98
return dnormals[itime][i];
99
}
100
101
/*! returns i'th radius of itime'th timestep */
102
__forceinline float radius(size_t i, size_t itime) const {
103
return vertices[itime][i].w;
104
}
105
106
/*! gathers the curve starting with i'th vertex */
107
__forceinline void gather(Vec3ff& p0, Vec3ff& p1, Vec3ff& p2, Vec3ff& p3, size_t i) const
108
{
109
p0 = vertex(i+0);
110
p1 = vertex(i+1);
111
p2 = vertex(i+2);
112
p3 = vertex(i+3);
113
}
114
115
/*! gathers the curve starting with i'th vertex of itime'th timestep */
116
__forceinline void gather(Vec3ff& p0, Vec3ff& p1, Vec3ff& p2, Vec3ff& p3, size_t i, size_t itime) const
117
{
118
p0 = vertex(i+0,itime);
119
p1 = vertex(i+1,itime);
120
p2 = vertex(i+2,itime);
121
p3 = vertex(i+3,itime);
122
}
123
124
/*! gathers the curve normals starting with i'th vertex */
125
__forceinline void gather_normals(Vec3fa& n0, Vec3fa& n1, Vec3fa& n2, Vec3fa& n3, size_t i) const
126
{
127
n0 = normal(i+0);
128
n1 = normal(i+1);
129
n2 = normal(i+2);
130
n3 = normal(i+3);
131
}
132
133
/*! gathers the curve starting with i'th vertex */
134
__forceinline void gather(Vec3ff& p0, Vec3ff& p1, Vec3ff& p2, Vec3ff& p3, Vec3fa& n0, Vec3fa& n1, Vec3fa& n2, Vec3fa& n3, size_t i) const
135
{
136
p0 = vertex(i+0);
137
p1 = vertex(i+1);
138
p2 = vertex(i+2);
139
p3 = vertex(i+3);
140
n0 = normal(i+0);
141
n1 = normal(i+1);
142
n2 = normal(i+2);
143
n3 = normal(i+3);
144
}
145
146
/*! gathers the curve starting with i'th vertex of itime'th timestep */
147
__forceinline void gather(Vec3ff& p0, Vec3ff& p1, Vec3ff& p2, Vec3ff& p3, Vec3fa& n0, Vec3fa& n1, Vec3fa& n2, Vec3fa& n3, size_t i, size_t itime) const
148
{
149
p0 = vertex(i+0,itime);
150
p1 = vertex(i+1,itime);
151
p2 = vertex(i+2,itime);
152
p3 = vertex(i+3,itime);
153
n0 = normal(i+0,itime);
154
n1 = normal(i+1,itime);
155
n2 = normal(i+2,itime);
156
n3 = normal(i+3,itime);
157
}
158
159
/*! prefetches the curve starting with i'th vertex of itime'th timestep */
160
__forceinline void prefetchL1_vertices(size_t i) const
161
{
162
prefetchL1(vertices0.getPtr(i)+0);
163
prefetchL1(vertices0.getPtr(i)+64);
164
}
165
166
/*! prefetches the curve starting with i'th vertex of itime'th timestep */
167
__forceinline void prefetchL2_vertices(size_t i) const
168
{
169
prefetchL2(vertices0.getPtr(i)+0);
170
prefetchL2(vertices0.getPtr(i)+64);
171
}
172
173
/*! loads curve vertices for specified time */
174
__forceinline void gather(Vec3ff& p0, Vec3ff& p1, Vec3ff& p2, Vec3ff& p3, size_t i, float time) const
175
{
176
float ftime;
177
const size_t itime = timeSegment(time, ftime);
178
179
const float t0 = 1.0f - ftime;
180
const float t1 = ftime;
181
Vec3ff a0,a1,a2,a3;
182
gather(a0,a1,a2,a3,i,itime);
183
Vec3ff b0,b1,b2,b3;
184
gather(b0,b1,b2,b3,i,itime+1);
185
p0 = madd(Vec3ff(t0),a0,t1*b0);
186
p1 = madd(Vec3ff(t0),a1,t1*b1);
187
p2 = madd(Vec3ff(t0),a2,t1*b2);
188
p3 = madd(Vec3ff(t0),a3,t1*b3);
189
}
190
191
/*! loads curve vertices for specified time */
192
__forceinline void gather_safe(Vec3ff& p0, Vec3ff& p1, Vec3ff& p2, Vec3ff& p3, size_t i, float time) const
193
{
194
if (hasMotionBlur()) gather(p0,p1,p2,p3,i,time);
195
else gather(p0,p1,p2,p3,i);
196
}
197
198
/*! loads curve vertices for specified time */
199
__forceinline void gather(Vec3ff& p0, Vec3ff& p1, Vec3ff& p2, Vec3ff& p3, Vec3fa& n0, Vec3fa& n1, Vec3fa& n2, Vec3fa& n3, size_t i, float time) const
200
{
201
float ftime;
202
const size_t itime = timeSegment(time, ftime);
203
204
const float t0 = 1.0f - ftime;
205
const float t1 = ftime;
206
Vec3ff a0,a1,a2,a3; Vec3fa an0,an1,an2,an3;
207
gather(a0,a1,a2,a3,an0,an1,an2,an3,i,itime);
208
Vec3ff b0,b1,b2,b3; Vec3fa bn0,bn1,bn2,bn3;
209
gather(b0,b1,b2,b3,bn0,bn1,bn2,bn3,i,itime+1);
210
p0 = madd(Vec3ff(t0),a0,t1*b0);
211
p1 = madd(Vec3ff(t0),a1,t1*b1);
212
p2 = madd(Vec3ff(t0),a2,t1*b2);
213
p3 = madd(Vec3ff(t0),a3,t1*b3);
214
n0 = madd(Vec3ff(t0),an0,t1*bn0);
215
n1 = madd(Vec3ff(t0),an1,t1*bn1);
216
n2 = madd(Vec3ff(t0),an2,t1*bn2);
217
n3 = madd(Vec3ff(t0),an3,t1*bn3);
218
}
219
220
/*! loads curve vertices for specified time for mblur and non-mblur case */
221
__forceinline void gather_safe(Vec3ff& p0, Vec3ff& p1, Vec3ff& p2, Vec3ff& p3, Vec3fa& n0, Vec3fa& n1, Vec3fa& n2, Vec3fa& n3, size_t i, float time) const
222
{
223
if (hasMotionBlur()) gather(p0,p1,p2,p3,n0,n1,n2,n3,i,time);
224
else gather(p0,p1,p2,p3,n0,n1,n2,n3,i);
225
}
226
227
template<typename SourceCurve3ff, typename SourceCurve3fa, typename TensorLinearCubicBezierSurface3fa>
228
__forceinline TensorLinearCubicBezierSurface3fa getNormalOrientedCurve(RayQueryContext* context, const Vec3fa& ray_org, const unsigned int primID, const size_t itime) const
229
{
230
Vec3ff v0,v1,v2,v3; Vec3fa n0,n1,n2,n3;
231
unsigned int vertexID = curve(primID);
232
gather(v0,v1,v2,v3,n0,n1,n2,n3,vertexID,itime);
233
SourceCurve3ff ccurve(v0,v1,v2,v3);
234
SourceCurve3fa ncurve(n0,n1,n2,n3);
235
ccurve = enlargeRadiusToMinWidth(context,this,ray_org,ccurve);
236
return TensorLinearCubicBezierSurface3fa::fromCenterAndNormalCurve(ccurve,ncurve);
237
}
238
239
template<typename SourceCurve3ff, typename SourceCurve3fa, typename TensorLinearCubicBezierSurface3fa>
240
__forceinline TensorLinearCubicBezierSurface3fa getNormalOrientedCurve(RayQueryContext* context, const Vec3fa& ray_org, const unsigned int primID, const float time) const
241
{
242
float ftime;
243
const size_t itime = timeSegment(time, ftime);
244
const TensorLinearCubicBezierSurface3fa curve0 = getNormalOrientedCurve<SourceCurve3ff, SourceCurve3fa, TensorLinearCubicBezierSurface3fa>(context,ray_org,primID,itime+0);
245
const TensorLinearCubicBezierSurface3fa curve1 = getNormalOrientedCurve<SourceCurve3ff, SourceCurve3fa, TensorLinearCubicBezierSurface3fa>(context,ray_org,primID,itime+1);
246
return clerp(curve0,curve1,ftime);
247
}
248
249
template<typename SourceCurve3ff, typename SourceCurve3fa, typename TensorLinearCubicBezierSurface3fa>
250
__forceinline TensorLinearCubicBezierSurface3fa getNormalOrientedCurveSafe(RayQueryContext* context, const Vec3fa& ray_org, const unsigned int primID, const float time) const
251
{
252
float ftime = 0.0f;
253
const size_t itime = hasMotionBlur() ? timeSegment(time, ftime) : 0;
254
const TensorLinearCubicBezierSurface3fa curve0 = getNormalOrientedCurve<SourceCurve3ff, SourceCurve3fa, TensorLinearCubicBezierSurface3fa>(context,ray_org,primID,itime+0);
255
if (hasMotionBlur()) {
256
const TensorLinearCubicBezierSurface3fa curve1 = getNormalOrientedCurve<SourceCurve3ff, SourceCurve3fa, TensorLinearCubicBezierSurface3fa>(context,ray_org,primID,itime+1);
257
return clerp(curve0,curve1,ftime);
258
}
259
return curve0;
260
}
261
262
/*! gathers the hermite curve starting with i'th vertex */
263
__forceinline void gather_hermite(Vec3ff& p0, Vec3ff& t0, Vec3ff& p1, Vec3ff& t1, size_t i) const
264
{
265
p0 = vertex (i+0);
266
p1 = vertex (i+1);
267
t0 = tangent(i+0);
268
t1 = tangent(i+1);
269
}
270
271
/*! gathers the hermite curve starting with i'th vertex of itime'th timestep */
272
__forceinline void gather_hermite(Vec3ff& p0, Vec3ff& t0, Vec3ff& p1, Vec3ff& t1, size_t i, size_t itime) const
273
{
274
p0 = vertex (i+0,itime);
275
p1 = vertex (i+1,itime);
276
t0 = tangent(i+0,itime);
277
t1 = tangent(i+1,itime);
278
}
279
280
/*! loads curve vertices for specified time */
281
__forceinline void gather_hermite(Vec3ff& p0, Vec3ff& t0, Vec3ff& p1, Vec3ff& t1, size_t i, float time) const
282
{
283
float ftime;
284
const size_t itime = timeSegment(time, ftime);
285
const float f0 = 1.0f - ftime, f1 = ftime;
286
Vec3ff ap0,at0,ap1,at1;
287
gather_hermite(ap0,at0,ap1,at1,i,itime);
288
Vec3ff bp0,bt0,bp1,bt1;
289
gather_hermite(bp0,bt0,bp1,bt1,i,itime+1);
290
p0 = madd(Vec3ff(f0),ap0,f1*bp0);
291
t0 = madd(Vec3ff(f0),at0,f1*bt0);
292
p1 = madd(Vec3ff(f0),ap1,f1*bp1);
293
t1 = madd(Vec3ff(f0),at1,f1*bt1);
294
}
295
296
/*! loads curve vertices for specified time for mblur and non-mblur geometry */
297
__forceinline void gather_hermite_safe(Vec3ff& p0, Vec3ff& t0, Vec3ff& p1, Vec3ff& t1, size_t i, float time) const
298
{
299
if (hasMotionBlur()) gather_hermite(p0,t0,p1,t1,i,time);
300
else gather_hermite(p0,t0,p1,t1,i);
301
}
302
303
/*! gathers the hermite curve starting with i'th vertex */
304
__forceinline void gather_hermite(Vec3ff& p0, Vec3ff& t0, Vec3fa& n0, Vec3fa& dn0, Vec3ff& p1, Vec3ff& t1, Vec3fa& n1, Vec3fa& dn1, size_t i) const
305
{
306
p0 = vertex (i+0);
307
p1 = vertex (i+1);
308
t0 = tangent(i+0);
309
t1 = tangent(i+1);
310
n0 = normal(i+0);
311
n1 = normal(i+1);
312
dn0 = dnormal(i+0);
313
dn1 = dnormal(i+1);
314
}
315
316
/*! gathers the hermite curve starting with i'th vertex of itime'th timestep */
317
__forceinline void gather_hermite(Vec3ff& p0, Vec3ff& t0, Vec3fa& n0, Vec3fa& dn0, Vec3ff& p1, Vec3ff& t1, Vec3fa& n1, Vec3fa& dn1, size_t i, size_t itime) const
318
{
319
p0 = vertex (i+0,itime);
320
p1 = vertex (i+1,itime);
321
t0 = tangent(i+0,itime);
322
t1 = tangent(i+1,itime);
323
n0 = normal(i+0,itime);
324
n1 = normal(i+1,itime);
325
dn0 = dnormal(i+0,itime);
326
dn1 = dnormal(i+1,itime);
327
}
328
329
/*! loads curve vertices for specified time */
330
__forceinline void gather_hermite(Vec3ff& p0, Vec3ff& t0, Vec3fa& n0, Vec3fa& dn0, Vec3ff& p1, Vec3ff& t1, Vec3fa& n1, Vec3fa& dn1, size_t i, float time) const
331
{
332
float ftime;
333
const size_t itime = timeSegment(time, ftime);
334
const float f0 = 1.0f - ftime, f1 = ftime;
335
Vec3ff ap0,at0,ap1,at1; Vec3fa an0,adn0,an1,adn1;
336
gather_hermite(ap0,at0,an0,adn0,ap1,at1,an1,adn1,i,itime);
337
Vec3ff bp0,bt0,bp1,bt1; Vec3fa bn0,bdn0,bn1,bdn1;
338
gather_hermite(bp0,bt0,bn0,bdn0,bp1,bt1,bn1,bdn1,i,itime+1);
339
p0 = madd(Vec3ff(f0),ap0,f1*bp0);
340
t0 = madd(Vec3ff(f0),at0,f1*bt0);
341
n0 = madd(Vec3ff(f0),an0,f1*bn0);
342
dn0= madd(Vec3ff(f0),adn0,f1*bdn0);
343
p1 = madd(Vec3ff(f0),ap1,f1*bp1);
344
t1 = madd(Vec3ff(f0),at1,f1*bt1);
345
n1 = madd(Vec3ff(f0),an1,f1*bn1);
346
dn1= madd(Vec3ff(f0),adn1,f1*bdn1);
347
}
348
349
/*! loads curve vertices for specified time */
350
__forceinline void gather_hermite_safe(Vec3ff& p0, Vec3ff& t0, Vec3fa& n0, Vec3fa& dn0, Vec3ff& p1, Vec3ff& t1, Vec3fa& n1, Vec3fa& dn1, size_t i, float time) const
351
{
352
if (hasMotionBlur()) gather_hermite(p0,t0,n0,dn0,p1,t1,n1,dn1,i,time);
353
else gather_hermite(p0,t0,n0,dn0,p1,t1,n1,dn1,i);
354
}
355
356
template<typename SourceCurve3ff, typename SourceCurve3fa, typename TensorLinearCubicBezierSurface3fa>
357
__forceinline TensorLinearCubicBezierSurface3fa getNormalOrientedHermiteCurve(RayQueryContext* context, const Vec3fa& ray_org, const unsigned int primID, const size_t itime) const
358
{
359
Vec3ff v0,t0,v1,t1; Vec3fa n0,dn0,n1,dn1;
360
unsigned int vertexID = curve(primID);
361
gather_hermite(v0,t0,n0,dn0,v1,t1,n1,dn1,vertexID,itime);
362
363
SourceCurve3ff ccurve(v0,t0,v1,t1);
364
SourceCurve3fa ncurve(n0,dn0,n1,dn1);
365
ccurve = enlargeRadiusToMinWidth(context,this,ray_org,ccurve);
366
return TensorLinearCubicBezierSurface3fa::fromCenterAndNormalCurve(ccurve,ncurve);
367
}
368
369
template<typename SourceCurve3ff, typename SourceCurve3fa, typename TensorLinearCubicBezierSurface3fa>
370
__forceinline TensorLinearCubicBezierSurface3fa getNormalOrientedHermiteCurve(RayQueryContext* context, const Vec3fa& ray_org, const unsigned int primID, const float time) const
371
{
372
float ftime;
373
const size_t itime = timeSegment(time, ftime);
374
const TensorLinearCubicBezierSurface3fa curve0 = getNormalOrientedHermiteCurve<SourceCurve3ff, SourceCurve3fa, TensorLinearCubicBezierSurface3fa>(context, ray_org, primID,itime+0);
375
const TensorLinearCubicBezierSurface3fa curve1 = getNormalOrientedHermiteCurve<SourceCurve3ff, SourceCurve3fa, TensorLinearCubicBezierSurface3fa>(context, ray_org, primID,itime+1);
376
return clerp(curve0,curve1,ftime);
377
}
378
379
template<typename SourceCurve3ff, typename SourceCurve3fa, typename TensorLinearCubicBezierSurface3fa>
380
__forceinline TensorLinearCubicBezierSurface3fa getNormalOrientedHermiteCurveSafe(RayQueryContext* context, const Vec3fa& ray_org, const unsigned int primID, const float time) const
381
{
382
float ftime = 0.0f;
383
const size_t itime = hasMotionBlur() ? timeSegment(time, ftime) : 0;
384
const TensorLinearCubicBezierSurface3fa curve0 = getNormalOrientedHermiteCurve<SourceCurve3ff, SourceCurve3fa, TensorLinearCubicBezierSurface3fa>(context, ray_org, primID,itime+0);
385
if (hasMotionBlur()) {
386
const TensorLinearCubicBezierSurface3fa curve1 = getNormalOrientedHermiteCurve<SourceCurve3ff, SourceCurve3fa, TensorLinearCubicBezierSurface3fa>(context, ray_org, primID,itime+1);
387
return clerp(curve0,curve1,ftime);
388
}
389
return curve0;
390
}
391
392
/* returns the projected area */
393
__forceinline float projectedPrimitiveArea(const size_t i) const {
394
return 1.0f;
395
}
396
397
private:
398
void resizeBuffers(unsigned int numSteps);
399
400
public:
401
BufferView<unsigned int> curves; //!< array of curve indices
402
BufferView<Vec3ff> vertices0; //!< fast access to first vertex buffer
403
BufferView<Vec3fa> normals0; //!< fast access to first normal buffer
404
BufferView<Vec3ff> tangents0; //!< fast access to first tangent buffer
405
BufferView<Vec3fa> dnormals0; //!< fast access to first normal derivative buffer
406
Device::vector<BufferView<Vec3ff>> vertices = device; //!< vertex array for each timestep
407
Device::vector<BufferView<Vec3fa>> normals = device; //!< normal array for each timestep
408
Device::vector<BufferView<Vec3ff>> tangents = device; //!< tangent array for each timestep
409
Device::vector<BufferView<Vec3fa>> dnormals = device; //!< normal derivative array for each timestep
410
BufferView<char> flags; //!< start, end flag per segment
411
Device::vector<BufferView<char>> vertexAttribs = device; //!< user buffers
412
int tessellationRate; //!< tessellation rate for flat curve
413
float maxRadiusScale = 1.0; //!< maximal min-width scaling of curve radii
414
};
415
416
namespace isa
417
{
418
419
template<template<typename Ty> class Curve>
420
struct CurveGeometryInterface : public CurveGeometry
421
{
422
typedef Curve<Vec3ff> Curve3ff;
423
typedef Curve<Vec3fa> Curve3fa;
424
425
CurveGeometryInterface (Device* device, Geometry::GType gtype)
426
: CurveGeometry(device,gtype) {}
427
428
__forceinline const Curve3ff getCurveScaledRadius(size_t i, size_t itime = 0) const
429
{
430
const unsigned int index = curve(i);
431
Vec3ff v0 = vertex(index+0,itime);
432
Vec3ff v1 = vertex(index+1,itime);
433
Vec3ff v2 = vertex(index+2,itime);
434
Vec3ff v3 = vertex(index+3,itime);
435
v0.w *= maxRadiusScale;
436
v1.w *= maxRadiusScale;
437
v2.w *= maxRadiusScale;
438
v3.w *= maxRadiusScale;
439
return Curve3ff (v0,v1,v2,v3);
440
}
441
442
__forceinline const Curve3ff getCurveScaledRadius(const LinearSpace3fa& space, size_t i, size_t itime = 0) const
443
{
444
const unsigned int index = curve(i);
445
const Vec3ff v0 = vertex(index+0,itime);
446
const Vec3ff v1 = vertex(index+1,itime);
447
const Vec3ff v2 = vertex(index+2,itime);
448
const Vec3ff v3 = vertex(index+3,itime);
449
const Vec3ff w0(xfmPoint(space,(Vec3fa)v0), maxRadiusScale*v0.w);
450
const Vec3ff w1(xfmPoint(space,(Vec3fa)v1), maxRadiusScale*v1.w);
451
const Vec3ff w2(xfmPoint(space,(Vec3fa)v2), maxRadiusScale*v2.w);
452
const Vec3ff w3(xfmPoint(space,(Vec3fa)v3), maxRadiusScale*v3.w);
453
return Curve3ff(w0,w1,w2,w3);
454
}
455
456
__forceinline const Curve3ff getCurveScaledRadius(const Vec3fa& ofs, const float scale, const float r_scale0, const LinearSpace3fa& space, size_t i, size_t itime = 0) const
457
{
458
const float r_scale = r_scale0*scale;
459
const unsigned int index = curve(i);
460
const Vec3ff v0 = vertex(index+0,itime);
461
const Vec3ff v1 = vertex(index+1,itime);
462
const Vec3ff v2 = vertex(index+2,itime);
463
const Vec3ff v3 = vertex(index+3,itime);
464
const Vec3ff w0(xfmPoint(space,((Vec3fa)v0-ofs)*Vec3fa(scale)), maxRadiusScale*v0.w*r_scale);
465
const Vec3ff w1(xfmPoint(space,((Vec3fa)v1-ofs)*Vec3fa(scale)), maxRadiusScale*v1.w*r_scale);
466
const Vec3ff w2(xfmPoint(space,((Vec3fa)v2-ofs)*Vec3fa(scale)), maxRadiusScale*v2.w*r_scale);
467
const Vec3ff w3(xfmPoint(space,((Vec3fa)v3-ofs)*Vec3fa(scale)), maxRadiusScale*v3.w*r_scale);
468
return Curve3ff(w0,w1,w2,w3);
469
}
470
471
__forceinline const Curve3fa getNormalCurve(size_t i, size_t itime = 0) const
472
{
473
const unsigned int index = curve(i);
474
const Vec3fa n0 = normal(index+0,itime);
475
const Vec3fa n1 = normal(index+1,itime);
476
const Vec3fa n2 = normal(index+2,itime);
477
const Vec3fa n3 = normal(index+3,itime);
478
return Curve3fa (n0,n1,n2,n3);
479
}
480
481
__forceinline const TensorLinearCubicBezierSurface3fa getOrientedCurveScaledRadius(size_t i, size_t itime = 0) const
482
{
483
const Curve3ff center = getCurveScaledRadius(i,itime);
484
const Curve3fa normal = getNormalCurve(i,itime);
485
const TensorLinearCubicBezierSurface3fa ocurve = TensorLinearCubicBezierSurface3fa::fromCenterAndNormalCurve(center,normal);
486
return ocurve;
487
}
488
489
__forceinline const TensorLinearCubicBezierSurface3fa getOrientedCurveScaledRadius(const LinearSpace3fa& space, size_t i, size_t itime = 0) const {
490
return getOrientedCurveScaledRadius(i,itime).xfm(space);
491
}
492
493
__forceinline const TensorLinearCubicBezierSurface3fa getOrientedCurveScaledRadius(const Vec3fa& ofs, const float scale, const LinearSpace3fa& space, size_t i, size_t itime = 0) const {
494
return getOrientedCurveScaledRadius(i,itime).xfm(space,ofs,scale);
495
}
496
497
/*! check if the i'th primitive is valid at the itime'th time step */
498
__forceinline bool valid(Geometry::GType ctype, size_t i, const range<size_t>& itime_range) const
499
{
500
const unsigned int index = curve(i);
501
if (index+3 >= numVertices()) return false;
502
503
for (size_t itime = itime_range.begin(); itime <= itime_range.end(); itime++)
504
{
505
const float r0 = radius(index+0,itime);
506
const float r1 = radius(index+1,itime);
507
const float r2 = radius(index+2,itime);
508
const float r3 = radius(index+3,itime);
509
if (!isvalid(r0) || !isvalid(r1) || !isvalid(r2) || !isvalid(r3))
510
return false;
511
512
const Vec3fa v0 = vertex(index+0,itime);
513
const Vec3fa v1 = vertex(index+1,itime);
514
const Vec3fa v2 = vertex(index+2,itime);
515
const Vec3fa v3 = vertex(index+3,itime);
516
if (!isvalid(v0) || !isvalid(v1) || !isvalid(v2) || !isvalid(v3))
517
return false;
518
519
if (ctype == Geometry::GTY_SUBTYPE_ORIENTED_CURVE)
520
{
521
const Vec3fa n0 = normal(index+0,itime);
522
const Vec3fa n1 = normal(index+1,itime);
523
if (!isvalid(n0) || !isvalid(n1))
524
return false;
525
526
const BBox3fa b = getOrientedCurveScaledRadius(i,itime).accurateBounds();
527
if (!isvalid(b))
528
return false;
529
}
530
}
531
532
return true;
533
}
534
535
template<int N>
536
void interpolate_impl(const RTCInterpolateArguments* const args)
537
{
538
unsigned int primID = args->primID;
539
float u = args->u;
540
RTCBufferType bufferType = args->bufferType;
541
unsigned int bufferSlot = args->bufferSlot;
542
float* P = args->P;
543
float* dPdu = args->dPdu;
544
float* ddPdudu = args->ddPdudu;
545
unsigned int valueCount = args->valueCount;
546
547
/* calculate base pointer and stride */
548
assert((bufferType == RTC_BUFFER_TYPE_VERTEX && bufferSlot < numTimeSteps) ||
549
(bufferType == RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE && bufferSlot <= vertexAttribs.size()));
550
const char* src = nullptr;
551
size_t stride = 0;
552
if (bufferType == RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE) {
553
src = vertexAttribs[bufferSlot].getPtr();
554
stride = vertexAttribs[bufferSlot].getStride();
555
} else {
556
src = vertices[bufferSlot].getPtr();
557
stride = vertices[bufferSlot].getStride();
558
}
559
560
for (unsigned int i=0; i<valueCount; i+=N)
561
{
562
size_t ofs = i*sizeof(float);
563
const size_t index = curves[primID];
564
const vbool<N> valid = vint<N>((int)i)+vint<N>(step) < vint<N>((int)valueCount);
565
const vfloat<N> p0 = mem<vfloat<N>>::loadu(valid,(float*)&src[(index+0)*stride+ofs]);
566
const vfloat<N> p1 = mem<vfloat<N>>::loadu(valid,(float*)&src[(index+1)*stride+ofs]);
567
const vfloat<N> p2 = mem<vfloat<N>>::loadu(valid,(float*)&src[(index+2)*stride+ofs]);
568
const vfloat<N> p3 = mem<vfloat<N>>::loadu(valid,(float*)&src[(index+3)*stride+ofs]);
569
570
const Curve<vfloat<N>> curve(p0,p1,p2,p3);
571
if (P ) mem<vfloat<N>>::storeu(valid,P+i, curve.eval(u));
572
if (dPdu ) mem<vfloat<N>>::storeu(valid,dPdu+i, curve.eval_du(u));
573
if (ddPdudu) mem<vfloat<N>>::storeu(valid,ddPdudu+i,curve.eval_dudu(u));
574
}
575
}
576
577
void interpolate(const RTCInterpolateArguments* const args) {
578
interpolate_impl<4>(args);
579
}
580
};
581
582
template<template<typename Ty> class Curve>
583
struct HermiteCurveGeometryInterface : public CurveGeometry
584
{
585
typedef Curve<Vec3ff> HermiteCurve3ff;
586
typedef Curve<Vec3fa> HermiteCurve3fa;
587
588
HermiteCurveGeometryInterface (Device* device, Geometry::GType gtype)
589
: CurveGeometry(device,gtype) {}
590
591
__forceinline const HermiteCurve3ff getCurveScaledRadius(size_t i, size_t itime = 0) const
592
{
593
const unsigned int index = curve(i);
594
Vec3ff v0 = vertex(index+0,itime);
595
Vec3ff v1 = vertex(index+1,itime);
596
Vec3ff t0 = tangent(index+0,itime);
597
Vec3ff t1 = tangent(index+1,itime);
598
v0.w *= maxRadiusScale;
599
v1.w *= maxRadiusScale;
600
t0.w *= maxRadiusScale;
601
t1.w *= maxRadiusScale;
602
return HermiteCurve3ff (v0,t0,v1,t1);
603
}
604
605
__forceinline const HermiteCurve3ff getCurveScaledRadius(const LinearSpace3fa& space, size_t i, size_t itime = 0) const
606
{
607
const unsigned int index = curve(i);
608
const Vec3ff v0 = vertex(index+0,itime);
609
const Vec3ff v1 = vertex(index+1,itime);
610
const Vec3ff t0 = tangent(index+0,itime);
611
const Vec3ff t1 = tangent(index+1,itime);
612
const Vec3ff V0(xfmPoint(space,(Vec3fa)v0),maxRadiusScale*v0.w);
613
const Vec3ff V1(xfmPoint(space,(Vec3fa)v1),maxRadiusScale*v1.w);
614
const Vec3ff T0(xfmVector(space,(Vec3fa)t0),maxRadiusScale*t0.w);
615
const Vec3ff T1(xfmVector(space,(Vec3fa)t1),maxRadiusScale*t1.w);
616
return HermiteCurve3ff(V0,T0,V1,T1);
617
}
618
619
__forceinline const HermiteCurve3ff getCurveScaledRadius(const Vec3fa& ofs, const float scale, const float r_scale0, const LinearSpace3fa& space, size_t i, size_t itime = 0) const
620
{
621
const float r_scale = r_scale0*scale;
622
const unsigned int index = curve(i);
623
const Vec3ff v0 = vertex(index+0,itime);
624
const Vec3ff v1 = vertex(index+1,itime);
625
const Vec3ff t0 = tangent(index+0,itime);
626
const Vec3ff t1 = tangent(index+1,itime);
627
const Vec3ff V0(xfmPoint(space,(v0-ofs)*Vec3fa(scale)), maxRadiusScale*v0.w*r_scale);
628
const Vec3ff V1(xfmPoint(space,(v1-ofs)*Vec3fa(scale)), maxRadiusScale*v1.w*r_scale);
629
const Vec3ff T0(xfmVector(space,t0*Vec3fa(scale)), maxRadiusScale*t0.w*r_scale);
630
const Vec3ff T1(xfmVector(space,t1*Vec3fa(scale)), maxRadiusScale*t1.w*r_scale);
631
return HermiteCurve3ff(V0,T0,V1,T1);
632
}
633
634
__forceinline const HermiteCurve3fa getNormalCurve(size_t i, size_t itime = 0) const
635
{
636
const unsigned int index = curve(i);
637
const Vec3fa n0 = normal(index+0,itime);
638
const Vec3fa n1 = normal(index+1,itime);
639
const Vec3fa dn0 = dnormal(index+0,itime);
640
const Vec3fa dn1 = dnormal(index+1,itime);
641
return HermiteCurve3fa (n0,dn0,n1,dn1);
642
}
643
644
__forceinline const TensorLinearCubicBezierSurface3fa getOrientedCurveScaledRadius(size_t i, size_t itime = 0) const
645
{
646
const HermiteCurve3ff center = getCurveScaledRadius(i,itime);
647
const HermiteCurve3fa normal = getNormalCurve(i,itime);
648
const TensorLinearCubicBezierSurface3fa ocurve = TensorLinearCubicBezierSurface3fa::fromCenterAndNormalCurve(center,normal);
649
return ocurve;
650
}
651
652
__forceinline const TensorLinearCubicBezierSurface3fa getOrientedCurveScaledRadius(const LinearSpace3fa& space, size_t i, size_t itime = 0) const {
653
return getOrientedCurveScaledRadius(i,itime).xfm(space);
654
}
655
656
__forceinline const TensorLinearCubicBezierSurface3fa getOrientedCurveScaledRadius(const Vec3fa& ofs, const float scale, const LinearSpace3fa& space, size_t i, size_t itime = 0) const {
657
return getOrientedCurveScaledRadius(i,itime).xfm(space,ofs,scale);
658
}
659
660
/*! check if the i'th primitive is valid at the itime'th time step */
661
__forceinline bool valid(Geometry::GType ctype, size_t i, const range<size_t>& itime_range) const
662
{
663
const unsigned int index = curve(i);
664
if (index+1 >= numVertices()) return false;
665
666
for (size_t itime = itime_range.begin(); itime <= itime_range.end(); itime++)
667
{
668
const Vec3ff v0 = vertex(index+0,itime);
669
const Vec3ff v1 = vertex(index+1,itime);
670
if (!isvalid4(v0) || !isvalid4(v1))
671
return false;
672
673
const Vec3ff t0 = tangent(index+0,itime);
674
const Vec3ff t1 = tangent(index+1,itime);
675
if (!isvalid4(t0) || !isvalid4(t1))
676
return false;
677
678
if (ctype == Geometry::GTY_SUBTYPE_ORIENTED_CURVE)
679
{
680
const Vec3fa n0 = normal(index+0,itime);
681
const Vec3fa n1 = normal(index+1,itime);
682
if (!isvalid(n0) || !isvalid(n1))
683
return false;
684
685
const Vec3fa dn0 = dnormal(index+0,itime);
686
const Vec3fa dn1 = dnormal(index+1,itime);
687
if (!isvalid(dn0) || !isvalid(dn1))
688
return false;
689
690
const BBox3fa b = getOrientedCurveScaledRadius(i,itime).accurateBounds();
691
if (!isvalid(b))
692
return false;
693
}
694
}
695
696
return true;
697
}
698
699
template<int N>
700
void interpolate_impl(const RTCInterpolateArguments* const args)
701
{
702
unsigned int primID = args->primID;
703
float u = args->u;
704
RTCBufferType bufferType = args->bufferType;
705
unsigned int bufferSlot = args->bufferSlot;
706
float* P = args->P;
707
float* dPdu = args->dPdu;
708
float* ddPdudu = args->ddPdudu;
709
unsigned int valueCount = args->valueCount;
710
711
/* we interpolate vertex attributes linearly for hermite basis */
712
if (bufferType == RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE)
713
{
714
assert(bufferSlot <= vertexAttribs.size());
715
const char* vsrc = vertexAttribs[bufferSlot].getPtr();
716
const size_t vstride = vertexAttribs[bufferSlot].getStride();
717
718
for (unsigned int i=0; i<valueCount; i+=N)
719
{
720
const size_t ofs = i*sizeof(float);
721
const size_t index = curves[primID];
722
const vbool<N> valid = vint<N>((int)i)+vint<N>(step) < vint<N>((int)valueCount);
723
const vfloat<N> p0 = mem<vfloat<N>>::loadu(valid,(float*)&vsrc[(index+0)*vstride+ofs]);
724
const vfloat<N> p1 = mem<vfloat<N>>::loadu(valid,(float*)&vsrc[(index+1)*vstride+ofs]);
725
726
if (P ) mem<vfloat<N>>::storeu(valid,P+i, madd(1.0f-u,p0,u*p1));
727
if (dPdu ) mem<vfloat<N>>::storeu(valid,dPdu+i, p1-p0);
728
if (ddPdudu) mem<vfloat<N>>::storeu(valid,ddPdudu+i,vfloat<N>(zero));
729
}
730
}
731
732
/* interpolation for vertex buffers */
733
else
734
{
735
assert(bufferSlot < numTimeSteps);
736
const char* vsrc = vertices[bufferSlot].getPtr();
737
const char* tsrc = tangents[bufferSlot].getPtr();
738
const size_t vstride = vertices[bufferSlot].getStride();
739
const size_t tstride = vertices[bufferSlot].getStride();
740
741
for (unsigned int i=0; i<valueCount; i+=N)
742
{
743
const size_t ofs = i*sizeof(float);
744
const size_t index = curves[primID];
745
const vbool<N> valid = vint<N>((int)i)+vint<N>(step) < vint<N>((int)valueCount);
746
const vfloat<N> p0 = mem<vfloat<N>>::loadu(valid,(float*)&vsrc[(index+0)*vstride+ofs]);
747
const vfloat<N> p1 = mem<vfloat<N>>::loadu(valid,(float*)&vsrc[(index+1)*vstride+ofs]);
748
const vfloat<N> t0 = mem<vfloat<N>>::loadu(valid,(float*)&tsrc[(index+0)*tstride+ofs]);
749
const vfloat<N> t1 = mem<vfloat<N>>::loadu(valid,(float*)&tsrc[(index+1)*tstride+ofs]);
750
751
const HermiteCurveT<vfloat<N>> curve(p0,t0,p1,t1);
752
if (P ) mem<vfloat<N>>::storeu(valid,P+i, curve.eval(u));
753
if (dPdu ) mem<vfloat<N>>::storeu(valid,dPdu+i, curve.eval_du(u));
754
if (ddPdudu) mem<vfloat<N>>::storeu(valid,ddPdudu+i,curve.eval_dudu(u));
755
}
756
}
757
}
758
759
void interpolate(const RTCInterpolateArguments* const args) {
760
interpolate_impl<4>(args);
761
}
762
};
763
}
764
765
DECLARE_ISA_FUNCTION(CurveGeometry*, createCurves, Device* COMMA Geometry::GType);
766
}
767
768