blob: 0269e98707b9aa6a5a27869ea35c4b6d80d1d4c7 [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"
Brian Osman08a84962019-06-14 10:17:16 -04009#include "src/sksl/SkSLByteCode.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "src/sksl/SkSLCompiler.h"
Brian Osmanbe0b3b72021-01-06 14:27:35 -050011#include "src/sksl/SkSLExternalFunction.h"
Brian Osmanf4a77732020-12-28 09:03:00 -050012#include "src/sksl/SkSLVMGenerator.h"
Ethan Nicholas91164d12019-05-15 15:29:54 -040013#include "src/utils/SkJSON.h"
Ethan Nicholas0e9401d2019-03-21 11:05:37 -040014
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "tests/Test.h"
Ethan Nicholas0e9401d2019-03-21 11:05:37 -040016
Brian Osman1f71f432020-11-18 15:44:46 -050017#if defined(SK_ENABLE_SKSL_INTERPRETER)
18
Brian Osmanf4a77732020-12-28 09:03:00 -050019struct ProgramBuilder {
20 ProgramBuilder(skiatest::Reporter* r, const char* src)
21 : fCaps(GrContextOptions{}), fCompiler(&fCaps) {
22 SkSL::Program::Settings settings;
Brian Osman54515b72021-01-07 14:38:08 -050023 // The SkSL inliner is well tested in other contexts. Here, we disable inlining entirely,
24 // to stress-test the VM generator's handling of function calls with varying signatures.
25 settings.fInlineThreshold = 0;
Brian Osman11233d22021-01-11 10:39:58 -050026 // For convenience, so we can test functions other than (and not called by) main.
27 settings.fRemoveDeadFunctions = false;
Brian Osman54515b72021-01-07 14:38:08 -050028
Brian Osmanf4a77732020-12-28 09:03:00 -050029 fProgram = fCompiler.convertProgram(SkSL::Program::kGeneric_Kind, SkSL::String(src),
30 settings);
31 if (!fProgram) {
32 ERRORF(r, "Program failed to compile:\n%s\n%s\n", src, fCompiler.errorText().c_str());
Brian Osmanb08cc022020-04-02 11:38:40 -040033 }
34 }
Brian Osmanf4a77732020-12-28 09:03:00 -050035
36 operator bool() const { return fProgram != nullptr; }
37 SkSL::Program& operator*() { return *fProgram; }
38
39 GrShaderCaps fCaps;
40 SkSL::Compiler fCompiler;
41 std::unique_ptr<SkSL::Program> fProgram;
42};
43
44struct ByteCodeBuilder {
45 ByteCodeBuilder(skiatest::Reporter* r, const char* src) : fProgram(r, src), fByteCode(nullptr) {
46 if (fProgram) {
47 fByteCode = fProgram.fCompiler.toByteCode(*fProgram);
48 if (!fByteCode) {
49 ERRORF(r, "Program failed to compile:\n%s\n%s\n", src,
50 fProgram.fCompiler.errorText().c_str());
51 }
52 }
53 }
54
55 operator bool() const { return fByteCode != nullptr; }
56 SkSL::ByteCode* operator->() { return fByteCode.get(); }
57
58 ProgramBuilder fProgram;
59 std::unique_ptr<SkSL::ByteCode> fByteCode;
60};
61
62static void verify_values(skiatest::Reporter* r,
63 const char* src,
64 const float* actual,
65 const float* expected,
66 int N,
Mike Klein606488a2021-01-07 13:55:00 -060067 bool exactCompare) {
68 auto exact_equiv = [](float x, float y) {
69 return x == y
70 || (isnan(x) && isnan(y));
Brian Osmanf4a77732020-12-28 09:03:00 -050071 };
72
Mike Klein606488a2021-01-07 13:55:00 -060073 bool valid = true;
74 for (int i = 0; i < N; ++i) {
75 if (exactCompare && !exact_equiv(actual[i], expected[i])) {
76 valid = false;
77 }
78 if (!exactCompare && !SkScalarNearlyEqual(actual[i], expected[i])) {
79 valid = false;
80 }
81 }
82
Brian Osmanf4a77732020-12-28 09:03:00 -050083 if (!valid) {
84 printf("for program: %s\n", src);
85 printf(" expected (");
86 const char* separator = "";
87 for (int i = 0; i < N; ++i) {
88 printf("%s%f", separator, expected[i]);
89 separator = ", ";
90 }
91 printf("), but received (");
92 separator = "";
93 for (int i = 0; i < N; ++i) {
94 printf("%s%f", separator, actual[i]);
95 separator = ", ";
96 }
97 printf(")\n");
98 }
99 REPORTER_ASSERT(r, valid);
Brian Osmanb08cc022020-04-02 11:38:40 -0400100}
101
Brian Osmanf4a77732020-12-28 09:03:00 -0500102void test_skvm(skiatest::Reporter* r, const char* src, float* in, const float* expected,
Mike Klein606488a2021-01-07 13:55:00 -0600103 bool exactCompare) {
Brian Osmanf4a77732020-12-28 09:03:00 -0500104 ProgramBuilder program(r, src);
105 if (!program) { return; }
106
107 const SkSL::FunctionDefinition* main = SkSL::Program_GetFunction(*program, "main");
108 REPORTER_ASSERT(r, main);
109
110 skvm::Builder b;
111 SkSL::SkVMSignature sig;
Brian Osmanc92df392021-01-11 13:16:28 -0500112 SkSL::ProgramToSkVM(*program, *main, &b, /*uniforms=*/{}, &sig);
Brian Osmanf4a77732020-12-28 09:03:00 -0500113 skvm::Program p = b.done();
114
Brian Osmanc92df392021-01-11 13:16:28 -0500115 REPORTER_ASSERT(r, p.nargs() == (int)(sig.fParameterSlots + sig.fReturnSlots));
Brian Osmanf4a77732020-12-28 09:03:00 -0500116
117 auto out = std::make_unique<float[]>(sig.fReturnSlots);
Brian Osmanc92df392021-01-11 13:16:28 -0500118 auto args = std::make_unique<void*[]>(sig.fParameterSlots + sig.fReturnSlots);
Brian Osmanf4a77732020-12-28 09:03:00 -0500119 for (size_t i = 0; i < sig.fParameterSlots; ++i) {
Brian Osmanc92df392021-01-11 13:16:28 -0500120 args[i] = in + i;
Ethan Nicholas746035a2019-04-23 13:31:09 -0400121 }
Brian Osmanf4a77732020-12-28 09:03:00 -0500122 for (size_t i = 0; i < sig.fReturnSlots; ++i) {
Brian Osmanc92df392021-01-11 13:16:28 -0500123 args[sig.fParameterSlots + i] = out.get() + i;
Brian Osmanf4a77732020-12-28 09:03:00 -0500124 }
125
126 // TODO: Test with and without JIT?
127 p.eval(1, args.get());
128
Mike Klein606488a2021-01-07 13:55:00 -0600129 verify_values(r, src, out.get(), expected, sig.fReturnSlots, exactCompare);
Brian Osmanf4a77732020-12-28 09:03:00 -0500130}
131
132void test(skiatest::Reporter* r, const char* src, float* in, const float* expected,
Mike Klein606488a2021-01-07 13:55:00 -0600133 bool exactCompare = true) {
134 test_skvm(r, src, in, expected, exactCompare);
Brian Osmanf4a77732020-12-28 09:03:00 -0500135
136 ByteCodeBuilder byteCode(r, src);
137 if (!byteCode) { return; }
138
139 const SkSL::ByteCodeFunction* main = byteCode->getFunction("main");
140 int returnCount = main->getReturnCount();
141 std::unique_ptr<float[]> out = std::unique_ptr<float[]>(new float[returnCount]);
142 SkAssertResult(byteCode->run(main, in, main->getParameterCount(), out.get(), returnCount,
143 nullptr, 0));
144
Mike Klein606488a2021-01-07 13:55:00 -0600145 verify_values(r, src, out.get(), expected, returnCount, exactCompare);
Ethan Nicholas746035a2019-04-23 13:31:09 -0400146}
147
Brian Osman569f12f2019-06-13 11:23:57 -0400148void vec_test(skiatest::Reporter* r, const char* src) {
Brian Osmanf4a77732020-12-28 09:03:00 -0500149 ByteCodeBuilder byteCode(r, src);
150 if (!byteCode) { return; }
Brian Osman08a84962019-06-14 10:17:16 -0400151
Brian Osmanb08cc022020-04-02 11:38:40 -0400152 const SkSL::ByteCodeFunction* main = byteCode->getFunction("main");
Brian Osman08a84962019-06-14 10:17:16 -0400153
Brian Osmanb23d66e2019-09-27 10:25:57 -0400154 // Test on four different vectors (with varying orderings to get divergent control flow)
155 const float input[16] = { 1, 2, 3, 4,
156 4, 3, 2, 1,
157 7, 5, 8, 6,
158 6, 8, 5, 7 };
159
Brian Osman569f12f2019-06-13 11:23:57 -0400160 float out_s[16], out_v[16];
161 memcpy(out_s, input, sizeof(out_s));
162 memcpy(out_v, input, sizeof(out_v));
163
Brian Osman08a84962019-06-14 10:17:16 -0400164 // First run in scalar mode to determine the expected output
165 for (int i = 0; i < 4; ++i) {
Brian Osmanb08cc022020-04-02 11:38:40 -0400166 SkAssertResult(byteCode->run(main, out_s + i * 4, 4, nullptr, 0, nullptr, 0));
Brian Osman08a84962019-06-14 10:17:16 -0400167 }
Brian Osman569f12f2019-06-13 11:23:57 -0400168
Brian Osmanb23d66e2019-09-27 10:25:57 -0400169 // Need to transpose input vectors for striped execution
170 auto transpose = [](float* v) {
171 for (int r = 0; r < 4; ++r)
172 for (int c = 0; c < r; ++c)
173 std::swap(v[r*4 + c], v[c*4 + r]);
174 };
175
176 // Need to transpose input vectors for striped execution
177 transpose(out_v);
178 float* args[] = { out_v, out_v + 4, out_v + 8, out_v + 12 };
179
Brian Osman08a84962019-06-14 10:17:16 -0400180 // Now run in parallel and compare results
Brian Osmanb08cc022020-04-02 11:38:40 -0400181 SkAssertResult(byteCode->runStriped(main, 4, args, 4, nullptr, 0, nullptr, 0));
Brian Osmanb23d66e2019-09-27 10:25:57 -0400182
183 // Transpose striped outputs back
184 transpose(out_v);
185
John Stilesc1c3c6d2020-08-15 23:22:53 -0400186 if (0 != memcmp(out_s, out_v, sizeof(out_s))) {
Brian Osman08a84962019-06-14 10:17:16 -0400187 printf("for program: %s\n", src);
188 for (int i = 0; i < 4; ++i) {
189 printf("(%g %g %g %g) -> (%g %g %g %g), expected (%g %g %g %g)\n",
190 input[4*i + 0], input[4*i + 1], input[4*i + 2], input[4*i + 3],
191 out_v[4*i + 0], out_v[4*i + 1], out_v[4*i + 2], out_v[4*i + 3],
192 out_s[4*i + 0], out_s[4*i + 1], out_s[4*i + 2], out_s[4*i + 3]);
Brian Osman569f12f2019-06-13 11:23:57 -0400193 }
Brian Osmanb08cc022020-04-02 11:38:40 -0400194 main->disassemble();
Brian Osman08a84962019-06-14 10:17:16 -0400195 REPORT_FAILURE(r, "VecInterpreter mismatch", SkString());
Brian Osman569f12f2019-06-13 11:23:57 -0400196 }
197}
198
Brian Osmanf4a77732020-12-28 09:03:00 -0500199void test_skvm(skiatest::Reporter* r, const char* src,
200 float inR, float inG, float inB, float inA,
201 float exR, float exG, float exB, float exA) {
202 ProgramBuilder program(r, src);
203 if (!program) { return; }
204
205 const SkSL::FunctionDefinition* main = SkSL::Program_GetFunction(*program, "main");
206 REPORTER_ASSERT(r, main);
207
208 skvm::Builder b;
Brian Osmanc92df392021-01-11 13:16:28 -0500209 SkSL::ProgramToSkVM(*program, *main, &b, /*uniforms=*/{});
Brian Osmanf4a77732020-12-28 09:03:00 -0500210 skvm::Program p = b.done();
211
212 // TODO: Test with and without JIT?
Brian Osmanc92df392021-01-11 13:16:28 -0500213 p.eval(1, &inR, &inG, &inB, &inA);
Brian Osmanf4a77732020-12-28 09:03:00 -0500214
215 float actual[4] = { inR, inG, inB, inA };
216 float expected[4] = { exR, exG, exB, exA };
217
Mike Klein606488a2021-01-07 13:55:00 -0600218 verify_values(r, src, actual, expected, 4, /*exactCompare=*/true);
Brian Osmanf4a77732020-12-28 09:03:00 -0500219
220 // TODO: vec_test with skvm
221}
222
223void test(skiatest::Reporter* r, const char* src,
224 float inR, float inG, float inB, float inA,
Brian Osman9333c872021-01-13 15:06:17 -0500225 float exR, float exG, float exB, float exA) {
226 test_skvm(r, src, inR, inG, inB, inA, exR, exG, exB, exA);
Brian Osman569f12f2019-06-13 11:23:57 -0400227
Brian Osmanf4a77732020-12-28 09:03:00 -0500228 ByteCodeBuilder byteCode(r, src);
229 if (!byteCode) { return; }
230
231 float inoutColor[4] = { inR, inG, inB, inA };
232 float expected[4] = { exR, exG, exB, exA };
233
234 const SkSL::ByteCodeFunction* main = byteCode->getFunction("main");
235 SkAssertResult(byteCode->run(main, inoutColor, 4, nullptr, 0, nullptr, 0));
236
Mike Klein606488a2021-01-07 13:55:00 -0600237 verify_values(r, src, inoutColor, expected, 4, /*exactCompare=*/true);
Brian Osmanf4a77732020-12-28 09:03:00 -0500238
Brian Osman569f12f2019-06-13 11:23:57 -0400239 // Do additional testing of 4x1 vs 1x4 to stress divergent control flow, etc.
240 vec_test(r, src);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400241}
Ben Wagner470e0ac2020-01-22 16:59:21 -0500242
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400243DEF_TEST(SkSLInterpreterAdd, r) {
244 test(r, "void main(inout half4 color) { color.r = color.r + color.g; }", 0.25, 0.75, 0, 0, 1,
245 0.75, 0, 0);
246 test(r, "void main(inout half4 color) { color += half4(1, 2, 3, 4); }", 4, 3, 2, 1, 5, 5, 5, 5);
247 test(r, "void main(inout half4 color) { half4 c = color; color += c; }", 0.25, 0.5, 0.75, 1,
248 0.5, 1, 1.5, 2);
John Stilesaf9b58e2021-01-13 17:48:36 -0500249 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 -0400250 4, 3, 0, 0);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400251}
252
253DEF_TEST(SkSLInterpreterSubtract, r) {
254 test(r, "void main(inout half4 color) { color.r = color.r - color.g; }", 1, 0.75, 0, 0, 0.25,
255 0.75, 0, 0);
256 test(r, "void main(inout half4 color) { color -= half4(1, 2, 3, 4); }", 5, 5, 5, 5, 4, 3, 2, 1);
257 test(r, "void main(inout half4 color) { half4 c = color; color -= c; }", 4, 3, 2, 1,
258 0, 0, 0, 0);
Ethan Nicholas354ecf32019-05-07 16:13:02 -0400259 test(r, "void main(inout half4 color) { color.x = -color.x; }", 4, 3, 2, 1, -4, 3, 2, 1);
260 test(r, "void main(inout half4 color) { color = -color; }", 4, 3, 2, 1, -4, -3, -2, -1);
John Stilesaf9b58e2021-01-13 17:48:36 -0500261 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 -0400262 2, 1, 0, 0);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400263}
264
265DEF_TEST(SkSLInterpreterMultiply, r) {
266 test(r, "void main(inout half4 color) { color.r = color.r * color.g; }", 2, 3, 0, 0, 6, 3, 0,
267 0);
268 test(r, "void main(inout half4 color) { color *= half4(1, 2, 3, 4); }", 2, 3, 4, 5, 2, 6, 12,
269 20);
270 test(r, "void main(inout half4 color) { half4 c = color; color *= c; }", 4, 3, 2, 1,
271 16, 9, 4, 1);
John Stilesaf9b58e2021-01-13 17:48:36 -0500272 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 -0400273 -6, -2, 0, 0);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400274}
275
276DEF_TEST(SkSLInterpreterDivide, r) {
277 test(r, "void main(inout half4 color) { color.r = color.r / color.g; }", 1, 2, 0, 0, 0.5, 2, 0,
278 0);
279 test(r, "void main(inout half4 color) { color /= half4(1, 2, 3, 4); }", 12, 12, 12, 12, 12, 6,
280 4, 3);
281 test(r, "void main(inout half4 color) { half4 c = color; color /= c; }", 4, 3, 2, 1,
282 1, 1, 1, 1);
John Stilesaf9b58e2021-01-13 17:48:36 -0500283 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 -0400284 -4, -2, 0, 0);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400285}
286
Ethan Nicholasd166d2e2019-09-23 11:43:45 -0400287DEF_TEST(SkSLInterpreterAnd, r) {
288 test(r, "void main(inout half4 color) { if (color.r > color.g && color.g > color.b) "
289 "color = half4(color.a); }", 2, 1, 0, 3, 3, 3, 3, 3);
290 test(r, "void main(inout half4 color) { if (color.r > color.g && color.g > color.b) "
291 "color = half4(color.a); }", 1, 1, 0, 3, 1, 1, 0, 3);
292 test(r, "void main(inout half4 color) { if (color.r > color.g && color.g > color.b) "
293 "color = half4(color.a); }", 2, 1, 1, 3, 2, 1, 1, 3);
John Stilesaf9b58e2021-01-13 17:48:36 -0500294 test(r, "half global; bool update() { global = 123; return true; }"
Ethan Nicholasd166d2e2019-09-23 11:43:45 -0400295 "void main(inout half4 color) { global = 0; if (color.r > color.g && update()) "
Brian Osman54515b72021-01-07 14:38:08 -0500296 "color = half4(color.a); color.a = global; }", 2, 1, 1, 3, 3, 3, 3, 123);
John Stilesaf9b58e2021-01-13 17:48:36 -0500297 test(r, "half global; bool update() { global = 123; return true; }"
Ethan Nicholasd166d2e2019-09-23 11:43:45 -0400298 "void main(inout half4 color) { global = 0; if (color.r > color.g && update()) "
Brian Osman54515b72021-01-07 14:38:08 -0500299 "color = half4(color.a); color.a = global; }", 1, 1, 1, 3, 1, 1, 1, 0);
Ethan Nicholasd166d2e2019-09-23 11:43:45 -0400300}
301
302DEF_TEST(SkSLInterpreterOr, r) {
303 test(r, "void main(inout half4 color) { if (color.r > color.g || color.g > color.b) "
304 "color = half4(color.a); }", 2, 1, 0, 3, 3, 3, 3, 3);
305 test(r, "void main(inout half4 color) { if (color.r > color.g || color.g > color.b) "
306 "color = half4(color.a); }", 1, 1, 0, 3, 3, 3, 3, 3);
307 test(r, "void main(inout half4 color) { if (color.r > color.g || color.g > color.b) "
308 "color = half4(color.a); }", 1, 1, 1, 3, 1, 1, 1, 3);
John Stilesaf9b58e2021-01-13 17:48:36 -0500309 test(r, "half global; bool update() { global = 123; return true; }"
Ethan Nicholasd166d2e2019-09-23 11:43:45 -0400310 "void main(inout half4 color) { global = 0; if (color.r > color.g || update()) "
Brian Osman54515b72021-01-07 14:38:08 -0500311 "color = half4(color.a); color.a = global; }", 1, 1, 1, 3, 3, 3, 3, 123);
John Stilesaf9b58e2021-01-13 17:48:36 -0500312 test(r, "half global; bool update() { global = 123; return true; }"
Ethan Nicholasd166d2e2019-09-23 11:43:45 -0400313 "void main(inout half4 color) { global = 0; if (color.r > color.g || update()) "
Brian Osman54515b72021-01-07 14:38:08 -0500314 "color = half4(color.a); color.a = global; }", 2, 1, 1, 3, 3, 3, 3, 0);
Ethan Nicholasd166d2e2019-09-23 11:43:45 -0400315}
316
Brian Osman29e013d2019-05-28 17:16:03 -0400317DEF_TEST(SkSLInterpreterMatrix, r) {
Brian Osman08a84962019-06-14 10:17:16 -0400318 float in[16];
319 float expected[16];
Brian Osman29e013d2019-05-28 17:16:03 -0400320
321 // Constructing matrix from scalar produces a diagonal matrix
Brian Osmanc0f2b642020-12-22 13:35:55 -0500322 in[0] = 2.0f;
323 expected[0] = 4.0f;
Brian Osman29e013d2019-05-28 17:16:03 -0400324 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 -0400325 in, expected);
Brian Osman29e013d2019-05-28 17:16:03 -0400326
Brian Osman29e013d2019-05-28 17:16:03 -0400327 // Constructing from a different-sized matrix fills the remaining space with the identity matrix
Brian Osmanc0f2b642020-12-22 13:35:55 -0500328 expected[0] = 3.0f;
Brian Osman29e013d2019-05-28 17:16:03 -0400329 test(r, "float main(float x) {"
Brian Osmanc0f2b642020-12-22 13:35:55 -0500330 "float2x2 m = float2x2(x);"
Brian Osman29e013d2019-05-28 17:16:03 -0400331 "float4x4 m2 = float4x4(m);"
332 "return m2[0][0] + m2[3][3]; }",
Brian Osmanb23d66e2019-09-27 10:25:57 -0400333 in, expected);
Brian Osman29e013d2019-05-28 17:16:03 -0400334
335 // Constructing a matrix from vectors or scalars fills in values in column-major order
336 in[0] = 1.0f;
337 in[1] = 2.0f;
338 in[2] = 4.0f;
339 in[3] = 8.0f;
340 expected[0] = 6.0f;
341 test(r, "float main(float4 v) { float2x2 m = float2x2(v); return m[0][1] + m[1][0]; }",
Brian Osmanb23d66e2019-09-27 10:25:57 -0400342 in, expected);
Brian Osman29e013d2019-05-28 17:16:03 -0400343
344 expected[0] = 10.0f;
345 test(r, "float main(float4 v) {"
346 "float2x2 m = float2x2(v.x, v.y, v.w, v.z);"
347 "return m[0][1] + m[1][0]; }",
Brian Osmanb23d66e2019-09-27 10:25:57 -0400348 in, expected);
Brian Osman29e013d2019-05-28 17:16:03 -0400349
Brian Osman1e855b22019-05-29 15:21:52 -0400350 // Initialize 16 values to be used as inputs to matrix tests
351 for (int i = 0; i < 16; ++i) { in[i] = (float)i; }
Brian Osman29e013d2019-05-28 17:16:03 -0400352
Brian Osman1e855b22019-05-29 15:21:52 -0400353 // M+M, M-S, S-M
354 for (int i = 0; i < 16; ++i) { expected[i] = (float)(2 * i); }
Brian Osmanb23d66e2019-09-27 10:25:57 -0400355 test(r, "float4x4 main(float4x4 m) { return m + m; }", in, expected);
Brian Osman1e855b22019-05-29 15:21:52 -0400356 for (int i = 0; i < 16; ++i) { expected[i] = (float)(i + 3); }
Brian Osmanb23d66e2019-09-27 10:25:57 -0400357 test(r, "float4x4 main(float4x4 m) { return m + 3.0; }", in, expected);
358 test(r, "float4x4 main(float4x4 m) { return 3.0 + m; }", in, expected);
Brian Osman1e855b22019-05-29 15:21:52 -0400359
360 // M-M, M-S, S-M
Brian Osmanc0f2b642020-12-22 13:35:55 -0500361 for (int i = 0; i < 4; ++i) { expected[i] = 4.0f; }
362 test(r, "float2x2 main(float2x2 m1, float2x2 m2) { return m2 - m1; }", in, expected);
Brian Osman1e855b22019-05-29 15:21:52 -0400363 for (int i = 0; i < 16; ++i) { expected[i] = (float)(i - 3); }
Brian Osmanb23d66e2019-09-27 10:25:57 -0400364 test(r, "float4x4 main(float4x4 m) { return m - 3.0; }", in, expected);
Brian Osman1e855b22019-05-29 15:21:52 -0400365 for (int i = 0; i < 16; ++i) { expected[i] = (float)(3 - i); }
Brian Osmanb23d66e2019-09-27 10:25:57 -0400366 test(r, "float4x4 main(float4x4 m) { return 3.0 - m; }", in, expected);
Brian Osman1e855b22019-05-29 15:21:52 -0400367
368 // M*S, S*M, M/S, S/M
369 for (int i = 0; i < 16; ++i) { expected[i] = (float)(i * 3); }
Brian Osmanb23d66e2019-09-27 10:25:57 -0400370 test(r, "float4x4 main(float4x4 m) { return m * 3.0; }", in, expected);
371 test(r, "float4x4 main(float4x4 m) { return 3.0 * m; }", in, expected);
Brian Osman1e855b22019-05-29 15:21:52 -0400372 for (int i = 0; i < 16; ++i) { expected[i] = (float)(i) / 2.0f; }
Brian Osmanb23d66e2019-09-27 10:25:57 -0400373 test(r, "float4x4 main(float4x4 m) { return m / 2.0; }", in, expected);
Brian Osman5bdf5252019-05-29 17:04:54 -0400374 for (int i = 0; i < 16; ++i) { expected[i] = 1.0f / (float)(i + 1); }
Brian Osmanb23d66e2019-09-27 10:25:57 -0400375 test(r, "float4x4 main(float4x4 m) { return 1.0 / (m + 1); }", in, expected);
Brian Osman1e855b22019-05-29 15:21:52 -0400376
377#if 0
378 // Matrix negation - legal in GLSL, not in SkSL?
379 for (int i = 0; i < 16; ++i) { expected[i] = (float)(-i); }
380 test(r, "float4x4 main(float4x4 m) { return -m; }", in, 16, expected);
381#endif
382
Brian Osman909231c2019-05-29 15:34:36 -0400383 // M*V, V*M
Brian Osmanc0f2b642020-12-22 13:35:55 -0500384 for (int i = 0; i < 3; ++i) {
385 expected[i] = 9.0f*i + 10.0f*(i+3) + 11.0f*(i+6);
Brian Osman909231c2019-05-29 15:34:36 -0400386 }
Brian Osmanc0f2b642020-12-22 13:35:55 -0500387 test(r, "float3 main(float3x3 m, float3 v) { return m * v; }", in, expected);
388 for (int i = 0; i < 3; ++i) {
389 expected[i] = 9.0f*(3*i) + 10.0f*(3*i+1) + 11.0f*(3*i+2);
Brian Osman909231c2019-05-29 15:34:36 -0400390 }
Brian Osmanc0f2b642020-12-22 13:35:55 -0500391 test(r, "float3 main(float3x3 m, float3 v) { return v * m; }", in, expected);
Brian Osman29e013d2019-05-28 17:16:03 -0400392
Brian Osman909231c2019-05-29 15:34:36 -0400393 // M*M
394 {
Mike Reed3ef77dd2020-04-06 10:41:09 -0400395 SkM44 m = SkM44::ColMajor(in);
Mike Reedb26b4e72020-01-22 14:31:21 -0500396 SkM44 m2;
397 float in2[16];
Brian Osman909231c2019-05-29 15:34:36 -0400398 for (int i = 0; i < 16; ++i) {
Mike Reedb26b4e72020-01-22 14:31:21 -0500399 in2[i] = (i + 4) % 16;
Brian Osman909231c2019-05-29 15:34:36 -0400400 }
Mike Reed3ef77dd2020-04-06 10:41:09 -0400401 m2 = SkM44::ColMajor(in2);
Brian Osman909231c2019-05-29 15:34:36 -0400402 m.setConcat(m, m2);
403 // Rearrange the columns on the RHS so we detect left-hand/right-hand errors
404 test(r, "float4x4 main(float4x4 m) { return m * float4x4(m[1], m[2], m[3], m[0]); }",
Brian Osmanb23d66e2019-09-27 10:25:57 -0400405 in, (float*)&m);
Brian Osman909231c2019-05-29 15:34:36 -0400406 }
Brian Osman29e013d2019-05-28 17:16:03 -0400407}
408
Brian Osman4e93feb2019-05-16 15:38:00 -0400409DEF_TEST(SkSLInterpreterTernary, r) {
410 test(r, "void main(inout half4 color) { color.r = color.g > color.b ? color.g : color.b; }",
411 0, 1, 2, 0, 2, 1, 2, 0);
412 test(r, "void main(inout half4 color) { color.r = color.g > color.b ? color.g : color.b; }",
413 0, 3, 2, 0, 3, 3, 2, 0);
414}
415
Brian Osman41672152019-05-14 13:37:30 -0400416DEF_TEST(SkSLInterpreterCast, r) {
Brian Osman08a84962019-06-14 10:17:16 -0400417 union Val {
418 float f;
Brian Osman08a84962019-06-14 10:17:16 -0400419 int32_t s;
420 };
Brian Osman41672152019-05-14 13:37:30 -0400421
Brian Osman08a84962019-06-14 10:17:16 -0400422 Val input[2];
423 Val expected[2];
Brian Osman41672152019-05-14 13:37:30 -0400424
Brian Osman08a84962019-06-14 10:17:16 -0400425 input[0].s = 3;
426 input[1].s = -5;
427 expected[0].f = 3.0f;
428 expected[1].f = -5.0f;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400429 test(r, "float main(int x) { return float (x); }", (float*)input, (float*)expected);
430 test(r, "float2 main(int2 x) { return float2(x); }", (float*)input, (float*)expected);
Brian Osman41672152019-05-14 13:37:30 -0400431
Brian Osman08a84962019-06-14 10:17:16 -0400432 input[0].f = 3.0f;
433 input[1].f = -5.0f;
434 expected[0].s = 3;
435 expected[1].s = -5;
Brian Osmanb08cc022020-04-02 11:38:40 -0400436 test(r, "int main(float x) { return int (x); }", (float*)input, (float*)expected);
437 test(r, "int2 main(float2 x) { return int2(x); }", (float*)input, (float*)expected);
Brian Osman08a84962019-06-14 10:17:16 -0400438
439 input[0].s = 3;
440 expected[0].f = 3.0f;
441 expected[1].f = 3.0f;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400442 test(r, "float2 main(int x) { return float2(x); }", (float*)input, (float*)expected);
Brian Osman41672152019-05-14 13:37:30 -0400443}
444
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400445DEF_TEST(SkSLInterpreterIf, r) {
446 test(r, "void main(inout half4 color) { if (color.r > color.g) color.a = 1; }", 5, 3, 0, 0,
447 5, 3, 0, 1);
448 test(r, "void main(inout half4 color) { if (color.r > color.g) color.a = 1; }", 5, 5, 0, 0,
449 5, 5, 0, 0);
450 test(r, "void main(inout half4 color) { if (color.r > color.g) color.a = 1; }", 5, 6, 0, 0,
451 5, 6, 0, 0);
452 test(r, "void main(inout half4 color) { if (color.r < color.g) color.a = 1; }", 3, 5, 0, 0,
453 3, 5, 0, 1);
454 test(r, "void main(inout half4 color) { if (color.r < color.g) color.a = 1; }", 5, 5, 0, 0,
455 5, 5, 0, 0);
456 test(r, "void main(inout half4 color) { if (color.r < color.g) color.a = 1; }", 6, 5, 0, 0,
457 6, 5, 0, 0);
458 test(r, "void main(inout half4 color) { if (color.r >= color.g) color.a = 1; }", 5, 3, 0, 0,
459 5, 3, 0, 1);
460 test(r, "void main(inout half4 color) { if (color.r >= color.g) color.a = 1; }", 5, 5, 0, 0,
461 5, 5, 0, 1);
462 test(r, "void main(inout half4 color) { if (color.r >= color.g) color.a = 1; }", 5, 6, 0, 0,
463 5, 6, 0, 0);
464 test(r, "void main(inout half4 color) { if (color.r <= color.g) color.a = 1; }", 3, 5, 0, 0,
465 3, 5, 0, 1);
466 test(r, "void main(inout half4 color) { if (color.r <= color.g) color.a = 1; }", 5, 5, 0, 0,
467 5, 5, 0, 1);
468 test(r, "void main(inout half4 color) { if (color.r <= color.g) color.a = 1; }", 6, 5, 0, 0,
469 6, 5, 0, 0);
470 test(r, "void main(inout half4 color) { if (color.r == color.g) color.a = 1; }", 2, 2, 0, 0,
471 2, 2, 0, 1);
472 test(r, "void main(inout half4 color) { if (color.r == color.g) color.a = 1; }", 2, -2, 0, 0,
473 2, -2, 0, 0);
474 test(r, "void main(inout half4 color) { if (color.r != color.g) color.a = 1; }", 2, 2, 0, 0,
475 2, 2, 0, 0);
476 test(r, "void main(inout half4 color) { if (color.r != color.g) color.a = 1; }", 2, -2, 0, 0,
477 2, -2, 0, 1);
Brian Osmane5bbce22019-09-23 12:38:40 -0400478 test(r, "void main(inout half4 color) { if (!(color.r == color.g)) color.a = 1; }", 2, 2, 0, 0,
479 2, 2, 0, 0);
480 test(r, "void main(inout half4 color) { if (!(color.r == color.g)) color.a = 1; }", 2, -2, 0, 0,
481 2, -2, 0, 1);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400482 test(r, "void main(inout half4 color) { if (color.r == color.g) color.a = 1; else "
483 "color.a = 2; }", 1, 1, 0, 0, 1, 1, 0, 1);
484 test(r, "void main(inout half4 color) { if (color.r == color.g) color.a = 1; else "
485 "color.a = 2; }", 2, -2, 0, 0, 2, -2, 0, 2);
486}
487
Brian Osman16e6fd52019-05-29 11:19:00 -0400488DEF_TEST(SkSLInterpreterIfVector, r) {
489 test(r, "void main(inout half4 color) { if (color.rg == color.ba) color.a = 1; }",
490 1, 2, 1, 2, 1, 2, 1, 1);
491 test(r, "void main(inout half4 color) { if (color.rg == color.ba) color.a = 1; }",
492 1, 2, 3, 2, 1, 2, 3, 2);
493 test(r, "void main(inout half4 color) { if (color.rg != color.ba) color.a = 1; }",
494 1, 2, 1, 2, 1, 2, 1, 2);
495 test(r, "void main(inout half4 color) { if (color.rg != color.ba) color.a = 1; }",
496 1, 2, 3, 2, 1, 2, 3, 1);
497}
498
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400499DEF_TEST(SkSLInterpreterFor, r) {
John Stilesaf9b58e2021-01-13 17:48:36 -0500500 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 -0500501 0, 0, 0, 0,
Brian Osman9333c872021-01-13 15:06:17 -0500502 55, 0, 0, 0);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400503 test(r,
504 "void main(inout half4 color) {"
505 " for (int i = 1; i <= 10; ++i)"
Brian Osman77ba8102021-01-12 17:15:30 -0500506 " for (int j = 1; j <= 10; ++j)"
John Stilesaf9b58e2021-01-13 17:48:36 -0500507 " if (j >= i) { color.r += half(j); }"
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400508 "}",
509 0, 0, 0, 0,
Brian Osman9333c872021-01-13 15:06:17 -0500510 385, 0, 0, 0);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400511 test(r,
512 "void main(inout half4 color) {"
513 " for (int i = 1; i <= 10; ++i)"
Brian Osman77ba8102021-01-12 17:15:30 -0500514 " for (int j = 1; j < 20 ; ++j) {"
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400515 " if (i == j) continue;"
516 " if (j > 10) break;"
John Stilesaf9b58e2021-01-13 17:48:36 -0500517 " color.r += half(j);"
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400518 " }"
519 "}",
520 0, 0, 0, 0,
Brian Osman9333c872021-01-13 15:06:17 -0500521 495, 0, 0, 0);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400522}
523
Brian Osmanf3fa6002019-05-17 14:26:53 -0400524DEF_TEST(SkSLInterpreterPrefixPostfix, r) {
525 test(r, "void main(inout half4 color) { color.r = ++color.g; }", 1, 2, 3, 4, 3, 3, 3, 4);
526 test(r, "void main(inout half4 color) { color.r = color.g++; }", 1, 2, 3, 4, 2, 3, 3, 4);
527}
528
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400529DEF_TEST(SkSLInterpreterSwizzle, r) {
530 test(r, "void main(inout half4 color) { color = color.abgr; }", 1, 2, 3, 4, 4, 3, 2, 1);
531 test(r, "void main(inout half4 color) { color.rgb = half4(5, 6, 7, 8).bbg; }", 1, 2, 3, 4, 7, 7,
532 6, 4);
John Stilesaf9b58e2021-01-13 17:48:36 -0500533 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 -0400534 5, 4);
535}
536
537DEF_TEST(SkSLInterpreterGlobal, r) {
John Stilesaf9b58e2021-01-13 17:48:36 -0500538 test(r, "int x; void main(inout half4 color) { x = 10; color.b = half(x); }", 1, 2, 3, 4, 1, 2,
539 10, 4);
Brian Osmanb7451292019-05-15 13:02:13 -0400540 test(r, "float4 x; void main(inout float4 color) { x = color * 2; color = x; }",
541 1, 2, 3, 4, 2, 4, 6, 8);
542 test(r, "float4 x; void main(inout float4 color) { x = float4(5, 6, 7, 8); color = x.wzyx; }",
543 1, 2, 3, 4, 8, 7, 6, 5);
Brian Osman1091f022019-05-16 09:42:16 -0400544 test(r, "float4 x; void main(inout float4 color) { x.wzyx = float4(5, 6, 7, 8); color = x; }",
545 1, 2, 3, 4, 8, 7, 6, 5);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400546}
Ethan Nicholas746035a2019-04-23 13:31:09 -0400547
548DEF_TEST(SkSLInterpreterGeneric, r) {
549 float value1 = 5;
550 float expected1 = 25;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400551 test(r, "float main(float x) { return x * x; }", &value1, &expected1);
Ethan Nicholas746035a2019-04-23 13:31:09 -0400552 float value2[2] = { 5, 25 };
553 float expected2[2] = { 25, 625 };
Brian Osmanb23d66e2019-09-27 10:25:57 -0400554 test(r, "float2 main(float x, float y) { return float2(x * x, y * y); }", value2, expected2);
Ethan Nicholas746035a2019-04-23 13:31:09 -0400555}
Brian Osmand369a5e2019-05-09 13:13:25 -0400556
Brian Osman07c117b2019-05-23 12:51:06 -0700557DEF_TEST(SkSLInterpreterCompound, r) {
558 struct RectAndColor { SkIRect fRect; SkColor4f fColor; };
559 struct ManyRects { int fNumRects; RectAndColor fRects[4]; };
560
561 const char* src =
562 // Some struct definitions
563 "struct Point { int x; int y; };\n"
564 "struct Rect { Point p0; Point p1; };\n"
565 "struct RectAndColor { Rect r; float4 color; };\n"
566
567 // Structs as globals, parameters, return values
568 "RectAndColor temp;\n"
569 "int rect_height(Rect r) { return r.p1.y - r.p0.y; }\n"
570 "RectAndColor make_blue_rect(int w, int h) {\n"
571 " temp.r.p0.x = temp.r.p0.y = 0;\n"
572 " temp.r.p1.x = w; temp.r.p1.y = h;\n"
573 " temp.color = float4(0, 1, 0, 1);\n"
574 " return temp;\n"
575 "}\n"
576
577 // Initialization and assignment of types larger than 4 slots
578 "RectAndColor init_big(RectAndColor r) { RectAndColor s = r; return s; }\n"
579 "RectAndColor copy_big(RectAndColor r) { RectAndColor s; s = r; return s; }\n"
580
581 // Same for arrays, including some non-constant indexing
Brian Osman07c117b2019-05-23 12:51:06 -0700582 "int median(int a[15]) { return a[7]; }\n"
John Stiles076e9a22020-12-03 10:37:45 -0500583
584 "float tempFloats[8];\n"
585 "float sums(float a[8]) {\n"
Brian Osman07c117b2019-05-23 12:51:06 -0700586 " tempFloats[0] = a[0];\n"
587 " for (int i = 1; i < 8; ++i) { tempFloats[i] = tempFloats[i - 1] + a[i]; }\n"
John Stiles076e9a22020-12-03 10:37:45 -0500588 " return tempFloats[7];\n"
Brian Osman07c117b2019-05-23 12:51:06 -0700589 "}\n"
590
Brian Osmanc92df392021-01-11 13:16:28 -0500591 // Uniforms, array-of-structs
Ethan Nicholas31cff272019-09-26 13:04:48 -0400592 "uniform Rect gRects[4];\n"
Brian Osmanc92df392021-01-11 13:16:28 -0500593 "Rect get_rect_2() { return gRects[2]; }\n"
Brian Osman07c117b2019-05-23 12:51:06 -0700594
595 // Kitchen sink (swizzles, inout, SoAoS)
596 "struct ManyRects { int numRects; RectAndColor rects[4]; };\n"
597 "void fill_rects(inout ManyRects mr) {\n"
Brian Osman77ba8102021-01-12 17:15:30 -0500598 " for (int i = 0; i < 4; ++i) {\n"
599 " if (i >= mr.numRects) { break; }\n"
Brian Osman07c117b2019-05-23 12:51:06 -0700600 " mr.rects[i].r = gRects[i];\n"
John Stilesaf9b58e2021-01-13 17:48:36 -0500601 " float b = float(mr.rects[i].r.p1.y);\n"
Brian Osman07c117b2019-05-23 12:51:06 -0700602 " mr.rects[i].color = float4(b, b, b, b);\n"
603 " }\n"
604 "}\n";
605
Brian Osmanc92df392021-01-11 13:16:28 -0500606 ProgramBuilder program(r, src);
Brian Osman07c117b2019-05-23 12:51:06 -0700607
Brian Osmanc92df392021-01-11 13:16:28 -0500608 auto rect_height = SkSL::Program_GetFunction(*program, "rect_height"),
609 make_blue_rect = SkSL::Program_GetFunction(*program, "make_blue_rect"),
610 median = SkSL::Program_GetFunction(*program, "median"),
611 sums = SkSL::Program_GetFunction(*program, "sums"),
612 get_rect_2 = SkSL::Program_GetFunction(*program, "get_rect_2"),
613 fill_rects = SkSL::Program_GetFunction(*program, "fill_rects");
Brian Osman07c117b2019-05-23 12:51:06 -0700614
615 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 -0500616
617 auto build = [&](const SkSL::FunctionDefinition* fn) {
618 skvm::Builder b;
619 skvm::Ptr uniformPtr = b.uniform();
620 skvm::Val uniforms[16];
621 for (int i = 0; i < 16; ++i) {
622 uniforms[i] = b.uniform32(uniformPtr, i * sizeof(int)).id;
623 }
624 SkSL::ProgramToSkVM(*program, *fn, &b, uniforms);
625 return b.done();
626 };
627
628 struct Args {
629 Args(void* uniformData) { fArgs.push_back(uniformData); }
630 void add(void* base, int n) {
631 for (int i = 0; i < n; ++i) {
632 fArgs.push_back(SkTAddOffset<void>(base, i * sizeof(float)));
633 }
634 }
635 std::vector<void*> fArgs;
636 };
Brian Osman07c117b2019-05-23 12:51:06 -0700637
Brian Osman07c117b2019-05-23 12:51:06 -0700638 {
639 SkIRect in = SkIRect::MakeXYWH(10, 10, 20, 30);
Brian Osmanb08cc022020-04-02 11:38:40 -0400640 int out = 0;
Brian Osmanc92df392021-01-11 13:16:28 -0500641 skvm::Program p = build(rect_height);
642 Args args(gRects);
643 args.add(&in, 4);
644 args.add(&out, 1);
645 p.eval(1, args.fArgs.data());
Brian Osmanb08cc022020-04-02 11:38:40 -0400646 REPORTER_ASSERT(r, out == 30);
Brian Osman07c117b2019-05-23 12:51:06 -0700647 }
648
649 {
650 int in[2] = { 15, 25 };
Brian Osmanb08cc022020-04-02 11:38:40 -0400651 RectAndColor out;
Brian Osmanc92df392021-01-11 13:16:28 -0500652 skvm::Program p = build(make_blue_rect);
653 Args args(gRects);
654 args.add(&in, 2);
655 args.add(&out, 8);
656 p.eval(1, args.fArgs.data());
Brian Osmanb08cc022020-04-02 11:38:40 -0400657 REPORTER_ASSERT(r, out.fRect.width() == 15);
658 REPORTER_ASSERT(r, out.fRect.height() == 25);
Brian Osman07c117b2019-05-23 12:51:06 -0700659 SkColor4f blue = { 0.0f, 1.0f, 0.0f, 1.0f };
Brian Osmanb08cc022020-04-02 11:38:40 -0400660 REPORTER_ASSERT(r, out.fColor == blue);
Brian Osman07c117b2019-05-23 12:51:06 -0700661 }
662
663 {
664 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 -0400665 int out = 0;
Brian Osmanc92df392021-01-11 13:16:28 -0500666 skvm::Program p = build(median);
667 Args args(gRects);
668 args.add(&in, 15);
669 args.add(&out, 1);
670 p.eval(1, args.fArgs.data());
Brian Osmanb08cc022020-04-02 11:38:40 -0400671 REPORTER_ASSERT(r, out == 8);
Brian Osman07c117b2019-05-23 12:51:06 -0700672 }
673
Brian Osman9333c872021-01-13 15:06:17 -0500674 // TODO: Doesn't work until SkVM generator supports indexing-by-loop variable
Brian Osmanc92df392021-01-11 13:16:28 -0500675 if (false) {
Brian Osman07c117b2019-05-23 12:51:06 -0700676 float in[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
John Stiles076e9a22020-12-03 10:37:45 -0500677 float out = 0;
Brian Osmanc92df392021-01-11 13:16:28 -0500678 skvm::Program p = build(sums);
679 Args args(gRects);
680 args.add(&in, 8);
681 args.add(&out, 1);
682 p.eval(1, args.fArgs.data());
John Stiles076e9a22020-12-03 10:37:45 -0500683 REPORTER_ASSERT(r, out == static_cast<float>((7 + 1) * (7 + 2) / 2));
Brian Osman07c117b2019-05-23 12:51:06 -0700684 }
685
686 {
Brian Osmanb08cc022020-04-02 11:38:40 -0400687 SkIRect out = SkIRect::MakeEmpty();
Brian Osmanc92df392021-01-11 13:16:28 -0500688 skvm::Program p = build(get_rect_2);
689 Args args(gRects);
690 args.add(&out, 4);
691 p.eval(1, args.fArgs.data());
Brian Osmanb08cc022020-04-02 11:38:40 -0400692 REPORTER_ASSERT(r, out == gRects[2]);
Brian Osman07c117b2019-05-23 12:51:06 -0700693 }
694
Brian Osman9333c872021-01-13 15:06:17 -0500695 // TODO: Doesn't work until SkVM generator supports indexing-by-loop variable
Brian Osmanc92df392021-01-11 13:16:28 -0500696 if (false) {
Brian Osman07c117b2019-05-23 12:51:06 -0700697 ManyRects in;
698 memset(&in, 0, sizeof(in));
699 in.fNumRects = 2;
Brian Osmanc92df392021-01-11 13:16:28 -0500700 skvm::Program p = build(fill_rects);
701 Args args(gRects);
702 args.add(&in, 33);
703 p.eval(1, args.fArgs.data());
Brian Osman07c117b2019-05-23 12:51:06 -0700704 ManyRects expected;
705 memset(&expected, 0, sizeof(expected));
706 expected.fNumRects = 2;
707 for (int i = 0; i < 2; ++i) {
708 expected.fRects[i].fRect = gRects[i];
709 float c = gRects[i].fBottom;
710 expected.fRects[i].fColor = { c, c, c, c };
711 }
712 REPORTER_ASSERT(r, memcmp(&in, &expected, sizeof(in)) == 0);
713 }
714}
Ethan Nicholas91164d12019-05-15 15:29:54 -0400715
Brian Osman869a3e82019-07-18 17:00:34 -0400716static void expect_failure(skiatest::Reporter* r, const char* src) {
Brian Osman0006ad02020-11-18 15:38:39 -0500717 GrShaderCaps caps(GrContextOptions{});
718 SkSL::Compiler compiler(&caps);
John Stiles759880a2020-09-11 12:10:43 -0400719 SkSL::Program::Settings settings;
720 auto program = compiler.convertProgram(SkSL::Program::kGeneric_Kind,
721 SkSL::String(src), settings);
Brian Osmanc39a1432021-01-11 13:15:23 -0500722 REPORTER_ASSERT(r, !program);
Brian Osman869a3e82019-07-18 17:00:34 -0400723}
724
725static void expect_run_failure(skiatest::Reporter* r, const char* src, float* in) {
Brian Osman0006ad02020-11-18 15:38:39 -0500726 GrShaderCaps caps(GrContextOptions{});
727 SkSL::Compiler compiler(&caps);
John Stiles759880a2020-09-11 12:10:43 -0400728 SkSL::Program::Settings settings;
729 auto program = compiler.convertProgram(SkSL::Program::kGeneric_Kind,
730 SkSL::String(src), settings);
Brian Osman869a3e82019-07-18 17:00:34 -0400731 REPORTER_ASSERT(r, program);
732
733 auto byteCode = compiler.toByteCode(*program);
734 REPORTER_ASSERT(r, byteCode);
735
Brian Osmanb08cc022020-04-02 11:38:40 -0400736 auto fun = byteCode->getFunction("main");
737 bool result = byteCode->run(fun, in, fun->getParameterCount(), nullptr, 0, nullptr, 0);
738 REPORTER_ASSERT(r, !result);
Brian Osman869a3e82019-07-18 17:00:34 -0400739}
740
Brian Osman818fd6d2020-12-30 15:06:22 -0500741DEF_TEST(SkSLInterpreterRestrictLoops, r) {
742 // while and do-while loops are not allowed
743 expect_failure(r, "void main(inout float x) { while (x < 1) { x++; } }");
744 expect_failure(r, "void main(inout float x) { do { x++; } while (x < 1); }");
745}
746
Brian Osman6f5358f2019-07-09 14:17:23 -0400747DEF_TEST(SkSLInterpreterRestrictFunctionCalls, r) {
Brian Osman6f5358f2019-07-09 14:17:23 -0400748 // Ensure that simple recursion is not allowed
Brian Osman869a3e82019-07-18 17:00:34 -0400749 expect_failure(r, "float main() { return main() + 1; }");
Brian Osman6f5358f2019-07-09 14:17:23 -0400750
751 // Ensure that calls to undefined functions are not allowed (to prevent mutual recursion)
Brian Osman869a3e82019-07-18 17:00:34 -0400752 expect_failure(r, "float foo(); float bar() { return foo(); } float foo() { return bar(); }");
Brian Osman869a3e82019-07-18 17:00:34 -0400753}
754
Brian Osman54515b72021-01-07 14:38:08 -0500755DEF_TEST(SkSLInterpreterReturnThenCall, r) {
756 // Test that early returns disable execution in subsequently called functions
757 const char* src = R"(
758 float y;
759 void inc () { ++y; }
760 void maybe_inc() { if (y < 0) return; inc(); }
761 void main(inout float x) { y = x; maybe_inc(); x = y; }
762 )";
763
764 ProgramBuilder program(r, src);
765 const SkSL::FunctionDefinition* main = SkSL::Program_GetFunction(*program, "main");
766 REPORTER_ASSERT(r, main);
767
768 skvm::Builder b;
Brian Osmanc92df392021-01-11 13:16:28 -0500769 SkSL::ProgramToSkVM(*program, *main, &b, /*uniforms=*/{});
Brian Osman54515b72021-01-07 14:38:08 -0500770 skvm::Program p = b.done();
771
772 float xs[] = { -2.0f, 0.0f, 3.0f, -1.0f };
Brian Osmanc92df392021-01-11 13:16:28 -0500773 p.eval(4, xs);
Brian Osman54515b72021-01-07 14:38:08 -0500774
775 REPORTER_ASSERT(r, xs[0] == -2.0f);
776 REPORTER_ASSERT(r, xs[1] == 1.0f);
777 REPORTER_ASSERT(r, xs[2] == 4.0f);
778 REPORTER_ASSERT(r, xs[3] == -1.0f);
779}
780
Mike Klein51dc2852020-10-15 19:41:20 -0500781DEF_TEST(SkSLInterpreterEarlyReturn, r) {
Brian Osmane2416d72021-01-06 16:36:56 -0500782 // Test early returns with divergent control flow
Mike Klein51dc2852020-10-15 19:41:20 -0500783 const char* src = "float main(float x, float y) { if (x < y) { return x; } return y; }";
784
Brian Osmane2416d72021-01-06 16:36:56 -0500785 ProgramBuilder program(r, src);
Mike Klein51dc2852020-10-15 19:41:20 -0500786
Brian Osmane2416d72021-01-06 16:36:56 -0500787 const SkSL::FunctionDefinition* main = SkSL::Program_GetFunction(*program, "main");
788 REPORTER_ASSERT(r, main);
Mike Klein51dc2852020-10-15 19:41:20 -0500789
Brian Osmane2416d72021-01-06 16:36:56 -0500790 skvm::Builder b;
Brian Osmanc92df392021-01-11 13:16:28 -0500791 SkSL::ProgramToSkVM(*program, *main, &b, /*uniforms=*/{});
Brian Osmane2416d72021-01-06 16:36:56 -0500792 skvm::Program p = b.done();
Mike Klein51dc2852020-10-15 19:41:20 -0500793
Mike Klein51dc2852020-10-15 19:41:20 -0500794 float xs[] = { 1.0f, 3.0f },
795 ys[] = { 2.0f, 2.0f };
796 float rets[2];
Brian Osmanc92df392021-01-11 13:16:28 -0500797 p.eval(2, xs, ys, rets);
Brian Osmane2416d72021-01-06 16:36:56 -0500798
Mike Klein51dc2852020-10-15 19:41:20 -0500799 REPORTER_ASSERT(r, rets[0] == 1.0f);
Brian Osmane2416d72021-01-06 16:36:56 -0500800 REPORTER_ASSERT(r, rets[1] == 2.0f);
Mike Klein51dc2852020-10-15 19:41:20 -0500801}
802
Brian Osman869a3e82019-07-18 17:00:34 -0400803DEF_TEST(SkSLInterpreterArrayBounds, r) {
John Stiles1b27c3d2020-12-07 12:14:55 -0500804 // Out of bounds array access at compile time prevents a program from being generated at all
805 // (tested in ArrayIndexOutOfRange.sksl).
Brian Osman869a3e82019-07-18 17:00:34 -0400806
John Stiles1b27c3d2020-12-07 12:14:55 -0500807 // Out of bounds array access at runtime is pinned, and we don't update any inout data.
Brian Osman869a3e82019-07-18 17:00:34 -0400808 float in[3] = { -1.0f, 1.0f, 2.0f };
809 expect_run_failure(r, "void main(inout float data[3]) { data[int(data[0])] = 0; }", in);
810 REPORTER_ASSERT(r, in[0] == -1.0f && in[1] == 1.0f && in[2] == 2.0f);
811
812 in[0] = 3.0f;
813 expect_run_failure(r, "void main(inout float data[3]) { data[int(data[0])] = 0; }", in);
814 REPORTER_ASSERT(r, in[0] == 3.0f && in[1] == 1.0f && in[2] == 2.0f);
Brian Osman6f5358f2019-07-09 14:17:23 -0400815}
816
Brian Osman226668a2019-05-14 16:47:30 -0400817DEF_TEST(SkSLInterpreterFunctions, r) {
818 const char* src =
819 "float sqr(float x) { return x * x; }\n"
Brian Osman226668a2019-05-14 16:47:30 -0400820 "float sub(float x, float y) { return x - y; }\n"
Brian Osman6f5358f2019-07-09 14:17:23 -0400821 "float main(float x) { return sub(sqr(x), x); }\n"
Brian Osman226668a2019-05-14 16:47:30 -0400822
823 // Different signatures
Brian Osman15c98cb2020-02-27 18:36:57 +0000824 "float dot(float2 a, float2 b) { return a.x*b.x + a.y*b.y; }\n"
825 "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 -0400826 "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 -0400827 "float dot2_test(float x) { return dot(float2(x, x + 1), float2(1, -1)); }\n";
Brian Osman226668a2019-05-14 16:47:30 -0400828
Brian Osman11233d22021-01-11 10:39:58 -0500829 ProgramBuilder program(r, src);
Brian Osman226668a2019-05-14 16:47:30 -0400830
Brian Osman11233d22021-01-11 10:39:58 -0500831 auto sub = SkSL::Program_GetFunction(*program, "sub");
832 auto sqr = SkSL::Program_GetFunction(*program, "sqr");
833 auto main = SkSL::Program_GetFunction(*program, "main");
834 auto tan = SkSL::Program_GetFunction(*program, "tan");
835 auto dot3 = SkSL::Program_GetFunction(*program, "dot3_test");
836 auto dot2 = SkSL::Program_GetFunction(*program, "dot2_test");
Brian Osman226668a2019-05-14 16:47:30 -0400837
838 REPORTER_ASSERT(r, sub);
839 REPORTER_ASSERT(r, sqr);
840 REPORTER_ASSERT(r, main);
Brian Osman11233d22021-01-11 10:39:58 -0500841 REPORTER_ASSERT(r, !tan); // Getting a non-existent function should return nullptr
Brian Osman226668a2019-05-14 16:47:30 -0400842 REPORTER_ASSERT(r, dot3);
843 REPORTER_ASSERT(r, dot2);
Brian Osman226668a2019-05-14 16:47:30 -0400844
Brian Osman11233d22021-01-11 10:39:58 -0500845 auto test_fn = [&](const SkSL::FunctionDefinition* fn, float in, float expected) {
846 skvm::Builder b;
Brian Osmanc92df392021-01-11 13:16:28 -0500847 SkSL::ProgramToSkVM(*program, *fn, &b, /*uniforms=*/{});
Brian Osman11233d22021-01-11 10:39:58 -0500848 skvm::Program p = b.done();
Brian Osman226668a2019-05-14 16:47:30 -0400849
Brian Osman11233d22021-01-11 10:39:58 -0500850 float out = 0.0f;
Brian Osmanc92df392021-01-11 13:16:28 -0500851 p.eval(1, &in, &out);
Brian Osman11233d22021-01-11 10:39:58 -0500852 REPORTER_ASSERT(r, out == expected);
853 };
Brian Osman226668a2019-05-14 16:47:30 -0400854
Brian Osman11233d22021-01-11 10:39:58 -0500855 test_fn(main, 3.0f, 6.0f);
856 test_fn(dot3, 3.0f, 9.0f);
857 test_fn(dot2, 3.0f, -1.0f);
Brian Osman8c80b192020-02-06 10:49:38 -0500858}
859
Brian Osmand3494ed2019-06-20 15:41:34 -0400860DEF_TEST(SkSLInterpreterOutParams, r) {
861 test(r,
862 "void oneAlpha(inout half4 color) { color.a = 1; }"
863 "void main(inout half4 color) { oneAlpha(color); }",
864 0, 0, 0, 0, 0, 0, 0, 1);
865 test(r,
Brian Osmanb08cc022020-04-02 11:38:40 -0400866 "half2 tricky(half x, half y, inout half2 color, half z) {"
Brian Osmand3494ed2019-06-20 15:41:34 -0400867 " color.xy = color.yx;"
868 " return half2(x + y, z);"
869 "}"
870 "void main(inout half4 color) {"
Brian Osmanb08cc022020-04-02 11:38:40 -0400871 " half2 t = tricky(1, 2, color.rb, 5);"
Brian Osmand3494ed2019-06-20 15:41:34 -0400872 " color.ga = t;"
873 "}",
Brian Osmanb08cc022020-04-02 11:38:40 -0400874 1, 2, 3, 4, 3, 3, 1, 5);
Brian Osmand3494ed2019-06-20 15:41:34 -0400875}
876
Brian Osman401a3662020-09-29 13:20:04 -0400877DEF_TEST(SkSLInterpreterSwizzleSingleLvalue, r) {
878 // Add in your SkSL here.
879 test(r,
880 "void main(inout half4 color) { color.xywz = half4(1,2,3,4); }",
881 0, 0, 0, 0, 1, 2, 4, 3);
882}
883
884DEF_TEST(SkSLInterpreterSwizzleDoubleLvalue, r) {
885 // Add in your SkSL here.
886 test(r,
887 "void main(inout half4 color) { color.xywz.yxzw = half4(1,2,3,4); }",
888 0, 0, 0, 0, 2, 1, 4, 3);
889}
890
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400891DEF_TEST(SkSLInterpreterMathFunctions, r) {
Brian Osmanb380e712019-07-24 17:02:39 -0400892 float value[4], expected[4];
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400893
Brian Osmanb08cc022020-04-02 11:38:40 -0400894 value[0] = 0.0f; expected[0] = 0.0f;
895 test(r, "float main(float x) { return sin(x); }", value, expected);
896 test(r, "float main(float x) { return tan(x); }", value, expected);
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400897
Brian Osmanb380e712019-07-24 17:02:39 -0400898 value[0] = 0.0f; expected[0] = 1.0f;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400899 test(r, "float main(float x) { return cos(x); }", value, expected);
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400900
Brian Osmanb380e712019-07-24 17:02:39 -0400901 value[0] = 25.0f; expected[0] = 5.0f;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400902 test(r, "float main(float x) { return sqrt(x); }", value, expected);
Brian Osmanb380e712019-07-24 17:02:39 -0400903
904 value[0] = 90.0f; expected[0] = sk_float_degrees_to_radians(value[0]);
Brian Osmanb23d66e2019-09-27 10:25:57 -0400905 test(r, "float main(float x) { return radians(x); }", value, expected);
Brian Osmanb380e712019-07-24 17:02:39 -0400906
907 value[0] = 1.0f; value[1] = -1.0f;
908 expected[0] = 1.0f / SK_FloatSqrt2; expected[1] = -1.0f / SK_FloatSqrt2;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400909 test(r, "float2 main(float2 x) { return normalize(x); }", value, expected);
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400910}
911
Brian Osmanfba386b2019-06-20 14:54:15 -0400912DEF_TEST(SkSLInterpreterVoidFunction, r) {
913 test(r,
914 "half x; void foo() { x = 1.0; }"
915 "void main(inout half4 color) { foo(); color.r = x; }",
916 0, 0, 0, 0, 1, 0, 0, 0);
917}
918
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400919DEF_TEST(SkSLInterpreterMix, r) {
Brian Osman08a84962019-06-14 10:17:16 -0400920 float value, expected;
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400921
922 value = 0.5f; expected = 0.0f;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400923 test(r, "float main(float x) { return mix(-10, 10, x); }", &value, &expected);
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400924 value = 0.75f; expected = 5.0f;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400925 test(r, "float main(float x) { return mix(-10, 10, x); }", &value, &expected);
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400926 value = 2.0f; expected = 30.0f;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400927 test(r, "float main(float x) { return mix(-10, 10, x); }", &value, &expected);
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400928
Brian Osman08a84962019-06-14 10:17:16 -0400929 float valueVectors[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f },
930 expectedVector[] = { 3.0f, 4.0f, 5.0f, 6.0f };
Brian Osmanb23d66e2019-09-27 10:25:57 -0400931 test(r, "float4 main(float4 x, float4 y) { return mix(x, y, 0.5); }", valueVectors,
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400932 expectedVector);
933}
934
935DEF_TEST(SkSLInterpreterCross, r) {
Brian Osman08a84962019-06-14 10:17:16 -0400936 float args[] = { 1.0f, 4.0f, -6.0f, -2.0f, 7.0f, -3.0f };
Mike Reedb26b4e72020-01-22 14:31:21 -0500937 SkV3 cross = SkV3::Cross({args[0], args[1], args[2]},
938 {args[3], args[4], args[5]});
939 float expected[] = { cross.x, cross.y, cross.z };
Brian Osmanb23d66e2019-09-27 10:25:57 -0400940 test(r, "float3 main(float3 x, float3 y) { return cross(x, y); }", args, expected);
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400941}
Ben Wagner470e0ac2020-01-22 16:59:21 -0500942
Mike Reed634c9412019-07-18 13:20:04 -0400943DEF_TEST(SkSLInterpreterInverse, r) {
944 {
945 SkMatrix m;
946 m.setRotate(30).postScale(1, 2);
947 float args[4] = { m[0], m[3], m[1], m[4] };
948 SkAssertResult(m.invert(&m));
949 float expt[4] = { m[0], m[3], m[1], m[4] };
Ben Wagner470e0ac2020-01-22 16:59:21 -0500950 test(r, "float2x2 main(float2x2 m) { return inverse(m); }", args, expt, false);
Mike Reed634c9412019-07-18 13:20:04 -0400951 }
952 {
953 SkMatrix m;
954 m.setRotate(30).postScale(1, 2).postTranslate(1, 2);
955 float args[9] = { m[0], m[3], m[6], m[1], m[4], m[7], m[2], m[5], m[8] };
956 SkAssertResult(m.invert(&m));
957 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 -0500958 test(r, "float3x3 main(float3x3 m) { return inverse(m); }", args, expt, false);
Mike Reed634c9412019-07-18 13:20:04 -0400959 }
960 {
961 float args[16], expt[16];
Mike Reed634c9412019-07-18 13:20:04 -0400962 // just some crazy thing that is invertible
Mike Reedb26b4e72020-01-22 14:31:21 -0500963 SkM44 m = {1, 2, 3, 4, 1, 2, 0, 3, 1, 0, 1, 4, 1, 3, 2, 0};
964 m.getColMajor(args);
Mike Reed634c9412019-07-18 13:20:04 -0400965 SkAssertResult(m.invert(&m));
Ben Wagner470e0ac2020-01-22 16:59:21 -0500966 m.getColMajor(expt);
967 test(r, "float4x4 main(float4x4 m) { return inverse(m); }", args, expt, false);
968 }
969}
970
971DEF_TEST(SkSLInterpreterDot, r) {
972 float args[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f };
973 float expected = args[0] * args[2] +
974 args[1] * args[3];
975 test(r, "float main(float2 x, float2 y) { return dot(x, y); }", args, &expected);
976
977 expected = args[0] * args[3] +
978 args[1] * args[4] +
979 args[2] * args[5];
980 test(r, "float main(float3 x, float3 y) { return dot(x, y); }", args, &expected);
981
982 expected = args[0] * args[4] +
983 args[1] * args[5] +
984 args[2] * args[6] +
985 args[3] * args[7];
986 test(r, "float main(float4 x, float4 y) { return dot(x, y); }", args, &expected);
987}
988
Brian Osmanbe0b3b72021-01-06 14:27:35 -0500989class ExternalSqrt : public SkSL::ExternalFunction {
Ben Wagner470e0ac2020-01-22 16:59:21 -0500990public:
Brian Osmanbe0b3b72021-01-06 14:27:35 -0500991 ExternalSqrt(const char* name, SkSL::Compiler& compiler)
John Stiles54e7c052021-01-11 14:22:36 -0500992 : INHERITED(name, *compiler.context().fTypes.fFloat)
Brian Osmanbe0b3b72021-01-06 14:27:35 -0500993 , fCompiler(compiler) {}
Ben Wagner470e0ac2020-01-22 16:59:21 -0500994
Brian Osmanbe0b3b72021-01-06 14:27:35 -0500995 int callParameterCount() const override { return 1; }
Ben Wagner470e0ac2020-01-22 16:59:21 -0500996
997 void getCallParameterTypes(const SkSL::Type** outTypes) const override {
John Stiles54e7c052021-01-11 14:22:36 -0500998 outTypes[0] = fCompiler.context().fTypes.fFloat.get();
Ben Wagner470e0ac2020-01-22 16:59:21 -0500999 }
1000
John Stiles534d7992020-08-18 00:06:01 -04001001 void call(int /*unusedIndex*/, float* arguments, float* outReturn) const override {
Brian Osmanbe0b3b72021-01-06 14:27:35 -05001002 outReturn[0] = sqrt(arguments[0]);
Ben Wagner470e0ac2020-01-22 16:59:21 -05001003 }
1004
1005private:
1006 SkSL::Compiler& fCompiler;
Brian Osmanbe0b3b72021-01-06 14:27:35 -05001007 using INHERITED = SkSL::ExternalFunction;
Ben Wagner470e0ac2020-01-22 16:59:21 -05001008};
1009
Brian Osmanbe0b3b72021-01-06 14:27:35 -05001010DEF_TEST(SkSLInterpreterExternalFunction, r) {
Brian Osman0006ad02020-11-18 15:38:39 -05001011 GrShaderCaps caps(GrContextOptions{});
1012 SkSL::Compiler compiler(&caps);
Ben Wagner470e0ac2020-01-22 16:59:21 -05001013 SkSL::Program::Settings settings;
1014 const char* src = "float main() {"
1015 " return external(25);"
1016 "}";
Brian Osmanbe0b3b72021-01-06 14:27:35 -05001017 std::vector<std::unique_ptr<SkSL::ExternalFunction>> externalFunctions;
1018 externalFunctions.push_back(std::make_unique<ExternalSqrt>("external", compiler));
Brian Osman32d53552020-09-23 13:55:20 -04001019 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(
Brian Osmanbe0b3b72021-01-06 14:27:35 -05001020 SkSL::Program::kGeneric_Kind, SkSL::String(src), settings, &externalFunctions);
Ben Wagner470e0ac2020-01-22 16:59:21 -05001021 REPORTER_ASSERT(r, program);
1022 if (program) {
1023 std::unique_ptr<SkSL::ByteCode> byteCode = compiler.toByteCode(*program);
1024 REPORTER_ASSERT(r, !compiler.errorCount());
1025 if (compiler.errorCount() > 0) {
1026 printf("%s\n%s", src, compiler.errorText().c_str());
1027 return;
1028 }
1029 const SkSL::ByteCodeFunction* main = byteCode->getFunction("main");
Brian Osmanb08cc022020-04-02 11:38:40 -04001030 float out;
1031 SkAssertResult(byteCode->run(main, nullptr, 0, &out, 1, nullptr, 0));
1032 REPORTER_ASSERT(r, out == 5.0);
Ben Wagner470e0ac2020-01-22 16:59:21 -05001033 } else {
1034 printf("%s\n%s", src, compiler.errorText().c_str());
1035 }
1036}
1037
Brian Osmanbe0b3b72021-01-06 14:27:35 -05001038class ExternalSqrt4 : public SkSL::ExternalFunction {
Ben Wagner470e0ac2020-01-22 16:59:21 -05001039public:
Brian Osmanbe0b3b72021-01-06 14:27:35 -05001040 ExternalSqrt4(const char* name, SkSL::Compiler& compiler)
John Stiles54e7c052021-01-11 14:22:36 -05001041 : INHERITED(name, *compiler.context().fTypes.fFloat4)
Brian Osmanbe0b3b72021-01-06 14:27:35 -05001042 , fCompiler(compiler) {}
Brian Osmanb08cc022020-04-02 11:38:40 -04001043
Brian Osmanbe0b3b72021-01-06 14:27:35 -05001044 int callParameterCount() const override { return 1; }
Brian Osmanb08cc022020-04-02 11:38:40 -04001045
Ben Wagner470e0ac2020-01-22 16:59:21 -05001046 void getCallParameterTypes(const SkSL::Type** outTypes) const override {
John Stiles54e7c052021-01-11 14:22:36 -05001047 outTypes[0] = fCompiler.context().fTypes.fFloat4.get();
Ben Wagner470e0ac2020-01-22 16:59:21 -05001048 }
Brian Osmanb08cc022020-04-02 11:38:40 -04001049
John Stiles534d7992020-08-18 00:06:01 -04001050 void call(int /*unusedIndex*/, float* arguments, float* outReturn) const override {
Brian Osmanbe0b3b72021-01-06 14:27:35 -05001051 outReturn[0] = sqrt(arguments[0]);
1052 outReturn[1] = sqrt(arguments[1]);
1053 outReturn[2] = sqrt(arguments[2]);
1054 outReturn[3] = sqrt(arguments[3]);
Ben Wagner470e0ac2020-01-22 16:59:21 -05001055 }
Brian Osmanb08cc022020-04-02 11:38:40 -04001056
Ben Wagner470e0ac2020-01-22 16:59:21 -05001057private:
1058 SkSL::Compiler& fCompiler;
Brian Osmanbe0b3b72021-01-06 14:27:35 -05001059 using INHERITED = SkSL::ExternalFunction;
Ben Wagner470e0ac2020-01-22 16:59:21 -05001060};
Brian Osmanb08cc022020-04-02 11:38:40 -04001061
1062
Brian Osmanbe0b3b72021-01-06 14:27:35 -05001063DEF_TEST(SkSLInterpreterExternalFunctionVector, r) {
Brian Osman0006ad02020-11-18 15:38:39 -05001064 GrShaderCaps caps(GrContextOptions{});
1065 SkSL::Compiler compiler(&caps);
Ben Wagner470e0ac2020-01-22 16:59:21 -05001066 SkSL::Program::Settings settings;
Brian Osman32d53552020-09-23 13:55:20 -04001067 const char* src =
1068 "float4 main() {"
1069 " return external(float4(1, 4, 9, 16));"
1070 "}";
Brian Osmanbe0b3b72021-01-06 14:27:35 -05001071 std::vector<std::unique_ptr<SkSL::ExternalFunction>> externalFunctions;
1072 externalFunctions.push_back(std::make_unique<ExternalSqrt4>("external", compiler));
Brian Osman32d53552020-09-23 13:55:20 -04001073 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(
Brian Osmanbe0b3b72021-01-06 14:27:35 -05001074 SkSL::Program::kGeneric_Kind, SkSL::String(src), settings, &externalFunctions);
Ben Wagner470e0ac2020-01-22 16:59:21 -05001075 REPORTER_ASSERT(r, program);
1076 if (program) {
1077 std::unique_ptr<SkSL::ByteCode> byteCode = compiler.toByteCode(*program);
1078 REPORTER_ASSERT(r, !compiler.errorCount());
1079 if (compiler.errorCount() > 0) {
1080 printf("%s\n%s", src, compiler.errorText().c_str());
1081 return;
1082 }
1083 const SkSL::ByteCodeFunction* main = byteCode->getFunction("main");
Brian Osmanb08cc022020-04-02 11:38:40 -04001084 float out[4];
1085 SkAssertResult(byteCode->run(main, nullptr, 0, out, 4, nullptr, 0));
1086 REPORTER_ASSERT(r, out[0] == 1.0);
1087 REPORTER_ASSERT(r, out[1] == 2.0);
1088 REPORTER_ASSERT(r, out[2] == 3.0);
1089 REPORTER_ASSERT(r, out[3] == 4.0);
Ben Wagner470e0ac2020-01-22 16:59:21 -05001090 } else {
1091 printf("%s\n%s", src, compiler.errorText().c_str());
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001092 }
1093}
Brian Osman1f71f432020-11-18 15:44:46 -05001094
1095#endif // SK_ENABLE_SKSL_INTERPRETER