blob: 82512691381219ad03c73da374b7780e5ef3a0ae [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 Nicholas91164d12019-05-15 15:29:54 -040012#include "src/utils/SkJSON.h"
Ethan Nicholas0e9401d2019-03-21 11:05:37 -040013
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "tests/Test.h"
Ethan Nicholas0e9401d2019-03-21 11:05:37 -040015
Ben Wagner470e0ac2020-01-22 16:59:21 -050016static bool nearly_equal(const float a[], const float b[], int count) {
17 for (int i = 0; i < count; ++i) {
18 if (!SkScalarNearlyEqual(a[i], b[i])) {
19 return false;
20 }
21 }
22 return true;
23}
24
25void test(skiatest::Reporter* r, const char* src, float* in, float* expected,
Mike Reed634c9412019-07-18 13:20:04 -040026 bool exactCompare = true) {
Ethan Nicholas746035a2019-04-23 13:31:09 -040027 SkSL::Compiler compiler;
28 SkSL::Program::Settings settings;
29 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(
30 SkSL::Program::kGeneric_Kind,
31 SkSL::String(src), settings);
32 REPORTER_ASSERT(r, program);
33 if (program) {
34 std::unique_ptr<SkSL::ByteCode> byteCode = compiler.toByteCode(*program);
Brian Osman80164412019-06-07 13:00:23 -040035 program.reset();
Ethan Nicholas746035a2019-04-23 13:31:09 -040036 REPORTER_ASSERT(r, !compiler.errorCount());
37 if (compiler.errorCount() > 0) {
38 printf("%s\n%s", src, compiler.errorText().c_str());
39 return;
40 }
Brian Osman886af0d2019-07-26 15:12:56 -040041 const SkSL::ByteCodeFunction* main = byteCode->getFunction("main");
Ben Wagner470e0ac2020-01-22 16:59:21 -050042 int returnCount = main->getReturnCount();
43 std::unique_ptr<float[]> out = std::unique_ptr<float[]>(new float[returnCount]);
44 SkAssertResult(byteCode->run(main, in, main->getParameterCount(), out.get(), returnCount,
45 nullptr, 0));
46 bool valid = exactCompare ? !memcmp(out.get(), expected, sizeof(float) * returnCount)
47 : nearly_equal(out.get(), expected, returnCount);
48 if (!valid) {
49 printf("for program: %s\n", src);
50 printf(" expected (");
51 const char* separator = "";
52 for (int i = 0; i < returnCount; ++i) {
53 printf("%s%f", separator, expected[i]);
54 separator = ", ";
55 }
56 printf("), but received (");
57 separator = "";
58 for (int i = 0; i < returnCount; ++i) {
59 printf("%s%f", separator, out.get()[i]);
60 separator = ", ";
61 }
62 printf(")\n");
63 main->disassemble();
Ethan Nicholas5f409862020-01-22 14:14:25 -050064 }
Ben Wagner470e0ac2020-01-22 16:59:21 -050065 REPORTER_ASSERT(r, valid);
Ethan Nicholas746035a2019-04-23 13:31:09 -040066 } else {
67 printf("%s\n%s", src, compiler.errorText().c_str());
68 }
69}
70
Brian Osman569f12f2019-06-13 11:23:57 -040071void vec_test(skiatest::Reporter* r, const char* src) {
Brian Osman08a84962019-06-14 10:17:16 -040072 SkSL::Compiler compiler;
73 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(
74 SkSL::Program::kGeneric_Kind, SkSL::String(src), SkSL::Program::Settings());
75 if (!program) {
76 REPORT_FAILURE(r, "!program", SkString(compiler.errorText().c_str()));
77 return;
78 }
79
80 std::unique_ptr<SkSL::ByteCode> byteCode = compiler.toByteCode(*program);
81 if (compiler.errorCount() > 0) {
82 REPORT_FAILURE(r, "!toByteCode", SkString(compiler.errorText().c_str()));
83 return;
84 }
85
Ben Wagner470e0ac2020-01-22 16:59:21 -050086 const SkSL::ByteCodeFunction* main = byteCode->getFunction("main");
Brian Osman08a84962019-06-14 10:17:16 -040087
Brian Osmanb23d66e2019-09-27 10:25:57 -040088 // Test on four different vectors (with varying orderings to get divergent control flow)
89 const float input[16] = { 1, 2, 3, 4,
90 4, 3, 2, 1,
91 7, 5, 8, 6,
92 6, 8, 5, 7 };
93
Brian Osman569f12f2019-06-13 11:23:57 -040094 float out_s[16], out_v[16];
95 memcpy(out_s, input, sizeof(out_s));
96 memcpy(out_v, input, sizeof(out_v));
97
Brian Osman08a84962019-06-14 10:17:16 -040098 // First run in scalar mode to determine the expected output
99 for (int i = 0; i < 4; ++i) {
Ben Wagner470e0ac2020-01-22 16:59:21 -0500100 SkAssertResult(byteCode->run(main, out_s + i * 4, 4, nullptr, 0, nullptr, 0));
Brian Osman08a84962019-06-14 10:17:16 -0400101 }
Brian Osman569f12f2019-06-13 11:23:57 -0400102
Brian Osmanb23d66e2019-09-27 10:25:57 -0400103 // Need to transpose input vectors for striped execution
104 auto transpose = [](float* v) {
105 for (int r = 0; r < 4; ++r)
106 for (int c = 0; c < r; ++c)
107 std::swap(v[r*4 + c], v[c*4 + r]);
108 };
109
110 // Need to transpose input vectors for striped execution
111 transpose(out_v);
112 float* args[] = { out_v, out_v + 4, out_v + 8, out_v + 12 };
113
Brian Osman08a84962019-06-14 10:17:16 -0400114 // Now run in parallel and compare results
Ben Wagner470e0ac2020-01-22 16:59:21 -0500115 SkAssertResult(byteCode->runStriped(main, 4, args, 4, nullptr, 0, nullptr, 0));
Brian Osmanb23d66e2019-09-27 10:25:57 -0400116
117 // Transpose striped outputs back
118 transpose(out_v);
119
Brian Osman08a84962019-06-14 10:17:16 -0400120 if (memcmp(out_s, out_v, sizeof(out_s)) != 0) {
121 printf("for program: %s\n", src);
122 for (int i = 0; i < 4; ++i) {
123 printf("(%g %g %g %g) -> (%g %g %g %g), expected (%g %g %g %g)\n",
124 input[4*i + 0], input[4*i + 1], input[4*i + 2], input[4*i + 3],
125 out_v[4*i + 0], out_v[4*i + 1], out_v[4*i + 2], out_v[4*i + 3],
126 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 -0400127 }
Ben Wagner470e0ac2020-01-22 16:59:21 -0500128 main->disassemble();
Brian Osman08a84962019-06-14 10:17:16 -0400129 REPORT_FAILURE(r, "VecInterpreter mismatch", SkString());
Brian Osman569f12f2019-06-13 11:23:57 -0400130 }
131}
132
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400133void test(skiatest::Reporter* r, const char* src, float inR, float inG, float inB, float inA,
Brian Osman08a84962019-06-14 10:17:16 -0400134 float expectedR, float expectedG, float expectedB, float expectedA) {
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400135 SkSL::Compiler compiler;
136 SkSL::Program::Settings settings;
137 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(
Ethan Nicholas746035a2019-04-23 13:31:09 -0400138 SkSL::Program::kGeneric_Kind,
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400139 SkSL::String(src), settings);
140 REPORTER_ASSERT(r, program);
141 if (program) {
142 std::unique_ptr<SkSL::ByteCode> byteCode = compiler.toByteCode(*program);
Brian Osman80164412019-06-07 13:00:23 -0400143 program.reset();
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400144 REPORTER_ASSERT(r, !compiler.errorCount());
145 if (compiler.errorCount() > 0) {
146 printf("%s\n%s", src, compiler.errorText().c_str());
147 return;
148 }
Brian Osman569f12f2019-06-13 11:23:57 -0400149 const SkSL::ByteCodeFunction* main = byteCode->getFunction("main");
Ben Wagner470e0ac2020-01-22 16:59:21 -0500150 float inoutColor[4] = { inR, inG, inB, inA };
151 SkAssertResult(byteCode->run(main, inoutColor, 4, nullptr, 0, nullptr, 0));
152 if (inoutColor[0] != expectedR || inoutColor[1] != expectedG ||
153 inoutColor[2] != expectedB || inoutColor[3] != expectedA) {
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400154 printf("for program: %s\n", src);
155 printf(" expected (%f, %f, %f, %f), but received (%f, %f, %f, %f)\n", expectedR,
Ben Wagner470e0ac2020-01-22 16:59:21 -0500156 expectedG, expectedB, expectedA, inoutColor[0], inoutColor[1], inoutColor[2],
157 inoutColor[3]);
Brian Osman08a84962019-06-14 10:17:16 -0400158 main->disassemble();
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400159 }
Ben Wagner470e0ac2020-01-22 16:59:21 -0500160 REPORTER_ASSERT(r, inoutColor[0] == expectedR);
161 REPORTER_ASSERT(r, inoutColor[1] == expectedG);
162 REPORTER_ASSERT(r, inoutColor[2] == expectedB);
163 REPORTER_ASSERT(r, inoutColor[3] == expectedA);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400164 } else {
165 printf("%s\n%s", src, compiler.errorText().c_str());
166 }
Brian Osman569f12f2019-06-13 11:23:57 -0400167
168 // Do additional testing of 4x1 vs 1x4 to stress divergent control flow, etc.
169 vec_test(r, src);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400170}
Ben Wagner470e0ac2020-01-22 16:59:21 -0500171
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400172DEF_TEST(SkSLInterpreterAdd, r) {
173 test(r, "void main(inout half4 color) { color.r = color.r + color.g; }", 0.25, 0.75, 0, 0, 1,
174 0.75, 0, 0);
175 test(r, "void main(inout half4 color) { color += half4(1, 2, 3, 4); }", 4, 3, 2, 1, 5, 5, 5, 5);
176 test(r, "void main(inout half4 color) { half4 c = color; color += c; }", 0.25, 0.5, 0.75, 1,
177 0.5, 1, 1.5, 2);
Ethan Nicholas6f624122019-09-24 13:07:06 -0400178 test(r, "void main(inout half4 color) { color.r = int(color.r) + int(color.g); }", 1, 3, 0, 0,
179 4, 3, 0, 0);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400180}
181
182DEF_TEST(SkSLInterpreterSubtract, r) {
183 test(r, "void main(inout half4 color) { color.r = color.r - color.g; }", 1, 0.75, 0, 0, 0.25,
184 0.75, 0, 0);
185 test(r, "void main(inout half4 color) { color -= half4(1, 2, 3, 4); }", 5, 5, 5, 5, 4, 3, 2, 1);
186 test(r, "void main(inout half4 color) { half4 c = color; color -= c; }", 4, 3, 2, 1,
187 0, 0, 0, 0);
Ethan Nicholas354ecf32019-05-07 16:13:02 -0400188 test(r, "void main(inout half4 color) { color.x = -color.x; }", 4, 3, 2, 1, -4, 3, 2, 1);
189 test(r, "void main(inout half4 color) { color = -color; }", 4, 3, 2, 1, -4, -3, -2, -1);
Ethan Nicholas6f624122019-09-24 13:07:06 -0400190 test(r, "void main(inout half4 color) { color.r = int(color.r) - int(color.g); }", 3, 1, 0, 0,
191 2, 1, 0, 0);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400192}
193
194DEF_TEST(SkSLInterpreterMultiply, r) {
195 test(r, "void main(inout half4 color) { color.r = color.r * color.g; }", 2, 3, 0, 0, 6, 3, 0,
196 0);
197 test(r, "void main(inout half4 color) { color *= half4(1, 2, 3, 4); }", 2, 3, 4, 5, 2, 6, 12,
198 20);
199 test(r, "void main(inout half4 color) { half4 c = color; color *= c; }", 4, 3, 2, 1,
200 16, 9, 4, 1);
Ethan Nicholas6f624122019-09-24 13:07:06 -0400201 test(r, "void main(inout half4 color) { color.r = int(color.r) * int(color.g); }", 3, -2, 0, 0,
202 -6, -2, 0, 0);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400203}
204
205DEF_TEST(SkSLInterpreterDivide, r) {
206 test(r, "void main(inout half4 color) { color.r = color.r / color.g; }", 1, 2, 0, 0, 0.5, 2, 0,
207 0);
208 test(r, "void main(inout half4 color) { color /= half4(1, 2, 3, 4); }", 12, 12, 12, 12, 12, 6,
209 4, 3);
210 test(r, "void main(inout half4 color) { half4 c = color; color /= c; }", 4, 3, 2, 1,
211 1, 1, 1, 1);
Ethan Nicholas6f624122019-09-24 13:07:06 -0400212 test(r, "void main(inout half4 color) { color.r = int(color.r) / int(color.g); }", 8, -2, 0, 0,
213 -4, -2, 0, 0);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400214}
215
216DEF_TEST(SkSLInterpreterRemainder, r) {
Brian Osman3b41baf2019-05-08 09:24:46 -0400217 test(r, "void main(inout half4 color) { color.r = color.r % color.g; }", 3.125, 2, 0, 0,
218 1.125, 2, 0, 0);
219 test(r, "void main(inout half4 color) { color %= half4(1, 2, 3, 4); }", 9.5, 9.5, 9.5, 9.5,
220 0.5, 1.5, 0.5, 1.5);
Ethan Nicholas6f624122019-09-24 13:07:06 -0400221 test(r, "void main(inout half4 color) { color.r = int(color.r) % int(color.g); }", 8, 3, 0, 0,
222 2, 3, 0, 0);
223 test(r, "void main(inout half4 color) { color.rg = half2(int2(int(color.r), int(color.g)) % "
224 "int(color.b)); }", 8, 10, 6, 0, 2, 4, 6, 0);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400225}
226
Ethan Nicholasd166d2e2019-09-23 11:43:45 -0400227DEF_TEST(SkSLInterpreterAnd, r) {
228 test(r, "void main(inout half4 color) { if (color.r > color.g && color.g > color.b) "
229 "color = half4(color.a); }", 2, 1, 0, 3, 3, 3, 3, 3);
230 test(r, "void main(inout half4 color) { if (color.r > color.g && color.g > color.b) "
231 "color = half4(color.a); }", 1, 1, 0, 3, 1, 1, 0, 3);
232 test(r, "void main(inout half4 color) { if (color.r > color.g && color.g > color.b) "
233 "color = half4(color.a); }", 2, 1, 1, 3, 2, 1, 1, 3);
234 test(r, "int global; bool update() { global = 123; return true; }"
235 "void main(inout half4 color) { global = 0; if (color.r > color.g && update()) "
236 "color = half4(color.a); color.a = global; }", 2, 1, 1, 3, 3, 3, 3, 123);
237 test(r, "int global; bool update() { global = 123; return true; }"
238 "void main(inout half4 color) { global = 0; if (color.r > color.g && update()) "
239 "color = half4(color.a); color.a = global; }", 1, 1, 1, 3, 1, 1, 1, 0);
240}
241
242DEF_TEST(SkSLInterpreterOr, r) {
243 test(r, "void main(inout half4 color) { if (color.r > color.g || color.g > color.b) "
244 "color = half4(color.a); }", 2, 1, 0, 3, 3, 3, 3, 3);
245 test(r, "void main(inout half4 color) { if (color.r > color.g || color.g > color.b) "
246 "color = half4(color.a); }", 1, 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, 1, 3, 1, 1, 1, 3);
249 test(r, "int global; bool update() { global = 123; return true; }"
250 "void main(inout half4 color) { global = 0; if (color.r > color.g || update()) "
251 "color = half4(color.a); color.a = global; }", 1, 1, 1, 3, 3, 3, 3, 123);
252 test(r, "int global; bool update() { global = 123; return true; }"
253 "void main(inout half4 color) { global = 0; if (color.r > color.g || update()) "
254 "color = half4(color.a); color.a = global; }", 2, 1, 1, 3, 3, 3, 3, 0);
255}
256
Brian Osmane5bbce22019-09-23 12:38:40 -0400257DEF_TEST(SkSLInterpreterBitwise, r) {
258 test(r, "void main(inout half4 color) { color.r = half(int(color.r) | 3); }",
259 5, 0, 0, 0, 7, 0, 0, 0);
260 test(r, "void main(inout half4 color) { color.r = half(int(color.r) & 3); }",
261 6, 0, 0, 0, 2, 0, 0, 0);
262 test(r, "void main(inout half4 color) { color.r = half(int(color.r) ^ 3); }",
263 5, 0, 0, 0, 6, 0, 0, 0);
264 test(r, "void main(inout half4 color) { color.r = half(~int(color.r) & 3); }",
265 6, 0, 0, 0, 1, 0, 0, 0);
Brian Osman4c2146f2019-09-24 09:39:38 -0400266
Brian Osman73fb39c2019-09-24 16:22:55 -0400267 test(r, "void main(inout half4 color) { color.r = half(uint(color.r) | 3); }",
268 5, 0, 0, 0, 7, 0, 0, 0);
269 test(r, "void main(inout half4 color) { color.r = half(uint(color.r) & 3); }",
270 6, 0, 0, 0, 2, 0, 0, 0);
271 test(r, "void main(inout half4 color) { color.r = half(uint(color.r) ^ 3); }",
272 5, 0, 0, 0, 6, 0, 0, 0);
273 test(r, "void main(inout half4 color) { color.r = half(~uint(color.r) & 3); }",
274 6, 0, 0, 0, 1, 0, 0, 0);
275
Brian Osman4c2146f2019-09-24 09:39:38 -0400276 // Shift operators
277 unsigned in = 0x80000011;
278 unsigned out;
279
280 out = 0x00000088;
Ben Wagner470e0ac2020-01-22 16:59:21 -0500281 test(r, "int main(int x) { return x << 3; }", (float*)&in, (float*)&out);
Brian Osman4c2146f2019-09-24 09:39:38 -0400282
283 out = 0xF0000002;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400284 test(r, "int main(int x) { return x >> 3; }", (float*)&in, (float*)&out);
Brian Osman4c2146f2019-09-24 09:39:38 -0400285
286 out = 0x10000002;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400287 test(r, "uint main(uint x) { return x >> 3; }", (float*)&in, (float*)&out);
Brian Osmane5bbce22019-09-23 12:38:40 -0400288}
289
Brian Osman29e013d2019-05-28 17:16:03 -0400290DEF_TEST(SkSLInterpreterMatrix, r) {
Brian Osman08a84962019-06-14 10:17:16 -0400291 float in[16];
292 float expected[16];
Brian Osman29e013d2019-05-28 17:16:03 -0400293
294 // Constructing matrix from scalar produces a diagonal matrix
295 in[0] = 1.0f;
296 expected[0] = 2.0f;
297 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 -0400298 in, expected);
Brian Osman29e013d2019-05-28 17:16:03 -0400299
300 // With non-square matrix
301 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 -0400302 in, expected);
Brian Osman29e013d2019-05-28 17:16:03 -0400303
304 // Constructing from a different-sized matrix fills the remaining space with the identity matrix
305 test(r, "float main(float x) {"
306 "float3x2 m = float3x2(x);"
307 "float4x4 m2 = float4x4(m);"
308 "return m2[0][0] + m2[3][3]; }",
Brian Osmanb23d66e2019-09-27 10:25:57 -0400309 in, expected);
Brian Osman29e013d2019-05-28 17:16:03 -0400310
311 // Constructing a matrix from vectors or scalars fills in values in column-major order
312 in[0] = 1.0f;
313 in[1] = 2.0f;
314 in[2] = 4.0f;
315 in[3] = 8.0f;
316 expected[0] = 6.0f;
317 test(r, "float main(float4 v) { float2x2 m = float2x2(v); return m[0][1] + m[1][0]; }",
Brian Osmanb23d66e2019-09-27 10:25:57 -0400318 in, expected);
Brian Osman29e013d2019-05-28 17:16:03 -0400319
320 expected[0] = 10.0f;
321 test(r, "float main(float4 v) {"
322 "float2x2 m = float2x2(v.x, v.y, v.w, v.z);"
323 "return m[0][1] + m[1][0]; }",
Brian Osmanb23d66e2019-09-27 10:25:57 -0400324 in, expected);
Brian Osman29e013d2019-05-28 17:16:03 -0400325
Brian Osman1e855b22019-05-29 15:21:52 -0400326 // Initialize 16 values to be used as inputs to matrix tests
327 for (int i = 0; i < 16; ++i) { in[i] = (float)i; }
Brian Osman29e013d2019-05-28 17:16:03 -0400328
Brian Osman1e855b22019-05-29 15:21:52 -0400329 // M+M, M-S, S-M
330 for (int i = 0; i < 16; ++i) { expected[i] = (float)(2 * i); }
Brian Osmanb23d66e2019-09-27 10:25:57 -0400331 test(r, "float4x4 main(float4x4 m) { return m + m; }", in, expected);
Brian Osman1e855b22019-05-29 15:21:52 -0400332 for (int i = 0; i < 16; ++i) { expected[i] = (float)(i + 3); }
Brian Osmanb23d66e2019-09-27 10:25:57 -0400333 test(r, "float4x4 main(float4x4 m) { return m + 3.0; }", in, expected);
334 test(r, "float4x4 main(float4x4 m) { return 3.0 + m; }", in, expected);
Brian Osman1e855b22019-05-29 15:21:52 -0400335
336 // M-M, M-S, S-M
337 for (int i = 0; i < 8; ++i) { expected[i] = 8.0f; }
Brian Osmanb23d66e2019-09-27 10:25:57 -0400338 test(r, "float4x2 main(float4x2 m1, float4x2 m2) { return m2 - m1; }", in, expected);
Brian Osman1e855b22019-05-29 15:21:52 -0400339 for (int i = 0; i < 16; ++i) { expected[i] = (float)(i - 3); }
Brian Osmanb23d66e2019-09-27 10:25:57 -0400340 test(r, "float4x4 main(float4x4 m) { return m - 3.0; }", in, expected);
Brian Osman1e855b22019-05-29 15:21:52 -0400341 for (int i = 0; i < 16; ++i) { expected[i] = (float)(3 - i); }
Brian Osmanb23d66e2019-09-27 10:25:57 -0400342 test(r, "float4x4 main(float4x4 m) { return 3.0 - m; }", in, expected);
Brian Osman1e855b22019-05-29 15:21:52 -0400343
344 // M*S, S*M, M/S, S/M
345 for (int i = 0; i < 16; ++i) { expected[i] = (float)(i * 3); }
Brian Osmanb23d66e2019-09-27 10:25:57 -0400346 test(r, "float4x4 main(float4x4 m) { return m * 3.0; }", in, expected);
347 test(r, "float4x4 main(float4x4 m) { return 3.0 * m; }", in, expected);
Brian Osman1e855b22019-05-29 15:21:52 -0400348 for (int i = 0; i < 16; ++i) { expected[i] = (float)(i) / 2.0f; }
Brian Osmanb23d66e2019-09-27 10:25:57 -0400349 test(r, "float4x4 main(float4x4 m) { return m / 2.0; }", in, expected);
Brian Osman5bdf5252019-05-29 17:04:54 -0400350 for (int i = 0; i < 16; ++i) { expected[i] = 1.0f / (float)(i + 1); }
Brian Osmanb23d66e2019-09-27 10:25:57 -0400351 test(r, "float4x4 main(float4x4 m) { return 1.0 / (m + 1); }", in, expected);
Brian Osman1e855b22019-05-29 15:21:52 -0400352
353#if 0
354 // Matrix negation - legal in GLSL, not in SkSL?
355 for (int i = 0; i < 16; ++i) { expected[i] = (float)(-i); }
356 test(r, "float4x4 main(float4x4 m) { return -m; }", in, 16, expected);
357#endif
358
Brian Osman909231c2019-05-29 15:34:36 -0400359 // M*V, V*M
360 for (int i = 0; i < 4; ++i) {
361 expected[i] = 12.0f*i + 13.0f*(i+4) + 14.0f*(i+8);
362 }
Brian Osmanb23d66e2019-09-27 10:25:57 -0400363 test(r, "float4 main(float3x4 m, float3 v) { return m * v; }", in, expected);
Brian Osman909231c2019-05-29 15:34:36 -0400364 for (int i = 0; i < 4; ++i) {
365 expected[i] = 12.0f*(3*i) + 13.0f*(3*i+1) + 14.0f*(3*i+2);
366 }
Brian Osmanb23d66e2019-09-27 10:25:57 -0400367 test(r, "float4 main(float4x3 m, float3 v) { return v * m; }", in, expected);
Brian Osman29e013d2019-05-28 17:16:03 -0400368
Brian Osman909231c2019-05-29 15:34:36 -0400369 // M*M
370 {
Mike Reedb26b4e72020-01-22 14:31:21 -0500371 SkM44 m;
372 m.setColMajor(in);
373 SkM44 m2;
374 float in2[16];
Brian Osman909231c2019-05-29 15:34:36 -0400375 for (int i = 0; i < 16; ++i) {
Mike Reedb26b4e72020-01-22 14:31:21 -0500376 in2[i] = (i + 4) % 16;
Brian Osman909231c2019-05-29 15:34:36 -0400377 }
Mike Reedb26b4e72020-01-22 14:31:21 -0500378 m2.setColMajor(in2);
Brian Osman909231c2019-05-29 15:34:36 -0400379 m.setConcat(m, m2);
380 // Rearrange the columns on the RHS so we detect left-hand/right-hand errors
381 test(r, "float4x4 main(float4x4 m) { return m * float4x4(m[1], m[2], m[3], m[0]); }",
Brian Osmanb23d66e2019-09-27 10:25:57 -0400382 in, (float*)&m);
Brian Osman909231c2019-05-29 15:34:36 -0400383 }
Brian Osman29e013d2019-05-28 17:16:03 -0400384}
385
Brian Osman4e93feb2019-05-16 15:38:00 -0400386DEF_TEST(SkSLInterpreterTernary, r) {
387 test(r, "void main(inout half4 color) { color.r = color.g > color.b ? color.g : color.b; }",
388 0, 1, 2, 0, 2, 1, 2, 0);
389 test(r, "void main(inout half4 color) { color.r = color.g > color.b ? color.g : color.b; }",
390 0, 3, 2, 0, 3, 3, 2, 0);
391}
392
Brian Osman41672152019-05-14 13:37:30 -0400393DEF_TEST(SkSLInterpreterCast, r) {
Brian Osman08a84962019-06-14 10:17:16 -0400394 union Val {
395 float f;
396 uint32_t u;
397 int32_t s;
398 };
Brian Osman41672152019-05-14 13:37:30 -0400399
Brian Osman08a84962019-06-14 10:17:16 -0400400 Val input[2];
401 Val expected[2];
Brian Osman41672152019-05-14 13:37:30 -0400402
Brian Osman08a84962019-06-14 10:17:16 -0400403 input[0].s = 3;
404 input[1].s = -5;
405 expected[0].f = 3.0f;
406 expected[1].f = -5.0f;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400407 test(r, "float main(int x) { return float (x); }", (float*)input, (float*)expected);
408 test(r, "float2 main(int2 x) { return float2(x); }", (float*)input, (float*)expected);
Brian Osman41672152019-05-14 13:37:30 -0400409
Brian Osman08a84962019-06-14 10:17:16 -0400410 input[0].u = 3;
411 input[1].u = 5;
412 expected[0].f = 3.0f;
413 expected[1].f = 5.0f;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400414 test(r, "float main(uint x) { return float (x); }", (float*)input, (float*)expected);
415 test(r, "float2 main(uint2 x) { return float2(x); }", (float*)input, (float*)expected);
Brian Osmanc51d7912019-05-22 15:16:16 -0700416
Brian Osman08a84962019-06-14 10:17:16 -0400417 input[0].f = 3.0f;
418 input[1].f = -5.0f;
419 expected[0].s = 3;
420 expected[1].s = -5;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400421 test(r, "int main(float x) { return int (x); }", (float*)input, (float*)expected);
422 test(r, "int2 main(float2 x) { return int2(x); }", (float*)input, (float*)expected);
Brian Osman08a84962019-06-14 10:17:16 -0400423
424 input[0].s = 3;
425 expected[0].f = 3.0f;
426 expected[1].f = 3.0f;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400427 test(r, "float2 main(int x) { return float2(x); }", (float*)input, (float*)expected);
Brian Osman41672152019-05-14 13:37:30 -0400428}
429
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400430DEF_TEST(SkSLInterpreterIf, r) {
431 test(r, "void main(inout half4 color) { if (color.r > color.g) color.a = 1; }", 5, 3, 0, 0,
432 5, 3, 0, 1);
433 test(r, "void main(inout half4 color) { if (color.r > color.g) color.a = 1; }", 5, 5, 0, 0,
434 5, 5, 0, 0);
435 test(r, "void main(inout half4 color) { if (color.r > color.g) color.a = 1; }", 5, 6, 0, 0,
436 5, 6, 0, 0);
437 test(r, "void main(inout half4 color) { if (color.r < color.g) color.a = 1; }", 3, 5, 0, 0,
438 3, 5, 0, 1);
439 test(r, "void main(inout half4 color) { if (color.r < color.g) color.a = 1; }", 5, 5, 0, 0,
440 5, 5, 0, 0);
441 test(r, "void main(inout half4 color) { if (color.r < color.g) color.a = 1; }", 6, 5, 0, 0,
442 6, 5, 0, 0);
443 test(r, "void main(inout half4 color) { if (color.r >= color.g) color.a = 1; }", 5, 3, 0, 0,
444 5, 3, 0, 1);
445 test(r, "void main(inout half4 color) { if (color.r >= color.g) color.a = 1; }", 5, 5, 0, 0,
446 5, 5, 0, 1);
447 test(r, "void main(inout half4 color) { if (color.r >= color.g) color.a = 1; }", 5, 6, 0, 0,
448 5, 6, 0, 0);
449 test(r, "void main(inout half4 color) { if (color.r <= color.g) color.a = 1; }", 3, 5, 0, 0,
450 3, 5, 0, 1);
451 test(r, "void main(inout half4 color) { if (color.r <= color.g) color.a = 1; }", 5, 5, 0, 0,
452 5, 5, 0, 1);
453 test(r, "void main(inout half4 color) { if (color.r <= color.g) color.a = 1; }", 6, 5, 0, 0,
454 6, 5, 0, 0);
455 test(r, "void main(inout half4 color) { if (color.r == color.g) color.a = 1; }", 2, 2, 0, 0,
456 2, 2, 0, 1);
457 test(r, "void main(inout half4 color) { if (color.r == color.g) color.a = 1; }", 2, -2, 0, 0,
458 2, -2, 0, 0);
459 test(r, "void main(inout half4 color) { if (color.r != color.g) color.a = 1; }", 2, 2, 0, 0,
460 2, 2, 0, 0);
461 test(r, "void main(inout half4 color) { if (color.r != color.g) color.a = 1; }", 2, -2, 0, 0,
462 2, -2, 0, 1);
Brian Osmane5bbce22019-09-23 12:38:40 -0400463 test(r, "void main(inout half4 color) { if (!(color.r == color.g)) color.a = 1; }", 2, 2, 0, 0,
464 2, 2, 0, 0);
465 test(r, "void main(inout half4 color) { if (!(color.r == color.g)) color.a = 1; }", 2, -2, 0, 0,
466 2, -2, 0, 1);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400467 test(r, "void main(inout half4 color) { if (color.r == color.g) color.a = 1; else "
468 "color.a = 2; }", 1, 1, 0, 0, 1, 1, 0, 1);
469 test(r, "void main(inout half4 color) { if (color.r == color.g) color.a = 1; else "
470 "color.a = 2; }", 2, -2, 0, 0, 2, -2, 0, 2);
471}
472
Brian Osman16e6fd52019-05-29 11:19:00 -0400473DEF_TEST(SkSLInterpreterIfVector, r) {
474 test(r, "void main(inout half4 color) { if (color.rg == color.ba) color.a = 1; }",
475 1, 2, 1, 2, 1, 2, 1, 1);
476 test(r, "void main(inout half4 color) { if (color.rg == color.ba) color.a = 1; }",
477 1, 2, 3, 2, 1, 2, 3, 2);
478 test(r, "void main(inout half4 color) { if (color.rg != color.ba) color.a = 1; }",
479 1, 2, 1, 2, 1, 2, 1, 2);
480 test(r, "void main(inout half4 color) { if (color.rg != color.ba) color.a = 1; }",
481 1, 2, 3, 2, 1, 2, 3, 1);
482}
483
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400484DEF_TEST(SkSLInterpreterWhile, r) {
Brian Osman52c1bf12019-07-18 13:12:19 -0400485 test(r, "void main(inout half4 color) { while (color.r < 8) { color.r++; } }",
486 1, 2, 3, 4, 8, 2, 3, 4);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400487 test(r, "void main(inout half4 color) { while (color.r < 1) color.r += 0.25; }", 0, 0, 0, 0, 1,
488 0, 0, 0);
Brian Osman569f12f2019-06-13 11:23:57 -0400489 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 -0400490 0, 0, 0);
491 test(r, "void main(inout half4 color) { while (true) { color.r += 0.5; "
Brian Osman569f12f2019-06-13 11:23:57 -0400492 "if (color.r > 5) break; } }", 0, 0, 0, 0, 5.5, 0, 0, 0);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400493 test(r, "void main(inout half4 color) { while (color.r < 10) { color.r += 0.5; "
494 "if (color.r < 5) continue; break; } }", 0, 0, 0, 0, 5, 0, 0, 0);
Brian Osman8d564572019-06-19 11:00:28 -0400495 test(r,
496 "void main(inout half4 color) {"
497 " while (true) {"
498 " if (color.r > 4) { break; }"
499 " while (true) { color.a = 1; break; }"
500 " break;"
501 " }"
502 "}",
503 6, 5, 4, 3, 6, 5, 4, 3);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400504}
505
506DEF_TEST(SkSLInterpreterDo, r) {
507 test(r, "void main(inout half4 color) { do color.r += 0.25; while (color.r < 1); }", 0, 0, 0, 0,
508 1, 0, 0, 0);
Brian Osman569f12f2019-06-13 11:23:57 -0400509 test(r, "void main(inout half4 color) { do color.r -= 0.25; while (color.r > 1); }", 0, 0, 0, 0,
510 -0.25, 0, 0, 0);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400511 test(r, "void main(inout half4 color) { do { color.r += 0.5; if (color.r > 1) break; } while "
512 "(true); }", 0, 0, 0, 0, 1.5, 0, 0, 0);
513 test(r, "void main(inout half4 color) {do { color.r += 0.5; if (color.r < 5) "
514 "continue; if (color.r >= 5) break; } while (true); }", 0, 0, 0, 0, 5, 0, 0, 0);
Brian Osman44d44762019-05-13 14:19:12 -0400515 test(r, "void main(inout half4 color) { do { color.r += 0.5; } while (false); }",
516 0, 0, 0, 0, 0.5, 0, 0, 0);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400517}
518
519DEF_TEST(SkSLInterpreterFor, r) {
520 test(r, "void main(inout half4 color) { for (int i = 1; i <= 10; ++i) color.r += i; }", 0, 0, 0,
521 0, 55, 0, 0, 0);
522 test(r,
523 "void main(inout half4 color) {"
524 " for (int i = 1; i <= 10; ++i)"
525 " for (int j = i; j <= 10; ++j)"
526 " color.r += j;"
527 "}",
528 0, 0, 0, 0,
529 385, 0, 0, 0);
530 test(r,
531 "void main(inout half4 color) {"
532 " for (int i = 1; i <= 10; ++i)"
533 " for (int j = 1; ; ++j) {"
534 " if (i == j) continue;"
535 " if (j > 10) break;"
536 " color.r += j;"
537 " }"
538 "}",
539 0, 0, 0, 0,
540 495, 0, 0, 0);
541}
542
Brian Osmanf3fa6002019-05-17 14:26:53 -0400543DEF_TEST(SkSLInterpreterPrefixPostfix, r) {
544 test(r, "void main(inout half4 color) { color.r = ++color.g; }", 1, 2, 3, 4, 3, 3, 3, 4);
545 test(r, "void main(inout half4 color) { color.r = color.g++; }", 1, 2, 3, 4, 2, 3, 3, 4);
546}
547
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400548DEF_TEST(SkSLInterpreterSwizzle, r) {
549 test(r, "void main(inout half4 color) { color = color.abgr; }", 1, 2, 3, 4, 4, 3, 2, 1);
550 test(r, "void main(inout half4 color) { color.rgb = half4(5, 6, 7, 8).bbg; }", 1, 2, 3, 4, 7, 7,
551 6, 4);
552 test(r, "void main(inout half4 color) { color.bgr = int3(5, 6, 7); }", 1, 2, 3, 4, 7, 6,
553 5, 4);
554}
555
556DEF_TEST(SkSLInterpreterGlobal, r) {
557 test(r, "int x; void main(inout half4 color) { x = 10; color.b = x; }", 1, 2, 3, 4, 1, 2, 10,
558 4);
Brian Osmanb7451292019-05-15 13:02:13 -0400559 test(r, "float4 x; void main(inout float4 color) { x = color * 2; color = x; }",
560 1, 2, 3, 4, 2, 4, 6, 8);
561 test(r, "float4 x; void main(inout float4 color) { x = float4(5, 6, 7, 8); color = x.wzyx; }",
562 1, 2, 3, 4, 8, 7, 6, 5);
Brian Osman1091f022019-05-16 09:42:16 -0400563 test(r, "float4 x; void main(inout float4 color) { x.wzyx = float4(5, 6, 7, 8); color = x; }",
564 1, 2, 3, 4, 8, 7, 6, 5);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400565}
Ethan Nicholas746035a2019-04-23 13:31:09 -0400566
567DEF_TEST(SkSLInterpreterGeneric, r) {
568 float value1 = 5;
569 float expected1 = 25;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400570 test(r, "float main(float x) { return x * x; }", &value1, &expected1);
Ethan Nicholas746035a2019-04-23 13:31:09 -0400571 float value2[2] = { 5, 25 };
572 float expected2[2] = { 25, 625 };
Brian Osmanb23d66e2019-09-27 10:25:57 -0400573 test(r, "float2 main(float x, float y) { return float2(x * x, y * y); }", value2, expected2);
Ethan Nicholas746035a2019-04-23 13:31:09 -0400574}
Brian Osmand369a5e2019-05-09 13:13:25 -0400575
Brian Osman07c117b2019-05-23 12:51:06 -0700576DEF_TEST(SkSLInterpreterCompound, r) {
577 struct RectAndColor { SkIRect fRect; SkColor4f fColor; };
578 struct ManyRects { int fNumRects; RectAndColor fRects[4]; };
579
580 const char* src =
581 // Some struct definitions
582 "struct Point { int x; int y; };\n"
583 "struct Rect { Point p0; Point p1; };\n"
584 "struct RectAndColor { Rect r; float4 color; };\n"
585
586 // Structs as globals, parameters, return values
587 "RectAndColor temp;\n"
588 "int rect_height(Rect r) { return r.p1.y - r.p0.y; }\n"
589 "RectAndColor make_blue_rect(int w, int h) {\n"
590 " temp.r.p0.x = temp.r.p0.y = 0;\n"
591 " temp.r.p1.x = w; temp.r.p1.y = h;\n"
592 " temp.color = float4(0, 1, 0, 1);\n"
593 " return temp;\n"
594 "}\n"
595
596 // Initialization and assignment of types larger than 4 slots
597 "RectAndColor init_big(RectAndColor r) { RectAndColor s = r; return s; }\n"
598 "RectAndColor copy_big(RectAndColor r) { RectAndColor s; s = r; return s; }\n"
599
600 // Same for arrays, including some non-constant indexing
601 "float tempFloats[8];\n"
602 "int median(int a[15]) { return a[7]; }\n"
603 "float[8] sums(float a[8]) {\n"
604 " float tempFloats[8];\n"
605 " tempFloats[0] = a[0];\n"
606 " for (int i = 1; i < 8; ++i) { tempFloats[i] = tempFloats[i - 1] + a[i]; }\n"
607 " return tempFloats;\n"
608 "}\n"
609
610 // Uniforms, array-of-structs, dynamic indices
Ethan Nicholas31cff272019-09-26 13:04:48 -0400611 "uniform Rect gRects[4];\n"
Brian Osman07c117b2019-05-23 12:51:06 -0700612 "Rect get_rect(int i) { return gRects[i]; }\n"
613
614 // Kitchen sink (swizzles, inout, SoAoS)
615 "struct ManyRects { int numRects; RectAndColor rects[4]; };\n"
616 "void fill_rects(inout ManyRects mr) {\n"
617 " for (int i = 0; i < mr.numRects; ++i) {\n"
618 " mr.rects[i].r = gRects[i];\n"
619 " float b = mr.rects[i].r.p1.y;\n"
620 " mr.rects[i].color = float4(b, b, b, b);\n"
621 " }\n"
622 "}\n";
623
624 SkSL::Compiler compiler;
625 SkSL::Program::Settings settings;
626 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(
627 SkSL::Program::kGeneric_Kind,
628 SkSL::String(src), settings);
629 REPORTER_ASSERT(r, program);
630
631 std::unique_ptr<SkSL::ByteCode> byteCode = compiler.toByteCode(*program);
632 REPORTER_ASSERT(r, !compiler.errorCount());
633
634 auto rect_height = byteCode->getFunction("rect_height"),
635 make_blue_rect = byteCode->getFunction("make_blue_rect"),
636 median = byteCode->getFunction("median"),
637 sums = byteCode->getFunction("sums"),
638 get_rect = byteCode->getFunction("get_rect"),
639 fill_rects = byteCode->getFunction("fill_rects");
640
641 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 -0400642 const float* fRects = (const float*)gRects;
Brian Osman07c117b2019-05-23 12:51:06 -0700643
Brian Osman07c117b2019-05-23 12:51:06 -0700644 {
645 SkIRect in = SkIRect::MakeXYWH(10, 10, 20, 30);
Ben Wagner470e0ac2020-01-22 16:59:21 -0500646 int out = 0;
647 SkAssertResult(byteCode->run(rect_height, (float*)&in, 4, (float*)&out, 1, fRects, 16));
648 REPORTER_ASSERT(r, out == 30);
Brian Osman07c117b2019-05-23 12:51:06 -0700649 }
650
651 {
652 int in[2] = { 15, 25 };
Ben Wagner470e0ac2020-01-22 16:59:21 -0500653 RectAndColor out;
654 SkAssertResult(byteCode->run(make_blue_rect, (float*)in, 2, (float*)&out, 8, fRects, 16));
655 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 };
Ben Wagner470e0ac2020-01-22 16:59:21 -0500658 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 };
Ben Wagner470e0ac2020-01-22 16:59:21 -0500663 int out = 0;
664 SkAssertResult(byteCode->run(median, (float*)in, 15, (float*)&out, 1, fRects, 16));
665 REPORTER_ASSERT(r, out == 8);
Brian Osman07c117b2019-05-23 12:51:06 -0700666 }
667
668 {
669 float in[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
Ben Wagner470e0ac2020-01-22 16:59:21 -0500670 float out[8] = { 0 };
671 SkAssertResult(byteCode->run(sums, in, 8, out, 8, fRects, 16));
Brian Osman07c117b2019-05-23 12:51:06 -0700672 for (int i = 0; i < 8; ++i) {
Ben Wagner470e0ac2020-01-22 16:59:21 -0500673 REPORTER_ASSERT(r, out[i] == static_cast<float>((i + 1) * (i + 2) / 2));
Brian Osman07c117b2019-05-23 12:51:06 -0700674 }
675 }
676
677 {
678 int in = 2;
Ben Wagner470e0ac2020-01-22 16:59:21 -0500679 SkIRect out = SkIRect::MakeEmpty();
680 SkAssertResult(byteCode->run(get_rect, (float*)&in, 1, (float*)&out, 4, fRects, 16));
681 REPORTER_ASSERT(r, out == gRects[2]);
Brian Osman07c117b2019-05-23 12:51:06 -0700682 }
683
684 {
685 ManyRects in;
686 memset(&in, 0, sizeof(in));
687 in.fNumRects = 2;
Ben Wagner470e0ac2020-01-22 16:59:21 -0500688 SkAssertResult(byteCode->run(fill_rects, (float*)&in, 33, nullptr, 0, fRects, 16));
Brian Osman07c117b2019-05-23 12:51:06 -0700689 ManyRects expected;
690 memset(&expected, 0, sizeof(expected));
691 expected.fNumRects = 2;
692 for (int i = 0; i < 2; ++i) {
693 expected.fRects[i].fRect = gRects[i];
694 float c = gRects[i].fBottom;
695 expected.fRects[i].fColor = { c, c, c, c };
696 }
697 REPORTER_ASSERT(r, memcmp(&in, &expected, sizeof(in)) == 0);
698 }
699}
Ethan Nicholas91164d12019-05-15 15:29:54 -0400700
Brian Osman869a3e82019-07-18 17:00:34 -0400701static void expect_failure(skiatest::Reporter* r, const char* src) {
702 SkSL::Compiler compiler;
703 auto program = compiler.convertProgram(SkSL::Program::kGeneric_Kind, SkSL::String(src),
704 SkSL::Program::Settings());
705 REPORTER_ASSERT(r, program);
706
707 auto byteCode = compiler.toByteCode(*program);
708 REPORTER_ASSERT(r, compiler.errorCount() > 0);
709 REPORTER_ASSERT(r, !byteCode);
710}
711
712static void expect_run_failure(skiatest::Reporter* r, const char* src, float* in) {
713 SkSL::Compiler compiler;
714 auto program = compiler.convertProgram(SkSL::Program::kGeneric_Kind, SkSL::String(src),
715 SkSL::Program::Settings());
716 REPORTER_ASSERT(r, program);
717
718 auto byteCode = compiler.toByteCode(*program);
719 REPORTER_ASSERT(r, byteCode);
720
Ben Wagner470e0ac2020-01-22 16:59:21 -0500721 auto fun = byteCode->getFunction("main");
722 bool result = byteCode->run(fun, in, fun->getParameterCount(), nullptr, 0, nullptr, 0);
723 REPORTER_ASSERT(r, !result);
Brian Osman869a3e82019-07-18 17:00:34 -0400724}
725
Brian Osman6f5358f2019-07-09 14:17:23 -0400726DEF_TEST(SkSLInterpreterRestrictFunctionCalls, r) {
Brian Osman6f5358f2019-07-09 14:17:23 -0400727 // Ensure that simple recursion is not allowed
Brian Osman869a3e82019-07-18 17:00:34 -0400728 expect_failure(r, "float main() { return main() + 1; }");
Brian Osman6f5358f2019-07-09 14:17:23 -0400729
730 // Ensure that calls to undefined functions are not allowed (to prevent mutual recursion)
Brian Osman869a3e82019-07-18 17:00:34 -0400731 expect_failure(r, "float foo(); float bar() { return foo(); } float foo() { return bar(); }");
Brian Osman4a47da72019-07-12 11:30:32 -0400732
733 // returns are not allowed inside conditionals (or loops, which are effectively the same thing)
Brian Osman869a3e82019-07-18 17:00:34 -0400734 expect_failure(r, "float main(float x, float y) { if (x < y) { return x; } return y; }");
735 expect_failure(r, "float main(float x) { while (x > 1) { return x; } return 0; }");
736}
737
738DEF_TEST(SkSLInterpreterArrayBounds, r) {
739 // Out of bounds array access at compile time
740 expect_failure(r, "float main(float x[4]) { return x[-1]; }");
741 expect_failure(r, "float2 main(float2 x[2]) { return x[2]; }");
742
743 // Out of bounds array access at runtime is pinned, and we don't update any inout data
744 float in[3] = { -1.0f, 1.0f, 2.0f };
745 expect_run_failure(r, "void main(inout float data[3]) { data[int(data[0])] = 0; }", in);
746 REPORTER_ASSERT(r, in[0] == -1.0f && in[1] == 1.0f && in[2] == 2.0f);
747
748 in[0] = 3.0f;
749 expect_run_failure(r, "void main(inout float data[3]) { data[int(data[0])] = 0; }", in);
750 REPORTER_ASSERT(r, in[0] == 3.0f && in[1] == 1.0f && in[2] == 2.0f);
Brian Osman6f5358f2019-07-09 14:17:23 -0400751}
752
Brian Osman226668a2019-05-14 16:47:30 -0400753DEF_TEST(SkSLInterpreterFunctions, r) {
754 const char* src =
755 "float sqr(float x) { return x * x; }\n"
Brian Osman226668a2019-05-14 16:47:30 -0400756 "float sub(float x, float y) { return x - y; }\n"
Brian Osman6f5358f2019-07-09 14:17:23 -0400757 "float main(float x) { return sub(sqr(x), x); }\n"
Brian Osman226668a2019-05-14 16:47:30 -0400758
759 // Different signatures
760 "float dot(float2 a, float2 b) { return a.x*b.x + a.y*b.y; }\n"
761 "float dot(float3 a, float3 b) { return a.x*b.x + a.y*b.y + a.z*b.z; }\n"
762 "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 -0400763 "float dot2_test(float x) { return dot(float2(x, x + 1), float2(1, -1)); }\n";
Brian Osman226668a2019-05-14 16:47:30 -0400764
765 SkSL::Compiler compiler;
766 SkSL::Program::Settings settings;
767 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(
768 SkSL::Program::kGeneric_Kind,
769 SkSL::String(src), settings);
770 REPORTER_ASSERT(r, program);
771
772 std::unique_ptr<SkSL::ByteCode> byteCode = compiler.toByteCode(*program);
773 REPORTER_ASSERT(r, !compiler.errorCount());
774
775 auto sub = byteCode->getFunction("sub");
776 auto sqr = byteCode->getFunction("sqr");
777 auto main = byteCode->getFunction("main");
778 auto tan = byteCode->getFunction("tan");
779 auto dot3 = byteCode->getFunction("dot3_test");
780 auto dot2 = byteCode->getFunction("dot2_test");
Brian Osman226668a2019-05-14 16:47:30 -0400781
782 REPORTER_ASSERT(r, sub);
783 REPORTER_ASSERT(r, sqr);
784 REPORTER_ASSERT(r, main);
785 REPORTER_ASSERT(r, !tan);
786 REPORTER_ASSERT(r, dot3);
787 REPORTER_ASSERT(r, dot2);
Brian Osman226668a2019-05-14 16:47:30 -0400788
Ben Wagner470e0ac2020-01-22 16:59:21 -0500789 float out = 0.0f;
Brian Osman226668a2019-05-14 16:47:30 -0400790 float in = 3.0f;
Ben Wagner470e0ac2020-01-22 16:59:21 -0500791 SkAssertResult(byteCode->run(main, &in, 1, &out, 1, nullptr, 0));
792 REPORTER_ASSERT(r, out = 6.0f);
Brian Osman226668a2019-05-14 16:47:30 -0400793
Ben Wagner470e0ac2020-01-22 16:59:21 -0500794 SkAssertResult(byteCode->run(dot3, &in, 1, &out, 1, nullptr, 0));
795 REPORTER_ASSERT(r, out = 9.0f);
Brian Osman226668a2019-05-14 16:47:30 -0400796
Ben Wagner470e0ac2020-01-22 16:59:21 -0500797 SkAssertResult(byteCode->run(dot2, &in, 1, &out, 1, nullptr, 0));
798 REPORTER_ASSERT(r, out = -1.0f);
Brian Osman226668a2019-05-14 16:47:30 -0400799}
Ethan Nicholas91164d12019-05-15 15:29:54 -0400800
Brian Osmand3494ed2019-06-20 15:41:34 -0400801DEF_TEST(SkSLInterpreterOutParams, r) {
802 test(r,
803 "void oneAlpha(inout half4 color) { color.a = 1; }"
804 "void main(inout half4 color) { oneAlpha(color); }",
805 0, 0, 0, 0, 0, 0, 0, 1);
806 test(r,
Ben Wagner470e0ac2020-01-22 16:59:21 -0500807 "half2 tricky(half x, half y, inout half2 color, half z) {"
Brian Osmand3494ed2019-06-20 15:41:34 -0400808 " color.xy = color.yx;"
809 " return half2(x + y, z);"
810 "}"
811 "void main(inout half4 color) {"
Ben Wagner470e0ac2020-01-22 16:59:21 -0500812 " half2 t = tricky(1, 2, color.rb, 5);"
Brian Osmand3494ed2019-06-20 15:41:34 -0400813 " color.ga = t;"
814 "}",
Ben Wagner470e0ac2020-01-22 16:59:21 -0500815 1, 2, 3, 4, 3, 3, 1, 5);
Brian Osmand3494ed2019-06-20 15:41:34 -0400816}
817
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400818DEF_TEST(SkSLInterpreterMathFunctions, r) {
Brian Osmanb380e712019-07-24 17:02:39 -0400819 float value[4], expected[4];
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400820
Brian Osmanb380e712019-07-24 17:02:39 -0400821 value[0] = 0.0f; expected[0] = 0.0f;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400822 test(r, "float main(float x) { return sin(x); }", value, expected);
823 test(r, "float main(float x) { return tan(x); }", value, expected);
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400824
Brian Osmanb380e712019-07-24 17:02:39 -0400825 value[0] = 0.0f; expected[0] = 1.0f;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400826 test(r, "float main(float x) { return cos(x); }", value, expected);
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400827
Brian Osmanb380e712019-07-24 17:02:39 -0400828 value[0] = 25.0f; expected[0] = 5.0f;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400829 test(r, "float main(float x) { return sqrt(x); }", value, expected);
Brian Osmanb380e712019-07-24 17:02:39 -0400830
831 value[0] = 90.0f; expected[0] = sk_float_degrees_to_radians(value[0]);
Brian Osmanb23d66e2019-09-27 10:25:57 -0400832 test(r, "float main(float x) { return radians(x); }", value, expected);
Brian Osmanb380e712019-07-24 17:02:39 -0400833
834 value[0] = 1.0f; value[1] = -1.0f;
835 expected[0] = 1.0f / SK_FloatSqrt2; expected[1] = -1.0f / SK_FloatSqrt2;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400836 test(r, "float2 main(float2 x) { return normalize(x); }", value, expected);
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400837}
838
Brian Osmanfba386b2019-06-20 14:54:15 -0400839DEF_TEST(SkSLInterpreterVoidFunction, r) {
840 test(r,
841 "half x; void foo() { x = 1.0; }"
842 "void main(inout half4 color) { foo(); color.r = x; }",
843 0, 0, 0, 0, 1, 0, 0, 0);
844}
845
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400846DEF_TEST(SkSLInterpreterMix, r) {
Brian Osman08a84962019-06-14 10:17:16 -0400847 float value, expected;
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400848
849 value = 0.5f; expected = 0.0f;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400850 test(r, "float main(float x) { return mix(-10, 10, x); }", &value, &expected);
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400851 value = 0.75f; expected = 5.0f;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400852 test(r, "float main(float x) { return mix(-10, 10, x); }", &value, &expected);
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400853 value = 2.0f; expected = 30.0f;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400854 test(r, "float main(float x) { return mix(-10, 10, x); }", &value, &expected);
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400855
Brian Osman08a84962019-06-14 10:17:16 -0400856 float valueVectors[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f },
857 expectedVector[] = { 3.0f, 4.0f, 5.0f, 6.0f };
Brian Osmanb23d66e2019-09-27 10:25:57 -0400858 test(r, "float4 main(float4 x, float4 y) { return mix(x, y, 0.5); }", valueVectors,
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400859 expectedVector);
860}
861
862DEF_TEST(SkSLInterpreterCross, r) {
Brian Osman08a84962019-06-14 10:17:16 -0400863 float args[] = { 1.0f, 4.0f, -6.0f, -2.0f, 7.0f, -3.0f };
Mike Reedb26b4e72020-01-22 14:31:21 -0500864 SkV3 cross = SkV3::Cross({args[0], args[1], args[2]},
865 {args[3], args[4], args[5]});
866 float expected[] = { cross.x, cross.y, cross.z };
Brian Osmanb23d66e2019-09-27 10:25:57 -0400867 test(r, "float3 main(float3 x, float3 y) { return cross(x, y); }", args, expected);
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400868}
Ben Wagner470e0ac2020-01-22 16:59:21 -0500869
Mike Reed634c9412019-07-18 13:20:04 -0400870DEF_TEST(SkSLInterpreterInverse, r) {
871 {
872 SkMatrix m;
873 m.setRotate(30).postScale(1, 2);
874 float args[4] = { m[0], m[3], m[1], m[4] };
875 SkAssertResult(m.invert(&m));
876 float expt[4] = { m[0], m[3], m[1], m[4] };
Ben Wagner470e0ac2020-01-22 16:59:21 -0500877 test(r, "float2x2 main(float2x2 m) { return inverse(m); }", args, expt, false);
Mike Reed634c9412019-07-18 13:20:04 -0400878 }
879 {
880 SkMatrix m;
881 m.setRotate(30).postScale(1, 2).postTranslate(1, 2);
882 float args[9] = { m[0], m[3], m[6], m[1], m[4], m[7], m[2], m[5], m[8] };
883 SkAssertResult(m.invert(&m));
884 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 -0500885 test(r, "float3x3 main(float3x3 m) { return inverse(m); }", args, expt, false);
Mike Reed634c9412019-07-18 13:20:04 -0400886 }
887 {
888 float args[16], expt[16];
Mike Reed634c9412019-07-18 13:20:04 -0400889 // just some crazy thing that is invertible
Mike Reedb26b4e72020-01-22 14:31:21 -0500890 SkM44 m = {1, 2, 3, 4, 1, 2, 0, 3, 1, 0, 1, 4, 1, 3, 2, 0};
891 m.getColMajor(args);
Mike Reed634c9412019-07-18 13:20:04 -0400892 SkAssertResult(m.invert(&m));
Ben Wagner470e0ac2020-01-22 16:59:21 -0500893 m.getColMajor(expt);
894 test(r, "float4x4 main(float4x4 m) { return inverse(m); }", args, expt, false);
895 }
896}
897
898DEF_TEST(SkSLInterpreterDot, r) {
899 float args[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f };
900 float expected = args[0] * args[2] +
901 args[1] * args[3];
902 test(r, "float main(float2 x, float2 y) { return dot(x, y); }", args, &expected);
903
904 expected = args[0] * args[3] +
905 args[1] * args[4] +
906 args[2] * args[5];
907 test(r, "float main(float3 x, float3 y) { return dot(x, y); }", args, &expected);
908
909 expected = args[0] * args[4] +
910 args[1] * args[5] +
911 args[2] * args[6] +
912 args[3] * args[7];
913 test(r, "float main(float4 x, float4 y) { return dot(x, y); }", args, &expected);
914}
915
916static const SkSL::Type& type_of(const skjson::Value* value, SkSL::Compiler* compiler) {
917 switch (value->getType()) {
918 case skjson::Value::Type::kNumber: {
919 float f = *value->as<skjson::NumberValue>();
920 if (f == (float) (int) f) {
921 return *compiler->context().fInt_Type;
922 }
923 return *compiler->context().fFloat_Type;
924 }
925 case skjson::Value::Type::kBool:
926 return *compiler->context().fBool_Type;
927 default:
928 return *compiler->context().fVoid_Type;
929 }
930}
931
932class JSONExternalValue : public SkSL::ExternalValue {
933public:
934 JSONExternalValue(const char* name, const skjson::Value* value, SkSL::Compiler* compiler)
935 : INHERITED(name, type_of(value, compiler))
936 , fValue(*value)
937 , fCompiler(*compiler) {}
938
939 bool canRead() const override {
940 return type() != *fCompiler.context().fVoid_Type;
941 }
942
943 void read(int /*unusedIndex*/, float* target) override {
944 if (type() == *fCompiler.context().fInt_Type) {
945 *(int*) target = *fValue.as<skjson::NumberValue>();
946 } else if (type() == *fCompiler.context().fFloat_Type) {
947 *(float*) target = *fValue.as<skjson::NumberValue>();
948 } else if (type() == *fCompiler.context().fBool_Type) {
949 // ByteCode "booleans" are actually bit-masks
950 *(int*) target = *fValue.as<skjson::BoolValue>() ? ~0 : 0;
951 } else {
952 SkASSERT(false);
953 }
954 }
955
956 SkSL::ExternalValue* getChild(const char* name) const override {
957 if (fValue.getType() == skjson::Value::Type::kObject) {
958 const skjson::Value& v = fValue.as<skjson::ObjectValue>()[name];
959 return (SkSL::ExternalValue*) fCompiler.takeOwnership(std::unique_ptr<Symbol>(
960 new JSONExternalValue(name, &v, &fCompiler)));
961 }
962 return nullptr;
963 }
964
965private:
966 const skjson::Value& fValue;
967 SkSL::Compiler& fCompiler;
968
969 typedef SkSL::ExternalValue INHERITED;
970};
971
972class PointerExternalValue : public SkSL::ExternalValue {
973public:
974 PointerExternalValue(const char* name, const SkSL::Type& type, void* data, size_t size)
975 : INHERITED(name, type)
976 , fData(data)
977 , fSize(size) {}
978
979 bool canRead() const override {
980 return true;
981 }
982
983 bool canWrite() const override {
984 return true;
985 }
986
987 void read(int /*unusedIndex*/, float* target) override {
988 memcpy(target, fData, fSize);
989 }
990
991 void write(int /*unusedIndex*/, float* src) override {
992 memcpy(fData, src, fSize);
993 }
994
995
996private:
997 void* fData;
998 size_t fSize;
999
1000 typedef SkSL::ExternalValue INHERITED;
1001};
1002
1003DEF_TEST(SkSLInterpreterExternalValues, r) {
1004 const char* json = "{ \"value1\": 12, \"child\": { \"value2\": true, \"value3\": 5.5 } }";
1005 skjson::DOM dom(json, strlen(json));
1006 SkSL::Compiler compiler;
1007 SkSL::Program::Settings settings;
1008 const char* src = "float main() {"
1009 " outValue = 152;"
1010 " return root.child.value2 ? root.value1 * root.child.value3 : -1;"
1011 "}";
1012 compiler.registerExternalValue((SkSL::ExternalValue*) compiler.takeOwnership(
1013 std::unique_ptr<SkSL::Symbol>(new JSONExternalValue("root", &dom.root(), &compiler))));
1014 int32_t outValue = -1;
1015 compiler.registerExternalValue((SkSL::ExternalValue*) compiler.takeOwnership(
1016 std::unique_ptr<SkSL::Symbol>(new PointerExternalValue("outValue",
1017 *compiler.context().fInt_Type,
1018 &outValue,
1019 sizeof(outValue)))));
1020 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(
1021 SkSL::Program::kGeneric_Kind,
1022 SkSL::String(src), settings);
1023 REPORTER_ASSERT(r, program);
1024 if (program) {
1025 std::unique_ptr<SkSL::ByteCode> byteCode = compiler.toByteCode(*program);
1026 REPORTER_ASSERT(r, !compiler.errorCount());
1027 if (compiler.errorCount() > 0) {
1028 printf("%s\n%s", src, compiler.errorText().c_str());
1029 return;
1030 }
1031 const SkSL::ByteCodeFunction* main = byteCode->getFunction("main");
1032 float out;
1033 SkAssertResult(byteCode->run(main, nullptr, 0, &out, 1, nullptr, 0));
1034 REPORTER_ASSERT(r, out == 66.0);
1035 REPORTER_ASSERT(r, outValue == 152);
1036 } else {
1037 printf("%s\n%s", src, compiler.errorText().c_str());
1038 }
1039}
1040
1041DEF_TEST(SkSLInterpreterExternalValuesVector, r) {
1042 SkSL::Compiler compiler;
1043 SkSL::Program::Settings settings;
1044 const char* src = "void main() {"
1045 " value *= 2;"
1046 "}";
1047 int32_t value[4] = { 1, 2, 3, 4 };
1048 compiler.registerExternalValue((SkSL::ExternalValue*) compiler.takeOwnership(
1049 std::unique_ptr<SkSL::Symbol>(new PointerExternalValue("value",
1050 *compiler.context().fInt4_Type,
1051 value,
1052 sizeof(value)))));
1053 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(SkSL::Program::kGeneric_Kind,
1054 SkSL::String(src),
1055 settings);
1056 REPORTER_ASSERT(r, program);
1057 if (program) {
1058 std::unique_ptr<SkSL::ByteCode> byteCode = compiler.toByteCode(*program);
1059 REPORTER_ASSERT(r, !compiler.errorCount());
1060 if (compiler.errorCount() > 0) {
1061 printf("%s\n%s", src, compiler.errorText().c_str());
1062 return;
1063 }
1064 const SkSL::ByteCodeFunction* main = byteCode->getFunction("main");
1065 SkAssertResult(byteCode->run(main, nullptr, 0, nullptr, 0, nullptr, 0));
1066 REPORTER_ASSERT(r, value[0] == 2);
1067 REPORTER_ASSERT(r, value[1] == 4);
1068 REPORTER_ASSERT(r, value[2] == 6);
1069 REPORTER_ASSERT(r, value[3] == 8);
1070 } else {
1071 printf("%s\n%s", src, compiler.errorText().c_str());
1072 }
1073}
1074
1075class FunctionExternalValue : public SkSL::ExternalValue {
1076public:
1077 FunctionExternalValue(const char* name, float(*function)(float), SkSL::Compiler& compiler)
1078 : INHERITED(name, *compiler.context().fFloat_Type)
1079 , fCompiler(compiler)
1080 , fFunction(function) {}
1081
1082 bool canCall() const override {
1083 return true;
1084 }
1085
1086 int callParameterCount() const override {
1087 return 1;
1088 }
1089
1090 void getCallParameterTypes(const SkSL::Type** outTypes) const override {
1091 outTypes[0] = fCompiler.context().fFloat_Type.get();
1092 }
1093
1094 void call(int /*unusedIndex*/, float* arguments, float* outReturn) override {
1095 outReturn[0] = fFunction(arguments[0]);
1096 }
1097
1098private:
1099 SkSL::Compiler& fCompiler;
1100
1101 float (*fFunction)(float);
1102
1103 typedef SkSL::ExternalValue INHERITED;
1104};
1105
1106DEF_TEST(SkSLInterpreterExternalValuesCall, r) {
1107 SkSL::Compiler compiler;
1108 SkSL::Program::Settings settings;
1109 const char* src = "float main() {"
1110 " return external(25);"
1111 "}";
1112 compiler.registerExternalValue((SkSL::ExternalValue*) compiler.takeOwnership(
1113 std::unique_ptr<SkSL::Symbol>(new FunctionExternalValue("external",
1114 [] (float x) {
1115 return (float) sqrt(x);
1116 },
1117 compiler))));
1118 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(SkSL::Program::kGeneric_Kind,
1119 SkSL::String(src),
1120 settings);
1121 REPORTER_ASSERT(r, program);
1122 if (program) {
1123 std::unique_ptr<SkSL::ByteCode> byteCode = compiler.toByteCode(*program);
1124 REPORTER_ASSERT(r, !compiler.errorCount());
1125 if (compiler.errorCount() > 0) {
1126 printf("%s\n%s", src, compiler.errorText().c_str());
1127 return;
1128 }
1129 const SkSL::ByteCodeFunction* main = byteCode->getFunction("main");
1130 float out;
1131 SkAssertResult(byteCode->run(main, nullptr, 0, &out, 1, nullptr, 0));
1132 REPORTER_ASSERT(r, out == 5.0);
1133 } else {
1134 printf("%s\n%s", src, compiler.errorText().c_str());
1135 }
1136}
1137
1138class VectorFunctionExternalValue : public SkSL::ExternalValue {
1139public:
1140 VectorFunctionExternalValue(const char* name, void(*function)(float[4], float[4]),
1141 SkSL::Compiler& compiler)
1142 : INHERITED(name, *compiler.context().fFloat4_Type)
1143 , fCompiler(compiler)
1144 , fFunction(function) {}
1145
1146 bool canCall() const override {
1147 return true;
1148 }
1149
1150 int callParameterCount() const override {
1151 return 1;
1152 }
1153
1154 void getCallParameterTypes(const SkSL::Type** outTypes) const override {
1155 outTypes[0] = fCompiler.context().fFloat4_Type.get();
1156 }
1157
1158 void call(int /*unusedIndex*/, float* arguments, float* outReturn) override {
1159 fFunction(arguments, outReturn);
1160 }
1161
1162private:
1163 SkSL::Compiler& fCompiler;
1164
1165 void (*fFunction)(float[4], float[4]);
1166
1167 typedef SkSL::ExternalValue INHERITED;
1168};
1169
1170
1171DEF_TEST(SkSLInterpreterExternalValuesVectorCall, r) {
1172 SkSL::Compiler compiler;
1173 SkSL::Program::Settings settings;
1174 const char* src = "float4 main() {"
1175 " return external(float4(1, 4, 9, 16));"
1176 "}";
1177 compiler.registerExternalValue((SkSL::ExternalValue*) compiler.takeOwnership(
1178 std::unique_ptr<SkSL::Symbol>(new VectorFunctionExternalValue("external",
1179 [] (float in[4], float out[4]) {
1180 out[0] = sqrt(in[0]);
1181 out[1] = sqrt(in[1]);
1182 out[2] = sqrt(in[2]);
1183 out[3] = sqrt(in[3]);
1184 },
1185 compiler))));
1186 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(SkSL::Program::kGeneric_Kind,
1187 SkSL::String(src),
1188 settings);
1189 REPORTER_ASSERT(r, program);
1190 if (program) {
1191 std::unique_ptr<SkSL::ByteCode> byteCode = compiler.toByteCode(*program);
1192 REPORTER_ASSERT(r, !compiler.errorCount());
1193 if (compiler.errorCount() > 0) {
1194 printf("%s\n%s", src, compiler.errorText().c_str());
1195 return;
1196 }
1197 const SkSL::ByteCodeFunction* main = byteCode->getFunction("main");
1198 float out[4];
1199 SkAssertResult(byteCode->run(main, nullptr, 0, out, 4, nullptr, 0));
1200 REPORTER_ASSERT(r, out[0] == 1.0);
1201 REPORTER_ASSERT(r, out[1] == 2.0);
1202 REPORTER_ASSERT(r, out[2] == 3.0);
1203 REPORTER_ASSERT(r, out[3] == 4.0);
1204 } else {
1205 printf("%s\n%s", src, compiler.errorText().c_str());
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001206 }
1207}