blob: bd0c64a93bb869293252473926de3132456c0018 [file] [log] [blame]
ethannicholasb3058bd2016-07-01 08:22:01 -07001/*
2 * Copyright 2016 Google Inc.
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 "SkSLCompiler.h"
9
10#include "Test.h"
11
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050012#if SK_SUPPORT_GPU
Ethan Nicholas7ef4b742016-11-11 15:16:46 -050013
ethannicholasb3058bd2016-07-01 08:22:01 -070014static void test_failure(skiatest::Reporter* r, const char* src, const char* error) {
15 SkSL::Compiler compiler;
Ethan Nicholas941e7e22016-12-12 15:33:30 -050016 SkSL::Program::Settings settings;
17 sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::Default();
18 settings.fCaps = caps.get();
19 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(SkSL::Program::kFragment_Kind,
20 SkString(src), settings);
21 if (program) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -040022 SkSL::String ignored;
Ethan Nicholas941e7e22016-12-12 15:33:30 -050023 compiler.toSPIRV(*program, &ignored);
24 }
Ethan Nicholas0df1b042017-03-31 13:56:23 -040025 SkSL::String skError(error);
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050026 if (compiler.errorText() != skError) {
ethannicholasb3058bd2016-07-01 08:22:01 -070027 SkDebugf("SKSL ERROR:\n source: %s\n expected: %s received: %s", src, error,
28 compiler.errorText().c_str());
29 }
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050030 REPORTER_ASSERT(r, compiler.errorText() == skError);
ethannicholasb3058bd2016-07-01 08:22:01 -070031}
32
33static void test_success(skiatest::Reporter* r, const char* src) {
34 SkSL::Compiler compiler;
Ethan Nicholas941e7e22016-12-12 15:33:30 -050035 SkSL::Program::Settings settings;
36 sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::Default();
37 settings.fCaps = caps.get();
38 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(SkSL::Program::kFragment_Kind,
39 SkString(src), settings);
40 REPORTER_ASSERT(r, program);
Ethan Nicholas0df1b042017-03-31 13:56:23 -040041 SkSL::String ignored;
Ethan Nicholas941e7e22016-12-12 15:33:30 -050042 REPORTER_ASSERT(r, compiler.toSPIRV(*program, &ignored));
ethannicholasb3058bd2016-07-01 08:22:01 -070043}
44
45DEF_TEST(SkSLUndefinedSymbol, r) {
46 test_failure(r,
47 "void main() { x = vec2(1); }",
48 "error: 1: unknown identifier 'x'\n1 error\n");
49}
50
51DEF_TEST(SkSLUndefinedFunction, r) {
52 test_failure(r,
53 "void main() { int x = foo(1); }",
54 "error: 1: unknown identifier 'foo'\n1 error\n");
55}
56
57DEF_TEST(SkSLGenericArgumentMismatch, r) {
58 test_failure(r,
59 "void main() { float x = sin(1, 2); }",
ethannicholas471e8942016-10-28 09:02:46 -070060 "error: 1: call to 'sin' expected 1 argument, but found 2\n1 error\n");
61 test_failure(r,
62 "void main() { float x = sin(true); }",
63 "error: 1: no match for sin(bool)\n1 error\n");
64 test_success(r,
65 "void main() { float x = sin(1); }");
ethannicholasb3058bd2016-07-01 08:22:01 -070066}
67
68DEF_TEST(SkSLArgumentCountMismatch, r) {
69 test_failure(r,
70 "float foo(float x) { return x * x; }"
71 "void main() { float x = foo(1, 2); }",
72 "error: 1: call to 'foo' expected 1 argument, but found 2\n1 error\n");
73}
74
75DEF_TEST(SkSLArgumentMismatch, r) {
76 test_failure(r,
77 "float foo(float x) { return x * x; }"
78 "void main() { float x = foo(true); }",
79 "error: 1: expected 'float', but found 'bool'\n1 error\n");
80}
81
82DEF_TEST(SkSLIfTypeMismatch, r) {
83 test_failure(r,
84 "void main() { if (3) { } }",
85 "error: 1: expected 'bool', but found 'int'\n1 error\n");
86}
87
88DEF_TEST(SkSLDoTypeMismatch, r) {
89 test_failure(r,
90 "void main() { do { } while (vec2(1)); }",
91 "error: 1: expected 'bool', but found 'vec2'\n1 error\n");
92}
93
94DEF_TEST(SkSLWhileTypeMismatch, r) {
95 test_failure(r,
96 "void main() { while (vec3(1)) { } }",
97 "error: 1: expected 'bool', but found 'vec3'\n1 error\n");
98}
99
100DEF_TEST(SkSLForTypeMismatch, r) {
101 test_failure(r,
102 "void main() { for (int x = 0; x; x++) { } }",
103 "error: 1: expected 'bool', but found 'int'\n1 error\n");
104}
105
106DEF_TEST(SkSLConstructorTypeMismatch, r) {
107 test_failure(r,
108 "void main() { vec2 x = vec2(1.0, false); }",
109 "error: 1: expected 'float', but found 'bool'\n1 error\n");
110 test_failure(r,
Ethan Nicholas4578a8e2016-11-01 11:57:42 -0400111 "void main() { vec2 x = vec2(bvec2(false)); }",
112 "error: 1: 'bvec2' is not a valid parameter to 'vec2' constructor\n1 error\n");
113 test_failure(r,
114 "void main() { bvec2 x = bvec2(vec2(1)); }",
115 "error: 1: 'vec2' is not a valid parameter to 'bvec2' constructor\n1 error\n");
116 test_failure(r,
ethannicholasb3058bd2016-07-01 08:22:01 -0700117 "void main() { bool x = bool(1.0); }",
118 "error: 1: cannot construct 'bool'\n1 error\n");
119 test_failure(r,
120 "struct foo { int x; }; void main() { foo x = foo(5); }",
121 "error: 1: cannot construct 'foo'\n1 error\n");
122 test_failure(r,
123 "struct foo { int x; } foo; void main() { float x = float(foo); }",
124 "error: 1: invalid argument to 'float' constructor (expected a number or bool, but found 'foo')\n1 error\n");
125 test_failure(r,
126 "struct foo { int x; } foo; void main() { vec2 x = vec2(foo); }",
127 "error: 1: 'foo' is not a valid parameter to 'vec2' constructor\n1 error\n");
128}
129
130DEF_TEST(SkSLConstructorArgumentCount, r) {
131 test_failure(r,
132 "void main() { vec3 x = vec3(1.0, 2.0); }",
133 "error: 1: invalid arguments to 'vec3' constructor (expected 3 scalars, but "
134 "found 2)\n1 error\n");
Ethan Nicholas84645e32017-02-09 13:57:14 -0500135 test_failure(r,
136 "void main() { vec3 x = vec3(1.0, 2.0, 3.0, 4.0); }",
137 "error: 1: invalid arguments to 'vec3' constructor (expected 3 scalars, but found "
138 "4)\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700139}
140
141DEF_TEST(SkSLSwizzleScalar, r) {
142 test_failure(r,
143 "void main() { float x = 1; float y = x.y; }",
144 "error: 1: cannot swizzle value of type 'float'\n1 error\n");
145}
146
147DEF_TEST(SkSLSwizzleMatrix, r) {
148 test_failure(r,
149 "void main() { mat2 x = mat2(1); float y = x.y; }",
150 "error: 1: cannot swizzle value of type 'mat2'\n1 error\n");
151}
152
153DEF_TEST(SkSLSwizzleOutOfBounds, r) {
154 test_failure(r,
155 "void main() { vec3 test = vec2(1).xyz; }",
156 "error: 1: invalid swizzle component 'z'\n1 error\n");
157}
158
159DEF_TEST(SkSLSwizzleTooManyComponents, r) {
160 test_failure(r,
161 "void main() { vec4 test = vec2(1).xxxxx; }",
162 "error: 1: too many components in swizzle mask 'xxxxx'\n1 error\n");
163}
164
165DEF_TEST(SkSLSwizzleDuplicateOutput, r) {
166 test_failure(r,
167 "void main() { vec4 test = vec4(1); test.xyyz = vec4(1); }",
168 "error: 1: cannot write to the same swizzle field more than once\n1 error\n");
169}
ethannicholasea4567c2016-10-17 11:24:37 -0700170
ethannicholasb3058bd2016-07-01 08:22:01 -0700171DEF_TEST(SkSLAssignmentTypeMismatch, r) {
172 test_failure(r,
173 "void main() { int x = 1.0; }",
174 "error: 1: expected 'int', but found 'float'\n1 error\n");
ethannicholasea4567c2016-10-17 11:24:37 -0700175 test_failure(r,
176 "void main() { int x; x = 1.0; }",
177 "error: 1: type mismatch: '=' cannot operate on 'int', 'float'\n1 error\n");
178 test_success(r,
179 "void main() { vec3 x = vec3(0); x *= 1.0; }");
180 test_failure(r,
181 "void main() { ivec3 x = ivec3(0); x *= 1.0; }",
182 "error: 1: type mismatch: '*=' cannot operate on 'ivec3', 'float'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700183}
184
185DEF_TEST(SkSLReturnFromVoid, r) {
186 test_failure(r,
187 "void main() { return true; }",
188 "error: 1: may not return a value from a void function\n1 error\n");
189}
190
191DEF_TEST(SkSLReturnMissingValue, r) {
192 test_failure(r,
193 "int foo() { return; } void main() { }",
194 "error: 1: expected function to return 'int'\n1 error\n");
195}
196
197DEF_TEST(SkSLReturnTypeMismatch, r) {
198 test_failure(r,
199 "int foo() { return 1.0; } void main() { }",
200 "error: 1: expected 'int', but found 'float'\n1 error\n");
201}
202
203DEF_TEST(SkSLDuplicateFunction, r) {
204 test_failure(r,
205 "void main() { } void main() { }",
206 "error: 1: duplicate definition of void main()\n1 error\n");
207 test_success(r,
208 "void main(); void main() { }");
209}
210
211DEF_TEST(SkSLUsingInvalidValue, r) {
212 test_failure(r,
213 "void main() { int x = int; }",
214 "error: 1: expected '(' to begin constructor invocation\n1 error\n");
215 test_failure(r,
216 "int test() { return 1; } void main() { int x = test; }",
217 "error: 1: expected '(' to begin function call\n1 error\n");
218}
219DEF_TEST(SkSLDifferentReturnType, r) {
220 test_failure(r,
ethannicholas22f939e2016-10-13 13:25:34 -0700221 "int main() { return 1; } void main() { }",
ethannicholasb3058bd2016-07-01 08:22:01 -0700222 "error: 1: functions 'void main()' and 'int main()' differ only in return type\n1 "
223 "error\n");
224}
225
226DEF_TEST(SkSLDifferentModifiers, r) {
227 test_failure(r,
228 "void test(int x); void test(out int x) { }",
229 "error: 1: modifiers on parameter 1 differ between declaration and definition\n1 "
230 "error\n");
231}
232
233DEF_TEST(SkSLDuplicateSymbol, r) {
234 test_failure(r,
235 "int main; void main() { }",
236 "error: 1: symbol 'main' was already defined\n1 error\n");
237
238 test_failure(r,
239 "int x; int x; void main() { }",
240 "error: 1: symbol 'x' was already defined\n1 error\n");
241
242 test_success(r, "int x; void main() { int x; }");
243}
244
245DEF_TEST(SkSLBinaryTypeMismatch, r) {
246 test_failure(r,
247 "void main() { float x = 3 * true; }",
248 "error: 1: type mismatch: '*' cannot operate on 'int', 'bool'\n1 error\n");
249 test_failure(r,
250 "void main() { bool x = 1 || 2.0; }",
251 "error: 1: type mismatch: '||' cannot operate on 'int', 'float'\n1 error\n");
252}
253
254DEF_TEST(SkSLCallNonFunction, r) {
255 test_failure(r,
256 "void main() { float x = 3; x(); }",
257 "error: 1: 'x' is not a function\n1 error\n");
258}
259
260DEF_TEST(SkSLInvalidUnary, r) {
261 test_failure(r,
262 "void main() { mat4 x = mat4(1); ++x; }",
263 "error: 1: '++' cannot operate on 'mat4'\n1 error\n");
264 test_failure(r,
265 "void main() { vec3 x = vec3(1); --x; }",
266 "error: 1: '--' cannot operate on 'vec3'\n1 error\n");
267 test_failure(r,
268 "void main() { mat4 x = mat4(1); x++; }",
269 "error: 1: '++' cannot operate on 'mat4'\n1 error\n");
270 test_failure(r,
271 "void main() { vec3 x = vec3(1); x--; }",
272 "error: 1: '--' cannot operate on 'vec3'\n1 error\n");
273 test_failure(r,
274 "void main() { int x = !12; }",
275 "error: 1: '!' cannot operate on 'int'\n1 error\n");
276 test_failure(r,
277 "struct foo { } bar; void main() { foo x = +bar; }",
278 "error: 1: '+' cannot operate on 'foo'\n1 error\n");
279 test_failure(r,
280 "struct foo { } bar; void main() { foo x = -bar; }",
281 "error: 1: '-' cannot operate on 'foo'\n1 error\n");
282 test_success(r,
283 "void main() { vec2 x = vec2(1, 1); x = +x; x = -x; }");
284}
285
286DEF_TEST(SkSLInvalidAssignment, r) {
287 test_failure(r,
288 "void main() { 1 = 2; }",
289 "error: 1: cannot assign to '1'\n1 error\n");
290 test_failure(r,
291 "uniform int x; void main() { x = 0; }",
292 "error: 1: cannot modify immutable variable 'x'\n1 error\n");
293 test_failure(r,
294 "const int x; void main() { x = 0; }",
295 "error: 1: cannot modify immutable variable 'x'\n1 error\n");
296}
297
298DEF_TEST(SkSLBadIndex, r) {
299 test_failure(r,
300 "void main() { int x = 2[0]; }",
301 "error: 1: expected array, but found 'int'\n1 error\n");
302 test_failure(r,
ethannicholas5961bc92016-10-12 06:39:56 -0700303 "void main() { vec2 x = vec2(0); int y = x[0][0]; }",
304 "error: 1: expected array, but found 'float'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700305}
306
307DEF_TEST(SkSLTernaryMismatch, r) {
308 test_failure(r,
309 "void main() { int x = 5 > 2 ? true : 1.0; }",
310 "error: 1: ternary operator result mismatch: 'bool', 'float'\n1 error\n");
Ethan Nicholas2be687a2017-01-03 16:44:39 -0500311 test_failure(r,
312 "void main() { int x = 5 > 2 ? vec3(1) : 1.0; }",
313 "error: 1: ternary operator result mismatch: 'vec3', 'float'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700314}
315
316DEF_TEST(SkSLInterfaceBlockStorageModifiers, r) {
317 test_failure(r,
318 "uniform foo { out int x; };",
319 "error: 1: interface block fields may not have storage qualifiers\n1 error\n");
320}
ethannicholas22f939e2016-10-13 13:25:34 -0700321
322DEF_TEST(SkSLUseWithoutInitialize, r) {
323 test_failure(r,
324 "void main() { int x; if (5 == 2) x = 3; x++; }",
325 "error: 1: 'x' has not been assigned\n1 error\n");
326 test_failure(r,
327 "void main() { int x[2][2]; int i; x[i][1] = 4; }",
328 "error: 1: 'i' has not been assigned\n1 error\n");
329 test_failure(r,
330 "int main() { int r; return r; }",
331 "error: 1: 'r' has not been assigned\n1 error\n");
332 test_failure(r,
333 "void main() { int x; int y = x; }",
334 "error: 1: 'x' has not been assigned\n1 error\n");
335 test_failure(r,
336 "void main() { bool x; if (true && (false || x)) return; }",
337 "error: 1: 'x' has not been assigned\n1 error\n");
Ethan Nicholasaf197692017-02-27 13:26:45 -0500338 test_failure(r,
339 "void main() { int x; switch (3) { case 0: x = 0; case 1: x = 1; }"
340 "sk_FragColor = vec4(x); }",
341 "error: 1: 'x' has not been assigned\n1 error\n");
ethannicholas22f939e2016-10-13 13:25:34 -0700342}
343
344DEF_TEST(SkSLUnreachable, r) {
345 test_failure(r,
346 "void main() { return; return; }",
347 "error: 1: unreachable\n1 error\n");
348 test_failure(r,
349 "void main() { for (;;) { continue; int x = 1; } }",
350 "error: 1: unreachable\n1 error\n");
351 test_failure(r,
352 "void main() { for (;;) { } return; }",
353 "error: 1: unreachable\n1 error\n");
354 test_failure(r,
355 "void main() { if (true) return; else discard; return; }",
356 "error: 1: unreachable\n1 error\n");
357 test_failure(r,
358 "void main() { return; while (true); }",
359 "error: 1: unreachable\n1 error\n");
360}
361
362DEF_TEST(SkSLNoReturn, r) {
363 test_failure(r,
364 "int foo() { if (2 > 5) return 3; }",
365 "error: 1: function can exit without returning a value\n1 error\n");
366}
367
368DEF_TEST(SkSLBreakOutsideLoop, r) {
369 test_failure(r,
370 "void foo() { while(true) {} if (true) break; }",
Ethan Nicholasaf197692017-02-27 13:26:45 -0500371 "error: 1: break statement must be inside a loop or switch\n1 error\n");
ethannicholas22f939e2016-10-13 13:25:34 -0700372}
373
374DEF_TEST(SkSLContinueOutsideLoop, r) {
375 test_failure(r,
376 "void foo() { for(;;); continue; }",
377 "error: 1: continue statement must be inside a loop\n1 error\n");
Ethan Nicholasaf197692017-02-27 13:26:45 -0500378 test_failure(r,
379 "void foo() { switch (1) { default: continue; } }",
380 "error: 1: continue statement must be inside a loop\n1 error\n");
ethannicholas22f939e2016-10-13 13:25:34 -0700381}
ethannicholas08a92112016-11-09 13:26:45 -0800382
383DEF_TEST(SkSLStaticIfError, r) {
384 // ensure eliminated branch of static if / ternary is still checked for errors
385 test_failure(r,
386 "void foo() { if (true); else x = 5; }",
387 "error: 1: unknown identifier 'x'\n1 error\n");
388 test_failure(r,
389 "void foo() { if (false) x = 5; }",
390 "error: 1: unknown identifier 'x'\n1 error\n");
391 test_failure(r,
392 "void foo() { true ? 5 : x; }",
393 "error: 1: unknown identifier 'x'\n1 error\n");
394 test_failure(r,
395 "void foo() { false ? x : 5; }",
396 "error: 1: unknown identifier 'x'\n1 error\n");
397}
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500398
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500399DEF_TEST(SkSLBadCap, r) {
400 test_failure(r,
401 "bool b = sk_Caps.bugFreeDriver;",
402 "error: 1: unknown capability flag 'bugFreeDriver'\n1 error\n");
403}
404
Ethan Nicholas19671772016-11-28 16:30:17 -0500405DEF_TEST(SkSLBadOffset, r) {
406 test_failure(r,
407 "struct Bad { layout (offset = 5) int x; } bad; void main() { bad.x = 5; }",
408 "error: 1: offset of field 'x' must be a multiple of 4\n1 error\n");
409 test_failure(r,
410 "struct Bad { int x; layout (offset = 0) int y; } bad; void main() { bad.x = 5; }",
411 "error: 1: offset of field 'y' must be at least 4\n1 error\n");
412}
413
Ethan Nicholas9a5610e2017-01-03 15:16:29 -0500414DEF_TEST(SkSLDivByZero, r) {
415 test_failure(r,
416 "int x = 1 / 0;",
417 "error: 1: division by zero\n1 error\n");
418 test_failure(r,
419 "float x = 1 / 0;",
420 "error: 1: division by zero\n1 error\n");
421 test_failure(r,
422 "float x = 1.0 / 0.0;",
423 "error: 1: division by zero\n1 error\n");
424 test_failure(r,
425 "float x = -67.0 / (3.0 - 3);",
426 "error: 1: division by zero\n1 error\n");
427}
428
Ethan Nicholas38657112017-02-09 17:01:22 -0500429DEF_TEST(SkSLUnsupportedGLSLIdentifiers, r) {
430 test_failure(r,
431 "void main() { float x = gl_FragCoord.x; };",
432 "error: 1: unknown identifier 'gl_FragCoord'\n1 error\n");
433 test_failure(r,
434 "void main() { float r = gl_FragColor.r; };",
435 "error: 1: unknown identifier 'gl_FragColor'\n1 error\n");
436}
437
Ethan Nicholasaf197692017-02-27 13:26:45 -0500438DEF_TEST(SkSLWrongSwitchTypes, r) {
439 test_failure(r,
440 "void main() { switch (vec2(1)) { case 1: break; } }",
441 "error: 1: expected 'int', but found 'vec2'\n1 error\n");
442 test_failure(r,
443 "void main() { switch (1) { case vec2(1): break; } }",
444 "error: 1: expected 'int', but found 'vec2'\n1 error\n");
445}
446
447DEF_TEST(SkSLNonConstantCase, r) {
448 test_failure(r,
449 "void main() { int x = 1; switch (1) { case x: break; } }",
450 "error: 1: case value must be a constant\n1 error\n");
451}
452
453DEF_TEST(SkSLDuplicateCase, r) {
454 test_failure(r,
455 "void main() { switch (1) { case 0: case 1: case 0: break; } }",
456 "error: 1: duplicate case value\n1 error\n");
457}
458
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500459#endif