blob: 301281c28eb6a9fd6ccfb8194651b88990793585 [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 Nicholas9e1138d2016-11-21 10:39:35 -050016 SkDynamicMemoryWStream out;
Ethan Nicholas941e7e22016-12-12 15:33:30 -050017 SkSL::Program::Settings settings;
18 sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::Default();
19 settings.fCaps = caps.get();
20 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(SkSL::Program::kFragment_Kind,
21 SkString(src), settings);
22 if (program) {
23 SkString ignored;
24 compiler.toSPIRV(*program, &ignored);
25 }
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050026 SkString skError(error);
27 if (compiler.errorText() != skError) {
ethannicholasb3058bd2016-07-01 08:22:01 -070028 SkDebugf("SKSL ERROR:\n source: %s\n expected: %s received: %s", src, error,
29 compiler.errorText().c_str());
30 }
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050031 REPORTER_ASSERT(r, compiler.errorText() == skError);
ethannicholasb3058bd2016-07-01 08:22:01 -070032}
33
34static void test_success(skiatest::Reporter* r, const char* src) {
35 SkSL::Compiler compiler;
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050036 SkDynamicMemoryWStream out;
Ethan Nicholas941e7e22016-12-12 15:33:30 -050037 SkSL::Program::Settings settings;
38 sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::Default();
39 settings.fCaps = caps.get();
40 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(SkSL::Program::kFragment_Kind,
41 SkString(src), settings);
42 REPORTER_ASSERT(r, program);
43 SkString ignored;
44 REPORTER_ASSERT(r, compiler.toSPIRV(*program, &ignored));
ethannicholasb3058bd2016-07-01 08:22:01 -070045}
46
47DEF_TEST(SkSLUndefinedSymbol, r) {
48 test_failure(r,
49 "void main() { x = vec2(1); }",
50 "error: 1: unknown identifier 'x'\n1 error\n");
51}
52
53DEF_TEST(SkSLUndefinedFunction, r) {
54 test_failure(r,
55 "void main() { int x = foo(1); }",
56 "error: 1: unknown identifier 'foo'\n1 error\n");
57}
58
59DEF_TEST(SkSLGenericArgumentMismatch, r) {
60 test_failure(r,
61 "void main() { float x = sin(1, 2); }",
ethannicholas471e8942016-10-28 09:02:46 -070062 "error: 1: call to 'sin' expected 1 argument, but found 2\n1 error\n");
63 test_failure(r,
64 "void main() { float x = sin(true); }",
65 "error: 1: no match for sin(bool)\n1 error\n");
66 test_success(r,
67 "void main() { float x = sin(1); }");
ethannicholasb3058bd2016-07-01 08:22:01 -070068}
69
70DEF_TEST(SkSLArgumentCountMismatch, r) {
71 test_failure(r,
72 "float foo(float x) { return x * x; }"
73 "void main() { float x = foo(1, 2); }",
74 "error: 1: call to 'foo' expected 1 argument, but found 2\n1 error\n");
75}
76
77DEF_TEST(SkSLArgumentMismatch, r) {
78 test_failure(r,
79 "float foo(float x) { return x * x; }"
80 "void main() { float x = foo(true); }",
81 "error: 1: expected 'float', but found 'bool'\n1 error\n");
82}
83
84DEF_TEST(SkSLIfTypeMismatch, r) {
85 test_failure(r,
86 "void main() { if (3) { } }",
87 "error: 1: expected 'bool', but found 'int'\n1 error\n");
88}
89
90DEF_TEST(SkSLDoTypeMismatch, r) {
91 test_failure(r,
92 "void main() { do { } while (vec2(1)); }",
93 "error: 1: expected 'bool', but found 'vec2'\n1 error\n");
94}
95
96DEF_TEST(SkSLWhileTypeMismatch, r) {
97 test_failure(r,
98 "void main() { while (vec3(1)) { } }",
99 "error: 1: expected 'bool', but found 'vec3'\n1 error\n");
100}
101
102DEF_TEST(SkSLForTypeMismatch, r) {
103 test_failure(r,
104 "void main() { for (int x = 0; x; x++) { } }",
105 "error: 1: expected 'bool', but found 'int'\n1 error\n");
106}
107
108DEF_TEST(SkSLConstructorTypeMismatch, r) {
109 test_failure(r,
110 "void main() { vec2 x = vec2(1.0, false); }",
111 "error: 1: expected 'float', but found 'bool'\n1 error\n");
112 test_failure(r,
Ethan Nicholas4578a8e2016-11-01 11:57:42 -0400113 "void main() { vec2 x = vec2(bvec2(false)); }",
114 "error: 1: 'bvec2' is not a valid parameter to 'vec2' constructor\n1 error\n");
115 test_failure(r,
116 "void main() { bvec2 x = bvec2(vec2(1)); }",
117 "error: 1: 'vec2' is not a valid parameter to 'bvec2' constructor\n1 error\n");
118 test_failure(r,
ethannicholasb3058bd2016-07-01 08:22:01 -0700119 "void main() { bool x = bool(1.0); }",
120 "error: 1: cannot construct 'bool'\n1 error\n");
121 test_failure(r,
122 "struct foo { int x; }; void main() { foo x = foo(5); }",
123 "error: 1: cannot construct 'foo'\n1 error\n");
124 test_failure(r,
125 "struct foo { int x; } foo; void main() { float x = float(foo); }",
126 "error: 1: invalid argument to 'float' constructor (expected a number or bool, but found 'foo')\n1 error\n");
127 test_failure(r,
128 "struct foo { int x; } foo; void main() { vec2 x = vec2(foo); }",
129 "error: 1: 'foo' is not a valid parameter to 'vec2' constructor\n1 error\n");
130}
131
132DEF_TEST(SkSLConstructorArgumentCount, r) {
133 test_failure(r,
134 "void main() { vec3 x = vec3(1.0, 2.0); }",
135 "error: 1: invalid arguments to 'vec3' constructor (expected 3 scalars, but "
136 "found 2)\n1 error\n");
Ethan Nicholas84645e32017-02-09 13:57:14 -0500137 test_failure(r,
138 "void main() { vec3 x = vec3(1.0, 2.0, 3.0, 4.0); }",
139 "error: 1: invalid arguments to 'vec3' constructor (expected 3 scalars, but found "
140 "4)\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700141}
142
143DEF_TEST(SkSLSwizzleScalar, r) {
144 test_failure(r,
145 "void main() { float x = 1; float y = x.y; }",
146 "error: 1: cannot swizzle value of type 'float'\n1 error\n");
147}
148
149DEF_TEST(SkSLSwizzleMatrix, r) {
150 test_failure(r,
151 "void main() { mat2 x = mat2(1); float y = x.y; }",
152 "error: 1: cannot swizzle value of type 'mat2'\n1 error\n");
153}
154
155DEF_TEST(SkSLSwizzleOutOfBounds, r) {
156 test_failure(r,
157 "void main() { vec3 test = vec2(1).xyz; }",
158 "error: 1: invalid swizzle component 'z'\n1 error\n");
159}
160
161DEF_TEST(SkSLSwizzleTooManyComponents, r) {
162 test_failure(r,
163 "void main() { vec4 test = vec2(1).xxxxx; }",
164 "error: 1: too many components in swizzle mask 'xxxxx'\n1 error\n");
165}
166
167DEF_TEST(SkSLSwizzleDuplicateOutput, r) {
168 test_failure(r,
169 "void main() { vec4 test = vec4(1); test.xyyz = vec4(1); }",
170 "error: 1: cannot write to the same swizzle field more than once\n1 error\n");
171}
ethannicholasea4567c2016-10-17 11:24:37 -0700172
ethannicholasb3058bd2016-07-01 08:22:01 -0700173DEF_TEST(SkSLAssignmentTypeMismatch, r) {
174 test_failure(r,
175 "void main() { int x = 1.0; }",
176 "error: 1: expected 'int', but found 'float'\n1 error\n");
ethannicholasea4567c2016-10-17 11:24:37 -0700177 test_failure(r,
178 "void main() { int x; x = 1.0; }",
179 "error: 1: type mismatch: '=' cannot operate on 'int', 'float'\n1 error\n");
180 test_success(r,
181 "void main() { vec3 x = vec3(0); x *= 1.0; }");
182 test_failure(r,
183 "void main() { ivec3 x = ivec3(0); x *= 1.0; }",
184 "error: 1: type mismatch: '*=' cannot operate on 'ivec3', 'float'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700185}
186
187DEF_TEST(SkSLReturnFromVoid, r) {
188 test_failure(r,
189 "void main() { return true; }",
190 "error: 1: may not return a value from a void function\n1 error\n");
191}
192
193DEF_TEST(SkSLReturnMissingValue, r) {
194 test_failure(r,
195 "int foo() { return; } void main() { }",
196 "error: 1: expected function to return 'int'\n1 error\n");
197}
198
199DEF_TEST(SkSLReturnTypeMismatch, r) {
200 test_failure(r,
201 "int foo() { return 1.0; } void main() { }",
202 "error: 1: expected 'int', but found 'float'\n1 error\n");
203}
204
205DEF_TEST(SkSLDuplicateFunction, r) {
206 test_failure(r,
207 "void main() { } void main() { }",
208 "error: 1: duplicate definition of void main()\n1 error\n");
209 test_success(r,
210 "void main(); void main() { }");
211}
212
213DEF_TEST(SkSLUsingInvalidValue, r) {
214 test_failure(r,
215 "void main() { int x = int; }",
216 "error: 1: expected '(' to begin constructor invocation\n1 error\n");
217 test_failure(r,
218 "int test() { return 1; } void main() { int x = test; }",
219 "error: 1: expected '(' to begin function call\n1 error\n");
220}
221DEF_TEST(SkSLDifferentReturnType, r) {
222 test_failure(r,
ethannicholas22f939e2016-10-13 13:25:34 -0700223 "int main() { return 1; } void main() { }",
ethannicholasb3058bd2016-07-01 08:22:01 -0700224 "error: 1: functions 'void main()' and 'int main()' differ only in return type\n1 "
225 "error\n");
226}
227
228DEF_TEST(SkSLDifferentModifiers, r) {
229 test_failure(r,
230 "void test(int x); void test(out int x) { }",
231 "error: 1: modifiers on parameter 1 differ between declaration and definition\n1 "
232 "error\n");
233}
234
235DEF_TEST(SkSLDuplicateSymbol, r) {
236 test_failure(r,
237 "int main; void main() { }",
238 "error: 1: symbol 'main' was already defined\n1 error\n");
239
240 test_failure(r,
241 "int x; int x; void main() { }",
242 "error: 1: symbol 'x' was already defined\n1 error\n");
243
244 test_success(r, "int x; void main() { int x; }");
245}
246
247DEF_TEST(SkSLBinaryTypeMismatch, r) {
248 test_failure(r,
249 "void main() { float x = 3 * true; }",
250 "error: 1: type mismatch: '*' cannot operate on 'int', 'bool'\n1 error\n");
251 test_failure(r,
252 "void main() { bool x = 1 || 2.0; }",
253 "error: 1: type mismatch: '||' cannot operate on 'int', 'float'\n1 error\n");
254}
255
256DEF_TEST(SkSLCallNonFunction, r) {
257 test_failure(r,
258 "void main() { float x = 3; x(); }",
259 "error: 1: 'x' is not a function\n1 error\n");
260}
261
262DEF_TEST(SkSLInvalidUnary, r) {
263 test_failure(r,
264 "void main() { mat4 x = mat4(1); ++x; }",
265 "error: 1: '++' cannot operate on 'mat4'\n1 error\n");
266 test_failure(r,
267 "void main() { vec3 x = vec3(1); --x; }",
268 "error: 1: '--' cannot operate on 'vec3'\n1 error\n");
269 test_failure(r,
270 "void main() { mat4 x = mat4(1); x++; }",
271 "error: 1: '++' cannot operate on 'mat4'\n1 error\n");
272 test_failure(r,
273 "void main() { vec3 x = vec3(1); x--; }",
274 "error: 1: '--' cannot operate on 'vec3'\n1 error\n");
275 test_failure(r,
276 "void main() { int x = !12; }",
277 "error: 1: '!' cannot operate on 'int'\n1 error\n");
278 test_failure(r,
279 "struct foo { } bar; void main() { foo x = +bar; }",
280 "error: 1: '+' cannot operate on 'foo'\n1 error\n");
281 test_failure(r,
282 "struct foo { } bar; void main() { foo x = -bar; }",
283 "error: 1: '-' cannot operate on 'foo'\n1 error\n");
284 test_success(r,
285 "void main() { vec2 x = vec2(1, 1); x = +x; x = -x; }");
286}
287
288DEF_TEST(SkSLInvalidAssignment, r) {
289 test_failure(r,
290 "void main() { 1 = 2; }",
291 "error: 1: cannot assign to '1'\n1 error\n");
292 test_failure(r,
293 "uniform int x; void main() { x = 0; }",
294 "error: 1: cannot modify immutable variable 'x'\n1 error\n");
295 test_failure(r,
296 "const int x; void main() { x = 0; }",
297 "error: 1: cannot modify immutable variable 'x'\n1 error\n");
298}
299
300DEF_TEST(SkSLBadIndex, r) {
301 test_failure(r,
302 "void main() { int x = 2[0]; }",
303 "error: 1: expected array, but found 'int'\n1 error\n");
304 test_failure(r,
ethannicholas5961bc92016-10-12 06:39:56 -0700305 "void main() { vec2 x = vec2(0); int y = x[0][0]; }",
306 "error: 1: expected array, but found 'float'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700307}
308
309DEF_TEST(SkSLTernaryMismatch, r) {
310 test_failure(r,
311 "void main() { int x = 5 > 2 ? true : 1.0; }",
312 "error: 1: ternary operator result mismatch: 'bool', 'float'\n1 error\n");
Ethan Nicholas2be687a2017-01-03 16:44:39 -0500313 test_failure(r,
314 "void main() { int x = 5 > 2 ? vec3(1) : 1.0; }",
315 "error: 1: ternary operator result mismatch: 'vec3', 'float'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700316}
317
318DEF_TEST(SkSLInterfaceBlockStorageModifiers, r) {
319 test_failure(r,
320 "uniform foo { out int x; };",
321 "error: 1: interface block fields may not have storage qualifiers\n1 error\n");
322}
ethannicholas22f939e2016-10-13 13:25:34 -0700323
324DEF_TEST(SkSLUseWithoutInitialize, r) {
325 test_failure(r,
326 "void main() { int x; if (5 == 2) x = 3; x++; }",
327 "error: 1: 'x' has not been assigned\n1 error\n");
328 test_failure(r,
329 "void main() { int x[2][2]; int i; x[i][1] = 4; }",
330 "error: 1: 'i' has not been assigned\n1 error\n");
331 test_failure(r,
332 "int main() { int r; return r; }",
333 "error: 1: 'r' has not been assigned\n1 error\n");
334 test_failure(r,
335 "void main() { int x; int y = x; }",
336 "error: 1: 'x' has not been assigned\n1 error\n");
337 test_failure(r,
338 "void main() { bool x; if (true && (false || x)) return; }",
339 "error: 1: 'x' has not been assigned\n1 error\n");
340}
341
342DEF_TEST(SkSLUnreachable, r) {
343 test_failure(r,
344 "void main() { return; return; }",
345 "error: 1: unreachable\n1 error\n");
346 test_failure(r,
347 "void main() { for (;;) { continue; int x = 1; } }",
348 "error: 1: unreachable\n1 error\n");
349 test_failure(r,
350 "void main() { for (;;) { } return; }",
351 "error: 1: unreachable\n1 error\n");
352 test_failure(r,
353 "void main() { if (true) return; else discard; return; }",
354 "error: 1: unreachable\n1 error\n");
355 test_failure(r,
356 "void main() { return; while (true); }",
357 "error: 1: unreachable\n1 error\n");
358}
359
360DEF_TEST(SkSLNoReturn, r) {
361 test_failure(r,
362 "int foo() { if (2 > 5) return 3; }",
363 "error: 1: function can exit without returning a value\n1 error\n");
364}
365
366DEF_TEST(SkSLBreakOutsideLoop, r) {
367 test_failure(r,
368 "void foo() { while(true) {} if (true) break; }",
369 "error: 1: break statement must be inside a loop\n1 error\n");
370}
371
372DEF_TEST(SkSLContinueOutsideLoop, r) {
373 test_failure(r,
374 "void foo() { for(;;); continue; }",
375 "error: 1: continue statement must be inside a loop\n1 error\n");
376}
ethannicholas08a92112016-11-09 13:26:45 -0800377
378DEF_TEST(SkSLStaticIfError, r) {
379 // ensure eliminated branch of static if / ternary is still checked for errors
380 test_failure(r,
381 "void foo() { if (true); else x = 5; }",
382 "error: 1: unknown identifier 'x'\n1 error\n");
383 test_failure(r,
384 "void foo() { if (false) x = 5; }",
385 "error: 1: unknown identifier 'x'\n1 error\n");
386 test_failure(r,
387 "void foo() { true ? 5 : x; }",
388 "error: 1: unknown identifier 'x'\n1 error\n");
389 test_failure(r,
390 "void foo() { false ? x : 5; }",
391 "error: 1: unknown identifier 'x'\n1 error\n");
392}
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500393
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500394DEF_TEST(SkSLBadCap, r) {
395 test_failure(r,
396 "bool b = sk_Caps.bugFreeDriver;",
397 "error: 1: unknown capability flag 'bugFreeDriver'\n1 error\n");
398}
399
Ethan Nicholas19671772016-11-28 16:30:17 -0500400DEF_TEST(SkSLBadOffset, r) {
401 test_failure(r,
402 "struct Bad { layout (offset = 5) int x; } bad; void main() { bad.x = 5; }",
403 "error: 1: offset of field 'x' must be a multiple of 4\n1 error\n");
404 test_failure(r,
405 "struct Bad { int x; layout (offset = 0) int y; } bad; void main() { bad.x = 5; }",
406 "error: 1: offset of field 'y' must be at least 4\n1 error\n");
407}
408
Ethan Nicholas9a5610e2017-01-03 15:16:29 -0500409DEF_TEST(SkSLDivByZero, r) {
410 test_failure(r,
411 "int x = 1 / 0;",
412 "error: 1: division by zero\n1 error\n");
413 test_failure(r,
414 "float x = 1 / 0;",
415 "error: 1: division by zero\n1 error\n");
416 test_failure(r,
417 "float x = 1.0 / 0.0;",
418 "error: 1: division by zero\n1 error\n");
419 test_failure(r,
420 "float x = -67.0 / (3.0 - 3);",
421 "error: 1: division by zero\n1 error\n");
422}
423
Ethan Nicholas38657112017-02-09 17:01:22 -0500424DEF_TEST(SkSLUnsupportedGLSLIdentifiers, r) {
425 test_failure(r,
426 "void main() { float x = gl_FragCoord.x; };",
427 "error: 1: unknown identifier 'gl_FragCoord'\n1 error\n");
428 test_failure(r,
429 "void main() { float r = gl_FragColor.r; };",
430 "error: 1: unknown identifier 'gl_FragColor'\n1 error\n");
431}
432
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500433#endif