blob: a33e6f16e6dc0ae53bcc643b974af76a41f02f72 [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");
137 test_success(r, "void main() { vec3 x = vec3(1.0, 2.0, 3.0, 4.0); }");
138}
139
140DEF_TEST(SkSLSwizzleScalar, r) {
141 test_failure(r,
142 "void main() { float x = 1; float y = x.y; }",
143 "error: 1: cannot swizzle value of type 'float'\n1 error\n");
144}
145
146DEF_TEST(SkSLSwizzleMatrix, r) {
147 test_failure(r,
148 "void main() { mat2 x = mat2(1); float y = x.y; }",
149 "error: 1: cannot swizzle value of type 'mat2'\n1 error\n");
150}
151
152DEF_TEST(SkSLSwizzleOutOfBounds, r) {
153 test_failure(r,
154 "void main() { vec3 test = vec2(1).xyz; }",
155 "error: 1: invalid swizzle component 'z'\n1 error\n");
156}
157
158DEF_TEST(SkSLSwizzleTooManyComponents, r) {
159 test_failure(r,
160 "void main() { vec4 test = vec2(1).xxxxx; }",
161 "error: 1: too many components in swizzle mask 'xxxxx'\n1 error\n");
162}
163
164DEF_TEST(SkSLSwizzleDuplicateOutput, r) {
165 test_failure(r,
166 "void main() { vec4 test = vec4(1); test.xyyz = vec4(1); }",
167 "error: 1: cannot write to the same swizzle field more than once\n1 error\n");
168}
ethannicholasea4567c2016-10-17 11:24:37 -0700169
ethannicholasb3058bd2016-07-01 08:22:01 -0700170DEF_TEST(SkSLAssignmentTypeMismatch, r) {
171 test_failure(r,
172 "void main() { int x = 1.0; }",
173 "error: 1: expected 'int', but found 'float'\n1 error\n");
ethannicholasea4567c2016-10-17 11:24:37 -0700174 test_failure(r,
175 "void main() { int x; x = 1.0; }",
176 "error: 1: type mismatch: '=' cannot operate on 'int', 'float'\n1 error\n");
177 test_success(r,
178 "void main() { vec3 x = vec3(0); x *= 1.0; }");
179 test_failure(r,
180 "void main() { ivec3 x = ivec3(0); x *= 1.0; }",
181 "error: 1: type mismatch: '*=' cannot operate on 'ivec3', 'float'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700182}
183
184DEF_TEST(SkSLReturnFromVoid, r) {
185 test_failure(r,
186 "void main() { return true; }",
187 "error: 1: may not return a value from a void function\n1 error\n");
188}
189
190DEF_TEST(SkSLReturnMissingValue, r) {
191 test_failure(r,
192 "int foo() { return; } void main() { }",
193 "error: 1: expected function to return 'int'\n1 error\n");
194}
195
196DEF_TEST(SkSLReturnTypeMismatch, r) {
197 test_failure(r,
198 "int foo() { return 1.0; } void main() { }",
199 "error: 1: expected 'int', but found 'float'\n1 error\n");
200}
201
202DEF_TEST(SkSLDuplicateFunction, r) {
203 test_failure(r,
204 "void main() { } void main() { }",
205 "error: 1: duplicate definition of void main()\n1 error\n");
206 test_success(r,
207 "void main(); void main() { }");
208}
209
210DEF_TEST(SkSLUsingInvalidValue, r) {
211 test_failure(r,
212 "void main() { int x = int; }",
213 "error: 1: expected '(' to begin constructor invocation\n1 error\n");
214 test_failure(r,
215 "int test() { return 1; } void main() { int x = test; }",
216 "error: 1: expected '(' to begin function call\n1 error\n");
217}
218DEF_TEST(SkSLDifferentReturnType, r) {
219 test_failure(r,
ethannicholas22f939e2016-10-13 13:25:34 -0700220 "int main() { return 1; } void main() { }",
ethannicholasb3058bd2016-07-01 08:22:01 -0700221 "error: 1: functions 'void main()' and 'int main()' differ only in return type\n1 "
222 "error\n");
223}
224
225DEF_TEST(SkSLDifferentModifiers, r) {
226 test_failure(r,
227 "void test(int x); void test(out int x) { }",
228 "error: 1: modifiers on parameter 1 differ between declaration and definition\n1 "
229 "error\n");
230}
231
232DEF_TEST(SkSLDuplicateSymbol, r) {
233 test_failure(r,
234 "int main; void main() { }",
235 "error: 1: symbol 'main' was already defined\n1 error\n");
236
237 test_failure(r,
238 "int x; int x; void main() { }",
239 "error: 1: symbol 'x' was already defined\n1 error\n");
240
241 test_success(r, "int x; void main() { int x; }");
242}
243
244DEF_TEST(SkSLBinaryTypeMismatch, r) {
245 test_failure(r,
246 "void main() { float x = 3 * true; }",
247 "error: 1: type mismatch: '*' cannot operate on 'int', 'bool'\n1 error\n");
248 test_failure(r,
249 "void main() { bool x = 1 || 2.0; }",
250 "error: 1: type mismatch: '||' cannot operate on 'int', 'float'\n1 error\n");
251}
252
253DEF_TEST(SkSLCallNonFunction, r) {
254 test_failure(r,
255 "void main() { float x = 3; x(); }",
256 "error: 1: 'x' is not a function\n1 error\n");
257}
258
259DEF_TEST(SkSLInvalidUnary, r) {
260 test_failure(r,
261 "void main() { mat4 x = mat4(1); ++x; }",
262 "error: 1: '++' cannot operate on 'mat4'\n1 error\n");
263 test_failure(r,
264 "void main() { vec3 x = vec3(1); --x; }",
265 "error: 1: '--' cannot operate on 'vec3'\n1 error\n");
266 test_failure(r,
267 "void main() { mat4 x = mat4(1); x++; }",
268 "error: 1: '++' cannot operate on 'mat4'\n1 error\n");
269 test_failure(r,
270 "void main() { vec3 x = vec3(1); x--; }",
271 "error: 1: '--' cannot operate on 'vec3'\n1 error\n");
272 test_failure(r,
273 "void main() { int x = !12; }",
274 "error: 1: '!' cannot operate on 'int'\n1 error\n");
275 test_failure(r,
276 "struct foo { } bar; void main() { foo x = +bar; }",
277 "error: 1: '+' cannot operate on 'foo'\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_success(r,
282 "void main() { vec2 x = vec2(1, 1); x = +x; x = -x; }");
283}
284
285DEF_TEST(SkSLInvalidAssignment, r) {
286 test_failure(r,
287 "void main() { 1 = 2; }",
288 "error: 1: cannot assign to '1'\n1 error\n");
289 test_failure(r,
290 "uniform int x; void main() { x = 0; }",
291 "error: 1: cannot modify immutable variable 'x'\n1 error\n");
292 test_failure(r,
293 "const int x; void main() { x = 0; }",
294 "error: 1: cannot modify immutable variable 'x'\n1 error\n");
295}
296
297DEF_TEST(SkSLBadIndex, r) {
298 test_failure(r,
299 "void main() { int x = 2[0]; }",
300 "error: 1: expected array, but found 'int'\n1 error\n");
301 test_failure(r,
ethannicholas5961bc92016-10-12 06:39:56 -0700302 "void main() { vec2 x = vec2(0); int y = x[0][0]; }",
303 "error: 1: expected array, but found 'float'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700304}
305
306DEF_TEST(SkSLTernaryMismatch, r) {
307 test_failure(r,
308 "void main() { int x = 5 > 2 ? true : 1.0; }",
309 "error: 1: ternary operator result mismatch: 'bool', 'float'\n1 error\n");
Ethan Nicholas2be687a2017-01-03 16:44:39 -0500310 test_failure(r,
311 "void main() { int x = 5 > 2 ? vec3(1) : 1.0; }",
312 "error: 1: ternary operator result mismatch: 'vec3', 'float'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700313}
314
315DEF_TEST(SkSLInterfaceBlockStorageModifiers, r) {
316 test_failure(r,
317 "uniform foo { out int x; };",
318 "error: 1: interface block fields may not have storage qualifiers\n1 error\n");
319}
ethannicholas22f939e2016-10-13 13:25:34 -0700320
321DEF_TEST(SkSLUseWithoutInitialize, r) {
322 test_failure(r,
323 "void main() { int x; if (5 == 2) x = 3; x++; }",
324 "error: 1: 'x' has not been assigned\n1 error\n");
325 test_failure(r,
326 "void main() { int x[2][2]; int i; x[i][1] = 4; }",
327 "error: 1: 'i' has not been assigned\n1 error\n");
328 test_failure(r,
329 "int main() { int r; return r; }",
330 "error: 1: 'r' has not been assigned\n1 error\n");
331 test_failure(r,
332 "void main() { int x; int y = x; }",
333 "error: 1: 'x' has not been assigned\n1 error\n");
334 test_failure(r,
335 "void main() { bool x; if (true && (false || x)) return; }",
336 "error: 1: 'x' has not been assigned\n1 error\n");
337}
338
339DEF_TEST(SkSLUnreachable, r) {
340 test_failure(r,
341 "void main() { return; return; }",
342 "error: 1: unreachable\n1 error\n");
343 test_failure(r,
344 "void main() { for (;;) { continue; int x = 1; } }",
345 "error: 1: unreachable\n1 error\n");
346 test_failure(r,
347 "void main() { for (;;) { } return; }",
348 "error: 1: unreachable\n1 error\n");
349 test_failure(r,
350 "void main() { if (true) return; else discard; return; }",
351 "error: 1: unreachable\n1 error\n");
352 test_failure(r,
353 "void main() { return; while (true); }",
354 "error: 1: unreachable\n1 error\n");
355}
356
357DEF_TEST(SkSLNoReturn, r) {
358 test_failure(r,
359 "int foo() { if (2 > 5) return 3; }",
360 "error: 1: function can exit without returning a value\n1 error\n");
361}
362
363DEF_TEST(SkSLBreakOutsideLoop, r) {
364 test_failure(r,
365 "void foo() { while(true) {} if (true) break; }",
366 "error: 1: break statement must be inside a loop\n1 error\n");
367}
368
369DEF_TEST(SkSLContinueOutsideLoop, r) {
370 test_failure(r,
371 "void foo() { for(;;); continue; }",
372 "error: 1: continue statement must be inside a loop\n1 error\n");
373}
ethannicholas08a92112016-11-09 13:26:45 -0800374
375DEF_TEST(SkSLStaticIfError, r) {
376 // ensure eliminated branch of static if / ternary is still checked for errors
377 test_failure(r,
378 "void foo() { if (true); else x = 5; }",
379 "error: 1: unknown identifier 'x'\n1 error\n");
380 test_failure(r,
381 "void foo() { if (false) x = 5; }",
382 "error: 1: unknown identifier 'x'\n1 error\n");
383 test_failure(r,
384 "void foo() { true ? 5 : x; }",
385 "error: 1: unknown identifier 'x'\n1 error\n");
386 test_failure(r,
387 "void foo() { false ? x : 5; }",
388 "error: 1: unknown identifier 'x'\n1 error\n");
389}
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500390
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500391DEF_TEST(SkSLBadCap, r) {
392 test_failure(r,
393 "bool b = sk_Caps.bugFreeDriver;",
394 "error: 1: unknown capability flag 'bugFreeDriver'\n1 error\n");
395}
396
Ethan Nicholas19671772016-11-28 16:30:17 -0500397DEF_TEST(SkSLBadOffset, r) {
398 test_failure(r,
399 "struct Bad { layout (offset = 5) int x; } bad; void main() { bad.x = 5; }",
400 "error: 1: offset of field 'x' must be a multiple of 4\n1 error\n");
401 test_failure(r,
402 "struct Bad { int x; layout (offset = 0) int y; } bad; void main() { bad.x = 5; }",
403 "error: 1: offset of field 'y' must be at least 4\n1 error\n");
404}
405
Ethan Nicholas9a5610e2017-01-03 15:16:29 -0500406DEF_TEST(SkSLDivByZero, r) {
407 test_failure(r,
408 "int x = 1 / 0;",
409 "error: 1: division by zero\n1 error\n");
410 test_failure(r,
411 "float x = 1 / 0;",
412 "error: 1: division by zero\n1 error\n");
413 test_failure(r,
414 "float x = 1.0 / 0.0;",
415 "error: 1: division by zero\n1 error\n");
416 test_failure(r,
417 "float x = -67.0 / (3.0 - 3);",
418 "error: 1: division by zero\n1 error\n");
419}
420
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500421#endif