1 /*
2 * Copyright 2019 Google LLC
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 "gm/gm.h"
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkData.h"
12 #include "include/core/SkFont.h"
13 #include "include/core/SkPaint.h"
14 #include "include/core/SkSize.h"
15 #include "include/core/SkString.h"
16 #include "include/core/SkSurface.h"
17 #include "include/effects/SkGradientShader.h"
18 #include "include/effects/SkImageFilters.h"
19 #include "include/effects/SkRuntimeEffect.h"
20 #include "include/utils/SkRandom.h"
21 #include "src/core/SkRuntimeEffectPriv.h"
22 #include "src/gpu/GrCaps.h"
23 #include "src/gpu/GrRecordingContextPriv.h"
24 #include "tools/Resources.h"
25 #include "tools/ToolUtils.h"
26
27 static constexpr int kBoxSize = 100;
28 static constexpr int kPadding = 5;
29 static constexpr int kLabelHeight = 15;
30
next_column(SkCanvas * canvas)31 static void next_column(SkCanvas* canvas) {
32 canvas->translate(kBoxSize + kPadding, 0);
33 }
34
next_row(SkCanvas * canvas)35 static void next_row(SkCanvas* canvas) {
36 canvas->restore();
37 canvas->translate(0, kBoxSize + kPadding + kLabelHeight);
38 canvas->save();
39 }
40
columns_to_width(int columns)41 static constexpr int columns_to_width(int columns) {
42 return (kPadding + kBoxSize) * columns + kPadding;
43 }
44
rows_to_height(int rows)45 static constexpr int rows_to_height(int rows) {
46 return (kPadding + kLabelHeight + kBoxSize) * rows + kPadding;
47 }
48
draw_label(SkCanvas * canvas,const char * label)49 static void draw_label(SkCanvas* canvas, const char* label) {
50 SkFont font(ToolUtils::create_portable_typeface());
51 SkPaint p(SkColors::kBlack);
52 SkRect bounds;
53 font.measureText(label, strlen(label), SkTextEncoding::kUTF8, &bounds);
54
55 canvas->drawSimpleText(label, strlen(label), SkTextEncoding::kUTF8,
56 (kBoxSize - bounds.width()) * 0.5f,
57 (kLabelHeight + bounds.height()) * 0.5f, font, p);
58 canvas->translate(0, kLabelHeight);
59 }
60
draw_shader(SkCanvas * canvas,sk_sp<SkShader> shader,bool allowRasterFallback=true)61 static SkBitmap draw_shader(SkCanvas* canvas, sk_sp<SkShader> shader,
62 bool allowRasterFallback = true) {
63 SkPaint paint;
64 paint.setShader(std::move(shader));
65
66 SkBitmap bitmap;
67 SkImageInfo info = SkImageInfo::MakeN32Premul({kBoxSize, kBoxSize});
68 auto surface = canvas->makeSurface(info);
69 if (allowRasterFallback && !surface) {
70 surface = SkSurface::MakeRaster(info);
71 }
72
73 if (surface) {
74 surface->getCanvas()->clear(SK_ColorWHITE);
75 surface->getCanvas()->scale(kBoxSize, kBoxSize);
76 surface->getCanvas()->drawRect({0, 0, 1, 1}, paint);
77
78 bitmap.allocPixels(info);
79 surface->readPixels(bitmap, 0, 0);
80
81 canvas->drawImage(bitmap.asImage(), 0, 0);
82 }
83 return bitmap;
84 }
85
86 /*
87 Test cases are inserted into the middle of this shader. The pasted expression is expected to
88 produce a single float. It can reference:
89
90 'x' : float in [xMin, xMax]
91 'xi' : int in [xMin, xMax]
92 'p' : float2 in [xMin, xMax] Lerps from (xMax, xMin) to (xMin, xMax)
93 'pi' : int2 in [xMin, xMax] Lerps from (xMax, xMin) to (xMin, xMax)
94 (helpful for intrinsics with a mix of scalar/vector params)
95 'v1' : float2(1)
96 'v2' : float2(2)
97 */
make_unary_sksl_1d(const char * fn)98 static SkString make_unary_sksl_1d(const char* fn) {
99 return SkStringPrintf(
100 "uniform float xScale; uniform float xBias;"
101 "uniform float yScale; uniform float yBias;"
102 "half4 main(float2 p) {"
103 " const float2 v1 = float2(1);"
104 " const float2 v2 = float2(2);"
105 " p = float2(p.x, 1 - p.x) * xScale + xBias;"
106 " float x = p.x;"
107 " int2 pi = int2(floor(p));"
108 " int xi = pi.x;"
109 " float y = float(%s) * yScale + yBias;"
110 " return y.xxx1;"
111 "}",
112 fn);
113 }
114
115 // Draws one row of boxes, then advances the canvas translation vertically
plot(SkCanvas * canvas,const char * fn,float xMin,float xMax,float yMin,float yMax,const char * label=nullptr,bool requireES3=false)116 static void plot(SkCanvas* canvas,
117 const char* fn,
118 float xMin,
119 float xMax,
120 float yMin,
121 float yMax,
122 const char* label = nullptr,
123 bool requireES3 = false) {
124 canvas->save();
125
126 draw_label(canvas, label ? label : fn);
127
128 auto [effect, error] = SkRuntimeEffect::MakeForShader(
129 make_unary_sksl_1d(fn),
130 requireES3 ? SkRuntimeEffectPriv::ES3Options() : SkRuntimeEffect::Options{});
131 if (!effect) {
132 SkDebugf("Error: %s\n", error.c_str());
133 return;
134 }
135
136 SkRuntimeShaderBuilder builder(effect);
137 builder.uniform("xScale") = xMax - xMin;
138 builder.uniform("xBias") = xMin;
139 builder.uniform("yScale") = 1.0f / (yMax - yMin);
140 builder.uniform("yBias") = -yMin / (yMax - yMin);
141
142 SkBitmap bitmap = draw_shader(canvas,
143 builder.makeShader(/*localMatrix=*/nullptr, /*isOpaque=*/false),
144 /*allowRasterFallback=*/!requireES3);
145 if (!bitmap.empty()) {
146 // Plot.
147 SkPaint plotPaint({ 0.0f, 0.5f, 0.0f, 1.0f });
148 SkPoint pts[kBoxSize];
149 for (int x = 0; x < kBoxSize; ++x) {
150 SkColor c = bitmap.getColor(x, 0);
151 SkScalar y = (1 - (SkColorGetR(c) / 255.0f)) * kBoxSize;
152 pts[x].set(x + 0.5f, y);
153 }
154 plotPaint.setAntiAlias(true);
155 canvas->drawPoints(SkCanvas::kPolygon_PointMode, kBoxSize, pts, plotPaint);
156 }
157
158 canvas->restore();
159 next_column(canvas);
160 }
161
plot_es3(SkCanvas * canvas,const char * fn,float xMin,float xMax,float yMin,float yMax,const char * label=nullptr)162 static void plot_es3(SkCanvas* canvas,
163 const char* fn,
164 float xMin,
165 float xMax,
166 float yMin,
167 float yMax,
168 const char* label = nullptr) {
169 plot(canvas, fn, xMin, xMax, yMin, yMax, label, /*requireES3=*/true);
170 }
171
172 // The OpenGL ES Shading Language, Version 1.00, Section 8.1
173 DEF_SIMPLE_GM(runtime_intrinsics_trig,
174 canvas,
175 columns_to_width(3),
176 rows_to_height(5)) {
177 const float kPI = SK_FloatPI, kTwoPI = 2 * SK_FloatPI, kPIOverTwo = SK_FloatPI / 2;
178
179 canvas->translate(kPadding, kPadding);
180 canvas->save();
181
182 plot(canvas, "radians(x)", 0.0f, 360.0f, 0.0f, kTwoPI);
183 plot(canvas, "degrees(x)", 0.0f, kTwoPI, 0.0f, 360.0f);
184 next_row(canvas);
185
186 plot(canvas, "sin(x)", 0.0f, kTwoPI, -1.0f, 1.0f);
187 plot(canvas, "cos(x)", 0.0f, kTwoPI, -1.0f, 1.0f);
188 plot(canvas, "tan(x)", 0.0f, kPI, -10.0f, 10.0f);
189 next_row(canvas);
190
191 plot(canvas, "asin(x)", -1.0f, 1.0f, -kPIOverTwo, kPIOverTwo);
192 plot(canvas, "acos(x)", -1.0f, 1.0f, 0.0f, kPI);
193 plot(canvas, "atan(x)", -10.0f, 10.0f, -kPIOverTwo, kPIOverTwo);
194 next_row(canvas);
195
196 plot(canvas, "atan(0.1, x)", -1.0f, 1.0f, 0.0f, kPI);
197 plot(canvas, "atan(-0.1, x)", -1.0f, 1.0f, -kPI, 0.0f);
198 next_row(canvas);
199
200 plot(canvas, "atan(x, 0.1)", -1.0f, 1.0f, -kPIOverTwo, kPIOverTwo);
201 plot(canvas, "atan(x, -0.1)", -1.0f, 1.0f, -kPI, kPI);
202 next_row(canvas);
203 }
204
205 // The OpenGL ES Shading Language, Version 3.00, Section 8.1
206 DEF_SIMPLE_GPU_GM_CAN_FAIL(runtime_intrinsics_trig_es3,
207 ctx, canvas, errorMsg,
208 columns_to_width(3),
209 rows_to_height(2)) {
210 if (!ctx->priv().caps()->shaderCaps()->supportsSkSLES3()) {
211 *errorMsg = "SkSL ES3 is not supported.";
212 return skiagm::DrawResult::kSkip;
213 }
214
215 canvas->translate(kPadding, kPadding);
216 canvas->save();
217
218 plot_es3(canvas, "sinh(x)", -2.0f, 2.0f, -4.0f, 4.0f);
219 plot_es3(canvas, "cosh(x)", -2.0f, 2.0f, 0.0f, 4.0f);
220 plot_es3(canvas, "tanh(x)", -2.0f, 2.0f, -1.0f, 1.0f);
221 next_row(canvas);
222
223 if (ctx->priv().caps()->shaderCaps()->inverseHyperbolicSupport()) {
224 plot_es3(canvas, "asinh(x)", -2.0f, 2.0f, -2.0f, 2.0f);
225 plot_es3(canvas, "acosh(x)", 0.0f, 5.0f, 0.0f, 3.0f);
226 plot_es3(canvas, "atanh(x)", -1.0f, 1.0f, -4.0f, 4.0f);
227 }
228 next_row(canvas);
229
230 return skiagm::DrawResult::kOk;
231 }
232
233 // The OpenGL ES Shading Language, Version 1.00, Section 8.2
234 DEF_SIMPLE_GM(runtime_intrinsics_exponential,
235 canvas,
236 columns_to_width(2),
237 rows_to_height(5)) {
238 canvas->translate(kPadding, kPadding);
239 canvas->save();
240
241 plot(canvas, "pow(x, 3)", 0.0f, 8.0f, 0.0f, 500.0f);
242 plot(canvas, "pow(x, -3)", 0.0f, 4.0f, 0.0f, 10.0f);
243 next_row(canvas);
244
245 plot(canvas, "pow(0.9, x)", -10.0f, 10.0f, 0.0f, 3.0f);
246 plot(canvas, "pow(1.1, x)", -10.0f, 10.0f, 0.0f, 3.0f);
247 next_row(canvas);
248
249 plot(canvas, "exp(x)", -1.0f, 7.0f, 0.0f, 1000.0f);
250 plot(canvas, "log(x)", 0.0f, 2.5f, -4.0f, 1.0f);
251 next_row(canvas);
252
253 plot(canvas, "exp2(x)", -1.0f, 7.0f, 0.0f, 130.0f);
254 plot(canvas, "log2(x)", 0.0f, 4.0f, -4.0f, 2.0f);
255 next_row(canvas);
256
257 plot(canvas, "sqrt(x)", 0.0f, 25.0f, 0.0f, 5.0f);
258 plot(canvas, "inversesqrt(x)", 0.0f, 25.0f, 0.2f, 4.0f);
259 next_row(canvas);
260 }
261
262 // The OpenGL ES Shading Language, Version 1.00, Section 8.3
263 DEF_SIMPLE_GM(runtime_intrinsics_common,
264 canvas,
265 columns_to_width(6),
266 rows_to_height(7)) {
267 canvas->translate(kPadding, kPadding);
268 canvas->save();
269
270 plot(canvas, "abs(x)", -10.0f, 10.0f, 0.0f, 10.0f);
271 plot(canvas, "sign(x)", -1.0f, 1.0f, -1.5f, 1.5f);
272 next_row(canvas);
273
274 plot(canvas, "floor(x)", -3.0f, 3.0f, -4.0f, 4.0f);
275 plot(canvas, "ceil(x)", -3.0f, 3.0f, -4.0f, 4.0f);
276 plot(canvas, "fract(x)", -3.0f, 3.0f, 0.0f, 1.0f);
277 plot(canvas, "mod(x, 2)", -4.0f, 4.0f, -2.0f, 2.0f, "mod(scalar)");
278 plot(canvas, "mod(p, -2).x", -4.0f, 4.0f, -2.0f, 2.0f, "mod(mixed)" );
279 plot(canvas, "mod(p, v2).x", -4.0f, 4.0f, -2.0f, 2.0f, "mod(vector)");
280 next_row(canvas);
281
282 plot(canvas, "min(x, 1)", 0.0f, 2.0f, 0.0f, 2.0f, "min(scalar)");
283 plot(canvas, "min(p, 1).x", 0.0f, 2.0f, 0.0f, 2.0f, "min(mixed)" );
284 plot(canvas, "min(p, v1).x", 0.0f, 2.0f, 0.0f, 2.0f, "min(vector)");
285 plot(canvas, "max(x, 1)", 0.0f, 2.0f, 0.0f, 2.0f, "max(scalar)");
286 plot(canvas, "max(p, 1).x", 0.0f, 2.0f, 0.0f, 2.0f, "max(mixed)" );
287 plot(canvas, "max(p, v1).x", 0.0f, 2.0f, 0.0f, 2.0f, "max(vector)");
288 next_row(canvas);
289
290 plot(canvas, "clamp(x, 1, 2)", 0.0f, 3.0f, 0.0f, 3.0f, "clamp(scalar)");
291 plot(canvas, "clamp(p, 1, 2).x", 0.0f, 3.0f, 0.0f, 3.0f, "clamp(mixed)" );
292 plot(canvas, "clamp(p, v1, v2).x", 0.0f, 3.0f, 0.0f, 3.0f, "clamp(vector)");
293 plot(canvas, "saturate(x)", -1.0f, 2.0f, -0.5f, 1.5f);
294 next_row(canvas);
295
296 plot(canvas, "mix(1, 2, x)", -1.0f, 2.0f, 0.0f, 3.0f, "mix(scalar)");
297 plot(canvas, "mix(v1, v2, x).x", -1.0f, 2.0f, 0.0f, 3.0f, "mix(mixed)" );
298 plot(canvas, "mix(v1, v2, p).x", -1.0f, 2.0f, 0.0f, 3.0f, "mix(vector)");
299 next_row(canvas);
300
301 plot(canvas, "step(1, x)", 0.0f, 2.0f, -0.5f, 1.5f, "step(scalar)");
302 plot(canvas, "step(1, p).x", 0.0f, 2.0f, -0.5f, 1.5f, "step(mixed)" );
303 plot(canvas, "step(v1, p).x", 0.0f, 2.0f, -0.5f, 1.5f, "step(vector)");
304 plot(canvas, "smoothstep(1, 2, x)", 0.5f, 2.5f, -0.5f, 1.5f, "smooth(scalar)");
305 plot(canvas, "smoothstep(1, 2, p).x", 0.5f, 2.5f, -0.5f, 1.5f, "smooth(mixed)" );
306 plot(canvas, "smoothstep(v1, v2, p).x", 0.5f, 2.5f, -0.5f, 1.5f, "smooth(vector)");
307 next_row(canvas);
308
309 plot(canvas, "floor(p).x", -3.0f, 3.0f, -4.0f, 4.0f);
310 plot(canvas, "ceil(p).x", -3.0f, 3.0f, -4.0f, 4.0f);
311 plot(canvas, "floor(p).y", -3.0f, 3.0f, -4.0f, 4.0f);
312 plot(canvas, "ceil(p).y", -3.0f, 3.0f, -4.0f, 4.0f);
313 next_row(canvas);
314 }
315
316 // The OpenGL ES Shading Language, Version 3.00, Section 8.1
317 DEF_SIMPLE_GPU_GM_CAN_FAIL(runtime_intrinsics_common_es3,
318 ctx, canvas, errorMsg,
319 columns_to_width(6),
320 rows_to_height(5)) {
321 if (!ctx->priv().caps()->shaderCaps()->supportsSkSLES3()) {
322 *errorMsg = "SkSL ES3 is not supported.";
323 return skiagm::DrawResult::kSkip;
324 }
325
326 canvas->translate(kPadding, kPadding);
327 canvas->save();
328
329 plot_es3(canvas, "floatBitsToInt(x)", -2, 2, -2'000'000'000, 2'000'000'000,
330 "floatBitsToInt(s)");
331 plot_es3(canvas, "floatBitsToInt(p).x", -2, 2, -2'000'000'000, 2'000'000'000,
332 "floatBitsToInt(v)");
333 plot_es3(canvas, "floatBitsToUint(x)", -2, 2, 0, 4'000'000'000,
334 "floatBitsToUint(s)");
335 plot_es3(canvas, "floatBitsToUint(p).x", -2, 2, 0, 4'000'000'000,
336 "floatBitsToUint(v)");
337 next_row(canvas);
338
339 plot_es3(canvas, "intBitsToFloat(xi)", -2'000'000'000, 2'000'000'000, -2, 2,
340 "intBitsToFloat(s)");
341 plot_es3(canvas, "intBitsToFloat(pi).x", -2'000'000'000, 2'000'000'000, -2, 2,
342 "intBitsToFloat(v)");
343 plot_es3(canvas, "uintBitsToFloat(uint(xi))", 0, 4'000'000'000, -2, 2,
344 "uintBitsToFloat(s)");
345 plot_es3(canvas, "uintBitsToFloat(uint2(pi)).x", 0, 4'000'000'000, -2, 2,
346 "uintBitsToFloat(v)");
347 next_row(canvas);
348
349 plot_es3(canvas, "trunc(x)", -2, 2, -3, 3);
350 plot_es3(canvas, "trunc(p).x", -2, 2, -3, 3);
351 plot_es3(canvas, "round(x)", -2, 2, -3, 3);
352 plot_es3(canvas, "round(p).x", -2, 2, -3, 3);
353 plot_es3(canvas, "roundEven(x)", -2, 2, -3, 3);
354 plot_es3(canvas, "roundEven(p).x", -2, 2, -3, 3);
355 next_row(canvas);
356
357 plot_es3(canvas, "min(xi, 1)", -2, 5, -3, 5, "min(int-scalar)");
358 plot_es3(canvas, "min(pi, 1).x", -2, 5, -3, 5, "min(int-mixed)" );
359 plot_es3(canvas, "min(pi, int2(1)).x", -2, 5, -3, 5, "min(int-vector)");
360 plot_es3(canvas, "max(xi, 1)", -2, 5, -3, 5, "max(int-scalar)");
361 plot_es3(canvas, "max(pi, 1).x", -2, 5, -3, 5, "max(int-mixed)" );
362 plot_es3(canvas, "max(pi, int2(1)).x", -2, 5, -3, 5, "max(int-vector)");
363 next_row(canvas);
364
365 plot_es3(canvas, "clamp(xi, 1, 3)", -1, 5, -1, 5, "clamp(int-scalar)");
366 plot_es3(canvas, "clamp(pi, 1, 3).x", -1, 5, -1, 5, "clamp(int-mixed)" );
367 plot_es3(canvas, "clamp(pi, int2(1), int2(3)).x", -1, 5, -1, 5, "clamp(int-vector)");
368 plot_es3(canvas, "mix(p.x, p.y, (x>0) )", -1, 2, 0, 3, "mix(scalar, bool)");
369 plot_es3(canvas, "mix(p.yx, p, (x>0).xx).x", -1, 2, 0, 3, "mix(vector, bool)");
370 next_row(canvas);
371
372 return skiagm::DrawResult::kOk;
373 }
374
375
376 // The OpenGL ES Shading Language, Version 1.00, Section 8.4
377 DEF_SIMPLE_GM(runtime_intrinsics_geometric,
378 canvas,
379 columns_to_width(4),
380 rows_to_height(5)) {
381 canvas->translate(kPadding, kPadding);
382 canvas->save();
383
384 plot(canvas, "length(x)", -1.0f, 1.0f, -0.5f, 1.5f);
385 plot(canvas, "length(p)", 0.0f, 1.0f, 0.5f, 1.5f);
386 plot(canvas, "distance(x, 0)", -1.0f, 1.0f, -0.5f, 1.5f);
387 plot(canvas, "distance(p, v1)", 0.0f, 1.0f, 0.5f, 1.5f);
388 next_row(canvas);
389
390 plot(canvas, "dot(x, 2)", -1.0f, 1.0f, -2.5f, 2.5f);
391 plot(canvas, "dot(p, p.y1)", -1.0f, 1.0f, -2.5f, 0.5f);
392 next_row(canvas);
393
394 plot(canvas, "cross(p.xy1, p.y1x).x", 0.0f, 1.0f, -1.0f, 1.0f);
395 plot(canvas, "cross(p.xy1, p.y1x).y", 0.0f, 1.0f, -1.0f, 1.0f);
396 plot(canvas, "cross(p.xy1, p.y1x).z", 0.0f, 1.0f, -1.0f, 1.0f);
397 next_row(canvas);
398
399 plot(canvas, "normalize(x)", -2.0f, 2.0f, -1.5f, 1.5f);
400 plot(canvas, "normalize(p).x", 0.0f, 2.0f, 0.0f, 1.0f);
401 plot(canvas, "normalize(p).y", 0.0f, 2.0f, 0.0f, 1.0f);
402 plot(canvas, "faceforward(v1, p.x0, v1.x0).x", -1.0f, 1.0f, -1.5f, 1.5f, "faceforward");
403 next_row(canvas);
404
405 plot(canvas, "reflect(p.x1, v1.0x).x", -1.0f, 1.0f, -1.0f, 1.0f, "reflect(horiz)");
406 plot(canvas, "reflect(p.x1, normalize(v1)).y", -1.0f, 1.0f, -1.0f, 1.0f, "reflect(diag)" );
407 plot(canvas, "refract(v1.x0, v1.0x, x).x", 0.0f, 1.0f, -1.0f, 1.0f, "refract().x");
408 plot(canvas, "refract(v1.x0, v1.0x, x).y", 0.0f, 1.0f, -1.0f, 1.0f, "refract().y");
409 next_row(canvas);
410 }
411
412 #define SKSL_MATRIX_SELECTORS \
413 "inline float2 sel2(float x) {" \
414 " return float2(" \
415 " x < 0.5 ? 1 : 0," \
416 " x >= 0.5 ? 1 : 0);" \
417 "}" \
418 "inline float3 sel3(float x) {" \
419 " return float3(" \
420 " x < 0.33 ? 1 : 0," \
421 " x >= 0.33 && x < 0.66 ? 1 : 0," \
422 " x >= 0.66 ? 1 : 0);" \
423 "}" \
424 "inline float4 sel4(float x) {" \
425 " return float4(" \
426 " x < 0.25 ? 1 : 0," \
427 " x >= 0.25 && x < 0.5 ? 1 : 0," \
428 " x >= 0.5 && x < 0.75 ? 1 : 0," \
429 " x >= 0.75 ? 1 : 0);" \
430 "}"
431
432 // Shader for testing matrixCompMult intrinsic
make_matrix_comp_mult_sksl(int dim)433 static SkString make_matrix_comp_mult_sksl(int dim) {
434 return SkStringPrintf(
435 "uniform float%dx%d m1;" // dim, dim
436 "uniform float%dx%d m2;" // dim, dim
437 SKSL_MATRIX_SELECTORS
438 "half4 main(float2 p) {"
439 " float%d colSel = sel%d(p.x);" // dim, dim
440 " float%d rowSel = sel%d(p.y);" // dim, dim
441 " float%d col = matrixCompMult(m1, m2) * colSel;" // dim
442 " float v = dot(col, rowSel);"
443 " return v.xxx1;"
444 "}", dim, dim, dim, dim, dim, dim, dim, dim, dim);
445 }
446
447 template <int N>
plot_matrix_comp_mult(SkCanvas * canvas,std::array<float,N * N> mtx1,std::array<float,N * N> mtx2,const char * label)448 static void plot_matrix_comp_mult(SkCanvas* canvas,
449 std::array<float, N*N> mtx1,
450 std::array<float, N*N> mtx2,
451 const char* label) {
452 canvas->save();
453
454 draw_label(canvas, label);
455
456 auto [effect, error] = SkRuntimeEffect::MakeForShader(make_matrix_comp_mult_sksl(N));
457 if (!effect) {
458 SkDebugf("Error: %s\n", error.c_str());
459 return;
460 }
461
462 SkRuntimeShaderBuilder builder(effect);
463 builder.uniform("m1") = mtx1;
464 builder.uniform("m2") = mtx2;
465
466 draw_shader(canvas, builder.makeShader(/*localMatrix=*/nullptr, /*isOpaque=*/false));
467
468 canvas->restore();
469 next_column(canvas);
470 }
471
472 // Shader for testing inverse() intrinsic
make_matrix_inverse_sksl(int dim)473 static SkString make_matrix_inverse_sksl(int dim) {
474 return SkStringPrintf(
475 "uniform float scale; uniform float bias;"
476 "uniform float%dx%d m;" // dim, dim
477 SKSL_MATRIX_SELECTORS
478 "half4 main(float2 p) {"
479 " float%d colSel = sel%d(p.x);" // dim, dim
480 " float%d rowSel = sel%d(p.y);" // dim, dim
481 " float%d col = inverse(m) * colSel;" // dim
482 " float v = dot(col, rowSel) * scale + bias;"
483 " return v.xxx1;"
484 "}", dim, dim, dim, dim, dim, dim, dim);
485 }
486
487 template <int N>
plot_matrix_inverse(SkCanvas * canvas,std::array<float,N * N> mtx,const char * label)488 static void plot_matrix_inverse(SkCanvas* canvas, std::array<float, N*N> mtx, const char* label) {
489 canvas->save();
490
491 draw_label(canvas, label);
492
493 auto [effect, error] = SkRuntimeEffect::MakeForShader(make_matrix_inverse_sksl(N));
494 if (!effect) {
495 SkDebugf("Error: %s\n", error.c_str());
496 return;
497 }
498
499 SkRuntimeShaderBuilder builder(effect);
500 builder.uniform("scale") = 0.5f;
501 builder.uniform("bias") = 0.5f;
502 builder.uniform("m") = mtx;
503
504 draw_shader(canvas, builder.makeShader(/*localMatrix=*/nullptr, /*isOpaque=*/false));
505
506 canvas->restore();
507 next_column(canvas);
508 }
509
510 // The OpenGL ES Shading Language, Version 1.00, Section 8.5
511 DEF_SIMPLE_GM(runtime_intrinsics_matrix,
512 canvas,
513 columns_to_width(3),
514 rows_to_height(2)) {
515 canvas->translate(kPadding, kPadding);
516 canvas->save();
517
518 // Random pairs of matrices where the elements of matrixCompMult(m1, m2) lie in [0, 1]
519 plot_matrix_comp_mult<2>(canvas,
520 {1.00f, 0.0f, 2.0f, 0.5f},
521 {0.75f, 2.0f, 0.2f, 1.2f},
522 "compMult(2x2)");
523
524 plot_matrix_comp_mult<3>(canvas,
525 {1.00f, 0.0f, 2.0f, 0.5f, -1.0f, -2.0f, -0.5f, 4.00f, 0.25f},
526 {0.75f, 2.0f, 0.2f, 1.2f, -0.8f, -0.1f, -1.8f, 0.25f, 2.00f},
527 "compMult(3x3)");
528
529 plot_matrix_comp_mult<4>(canvas,
530 {1.00f, 0.0f, 2.0f, 0.5f, -1.0f, -2.0f, -0.5f, 4.00f, 0.25f, 0.05f,
531 10.00f, -0.66f, -1.0f, -0.5f, 0.5f, 0.66f},
532 {0.75f, 2.0f, 0.2f, 1.2f, -0.8f, -0.1f, -1.8f, 0.25f, 2.00f, 2.00f,
533 0.03f, -1.00f, -1.0f, -0.5f, 1.7f, 0.66f},
534 "compMult(4x4)");
535 next_row(canvas);
536
537 // Random, invertible matrices where the elements of inverse(m) lie in [-1, 1]
538 plot_matrix_inverse<2>(canvas,
539 { 1.20f, 0.68f,
540 -0.27f, -1.55f},
541 "inverse(2x2)");
542
543 plot_matrix_inverse<3>(canvas,
544 {-1.13f, -2.96f, -0.14f,
545 1.45f, -1.88f, -1.02f,
546 -2.54f, -2.58f, -1.17f},
547 "inverse(3x3)");
548
549 plot_matrix_inverse<4>(canvas,
550 {-1.51f, -3.95f, -0.19f, 1.93f,
551 -2.51f, -1.35f, -3.39f, -3.45f,
552 -1.56f, 1.61f, -0.22f, -1.08f,
553 -2.81f, -2.14f, -0.09f, 3.00f},
554 "inverse(4x4)");
555 next_row(canvas);
556 }
557
558 /*
559 Specialized shader for testing relational operators.
560 */
make_bvec_sksl(const char * type,const char * fn)561 static SkString make_bvec_sksl(const char* type, const char* fn) {
562 // We use negative floats, to ensure that the integer variants are working with the correct
563 // interpretation of the data.
564 return SkStringPrintf(
565 "uniform %s2 v1;"
566 "half4 main(float2 p) {"
567 " p.x = p.x < 0.33 ? -3.0 : (p.x < 0.66 ? -2.0 : -1.0);"
568 " p.y = p.y < 0.33 ? -3.0 : (p.y < 0.66 ? -2.0 : -1.0);"
569 " bool2 cmp = %s;"
570 " return half4(cmp.x ? 1.0 : 0.0, cmp.y ? 1.0 : 0.0, 0, 1);"
571 "}",
572 type, fn);
573 }
574
575 template <typename T = float>
plot_bvec(SkCanvas * canvas,const char * fn,const char * label)576 static void plot_bvec(SkCanvas* canvas, const char* fn, const char* label) {
577 canvas->save();
578
579 draw_label(canvas, label);
580
581 const char* type = std::is_integral<T>::value ? "int" : "float";
582 auto [effect, error] = SkRuntimeEffect::MakeForShader(make_bvec_sksl(type, fn));
583 if (!effect) {
584 SkDebugf("Error: %s\n", error.c_str());
585 return;
586 }
587
588 T uniformData[2] = { -2, -2 };
589 sk_sp<SkData> uniforms = SkData::MakeWithCopy(uniformData, sizeof(uniformData));
590
591 draw_shader(canvas,
592 effect->makeShader(uniforms,
593 /*children=*/nullptr,
594 /*childCount=*/0,
595 /*localMatrix=*/nullptr,
596 /*isOpaque=*/false));
597
598 canvas->restore();
599 next_column(canvas);
600 }
601
602 // The OpenGL ES Shading Language, Version 1.00, Section 8.6
603 DEF_SIMPLE_GM(runtime_intrinsics_relational,
604 canvas,
605 columns_to_width(4),
606 rows_to_height(6)) {
607 canvas->translate(kPadding, kPadding);
608 canvas->save();
609
610 plot_bvec<float>(canvas, "lessThan(p, v1)", "lessThan");
611 plot_bvec<int> (canvas, "lessThan(int2(p), v1)", "lessThan(int)");
612 plot_bvec<float>(canvas, "lessThanEqual(p, v1)", "lessThanEqual");
613 plot_bvec<int> (canvas, "lessThanEqual(int2(p), v1)", "lessThanEqual(int)");
614 next_row(canvas);
615
616 plot_bvec<float>(canvas, "greaterThan(p, v1)", "greaterThan");
617 plot_bvec<int> (canvas, "greaterThan(int2(p), v1)", "greaterThan(int)");
618 plot_bvec<float>(canvas, "greaterThanEqual(p, v1)", "greaterThanEqual");
619 plot_bvec<int> (canvas, "greaterThanEqual(int2(p), v1)", "greaterThanEqual(int)");
620 next_row(canvas);
621
622 plot_bvec<float>(canvas, "equal(p, v1)", "equal");
623 plot_bvec<int> (canvas, "equal(int2(p), v1)", "equal(int)");
624 plot_bvec<float>(canvas, "notEqual(p, v1)", "notEqual");
625 plot_bvec<int> (canvas, "notEqual(int2(p), v1)", "notEqual(int)");
626 next_row(canvas);
627
628 plot_bvec(canvas, "equal( lessThanEqual(p, v1), greaterThanEqual(p, v1))", "equal(bvec)");
629 plot_bvec(canvas, "notEqual(lessThanEqual(p, v1), greaterThanEqual(p, v1))", "notequal(bvec)");
630 next_row(canvas);
631
632 plot_bvec(canvas, "not(notEqual(p, v1))", "not(notEqual)");
633 plot_bvec(canvas, "not(equal(p, v1))", "not(equal)");
634 next_row(canvas);
635
636 plot_bvec(canvas, "bool2(any(equal(p, v1)))", "any(equal)");
637 plot_bvec(canvas, "bool2(all(equal(p, v1)))", "all(equal)");
638 next_row(canvas);
639 }
640