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