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 14static void test(skiatest::Reporter* r, const char* src, const SkSL::Program::Settings& settings, 15 const char* expected, SkSL::Program::Inputs* inputs, 16 SkSL::Program::Kind kind = SkSL::Program::kFragment_Kind) { 17 SkSL::Compiler compiler; 18 SkString output; 19 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(kind, SkString(src), settings); 20 if (!program) { 21 SkDebugf("Unexpected error compiling %s\n%s", src, compiler.errorText().c_str()); 22 } 23 REPORTER_ASSERT(r, program); 24 *inputs = program->fInputs; 25 REPORTER_ASSERT(r, compiler.toGLSL(*program, &output)); 26 if (program) { 27 SkString skExpected(expected); 28 if (output != skExpected) { 29 SkDebugf("GLSL MISMATCH:\nsource:\n%s\n\nexpected:\n'%s'\n\nreceived:\n'%s'", src, 30 expected, output.c_str()); 31 } 32 REPORTER_ASSERT(r, output == skExpected); 33 } 34} 35 36static void test(skiatest::Reporter* r, const char* src, const GrShaderCaps& caps, 37 const char* expected, SkSL::Program::Kind kind = SkSL::Program::kFragment_Kind) { 38 SkSL::Program::Settings settings; 39 settings.fCaps = ∩︀ 40 SkSL::Program::Inputs inputs; 41 test(r, src, settings, expected, &inputs, kind); 42} 43 44DEF_TEST(SkSLHelloWorld, r) { 45 test(r, 46 "void main() { sk_FragColor = vec4(0.75); }", 47 *SkSL::ShaderCapsFactory::Default(), 48 "#version 400\n" 49 "out vec4 sk_FragColor;\n" 50 "void main() {\n" 51 " sk_FragColor = vec4(0.75);\n" 52 "}\n"); 53} 54 55DEF_TEST(SkSLControl, r) { 56 test(r, 57 "void main() {" 58 "if (sqrt(2) > 5) { sk_FragColor = vec4(0.75); } else { discard; }" 59 "int i = 0;" 60 "while (i < 10) sk_FragColor *= 0.5;" 61 "do { sk_FragColor += 0.01; } while (sk_FragColor.x < 0.75);" 62 "for (int i = 0; i < 10; i++) {" 63 "if (i % 2 == 1) break; else continue;" 64 "}" 65 "return;" 66 "}", 67 *SkSL::ShaderCapsFactory::Default(), 68 "#version 400\n" 69 "out vec4 sk_FragColor;\n" 70 "void main() {\n" 71 " if (sqrt(2.0) > 5.0) {\n" 72 " sk_FragColor = vec4(0.75);\n" 73 " } else {\n" 74 " discard;\n" 75 " }\n" 76 " int i = 0;\n" 77 " while (true) sk_FragColor *= 0.5;\n" 78 " do {\n" 79 " sk_FragColor += 0.01;\n" 80 " } while (sk_FragColor.x < 0.75);\n" 81 " for (int i = 0;i < 10; i++) {\n" 82 " if (i % 2 == 1) break; else continue;\n" 83 " }\n" 84 " return;\n" 85 "}\n"); 86} 87 88DEF_TEST(SkSLFunctions, r) { 89 test(r, 90 "float foo(float v[2]) { return v[0] * v[1]; }" 91 "void bar(inout float x) { float y[2], z; y[0] = x; y[1] = x * 2; z = foo(y); x = z; }" 92 "void main() { float x = 10; bar(x); sk_FragColor = vec4(x); }", 93 *SkSL::ShaderCapsFactory::Default(), 94 "#version 400\n" 95 "out vec4 sk_FragColor;\n" 96 "float foo(in float v[2]) {\n" 97 " return v[0] * v[1];\n" 98 "}\n" 99 "void bar(inout float x) {\n" 100 " float y[2], z;\n" 101 " y[0] = x;\n" 102 " y[1] = x * 2.0;\n" 103 " z = foo(y);\n" 104 " x = z;\n" 105 "}\n" 106 "void main() {\n" 107 " float x = 10.0;\n" 108 " bar(10.0);\n" 109 " sk_FragColor = vec4(10.0);\n" 110 "}\n"); 111} 112 113DEF_TEST(SkSLOperators, r) { 114 test(r, 115 "void main() {" 116 "float x = 1, y = 2;" 117 "int z = 3;" 118 "x = x - x + y * z * x * (y - z);" 119 "y = x / y / z;" 120 "z = (z / 2 % 3 << 4) >> 2 << 1;" 121 "bool b = (x > 4) == x < 2 || 2 >= sqrt(2) && y <= z;" 122 "x += 12;" 123 "x -= 12;" 124 "x *= y /= z = 10;" 125 "b ||= false;" 126 "b &&= true;" 127 "b ^^= false;" 128 "z |= 0;" 129 "z &= -1;" 130 "z ^= 0;" 131 "z >>= 2;" 132 "z <<= 4;" 133 "z %= 5;" 134 "}", 135 *SkSL::ShaderCapsFactory::Default(), 136 "#version 400\n" 137 "out vec4 sk_FragColor;\n" 138 "void main() {\n" 139 " float x = 1.0, y = 2.0;\n" 140 " int z = 3;\n" 141 " x = -6.0;\n" 142 " y = -1.0;\n" 143 " z = 8;\n" 144 " bool b = false == true || 2.0 >= sqrt(2.0) && true;\n" 145 " x += 12.0;\n" 146 " x -= 12.0;\n" 147 " x *= (y /= float(z = 10));\n" 148 " b ||= false;\n" 149 " b &&= true;\n" 150 " b ^^= false;\n" 151 " z |= 0;\n" 152 " z &= -1;\n" 153 " z ^= 0;\n" 154 " z >>= 2;\n" 155 " z <<= 4;\n" 156 " z %= 5;\n" 157 "}\n"); 158} 159 160DEF_TEST(SkSLMatrices, r) { 161 test(r, 162 "void main() {" 163 "mat2x4 x = mat2x4(1);" 164 "mat3x2 y = mat3x2(1, 0, 0, 1, vec2(2, 2));" 165 "mat3x4 z = x * y;" 166 "vec3 v1 = mat3(1) * vec3(1);" 167 "vec3 v2 = vec3(1) * mat3(1);" 168 "}", 169 *SkSL::ShaderCapsFactory::Default(), 170 "#version 400\n" 171 "out vec4 sk_FragColor;\n" 172 "void main() {\n" 173 " mat2x4 x = mat2x4(1.0);\n" 174 " mat3x2 y = mat3x2(1.0, 0.0, 0.0, 1.0, vec2(2.0, 2.0));\n" 175 " mat3x4 z = x * y;\n" 176 " vec3 v1 = mat3(1.0) * vec3(1.0);\n" 177 " vec3 v2 = vec3(1.0) * mat3(1.0);\n" 178 "}\n"); 179} 180 181DEF_TEST(SkSLInterfaceBlock, r) { 182 test(r, 183 "uniform testBlock {" 184 "float x;" 185 "float y[2];" 186 "layout(binding=12) mat3x2 z;" 187 "bool w;" 188 "};" 189 "void main() {" 190 " sk_FragColor = vec4(x, y[0], y[1], 0);" 191 "}", 192 *SkSL::ShaderCapsFactory::Default(), 193 "#version 400\n" 194 "out vec4 sk_FragColor;\n" 195 "uniform testBlock {\n" 196 " float x;\n" 197 " float[2] y;\n" 198 " layout (binding = 12) mat3x2 z;\n" 199 " bool w;\n" 200 "};\n" 201 "void main() {\n" 202 " sk_FragColor = vec4(x, y[0], y[1], 0.0);\n" 203 "}\n"); 204 test(r, 205 "uniform testBlock {" 206 "float x;" 207 "} test;" 208 "void main() {" 209 " sk_FragColor = vec4(test.x);" 210 "}", 211 *SkSL::ShaderCapsFactory::Default(), 212 "#version 400\n" 213 "out vec4 sk_FragColor;\n" 214 "uniform testBlock {\n" 215 " float x;\n" 216 "} test;\n" 217 "void main() {\n" 218 " sk_FragColor = vec4(test.x);\n" 219 "}\n"); 220 test(r, 221 "uniform testBlock {" 222 "float x;" 223 "} test[2];" 224 "void main() {" 225 " sk_FragColor = vec4(test[1].x);" 226 "}", 227 *SkSL::ShaderCapsFactory::Default(), 228 "#version 400\n" 229 "out vec4 sk_FragColor;\n" 230 "uniform testBlock {\n" 231 " float x;\n" 232 "} test[2];\n" 233 "void main() {\n" 234 " sk_FragColor = vec4(test[1].x);\n" 235 "}\n"); 236} 237 238DEF_TEST(SkSLStructs, r) { 239 test(r, 240 "struct A {" 241 "int x;" 242 "int y;" 243 "} a1, a2;" 244 "A a3;" 245 "struct B {" 246 "float x;" 247 "float y[2];" 248 "layout(binding=1) A z;" 249 "};" 250 "B b1, b2, b3;" 251 "void main() {" 252 "}", 253 *SkSL::ShaderCapsFactory::Default(), 254 "#version 400\n" 255 "out vec4 sk_FragColor;\n" 256 "struct A {\n" 257 " int x;\n" 258 " int y;\n" 259 "} a1, a2;\n" 260 "A a3;\n" 261 "struct B {\n" 262 " float x;\n" 263 " float[2] y;\n" 264 " layout (binding = 1) A z;\n" 265 "} b1, b2, b3;\n" 266 "void main() {\n" 267 "}\n"); 268} 269 270DEF_TEST(SkSLVersion, r) { 271 test(r, 272 "in float test; void main() { sk_FragColor = vec4(0.75); }", 273 *SkSL::ShaderCapsFactory::Version450Core(), 274 "#version 450 core\n" 275 "out vec4 sk_FragColor;\n" 276 "in float test;\n" 277 "void main() {\n" 278 " sk_FragColor = vec4(0.75);\n" 279 "}\n"); 280 test(r, 281 "in float test; void main() { sk_FragColor = vec4(0.75); }", 282 *SkSL::ShaderCapsFactory::Version110(), 283 "#version 110\n" 284 "varying float test;\n" 285 "void main() {\n" 286 " gl_FragColor = vec4(0.75);\n" 287 "}\n"); 288} 289 290DEF_TEST(SkSLUsesPrecisionModifiers, r) { 291 test(r, 292 "void main() { float x = 0.75; highp float y = 1; }", 293 *SkSL::ShaderCapsFactory::Default(), 294 "#version 400\n" 295 "out vec4 sk_FragColor;\n" 296 "void main() {\n" 297 " float x = 0.75;\n" 298 " float y = 1.0;\n" 299 "}\n"); 300 test(r, 301 "void main() { float x = 0.75; highp float y = 1; }", 302 *SkSL::ShaderCapsFactory::UsesPrecisionModifiers(), 303 "#version 400\n" 304 "precision highp float;\n" 305 "out mediump vec4 sk_FragColor;\n" 306 "void main() {\n" 307 " float x = 0.75;\n" 308 " highp float y = 1.0;\n" 309 "}\n"); 310} 311 312DEF_TEST(SkSLMinAbs, r) { 313 test(r, 314 "void main() {" 315 "float x = -5;" 316 "x = min(abs(x), 6);" 317 "}", 318 *SkSL::ShaderCapsFactory::Default(), 319 "#version 400\n" 320 "out vec4 sk_FragColor;\n" 321 "void main() {\n" 322 " float x = -5.0;\n" 323 " x = min(abs(-5.0), 6.0);\n" 324 "}\n"); 325 326 test(r, 327 "void main() {" 328 "float x = -5.0;" 329 "x = min(abs(x), 6.0);" 330 "}", 331 *SkSL::ShaderCapsFactory::CannotUseMinAndAbsTogether(), 332 "#version 400\n" 333 "out vec4 sk_FragColor;\n" 334 "void main() {\n" 335 " float minAbsHackVar0;\n" 336 " float minAbsHackVar1;\n" 337 " float x = -5.0;\n" 338 " x = ((minAbsHackVar0 = abs(-5.0)) < (minAbsHackVar1 = 6.0) ? minAbsHackVar0 : " 339 "minAbsHackVar1);\n" 340 "}\n"); 341} 342 343DEF_TEST(SkSLNegatedAtan, r) { 344 test(r, 345 "void main() { vec2 x = vec2(1, 2); float y = atan(x.x, -(2 * x.y)); }", 346 *SkSL::ShaderCapsFactory::Default(), 347 "#version 400\n" 348 "out vec4 sk_FragColor;\n" 349 "void main() {\n" 350 " vec2 x = vec2(1.0, 2.0);\n" 351 " float y = atan(x.x, -(2.0 * x.y));\n" 352 "}\n"); 353 test(r, 354 "void main() { vec2 x = vec2(1, 2); float y = atan(x.x, -(2 * x.y)); }", 355 *SkSL::ShaderCapsFactory::MustForceNegatedAtanParamToFloat(), 356 "#version 400\n" 357 "out vec4 sk_FragColor;\n" 358 "void main() {\n" 359 " vec2 x = vec2(1.0, 2.0);\n" 360 " float y = atan(x.x, -1.0 * (2.0 * x.y));\n" 361 "}\n"); 362} 363 364DEF_TEST(SkSLModifiersDeclaration, r) { 365 test(r, 366 "layout(blend_support_all_equations) out;" 367 "void main() { }", 368 *SkSL::ShaderCapsFactory::Default(), 369 "#version 400\n" 370 "out vec4 sk_FragColor;\n" 371 "layout (blend_support_all_equations) out ;\n" 372 "void main() {\n" 373 "}\n"); 374} 375 376DEF_TEST(SkSLHex, r) { 377 test(r, 378 "void main() {" 379 "int i1 = 0x0;" 380 "int i2 = 0x1234abcd;" 381 "int i3 = 0x7fffffff;" 382 "int i4 = 0xffffffff;" 383 "int i5 = -0xbeef;" 384 "uint u1 = 0x0;" 385 "uint u2 = 0x1234abcd;" 386 "uint u3 = 0x7fffffff;" 387 "uint u4 = 0xffffffff;" 388 "}", 389 *SkSL::ShaderCapsFactory::Default(), 390 "#version 400\n" 391 "out vec4 sk_FragColor;\n" 392 "void main() {\n" 393 " int i1 = 0;\n" 394 " int i2 = 305441741;\n" 395 " int i3 = 2147483647;\n" 396 " int i4 = -1;\n" 397 " int i5 = -48879;\n" 398 " uint u1 = 0u;\n" 399 " uint u2 = 305441741u;\n" 400 " uint u3 = 2147483647u;\n" 401 " uint u4 = 4294967295u;\n" 402 "}\n"); 403} 404 405DEF_TEST(SkSLVectorConstructors, r) { 406 test(r, 407 "vec2 v1 = vec2(1);" 408 "vec2 v2 = vec2(1, 2);" 409 "vec2 v3 = vec2(vec2(1));" 410 "vec3 v4 = vec3(vec2(1), 1.0);" 411 "ivec2 v5 = ivec2(1);" 412 "ivec2 v6 = ivec2(vec2(1, 2));" 413 "vec2 v7 = vec2(ivec2(1, 2));", 414 *SkSL::ShaderCapsFactory::Default(), 415 "#version 400\n" 416 "out vec4 sk_FragColor;\n" 417 "vec2 v1 = vec2(1.0);\n" 418 "vec2 v2 = vec2(1.0, 2.0);\n" 419 "vec2 v3 = vec2(1.0);\n" 420 "vec3 v4 = vec3(vec2(1.0), 1.0);\n" 421 "ivec2 v5 = ivec2(1);\n" 422 "ivec2 v6 = ivec2(vec2(1.0, 2.0));\n" 423 "vec2 v7 = vec2(ivec2(1, 2));\n"); 424} 425 426DEF_TEST(SkSLArrayConstructors, r) { 427 test(r, 428 "float test1[] = float[](1, 2, 3, 4);" 429 "vec2 test2[] = vec2[](vec2(1, 2), vec2(3, 4));" 430 "mat4 test3[] = mat4[]();", 431 *SkSL::ShaderCapsFactory::Default(), 432 "#version 400\n" 433 "out vec4 sk_FragColor;\n" 434 "float test1[] = float[](1.0, 2.0, 3.0, 4.0);\n" 435 "vec2 test2[] = vec2[](vec2(1.0, 2.0), vec2(3.0, 4.0));\n" 436 "mat4 test3[] = mat4[]();\n"); 437} 438 439DEF_TEST(SkSLDerivatives, r) { 440 test(r, 441 "void main() { float x = dFdx(1); }", 442 *SkSL::ShaderCapsFactory::Default(), 443 "#version 400\n" 444 "out vec4 sk_FragColor;\n" 445 "void main() {\n" 446 " float x = dFdx(1.0);\n" 447 "}\n"); 448 test(r, 449 "void main() { float x = 1; }", 450 *SkSL::ShaderCapsFactory::ShaderDerivativeExtensionString(), 451 "#version 400\n" 452 "out vec4 sk_FragColor;\n" 453 "void main() {\n" 454 " float x = 1.0;\n" 455 "}\n"); 456 test(r, 457 "void main() { float x = dFdx(1); }", 458 *SkSL::ShaderCapsFactory::ShaderDerivativeExtensionString(), 459 "#version 400\n" 460 "#extension GL_OES_standard_derivatives : require\n" 461 "out vec4 sk_FragColor;\n" 462 "void main() {\n" 463 " float x = dFdx(1.0);\n" 464 "}\n"); 465} 466 467DEF_TEST(SkSLConstantFolding, r) { 468 test(r, 469 "void main() {" 470 "float f_add = 32 + 2;" 471 "float f_sub = 32 - 2;" 472 "float f_mul = 32 * 2;" 473 "float f_div = 32 / 2;" 474 "float mixed = (12 > 2.0) ? (10 * 2 / 5 + 18 - 3) : 0;" 475 "int i_add = 32 + 2;" 476 "int i_sub = 32 - 2;" 477 "int i_mul = 32 * 2;" 478 "int i_div = 32 / 2;" 479 "int i_or = 12 | 6;" 480 "int i_and = 254 & 7;" 481 "int i_xor = 2 ^ 7;" 482 "int i_shl = 1 << 4;" 483 "int i_shr = 128 >> 2;" 484 "bool gt_it = 6 > 5;" 485 "bool gt_if = 6 > 6;" 486 "bool gt_ft = 6.0 > 5.0;" 487 "bool gt_ff = 6.0 > 6.0;" 488 "bool gte_it = 6 >= 6;" 489 "bool gte_if = 6 >= 7;" 490 "bool gte_ft = 6.0 >= 6.0;" 491 "bool gte_ff = 6.0 >= 7.0;" 492 "bool lte_it = 6 <= 6;" 493 "bool lte_if = 6 <= 5;" 494 "bool lte_ft = 6.0 <= 6.0;" 495 "bool lte_ff = 6.0 <= 5.0;" 496 "bool or_t = 1 == 1 || 2 == 8;" 497 "bool or_f = 1 > 1 || 2 == 8;" 498 "bool and_t = 1 == 1 && 2 <= 8;" 499 "bool and_f = 1 == 2 && 2 == 8;" 500 "bool xor_t = 1 == 1 ^^ 1 != 1;" 501 "bool xor_f = 1 == 1 ^^ 1 == 1;" 502 "int ternary = 10 > 5 ? 10 : 5;" 503 "}", 504 *SkSL::ShaderCapsFactory::Default(), 505 "#version 400\n" 506 "out vec4 sk_FragColor;\n" 507 "void main() {\n" 508 " float f_add = 34.0;\n" 509 " float f_sub = 30.0;\n" 510 " float f_mul = 64.0;\n" 511 " float f_div = 16.0;\n" 512 " float mixed = 19.0;\n" 513 " int i_add = 34;\n" 514 " int i_sub = 30;\n" 515 " int i_mul = 64;\n" 516 " int i_div = 16;\n" 517 " int i_or = 14;\n" 518 " int i_and = 6;\n" 519 " int i_xor = 5;\n" 520 " int i_shl = 16;\n" 521 " int i_shr = 32;\n" 522 " bool gt_it = true;\n" 523 " bool gt_if = false;\n" 524 " bool gt_ft = true;\n" 525 " bool gt_ff = false;\n" 526 " bool gte_it = true;\n" 527 " bool gte_if = false;\n" 528 " bool gte_ft = true;\n" 529 " bool gte_ff = false;\n" 530 " bool lte_it = true;\n" 531 " bool lte_if = false;\n" 532 " bool lte_ft = true;\n" 533 " bool lte_ff = false;\n" 534 " bool or_t = true;\n" 535 " bool or_f = false;\n" 536 " bool and_t = true;\n" 537 " bool and_f = false;\n" 538 " bool xor_t = true;\n" 539 " bool xor_f = false;\n" 540 " int ternary = 10;\n" 541 "}\n"); 542} 543 544DEF_TEST(SkSLStaticIf, r) { 545 test(r, 546 "void main() {" 547 "int x;" 548 "if (true) x = 1;" 549 "if (2 > 1) x = 2; else x = 3;" 550 "if (1 > 2) x = 4; else x = 5;" 551 "if (false) x = 6;" 552 "}", 553 *SkSL::ShaderCapsFactory::Default(), 554 "#version 400\n" 555 "out vec4 sk_FragColor;\n" 556 "void main() {\n" 557 " int x;\n" 558 " x = 1;\n" 559 " x = 2;\n" 560 " x = 5;\n" 561 " {\n" 562 " }\n" 563 "}\n"); 564} 565 566DEF_TEST(SkSLCaps, r) { 567 test(r, 568 "void main() {" 569 "int x;" 570 "if (sk_Caps.externalTextureSupport) x = 1;" 571 "if (sk_Caps.fbFetchSupport) x = 2;" 572 "if (sk_Caps.dropsTileOnZeroDivide && sk_Caps.texelFetchSupport) x = 3;" 573 "if (sk_Caps.dropsTileOnZeroDivide && sk_Caps.canUseAnyFunctionInShader) x = 4;" 574 "}", 575 *SkSL::ShaderCapsFactory::VariousCaps(), 576 "#version 400\n" 577 "out vec4 sk_FragColor;\n" 578 "void main() {\n" 579 " int x;\n" 580 " x = 1;\n" 581 " {\n" 582 " }\n" 583 " x = 3;\n" 584 " {\n" 585 " }\n" 586 "}\n"); 587} 588 589DEF_TEST(SkSLTexture, r) { 590 test(r, 591 "uniform sampler1D one;" 592 "uniform sampler2D two;" 593 "void main() {" 594 "vec4 a = texture(one, 0);" 595 "vec4 b = texture(two, vec2(0));" 596 "vec4 c = texture(one, vec2(0));" 597 "vec4 d = texture(two, vec3(0));" 598 "}", 599 *SkSL::ShaderCapsFactory::Default(), 600 "#version 400\n" 601 "out vec4 sk_FragColor;\n" 602 "uniform sampler1D one;\n" 603 "uniform sampler2D two;\n" 604 "void main() {\n" 605 " vec4 a = texture(one, 0.0);\n" 606 " vec4 b = texture(two, vec2(0.0));\n" 607 " vec4 c = textureProj(one, vec2(0.0));\n" 608 " vec4 d = textureProj(two, vec3(0.0));\n" 609 "}\n"); 610 test(r, 611 "uniform sampler1D one;" 612 "uniform sampler2D two;" 613 "void main() {" 614 "vec4 a = texture(one, 0);" 615 "vec4 b = texture(two, vec2(0));" 616 "vec4 c = texture(one, vec2(0));" 617 "vec4 d = texture(two, vec3(0));" 618 "}", 619 *SkSL::ShaderCapsFactory::Version110(), 620 "#version 110\n" 621 "uniform sampler1D one;\n" 622 "uniform sampler2D two;\n" 623 "void main() {\n" 624 " vec4 a = texture1D(one, 0.0);\n" 625 " vec4 b = texture2D(two, vec2(0.0));\n" 626 " vec4 c = texture1DProj(one, vec2(0.0));\n" 627 " vec4 d = texture2DProj(two, vec3(0.0));\n" 628 "}\n"); 629} 630 631DEF_TEST(SkSLOffset, r) { 632 test(r, 633 "struct Test {" 634 "layout(offset = 0) int x;" 635 "layout(offset = 4) int y;" 636 "int z;" 637 "} test;", 638 *SkSL::ShaderCapsFactory::Default(), 639 "#version 400\n" 640 "out vec4 sk_FragColor;\n" 641 "struct Test {\n" 642 " layout (offset = 0) int x;\n" 643 " layout (offset = 4) int y;\n" 644 " int z;\n" 645 "} test;\n"); 646} 647 648DEF_TEST(SkSLFragCoord, r) { 649 SkSL::Program::Settings settings; 650 settings.fFlipY = true; 651 sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::FragCoordsOld(); 652 settings.fCaps = caps.get(); 653 SkSL::Program::Inputs inputs; 654 test(r, 655 "void main() { sk_FragColor.xy = sk_FragCoord.xy; }", 656 settings, 657 "#version 110\n" 658 "#extension GL_ARB_fragment_coord_conventions : require\n" 659 "layout(origin_upper_left) in vec4 gl_FragCoord;\n" 660 "void main() {\n" 661 " gl_FragColor.xy = gl_FragCoord.xy;\n" 662 "}\n", 663 &inputs); 664 REPORTER_ASSERT(r, !inputs.fRTHeight); 665 666 caps = SkSL::ShaderCapsFactory::FragCoordsNew(); 667 settings.fCaps = caps.get(); 668 test(r, 669 "void main() { sk_FragColor.xy = sk_FragCoord.xy; }", 670 settings, 671 "#version 400\n" 672 "layout(origin_upper_left) in vec4 gl_FragCoord;\n" 673 "out vec4 sk_FragColor;\n" 674 "void main() {\n" 675 " sk_FragColor.xy = gl_FragCoord.xy;\n" 676 "}\n", 677 &inputs); 678 REPORTER_ASSERT(r, !inputs.fRTHeight); 679 680 caps = SkSL::ShaderCapsFactory::Default(); 681 settings.fCaps = caps.get(); 682 test(r, 683 "void main() { sk_FragColor.xy = sk_FragCoord.xy; }", 684 settings, 685 "#version 400\n" 686 "uniform float u_skRTHeight;\n" 687 "out vec4 sk_FragColor;\n" 688 "void main() {\n" 689 " vec2 _sktmpCoord = gl_FragCoord.xy;\n" 690 " vec4 sk_FragCoord = vec4(_sktmpCoord.x, u_skRTHeight - _sktmpCoord.y, 1.0, 1.0);\n" 691 " sk_FragColor.xy = sk_FragCoord.xy;\n" 692 "}\n", 693 &inputs); 694 REPORTER_ASSERT(r, inputs.fRTHeight); 695 696 settings.fFlipY = false; 697 test(r, 698 "void main() { sk_FragColor.xy = sk_FragCoord.xy; }", 699 settings, 700 "#version 400\n" 701 "out vec4 sk_FragColor;\n" 702 "void main() {\n" 703 " sk_FragColor.xy = gl_FragCoord.xy;\n" 704 "}\n", 705 &inputs); 706 REPORTER_ASSERT(r, !inputs.fRTHeight); 707} 708 709DEF_TEST(SkSLVertexID, r) { 710 test(r, 711 "out int id; void main() { id = sk_VertexID; }", 712 *SkSL::ShaderCapsFactory::Default(), 713 "#version 400\n" 714 "out int id;\n" 715 "void main() {\n" 716 " id = gl_VertexID;\n" 717 "}\n", 718 SkSL::Program::kVertex_Kind); 719} 720 721DEF_TEST(SkSLClipDistance, r) { 722 test(r, 723 "void main() { sk_ClipDistance[0] = 0; }", 724 *SkSL::ShaderCapsFactory::Default(), 725 "#version 400\n" 726 "void main() {\n" 727 " gl_ClipDistance[0] = 0.0;\n" 728 "}\n", 729 SkSL::Program::kVertex_Kind); 730 test(r, 731 "void main() { sk_FragColor = vec4(sk_ClipDistance[0]); }", 732 *SkSL::ShaderCapsFactory::Default(), 733 "#version 400\n" 734 "out vec4 sk_FragColor;\n" 735 "void main() {\n" 736 " sk_FragColor = vec4(gl_ClipDistance[0]);\n" 737 "}\n"); 738} 739 740DEF_TEST(SkSLArrayTypes, r) { 741 test(r, 742 "void main() { vec2 x[2] = vec2[2](vec2(1), vec2(2));" 743 "vec2[2] y = vec2[2](vec2(3), vec2(4)); }", 744 *SkSL::ShaderCapsFactory::Default(), 745 "#version 400\n" 746 "out vec4 sk_FragColor;\n" 747 "void main() {\n" 748 " vec2 x[2] = vec2[2](vec2(1.0), vec2(2.0));\n" 749 " vec2[2] y = vec2[2](vec2(3.0), vec2(4.0));\n" 750 "}\n"); 751} 752 753DEF_TEST(SkSLGeometry, r) { 754 test(r, 755 "layout(points) in;" 756 "layout(invocations = 2) in;" 757 "layout(line_strip, max_vertices = 2) out;" 758 "void main() {" 759 "gl_Position = sk_in[0].gl_Position + vec4(-0.5, 0, 0, sk_InvocationID);" 760 "EmitVertex();" 761 "gl_Position = sk_in[0].gl_Position + vec4(0.5, 0, 0, sk_InvocationID);" 762 "EmitVertex();" 763 "EndPrimitive();" 764 "}", 765 *SkSL::ShaderCapsFactory::Default(), 766 "#version 400\n" 767 "layout (points) in ;\n" 768 "layout (invocations = 2) in ;\n" 769 "layout (line_strip, max_vertices = 2) out ;\n" 770 "void main() {\n" 771 " gl_Position = gl_in[0].gl_Position + vec4(-0.5, 0.0, 0.0, float(gl_InvocationID));\n" 772 " EmitVertex();\n" 773 " gl_Position = gl_in[0].gl_Position + vec4(0.5, 0.0, 0.0, float(gl_InvocationID));\n" 774 " EmitVertex();\n" 775 " EndPrimitive();\n" 776 "}\n", 777 SkSL::Program::kGeometry_Kind); 778} 779 780DEF_TEST(SkSLSwitch, r) { 781 test(r, 782 "void main() {" 783 " float x;" 784 " switch (1) {" 785 " case 0:" 786 " x = 0.0;" 787 " break;" 788 " case 1:" 789 " x = 1.0;" 790 " break;" 791 " default:" 792 " x = 2.0;" 793 " }" 794 " sk_FragColor = vec4(x);" 795 "}", 796 *SkSL::ShaderCapsFactory::Default(), 797 "#version 400\n" 798 "out vec4 sk_FragColor;\n" 799 "void main() {\n" 800 " float x;\n" 801 " switch (1) {\n" 802 " case 0:\n" 803 " x = 0.0;\n" 804 " break;\n" 805 " case 1:\n" 806 " x = 1.0;\n" 807 " break;\n" 808 " default:\n" 809 " x = 2.0;\n" 810 " }\n" 811 " sk_FragColor = vec4(x);\n" 812 "}\n"); 813 test(r, 814 "void main() {" 815 " float x;" 816 " switch (2) {" 817 " case 0:" 818 " x = 0.0;" 819 " case 1:" 820 " x = 1.0;" 821 " default:" 822 " x = 2.0;" 823 " }" 824 " sk_FragColor = vec4(x);" 825 "}", 826 *SkSL::ShaderCapsFactory::Default(), 827 "#version 400\n" 828 "out vec4 sk_FragColor;\n" 829 "void main() {\n" 830 " float x;\n" 831 " switch (2) {\n" 832 " case 0:\n" 833 " x = 0.0;\n" 834 " case 1:\n" 835 " x = 1.0;\n" 836 " default:\n" 837 " x = 2.0;\n" 838 " }\n" 839 " sk_FragColor = vec4(2.0);\n" 840 "}\n"); 841 test(r, 842 "void main() {" 843 " float x = 0.0;" 844 " switch (3) {" 845 " case 0:" 846 " x = 0.0;" 847 " case 1:" 848 " x = 1.0;" 849 " }" 850 " sk_FragColor = vec4(x);" 851 "}", 852 *SkSL::ShaderCapsFactory::Default(), 853 "#version 400\n" 854 "out vec4 sk_FragColor;\n" 855 "void main() {\n" 856 " float x = 0.0;\n" 857 " switch (3) {\n" 858 " case 0:\n" 859 " x = 0.0;\n" 860 " case 1:\n" 861 " x = 1.0;\n" 862 " }\n" 863 " sk_FragColor = vec4(x);\n" 864 "}\n"); 865} 866 867#endif 868