blob: 16e7fe7316deff7da1eda1b243edee2463532301 [file] [log] [blame]
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001/*
2 * Copyright 2019 Google LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Reed46f5c5f2020-02-20 15:42:29 -05008#include "include/core/SkM44.h"
Brian Osman08a84962019-06-14 10:17:16 -04009#include "src/sksl/SkSLByteCode.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "src/sksl/SkSLCompiler.h"
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 Osman1f71f432020-11-18 15:44:46 -050016#if defined(SK_ENABLE_SKSL_INTERPRETER)
17
Brian Osmanb08cc022020-04-02 11:38:40 -040018static bool nearly_equal(const float a[], const float b[], int count) {
19 for (int i = 0; i < count; ++i) {
20 if (!SkScalarNearlyEqual(a[i], b[i])) {
21 return false;
22 }
23 }
24 return true;
25}
26
Ben Wagner470e0ac2020-01-22 16:59:21 -050027void test(skiatest::Reporter* r, const char* src, float* in, float* expected,
Brian Osmanb08cc022020-04-02 11:38:40 -040028 bool exactCompare = true) {
Brian Osman0006ad02020-11-18 15:38:39 -050029 GrShaderCaps caps(GrContextOptions{});
30 SkSL::Compiler compiler(&caps);
Ethan Nicholas746035a2019-04-23 13:31:09 -040031 SkSL::Program::Settings settings;
John Stiles759880a2020-09-11 12:10:43 -040032 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(SkSL::Program::kGeneric_Kind,
33 SkSL::String(src), settings);
Ethan Nicholas746035a2019-04-23 13:31:09 -040034 REPORTER_ASSERT(r, program);
35 if (program) {
36 std::unique_ptr<SkSL::ByteCode> byteCode = compiler.toByteCode(*program);
Brian Osman80164412019-06-07 13:00:23 -040037 program.reset();
Ethan Nicholas746035a2019-04-23 13:31:09 -040038 REPORTER_ASSERT(r, !compiler.errorCount());
39 if (compiler.errorCount() > 0) {
40 printf("%s\n%s", src, compiler.errorText().c_str());
41 return;
42 }
Brian Osman886af0d2019-07-26 15:12:56 -040043 const SkSL::ByteCodeFunction* main = byteCode->getFunction("main");
Brian Osmanb08cc022020-04-02 11:38:40 -040044 int returnCount = main->getReturnCount();
45 std::unique_ptr<float[]> out = std::unique_ptr<float[]>(new float[returnCount]);
46 SkAssertResult(byteCode->run(main, in, main->getParameterCount(), out.get(), returnCount,
47 nullptr, 0));
48 bool valid = exactCompare ? !memcmp(out.get(), expected, sizeof(float) * returnCount)
49 : nearly_equal(out.get(), expected, returnCount);
50 if (!valid) {
51 printf("for program: %s\n", src);
52 printf(" expected (");
53 const char* separator = "";
54 for (int i = 0; i < returnCount; ++i) {
55 printf("%s%f", separator, expected[i]);
56 separator = ", ";
Ben Wagner470e0ac2020-01-22 16:59:21 -050057 }
Brian Osmanb08cc022020-04-02 11:38:40 -040058 printf("), but received (");
59 separator = "";
60 for (int i = 0; i < returnCount; ++i) {
61 printf("%s%f", separator, out.get()[i]);
62 separator = ", ";
63 }
64 printf(")\n");
65 main->disassemble();
Ethan Nicholas5f409862020-01-22 14:14:25 -050066 }
Brian Osmanb08cc022020-04-02 11:38:40 -040067 REPORTER_ASSERT(r, valid);
Ethan Nicholas746035a2019-04-23 13:31:09 -040068 } else {
69 printf("%s\n%s", src, compiler.errorText().c_str());
70 }
71}
72
Brian Osman569f12f2019-06-13 11:23:57 -040073void vec_test(skiatest::Reporter* r, const char* src) {
Brian Osman0006ad02020-11-18 15:38:39 -050074 GrShaderCaps caps(GrContextOptions{});
75 SkSL::Compiler compiler(&caps);
John Stiles759880a2020-09-11 12:10:43 -040076 SkSL::Program::Settings settings;
77 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(SkSL::Program::kGeneric_Kind,
78 SkSL::String(src), settings);
Brian Osman08a84962019-06-14 10:17:16 -040079 if (!program) {
80 REPORT_FAILURE(r, "!program", SkString(compiler.errorText().c_str()));
81 return;
82 }
83
84 std::unique_ptr<SkSL::ByteCode> byteCode = compiler.toByteCode(*program);
85 if (compiler.errorCount() > 0) {
86 REPORT_FAILURE(r, "!toByteCode", SkString(compiler.errorText().c_str()));
87 return;
88 }
89
Brian Osmanb08cc022020-04-02 11:38:40 -040090 const SkSL::ByteCodeFunction* main = byteCode->getFunction("main");
Brian Osman08a84962019-06-14 10:17:16 -040091
Brian Osmanb23d66e2019-09-27 10:25:57 -040092 // Test on four different vectors (with varying orderings to get divergent control flow)
93 const float input[16] = { 1, 2, 3, 4,
94 4, 3, 2, 1,
95 7, 5, 8, 6,
96 6, 8, 5, 7 };
97
Brian Osman569f12f2019-06-13 11:23:57 -040098 float out_s[16], out_v[16];
99 memcpy(out_s, input, sizeof(out_s));
100 memcpy(out_v, input, sizeof(out_v));
101
Brian Osman08a84962019-06-14 10:17:16 -0400102 // First run in scalar mode to determine the expected output
103 for (int i = 0; i < 4; ++i) {
Brian Osmanb08cc022020-04-02 11:38:40 -0400104 SkAssertResult(byteCode->run(main, out_s + i * 4, 4, nullptr, 0, nullptr, 0));
Brian Osman08a84962019-06-14 10:17:16 -0400105 }
Brian Osman569f12f2019-06-13 11:23:57 -0400106
Brian Osmanb23d66e2019-09-27 10:25:57 -0400107 // Need to transpose input vectors for striped execution
108 auto transpose = [](float* v) {
109 for (int r = 0; r < 4; ++r)
110 for (int c = 0; c < r; ++c)
111 std::swap(v[r*4 + c], v[c*4 + r]);
112 };
113
114 // Need to transpose input vectors for striped execution
115 transpose(out_v);
116 float* args[] = { out_v, out_v + 4, out_v + 8, out_v + 12 };
117
Brian Osman08a84962019-06-14 10:17:16 -0400118 // Now run in parallel and compare results
Brian Osmanb08cc022020-04-02 11:38:40 -0400119 SkAssertResult(byteCode->runStriped(main, 4, args, 4, nullptr, 0, nullptr, 0));
Brian Osmanb23d66e2019-09-27 10:25:57 -0400120
121 // Transpose striped outputs back
122 transpose(out_v);
123
John Stilesc1c3c6d2020-08-15 23:22:53 -0400124 if (0 != memcmp(out_s, out_v, sizeof(out_s))) {
Brian Osman08a84962019-06-14 10:17:16 -0400125 printf("for program: %s\n", src);
126 for (int i = 0; i < 4; ++i) {
127 printf("(%g %g %g %g) -> (%g %g %g %g), expected (%g %g %g %g)\n",
128 input[4*i + 0], input[4*i + 1], input[4*i + 2], input[4*i + 3],
129 out_v[4*i + 0], out_v[4*i + 1], out_v[4*i + 2], out_v[4*i + 3],
130 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 -0400131 }
Brian Osmanb08cc022020-04-02 11:38:40 -0400132 main->disassemble();
Brian Osman08a84962019-06-14 10:17:16 -0400133 REPORT_FAILURE(r, "VecInterpreter mismatch", SkString());
Brian Osman569f12f2019-06-13 11:23:57 -0400134 }
135}
136
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400137void test(skiatest::Reporter* r, const char* src, float inR, float inG, float inB, float inA,
Brian Osman08a84962019-06-14 10:17:16 -0400138 float expectedR, float expectedG, float expectedB, float expectedA) {
Brian Osman0006ad02020-11-18 15:38:39 -0500139 GrShaderCaps caps(GrContextOptions{});
140 SkSL::Compiler compiler(&caps);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400141 SkSL::Program::Settings settings;
John Stiles759880a2020-09-11 12:10:43 -0400142 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(SkSL::Program::kGeneric_Kind,
143 SkSL::String(src), settings);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400144 REPORTER_ASSERT(r, program);
145 if (program) {
146 std::unique_ptr<SkSL::ByteCode> byteCode = compiler.toByteCode(*program);
Brian Osman80164412019-06-07 13:00:23 -0400147 program.reset();
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400148 REPORTER_ASSERT(r, !compiler.errorCount());
149 if (compiler.errorCount() > 0) {
150 printf("%s\n%s", src, compiler.errorText().c_str());
151 return;
152 }
Brian Osman569f12f2019-06-13 11:23:57 -0400153 const SkSL::ByteCodeFunction* main = byteCode->getFunction("main");
Brian Osmanb08cc022020-04-02 11:38:40 -0400154 float inoutColor[4] = { inR, inG, inB, inA };
155 SkAssertResult(byteCode->run(main, inoutColor, 4, nullptr, 0, nullptr, 0));
156 if (inoutColor[0] != expectedR || inoutColor[1] != expectedG ||
157 inoutColor[2] != expectedB || inoutColor[3] != expectedA) {
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400158 printf("for program: %s\n", src);
159 printf(" expected (%f, %f, %f, %f), but received (%f, %f, %f, %f)\n", expectedR,
Brian Osmanb08cc022020-04-02 11:38:40 -0400160 expectedG, expectedB, expectedA, inoutColor[0], inoutColor[1], inoutColor[2],
161 inoutColor[3]);
Brian Osman2e19af02020-03-07 17:43:49 +0000162 main->disassemble();
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400163 }
Brian Osmanb08cc022020-04-02 11:38:40 -0400164 REPORTER_ASSERT(r, inoutColor[0] == expectedR);
165 REPORTER_ASSERT(r, inoutColor[1] == expectedG);
166 REPORTER_ASSERT(r, inoutColor[2] == expectedB);
167 REPORTER_ASSERT(r, inoutColor[3] == expectedA);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400168 } else {
169 printf("%s\n%s", src, compiler.errorText().c_str());
170 }
Brian Osman569f12f2019-06-13 11:23:57 -0400171
172 // Do additional testing of 4x1 vs 1x4 to stress divergent control flow, etc.
173 vec_test(r, src);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400174}
Ben Wagner470e0ac2020-01-22 16:59:21 -0500175
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400176DEF_TEST(SkSLInterpreterAdd, r) {
177 test(r, "void main(inout half4 color) { color.r = color.r + color.g; }", 0.25, 0.75, 0, 0, 1,
178 0.75, 0, 0);
179 test(r, "void main(inout half4 color) { color += half4(1, 2, 3, 4); }", 4, 3, 2, 1, 5, 5, 5, 5);
180 test(r, "void main(inout half4 color) { half4 c = color; color += c; }", 0.25, 0.5, 0.75, 1,
181 0.5, 1, 1.5, 2);
Ethan Nicholas6f624122019-09-24 13:07:06 -0400182 test(r, "void main(inout half4 color) { color.r = int(color.r) + int(color.g); }", 1, 3, 0, 0,
183 4, 3, 0, 0);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400184}
185
186DEF_TEST(SkSLInterpreterSubtract, r) {
187 test(r, "void main(inout half4 color) { color.r = color.r - color.g; }", 1, 0.75, 0, 0, 0.25,
188 0.75, 0, 0);
189 test(r, "void main(inout half4 color) { color -= half4(1, 2, 3, 4); }", 5, 5, 5, 5, 4, 3, 2, 1);
190 test(r, "void main(inout half4 color) { half4 c = color; color -= c; }", 4, 3, 2, 1,
191 0, 0, 0, 0);
Ethan Nicholas354ecf32019-05-07 16:13:02 -0400192 test(r, "void main(inout half4 color) { color.x = -color.x; }", 4, 3, 2, 1, -4, 3, 2, 1);
193 test(r, "void main(inout half4 color) { color = -color; }", 4, 3, 2, 1, -4, -3, -2, -1);
Ethan Nicholas6f624122019-09-24 13:07:06 -0400194 test(r, "void main(inout half4 color) { color.r = int(color.r) - int(color.g); }", 3, 1, 0, 0,
195 2, 1, 0, 0);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400196}
197
198DEF_TEST(SkSLInterpreterMultiply, r) {
199 test(r, "void main(inout half4 color) { color.r = color.r * color.g; }", 2, 3, 0, 0, 6, 3, 0,
200 0);
201 test(r, "void main(inout half4 color) { color *= half4(1, 2, 3, 4); }", 2, 3, 4, 5, 2, 6, 12,
202 20);
203 test(r, "void main(inout half4 color) { half4 c = color; color *= c; }", 4, 3, 2, 1,
204 16, 9, 4, 1);
Ethan Nicholas6f624122019-09-24 13:07:06 -0400205 test(r, "void main(inout half4 color) { color.r = int(color.r) * int(color.g); }", 3, -2, 0, 0,
206 -6, -2, 0, 0);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400207}
208
209DEF_TEST(SkSLInterpreterDivide, r) {
210 test(r, "void main(inout half4 color) { color.r = color.r / color.g; }", 1, 2, 0, 0, 0.5, 2, 0,
211 0);
212 test(r, "void main(inout half4 color) { color /= half4(1, 2, 3, 4); }", 12, 12, 12, 12, 12, 6,
213 4, 3);
214 test(r, "void main(inout half4 color) { half4 c = color; color /= c; }", 4, 3, 2, 1,
215 1, 1, 1, 1);
Ethan Nicholas6f624122019-09-24 13:07:06 -0400216 test(r, "void main(inout half4 color) { color.r = int(color.r) / int(color.g); }", 8, -2, 0, 0,
217 -4, -2, 0, 0);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400218}
219
220DEF_TEST(SkSLInterpreterRemainder, r) {
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;
Brian Osmanb08cc022020-04-02 11:38:40 -0400281 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
Brian Osmanc0f2b642020-12-22 13:35:55 -0500295 in[0] = 2.0f;
296 expected[0] = 4.0f;
Brian Osman29e013d2019-05-28 17:16:03 -0400297 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
Brian Osman29e013d2019-05-28 17:16:03 -0400300 // Constructing from a different-sized matrix fills the remaining space with the identity matrix
Brian Osmanc0f2b642020-12-22 13:35:55 -0500301 expected[0] = 3.0f;
Brian Osman29e013d2019-05-28 17:16:03 -0400302 test(r, "float main(float x) {"
Brian Osmanc0f2b642020-12-22 13:35:55 -0500303 "float2x2 m = float2x2(x);"
Brian Osman29e013d2019-05-28 17:16:03 -0400304 "float4x4 m2 = float4x4(m);"
305 "return m2[0][0] + m2[3][3]; }",
Brian Osmanb23d66e2019-09-27 10:25:57 -0400306 in, expected);
Brian Osman29e013d2019-05-28 17:16:03 -0400307
308 // Constructing a matrix from vectors or scalars fills in values in column-major order
309 in[0] = 1.0f;
310 in[1] = 2.0f;
311 in[2] = 4.0f;
312 in[3] = 8.0f;
313 expected[0] = 6.0f;
314 test(r, "float main(float4 v) { float2x2 m = float2x2(v); return m[0][1] + m[1][0]; }",
Brian Osmanb23d66e2019-09-27 10:25:57 -0400315 in, expected);
Brian Osman29e013d2019-05-28 17:16:03 -0400316
317 expected[0] = 10.0f;
318 test(r, "float main(float4 v) {"
319 "float2x2 m = float2x2(v.x, v.y, v.w, v.z);"
320 "return m[0][1] + m[1][0]; }",
Brian Osmanb23d66e2019-09-27 10:25:57 -0400321 in, expected);
Brian Osman29e013d2019-05-28 17:16:03 -0400322
Brian Osman1e855b22019-05-29 15:21:52 -0400323 // Initialize 16 values to be used as inputs to matrix tests
324 for (int i = 0; i < 16; ++i) { in[i] = (float)i; }
Brian Osman29e013d2019-05-28 17:16:03 -0400325
Brian Osman1e855b22019-05-29 15:21:52 -0400326 // M+M, M-S, S-M
327 for (int i = 0; i < 16; ++i) { expected[i] = (float)(2 * i); }
Brian Osmanb23d66e2019-09-27 10:25:57 -0400328 test(r, "float4x4 main(float4x4 m) { return m + m; }", in, expected);
Brian Osman1e855b22019-05-29 15:21:52 -0400329 for (int i = 0; i < 16; ++i) { expected[i] = (float)(i + 3); }
Brian Osmanb23d66e2019-09-27 10:25:57 -0400330 test(r, "float4x4 main(float4x4 m) { return m + 3.0; }", in, expected);
331 test(r, "float4x4 main(float4x4 m) { return 3.0 + m; }", in, expected);
Brian Osman1e855b22019-05-29 15:21:52 -0400332
333 // M-M, M-S, S-M
Brian Osmanc0f2b642020-12-22 13:35:55 -0500334 for (int i = 0; i < 4; ++i) { expected[i] = 4.0f; }
335 test(r, "float2x2 main(float2x2 m1, float2x2 m2) { return m2 - m1; }", in, expected);
Brian Osman1e855b22019-05-29 15:21:52 -0400336 for (int i = 0; i < 16; ++i) { expected[i] = (float)(i - 3); }
Brian Osmanb23d66e2019-09-27 10:25:57 -0400337 test(r, "float4x4 main(float4x4 m) { return m - 3.0; }", in, expected);
Brian Osman1e855b22019-05-29 15:21:52 -0400338 for (int i = 0; i < 16; ++i) { expected[i] = (float)(3 - i); }
Brian Osmanb23d66e2019-09-27 10:25:57 -0400339 test(r, "float4x4 main(float4x4 m) { return 3.0 - m; }", in, expected);
Brian Osman1e855b22019-05-29 15:21:52 -0400340
341 // M*S, S*M, M/S, S/M
342 for (int i = 0; i < 16; ++i) { expected[i] = (float)(i * 3); }
Brian Osmanb23d66e2019-09-27 10:25:57 -0400343 test(r, "float4x4 main(float4x4 m) { return m * 3.0; }", in, expected);
344 test(r, "float4x4 main(float4x4 m) { return 3.0 * m; }", in, expected);
Brian Osman1e855b22019-05-29 15:21:52 -0400345 for (int i = 0; i < 16; ++i) { expected[i] = (float)(i) / 2.0f; }
Brian Osmanb23d66e2019-09-27 10:25:57 -0400346 test(r, "float4x4 main(float4x4 m) { return m / 2.0; }", in, expected);
Brian Osman5bdf5252019-05-29 17:04:54 -0400347 for (int i = 0; i < 16; ++i) { expected[i] = 1.0f / (float)(i + 1); }
Brian Osmanb23d66e2019-09-27 10:25:57 -0400348 test(r, "float4x4 main(float4x4 m) { return 1.0 / (m + 1); }", in, expected);
Brian Osman1e855b22019-05-29 15:21:52 -0400349
350#if 0
351 // Matrix negation - legal in GLSL, not in SkSL?
352 for (int i = 0; i < 16; ++i) { expected[i] = (float)(-i); }
353 test(r, "float4x4 main(float4x4 m) { return -m; }", in, 16, expected);
354#endif
355
Brian Osman909231c2019-05-29 15:34:36 -0400356 // M*V, V*M
Brian Osmanc0f2b642020-12-22 13:35:55 -0500357 for (int i = 0; i < 3; ++i) {
358 expected[i] = 9.0f*i + 10.0f*(i+3) + 11.0f*(i+6);
Brian Osman909231c2019-05-29 15:34:36 -0400359 }
Brian Osmanc0f2b642020-12-22 13:35:55 -0500360 test(r, "float3 main(float3x3 m, float3 v) { return m * v; }", in, expected);
361 for (int i = 0; i < 3; ++i) {
362 expected[i] = 9.0f*(3*i) + 10.0f*(3*i+1) + 11.0f*(3*i+2);
Brian Osman909231c2019-05-29 15:34:36 -0400363 }
Brian Osmanc0f2b642020-12-22 13:35:55 -0500364 test(r, "float3 main(float3x3 m, float3 v) { return v * m; }", in, expected);
Brian Osman29e013d2019-05-28 17:16:03 -0400365
Brian Osman909231c2019-05-29 15:34:36 -0400366 // M*M
367 {
Mike Reed3ef77dd2020-04-06 10:41:09 -0400368 SkM44 m = SkM44::ColMajor(in);
Mike Reedb26b4e72020-01-22 14:31:21 -0500369 SkM44 m2;
370 float in2[16];
Brian Osman909231c2019-05-29 15:34:36 -0400371 for (int i = 0; i < 16; ++i) {
Mike Reedb26b4e72020-01-22 14:31:21 -0500372 in2[i] = (i + 4) % 16;
Brian Osman909231c2019-05-29 15:34:36 -0400373 }
Mike Reed3ef77dd2020-04-06 10:41:09 -0400374 m2 = SkM44::ColMajor(in2);
Brian Osman909231c2019-05-29 15:34:36 -0400375 m.setConcat(m, m2);
376 // Rearrange the columns on the RHS so we detect left-hand/right-hand errors
377 test(r, "float4x4 main(float4x4 m) { return m * float4x4(m[1], m[2], m[3], m[0]); }",
Brian Osmanb23d66e2019-09-27 10:25:57 -0400378 in, (float*)&m);
Brian Osman909231c2019-05-29 15:34:36 -0400379 }
Brian Osman29e013d2019-05-28 17:16:03 -0400380}
381
Brian Osman4e93feb2019-05-16 15:38:00 -0400382DEF_TEST(SkSLInterpreterTernary, r) {
383 test(r, "void main(inout half4 color) { color.r = color.g > color.b ? color.g : color.b; }",
384 0, 1, 2, 0, 2, 1, 2, 0);
385 test(r, "void main(inout half4 color) { color.r = color.g > color.b ? color.g : color.b; }",
386 0, 3, 2, 0, 3, 3, 2, 0);
387}
388
Brian Osman41672152019-05-14 13:37:30 -0400389DEF_TEST(SkSLInterpreterCast, r) {
Brian Osman08a84962019-06-14 10:17:16 -0400390 union Val {
391 float f;
392 uint32_t u;
393 int32_t s;
394 };
Brian Osman41672152019-05-14 13:37:30 -0400395
Brian Osman08a84962019-06-14 10:17:16 -0400396 Val input[2];
397 Val expected[2];
Brian Osman41672152019-05-14 13:37:30 -0400398
Brian Osman08a84962019-06-14 10:17:16 -0400399 input[0].s = 3;
400 input[1].s = -5;
401 expected[0].f = 3.0f;
402 expected[1].f = -5.0f;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400403 test(r, "float main(int x) { return float (x); }", (float*)input, (float*)expected);
404 test(r, "float2 main(int2 x) { return float2(x); }", (float*)input, (float*)expected);
Brian Osman41672152019-05-14 13:37:30 -0400405
Brian Osman08a84962019-06-14 10:17:16 -0400406 input[0].u = 3;
407 input[1].u = 5;
408 expected[0].f = 3.0f;
409 expected[1].f = 5.0f;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400410 test(r, "float main(uint x) { return float (x); }", (float*)input, (float*)expected);
411 test(r, "float2 main(uint2 x) { return float2(x); }", (float*)input, (float*)expected);
Brian Osmanc51d7912019-05-22 15:16:16 -0700412
Brian Osman08a84962019-06-14 10:17:16 -0400413 input[0].f = 3.0f;
414 input[1].f = -5.0f;
415 expected[0].s = 3;
416 expected[1].s = -5;
Brian Osmanb08cc022020-04-02 11:38:40 -0400417 test(r, "int main(float x) { return int (x); }", (float*)input, (float*)expected);
418 test(r, "int2 main(float2 x) { return int2(x); }", (float*)input, (float*)expected);
Brian Osman08a84962019-06-14 10:17:16 -0400419
420 input[0].s = 3;
421 expected[0].f = 3.0f;
422 expected[1].f = 3.0f;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400423 test(r, "float2 main(int x) { return float2(x); }", (float*)input, (float*)expected);
Brian Osman41672152019-05-14 13:37:30 -0400424}
425
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400426DEF_TEST(SkSLInterpreterIf, r) {
427 test(r, "void main(inout half4 color) { if (color.r > color.g) color.a = 1; }", 5, 3, 0, 0,
428 5, 3, 0, 1);
429 test(r, "void main(inout half4 color) { if (color.r > color.g) color.a = 1; }", 5, 5, 0, 0,
430 5, 5, 0, 0);
431 test(r, "void main(inout half4 color) { if (color.r > color.g) color.a = 1; }", 5, 6, 0, 0,
432 5, 6, 0, 0);
433 test(r, "void main(inout half4 color) { if (color.r < color.g) color.a = 1; }", 3, 5, 0, 0,
434 3, 5, 0, 1);
435 test(r, "void main(inout half4 color) { if (color.r < color.g) color.a = 1; }", 5, 5, 0, 0,
436 5, 5, 0, 0);
437 test(r, "void main(inout half4 color) { if (color.r < color.g) color.a = 1; }", 6, 5, 0, 0,
438 6, 5, 0, 0);
439 test(r, "void main(inout half4 color) { if (color.r >= color.g) color.a = 1; }", 5, 3, 0, 0,
440 5, 3, 0, 1);
441 test(r, "void main(inout half4 color) { if (color.r >= color.g) color.a = 1; }", 5, 5, 0, 0,
442 5, 5, 0, 1);
443 test(r, "void main(inout half4 color) { if (color.r >= color.g) color.a = 1; }", 5, 6, 0, 0,
444 5, 6, 0, 0);
445 test(r, "void main(inout half4 color) { if (color.r <= color.g) color.a = 1; }", 3, 5, 0, 0,
446 3, 5, 0, 1);
447 test(r, "void main(inout half4 color) { if (color.r <= color.g) color.a = 1; }", 5, 5, 0, 0,
448 5, 5, 0, 1);
449 test(r, "void main(inout half4 color) { if (color.r <= color.g) color.a = 1; }", 6, 5, 0, 0,
450 6, 5, 0, 0);
451 test(r, "void main(inout half4 color) { if (color.r == color.g) color.a = 1; }", 2, 2, 0, 0,
452 2, 2, 0, 1);
453 test(r, "void main(inout half4 color) { if (color.r == color.g) color.a = 1; }", 2, -2, 0, 0,
454 2, -2, 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, 0);
457 test(r, "void main(inout half4 color) { if (color.r != color.g) color.a = 1; }", 2, -2, 0, 0,
458 2, -2, 0, 1);
Brian Osmane5bbce22019-09-23 12:38:40 -0400459 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);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400463 test(r, "void main(inout half4 color) { if (color.r == color.g) color.a = 1; else "
464 "color.a = 2; }", 1, 1, 0, 0, 1, 1, 0, 1);
465 test(r, "void main(inout half4 color) { if (color.r == color.g) color.a = 1; else "
466 "color.a = 2; }", 2, -2, 0, 0, 2, -2, 0, 2);
467}
468
Brian Osman16e6fd52019-05-29 11:19:00 -0400469DEF_TEST(SkSLInterpreterIfVector, r) {
470 test(r, "void main(inout half4 color) { if (color.rg == color.ba) color.a = 1; }",
471 1, 2, 1, 2, 1, 2, 1, 1);
472 test(r, "void main(inout half4 color) { if (color.rg == color.ba) color.a = 1; }",
473 1, 2, 3, 2, 1, 2, 3, 2);
474 test(r, "void main(inout half4 color) { if (color.rg != color.ba) color.a = 1; }",
475 1, 2, 1, 2, 1, 2, 1, 2);
476 test(r, "void main(inout half4 color) { if (color.rg != color.ba) color.a = 1; }",
477 1, 2, 3, 2, 1, 2, 3, 1);
478}
479
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400480DEF_TEST(SkSLInterpreterWhile, r) {
Brian Osman52c1bf12019-07-18 13:12:19 -0400481 test(r, "void main(inout half4 color) { while (color.r < 8) { color.r++; } }",
482 1, 2, 3, 4, 8, 2, 3, 4);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400483 test(r, "void main(inout half4 color) { while (color.r < 1) color.r += 0.25; }", 0, 0, 0, 0, 1,
484 0, 0, 0);
Brian Osman569f12f2019-06-13 11:23:57 -0400485 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 -0400486 0, 0, 0);
487 test(r, "void main(inout half4 color) { while (true) { color.r += 0.5; "
Brian Osman569f12f2019-06-13 11:23:57 -0400488 "if (color.r > 5) break; } }", 0, 0, 0, 0, 5.5, 0, 0, 0);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400489 test(r, "void main(inout half4 color) { while (color.r < 10) { color.r += 0.5; "
490 "if (color.r < 5) continue; break; } }", 0, 0, 0, 0, 5, 0, 0, 0);
Brian Osman8d564572019-06-19 11:00:28 -0400491 test(r,
492 "void main(inout half4 color) {"
493 " while (true) {"
494 " if (color.r > 4) { break; }"
495 " while (true) { color.a = 1; break; }"
496 " break;"
497 " }"
498 "}",
499 6, 5, 4, 3, 6, 5, 4, 3);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400500}
501
502DEF_TEST(SkSLInterpreterDo, r) {
503 test(r, "void main(inout half4 color) { do color.r += 0.25; while (color.r < 1); }", 0, 0, 0, 0,
504 1, 0, 0, 0);
Brian Osman569f12f2019-06-13 11:23:57 -0400505 test(r, "void main(inout half4 color) { do color.r -= 0.25; while (color.r > 1); }", 0, 0, 0, 0,
506 -0.25, 0, 0, 0);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400507 test(r, "void main(inout half4 color) { do { color.r += 0.5; if (color.r > 1) break; } while "
508 "(true); }", 0, 0, 0, 0, 1.5, 0, 0, 0);
509 test(r, "void main(inout half4 color) {do { color.r += 0.5; if (color.r < 5) "
510 "continue; if (color.r >= 5) break; } while (true); }", 0, 0, 0, 0, 5, 0, 0, 0);
Brian Osman44d44762019-05-13 14:19:12 -0400511 test(r, "void main(inout half4 color) { do { color.r += 0.5; } while (false); }",
512 0, 0, 0, 0, 0.5, 0, 0, 0);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400513}
514
515DEF_TEST(SkSLInterpreterFor, r) {
516 test(r, "void main(inout half4 color) { for (int i = 1; i <= 10; ++i) color.r += i; }", 0, 0, 0,
517 0, 55, 0, 0, 0);
518 test(r,
519 "void main(inout half4 color) {"
520 " for (int i = 1; i <= 10; ++i)"
521 " for (int j = i; j <= 10; ++j)"
522 " color.r += j;"
523 "}",
524 0, 0, 0, 0,
525 385, 0, 0, 0);
526 test(r,
527 "void main(inout half4 color) {"
528 " for (int i = 1; i <= 10; ++i)"
529 " for (int j = 1; ; ++j) {"
530 " if (i == j) continue;"
531 " if (j > 10) break;"
532 " color.r += j;"
533 " }"
534 "}",
535 0, 0, 0, 0,
536 495, 0, 0, 0);
537}
538
Brian Osmanf3fa6002019-05-17 14:26:53 -0400539DEF_TEST(SkSLInterpreterPrefixPostfix, r) {
540 test(r, "void main(inout half4 color) { color.r = ++color.g; }", 1, 2, 3, 4, 3, 3, 3, 4);
541 test(r, "void main(inout half4 color) { color.r = color.g++; }", 1, 2, 3, 4, 2, 3, 3, 4);
542}
543
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400544DEF_TEST(SkSLInterpreterSwizzle, r) {
545 test(r, "void main(inout half4 color) { color = color.abgr; }", 1, 2, 3, 4, 4, 3, 2, 1);
546 test(r, "void main(inout half4 color) { color.rgb = half4(5, 6, 7, 8).bbg; }", 1, 2, 3, 4, 7, 7,
547 6, 4);
548 test(r, "void main(inout half4 color) { color.bgr = int3(5, 6, 7); }", 1, 2, 3, 4, 7, 6,
549 5, 4);
550}
551
552DEF_TEST(SkSLInterpreterGlobal, r) {
553 test(r, "int x; void main(inout half4 color) { x = 10; color.b = x; }", 1, 2, 3, 4, 1, 2, 10,
554 4);
Brian Osmanb7451292019-05-15 13:02:13 -0400555 test(r, "float4 x; void main(inout float4 color) { x = color * 2; color = x; }",
556 1, 2, 3, 4, 2, 4, 6, 8);
557 test(r, "float4 x; void main(inout float4 color) { x = float4(5, 6, 7, 8); color = x.wzyx; }",
558 1, 2, 3, 4, 8, 7, 6, 5);
Brian Osman1091f022019-05-16 09:42:16 -0400559 test(r, "float4 x; void main(inout float4 color) { x.wzyx = float4(5, 6, 7, 8); color = x; }",
560 1, 2, 3, 4, 8, 7, 6, 5);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400561}
Ethan Nicholas746035a2019-04-23 13:31:09 -0400562
563DEF_TEST(SkSLInterpreterGeneric, r) {
564 float value1 = 5;
565 float expected1 = 25;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400566 test(r, "float main(float x) { return x * x; }", &value1, &expected1);
Ethan Nicholas746035a2019-04-23 13:31:09 -0400567 float value2[2] = { 5, 25 };
568 float expected2[2] = { 25, 625 };
Brian Osmanb23d66e2019-09-27 10:25:57 -0400569 test(r, "float2 main(float x, float y) { return float2(x * x, y * y); }", value2, expected2);
Ethan Nicholas746035a2019-04-23 13:31:09 -0400570}
Brian Osmand369a5e2019-05-09 13:13:25 -0400571
Brian Osman07c117b2019-05-23 12:51:06 -0700572DEF_TEST(SkSLInterpreterCompound, r) {
573 struct RectAndColor { SkIRect fRect; SkColor4f fColor; };
574 struct ManyRects { int fNumRects; RectAndColor fRects[4]; };
575
576 const char* src =
577 // Some struct definitions
578 "struct Point { int x; int y; };\n"
579 "struct Rect { Point p0; Point p1; };\n"
580 "struct RectAndColor { Rect r; float4 color; };\n"
581
582 // Structs as globals, parameters, return values
583 "RectAndColor temp;\n"
584 "int rect_height(Rect r) { return r.p1.y - r.p0.y; }\n"
585 "RectAndColor make_blue_rect(int w, int h) {\n"
586 " temp.r.p0.x = temp.r.p0.y = 0;\n"
587 " temp.r.p1.x = w; temp.r.p1.y = h;\n"
588 " temp.color = float4(0, 1, 0, 1);\n"
589 " return temp;\n"
590 "}\n"
591
592 // Initialization and assignment of types larger than 4 slots
593 "RectAndColor init_big(RectAndColor r) { RectAndColor s = r; return s; }\n"
594 "RectAndColor copy_big(RectAndColor r) { RectAndColor s; s = r; return s; }\n"
595
596 // Same for arrays, including some non-constant indexing
Brian Osman07c117b2019-05-23 12:51:06 -0700597 "int median(int a[15]) { return a[7]; }\n"
John Stiles076e9a22020-12-03 10:37:45 -0500598
599 "float tempFloats[8];\n"
600 "float sums(float a[8]) {\n"
Brian Osman07c117b2019-05-23 12:51:06 -0700601 " tempFloats[0] = a[0];\n"
602 " for (int i = 1; i < 8; ++i) { tempFloats[i] = tempFloats[i - 1] + a[i]; }\n"
John Stiles076e9a22020-12-03 10:37:45 -0500603 " return tempFloats[7];\n"
Brian Osman07c117b2019-05-23 12:51:06 -0700604 "}\n"
605
606 // Uniforms, array-of-structs, dynamic indices
Ethan Nicholas31cff272019-09-26 13:04:48 -0400607 "uniform Rect gRects[4];\n"
Brian Osman07c117b2019-05-23 12:51:06 -0700608 "Rect get_rect(int i) { return gRects[i]; }\n"
609
610 // Kitchen sink (swizzles, inout, SoAoS)
611 "struct ManyRects { int numRects; RectAndColor rects[4]; };\n"
612 "void fill_rects(inout ManyRects mr) {\n"
613 " for (int i = 0; i < mr.numRects; ++i) {\n"
614 " mr.rects[i].r = gRects[i];\n"
615 " float b = mr.rects[i].r.p1.y;\n"
616 " mr.rects[i].color = float4(b, b, b, b);\n"
617 " }\n"
618 "}\n";
619
Brian Osman0006ad02020-11-18 15:38:39 -0500620 GrShaderCaps caps(GrContextOptions{});
621 SkSL::Compiler compiler(&caps);
Brian Osman07c117b2019-05-23 12:51:06 -0700622 SkSL::Program::Settings settings;
Ethan Nicholase8ad02c2020-06-03 16:58:20 -0400623 settings.fRemoveDeadFunctions = false;
John Stiles759880a2020-09-11 12:10:43 -0400624 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(SkSL::Program::kGeneric_Kind,
625 SkSL::String(src), settings);
Brian Osman07c117b2019-05-23 12:51:06 -0700626 REPORTER_ASSERT(r, program);
627
628 std::unique_ptr<SkSL::ByteCode> byteCode = compiler.toByteCode(*program);
629 REPORTER_ASSERT(r, !compiler.errorCount());
630
631 auto rect_height = byteCode->getFunction("rect_height"),
632 make_blue_rect = byteCode->getFunction("make_blue_rect"),
633 median = byteCode->getFunction("median"),
634 sums = byteCode->getFunction("sums"),
635 get_rect = byteCode->getFunction("get_rect"),
636 fill_rects = byteCode->getFunction("fill_rects");
637
638 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 -0400639 const float* fRects = (const float*)gRects;
Brian Osman07c117b2019-05-23 12:51:06 -0700640
Brian Osman07c117b2019-05-23 12:51:06 -0700641 {
642 SkIRect in = SkIRect::MakeXYWH(10, 10, 20, 30);
Brian Osmanb08cc022020-04-02 11:38:40 -0400643 int out = 0;
644 SkAssertResult(byteCode->run(rect_height, (float*)&in, 4, (float*)&out, 1, fRects, 16));
645 REPORTER_ASSERT(r, out == 30);
Brian Osman07c117b2019-05-23 12:51:06 -0700646 }
647
648 {
649 int in[2] = { 15, 25 };
Brian Osmanb08cc022020-04-02 11:38:40 -0400650 RectAndColor out;
651 SkAssertResult(byteCode->run(make_blue_rect, (float*)in, 2, (float*)&out, 8, fRects, 16));
652 REPORTER_ASSERT(r, out.fRect.width() == 15);
653 REPORTER_ASSERT(r, out.fRect.height() == 25);
Brian Osman07c117b2019-05-23 12:51:06 -0700654 SkColor4f blue = { 0.0f, 1.0f, 0.0f, 1.0f };
Brian Osmanb08cc022020-04-02 11:38:40 -0400655 REPORTER_ASSERT(r, out.fColor == blue);
Brian Osman07c117b2019-05-23 12:51:06 -0700656 }
657
658 {
659 int in[15] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
Brian Osmanb08cc022020-04-02 11:38:40 -0400660 int out = 0;
661 SkAssertResult(byteCode->run(median, (float*)in, 15, (float*)&out, 1, fRects, 16));
662 REPORTER_ASSERT(r, out == 8);
Brian Osman07c117b2019-05-23 12:51:06 -0700663 }
664
665 {
666 float in[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
John Stiles076e9a22020-12-03 10:37:45 -0500667 float out = 0;
668 SkAssertResult(byteCode->run(sums, in, 8, &out, 1, fRects, 16));
669 REPORTER_ASSERT(r, out == static_cast<float>((7 + 1) * (7 + 2) / 2));
Brian Osman07c117b2019-05-23 12:51:06 -0700670 }
671
672 {
673 int in = 2;
Brian Osmanb08cc022020-04-02 11:38:40 -0400674 SkIRect out = SkIRect::MakeEmpty();
675 SkAssertResult(byteCode->run(get_rect, (float*)&in, 1, (float*)&out, 4, fRects, 16));
676 REPORTER_ASSERT(r, out == gRects[2]);
Brian Osman07c117b2019-05-23 12:51:06 -0700677 }
678
679 {
680 ManyRects in;
681 memset(&in, 0, sizeof(in));
682 in.fNumRects = 2;
Brian Osmanb08cc022020-04-02 11:38:40 -0400683 SkAssertResult(byteCode->run(fill_rects, (float*)&in, 33, nullptr, 0, fRects, 16));
Brian Osman07c117b2019-05-23 12:51:06 -0700684 ManyRects expected;
685 memset(&expected, 0, sizeof(expected));
686 expected.fNumRects = 2;
687 for (int i = 0; i < 2; ++i) {
688 expected.fRects[i].fRect = gRects[i];
689 float c = gRects[i].fBottom;
690 expected.fRects[i].fColor = { c, c, c, c };
691 }
692 REPORTER_ASSERT(r, memcmp(&in, &expected, sizeof(in)) == 0);
693 }
694}
Ethan Nicholas91164d12019-05-15 15:29:54 -0400695
Brian Osman869a3e82019-07-18 17:00:34 -0400696static void expect_failure(skiatest::Reporter* r, const char* src) {
Brian Osman0006ad02020-11-18 15:38:39 -0500697 GrShaderCaps caps(GrContextOptions{});
698 SkSL::Compiler compiler(&caps);
John Stiles759880a2020-09-11 12:10:43 -0400699 SkSL::Program::Settings settings;
700 auto program = compiler.convertProgram(SkSL::Program::kGeneric_Kind,
701 SkSL::String(src), settings);
Brian Osman869a3e82019-07-18 17:00:34 -0400702 REPORTER_ASSERT(r, program);
703
704 auto byteCode = compiler.toByteCode(*program);
705 REPORTER_ASSERT(r, compiler.errorCount() > 0);
706 REPORTER_ASSERT(r, !byteCode);
707}
708
709static void expect_run_failure(skiatest::Reporter* r, const char* src, float* in) {
Brian Osman0006ad02020-11-18 15:38:39 -0500710 GrShaderCaps caps(GrContextOptions{});
711 SkSL::Compiler compiler(&caps);
John Stiles759880a2020-09-11 12:10:43 -0400712 SkSL::Program::Settings settings;
713 auto program = compiler.convertProgram(SkSL::Program::kGeneric_Kind,
714 SkSL::String(src), settings);
Brian Osman869a3e82019-07-18 17:00:34 -0400715 REPORTER_ASSERT(r, program);
716
717 auto byteCode = compiler.toByteCode(*program);
718 REPORTER_ASSERT(r, byteCode);
719
Brian Osmanb08cc022020-04-02 11:38:40 -0400720 auto fun = byteCode->getFunction("main");
721 bool result = byteCode->run(fun, in, fun->getParameterCount(), nullptr, 0, nullptr, 0);
722 REPORTER_ASSERT(r, !result);
Brian Osman869a3e82019-07-18 17:00:34 -0400723}
724
Brian Osman6f5358f2019-07-09 14:17:23 -0400725DEF_TEST(SkSLInterpreterRestrictFunctionCalls, r) {
Brian Osman6f5358f2019-07-09 14:17:23 -0400726 // Ensure that simple recursion is not allowed
Brian Osman869a3e82019-07-18 17:00:34 -0400727 expect_failure(r, "float main() { return main() + 1; }");
Brian Osman6f5358f2019-07-09 14:17:23 -0400728
729 // Ensure that calls to undefined functions are not allowed (to prevent mutual recursion)
Brian Osman869a3e82019-07-18 17:00:34 -0400730 expect_failure(r, "float foo(); float bar() { return foo(); } float foo() { return bar(); }");
Brian Osman4a47da72019-07-12 11:30:32 -0400731
Mike Klein51dc2852020-10-15 19:41:20 -0500732 // returns are not allowed inside loops
Brian Osmand6f23382020-12-15 17:08:59 -0500733 expect_failure(r, "float main(float x)"
734 "{ while (x > 1) { if (x > 2) { return x; } } return 0; }");
Brian Osman869a3e82019-07-18 17:00:34 -0400735}
736
Mike Klein51dc2852020-10-15 19:41:20 -0500737DEF_TEST(SkSLInterpreterEarlyReturn, r) {
738 // Unlike returns in loops, returns in conditionals should work.
739 const char* src = "float main(float x, float y) { if (x < y) { return x; } return y; }";
740
Brian Osman0006ad02020-11-18 15:38:39 -0500741 GrShaderCaps caps(GrContextOptions{});
742 SkSL::Compiler compiler(&caps);
Mike Klein51dc2852020-10-15 19:41:20 -0500743 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(SkSL::Program::kGeneric_Kind,
744 SkSL::String(src),
745 SkSL::Program::Settings{});
746 REPORTER_ASSERT(r, program);
747
748 std::unique_ptr<SkSL::ByteCode> byteCode = compiler.toByteCode(*program);
749 REPORTER_ASSERT(r, byteCode);
750
751 // Our function should work fine via run().
752 const SkSL::ByteCodeFunction* fun = byteCode->getFunction("main");
753 float in[] = { 1.0f, 2.0f };
754 float ret;
755 REPORTER_ASSERT(r, byteCode->run(fun, in,2, &ret,1, nullptr,0));
756 REPORTER_ASSERT(r, ret == 1.0f);
757
758 in[0] = 3.0f;
759 REPORTER_ASSERT(r, byteCode->run(fun, in,2, &ret,1, nullptr,0));
760 REPORTER_ASSERT(r, ret == 2.0f);
761
762 // Now same again via runStriped(), won't quite work yet.
763 float xs[] = { 1.0f, 3.0f },
764 ys[] = { 2.0f, 2.0f };
765 float rets[2];
766 float* ins[] = { xs, ys };
767 float* outs[] = { rets+0, rets+1 } ;
768 REPORTER_ASSERT(r, byteCode->runStriped(fun,2, ins,2, outs,1, nullptr,0));
769 REPORTER_ASSERT(r, rets[0] == 1.0f);
770 //REPORTER_ASSERT(r, rets[1] == 2.0f); // TODO: skia:10852, make striped early returns work.
771}
772
Brian Osman869a3e82019-07-18 17:00:34 -0400773DEF_TEST(SkSLInterpreterArrayBounds, r) {
John Stiles1b27c3d2020-12-07 12:14:55 -0500774 // Out of bounds array access at compile time prevents a program from being generated at all
775 // (tested in ArrayIndexOutOfRange.sksl).
Brian Osman869a3e82019-07-18 17:00:34 -0400776
John Stiles1b27c3d2020-12-07 12:14:55 -0500777 // Out of bounds array access at runtime is pinned, and we don't update any inout data.
Brian Osman869a3e82019-07-18 17:00:34 -0400778 float in[3] = { -1.0f, 1.0f, 2.0f };
779 expect_run_failure(r, "void main(inout float data[3]) { data[int(data[0])] = 0; }", in);
780 REPORTER_ASSERT(r, in[0] == -1.0f && in[1] == 1.0f && in[2] == 2.0f);
781
782 in[0] = 3.0f;
783 expect_run_failure(r, "void main(inout float data[3]) { data[int(data[0])] = 0; }", in);
784 REPORTER_ASSERT(r, in[0] == 3.0f && in[1] == 1.0f && in[2] == 2.0f);
Brian Osman6f5358f2019-07-09 14:17:23 -0400785}
786
Brian Osman226668a2019-05-14 16:47:30 -0400787DEF_TEST(SkSLInterpreterFunctions, r) {
788 const char* src =
789 "float sqr(float x) { return x * x; }\n"
Brian Osman226668a2019-05-14 16:47:30 -0400790 "float sub(float x, float y) { return x - y; }\n"
Brian Osman6f5358f2019-07-09 14:17:23 -0400791 "float main(float x) { return sub(sqr(x), x); }\n"
Brian Osman226668a2019-05-14 16:47:30 -0400792
793 // Different signatures
Brian Osman15c98cb2020-02-27 18:36:57 +0000794 "float dot(float2 a, float2 b) { return a.x*b.x + a.y*b.y; }\n"
795 "float dot(float3 a, float3 b) { return a.x*b.x + a.y*b.y + a.z*b.z; }\n"
Brian Osman226668a2019-05-14 16:47:30 -0400796 "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 -0400797 "float dot2_test(float x) { return dot(float2(x, x + 1), float2(1, -1)); }\n";
Brian Osman226668a2019-05-14 16:47:30 -0400798
Brian Osman0006ad02020-11-18 15:38:39 -0500799 GrShaderCaps caps(GrContextOptions{});
800 SkSL::Compiler compiler(&caps);
Brian Osman226668a2019-05-14 16:47:30 -0400801 SkSL::Program::Settings settings;
Ethan Nicholase8ad02c2020-06-03 16:58:20 -0400802 settings.fRemoveDeadFunctions = false;
John Stiles759880a2020-09-11 12:10:43 -0400803 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(SkSL::Program::kGeneric_Kind,
804 SkSL::String(src), settings);
Brian Osman226668a2019-05-14 16:47:30 -0400805 REPORTER_ASSERT(r, program);
806
807 std::unique_ptr<SkSL::ByteCode> byteCode = compiler.toByteCode(*program);
808 REPORTER_ASSERT(r, !compiler.errorCount());
809
810 auto sub = byteCode->getFunction("sub");
811 auto sqr = byteCode->getFunction("sqr");
812 auto main = byteCode->getFunction("main");
813 auto tan = byteCode->getFunction("tan");
814 auto dot3 = byteCode->getFunction("dot3_test");
815 auto dot2 = byteCode->getFunction("dot2_test");
Brian Osman226668a2019-05-14 16:47:30 -0400816
817 REPORTER_ASSERT(r, sub);
818 REPORTER_ASSERT(r, sqr);
819 REPORTER_ASSERT(r, main);
820 REPORTER_ASSERT(r, !tan);
821 REPORTER_ASSERT(r, dot3);
822 REPORTER_ASSERT(r, dot2);
Brian Osman226668a2019-05-14 16:47:30 -0400823
Brian Osmanb08cc022020-04-02 11:38:40 -0400824 float out = 0.0f;
Brian Osman226668a2019-05-14 16:47:30 -0400825 float in = 3.0f;
Brian Osmanb08cc022020-04-02 11:38:40 -0400826 SkAssertResult(byteCode->run(main, &in, 1, &out, 1, nullptr, 0));
827 REPORTER_ASSERT(r, out = 6.0f);
Brian Osman226668a2019-05-14 16:47:30 -0400828
Brian Osmanb08cc022020-04-02 11:38:40 -0400829 SkAssertResult(byteCode->run(dot3, &in, 1, &out, 1, nullptr, 0));
830 REPORTER_ASSERT(r, out = 9.0f);
Brian Osman226668a2019-05-14 16:47:30 -0400831
Brian Osmanb08cc022020-04-02 11:38:40 -0400832 SkAssertResult(byteCode->run(dot2, &in, 1, &out, 1, nullptr, 0));
833 REPORTER_ASSERT(r, out = -1.0f);
Brian Osman8c80b192020-02-06 10:49:38 -0500834}
835
Brian Osmand3494ed2019-06-20 15:41:34 -0400836DEF_TEST(SkSLInterpreterOutParams, r) {
837 test(r,
838 "void oneAlpha(inout half4 color) { color.a = 1; }"
839 "void main(inout half4 color) { oneAlpha(color); }",
840 0, 0, 0, 0, 0, 0, 0, 1);
841 test(r,
Brian Osmanb08cc022020-04-02 11:38:40 -0400842 "half2 tricky(half x, half y, inout half2 color, half z) {"
Brian Osmand3494ed2019-06-20 15:41:34 -0400843 " color.xy = color.yx;"
844 " return half2(x + y, z);"
845 "}"
846 "void main(inout half4 color) {"
Brian Osmanb08cc022020-04-02 11:38:40 -0400847 " half2 t = tricky(1, 2, color.rb, 5);"
Brian Osmand3494ed2019-06-20 15:41:34 -0400848 " color.ga = t;"
849 "}",
Brian Osmanb08cc022020-04-02 11:38:40 -0400850 1, 2, 3, 4, 3, 3, 1, 5);
Brian Osmand3494ed2019-06-20 15:41:34 -0400851}
852
Brian Osman401a3662020-09-29 13:20:04 -0400853DEF_TEST(SkSLInterpreterSwizzleSingleLvalue, r) {
854 // Add in your SkSL here.
855 test(r,
856 "void main(inout half4 color) { color.xywz = half4(1,2,3,4); }",
857 0, 0, 0, 0, 1, 2, 4, 3);
858}
859
860DEF_TEST(SkSLInterpreterSwizzleDoubleLvalue, r) {
861 // Add in your SkSL here.
862 test(r,
863 "void main(inout half4 color) { color.xywz.yxzw = half4(1,2,3,4); }",
864 0, 0, 0, 0, 2, 1, 4, 3);
865}
866
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400867DEF_TEST(SkSLInterpreterMathFunctions, r) {
Brian Osmanb380e712019-07-24 17:02:39 -0400868 float value[4], expected[4];
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400869
Brian Osmanb08cc022020-04-02 11:38:40 -0400870 value[0] = 0.0f; expected[0] = 0.0f;
871 test(r, "float main(float x) { return sin(x); }", value, expected);
872 test(r, "float main(float x) { return tan(x); }", value, expected);
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400873
Brian Osmanb380e712019-07-24 17:02:39 -0400874 value[0] = 0.0f; expected[0] = 1.0f;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400875 test(r, "float main(float x) { return cos(x); }", value, expected);
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400876
Brian Osmanb380e712019-07-24 17:02:39 -0400877 value[0] = 25.0f; expected[0] = 5.0f;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400878 test(r, "float main(float x) { return sqrt(x); }", value, expected);
Brian Osmanb380e712019-07-24 17:02:39 -0400879
880 value[0] = 90.0f; expected[0] = sk_float_degrees_to_radians(value[0]);
Brian Osmanb23d66e2019-09-27 10:25:57 -0400881 test(r, "float main(float x) { return radians(x); }", value, expected);
Brian Osmanb380e712019-07-24 17:02:39 -0400882
883 value[0] = 1.0f; value[1] = -1.0f;
884 expected[0] = 1.0f / SK_FloatSqrt2; expected[1] = -1.0f / SK_FloatSqrt2;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400885 test(r, "float2 main(float2 x) { return normalize(x); }", value, expected);
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400886}
887
Brian Osmanfba386b2019-06-20 14:54:15 -0400888DEF_TEST(SkSLInterpreterVoidFunction, r) {
889 test(r,
890 "half x; void foo() { x = 1.0; }"
891 "void main(inout half4 color) { foo(); color.r = x; }",
892 0, 0, 0, 0, 1, 0, 0, 0);
893}
894
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400895DEF_TEST(SkSLInterpreterMix, r) {
Brian Osman08a84962019-06-14 10:17:16 -0400896 float value, expected;
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400897
898 value = 0.5f; expected = 0.0f;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400899 test(r, "float main(float x) { return mix(-10, 10, x); }", &value, &expected);
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400900 value = 0.75f; expected = 5.0f;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400901 test(r, "float main(float x) { return mix(-10, 10, x); }", &value, &expected);
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400902 value = 2.0f; expected = 30.0f;
Brian Osmanb23d66e2019-09-27 10:25:57 -0400903 test(r, "float main(float x) { return mix(-10, 10, x); }", &value, &expected);
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400904
Brian Osman08a84962019-06-14 10:17:16 -0400905 float valueVectors[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f },
906 expectedVector[] = { 3.0f, 4.0f, 5.0f, 6.0f };
Brian Osmanb23d66e2019-09-27 10:25:57 -0400907 test(r, "float4 main(float4 x, float4 y) { return mix(x, y, 0.5); }", valueVectors,
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400908 expectedVector);
909}
910
911DEF_TEST(SkSLInterpreterCross, r) {
Brian Osman08a84962019-06-14 10:17:16 -0400912 float args[] = { 1.0f, 4.0f, -6.0f, -2.0f, 7.0f, -3.0f };
Mike Reedb26b4e72020-01-22 14:31:21 -0500913 SkV3 cross = SkV3::Cross({args[0], args[1], args[2]},
914 {args[3], args[4], args[5]});
915 float expected[] = { cross.x, cross.y, cross.z };
Brian Osmanb23d66e2019-09-27 10:25:57 -0400916 test(r, "float3 main(float3 x, float3 y) { return cross(x, y); }", args, expected);
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400917}
Ben Wagner470e0ac2020-01-22 16:59:21 -0500918
Mike Reed634c9412019-07-18 13:20:04 -0400919DEF_TEST(SkSLInterpreterInverse, r) {
920 {
921 SkMatrix m;
922 m.setRotate(30).postScale(1, 2);
923 float args[4] = { m[0], m[3], m[1], m[4] };
924 SkAssertResult(m.invert(&m));
925 float expt[4] = { m[0], m[3], m[1], m[4] };
Ben Wagner470e0ac2020-01-22 16:59:21 -0500926 test(r, "float2x2 main(float2x2 m) { return inverse(m); }", args, expt, false);
Mike Reed634c9412019-07-18 13:20:04 -0400927 }
928 {
929 SkMatrix m;
930 m.setRotate(30).postScale(1, 2).postTranslate(1, 2);
931 float args[9] = { m[0], m[3], m[6], m[1], m[4], m[7], m[2], m[5], m[8] };
932 SkAssertResult(m.invert(&m));
933 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 -0500934 test(r, "float3x3 main(float3x3 m) { return inverse(m); }", args, expt, false);
Mike Reed634c9412019-07-18 13:20:04 -0400935 }
936 {
937 float args[16], expt[16];
Mike Reed634c9412019-07-18 13:20:04 -0400938 // just some crazy thing that is invertible
Mike Reedb26b4e72020-01-22 14:31:21 -0500939 SkM44 m = {1, 2, 3, 4, 1, 2, 0, 3, 1, 0, 1, 4, 1, 3, 2, 0};
940 m.getColMajor(args);
Mike Reed634c9412019-07-18 13:20:04 -0400941 SkAssertResult(m.invert(&m));
Ben Wagner470e0ac2020-01-22 16:59:21 -0500942 m.getColMajor(expt);
943 test(r, "float4x4 main(float4x4 m) { return inverse(m); }", args, expt, false);
944 }
945}
946
947DEF_TEST(SkSLInterpreterDot, r) {
948 float args[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f };
949 float expected = args[0] * args[2] +
950 args[1] * args[3];
951 test(r, "float main(float2 x, float2 y) { return dot(x, y); }", args, &expected);
952
953 expected = args[0] * args[3] +
954 args[1] * args[4] +
955 args[2] * args[5];
956 test(r, "float main(float3 x, float3 y) { return dot(x, y); }", args, &expected);
957
958 expected = args[0] * args[4] +
959 args[1] * args[5] +
960 args[2] * args[6] +
961 args[3] * args[7];
962 test(r, "float main(float4 x, float4 y) { return dot(x, y); }", args, &expected);
963}
964
965static const SkSL::Type& type_of(const skjson::Value* value, SkSL::Compiler* compiler) {
966 switch (value->getType()) {
967 case skjson::Value::Type::kNumber: {
968 float f = *value->as<skjson::NumberValue>();
969 if (f == (float) (int) f) {
970 return *compiler->context().fInt_Type;
971 }
972 return *compiler->context().fFloat_Type;
973 }
974 case skjson::Value::Type::kBool:
975 return *compiler->context().fBool_Type;
976 default:
977 return *compiler->context().fVoid_Type;
978 }
979}
980
981class JSONExternalValue : public SkSL::ExternalValue {
982public:
983 JSONExternalValue(const char* name, const skjson::Value* value, SkSL::Compiler* compiler)
984 : INHERITED(name, type_of(value, compiler))
985 , fValue(*value)
986 , fCompiler(*compiler) {}
987
988 bool canRead() const override {
989 return type() != *fCompiler.context().fVoid_Type;
990 }
991
John Stiles534d7992020-08-18 00:06:01 -0400992 void read(int /*unusedIndex*/, float* target) const override {
Ben Wagner470e0ac2020-01-22 16:59:21 -0500993 if (type() == *fCompiler.context().fInt_Type) {
994 *(int*) target = *fValue.as<skjson::NumberValue>();
995 } else if (type() == *fCompiler.context().fFloat_Type) {
996 *(float*) target = *fValue.as<skjson::NumberValue>();
997 } else if (type() == *fCompiler.context().fBool_Type) {
998 // ByteCode "booleans" are actually bit-masks
999 *(int*) target = *fValue.as<skjson::BoolValue>() ? ~0 : 0;
1000 } else {
1001 SkASSERT(false);
1002 }
1003 }
1004
1005 SkSL::ExternalValue* getChild(const char* name) const override {
1006 if (fValue.getType() == skjson::Value::Type::kObject) {
1007 const skjson::Value& v = fValue.as<skjson::ObjectValue>()[name];
Brian Osman32d53552020-09-23 13:55:20 -04001008 fOwned.push_back(std::make_unique<JSONExternalValue>(name, &v, &fCompiler));
1009 return fOwned.back().get();
Ben Wagner470e0ac2020-01-22 16:59:21 -05001010 }
1011 return nullptr;
1012 }
1013
1014private:
1015 const skjson::Value& fValue;
1016 SkSL::Compiler& fCompiler;
Brian Osman32d53552020-09-23 13:55:20 -04001017 mutable std::vector<std::unique_ptr<SkSL::ExternalValue>> fOwned;
Ben Wagner470e0ac2020-01-22 16:59:21 -05001018
John Stiles7571f9e2020-09-02 22:42:33 -04001019 using INHERITED = SkSL::ExternalValue;
Ben Wagner470e0ac2020-01-22 16:59:21 -05001020};
1021
1022class PointerExternalValue : public SkSL::ExternalValue {
1023public:
1024 PointerExternalValue(const char* name, const SkSL::Type& type, void* data, size_t size)
1025 : INHERITED(name, type)
Ethan Nicholas7bd60432020-09-25 14:31:59 -04001026 , fBytes(data)
Ben Wagner470e0ac2020-01-22 16:59:21 -05001027 , fSize(size) {}
1028
1029 bool canRead() const override {
1030 return true;
1031 }
1032
1033 bool canWrite() const override {
1034 return true;
1035 }
1036
John Stiles534d7992020-08-18 00:06:01 -04001037 void read(int /*unusedIndex*/, float* target) const override {
Ethan Nicholas7bd60432020-09-25 14:31:59 -04001038 memcpy(target, fBytes, fSize);
Ben Wagner470e0ac2020-01-22 16:59:21 -05001039 }
1040
John Stiles534d7992020-08-18 00:06:01 -04001041 void write(int /*unusedIndex*/, float* src) const override {
Ethan Nicholas7bd60432020-09-25 14:31:59 -04001042 memcpy(fBytes, src, fSize);
Ben Wagner470e0ac2020-01-22 16:59:21 -05001043 }
1044
1045
1046private:
Ethan Nicholas7bd60432020-09-25 14:31:59 -04001047 void* fBytes;
Ben Wagner470e0ac2020-01-22 16:59:21 -05001048 size_t fSize;
1049
John Stiles7571f9e2020-09-02 22:42:33 -04001050 using INHERITED = SkSL::ExternalValue;
Ben Wagner470e0ac2020-01-22 16:59:21 -05001051};
1052
1053DEF_TEST(SkSLInterpreterExternalValues, r) {
1054 const char* json = "{ \"value1\": 12, \"child\": { \"value2\": true, \"value3\": 5.5 } }";
1055 skjson::DOM dom(json, strlen(json));
Brian Osman0006ad02020-11-18 15:38:39 -05001056 GrShaderCaps caps(GrContextOptions{});
1057 SkSL::Compiler compiler(&caps);
Ben Wagner470e0ac2020-01-22 16:59:21 -05001058 SkSL::Program::Settings settings;
1059 const char* src = "float main() {"
1060 " outValue = 152;"
1061 " return root.child.value2 ? root.value1 * root.child.value3 : -1;"
1062 "}";
Ben Wagner470e0ac2020-01-22 16:59:21 -05001063 int32_t outValue = -1;
Brian Osman32d53552020-09-23 13:55:20 -04001064 std::vector<std::unique_ptr<SkSL::ExternalValue>> externalValues;
1065 externalValues.push_back(std::make_unique<JSONExternalValue>("root", &dom.root(), &compiler));
1066 externalValues.push_back(std::make_unique<PointerExternalValue>(
1067 "outValue", *compiler.context().fInt_Type, &outValue, sizeof(outValue)));
Ben Wagner470e0ac2020-01-22 16:59:21 -05001068 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(
Brian Osman32d53552020-09-23 13:55:20 -04001069 SkSL::Program::kGeneric_Kind, SkSL::String(src), settings, &externalValues);
Ben Wagner470e0ac2020-01-22 16:59:21 -05001070 REPORTER_ASSERT(r, program);
1071 if (program) {
1072 std::unique_ptr<SkSL::ByteCode> byteCode = compiler.toByteCode(*program);
1073 REPORTER_ASSERT(r, !compiler.errorCount());
1074 if (compiler.errorCount() > 0) {
1075 printf("%s\n%s", src, compiler.errorText().c_str());
1076 return;
1077 }
1078 const SkSL::ByteCodeFunction* main = byteCode->getFunction("main");
Brian Osmanb08cc022020-04-02 11:38:40 -04001079 float out;
1080 SkAssertResult(byteCode->run(main, nullptr, 0, &out, 1, nullptr, 0));
1081 REPORTER_ASSERT(r, out == 66.0);
Ben Wagner470e0ac2020-01-22 16:59:21 -05001082 REPORTER_ASSERT(r, outValue == 152);
1083 } else {
1084 printf("%s\n%s", src, compiler.errorText().c_str());
1085 }
1086}
1087
1088DEF_TEST(SkSLInterpreterExternalValuesVector, r) {
Brian Osman0006ad02020-11-18 15:38:39 -05001089 GrShaderCaps caps(GrContextOptions{});
1090 SkSL::Compiler compiler(&caps);
Ben Wagner470e0ac2020-01-22 16:59:21 -05001091 SkSL::Program::Settings settings;
1092 const char* src = "void main() {"
1093 " value *= 2;"
1094 "}";
1095 int32_t value[4] = { 1, 2, 3, 4 };
Brian Osman32d53552020-09-23 13:55:20 -04001096 std::vector<std::unique_ptr<SkSL::ExternalValue>> externalValues;
1097 externalValues.push_back(std::make_unique<PointerExternalValue>(
1098 "value", *compiler.context().fInt4_Type, value, sizeof(value)));
1099 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(
1100 SkSL::Program::kGeneric_Kind, SkSL::String(src), settings, &externalValues);
Ben Wagner470e0ac2020-01-22 16:59:21 -05001101 REPORTER_ASSERT(r, program);
1102 if (program) {
1103 std::unique_ptr<SkSL::ByteCode> byteCode = compiler.toByteCode(*program);
1104 REPORTER_ASSERT(r, !compiler.errorCount());
1105 if (compiler.errorCount() > 0) {
1106 printf("%s\n%s", src, compiler.errorText().c_str());
1107 return;
1108 }
1109 const SkSL::ByteCodeFunction* main = byteCode->getFunction("main");
Brian Osmanb08cc022020-04-02 11:38:40 -04001110 SkAssertResult(byteCode->run(main, nullptr, 0, nullptr, 0, nullptr, 0));
Ben Wagner470e0ac2020-01-22 16:59:21 -05001111 REPORTER_ASSERT(r, value[0] == 2);
1112 REPORTER_ASSERT(r, value[1] == 4);
1113 REPORTER_ASSERT(r, value[2] == 6);
1114 REPORTER_ASSERT(r, value[3] == 8);
1115 } else {
1116 printf("%s\n%s", src, compiler.errorText().c_str());
1117 }
1118}
1119
1120class FunctionExternalValue : public SkSL::ExternalValue {
1121public:
1122 FunctionExternalValue(const char* name, float(*function)(float), SkSL::Compiler& compiler)
1123 : INHERITED(name, *compiler.context().fFloat_Type)
1124 , fCompiler(compiler)
1125 , fFunction(function) {}
1126
1127 bool canCall() const override {
1128 return true;
1129 }
1130
1131 int callParameterCount() const override {
1132 return 1;
1133 }
1134
1135 void getCallParameterTypes(const SkSL::Type** outTypes) const override {
1136 outTypes[0] = fCompiler.context().fFloat_Type.get();
1137 }
1138
John Stiles534d7992020-08-18 00:06:01 -04001139 void call(int /*unusedIndex*/, float* arguments, float* outReturn) const override {
Ben Wagner470e0ac2020-01-22 16:59:21 -05001140 outReturn[0] = fFunction(arguments[0]);
1141 }
1142
1143private:
1144 SkSL::Compiler& fCompiler;
1145
1146 float (*fFunction)(float);
1147
John Stiles7571f9e2020-09-02 22:42:33 -04001148 using INHERITED = SkSL::ExternalValue;
Ben Wagner470e0ac2020-01-22 16:59:21 -05001149};
1150
1151DEF_TEST(SkSLInterpreterExternalValuesCall, r) {
Brian Osman0006ad02020-11-18 15:38:39 -05001152 GrShaderCaps caps(GrContextOptions{});
1153 SkSL::Compiler compiler(&caps);
Ben Wagner470e0ac2020-01-22 16:59:21 -05001154 SkSL::Program::Settings settings;
1155 const char* src = "float main() {"
1156 " return external(25);"
1157 "}";
Brian Osman32d53552020-09-23 13:55:20 -04001158 std::vector<std::unique_ptr<SkSL::ExternalValue>> externalValues;
1159 externalValues.push_back(std::make_unique<FunctionExternalValue>(
1160 "external", [](float x) { return (float)sqrt(x); }, compiler));
1161 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(
1162 SkSL::Program::kGeneric_Kind, SkSL::String(src), settings, &externalValues);
Ben Wagner470e0ac2020-01-22 16:59:21 -05001163 REPORTER_ASSERT(r, program);
1164 if (program) {
1165 std::unique_ptr<SkSL::ByteCode> byteCode = compiler.toByteCode(*program);
1166 REPORTER_ASSERT(r, !compiler.errorCount());
1167 if (compiler.errorCount() > 0) {
1168 printf("%s\n%s", src, compiler.errorText().c_str());
1169 return;
1170 }
1171 const SkSL::ByteCodeFunction* main = byteCode->getFunction("main");
Brian Osmanb08cc022020-04-02 11:38:40 -04001172 float out;
1173 SkAssertResult(byteCode->run(main, nullptr, 0, &out, 1, nullptr, 0));
1174 REPORTER_ASSERT(r, out == 5.0);
Ben Wagner470e0ac2020-01-22 16:59:21 -05001175 } else {
1176 printf("%s\n%s", src, compiler.errorText().c_str());
1177 }
1178}
1179
1180class VectorFunctionExternalValue : public SkSL::ExternalValue {
1181public:
1182 VectorFunctionExternalValue(const char* name, void(*function)(float[4], float[4]),
1183 SkSL::Compiler& compiler)
1184 : INHERITED(name, *compiler.context().fFloat4_Type)
1185 , fCompiler(compiler)
1186 , fFunction(function) {}
Brian Osmanb08cc022020-04-02 11:38:40 -04001187
Ben Wagner470e0ac2020-01-22 16:59:21 -05001188 bool canCall() const override {
1189 return true;
1190 }
Brian Osmanb08cc022020-04-02 11:38:40 -04001191
Ben Wagner470e0ac2020-01-22 16:59:21 -05001192 int callParameterCount() const override {
1193 return 1;
1194 }
Brian Osmanb08cc022020-04-02 11:38:40 -04001195
Ben Wagner470e0ac2020-01-22 16:59:21 -05001196 void getCallParameterTypes(const SkSL::Type** outTypes) const override {
1197 outTypes[0] = fCompiler.context().fFloat4_Type.get();
1198 }
Brian Osmanb08cc022020-04-02 11:38:40 -04001199
John Stiles534d7992020-08-18 00:06:01 -04001200 void call(int /*unusedIndex*/, float* arguments, float* outReturn) const override {
Ben Wagner470e0ac2020-01-22 16:59:21 -05001201 fFunction(arguments, outReturn);
1202 }
Brian Osmanb08cc022020-04-02 11:38:40 -04001203
Ben Wagner470e0ac2020-01-22 16:59:21 -05001204private:
1205 SkSL::Compiler& fCompiler;
Brian Osmanb08cc022020-04-02 11:38:40 -04001206
Ben Wagner470e0ac2020-01-22 16:59:21 -05001207 void (*fFunction)(float[4], float[4]);
Brian Osmanb08cc022020-04-02 11:38:40 -04001208
John Stiles7571f9e2020-09-02 22:42:33 -04001209 using INHERITED = SkSL::ExternalValue;
Ben Wagner470e0ac2020-01-22 16:59:21 -05001210};
Brian Osmanb08cc022020-04-02 11:38:40 -04001211
1212
Ben Wagner470e0ac2020-01-22 16:59:21 -05001213DEF_TEST(SkSLInterpreterExternalValuesVectorCall, r) {
Brian Osman0006ad02020-11-18 15:38:39 -05001214 GrShaderCaps caps(GrContextOptions{});
1215 SkSL::Compiler compiler(&caps);
Ben Wagner470e0ac2020-01-22 16:59:21 -05001216 SkSL::Program::Settings settings;
Brian Osman32d53552020-09-23 13:55:20 -04001217 const char* src =
1218 "float4 main() {"
1219 " return external(float4(1, 4, 9, 16));"
1220 "}";
1221 std::vector<std::unique_ptr<SkSL::ExternalValue>> externalValues;
1222 externalValues.push_back(std::make_unique<VectorFunctionExternalValue>(
1223 "external",
1224 [](float in[4], float out[4]) {
1225 out[0] = sqrt(in[0]);
1226 out[1] = sqrt(in[1]);
1227 out[2] = sqrt(in[2]);
1228 out[3] = sqrt(in[3]);
1229 },
1230 compiler));
1231 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(
1232 SkSL::Program::kGeneric_Kind, SkSL::String(src), settings, &externalValues);
Ben Wagner470e0ac2020-01-22 16:59:21 -05001233 REPORTER_ASSERT(r, program);
1234 if (program) {
1235 std::unique_ptr<SkSL::ByteCode> byteCode = compiler.toByteCode(*program);
1236 REPORTER_ASSERT(r, !compiler.errorCount());
1237 if (compiler.errorCount() > 0) {
1238 printf("%s\n%s", src, compiler.errorText().c_str());
1239 return;
1240 }
1241 const SkSL::ByteCodeFunction* main = byteCode->getFunction("main");
Brian Osmanb08cc022020-04-02 11:38:40 -04001242 float out[4];
1243 SkAssertResult(byteCode->run(main, nullptr, 0, out, 4, nullptr, 0));
1244 REPORTER_ASSERT(r, out[0] == 1.0);
1245 REPORTER_ASSERT(r, out[1] == 2.0);
1246 REPORTER_ASSERT(r, out[2] == 3.0);
1247 REPORTER_ASSERT(r, out[3] == 4.0);
Ben Wagner470e0ac2020-01-22 16:59:21 -05001248 } else {
1249 printf("%s\n%s", src, compiler.errorText().c_str());
Ethan Nicholas9e6a3932019-05-17 16:31:21 -04001250 }
1251}
Brian Osman1f71f432020-11-18 15:44:46 -05001252
1253#endif // SK_ENABLE_SKSL_INTERPRETER