blob: 3d0e4f47c378be116a16c10cd26514cf54b203f4 [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
Brian Osman035606d2021-01-15 15:30:47 -0500377 // Matrix negation
Brian Osman1e855b22019-05-29 15:21:52 -0400378 for (int i = 0; i < 16; ++i) { expected[i] = (float)(-i); }
Brian Osman035606d2021-01-15 15:30:47 -0500379 test(r, "float4x4 main(float4x4 m) { return -m; }", in, expected);
Brian Osman1e855b22019-05-29 15:21:52 -0400380
Brian Osman909231c2019-05-29 15:34:36 -0400381 // M*V, V*M
Brian Osmanc0f2b642020-12-22 13:35:55 -0500382 for (int i = 0; i < 3; ++i) {
383 expected[i] = 9.0f*i + 10.0f*(i+3) + 11.0f*(i+6);
Brian Osman909231c2019-05-29 15:34:36 -0400384 }
Brian Osmanc0f2b642020-12-22 13:35:55 -0500385 test(r, "float3 main(float3x3 m, float3 v) { return m * v; }", in, expected);
386 for (int i = 0; i < 3; ++i) {
387 expected[i] = 9.0f*(3*i) + 10.0f*(3*i+1) + 11.0f*(3*i+2);
Brian Osman909231c2019-05-29 15:34:36 -0400388 }
Brian Osmanc0f2b642020-12-22 13:35:55 -0500389 test(r, "float3 main(float3x3 m, float3 v) { return v * m; }", in, expected);
Brian Osman29e013d2019-05-28 17:16:03 -0400390
Brian Osman909231c2019-05-29 15:34:36 -0400391 // M*M
392 {
Mike Reed3ef77dd2020-04-06 10:41:09 -0400393 SkM44 m = SkM44::ColMajor(in);
Mike Reedb26b4e72020-01-22 14:31:21 -0500394 SkM44 m2;
395 float in2[16];
Brian Osman909231c2019-05-29 15:34:36 -0400396 for (int i = 0; i < 16; ++i) {
Mike Reedb26b4e72020-01-22 14:31:21 -0500397 in2[i] = (i + 4) % 16;
Brian Osman909231c2019-05-29 15:34:36 -0400398 }
Mike Reed3ef77dd2020-04-06 10:41:09 -0400399 m2 = SkM44::ColMajor(in2);
Brian Osman909231c2019-05-29 15:34:36 -0400400 m.setConcat(m, m2);
401 // Rearrange the columns on the RHS so we detect left-hand/right-hand errors
402 test(r, "float4x4 main(float4x4 m) { return m * float4x4(m[1], m[2], m[3], m[0]); }",
Brian Osmanb23d66e2019-09-27 10:25:57 -0400403 in, (float*)&m);
Brian Osman909231c2019-05-29 15:34:36 -0400404 }
Brian Osman29e013d2019-05-28 17:16:03 -0400405}
406
Brian Osman4e93feb2019-05-16 15:38:00 -0400407DEF_TEST(SkSLInterpreterTernary, r) {
408 test(r, "void main(inout half4 color) { color.r = color.g > color.b ? color.g : color.b; }",
409 0, 1, 2, 0, 2, 1, 2, 0);
410 test(r, "void main(inout half4 color) { color.r = color.g > color.b ? color.g : color.b; }",
411 0, 3, 2, 0, 3, 3, 2, 0);
412}
413
Brian Osman41672152019-05-14 13:37:30 -0400414DEF_TEST(SkSLInterpreterCast, r) {
Brian Osman08a84962019-06-14 10:17:16 -0400415 union Val {
416 float f;
Brian Osman08a84962019-06-14 10:17:16 -0400417 int32_t s;
418 };
Brian Osman41672152019-05-14 13:37:30 -0400419
Brian Osman08a84962019-06-14 10:17:16 -0400420 Val input[2];
421 Val expected[2];
Brian Osman41672152019-05-14 13:37:30 -0400422
Brian Osman08a84962019-06-14 10:17:16 -0400423 input[0].s = 3;
424 input[1].s = -5;
425 expected[0].f = 3.0f;
426 expected[1].f = -5.0f;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400427 test(r, "float main(int x) { return float (x); }", (float*)input, (float*)expected);
428 test(r, "float2 main(int2 x) { return float2(x); }", (float*)input, (float*)expected);
Brian Osman41672152019-05-14 13:37:30 -0400429
Brian Osman08a84962019-06-14 10:17:16 -0400430 input[0].f = 3.0f;
431 input[1].f = -5.0f;
432 expected[0].s = 3;
433 expected[1].s = -5;
Brian Osmanb08cc022020-04-02 11:38:40 -0400434 test(r, "int main(float x) { return int (x); }", (float*)input, (float*)expected);
435 test(r, "int2 main(float2 x) { return int2(x); }", (float*)input, (float*)expected);
Brian Osman08a84962019-06-14 10:17:16 -0400436
437 input[0].s = 3;
438 expected[0].f = 3.0f;
439 expected[1].f = 3.0f;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400440 test(r, "float2 main(int x) { return float2(x); }", (float*)input, (float*)expected);
Brian Osman41672152019-05-14 13:37:30 -0400441}
442
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400443DEF_TEST(SkSLInterpreterIf, r) {
444 test(r, "void main(inout half4 color) { if (color.r > color.g) color.a = 1; }", 5, 3, 0, 0,
445 5, 3, 0, 1);
446 test(r, "void main(inout half4 color) { if (color.r > color.g) color.a = 1; }", 5, 5, 0, 0,
447 5, 5, 0, 0);
448 test(r, "void main(inout half4 color) { if (color.r > color.g) color.a = 1; }", 5, 6, 0, 0,
449 5, 6, 0, 0);
450 test(r, "void main(inout half4 color) { if (color.r < color.g) color.a = 1; }", 3, 5, 0, 0,
451 3, 5, 0, 1);
452 test(r, "void main(inout half4 color) { if (color.r < color.g) color.a = 1; }", 5, 5, 0, 0,
453 5, 5, 0, 0);
454 test(r, "void main(inout half4 color) { if (color.r < color.g) color.a = 1; }", 6, 5, 0, 0,
455 6, 5, 0, 0);
456 test(r, "void main(inout half4 color) { if (color.r >= color.g) color.a = 1; }", 5, 3, 0, 0,
457 5, 3, 0, 1);
458 test(r, "void main(inout half4 color) { if (color.r >= color.g) color.a = 1; }", 5, 5, 0, 0,
459 5, 5, 0, 1);
460 test(r, "void main(inout half4 color) { if (color.r >= color.g) color.a = 1; }", 5, 6, 0, 0,
461 5, 6, 0, 0);
462 test(r, "void main(inout half4 color) { if (color.r <= color.g) color.a = 1; }", 3, 5, 0, 0,
463 3, 5, 0, 1);
464 test(r, "void main(inout half4 color) { if (color.r <= color.g) color.a = 1; }", 5, 5, 0, 0,
465 5, 5, 0, 1);
466 test(r, "void main(inout half4 color) { if (color.r <= color.g) color.a = 1; }", 6, 5, 0, 0,
467 6, 5, 0, 0);
468 test(r, "void main(inout half4 color) { if (color.r == color.g) color.a = 1; }", 2, 2, 0, 0,
469 2, 2, 0, 1);
470 test(r, "void main(inout half4 color) { if (color.r == color.g) color.a = 1; }", 2, -2, 0, 0,
471 2, -2, 0, 0);
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, 1);
Brian Osmane5bbce22019-09-23 12:38:40 -0400476 test(r, "void main(inout half4 color) { if (!(color.r == color.g)) color.a = 1; }", 2, 2, 0, 0,
477 2, 2, 0, 0);
478 test(r, "void main(inout half4 color) { if (!(color.r == color.g)) color.a = 1; }", 2, -2, 0, 0,
479 2, -2, 0, 1);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400480 test(r, "void main(inout half4 color) { if (color.r == color.g) color.a = 1; else "
481 "color.a = 2; }", 1, 1, 0, 0, 1, 1, 0, 1);
482 test(r, "void main(inout half4 color) { if (color.r == color.g) color.a = 1; else "
483 "color.a = 2; }", 2, -2, 0, 0, 2, -2, 0, 2);
484}
485
Brian Osman16e6fd52019-05-29 11:19:00 -0400486DEF_TEST(SkSLInterpreterIfVector, r) {
487 test(r, "void main(inout half4 color) { if (color.rg == color.ba) color.a = 1; }",
488 1, 2, 1, 2, 1, 2, 1, 1);
489 test(r, "void main(inout half4 color) { if (color.rg == color.ba) color.a = 1; }",
490 1, 2, 3, 2, 1, 2, 3, 2);
491 test(r, "void main(inout half4 color) { if (color.rg != color.ba) color.a = 1; }",
492 1, 2, 1, 2, 1, 2, 1, 2);
493 test(r, "void main(inout half4 color) { if (color.rg != color.ba) color.a = 1; }",
494 1, 2, 3, 2, 1, 2, 3, 1);
495}
496
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400497DEF_TEST(SkSLInterpreterFor, r) {
John Stilesaf9b58e2021-01-13 17:48:36 -0500498 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 -0500499 0, 0, 0, 0,
Brian Osman9333c872021-01-13 15:06:17 -0500500 55, 0, 0, 0);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400501 test(r,
502 "void main(inout half4 color) {"
503 " for (int i = 1; i <= 10; ++i)"
Brian Osman77ba8102021-01-12 17:15:30 -0500504 " for (int j = 1; j <= 10; ++j)"
John Stilesaf9b58e2021-01-13 17:48:36 -0500505 " if (j >= i) { color.r += half(j); }"
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400506 "}",
507 0, 0, 0, 0,
Brian Osman9333c872021-01-13 15:06:17 -0500508 385, 0, 0, 0);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400509 test(r,
510 "void main(inout half4 color) {"
511 " for (int i = 1; i <= 10; ++i)"
Brian Osman77ba8102021-01-12 17:15:30 -0500512 " for (int j = 1; j < 20 ; ++j) {"
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400513 " if (i == j) continue;"
514 " if (j > 10) break;"
John Stilesaf9b58e2021-01-13 17:48:36 -0500515 " color.r += half(j);"
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400516 " }"
517 "}",
518 0, 0, 0, 0,
Brian Osman9333c872021-01-13 15:06:17 -0500519 495, 0, 0, 0);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400520}
521
Brian Osmanf3fa6002019-05-17 14:26:53 -0400522DEF_TEST(SkSLInterpreterPrefixPostfix, r) {
523 test(r, "void main(inout half4 color) { color.r = ++color.g; }", 1, 2, 3, 4, 3, 3, 3, 4);
524 test(r, "void main(inout half4 color) { color.r = color.g++; }", 1, 2, 3, 4, 2, 3, 3, 4);
525}
526
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400527DEF_TEST(SkSLInterpreterSwizzle, r) {
528 test(r, "void main(inout half4 color) { color = color.abgr; }", 1, 2, 3, 4, 4, 3, 2, 1);
529 test(r, "void main(inout half4 color) { color.rgb = half4(5, 6, 7, 8).bbg; }", 1, 2, 3, 4, 7, 7,
530 6, 4);
John Stilesaf9b58e2021-01-13 17:48:36 -0500531 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 -0400532 5, 4);
533}
534
535DEF_TEST(SkSLInterpreterGlobal, r) {
John Stilesaf9b58e2021-01-13 17:48:36 -0500536 test(r, "int x; void main(inout half4 color) { x = 10; color.b = half(x); }", 1, 2, 3, 4, 1, 2,
537 10, 4);
Brian Osmanb7451292019-05-15 13:02:13 -0400538 test(r, "float4 x; void main(inout float4 color) { x = color * 2; color = x; }",
539 1, 2, 3, 4, 2, 4, 6, 8);
540 test(r, "float4 x; void main(inout float4 color) { x = float4(5, 6, 7, 8); color = x.wzyx; }",
541 1, 2, 3, 4, 8, 7, 6, 5);
Brian Osman1091f022019-05-16 09:42:16 -0400542 test(r, "float4 x; void main(inout float4 color) { x.wzyx = float4(5, 6, 7, 8); color = x; }",
543 1, 2, 3, 4, 8, 7, 6, 5);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400544}
Ethan Nicholas746035a2019-04-23 13:31:09 -0400545
546DEF_TEST(SkSLInterpreterGeneric, r) {
547 float value1 = 5;
548 float expected1 = 25;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400549 test(r, "float main(float x) { return x * x; }", &value1, &expected1);
Ethan Nicholas746035a2019-04-23 13:31:09 -0400550 float value2[2] = { 5, 25 };
551 float expected2[2] = { 25, 625 };
Brian Osmanb23d66e2019-09-27 10:25:57 -0400552 test(r, "float2 main(float x, float y) { return float2(x * x, y * y); }", value2, expected2);
Ethan Nicholas746035a2019-04-23 13:31:09 -0400553}
Brian Osmand369a5e2019-05-09 13:13:25 -0400554
Brian Osman07c117b2019-05-23 12:51:06 -0700555DEF_TEST(SkSLInterpreterCompound, r) {
556 struct RectAndColor { SkIRect fRect; SkColor4f fColor; };
557 struct ManyRects { int fNumRects; RectAndColor fRects[4]; };
558
559 const char* src =
560 // Some struct definitions
561 "struct Point { int x; int y; };\n"
562 "struct Rect { Point p0; Point p1; };\n"
563 "struct RectAndColor { Rect r; float4 color; };\n"
564
565 // Structs as globals, parameters, return values
566 "RectAndColor temp;\n"
567 "int rect_height(Rect r) { return r.p1.y - r.p0.y; }\n"
568 "RectAndColor make_blue_rect(int w, int h) {\n"
569 " temp.r.p0.x = temp.r.p0.y = 0;\n"
570 " temp.r.p1.x = w; temp.r.p1.y = h;\n"
571 " temp.color = float4(0, 1, 0, 1);\n"
572 " return temp;\n"
573 "}\n"
574
575 // Initialization and assignment of types larger than 4 slots
576 "RectAndColor init_big(RectAndColor r) { RectAndColor s = r; return s; }\n"
577 "RectAndColor copy_big(RectAndColor r) { RectAndColor s; s = r; return s; }\n"
578
579 // Same for arrays, including some non-constant indexing
Brian Osman07c117b2019-05-23 12:51:06 -0700580 "int median(int a[15]) { return a[7]; }\n"
John Stiles076e9a22020-12-03 10:37:45 -0500581
582 "float tempFloats[8];\n"
583 "float sums(float a[8]) {\n"
Brian Osman07c117b2019-05-23 12:51:06 -0700584 " tempFloats[0] = a[0];\n"
585 " for (int i = 1; i < 8; ++i) { tempFloats[i] = tempFloats[i - 1] + a[i]; }\n"
John Stiles076e9a22020-12-03 10:37:45 -0500586 " return tempFloats[7];\n"
Brian Osman07c117b2019-05-23 12:51:06 -0700587 "}\n"
588
Brian Osmanc92df392021-01-11 13:16:28 -0500589 // Uniforms, array-of-structs
Ethan Nicholas31cff272019-09-26 13:04:48 -0400590 "uniform Rect gRects[4];\n"
Brian Osmanc92df392021-01-11 13:16:28 -0500591 "Rect get_rect_2() { return gRects[2]; }\n"
Brian Osman07c117b2019-05-23 12:51:06 -0700592
593 // Kitchen sink (swizzles, inout, SoAoS)
594 "struct ManyRects { int numRects; RectAndColor rects[4]; };\n"
595 "void fill_rects(inout ManyRects mr) {\n"
Brian Osman77ba8102021-01-12 17:15:30 -0500596 " for (int i = 0; i < 4; ++i) {\n"
597 " if (i >= mr.numRects) { break; }\n"
Brian Osman07c117b2019-05-23 12:51:06 -0700598 " mr.rects[i].r = gRects[i];\n"
John Stilesaf9b58e2021-01-13 17:48:36 -0500599 " float b = float(mr.rects[i].r.p1.y);\n"
Brian Osman07c117b2019-05-23 12:51:06 -0700600 " mr.rects[i].color = float4(b, b, b, b);\n"
601 " }\n"
602 "}\n";
603
Brian Osmanc92df392021-01-11 13:16:28 -0500604 ProgramBuilder program(r, src);
Brian Osman07c117b2019-05-23 12:51:06 -0700605
Brian Osmanc92df392021-01-11 13:16:28 -0500606 auto rect_height = SkSL::Program_GetFunction(*program, "rect_height"),
607 make_blue_rect = SkSL::Program_GetFunction(*program, "make_blue_rect"),
608 median = SkSL::Program_GetFunction(*program, "median"),
609 sums = SkSL::Program_GetFunction(*program, "sums"),
610 get_rect_2 = SkSL::Program_GetFunction(*program, "get_rect_2"),
611 fill_rects = SkSL::Program_GetFunction(*program, "fill_rects");
Brian Osman07c117b2019-05-23 12:51:06 -0700612
613 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 -0500614
615 auto build = [&](const SkSL::FunctionDefinition* fn) {
616 skvm::Builder b;
617 skvm::Ptr uniformPtr = b.uniform();
618 skvm::Val uniforms[16];
619 for (int i = 0; i < 16; ++i) {
620 uniforms[i] = b.uniform32(uniformPtr, i * sizeof(int)).id;
621 }
622 SkSL::ProgramToSkVM(*program, *fn, &b, uniforms);
623 return b.done();
624 };
625
626 struct Args {
627 Args(void* uniformData) { fArgs.push_back(uniformData); }
628 void add(void* base, int n) {
629 for (int i = 0; i < n; ++i) {
630 fArgs.push_back(SkTAddOffset<void>(base, i * sizeof(float)));
631 }
632 }
633 std::vector<void*> fArgs;
634 };
Brian Osman07c117b2019-05-23 12:51:06 -0700635
Brian Osman07c117b2019-05-23 12:51:06 -0700636 {
637 SkIRect in = SkIRect::MakeXYWH(10, 10, 20, 30);
Brian Osmanb08cc022020-04-02 11:38:40 -0400638 int out = 0;
Brian Osmanc92df392021-01-11 13:16:28 -0500639 skvm::Program p = build(rect_height);
640 Args args(gRects);
641 args.add(&in, 4);
642 args.add(&out, 1);
643 p.eval(1, args.fArgs.data());
Brian Osmanb08cc022020-04-02 11:38:40 -0400644 REPORTER_ASSERT(r, out == 30);
Brian Osman07c117b2019-05-23 12:51:06 -0700645 }
646
647 {
648 int in[2] = { 15, 25 };
Brian Osmanb08cc022020-04-02 11:38:40 -0400649 RectAndColor out;
Brian Osmanc92df392021-01-11 13:16:28 -0500650 skvm::Program p = build(make_blue_rect);
651 Args args(gRects);
652 args.add(&in, 2);
653 args.add(&out, 8);
654 p.eval(1, args.fArgs.data());
Brian Osmanb08cc022020-04-02 11:38:40 -0400655 REPORTER_ASSERT(r, out.fRect.width() == 15);
656 REPORTER_ASSERT(r, out.fRect.height() == 25);
Brian Osman07c117b2019-05-23 12:51:06 -0700657 SkColor4f blue = { 0.0f, 1.0f, 0.0f, 1.0f };
Brian Osmanb08cc022020-04-02 11:38:40 -0400658 REPORTER_ASSERT(r, out.fColor == blue);
Brian Osman07c117b2019-05-23 12:51:06 -0700659 }
660
661 {
662 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 -0400663 int out = 0;
Brian Osmanc92df392021-01-11 13:16:28 -0500664 skvm::Program p = build(median);
665 Args args(gRects);
666 args.add(&in, 15);
667 args.add(&out, 1);
668 p.eval(1, args.fArgs.data());
Brian Osmanb08cc022020-04-02 11:38:40 -0400669 REPORTER_ASSERT(r, out == 8);
Brian Osman07c117b2019-05-23 12:51:06 -0700670 }
671
Brian Osmanb8ebe232021-01-19 16:33:11 -0500672 {
Brian Osman07c117b2019-05-23 12:51:06 -0700673 float in[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
John Stiles076e9a22020-12-03 10:37:45 -0500674 float out = 0;
Brian Osmanc92df392021-01-11 13:16:28 -0500675 skvm::Program p = build(sums);
676 Args args(gRects);
677 args.add(&in, 8);
678 args.add(&out, 1);
679 p.eval(1, args.fArgs.data());
John Stiles076e9a22020-12-03 10:37:45 -0500680 REPORTER_ASSERT(r, out == static_cast<float>((7 + 1) * (7 + 2) / 2));
Brian Osman07c117b2019-05-23 12:51:06 -0700681 }
682
683 {
Brian Osmanb08cc022020-04-02 11:38:40 -0400684 SkIRect out = SkIRect::MakeEmpty();
Brian Osmanc92df392021-01-11 13:16:28 -0500685 skvm::Program p = build(get_rect_2);
686 Args args(gRects);
687 args.add(&out, 4);
688 p.eval(1, args.fArgs.data());
Brian Osmanb08cc022020-04-02 11:38:40 -0400689 REPORTER_ASSERT(r, out == gRects[2]);
Brian Osman07c117b2019-05-23 12:51:06 -0700690 }
691
Brian Osmanb8ebe232021-01-19 16:33:11 -0500692 {
Brian Osman07c117b2019-05-23 12:51:06 -0700693 ManyRects in;
694 memset(&in, 0, sizeof(in));
695 in.fNumRects = 2;
Brian Osmanc92df392021-01-11 13:16:28 -0500696 skvm::Program p = build(fill_rects);
697 Args args(gRects);
698 args.add(&in, 33);
699 p.eval(1, args.fArgs.data());
Brian Osman07c117b2019-05-23 12:51:06 -0700700 ManyRects expected;
701 memset(&expected, 0, sizeof(expected));
702 expected.fNumRects = 2;
703 for (int i = 0; i < 2; ++i) {
704 expected.fRects[i].fRect = gRects[i];
705 float c = gRects[i].fBottom;
706 expected.fRects[i].fColor = { c, c, c, c };
707 }
708 REPORTER_ASSERT(r, memcmp(&in, &expected, sizeof(in)) == 0);
709 }
710}
Ethan Nicholas91164d12019-05-15 15:29:54 -0400711
Brian Osman869a3e82019-07-18 17:00:34 -0400712static void expect_failure(skiatest::Reporter* r, const char* src) {
Brian Osman0006ad02020-11-18 15:38:39 -0500713 GrShaderCaps caps(GrContextOptions{});
714 SkSL::Compiler compiler(&caps);
John Stiles759880a2020-09-11 12:10:43 -0400715 SkSL::Program::Settings settings;
716 auto program = compiler.convertProgram(SkSL::Program::kGeneric_Kind,
717 SkSL::String(src), settings);
Brian Osmanc39a1432021-01-11 13:15:23 -0500718 REPORTER_ASSERT(r, !program);
Brian Osman869a3e82019-07-18 17:00:34 -0400719}
720
Brian Osman818fd6d2020-12-30 15:06:22 -0500721DEF_TEST(SkSLInterpreterRestrictLoops, r) {
722 // while and do-while loops are not allowed
723 expect_failure(r, "void main(inout float x) { while (x < 1) { x++; } }");
724 expect_failure(r, "void main(inout float x) { do { x++; } while (x < 1); }");
725}
726
Brian Osman6f5358f2019-07-09 14:17:23 -0400727DEF_TEST(SkSLInterpreterRestrictFunctionCalls, r) {
Brian Osman6f5358f2019-07-09 14:17:23 -0400728 // Ensure that simple recursion is not allowed
Brian Osman869a3e82019-07-18 17:00:34 -0400729 expect_failure(r, "float main() { return main() + 1; }");
Brian Osman6f5358f2019-07-09 14:17:23 -0400730
731 // Ensure that calls to undefined functions are not allowed (to prevent mutual recursion)
Brian Osman869a3e82019-07-18 17:00:34 -0400732 expect_failure(r, "float foo(); float bar() { return foo(); } float foo() { return bar(); }");
Brian Osman869a3e82019-07-18 17:00:34 -0400733}
734
Brian Osman54515b72021-01-07 14:38:08 -0500735DEF_TEST(SkSLInterpreterReturnThenCall, r) {
736 // Test that early returns disable execution in subsequently called functions
737 const char* src = R"(
738 float y;
739 void inc () { ++y; }
740 void maybe_inc() { if (y < 0) return; inc(); }
741 void main(inout float x) { y = x; maybe_inc(); x = y; }
742 )";
743
744 ProgramBuilder program(r, src);
745 const SkSL::FunctionDefinition* main = SkSL::Program_GetFunction(*program, "main");
746 REPORTER_ASSERT(r, main);
747
748 skvm::Builder b;
Brian Osmanc92df392021-01-11 13:16:28 -0500749 SkSL::ProgramToSkVM(*program, *main, &b, /*uniforms=*/{});
Brian Osman54515b72021-01-07 14:38:08 -0500750 skvm::Program p = b.done();
751
752 float xs[] = { -2.0f, 0.0f, 3.0f, -1.0f };
Brian Osmanc92df392021-01-11 13:16:28 -0500753 p.eval(4, xs);
Brian Osman54515b72021-01-07 14:38:08 -0500754
755 REPORTER_ASSERT(r, xs[0] == -2.0f);
756 REPORTER_ASSERT(r, xs[1] == 1.0f);
757 REPORTER_ASSERT(r, xs[2] == 4.0f);
758 REPORTER_ASSERT(r, xs[3] == -1.0f);
759}
760
Mike Klein51dc2852020-10-15 19:41:20 -0500761DEF_TEST(SkSLInterpreterEarlyReturn, r) {
Brian Osmane2416d72021-01-06 16:36:56 -0500762 // Test early returns with divergent control flow
Mike Klein51dc2852020-10-15 19:41:20 -0500763 const char* src = "float main(float x, float y) { if (x < y) { return x; } return y; }";
764
Brian Osmane2416d72021-01-06 16:36:56 -0500765 ProgramBuilder program(r, src);
Mike Klein51dc2852020-10-15 19:41:20 -0500766
Brian Osmane2416d72021-01-06 16:36:56 -0500767 const SkSL::FunctionDefinition* main = SkSL::Program_GetFunction(*program, "main");
768 REPORTER_ASSERT(r, main);
Mike Klein51dc2852020-10-15 19:41:20 -0500769
Brian Osmane2416d72021-01-06 16:36:56 -0500770 skvm::Builder b;
Brian Osmanc92df392021-01-11 13:16:28 -0500771 SkSL::ProgramToSkVM(*program, *main, &b, /*uniforms=*/{});
Brian Osmane2416d72021-01-06 16:36:56 -0500772 skvm::Program p = b.done();
Mike Klein51dc2852020-10-15 19:41:20 -0500773
Mike Klein51dc2852020-10-15 19:41:20 -0500774 float xs[] = { 1.0f, 3.0f },
775 ys[] = { 2.0f, 2.0f };
776 float rets[2];
Brian Osmanc92df392021-01-11 13:16:28 -0500777 p.eval(2, xs, ys, rets);
Brian Osmane2416d72021-01-06 16:36:56 -0500778
Mike Klein51dc2852020-10-15 19:41:20 -0500779 REPORTER_ASSERT(r, rets[0] == 1.0f);
Brian Osmane2416d72021-01-06 16:36:56 -0500780 REPORTER_ASSERT(r, rets[1] == 2.0f);
Mike Klein51dc2852020-10-15 19:41:20 -0500781}
782
Brian Osman226668a2019-05-14 16:47:30 -0400783DEF_TEST(SkSLInterpreterFunctions, r) {
784 const char* src =
785 "float sqr(float x) { return x * x; }\n"
Brian Osman226668a2019-05-14 16:47:30 -0400786 "float sub(float x, float y) { return x - y; }\n"
Brian Osman6f5358f2019-07-09 14:17:23 -0400787 "float main(float x) { return sub(sqr(x), x); }\n"
Brian Osman226668a2019-05-14 16:47:30 -0400788
789 // Different signatures
Brian Osman15c98cb2020-02-27 18:36:57 +0000790 "float dot(float2 a, float2 b) { return a.x*b.x + a.y*b.y; }\n"
791 "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 -0400792 "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 -0400793 "float dot2_test(float x) { return dot(float2(x, x + 1), float2(1, -1)); }\n";
Brian Osman226668a2019-05-14 16:47:30 -0400794
Brian Osman11233d22021-01-11 10:39:58 -0500795 ProgramBuilder program(r, src);
Brian Osman226668a2019-05-14 16:47:30 -0400796
Brian Osman11233d22021-01-11 10:39:58 -0500797 auto sub = SkSL::Program_GetFunction(*program, "sub");
798 auto sqr = SkSL::Program_GetFunction(*program, "sqr");
799 auto main = SkSL::Program_GetFunction(*program, "main");
800 auto tan = SkSL::Program_GetFunction(*program, "tan");
801 auto dot3 = SkSL::Program_GetFunction(*program, "dot3_test");
802 auto dot2 = SkSL::Program_GetFunction(*program, "dot2_test");
Brian Osman226668a2019-05-14 16:47:30 -0400803
804 REPORTER_ASSERT(r, sub);
805 REPORTER_ASSERT(r, sqr);
806 REPORTER_ASSERT(r, main);
Brian Osman11233d22021-01-11 10:39:58 -0500807 REPORTER_ASSERT(r, !tan); // Getting a non-existent function should return nullptr
Brian Osman226668a2019-05-14 16:47:30 -0400808 REPORTER_ASSERT(r, dot3);
809 REPORTER_ASSERT(r, dot2);
Brian Osman226668a2019-05-14 16:47:30 -0400810
Brian Osman11233d22021-01-11 10:39:58 -0500811 auto test_fn = [&](const SkSL::FunctionDefinition* fn, float in, float expected) {
812 skvm::Builder b;
Brian Osmanc92df392021-01-11 13:16:28 -0500813 SkSL::ProgramToSkVM(*program, *fn, &b, /*uniforms=*/{});
Brian Osman11233d22021-01-11 10:39:58 -0500814 skvm::Program p = b.done();
Brian Osman226668a2019-05-14 16:47:30 -0400815
Brian Osman11233d22021-01-11 10:39:58 -0500816 float out = 0.0f;
Brian Osmanc92df392021-01-11 13:16:28 -0500817 p.eval(1, &in, &out);
Brian Osman11233d22021-01-11 10:39:58 -0500818 REPORTER_ASSERT(r, out == expected);
819 };
Brian Osman226668a2019-05-14 16:47:30 -0400820
Brian Osman11233d22021-01-11 10:39:58 -0500821 test_fn(main, 3.0f, 6.0f);
822 test_fn(dot3, 3.0f, 9.0f);
823 test_fn(dot2, 3.0f, -1.0f);
Brian Osman8c80b192020-02-06 10:49:38 -0500824}
825
Brian Osmand3494ed2019-06-20 15:41:34 -0400826DEF_TEST(SkSLInterpreterOutParams, r) {
827 test(r,
828 "void oneAlpha(inout half4 color) { color.a = 1; }"
829 "void main(inout half4 color) { oneAlpha(color); }",
830 0, 0, 0, 0, 0, 0, 0, 1);
831 test(r,
Brian Osmanb08cc022020-04-02 11:38:40 -0400832 "half2 tricky(half x, half y, inout half2 color, half z) {"
Brian Osmand3494ed2019-06-20 15:41:34 -0400833 " color.xy = color.yx;"
834 " return half2(x + y, z);"
835 "}"
836 "void main(inout half4 color) {"
Brian Osmanb08cc022020-04-02 11:38:40 -0400837 " half2 t = tricky(1, 2, color.rb, 5);"
Brian Osmand3494ed2019-06-20 15:41:34 -0400838 " color.ga = t;"
839 "}",
Brian Osmanb08cc022020-04-02 11:38:40 -0400840 1, 2, 3, 4, 3, 3, 1, 5);
Brian Osmand3494ed2019-06-20 15:41:34 -0400841}
842
Brian Osman401a3662020-09-29 13:20:04 -0400843DEF_TEST(SkSLInterpreterSwizzleSingleLvalue, r) {
844 // Add in your SkSL here.
845 test(r,
846 "void main(inout half4 color) { color.xywz = half4(1,2,3,4); }",
847 0, 0, 0, 0, 1, 2, 4, 3);
848}
849
850DEF_TEST(SkSLInterpreterSwizzleDoubleLvalue, r) {
851 // Add in your SkSL here.
852 test(r,
853 "void main(inout half4 color) { color.xywz.yxzw = half4(1,2,3,4); }",
854 0, 0, 0, 0, 2, 1, 4, 3);
855}
856
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400857DEF_TEST(SkSLInterpreterMathFunctions, r) {
Brian Osmanb380e712019-07-24 17:02:39 -0400858 float value[4], expected[4];
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400859
Brian Osmanb08cc022020-04-02 11:38:40 -0400860 value[0] = 0.0f; expected[0] = 0.0f;
861 test(r, "float main(float x) { return sin(x); }", value, expected);
862 test(r, "float main(float x) { return tan(x); }", value, expected);
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400863
Brian Osmanb380e712019-07-24 17:02:39 -0400864 value[0] = 0.0f; expected[0] = 1.0f;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400865 test(r, "float main(float x) { return cos(x); }", value, expected);
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400866
Brian Osmanb380e712019-07-24 17:02:39 -0400867 value[0] = 25.0f; expected[0] = 5.0f;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400868 test(r, "float main(float x) { return sqrt(x); }", value, expected);
Brian Osmanb380e712019-07-24 17:02:39 -0400869
870 value[0] = 90.0f; expected[0] = sk_float_degrees_to_radians(value[0]);
Brian Osmanb23d66e2019-09-27 10:25:57 -0400871 test(r, "float main(float x) { return radians(x); }", value, expected);
Brian Osmanb380e712019-07-24 17:02:39 -0400872
873 value[0] = 1.0f; value[1] = -1.0f;
874 expected[0] = 1.0f / SK_FloatSqrt2; expected[1] = -1.0f / SK_FloatSqrt2;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400875 test(r, "float2 main(float2 x) { return normalize(x); }", value, expected);
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400876}
877
Brian Osmanfba386b2019-06-20 14:54:15 -0400878DEF_TEST(SkSLInterpreterVoidFunction, r) {
879 test(r,
880 "half x; void foo() { x = 1.0; }"
881 "void main(inout half4 color) { foo(); color.r = x; }",
882 0, 0, 0, 0, 1, 0, 0, 0);
883}
884
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400885DEF_TEST(SkSLInterpreterMix, r) {
Brian Osman08a84962019-06-14 10:17:16 -0400886 float value, expected;
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400887
888 value = 0.5f; expected = 0.0f;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400889 test(r, "float main(float x) { return mix(-10, 10, x); }", &value, &expected);
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400890 value = 0.75f; expected = 5.0f;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400891 test(r, "float main(float x) { return mix(-10, 10, x); }", &value, &expected);
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400892 value = 2.0f; expected = 30.0f;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400893 test(r, "float main(float x) { return mix(-10, 10, x); }", &value, &expected);
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400894
Brian Osman08a84962019-06-14 10:17:16 -0400895 float valueVectors[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f },
896 expectedVector[] = { 3.0f, 4.0f, 5.0f, 6.0f };
Brian Osmanb23d66e2019-09-27 10:25:57 -0400897 test(r, "float4 main(float4 x, float4 y) { return mix(x, y, 0.5); }", valueVectors,
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400898 expectedVector);
899}
900
901DEF_TEST(SkSLInterpreterCross, r) {
Brian Osman08a84962019-06-14 10:17:16 -0400902 float args[] = { 1.0f, 4.0f, -6.0f, -2.0f, 7.0f, -3.0f };
Mike Reedb26b4e72020-01-22 14:31:21 -0500903 SkV3 cross = SkV3::Cross({args[0], args[1], args[2]},
904 {args[3], args[4], args[5]});
905 float expected[] = { cross.x, cross.y, cross.z };
Brian Osmanb23d66e2019-09-27 10:25:57 -0400906 test(r, "float3 main(float3 x, float3 y) { return cross(x, y); }", args, expected);
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400907}
Ben Wagner470e0ac2020-01-22 16:59:21 -0500908
Mike Reed634c9412019-07-18 13:20:04 -0400909DEF_TEST(SkSLInterpreterInverse, r) {
910 {
911 SkMatrix m;
912 m.setRotate(30).postScale(1, 2);
913 float args[4] = { m[0], m[3], m[1], m[4] };
914 SkAssertResult(m.invert(&m));
915 float expt[4] = { m[0], m[3], m[1], m[4] };
Ben Wagner470e0ac2020-01-22 16:59:21 -0500916 test(r, "float2x2 main(float2x2 m) { return inverse(m); }", args, expt, false);
Mike Reed634c9412019-07-18 13:20:04 -0400917 }
918 {
919 SkMatrix m;
920 m.setRotate(30).postScale(1, 2).postTranslate(1, 2);
921 float args[9] = { m[0], m[3], m[6], m[1], m[4], m[7], m[2], m[5], m[8] };
922 SkAssertResult(m.invert(&m));
923 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 -0500924 test(r, "float3x3 main(float3x3 m) { return inverse(m); }", args, expt, false);
Mike Reed634c9412019-07-18 13:20:04 -0400925 }
926 {
927 float args[16], expt[16];
Mike Reed634c9412019-07-18 13:20:04 -0400928 // just some crazy thing that is invertible
Mike Reedb26b4e72020-01-22 14:31:21 -0500929 SkM44 m = {1, 2, 3, 4, 1, 2, 0, 3, 1, 0, 1, 4, 1, 3, 2, 0};
930 m.getColMajor(args);
Mike Reed634c9412019-07-18 13:20:04 -0400931 SkAssertResult(m.invert(&m));
Ben Wagner470e0ac2020-01-22 16:59:21 -0500932 m.getColMajor(expt);
933 test(r, "float4x4 main(float4x4 m) { return inverse(m); }", args, expt, false);
934 }
935}
936
937DEF_TEST(SkSLInterpreterDot, r) {
938 float args[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f };
939 float expected = args[0] * args[2] +
940 args[1] * args[3];
941 test(r, "float main(float2 x, float2 y) { return dot(x, y); }", args, &expected);
942
943 expected = args[0] * args[3] +
944 args[1] * args[4] +
945 args[2] * args[5];
946 test(r, "float main(float3 x, float3 y) { return dot(x, y); }", args, &expected);
947
948 expected = args[0] * args[4] +
949 args[1] * args[5] +
950 args[2] * args[6] +
951 args[3] * args[7];
952 test(r, "float main(float4 x, float4 y) { return dot(x, y); }", args, &expected);
953}
954
Brian Osmanbe0b3b72021-01-06 14:27:35 -0500955class ExternalSqrt : public SkSL::ExternalFunction {
Ben Wagner470e0ac2020-01-22 16:59:21 -0500956public:
Brian Osmanbe0b3b72021-01-06 14:27:35 -0500957 ExternalSqrt(const char* name, SkSL::Compiler& compiler)
John Stiles54e7c052021-01-11 14:22:36 -0500958 : INHERITED(name, *compiler.context().fTypes.fFloat)
Brian Osmanbe0b3b72021-01-06 14:27:35 -0500959 , fCompiler(compiler) {}
Ben Wagner470e0ac2020-01-22 16:59:21 -0500960
Brian Osmanbe0b3b72021-01-06 14:27:35 -0500961 int callParameterCount() const override { return 1; }
Ben Wagner470e0ac2020-01-22 16:59:21 -0500962
963 void getCallParameterTypes(const SkSL::Type** outTypes) const override {
John Stiles54e7c052021-01-11 14:22:36 -0500964 outTypes[0] = fCompiler.context().fTypes.fFloat.get();
Ben Wagner470e0ac2020-01-22 16:59:21 -0500965 }
966
John Stiles534d7992020-08-18 00:06:01 -0400967 void call(int /*unusedIndex*/, float* arguments, float* outReturn) const override {
Brian Osmanbe0b3b72021-01-06 14:27:35 -0500968 outReturn[0] = sqrt(arguments[0]);
Ben Wagner470e0ac2020-01-22 16:59:21 -0500969 }
970
Brian Osmandd50b0c2021-01-11 17:04:29 -0500971 void call(skvm::Builder* b,
972 skvm::F32* arguments,
973 skvm::F32* outResult,
974 skvm::I32 mask) const override {
975 outResult[0] = sqrt(arguments[0]);
976 }
977
Ben Wagner470e0ac2020-01-22 16:59:21 -0500978private:
979 SkSL::Compiler& fCompiler;
Brian Osmanbe0b3b72021-01-06 14:27:35 -0500980 using INHERITED = SkSL::ExternalFunction;
Ben Wagner470e0ac2020-01-22 16:59:21 -0500981};
982
Brian Osmanbe0b3b72021-01-06 14:27:35 -0500983DEF_TEST(SkSLInterpreterExternalFunction, r) {
Brian Osman0006ad02020-11-18 15:38:39 -0500984 GrShaderCaps caps(GrContextOptions{});
985 SkSL::Compiler compiler(&caps);
Ben Wagner470e0ac2020-01-22 16:59:21 -0500986 SkSL::Program::Settings settings;
Brian Osmandd50b0c2021-01-11 17:04:29 -0500987 const char* src = "float main() { return external(25); }";
Brian Osmanbe0b3b72021-01-06 14:27:35 -0500988 std::vector<std::unique_ptr<SkSL::ExternalFunction>> externalFunctions;
989 externalFunctions.push_back(std::make_unique<ExternalSqrt>("external", compiler));
Brian Osman32d53552020-09-23 13:55:20 -0400990 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(
Brian Osmanbe0b3b72021-01-06 14:27:35 -0500991 SkSL::Program::kGeneric_Kind, SkSL::String(src), settings, &externalFunctions);
Ben Wagner470e0ac2020-01-22 16:59:21 -0500992 REPORTER_ASSERT(r, program);
Brian Osmandd50b0c2021-01-11 17:04:29 -0500993
994 const SkSL::FunctionDefinition* main = SkSL::Program_GetFunction(*program, "main");
995
996 skvm::Builder b;
997 SkSL::ProgramToSkVM(*program, *main, &b, /*uniforms=*/{});
998 skvm::Program p = b.done();
999
1000 float out;
1001 p.eval(1, &out);
1002 REPORTER_ASSERT(r, out == 5.0);
Ben Wagner470e0ac2020-01-22 16:59:21 -05001003}
1004
Brian Osmandd50b0c2021-01-11 17:04:29 -05001005class ExternalTable : public SkSL::ExternalFunction {
Ben Wagner470e0ac2020-01-22 16:59:21 -05001006public:
Brian Osmandd50b0c2021-01-11 17:04:29 -05001007 ExternalTable(const char* name, SkSL::Compiler& compiler, skvm::Uniforms* uniforms)
1008 : INHERITED(name, *compiler.context().fTypes.fFloat)
1009 , fCompiler(compiler)
1010 , fTable{1, 2, 4, 8} {
1011 fAddr = uniforms->pushPtr(fTable);
1012 }
Brian Osmanb08cc022020-04-02 11:38:40 -04001013
Brian Osmanbe0b3b72021-01-06 14:27:35 -05001014 int callParameterCount() const override { return 1; }
Brian Osmanb08cc022020-04-02 11:38:40 -04001015
Ben Wagner470e0ac2020-01-22 16:59:21 -05001016 void getCallParameterTypes(const SkSL::Type** outTypes) const override {
Brian Osmandd50b0c2021-01-11 17:04:29 -05001017 outTypes[0] = fCompiler.context().fTypes.fFloat.get();
Ben Wagner470e0ac2020-01-22 16:59:21 -05001018 }
Brian Osmanb08cc022020-04-02 11:38:40 -04001019
John Stiles534d7992020-08-18 00:06:01 -04001020 void call(int /*unusedIndex*/, float* arguments, float* outReturn) const override {
Brian Osmandd50b0c2021-01-11 17:04:29 -05001021 SkASSERT(false);
1022 }
1023
1024 void call(skvm::Builder* b,
1025 skvm::F32* arguments,
1026 skvm::F32* outResult,
1027 skvm::I32 mask) const override {
1028 skvm::I32 index = skvm::trunc(arguments[0] * 4);
1029 index = max(0, min(index, 3));
1030 outResult[0] = b->gatherF(fAddr, index);
Ben Wagner470e0ac2020-01-22 16:59:21 -05001031 }
Brian Osmanb08cc022020-04-02 11:38:40 -04001032
Ben Wagner470e0ac2020-01-22 16:59:21 -05001033private:
1034 SkSL::Compiler& fCompiler;
Brian Osmandd50b0c2021-01-11 17:04:29 -05001035 skvm::Uniform fAddr;
1036 float fTable[4];
Brian Osmanbe0b3b72021-01-06 14:27:35 -05001037 using INHERITED = SkSL::ExternalFunction;
Ben Wagner470e0ac2020-01-22 16:59:21 -05001038};
Brian Osmanb08cc022020-04-02 11:38:40 -04001039
Brian Osmandd50b0c2021-01-11 17:04:29 -05001040DEF_TEST(SkSLInterpreterExternalTable, r) {
Brian Osman0006ad02020-11-18 15:38:39 -05001041 GrShaderCaps caps(GrContextOptions{});
1042 SkSL::Compiler compiler(&caps);
Ben Wagner470e0ac2020-01-22 16:59:21 -05001043 SkSL::Program::Settings settings;
Brian Osman32d53552020-09-23 13:55:20 -04001044 const char* src =
Brian Osmandd50b0c2021-01-11 17:04:29 -05001045 "float4 main() { return float4(table(2), table(-1), table(0.4), table(0.6)); }";
Brian Osmanbe0b3b72021-01-06 14:27:35 -05001046 std::vector<std::unique_ptr<SkSL::ExternalFunction>> externalFunctions;
Brian Osmandd50b0c2021-01-11 17:04:29 -05001047
1048 skvm::Builder b;
1049 skvm::Uniforms u(b.uniform(), 0);
1050
1051 externalFunctions.push_back(std::make_unique<ExternalTable>("table", compiler, &u));
Brian Osman32d53552020-09-23 13:55:20 -04001052 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(
Brian Osmanbe0b3b72021-01-06 14:27:35 -05001053 SkSL::Program::kGeneric_Kind, SkSL::String(src), settings, &externalFunctions);
Ben Wagner470e0ac2020-01-22 16:59:21 -05001054 REPORTER_ASSERT(r, program);
Brian Osmandd50b0c2021-01-11 17:04:29 -05001055
1056 const SkSL::FunctionDefinition* main = SkSL::Program_GetFunction(*program, "main");
1057
1058 SkSL::ProgramToSkVM(*program, *main, &b, /*uniforms=*/{});
1059 skvm::Program p = b.done();
1060
1061 float out[4];
1062 p.eval(1, u.buf.data(), &out[0], &out[1], &out[2], &out[3]);
1063 REPORTER_ASSERT(r, out[0] == 8.0);
1064 REPORTER_ASSERT(r, out[1] == 1.0);
1065 REPORTER_ASSERT(r, out[2] == 2.0);
1066 REPORTER_ASSERT(r, out[3] == 4.0);
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001067}
Brian Osman1f71f432020-11-18 15:44:46 -05001068
1069#endif // SK_ENABLE_SKSL_INTERPRETER