1 /* 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 12 #if SK_SUPPORT_GPU 13 test_failure(skiatest::Reporter * r,const char * src,const char * error)14 static void test_failure(skiatest::Reporter* r, const char* src, const char* error) { 15 SkSL::Compiler compiler; 16 SkDynamicMemoryWStream out; 17 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 } 26 SkString skError(error); 27 if (compiler.errorText() != skError) { 28 SkDebugf("SKSL ERROR:\n source: %s\n expected: %s received: %s", src, error, 29 compiler.errorText().c_str()); 30 } 31 REPORTER_ASSERT(r, compiler.errorText() == skError); 32 } 33 test_success(skiatest::Reporter * r,const char * src)34 static void test_success(skiatest::Reporter* r, const char* src) { 35 SkSL::Compiler compiler; 36 SkDynamicMemoryWStream out; 37 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)); 45 } 46 DEF_TEST(SkSLUndefinedSymbol,r)47 DEF_TEST(SkSLUndefinedSymbol, r) { 48 test_failure(r, 49 "void main() { x = vec2(1); }", 50 "error: 1: unknown identifier 'x'\n1 error\n"); 51 } 52 DEF_TEST(SkSLUndefinedFunction,r)53 DEF_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 DEF_TEST(SkSLGenericArgumentMismatch,r)59 DEF_TEST(SkSLGenericArgumentMismatch, r) { 60 test_failure(r, 61 "void main() { float x = sin(1, 2); }", 62 "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); }"); 68 } 69 DEF_TEST(SkSLArgumentCountMismatch,r)70 DEF_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 DEF_TEST(SkSLArgumentMismatch,r)77 DEF_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 DEF_TEST(SkSLIfTypeMismatch,r)84 DEF_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 DEF_TEST(SkSLDoTypeMismatch,r)90 DEF_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 DEF_TEST(SkSLWhileTypeMismatch,r)96 DEF_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 DEF_TEST(SkSLForTypeMismatch,r)102 DEF_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 DEF_TEST(SkSLConstructorTypeMismatch,r)108 DEF_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, 113 "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, 119 "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 DEF_TEST(SkSLConstructorArgumentCount,r)132 DEF_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_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"); 141 } 142 DEF_TEST(SkSLSwizzleScalar,r)143 DEF_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 DEF_TEST(SkSLSwizzleMatrix,r)149 DEF_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 DEF_TEST(SkSLSwizzleOutOfBounds,r)155 DEF_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 DEF_TEST(SkSLSwizzleTooManyComponents,r)161 DEF_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 DEF_TEST(SkSLSwizzleDuplicateOutput,r)167 DEF_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 } 172 DEF_TEST(SkSLAssignmentTypeMismatch,r)173 DEF_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"); 177 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"); 185 } 186 DEF_TEST(SkSLReturnFromVoid,r)187 DEF_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 DEF_TEST(SkSLReturnMissingValue,r)193 DEF_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 DEF_TEST(SkSLReturnTypeMismatch,r)199 DEF_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 DEF_TEST(SkSLDuplicateFunction,r)205 DEF_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 DEF_TEST(SkSLUsingInvalidValue,r)213 DEF_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 } DEF_TEST(SkSLDifferentReturnType,r)221 DEF_TEST(SkSLDifferentReturnType, r) { 222 test_failure(r, 223 "int main() { return 1; } void main() { }", 224 "error: 1: functions 'void main()' and 'int main()' differ only in return type\n1 " 225 "error\n"); 226 } 227 DEF_TEST(SkSLDifferentModifiers,r)228 DEF_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 DEF_TEST(SkSLDuplicateSymbol,r)235 DEF_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 DEF_TEST(SkSLBinaryTypeMismatch,r)247 DEF_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 DEF_TEST(SkSLCallNonFunction,r)256 DEF_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 DEF_TEST(SkSLInvalidUnary,r)262 DEF_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 DEF_TEST(SkSLInvalidAssignment,r)288 DEF_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 DEF_TEST(SkSLBadIndex,r)300 DEF_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, 305 "void main() { vec2 x = vec2(0); int y = x[0][0]; }", 306 "error: 1: expected array, but found 'float'\n1 error\n"); 307 } 308 DEF_TEST(SkSLTernaryMismatch,r)309 DEF_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"); 313 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"); 316 } 317 DEF_TEST(SkSLInterfaceBlockStorageModifiers,r)318 DEF_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 } 323 DEF_TEST(SkSLUseWithoutInitialize,r)324 DEF_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 test_failure(r, 341 "void main() { int x; switch (3) { case 0: x = 0; case 1: x = 1; }" 342 "sk_FragColor = vec4(x); }", 343 "error: 1: 'x' has not been assigned\n1 error\n"); 344 } 345 DEF_TEST(SkSLUnreachable,r)346 DEF_TEST(SkSLUnreachable, r) { 347 test_failure(r, 348 "void main() { return; return; }", 349 "error: 1: unreachable\n1 error\n"); 350 test_failure(r, 351 "void main() { for (;;) { continue; int x = 1; } }", 352 "error: 1: unreachable\n1 error\n"); 353 test_failure(r, 354 "void main() { for (;;) { } return; }", 355 "error: 1: unreachable\n1 error\n"); 356 test_failure(r, 357 "void main() { if (true) return; else discard; return; }", 358 "error: 1: unreachable\n1 error\n"); 359 test_failure(r, 360 "void main() { return; while (true); }", 361 "error: 1: unreachable\n1 error\n"); 362 } 363 DEF_TEST(SkSLNoReturn,r)364 DEF_TEST(SkSLNoReturn, r) { 365 test_failure(r, 366 "int foo() { if (2 > 5) return 3; }", 367 "error: 1: function can exit without returning a value\n1 error\n"); 368 } 369 DEF_TEST(SkSLBreakOutsideLoop,r)370 DEF_TEST(SkSLBreakOutsideLoop, r) { 371 test_failure(r, 372 "void foo() { while(true) {} if (true) break; }", 373 "error: 1: break statement must be inside a loop or switch\n1 error\n"); 374 } 375 DEF_TEST(SkSLContinueOutsideLoop,r)376 DEF_TEST(SkSLContinueOutsideLoop, r) { 377 test_failure(r, 378 "void foo() { for(;;); continue; }", 379 "error: 1: continue statement must be inside a loop\n1 error\n"); 380 test_failure(r, 381 "void foo() { switch (1) { default: continue; } }", 382 "error: 1: continue statement must be inside a loop\n1 error\n"); 383 } 384 DEF_TEST(SkSLStaticIfError,r)385 DEF_TEST(SkSLStaticIfError, r) { 386 // ensure eliminated branch of static if / ternary is still checked for errors 387 test_failure(r, 388 "void foo() { if (true); else x = 5; }", 389 "error: 1: unknown identifier 'x'\n1 error\n"); 390 test_failure(r, 391 "void foo() { if (false) x = 5; }", 392 "error: 1: unknown identifier 'x'\n1 error\n"); 393 test_failure(r, 394 "void foo() { true ? 5 : x; }", 395 "error: 1: unknown identifier 'x'\n1 error\n"); 396 test_failure(r, 397 "void foo() { false ? x : 5; }", 398 "error: 1: unknown identifier 'x'\n1 error\n"); 399 } 400 DEF_TEST(SkSLBadCap,r)401 DEF_TEST(SkSLBadCap, r) { 402 test_failure(r, 403 "bool b = sk_Caps.bugFreeDriver;", 404 "error: 1: unknown capability flag 'bugFreeDriver'\n1 error\n"); 405 } 406 DEF_TEST(SkSLBadOffset,r)407 DEF_TEST(SkSLBadOffset, r) { 408 test_failure(r, 409 "struct Bad { layout (offset = 5) int x; } bad; void main() { bad.x = 5; }", 410 "error: 1: offset of field 'x' must be a multiple of 4\n1 error\n"); 411 test_failure(r, 412 "struct Bad { int x; layout (offset = 0) int y; } bad; void main() { bad.x = 5; }", 413 "error: 1: offset of field 'y' must be at least 4\n1 error\n"); 414 } 415 DEF_TEST(SkSLDivByZero,r)416 DEF_TEST(SkSLDivByZero, r) { 417 test_failure(r, 418 "int x = 1 / 0;", 419 "error: 1: division by zero\n1 error\n"); 420 test_failure(r, 421 "float x = 1 / 0;", 422 "error: 1: division by zero\n1 error\n"); 423 test_failure(r, 424 "float x = 1.0 / 0.0;", 425 "error: 1: division by zero\n1 error\n"); 426 test_failure(r, 427 "float x = -67.0 / (3.0 - 3);", 428 "error: 1: division by zero\n1 error\n"); 429 } 430 DEF_TEST(SkSLUnsupportedGLSLIdentifiers,r)431 DEF_TEST(SkSLUnsupportedGLSLIdentifiers, r) { 432 test_failure(r, 433 "void main() { float x = gl_FragCoord.x; };", 434 "error: 1: unknown identifier 'gl_FragCoord'\n1 error\n"); 435 test_failure(r, 436 "void main() { float r = gl_FragColor.r; };", 437 "error: 1: unknown identifier 'gl_FragColor'\n1 error\n"); 438 } 439 DEF_TEST(SkSLWrongSwitchTypes,r)440 DEF_TEST(SkSLWrongSwitchTypes, r) { 441 test_failure(r, 442 "void main() { switch (vec2(1)) { case 1: break; } }", 443 "error: 1: expected 'int', but found 'vec2'\n1 error\n"); 444 test_failure(r, 445 "void main() { switch (1) { case vec2(1): break; } }", 446 "error: 1: expected 'int', but found 'vec2'\n1 error\n"); 447 } 448 DEF_TEST(SkSLNonConstantCase,r)449 DEF_TEST(SkSLNonConstantCase, r) { 450 test_failure(r, 451 "void main() { int x = 1; switch (1) { case x: break; } }", 452 "error: 1: case value must be a constant\n1 error\n"); 453 } 454 DEF_TEST(SkSLDuplicateCase,r)455 DEF_TEST(SkSLDuplicateCase, r) { 456 test_failure(r, 457 "void main() { switch (1) { case 0: case 1: case 0: break; } }", 458 "error: 1: duplicate case value\n1 error\n"); 459 } 460 461 #endif 462