blob: 0b2cc49c0a756808e3a1c18986b8b18f1ab247d2 [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
Ethan Nicholasae9633b2019-05-24 12:46:34 -04008#include "include/core/SkPoint3.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
Brian Osman08a84962019-06-14 10:17:16 -040016void test(skiatest::Reporter* r, const char* src, float* in, int expectedCount, float* expected) {
Ethan Nicholas746035a2019-04-23 13:31:09 -040017 SkSL::Compiler compiler;
18 SkSL::Program::Settings settings;
19 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(
20 SkSL::Program::kGeneric_Kind,
21 SkSL::String(src), settings);
22 REPORTER_ASSERT(r, program);
23 if (program) {
24 std::unique_ptr<SkSL::ByteCode> byteCode = compiler.toByteCode(*program);
Brian Osman80164412019-06-07 13:00:23 -040025 program.reset();
Ethan Nicholas746035a2019-04-23 13:31:09 -040026 REPORTER_ASSERT(r, !compiler.errorCount());
27 if (compiler.errorCount() > 0) {
28 printf("%s\n%s", src, compiler.errorText().c_str());
29 return;
30 }
31 SkSL::ByteCodeFunction* main = byteCode->fFunctions[0].get();
Brian Osman08a84962019-06-14 10:17:16 -040032 std::unique_ptr<float[]> out = std::unique_ptr<float[]>(new float[expectedCount]);
33 byteCode->run(main, in, out.get(), 1, nullptr, 0);
34 bool valid = !memcmp(out.get(), expected, sizeof(float) * expectedCount);
Ethan Nicholas746035a2019-04-23 13:31:09 -040035 if (!valid) {
36 printf("for program: %s\n", src);
37 printf(" expected (");
38 const char* separator = "";
39 for (int i = 0; i < expectedCount; ++i) {
Brian Osman08a84962019-06-14 10:17:16 -040040 printf("%s%f", separator, expected[i]);
Ethan Nicholas746035a2019-04-23 13:31:09 -040041 separator = ", ";
42 }
43 printf("), but received (");
44 separator = "";
45 for (int i = 0; i < expectedCount; ++i) {
Brian Osman08a84962019-06-14 10:17:16 -040046 printf("%s%f", separator, out.get()[i]);
Ethan Nicholas746035a2019-04-23 13:31:09 -040047 separator = ", ";
48 }
49 printf(")\n");
Brian Osman08a84962019-06-14 10:17:16 -040050 main->disassemble();
Ethan Nicholas746035a2019-04-23 13:31:09 -040051 }
52 REPORTER_ASSERT(r, valid);
53 } else {
54 printf("%s\n%s", src, compiler.errorText().c_str());
55 }
56}
57
Brian Osman569f12f2019-06-13 11:23:57 -040058void vec_test(skiatest::Reporter* r, const char* src) {
59 // Test on four different vectors (with varying orderings to get divergent control flow)
60 const float input[16] = { 1, 2, 3, 4,
61 4, 3, 2, 1,
62 7, 5, 8, 6,
63 6, 8, 5, 7 };
64
Brian Osman08a84962019-06-14 10:17:16 -040065 SkSL::Compiler compiler;
66 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(
67 SkSL::Program::kGeneric_Kind, SkSL::String(src), SkSL::Program::Settings());
68 if (!program) {
69 REPORT_FAILURE(r, "!program", SkString(compiler.errorText().c_str()));
70 return;
71 }
72
73 std::unique_ptr<SkSL::ByteCode> byteCode = compiler.toByteCode(*program);
74 if (compiler.errorCount() > 0) {
75 REPORT_FAILURE(r, "!toByteCode", SkString(compiler.errorText().c_str()));
76 return;
77 }
78
79 const SkSL::ByteCodeFunction* main = byteCode->getFunction("main");
80
Brian Osman569f12f2019-06-13 11:23:57 -040081 float out_s[16], out_v[16];
82 memcpy(out_s, input, sizeof(out_s));
83 memcpy(out_v, input, sizeof(out_v));
84
Brian Osman08a84962019-06-14 10:17:16 -040085 // First run in scalar mode to determine the expected output
86 for (int i = 0; i < 4; ++i) {
87 byteCode->run(main, out_s + i * 4, nullptr, 1, nullptr, 0);
88 }
Brian Osman569f12f2019-06-13 11:23:57 -040089
Brian Osman08a84962019-06-14 10:17:16 -040090 // Now run in parallel and compare results
91 byteCode->run(main, out_v, nullptr, 4, nullptr, 0);
92 if (memcmp(out_s, out_v, sizeof(out_s)) != 0) {
93 printf("for program: %s\n", src);
94 for (int i = 0; i < 4; ++i) {
95 printf("(%g %g %g %g) -> (%g %g %g %g), expected (%g %g %g %g)\n",
96 input[4*i + 0], input[4*i + 1], input[4*i + 2], input[4*i + 3],
97 out_v[4*i + 0], out_v[4*i + 1], out_v[4*i + 2], out_v[4*i + 3],
98 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 -040099 }
Brian Osman08a84962019-06-14 10:17:16 -0400100 main->disassemble();
101 REPORT_FAILURE(r, "VecInterpreter mismatch", SkString());
Brian Osman569f12f2019-06-13 11:23:57 -0400102 }
103}
104
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400105void test(skiatest::Reporter* r, const char* src, float inR, float inG, float inB, float inA,
Brian Osman08a84962019-06-14 10:17:16 -0400106 float expectedR, float expectedG, float expectedB, float expectedA) {
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400107 SkSL::Compiler compiler;
108 SkSL::Program::Settings settings;
109 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(
Ethan Nicholas746035a2019-04-23 13:31:09 -0400110 SkSL::Program::kGeneric_Kind,
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400111 SkSL::String(src), settings);
112 REPORTER_ASSERT(r, program);
113 if (program) {
114 std::unique_ptr<SkSL::ByteCode> byteCode = compiler.toByteCode(*program);
Brian Osman80164412019-06-07 13:00:23 -0400115 program.reset();
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400116 REPORTER_ASSERT(r, !compiler.errorCount());
117 if (compiler.errorCount() > 0) {
118 printf("%s\n%s", src, compiler.errorText().c_str());
119 return;
120 }
Brian Osman569f12f2019-06-13 11:23:57 -0400121 const SkSL::ByteCodeFunction* main = byteCode->getFunction("main");
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400122 float inoutColor[4] = { inR, inG, inB, inA };
Brian Osman08a84962019-06-14 10:17:16 -0400123 byteCode->run(main, inoutColor, nullptr, 1, nullptr, 0);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400124 if (inoutColor[0] != expectedR || inoutColor[1] != expectedG ||
125 inoutColor[2] != expectedB || inoutColor[3] != expectedA) {
126 printf("for program: %s\n", src);
127 printf(" expected (%f, %f, %f, %f), but received (%f, %f, %f, %f)\n", expectedR,
128 expectedG, expectedB, expectedA, inoutColor[0], inoutColor[1], inoutColor[2],
129 inoutColor[3]);
Brian Osman08a84962019-06-14 10:17:16 -0400130 main->disassemble();
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400131 }
132 REPORTER_ASSERT(r, inoutColor[0] == expectedR);
133 REPORTER_ASSERT(r, inoutColor[1] == expectedG);
134 REPORTER_ASSERT(r, inoutColor[2] == expectedB);
135 REPORTER_ASSERT(r, inoutColor[3] == expectedA);
136 } else {
137 printf("%s\n%s", src, compiler.errorText().c_str());
138 }
Brian Osman569f12f2019-06-13 11:23:57 -0400139
140 // Do additional testing of 4x1 vs 1x4 to stress divergent control flow, etc.
141 vec_test(r, src);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400142}
143
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400144DEF_TEST(SkSLInterpreterAdd, r) {
145 test(r, "void main(inout half4 color) { color.r = color.r + color.g; }", 0.25, 0.75, 0, 0, 1,
146 0.75, 0, 0);
147 test(r, "void main(inout half4 color) { color += half4(1, 2, 3, 4); }", 4, 3, 2, 1, 5, 5, 5, 5);
148 test(r, "void main(inout half4 color) { half4 c = color; color += c; }", 0.25, 0.5, 0.75, 1,
149 0.5, 1, 1.5, 2);
150 test(r, "void main(inout half4 color) { int a = 1; int b = 3; color.r = a + b; }", 1, 2, 3, 4,
151 4, 2, 3, 4);
152}
153
154DEF_TEST(SkSLInterpreterSubtract, r) {
155 test(r, "void main(inout half4 color) { color.r = color.r - color.g; }", 1, 0.75, 0, 0, 0.25,
156 0.75, 0, 0);
157 test(r, "void main(inout half4 color) { color -= half4(1, 2, 3, 4); }", 5, 5, 5, 5, 4, 3, 2, 1);
158 test(r, "void main(inout half4 color) { half4 c = color; color -= c; }", 4, 3, 2, 1,
159 0, 0, 0, 0);
Ethan Nicholas354ecf32019-05-07 16:13:02 -0400160 test(r, "void main(inout half4 color) { color.x = -color.x; }", 4, 3, 2, 1, -4, 3, 2, 1);
161 test(r, "void main(inout half4 color) { color = -color; }", 4, 3, 2, 1, -4, -3, -2, -1);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400162 test(r, "void main(inout half4 color) { int a = 3; int b = 1; color.r = a - b; }", 0, 0, 0, 0,
163 2, 0, 0, 0);
164}
165
166DEF_TEST(SkSLInterpreterMultiply, r) {
167 test(r, "void main(inout half4 color) { color.r = color.r * color.g; }", 2, 3, 0, 0, 6, 3, 0,
168 0);
169 test(r, "void main(inout half4 color) { color *= half4(1, 2, 3, 4); }", 2, 3, 4, 5, 2, 6, 12,
170 20);
171 test(r, "void main(inout half4 color) { half4 c = color; color *= c; }", 4, 3, 2, 1,
172 16, 9, 4, 1);
173 test(r, "void main(inout half4 color) { int a = 3; int b = -2; color.r = a * b; }", 0, 0, 0, 0,
174 -6, 0, 0, 0);
175}
176
177DEF_TEST(SkSLInterpreterDivide, r) {
178 test(r, "void main(inout half4 color) { color.r = color.r / color.g; }", 1, 2, 0, 0, 0.5, 2, 0,
179 0);
180 test(r, "void main(inout half4 color) { color /= half4(1, 2, 3, 4); }", 12, 12, 12, 12, 12, 6,
181 4, 3);
182 test(r, "void main(inout half4 color) { half4 c = color; color /= c; }", 4, 3, 2, 1,
183 1, 1, 1, 1);
184 test(r, "void main(inout half4 color) { int a = 8; int b = -2; color.r = a / b; }", 0, 0, 0, 0,
185 -4, 0, 0, 0);
186}
187
188DEF_TEST(SkSLInterpreterRemainder, r) {
Brian Osman3b41baf2019-05-08 09:24:46 -0400189 test(r, "void main(inout half4 color) { color.r = color.r % color.g; }", 3.125, 2, 0, 0,
190 1.125, 2, 0, 0);
191 test(r, "void main(inout half4 color) { color %= half4(1, 2, 3, 4); }", 9.5, 9.5, 9.5, 9.5,
192 0.5, 1.5, 0.5, 1.5);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400193 test(r, "void main(inout half4 color) { int a = 8; int b = 3; a %= b; color.r = a; }", 0, 0, 0,
194 0, 2, 0, 0, 0);
195 test(r, "void main(inout half4 color) { int a = 8; int b = 3; color.r = a % b; }", 0, 0, 0, 0,
196 2, 0, 0, 0);
197 test(r, "void main(inout half4 color) { int2 a = int2(8, 10); a %= 6; color.rg = a; }", 0, 0, 0,
198 0, 2, 4, 0, 0);
199}
200
Brian Osman29e013d2019-05-28 17:16:03 -0400201DEF_TEST(SkSLInterpreterMatrix, r) {
Brian Osman08a84962019-06-14 10:17:16 -0400202 float in[16];
203 float expected[16];
Brian Osman29e013d2019-05-28 17:16:03 -0400204
205 // Constructing matrix from scalar produces a diagonal matrix
206 in[0] = 1.0f;
207 expected[0] = 2.0f;
208 test(r, "float main(float x) { float4x4 m = float4x4(x); return m[1][1] + m[1][2] + m[2][2]; }",
209 in, 1, expected);
210
211 // With non-square matrix
212 test(r, "float main(float x) { float3x2 m = float3x2(x); return m[0][0] + m[1][1] + m[2][1]; }",
213 in, 1, expected);
214
215 // Constructing from a different-sized matrix fills the remaining space with the identity matrix
216 test(r, "float main(float x) {"
217 "float3x2 m = float3x2(x);"
218 "float4x4 m2 = float4x4(m);"
219 "return m2[0][0] + m2[3][3]; }",
220 in, 1, expected);
221
222 // Constructing a matrix from vectors or scalars fills in values in column-major order
223 in[0] = 1.0f;
224 in[1] = 2.0f;
225 in[2] = 4.0f;
226 in[3] = 8.0f;
227 expected[0] = 6.0f;
228 test(r, "float main(float4 v) { float2x2 m = float2x2(v); return m[0][1] + m[1][0]; }",
229 in, 1, expected);
230
231 expected[0] = 10.0f;
232 test(r, "float main(float4 v) {"
233 "float2x2 m = float2x2(v.x, v.y, v.w, v.z);"
234 "return m[0][1] + m[1][0]; }",
235 in, 1, expected);
236
Brian Osman1e855b22019-05-29 15:21:52 -0400237 // Initialize 16 values to be used as inputs to matrix tests
238 for (int i = 0; i < 16; ++i) { in[i] = (float)i; }
Brian Osman29e013d2019-05-28 17:16:03 -0400239
Brian Osman1e855b22019-05-29 15:21:52 -0400240 // M+M, M-S, S-M
241 for (int i = 0; i < 16; ++i) { expected[i] = (float)(2 * i); }
242 test(r, "float4x4 main(float4x4 m) { return m + m; }", in, 16, expected);
243 for (int i = 0; i < 16; ++i) { expected[i] = (float)(i + 3); }
244 test(r, "float4x4 main(float4x4 m) { return m + 3.0; }", in, 16, expected);
245 test(r, "float4x4 main(float4x4 m) { return 3.0 + m; }", in, 16, expected);
246
247 // M-M, M-S, S-M
248 for (int i = 0; i < 8; ++i) { expected[i] = 8.0f; }
249 test(r, "float4x2 main(float4x2 m1, float4x2 m2) { return m2 - m1; }", in, 8, expected);
250 for (int i = 0; i < 16; ++i) { expected[i] = (float)(i - 3); }
251 test(r, "float4x4 main(float4x4 m) { return m - 3.0; }", in, 16, expected);
252 for (int i = 0; i < 16; ++i) { expected[i] = (float)(3 - i); }
253 test(r, "float4x4 main(float4x4 m) { return 3.0 - m; }", in, 16, expected);
254
255 // M*S, S*M, M/S, S/M
256 for (int i = 0; i < 16; ++i) { expected[i] = (float)(i * 3); }
257 test(r, "float4x4 main(float4x4 m) { return m * 3.0; }", in, 16, expected);
258 test(r, "float4x4 main(float4x4 m) { return 3.0 * m; }", in, 16, expected);
259 for (int i = 0; i < 16; ++i) { expected[i] = (float)(i) / 2.0f; }
260 test(r, "float4x4 main(float4x4 m) { return m / 2.0; }", in, 16, expected);
Brian Osman5bdf5252019-05-29 17:04:54 -0400261 for (int i = 0; i < 16; ++i) { expected[i] = 1.0f / (float)(i + 1); }
262 test(r, "float4x4 main(float4x4 m) { return 1.0 / (m + 1); }", in, 16, expected);
Brian Osman1e855b22019-05-29 15:21:52 -0400263
264#if 0
265 // Matrix negation - legal in GLSL, not in SkSL?
266 for (int i = 0; i < 16; ++i) { expected[i] = (float)(-i); }
267 test(r, "float4x4 main(float4x4 m) { return -m; }", in, 16, expected);
268#endif
269
Brian Osman909231c2019-05-29 15:34:36 -0400270 // M*V, V*M
271 for (int i = 0; i < 4; ++i) {
272 expected[i] = 12.0f*i + 13.0f*(i+4) + 14.0f*(i+8);
273 }
274 test(r, "float4 main(float3x4 m, float3 v) { return m * v; }", in, 4, expected);
275 for (int i = 0; i < 4; ++i) {
276 expected[i] = 12.0f*(3*i) + 13.0f*(3*i+1) + 14.0f*(3*i+2);
277 }
278 test(r, "float4 main(float4x3 m, float3 v) { return v * m; }", in, 4, expected);
Brian Osman29e013d2019-05-28 17:16:03 -0400279
Brian Osman909231c2019-05-29 15:34:36 -0400280 // M*M
281 {
282 SkMatrix44 m;
Brian Osman08a84962019-06-14 10:17:16 -0400283 m.setColMajorf(in);
Brian Osman909231c2019-05-29 15:34:36 -0400284 SkMatrix44 m2;
285 for (int i = 0; i < 16; ++i) {
286 m2.set(i % 4, i / 4, (i + 4) % 16);
287 }
288 m.setConcat(m, m2);
289 // Rearrange the columns on the RHS so we detect left-hand/right-hand errors
290 test(r, "float4x4 main(float4x4 m) { return m * float4x4(m[1], m[2], m[3], m[0]); }",
Brian Osman08a84962019-06-14 10:17:16 -0400291 in, 16, (float*)&m);
Brian Osman909231c2019-05-29 15:34:36 -0400292 }
Brian Osman29e013d2019-05-28 17:16:03 -0400293}
294
Brian Osman4e93feb2019-05-16 15:38:00 -0400295DEF_TEST(SkSLInterpreterTernary, r) {
296 test(r, "void main(inout half4 color) { color.r = color.g > color.b ? color.g : color.b; }",
297 0, 1, 2, 0, 2, 1, 2, 0);
298 test(r, "void main(inout half4 color) { color.r = color.g > color.b ? color.g : color.b; }",
299 0, 3, 2, 0, 3, 3, 2, 0);
300}
301
Brian Osman41672152019-05-14 13:37:30 -0400302DEF_TEST(SkSLInterpreterCast, r) {
Brian Osman08a84962019-06-14 10:17:16 -0400303 union Val {
304 float f;
305 uint32_t u;
306 int32_t s;
307 };
Brian Osman41672152019-05-14 13:37:30 -0400308
Brian Osman08a84962019-06-14 10:17:16 -0400309 Val input[2];
310 Val expected[2];
Brian Osman41672152019-05-14 13:37:30 -0400311
Brian Osman08a84962019-06-14 10:17:16 -0400312 input[0].s = 3;
313 input[1].s = -5;
314 expected[0].f = 3.0f;
315 expected[1].f = -5.0f;
316 test(r, "float main(int x) { return float (x); }", (float*)input, 1, (float*)expected);
317 test(r, "float2 main(int2 x) { return float2(x); }", (float*)input, 2, (float*)expected);
Brian Osman41672152019-05-14 13:37:30 -0400318
Brian Osman08a84962019-06-14 10:17:16 -0400319 input[0].u = 3;
320 input[1].u = 5;
321 expected[0].f = 3.0f;
322 expected[1].f = 5.0f;
323 test(r, "float main(uint x) { return float (x); }", (float*)input, 1, (float*)expected);
324 test(r, "float2 main(uint2 x) { return float2(x); }", (float*)input, 2, (float*)expected);
Brian Osmanc51d7912019-05-22 15:16:16 -0700325
Brian Osman08a84962019-06-14 10:17:16 -0400326 input[0].f = 3.0f;
327 input[1].f = -5.0f;
328 expected[0].s = 3;
329 expected[1].s = -5;
330 test(r, "int main(float x) { return int (x); }", (float*)input, 1, (float*)expected);
331 test(r, "int2 main(float2 x) { return int2(x); }", (float*)input, 2, (float*)expected);
332
333 input[0].s = 3;
334 expected[0].f = 3.0f;
335 expected[1].f = 3.0f;
336 test(r, "float2 main(int x) { return float2(x); }", (float*)input, 2, (float*)expected);
Brian Osman41672152019-05-14 13:37:30 -0400337}
338
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400339DEF_TEST(SkSLInterpreterIf, r) {
340 test(r, "void main(inout half4 color) { if (color.r > color.g) color.a = 1; }", 5, 3, 0, 0,
341 5, 3, 0, 1);
342 test(r, "void main(inout half4 color) { if (color.r > color.g) color.a = 1; }", 5, 5, 0, 0,
343 5, 5, 0, 0);
344 test(r, "void main(inout half4 color) { if (color.r > color.g) color.a = 1; }", 5, 6, 0, 0,
345 5, 6, 0, 0);
346 test(r, "void main(inout half4 color) { if (color.r < color.g) color.a = 1; }", 3, 5, 0, 0,
347 3, 5, 0, 1);
348 test(r, "void main(inout half4 color) { if (color.r < color.g) color.a = 1; }", 5, 5, 0, 0,
349 5, 5, 0, 0);
350 test(r, "void main(inout half4 color) { if (color.r < color.g) color.a = 1; }", 6, 5, 0, 0,
351 6, 5, 0, 0);
352 test(r, "void main(inout half4 color) { if (color.r >= color.g) color.a = 1; }", 5, 3, 0, 0,
353 5, 3, 0, 1);
354 test(r, "void main(inout half4 color) { if (color.r >= color.g) color.a = 1; }", 5, 5, 0, 0,
355 5, 5, 0, 1);
356 test(r, "void main(inout half4 color) { if (color.r >= color.g) color.a = 1; }", 5, 6, 0, 0,
357 5, 6, 0, 0);
358 test(r, "void main(inout half4 color) { if (color.r <= color.g) color.a = 1; }", 3, 5, 0, 0,
359 3, 5, 0, 1);
360 test(r, "void main(inout half4 color) { if (color.r <= color.g) color.a = 1; }", 5, 5, 0, 0,
361 5, 5, 0, 1);
362 test(r, "void main(inout half4 color) { if (color.r <= color.g) color.a = 1; }", 6, 5, 0, 0,
363 6, 5, 0, 0);
364 test(r, "void main(inout half4 color) { if (color.r == color.g) color.a = 1; }", 2, 2, 0, 0,
365 2, 2, 0, 1);
366 test(r, "void main(inout half4 color) { if (color.r == color.g) color.a = 1; }", 2, -2, 0, 0,
367 2, -2, 0, 0);
368 test(r, "void main(inout half4 color) { if (color.r != color.g) color.a = 1; }", 2, 2, 0, 0,
369 2, 2, 0, 0);
370 test(r, "void main(inout half4 color) { if (color.r != color.g) color.a = 1; }", 2, -2, 0, 0,
371 2, -2, 0, 1);
372 test(r, "void main(inout half4 color) { if (color.r == color.g) color.a = 1; else "
373 "color.a = 2; }", 1, 1, 0, 0, 1, 1, 0, 1);
374 test(r, "void main(inout half4 color) { if (color.r == color.g) color.a = 1; else "
375 "color.a = 2; }", 2, -2, 0, 0, 2, -2, 0, 2);
376}
377
Brian Osman16e6fd52019-05-29 11:19:00 -0400378DEF_TEST(SkSLInterpreterIfVector, r) {
379 test(r, "void main(inout half4 color) { if (color.rg == color.ba) color.a = 1; }",
380 1, 2, 1, 2, 1, 2, 1, 1);
381 test(r, "void main(inout half4 color) { if (color.rg == color.ba) color.a = 1; }",
382 1, 2, 3, 2, 1, 2, 3, 2);
383 test(r, "void main(inout half4 color) { if (color.rg != color.ba) color.a = 1; }",
384 1, 2, 1, 2, 1, 2, 1, 2);
385 test(r, "void main(inout half4 color) { if (color.rg != color.ba) color.a = 1; }",
386 1, 2, 3, 2, 1, 2, 3, 1);
387}
388
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400389DEF_TEST(SkSLInterpreterWhile, r) {
390 test(r, "void main(inout half4 color) { while (color.r < 1) color.r += 0.25; }", 0, 0, 0, 0, 1,
391 0, 0, 0);
Brian Osman569f12f2019-06-13 11:23:57 -0400392 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 -0400393 0, 0, 0);
394 test(r, "void main(inout half4 color) { while (true) { color.r += 0.5; "
Brian Osman569f12f2019-06-13 11:23:57 -0400395 "if (color.r > 5) break; } }", 0, 0, 0, 0, 5.5, 0, 0, 0);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400396 test(r, "void main(inout half4 color) { while (color.r < 10) { color.r += 0.5; "
397 "if (color.r < 5) continue; break; } }", 0, 0, 0, 0, 5, 0, 0, 0);
Brian Osman8d564572019-06-19 11:00:28 -0400398 test(r,
399 "void main(inout half4 color) {"
400 " while (true) {"
401 " if (color.r > 4) { break; }"
402 " while (true) { color.a = 1; break; }"
403 " break;"
404 " }"
405 "}",
406 6, 5, 4, 3, 6, 5, 4, 3);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400407}
408
409DEF_TEST(SkSLInterpreterDo, r) {
410 test(r, "void main(inout half4 color) { do color.r += 0.25; while (color.r < 1); }", 0, 0, 0, 0,
411 1, 0, 0, 0);
Brian Osman569f12f2019-06-13 11:23:57 -0400412 test(r, "void main(inout half4 color) { do color.r -= 0.25; while (color.r > 1); }", 0, 0, 0, 0,
413 -0.25, 0, 0, 0);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400414 test(r, "void main(inout half4 color) { do { color.r += 0.5; if (color.r > 1) break; } while "
415 "(true); }", 0, 0, 0, 0, 1.5, 0, 0, 0);
416 test(r, "void main(inout half4 color) {do { color.r += 0.5; if (color.r < 5) "
417 "continue; if (color.r >= 5) break; } while (true); }", 0, 0, 0, 0, 5, 0, 0, 0);
Brian Osman44d44762019-05-13 14:19:12 -0400418 test(r, "void main(inout half4 color) { do { color.r += 0.5; } while (false); }",
419 0, 0, 0, 0, 0.5, 0, 0, 0);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400420}
421
422DEF_TEST(SkSLInterpreterFor, r) {
423 test(r, "void main(inout half4 color) { for (int i = 1; i <= 10; ++i) color.r += i; }", 0, 0, 0,
424 0, 55, 0, 0, 0);
425 test(r,
426 "void main(inout half4 color) {"
427 " for (int i = 1; i <= 10; ++i)"
428 " for (int j = i; j <= 10; ++j)"
429 " color.r += j;"
430 "}",
431 0, 0, 0, 0,
432 385, 0, 0, 0);
433 test(r,
434 "void main(inout half4 color) {"
435 " for (int i = 1; i <= 10; ++i)"
436 " for (int j = 1; ; ++j) {"
437 " if (i == j) continue;"
438 " if (j > 10) break;"
439 " color.r += j;"
440 " }"
441 "}",
442 0, 0, 0, 0,
443 495, 0, 0, 0);
444}
445
Brian Osmanf3fa6002019-05-17 14:26:53 -0400446DEF_TEST(SkSLInterpreterPrefixPostfix, r) {
447 test(r, "void main(inout half4 color) { color.r = ++color.g; }", 1, 2, 3, 4, 3, 3, 3, 4);
448 test(r, "void main(inout half4 color) { color.r = color.g++; }", 1, 2, 3, 4, 2, 3, 3, 4);
449}
450
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400451DEF_TEST(SkSLInterpreterSwizzle, r) {
452 test(r, "void main(inout half4 color) { color = color.abgr; }", 1, 2, 3, 4, 4, 3, 2, 1);
453 test(r, "void main(inout half4 color) { color.rgb = half4(5, 6, 7, 8).bbg; }", 1, 2, 3, 4, 7, 7,
454 6, 4);
455 test(r, "void main(inout half4 color) { color.bgr = int3(5, 6, 7); }", 1, 2, 3, 4, 7, 6,
456 5, 4);
457}
458
459DEF_TEST(SkSLInterpreterGlobal, r) {
460 test(r, "int x; void main(inout half4 color) { x = 10; color.b = x; }", 1, 2, 3, 4, 1, 2, 10,
461 4);
Brian Osmanb7451292019-05-15 13:02:13 -0400462 test(r, "float4 x; void main(inout float4 color) { x = color * 2; color = x; }",
463 1, 2, 3, 4, 2, 4, 6, 8);
464 test(r, "float4 x; void main(inout float4 color) { x = float4(5, 6, 7, 8); color = x.wzyx; }",
465 1, 2, 3, 4, 8, 7, 6, 5);
Brian Osman1091f022019-05-16 09:42:16 -0400466 test(r, "float4 x; void main(inout float4 color) { x.wzyx = float4(5, 6, 7, 8); color = x; }",
467 1, 2, 3, 4, 8, 7, 6, 5);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400468}
Ethan Nicholas746035a2019-04-23 13:31:09 -0400469
470DEF_TEST(SkSLInterpreterGeneric, r) {
471 float value1 = 5;
472 float expected1 = 25;
Brian Osman08a84962019-06-14 10:17:16 -0400473 test(r, "float main(float x) { return x * x; }", &value1, 1, &expected1);
Ethan Nicholas746035a2019-04-23 13:31:09 -0400474 float value2[2] = { 5, 25 };
475 float expected2[2] = { 25, 625 };
Brian Osman08a84962019-06-14 10:17:16 -0400476 test(r, "float2 main(float x, float y) { return float2(x * x, y * y); }", value2, 2, expected2);
Ethan Nicholas746035a2019-04-23 13:31:09 -0400477}
Brian Osmand369a5e2019-05-09 13:13:25 -0400478
Brian Osman07c117b2019-05-23 12:51:06 -0700479DEF_TEST(SkSLInterpreterCompound, r) {
480 struct RectAndColor { SkIRect fRect; SkColor4f fColor; };
481 struct ManyRects { int fNumRects; RectAndColor fRects[4]; };
482
483 const char* src =
484 // Some struct definitions
485 "struct Point { int x; int y; };\n"
486 "struct Rect { Point p0; Point p1; };\n"
487 "struct RectAndColor { Rect r; float4 color; };\n"
488
489 // Structs as globals, parameters, return values
490 "RectAndColor temp;\n"
491 "int rect_height(Rect r) { return r.p1.y - r.p0.y; }\n"
492 "RectAndColor make_blue_rect(int w, int h) {\n"
493 " temp.r.p0.x = temp.r.p0.y = 0;\n"
494 " temp.r.p1.x = w; temp.r.p1.y = h;\n"
495 " temp.color = float4(0, 1, 0, 1);\n"
496 " return temp;\n"
497 "}\n"
498
499 // Initialization and assignment of types larger than 4 slots
500 "RectAndColor init_big(RectAndColor r) { RectAndColor s = r; return s; }\n"
501 "RectAndColor copy_big(RectAndColor r) { RectAndColor s; s = r; return s; }\n"
502
503 // Same for arrays, including some non-constant indexing
504 "float tempFloats[8];\n"
505 "int median(int a[15]) { return a[7]; }\n"
506 "float[8] sums(float a[8]) {\n"
507 " float tempFloats[8];\n"
508 " tempFloats[0] = a[0];\n"
509 " for (int i = 1; i < 8; ++i) { tempFloats[i] = tempFloats[i - 1] + a[i]; }\n"
510 " return tempFloats;\n"
511 "}\n"
512
513 // Uniforms, array-of-structs, dynamic indices
514 "in uniform Rect gRects[4];\n"
515 "Rect get_rect(int i) { return gRects[i]; }\n"
516
517 // Kitchen sink (swizzles, inout, SoAoS)
518 "struct ManyRects { int numRects; RectAndColor rects[4]; };\n"
519 "void fill_rects(inout ManyRects mr) {\n"
520 " for (int i = 0; i < mr.numRects; ++i) {\n"
521 " mr.rects[i].r = gRects[i];\n"
522 " float b = mr.rects[i].r.p1.y;\n"
523 " mr.rects[i].color = float4(b, b, b, b);\n"
524 " }\n"
525 "}\n";
526
527 SkSL::Compiler compiler;
528 SkSL::Program::Settings settings;
529 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(
530 SkSL::Program::kGeneric_Kind,
531 SkSL::String(src), settings);
532 REPORTER_ASSERT(r, program);
533
534 std::unique_ptr<SkSL::ByteCode> byteCode = compiler.toByteCode(*program);
535 REPORTER_ASSERT(r, !compiler.errorCount());
536
537 auto rect_height = byteCode->getFunction("rect_height"),
538 make_blue_rect = byteCode->getFunction("make_blue_rect"),
539 median = byteCode->getFunction("median"),
540 sums = byteCode->getFunction("sums"),
541 get_rect = byteCode->getFunction("get_rect"),
542 fill_rects = byteCode->getFunction("fill_rects");
543
544 SkIRect gRects[4] = { { 1,2,3,4 }, { 5,6,7,8 }, { 9,10,11,12 }, { 13,14,15,16 } };
545
Brian Osman07c117b2019-05-23 12:51:06 -0700546 {
547 SkIRect in = SkIRect::MakeXYWH(10, 10, 20, 30);
548 int out = 0;
Brian Osman08a84962019-06-14 10:17:16 -0400549 byteCode->run(rect_height, (float*)&in, (float*)&out, 1, (float*)gRects, 16);
Brian Osman07c117b2019-05-23 12:51:06 -0700550 REPORTER_ASSERT(r, out == 30);
551 }
552
553 {
554 int in[2] = { 15, 25 };
555 RectAndColor out;
Brian Osman08a84962019-06-14 10:17:16 -0400556 byteCode->run(make_blue_rect, (float*)in, (float*)&out, 1, (float*)gRects, 16);
Brian Osman07c117b2019-05-23 12:51:06 -0700557 REPORTER_ASSERT(r, out.fRect.width() == 15);
558 REPORTER_ASSERT(r, out.fRect.height() == 25);
559 SkColor4f blue = { 0.0f, 1.0f, 0.0f, 1.0f };
560 REPORTER_ASSERT(r, out.fColor == blue);
561 }
562
563 {
564 int in[15] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
565 int out = 0;
Brian Osman08a84962019-06-14 10:17:16 -0400566 byteCode->run(median, (float*)in, (float*)&out, 1, (float*)gRects, 16);
Brian Osman07c117b2019-05-23 12:51:06 -0700567 REPORTER_ASSERT(r, out == 8);
568 }
569
570 {
571 float in[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
572 float out[8] = { 0 };
Brian Osman08a84962019-06-14 10:17:16 -0400573 byteCode->run(sums, in, out, 1, (float*)gRects, 16);
Brian Osman07c117b2019-05-23 12:51:06 -0700574 for (int i = 0; i < 8; ++i) {
575 REPORTER_ASSERT(r, out[i] == static_cast<float>((i + 1) * (i + 2) / 2));
576 }
577 }
578
579 {
580 int in = 2;
581 SkIRect out = SkIRect::MakeEmpty();
Brian Osman08a84962019-06-14 10:17:16 -0400582 byteCode->run(get_rect, (float*)&in, (float*)&out, 1, (float*)gRects, 16);
Brian Osman07c117b2019-05-23 12:51:06 -0700583 REPORTER_ASSERT(r, out == gRects[2]);
584 }
585
586 {
587 ManyRects in;
588 memset(&in, 0, sizeof(in));
589 in.fNumRects = 2;
Brian Osman08a84962019-06-14 10:17:16 -0400590 byteCode->run(fill_rects, (float*)&in, nullptr, 1, (float*)gRects, 16);
Brian Osman07c117b2019-05-23 12:51:06 -0700591 ManyRects expected;
592 memset(&expected, 0, sizeof(expected));
593 expected.fNumRects = 2;
594 for (int i = 0; i < 2; ++i) {
595 expected.fRects[i].fRect = gRects[i];
596 float c = gRects[i].fBottom;
597 expected.fRects[i].fColor = { c, c, c, c };
598 }
599 REPORTER_ASSERT(r, memcmp(&in, &expected, sizeof(in)) == 0);
600 }
601}
Ethan Nicholas91164d12019-05-15 15:29:54 -0400602
Brian Osman6f5358f2019-07-09 14:17:23 -0400603DEF_TEST(SkSLInterpreterRestrictFunctionCalls, r) {
Brian Osman6f5358f2019-07-09 14:17:23 -0400604 auto check = [r](const char* src) {
605 SkSL::Compiler compiler;
606 auto program = compiler.convertProgram(SkSL::Program::kGeneric_Kind, SkSL::String(src),
607 SkSL::Program::Settings());
608 REPORTER_ASSERT(r, program);
609
610 auto byteCode = compiler.toByteCode(*program);
611 REPORTER_ASSERT(r, compiler.errorCount() > 0);
612 REPORTER_ASSERT(r, !byteCode);
613 };
614
615 // Ensure that simple recursion is not allowed
616 check("float main() { return main() + 1; }");
617
618 // Ensure that calls to undefined functions are not allowed (to prevent mutual recursion)
619 check("float foo(); float bar() { return foo(); } float foo() { return bar(); }");
Brian Osman4a47da72019-07-12 11:30:32 -0400620
621 // returns are not allowed inside conditionals (or loops, which are effectively the same thing)
622 check("float main(float x, float y) { if (x < y) { return x; } return y; }");
623 check("float main(float x) { while (x > 1) { return x; } return 0; }");
Brian Osman6f5358f2019-07-09 14:17:23 -0400624}
625
Brian Osman226668a2019-05-14 16:47:30 -0400626DEF_TEST(SkSLInterpreterFunctions, r) {
627 const char* src =
628 "float sqr(float x) { return x * x; }\n"
Brian Osman226668a2019-05-14 16:47:30 -0400629 "float sub(float x, float y) { return x - y; }\n"
Brian Osman6f5358f2019-07-09 14:17:23 -0400630 "float main(float x) { return sub(sqr(x), x); }\n"
Brian Osman226668a2019-05-14 16:47:30 -0400631
632 // Different signatures
633 "float dot(float2 a, float2 b) { return a.x*b.x + a.y*b.y; }\n"
634 "float dot(float3 a, float3 b) { return a.x*b.x + a.y*b.y + a.z*b.z; }\n"
635 "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 -0400636 "float dot2_test(float x) { return dot(float2(x, x + 1), float2(1, -1)); }\n";
Brian Osman226668a2019-05-14 16:47:30 -0400637
638 SkSL::Compiler compiler;
639 SkSL::Program::Settings settings;
640 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(
641 SkSL::Program::kGeneric_Kind,
642 SkSL::String(src), settings);
643 REPORTER_ASSERT(r, program);
644
645 std::unique_ptr<SkSL::ByteCode> byteCode = compiler.toByteCode(*program);
646 REPORTER_ASSERT(r, !compiler.errorCount());
647
648 auto sub = byteCode->getFunction("sub");
649 auto sqr = byteCode->getFunction("sqr");
650 auto main = byteCode->getFunction("main");
651 auto tan = byteCode->getFunction("tan");
652 auto dot3 = byteCode->getFunction("dot3_test");
653 auto dot2 = byteCode->getFunction("dot2_test");
Brian Osman226668a2019-05-14 16:47:30 -0400654
655 REPORTER_ASSERT(r, sub);
656 REPORTER_ASSERT(r, sqr);
657 REPORTER_ASSERT(r, main);
658 REPORTER_ASSERT(r, !tan);
659 REPORTER_ASSERT(r, dot3);
660 REPORTER_ASSERT(r, dot2);
Brian Osman226668a2019-05-14 16:47:30 -0400661
Brian Osman226668a2019-05-14 16:47:30 -0400662 float out = 0.0f;
663 float in = 3.0f;
Brian Osman08a84962019-06-14 10:17:16 -0400664 byteCode->run(main, &in, &out, 1, nullptr, 0);
Brian Osman226668a2019-05-14 16:47:30 -0400665 REPORTER_ASSERT(r, out = 6.0f);
666
Brian Osman08a84962019-06-14 10:17:16 -0400667 byteCode->run(dot3, &in, &out, 1, nullptr, 0);
Brian Osman226668a2019-05-14 16:47:30 -0400668 REPORTER_ASSERT(r, out = 9.0f);
669
Brian Osman08a84962019-06-14 10:17:16 -0400670 byteCode->run(dot2, &in, &out, 1, nullptr, 0);
Brian Osman226668a2019-05-14 16:47:30 -0400671 REPORTER_ASSERT(r, out = -1.0f);
Brian Osman226668a2019-05-14 16:47:30 -0400672}
Ethan Nicholas91164d12019-05-15 15:29:54 -0400673
Brian Osmand3494ed2019-06-20 15:41:34 -0400674DEF_TEST(SkSLInterpreterOutParams, r) {
675 test(r,
676 "void oneAlpha(inout half4 color) { color.a = 1; }"
677 "void main(inout half4 color) { oneAlpha(color); }",
678 0, 0, 0, 0, 0, 0, 0, 1);
679 test(r,
680 "half2 tricky(half x, half y, inout half2 color, half z) {"
681 " color.xy = color.yx;"
682 " return half2(x + y, z);"
683 "}"
684 "void main(inout half4 color) {"
685 " half2 t = tricky(1, 2, color.rb, 5);"
686 " color.ga = t;"
687 "}",
688 1, 2, 3, 4, 3, 3, 1, 5);
689}
690
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400691DEF_TEST(SkSLInterpreterMathFunctions, r) {
Brian Osman08a84962019-06-14 10:17:16 -0400692 float value, expected;
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400693
694 value = 0.0f; expected = 0.0f;
695 test(r, "float main(float x) { return sin(x); }", &value, 1, &expected);
696 test(r, "float main(float x) { return tan(x); }", &value, 1, &expected);
697
698 value = 0.0f; expected = 1.0f;
699 test(r, "float main(float x) { return cos(x); }", &value, 1, &expected);
700
701 value = 25.0f; expected = 5.0f;
702 test(r, "float main(float x) { return sqrt(x); }", &value, 1, &expected);
703}
704
Brian Osmanfba386b2019-06-20 14:54:15 -0400705DEF_TEST(SkSLInterpreterVoidFunction, r) {
706 test(r,
707 "half x; void foo() { x = 1.0; }"
708 "void main(inout half4 color) { foo(); color.r = x; }",
709 0, 0, 0, 0, 1, 0, 0, 0);
710}
711
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400712DEF_TEST(SkSLInterpreterMix, r) {
Brian Osman08a84962019-06-14 10:17:16 -0400713 float value, expected;
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400714
715 value = 0.5f; expected = 0.0f;
716 test(r, "float main(float x) { return mix(-10, 10, x); }", &value, 1, &expected);
717 value = 0.75f; expected = 5.0f;
718 test(r, "float main(float x) { return mix(-10, 10, x); }", &value, 1, &expected);
719 value = 2.0f; expected = 30.0f;
720 test(r, "float main(float x) { return mix(-10, 10, x); }", &value, 1, &expected);
721
Brian Osman08a84962019-06-14 10:17:16 -0400722 float valueVectors[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f },
723 expectedVector[] = { 3.0f, 4.0f, 5.0f, 6.0f };
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400724 test(r, "float4 main(float4 x, float4 y) { return mix(x, y, 0.5); }", valueVectors, 4,
725 expectedVector);
726}
727
728DEF_TEST(SkSLInterpreterCross, r) {
Brian Osman08a84962019-06-14 10:17:16 -0400729 float args[] = { 1.0f, 4.0f, -6.0f, -2.0f, 7.0f, -3.0f };
730 SkPoint3 cross = SkPoint3::CrossProduct(SkPoint3::Make(args[0], args[1], args[2]),
731 SkPoint3::Make(args[3], args[4], args[5]));
732 float expected[] = { cross.fX, cross.fY, cross.fZ };
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400733 test(r, "float3 main(float3 x, float3 y) { return cross(x, y); }", args, 3, expected);
734}
735
736DEF_TEST(SkSLInterpreterDot, r) {
Brian Osman08a84962019-06-14 10:17:16 -0400737 float args[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f };
738 float expected = args[0] * args[2] +
739 args[1] * args[3];
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400740 test(r, "float main(float2 x, float2 y) { return dot(x, y); }", args, 1, &expected);
741
Brian Osman08a84962019-06-14 10:17:16 -0400742 expected = args[0] * args[3] +
743 args[1] * args[4] +
744 args[2] * args[5];
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400745 test(r, "float main(float3 x, float3 y) { return dot(x, y); }", args, 1, &expected);
746
Brian Osman08a84962019-06-14 10:17:16 -0400747 expected = args[0] * args[4] +
748 args[1] * args[5] +
749 args[2] * args[6] +
750 args[3] * args[7];
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400751 test(r, "float main(float4 x, float4 y) { return dot(x, y); }", args, 1, &expected);
752}
753
Ethan Nicholas91164d12019-05-15 15:29:54 -0400754static const SkSL::Type& type_of(const skjson::Value* value, SkSL::Compiler* compiler) {
755 switch (value->getType()) {
756 case skjson::Value::Type::kNumber: {
757 float f = *value->as<skjson::NumberValue>();
758 if (f == (float) (int) f) {
759 return *compiler->context().fInt_Type;
760 }
761 return *compiler->context().fFloat_Type;
762 }
763 case skjson::Value::Type::kBool:
764 return *compiler->context().fBool_Type;
765 default:
766 return *compiler->context().fVoid_Type;
767 }
768}
769
770class JSONExternalValue : public SkSL::ExternalValue {
771public:
772 JSONExternalValue(const char* name, const skjson::Value* value, SkSL::Compiler* compiler)
773 : INHERITED(name, type_of(value, compiler))
774 , fValue(*value)
775 , fCompiler(*compiler) {}
776
777 bool canRead() const override {
778 return type() != *fCompiler.context().fVoid_Type;
779 }
780
Brian Osman1a79f0b2019-06-24 16:32:14 -0400781 void read(int /*unusedIndex*/, float* target) override {
Ethan Nicholas91164d12019-05-15 15:29:54 -0400782 if (type() == *fCompiler.context().fInt_Type) {
783 *(int*) target = *fValue.as<skjson::NumberValue>();
784 } else if (type() == *fCompiler.context().fFloat_Type) {
785 *(float*) target = *fValue.as<skjson::NumberValue>();
786 } else if (type() == *fCompiler.context().fBool_Type) {
Brian Osman4a47da72019-07-12 11:30:32 -0400787 // ByteCode "booleans" are actually bit-masks
788 *(int*) target = *fValue.as<skjson::BoolValue>() ? ~0 : 0;
Ethan Nicholas91164d12019-05-15 15:29:54 -0400789 } else {
790 SkASSERT(false);
791 }
792 }
793
794 SkSL::ExternalValue* getChild(const char* name) const override {
795 if (fValue.getType() == skjson::Value::Type::kObject) {
796 const skjson::Value& v = fValue.as<skjson::ObjectValue>()[name];
797 return (SkSL::ExternalValue*) fCompiler.takeOwnership(std::unique_ptr<Symbol>(
798 new JSONExternalValue(name, &v, &fCompiler)));
799 }
800 return nullptr;
801 }
802
803private:
804 const skjson::Value& fValue;
805 SkSL::Compiler& fCompiler;
806
807 typedef SkSL::ExternalValue INHERITED;
808};
809
810class PointerExternalValue : public SkSL::ExternalValue {
811public:
812 PointerExternalValue(const char* name, const SkSL::Type& type, void* data, size_t size)
813 : INHERITED(name, type)
814 , fData(data)
815 , fSize(size) {}
816
817 bool canRead() const override {
818 return true;
819 }
820
821 bool canWrite() const override {
822 return true;
823 }
824
Brian Osman1a79f0b2019-06-24 16:32:14 -0400825 void read(int /*unusedIndex*/, float* target) override {
Ethan Nicholas91164d12019-05-15 15:29:54 -0400826 memcpy(target, fData, fSize);
827 }
828
Brian Osman1a79f0b2019-06-24 16:32:14 -0400829 void write(int /*unusedIndex*/, float* src) override {
Ethan Nicholas91164d12019-05-15 15:29:54 -0400830 memcpy(fData, src, fSize);
831 }
832
833
834private:
835 void* fData;
836 size_t fSize;
837
838 typedef SkSL::ExternalValue INHERITED;
839};
840
841DEF_TEST(SkSLInterpreterExternalValues, r) {
842 const char* json = "{ \"value1\": 12, \"child\": { \"value2\": true, \"value3\": 5.5 } }";
843 skjson::DOM dom(json, strlen(json));
844 SkSL::Compiler compiler;
845 SkSL::Program::Settings settings;
846 const char* src = "float main() {"
847 " outValue = 152;"
Brian Osman4a47da72019-07-12 11:30:32 -0400848 " return root.child.value2 ? root.value1 * root.child.value3 : -1;"
Ethan Nicholas91164d12019-05-15 15:29:54 -0400849 "}";
850 compiler.registerExternalValue((SkSL::ExternalValue*) compiler.takeOwnership(
851 std::unique_ptr<SkSL::Symbol>(new JSONExternalValue("root", &dom.root(), &compiler))));
852 int32_t outValue = -1;
853 compiler.registerExternalValue((SkSL::ExternalValue*) compiler.takeOwnership(
854 std::unique_ptr<SkSL::Symbol>(new PointerExternalValue("outValue",
855 *compiler.context().fInt_Type,
856 &outValue,
857 sizeof(outValue)))));
858 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(
859 SkSL::Program::kGeneric_Kind,
860 SkSL::String(src), settings);
861 REPORTER_ASSERT(r, program);
862 if (program) {
863 std::unique_ptr<SkSL::ByteCode> byteCode = compiler.toByteCode(*program);
864 REPORTER_ASSERT(r, !compiler.errorCount());
865 if (compiler.errorCount() > 0) {
866 printf("%s\n%s", src, compiler.errorText().c_str());
867 return;
868 }
869 SkSL::ByteCodeFunction* main = byteCode->fFunctions[0].get();
Brian Osman08a84962019-06-14 10:17:16 -0400870 float out;
871 byteCode->run(main, nullptr, &out, 1, nullptr, 0);
872 REPORTER_ASSERT(r, out == 66.0);
Ethan Nicholas91164d12019-05-15 15:29:54 -0400873 REPORTER_ASSERT(r, outValue == 152);
874 } else {
875 printf("%s\n%s", src, compiler.errorText().c_str());
876 }
877}
878
879DEF_TEST(SkSLInterpreterExternalValuesVector, r) {
880 SkSL::Compiler compiler;
881 SkSL::Program::Settings settings;
882 const char* src = "void main() {"
883 " value *= 2;"
884 "}";
885 int32_t value[4] = { 1, 2, 3, 4 };
886 compiler.registerExternalValue((SkSL::ExternalValue*) compiler.takeOwnership(
887 std::unique_ptr<SkSL::Symbol>(new PointerExternalValue("value",
888 *compiler.context().fInt4_Type,
889 value,
890 sizeof(value)))));
891 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(SkSL::Program::kGeneric_Kind,
892 SkSL::String(src),
893 settings);
894 REPORTER_ASSERT(r, program);
895 if (program) {
896 std::unique_ptr<SkSL::ByteCode> byteCode = compiler.toByteCode(*program);
897 REPORTER_ASSERT(r, !compiler.errorCount());
898 if (compiler.errorCount() > 0) {
899 printf("%s\n%s", src, compiler.errorText().c_str());
900 return;
901 }
902 SkSL::ByteCodeFunction* main = byteCode->fFunctions[0].get();
Brian Osman08a84962019-06-14 10:17:16 -0400903 byteCode->run(main, nullptr, nullptr, 1, nullptr, 0);
Ethan Nicholas91164d12019-05-15 15:29:54 -0400904 REPORTER_ASSERT(r, value[0] == 2);
905 REPORTER_ASSERT(r, value[1] == 4);
906 REPORTER_ASSERT(r, value[2] == 6);
907 REPORTER_ASSERT(r, value[3] == 8);
908 } else {
909 printf("%s\n%s", src, compiler.errorText().c_str());
910 }
911}
Ethan Nicholas9e6a3932019-05-17 16:31:21 -0400912
913class FunctionExternalValue : public SkSL::ExternalValue {
914public:
915 FunctionExternalValue(const char* name, float(*function)(float), SkSL::Compiler& compiler)
916 : INHERITED(name, *compiler.context().fFloat_Type)
917 , fCompiler(compiler)
918 , fFunction(function) {}
919
920 bool canCall() const override {
921 return true;
922 }
923
924 int callParameterCount() const override {
925 return 1;
926 }
927
928 void getCallParameterTypes(const SkSL::Type** outTypes) const override {
929 outTypes[0] = fCompiler.context().fFloat_Type.get();
930 }
931
Brian Osman1a79f0b2019-06-24 16:32:14 -0400932 void call(int /*unusedIndex*/, float* arguments, float* outReturn) override {
933 outReturn[0] = fFunction(arguments[0]);
Ethan Nicholas9e6a3932019-05-17 16:31:21 -0400934 }
935
936private:
937 SkSL::Compiler& fCompiler;
938
939 float (*fFunction)(float);
940
941 typedef SkSL::ExternalValue INHERITED;
942};
943
944DEF_TEST(SkSLInterpreterExternalValuesCall, r) {
945 SkSL::Compiler compiler;
946 SkSL::Program::Settings settings;
947 const char* src = "float main() {"
948 " return external(25);"
949 "}";
950 compiler.registerExternalValue((SkSL::ExternalValue*) compiler.takeOwnership(
951 std::unique_ptr<SkSL::Symbol>(new FunctionExternalValue("external",
952 [] (float x) {
953 return (float) sqrt(x);
954 },
955 compiler))));
956 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(SkSL::Program::kGeneric_Kind,
957 SkSL::String(src),
958 settings);
959 REPORTER_ASSERT(r, program);
960 if (program) {
961 std::unique_ptr<SkSL::ByteCode> byteCode = compiler.toByteCode(*program);
962 REPORTER_ASSERT(r, !compiler.errorCount());
963 if (compiler.errorCount() > 0) {
964 printf("%s\n%s", src, compiler.errorText().c_str());
965 return;
966 }
967 SkSL::ByteCodeFunction* main = byteCode->fFunctions[0].get();
Brian Osman08a84962019-06-14 10:17:16 -0400968 float out;
969 byteCode->run(main, nullptr, &out, 1, nullptr, 0);
970 REPORTER_ASSERT(r, out == 5.0);
Ethan Nicholas9e6a3932019-05-17 16:31:21 -0400971 } else {
972 printf("%s\n%s", src, compiler.errorText().c_str());
973 }
974}
975
976class VectorFunctionExternalValue : public SkSL::ExternalValue {
977public:
978 VectorFunctionExternalValue(const char* name, void(*function)(float[4], float[4]),
979 SkSL::Compiler& compiler)
980 : INHERITED(name, *compiler.context().fFloat4_Type)
981 , fCompiler(compiler)
982 , fFunction(function) {}
983
984 bool canCall() const override {
985 return true;
986 }
987
988 int callParameterCount() const override {
989 return 1;
990 }
991
992 void getCallParameterTypes(const SkSL::Type** outTypes) const override {
993 outTypes[0] = fCompiler.context().fFloat4_Type.get();
994 }
995
Brian Osman1a79f0b2019-06-24 16:32:14 -0400996 void call(int /*unusedIndex*/, float* arguments, float* outReturn) override {
997 fFunction(arguments, outReturn);
Ethan Nicholas9e6a3932019-05-17 16:31:21 -0400998 }
999
1000private:
1001 SkSL::Compiler& fCompiler;
1002
1003 void (*fFunction)(float[4], float[4]);
1004
1005 typedef SkSL::ExternalValue INHERITED;
1006};
1007
1008
1009DEF_TEST(SkSLInterpreterExternalValuesVectorCall, r) {
1010 SkSL::Compiler compiler;
1011 SkSL::Program::Settings settings;
1012 const char* src = "float4 main() {"
1013 " return external(float4(1, 4, 9, 16));"
1014 "}";
1015 compiler.registerExternalValue((SkSL::ExternalValue*) compiler.takeOwnership(
1016 std::unique_ptr<SkSL::Symbol>(new VectorFunctionExternalValue("external",
1017 [] (float in[4], float out[4]) {
1018 out[0] = sqrt(in[0]);
1019 out[1] = sqrt(in[1]);
1020 out[2] = sqrt(in[2]);
1021 out[3] = sqrt(in[3]);
1022 },
1023 compiler))));
1024 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(SkSL::Program::kGeneric_Kind,
1025 SkSL::String(src),
1026 settings);
1027 REPORTER_ASSERT(r, program);
1028 if (program) {
1029 std::unique_ptr<SkSL::ByteCode> byteCode = compiler.toByteCode(*program);
1030 REPORTER_ASSERT(r, !compiler.errorCount());
1031 if (compiler.errorCount() > 0) {
1032 printf("%s\n%s", src, compiler.errorText().c_str());
1033 return;
1034 }
1035 SkSL::ByteCodeFunction* main = byteCode->fFunctions[0].get();
Brian Osman08a84962019-06-14 10:17:16 -04001036 float out[4];
1037 byteCode->run(main, nullptr, out, 1, nullptr, 0);
1038 REPORTER_ASSERT(r, out[0] == 1.0);
1039 REPORTER_ASSERT(r, out[1] == 2.0);
1040 REPORTER_ASSERT(r, out[2] == 3.0);
1041 REPORTER_ASSERT(r, out[3] == 4.0);
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001042 } else {
1043 printf("%s\n%s", src, compiler.errorText().c_str());
1044 }
1045}