Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/embree/kernels/subdiv/feature_adaptive_eval.h
9913 views
1
// Copyright 2009-2021 Intel Corporation
2
// SPDX-License-Identifier: Apache-2.0
3
4
#pragma once
5
6
#include "patch.h"
7
8
namespace embree
9
{
10
namespace isa
11
{
12
template<typename Vertex, typename Vertex_t = Vertex>
13
struct FeatureAdaptiveEval
14
{
15
public:
16
17
typedef PatchT<Vertex,Vertex_t> Patch;
18
typedef typename Patch::Ref Ref;
19
typedef GeneralCatmullClarkPatchT<Vertex,Vertex_t> GeneralCatmullClarkPatch;
20
typedef CatmullClark1RingT<Vertex,Vertex_t> CatmullClarkRing;
21
typedef CatmullClarkPatchT<Vertex,Vertex_t> CatmullClarkPatch;
22
typedef BSplinePatchT<Vertex,Vertex_t> BSplinePatch;
23
typedef BezierPatchT<Vertex,Vertex_t> BezierPatch;
24
typedef GregoryPatchT<Vertex,Vertex_t> GregoryPatch;
25
typedef BilinearPatchT<Vertex,Vertex_t> BilinearPatch;
26
typedef BezierCurveT<Vertex> BezierCurve;
27
28
public:
29
30
FeatureAdaptiveEval (const HalfEdge* edge, const char* vertices, size_t stride, const float u, const float v,
31
Vertex* P, Vertex* dPdu, Vertex* dPdv, Vertex* ddPdudu, Vertex* ddPdvdv, Vertex* ddPdudv)
32
: P(P), dPdu(dPdu), dPdv(dPdv), ddPdudu(ddPdudu), ddPdvdv(ddPdvdv), ddPdudv(ddPdudv)
33
{
34
switch (edge->patch_type) {
35
case HalfEdge::BILINEAR_PATCH: BilinearPatch(edge,vertices,stride).eval(u,v,P,dPdu,dPdv,ddPdudu,ddPdvdv,ddPdudv,1.0f); break;
36
case HalfEdge::REGULAR_QUAD_PATCH: RegularPatchT(edge,vertices,stride).eval(u,v,P,dPdu,dPdv,ddPdudu,ddPdvdv,ddPdudv,1.0f); break;
37
#if PATCH_USE_GREGORY == 2
38
case HalfEdge::IRREGULAR_QUAD_PATCH: GregoryPatch(edge,vertices,stride).eval(u,v,P,dPdu,dPdv,ddPdudu,ddPdvdv,ddPdudv,1.0f); break;
39
#endif
40
default: {
41
GeneralCatmullClarkPatch patch(edge,vertices,stride);
42
eval(patch,Vec2f(u,v),0);
43
break;
44
}
45
}
46
}
47
48
FeatureAdaptiveEval (CatmullClarkPatch& patch, const float u, const float v, float dscale, size_t depth,
49
Vertex* P, Vertex* dPdu, Vertex* dPdv, Vertex* ddPdudu, Vertex* ddPdvdv, Vertex* ddPdudv)
50
: P(P), dPdu(dPdu), dPdv(dPdv), ddPdudu(ddPdudu), ddPdvdv(ddPdvdv), ddPdudv(ddPdudv)
51
{
52
eval(patch,Vec2f(u,v),dscale,depth);
53
}
54
55
void eval_general_quad(const GeneralCatmullClarkPatch& patch, array_t<CatmullClarkPatch,GeneralCatmullClarkPatch::SIZE>& patches, const Vec2f& uv, size_t depth)
56
{
57
float u = uv.x, v = uv.y;
58
if (v < 0.5f) {
59
if (u < 0.5f) {
60
#if PATCH_USE_GREGORY == 2
61
BezierCurve borders[2]; patch.getLimitBorder(borders,0);
62
BezierCurve border0l,border0r; borders[0].subdivide(border0l,border0r);
63
BezierCurve border2l,border2r; borders[1].subdivide(border2l,border2r);
64
eval(patches[0],Vec2f(2.0f*u,2.0f*v),2.0f,depth+1, &border0l, nullptr, nullptr, &border2r);
65
#else
66
eval(patches[0],Vec2f(2.0f*u,2.0f*v),2.0f,depth+1);
67
#endif
68
if (dPdu && dPdv) {
69
const Vertex dpdx = *dPdu, dpdy = *dPdv;
70
*dPdu = dpdx; *dPdv = dpdy;
71
}
72
}
73
else {
74
#if PATCH_USE_GREGORY == 2
75
BezierCurve borders[2]; patch.getLimitBorder(borders,1);
76
BezierCurve border0l,border0r; borders[0].subdivide(border0l,border0r);
77
BezierCurve border2l,border2r; borders[1].subdivide(border2l,border2r);
78
eval(patches[1],Vec2f(2.0f*v,2.0f-2.0f*u),2.0f,depth+1, &border0l, nullptr, nullptr, &border2r);
79
#else
80
eval(patches[1],Vec2f(2.0f*v,2.0f-2.0f*u),2.0f,depth+1);
81
#endif
82
if (dPdu && dPdv) {
83
const Vertex dpdx = *dPdu, dpdy = *dPdv;
84
*dPdu = -dpdy; *dPdv = dpdx;
85
}
86
}
87
} else {
88
if (u > 0.5f) {
89
#if PATCH_USE_GREGORY == 2
90
BezierCurve borders[2]; patch.getLimitBorder(borders,2);
91
BezierCurve border0l,border0r; borders[0].subdivide(border0l,border0r);
92
BezierCurve border2l,border2r; borders[1].subdivide(border2l,border2r);
93
eval(patches[2],Vec2f(2.0f-2.0f*u,2.0f-2.0f*v),2.0f,depth+1, &border0l, nullptr, nullptr, &border2r);
94
#else
95
eval(patches[2],Vec2f(2.0f-2.0f*u,2.0f-2.0f*v),2.0f,depth+1);
96
#endif
97
if (dPdu && dPdv) {
98
const Vertex dpdx = *dPdu, dpdy = *dPdv;
99
*dPdu = -dpdx; *dPdv = -dpdy;
100
}
101
}
102
else {
103
#if PATCH_USE_GREGORY == 2
104
BezierCurve borders[2]; patch.getLimitBorder(borders,3);
105
BezierCurve border0l,border0r; borders[0].subdivide(border0l,border0r);
106
BezierCurve border2l,border2r; borders[1].subdivide(border2l,border2r);
107
eval(patches[3],Vec2f(2.0f-2.0f*v,2.0f*u),2.0f,depth+1, &border0l, nullptr, nullptr, &border2r);
108
#else
109
eval(patches[3],Vec2f(2.0f-2.0f*v,2.0f*u),2.0f,depth+1);
110
#endif
111
if (dPdu && dPdv) {
112
const Vertex dpdx = *dPdu, dpdy = *dPdv;
113
*dPdu = dpdy; *dPdv = -dpdx;
114
}
115
}
116
}
117
}
118
119
__forceinline bool final(const CatmullClarkPatch& patch, const typename CatmullClarkRing::Type type, size_t depth)
120
{
121
const int max_eval_depth = (type & CatmullClarkRing::TYPE_CREASES) ? PATCH_MAX_EVAL_DEPTH_CREASE : PATCH_MAX_EVAL_DEPTH_IRREGULAR;
122
//#if PATCH_MIN_RESOLUTION
123
// return patch.isFinalResolution(PATCH_MIN_RESOLUTION) || depth>=(size_t)max_eval_depth;
124
//#else
125
return depth>=(size_t)max_eval_depth;
126
//#endif
127
}
128
129
void eval(CatmullClarkPatch& patch, Vec2f uv, float dscale, size_t depth,
130
BezierCurve* border0 = nullptr, BezierCurve* border1 = nullptr, BezierCurve* border2 = nullptr, BezierCurve* border3 = nullptr)
131
{
132
while (true)
133
{
134
typename CatmullClarkPatch::Type ty = patch.type();
135
136
if (unlikely(final(patch,ty,depth)))
137
{
138
if (ty & CatmullClarkRing::TYPE_REGULAR) {
139
RegularPatch(patch,border0,border1,border2,border3).eval(uv.x,uv.y,P,dPdu,dPdv,ddPdudu,ddPdvdv,ddPdudv,dscale);
140
PATCH_DEBUG_SUBDIVISION(234423,c,c,-1);
141
return;
142
} else {
143
IrregularFillPatch(patch,border0,border1,border2,border3).eval(uv.x,uv.y,P,dPdu,dPdv,ddPdudu,ddPdvdv,ddPdudv,dscale);
144
PATCH_DEBUG_SUBDIVISION(34534,c,-1,c);
145
return;
146
}
147
}
148
else if (ty & CatmullClarkRing::TYPE_REGULAR_CREASES) {
149
assert(depth > 0);
150
RegularPatch(patch,border0,border1,border2,border3).eval(uv.x,uv.y,P,dPdu,dPdv,ddPdudu,ddPdvdv,ddPdudv,dscale);
151
PATCH_DEBUG_SUBDIVISION(43524,c,c,-1);
152
return;
153
}
154
#if PATCH_USE_GREGORY == 2
155
else if (ty & CatmullClarkRing::TYPE_GREGORY_CREASES) {
156
assert(depth > 0);
157
GregoryPatch(patch,border0,border1,border2,border3).eval(uv.x,uv.y,P,dPdu,dPdv,ddPdudu,ddPdvdv,ddPdudv,dscale);
158
PATCH_DEBUG_SUBDIVISION(23498,c,-1,c);
159
return;
160
}
161
#endif
162
else
163
{
164
array_t<CatmullClarkPatch,4> patches;
165
patch.subdivide(patches); // FIXME: only have to generate one of the patches
166
167
const float u = uv.x, v = uv.y;
168
if (v < 0.5f) {
169
if (u < 0.5f) { patch = patches[0]; uv = Vec2f(2.0f*u,2.0f*v); dscale *= 2.0f; }
170
else { patch = patches[1]; uv = Vec2f(2.0f*u-1.0f,2.0f*v); dscale *= 2.0f; }
171
} else {
172
if (u > 0.5f) { patch = patches[2]; uv = Vec2f(2.0f*u-1.0f,2.0f*v-1.0f); dscale *= 2.0f; }
173
else { patch = patches[3]; uv = Vec2f(2.0f*u,2.0f*v-1.0f); dscale *= 2.0f; }
174
}
175
depth++;
176
}
177
}
178
}
179
180
void eval(const GeneralCatmullClarkPatch& patch, const Vec2f& uv, const size_t depth)
181
{
182
/* convert into standard quad patch if possible */
183
if (likely(patch.isQuadPatch()))
184
{
185
CatmullClarkPatch qpatch; patch.init(qpatch);
186
return eval(qpatch,uv,1.0f,depth);
187
}
188
189
/* subdivide patch */
190
unsigned N;
191
array_t<CatmullClarkPatch,GeneralCatmullClarkPatch::SIZE> patches;
192
patch.subdivide(patches,N); // FIXME: only have to generate one of the patches
193
194
/* parametrization for quads */
195
if (N == 4)
196
eval_general_quad(patch,patches,uv,depth);
197
198
/* parametrization for arbitrary polygons */
199
else
200
{
201
const unsigned l = (unsigned) floor(0.5f*uv.x); const float u = 2.0f*frac(0.5f*uv.x)-0.5f;
202
const unsigned h = (unsigned) floor(0.5f*uv.y); const float v = 2.0f*frac(0.5f*uv.y)-0.5f;
203
const unsigned i = 4*h+l; assert(i<N);
204
if (i >= N) return;
205
206
#if PATCH_USE_GREGORY == 2
207
BezierCurve borders[2]; patch.getLimitBorder(borders,i);
208
BezierCurve border0l,border0r; borders[0].subdivide(border0l,border0r);
209
BezierCurve border2l,border2r; borders[1].subdivide(border2l,border2r);
210
eval(patches[i],Vec2f(u,v),1.0f,depth+1, &border0l, nullptr, nullptr, &border2r);
211
#else
212
eval(patches[i],Vec2f(u,v),1.0f,depth+1);
213
#endif
214
}
215
}
216
217
private:
218
Vertex* const P;
219
Vertex* const dPdu;
220
Vertex* const dPdv;
221
Vertex* const ddPdudu;
222
Vertex* const ddPdvdv;
223
Vertex* const ddPdudv;
224
};
225
}
226
}
227
228