blob: f1ecb201f00225973456cbf6fa9ca25e7092ef8d [file] [log] [blame]
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001/*
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
Mike Reed46f5c5f2020-02-20 15:42:29 -05008#include "include/core/SkM44.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "src/sksl/SkSLCompiler.h"
Brian Osmanbe0b3b72021-01-06 14:27:35 -050010#include "src/sksl/SkSLExternalFunction.h"
Brian Osmanf4a77732020-12-28 09:03:00 -050011#include "src/sksl/SkSLVMGenerator.h"
Ethan Nicholas91164d12019-05-15 15:29:54 -040012#include "src/utils/SkJSON.h"
Ethan Nicholas0e9401d2019-03-21 11:05:37 -040013
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "tests/Test.h"
Ethan Nicholas0e9401d2019-03-21 11:05:37 -040015
Brian Osmanf4a77732020-12-28 09:03:00 -050016struct ProgramBuilder {
17 ProgramBuilder(skiatest::Reporter* r, const char* src)
18 : fCaps(GrContextOptions{}), fCompiler(&fCaps) {
19 SkSL::Program::Settings settings;
Brian Osman54515b72021-01-07 14:38:08 -050020 // The SkSL inliner is well tested in other contexts. Here, we disable inlining entirely,
21 // to stress-test the VM generator's handling of function calls with varying signatures.
22 settings.fInlineThreshold = 0;
Brian Osman11233d22021-01-11 10:39:58 -050023 // For convenience, so we can test functions other than (and not called by) main.
24 settings.fRemoveDeadFunctions = false;
Brian Osman54515b72021-01-07 14:38:08 -050025
Brian Osmanf4a77732020-12-28 09:03:00 -050026 fProgram = fCompiler.convertProgram(SkSL::Program::kGeneric_Kind, SkSL::String(src),
27 settings);
28 if (!fProgram) {
29 ERRORF(r, "Program failed to compile:\n%s\n%s\n", src, fCompiler.errorText().c_str());
Brian Osmanb08cc022020-04-02 11:38:40 -040030 }
31 }
Brian Osmanf4a77732020-12-28 09:03:00 -050032
33 operator bool() const { return fProgram != nullptr; }
34 SkSL::Program& operator*() { return *fProgram; }
35
36 GrShaderCaps fCaps;
37 SkSL::Compiler fCompiler;
38 std::unique_ptr<SkSL::Program> fProgram;
39};
40
Brian Osmanf4a77732020-12-28 09:03:00 -050041static void verify_values(skiatest::Reporter* r,
42 const char* src,
43 const float* actual,
44 const float* expected,
45 int N,
Mike Klein606488a2021-01-07 13:55:00 -060046 bool exactCompare) {
47 auto exact_equiv = [](float x, float y) {
48 return x == y
49 || (isnan(x) && isnan(y));
Brian Osmanf4a77732020-12-28 09:03:00 -050050 };
51
Mike Klein606488a2021-01-07 13:55:00 -060052 bool valid = true;
53 for (int i = 0; i < N; ++i) {
54 if (exactCompare && !exact_equiv(actual[i], expected[i])) {
55 valid = false;
56 }
57 if (!exactCompare && !SkScalarNearlyEqual(actual[i], expected[i])) {
58 valid = false;
59 }
60 }
61
Brian Osmanf4a77732020-12-28 09:03:00 -050062 if (!valid) {
63 printf("for program: %s\n", src);
64 printf(" expected (");
65 const char* separator = "";
66 for (int i = 0; i < N; ++i) {
67 printf("%s%f", separator, expected[i]);
68 separator = ", ";
69 }
70 printf("), but received (");
71 separator = "";
72 for (int i = 0; i < N; ++i) {
73 printf("%s%f", separator, actual[i]);
74 separator = ", ";
75 }
76 printf(")\n");
77 }
78 REPORTER_ASSERT(r, valid);
Brian Osmanb08cc022020-04-02 11:38:40 -040079}
80
Brian Osmance750362021-01-21 16:33:06 -050081void test(skiatest::Reporter* r, const char* src, float* in, const float* expected,
82 bool exactCompare = true) {
Brian Osmanf4a77732020-12-28 09:03:00 -050083 ProgramBuilder program(r, src);
84 if (!program) { return; }
85
86 const SkSL::FunctionDefinition* main = SkSL::Program_GetFunction(*program, "main");
87 REPORTER_ASSERT(r, main);
88
89 skvm::Builder b;
90 SkSL::SkVMSignature sig;
Brian Osmanc92df392021-01-11 13:16:28 -050091 SkSL::ProgramToSkVM(*program, *main, &b, /*uniforms=*/{}, &sig);
Brian Osmanf4a77732020-12-28 09:03:00 -050092 skvm::Program p = b.done();
93
Brian Osmanc92df392021-01-11 13:16:28 -050094 REPORTER_ASSERT(r, p.nargs() == (int)(sig.fParameterSlots + sig.fReturnSlots));
Brian Osmanf4a77732020-12-28 09:03:00 -050095
96 auto out = std::make_unique<float[]>(sig.fReturnSlots);
Brian Osmanc92df392021-01-11 13:16:28 -050097 auto args = std::make_unique<void*[]>(sig.fParameterSlots + sig.fReturnSlots);
Brian Osmanf4a77732020-12-28 09:03:00 -050098 for (size_t i = 0; i < sig.fParameterSlots; ++i) {
Brian Osmanc92df392021-01-11 13:16:28 -050099 args[i] = in + i;
Ethan Nicholas746035a2019-04-23 13:31:09 -0400100 }
Brian Osmanf4a77732020-12-28 09:03:00 -0500101 for (size_t i = 0; i < sig.fReturnSlots; ++i) {
Brian Osmanc92df392021-01-11 13:16:28 -0500102 args[sig.fParameterSlots + i] = out.get() + i;
Brian Osmanf4a77732020-12-28 09:03:00 -0500103 }
104
105 // TODO: Test with and without JIT?
106 p.eval(1, args.get());
107
Mike Klein606488a2021-01-07 13:55:00 -0600108 verify_values(r, src, out.get(), expected, sig.fReturnSlots, exactCompare);
Brian Osmanf4a77732020-12-28 09:03:00 -0500109}
110
Brian Osmance750362021-01-21 16:33:06 -0500111void test(skiatest::Reporter* r, const char* src,
112 float inR, float inG, float inB, float inA,
113 float exR, float exG, float exB, float exA) {
Brian Osmanf4a77732020-12-28 09:03:00 -0500114 ProgramBuilder program(r, src);
115 if (!program) { return; }
116
117 const SkSL::FunctionDefinition* main = SkSL::Program_GetFunction(*program, "main");
118 REPORTER_ASSERT(r, main);
119
120 skvm::Builder b;
Brian Osmanc92df392021-01-11 13:16:28 -0500121 SkSL::ProgramToSkVM(*program, *main, &b, /*uniforms=*/{});
Brian Osmanf4a77732020-12-28 09:03:00 -0500122 skvm::Program p = b.done();
123
124 // TODO: Test with and without JIT?
Brian Osmanc92df392021-01-11 13:16:28 -0500125 p.eval(1, &inR, &inG, &inB, &inA);
Brian Osmanf4a77732020-12-28 09:03:00 -0500126
127 float actual[4] = { inR, inG, inB, inA };
128 float expected[4] = { exR, exG, exB, exA };
129
Mike Klein606488a2021-01-07 13:55:00 -0600130 verify_values(r, src, actual, expected, 4, /*exactCompare=*/true);
Brian Osmanf4a77732020-12-28 09:03:00 -0500131
132 // TODO: vec_test with skvm
133}
134
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400135DEF_TEST(SkSLInterpreterAdd, r) {
136 test(r, "void main(inout half4 color) { color.r = color.r + color.g; }", 0.25, 0.75, 0, 0, 1,
137 0.75, 0, 0);
138 test(r, "void main(inout half4 color) { color += half4(1, 2, 3, 4); }", 4, 3, 2, 1, 5, 5, 5, 5);
139 test(r, "void main(inout half4 color) { half4 c = color; color += c; }", 0.25, 0.5, 0.75, 1,
140 0.5, 1, 1.5, 2);
John Stilesaf9b58e2021-01-13 17:48:36 -0500141 test(r, "void main(inout half4 color) { color.r = half(int(color.r) + int(color.g)); }", 1, 3, 0, 0,
Ethan Nicholas6f624122019-09-24 13:07:06 -0400142 4, 3, 0, 0);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400143}
144
145DEF_TEST(SkSLInterpreterSubtract, r) {
146 test(r, "void main(inout half4 color) { color.r = color.r - color.g; }", 1, 0.75, 0, 0, 0.25,
147 0.75, 0, 0);
148 test(r, "void main(inout half4 color) { color -= half4(1, 2, 3, 4); }", 5, 5, 5, 5, 4, 3, 2, 1);
149 test(r, "void main(inout half4 color) { half4 c = color; color -= c; }", 4, 3, 2, 1,
150 0, 0, 0, 0);
Ethan Nicholas354ecf32019-05-07 16:13:02 -0400151 test(r, "void main(inout half4 color) { color.x = -color.x; }", 4, 3, 2, 1, -4, 3, 2, 1);
152 test(r, "void main(inout half4 color) { color = -color; }", 4, 3, 2, 1, -4, -3, -2, -1);
John Stilesaf9b58e2021-01-13 17:48:36 -0500153 test(r, "void main(inout half4 color) { color.r = half(int(color.r) - int(color.g)); }", 3, 1, 0, 0,
Ethan Nicholas6f624122019-09-24 13:07:06 -0400154 2, 1, 0, 0);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400155}
156
157DEF_TEST(SkSLInterpreterMultiply, r) {
158 test(r, "void main(inout half4 color) { color.r = color.r * color.g; }", 2, 3, 0, 0, 6, 3, 0,
159 0);
160 test(r, "void main(inout half4 color) { color *= half4(1, 2, 3, 4); }", 2, 3, 4, 5, 2, 6, 12,
161 20);
162 test(r, "void main(inout half4 color) { half4 c = color; color *= c; }", 4, 3, 2, 1,
163 16, 9, 4, 1);
John Stilesaf9b58e2021-01-13 17:48:36 -0500164 test(r, "void main(inout half4 color) { color.r = half(int(color.r) * int(color.g)); }", 3, -2, 0, 0,
Ethan Nicholas6f624122019-09-24 13:07:06 -0400165 -6, -2, 0, 0);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400166}
167
168DEF_TEST(SkSLInterpreterDivide, r) {
169 test(r, "void main(inout half4 color) { color.r = color.r / color.g; }", 1, 2, 0, 0, 0.5, 2, 0,
170 0);
171 test(r, "void main(inout half4 color) { color /= half4(1, 2, 3, 4); }", 12, 12, 12, 12, 12, 6,
172 4, 3);
173 test(r, "void main(inout half4 color) { half4 c = color; color /= c; }", 4, 3, 2, 1,
174 1, 1, 1, 1);
John Stilesaf9b58e2021-01-13 17:48:36 -0500175 test(r, "void main(inout half4 color) { color.r = half(int(color.r) / int(color.g)); }", 8, -2, 0, 0,
Ethan Nicholas6f624122019-09-24 13:07:06 -0400176 -4, -2, 0, 0);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400177}
178
Ethan Nicholasd166d2e2019-09-23 11:43:45 -0400179DEF_TEST(SkSLInterpreterAnd, r) {
180 test(r, "void main(inout half4 color) { if (color.r > color.g && color.g > color.b) "
181 "color = half4(color.a); }", 2, 1, 0, 3, 3, 3, 3, 3);
182 test(r, "void main(inout half4 color) { if (color.r > color.g && color.g > color.b) "
183 "color = half4(color.a); }", 1, 1, 0, 3, 1, 1, 0, 3);
184 test(r, "void main(inout half4 color) { if (color.r > color.g && color.g > color.b) "
185 "color = half4(color.a); }", 2, 1, 1, 3, 2, 1, 1, 3);
John Stilesaf9b58e2021-01-13 17:48:36 -0500186 test(r, "half global; bool update() { global = 123; return true; }"
Ethan Nicholasd166d2e2019-09-23 11:43:45 -0400187 "void main(inout half4 color) { global = 0; if (color.r > color.g && update()) "
Brian Osman54515b72021-01-07 14:38:08 -0500188 "color = half4(color.a); color.a = global; }", 2, 1, 1, 3, 3, 3, 3, 123);
John Stilesaf9b58e2021-01-13 17:48:36 -0500189 test(r, "half global; bool update() { global = 123; return true; }"
Ethan Nicholasd166d2e2019-09-23 11:43:45 -0400190 "void main(inout half4 color) { global = 0; if (color.r > color.g && update()) "
Brian Osman54515b72021-01-07 14:38:08 -0500191 "color = half4(color.a); color.a = global; }", 1, 1, 1, 3, 1, 1, 1, 0);
Ethan Nicholasd166d2e2019-09-23 11:43:45 -0400192}
193
194DEF_TEST(SkSLInterpreterOr, r) {
195 test(r, "void main(inout half4 color) { if (color.r > color.g || color.g > color.b) "
196 "color = half4(color.a); }", 2, 1, 0, 3, 3, 3, 3, 3);
197 test(r, "void main(inout half4 color) { if (color.r > color.g || color.g > color.b) "
198 "color = half4(color.a); }", 1, 1, 0, 3, 3, 3, 3, 3);
199 test(r, "void main(inout half4 color) { if (color.r > color.g || color.g > color.b) "
200 "color = half4(color.a); }", 1, 1, 1, 3, 1, 1, 1, 3);
John Stilesaf9b58e2021-01-13 17:48:36 -0500201 test(r, "half global; bool update() { global = 123; return true; }"
Ethan Nicholasd166d2e2019-09-23 11:43:45 -0400202 "void main(inout half4 color) { global = 0; if (color.r > color.g || update()) "
Brian Osman54515b72021-01-07 14:38:08 -0500203 "color = half4(color.a); color.a = global; }", 1, 1, 1, 3, 3, 3, 3, 123);
John Stilesaf9b58e2021-01-13 17:48:36 -0500204 test(r, "half global; bool update() { global = 123; return true; }"
Ethan Nicholasd166d2e2019-09-23 11:43:45 -0400205 "void main(inout half4 color) { global = 0; if (color.r > color.g || update()) "
Brian Osman54515b72021-01-07 14:38:08 -0500206 "color = half4(color.a); color.a = global; }", 2, 1, 1, 3, 3, 3, 3, 0);
Ethan Nicholasd166d2e2019-09-23 11:43:45 -0400207}
208
Brian Osman29e013d2019-05-28 17:16:03 -0400209DEF_TEST(SkSLInterpreterMatrix, r) {
Brian Osman08a84962019-06-14 10:17:16 -0400210 float in[16];
211 float expected[16];
Brian Osman29e013d2019-05-28 17:16:03 -0400212
213 // Constructing matrix from scalar produces a diagonal matrix
Brian Osmanc0f2b642020-12-22 13:35:55 -0500214 in[0] = 2.0f;
215 expected[0] = 4.0f;
Brian Osman29e013d2019-05-28 17:16:03 -0400216 test(r, "float main(float x) { float4x4 m = float4x4(x); return m[1][1] + m[1][2] + m[2][2]; }",
Brian Osmanb23d66e2019-09-27 10:25:57 -0400217 in, expected);
Brian Osman29e013d2019-05-28 17:16:03 -0400218
Brian Osman29e013d2019-05-28 17:16:03 -0400219 // Constructing from a different-sized matrix fills the remaining space with the identity matrix
Brian Osmanc0f2b642020-12-22 13:35:55 -0500220 expected[0] = 3.0f;
Brian Osman29e013d2019-05-28 17:16:03 -0400221 test(r, "float main(float x) {"
Brian Osmanc0f2b642020-12-22 13:35:55 -0500222 "float2x2 m = float2x2(x);"
Brian Osman29e013d2019-05-28 17:16:03 -0400223 "float4x4 m2 = float4x4(m);"
224 "return m2[0][0] + m2[3][3]; }",
Brian Osmanb23d66e2019-09-27 10:25:57 -0400225 in, expected);
Brian Osman29e013d2019-05-28 17:16:03 -0400226
227 // Constructing a matrix from vectors or scalars fills in values in column-major order
228 in[0] = 1.0f;
229 in[1] = 2.0f;
230 in[2] = 4.0f;
231 in[3] = 8.0f;
232 expected[0] = 6.0f;
233 test(r, "float main(float4 v) { float2x2 m = float2x2(v); return m[0][1] + m[1][0]; }",
Brian Osmanb23d66e2019-09-27 10:25:57 -0400234 in, expected);
Brian Osman29e013d2019-05-28 17:16:03 -0400235
236 expected[0] = 10.0f;
237 test(r, "float main(float4 v) {"
238 "float2x2 m = float2x2(v.x, v.y, v.w, v.z);"
239 "return m[0][1] + m[1][0]; }",
Brian Osmanb23d66e2019-09-27 10:25:57 -0400240 in, expected);
Brian Osman29e013d2019-05-28 17:16:03 -0400241
Brian Osman1e855b22019-05-29 15:21:52 -0400242 // Initialize 16 values to be used as inputs to matrix tests
243 for (int i = 0; i < 16; ++i) { in[i] = (float)i; }
Brian Osman29e013d2019-05-28 17:16:03 -0400244
Brian Osman1e855b22019-05-29 15:21:52 -0400245 // M+M, M-S, S-M
246 for (int i = 0; i < 16; ++i) { expected[i] = (float)(2 * i); }
Brian Osmanb23d66e2019-09-27 10:25:57 -0400247 test(r, "float4x4 main(float4x4 m) { return m + m; }", in, expected);
Brian Osman1e855b22019-05-29 15:21:52 -0400248 for (int i = 0; i < 16; ++i) { expected[i] = (float)(i + 3); }
Brian Osmanb23d66e2019-09-27 10:25:57 -0400249 test(r, "float4x4 main(float4x4 m) { return m + 3.0; }", in, expected);
250 test(r, "float4x4 main(float4x4 m) { return 3.0 + m; }", in, expected);
Brian Osman1e855b22019-05-29 15:21:52 -0400251
252 // M-M, M-S, S-M
Brian Osmanc0f2b642020-12-22 13:35:55 -0500253 for (int i = 0; i < 4; ++i) { expected[i] = 4.0f; }
254 test(r, "float2x2 main(float2x2 m1, float2x2 m2) { return m2 - m1; }", in, expected);
Brian Osman1e855b22019-05-29 15:21:52 -0400255 for (int i = 0; i < 16; ++i) { expected[i] = (float)(i - 3); }
Brian Osmanb23d66e2019-09-27 10:25:57 -0400256 test(r, "float4x4 main(float4x4 m) { return m - 3.0; }", in, expected);
Brian Osman1e855b22019-05-29 15:21:52 -0400257 for (int i = 0; i < 16; ++i) { expected[i] = (float)(3 - i); }
Brian Osmanb23d66e2019-09-27 10:25:57 -0400258 test(r, "float4x4 main(float4x4 m) { return 3.0 - m; }", in, expected);
Brian Osman1e855b22019-05-29 15:21:52 -0400259
260 // M*S, S*M, M/S, S/M
261 for (int i = 0; i < 16; ++i) { expected[i] = (float)(i * 3); }
Brian Osmanb23d66e2019-09-27 10:25:57 -0400262 test(r, "float4x4 main(float4x4 m) { return m * 3.0; }", in, expected);
263 test(r, "float4x4 main(float4x4 m) { return 3.0 * m; }", in, expected);
Brian Osman1e855b22019-05-29 15:21:52 -0400264 for (int i = 0; i < 16; ++i) { expected[i] = (float)(i) / 2.0f; }
Brian Osmanb23d66e2019-09-27 10:25:57 -0400265 test(r, "float4x4 main(float4x4 m) { return m / 2.0; }", in, expected);
Brian Osman5bdf5252019-05-29 17:04:54 -0400266 for (int i = 0; i < 16; ++i) { expected[i] = 1.0f / (float)(i + 1); }
Brian Osmanb23d66e2019-09-27 10:25:57 -0400267 test(r, "float4x4 main(float4x4 m) { return 1.0 / (m + 1); }", in, expected);
Brian Osman1e855b22019-05-29 15:21:52 -0400268
Brian Osman035606d2021-01-15 15:30:47 -0500269 // Matrix negation
Brian Osman1e855b22019-05-29 15:21:52 -0400270 for (int i = 0; i < 16; ++i) { expected[i] = (float)(-i); }
Brian Osman035606d2021-01-15 15:30:47 -0500271 test(r, "float4x4 main(float4x4 m) { return -m; }", in, expected);
Brian Osman1e855b22019-05-29 15:21:52 -0400272
Brian Osman909231c2019-05-29 15:34:36 -0400273 // M*V, V*M
Brian Osmanc0f2b642020-12-22 13:35:55 -0500274 for (int i = 0; i < 3; ++i) {
275 expected[i] = 9.0f*i + 10.0f*(i+3) + 11.0f*(i+6);
Brian Osman909231c2019-05-29 15:34:36 -0400276 }
Brian Osmanc0f2b642020-12-22 13:35:55 -0500277 test(r, "float3 main(float3x3 m, float3 v) { return m * v; }", in, expected);
278 for (int i = 0; i < 3; ++i) {
279 expected[i] = 9.0f*(3*i) + 10.0f*(3*i+1) + 11.0f*(3*i+2);
Brian Osman909231c2019-05-29 15:34:36 -0400280 }
Brian Osmanc0f2b642020-12-22 13:35:55 -0500281 test(r, "float3 main(float3x3 m, float3 v) { return v * m; }", in, expected);
Brian Osman29e013d2019-05-28 17:16:03 -0400282
Brian Osman909231c2019-05-29 15:34:36 -0400283 // M*M
284 {
Mike Reed3ef77dd2020-04-06 10:41:09 -0400285 SkM44 m = SkM44::ColMajor(in);
Mike Reedb26b4e72020-01-22 14:31:21 -0500286 SkM44 m2;
287 float in2[16];
Brian Osman909231c2019-05-29 15:34:36 -0400288 for (int i = 0; i < 16; ++i) {
Mike Reedb26b4e72020-01-22 14:31:21 -0500289 in2[i] = (i + 4) % 16;
Brian Osman909231c2019-05-29 15:34:36 -0400290 }
Mike Reed3ef77dd2020-04-06 10:41:09 -0400291 m2 = SkM44::ColMajor(in2);
Brian Osman909231c2019-05-29 15:34:36 -0400292 m.setConcat(m, m2);
293 // Rearrange the columns on the RHS so we detect left-hand/right-hand errors
294 test(r, "float4x4 main(float4x4 m) { return m * float4x4(m[1], m[2], m[3], m[0]); }",
Brian Osmanb23d66e2019-09-27 10:25:57 -0400295 in, (float*)&m);
Brian Osman909231c2019-05-29 15:34:36 -0400296 }
Brian Osman29e013d2019-05-28 17:16:03 -0400297}
298
Brian Osman4e93feb2019-05-16 15:38:00 -0400299DEF_TEST(SkSLInterpreterTernary, r) {
300 test(r, "void main(inout half4 color) { color.r = color.g > color.b ? color.g : color.b; }",
301 0, 1, 2, 0, 2, 1, 2, 0);
302 test(r, "void main(inout half4 color) { color.r = color.g > color.b ? color.g : color.b; }",
303 0, 3, 2, 0, 3, 3, 2, 0);
304}
305
Brian Osman41672152019-05-14 13:37:30 -0400306DEF_TEST(SkSLInterpreterCast, r) {
Brian Osman08a84962019-06-14 10:17:16 -0400307 union Val {
308 float f;
Brian Osman08a84962019-06-14 10:17:16 -0400309 int32_t s;
310 };
Brian Osman41672152019-05-14 13:37:30 -0400311
Brian Osman08a84962019-06-14 10:17:16 -0400312 Val input[2];
313 Val expected[2];
Brian Osman41672152019-05-14 13:37:30 -0400314
Brian Osman08a84962019-06-14 10:17:16 -0400315 input[0].s = 3;
316 input[1].s = -5;
317 expected[0].f = 3.0f;
318 expected[1].f = -5.0f;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400319 test(r, "float main(int x) { return float (x); }", (float*)input, (float*)expected);
320 test(r, "float2 main(int2 x) { return float2(x); }", (float*)input, (float*)expected);
Brian Osman41672152019-05-14 13:37:30 -0400321
Brian Osman08a84962019-06-14 10:17:16 -0400322 input[0].f = 3.0f;
323 input[1].f = -5.0f;
324 expected[0].s = 3;
325 expected[1].s = -5;
Brian Osmanb08cc022020-04-02 11:38:40 -0400326 test(r, "int main(float x) { return int (x); }", (float*)input, (float*)expected);
327 test(r, "int2 main(float2 x) { return int2(x); }", (float*)input, (float*)expected);
Brian Osman08a84962019-06-14 10:17:16 -0400328
329 input[0].s = 3;
330 expected[0].f = 3.0f;
331 expected[1].f = 3.0f;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400332 test(r, "float2 main(int x) { return float2(x); }", (float*)input, (float*)expected);
Brian Osman41672152019-05-14 13:37:30 -0400333}
334
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400335DEF_TEST(SkSLInterpreterIf, r) {
336 test(r, "void main(inout half4 color) { if (color.r > color.g) color.a = 1; }", 5, 3, 0, 0,
337 5, 3, 0, 1);
338 test(r, "void main(inout half4 color) { if (color.r > color.g) color.a = 1; }", 5, 5, 0, 0,
339 5, 5, 0, 0);
340 test(r, "void main(inout half4 color) { if (color.r > color.g) color.a = 1; }", 5, 6, 0, 0,
341 5, 6, 0, 0);
342 test(r, "void main(inout half4 color) { if (color.r < color.g) color.a = 1; }", 3, 5, 0, 0,
343 3, 5, 0, 1);
344 test(r, "void main(inout half4 color) { if (color.r < color.g) color.a = 1; }", 5, 5, 0, 0,
345 5, 5, 0, 0);
346 test(r, "void main(inout half4 color) { if (color.r < color.g) color.a = 1; }", 6, 5, 0, 0,
347 6, 5, 0, 0);
348 test(r, "void main(inout half4 color) { if (color.r >= color.g) color.a = 1; }", 5, 3, 0, 0,
349 5, 3, 0, 1);
350 test(r, "void main(inout half4 color) { if (color.r >= color.g) color.a = 1; }", 5, 5, 0, 0,
351 5, 5, 0, 1);
352 test(r, "void main(inout half4 color) { if (color.r >= color.g) color.a = 1; }", 5, 6, 0, 0,
353 5, 6, 0, 0);
354 test(r, "void main(inout half4 color) { if (color.r <= color.g) color.a = 1; }", 3, 5, 0, 0,
355 3, 5, 0, 1);
356 test(r, "void main(inout half4 color) { if (color.r <= color.g) color.a = 1; }", 5, 5, 0, 0,
357 5, 5, 0, 1);
358 test(r, "void main(inout half4 color) { if (color.r <= color.g) color.a = 1; }", 6, 5, 0, 0,
359 6, 5, 0, 0);
360 test(r, "void main(inout half4 color) { if (color.r == color.g) color.a = 1; }", 2, 2, 0, 0,
361 2, 2, 0, 1);
362 test(r, "void main(inout half4 color) { if (color.r == color.g) color.a = 1; }", 2, -2, 0, 0,
363 2, -2, 0, 0);
364 test(r, "void main(inout half4 color) { if (color.r != color.g) color.a = 1; }", 2, 2, 0, 0,
365 2, 2, 0, 0);
366 test(r, "void main(inout half4 color) { if (color.r != color.g) color.a = 1; }", 2, -2, 0, 0,
367 2, -2, 0, 1);
Brian Osmane5bbce22019-09-23 12:38:40 -0400368 test(r, "void main(inout half4 color) { if (!(color.r == color.g)) color.a = 1; }", 2, 2, 0, 0,
369 2, 2, 0, 0);
370 test(r, "void main(inout half4 color) { if (!(color.r == color.g)) color.a = 1; }", 2, -2, 0, 0,
371 2, -2, 0, 1);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400372 test(r, "void main(inout half4 color) { if (color.r == color.g) color.a = 1; else "
373 "color.a = 2; }", 1, 1, 0, 0, 1, 1, 0, 1);
374 test(r, "void main(inout half4 color) { if (color.r == color.g) color.a = 1; else "
375 "color.a = 2; }", 2, -2, 0, 0, 2, -2, 0, 2);
376}
377
Brian Osman16e6fd52019-05-29 11:19:00 -0400378DEF_TEST(SkSLInterpreterIfVector, r) {
379 test(r, "void main(inout half4 color) { if (color.rg == color.ba) color.a = 1; }",
380 1, 2, 1, 2, 1, 2, 1, 1);
381 test(r, "void main(inout half4 color) { if (color.rg == color.ba) color.a = 1; }",
382 1, 2, 3, 2, 1, 2, 3, 2);
383 test(r, "void main(inout half4 color) { if (color.rg != color.ba) color.a = 1; }",
384 1, 2, 1, 2, 1, 2, 1, 2);
385 test(r, "void main(inout half4 color) { if (color.rg != color.ba) color.a = 1; }",
386 1, 2, 3, 2, 1, 2, 3, 1);
387}
388
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400389DEF_TEST(SkSLInterpreterFor, r) {
John Stilesaf9b58e2021-01-13 17:48:36 -0500390 test(r, "void main(inout half4 color) { for (int i = 1; i <= 10; ++i) color.r += half(i); }",
Brian Osmanf4a77732020-12-28 09:03:00 -0500391 0, 0, 0, 0,
Brian Osman9333c872021-01-13 15:06:17 -0500392 55, 0, 0, 0);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400393 test(r,
394 "void main(inout half4 color) {"
395 " for (int i = 1; i <= 10; ++i)"
Brian Osman77ba8102021-01-12 17:15:30 -0500396 " for (int j = 1; j <= 10; ++j)"
John Stilesaf9b58e2021-01-13 17:48:36 -0500397 " if (j >= i) { color.r += half(j); }"
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400398 "}",
399 0, 0, 0, 0,
Brian Osman9333c872021-01-13 15:06:17 -0500400 385, 0, 0, 0);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400401 test(r,
402 "void main(inout half4 color) {"
403 " for (int i = 1; i <= 10; ++i)"
Brian Osman77ba8102021-01-12 17:15:30 -0500404 " for (int j = 1; j < 20 ; ++j) {"
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400405 " if (i == j) continue;"
406 " if (j > 10) break;"
John Stilesaf9b58e2021-01-13 17:48:36 -0500407 " color.r += half(j);"
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400408 " }"
409 "}",
410 0, 0, 0, 0,
Brian Osman9333c872021-01-13 15:06:17 -0500411 495, 0, 0, 0);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400412}
413
Brian Osmanf3fa6002019-05-17 14:26:53 -0400414DEF_TEST(SkSLInterpreterPrefixPostfix, r) {
415 test(r, "void main(inout half4 color) { color.r = ++color.g; }", 1, 2, 3, 4, 3, 3, 3, 4);
416 test(r, "void main(inout half4 color) { color.r = color.g++; }", 1, 2, 3, 4, 2, 3, 3, 4);
417}
418
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400419DEF_TEST(SkSLInterpreterSwizzle, r) {
420 test(r, "void main(inout half4 color) { color = color.abgr; }", 1, 2, 3, 4, 4, 3, 2, 1);
421 test(r, "void main(inout half4 color) { color.rgb = half4(5, 6, 7, 8).bbg; }", 1, 2, 3, 4, 7, 7,
422 6, 4);
John Stilesaf9b58e2021-01-13 17:48:36 -0500423 test(r, "void main(inout half4 color) { color.bgr = half3(5, 6, 7); }", 1, 2, 3, 4, 7, 6,
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400424 5, 4);
425}
426
427DEF_TEST(SkSLInterpreterGlobal, r) {
John Stilesaf9b58e2021-01-13 17:48:36 -0500428 test(r, "int x; void main(inout half4 color) { x = 10; color.b = half(x); }", 1, 2, 3, 4, 1, 2,
429 10, 4);
Brian Osmanb7451292019-05-15 13:02:13 -0400430 test(r, "float4 x; void main(inout float4 color) { x = color * 2; color = x; }",
431 1, 2, 3, 4, 2, 4, 6, 8);
432 test(r, "float4 x; void main(inout float4 color) { x = float4(5, 6, 7, 8); color = x.wzyx; }",
433 1, 2, 3, 4, 8, 7, 6, 5);
Brian Osman1091f022019-05-16 09:42:16 -0400434 test(r, "float4 x; void main(inout float4 color) { x.wzyx = float4(5, 6, 7, 8); color = x; }",
435 1, 2, 3, 4, 8, 7, 6, 5);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400436}
Ethan Nicholas746035a2019-04-23 13:31:09 -0400437
438DEF_TEST(SkSLInterpreterGeneric, r) {
439 float value1 = 5;
440 float expected1 = 25;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400441 test(r, "float main(float x) { return x * x; }", &value1, &expected1);
Ethan Nicholas746035a2019-04-23 13:31:09 -0400442 float value2[2] = { 5, 25 };
443 float expected2[2] = { 25, 625 };
Brian Osmanb23d66e2019-09-27 10:25:57 -0400444 test(r, "float2 main(float x, float y) { return float2(x * x, y * y); }", value2, expected2);
Ethan Nicholas746035a2019-04-23 13:31:09 -0400445}
Brian Osmand369a5e2019-05-09 13:13:25 -0400446
Brian Osman07c117b2019-05-23 12:51:06 -0700447DEF_TEST(SkSLInterpreterCompound, r) {
448 struct RectAndColor { SkIRect fRect; SkColor4f fColor; };
449 struct ManyRects { int fNumRects; RectAndColor fRects[4]; };
450
451 const char* src =
452 // Some struct definitions
453 "struct Point { int x; int y; };\n"
454 "struct Rect { Point p0; Point p1; };\n"
455 "struct RectAndColor { Rect r; float4 color; };\n"
456
457 // Structs as globals, parameters, return values
458 "RectAndColor temp;\n"
459 "int rect_height(Rect r) { return r.p1.y - r.p0.y; }\n"
460 "RectAndColor make_blue_rect(int w, int h) {\n"
461 " temp.r.p0.x = temp.r.p0.y = 0;\n"
462 " temp.r.p1.x = w; temp.r.p1.y = h;\n"
463 " temp.color = float4(0, 1, 0, 1);\n"
464 " return temp;\n"
465 "}\n"
466
467 // Initialization and assignment of types larger than 4 slots
468 "RectAndColor init_big(RectAndColor r) { RectAndColor s = r; return s; }\n"
469 "RectAndColor copy_big(RectAndColor r) { RectAndColor s; s = r; return s; }\n"
470
471 // Same for arrays, including some non-constant indexing
Brian Osman07c117b2019-05-23 12:51:06 -0700472 "int median(int a[15]) { return a[7]; }\n"
John Stiles076e9a22020-12-03 10:37:45 -0500473
474 "float tempFloats[8];\n"
475 "float sums(float a[8]) {\n"
Brian Osman07c117b2019-05-23 12:51:06 -0700476 " tempFloats[0] = a[0];\n"
477 " for (int i = 1; i < 8; ++i) { tempFloats[i] = tempFloats[i - 1] + a[i]; }\n"
John Stiles076e9a22020-12-03 10:37:45 -0500478 " return tempFloats[7];\n"
Brian Osman07c117b2019-05-23 12:51:06 -0700479 "}\n"
480
Brian Osmanc92df392021-01-11 13:16:28 -0500481 // Uniforms, array-of-structs
Ethan Nicholas31cff272019-09-26 13:04:48 -0400482 "uniform Rect gRects[4];\n"
Brian Osmanc92df392021-01-11 13:16:28 -0500483 "Rect get_rect_2() { return gRects[2]; }\n"
Brian Osman07c117b2019-05-23 12:51:06 -0700484
485 // Kitchen sink (swizzles, inout, SoAoS)
486 "struct ManyRects { int numRects; RectAndColor rects[4]; };\n"
487 "void fill_rects(inout ManyRects mr) {\n"
Brian Osman77ba8102021-01-12 17:15:30 -0500488 " for (int i = 0; i < 4; ++i) {\n"
489 " if (i >= mr.numRects) { break; }\n"
Brian Osman07c117b2019-05-23 12:51:06 -0700490 " mr.rects[i].r = gRects[i];\n"
John Stilesaf9b58e2021-01-13 17:48:36 -0500491 " float b = float(mr.rects[i].r.p1.y);\n"
Brian Osman07c117b2019-05-23 12:51:06 -0700492 " mr.rects[i].color = float4(b, b, b, b);\n"
493 " }\n"
494 "}\n";
495
Brian Osmanc92df392021-01-11 13:16:28 -0500496 ProgramBuilder program(r, src);
Brian Osman07c117b2019-05-23 12:51:06 -0700497
Brian Osmanc92df392021-01-11 13:16:28 -0500498 auto rect_height = SkSL::Program_GetFunction(*program, "rect_height"),
499 make_blue_rect = SkSL::Program_GetFunction(*program, "make_blue_rect"),
500 median = SkSL::Program_GetFunction(*program, "median"),
501 sums = SkSL::Program_GetFunction(*program, "sums"),
502 get_rect_2 = SkSL::Program_GetFunction(*program, "get_rect_2"),
503 fill_rects = SkSL::Program_GetFunction(*program, "fill_rects");
Brian Osman07c117b2019-05-23 12:51:06 -0700504
505 SkIRect gRects[4] = { { 1,2,3,4 }, { 5,6,7,8 }, { 9,10,11,12 }, { 13,14,15,16 } };
Brian Osmanc92df392021-01-11 13:16:28 -0500506
507 auto build = [&](const SkSL::FunctionDefinition* fn) {
508 skvm::Builder b;
509 skvm::Ptr uniformPtr = b.uniform();
510 skvm::Val uniforms[16];
511 for (int i = 0; i < 16; ++i) {
512 uniforms[i] = b.uniform32(uniformPtr, i * sizeof(int)).id;
513 }
514 SkSL::ProgramToSkVM(*program, *fn, &b, uniforms);
515 return b.done();
516 };
517
518 struct Args {
519 Args(void* uniformData) { fArgs.push_back(uniformData); }
520 void add(void* base, int n) {
521 for (int i = 0; i < n; ++i) {
522 fArgs.push_back(SkTAddOffset<void>(base, i * sizeof(float)));
523 }
524 }
525 std::vector<void*> fArgs;
526 };
Brian Osman07c117b2019-05-23 12:51:06 -0700527
Brian Osman07c117b2019-05-23 12:51:06 -0700528 {
529 SkIRect in = SkIRect::MakeXYWH(10, 10, 20, 30);
Brian Osmanb08cc022020-04-02 11:38:40 -0400530 int out = 0;
Brian Osmanc92df392021-01-11 13:16:28 -0500531 skvm::Program p = build(rect_height);
532 Args args(gRects);
533 args.add(&in, 4);
534 args.add(&out, 1);
535 p.eval(1, args.fArgs.data());
Brian Osmanb08cc022020-04-02 11:38:40 -0400536 REPORTER_ASSERT(r, out == 30);
Brian Osman07c117b2019-05-23 12:51:06 -0700537 }
538
539 {
540 int in[2] = { 15, 25 };
Brian Osmanb08cc022020-04-02 11:38:40 -0400541 RectAndColor out;
Brian Osmanc92df392021-01-11 13:16:28 -0500542 skvm::Program p = build(make_blue_rect);
543 Args args(gRects);
544 args.add(&in, 2);
545 args.add(&out, 8);
546 p.eval(1, args.fArgs.data());
Brian Osmanb08cc022020-04-02 11:38:40 -0400547 REPORTER_ASSERT(r, out.fRect.width() == 15);
548 REPORTER_ASSERT(r, out.fRect.height() == 25);
Brian Osman07c117b2019-05-23 12:51:06 -0700549 SkColor4f blue = { 0.0f, 1.0f, 0.0f, 1.0f };
Brian Osmanb08cc022020-04-02 11:38:40 -0400550 REPORTER_ASSERT(r, out.fColor == blue);
Brian Osman07c117b2019-05-23 12:51:06 -0700551 }
552
553 {
554 int in[15] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
Brian Osmanb08cc022020-04-02 11:38:40 -0400555 int out = 0;
Brian Osmanc92df392021-01-11 13:16:28 -0500556 skvm::Program p = build(median);
557 Args args(gRects);
558 args.add(&in, 15);
559 args.add(&out, 1);
560 p.eval(1, args.fArgs.data());
Brian Osmanb08cc022020-04-02 11:38:40 -0400561 REPORTER_ASSERT(r, out == 8);
Brian Osman07c117b2019-05-23 12:51:06 -0700562 }
563
Brian Osmanb8ebe232021-01-19 16:33:11 -0500564 {
Brian Osman07c117b2019-05-23 12:51:06 -0700565 float in[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
John Stiles076e9a22020-12-03 10:37:45 -0500566 float out = 0;
Brian Osmanc92df392021-01-11 13:16:28 -0500567 skvm::Program p = build(sums);
568 Args args(gRects);
569 args.add(&in, 8);
570 args.add(&out, 1);
571 p.eval(1, args.fArgs.data());
John Stiles076e9a22020-12-03 10:37:45 -0500572 REPORTER_ASSERT(r, out == static_cast<float>((7 + 1) * (7 + 2) / 2));
Brian Osman07c117b2019-05-23 12:51:06 -0700573 }
574
575 {
Brian Osmanb08cc022020-04-02 11:38:40 -0400576 SkIRect out = SkIRect::MakeEmpty();
Brian Osmanc92df392021-01-11 13:16:28 -0500577 skvm::Program p = build(get_rect_2);
578 Args args(gRects);
579 args.add(&out, 4);
580 p.eval(1, args.fArgs.data());
Brian Osmanb08cc022020-04-02 11:38:40 -0400581 REPORTER_ASSERT(r, out == gRects[2]);
Brian Osman07c117b2019-05-23 12:51:06 -0700582 }
583
Brian Osmanb8ebe232021-01-19 16:33:11 -0500584 {
Brian Osman07c117b2019-05-23 12:51:06 -0700585 ManyRects in;
586 memset(&in, 0, sizeof(in));
587 in.fNumRects = 2;
Brian Osmanc92df392021-01-11 13:16:28 -0500588 skvm::Program p = build(fill_rects);
589 Args args(gRects);
590 args.add(&in, 33);
591 p.eval(1, args.fArgs.data());
Brian Osman07c117b2019-05-23 12:51:06 -0700592 ManyRects expected;
593 memset(&expected, 0, sizeof(expected));
594 expected.fNumRects = 2;
595 for (int i = 0; i < 2; ++i) {
596 expected.fRects[i].fRect = gRects[i];
597 float c = gRects[i].fBottom;
598 expected.fRects[i].fColor = { c, c, c, c };
599 }
600 REPORTER_ASSERT(r, memcmp(&in, &expected, sizeof(in)) == 0);
601 }
602}
Ethan Nicholas91164d12019-05-15 15:29:54 -0400603
Brian Osman869a3e82019-07-18 17:00:34 -0400604static void expect_failure(skiatest::Reporter* r, const char* src) {
Brian Osman0006ad02020-11-18 15:38:39 -0500605 GrShaderCaps caps(GrContextOptions{});
606 SkSL::Compiler compiler(&caps);
John Stiles759880a2020-09-11 12:10:43 -0400607 SkSL::Program::Settings settings;
608 auto program = compiler.convertProgram(SkSL::Program::kGeneric_Kind,
609 SkSL::String(src), settings);
Brian Osmanc39a1432021-01-11 13:15:23 -0500610 REPORTER_ASSERT(r, !program);
Brian Osman869a3e82019-07-18 17:00:34 -0400611}
612
Brian Osman818fd6d2020-12-30 15:06:22 -0500613DEF_TEST(SkSLInterpreterRestrictLoops, r) {
614 // while and do-while loops are not allowed
615 expect_failure(r, "void main(inout float x) { while (x < 1) { x++; } }");
616 expect_failure(r, "void main(inout float x) { do { x++; } while (x < 1); }");
617}
618
Brian Osman6f5358f2019-07-09 14:17:23 -0400619DEF_TEST(SkSLInterpreterRestrictFunctionCalls, r) {
Brian Osman6f5358f2019-07-09 14:17:23 -0400620 // Ensure that simple recursion is not allowed
Brian Osman869a3e82019-07-18 17:00:34 -0400621 expect_failure(r, "float main() { return main() + 1; }");
Brian Osman6f5358f2019-07-09 14:17:23 -0400622
623 // Ensure that calls to undefined functions are not allowed (to prevent mutual recursion)
Brian Osman869a3e82019-07-18 17:00:34 -0400624 expect_failure(r, "float foo(); float bar() { return foo(); } float foo() { return bar(); }");
Brian Osman869a3e82019-07-18 17:00:34 -0400625}
626
Brian Osman54515b72021-01-07 14:38:08 -0500627DEF_TEST(SkSLInterpreterReturnThenCall, r) {
628 // Test that early returns disable execution in subsequently called functions
629 const char* src = R"(
630 float y;
631 void inc () { ++y; }
632 void maybe_inc() { if (y < 0) return; inc(); }
633 void main(inout float x) { y = x; maybe_inc(); x = y; }
634 )";
635
636 ProgramBuilder program(r, src);
637 const SkSL::FunctionDefinition* main = SkSL::Program_GetFunction(*program, "main");
638 REPORTER_ASSERT(r, main);
639
640 skvm::Builder b;
Brian Osmanc92df392021-01-11 13:16:28 -0500641 SkSL::ProgramToSkVM(*program, *main, &b, /*uniforms=*/{});
Brian Osman54515b72021-01-07 14:38:08 -0500642 skvm::Program p = b.done();
643
644 float xs[] = { -2.0f, 0.0f, 3.0f, -1.0f };
Brian Osmanc92df392021-01-11 13:16:28 -0500645 p.eval(4, xs);
Brian Osman54515b72021-01-07 14:38:08 -0500646
647 REPORTER_ASSERT(r, xs[0] == -2.0f);
648 REPORTER_ASSERT(r, xs[1] == 1.0f);
649 REPORTER_ASSERT(r, xs[2] == 4.0f);
650 REPORTER_ASSERT(r, xs[3] == -1.0f);
651}
652
Mike Klein51dc2852020-10-15 19:41:20 -0500653DEF_TEST(SkSLInterpreterEarlyReturn, r) {
Brian Osmane2416d72021-01-06 16:36:56 -0500654 // Test early returns with divergent control flow
Mike Klein51dc2852020-10-15 19:41:20 -0500655 const char* src = "float main(float x, float y) { if (x < y) { return x; } return y; }";
656
Brian Osmane2416d72021-01-06 16:36:56 -0500657 ProgramBuilder program(r, src);
Mike Klein51dc2852020-10-15 19:41:20 -0500658
Brian Osmane2416d72021-01-06 16:36:56 -0500659 const SkSL::FunctionDefinition* main = SkSL::Program_GetFunction(*program, "main");
660 REPORTER_ASSERT(r, main);
Mike Klein51dc2852020-10-15 19:41:20 -0500661
Brian Osmane2416d72021-01-06 16:36:56 -0500662 skvm::Builder b;
Brian Osmanc92df392021-01-11 13:16:28 -0500663 SkSL::ProgramToSkVM(*program, *main, &b, /*uniforms=*/{});
Brian Osmane2416d72021-01-06 16:36:56 -0500664 skvm::Program p = b.done();
Mike Klein51dc2852020-10-15 19:41:20 -0500665
Mike Klein51dc2852020-10-15 19:41:20 -0500666 float xs[] = { 1.0f, 3.0f },
667 ys[] = { 2.0f, 2.0f };
668 float rets[2];
Brian Osmanc92df392021-01-11 13:16:28 -0500669 p.eval(2, xs, ys, rets);
Brian Osmane2416d72021-01-06 16:36:56 -0500670
Mike Klein51dc2852020-10-15 19:41:20 -0500671 REPORTER_ASSERT(r, rets[0] == 1.0f);
Brian Osmane2416d72021-01-06 16:36:56 -0500672 REPORTER_ASSERT(r, rets[1] == 2.0f);
Mike Klein51dc2852020-10-15 19:41:20 -0500673}
674
Brian Osman226668a2019-05-14 16:47:30 -0400675DEF_TEST(SkSLInterpreterFunctions, r) {
676 const char* src =
677 "float sqr(float x) { return x * x; }\n"
Brian Osman226668a2019-05-14 16:47:30 -0400678 "float sub(float x, float y) { return x - y; }\n"
Brian Osman6f5358f2019-07-09 14:17:23 -0400679 "float main(float x) { return sub(sqr(x), x); }\n"
Brian Osman226668a2019-05-14 16:47:30 -0400680
681 // Different signatures
Brian Osman15c98cb2020-02-27 18:36:57 +0000682 "float dot(float2 a, float2 b) { return a.x*b.x + a.y*b.y; }\n"
683 "float dot(float3 a, float3 b) { return a.x*b.x + a.y*b.y + a.z*b.z; }\n"
Brian Osman226668a2019-05-14 16:47:30 -0400684 "float dot3_test(float x) { return dot(float3(x, x + 1, x + 2), float3(1, -1, 2)); }\n"
Brian Osman6f5358f2019-07-09 14:17:23 -0400685 "float dot2_test(float x) { return dot(float2(x, x + 1), float2(1, -1)); }\n";
Brian Osman226668a2019-05-14 16:47:30 -0400686
Brian Osman11233d22021-01-11 10:39:58 -0500687 ProgramBuilder program(r, src);
Brian Osman226668a2019-05-14 16:47:30 -0400688
Brian Osman11233d22021-01-11 10:39:58 -0500689 auto sub = SkSL::Program_GetFunction(*program, "sub");
690 auto sqr = SkSL::Program_GetFunction(*program, "sqr");
691 auto main = SkSL::Program_GetFunction(*program, "main");
692 auto tan = SkSL::Program_GetFunction(*program, "tan");
693 auto dot3 = SkSL::Program_GetFunction(*program, "dot3_test");
694 auto dot2 = SkSL::Program_GetFunction(*program, "dot2_test");
Brian Osman226668a2019-05-14 16:47:30 -0400695
696 REPORTER_ASSERT(r, sub);
697 REPORTER_ASSERT(r, sqr);
698 REPORTER_ASSERT(r, main);
Brian Osman11233d22021-01-11 10:39:58 -0500699 REPORTER_ASSERT(r, !tan); // Getting a non-existent function should return nullptr
Brian Osman226668a2019-05-14 16:47:30 -0400700 REPORTER_ASSERT(r, dot3);
701 REPORTER_ASSERT(r, dot2);
Brian Osman226668a2019-05-14 16:47:30 -0400702
Brian Osman11233d22021-01-11 10:39:58 -0500703 auto test_fn = [&](const SkSL::FunctionDefinition* fn, float in, float expected) {
704 skvm::Builder b;
Brian Osmanc92df392021-01-11 13:16:28 -0500705 SkSL::ProgramToSkVM(*program, *fn, &b, /*uniforms=*/{});
Brian Osman11233d22021-01-11 10:39:58 -0500706 skvm::Program p = b.done();
Brian Osman226668a2019-05-14 16:47:30 -0400707
Brian Osman11233d22021-01-11 10:39:58 -0500708 float out = 0.0f;
Brian Osmanc92df392021-01-11 13:16:28 -0500709 p.eval(1, &in, &out);
Brian Osman11233d22021-01-11 10:39:58 -0500710 REPORTER_ASSERT(r, out == expected);
711 };
Brian Osman226668a2019-05-14 16:47:30 -0400712
Brian Osman11233d22021-01-11 10:39:58 -0500713 test_fn(main, 3.0f, 6.0f);
714 test_fn(dot3, 3.0f, 9.0f);
715 test_fn(dot2, 3.0f, -1.0f);
Brian Osman8c80b192020-02-06 10:49:38 -0500716}
717
Brian Osmand3494ed2019-06-20 15:41:34 -0400718DEF_TEST(SkSLInterpreterOutParams, r) {
719 test(r,
720 "void oneAlpha(inout half4 color) { color.a = 1; }"
721 "void main(inout half4 color) { oneAlpha(color); }",
722 0, 0, 0, 0, 0, 0, 0, 1);
723 test(r,
Brian Osmanb08cc022020-04-02 11:38:40 -0400724 "half2 tricky(half x, half y, inout half2 color, half z) {"
Brian Osmand3494ed2019-06-20 15:41:34 -0400725 " color.xy = color.yx;"
726 " return half2(x + y, z);"
727 "}"
728 "void main(inout half4 color) {"
Brian Osmanb08cc022020-04-02 11:38:40 -0400729 " half2 t = tricky(1, 2, color.rb, 5);"
Brian Osmand3494ed2019-06-20 15:41:34 -0400730 " color.ga = t;"
731 "}",
Brian Osmanb08cc022020-04-02 11:38:40 -0400732 1, 2, 3, 4, 3, 3, 1, 5);
Brian Osmand3494ed2019-06-20 15:41:34 -0400733}
734
Brian Osman401a3662020-09-29 13:20:04 -0400735DEF_TEST(SkSLInterpreterSwizzleSingleLvalue, r) {
736 // Add in your SkSL here.
737 test(r,
738 "void main(inout half4 color) { color.xywz = half4(1,2,3,4); }",
739 0, 0, 0, 0, 1, 2, 4, 3);
740}
741
742DEF_TEST(SkSLInterpreterSwizzleDoubleLvalue, r) {
743 // Add in your SkSL here.
744 test(r,
745 "void main(inout half4 color) { color.xywz.yxzw = half4(1,2,3,4); }",
746 0, 0, 0, 0, 2, 1, 4, 3);
747}
748
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400749DEF_TEST(SkSLInterpreterMathFunctions, r) {
Brian Osmanb380e712019-07-24 17:02:39 -0400750 float value[4], expected[4];
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400751
Brian Osmanb08cc022020-04-02 11:38:40 -0400752 value[0] = 0.0f; expected[0] = 0.0f;
753 test(r, "float main(float x) { return sin(x); }", value, expected);
754 test(r, "float main(float x) { return tan(x); }", value, expected);
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400755
Brian Osmanb380e712019-07-24 17:02:39 -0400756 value[0] = 0.0f; expected[0] = 1.0f;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400757 test(r, "float main(float x) { return cos(x); }", value, expected);
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400758
Brian Osmanb380e712019-07-24 17:02:39 -0400759 value[0] = 25.0f; expected[0] = 5.0f;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400760 test(r, "float main(float x) { return sqrt(x); }", value, expected);
Brian Osmanb380e712019-07-24 17:02:39 -0400761
762 value[0] = 90.0f; expected[0] = sk_float_degrees_to_radians(value[0]);
Brian Osmanb23d66e2019-09-27 10:25:57 -0400763 test(r, "float main(float x) { return radians(x); }", value, expected);
Brian Osmanb380e712019-07-24 17:02:39 -0400764
765 value[0] = 1.0f; value[1] = -1.0f;
766 expected[0] = 1.0f / SK_FloatSqrt2; expected[1] = -1.0f / SK_FloatSqrt2;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400767 test(r, "float2 main(float2 x) { return normalize(x); }", value, expected);
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400768}
769
Brian Osmanfba386b2019-06-20 14:54:15 -0400770DEF_TEST(SkSLInterpreterVoidFunction, r) {
771 test(r,
772 "half x; void foo() { x = 1.0; }"
773 "void main(inout half4 color) { foo(); color.r = x; }",
774 0, 0, 0, 0, 1, 0, 0, 0);
775}
776
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400777DEF_TEST(SkSLInterpreterMix, r) {
Brian Osman08a84962019-06-14 10:17:16 -0400778 float value, expected;
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400779
780 value = 0.5f; expected = 0.0f;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400781 test(r, "float main(float x) { return mix(-10, 10, x); }", &value, &expected);
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400782 value = 0.75f; expected = 5.0f;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400783 test(r, "float main(float x) { return mix(-10, 10, x); }", &value, &expected);
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400784 value = 2.0f; expected = 30.0f;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400785 test(r, "float main(float x) { return mix(-10, 10, x); }", &value, &expected);
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400786
Brian Osman08a84962019-06-14 10:17:16 -0400787 float valueVectors[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f },
788 expectedVector[] = { 3.0f, 4.0f, 5.0f, 6.0f };
Brian Osmanb23d66e2019-09-27 10:25:57 -0400789 test(r, "float4 main(float4 x, float4 y) { return mix(x, y, 0.5); }", valueVectors,
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400790 expectedVector);
791}
792
793DEF_TEST(SkSLInterpreterCross, r) {
Brian Osman08a84962019-06-14 10:17:16 -0400794 float args[] = { 1.0f, 4.0f, -6.0f, -2.0f, 7.0f, -3.0f };
Mike Reedb26b4e72020-01-22 14:31:21 -0500795 SkV3 cross = SkV3::Cross({args[0], args[1], args[2]},
796 {args[3], args[4], args[5]});
797 float expected[] = { cross.x, cross.y, cross.z };
Brian Osmanb23d66e2019-09-27 10:25:57 -0400798 test(r, "float3 main(float3 x, float3 y) { return cross(x, y); }", args, expected);
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400799}
Ben Wagner470e0ac2020-01-22 16:59:21 -0500800
Mike Reed634c9412019-07-18 13:20:04 -0400801DEF_TEST(SkSLInterpreterInverse, r) {
802 {
803 SkMatrix m;
804 m.setRotate(30).postScale(1, 2);
805 float args[4] = { m[0], m[3], m[1], m[4] };
806 SkAssertResult(m.invert(&m));
807 float expt[4] = { m[0], m[3], m[1], m[4] };
Ben Wagner470e0ac2020-01-22 16:59:21 -0500808 test(r, "float2x2 main(float2x2 m) { return inverse(m); }", args, expt, false);
Mike Reed634c9412019-07-18 13:20:04 -0400809 }
810 {
811 SkMatrix m;
812 m.setRotate(30).postScale(1, 2).postTranslate(1, 2);
813 float args[9] = { m[0], m[3], m[6], m[1], m[4], m[7], m[2], m[5], m[8] };
814 SkAssertResult(m.invert(&m));
815 float expt[9] = { m[0], m[3], m[6], m[1], m[4], m[7], m[2], m[5], m[8] };
Ben Wagner470e0ac2020-01-22 16:59:21 -0500816 test(r, "float3x3 main(float3x3 m) { return inverse(m); }", args, expt, false);
Mike Reed634c9412019-07-18 13:20:04 -0400817 }
818 {
819 float args[16], expt[16];
Mike Reed634c9412019-07-18 13:20:04 -0400820 // just some crazy thing that is invertible
Mike Reedb26b4e72020-01-22 14:31:21 -0500821 SkM44 m = {1, 2, 3, 4, 1, 2, 0, 3, 1, 0, 1, 4, 1, 3, 2, 0};
822 m.getColMajor(args);
Mike Reed634c9412019-07-18 13:20:04 -0400823 SkAssertResult(m.invert(&m));
Ben Wagner470e0ac2020-01-22 16:59:21 -0500824 m.getColMajor(expt);
825 test(r, "float4x4 main(float4x4 m) { return inverse(m); }", args, expt, false);
826 }
827}
828
829DEF_TEST(SkSLInterpreterDot, r) {
830 float args[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f };
831 float expected = args[0] * args[2] +
832 args[1] * args[3];
833 test(r, "float main(float2 x, float2 y) { return dot(x, y); }", args, &expected);
834
835 expected = args[0] * args[3] +
836 args[1] * args[4] +
837 args[2] * args[5];
838 test(r, "float main(float3 x, float3 y) { return dot(x, y); }", args, &expected);
839
840 expected = args[0] * args[4] +
841 args[1] * args[5] +
842 args[2] * args[6] +
843 args[3] * args[7];
844 test(r, "float main(float4 x, float4 y) { return dot(x, y); }", args, &expected);
845}
846
Brian Osmanbe0b3b72021-01-06 14:27:35 -0500847class ExternalSqrt : public SkSL::ExternalFunction {
Ben Wagner470e0ac2020-01-22 16:59:21 -0500848public:
Brian Osmanbe0b3b72021-01-06 14:27:35 -0500849 ExternalSqrt(const char* name, SkSL::Compiler& compiler)
John Stiles54e7c052021-01-11 14:22:36 -0500850 : INHERITED(name, *compiler.context().fTypes.fFloat)
Brian Osmanbe0b3b72021-01-06 14:27:35 -0500851 , fCompiler(compiler) {}
Ben Wagner470e0ac2020-01-22 16:59:21 -0500852
Brian Osmanbe0b3b72021-01-06 14:27:35 -0500853 int callParameterCount() const override { return 1; }
Ben Wagner470e0ac2020-01-22 16:59:21 -0500854
855 void getCallParameterTypes(const SkSL::Type** outTypes) const override {
John Stiles54e7c052021-01-11 14:22:36 -0500856 outTypes[0] = fCompiler.context().fTypes.fFloat.get();
Ben Wagner470e0ac2020-01-22 16:59:21 -0500857 }
858
Brian Osmandd50b0c2021-01-11 17:04:29 -0500859 void call(skvm::Builder* b,
860 skvm::F32* arguments,
861 skvm::F32* outResult,
862 skvm::I32 mask) const override {
863 outResult[0] = sqrt(arguments[0]);
864 }
865
Ben Wagner470e0ac2020-01-22 16:59:21 -0500866private:
867 SkSL::Compiler& fCompiler;
Brian Osmanbe0b3b72021-01-06 14:27:35 -0500868 using INHERITED = SkSL::ExternalFunction;
Ben Wagner470e0ac2020-01-22 16:59:21 -0500869};
870
Brian Osmanbe0b3b72021-01-06 14:27:35 -0500871DEF_TEST(SkSLInterpreterExternalFunction, r) {
Brian Osman0006ad02020-11-18 15:38:39 -0500872 GrShaderCaps caps(GrContextOptions{});
873 SkSL::Compiler compiler(&caps);
Ben Wagner470e0ac2020-01-22 16:59:21 -0500874 SkSL::Program::Settings settings;
Brian Osmandd50b0c2021-01-11 17:04:29 -0500875 const char* src = "float main() { return external(25); }";
Brian Osmanbe0b3b72021-01-06 14:27:35 -0500876 std::vector<std::unique_ptr<SkSL::ExternalFunction>> externalFunctions;
877 externalFunctions.push_back(std::make_unique<ExternalSqrt>("external", compiler));
Brian Osman32d53552020-09-23 13:55:20 -0400878 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(
Brian Osmanbe0b3b72021-01-06 14:27:35 -0500879 SkSL::Program::kGeneric_Kind, SkSL::String(src), settings, &externalFunctions);
Ben Wagner470e0ac2020-01-22 16:59:21 -0500880 REPORTER_ASSERT(r, program);
Brian Osmandd50b0c2021-01-11 17:04:29 -0500881
882 const SkSL::FunctionDefinition* main = SkSL::Program_GetFunction(*program, "main");
883
884 skvm::Builder b;
885 SkSL::ProgramToSkVM(*program, *main, &b, /*uniforms=*/{});
886 skvm::Program p = b.done();
887
888 float out;
889 p.eval(1, &out);
890 REPORTER_ASSERT(r, out == 5.0);
Ben Wagner470e0ac2020-01-22 16:59:21 -0500891}
892
Brian Osmandd50b0c2021-01-11 17:04:29 -0500893class ExternalTable : public SkSL::ExternalFunction {
Ben Wagner470e0ac2020-01-22 16:59:21 -0500894public:
Brian Osmandd50b0c2021-01-11 17:04:29 -0500895 ExternalTable(const char* name, SkSL::Compiler& compiler, skvm::Uniforms* uniforms)
896 : INHERITED(name, *compiler.context().fTypes.fFloat)
897 , fCompiler(compiler)
898 , fTable{1, 2, 4, 8} {
899 fAddr = uniforms->pushPtr(fTable);
900 }
Brian Osmanb08cc022020-04-02 11:38:40 -0400901
Brian Osmanbe0b3b72021-01-06 14:27:35 -0500902 int callParameterCount() const override { return 1; }
Brian Osmanb08cc022020-04-02 11:38:40 -0400903
Ben Wagner470e0ac2020-01-22 16:59:21 -0500904 void getCallParameterTypes(const SkSL::Type** outTypes) const override {
Brian Osmandd50b0c2021-01-11 17:04:29 -0500905 outTypes[0] = fCompiler.context().fTypes.fFloat.get();
Ben Wagner470e0ac2020-01-22 16:59:21 -0500906 }
Brian Osmanb08cc022020-04-02 11:38:40 -0400907
Brian Osmandd50b0c2021-01-11 17:04:29 -0500908 void call(skvm::Builder* b,
909 skvm::F32* arguments,
910 skvm::F32* outResult,
911 skvm::I32 mask) const override {
912 skvm::I32 index = skvm::trunc(arguments[0] * 4);
913 index = max(0, min(index, 3));
914 outResult[0] = b->gatherF(fAddr, index);
Ben Wagner470e0ac2020-01-22 16:59:21 -0500915 }
Brian Osmanb08cc022020-04-02 11:38:40 -0400916
Ben Wagner470e0ac2020-01-22 16:59:21 -0500917private:
918 SkSL::Compiler& fCompiler;
Brian Osmandd50b0c2021-01-11 17:04:29 -0500919 skvm::Uniform fAddr;
920 float fTable[4];
Brian Osmanbe0b3b72021-01-06 14:27:35 -0500921 using INHERITED = SkSL::ExternalFunction;
Ben Wagner470e0ac2020-01-22 16:59:21 -0500922};
Brian Osmanb08cc022020-04-02 11:38:40 -0400923
Brian Osmandd50b0c2021-01-11 17:04:29 -0500924DEF_TEST(SkSLInterpreterExternalTable, r) {
Brian Osman0006ad02020-11-18 15:38:39 -0500925 GrShaderCaps caps(GrContextOptions{});
926 SkSL::Compiler compiler(&caps);
Ben Wagner470e0ac2020-01-22 16:59:21 -0500927 SkSL::Program::Settings settings;
Brian Osman32d53552020-09-23 13:55:20 -0400928 const char* src =
Brian Osmandd50b0c2021-01-11 17:04:29 -0500929 "float4 main() { return float4(table(2), table(-1), table(0.4), table(0.6)); }";
Brian Osmanbe0b3b72021-01-06 14:27:35 -0500930 std::vector<std::unique_ptr<SkSL::ExternalFunction>> externalFunctions;
Brian Osmandd50b0c2021-01-11 17:04:29 -0500931
932 skvm::Builder b;
933 skvm::Uniforms u(b.uniform(), 0);
934
935 externalFunctions.push_back(std::make_unique<ExternalTable>("table", compiler, &u));
Brian Osman32d53552020-09-23 13:55:20 -0400936 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(
Brian Osmanbe0b3b72021-01-06 14:27:35 -0500937 SkSL::Program::kGeneric_Kind, SkSL::String(src), settings, &externalFunctions);
Ben Wagner470e0ac2020-01-22 16:59:21 -0500938 REPORTER_ASSERT(r, program);
Brian Osmandd50b0c2021-01-11 17:04:29 -0500939
940 const SkSL::FunctionDefinition* main = SkSL::Program_GetFunction(*program, "main");
941
942 SkSL::ProgramToSkVM(*program, *main, &b, /*uniforms=*/{});
943 skvm::Program p = b.done();
944
945 float out[4];
946 p.eval(1, u.buf.data(), &out[0], &out[1], &out[2], &out[3]);
947 REPORTER_ASSERT(r, out[0] == 8.0);
948 REPORTER_ASSERT(r, out[1] == 1.0);
949 REPORTER_ASSERT(r, out[2] == 2.0);
950 REPORTER_ASSERT(r, out[3] == 4.0);
Ethan Nicholas9e6a3932019-05-17 16:31:21 -0400951}