blob: d443b1844f3669594e69e17ee1668d110b56b2b5 [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");
Ethan Nicholas3deaeb22017-04-25 14:42:11 -0400128 test_failure(r,
129 "void main() { mat2 x = mat2(true); }",
130 "error: 1: expected 'float', but found 'bool'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700131}
132
133DEF_TEST(SkSLConstructorArgumentCount, r) {
134 test_failure(r,
135 "void main() { vec3 x = vec3(1.0, 2.0); }",
136 "error: 1: invalid arguments to 'vec3' constructor (expected 3 scalars, but "
137 "found 2)\n1 error\n");
Ethan Nicholas84645e32017-02-09 13:57:14 -0500138 test_failure(r,
139 "void main() { vec3 x = vec3(1.0, 2.0, 3.0, 4.0); }",
140 "error: 1: invalid arguments to 'vec3' constructor (expected 3 scalars, but found "
141 "4)\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700142}
143
144DEF_TEST(SkSLSwizzleScalar, r) {
145 test_failure(r,
146 "void main() { float x = 1; float y = x.y; }",
147 "error: 1: cannot swizzle value of type 'float'\n1 error\n");
148}
149
150DEF_TEST(SkSLSwizzleMatrix, r) {
151 test_failure(r,
152 "void main() { mat2 x = mat2(1); float y = x.y; }",
153 "error: 1: cannot swizzle value of type 'mat2'\n1 error\n");
154}
155
156DEF_TEST(SkSLSwizzleOutOfBounds, r) {
157 test_failure(r,
158 "void main() { vec3 test = vec2(1).xyz; }",
159 "error: 1: invalid swizzle component 'z'\n1 error\n");
160}
161
162DEF_TEST(SkSLSwizzleTooManyComponents, r) {
163 test_failure(r,
164 "void main() { vec4 test = vec2(1).xxxxx; }",
165 "error: 1: too many components in swizzle mask 'xxxxx'\n1 error\n");
166}
167
168DEF_TEST(SkSLSwizzleDuplicateOutput, r) {
169 test_failure(r,
170 "void main() { vec4 test = vec4(1); test.xyyz = vec4(1); }",
171 "error: 1: cannot write to the same swizzle field more than once\n1 error\n");
172}
ethannicholasea4567c2016-10-17 11:24:37 -0700173
ethannicholasb3058bd2016-07-01 08:22:01 -0700174DEF_TEST(SkSLAssignmentTypeMismatch, r) {
175 test_failure(r,
176 "void main() { int x = 1.0; }",
177 "error: 1: expected 'int', but found 'float'\n1 error\n");
ethannicholasea4567c2016-10-17 11:24:37 -0700178 test_failure(r,
179 "void main() { int x; x = 1.0; }",
180 "error: 1: type mismatch: '=' cannot operate on 'int', 'float'\n1 error\n");
181 test_success(r,
182 "void main() { vec3 x = vec3(0); x *= 1.0; }");
183 test_failure(r,
184 "void main() { ivec3 x = ivec3(0); x *= 1.0; }",
185 "error: 1: type mismatch: '*=' cannot operate on 'ivec3', 'float'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700186}
187
188DEF_TEST(SkSLReturnFromVoid, r) {
189 test_failure(r,
190 "void main() { return true; }",
191 "error: 1: may not return a value from a void function\n1 error\n");
192}
193
194DEF_TEST(SkSLReturnMissingValue, r) {
195 test_failure(r,
196 "int foo() { return; } void main() { }",
197 "error: 1: expected function to return 'int'\n1 error\n");
198}
199
200DEF_TEST(SkSLReturnTypeMismatch, r) {
201 test_failure(r,
202 "int foo() { return 1.0; } void main() { }",
203 "error: 1: expected 'int', but found 'float'\n1 error\n");
204}
205
206DEF_TEST(SkSLDuplicateFunction, r) {
207 test_failure(r,
208 "void main() { } void main() { }",
209 "error: 1: duplicate definition of void main()\n1 error\n");
210 test_success(r,
211 "void main(); void main() { }");
212}
213
214DEF_TEST(SkSLUsingInvalidValue, r) {
215 test_failure(r,
216 "void main() { int x = int; }",
217 "error: 1: expected '(' to begin constructor invocation\n1 error\n");
218 test_failure(r,
219 "int test() { return 1; } void main() { int x = test; }",
220 "error: 1: expected '(' to begin function call\n1 error\n");
221}
222DEF_TEST(SkSLDifferentReturnType, r) {
223 test_failure(r,
ethannicholas22f939e2016-10-13 13:25:34 -0700224 "int main() { return 1; } void main() { }",
ethannicholasb3058bd2016-07-01 08:22:01 -0700225 "error: 1: functions 'void main()' and 'int main()' differ only in return type\n1 "
226 "error\n");
227}
228
229DEF_TEST(SkSLDifferentModifiers, r) {
230 test_failure(r,
231 "void test(int x); void test(out int x) { }",
232 "error: 1: modifiers on parameter 1 differ between declaration and definition\n1 "
233 "error\n");
234}
235
236DEF_TEST(SkSLDuplicateSymbol, r) {
237 test_failure(r,
238 "int main; void main() { }",
239 "error: 1: symbol 'main' was already defined\n1 error\n");
240
241 test_failure(r,
242 "int x; int x; void main() { }",
243 "error: 1: symbol 'x' was already defined\n1 error\n");
244
245 test_success(r, "int x; void main() { int x; }");
246}
247
248DEF_TEST(SkSLBinaryTypeMismatch, r) {
249 test_failure(r,
250 "void main() { float x = 3 * true; }",
251 "error: 1: type mismatch: '*' cannot operate on 'int', 'bool'\n1 error\n");
252 test_failure(r,
253 "void main() { bool x = 1 || 2.0; }",
254 "error: 1: type mismatch: '||' cannot operate on 'int', 'float'\n1 error\n");
255}
256
257DEF_TEST(SkSLCallNonFunction, r) {
258 test_failure(r,
259 "void main() { float x = 3; x(); }",
260 "error: 1: 'x' is not a function\n1 error\n");
261}
262
263DEF_TEST(SkSLInvalidUnary, r) {
264 test_failure(r,
265 "void main() { mat4 x = mat4(1); ++x; }",
266 "error: 1: '++' cannot operate on 'mat4'\n1 error\n");
267 test_failure(r,
268 "void main() { vec3 x = vec3(1); --x; }",
269 "error: 1: '--' cannot operate on 'vec3'\n1 error\n");
270 test_failure(r,
271 "void main() { mat4 x = mat4(1); x++; }",
272 "error: 1: '++' cannot operate on 'mat4'\n1 error\n");
273 test_failure(r,
274 "void main() { vec3 x = vec3(1); x--; }",
275 "error: 1: '--' cannot operate on 'vec3'\n1 error\n");
276 test_failure(r,
277 "void main() { int x = !12; }",
278 "error: 1: '!' cannot operate on 'int'\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_failure(r,
283 "struct foo { } bar; void main() { foo x = -bar; }",
284 "error: 1: '-' cannot operate on 'foo'\n1 error\n");
285 test_success(r,
286 "void main() { vec2 x = vec2(1, 1); x = +x; x = -x; }");
287}
288
289DEF_TEST(SkSLInvalidAssignment, r) {
290 test_failure(r,
291 "void main() { 1 = 2; }",
292 "error: 1: cannot assign to '1'\n1 error\n");
293 test_failure(r,
294 "uniform int x; void main() { x = 0; }",
295 "error: 1: cannot modify immutable variable 'x'\n1 error\n");
296 test_failure(r,
297 "const int x; void main() { x = 0; }",
298 "error: 1: cannot modify immutable variable 'x'\n1 error\n");
299}
300
301DEF_TEST(SkSLBadIndex, r) {
302 test_failure(r,
303 "void main() { int x = 2[0]; }",
304 "error: 1: expected array, but found 'int'\n1 error\n");
305 test_failure(r,
ethannicholas5961bc92016-10-12 06:39:56 -0700306 "void main() { vec2 x = vec2(0); int y = x[0][0]; }",
307 "error: 1: expected array, but found 'float'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700308}
309
310DEF_TEST(SkSLTernaryMismatch, r) {
311 test_failure(r,
312 "void main() { int x = 5 > 2 ? true : 1.0; }",
313 "error: 1: ternary operator result mismatch: 'bool', 'float'\n1 error\n");
Ethan Nicholas2be687a2017-01-03 16:44:39 -0500314 test_failure(r,
315 "void main() { int x = 5 > 2 ? vec3(1) : 1.0; }",
316 "error: 1: ternary operator result mismatch: 'vec3', 'float'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700317}
318
319DEF_TEST(SkSLInterfaceBlockStorageModifiers, r) {
320 test_failure(r,
321 "uniform foo { out int x; };",
322 "error: 1: interface block fields may not have storage qualifiers\n1 error\n");
323}
ethannicholas22f939e2016-10-13 13:25:34 -0700324
325DEF_TEST(SkSLUseWithoutInitialize, r) {
326 test_failure(r,
327 "void main() { int x; if (5 == 2) x = 3; x++; }",
328 "error: 1: 'x' has not been assigned\n1 error\n");
329 test_failure(r,
330 "void main() { int x[2][2]; int i; x[i][1] = 4; }",
331 "error: 1: 'i' has not been assigned\n1 error\n");
332 test_failure(r,
333 "int main() { int r; return r; }",
334 "error: 1: 'r' has not been assigned\n1 error\n");
335 test_failure(r,
336 "void main() { int x; int y = x; }",
337 "error: 1: 'x' has not been assigned\n1 error\n");
338 test_failure(r,
339 "void main() { bool x; if (true && (false || x)) return; }",
340 "error: 1: 'x' has not been assigned\n1 error\n");
Ethan Nicholasaf197692017-02-27 13:26:45 -0500341 test_failure(r,
342 "void main() { int x; switch (3) { case 0: x = 0; case 1: x = 1; }"
343 "sk_FragColor = vec4(x); }",
344 "error: 1: 'x' has not been assigned\n1 error\n");
ethannicholas22f939e2016-10-13 13:25:34 -0700345}
346
347DEF_TEST(SkSLUnreachable, r) {
348 test_failure(r,
349 "void main() { return; return; }",
350 "error: 1: unreachable\n1 error\n");
351 test_failure(r,
352 "void main() { for (;;) { continue; int x = 1; } }",
353 "error: 1: unreachable\n1 error\n");
354 test_failure(r,
355 "void main() { for (;;) { } return; }",
356 "error: 1: unreachable\n1 error\n");
357 test_failure(r,
358 "void main() { if (true) return; else discard; return; }",
359 "error: 1: unreachable\n1 error\n");
360 test_failure(r,
361 "void main() { return; while (true); }",
362 "error: 1: unreachable\n1 error\n");
363}
364
365DEF_TEST(SkSLNoReturn, r) {
366 test_failure(r,
367 "int foo() { if (2 > 5) return 3; }",
368 "error: 1: function can exit without returning a value\n1 error\n");
369}
370
371DEF_TEST(SkSLBreakOutsideLoop, r) {
372 test_failure(r,
373 "void foo() { while(true) {} if (true) break; }",
Ethan Nicholasaf197692017-02-27 13:26:45 -0500374 "error: 1: break statement must be inside a loop or switch\n1 error\n");
ethannicholas22f939e2016-10-13 13:25:34 -0700375}
376
377DEF_TEST(SkSLContinueOutsideLoop, r) {
378 test_failure(r,
379 "void foo() { for(;;); continue; }",
380 "error: 1: continue statement must be inside a loop\n1 error\n");
Ethan Nicholasaf197692017-02-27 13:26:45 -0500381 test_failure(r,
382 "void foo() { switch (1) { default: continue; } }",
383 "error: 1: continue statement must be inside a loop\n1 error\n");
ethannicholas22f939e2016-10-13 13:25:34 -0700384}
ethannicholas08a92112016-11-09 13:26:45 -0800385
386DEF_TEST(SkSLStaticIfError, r) {
387 // ensure eliminated branch of static if / ternary is still checked for errors
388 test_failure(r,
389 "void foo() { if (true); else x = 5; }",
390 "error: 1: unknown identifier 'x'\n1 error\n");
391 test_failure(r,
392 "void foo() { if (false) x = 5; }",
393 "error: 1: unknown identifier 'x'\n1 error\n");
394 test_failure(r,
395 "void foo() { true ? 5 : x; }",
396 "error: 1: unknown identifier 'x'\n1 error\n");
397 test_failure(r,
398 "void foo() { false ? x : 5; }",
399 "error: 1: unknown identifier 'x'\n1 error\n");
400}
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500401
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500402DEF_TEST(SkSLBadCap, r) {
403 test_failure(r,
404 "bool b = sk_Caps.bugFreeDriver;",
405 "error: 1: unknown capability flag 'bugFreeDriver'\n1 error\n");
406}
407
Ethan Nicholas19671772016-11-28 16:30:17 -0500408DEF_TEST(SkSLBadOffset, r) {
409 test_failure(r,
410 "struct Bad { layout (offset = 5) int x; } bad; void main() { bad.x = 5; }",
411 "error: 1: offset of field 'x' must be a multiple of 4\n1 error\n");
412 test_failure(r,
413 "struct Bad { int x; layout (offset = 0) int y; } bad; void main() { bad.x = 5; }",
414 "error: 1: offset of field 'y' must be at least 4\n1 error\n");
415}
416
Ethan Nicholas9a5610e2017-01-03 15:16:29 -0500417DEF_TEST(SkSLDivByZero, r) {
418 test_failure(r,
419 "int x = 1 / 0;",
420 "error: 1: division by zero\n1 error\n");
421 test_failure(r,
422 "float x = 1 / 0;",
423 "error: 1: division by zero\n1 error\n");
424 test_failure(r,
425 "float x = 1.0 / 0.0;",
426 "error: 1: division by zero\n1 error\n");
427 test_failure(r,
428 "float x = -67.0 / (3.0 - 3);",
429 "error: 1: division by zero\n1 error\n");
430}
431
Ethan Nicholas38657112017-02-09 17:01:22 -0500432DEF_TEST(SkSLUnsupportedGLSLIdentifiers, r) {
433 test_failure(r,
434 "void main() { float x = gl_FragCoord.x; };",
435 "error: 1: unknown identifier 'gl_FragCoord'\n1 error\n");
436 test_failure(r,
437 "void main() { float r = gl_FragColor.r; };",
438 "error: 1: unknown identifier 'gl_FragColor'\n1 error\n");
439}
440
Ethan Nicholasaf197692017-02-27 13:26:45 -0500441DEF_TEST(SkSLWrongSwitchTypes, r) {
442 test_failure(r,
443 "void main() { switch (vec2(1)) { case 1: break; } }",
444 "error: 1: expected 'int', but found 'vec2'\n1 error\n");
445 test_failure(r,
446 "void main() { switch (1) { case vec2(1): break; } }",
447 "error: 1: expected 'int', but found 'vec2'\n1 error\n");
448}
449
450DEF_TEST(SkSLNonConstantCase, r) {
451 test_failure(r,
452 "void main() { int x = 1; switch (1) { case x: break; } }",
453 "error: 1: case value must be a constant\n1 error\n");
454}
455
456DEF_TEST(SkSLDuplicateCase, r) {
457 test_failure(r,
458 "void main() { switch (1) { case 0: case 1: case 0: break; } }",
459 "error: 1: duplicate case value\n1 error\n");
460}
461
Ethan Nicholas0dd30d92017-05-01 16:57:07 -0400462DEF_TEST(SkSLFieldAfterRuntimeArray, r) {
463 test_failure(r,
464 "buffer broken { float x[]; float y; };",
465 "error: 1: only the last entry in an interface block may be a runtime-sized "
466 "array\n1 error\n");
467}
468
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500469#endif