blob: 1cb8a6301c1a364fda10edff9b3d56ab58aa11ef [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 Reedb26b4e72020-01-22 14:31:21 -05008#include "include/private/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"
Ethan Nicholas91164d12019-05-15 15:29:54 -040011#include "src/sksl/SkSLExternalValue.h"
Ethan Nicholasb962eff2020-01-23 16:49:41 -050012#include "src/sksl/SkSLInterpreter.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
Ben Wagner470e0ac2020-01-22 16:59:21 -050017void test(skiatest::Reporter* r, const char* src, float* in, float* expected,
Ethan Nicholasb962eff2020-01-23 16:49:41 -050018 bool exactCompare = false) {
Ethan Nicholas746035a2019-04-23 13:31:09 -040019 SkSL::Compiler compiler;
20 SkSL::Program::Settings settings;
21 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(
22 SkSL::Program::kGeneric_Kind,
23 SkSL::String(src), settings);
24 REPORTER_ASSERT(r, program);
25 if (program) {
26 std::unique_ptr<SkSL::ByteCode> byteCode = compiler.toByteCode(*program);
Brian Osman80164412019-06-07 13:00:23 -040027 program.reset();
Ethan Nicholas746035a2019-04-23 13:31:09 -040028 REPORTER_ASSERT(r, !compiler.errorCount());
29 if (compiler.errorCount() > 0) {
30 printf("%s\n%s", src, compiler.errorText().c_str());
31 return;
32 }
Brian Osman886af0d2019-07-26 15:12:56 -040033 const SkSL::ByteCodeFunction* main = byteCode->getFunction("main");
Ethan Nicholasb962eff2020-01-23 16:49:41 -050034 SkSL::Interpreter<1> interpreter(std::move(byteCode));
35 SkSL::ByteCode::Vector<1>* result;
36 bool success = interpreter.run(main, (SkSL::ByteCode::Vector<1>*) in, &result);
37 REPORTER_ASSERT(r, success);
38 for (int i = 0; i < main->getReturnSlotCount(); ++i) {
39 if (exactCompare) {
40 REPORTER_ASSERT(r, result[i].fInt[0] == ((int32_t*) expected)[i]);
41 } else {
42 REPORTER_ASSERT(r, SkScalarNearlyZero(result[i].fFloat[0] - expected[i]));
Ben Wagner470e0ac2020-01-22 16:59:21 -050043 }
Ethan Nicholas5f409862020-01-22 14:14:25 -050044 }
Ethan Nicholas746035a2019-04-23 13:31:09 -040045 } else {
46 printf("%s\n%s", src, compiler.errorText().c_str());
47 }
48}
49
Brian Osman569f12f2019-06-13 11:23:57 -040050void vec_test(skiatest::Reporter* r, const char* src) {
Brian Osman08a84962019-06-14 10:17:16 -040051 SkSL::Compiler compiler;
52 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(
53 SkSL::Program::kGeneric_Kind, SkSL::String(src), SkSL::Program::Settings());
54 if (!program) {
55 REPORT_FAILURE(r, "!program", SkString(compiler.errorText().c_str()));
56 return;
57 }
58
59 std::unique_ptr<SkSL::ByteCode> byteCode = compiler.toByteCode(*program);
60 if (compiler.errorCount() > 0) {
61 REPORT_FAILURE(r, "!toByteCode", SkString(compiler.errorText().c_str()));
62 return;
63 }
64
Ethan Nicholasb962eff2020-01-23 16:49:41 -050065 const SkSL::ByteCodeFunction* main1 = byteCode->getFunction("main");
66 SkSL::Interpreter<1> interpreter1(std::move(byteCode));
Brian Osman08a84962019-06-14 10:17:16 -040067
Brian Osmanb23d66e2019-09-27 10:25:57 -040068 // Test on four different vectors (with varying orderings to get divergent control flow)
69 const float input[16] = { 1, 2, 3, 4,
70 4, 3, 2, 1,
71 7, 5, 8, 6,
72 6, 8, 5, 7 };
73
Brian Osman569f12f2019-06-13 11:23:57 -040074 float out_s[16], out_v[16];
75 memcpy(out_s, input, sizeof(out_s));
76 memcpy(out_v, input, sizeof(out_v));
77
Brian Osman08a84962019-06-14 10:17:16 -040078 // First run in scalar mode to determine the expected output
79 for (int i = 0; i < 4; ++i) {
Ethan Nicholasb962eff2020-01-23 16:49:41 -050080 SkAssertResult(interpreter1.run(main1, (SkSL::ByteCode::Vector<1>*) (out_s + i * 4),
81 nullptr));
Brian Osman08a84962019-06-14 10:17:16 -040082 }
Brian Osman569f12f2019-06-13 11:23:57 -040083
Ethan Nicholasb962eff2020-01-23 16:49:41 -050084 byteCode = compiler.toByteCode(*program);
85 SkASSERT(compiler.errorCount() == 0);
86
87 const SkSL::ByteCodeFunction* main4 = byteCode->getFunction("main");
88 SkSL::Interpreter<4> interpreter4(std::move(byteCode));
89
Brian Osmanb23d66e2019-09-27 10:25:57 -040090 // Need to transpose input vectors for striped execution
91 auto transpose = [](float* v) {
92 for (int r = 0; r < 4; ++r)
93 for (int c = 0; c < r; ++c)
94 std::swap(v[r*4 + c], v[c*4 + r]);
95 };
96
97 // Need to transpose input vectors for striped execution
98 transpose(out_v);
99 float* args[] = { out_v, out_v + 4, out_v + 8, out_v + 12 };
100
Brian Osman08a84962019-06-14 10:17:16 -0400101 // Now run in parallel and compare results
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500102 SkAssertResult(interpreter4.runStriped(main4, 4, (float**) args));
Brian Osmanb23d66e2019-09-27 10:25:57 -0400103
104 // Transpose striped outputs back
105 transpose(out_v);
106
Brian Osman08a84962019-06-14 10:17:16 -0400107 if (memcmp(out_s, out_v, sizeof(out_s)) != 0) {
108 printf("for program: %s\n", src);
109 for (int i = 0; i < 4; ++i) {
110 printf("(%g %g %g %g) -> (%g %g %g %g), expected (%g %g %g %g)\n",
111 input[4*i + 0], input[4*i + 1], input[4*i + 2], input[4*i + 3],
112 out_v[4*i + 0], out_v[4*i + 1], out_v[4*i + 2], out_v[4*i + 3],
113 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 -0400114 }
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500115 main4->disassemble();
Brian Osman08a84962019-06-14 10:17:16 -0400116 REPORT_FAILURE(r, "VecInterpreter mismatch", SkString());
Brian Osman569f12f2019-06-13 11:23:57 -0400117 }
118}
119
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400120void test(skiatest::Reporter* r, const char* src, float inR, float inG, float inB, float inA,
Brian Osman08a84962019-06-14 10:17:16 -0400121 float expectedR, float expectedG, float expectedB, float expectedA) {
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400122 SkSL::Compiler compiler;
123 SkSL::Program::Settings settings;
124 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(
Ethan Nicholas746035a2019-04-23 13:31:09 -0400125 SkSL::Program::kGeneric_Kind,
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400126 SkSL::String(src), settings);
127 REPORTER_ASSERT(r, program);
128 if (program) {
129 std::unique_ptr<SkSL::ByteCode> byteCode = compiler.toByteCode(*program);
Brian Osman80164412019-06-07 13:00:23 -0400130 program.reset();
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400131 REPORTER_ASSERT(r, !compiler.errorCount());
132 if (compiler.errorCount() > 0) {
133 printf("%s\n%s", src, compiler.errorText().c_str());
134 return;
135 }
Brian Osman569f12f2019-06-13 11:23:57 -0400136 const SkSL::ByteCodeFunction* main = byteCode->getFunction("main");
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500137 SkSL::ByteCode::Vector<1> inoutColor[4];
138 inoutColor[0].fFloat[0] = inR;
139 inoutColor[1].fFloat[0] = inG;
140 inoutColor[2].fFloat[0] = inB;
141 inoutColor[3].fFloat[0] = inA;
142 SkSL::Interpreter<1> interpreter(std::move(byteCode));
143 bool success = interpreter.run(main, inoutColor, nullptr);
144 REPORTER_ASSERT(r, success);
145 if (inoutColor[0].fFloat[0] != expectedR || inoutColor[1].fFloat[0] != expectedG ||
146 inoutColor[2].fFloat[0] != expectedB || inoutColor[3].fFloat[0] != expectedA) {
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400147 printf("for program: %s\n", src);
148 printf(" expected (%f, %f, %f, %f), but received (%f, %f, %f, %f)\n", expectedR,
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500149 expectedG, expectedB, expectedA, inoutColor[0].fFloat[0],
150 inoutColor[1].fFloat[0], inoutColor[2].fFloat[0], inoutColor[3].fFloat[0]);
Brian Osman08a84962019-06-14 10:17:16 -0400151 main->disassemble();
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400152 }
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500153 REPORTER_ASSERT(r, inoutColor[0].fFloat[0] == expectedR);
154 REPORTER_ASSERT(r, inoutColor[1].fFloat[0] == expectedG);
155 REPORTER_ASSERT(r, inoutColor[2].fFloat[0] == expectedB);
156 REPORTER_ASSERT(r, inoutColor[3].fFloat[0] == expectedA);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400157 } else {
158 printf("%s\n%s", src, compiler.errorText().c_str());
159 }
Brian Osman569f12f2019-06-13 11:23:57 -0400160
161 // Do additional testing of 4x1 vs 1x4 to stress divergent control flow, etc.
162 vec_test(r, src);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400163}
Ben Wagner470e0ac2020-01-22 16:59:21 -0500164
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400165DEF_TEST(SkSLInterpreterAdd, r) {
166 test(r, "void main(inout half4 color) { color.r = color.r + color.g; }", 0.25, 0.75, 0, 0, 1,
167 0.75, 0, 0);
168 test(r, "void main(inout half4 color) { color += half4(1, 2, 3, 4); }", 4, 3, 2, 1, 5, 5, 5, 5);
169 test(r, "void main(inout half4 color) { half4 c = color; color += c; }", 0.25, 0.5, 0.75, 1,
170 0.5, 1, 1.5, 2);
Ethan Nicholas6f624122019-09-24 13:07:06 -0400171 test(r, "void main(inout half4 color) { color.r = int(color.r) + int(color.g); }", 1, 3, 0, 0,
172 4, 3, 0, 0);
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500173 test(r, "void main(inout half4 color) { color.rg = color.r + color.gb; }", 1, 2, 3, 4,
174 3, 4, 3, 4);
175 test(r, "void main(inout half4 color) { color.rg = color.rg + color.b; }", 1, 2, 3, 4,
176 4, 5, 3, 4);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400177}
178
179DEF_TEST(SkSLInterpreterSubtract, r) {
180 test(r, "void main(inout half4 color) { color.r = color.r - color.g; }", 1, 0.75, 0, 0, 0.25,
181 0.75, 0, 0);
182 test(r, "void main(inout half4 color) { color -= half4(1, 2, 3, 4); }", 5, 5, 5, 5, 4, 3, 2, 1);
183 test(r, "void main(inout half4 color) { half4 c = color; color -= c; }", 4, 3, 2, 1,
184 0, 0, 0, 0);
Ethan Nicholas354ecf32019-05-07 16:13:02 -0400185 test(r, "void main(inout half4 color) { color.x = -color.x; }", 4, 3, 2, 1, -4, 3, 2, 1);
186 test(r, "void main(inout half4 color) { color = -color; }", 4, 3, 2, 1, -4, -3, -2, -1);
Ethan Nicholas6f624122019-09-24 13:07:06 -0400187 test(r, "void main(inout half4 color) { color.r = int(color.r) - int(color.g); }", 3, 1, 0, 0,
188 2, 1, 0, 0);
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500189 test(r, "void main(inout half4 color) { color.rg = color.r - color.gb; }", 1, 2, 3, 4,
190 -1, -2, 3, 4);
191 test(r, "void main(inout half4 color) { color.rg = color.rg - color.b; }", 1, 2, 3, 4,
192 -2, -1, 3, 4);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400193}
194
195DEF_TEST(SkSLInterpreterMultiply, r) {
196 test(r, "void main(inout half4 color) { color.r = color.r * color.g; }", 2, 3, 0, 0, 6, 3, 0,
197 0);
198 test(r, "void main(inout half4 color) { color *= half4(1, 2, 3, 4); }", 2, 3, 4, 5, 2, 6, 12,
199 20);
200 test(r, "void main(inout half4 color) { half4 c = color; color *= c; }", 4, 3, 2, 1,
201 16, 9, 4, 1);
Ethan Nicholas6f624122019-09-24 13:07:06 -0400202 test(r, "void main(inout half4 color) { color.r = int(color.r) * int(color.g); }", 3, -2, 0, 0,
203 -6, -2, 0, 0);
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500204 test(r, "void main(inout half4 color) { color.rg = color.r * color.gb; }", 5, 2, 3, 4,
205 10, 15, 3, 4);
206 test(r, "void main(inout half4 color) { color.rg = color.rg * color.b; }", 1, 2, 3, 4,
207 3, 6, 3, 4);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400208}
209
210DEF_TEST(SkSLInterpreterDivide, r) {
211 test(r, "void main(inout half4 color) { color.r = color.r / color.g; }", 1, 2, 0, 0, 0.5, 2, 0,
212 0);
213 test(r, "void main(inout half4 color) { color /= half4(1, 2, 3, 4); }", 12, 12, 12, 12, 12, 6,
214 4, 3);
215 test(r, "void main(inout half4 color) { half4 c = color; color /= c; }", 4, 3, 2, 1,
216 1, 1, 1, 1);
Ethan Nicholas6f624122019-09-24 13:07:06 -0400217 test(r, "void main(inout half4 color) { color.r = int(color.r) / int(color.g); }", 8, -2, 0, 0,
218 -4, -2, 0, 0);
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500219 test(r, "void main(inout half4 color) { color.rg = color.r / color.gb; }", 12, 2, 3, 4,
220 6, 4, 3, 4);
221 test(r, "void main(inout half4 color) { color.rg = color.rg / color.b; }", 6, 3, 3, 4,
222 2, 1, 3, 4);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400223}
224
225DEF_TEST(SkSLInterpreterRemainder, r) {
Brian Osman3b41baf2019-05-08 09:24:46 -0400226 test(r, "void main(inout half4 color) { color.r = color.r % color.g; }", 3.125, 2, 0, 0,
227 1.125, 2, 0, 0);
228 test(r, "void main(inout half4 color) { color %= half4(1, 2, 3, 4); }", 9.5, 9.5, 9.5, 9.5,
229 0.5, 1.5, 0.5, 1.5);
Ethan Nicholas6f624122019-09-24 13:07:06 -0400230 test(r, "void main(inout half4 color) { color.r = int(color.r) % int(color.g); }", 8, 3, 0, 0,
231 2, 3, 0, 0);
232 test(r, "void main(inout half4 color) { color.rg = half2(int2(int(color.r), int(color.g)) % "
233 "int(color.b)); }", 8, 10, 6, 0, 2, 4, 6, 0);
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500234 test(r, "void main(inout half4 color) { color.rg = color.r + color.gb; }", 1, 2, 3, 4,
235 3, 4, 3, 4);
236 test(r, "void main(inout half4 color) { color.rg = color.rg + color.b; }", 1, 2, 3, 4,
237 4, 5, 3, 4);
238 test(r, "void main(inout half4 color) { color.rg = color.r % color.gb; }", 10, 2, 3, 4,
239 0, 1, 3, 4);
240 test(r, "void main(inout half4 color) { color.rg = color.rg % color.b; }", 6, 3, 4, 4,
241 2, 3, 4, 4);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400242}
243
Ethan Nicholasd166d2e2019-09-23 11:43:45 -0400244DEF_TEST(SkSLInterpreterAnd, r) {
245 test(r, "void main(inout half4 color) { if (color.r > color.g && color.g > color.b) "
246 "color = half4(color.a); }", 2, 1, 0, 3, 3, 3, 3, 3);
247 test(r, "void main(inout half4 color) { if (color.r > color.g && color.g > color.b) "
248 "color = half4(color.a); }", 1, 1, 0, 3, 1, 1, 0, 3);
249 test(r, "void main(inout half4 color) { if (color.r > color.g && color.g > color.b) "
250 "color = half4(color.a); }", 2, 1, 1, 3, 2, 1, 1, 3);
251 test(r, "int global; bool update() { global = 123; return true; }"
252 "void main(inout half4 color) { global = 0; if (color.r > color.g && update()) "
253 "color = half4(color.a); color.a = global; }", 2, 1, 1, 3, 3, 3, 3, 123);
254 test(r, "int global; bool update() { global = 123; return true; }"
255 "void main(inout half4 color) { global = 0; if (color.r > color.g && update()) "
256 "color = half4(color.a); color.a = global; }", 1, 1, 1, 3, 1, 1, 1, 0);
257}
258
259DEF_TEST(SkSLInterpreterOr, r) {
260 test(r, "void main(inout half4 color) { if (color.r > color.g || color.g > color.b) "
261 "color = half4(color.a); }", 2, 1, 0, 3, 3, 3, 3, 3);
262 test(r, "void main(inout half4 color) { if (color.r > color.g || color.g > color.b) "
263 "color = half4(color.a); }", 1, 1, 0, 3, 3, 3, 3, 3);
264 test(r, "void main(inout half4 color) { if (color.r > color.g || color.g > color.b) "
265 "color = half4(color.a); }", 1, 1, 1, 3, 1, 1, 1, 3);
266 test(r, "int global; bool update() { global = 123; return true; }"
267 "void main(inout half4 color) { global = 0; if (color.r > color.g || update()) "
268 "color = half4(color.a); color.a = global; }", 1, 1, 1, 3, 3, 3, 3, 123);
269 test(r, "int global; bool update() { global = 123; return true; }"
270 "void main(inout half4 color) { global = 0; if (color.r > color.g || update()) "
271 "color = half4(color.a); color.a = global; }", 2, 1, 1, 3, 3, 3, 3, 0);
272}
273
Brian Osmane5bbce22019-09-23 12:38:40 -0400274DEF_TEST(SkSLInterpreterBitwise, r) {
275 test(r, "void main(inout half4 color) { color.r = half(int(color.r) | 3); }",
276 5, 0, 0, 0, 7, 0, 0, 0);
277 test(r, "void main(inout half4 color) { color.r = half(int(color.r) & 3); }",
278 6, 0, 0, 0, 2, 0, 0, 0);
279 test(r, "void main(inout half4 color) { color.r = half(int(color.r) ^ 3); }",
280 5, 0, 0, 0, 6, 0, 0, 0);
281 test(r, "void main(inout half4 color) { color.r = half(~int(color.r) & 3); }",
282 6, 0, 0, 0, 1, 0, 0, 0);
Brian Osman4c2146f2019-09-24 09:39:38 -0400283
Brian Osman73fb39c2019-09-24 16:22:55 -0400284 test(r, "void main(inout half4 color) { color.r = half(uint(color.r) | 3); }",
285 5, 0, 0, 0, 7, 0, 0, 0);
286 test(r, "void main(inout half4 color) { color.r = half(uint(color.r) & 3); }",
287 6, 0, 0, 0, 2, 0, 0, 0);
288 test(r, "void main(inout half4 color) { color.r = half(uint(color.r) ^ 3); }",
289 5, 0, 0, 0, 6, 0, 0, 0);
290 test(r, "void main(inout half4 color) { color.r = half(~uint(color.r) & 3); }",
291 6, 0, 0, 0, 1, 0, 0, 0);
292
Brian Osman4c2146f2019-09-24 09:39:38 -0400293 // Shift operators
294 unsigned in = 0x80000011;
295 unsigned out;
296
297 out = 0x00000088;
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500298 test(r, "int main(int x) { return x << 3; }", (float*)&in, (float*)&out);
Brian Osman4c2146f2019-09-24 09:39:38 -0400299
300 out = 0xF0000002;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400301 test(r, "int main(int x) { return x >> 3; }", (float*)&in, (float*)&out);
Brian Osman4c2146f2019-09-24 09:39:38 -0400302
303 out = 0x10000002;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400304 test(r, "uint main(uint x) { return x >> 3; }", (float*)&in, (float*)&out);
Brian Osmane5bbce22019-09-23 12:38:40 -0400305}
306
Brian Osman29e013d2019-05-28 17:16:03 -0400307DEF_TEST(SkSLInterpreterMatrix, r) {
Brian Osman08a84962019-06-14 10:17:16 -0400308 float in[16];
309 float expected[16];
Brian Osman29e013d2019-05-28 17:16:03 -0400310
311 // Constructing matrix from scalar produces a diagonal matrix
312 in[0] = 1.0f;
313 expected[0] = 2.0f;
314 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 -0400315 in, expected);
Brian Osman29e013d2019-05-28 17:16:03 -0400316
317 // With non-square matrix
318 test(r, "float main(float x) { float3x2 m = float3x2(x); return m[0][0] + m[1][1] + m[2][1]; }",
Brian Osmanb23d66e2019-09-27 10:25:57 -0400319 in, expected);
Brian Osman29e013d2019-05-28 17:16:03 -0400320
321 // Constructing from a different-sized matrix fills the remaining space with the identity matrix
322 test(r, "float main(float x) {"
323 "float3x2 m = float3x2(x);"
324 "float4x4 m2 = float4x4(m);"
325 "return m2[0][0] + m2[3][3]; }",
Brian Osmanb23d66e2019-09-27 10:25:57 -0400326 in, expected);
Brian Osman29e013d2019-05-28 17:16:03 -0400327
328 // Constructing a matrix from vectors or scalars fills in values in column-major order
329 in[0] = 1.0f;
330 in[1] = 2.0f;
331 in[2] = 4.0f;
332 in[3] = 8.0f;
333 expected[0] = 6.0f;
334 test(r, "float main(float4 v) { float2x2 m = float2x2(v); return m[0][1] + m[1][0]; }",
Brian Osmanb23d66e2019-09-27 10:25:57 -0400335 in, expected);
Brian Osman29e013d2019-05-28 17:16:03 -0400336
337 expected[0] = 10.0f;
338 test(r, "float main(float4 v) {"
339 "float2x2 m = float2x2(v.x, v.y, v.w, v.z);"
340 "return m[0][1] + m[1][0]; }",
Brian Osmanb23d66e2019-09-27 10:25:57 -0400341 in, expected);
Brian Osman29e013d2019-05-28 17:16:03 -0400342
Brian Osman1e855b22019-05-29 15:21:52 -0400343 // Initialize 16 values to be used as inputs to matrix tests
344 for (int i = 0; i < 16; ++i) { in[i] = (float)i; }
Brian Osman29e013d2019-05-28 17:16:03 -0400345
Brian Osman1e855b22019-05-29 15:21:52 -0400346 // M+M, M-S, S-M
347 for (int i = 0; i < 16; ++i) { expected[i] = (float)(2 * i); }
Brian Osmanb23d66e2019-09-27 10:25:57 -0400348 test(r, "float4x4 main(float4x4 m) { return m + m; }", in, expected);
Brian Osman1e855b22019-05-29 15:21:52 -0400349 for (int i = 0; i < 16; ++i) { expected[i] = (float)(i + 3); }
Brian Osmanb23d66e2019-09-27 10:25:57 -0400350 test(r, "float4x4 main(float4x4 m) { return m + 3.0; }", in, expected);
351 test(r, "float4x4 main(float4x4 m) { return 3.0 + m; }", in, expected);
Brian Osman1e855b22019-05-29 15:21:52 -0400352
353 // M-M, M-S, S-M
354 for (int i = 0; i < 8; ++i) { expected[i] = 8.0f; }
Brian Osmanb23d66e2019-09-27 10:25:57 -0400355 test(r, "float4x2 main(float4x2 m1, float4x2 m2) { return m2 - m1; }", 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);
Brian Osman1e855b22019-05-29 15:21:52 -0400358 for (int i = 0; i < 16; ++i) { expected[i] = (float)(3 - i); }
Brian Osmanb23d66e2019-09-27 10:25:57 -0400359 test(r, "float4x4 main(float4x4 m) { return 3.0 - m; }", in, expected);
Brian Osman1e855b22019-05-29 15:21:52 -0400360
361 // M*S, S*M, M/S, S/M
362 for (int i = 0; i < 16; ++i) { expected[i] = (float)(i * 3); }
Brian Osmanb23d66e2019-09-27 10:25:57 -0400363 test(r, "float4x4 main(float4x4 m) { return m * 3.0; }", in, expected);
364 test(r, "float4x4 main(float4x4 m) { return 3.0 * m; }", in, expected);
Brian Osman1e855b22019-05-29 15:21:52 -0400365 for (int i = 0; i < 16; ++i) { expected[i] = (float)(i) / 2.0f; }
Brian Osmanb23d66e2019-09-27 10:25:57 -0400366 test(r, "float4x4 main(float4x4 m) { return m / 2.0; }", in, expected);
Brian Osman5bdf5252019-05-29 17:04:54 -0400367 for (int i = 0; i < 16; ++i) { expected[i] = 1.0f / (float)(i + 1); }
Brian Osmanb23d66e2019-09-27 10:25:57 -0400368 test(r, "float4x4 main(float4x4 m) { return 1.0 / (m + 1); }", in, expected);
Brian Osman1e855b22019-05-29 15:21:52 -0400369
370#if 0
371 // Matrix negation - legal in GLSL, not in SkSL?
372 for (int i = 0; i < 16; ++i) { expected[i] = (float)(-i); }
373 test(r, "float4x4 main(float4x4 m) { return -m; }", in, 16, expected);
374#endif
375
Brian Osman909231c2019-05-29 15:34:36 -0400376 // M*V, V*M
377 for (int i = 0; i < 4; ++i) {
378 expected[i] = 12.0f*i + 13.0f*(i+4) + 14.0f*(i+8);
379 }
Brian Osmanb23d66e2019-09-27 10:25:57 -0400380 test(r, "float4 main(float3x4 m, float3 v) { return m * v; }", in, expected);
Brian Osman909231c2019-05-29 15:34:36 -0400381 for (int i = 0; i < 4; ++i) {
382 expected[i] = 12.0f*(3*i) + 13.0f*(3*i+1) + 14.0f*(3*i+2);
383 }
Brian Osmanb23d66e2019-09-27 10:25:57 -0400384 test(r, "float4 main(float4x3 m, float3 v) { return v * m; }", in, expected);
Brian Osman29e013d2019-05-28 17:16:03 -0400385
Brian Osman909231c2019-05-29 15:34:36 -0400386 // M*M
387 {
Mike Reedb26b4e72020-01-22 14:31:21 -0500388 SkM44 m;
389 m.setColMajor(in);
390 SkM44 m2;
391 float in2[16];
Brian Osman909231c2019-05-29 15:34:36 -0400392 for (int i = 0; i < 16; ++i) {
Mike Reedb26b4e72020-01-22 14:31:21 -0500393 in2[i] = (i + 4) % 16;
Brian Osman909231c2019-05-29 15:34:36 -0400394 }
Mike Reedb26b4e72020-01-22 14:31:21 -0500395 m2.setColMajor(in2);
Brian Osman909231c2019-05-29 15:34:36 -0400396 m.setConcat(m, m2);
397 // Rearrange the columns on the RHS so we detect left-hand/right-hand errors
398 test(r, "float4x4 main(float4x4 m) { return m * float4x4(m[1], m[2], m[3], m[0]); }",
Brian Osmanb23d66e2019-09-27 10:25:57 -0400399 in, (float*)&m);
Brian Osman909231c2019-05-29 15:34:36 -0400400 }
Brian Osman29e013d2019-05-28 17:16:03 -0400401}
402
Brian Osman4e93feb2019-05-16 15:38:00 -0400403DEF_TEST(SkSLInterpreterTernary, r) {
404 test(r, "void main(inout half4 color) { color.r = color.g > color.b ? color.g : color.b; }",
405 0, 1, 2, 0, 2, 1, 2, 0);
406 test(r, "void main(inout half4 color) { color.r = color.g > color.b ? color.g : color.b; }",
407 0, 3, 2, 0, 3, 3, 2, 0);
408}
409
Brian Osman41672152019-05-14 13:37:30 -0400410DEF_TEST(SkSLInterpreterCast, r) {
Brian Osman08a84962019-06-14 10:17:16 -0400411 union Val {
412 float f;
413 uint32_t u;
414 int32_t s;
415 };
Brian Osman41672152019-05-14 13:37:30 -0400416
Brian Osman08a84962019-06-14 10:17:16 -0400417 Val input[2];
418 Val expected[2];
Brian Osman41672152019-05-14 13:37:30 -0400419
Brian Osman08a84962019-06-14 10:17:16 -0400420 input[0].s = 3;
421 input[1].s = -5;
422 expected[0].f = 3.0f;
423 expected[1].f = -5.0f;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400424 test(r, "float main(int x) { return float (x); }", (float*)input, (float*)expected);
425 test(r, "float2 main(int2 x) { return float2(x); }", (float*)input, (float*)expected);
Brian Osman41672152019-05-14 13:37:30 -0400426
Brian Osman08a84962019-06-14 10:17:16 -0400427 input[0].u = 3;
428 input[1].u = 5;
429 expected[0].f = 3.0f;
430 expected[1].f = 5.0f;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400431 test(r, "float main(uint x) { return float (x); }", (float*)input, (float*)expected);
432 test(r, "float2 main(uint2 x) { return float2(x); }", (float*)input, (float*)expected);
Brian Osmanc51d7912019-05-22 15:16:16 -0700433
Brian Osman08a84962019-06-14 10:17:16 -0400434 input[0].f = 3.0f;
435 input[1].f = -5.0f;
436 expected[0].s = 3;
437 expected[1].s = -5;
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500438 test(r, "int main(float x) { return int (x); }", (float*)input, (float*)expected, true);
439 test(r, "int2 main(float2 x) { return int2(x); }", (float*)input, (float*)expected, true);
Brian Osman08a84962019-06-14 10:17:16 -0400440
441 input[0].s = 3;
442 expected[0].f = 3.0f;
443 expected[1].f = 3.0f;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400444 test(r, "float2 main(int x) { return float2(x); }", (float*)input, (float*)expected);
Brian Osman41672152019-05-14 13:37:30 -0400445}
446
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400447DEF_TEST(SkSLInterpreterIf, r) {
448 test(r, "void main(inout half4 color) { if (color.r > color.g) color.a = 1; }", 5, 3, 0, 0,
449 5, 3, 0, 1);
450 test(r, "void main(inout half4 color) { if (color.r > color.g) color.a = 1; }", 5, 5, 0, 0,
451 5, 5, 0, 0);
452 test(r, "void main(inout half4 color) { if (color.r > color.g) color.a = 1; }", 5, 6, 0, 0,
453 5, 6, 0, 0);
454 test(r, "void main(inout half4 color) { if (color.r < color.g) color.a = 1; }", 3, 5, 0, 0,
455 3, 5, 0, 1);
456 test(r, "void main(inout half4 color) { if (color.r < color.g) color.a = 1; }", 5, 5, 0, 0,
457 5, 5, 0, 0);
458 test(r, "void main(inout half4 color) { if (color.r < color.g) color.a = 1; }", 6, 5, 0, 0,
459 6, 5, 0, 0);
460 test(r, "void main(inout half4 color) { if (color.r >= color.g) color.a = 1; }", 5, 3, 0, 0,
461 5, 3, 0, 1);
462 test(r, "void main(inout half4 color) { if (color.r >= color.g) color.a = 1; }", 5, 5, 0, 0,
463 5, 5, 0, 1);
464 test(r, "void main(inout half4 color) { if (color.r >= color.g) color.a = 1; }", 5, 6, 0, 0,
465 5, 6, 0, 0);
466 test(r, "void main(inout half4 color) { if (color.r <= color.g) color.a = 1; }", 3, 5, 0, 0,
467 3, 5, 0, 1);
468 test(r, "void main(inout half4 color) { if (color.r <= color.g) color.a = 1; }", 5, 5, 0, 0,
469 5, 5, 0, 1);
470 test(r, "void main(inout half4 color) { if (color.r <= color.g) color.a = 1; }", 6, 5, 0, 0,
471 6, 5, 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, 1);
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, 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);
Brian Osmane5bbce22019-09-23 12:38:40 -0400480 test(r, "void main(inout half4 color) { if (!(color.r == color.g)) color.a = 1; }", 2, 2, 0, 0,
481 2, 2, 0, 0);
482 test(r, "void main(inout half4 color) { if (!(color.r == color.g)) color.a = 1; }", 2, -2, 0, 0,
483 2, -2, 0, 1);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400484 test(r, "void main(inout half4 color) { if (color.r == color.g) color.a = 1; else "
485 "color.a = 2; }", 1, 1, 0, 0, 1, 1, 0, 1);
486 test(r, "void main(inout half4 color) { if (color.r == color.g) color.a = 1; else "
487 "color.a = 2; }", 2, -2, 0, 0, 2, -2, 0, 2);
488}
489
Brian Osman16e6fd52019-05-29 11:19:00 -0400490DEF_TEST(SkSLInterpreterIfVector, r) {
491 test(r, "void main(inout half4 color) { if (color.rg == color.ba) color.a = 1; }",
492 1, 2, 1, 2, 1, 2, 1, 1);
493 test(r, "void main(inout half4 color) { if (color.rg == color.ba) color.a = 1; }",
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500494 1, 2, 1, 3, 1, 2, 1, 3);
495 test(r, "void main(inout half4 color) { if (color.rg == color.ba) color.a = 1; }",
Brian Osman16e6fd52019-05-29 11:19:00 -0400496 1, 2, 3, 2, 1, 2, 3, 2);
497 test(r, "void main(inout half4 color) { if (color.rg != color.ba) color.a = 1; }",
498 1, 2, 1, 2, 1, 2, 1, 2);
499 test(r, "void main(inout half4 color) { if (color.rg != color.ba) color.a = 1; }",
500 1, 2, 3, 2, 1, 2, 3, 1);
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500501 test(r, "void main(inout half4 color) { if (color.rg != color.ba) color.a = 1; }",
502 1, 2, 1, 3, 1, 2, 1, 1);
Brian Osman16e6fd52019-05-29 11:19:00 -0400503}
504
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400505DEF_TEST(SkSLInterpreterWhile, r) {
Brian Osman52c1bf12019-07-18 13:12:19 -0400506 test(r, "void main(inout half4 color) { while (color.r < 8) { color.r++; } }",
507 1, 2, 3, 4, 8, 2, 3, 4);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400508 test(r, "void main(inout half4 color) { while (color.r < 1) color.r += 0.25; }", 0, 0, 0, 0, 1,
509 0, 0, 0);
Brian Osman569f12f2019-06-13 11:23:57 -0400510 test(r, "void main(inout half4 color) { while (color.r > 1) color.r -= 0.25; }", 0, 0, 0, 0, 0,
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400511 0, 0, 0);
512 test(r, "void main(inout half4 color) { while (true) { color.r += 0.5; "
Brian Osman569f12f2019-06-13 11:23:57 -0400513 "if (color.r > 5) break; } }", 0, 0, 0, 0, 5.5, 0, 0, 0);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400514 test(r, "void main(inout half4 color) { while (color.r < 10) { color.r += 0.5; "
515 "if (color.r < 5) continue; break; } }", 0, 0, 0, 0, 5, 0, 0, 0);
Brian Osman8d564572019-06-19 11:00:28 -0400516 test(r,
517 "void main(inout half4 color) {"
518 " while (true) {"
519 " if (color.r > 4) { break; }"
520 " while (true) { color.a = 1; break; }"
521 " break;"
522 " }"
523 "}",
524 6, 5, 4, 3, 6, 5, 4, 3);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400525}
526
527DEF_TEST(SkSLInterpreterDo, r) {
528 test(r, "void main(inout half4 color) { do color.r += 0.25; while (color.r < 1); }", 0, 0, 0, 0,
529 1, 0, 0, 0);
Brian Osman569f12f2019-06-13 11:23:57 -0400530 test(r, "void main(inout half4 color) { do color.r -= 0.25; while (color.r > 1); }", 0, 0, 0, 0,
531 -0.25, 0, 0, 0);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400532 test(r, "void main(inout half4 color) { do { color.r += 0.5; if (color.r > 1) break; } while "
533 "(true); }", 0, 0, 0, 0, 1.5, 0, 0, 0);
534 test(r, "void main(inout half4 color) {do { color.r += 0.5; if (color.r < 5) "
535 "continue; if (color.r >= 5) break; } while (true); }", 0, 0, 0, 0, 5, 0, 0, 0);
Brian Osman44d44762019-05-13 14:19:12 -0400536 test(r, "void main(inout half4 color) { do { color.r += 0.5; } while (false); }",
537 0, 0, 0, 0, 0.5, 0, 0, 0);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400538}
539
540DEF_TEST(SkSLInterpreterFor, r) {
541 test(r, "void main(inout half4 color) { for (int i = 1; i <= 10; ++i) color.r += i; }", 0, 0, 0,
542 0, 55, 0, 0, 0);
543 test(r,
544 "void main(inout half4 color) {"
545 " for (int i = 1; i <= 10; ++i)"
546 " for (int j = i; j <= 10; ++j)"
547 " color.r += j;"
548 "}",
549 0, 0, 0, 0,
550 385, 0, 0, 0);
551 test(r,
552 "void main(inout half4 color) {"
553 " for (int i = 1; i <= 10; ++i)"
554 " for (int j = 1; ; ++j) {"
555 " if (i == j) continue;"
556 " if (j > 10) break;"
557 " color.r += j;"
558 " }"
559 "}",
560 0, 0, 0, 0,
561 495, 0, 0, 0);
562}
563
Brian Osmanf3fa6002019-05-17 14:26:53 -0400564DEF_TEST(SkSLInterpreterPrefixPostfix, r) {
565 test(r, "void main(inout half4 color) { color.r = ++color.g; }", 1, 2, 3, 4, 3, 3, 3, 4);
566 test(r, "void main(inout half4 color) { color.r = color.g++; }", 1, 2, 3, 4, 2, 3, 3, 4);
567}
568
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400569DEF_TEST(SkSLInterpreterSwizzle, r) {
570 test(r, "void main(inout half4 color) { color = color.abgr; }", 1, 2, 3, 4, 4, 3, 2, 1);
571 test(r, "void main(inout half4 color) { color.rgb = half4(5, 6, 7, 8).bbg; }", 1, 2, 3, 4, 7, 7,
572 6, 4);
573 test(r, "void main(inout half4 color) { color.bgr = int3(5, 6, 7); }", 1, 2, 3, 4, 7, 6,
574 5, 4);
575}
576
577DEF_TEST(SkSLInterpreterGlobal, r) {
578 test(r, "int x; void main(inout half4 color) { x = 10; color.b = x; }", 1, 2, 3, 4, 1, 2, 10,
579 4);
Brian Osmanb7451292019-05-15 13:02:13 -0400580 test(r, "float4 x; void main(inout float4 color) { x = color * 2; color = x; }",
581 1, 2, 3, 4, 2, 4, 6, 8);
582 test(r, "float4 x; void main(inout float4 color) { x = float4(5, 6, 7, 8); color = x.wzyx; }",
583 1, 2, 3, 4, 8, 7, 6, 5);
Brian Osman1091f022019-05-16 09:42:16 -0400584 test(r, "float4 x; void main(inout float4 color) { x.wzyx = float4(5, 6, 7, 8); color = x; }",
585 1, 2, 3, 4, 8, 7, 6, 5);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400586}
Ethan Nicholas746035a2019-04-23 13:31:09 -0400587
588DEF_TEST(SkSLInterpreterGeneric, r) {
589 float value1 = 5;
590 float expected1 = 25;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400591 test(r, "float main(float x) { return x * x; }", &value1, &expected1);
Ethan Nicholas746035a2019-04-23 13:31:09 -0400592 float value2[2] = { 5, 25 };
593 float expected2[2] = { 25, 625 };
Brian Osmanb23d66e2019-09-27 10:25:57 -0400594 test(r, "float2 main(float x, float y) { return float2(x * x, y * y); }", value2, expected2);
Ethan Nicholas746035a2019-04-23 13:31:09 -0400595}
Brian Osmand369a5e2019-05-09 13:13:25 -0400596
Brian Osman07c117b2019-05-23 12:51:06 -0700597DEF_TEST(SkSLInterpreterCompound, r) {
598 struct RectAndColor { SkIRect fRect; SkColor4f fColor; };
599 struct ManyRects { int fNumRects; RectAndColor fRects[4]; };
600
601 const char* src =
602 // Some struct definitions
603 "struct Point { int x; int y; };\n"
604 "struct Rect { Point p0; Point p1; };\n"
605 "struct RectAndColor { Rect r; float4 color; };\n"
606
607 // Structs as globals, parameters, return values
608 "RectAndColor temp;\n"
609 "int rect_height(Rect r) { return r.p1.y - r.p0.y; }\n"
610 "RectAndColor make_blue_rect(int w, int h) {\n"
611 " temp.r.p0.x = temp.r.p0.y = 0;\n"
612 " temp.r.p1.x = w; temp.r.p1.y = h;\n"
613 " temp.color = float4(0, 1, 0, 1);\n"
614 " return temp;\n"
615 "}\n"
616
617 // Initialization and assignment of types larger than 4 slots
618 "RectAndColor init_big(RectAndColor r) { RectAndColor s = r; return s; }\n"
619 "RectAndColor copy_big(RectAndColor r) { RectAndColor s; s = r; return s; }\n"
620
621 // Same for arrays, including some non-constant indexing
622 "float tempFloats[8];\n"
623 "int median(int a[15]) { return a[7]; }\n"
624 "float[8] sums(float a[8]) {\n"
625 " float tempFloats[8];\n"
626 " tempFloats[0] = a[0];\n"
627 " for (int i = 1; i < 8; ++i) { tempFloats[i] = tempFloats[i - 1] + a[i]; }\n"
628 " return tempFloats;\n"
629 "}\n"
630
631 // Uniforms, array-of-structs, dynamic indices
Ethan Nicholas31cff272019-09-26 13:04:48 -0400632 "uniform Rect gRects[4];\n"
Brian Osman07c117b2019-05-23 12:51:06 -0700633 "Rect get_rect(int i) { return gRects[i]; }\n"
634
635 // Kitchen sink (swizzles, inout, SoAoS)
636 "struct ManyRects { int numRects; RectAndColor rects[4]; };\n"
637 "void fill_rects(inout ManyRects mr) {\n"
638 " for (int i = 0; i < mr.numRects; ++i) {\n"
639 " mr.rects[i].r = gRects[i];\n"
640 " float b = mr.rects[i].r.p1.y;\n"
641 " mr.rects[i].color = float4(b, b, b, b);\n"
642 " }\n"
643 "}\n";
644
645 SkSL::Compiler compiler;
646 SkSL::Program::Settings settings;
647 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(
648 SkSL::Program::kGeneric_Kind,
649 SkSL::String(src), settings);
650 REPORTER_ASSERT(r, program);
651
652 std::unique_ptr<SkSL::ByteCode> byteCode = compiler.toByteCode(*program);
653 REPORTER_ASSERT(r, !compiler.errorCount());
654
655 auto rect_height = byteCode->getFunction("rect_height"),
656 make_blue_rect = byteCode->getFunction("make_blue_rect"),
657 median = byteCode->getFunction("median"),
658 sums = byteCode->getFunction("sums"),
659 get_rect = byteCode->getFunction("get_rect"),
660 fill_rects = byteCode->getFunction("fill_rects");
661
662 SkIRect gRects[4] = { { 1,2,3,4 }, { 5,6,7,8 }, { 9,10,11,12 }, { 13,14,15,16 } };
Brian Osmanb23d66e2019-09-27 10:25:57 -0400663 const float* fRects = (const float*)gRects;
Brian Osman07c117b2019-05-23 12:51:06 -0700664
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500665 SkSL::Interpreter<1> interpreter(std::move(byteCode));
666 auto geti = [](SkSL::Interpreter<1>::Vector* v) { return v->fInt[0]; };
667 auto getf = [](SkSL::Interpreter<1>::Vector* v) { return v->fFloat[0]; };
668
Brian Osman07c117b2019-05-23 12:51:06 -0700669 {
670 SkIRect in = SkIRect::MakeXYWH(10, 10, 20, 30);
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500671 SkSL::Interpreter<1>::Vector* out;
672 bool success = interpreter.run(rect_height, (SkSL::Interpreter<1>::Vector*) &in, &out);
673 REPORTER_ASSERT(r, success);
674 REPORTER_ASSERT(r, geti(out) == 30);
Brian Osman07c117b2019-05-23 12:51:06 -0700675 }
676
677 {
678 int in[2] = { 15, 25 };
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500679 SkSL::Interpreter<1>::Vector* out;
680 bool success = interpreter.run(make_blue_rect, (SkSL::Interpreter<1>::Vector*) in, &out);
681 REPORTER_ASSERT(r, success);
682 RectAndColor result{ { geti(out), geti(out + 1), geti(out + 2), geti(out + 3) },
683 { getf(out + 4), getf(out + 5), getf(out + 6), getf(out + 7) } };
684 REPORTER_ASSERT(r, result.fRect.width() == 15);
685 REPORTER_ASSERT(r, result.fRect.height() == 25);
Brian Osman07c117b2019-05-23 12:51:06 -0700686 SkColor4f blue = { 0.0f, 1.0f, 0.0f, 1.0f };
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500687 REPORTER_ASSERT(r, result.fColor == blue);
Brian Osman07c117b2019-05-23 12:51:06 -0700688 }
689
690 {
691 int in[15] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500692 SkSL::Interpreter<1>::Vector* out;
693 bool success = interpreter.run(median, (SkSL::Interpreter<1>::Vector*) in, &out);
694 REPORTER_ASSERT(r, success);
695 REPORTER_ASSERT(r, geti(out) == 8);
Brian Osman07c117b2019-05-23 12:51:06 -0700696 }
697
698 {
699 float in[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500700 SkSL::Interpreter<1>::Vector* out;
701 bool success = interpreter.run(sums, (SkSL::Interpreter<1>::Vector*) in, &out);
702 REPORTER_ASSERT(r, success);
Brian Osman07c117b2019-05-23 12:51:06 -0700703 for (int i = 0; i < 8; ++i) {
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500704 REPORTER_ASSERT(r, getf(out + i) == static_cast<float>((i + 1) * (i + 2) / 2));
Brian Osman07c117b2019-05-23 12:51:06 -0700705 }
706 }
707
708 {
709 int in = 2;
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500710 interpreter.setUniforms(fRects);
711 SkSL::Interpreter<1>::Vector* out;
712 bool success = interpreter.run(get_rect, (SkSL::Interpreter<1>::Vector*) &in, &out);
713 REPORTER_ASSERT(r, success);
714 REPORTER_ASSERT(r, geti(out) == gRects[2].fLeft);
715 REPORTER_ASSERT(r, geti(out + 1) == gRects[2].fTop);
716 REPORTER_ASSERT(r, geti(out + 2) == gRects[2].fRight);
717 REPORTER_ASSERT(r, geti(out + 3) == gRects[2].fBottom);
Brian Osman07c117b2019-05-23 12:51:06 -0700718 }
719
720 {
721 ManyRects in;
722 memset(&in, 0, sizeof(in));
723 in.fNumRects = 2;
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500724 bool success = interpreter.run(fill_rects, (SkSL::Interpreter<1>::Vector*) &in, nullptr);
725 REPORTER_ASSERT(r, success);
Brian Osman07c117b2019-05-23 12:51:06 -0700726 ManyRects expected;
727 memset(&expected, 0, sizeof(expected));
728 expected.fNumRects = 2;
729 for (int i = 0; i < 2; ++i) {
730 expected.fRects[i].fRect = gRects[i];
731 float c = gRects[i].fBottom;
732 expected.fRects[i].fColor = { c, c, c, c };
733 }
734 REPORTER_ASSERT(r, memcmp(&in, &expected, sizeof(in)) == 0);
735 }
736}
Ethan Nicholas91164d12019-05-15 15:29:54 -0400737
Brian Osman869a3e82019-07-18 17:00:34 -0400738static void expect_failure(skiatest::Reporter* r, const char* src) {
739 SkSL::Compiler compiler;
740 auto program = compiler.convertProgram(SkSL::Program::kGeneric_Kind, SkSL::String(src),
741 SkSL::Program::Settings());
742 REPORTER_ASSERT(r, program);
743
744 auto byteCode = compiler.toByteCode(*program);
745 REPORTER_ASSERT(r, compiler.errorCount() > 0);
746 REPORTER_ASSERT(r, !byteCode);
747}
748
749static void expect_run_failure(skiatest::Reporter* r, const char* src, float* in) {
750 SkSL::Compiler compiler;
751 auto program = compiler.convertProgram(SkSL::Program::kGeneric_Kind, SkSL::String(src),
752 SkSL::Program::Settings());
753 REPORTER_ASSERT(r, program);
754
755 auto byteCode = compiler.toByteCode(*program);
756 REPORTER_ASSERT(r, byteCode);
757
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500758 auto main = byteCode->getFunction("main");
759 SkSL::Interpreter<1> interpreter(std::move(byteCode));
760 SkSL::ByteCode::Vector<1>* result;
761 bool success = interpreter.run(main, (SkSL::ByteCode::Vector<1>*) in, &result);
762 REPORTER_ASSERT(r, !success);
Brian Osman869a3e82019-07-18 17:00:34 -0400763}
764
Brian Osman6f5358f2019-07-09 14:17:23 -0400765DEF_TEST(SkSLInterpreterRestrictFunctionCalls, r) {
Brian Osman6f5358f2019-07-09 14:17:23 -0400766 // Ensure that simple recursion is not allowed
Brian Osman869a3e82019-07-18 17:00:34 -0400767 expect_failure(r, "float main() { return main() + 1; }");
Brian Osman6f5358f2019-07-09 14:17:23 -0400768
769 // Ensure that calls to undefined functions are not allowed (to prevent mutual recursion)
Brian Osman869a3e82019-07-18 17:00:34 -0400770 expect_failure(r, "float foo(); float bar() { return foo(); } float foo() { return bar(); }");
Brian Osman4a47da72019-07-12 11:30:32 -0400771
772 // returns are not allowed inside conditionals (or loops, which are effectively the same thing)
Brian Osman869a3e82019-07-18 17:00:34 -0400773 expect_failure(r, "float main(float x, float y) { if (x < y) { return x; } return y; }");
774 expect_failure(r, "float main(float x) { while (x > 1) { return x; } return 0; }");
775}
776
777DEF_TEST(SkSLInterpreterArrayBounds, r) {
778 // Out of bounds array access at compile time
779 expect_failure(r, "float main(float x[4]) { return x[-1]; }");
780 expect_failure(r, "float2 main(float2 x[2]) { return x[2]; }");
781
782 // Out of bounds array access at runtime is pinned, and we don't update any inout data
783 float in[3] = { -1.0f, 1.0f, 2.0f };
784 expect_run_failure(r, "void main(inout float data[3]) { data[int(data[0])] = 0; }", in);
785 REPORTER_ASSERT(r, in[0] == -1.0f && in[1] == 1.0f && in[2] == 2.0f);
786
787 in[0] = 3.0f;
788 expect_run_failure(r, "void main(inout float data[3]) { data[int(data[0])] = 0; }", in);
789 REPORTER_ASSERT(r, in[0] == 3.0f && in[1] == 1.0f && in[2] == 2.0f);
Brian Osman6f5358f2019-07-09 14:17:23 -0400790}
791
Brian Osman226668a2019-05-14 16:47:30 -0400792DEF_TEST(SkSLInterpreterFunctions, r) {
793 const char* src =
794 "float sqr(float x) { return x * x; }\n"
Brian Osman226668a2019-05-14 16:47:30 -0400795 "float sub(float x, float y) { return x - y; }\n"
Brian Osman6f5358f2019-07-09 14:17:23 -0400796 "float main(float x) { return sub(sqr(x), x); }\n"
Brian Osman226668a2019-05-14 16:47:30 -0400797
798 // Different signatures
799 "float dot(float2 a, float2 b) { return a.x*b.x + a.y*b.y; }\n"
800 "float dot(float3 a, float3 b) { return a.x*b.x + a.y*b.y + a.z*b.z; }\n"
801 "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 -0400802 "float dot2_test(float x) { return dot(float2(x, x + 1), float2(1, -1)); }\n";
Brian Osman226668a2019-05-14 16:47:30 -0400803
804 SkSL::Compiler compiler;
805 SkSL::Program::Settings settings;
806 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(
807 SkSL::Program::kGeneric_Kind,
808 SkSL::String(src), settings);
809 REPORTER_ASSERT(r, program);
810
811 std::unique_ptr<SkSL::ByteCode> byteCode = compiler.toByteCode(*program);
812 REPORTER_ASSERT(r, !compiler.errorCount());
813
814 auto sub = byteCode->getFunction("sub");
815 auto sqr = byteCode->getFunction("sqr");
816 auto main = byteCode->getFunction("main");
817 auto tan = byteCode->getFunction("tan");
818 auto dot3 = byteCode->getFunction("dot3_test");
819 auto dot2 = byteCode->getFunction("dot2_test");
Brian Osman226668a2019-05-14 16:47:30 -0400820
821 REPORTER_ASSERT(r, sub);
822 REPORTER_ASSERT(r, sqr);
823 REPORTER_ASSERT(r, main);
824 REPORTER_ASSERT(r, !tan);
825 REPORTER_ASSERT(r, dot3);
826 REPORTER_ASSERT(r, dot2);
Brian Osman226668a2019-05-14 16:47:30 -0400827
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500828 SkSL::Interpreter<1> interpreter(std::move(byteCode));
Brian Osman226668a2019-05-14 16:47:30 -0400829 float in = 3.0f;
Brian Osman226668a2019-05-14 16:47:30 -0400830
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500831 SkSL::Interpreter<1>::Vector* out;
832 bool success = interpreter.run(main, (SkSL::Interpreter<1>::Vector*) &in, &out);
833 REPORTER_ASSERT(r, success);
834 REPORTER_ASSERT(r, out->fFloat[0] = 6.0f);
Brian Osman226668a2019-05-14 16:47:30 -0400835
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500836 success = interpreter.run(dot3, (SkSL::Interpreter<1>::Vector*) &in, &out);
837 REPORTER_ASSERT(r, success);
838 REPORTER_ASSERT(r, out->fFloat[0] = 9.0f);
839
840 success = interpreter.run(dot2, (SkSL::Interpreter<1>::Vector*) &in, &out);
841 REPORTER_ASSERT(r, success);
842 REPORTER_ASSERT(r, out->fFloat[0] = -1.0f);
Brian Osman226668a2019-05-14 16:47:30 -0400843}
Ethan Nicholas91164d12019-05-15 15:29:54 -0400844
Brian Osmand3494ed2019-06-20 15:41:34 -0400845DEF_TEST(SkSLInterpreterOutParams, r) {
846 test(r,
847 "void oneAlpha(inout half4 color) { color.a = 1; }"
848 "void main(inout half4 color) { oneAlpha(color); }",
849 0, 0, 0, 0, 0, 0, 0, 1);
850 test(r,
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500851 "half2 tricky(half x, half y, inout half2 color, half z, out half w) {"
Brian Osmand3494ed2019-06-20 15:41:34 -0400852 " color.xy = color.yx;"
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500853 " w = 47;"
Brian Osmand3494ed2019-06-20 15:41:34 -0400854 " return half2(x + y, z);"
855 "}"
856 "void main(inout half4 color) {"
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500857 " half w;"
858 " half2 t = tricky(1, 2, color.rb, 5, w);"
859 " color.r += w;"
Brian Osmand3494ed2019-06-20 15:41:34 -0400860 " color.ga = t;"
861 "}",
Ethan Nicholasb962eff2020-01-23 16:49:41 -0500862 1, 2, 3, 4, 50, 3, 1, 5);
Brian Osmand3494ed2019-06-20 15:41:34 -0400863}
864
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400865DEF_TEST(SkSLInterpreterMathFunctions, r) {
Brian Osmanb380e712019-07-24 17:02:39 -0400866 float value[4], expected[4];
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400867
Brian Osmanb380e712019-07-24 17:02:39 -0400868 value[0] = 0.0f; expected[0] = 0.0f;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400869 test(r, "float main(float x) { return sin(x); }", value, expected);
870 test(r, "float main(float x) { return tan(x); }", value, expected);
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400871
Brian Osmanb380e712019-07-24 17:02:39 -0400872 value[0] = 0.0f; expected[0] = 1.0f;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400873 test(r, "float main(float x) { return cos(x); }", value, expected);
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400874
Brian Osmanb380e712019-07-24 17:02:39 -0400875 value[0] = 25.0f; expected[0] = 5.0f;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400876 test(r, "float main(float x) { return sqrt(x); }", value, expected);
Brian Osmanb380e712019-07-24 17:02:39 -0400877
878 value[0] = 90.0f; expected[0] = sk_float_degrees_to_radians(value[0]);
Brian Osmanb23d66e2019-09-27 10:25:57 -0400879 test(r, "float main(float x) { return radians(x); }", value, expected);
Brian Osmanb380e712019-07-24 17:02:39 -0400880
881 value[0] = 1.0f; value[1] = -1.0f;
882 expected[0] = 1.0f / SK_FloatSqrt2; expected[1] = -1.0f / SK_FloatSqrt2;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400883 test(r, "float2 main(float2 x) { return normalize(x); }", value, expected);
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400884}
885
Brian Osmanfba386b2019-06-20 14:54:15 -0400886DEF_TEST(SkSLInterpreterVoidFunction, r) {
887 test(r,
888 "half x; void foo() { x = 1.0; }"
889 "void main(inout half4 color) { foo(); color.r = x; }",
890 0, 0, 0, 0, 1, 0, 0, 0);
891}
892
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400893DEF_TEST(SkSLInterpreterMix, r) {
Brian Osman08a84962019-06-14 10:17:16 -0400894 float value, expected;
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400895
896 value = 0.5f; expected = 0.0f;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400897 test(r, "float main(float x) { return mix(-10, 10, x); }", &value, &expected);
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400898 value = 0.75f; expected = 5.0f;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400899 test(r, "float main(float x) { return mix(-10, 10, x); }", &value, &expected);
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400900 value = 2.0f; expected = 30.0f;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400901 test(r, "float main(float x) { return mix(-10, 10, x); }", &value, &expected);
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400902
Brian Osman08a84962019-06-14 10:17:16 -0400903 float valueVectors[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f },
904 expectedVector[] = { 3.0f, 4.0f, 5.0f, 6.0f };
Brian Osmanb23d66e2019-09-27 10:25:57 -0400905 test(r, "float4 main(float4 x, float4 y) { return mix(x, y, 0.5); }", valueVectors,
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400906 expectedVector);
907}
908
909DEF_TEST(SkSLInterpreterCross, r) {
Brian Osman08a84962019-06-14 10:17:16 -0400910 float args[] = { 1.0f, 4.0f, -6.0f, -2.0f, 7.0f, -3.0f };
Mike Reedb26b4e72020-01-22 14:31:21 -0500911 SkV3 cross = SkV3::Cross({args[0], args[1], args[2]},
912 {args[3], args[4], args[5]});
913 float expected[] = { cross.x, cross.y, cross.z };
Brian Osmanb23d66e2019-09-27 10:25:57 -0400914 test(r, "float3 main(float3 x, float3 y) { return cross(x, y); }", args, expected);
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400915}
Ben Wagner470e0ac2020-01-22 16:59:21 -0500916
Mike Reed634c9412019-07-18 13:20:04 -0400917DEF_TEST(SkSLInterpreterInverse, r) {
918 {
919 SkMatrix m;
920 m.setRotate(30).postScale(1, 2);
921 float args[4] = { m[0], m[3], m[1], m[4] };
922 SkAssertResult(m.invert(&m));
923 float expt[4] = { m[0], m[3], m[1], m[4] };
Ben Wagner470e0ac2020-01-22 16:59:21 -0500924 test(r, "float2x2 main(float2x2 m) { return inverse(m); }", args, expt, false);
Mike Reed634c9412019-07-18 13:20:04 -0400925 }
926 {
927 SkMatrix m;
928 m.setRotate(30).postScale(1, 2).postTranslate(1, 2);
929 float args[9] = { m[0], m[3], m[6], m[1], m[4], m[7], m[2], m[5], m[8] };
930 SkAssertResult(m.invert(&m));
931 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 -0500932 test(r, "float3x3 main(float3x3 m) { return inverse(m); }", args, expt, false);
Mike Reed634c9412019-07-18 13:20:04 -0400933 }
934 {
935 float args[16], expt[16];
Mike Reed634c9412019-07-18 13:20:04 -0400936 // just some crazy thing that is invertible
Mike Reedb26b4e72020-01-22 14:31:21 -0500937 SkM44 m = {1, 2, 3, 4, 1, 2, 0, 3, 1, 0, 1, 4, 1, 3, 2, 0};
938 m.getColMajor(args);
Mike Reed634c9412019-07-18 13:20:04 -0400939 SkAssertResult(m.invert(&m));
Ben Wagner470e0ac2020-01-22 16:59:21 -0500940 m.getColMajor(expt);
941 test(r, "float4x4 main(float4x4 m) { return inverse(m); }", args, expt, false);
942 }
943}
944
945DEF_TEST(SkSLInterpreterDot, r) {
946 float args[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f };
947 float expected = args[0] * args[2] +
948 args[1] * args[3];
949 test(r, "float main(float2 x, float2 y) { return dot(x, y); }", args, &expected);
950
951 expected = args[0] * args[3] +
952 args[1] * args[4] +
953 args[2] * args[5];
954 test(r, "float main(float3 x, float3 y) { return dot(x, y); }", args, &expected);
955
956 expected = args[0] * args[4] +
957 args[1] * args[5] +
958 args[2] * args[6] +
959 args[3] * args[7];
960 test(r, "float main(float4 x, float4 y) { return dot(x, y); }", args, &expected);
961}
962
963static const SkSL::Type& type_of(const skjson::Value* value, SkSL::Compiler* compiler) {
964 switch (value->getType()) {
965 case skjson::Value::Type::kNumber: {
966 float f = *value->as<skjson::NumberValue>();
967 if (f == (float) (int) f) {
968 return *compiler->context().fInt_Type;
969 }
970 return *compiler->context().fFloat_Type;
971 }
972 case skjson::Value::Type::kBool:
973 return *compiler->context().fBool_Type;
974 default:
975 return *compiler->context().fVoid_Type;
976 }
977}
978
979class JSONExternalValue : public SkSL::ExternalValue {
980public:
981 JSONExternalValue(const char* name, const skjson::Value* value, SkSL::Compiler* compiler)
982 : INHERITED(name, type_of(value, compiler))
983 , fValue(*value)
984 , fCompiler(*compiler) {}
985
986 bool canRead() const override {
987 return type() != *fCompiler.context().fVoid_Type;
988 }
989
990 void read(int /*unusedIndex*/, float* target) override {
991 if (type() == *fCompiler.context().fInt_Type) {
992 *(int*) target = *fValue.as<skjson::NumberValue>();
993 } else if (type() == *fCompiler.context().fFloat_Type) {
994 *(float*) target = *fValue.as<skjson::NumberValue>();
995 } else if (type() == *fCompiler.context().fBool_Type) {
996 // ByteCode "booleans" are actually bit-masks
997 *(int*) target = *fValue.as<skjson::BoolValue>() ? ~0 : 0;
998 } else {
999 SkASSERT(false);
1000 }
1001 }
1002
1003 SkSL::ExternalValue* getChild(const char* name) const override {
1004 if (fValue.getType() == skjson::Value::Type::kObject) {
1005 const skjson::Value& v = fValue.as<skjson::ObjectValue>()[name];
1006 return (SkSL::ExternalValue*) fCompiler.takeOwnership(std::unique_ptr<Symbol>(
1007 new JSONExternalValue(name, &v, &fCompiler)));
1008 }
1009 return nullptr;
1010 }
1011
1012private:
1013 const skjson::Value& fValue;
1014 SkSL::Compiler& fCompiler;
1015
1016 typedef SkSL::ExternalValue INHERITED;
1017};
1018
1019class PointerExternalValue : public SkSL::ExternalValue {
1020public:
1021 PointerExternalValue(const char* name, const SkSL::Type& type, void* data, size_t size)
1022 : INHERITED(name, type)
1023 , fData(data)
1024 , fSize(size) {}
1025
1026 bool canRead() const override {
1027 return true;
1028 }
1029
1030 bool canWrite() const override {
1031 return true;
1032 }
1033
1034 void read(int /*unusedIndex*/, float* target) override {
1035 memcpy(target, fData, fSize);
1036 }
1037
1038 void write(int /*unusedIndex*/, float* src) override {
1039 memcpy(fData, src, fSize);
1040 }
1041
1042
1043private:
1044 void* fData;
1045 size_t fSize;
1046
1047 typedef SkSL::ExternalValue INHERITED;
1048};
1049
1050DEF_TEST(SkSLInterpreterExternalValues, r) {
1051 const char* json = "{ \"value1\": 12, \"child\": { \"value2\": true, \"value3\": 5.5 } }";
1052 skjson::DOM dom(json, strlen(json));
1053 SkSL::Compiler compiler;
1054 SkSL::Program::Settings settings;
1055 const char* src = "float main() {"
1056 " outValue = 152;"
1057 " return root.child.value2 ? root.value1 * root.child.value3 : -1;"
1058 "}";
1059 compiler.registerExternalValue((SkSL::ExternalValue*) compiler.takeOwnership(
1060 std::unique_ptr<SkSL::Symbol>(new JSONExternalValue("root", &dom.root(), &compiler))));
1061 int32_t outValue = -1;
1062 compiler.registerExternalValue((SkSL::ExternalValue*) compiler.takeOwnership(
1063 std::unique_ptr<SkSL::Symbol>(new PointerExternalValue("outValue",
1064 *compiler.context().fInt_Type,
1065 &outValue,
1066 sizeof(outValue)))));
1067 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(
1068 SkSL::Program::kGeneric_Kind,
1069 SkSL::String(src), settings);
1070 REPORTER_ASSERT(r, program);
1071 if (program) {
1072 std::unique_ptr<SkSL::ByteCode> byteCode = compiler.toByteCode(*program);
1073 REPORTER_ASSERT(r, !compiler.errorCount());
1074 if (compiler.errorCount() > 0) {
1075 printf("%s\n%s", src, compiler.errorText().c_str());
1076 return;
1077 }
1078 const SkSL::ByteCodeFunction* main = byteCode->getFunction("main");
Ethan Nicholasb962eff2020-01-23 16:49:41 -05001079 SkSL::Interpreter<1> interpreter(std::move(byteCode));
1080 SkSL::ByteCode::Vector<1>* result;
1081 bool success = interpreter.run(main, nullptr, &result);
1082 REPORTER_ASSERT(r, success);
1083 REPORTER_ASSERT(r, result->fFloat[0] == 66.0);
Ben Wagner470e0ac2020-01-22 16:59:21 -05001084 REPORTER_ASSERT(r, outValue == 152);
1085 } else {
1086 printf("%s\n%s", src, compiler.errorText().c_str());
1087 }
1088}
1089
1090DEF_TEST(SkSLInterpreterExternalValuesVector, r) {
1091 SkSL::Compiler compiler;
1092 SkSL::Program::Settings settings;
1093 const char* src = "void main() {"
1094 " value *= 2;"
1095 "}";
1096 int32_t value[4] = { 1, 2, 3, 4 };
1097 compiler.registerExternalValue((SkSL::ExternalValue*) compiler.takeOwnership(
1098 std::unique_ptr<SkSL::Symbol>(new PointerExternalValue("value",
1099 *compiler.context().fInt4_Type,
1100 value,
1101 sizeof(value)))));
1102 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(SkSL::Program::kGeneric_Kind,
1103 SkSL::String(src),
1104 settings);
1105 REPORTER_ASSERT(r, program);
1106 if (program) {
1107 std::unique_ptr<SkSL::ByteCode> byteCode = compiler.toByteCode(*program);
1108 REPORTER_ASSERT(r, !compiler.errorCount());
1109 if (compiler.errorCount() > 0) {
1110 printf("%s\n%s", src, compiler.errorText().c_str());
1111 return;
1112 }
1113 const SkSL::ByteCodeFunction* main = byteCode->getFunction("main");
Ethan Nicholasb962eff2020-01-23 16:49:41 -05001114 SkSL::Interpreter<1> interpreter(std::move(byteCode));
1115 bool success = interpreter.run(main, nullptr, nullptr);
1116 REPORTER_ASSERT(r, success);
Ben Wagner470e0ac2020-01-22 16:59:21 -05001117 REPORTER_ASSERT(r, value[0] == 2);
1118 REPORTER_ASSERT(r, value[1] == 4);
1119 REPORTER_ASSERT(r, value[2] == 6);
1120 REPORTER_ASSERT(r, value[3] == 8);
1121 } else {
1122 printf("%s\n%s", src, compiler.errorText().c_str());
1123 }
1124}
1125
1126class FunctionExternalValue : public SkSL::ExternalValue {
1127public:
1128 FunctionExternalValue(const char* name, float(*function)(float), SkSL::Compiler& compiler)
1129 : INHERITED(name, *compiler.context().fFloat_Type)
1130 , fCompiler(compiler)
1131 , fFunction(function) {}
1132
1133 bool canCall() const override {
1134 return true;
1135 }
1136
1137 int callParameterCount() const override {
1138 return 1;
1139 }
1140
1141 void getCallParameterTypes(const SkSL::Type** outTypes) const override {
1142 outTypes[0] = fCompiler.context().fFloat_Type.get();
1143 }
1144
1145 void call(int /*unusedIndex*/, float* arguments, float* outReturn) override {
1146 outReturn[0] = fFunction(arguments[0]);
1147 }
1148
1149private:
1150 SkSL::Compiler& fCompiler;
1151
1152 float (*fFunction)(float);
1153
1154 typedef SkSL::ExternalValue INHERITED;
1155};
1156
1157DEF_TEST(SkSLInterpreterExternalValuesCall, r) {
1158 SkSL::Compiler compiler;
1159 SkSL::Program::Settings settings;
1160 const char* src = "float main() {"
1161 " return external(25);"
1162 "}";
1163 compiler.registerExternalValue((SkSL::ExternalValue*) compiler.takeOwnership(
1164 std::unique_ptr<SkSL::Symbol>(new FunctionExternalValue("external",
1165 [] (float x) {
1166 return (float) sqrt(x);
1167 },
1168 compiler))));
1169 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(SkSL::Program::kGeneric_Kind,
1170 SkSL::String(src),
1171 settings);
1172 REPORTER_ASSERT(r, program);
1173 if (program) {
1174 std::unique_ptr<SkSL::ByteCode> byteCode = compiler.toByteCode(*program);
1175 REPORTER_ASSERT(r, !compiler.errorCount());
1176 if (compiler.errorCount() > 0) {
1177 printf("%s\n%s", src, compiler.errorText().c_str());
1178 return;
1179 }
1180 const SkSL::ByteCodeFunction* main = byteCode->getFunction("main");
Ethan Nicholasb962eff2020-01-23 16:49:41 -05001181 SkSL::Interpreter<1> interpreter(std::move(byteCode));
1182 SkSL::ByteCode::Vector<1>* result;
1183 bool success = interpreter.run(main, nullptr, &result);
1184 REPORTER_ASSERT(r, success);
1185 REPORTER_ASSERT(r, result->fFloat[0] == 5.0);
Ben Wagner470e0ac2020-01-22 16:59:21 -05001186 } else {
1187 printf("%s\n%s", src, compiler.errorText().c_str());
1188 }
1189}
1190
1191class VectorFunctionExternalValue : public SkSL::ExternalValue {
1192public:
1193 VectorFunctionExternalValue(const char* name, void(*function)(float[4], float[4]),
1194 SkSL::Compiler& compiler)
1195 : INHERITED(name, *compiler.context().fFloat4_Type)
1196 , fCompiler(compiler)
1197 , fFunction(function) {}
Ben Wagner470e0ac2020-01-22 16:59:21 -05001198 bool canCall() const override {
1199 return true;
1200 }
Ben Wagner470e0ac2020-01-22 16:59:21 -05001201 int callParameterCount() const override {
1202 return 1;
1203 }
Ben Wagner470e0ac2020-01-22 16:59:21 -05001204 void getCallParameterTypes(const SkSL::Type** outTypes) const override {
1205 outTypes[0] = fCompiler.context().fFloat4_Type.get();
1206 }
Ben Wagner470e0ac2020-01-22 16:59:21 -05001207 void call(int /*unusedIndex*/, float* arguments, float* outReturn) override {
1208 fFunction(arguments, outReturn);
1209 }
Ben Wagner470e0ac2020-01-22 16:59:21 -05001210private:
1211 SkSL::Compiler& fCompiler;
Ben Wagner470e0ac2020-01-22 16:59:21 -05001212 void (*fFunction)(float[4], float[4]);
Ben Wagner470e0ac2020-01-22 16:59:21 -05001213 typedef SkSL::ExternalValue INHERITED;
1214};
Ben Wagner470e0ac2020-01-22 16:59:21 -05001215DEF_TEST(SkSLInterpreterExternalValuesVectorCall, r) {
1216 SkSL::Compiler compiler;
1217 SkSL::Program::Settings settings;
1218 const char* src = "float4 main() {"
1219 " return external(float4(1, 4, 9, 16));"
1220 "}";
1221 compiler.registerExternalValue((SkSL::ExternalValue*) compiler.takeOwnership(
1222 std::unique_ptr<SkSL::Symbol>(new VectorFunctionExternalValue("external",
1223 [] (float in[4], float out[4]) {
1224 out[0] = sqrt(in[0]);
1225 out[1] = sqrt(in[1]);
1226 out[2] = sqrt(in[2]);
1227 out[3] = sqrt(in[3]);
1228 },
1229 compiler))));
1230 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(SkSL::Program::kGeneric_Kind,
1231 SkSL::String(src),
1232 settings);
1233 REPORTER_ASSERT(r, program);
1234 if (program) {
1235 std::unique_ptr<SkSL::ByteCode> byteCode = compiler.toByteCode(*program);
1236 REPORTER_ASSERT(r, !compiler.errorCount());
1237 if (compiler.errorCount() > 0) {
1238 printf("%s\n%s", src, compiler.errorText().c_str());
1239 return;
1240 }
1241 const SkSL::ByteCodeFunction* main = byteCode->getFunction("main");
Ethan Nicholasb962eff2020-01-23 16:49:41 -05001242 SkSL::Interpreter<1> interpreter(std::move(byteCode));
1243 SkSL::ByteCode::Vector<1>* result;
1244 bool success = interpreter.run(main, nullptr, &result);
1245 REPORTER_ASSERT(r, success);
1246 REPORTER_ASSERT(r, result[0].fFloat[0] == 1.0);
1247 REPORTER_ASSERT(r, result[1].fFloat[0] == 2.0);
1248 REPORTER_ASSERT(r, result[2].fFloat[0] == 3.0);
1249 REPORTER_ASSERT(r, result[3].fFloat[0] == 4.0);
Ben Wagner470e0ac2020-01-22 16:59:21 -05001250 } else {
1251 printf("%s\n%s", src, compiler.errorText().c_str());
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001252 }
1253}