1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
// Copyright 2009-2020 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include "../common/ray.h"
#include "quad_intersector.h"
#include "curve_intersector_precalculations.h"
#define Bezier1Intersector1 RibbonCurve1Intersector1
#define Bezier1IntersectorK RibbonCurve1IntersectorK
namespace embree
{
namespace isa
{
template<typename NativeCurve3ff, int M>
struct RibbonHit
{
__forceinline RibbonHit() {}
__forceinline RibbonHit(const vbool<M>& valid, const vfloat<M>& U, const vfloat<M>& V, const vfloat<M>& T, const int i, const int N,
const NativeCurve3ff& curve3D)
: U(U), V(V), T(T), i(i), N(N), curve3D(curve3D), valid(valid) {}
__forceinline void finalize()
{
vu = (vfloat<M>(step)+U+vfloat<M>(float(i)))*(1.0f/float(N));
vv = V;
vt = T;
}
__forceinline Vec2f uv (const size_t i) const { return Vec2f(vu[i],vv[i]); }
__forceinline float t (const size_t i) const { return vt[i]; }
__forceinline Vec3fa Ng(const size_t i) const {
return curve3D.eval_du(vu[i]);
}
public:
vfloat<M> U;
vfloat<M> V;
vfloat<M> T;
int i, N;
NativeCurve3ff curve3D;
public:
vbool<M> valid;
vfloat<M> vu;
vfloat<M> vv;
vfloat<M> vt;
};
/* calculate squared distance of point p0 to line p1->p2 */
__forceinline std::pair<vfloatx,vfloatx> sqr_point_line_distance(const Vec2vfx& p0, const Vec2vfx& p1, const Vec2vfx& p2)
{
const vfloatx num = det(p2-p1,p1-p0);
const vfloatx den2 = dot(p2-p1,p2-p1);
return std::make_pair(num*num,den2);
}
/* performs culling against a cylinder */
__forceinline vboolx cylinder_culling_test(const Vec2vfx& p0, const Vec2vfx& p1, const Vec2vfx& p2, const vfloatx& r)
{
const std::pair<vfloatx,vfloatx> d = sqr_point_line_distance(p0,p1,p2);
return d.first <= r*r*d.second;
}
template<typename NativeCurve3ff, typename Epilog>
__forceinline bool intersect_ribbon(const Vec3fa& ray_org, const Vec3fa& ray_dir, const float ray_tnear, const float& ray_tfar,
const LinearSpace3fa& ray_space, const float& depth_scale,
const NativeCurve3ff& curve3D, const int N,
const Epilog& epilog)
{
/* transform control points into ray space */
const NativeCurve3ff curve2D = curve3D.xfm_pr(ray_space,ray_org);
float eps = 4.0f*float(ulp)*reduce_max(max(abs(curve2D.v0),abs(curve2D.v1),abs(curve2D.v2),abs(curve2D.v3)));
/* evaluate the bezier curve */
bool ishit = false;
vboolx valid = vfloatx(step) < vfloatx(float(N));
const Vec4vfx p0 = curve2D.template eval0<VSIZEX>(0,N);
const Vec4vfx p1 = curve2D.template eval1<VSIZEX>(0,N);
valid &= cylinder_culling_test(zero,Vec2vfx(p0.x,p0.y),Vec2vfx(p1.x,p1.y),max(p0.w,p1.w));
if (any(valid))
{
Vec3vfx dp0dt = curve2D.template derivative0<VSIZEX>(0,N);
Vec3vfx dp1dt = curve2D.template derivative1<VSIZEX>(0,N);
dp0dt = select(reduce_max(abs(dp0dt)) < vfloatx(eps),Vec3vfx(p1-p0),dp0dt);
dp1dt = select(reduce_max(abs(dp1dt)) < vfloatx(eps),Vec3vfx(p1-p0),dp1dt);
const Vec3vfx n0(dp0dt.y,-dp0dt.x,0.0f);
const Vec3vfx n1(dp1dt.y,-dp1dt.x,0.0f);
const Vec3vfx nn0 = normalize(n0);
const Vec3vfx nn1 = normalize(n1);
const Vec3vfx lp0 = madd(p0.w,nn0,Vec3vfx(p0));
const Vec3vfx lp1 = madd(p1.w,nn1,Vec3vfx(p1));
const Vec3vfx up0 = nmadd(p0.w,nn0,Vec3vfx(p0));
const Vec3vfx up1 = nmadd(p1.w,nn1,Vec3vfx(p1));
vfloatx vu,vv,vt;
vboolx valid0 = intersect_quad_backface_culling(valid,zero,Vec3fa(0,0,1),ray_tnear,ray_tfar,lp0,lp1,up1,up0,vu,vv,vt);
if (any(valid0))
{
/* ignore self intersections */
if (EMBREE_CURVE_SELF_INTERSECTION_AVOIDANCE_FACTOR != 0.0f) {
vfloatx r = lerp(p0.w, p1.w, vu);
valid0 &= vt > float(EMBREE_CURVE_SELF_INTERSECTION_AVOIDANCE_FACTOR)*r*depth_scale;
}
if (any(valid0))
{
vv = madd(2.0f,vv,vfloatx(-1.0f));
RibbonHit<NativeCurve3ff,VSIZEX> bhit(valid0,vu,vv,vt,0,N,curve3D);
ishit |= epilog(bhit.valid,bhit);
}
}
}
if (unlikely(VSIZEX < N))
{
/* process SIMD-size many segments per iteration */
for (int i=VSIZEX; i<N; i+=VSIZEX)
{
/* evaluate the bezier curve */
vboolx valid = vintx(i)+vintx(step) < vintx(N);
const Vec4vfx p0 = curve2D.template eval0<VSIZEX>(i,N);
const Vec4vfx p1 = curve2D.template eval1<VSIZEX>(i,N);
valid &= cylinder_culling_test(zero,Vec2vfx(p0.x,p0.y),Vec2vfx(p1.x,p1.y),max(p0.w,p1.w));
if (none(valid)) continue;
Vec3vfx dp0dt = curve2D.template derivative0<VSIZEX>(i,N);
Vec3vfx dp1dt = curve2D.template derivative1<VSIZEX>(i,N);
dp0dt = select(reduce_max(abs(dp0dt)) < vfloatx(eps),Vec3vfx(p1-p0),dp0dt);
dp1dt = select(reduce_max(abs(dp1dt)) < vfloatx(eps),Vec3vfx(p1-p0),dp1dt);
const Vec3vfx n0(dp0dt.y,-dp0dt.x,0.0f);
const Vec3vfx n1(dp1dt.y,-dp1dt.x,0.0f);
const Vec3vfx nn0 = normalize(n0);
const Vec3vfx nn1 = normalize(n1);
const Vec3vfx lp0 = madd(p0.w,nn0,Vec3vfx(p0));
const Vec3vfx lp1 = madd(p1.w,nn1,Vec3vfx(p1));
const Vec3vfx up0 = nmadd(p0.w,nn0,Vec3vfx(p0));
const Vec3vfx up1 = nmadd(p1.w,nn1,Vec3vfx(p1));
vfloatx vu,vv,vt;
vboolx valid0 = intersect_quad_backface_culling(valid,zero,Vec3fa(0,0,1),ray_tnear,ray_tfar,lp0,lp1,up1,up0,vu,vv,vt);
if (any(valid0))
{
/* ignore self intersections */
if (EMBREE_CURVE_SELF_INTERSECTION_AVOIDANCE_FACTOR != 0.0f) {
vfloatx r = lerp(p0.w, p1.w, vu);
valid0 &= vt > float(EMBREE_CURVE_SELF_INTERSECTION_AVOIDANCE_FACTOR)*r*depth_scale;
}
if (any(valid0))
{
vv = madd(2.0f,vv,vfloatx(-1.0f));
RibbonHit<NativeCurve3ff,VSIZEX> bhit(valid0,vu,vv,vt,i,N,curve3D);
ishit |= epilog(bhit.valid,bhit);
}
}
}
}
return ishit;
}
template<template<typename Ty> class NativeCurve>
struct RibbonCurve1Intersector1
{
typedef NativeCurve<Vec3ff> NativeCurve3ff;
template<typename Epilog>
__forceinline bool intersect(const CurvePrecalculations1& pre, Ray& ray,
IntersectContext* context,
const CurveGeometry* geom, const unsigned int primID,
const Vec3ff& v0, const Vec3ff& v1, const Vec3ff& v2, const Vec3ff& v3,
const Epilog& epilog)
{
const int N = geom->tessellationRate;
NativeCurve3ff curve(v0,v1,v2,v3);
curve = enlargeRadiusToMinWidth(context,geom,ray.org,curve);
return intersect_ribbon<NativeCurve3ff>(ray.org,ray.dir,ray.tnear(),ray.tfar,
pre.ray_space,pre.depth_scale,
curve,N,
epilog);
}
};
template<template<typename Ty> class NativeCurve, int K>
struct RibbonCurve1IntersectorK
{
typedef NativeCurve<Vec3ff> NativeCurve3ff;
template<typename Epilog>
__forceinline bool intersect(const CurvePrecalculationsK<K>& pre, RayK<K>& ray, size_t k,
IntersectContext* context,
const CurveGeometry* geom, const unsigned int primID,
const Vec3ff& v0, const Vec3ff& v1, const Vec3ff& v2, const Vec3ff& v3,
const Epilog& epilog)
{
const int N = geom->tessellationRate;
const Vec3fa ray_org(ray.org.x[k],ray.org.y[k],ray.org.z[k]);
const Vec3fa ray_dir(ray.dir.x[k],ray.dir.y[k],ray.dir.z[k]);
NativeCurve3ff curve(v0,v1,v2,v3);
curve = enlargeRadiusToMinWidth(context,geom,ray_org,curve);
return intersect_ribbon<NativeCurve3ff>(ray_org,ray_dir,ray.tnear()[k],ray.tfar[k],
pre.ray_space[k],pre.depth_scale[k],
curve,N,
epilog);
}
};
}
}
|