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