Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/embree/kernels/subdiv/bezier_curve.h
9913 views
1
// Copyright 2009-2021 Intel Corporation
2
// SPDX-License-Identifier: Apache-2.0
3
4
#pragma once
5
6
#include "../common/default.h"
7
//#include "../common/scene_curves.h"
8
#include "../common/context.h"
9
10
namespace embree
11
{
12
class BezierBasis
13
{
14
public:
15
16
template<typename T>
17
static __forceinline Vec4<T> eval(const T& u)
18
{
19
const T t1 = u;
20
const T t0 = 1.0f-t1;
21
const T B0 = t0 * t0 * t0;
22
const T B1 = 3.0f * t1 * (t0 * t0);
23
const T B2 = 3.0f * (t1 * t1) * t0;
24
const T B3 = t1 * t1 * t1;
25
return Vec4<T>(B0,B1,B2,B3);
26
}
27
28
template<typename T>
29
static __forceinline Vec4<T> derivative(const T& u)
30
{
31
const T t1 = u;
32
const T t0 = 1.0f-t1;
33
const T B0 = -(t0*t0);
34
const T B1 = madd(-2.0f,t0*t1,t0*t0);
35
const T B2 = msub(+2.0f,t0*t1,t1*t1);
36
const T B3 = +(t1*t1);
37
return T(3.0f)*Vec4<T>(B0,B1,B2,B3);
38
}
39
40
template<typename T>
41
static __forceinline Vec4<T> derivative2(const T& u)
42
{
43
const T t1 = u;
44
const T t0 = 1.0f-t1;
45
const T B0 = t0;
46
const T B1 = madd(-2.0f,t0,t1);
47
const T B2 = madd(-2.0f,t1,t0);
48
const T B3 = t1;
49
return T(6.0f)*Vec4<T>(B0,B1,B2,B3);
50
}
51
};
52
53
struct PrecomputedBezierBasis
54
{
55
enum { N = 16 };
56
public:
57
PrecomputedBezierBasis() {}
58
PrecomputedBezierBasis(int shift);
59
60
/* basis for bezier evaluation */
61
public:
62
float c0[N+1][N+1];
63
float c1[N+1][N+1];
64
float c2[N+1][N+1];
65
float c3[N+1][N+1];
66
67
/* basis for bezier derivative evaluation */
68
public:
69
float d0[N+1][N+1];
70
float d1[N+1][N+1];
71
float d2[N+1][N+1];
72
float d3[N+1][N+1];
73
};
74
extern PrecomputedBezierBasis bezier_basis0;
75
extern PrecomputedBezierBasis bezier_basis1;
76
77
78
template<typename V>
79
struct LinearBezierCurve
80
{
81
V v0,v1;
82
83
__forceinline LinearBezierCurve () {}
84
85
__forceinline LinearBezierCurve (const LinearBezierCurve& other)
86
: v0(other.v0), v1(other.v1) {}
87
88
__forceinline LinearBezierCurve& operator= (const LinearBezierCurve& other) {
89
v0 = other.v0; v1 = other.v1; return *this;
90
}
91
92
__forceinline LinearBezierCurve (const V& v0, const V& v1)
93
: v0(v0), v1(v1) {}
94
95
__forceinline V begin() const { return v0; }
96
__forceinline V end () const { return v1; }
97
98
bool hasRoot() const;
99
100
friend embree_ostream operator<<(embree_ostream cout, const LinearBezierCurve& a) {
101
return cout << "LinearBezierCurve (" << a.v0 << ", " << a.v1 << ")";
102
}
103
};
104
105
template<> __forceinline bool LinearBezierCurve<Interval1f>::hasRoot() const {
106
return numRoots(v0,v1);
107
}
108
109
template<typename V>
110
struct QuadraticBezierCurve
111
{
112
V v0,v1,v2;
113
114
__forceinline QuadraticBezierCurve () {}
115
116
__forceinline QuadraticBezierCurve (const QuadraticBezierCurve& other)
117
: v0(other.v0), v1(other.v1), v2(other.v2) {}
118
119
__forceinline QuadraticBezierCurve& operator= (const QuadraticBezierCurve& other) {
120
v0 = other.v0; v1 = other.v1; v2 = other.v2; return *this;
121
}
122
123
__forceinline QuadraticBezierCurve (const V& v0, const V& v1, const V& v2)
124
: v0(v0), v1(v1), v2(v2) {}
125
126
__forceinline V begin() const { return v0; }
127
__forceinline V end () const { return v2; }
128
129
__forceinline V interval() const {
130
return merge(v0,v1,v2);
131
}
132
133
__forceinline BBox<V> bounds() const {
134
return merge(BBox<V>(v0),BBox<V>(v1),BBox<V>(v2));
135
}
136
137
friend embree_ostream operator<<(embree_ostream cout, const QuadraticBezierCurve& a) {
138
return cout << "QuadraticBezierCurve (" << a.v0 << ", " << a.v1 << ", " << a.v2 << ")";
139
}
140
};
141
142
143
typedef QuadraticBezierCurve<float> QuadraticBezierCurve1f;
144
typedef QuadraticBezierCurve<Vec2fa> QuadraticBezierCurve2fa;
145
typedef QuadraticBezierCurve<Vec3fa> QuadraticBezierCurve3fa;
146
147
template<typename Vertex>
148
struct CubicBezierCurve
149
{
150
Vertex v0,v1,v2,v3;
151
152
__forceinline CubicBezierCurve() {}
153
154
template<typename T1>
155
__forceinline CubicBezierCurve (const CubicBezierCurve<T1>& other)
156
: v0(other.v0), v1(other.v1), v2(other.v2), v3(other.v3) {}
157
158
__forceinline CubicBezierCurve& operator= (const CubicBezierCurve& other) {
159
v0 = other.v0; v1 = other.v1; v2 = other.v2; v3 = other.v3; return *this;
160
}
161
162
__forceinline CubicBezierCurve(const Vertex& v0, const Vertex& v1, const Vertex& v2, const Vertex& v3)
163
: v0(v0), v1(v1), v2(v2), v3(v3) {}
164
165
__forceinline Vertex begin() const {
166
return v0;
167
}
168
169
__forceinline Vertex end() const {
170
return v3;
171
}
172
173
__forceinline Vertex center() const {
174
return 0.25f*(v0+v1+v2+v3);
175
}
176
177
__forceinline Vertex begin_direction() const {
178
return v1-v0;
179
}
180
181
__forceinline Vertex end_direction() const {
182
return v3-v2;
183
}
184
185
__forceinline CubicBezierCurve<float> xfm(const Vertex& dx) const {
186
return CubicBezierCurve<float>(dot(v0,dx),dot(v1,dx),dot(v2,dx),dot(v3,dx));
187
}
188
189
template<int W>
190
__forceinline CubicBezierCurve<vfloat<W>> vxfm(const Vertex& dx) const {
191
return CubicBezierCurve<vfloat<W>>(dot(v0,dx),dot(v1,dx),dot(v2,dx),dot(v3,dx));
192
}
193
194
__forceinline CubicBezierCurve<float> xfm(const Vertex& dx, const Vertex& p) const {
195
return CubicBezierCurve<float>(dot(v0-p,dx),dot(v1-p,dx),dot(v2-p,dx),dot(v3-p,dx));
196
}
197
198
__forceinline CubicBezierCurve<Vec3fa> xfm(const LinearSpace3fa& space) const
199
{
200
const Vec3fa q0 = xfmVector(space,v0);
201
const Vec3fa q1 = xfmVector(space,v1);
202
const Vec3fa q2 = xfmVector(space,v2);
203
const Vec3fa q3 = xfmVector(space,v3);
204
return CubicBezierCurve<Vec3fa>(q0,q1,q2,q3);
205
}
206
207
__forceinline CubicBezierCurve<Vec3fa> xfm(const LinearSpace3fa& space, const Vec3fa& p) const
208
{
209
const Vec3fa q0 = xfmVector(space,v0-p);
210
const Vec3fa q1 = xfmVector(space,v1-p);
211
const Vec3fa q2 = xfmVector(space,v2-p);
212
const Vec3fa q3 = xfmVector(space,v3-p);
213
return CubicBezierCurve<Vec3fa>(q0,q1,q2,q3);
214
}
215
216
__forceinline CubicBezierCurve<Vec3ff> xfm_pr(const LinearSpace3fa& space, const Vec3fa& p) const
217
{
218
const Vec3ff q0(xfmVector(space,(Vec3fa)v0-p), v0.w);
219
const Vec3ff q1(xfmVector(space,(Vec3fa)v1-p), v1.w);
220
const Vec3ff q2(xfmVector(space,(Vec3fa)v2-p), v2.w);
221
const Vec3ff q3(xfmVector(space,(Vec3fa)v3-p), v3.w);
222
return CubicBezierCurve<Vec3ff>(q0,q1,q2,q3);
223
}
224
225
__forceinline CubicBezierCurve<Vec3fa> xfm(const LinearSpace3fa& space, const Vec3fa& p, const float s) const
226
{
227
const Vec3fa q0 = xfmVector(space,s*(v0-p));
228
const Vec3fa q1 = xfmVector(space,s*(v1-p));
229
const Vec3fa q2 = xfmVector(space,s*(v2-p));
230
const Vec3fa q3 = xfmVector(space,s*(v3-p));
231
return CubicBezierCurve<Vec3fa>(q0,q1,q2,q3);
232
}
233
234
__forceinline int maxRoots() const;
235
236
__forceinline BBox<Vertex> bounds() const {
237
return merge(BBox<Vertex>(v0),BBox<Vertex>(v1),BBox<Vertex>(v2),BBox<Vertex>(v3));
238
}
239
240
__forceinline friend CubicBezierCurve operator +( const CubicBezierCurve& a, const CubicBezierCurve& b ) {
241
return CubicBezierCurve(a.v0+b.v0,a.v1+b.v1,a.v2+b.v2,a.v3+b.v3);
242
}
243
244
__forceinline friend CubicBezierCurve operator -( const CubicBezierCurve& a, const CubicBezierCurve& b ) {
245
return CubicBezierCurve(a.v0-b.v0,a.v1-b.v1,a.v2-b.v2,a.v3-b.v3);
246
}
247
248
__forceinline friend CubicBezierCurve operator -( const CubicBezierCurve& a, const Vertex& b ) {
249
return CubicBezierCurve(a.v0-b,a.v1-b,a.v2-b,a.v3-b);
250
}
251
252
__forceinline friend CubicBezierCurve operator *( const Vertex& a, const CubicBezierCurve& b ) {
253
return CubicBezierCurve(a*b.v0,a*b.v1,a*b.v2,a*b.v3);
254
}
255
256
__forceinline friend CubicBezierCurve cmadd( const Vertex& a, const CubicBezierCurve& b, const CubicBezierCurve& c) {
257
return CubicBezierCurve(madd(a,b.v0,c.v0),madd(a,b.v1,c.v1),madd(a,b.v2,c.v2),madd(a,b.v3,c.v3));
258
}
259
260
__forceinline friend CubicBezierCurve clerp ( const CubicBezierCurve& a, const CubicBezierCurve& b, const Vertex& t ) {
261
return cmadd((Vertex(1.0f)-t),a,t*b);
262
}
263
264
__forceinline friend CubicBezierCurve merge ( const CubicBezierCurve& a, const CubicBezierCurve& b ) {
265
return CubicBezierCurve(merge(a.v0,b.v0),merge(a.v1,b.v1),merge(a.v2,b.v2),merge(a.v3,b.v3));
266
}
267
268
__forceinline void split(CubicBezierCurve& left, CubicBezierCurve& right, const float t = 0.5f) const
269
{
270
const Vertex p00 = v0;
271
const Vertex p01 = v1;
272
const Vertex p02 = v2;
273
const Vertex p03 = v3;
274
275
const Vertex p10 = lerp(p00,p01,t);
276
const Vertex p11 = lerp(p01,p02,t);
277
const Vertex p12 = lerp(p02,p03,t);
278
const Vertex p20 = lerp(p10,p11,t);
279
const Vertex p21 = lerp(p11,p12,t);
280
const Vertex p30 = lerp(p20,p21,t);
281
282
new (&left ) CubicBezierCurve(p00,p10,p20,p30);
283
new (&right) CubicBezierCurve(p30,p21,p12,p03);
284
}
285
286
__forceinline CubicBezierCurve<Vec2vfx> split() const
287
{
288
const float u0 = 0.0f, u1 = 1.0f;
289
const float dscale = (u1-u0)*(1.0f/(3.0f*(VSIZEX-1)));
290
const vfloatx vu0 = lerp(u0,u1,vfloatx(StepTy())*(1.0f/(VSIZEX-1)));
291
Vec2vfx P0, dP0du; evalN(vu0,P0,dP0du); dP0du = dP0du * Vec2vfx(dscale);
292
const Vec2vfx P3 = shift_right_1(P0);
293
const Vec2vfx dP3du = shift_right_1(dP0du);
294
const Vec2vfx P1 = P0 + dP0du;
295
const Vec2vfx P2 = P3 - dP3du;
296
return CubicBezierCurve<Vec2vfx>(P0,P1,P2,P3);
297
}
298
299
__forceinline CubicBezierCurve<Vec2vfx> split(const BBox1f& u) const
300
{
301
const float u0 = u.lower, u1 = u.upper;
302
const float dscale = (u1-u0)*(1.0f/(3.0f*(VSIZEX-1)));
303
const vfloatx vu0 = lerp(u0,u1,vfloatx(StepTy())*(1.0f/(VSIZEX-1)));
304
Vec2vfx P0, dP0du; evalN(vu0,P0,dP0du); dP0du = dP0du * Vec2vfx(dscale);
305
const Vec2vfx P3 = shift_right_1(P0);
306
const Vec2vfx dP3du = shift_right_1(dP0du);
307
const Vec2vfx P1 = P0 + dP0du;
308
const Vec2vfx P2 = P3 - dP3du;
309
return CubicBezierCurve<Vec2vfx>(P0,P1,P2,P3);
310
}
311
312
template<int W>
313
__forceinline CubicBezierCurve<Vec2vf<W>> split(const BBox1f& u, int i, int N) const
314
{
315
const float u0 = u.lower, u1 = u.upper;
316
const float dscale = (u1-u0)*(1.0f/(3.0f*N));
317
const vfloat<W> vu0 = lerp(u0,u1,(vfloat<W>(i)+vfloat<W>(StepTy()))*(1.0f/N));
318
Vec2vf<W> P0, dP0du; evalN(vu0,P0,dP0du); dP0du = dP0du * Vec2vf<W>(dscale);
319
const Vec2vf<W> P3 = shift_right_1(P0);
320
const Vec2vf<W> dP3du = shift_right_1(dP0du);
321
const Vec2vf<W> P1 = P0 + dP0du;
322
const Vec2vf<W> P2 = P3 - dP3du;
323
return CubicBezierCurve<Vec2vf<W>>(P0,P1,P2,P3);
324
}
325
326
__forceinline CubicBezierCurve<Vec2f> split1(const BBox1f& u, int i, int N) const
327
{
328
const float u0 = u.lower, u1 = u.upper;
329
const float dscale = (u1-u0)*(1.0f/(3.0f*N));
330
const float vu0 = lerp(u0,u1,(float(i)+0)*(1.0f/N));
331
const float vu1 = lerp(u0,u1,(float(i)+1)*(1.0f/N));
332
Vec2fa P0, dP0du; eval(vu0,P0,dP0du); dP0du = dP0du * Vec2fa(dscale);
333
Vec2fa P3, dP3du; eval(vu1,P3,dP3du); dP3du = dP3du * Vec2fa(dscale);
334
const Vec2fa P1 = P0 + dP0du;
335
const Vec2fa P2 = P3 - dP3du;
336
return CubicBezierCurve<Vec2f>(P0,P1,P2,P3);
337
}
338
339
__forceinline void eval(float t, Vertex& p, Vertex& dp) const
340
{
341
const Vertex p00 = v0;
342
const Vertex p01 = v1;
343
const Vertex p02 = v2;
344
const Vertex p03 = v3;
345
346
const Vertex p10 = lerp(p00,p01,t);
347
const Vertex p11 = lerp(p01,p02,t);
348
const Vertex p12 = lerp(p02,p03,t);
349
const Vertex p20 = lerp(p10,p11,t);
350
const Vertex p21 = lerp(p11,p12,t);
351
const Vertex p30 = lerp(p20,p21,t);
352
353
p = p30;
354
dp = Vertex(3.0f)*(p21-p20);
355
}
356
357
#if 0
358
__forceinline Vertex eval(float t) const
359
{
360
const Vertex p00 = v0;
361
const Vertex p01 = v1;
362
const Vertex p02 = v2;
363
const Vertex p03 = v3;
364
365
const Vertex p10 = lerp(p00,p01,t);
366
const Vertex p11 = lerp(p01,p02,t);
367
const Vertex p12 = lerp(p02,p03,t);
368
const Vertex p20 = lerp(p10,p11,t);
369
const Vertex p21 = lerp(p11,p12,t);
370
const Vertex p30 = lerp(p20,p21,t);
371
372
return p30;
373
}
374
#else
375
__forceinline Vertex eval(const float t) const
376
{
377
const Vec4<float> b = BezierBasis::eval(t);
378
return madd(b.x,v0,madd(b.y,v1,madd(b.z,v2,b.w*v3)));
379
}
380
#endif
381
382
__forceinline Vertex eval_dt(float t) const
383
{
384
const Vertex p00 = v1-v0;
385
const Vertex p01 = v2-v1;
386
const Vertex p02 = v3-v2;
387
const Vertex p10 = lerp(p00,p01,t);
388
const Vertex p11 = lerp(p01,p02,t);
389
const Vertex p20 = lerp(p10,p11,t);
390
return Vertex(3.0f)*p20;
391
}
392
393
__forceinline Vertex eval_du(const float t) const
394
{
395
const Vec4<float> b = BezierBasis::derivative(t);
396
return madd(b.x,v0,madd(b.y,v1,madd(b.z,v2,b.w*v3)));
397
}
398
399
__forceinline Vertex eval_dudu(const float t) const
400
{
401
const Vec4<float> b = BezierBasis::derivative2(t);
402
return madd(b.x,v0,madd(b.y,v1,madd(b.z,v2,b.w*v3)));
403
}
404
405
__forceinline void evalN(const vfloatx& t, Vec2vfx& p, Vec2vfx& dp) const
406
{
407
const Vec2vfx p00 = v0;
408
const Vec2vfx p01 = v1;
409
const Vec2vfx p02 = v2;
410
const Vec2vfx p03 = v3;
411
412
const Vec2vfx p10 = lerp(p00,p01,t);
413
const Vec2vfx p11 = lerp(p01,p02,t);
414
const Vec2vfx p12 = lerp(p02,p03,t);
415
416
const Vec2vfx p20 = lerp(p10,p11,t);
417
const Vec2vfx p21 = lerp(p11,p12,t);
418
419
const Vec2vfx p30 = lerp(p20,p21,t);
420
421
p = p30;
422
dp = vfloatx(3.0f)*(p21-p20);
423
}
424
425
__forceinline void eval(const float t, Vertex& p, Vertex& dp, Vertex& ddp) const
426
{
427
const Vertex p00 = v0;
428
const Vertex p01 = v1;
429
const Vertex p02 = v2;
430
const Vertex p03 = v3;
431
const Vertex p10 = lerp(p00,p01,t);
432
const Vertex p11 = lerp(p01,p02,t);
433
const Vertex p12 = lerp(p02,p03,t);
434
const Vertex p20 = lerp(p10,p11,t);
435
const Vertex p21 = lerp(p11,p12,t);
436
const Vertex p30 = lerp(p20,p21,t);
437
p = p30;
438
dp = 3.0f*(p21-p20);
439
ddp = eval_dudu(t);
440
}
441
442
__forceinline CubicBezierCurve clip(const Interval1f& u1) const
443
{
444
Vertex f0,df0; eval(u1.lower,f0,df0);
445
Vertex f1,df1; eval(u1.upper,f1,df1);
446
float s = u1.upper-u1.lower;
447
return CubicBezierCurve(f0,f0+s*(1.0f/3.0f)*df0,f1-s*(1.0f/3.0f)*df1,f1);
448
}
449
450
__forceinline QuadraticBezierCurve<Vertex> derivative() const
451
{
452
const Vertex q0 = 3.0f*(v1-v0);
453
const Vertex q1 = 3.0f*(v2-v1);
454
const Vertex q2 = 3.0f*(v3-v2);
455
return QuadraticBezierCurve<Vertex>(q0,q1,q2);
456
}
457
458
__forceinline BBox<Vertex> derivative_bounds(const Interval1f& u1) const
459
{
460
Vertex f0,df0; eval(u1.lower,f0,df0);
461
Vertex f3,df3; eval(u1.upper,f3,df3);
462
const float s = u1.upper-u1.lower;
463
const Vertex f1 = f0+s*(1.0f/3.0f)*df0;
464
const Vertex f2 = f3-s*(1.0f/3.0f)*df3;
465
const Vertex q0 = s*df0;
466
const Vertex q1 = 3.0f*(f2-f1);
467
const Vertex q2 = s*df3;
468
return merge(BBox<Vertex>(q0),BBox<Vertex>(q1),BBox<Vertex>(q2));
469
}
470
471
template<int M>
472
__forceinline Vec4vf<M> veval(const vfloat<M>& t) const
473
{
474
const Vec4vf<M> b = BezierBasis::eval(t);
475
return madd(b.x, Vec4vf<M>(v0), madd(b.y, Vec4vf<M>(v1), madd(b.z, Vec4vf<M>(v2), b.w * Vec4vf<M>(v3))));
476
}
477
478
template<int M>
479
__forceinline Vec4vf<M> veval_du(const vfloat<M>& t) const
480
{
481
const Vec4vf<M> b = BezierBasis::derivative(t);
482
return madd(b.x, Vec4vf<M>(v0), madd(b.y, Vec4vf<M>(v1), madd(b.z, Vec4vf<M>(v2), b.w * Vec4vf<M>(v3))));
483
}
484
485
template<int M>
486
__forceinline Vec4vf<M> veval_dudu(const vfloat<M>& t) const
487
{
488
const Vec4vf<M> b = BezierBasis::derivative2(t);
489
return madd(b.x, Vec4vf<M>(v0), madd(b.y, Vec4vf<M>(v1), madd(b.z, Vec4vf<M>(v2), b.w * Vec4vf<M>(v3))));
490
}
491
492
template<int M, typename Vec>
493
__forceinline void veval(const vfloat<M>& t, Vec& p, Vec& dp) const
494
{
495
const Vec p00 = v0;
496
const Vec p01 = v1;
497
const Vec p02 = v2;
498
const Vec p03 = v3;
499
500
const Vec p10 = lerp(p00,p01,t);
501
const Vec p11 = lerp(p01,p02,t);
502
const Vec p12 = lerp(p02,p03,t);
503
const Vec p20 = lerp(p10,p11,t);
504
const Vec p21 = lerp(p11,p12,t);
505
const Vec p30 = lerp(p20,p21,t);
506
507
p = p30;
508
dp = vfloat<M>(3.0f)*(p21-p20);
509
}
510
511
template<int M, typename Vec = Vec4vf<M>>
512
__forceinline Vec eval0(const int ofs, const int size) const
513
{
514
assert(size <= PrecomputedBezierBasis::N);
515
assert(ofs <= size);
516
#if defined(EMBREE_SYCL_SUPPORT) && defined(__SYCL_DEVICE_ONLY__)
517
assert(size > 0);
518
const vfloat<M> t = (vfloat<M>(step) + vfloat<M>(ofs+0))*rcp(float(size));
519
Vec p,dp; veval<M>(t,p,dp);
520
return p;
521
#else
522
return madd(vfloat<M>::loadu(&bezier_basis0.c0[size][ofs]), Vec(v0),
523
madd(vfloat<M>::loadu(&bezier_basis0.c1[size][ofs]), Vec(v1),
524
madd(vfloat<M>::loadu(&bezier_basis0.c2[size][ofs]), Vec(v2),
525
vfloat<M>::loadu(&bezier_basis0.c3[size][ofs]) * Vec(v3))));
526
#endif
527
}
528
529
template<int M, typename Vec = Vec4vf<M>>
530
__forceinline Vec eval1(const int ofs, const int size) const
531
{
532
assert(size <= PrecomputedBezierBasis::N);
533
assert(ofs <= size);
534
#if defined(EMBREE_SYCL_SUPPORT) && defined(__SYCL_DEVICE_ONLY__)
535
assert(size > 0);
536
const vfloat<M> t = (vfloat<M>(step) + vfloat<M>(ofs+1))*rcp(float(size));
537
Vec p,dp; veval<M>(t,p,dp);
538
return p;
539
#else
540
return madd(vfloat<M>::loadu(&bezier_basis1.c0[size][ofs]), Vec(v0),
541
madd(vfloat<M>::loadu(&bezier_basis1.c1[size][ofs]), Vec(v1),
542
madd(vfloat<M>::loadu(&bezier_basis1.c2[size][ofs]), Vec(v2),
543
vfloat<M>::loadu(&bezier_basis1.c3[size][ofs]) * Vec(v3))));
544
#endif
545
}
546
547
template<int M, typename Vec = Vec4vf<M>>
548
__forceinline Vec derivative0(const int ofs, const int size) const
549
{
550
assert(size <= PrecomputedBezierBasis::N);
551
assert(ofs <= size);
552
#if defined(EMBREE_SYCL_SUPPORT) && defined(__SYCL_DEVICE_ONLY__)
553
assert(size > 0);
554
const vfloat<M> t = (vfloat<M>(step) + vfloat<M>(ofs+0))*rcp(float(size));
555
Vec p,dp; veval<M>(t,p,dp);
556
return dp;
557
#else
558
return madd(vfloat<M>::loadu(&bezier_basis0.d0[size][ofs]), Vec(v0),
559
madd(vfloat<M>::loadu(&bezier_basis0.d1[size][ofs]), Vec(v1),
560
madd(vfloat<M>::loadu(&bezier_basis0.d2[size][ofs]), Vec(v2),
561
vfloat<M>::loadu(&bezier_basis0.d3[size][ofs]) * Vec(v3))));
562
#endif
563
}
564
565
template<int M, typename Vec = Vec4vf<M>>
566
__forceinline Vec derivative1(const int ofs, const int size) const
567
{
568
assert(size <= PrecomputedBezierBasis::N);
569
assert(ofs <= size);
570
#if defined(EMBREE_SYCL_SUPPORT) && defined(__SYCL_DEVICE_ONLY__)
571
assert(size > 0);
572
const vfloat<M> t = (vfloat<M>(step) + vfloat<M>(ofs+1))*rcp(float(size));
573
Vec p,dp; veval<M>(t,p,dp);
574
return dp;
575
#else
576
return madd(vfloat<M>::loadu(&bezier_basis1.d0[size][ofs]), Vec(v0),
577
madd(vfloat<M>::loadu(&bezier_basis1.d1[size][ofs]), Vec(v1),
578
madd(vfloat<M>::loadu(&bezier_basis1.d2[size][ofs]), Vec(v2),
579
vfloat<M>::loadu(&bezier_basis1.d3[size][ofs]) * Vec(v3))));
580
#endif
581
}
582
583
/* calculates bounds of bezier curve geometry */
584
__forceinline BBox3fa accurateBounds() const
585
{
586
const int N = 7;
587
const float scale = 1.0f/(3.0f*(N-1));
588
Vec3vfx pl(pos_inf), pu(neg_inf);
589
for (int i=0; i<=N; i+=VSIZEX)
590
{
591
vintx vi = vintx(i)+vintx(StepTy());
592
vboolx valid = vi <= vintx(N);
593
const Vec3vfx p = eval0<VSIZEX,Vec3vf<VSIZEX>>(i,N);
594
const Vec3vfx dp = derivative0<VSIZEX,Vec3vf<VSIZEX>>(i,N);
595
const Vec3vfx pm = p-Vec3vfx(scale)*select(vi!=vintx(0),dp,Vec3vfx(zero));
596
const Vec3vfx pp = p+Vec3vfx(scale)*select(vi!=vintx(N),dp,Vec3vfx(zero));
597
pl = select(valid,min(pl,p,pm,pp),pl); // FIXME: use masked min
598
pu = select(valid,max(pu,p,pm,pp),pu); // FIXME: use masked min
599
}
600
const Vec3fa lower(reduce_min(pl.x),reduce_min(pl.y),reduce_min(pl.z));
601
const Vec3fa upper(reduce_max(pu.x),reduce_max(pu.y),reduce_max(pu.z));
602
return BBox3fa(lower,upper);
603
}
604
605
/* calculates bounds of bezier curve geometry */
606
__forceinline BBox3fa accurateRoundBounds() const
607
{
608
const int N = 7;
609
const float scale = 1.0f/(3.0f*(N-1));
610
Vec4vfx pl(pos_inf), pu(neg_inf);
611
for (int i=0; i<=N; i+=VSIZEX)
612
{
613
vintx vi = vintx(i)+vintx(StepTy());
614
vboolx valid = vi <= vintx(N);
615
const Vec4vfx p = eval0<VSIZEX>(i,N);
616
const Vec4vfx dp = derivative0<VSIZEX>(i,N);
617
const Vec4vfx pm = p-Vec4vfx(scale)*select(vi!=vintx(0),dp,Vec4vfx(zero));
618
const Vec4vfx pp = p+Vec4vfx(scale)*select(vi!=vintx(N),dp,Vec4vfx(zero));
619
pl = select(valid,min(pl,p,pm,pp),pl); // FIXME: use masked min
620
pu = select(valid,max(pu,p,pm,pp),pu); // FIXME: use masked min
621
}
622
const Vec3fa lower(reduce_min(pl.x),reduce_min(pl.y),reduce_min(pl.z));
623
const Vec3fa upper(reduce_max(pu.x),reduce_max(pu.y),reduce_max(pu.z));
624
const float r_min = reduce_min(pl.w);
625
const float r_max = reduce_max(pu.w);
626
const Vec3fa upper_r = Vec3fa(max(abs(r_min),abs(r_max)));
627
return enlarge(BBox3fa(lower,upper),upper_r);
628
}
629
630
/* calculates bounds when tessellated into N line segments */
631
__forceinline BBox3fa accurateFlatBounds(int N) const
632
{
633
if (likely(N == 4))
634
{
635
const Vec4vf4 pi = eval0<4>(0,4);
636
const Vec3fa lower(reduce_min(pi.x),reduce_min(pi.y),reduce_min(pi.z));
637
const Vec3fa upper(reduce_max(pi.x),reduce_max(pi.y),reduce_max(pi.z));
638
const Vec3fa upper_r = Vec3fa(reduce_max(abs(pi.w)));
639
return enlarge(BBox3fa(min(lower,v3),max(upper,v3)),max(upper_r,Vec3fa(abs(v3.w))));
640
}
641
else
642
{
643
Vec3vfx pl(pos_inf), pu(neg_inf); vfloatx ru(0.0f);
644
for (int i=0; i<N; i+=VSIZEX)
645
{
646
vboolx valid = vintx(i)+vintx(StepTy()) < vintx(N);
647
const Vec4vfx pi = eval0<VSIZEX>(i,N);
648
649
pl.x = select(valid,min(pl.x,pi.x),pl.x); // FIXME: use masked min
650
pl.y = select(valid,min(pl.y,pi.y),pl.y);
651
pl.z = select(valid,min(pl.z,pi.z),pl.z);
652
653
pu.x = select(valid,max(pu.x,pi.x),pu.x); // FIXME: use masked min
654
pu.y = select(valid,max(pu.y,pi.y),pu.y);
655
pu.z = select(valid,max(pu.z,pi.z),pu.z);
656
657
ru = select(valid,max(ru,abs(pi.w)),ru);
658
}
659
const Vec3fa lower(reduce_min(pl.x),reduce_min(pl.y),reduce_min(pl.z));
660
const Vec3fa upper(reduce_max(pu.x),reduce_max(pu.y),reduce_max(pu.z));
661
const Vec3fa upper_r(reduce_max(ru));
662
return enlarge(BBox3fa(min(lower,v3),max(upper,v3)),max(upper_r,Vec3fa(abs(v3.w))));
663
}
664
}
665
666
friend __forceinline embree_ostream operator<<(embree_ostream cout, const CubicBezierCurve& curve) {
667
return cout << "CubicBezierCurve { v0 = " << curve.v0 << ", v1 = " << curve.v1 << ", v2 = " << curve.v2 << ", v3 = " << curve.v3 << " }";
668
}
669
};
670
671
#if defined(__AVX__)
672
template<>
673
__forceinline CubicBezierCurve<vfloat4> CubicBezierCurve<vfloat4>::clip(const Interval1f& u1) const
674
{
675
const vfloat8 p00 = vfloat8(v0);
676
const vfloat8 p01 = vfloat8(v1);
677
const vfloat8 p02 = vfloat8(v2);
678
const vfloat8 p03 = vfloat8(v3);
679
680
const vfloat8 t(vfloat4(u1.lower),vfloat4(u1.upper));
681
const vfloat8 p10 = lerp(p00,p01,t);
682
const vfloat8 p11 = lerp(p01,p02,t);
683
const vfloat8 p12 = lerp(p02,p03,t);
684
const vfloat8 p20 = lerp(p10,p11,t);
685
const vfloat8 p21 = lerp(p11,p12,t);
686
const vfloat8 p30 = lerp(p20,p21,t);
687
688
const vfloat8 f01 = p30;
689
const vfloat8 df01 = vfloat8(3.0f)*(p21-p20);
690
691
const vfloat4 f0 = extract4<0>(f01), f1 = extract4<1>(f01);
692
const vfloat4 df0 = extract4<0>(df01), df1 = extract4<1>(df01);
693
const float s = u1.upper-u1.lower;
694
return CubicBezierCurve(f0,f0+s*(1.0f/3.0f)*df0,f1-s*(1.0f/3.0f)*df1,f1);
695
}
696
#endif
697
698
template<typename Vertex> using BezierCurveT = CubicBezierCurve<Vertex>;
699
700
typedef CubicBezierCurve<float> CubicBezierCurve1f;
701
typedef CubicBezierCurve<Vec2fa> CubicBezierCurve2fa;
702
typedef CubicBezierCurve<Vec3fa> CubicBezierCurve3fa;
703
typedef CubicBezierCurve<Vec3fa> BezierCurve3fa;
704
typedef CubicBezierCurve<Vec3ff> BezierCurve3ff;
705
706
template<> __forceinline int CubicBezierCurve<float>::maxRoots() const
707
{
708
float eps = 1E-4f;
709
bool neg0 = v0 <= 0.0f; bool zero0 = fabs(v0) < eps;
710
bool neg1 = v1 <= 0.0f; bool zero1 = fabs(v1) < eps;
711
bool neg2 = v2 <= 0.0f; bool zero2 = fabs(v2) < eps;
712
bool neg3 = v3 <= 0.0f; bool zero3 = fabs(v3) < eps;
713
return (neg0 != neg1 || zero0) + (neg1 != neg2 || zero1) + (neg2 != neg3 || zero2 || zero3);
714
}
715
716
template<> __forceinline int CubicBezierCurve<Interval1f>::maxRoots() const {
717
return numRoots(v0,v1) + numRoots(v1,v2) + numRoots(v2,v3);
718
}
719
720
struct CurveGeometry; // FIXME: this code should move !
721
template<typename CurveGeometry>
722
__forceinline CubicBezierCurve<Vec3ff> enlargeRadiusToMinWidth(const RayQueryContext* context, const CurveGeometry* geom, const Vec3fa& ray_org, const CubicBezierCurve<Vec3ff>& curve)
723
{
724
return CubicBezierCurve<Vec3ff>(enlargeRadiusToMinWidth(context,geom,ray_org,curve.v0),
725
enlargeRadiusToMinWidth(context,geom,ray_org,curve.v1),
726
enlargeRadiusToMinWidth(context,geom,ray_org,curve.v2),
727
enlargeRadiusToMinWidth(context,geom,ray_org,curve.v3));
728
}
729
}
730
731