1 /*
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
8 #include "include/core/SkM44.h"
9 #include "src/sksl/SkSLCompiler.h"
10 #include "src/sksl/codegen/SkSLVMCodeGenerator.h"
11 #include "src/sksl/ir/SkSLExternalFunction.h"
12 #include "src/sksl/tracing/SkVMDebugTrace.h"
13 #include "src/utils/SkJSON.h"
14
15 #include "tests/Test.h"
16
17 struct ProgramBuilder {
ProgramBuilderProgramBuilder18 ProgramBuilder(skiatest::Reporter* r, const char* src)
19 : fCompiler(&fCaps) {
20 SkSL::Program::Settings settings;
21 // The SkSL inliner is well tested in other contexts. Here, we disable inlining entirely,
22 // to stress-test the VM generator's handling of function calls with varying signatures.
23 settings.fInlineThreshold = 0;
24 // For convenience, so we can test functions other than (and not called by) main.
25 settings.fRemoveDeadFunctions = false;
26
27 fProgram = fCompiler.convertProgram(SkSL::ProgramKind::kGeneric, std::string(src),
28 settings);
29 if (!fProgram) {
30 ERRORF(r, "Program failed to compile:\n%s\n%s\n", src, fCompiler.errorText().c_str());
31 }
32 }
33
operator boolProgramBuilder34 explicit operator bool() const { return fProgram != nullptr; }
operator *ProgramBuilder35 SkSL::Program& operator*() { return *fProgram; }
36
37 SkSL::ShaderCaps fCaps;
38 SkSL::Compiler fCompiler;
39 std::unique_ptr<SkSL::Program> fProgram;
40 };
41
verify_values(skiatest::Reporter * r,const char * src,const float * actual,const float * expected,int N,bool exactCompare)42 static void verify_values(skiatest::Reporter* r,
43 const char* src,
44 const float* actual,
45 const float* expected,
46 int N,
47 bool exactCompare) {
48 auto exact_equiv = [](float x, float y) {
49 return x == y
50 || (isnan(x) && isnan(y));
51 };
52
53 bool valid = true;
54 for (int i = 0; i < N; ++i) {
55 if (exactCompare && !exact_equiv(actual[i], expected[i])) {
56 valid = false;
57 }
58 if (!exactCompare && !SkScalarNearlyEqual(actual[i], expected[i])) {
59 valid = false;
60 }
61 }
62
63 if (!valid) {
64 printf("for program: %s\n", src);
65 printf(" expected (");
66 const char* separator = "";
67 for (int i = 0; i < N; ++i) {
68 printf("%s%f", separator, expected[i]);
69 separator = ", ";
70 }
71 printf("), but received (");
72 separator = "";
73 for (int i = 0; i < N; ++i) {
74 printf("%s%f", separator, actual[i]);
75 separator = ", ";
76 }
77 printf(")\n");
78 }
79 REPORTER_ASSERT(r, valid);
80 }
81
test(skiatest::Reporter * r,const char * src,float * in,const float * expected,bool exactCompare=true)82 void test(skiatest::Reporter* r, const char* src, float* in, const float* expected,
83 bool exactCompare = true) {
84 ProgramBuilder program(r, src);
85 if (!program) { return; }
86
87 const SkSL::FunctionDefinition* main = SkSL::Program_GetFunction(*program, "main");
88 REPORTER_ASSERT(r, main);
89
90 skvm::Builder b;
91 SkSL::SkVMSignature sig;
92 SkSL::ProgramToSkVM(*program, *main, &b, /*debugTrace=*/nullptr, /*uniforms=*/{}, &sig);
93 skvm::Program p = b.done();
94
95 REPORTER_ASSERT(r, p.nargs() == (int)(sig.fParameterSlots + sig.fReturnSlots));
96
97 auto out = std::make_unique<float[]>(sig.fReturnSlots);
98 auto args = std::make_unique<void*[]>(sig.fParameterSlots + sig.fReturnSlots);
99 for (size_t i = 0; i < sig.fParameterSlots; ++i) {
100 args[i] = in + i;
101 }
102 for (size_t i = 0; i < sig.fReturnSlots; ++i) {
103 args[sig.fParameterSlots + i] = out.get() + i;
104 }
105
106 // TODO: Test with and without JIT?
107 p.eval(1, args.get());
108
109 verify_values(r, src, out.get(), expected, sig.fReturnSlots, exactCompare);
110 }
111
test(skiatest::Reporter * r,const char * src,float inR,float inG,float inB,float inA,float exR,float exG,float exB,float exA)112 void test(skiatest::Reporter* r, const char* src,
113 float inR, float inG, float inB, float inA,
114 float exR, float exG, float exB, float exA) {
115 ProgramBuilder program(r, src);
116 if (!program) { return; }
117
118 const SkSL::FunctionDefinition* main = SkSL::Program_GetFunction(*program, "main");
119 REPORTER_ASSERT(r, main);
120
121 skvm::Builder b;
122 SkSL::ProgramToSkVM(*program, *main, &b, /*debugTrace=*/nullptr, /*uniforms=*/{});
123 skvm::Program p = b.done();
124
125 // TODO: Test with and without JIT?
126 p.eval(1, &inR, &inG, &inB, &inA);
127
128 float actual[4] = { inR, inG, inB, inA };
129 float expected[4] = { exR, exG, exB, exA };
130
131 verify_values(r, src, actual, expected, 4, /*exactCompare=*/true);
132
133 // TODO: vec_test with skvm
134 }
135
DEF_TEST(SkSLInterpreterAdd,r)136 DEF_TEST(SkSLInterpreterAdd, r) {
137 test(r, "void main(inout half4 color) { color.r = color.r + color.g; }", 0.25, 0.75, 0, 0, 1,
138 0.75, 0, 0);
139 test(r, "void main(inout half4 color) { color += half4(1, 2, 3, 4); }", 4, 3, 2, 1, 5, 5, 5, 5);
140 test(r, "void main(inout half4 color) { half4 c = color; color += c; }", 0.25, 0.5, 0.75, 1,
141 0.5, 1, 1.5, 2);
142 test(r, "void main(inout half4 color) { color.r = half(int(color.r) + int(color.g)); }", 1, 3, 0, 0,
143 4, 3, 0, 0);
144 }
145
DEF_TEST(SkSLInterpreterSubtract,r)146 DEF_TEST(SkSLInterpreterSubtract, r) {
147 test(r, "void main(inout half4 color) { color.r = color.r - color.g; }", 1, 0.75, 0, 0, 0.25,
148 0.75, 0, 0);
149 test(r, "void main(inout half4 color) { color -= half4(1, 2, 3, 4); }", 5, 5, 5, 5, 4, 3, 2, 1);
150 test(r, "void main(inout half4 color) { half4 c = color; color -= c; }", 4, 3, 2, 1,
151 0, 0, 0, 0);
152 test(r, "void main(inout half4 color) { color.x = -color.x; }", 4, 3, 2, 1, -4, 3, 2, 1);
153 test(r, "void main(inout half4 color) { color = -color; }", 4, 3, 2, 1, -4, -3, -2, -1);
154 test(r, "void main(inout half4 color) { color.r = half(int(color.r) - int(color.g)); }", 3, 1, 0, 0,
155 2, 1, 0, 0);
156 }
157
DEF_TEST(SkSLInterpreterMultiply,r)158 DEF_TEST(SkSLInterpreterMultiply, r) {
159 test(r, "void main(inout half4 color) { color.r = color.r * color.g; }", 2, 3, 0, 0, 6, 3, 0,
160 0);
161 test(r, "void main(inout half4 color) { color *= half4(1, 2, 3, 4); }", 2, 3, 4, 5, 2, 6, 12,
162 20);
163 test(r, "void main(inout half4 color) { half4 c = color; color *= c; }", 4, 3, 2, 1,
164 16, 9, 4, 1);
165 test(r, "void main(inout half4 color) { color.r = half(int(color.r) * int(color.g)); }", 3, -2, 0, 0,
166 -6, -2, 0, 0);
167 }
168
DEF_TEST(SkSLInterpreterDivide,r)169 DEF_TEST(SkSLInterpreterDivide, r) {
170 test(r, "void main(inout half4 color) { color.r = color.r / color.g; }", 1, 2, 0, 0, 0.5, 2, 0,
171 0);
172 test(r, "void main(inout half4 color) { color /= half4(1, 2, 3, 4); }", 12, 12, 12, 12, 12, 6,
173 4, 3);
174 test(r, "void main(inout half4 color) { half4 c = color; color /= c; }", 4, 3, 2, 1,
175 1, 1, 1, 1);
176 test(r, "void main(inout half4 color) { color.r = half(int(color.r) / int(color.g)); }", 8, -2, 0, 0,
177 -4, -2, 0, 0);
178 }
179
DEF_TEST(SkSLInterpreterAnd,r)180 DEF_TEST(SkSLInterpreterAnd, r) {
181 test(r, "void main(inout half4 color) { if (color.r > color.g && color.g > color.b) "
182 "color = half4(color.a); }", 2, 1, 0, 3, 3, 3, 3, 3);
183 test(r, "void main(inout half4 color) { if (color.r > color.g && color.g > color.b) "
184 "color = half4(color.a); }", 1, 1, 0, 3, 1, 1, 0, 3);
185 test(r, "void main(inout half4 color) { if (color.r > color.g && color.g > color.b) "
186 "color = half4(color.a); }", 2, 1, 1, 3, 2, 1, 1, 3);
187 test(r, "half global; bool update() { global = 123; return true; }"
188 "void main(inout half4 color) { global = 0; if (color.r > color.g && update()) "
189 "color = half4(color.a); color.a = global; }", 2, 1, 1, 3, 3, 3, 3, 123);
190 test(r, "half global; bool update() { global = 123; return true; }"
191 "void main(inout half4 color) { global = 0; if (color.r > color.g && update()) "
192 "color = half4(color.a); color.a = global; }", 1, 1, 1, 3, 1, 1, 1, 0);
193 }
194
DEF_TEST(SkSLInterpreterOr,r)195 DEF_TEST(SkSLInterpreterOr, r) {
196 test(r, "void main(inout half4 color) { if (color.r > color.g || color.g > color.b) "
197 "color = half4(color.a); }", 2, 1, 0, 3, 3, 3, 3, 3);
198 test(r, "void main(inout half4 color) { if (color.r > color.g || color.g > color.b) "
199 "color = half4(color.a); }", 1, 1, 0, 3, 3, 3, 3, 3);
200 test(r, "void main(inout half4 color) { if (color.r > color.g || color.g > color.b) "
201 "color = half4(color.a); }", 1, 1, 1, 3, 1, 1, 1, 3);
202 test(r, "half global; bool update() { global = 123; return true; }"
203 "void main(inout half4 color) { global = 0; if (color.r > color.g || update()) "
204 "color = half4(color.a); color.a = global; }", 1, 1, 1, 3, 3, 3, 3, 123);
205 test(r, "half global; bool update() { global = 123; return true; }"
206 "void main(inout half4 color) { global = 0; if (color.r > color.g || update()) "
207 "color = half4(color.a); color.a = global; }", 2, 1, 1, 3, 3, 3, 3, 0);
208 }
209
DEF_TEST(SkSLInterpreterMatrix,r)210 DEF_TEST(SkSLInterpreterMatrix, r) {
211 float in[16];
212 float expected[16];
213
214 // Constructing matrix from scalar produces a diagonal matrix
215 in[0] = 2.0f;
216 expected[0] = 4.0f;
217 test(r, "float main(float x) { float4x4 m = float4x4(x); return m[1][1] + m[1][2] + m[2][2]; }",
218 in, expected);
219
220 // Constructing from a different-sized matrix fills the remaining space with the identity matrix
221 expected[0] = 3.0f;
222 test(r, "float main(float x) {"
223 "float2x2 m = float2x2(x);"
224 "float4x4 m2 = float4x4(m);"
225 "return m2[0][0] + m2[3][3]; }",
226 in, expected);
227
228 // Constructing a matrix from vectors or scalars fills in values in column-major order
229 in[0] = 1.0f;
230 in[1] = 2.0f;
231 in[2] = 4.0f;
232 in[3] = 8.0f;
233 expected[0] = 6.0f;
234 test(r, "float main(float4 v) { float2x2 m = float2x2(v); return m[0][1] + m[1][0]; }",
235 in, expected);
236
237 expected[0] = 10.0f;
238 test(r, "float main(float4 v) {"
239 "float2x2 m = float2x2(v.x, v.y, v.w, v.z);"
240 "return m[0][1] + m[1][0]; }",
241 in, expected);
242
243 // Initialize 16 values to be used as inputs to matrix tests
244 for (int i = 0; i < 16; ++i) { in[i] = (float)i; }
245
246 // M+M, M-S, S-M
247 for (int i = 0; i < 16; ++i) { expected[i] = (float)(2 * i); }
248 test(r, "float4x4 main(float4x4 m) { return m + m; }", in, expected);
249 for (int i = 0; i < 16; ++i) { expected[i] = (float)(i + 3); }
250 test(r, "float4x4 main(float4x4 m) { return m + 3.0; }", in, expected);
251 test(r, "float4x4 main(float4x4 m) { return 3.0 + m; }", in, expected);
252
253 // M-M, M-S, S-M
254 for (int i = 0; i < 4; ++i) { expected[i] = 4.0f; }
255 test(r, "float2x2 main(float2x2 m1, float2x2 m2) { return m2 - m1; }", in, expected);
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, expected);
258 for (int i = 0; i < 16; ++i) { expected[i] = (float)(3 - i); }
259 test(r, "float4x4 main(float4x4 m) { return 3.0 - m; }", in, expected);
260
261 // M*S, S*M, M/S, S/M
262 for (int i = 0; i < 16; ++i) { expected[i] = (float)(i * 3); }
263 test(r, "float4x4 main(float4x4 m) { return m * 3.0; }", in, expected);
264 test(r, "float4x4 main(float4x4 m) { return 3.0 * m; }", in, expected);
265 for (int i = 0; i < 16; ++i) { expected[i] = (float)(i) / 2.0f; }
266 test(r, "float4x4 main(float4x4 m) { return m / 2.0; }", in, expected);
267 for (int i = 0; i < 16; ++i) { expected[i] = 1.0f / (float)(i + 1); }
268 test(r, "float4x4 main(float4x4 m) { return 1.0 / (m + 1); }", in, expected);
269
270 // Matrix negation
271 for (int i = 0; i < 16; ++i) { expected[i] = (float)(-i); }
272 test(r, "float4x4 main(float4x4 m) { return -m; }", in, expected);
273
274 // M*V, V*M
275 for (int i = 0; i < 3; ++i) {
276 expected[i] = 9.0f*i + 10.0f*(i+3) + 11.0f*(i+6);
277 }
278 test(r, "float3 main(float3x3 m, float3 v) { return m * v; }", in, expected);
279 for (int i = 0; i < 3; ++i) {
280 expected[i] = 9.0f*(3*i) + 10.0f*(3*i+1) + 11.0f*(3*i+2);
281 }
282 test(r, "float3 main(float3x3 m, float3 v) { return v * m; }", in, expected);
283
284 // M*M
285 {
286 SkM44 m = SkM44::ColMajor(in);
287 SkM44 m2;
288 float in2[16];
289 for (int i = 0; i < 16; ++i) {
290 in2[i] = (i + 4) % 16;
291 }
292 m2 = SkM44::ColMajor(in2);
293 m.setConcat(m, m2);
294 // Rearrange the columns on the RHS so we detect left-hand/right-hand errors
295 test(r, "float4x4 main(float4x4 m) { return m * float4x4(m[1], m[2], m[3], m[0]); }",
296 in, (float*)&m);
297 }
298 }
299
DEF_TEST(SkSLInterpreterTernary,r)300 DEF_TEST(SkSLInterpreterTernary, r) {
301 test(r, "void main(inout half4 color) { color.r = color.g > color.b ? color.g : color.b; }",
302 0, 1, 2, 0, 2, 1, 2, 0);
303 test(r, "void main(inout half4 color) { color.r = color.g > color.b ? color.g : color.b; }",
304 0, 3, 2, 0, 3, 3, 2, 0);
305 }
306
DEF_TEST(SkSLInterpreterCast,r)307 DEF_TEST(SkSLInterpreterCast, r) {
308 union Val {
309 float f;
310 int32_t s;
311 };
312
313 Val input[2];
314 Val expected[2];
315
316 input[0].s = 3;
317 input[1].s = -5;
318 expected[0].f = 3.0f;
319 expected[1].f = -5.0f;
320 test(r, "float main(int x) { return float (x); }", (float*)input, (float*)expected);
321 test(r, "float2 main(int2 x) { return float2(x); }", (float*)input, (float*)expected);
322
323 input[0].f = 3.0f;
324 input[1].f = -5.0f;
325 expected[0].s = 3;
326 expected[1].s = -5;
327 test(r, "int main(float x) { return int (x); }", (float*)input, (float*)expected);
328 test(r, "int2 main(float2 x) { return int2(x); }", (float*)input, (float*)expected);
329
330 input[0].s = 3;
331 expected[0].f = 3.0f;
332 expected[1].f = 3.0f;
333 test(r, "float2 main(int x) { return float2(x); }", (float*)input, (float*)expected);
334 }
335
DEF_TEST(SkSLInterpreterIf,r)336 DEF_TEST(SkSLInterpreterIf, r) {
337 test(r, "void main(inout half4 color) { if (color.r > color.g) color.a = 1; }", 5, 3, 0, 0,
338 5, 3, 0, 1);
339 test(r, "void main(inout half4 color) { if (color.r > color.g) color.a = 1; }", 5, 5, 0, 0,
340 5, 5, 0, 0);
341 test(r, "void main(inout half4 color) { if (color.r > color.g) color.a = 1; }", 5, 6, 0, 0,
342 5, 6, 0, 0);
343 test(r, "void main(inout half4 color) { if (color.r < color.g) color.a = 1; }", 3, 5, 0, 0,
344 3, 5, 0, 1);
345 test(r, "void main(inout half4 color) { if (color.r < color.g) color.a = 1; }", 5, 5, 0, 0,
346 5, 5, 0, 0);
347 test(r, "void main(inout half4 color) { if (color.r < color.g) color.a = 1; }", 6, 5, 0, 0,
348 6, 5, 0, 0);
349 test(r, "void main(inout half4 color) { if (color.r >= color.g) color.a = 1; }", 5, 3, 0, 0,
350 5, 3, 0, 1);
351 test(r, "void main(inout half4 color) { if (color.r >= color.g) color.a = 1; }", 5, 5, 0, 0,
352 5, 5, 0, 1);
353 test(r, "void main(inout half4 color) { if (color.r >= color.g) color.a = 1; }", 5, 6, 0, 0,
354 5, 6, 0, 0);
355 test(r, "void main(inout half4 color) { if (color.r <= color.g) color.a = 1; }", 3, 5, 0, 0,
356 3, 5, 0, 1);
357 test(r, "void main(inout half4 color) { if (color.r <= color.g) color.a = 1; }", 5, 5, 0, 0,
358 5, 5, 0, 1);
359 test(r, "void main(inout half4 color) { if (color.r <= color.g) color.a = 1; }", 6, 5, 0, 0,
360 6, 5, 0, 0);
361 test(r, "void main(inout half4 color) { if (color.r == color.g) color.a = 1; }", 2, 2, 0, 0,
362 2, 2, 0, 1);
363 test(r, "void main(inout half4 color) { if (color.r == color.g) color.a = 1; }", 2, -2, 0, 0,
364 2, -2, 0, 0);
365 test(r, "void main(inout half4 color) { if (color.r != color.g) color.a = 1; }", 2, 2, 0, 0,
366 2, 2, 0, 0);
367 test(r, "void main(inout half4 color) { if (color.r != color.g) color.a = 1; }", 2, -2, 0, 0,
368 2, -2, 0, 1);
369 test(r, "void main(inout half4 color) { if (!(color.r == color.g)) color.a = 1; }", 2, 2, 0, 0,
370 2, 2, 0, 0);
371 test(r, "void main(inout half4 color) { if (!(color.r == color.g)) color.a = 1; }", 2, -2, 0, 0,
372 2, -2, 0, 1);
373 test(r, "void main(inout half4 color) { if (color.r == color.g) color.a = 1; else "
374 "color.a = 2; }", 1, 1, 0, 0, 1, 1, 0, 1);
375 test(r, "void main(inout half4 color) { if (color.r == color.g) color.a = 1; else "
376 "color.a = 2; }", 2, -2, 0, 0, 2, -2, 0, 2);
377 }
378
DEF_TEST(SkSLInterpreterIfVector,r)379 DEF_TEST(SkSLInterpreterIfVector, r) {
380 test(r, "void main(inout half4 color) { if (color.rg == color.ba) color.a = 1; }",
381 1, 2, 1, 2, 1, 2, 1, 1);
382 test(r, "void main(inout half4 color) { if (color.rg == color.ba) color.a = 1; }",
383 1, 2, 3, 2, 1, 2, 3, 2);
384 test(r, "void main(inout half4 color) { if (color.rg != color.ba) color.a = 1; }",
385 1, 2, 1, 2, 1, 2, 1, 2);
386 test(r, "void main(inout half4 color) { if (color.rg != color.ba) color.a = 1; }",
387 1, 2, 3, 2, 1, 2, 3, 1);
388 }
389
DEF_TEST(SkSLInterpreterFor,r)390 DEF_TEST(SkSLInterpreterFor, r) {
391 test(r, "void main(inout half4 color) { for (int i = 1; i <= 10; ++i) color.r += half(i); }",
392 0, 0, 0, 0,
393 55, 0, 0, 0);
394 test(r,
395 "void main(inout half4 color) {"
396 " for (int i = 1; i <= 10; ++i)"
397 " for (int j = 1; j <= 10; ++j)"
398 " if (j >= i) { color.r += half(j); }"
399 "}",
400 0, 0, 0, 0,
401 385, 0, 0, 0);
402 test(r,
403 "void main(inout half4 color) {"
404 " for (int i = 1; i <= 10; ++i)"
405 " for (int j = 1; j < 20 ; ++j) {"
406 " if (i == j) continue;"
407 " if (j > 10) break;"
408 " color.r += half(j);"
409 " }"
410 "}",
411 0, 0, 0, 0,
412 495, 0, 0, 0);
413 }
414
DEF_TEST(SkSLInterpreterPrefixPostfix,r)415 DEF_TEST(SkSLInterpreterPrefixPostfix, r) {
416 test(r, "void main(inout half4 color) { color.r = ++color.g; }", 1, 2, 3, 4, 3, 3, 3, 4);
417 test(r, "void main(inout half4 color) { color.r = color.g++; }", 1, 2, 3, 4, 2, 3, 3, 4);
418 }
419
DEF_TEST(SkSLInterpreterSwizzle,r)420 DEF_TEST(SkSLInterpreterSwizzle, r) {
421 test(r, "void main(inout half4 color) { color = color.abgr; }", 1, 2, 3, 4, 4, 3, 2, 1);
422 test(r, "void main(inout half4 color) { color.rgb = half4(5, 6, 7, 8).bbg; }", 1, 2, 3, 4, 7, 7,
423 6, 4);
424 test(r, "void main(inout half4 color) { color.bgr = half3(5, 6, 7); }", 1, 2, 3, 4, 7, 6,
425 5, 4);
426 }
427
DEF_TEST(SkSLInterpreterGlobal,r)428 DEF_TEST(SkSLInterpreterGlobal, r) {
429 test(r, "int x; void main(inout half4 color) { x = 10; color.b = half(x); }", 1, 2, 3, 4, 1, 2,
430 10, 4);
431 test(r, "float4 x; void main(inout float4 color) { x = color * 2; color = x; }",
432 1, 2, 3, 4, 2, 4, 6, 8);
433 test(r, "float4 x; void main(inout float4 color) { x = float4(5, 6, 7, 8); color = x.wzyx; }",
434 1, 2, 3, 4, 8, 7, 6, 5);
435 test(r, "float4 x; void main(inout float4 color) { x.wzyx = float4(5, 6, 7, 8); color = x; }",
436 1, 2, 3, 4, 8, 7, 6, 5);
437 }
438
DEF_TEST(SkSLInterpreterGeneric,r)439 DEF_TEST(SkSLInterpreterGeneric, r) {
440 float value1 = 5;
441 float expected1 = 25;
442 test(r, "float main(float x) { return x * x; }", &value1, &expected1);
443 float value2[2] = { 5, 25 };
444 float expected2[2] = { 25, 625 };
445 test(r, "float2 main(float x, float y) { return float2(x * x, y * y); }", value2, expected2);
446 }
447
DEF_TEST(SkSLInterpreterFieldAccessComplex,r)448 DEF_TEST(SkSLInterpreterFieldAccessComplex, r) {
449 const char* src = R"(
450 struct P { float x; float y; };
451 P make_point() { P p; p.x = 7; p.y = 3; return p; }
452 float main() { return make_point().y; }
453 )";
454
455 float expected = 3.0f;
456 test(r, src, /*in=*/nullptr, &expected);
457 }
458
DEF_TEST(SkSLInterpreterIndexComplex,r)459 DEF_TEST(SkSLInterpreterIndexComplex, r) {
460 const char* src = R"(
461 float2x2 make_mtx() { return float2x2(1, 2, 3, 4); }
462 float main() { return make_mtx()[1][0]; }
463 )";
464
465 float expected = 3.0f;
466 test(r, src, /*in=*/nullptr, &expected);
467 }
468
DEF_TEST(SkSLInterpreterCompound,r)469 DEF_TEST(SkSLInterpreterCompound, r) {
470 struct RectAndColor { SkIRect fRect; SkColor4f fColor; };
471 struct ManyRects { int fNumRects; RectAndColor fRects[4]; };
472
473 const char* src =
474 // Some struct definitions
475 "struct Point { int x; int y; };\n"
476 "struct Rect { Point p0; Point p1; };\n"
477 "struct RectAndColor { Rect r; float4 color; };\n"
478
479 // Structs as globals, parameters, return values
480 "RectAndColor temp;\n"
481 "int rect_height(Rect r) { return r.p1.y - r.p0.y; }\n"
482 "RectAndColor make_blue_rect(int w, int h) {\n"
483 " temp.r.p0.x = temp.r.p0.y = 0;\n"
484 " temp.r.p1.x = w; temp.r.p1.y = h;\n"
485 " temp.color = float4(0, 1, 0, 1);\n"
486 " return temp;\n"
487 "}\n"
488
489 // Initialization and assignment of types larger than 4 slots
490 "RectAndColor init_big(RectAndColor r) { RectAndColor s = r; return s; }\n"
491 "RectAndColor copy_big(RectAndColor r) { RectAndColor s; s = r; return s; }\n"
492
493 // Same for arrays, including some non-constant indexing
494 "int median(int a[15]) { return a[7]; }\n"
495
496 "float tempFloats[8];\n"
497 "float sums(float a[8]) {\n"
498 " tempFloats[0] = a[0];\n"
499 " for (int i = 1; i < 8; ++i) { tempFloats[i] = tempFloats[i - 1] + a[i]; }\n"
500 " return tempFloats[7];\n"
501 "}\n"
502
503 // Uniforms, array-of-structs
504 "uniform Rect gRects[4];\n"
505 "Rect get_rect_2() { return gRects[2]; }\n"
506
507 // Kitchen sink (swizzles, inout, SoAoS)
508 "struct ManyRects { int numRects; RectAndColor rects[4]; };\n"
509 "void fill_rects(inout ManyRects mr) {\n"
510 " for (int i = 0; i < 4; ++i) {\n"
511 " if (i >= mr.numRects) { break; }\n"
512 " mr.rects[i].r = gRects[i];\n"
513 " float b = float(mr.rects[i].r.p1.y);\n"
514 " mr.rects[i].color = float4(b, b, b, b);\n"
515 " }\n"
516 "}\n";
517
518 ProgramBuilder program(r, src);
519
520 auto rect_height = SkSL::Program_GetFunction(*program, "rect_height"),
521 make_blue_rect = SkSL::Program_GetFunction(*program, "make_blue_rect"),
522 median = SkSL::Program_GetFunction(*program, "median"),
523 sums = SkSL::Program_GetFunction(*program, "sums"),
524 get_rect_2 = SkSL::Program_GetFunction(*program, "get_rect_2"),
525 fill_rects = SkSL::Program_GetFunction(*program, "fill_rects");
526
527 SkIRect gRects[4] = { { 1,2,3,4 }, { 5,6,7,8 }, { 9,10,11,12 }, { 13,14,15,16 } };
528
529 auto build = [&](const SkSL::FunctionDefinition* fn) {
530 skvm::Builder b;
531 skvm::UPtr uniformPtr = b.uniform();
532 skvm::Val uniforms[16];
533 for (int i = 0; i < 16; ++i) {
534 uniforms[i] = b.uniform32(uniformPtr, i * sizeof(int)).id;
535 }
536 SkSL::ProgramToSkVM(*program, *fn, &b, /*debugTrace=*/nullptr, SkMakeSpan(uniforms));
537 return b.done();
538 };
539
540 struct Args {
541 Args(void* uniformData) { fArgs.push_back(uniformData); }
542 void add(void* base, int n) {
543 for (int i = 0; i < n; ++i) {
544 fArgs.push_back(SkTAddOffset<void>(base, i * sizeof(float)));
545 }
546 }
547 std::vector<void*> fArgs;
548 };
549
550 {
551 SkIRect in = SkIRect::MakeXYWH(10, 10, 20, 30);
552 int out = 0;
553 skvm::Program p = build(rect_height);
554 Args args(gRects);
555 args.add(&in, 4);
556 args.add(&out, 1);
557 p.eval(1, args.fArgs.data());
558 REPORTER_ASSERT(r, out == 30);
559 }
560
561 {
562 int in[2] = { 15, 25 };
563 RectAndColor out;
564 skvm::Program p = build(make_blue_rect);
565 Args args(gRects);
566 args.add(&in, 2);
567 args.add(&out, 8);
568 p.eval(1, args.fArgs.data());
569 REPORTER_ASSERT(r, out.fRect.width() == 15);
570 REPORTER_ASSERT(r, out.fRect.height() == 25);
571 SkColor4f blue = { 0.0f, 1.0f, 0.0f, 1.0f };
572 REPORTER_ASSERT(r, out.fColor == blue);
573 }
574
575 {
576 int in[15] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
577 int out = 0;
578 skvm::Program p = build(median);
579 Args args(gRects);
580 args.add(&in, 15);
581 args.add(&out, 1);
582 p.eval(1, args.fArgs.data());
583 REPORTER_ASSERT(r, out == 8);
584 }
585
586 {
587 float in[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
588 float out = 0;
589 skvm::Program p = build(sums);
590 Args args(gRects);
591 args.add(&in, 8);
592 args.add(&out, 1);
593 p.eval(1, args.fArgs.data());
594 REPORTER_ASSERT(r, out == static_cast<float>((7 + 1) * (7 + 2) / 2));
595 }
596
597 {
598 SkIRect out = SkIRect::MakeEmpty();
599 skvm::Program p = build(get_rect_2);
600 Args args(gRects);
601 args.add(&out, 4);
602 p.eval(1, args.fArgs.data());
603 REPORTER_ASSERT(r, out == gRects[2]);
604 }
605
606 {
607 ManyRects in;
608 memset(&in, 0, sizeof(in));
609 in.fNumRects = 2;
610 skvm::Program p = build(fill_rects);
611 Args args(gRects);
612 args.add(&in, 33);
613 p.eval(1, args.fArgs.data());
614 ManyRects expected;
615 memset(&expected, 0, sizeof(expected));
616 expected.fNumRects = 2;
617 for (int i = 0; i < 2; ++i) {
618 expected.fRects[i].fRect = gRects[i];
619 float c = gRects[i].fBottom;
620 expected.fRects[i].fColor = { c, c, c, c };
621 }
622 REPORTER_ASSERT(r, memcmp(&in, &expected, sizeof(in)) == 0);
623 }
624 }
625
expect_failure(skiatest::Reporter * r,const char * src)626 static void expect_failure(skiatest::Reporter* r, const char* src) {
627 SkSL::ShaderCaps caps;
628 SkSL::Compiler compiler(&caps);
629 SkSL::Program::Settings settings;
630 auto program = compiler.convertProgram(SkSL::ProgramKind::kGeneric,
631 std::string(src), settings);
632 REPORTER_ASSERT(r, !program);
633 }
634
DEF_TEST(SkSLInterpreterRestrictLoops,r)635 DEF_TEST(SkSLInterpreterRestrictLoops, r) {
636 // while and do-while loops are not allowed
637 expect_failure(r, "void main(inout float x) { while (x < 1) { x++; } }");
638 expect_failure(r, "void main(inout float x) { do { x++; } while (x < 1); }");
639 }
640
DEF_TEST(SkSLInterpreterReturnThenCall,r)641 DEF_TEST(SkSLInterpreterReturnThenCall, r) {
642 // Test that early returns disable execution in subsequently called functions
643 const char* src = R"(
644 float y;
645 void inc () { ++y; }
646 void maybe_inc() { if (y < 0) return; inc(); }
647 void main(inout float x) { y = x; maybe_inc(); x = y; }
648 )";
649
650 ProgramBuilder program(r, src);
651 const SkSL::FunctionDefinition* main = SkSL::Program_GetFunction(*program, "main");
652 REPORTER_ASSERT(r, main);
653
654 skvm::Builder b;
655 SkSL::ProgramToSkVM(*program, *main, &b, /*debugTrace=*/nullptr, /*uniforms=*/{});
656 skvm::Program p = b.done();
657
658 float xs[] = { -2.0f, 0.0f, 3.0f, -1.0f };
659 p.eval(4, xs);
660
661 REPORTER_ASSERT(r, xs[0] == -2.0f);
662 REPORTER_ASSERT(r, xs[1] == 1.0f);
663 REPORTER_ASSERT(r, xs[2] == 4.0f);
664 REPORTER_ASSERT(r, xs[3] == -1.0f);
665 }
666
DEF_TEST(SkSLInterpreterEarlyReturn,r)667 DEF_TEST(SkSLInterpreterEarlyReturn, r) {
668 // Test early returns with divergent control flow
669 const char* src = "float main(float x, float y) { if (x < y) { return x; } return y; }";
670
671 ProgramBuilder program(r, src);
672
673 const SkSL::FunctionDefinition* main = SkSL::Program_GetFunction(*program, "main");
674 REPORTER_ASSERT(r, main);
675
676 skvm::Builder b;
677 SkSL::ProgramToSkVM(*program, *main, &b, /*debugTrace=*/nullptr, /*uniforms=*/{});
678 skvm::Program p = b.done();
679
680 float xs[] = { 1.0f, 3.0f },
681 ys[] = { 2.0f, 2.0f };
682 float rets[2];
683 p.eval(2, xs, ys, rets);
684
685 REPORTER_ASSERT(r, rets[0] == 1.0f);
686 REPORTER_ASSERT(r, rets[1] == 2.0f);
687 }
688
DEF_TEST(SkSLInterpreterFunctions,r)689 DEF_TEST(SkSLInterpreterFunctions, r) {
690 const char* src =
691 "float sqr(float x) { return x * x; }\n"
692 "float sub(float x, float y) { return x - y; }\n"
693 "float main(float x) { return sub(sqr(x), x); }\n"
694
695 // Different signatures
696 "float dot(float2 a, float2 b) { return a.x*b.x + a.y*b.y; }\n"
697 "float dot(float3 a, float3 b) { return a.x*b.x + a.y*b.y + a.z*b.z; }\n"
698 "float dot3_test(float x) { return dot(float3(x, x + 1, x + 2), float3(1, -1, 2)); }\n"
699 "float dot2_test(float x) { return dot(float2(x, x + 1), float2(1, -1)); }\n";
700
701 ProgramBuilder program(r, src);
702
703 auto sub = SkSL::Program_GetFunction(*program, "sub");
704 auto sqr = SkSL::Program_GetFunction(*program, "sqr");
705 auto main = SkSL::Program_GetFunction(*program, "main");
706 auto tan = SkSL::Program_GetFunction(*program, "tan");
707 auto dot3 = SkSL::Program_GetFunction(*program, "dot3_test");
708 auto dot2 = SkSL::Program_GetFunction(*program, "dot2_test");
709
710 REPORTER_ASSERT(r, sub);
711 REPORTER_ASSERT(r, sqr);
712 REPORTER_ASSERT(r, main);
713 REPORTER_ASSERT(r, !tan); // Getting a non-existent function should return nullptr
714 REPORTER_ASSERT(r, dot3);
715 REPORTER_ASSERT(r, dot2);
716
717 auto test_fn = [&](const SkSL::FunctionDefinition* fn, float in, float expected) {
718 skvm::Builder b;
719 SkSL::ProgramToSkVM(*program, *fn, &b, /*debugTrace=*/nullptr, /*uniforms=*/{});
720 skvm::Program p = b.done();
721
722 float out = 0.0f;
723 p.eval(1, &in, &out);
724 REPORTER_ASSERT(r, out == expected);
725 };
726
727 test_fn(main, 3.0f, 6.0f);
728 test_fn(dot3, 3.0f, 9.0f);
729 test_fn(dot2, 3.0f, -1.0f);
730 }
731
DEF_TEST(SkSLInterpreterOutParams,r)732 DEF_TEST(SkSLInterpreterOutParams, r) {
733 test(r,
734 "void oneAlpha(inout half4 color) { color.a = 1; }"
735 "void main(inout half4 color) { oneAlpha(color); }",
736 0, 0, 0, 0, 0, 0, 0, 1);
737 test(r,
738 "half2 tricky(half x, half y, inout half2 color, half z) {"
739 " color.xy = color.yx;"
740 " return half2(x + y, z);"
741 "}"
742 "void main(inout half4 color) {"
743 " half2 t = tricky(1, 2, color.rb, 5);"
744 " color.ga = t;"
745 "}",
746 1, 2, 3, 4, 3, 3, 1, 5);
747 }
748
DEF_TEST(SkSLInterpreterSwizzleSingleLvalue,r)749 DEF_TEST(SkSLInterpreterSwizzleSingleLvalue, r) {
750 test(r,
751 "void main(inout half4 color) { color.xywz = half4(1,2,3,4); }",
752 0, 0, 0, 0, 1, 2, 4, 3);
753 }
754
DEF_TEST(SkSLInterpreterSwizzleDoubleLvalue,r)755 DEF_TEST(SkSLInterpreterSwizzleDoubleLvalue, r) {
756 test(r,
757 "void main(inout half4 color) { color.xywz.yxzw = half4(1,2,3,4); }",
758 0, 0, 0, 0, 2, 1, 4, 3);
759 }
760
DEF_TEST(SkSLInterpreterSwizzleIndexLvalue,r)761 DEF_TEST(SkSLInterpreterSwizzleIndexLvalue, r) {
762 const char* src = R"(
763 void main(inout half4 color) {
764 for (int i = 0; i < 4; i++) {
765 color.wzyx[i] += half(i);
766 }
767 }
768 )";
769 test(r, src, 0, 0, 0, 0, 3, 2, 1, 0);
770 }
771
DEF_TEST(SkSLInterpreterMathFunctions,r)772 DEF_TEST(SkSLInterpreterMathFunctions, r) {
773 float value[4], expected[4];
774
775 value[0] = 0.0f; expected[0] = 0.0f;
776 test(r, "float main(float x) { return sin(x); }", value, expected);
777 test(r, "float main(float x) { return tan(x); }", value, expected);
778
779 value[0] = 0.0f; expected[0] = 1.0f;
780 test(r, "float main(float x) { return cos(x); }", value, expected);
781
782 value[0] = 25.0f; expected[0] = 5.0f;
783 test(r, "float main(float x) { return sqrt(x); }", value, expected);
784
785 value[0] = 90.0f; expected[0] = sk_float_degrees_to_radians(value[0]);
786 test(r, "float main(float x) { return radians(x); }", value, expected);
787
788 value[0] = 1.0f; value[1] = -1.0f;
789 expected[0] = 1.0f / SK_FloatSqrt2; expected[1] = -1.0f / SK_FloatSqrt2;
790 test(r, "float2 main(float2 x) { return normalize(x); }", value, expected);
791 }
792
DEF_TEST(SkSLInterpreterVoidFunction,r)793 DEF_TEST(SkSLInterpreterVoidFunction, r) {
794 test(r,
795 "half x; void foo() { x = 1.0; }"
796 "void main(inout half4 color) { foo(); color.r = x; }",
797 0, 0, 0, 0, 1, 0, 0, 0);
798 }
799
DEF_TEST(SkSLInterpreterMix,r)800 DEF_TEST(SkSLInterpreterMix, r) {
801 float value, expected;
802
803 value = 0.5f; expected = 0.0f;
804 test(r, "float main(float x) { return mix(-10, 10, x); }", &value, &expected);
805 value = 0.75f; expected = 5.0f;
806 test(r, "float main(float x) { return mix(-10, 10, x); }", &value, &expected);
807 value = 2.0f; expected = 30.0f;
808 test(r, "float main(float x) { return mix(-10, 10, x); }", &value, &expected);
809
810 float valueVectors[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f },
811 expectedVector[] = { 3.0f, 4.0f, 5.0f, 6.0f };
812 test(r, "float4 main(float4 x, float4 y) { return mix(x, y, 0.5); }", valueVectors,
813 expectedVector);
814 }
815
DEF_TEST(SkSLInterpreterCross,r)816 DEF_TEST(SkSLInterpreterCross, r) {
817 float args[] = { 1.0f, 4.0f, -6.0f, -2.0f, 7.0f, -3.0f };
818 SkV3 cross = SkV3::Cross({args[0], args[1], args[2]},
819 {args[3], args[4], args[5]});
820 float expected[] = { cross.x, cross.y, cross.z };
821 test(r, "float3 main(float3 x, float3 y) { return cross(x, y); }", args, expected);
822 }
823
DEF_TEST(SkSLInterpreterInverse,r)824 DEF_TEST(SkSLInterpreterInverse, r) {
825 {
826 SkMatrix m;
827 m.setRotate(30).postScale(1, 2);
828 float args[4] = { m[0], m[3], m[1], m[4] };
829 SkAssertResult(m.invert(&m));
830 float expt[4] = { m[0], m[3], m[1], m[4] };
831 test(r, "float2x2 main(float2x2 m) { return inverse(m); }", args, expt, false);
832 }
833 {
834 SkMatrix m;
835 m.setRotate(30).postScale(1, 2).postTranslate(1, 2);
836 float args[9] = { m[0], m[3], m[6], m[1], m[4], m[7], m[2], m[5], m[8] };
837 SkAssertResult(m.invert(&m));
838 float expt[9] = { m[0], m[3], m[6], m[1], m[4], m[7], m[2], m[5], m[8] };
839 test(r, "float3x3 main(float3x3 m) { return inverse(m); }", args, expt, false);
840 }
841 {
842 float args[16], expt[16];
843 // just some crazy thing that is invertible
844 SkM44 m = {1, 2, 3, 4, 1, 2, 0, 3, 1, 0, 1, 4, 1, 3, 2, 0};
845 m.getColMajor(args);
846 SkAssertResult(m.invert(&m));
847 m.getColMajor(expt);
848 test(r, "float4x4 main(float4x4 m) { return inverse(m); }", args, expt, false);
849 }
850 }
851
DEF_TEST(SkSLInterpreterDot,r)852 DEF_TEST(SkSLInterpreterDot, r) {
853 float args[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f };
854 float expected = args[0] * args[2] +
855 args[1] * args[3];
856 test(r, "float main(float2 x, float2 y) { return dot(x, y); }", args, &expected);
857
858 expected = args[0] * args[3] +
859 args[1] * args[4] +
860 args[2] * args[5];
861 test(r, "float main(float3 x, float3 y) { return dot(x, y); }", args, &expected);
862
863 expected = args[0] * args[4] +
864 args[1] * args[5] +
865 args[2] * args[6] +
866 args[3] * args[7];
867 test(r, "float main(float4 x, float4 y) { return dot(x, y); }", args, &expected);
868 }
869
870 class ExternalSqrt : public SkSL::ExternalFunction {
871 public:
ExternalSqrt(const char * name,SkSL::Compiler & compiler)872 ExternalSqrt(const char* name, SkSL::Compiler& compiler)
873 : INHERITED(name, *compiler.context().fTypes.fFloat)
874 , fCompiler(compiler) {}
875
callParameterCount() const876 int callParameterCount() const override { return 1; }
877
getCallParameterTypes(const SkSL::Type ** outTypes) const878 void getCallParameterTypes(const SkSL::Type** outTypes) const override {
879 outTypes[0] = fCompiler.context().fTypes.fFloat.get();
880 }
881
call(skvm::Builder * b,skvm::F32 * arguments,skvm::F32 * outResult,skvm::I32 mask) const882 void call(skvm::Builder* b,
883 skvm::F32* arguments,
884 skvm::F32* outResult,
885 skvm::I32 mask) const override {
886 outResult[0] = sqrt(arguments[0]);
887 }
888
889 private:
890 SkSL::Compiler& fCompiler;
891 using INHERITED = SkSL::ExternalFunction;
892 };
893
DEF_TEST(SkSLInterpreterExternalFunction,r)894 DEF_TEST(SkSLInterpreterExternalFunction, r) {
895 SkSL::ShaderCaps caps;
896 SkSL::Compiler compiler(&caps);
897 SkSL::Program::Settings settings;
898 const char* src = "float main() { return externalSqrt(25); }";
899 std::vector<std::unique_ptr<SkSL::ExternalFunction>> externalFunctions;
900 externalFunctions.push_back(std::make_unique<ExternalSqrt>("externalSqrt", compiler));
901 settings.fExternalFunctions = &externalFunctions;
902 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(
903 SkSL::ProgramKind::kGeneric, std::string(src), settings);
904 REPORTER_ASSERT(r, program);
905
906 const SkSL::FunctionDefinition* main = SkSL::Program_GetFunction(*program, "main");
907
908 skvm::Builder b;
909 SkSL::ProgramToSkVM(*program, *main, &b, /*debugTrace=*/nullptr, /*uniforms=*/{});
910 skvm::Program p = b.done();
911
912 float out;
913 p.eval(1, &out);
914 REPORTER_ASSERT(r, out == 5.0);
915 }
916
917 class ExternalTable : public SkSL::ExternalFunction {
918 public:
ExternalTable(const char * name,SkSL::Compiler & compiler,skvm::Uniforms * uniforms)919 ExternalTable(const char* name, SkSL::Compiler& compiler, skvm::Uniforms* uniforms)
920 : INHERITED(name, *compiler.context().fTypes.fFloat)
921 , fCompiler(compiler)
922 , fTable{1, 2, 4, 8} {
923 fAddr = uniforms->pushPtr(fTable);
924 }
925
callParameterCount() const926 int callParameterCount() const override { return 1; }
927
getCallParameterTypes(const SkSL::Type ** outTypes) const928 void getCallParameterTypes(const SkSL::Type** outTypes) const override {
929 outTypes[0] = fCompiler.context().fTypes.fFloat.get();
930 }
931
call(skvm::Builder * b,skvm::F32 * arguments,skvm::F32 * outResult,skvm::I32 mask) const932 void call(skvm::Builder* b,
933 skvm::F32* arguments,
934 skvm::F32* outResult,
935 skvm::I32 mask) const override {
936 skvm::I32 index = skvm::trunc(arguments[0] * 4);
937 index = max(0, min(index, 3));
938 outResult[0] = b->gatherF(fAddr, index);
939 }
940
941 private:
942 SkSL::Compiler& fCompiler;
943 skvm::Uniform fAddr;
944 float fTable[4];
945 using INHERITED = SkSL::ExternalFunction;
946 };
947
DEF_TEST(SkSLInterpreterExternalTable,r)948 DEF_TEST(SkSLInterpreterExternalTable, r) {
949 SkSL::ShaderCaps caps;
950 SkSL::Compiler compiler(&caps);
951 SkSL::Program::Settings settings;
952 const char* src =
953 "float4 main() { return float4(table(2), table(-1), table(0.4), table(0.6)); }";
954 std::vector<std::unique_ptr<SkSL::ExternalFunction>> externalFunctions;
955
956 skvm::Builder b;
957 skvm::Uniforms u(b.uniform(), 0);
958
959 externalFunctions.push_back(std::make_unique<ExternalTable>("table", compiler, &u));
960 settings.fExternalFunctions = &externalFunctions;
961 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(
962 SkSL::ProgramKind::kGeneric, std::string(src), settings);
963 REPORTER_ASSERT(r, program);
964
965 const SkSL::FunctionDefinition* main = SkSL::Program_GetFunction(*program, "main");
966
967 SkSL::ProgramToSkVM(*program, *main, &b, /*debugTrace=*/nullptr, /*uniforms=*/{});
968 skvm::Program p = b.done();
969
970 float out[4];
971 p.eval(1, u.buf.data(), &out[0], &out[1], &out[2], &out[3]);
972 REPORTER_ASSERT(r, out[0] == 8.0);
973 REPORTER_ASSERT(r, out[1] == 1.0);
974 REPORTER_ASSERT(r, out[2] == 2.0);
975 REPORTER_ASSERT(r, out[3] == 4.0);
976 }
977
DEF_TEST(SkSLInterpreterTrace,r)978 DEF_TEST(SkSLInterpreterTrace, r) {
979 SkSL::ShaderCaps caps;
980 SkSL::Compiler compiler(&caps);
981 SkSL::Program::Settings settings;
982 settings.fOptimize = false;
983
984 constexpr const char kSrc[] =
985 R"(bool less_than(float left, int right) {
986 bool comparison = left < float(right);
987 if (comparison) {
988 return true;
989 } else {
990 return false;
991 }
992 }
993
994 int main() {
995 float2 a[2];
996 for (float loop = 10; loop <= 30; loop += 10) {
997 half4 v = half4(loop, loop+1, loop+2, loop+3);
998 float2x2 m = float2x2(v);
999 a[0] = float2(loop, loop+1); a[1] = float2(loop+2, loop+3);
1000 bool function_result = less_than(loop, 20);
1001 }
1002 return 40;
1003 }
1004 )";
1005 skvm::Builder b;
1006 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(SkSL::ProgramKind::kGeneric,
1007 std::string(kSrc), settings);
1008 REPORTER_ASSERT(r, program);
1009
1010 const SkSL::FunctionDefinition* main = SkSL::Program_GetFunction(*program, "main");
1011 SkSL::SkVMDebugTrace debugTrace;
1012 SkSL::ProgramToSkVM(*program, *main, &b, &debugTrace, /*uniforms=*/{});
1013 skvm::Program p = b.done();
1014 REPORTER_ASSERT(r, p.nargs() == 1);
1015
1016 int result;
1017 p.eval(1, &result);
1018
1019 SkDynamicMemoryWStream streamDump;
1020 debugTrace.dump(&streamDump);
1021
1022 sk_sp<SkData> dataDump = streamDump.detachAsData();
1023 std::string_view trace{static_cast<const char*>(dataDump->data()), dataDump->size()};
1024
1025 REPORTER_ASSERT(r, result == 40);
1026 REPORTER_ASSERT(r, trace ==
1027 R"($0 = [main].result (int, L10)
1028 $1 = a[0] (float2 : slot 1/2, L11)
1029 $2 = a[0] (float2 : slot 2/2, L11)
1030 $3 = a[1] (float2 : slot 1/2, L11)
1031 $4 = a[1] (float2 : slot 2/2, L11)
1032 $5 = loop (float, L12)
1033 $6 = v (float4 : slot 1/4, L13)
1034 $7 = v (float4 : slot 2/4, L13)
1035 $8 = v (float4 : slot 3/4, L13)
1036 $9 = v (float4 : slot 4/4, L13)
1037 $10 = m (float2x2 : slot 1/4, L14)
1038 $11 = m (float2x2 : slot 2/4, L14)
1039 $12 = m (float2x2 : slot 3/4, L14)
1040 $13 = m (float2x2 : slot 4/4, L14)
1041 $14 = function_result (bool, L16)
1042 $15 = [less_than].result (bool, L1)
1043 $16 = left (float, L1)
1044 $17 = right (int, L1)
1045 $18 = comparison (bool, L2)
1046 F0 = int main()
1047 F1 = bool less_than(float left, int right)
1048
1049 enter int main()
1050 scope +1
1051 line 11
1052 a[0].x = 0
1053 a[0].y = 0
1054 a[1].x = 0
1055 a[1].y = 0
1056 line 12
1057 scope +1
1058 loop = 10
1059 scope +1
1060 line 13
1061 v.x = 10
1062 v.y = 11
1063 v.z = 12
1064 v.w = 13
1065 line 14
1066 m[0][0] = 10
1067 m[0][1] = 11
1068 m[1][0] = 12
1069 m[1][1] = 13
1070 line 15
1071 a[0].x = 10
1072 a[0].y = 11
1073 line 15
1074 a[1].x = 12
1075 a[1].y = 13
1076 line 16
1077 enter bool less_than(float left, int right)
1078 left = 10
1079 right = 20
1080 scope +1
1081 line 2
1082 comparison = true
1083 line 3
1084 scope +1
1085 line 4
1086 [less_than].result = true
1087 scope -1
1088 scope -1
1089 exit bool less_than(float left, int right)
1090 function_result = true
1091 scope -1
1092 line 12
1093 loop = 20
1094 scope +1
1095 line 13
1096 v.x = 20
1097 v.y = 21
1098 v.z = 22
1099 v.w = 23
1100 line 14
1101 m[0][0] = 20
1102 m[0][1] = 21
1103 m[1][0] = 22
1104 m[1][1] = 23
1105 line 15
1106 a[0].x = 20
1107 a[0].y = 21
1108 line 15
1109 a[1].x = 22
1110 a[1].y = 23
1111 line 16
1112 enter bool less_than(float left, int right)
1113 left = 20
1114 right = 20
1115 scope +1
1116 line 2
1117 comparison = false
1118 line 3
1119 scope +1
1120 line 6
1121 [less_than].result = false
1122 scope -1
1123 scope -1
1124 exit bool less_than(float left, int right)
1125 function_result = false
1126 scope -1
1127 line 12
1128 loop = 30
1129 scope +1
1130 line 13
1131 v.x = 30
1132 v.y = 31
1133 v.z = 32
1134 v.w = 33
1135 line 14
1136 m[0][0] = 30
1137 m[0][1] = 31
1138 m[1][0] = 32
1139 m[1][1] = 33
1140 line 15
1141 a[0].x = 30
1142 a[0].y = 31
1143 line 15
1144 a[1].x = 32
1145 a[1].y = 33
1146 line 16
1147 enter bool less_than(float left, int right)
1148 left = 30
1149 right = 20
1150 scope +1
1151 line 2
1152 comparison = false
1153 line 3
1154 scope +1
1155 line 6
1156 [less_than].result = false
1157 scope -1
1158 scope -1
1159 exit bool less_than(float left, int right)
1160 function_result = false
1161 scope -1
1162 line 12
1163 scope -1
1164 line 18
1165 [main].result = 40
1166 scope -1
1167 exit int main()
1168 )", "Trace output does not match expectation:\n%.*s\n", (int)trace.size(), trace.data());
1169 }
1170