blob: a1c646102ed9b4fc146f2f56873fdf52c71da70d [file] [log] [blame]
Chris Daltonf6bf5162020-05-13 19:18:46 -06001/*
2 * Copyright 2020 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "include/utils/SkRandom.h"
9#include "src/core/SkGeometry.h"
10#include "src/gpu/tessellate/GrWangsFormula.h"
11#include "tests/Test.h"
12
13constexpr static int kIntolerance = 4; // 1/4 pixel max error.
14
15const SkPoint kSerp[4] = {
16 {285.625f, 499.687f}, {411.625f, 808.188f}, {1064.62f, 135.688f}, {1042.63f, 585.187f}};
17
18const SkPoint kLoop[4] = {
19 {635.625f, 614.687f}, {171.625f, 236.188f}, {1064.62f, 135.688f}, {516.625f, 570.187f}};
20
21const SkPoint kQuad[4] = {
22 {460.625f, 557.187f}, {707.121f, 209.688f}, {779.628f, 577.687f}};
23
Chris Dalton4dd3c8c2020-10-30 22:45:58 -060024static float wangs_formula_quadratic_reference_impl(float intolerance, const SkPoint p[3]) {
25 float k = (2 * 1) / 8.f * intolerance;
26 return sqrtf(k * (p[0] - p[1]*2 + p[2]).length());
Chris Daltonfc396a82020-09-23 20:11:26 -060027}
28
Chris Dalton4dd3c8c2020-10-30 22:45:58 -060029static float wangs_formula_cubic_reference_impl(float intolerance, const SkPoint p[4]) {
30 float k = (3 * 2) / 8.f * intolerance;
31 return sqrtf(k * std::max((p[0] - p[1]*2 + p[2]).length(),
32 (p[1] - p[2]*2 + p[3]).length()));
Chris Daltonfc396a82020-09-23 20:11:26 -060033}
34
35static void for_random_matrices(SkRandom* rand, std::function<void(const SkMatrix&)> f) {
Chris Daltonf6bf5162020-05-13 19:18:46 -060036 SkMatrix m;
37 m.setIdentity();
38 f(m);
39
40 for (int i = -10; i <= 30; ++i) {
41 for (int j = -10; j <= 30; ++j) {
42 m.setScaleX(std::ldexp(1 + rand->nextF(), i));
43 m.setSkewX(0);
44 m.setSkewY(0);
45 m.setScaleY(std::ldexp(1 + rand->nextF(), j));
46 f(m);
47
48 m.setScaleX(std::ldexp(1 + rand->nextF(), i));
49 m.setSkewX(std::ldexp(1 + rand->nextF(), (j + i) / 2));
50 m.setSkewY(std::ldexp(1 + rand->nextF(), (j + i) / 2));
51 m.setScaleY(std::ldexp(1 + rand->nextF(), j));
52 f(m);
53 }
54 }
55}
56
Chris Daltonfc396a82020-09-23 20:11:26 -060057static void for_random_beziers(int numPoints, SkRandom* rand,
Tyler Denniston4be95c52020-12-02 14:22:12 -050058 std::function<void(const SkPoint[])> f,
59 int maxExponent = 30) {
Chris Daltonf6bf5162020-05-13 19:18:46 -060060 SkASSERT(numPoints <= 4);
61 SkPoint pts[4];
Tyler Denniston4be95c52020-12-02 14:22:12 -050062 for (int i = -10; i <= maxExponent; ++i) {
Chris Daltonf6bf5162020-05-13 19:18:46 -060063 for (int j = 0; j < numPoints; ++j) {
64 pts[j].set(std::ldexp(1 + rand->nextF(), i), std::ldexp(1 + rand->nextF(), i));
65 }
66 f(pts);
67 }
68}
69
70// Ensure the optimized "*_log2" versions return the same value as ceil(std::log2(f)).
71DEF_TEST(WangsFormula_log2, r) {
72 // Constructs a cubic such that the 'length' term in wang's formula == term.
73 //
74 // f = sqrt(k * length(max(abs(p0 - p1*2 + p2),
75 // abs(p1 - p2*2 + p3))));
76 auto setupCubicLengthTerm = [](int seed, SkPoint pts[], float term) {
77 memset(pts, 0, sizeof(SkPoint) * 4);
78
79 SkPoint term2d = (seed & 1) ?
80 SkPoint::Make(term, 0) : SkPoint::Make(.5f, std::sqrt(3)/2) * term;
81 seed >>= 1;
82
83 if (seed & 1) {
84 term2d.fX = -term2d.fX;
85 }
86 seed >>= 1;
87
88 if (seed & 1) {
89 std::swap(term2d.fX, term2d.fY);
90 }
91 seed >>= 1;
92
93 switch (seed % 4) {
94 case 0:
95 pts[0] = term2d;
96 pts[3] = term2d * .75f;
97 return;
98 case 1:
99 pts[1] = term2d * -.5f;
100 return;
101 case 2:
102 pts[1] = term2d * -.5f;
103 return;
104 case 3:
105 pts[3] = term2d;
106 pts[0] = term2d * .75f;
107 return;
108 }
109 };
110
111 // Constructs a quadratic such that the 'length' term in wang's formula == term.
112 //
113 // f = sqrt(k * length(p0 - p1*2 + p2));
114 auto setupQuadraticLengthTerm = [](int seed, SkPoint pts[], float term) {
115 memset(pts, 0, sizeof(SkPoint) * 3);
116
117 SkPoint term2d = (seed & 1) ?
118 SkPoint::Make(term, 0) : SkPoint::Make(.5f, std::sqrt(3)/2) * term;
119 seed >>= 1;
120
121 if (seed & 1) {
122 term2d.fX = -term2d.fX;
123 }
124 seed >>= 1;
125
126 if (seed & 1) {
127 std::swap(term2d.fX, term2d.fY);
128 }
129 seed >>= 1;
130
131 switch (seed % 3) {
132 case 0:
133 pts[0] = term2d;
134 return;
135 case 1:
136 pts[1] = term2d * -.5f;
137 return;
138 case 2:
139 pts[2] = term2d;
140 return;
141 }
142 };
143
Chris Daltonfc396a82020-09-23 20:11:26 -0600144 // GrWangsFormula::cubic and ::quadratic both use rsqrt instead of sqrt for speed. Linearization
145 // is all approximate anyway, so as long as we are within ~1/2 tessellation segment of the
146 // reference value we are good enough.
147 constexpr static float kTessellationTolerance = 1/128.f;
148
Chris Daltonf6bf5162020-05-13 19:18:46 -0600149 for (int level = 0; level < 30; ++level) {
150 float epsilon = std::ldexp(SK_ScalarNearlyZero, level * 2);
151 SkPoint pts[4];
152
153 {
154 // Test cubic boundaries.
155 // f = sqrt(k * length(max(abs(p0 - p1*2 + p2),
156 // abs(p1 - p2*2 + p3))));
157 constexpr static float k = (3 * 2) / (8 * (1.f/kIntolerance));
158 float x = std::ldexp(1, level * 2) / k;
159 setupCubicLengthTerm(level << 1, pts, x - epsilon);
Chris Daltonfc396a82020-09-23 20:11:26 -0600160 float referenceValue = wangs_formula_cubic_reference_impl(kIntolerance, pts);
161 REPORTER_ASSERT(r, std::ceil(std::log2(referenceValue)) == level);
162 float c = GrWangsFormula::cubic(kIntolerance, pts);
163 REPORTER_ASSERT(r, SkScalarNearlyEqual(c/referenceValue, 1, kTessellationTolerance));
Chris Daltonf6bf5162020-05-13 19:18:46 -0600164 REPORTER_ASSERT(r, GrWangsFormula::cubic_log2(kIntolerance, pts) == level);
165 setupCubicLengthTerm(level << 1, pts, x + epsilon);
Chris Daltonfc396a82020-09-23 20:11:26 -0600166 referenceValue = wangs_formula_cubic_reference_impl(kIntolerance, pts);
167 REPORTER_ASSERT(r, std::ceil(std::log2(referenceValue)) == level + 1);
168 c = GrWangsFormula::cubic(kIntolerance, pts);
169 REPORTER_ASSERT(r, SkScalarNearlyEqual(c/referenceValue, 1, kTessellationTolerance));
Chris Daltonf6bf5162020-05-13 19:18:46 -0600170 REPORTER_ASSERT(r, GrWangsFormula::cubic_log2(kIntolerance, pts) == level + 1);
171 }
172
173 {
174 // Test quadratic boundaries.
175 // f = std::sqrt(k * Length(p0 - p1*2 + p2));
176 constexpr static float k = 2 / (8 * (1.f/kIntolerance));
177 float x = std::ldexp(1, level * 2) / k;
178 setupQuadraticLengthTerm(level << 1, pts, x - epsilon);
Chris Daltonfc396a82020-09-23 20:11:26 -0600179 float referenceValue = wangs_formula_quadratic_reference_impl(kIntolerance, pts);
180 REPORTER_ASSERT(r, std::ceil(std::log2(referenceValue)) == level);
181 float q = GrWangsFormula::quadratic(kIntolerance, pts);
182 REPORTER_ASSERT(r, SkScalarNearlyEqual(q/referenceValue, 1, kTessellationTolerance));
Chris Daltonf6bf5162020-05-13 19:18:46 -0600183 REPORTER_ASSERT(r, GrWangsFormula::quadratic_log2(kIntolerance, pts) == level);
184 setupQuadraticLengthTerm(level << 1, pts, x + epsilon);
Chris Daltonfc396a82020-09-23 20:11:26 -0600185 referenceValue = wangs_formula_quadratic_reference_impl(kIntolerance, pts);
186 REPORTER_ASSERT(r, std::ceil(std::log2(referenceValue)) == level+1);
187 q = GrWangsFormula::quadratic(kIntolerance, pts);
188 REPORTER_ASSERT(r, SkScalarNearlyEqual(q/referenceValue, 1, kTessellationTolerance));
Chris Daltonf6bf5162020-05-13 19:18:46 -0600189 REPORTER_ASSERT(r, GrWangsFormula::quadratic_log2(kIntolerance, pts) == level + 1);
190 }
191 }
192
193 auto check_cubic_log2 = [&](const SkPoint* pts) {
Chris Daltonfc396a82020-09-23 20:11:26 -0600194 float f = std::max(1.f, wangs_formula_cubic_reference_impl(kIntolerance, pts));
Chris Daltonf6bf5162020-05-13 19:18:46 -0600195 int f_log2 = GrWangsFormula::cubic_log2(kIntolerance, pts);
196 REPORTER_ASSERT(r, SkScalarCeilToInt(std::log2(f)) == f_log2);
Chris Daltonfc396a82020-09-23 20:11:26 -0600197 float c = std::max(1.f, GrWangsFormula::cubic(kIntolerance, pts));
198 REPORTER_ASSERT(r, SkScalarNearlyEqual(c/f, 1, kTessellationTolerance));
Chris Daltonf6bf5162020-05-13 19:18:46 -0600199 };
200
201 auto check_quadratic_log2 = [&](const SkPoint* pts) {
Chris Daltonfc396a82020-09-23 20:11:26 -0600202 float f = std::max(1.f, wangs_formula_quadratic_reference_impl(kIntolerance, pts));
Chris Daltonf6bf5162020-05-13 19:18:46 -0600203 int f_log2 = GrWangsFormula::quadratic_log2(kIntolerance, pts);
204 REPORTER_ASSERT(r, SkScalarCeilToInt(std::log2(f)) == f_log2);
Chris Daltonfc396a82020-09-23 20:11:26 -0600205 float q = std::max(1.f, GrWangsFormula::quadratic(kIntolerance, pts));
206 REPORTER_ASSERT(r, SkScalarNearlyEqual(q/f, 1, kTessellationTolerance));
Chris Daltonf6bf5162020-05-13 19:18:46 -0600207 };
208
209 SkRandom rand;
210
211 for_random_matrices(&rand, [&](const SkMatrix& m) {
212 SkPoint pts[4];
213 m.mapPoints(pts, kSerp, 4);
214 check_cubic_log2(pts);
215
216 m.mapPoints(pts, kLoop, 4);
217 check_cubic_log2(pts);
218
219 m.mapPoints(pts, kQuad, 3);
220 check_quadratic_log2(pts);
221 });
222
223 for_random_beziers(4, &rand, [&](const SkPoint pts[]) {
224 check_cubic_log2(pts);
225 });
226
227 for_random_beziers(3, &rand, [&](const SkPoint pts[]) {
228 check_quadratic_log2(pts);
229 });
230}
231
232// Ensure using transformations gives the same result as pre-transforming all points.
233DEF_TEST(WangsFormula_vectorXforms, r) {
234 auto check_cubic_log2_with_transform = [&](const SkPoint* pts, const SkMatrix& m){
235 SkPoint ptsXformed[4];
236 m.mapPoints(ptsXformed, pts, 4);
237 int expected = GrWangsFormula::cubic_log2(kIntolerance, ptsXformed);
238 int actual = GrWangsFormula::cubic_log2(kIntolerance, pts, GrVectorXform(m));
239 REPORTER_ASSERT(r, actual == expected);
240 };
241
242 auto check_quadratic_log2_with_transform = [&](const SkPoint* pts, const SkMatrix& m) {
243 SkPoint ptsXformed[3];
244 m.mapPoints(ptsXformed, pts, 3);
245 int expected = GrWangsFormula::quadratic_log2(kIntolerance, ptsXformed);
246 int actual = GrWangsFormula::quadratic_log2(kIntolerance, pts, GrVectorXform(m));
247 REPORTER_ASSERT(r, actual == expected);
248 };
249
250 SkRandom rand;
251
252 for_random_matrices(&rand, [&](const SkMatrix& m) {
253 check_cubic_log2_with_transform(kSerp, m);
254 check_cubic_log2_with_transform(kLoop, m);
255 check_quadratic_log2_with_transform(kQuad, m);
256
257 for_random_beziers(4, &rand, [&](const SkPoint pts[]) {
258 check_cubic_log2_with_transform(pts, m);
259 });
260
261 for_random_beziers(3, &rand, [&](const SkPoint pts[]) {
262 check_quadratic_log2_with_transform(pts, m);
263 });
264 });
Chris Daltonb96995d2020-06-04 16:44:29 -0600265}
Chris Daltonf6bf5162020-05-13 19:18:46 -0600266
Chris Daltonb96995d2020-06-04 16:44:29 -0600267DEF_TEST(WangsFormula_worst_case_cubic, r) {
268 {
269 SkPoint worstP[] = {{0,0}, {100,100}, {0,0}, {0,0}};
270 REPORTER_ASSERT(r, GrWangsFormula::worst_case_cubic(kIntolerance, 100, 100) ==
Chris Daltonfc396a82020-09-23 20:11:26 -0600271 wangs_formula_cubic_reference_impl(kIntolerance, worstP));
Chris Daltonb96995d2020-06-04 16:44:29 -0600272 REPORTER_ASSERT(r, GrWangsFormula::worst_case_cubic_log2(kIntolerance, 100, 100) ==
273 GrWangsFormula::cubic_log2(kIntolerance, worstP));
274 }
275 {
276 SkPoint worstP[] = {{100,100}, {100,100}, {200,200}, {100,100}};
277 REPORTER_ASSERT(r, GrWangsFormula::worst_case_cubic(kIntolerance, 100, 100) ==
Chris Daltonfc396a82020-09-23 20:11:26 -0600278 wangs_formula_cubic_reference_impl(kIntolerance, worstP));
Chris Daltonb96995d2020-06-04 16:44:29 -0600279 REPORTER_ASSERT(r, GrWangsFormula::worst_case_cubic_log2(kIntolerance, 100, 100) ==
280 GrWangsFormula::cubic_log2(kIntolerance, worstP));
281 }
282 auto check_worst_case_cubic = [&](const SkPoint* pts) {
283 SkRect bbox;
284 bbox.setBoundsNoCheck(pts, 4);
285 float worst = GrWangsFormula::worst_case_cubic(kIntolerance, bbox.width(), bbox.height());
286 int worst_log2 = GrWangsFormula::worst_case_cubic_log2(kIntolerance, bbox.width(),
287 bbox.height());
Chris Daltonfc396a82020-09-23 20:11:26 -0600288 float actual = wangs_formula_cubic_reference_impl(kIntolerance, pts);
Chris Daltonb96995d2020-06-04 16:44:29 -0600289 REPORTER_ASSERT(r, worst >= actual);
290 REPORTER_ASSERT(r, std::ceil(std::log2(std::max(1.f, worst))) == worst_log2);
Chris Daltonb96995d2020-06-04 16:44:29 -0600291 };
292 SkRandom rand;
293 for (int i = 0; i < 100; ++i) {
294 for_random_beziers(4, &rand, [&](const SkPoint pts[]) {
295 check_worst_case_cubic(pts);
296 });
297 }
Chris Daltonf6bf5162020-05-13 19:18:46 -0600298}
Tyler Denniston4be95c52020-12-02 14:22:12 -0500299
300// Ensure Wang's formula for quads produces max error within tolerance.
301DEF_TEST(WangsFormula_quad_within_tol, r) {
302 // Wang's formula and the quad math starts to lose precision with very large
303 // coordinate values, so limit the magnitude a bit to prevent test failures
304 // due to loss of precision.
305 constexpr int maxExponent = 15;
306 SkRandom rand;
307 for_random_beziers(3, &rand, [&r](const SkPoint pts[]) {
308 const int nsegs = static_cast<int>(
309 std::ceil(wangs_formula_quadratic_reference_impl(kIntolerance, pts)));
310
311 const float tdelta = 1.f / nsegs;
312 for (int j = 0; j < nsegs; ++j) {
313 const float tmin = j * tdelta, tmax = (j + 1) * tdelta;
314
315 // Get section of quad in [tmin,tmax]
316 const SkPoint* sectionPts;
317 SkPoint tmp0[5];
318 SkPoint tmp1[5];
319 if (tmin == 0) {
320 if (tmax == 1) {
321 sectionPts = pts;
322 } else {
323 SkChopQuadAt(pts, tmp0, tmax);
324 sectionPts = tmp0;
325 }
326 } else {
327 SkChopQuadAt(pts, tmp0, tmin);
328 if (tmax == 1) {
329 sectionPts = tmp0 + 2;
330 } else {
331 SkChopQuadAt(tmp0 + 2, tmp1, (tmax - tmin) / (1 - tmin));
332 sectionPts = tmp1;
333 }
334 }
335
336 // For quads, max distance from baseline is always at t=0.5.
337 SkPoint p;
338 p = SkEvalQuadAt(sectionPts, 0.5f);
339
340 // Get distance of p to baseline
341 const SkPoint n = {sectionPts[2].fY - sectionPts[0].fY,
342 sectionPts[0].fX - sectionPts[2].fX};
343 const float d = std::abs((p - sectionPts[0]).dot(n)) / n.length();
344
345 // Check distance is within specified tolerance
346 REPORTER_ASSERT(r, d <= (1.f / kIntolerance) + SK_ScalarNearlyZero);
347 }
348 }, maxExponent);
349}