blob: d1dea4dfbe3f1bf451863e72e2bd0605a20a3a29 [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 Osmanfa71ffa2021-01-26 14:05:31 -0500447DEF_TEST(SkSLInterpreterFieldAccessComplex, r) {
448 const char* src = R"(
449 struct P { float x; float y; };
450 P make_point() { P p; p.x = 7; p.y = 3; return p; }
451 float main() { return make_point().y; }
452 )";
453
454 float expected = 3.0f;
455 test(r, src, /*in=*/nullptr, &expected);
456}
457
458DEF_TEST(SkSLInterpreterIndexComplex, r) {
459 const char* src = R"(
460 float2x2 make_mtx() { return float2x2(1, 2, 3, 4); }
461 float main() { return make_mtx()[1][0]; }
462 )";
463
464 float expected = 3.0f;
465 test(r, src, /*in=*/nullptr, &expected);
466}
467
Brian Osman07c117b2019-05-23 12:51:06 -0700468DEF_TEST(SkSLInterpreterCompound, r) {
469 struct RectAndColor { SkIRect fRect; SkColor4f fColor; };
470 struct ManyRects { int fNumRects; RectAndColor fRects[4]; };
471
472 const char* src =
473 // Some struct definitions
474 "struct Point { int x; int y; };\n"
475 "struct Rect { Point p0; Point p1; };\n"
476 "struct RectAndColor { Rect r; float4 color; };\n"
477
478 // Structs as globals, parameters, return values
479 "RectAndColor temp;\n"
480 "int rect_height(Rect r) { return r.p1.y - r.p0.y; }\n"
481 "RectAndColor make_blue_rect(int w, int h) {\n"
482 " temp.r.p0.x = temp.r.p0.y = 0;\n"
483 " temp.r.p1.x = w; temp.r.p1.y = h;\n"
484 " temp.color = float4(0, 1, 0, 1);\n"
485 " return temp;\n"
486 "}\n"
487
488 // Initialization and assignment of types larger than 4 slots
489 "RectAndColor init_big(RectAndColor r) { RectAndColor s = r; return s; }\n"
490 "RectAndColor copy_big(RectAndColor r) { RectAndColor s; s = r; return s; }\n"
491
492 // Same for arrays, including some non-constant indexing
Brian Osman07c117b2019-05-23 12:51:06 -0700493 "int median(int a[15]) { return a[7]; }\n"
John Stiles076e9a22020-12-03 10:37:45 -0500494
495 "float tempFloats[8];\n"
496 "float sums(float a[8]) {\n"
Brian Osman07c117b2019-05-23 12:51:06 -0700497 " tempFloats[0] = a[0];\n"
498 " for (int i = 1; i < 8; ++i) { tempFloats[i] = tempFloats[i - 1] + a[i]; }\n"
John Stiles076e9a22020-12-03 10:37:45 -0500499 " return tempFloats[7];\n"
Brian Osman07c117b2019-05-23 12:51:06 -0700500 "}\n"
501
Brian Osmanc92df392021-01-11 13:16:28 -0500502 // Uniforms, array-of-structs
Ethan Nicholas31cff272019-09-26 13:04:48 -0400503 "uniform Rect gRects[4];\n"
Brian Osmanc92df392021-01-11 13:16:28 -0500504 "Rect get_rect_2() { return gRects[2]; }\n"
Brian Osman07c117b2019-05-23 12:51:06 -0700505
506 // Kitchen sink (swizzles, inout, SoAoS)
507 "struct ManyRects { int numRects; RectAndColor rects[4]; };\n"
508 "void fill_rects(inout ManyRects mr) {\n"
Brian Osman77ba8102021-01-12 17:15:30 -0500509 " for (int i = 0; i < 4; ++i) {\n"
510 " if (i >= mr.numRects) { break; }\n"
Brian Osman07c117b2019-05-23 12:51:06 -0700511 " mr.rects[i].r = gRects[i];\n"
John Stilesaf9b58e2021-01-13 17:48:36 -0500512 " float b = float(mr.rects[i].r.p1.y);\n"
Brian Osman07c117b2019-05-23 12:51:06 -0700513 " mr.rects[i].color = float4(b, b, b, b);\n"
514 " }\n"
515 "}\n";
516
Brian Osmanc92df392021-01-11 13:16:28 -0500517 ProgramBuilder program(r, src);
Brian Osman07c117b2019-05-23 12:51:06 -0700518
Brian Osmanc92df392021-01-11 13:16:28 -0500519 auto rect_height = SkSL::Program_GetFunction(*program, "rect_height"),
520 make_blue_rect = SkSL::Program_GetFunction(*program, "make_blue_rect"),
521 median = SkSL::Program_GetFunction(*program, "median"),
522 sums = SkSL::Program_GetFunction(*program, "sums"),
523 get_rect_2 = SkSL::Program_GetFunction(*program, "get_rect_2"),
524 fill_rects = SkSL::Program_GetFunction(*program, "fill_rects");
Brian Osman07c117b2019-05-23 12:51:06 -0700525
526 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 -0500527
528 auto build = [&](const SkSL::FunctionDefinition* fn) {
529 skvm::Builder b;
530 skvm::Ptr uniformPtr = b.uniform();
531 skvm::Val uniforms[16];
532 for (int i = 0; i < 16; ++i) {
533 uniforms[i] = b.uniform32(uniformPtr, i * sizeof(int)).id;
534 }
535 SkSL::ProgramToSkVM(*program, *fn, &b, uniforms);
536 return b.done();
537 };
538
539 struct Args {
540 Args(void* uniformData) { fArgs.push_back(uniformData); }
541 void add(void* base, int n) {
542 for (int i = 0; i < n; ++i) {
543 fArgs.push_back(SkTAddOffset<void>(base, i * sizeof(float)));
544 }
545 }
546 std::vector<void*> fArgs;
547 };
Brian Osman07c117b2019-05-23 12:51:06 -0700548
Brian Osman07c117b2019-05-23 12:51:06 -0700549 {
550 SkIRect in = SkIRect::MakeXYWH(10, 10, 20, 30);
Brian Osmanb08cc022020-04-02 11:38:40 -0400551 int out = 0;
Brian Osmanc92df392021-01-11 13:16:28 -0500552 skvm::Program p = build(rect_height);
553 Args args(gRects);
554 args.add(&in, 4);
555 args.add(&out, 1);
556 p.eval(1, args.fArgs.data());
Brian Osmanb08cc022020-04-02 11:38:40 -0400557 REPORTER_ASSERT(r, out == 30);
Brian Osman07c117b2019-05-23 12:51:06 -0700558 }
559
560 {
561 int in[2] = { 15, 25 };
Brian Osmanb08cc022020-04-02 11:38:40 -0400562 RectAndColor out;
Brian Osmanc92df392021-01-11 13:16:28 -0500563 skvm::Program p = build(make_blue_rect);
564 Args args(gRects);
565 args.add(&in, 2);
566 args.add(&out, 8);
567 p.eval(1, args.fArgs.data());
Brian Osmanb08cc022020-04-02 11:38:40 -0400568 REPORTER_ASSERT(r, out.fRect.width() == 15);
569 REPORTER_ASSERT(r, out.fRect.height() == 25);
Brian Osman07c117b2019-05-23 12:51:06 -0700570 SkColor4f blue = { 0.0f, 1.0f, 0.0f, 1.0f };
Brian Osmanb08cc022020-04-02 11:38:40 -0400571 REPORTER_ASSERT(r, out.fColor == blue);
Brian Osman07c117b2019-05-23 12:51:06 -0700572 }
573
574 {
575 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 -0400576 int out = 0;
Brian Osmanc92df392021-01-11 13:16:28 -0500577 skvm::Program p = build(median);
578 Args args(gRects);
579 args.add(&in, 15);
580 args.add(&out, 1);
581 p.eval(1, args.fArgs.data());
Brian Osmanb08cc022020-04-02 11:38:40 -0400582 REPORTER_ASSERT(r, out == 8);
Brian Osman07c117b2019-05-23 12:51:06 -0700583 }
584
Brian Osmanb8ebe232021-01-19 16:33:11 -0500585 {
Brian Osman07c117b2019-05-23 12:51:06 -0700586 float in[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
John Stiles076e9a22020-12-03 10:37:45 -0500587 float out = 0;
Brian Osmanc92df392021-01-11 13:16:28 -0500588 skvm::Program p = build(sums);
589 Args args(gRects);
590 args.add(&in, 8);
591 args.add(&out, 1);
592 p.eval(1, args.fArgs.data());
John Stiles076e9a22020-12-03 10:37:45 -0500593 REPORTER_ASSERT(r, out == static_cast<float>((7 + 1) * (7 + 2) / 2));
Brian Osman07c117b2019-05-23 12:51:06 -0700594 }
595
596 {
Brian Osmanb08cc022020-04-02 11:38:40 -0400597 SkIRect out = SkIRect::MakeEmpty();
Brian Osmanc92df392021-01-11 13:16:28 -0500598 skvm::Program p = build(get_rect_2);
599 Args args(gRects);
600 args.add(&out, 4);
601 p.eval(1, args.fArgs.data());
Brian Osmanb08cc022020-04-02 11:38:40 -0400602 REPORTER_ASSERT(r, out == gRects[2]);
Brian Osman07c117b2019-05-23 12:51:06 -0700603 }
604
Brian Osmanb8ebe232021-01-19 16:33:11 -0500605 {
Brian Osman07c117b2019-05-23 12:51:06 -0700606 ManyRects in;
607 memset(&in, 0, sizeof(in));
608 in.fNumRects = 2;
Brian Osmanc92df392021-01-11 13:16:28 -0500609 skvm::Program p = build(fill_rects);
610 Args args(gRects);
611 args.add(&in, 33);
612 p.eval(1, args.fArgs.data());
Brian Osman07c117b2019-05-23 12:51:06 -0700613 ManyRects expected;
614 memset(&expected, 0, sizeof(expected));
615 expected.fNumRects = 2;
616 for (int i = 0; i < 2; ++i) {
617 expected.fRects[i].fRect = gRects[i];
618 float c = gRects[i].fBottom;
619 expected.fRects[i].fColor = { c, c, c, c };
620 }
621 REPORTER_ASSERT(r, memcmp(&in, &expected, sizeof(in)) == 0);
622 }
623}
Ethan Nicholas91164d12019-05-15 15:29:54 -0400624
Brian Osman869a3e82019-07-18 17:00:34 -0400625static void expect_failure(skiatest::Reporter* r, const char* src) {
Brian Osman0006ad02020-11-18 15:38:39 -0500626 GrShaderCaps caps(GrContextOptions{});
627 SkSL::Compiler compiler(&caps);
John Stiles759880a2020-09-11 12:10:43 -0400628 SkSL::Program::Settings settings;
629 auto program = compiler.convertProgram(SkSL::Program::kGeneric_Kind,
630 SkSL::String(src), settings);
Brian Osmanc39a1432021-01-11 13:15:23 -0500631 REPORTER_ASSERT(r, !program);
Brian Osman869a3e82019-07-18 17:00:34 -0400632}
633
Brian Osman818fd6d2020-12-30 15:06:22 -0500634DEF_TEST(SkSLInterpreterRestrictLoops, r) {
635 // while and do-while loops are not allowed
636 expect_failure(r, "void main(inout float x) { while (x < 1) { x++; } }");
637 expect_failure(r, "void main(inout float x) { do { x++; } while (x < 1); }");
638}
639
Brian Osman6f5358f2019-07-09 14:17:23 -0400640DEF_TEST(SkSLInterpreterRestrictFunctionCalls, r) {
Brian Osman6f5358f2019-07-09 14:17:23 -0400641 // Ensure that simple recursion is not allowed
Brian Osman869a3e82019-07-18 17:00:34 -0400642 expect_failure(r, "float main() { return main() + 1; }");
Brian Osman6f5358f2019-07-09 14:17:23 -0400643
644 // Ensure that calls to undefined functions are not allowed (to prevent mutual recursion)
Brian Osman869a3e82019-07-18 17:00:34 -0400645 expect_failure(r, "float foo(); float bar() { return foo(); } float foo() { return bar(); }");
Brian Osman869a3e82019-07-18 17:00:34 -0400646}
647
Brian Osman54515b72021-01-07 14:38:08 -0500648DEF_TEST(SkSLInterpreterReturnThenCall, r) {
649 // Test that early returns disable execution in subsequently called functions
650 const char* src = R"(
651 float y;
652 void inc () { ++y; }
653 void maybe_inc() { if (y < 0) return; inc(); }
654 void main(inout float x) { y = x; maybe_inc(); x = y; }
655 )";
656
657 ProgramBuilder program(r, src);
658 const SkSL::FunctionDefinition* main = SkSL::Program_GetFunction(*program, "main");
659 REPORTER_ASSERT(r, main);
660
661 skvm::Builder b;
Brian Osmanc92df392021-01-11 13:16:28 -0500662 SkSL::ProgramToSkVM(*program, *main, &b, /*uniforms=*/{});
Brian Osman54515b72021-01-07 14:38:08 -0500663 skvm::Program p = b.done();
664
665 float xs[] = { -2.0f, 0.0f, 3.0f, -1.0f };
Brian Osmanc92df392021-01-11 13:16:28 -0500666 p.eval(4, xs);
Brian Osman54515b72021-01-07 14:38:08 -0500667
668 REPORTER_ASSERT(r, xs[0] == -2.0f);
669 REPORTER_ASSERT(r, xs[1] == 1.0f);
670 REPORTER_ASSERT(r, xs[2] == 4.0f);
671 REPORTER_ASSERT(r, xs[3] == -1.0f);
672}
673
Mike Klein51dc2852020-10-15 19:41:20 -0500674DEF_TEST(SkSLInterpreterEarlyReturn, r) {
Brian Osmane2416d72021-01-06 16:36:56 -0500675 // Test early returns with divergent control flow
Mike Klein51dc2852020-10-15 19:41:20 -0500676 const char* src = "float main(float x, float y) { if (x < y) { return x; } return y; }";
677
Brian Osmane2416d72021-01-06 16:36:56 -0500678 ProgramBuilder program(r, src);
Mike Klein51dc2852020-10-15 19:41:20 -0500679
Brian Osmane2416d72021-01-06 16:36:56 -0500680 const SkSL::FunctionDefinition* main = SkSL::Program_GetFunction(*program, "main");
681 REPORTER_ASSERT(r, main);
Mike Klein51dc2852020-10-15 19:41:20 -0500682
Brian Osmane2416d72021-01-06 16:36:56 -0500683 skvm::Builder b;
Brian Osmanc92df392021-01-11 13:16:28 -0500684 SkSL::ProgramToSkVM(*program, *main, &b, /*uniforms=*/{});
Brian Osmane2416d72021-01-06 16:36:56 -0500685 skvm::Program p = b.done();
Mike Klein51dc2852020-10-15 19:41:20 -0500686
Mike Klein51dc2852020-10-15 19:41:20 -0500687 float xs[] = { 1.0f, 3.0f },
688 ys[] = { 2.0f, 2.0f };
689 float rets[2];
Brian Osmanc92df392021-01-11 13:16:28 -0500690 p.eval(2, xs, ys, rets);
Brian Osmane2416d72021-01-06 16:36:56 -0500691
Mike Klein51dc2852020-10-15 19:41:20 -0500692 REPORTER_ASSERT(r, rets[0] == 1.0f);
Brian Osmane2416d72021-01-06 16:36:56 -0500693 REPORTER_ASSERT(r, rets[1] == 2.0f);
Mike Klein51dc2852020-10-15 19:41:20 -0500694}
695
Brian Osman226668a2019-05-14 16:47:30 -0400696DEF_TEST(SkSLInterpreterFunctions, r) {
697 const char* src =
698 "float sqr(float x) { return x * x; }\n"
Brian Osman226668a2019-05-14 16:47:30 -0400699 "float sub(float x, float y) { return x - y; }\n"
Brian Osman6f5358f2019-07-09 14:17:23 -0400700 "float main(float x) { return sub(sqr(x), x); }\n"
Brian Osman226668a2019-05-14 16:47:30 -0400701
702 // Different signatures
Brian Osman15c98cb2020-02-27 18:36:57 +0000703 "float dot(float2 a, float2 b) { return a.x*b.x + a.y*b.y; }\n"
704 "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 -0400705 "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 -0400706 "float dot2_test(float x) { return dot(float2(x, x + 1), float2(1, -1)); }\n";
Brian Osman226668a2019-05-14 16:47:30 -0400707
Brian Osman11233d22021-01-11 10:39:58 -0500708 ProgramBuilder program(r, src);
Brian Osman226668a2019-05-14 16:47:30 -0400709
Brian Osman11233d22021-01-11 10:39:58 -0500710 auto sub = SkSL::Program_GetFunction(*program, "sub");
711 auto sqr = SkSL::Program_GetFunction(*program, "sqr");
712 auto main = SkSL::Program_GetFunction(*program, "main");
713 auto tan = SkSL::Program_GetFunction(*program, "tan");
714 auto dot3 = SkSL::Program_GetFunction(*program, "dot3_test");
715 auto dot2 = SkSL::Program_GetFunction(*program, "dot2_test");
Brian Osman226668a2019-05-14 16:47:30 -0400716
717 REPORTER_ASSERT(r, sub);
718 REPORTER_ASSERT(r, sqr);
719 REPORTER_ASSERT(r, main);
Brian Osman11233d22021-01-11 10:39:58 -0500720 REPORTER_ASSERT(r, !tan); // Getting a non-existent function should return nullptr
Brian Osman226668a2019-05-14 16:47:30 -0400721 REPORTER_ASSERT(r, dot3);
722 REPORTER_ASSERT(r, dot2);
Brian Osman226668a2019-05-14 16:47:30 -0400723
Brian Osman11233d22021-01-11 10:39:58 -0500724 auto test_fn = [&](const SkSL::FunctionDefinition* fn, float in, float expected) {
725 skvm::Builder b;
Brian Osmanc92df392021-01-11 13:16:28 -0500726 SkSL::ProgramToSkVM(*program, *fn, &b, /*uniforms=*/{});
Brian Osman11233d22021-01-11 10:39:58 -0500727 skvm::Program p = b.done();
Brian Osman226668a2019-05-14 16:47:30 -0400728
Brian Osman11233d22021-01-11 10:39:58 -0500729 float out = 0.0f;
Brian Osmanc92df392021-01-11 13:16:28 -0500730 p.eval(1, &in, &out);
Brian Osman11233d22021-01-11 10:39:58 -0500731 REPORTER_ASSERT(r, out == expected);
732 };
Brian Osman226668a2019-05-14 16:47:30 -0400733
Brian Osman11233d22021-01-11 10:39:58 -0500734 test_fn(main, 3.0f, 6.0f);
735 test_fn(dot3, 3.0f, 9.0f);
736 test_fn(dot2, 3.0f, -1.0f);
Brian Osman8c80b192020-02-06 10:49:38 -0500737}
738
Brian Osmand3494ed2019-06-20 15:41:34 -0400739DEF_TEST(SkSLInterpreterOutParams, r) {
740 test(r,
741 "void oneAlpha(inout half4 color) { color.a = 1; }"
742 "void main(inout half4 color) { oneAlpha(color); }",
743 0, 0, 0, 0, 0, 0, 0, 1);
744 test(r,
Brian Osmanb08cc022020-04-02 11:38:40 -0400745 "half2 tricky(half x, half y, inout half2 color, half z) {"
Brian Osmand3494ed2019-06-20 15:41:34 -0400746 " color.xy = color.yx;"
747 " return half2(x + y, z);"
748 "}"
749 "void main(inout half4 color) {"
Brian Osmanb08cc022020-04-02 11:38:40 -0400750 " half2 t = tricky(1, 2, color.rb, 5);"
Brian Osmand3494ed2019-06-20 15:41:34 -0400751 " color.ga = t;"
752 "}",
Brian Osmanb08cc022020-04-02 11:38:40 -0400753 1, 2, 3, 4, 3, 3, 1, 5);
Brian Osmand3494ed2019-06-20 15:41:34 -0400754}
755
Brian Osman401a3662020-09-29 13:20:04 -0400756DEF_TEST(SkSLInterpreterSwizzleSingleLvalue, r) {
Brian Osman401a3662020-09-29 13:20:04 -0400757 test(r,
758 "void main(inout half4 color) { color.xywz = half4(1,2,3,4); }",
759 0, 0, 0, 0, 1, 2, 4, 3);
760}
761
762DEF_TEST(SkSLInterpreterSwizzleDoubleLvalue, r) {
Brian Osman401a3662020-09-29 13:20:04 -0400763 test(r,
764 "void main(inout half4 color) { color.xywz.yxzw = half4(1,2,3,4); }",
765 0, 0, 0, 0, 2, 1, 4, 3);
766}
767
Brian Osman21f57072021-01-25 13:51:57 -0500768DEF_TEST(SkSLInterpreterSwizzleIndexLvalue, r) {
769 const char* src = R"(
770 void main(inout half4 color) {
771 for (int i = 0; i < 4; i++) {
772 color.wzyx[i] += half(i);
773 }
774 }
775 )";
776 test(r, src, 0, 0, 0, 0, 3, 2, 1, 0);
777}
778
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400779DEF_TEST(SkSLInterpreterMathFunctions, r) {
Brian Osmanb380e712019-07-24 17:02:39 -0400780 float value[4], expected[4];
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400781
Brian Osmanb08cc022020-04-02 11:38:40 -0400782 value[0] = 0.0f; expected[0] = 0.0f;
783 test(r, "float main(float x) { return sin(x); }", value, expected);
784 test(r, "float main(float x) { return tan(x); }", value, expected);
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400785
Brian Osmanb380e712019-07-24 17:02:39 -0400786 value[0] = 0.0f; expected[0] = 1.0f;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400787 test(r, "float main(float x) { return cos(x); }", value, expected);
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400788
Brian Osmanb380e712019-07-24 17:02:39 -0400789 value[0] = 25.0f; expected[0] = 5.0f;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400790 test(r, "float main(float x) { return sqrt(x); }", value, expected);
Brian Osmanb380e712019-07-24 17:02:39 -0400791
792 value[0] = 90.0f; expected[0] = sk_float_degrees_to_radians(value[0]);
Brian Osmanb23d66e2019-09-27 10:25:57 -0400793 test(r, "float main(float x) { return radians(x); }", value, expected);
Brian Osmanb380e712019-07-24 17:02:39 -0400794
795 value[0] = 1.0f; value[1] = -1.0f;
796 expected[0] = 1.0f / SK_FloatSqrt2; expected[1] = -1.0f / SK_FloatSqrt2;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400797 test(r, "float2 main(float2 x) { return normalize(x); }", value, expected);
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400798}
799
Brian Osmanfba386b2019-06-20 14:54:15 -0400800DEF_TEST(SkSLInterpreterVoidFunction, r) {
801 test(r,
802 "half x; void foo() { x = 1.0; }"
803 "void main(inout half4 color) { foo(); color.r = x; }",
804 0, 0, 0, 0, 1, 0, 0, 0);
805}
806
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400807DEF_TEST(SkSLInterpreterMix, r) {
Brian Osman08a84962019-06-14 10:17:16 -0400808 float value, expected;
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400809
810 value = 0.5f; expected = 0.0f;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400811 test(r, "float main(float x) { return mix(-10, 10, x); }", &value, &expected);
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400812 value = 0.75f; expected = 5.0f;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400813 test(r, "float main(float x) { return mix(-10, 10, x); }", &value, &expected);
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400814 value = 2.0f; expected = 30.0f;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400815 test(r, "float main(float x) { return mix(-10, 10, x); }", &value, &expected);
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400816
Brian Osman08a84962019-06-14 10:17:16 -0400817 float valueVectors[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f },
818 expectedVector[] = { 3.0f, 4.0f, 5.0f, 6.0f };
Brian Osmanb23d66e2019-09-27 10:25:57 -0400819 test(r, "float4 main(float4 x, float4 y) { return mix(x, y, 0.5); }", valueVectors,
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400820 expectedVector);
821}
822
823DEF_TEST(SkSLInterpreterCross, r) {
Brian Osman08a84962019-06-14 10:17:16 -0400824 float args[] = { 1.0f, 4.0f, -6.0f, -2.0f, 7.0f, -3.0f };
Mike Reedb26b4e72020-01-22 14:31:21 -0500825 SkV3 cross = SkV3::Cross({args[0], args[1], args[2]},
826 {args[3], args[4], args[5]});
827 float expected[] = { cross.x, cross.y, cross.z };
Brian Osmanb23d66e2019-09-27 10:25:57 -0400828 test(r, "float3 main(float3 x, float3 y) { return cross(x, y); }", args, expected);
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400829}
Ben Wagner470e0ac2020-01-22 16:59:21 -0500830
Mike Reed634c9412019-07-18 13:20:04 -0400831DEF_TEST(SkSLInterpreterInverse, r) {
832 {
833 SkMatrix m;
834 m.setRotate(30).postScale(1, 2);
835 float args[4] = { m[0], m[3], m[1], m[4] };
836 SkAssertResult(m.invert(&m));
837 float expt[4] = { m[0], m[3], m[1], m[4] };
Ben Wagner470e0ac2020-01-22 16:59:21 -0500838 test(r, "float2x2 main(float2x2 m) { return inverse(m); }", args, expt, false);
Mike Reed634c9412019-07-18 13:20:04 -0400839 }
840 {
841 SkMatrix m;
842 m.setRotate(30).postScale(1, 2).postTranslate(1, 2);
843 float args[9] = { m[0], m[3], m[6], m[1], m[4], m[7], m[2], m[5], m[8] };
844 SkAssertResult(m.invert(&m));
845 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 -0500846 test(r, "float3x3 main(float3x3 m) { return inverse(m); }", args, expt, false);
Mike Reed634c9412019-07-18 13:20:04 -0400847 }
848 {
849 float args[16], expt[16];
Mike Reed634c9412019-07-18 13:20:04 -0400850 // just some crazy thing that is invertible
Mike Reedb26b4e72020-01-22 14:31:21 -0500851 SkM44 m = {1, 2, 3, 4, 1, 2, 0, 3, 1, 0, 1, 4, 1, 3, 2, 0};
852 m.getColMajor(args);
Mike Reed634c9412019-07-18 13:20:04 -0400853 SkAssertResult(m.invert(&m));
Ben Wagner470e0ac2020-01-22 16:59:21 -0500854 m.getColMajor(expt);
855 test(r, "float4x4 main(float4x4 m) { return inverse(m); }", args, expt, false);
856 }
857}
858
859DEF_TEST(SkSLInterpreterDot, r) {
860 float args[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f };
861 float expected = args[0] * args[2] +
862 args[1] * args[3];
863 test(r, "float main(float2 x, float2 y) { return dot(x, y); }", args, &expected);
864
865 expected = args[0] * args[3] +
866 args[1] * args[4] +
867 args[2] * args[5];
868 test(r, "float main(float3 x, float3 y) { return dot(x, y); }", args, &expected);
869
870 expected = args[0] * args[4] +
871 args[1] * args[5] +
872 args[2] * args[6] +
873 args[3] * args[7];
874 test(r, "float main(float4 x, float4 y) { return dot(x, y); }", args, &expected);
875}
876
Brian Osmanbe0b3b72021-01-06 14:27:35 -0500877class ExternalSqrt : public SkSL::ExternalFunction {
Ben Wagner470e0ac2020-01-22 16:59:21 -0500878public:
Brian Osmanbe0b3b72021-01-06 14:27:35 -0500879 ExternalSqrt(const char* name, SkSL::Compiler& compiler)
John Stiles54e7c052021-01-11 14:22:36 -0500880 : INHERITED(name, *compiler.context().fTypes.fFloat)
Brian Osmanbe0b3b72021-01-06 14:27:35 -0500881 , fCompiler(compiler) {}
Ben Wagner470e0ac2020-01-22 16:59:21 -0500882
Brian Osmanbe0b3b72021-01-06 14:27:35 -0500883 int callParameterCount() const override { return 1; }
Ben Wagner470e0ac2020-01-22 16:59:21 -0500884
885 void getCallParameterTypes(const SkSL::Type** outTypes) const override {
John Stiles54e7c052021-01-11 14:22:36 -0500886 outTypes[0] = fCompiler.context().fTypes.fFloat.get();
Ben Wagner470e0ac2020-01-22 16:59:21 -0500887 }
888
Brian Osmandd50b0c2021-01-11 17:04:29 -0500889 void call(skvm::Builder* b,
890 skvm::F32* arguments,
891 skvm::F32* outResult,
892 skvm::I32 mask) const override {
893 outResult[0] = sqrt(arguments[0]);
894 }
895
Ben Wagner470e0ac2020-01-22 16:59:21 -0500896private:
897 SkSL::Compiler& fCompiler;
Brian Osmanbe0b3b72021-01-06 14:27:35 -0500898 using INHERITED = SkSL::ExternalFunction;
Ben Wagner470e0ac2020-01-22 16:59:21 -0500899};
900
Brian Osmanbe0b3b72021-01-06 14:27:35 -0500901DEF_TEST(SkSLInterpreterExternalFunction, r) {
Brian Osman0006ad02020-11-18 15:38:39 -0500902 GrShaderCaps caps(GrContextOptions{});
903 SkSL::Compiler compiler(&caps);
Ben Wagner470e0ac2020-01-22 16:59:21 -0500904 SkSL::Program::Settings settings;
Brian Osmandd50b0c2021-01-11 17:04:29 -0500905 const char* src = "float main() { return external(25); }";
Brian Osmanbe0b3b72021-01-06 14:27:35 -0500906 std::vector<std::unique_ptr<SkSL::ExternalFunction>> externalFunctions;
907 externalFunctions.push_back(std::make_unique<ExternalSqrt>("external", compiler));
Brian Osman32d53552020-09-23 13:55:20 -0400908 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(
Brian Osmanbe0b3b72021-01-06 14:27:35 -0500909 SkSL::Program::kGeneric_Kind, SkSL::String(src), settings, &externalFunctions);
Ben Wagner470e0ac2020-01-22 16:59:21 -0500910 REPORTER_ASSERT(r, program);
Brian Osmandd50b0c2021-01-11 17:04:29 -0500911
912 const SkSL::FunctionDefinition* main = SkSL::Program_GetFunction(*program, "main");
913
914 skvm::Builder b;
915 SkSL::ProgramToSkVM(*program, *main, &b, /*uniforms=*/{});
916 skvm::Program p = b.done();
917
918 float out;
919 p.eval(1, &out);
920 REPORTER_ASSERT(r, out == 5.0);
Ben Wagner470e0ac2020-01-22 16:59:21 -0500921}
922
Brian Osmandd50b0c2021-01-11 17:04:29 -0500923class ExternalTable : public SkSL::ExternalFunction {
Ben Wagner470e0ac2020-01-22 16:59:21 -0500924public:
Brian Osmandd50b0c2021-01-11 17:04:29 -0500925 ExternalTable(const char* name, SkSL::Compiler& compiler, skvm::Uniforms* uniforms)
926 : INHERITED(name, *compiler.context().fTypes.fFloat)
927 , fCompiler(compiler)
928 , fTable{1, 2, 4, 8} {
929 fAddr = uniforms->pushPtr(fTable);
930 }
Brian Osmanb08cc022020-04-02 11:38:40 -0400931
Brian Osmanbe0b3b72021-01-06 14:27:35 -0500932 int callParameterCount() const override { return 1; }
Brian Osmanb08cc022020-04-02 11:38:40 -0400933
Ben Wagner470e0ac2020-01-22 16:59:21 -0500934 void getCallParameterTypes(const SkSL::Type** outTypes) const override {
Brian Osmandd50b0c2021-01-11 17:04:29 -0500935 outTypes[0] = fCompiler.context().fTypes.fFloat.get();
Ben Wagner470e0ac2020-01-22 16:59:21 -0500936 }
Brian Osmanb08cc022020-04-02 11:38:40 -0400937
Brian Osmandd50b0c2021-01-11 17:04:29 -0500938 void call(skvm::Builder* b,
939 skvm::F32* arguments,
940 skvm::F32* outResult,
941 skvm::I32 mask) const override {
942 skvm::I32 index = skvm::trunc(arguments[0] * 4);
943 index = max(0, min(index, 3));
944 outResult[0] = b->gatherF(fAddr, index);
Ben Wagner470e0ac2020-01-22 16:59:21 -0500945 }
Brian Osmanb08cc022020-04-02 11:38:40 -0400946
Ben Wagner470e0ac2020-01-22 16:59:21 -0500947private:
948 SkSL::Compiler& fCompiler;
Brian Osmandd50b0c2021-01-11 17:04:29 -0500949 skvm::Uniform fAddr;
950 float fTable[4];
Brian Osmanbe0b3b72021-01-06 14:27:35 -0500951 using INHERITED = SkSL::ExternalFunction;
Ben Wagner470e0ac2020-01-22 16:59:21 -0500952};
Brian Osmanb08cc022020-04-02 11:38:40 -0400953
Brian Osmandd50b0c2021-01-11 17:04:29 -0500954DEF_TEST(SkSLInterpreterExternalTable, r) {
Brian Osman0006ad02020-11-18 15:38:39 -0500955 GrShaderCaps caps(GrContextOptions{});
956 SkSL::Compiler compiler(&caps);
Ben Wagner470e0ac2020-01-22 16:59:21 -0500957 SkSL::Program::Settings settings;
Brian Osman32d53552020-09-23 13:55:20 -0400958 const char* src =
Brian Osmandd50b0c2021-01-11 17:04:29 -0500959 "float4 main() { return float4(table(2), table(-1), table(0.4), table(0.6)); }";
Brian Osmanbe0b3b72021-01-06 14:27:35 -0500960 std::vector<std::unique_ptr<SkSL::ExternalFunction>> externalFunctions;
Brian Osmandd50b0c2021-01-11 17:04:29 -0500961
962 skvm::Builder b;
963 skvm::Uniforms u(b.uniform(), 0);
964
965 externalFunctions.push_back(std::make_unique<ExternalTable>("table", compiler, &u));
Brian Osman32d53552020-09-23 13:55:20 -0400966 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(
Brian Osmanbe0b3b72021-01-06 14:27:35 -0500967 SkSL::Program::kGeneric_Kind, SkSL::String(src), settings, &externalFunctions);
Ben Wagner470e0ac2020-01-22 16:59:21 -0500968 REPORTER_ASSERT(r, program);
Brian Osmandd50b0c2021-01-11 17:04:29 -0500969
970 const SkSL::FunctionDefinition* main = SkSL::Program_GetFunction(*program, "main");
971
972 SkSL::ProgramToSkVM(*program, *main, &b, /*uniforms=*/{});
973 skvm::Program p = b.done();
974
975 float out[4];
976 p.eval(1, u.buf.data(), &out[0], &out[1], &out[2], &out[3]);
977 REPORTER_ASSERT(r, out[0] == 8.0);
978 REPORTER_ASSERT(r, out[1] == 1.0);
979 REPORTER_ASSERT(r, out[2] == 2.0);
980 REPORTER_ASSERT(r, out[3] == 4.0);
Ethan Nicholas9e6a3932019-05-17 16:31:21 -0400981}