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 14 // Note that the optimizer will aggressively kill dead code and substitute constants in place of 15 // variables, so we have to jump through a few hoops to ensure that the code in these tests has the 16 // necessary side-effects to remain live. In some cases we rely on the optimizer not (yet) being 17 // smart enough to optimize around certain constructs; as the optimizer gets smarter it will 18 // undoubtedly end up breaking some of these tests. That is a good thing, as long as the new code is 19 // equivalent! 20 21 static void test(skiatest::Reporter* r, const char* src, const SkSL::Program::Settings& settings, 22 const char* expected, SkSL::Program::Inputs* inputs, 23 SkSL::Program::Kind kind = SkSL::Program::kFragment_Kind) { 24 SkSL::Compiler compiler; 25 SkSL::String output; 26 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(kind, SkSL::String(src), 27 settings); 28 if (!program) { 29 SkDebugf("Unexpected error compiling %s\n%s", src, compiler.errorText().c_str()); 30 } 31 REPORTER_ASSERT(r, program); 32 *inputs = program->fInputs; 33 REPORTER_ASSERT(r, compiler.toGLSL(*program, &output)); 34 if (program) { 35 SkSL::String skExpected(expected); 36 if (output != skExpected) { 37 SkDebugf("GLSL MISMATCH:\nsource:\n%s\n\nexpected:\n'%s'\n\nreceived:\n'%s'", src, 38 expected, output.c_str()); 39 } 40 REPORTER_ASSERT(r, output == skExpected); 41 } 42 } 43 44 static void test(skiatest::Reporter* r, const char* src, const GrShaderCaps& caps, 45 const char* expected, SkSL::Program::Kind kind = SkSL::Program::kFragment_Kind) { 46 SkSL::Program::Settings settings; 47 settings.fCaps = ∩︀ 48 SkSL::Program::Inputs inputs; 49 test(r, src, settings, expected, &inputs, kind); 50 } 51 52 DEF_TEST(SkSLHelloWorld, r) { 53 test(r, 54 "void main() { sk_FragColor = float4(0.75); }", 55 *SkSL::ShaderCapsFactory::Default(), 56 "#version 400\n" 57 "out vec4 sk_FragColor;\n" 58 "void main() {\n" 59 " sk_FragColor = vec4(0.75);\n" 60 "}\n"); 61 } 62 63 DEF_TEST(SkSLControl, r) { 64 test(r, 65 "void main() {" 66 "if (sqrt(2) > 5) { sk_FragColor = float4(0.75); } else { discard; }" 67 "int i = 0;" 68 "while (i < 10) { sk_FragColor *= 0.5; i++; }" 69 "do { sk_FragColor += 0.01; } while (sk_FragColor.x < 0.75);" 70 "for (int i = 0; i < 10; i++) {" 71 "if (i % 2 == 1) break; else continue;" 72 "}" 73 "return;" 74 "}", 75 *SkSL::ShaderCapsFactory::Default(), 76 "#version 400\n" 77 "out vec4 sk_FragColor;\n" 78 "void main() {\n" 79 " if (sqrt(2.0) > 5.0) {\n" 80 " sk_FragColor = vec4(0.75);\n" 81 " } else {\n" 82 " discard;\n" 83 " }\n" 84 " int i = 0;\n" 85 " while (i < 10) {\n" 86 " sk_FragColor *= 0.5;\n" 87 " i++;\n" 88 " }\n" 89 " do {\n" 90 " sk_FragColor += 0.01;\n" 91 " } while (sk_FragColor.x < 0.75);\n" 92 " for (int i = 0;i < 10; i++) {\n" 93 " if (i % 2 == 1) break; else continue;\n" 94 " }\n" 95 " return;\n" 96 "}\n"); 97 } 98 99 DEF_TEST(SkSLFunctions, r) { 100 test(r, 101 "float foo(float v[2]) { return v[0] * v[1]; }" 102 "void bar(inout float x) { float y[2], z; y[0] = x; y[1] = x * 2; z = foo(y); x = z; }" 103 "void main() { float x = 10; bar(x); sk_FragColor = float4(x); }", 104 *SkSL::ShaderCapsFactory::Default(), 105 "#version 400\n" 106 "out vec4 sk_FragColor;\n" 107 "float foo(float v[2]) {\n" 108 " return v[0] * v[1];\n" 109 "}\n" 110 "void bar(inout float x) {\n" 111 " float y[2], z;\n" 112 " y[0] = x;\n" 113 " y[1] = x * 2.0;\n" 114 " z = foo(y);\n" 115 " x = z;\n" 116 "}\n" 117 "void main() {\n" 118 " float x = 10.0;\n" 119 " bar(x);\n" 120 " sk_FragColor = vec4(x);\n" 121 "}\n"); 122 } 123 124 DEF_TEST(SkSLOperators, r) { 125 test(r, 126 "void main() {" 127 "float x = 1, y = 2;" 128 "int z = 3;" 129 "x = x - x + y * z * x * (y - z);" 130 "y = x / y / z;" 131 "z = (z / 2 % 3 << 4) >> 2 << 1;" 132 "bool b = (x > 4) == x < 2 || 2 >= sqrt(2) && y <= z;" 133 "x += 12;" 134 "x -= 12;" 135 "x *= y /= z = 10;" 136 "b ||= false;" 137 "b &&= true;" 138 "b ^^= false;" 139 "z |= 0;" 140 "z &= -1;" 141 "z ^= 0;" 142 "z >>= 2;" 143 "z <<= 4;" 144 "z %= 5;" 145 "x = (float2(sqrt(1)) , 6);" 146 "z = (float2(sqrt(1)) , 6);" 147 "}", 148 *SkSL::ShaderCapsFactory::Default(), 149 "#version 400\n" 150 "out vec4 sk_FragColor;\n" 151 "void main() {\n" 152 " float x = 1.0, y = 2.0;\n" 153 " int z = 3;\n" 154 " x = -6.0;\n" 155 " y = -1.0;\n" 156 " z = 8;\n" 157 " bool b = false == true || 2.0 >= sqrt(2.0) && true;\n" 158 " x += 12.0;\n" 159 " x -= 12.0;\n" 160 " x *= (y /= float(z = 10));\n" 161 " b ||= false;\n" 162 " b &&= true;\n" 163 " b ^^= false;\n" 164 " z |= 0;\n" 165 " z &= -1;\n" 166 " z ^= 0;\n" 167 " z >>= 2;\n" 168 " z <<= 4;\n" 169 " z %= 5;\n" 170 " x = float((vec2(sqrt(1.0)) , 6));\n" 171 " z = (vec2(sqrt(1.0)) , 6);\n" 172 "}\n"); 173 } 174 175 DEF_TEST(SkSLMatrices, r) { 176 test(r, 177 "void main() {" 178 "float2x4 x = float2x4(1);" 179 "float3x2 y = float3x2(1, 0, 0, 1, float2(2, 2));" 180 "float3x4 z = x * y;" 181 "float3 v1 = float3x3(1) * float3(2);" 182 "float3 v2 = float3(2) * float3x3(1);" 183 "sk_FragColor = float4(z[0].x, v1 + v2);" 184 "}", 185 *SkSL::ShaderCapsFactory::Default(), 186 "#version 400\n" 187 "out vec4 sk_FragColor;\n" 188 "void main() {\n" 189 " mat3x4 z = mat2x4(1.0) * mat3x2(1.0, 0.0, 0.0, 1.0, vec2(2.0, 2.0));\n" 190 " vec3 v1 = mat3(1.0) * vec3(2.0);\n" 191 " vec3 v2 = vec3(2.0) * mat3(1.0);\n" 192 " sk_FragColor = vec4(z[0].x, v1 + v2);\n" 193 "}\n"); 194 } 195 196 DEF_TEST(SkSLInterfaceBlock, r) { 197 test(r, 198 "uniform testBlock {" 199 "half x;" 200 "half y[2];" 201 "layout(binding=12) half3x2 z;" 202 "bool w;" 203 "};" 204 "void main() {" 205 " sk_FragColor = half4(x, y[0], y[1], 0);" 206 "}", 207 *SkSL::ShaderCapsFactory::Default(), 208 "#version 400\n" 209 "out vec4 sk_FragColor;\n" 210 "uniform testBlock {\n" 211 " float x;\n" 212 " float[2] y;\n" 213 " layout (binding = 12) mat3x2 z;\n" 214 " bool w;\n" 215 "};\n" 216 "void main() {\n" 217 " sk_FragColor = vec4(x, y[0], y[1], 0.0);\n" 218 "}\n"); 219 test(r, 220 "uniform testBlock {" 221 "float x;" 222 "} test;" 223 "void main() {" 224 " sk_FragColor = half4(test.x);" 225 "}", 226 *SkSL::ShaderCapsFactory::Default(), 227 "#version 400\n" 228 "out vec4 sk_FragColor;\n" 229 "uniform testBlock {\n" 230 " float x;\n" 231 "} test;\n" 232 "void main() {\n" 233 " sk_FragColor = vec4(test.x);\n" 234 "}\n"); 235 test(r, 236 "uniform testBlock {" 237 "float x;" 238 "} test[2];" 239 "void main() {" 240 " sk_FragColor = half4(test[1].x);" 241 "}", 242 *SkSL::ShaderCapsFactory::Default(), 243 "#version 400\n" 244 "out vec4 sk_FragColor;\n" 245 "uniform testBlock {\n" 246 " float x;\n" 247 "} test[2];\n" 248 "void main() {\n" 249 " sk_FragColor = vec4(test[1].x);\n" 250 "}\n"); 251 } 252 253 DEF_TEST(SkSLStructs, r) { 254 test(r, 255 "struct A {" 256 "int x;" 257 "int y;" 258 "} a1, a2;" 259 "A a3;" 260 "struct B {" 261 "float x;" 262 "float y[2];" 263 "layout(binding=1) A z;" 264 "};" 265 "B b1, b2, b3;" 266 "void main() {" 267 "}", 268 *SkSL::ShaderCapsFactory::Default(), 269 "#version 400\n" 270 "out vec4 sk_FragColor;\n" 271 "struct A {\n" 272 " int x;\n" 273 " int y;\n" 274 "} a1, a2;\n" 275 "A a3;\n" 276 "struct B {\n" 277 " float x;\n" 278 " float[2] y;\n" 279 " layout (binding = 1) A z;\n" 280 "} b1, b2, b3;\n" 281 "void main() {\n" 282 "}\n"); 283 } 284 285 DEF_TEST(SkSLVersion, r) { 286 test(r, 287 "in float test; void main() { sk_FragColor = float4(0.75); }", 288 *SkSL::ShaderCapsFactory::Version450Core(), 289 "#version 450 core\n" 290 "out vec4 sk_FragColor;\n" 291 "in float test;\n" 292 "void main() {\n" 293 " sk_FragColor = vec4(0.75);\n" 294 "}\n"); 295 test(r, 296 "in float test; void main() { sk_FragColor = float4(0.75); }", 297 *SkSL::ShaderCapsFactory::Version110(), 298 "#version 110\n" 299 "varying float test;\n" 300 "void main() {\n" 301 " gl_FragColor = vec4(0.75);\n" 302 "}\n"); 303 } 304 305 DEF_TEST(SkSLUsesPrecisionModifiers, r) { 306 test(r, 307 "void main() { half x = 0.75; float y = 1; x++; y++;" 308 "sk_FragColor.rg = half2(x, y); }", 309 *SkSL::ShaderCapsFactory::Default(), 310 "#version 400\n" 311 "out vec4 sk_FragColor;\n" 312 "void main() {\n" 313 " float x = 0.75;\n" 314 " float y = 1.0;\n" 315 " x++;\n" 316 " y++;\n" 317 " sk_FragColor.xy = vec2(x, y);\n" 318 "}\n"); 319 test(r, 320 "void main() { half x = 0.75; float y = 1; x++; y++;" 321 "sk_FragColor.rg = half2(x, y); }", 322 *SkSL::ShaderCapsFactory::UsesPrecisionModifiers(), 323 "#version 400\n" 324 "precision mediump float;\n" 325 "out mediump vec4 sk_FragColor;\n" 326 "void main() {\n" 327 " mediump float x = 0.75;\n" 328 " highp float y = 1.0;\n" 329 " x++;\n" 330 " y++;\n" 331 " sk_FragColor.xy = vec2(x, y);\n" 332 "}\n"); 333 } 334 335 DEF_TEST(SkSLMinAbs, r) { 336 test(r, 337 "void main() {" 338 "float x = -5;" 339 "sk_FragColor.r = min(abs(x), 6);" 340 "}", 341 *SkSL::ShaderCapsFactory::Default(), 342 "#version 400\n" 343 "out vec4 sk_FragColor;\n" 344 "void main() {\n" 345 " sk_FragColor.x = min(abs(-5.0), 6.0);\n" 346 "}\n"); 347 348 test(r, 349 "void main() {" 350 "float x = -5.0;" 351 "sk_FragColor.r = min(abs(x), 6.0);" 352 "}", 353 *SkSL::ShaderCapsFactory::CannotUseMinAndAbsTogether(), 354 "#version 400\n" 355 "out vec4 sk_FragColor;\n" 356 "void main() {\n" 357 " float minAbsHackVar0;\n" 358 " float minAbsHackVar1;\n" 359 " sk_FragColor.x = ((minAbsHackVar0 = abs(-5.0)) < (minAbsHackVar1 = 6.0) ? " 360 "minAbsHackVar0 : minAbsHackVar1);\n" 361 "}\n"); 362 } 363 364 DEF_TEST(SkSLFractNegative, r) { 365 static constexpr char input[] = 366 "void main() {" 367 "float x = -42.0;" 368 "sk_FragColor.r = fract(x);" 369 "}"; 370 static constexpr char output_default[] = 371 "#version 400\n" 372 "out vec4 sk_FragColor;\n" 373 "void main() {\n" 374 " sk_FragColor.x = fract(-42.0);\n" 375 "}\n"; 376 static constexpr char output_workaround[] = 377 "#version 400\n" 378 "out vec4 sk_FragColor;\n" 379 "void main() {\n" 380 " sk_FragColor.x = (0.5 - sign(-42.0) * (0.5 - fract(abs(-42.0))));\n" 381 "}\n"; 382 383 test(r, input, *SkSL::ShaderCapsFactory::Default(), output_default); 384 test(r, input, *SkSL::ShaderCapsFactory::CannotUseFractForNegativeValues(), output_workaround); 385 } 386 387 DEF_TEST(SkSLNegatedAtan, r) { 388 test(r, 389 "void main() { float2 x = float2(sqrt(2)); sk_FragColor.r = atan(x.x, -x.y); }", 390 *SkSL::ShaderCapsFactory::Default(), 391 "#version 400\n" 392 "out vec4 sk_FragColor;\n" 393 "void main() {\n" 394 " vec2 x = vec2(sqrt(2.0));\n" 395 " sk_FragColor.x = atan(x.x, -x.y);\n" 396 "}\n"); 397 test(r, 398 "void main() { float2 x = float2(sqrt(2)); sk_FragColor.r = atan(x.x, -x.y); }", 399 *SkSL::ShaderCapsFactory::MustForceNegatedAtanParamToFloat(), 400 "#version 400\n" 401 "out vec4 sk_FragColor;\n" 402 "void main() {\n" 403 " vec2 x = vec2(sqrt(2.0));\n" 404 " sk_FragColor.x = atan(x.x, -1.0 * x.y);\n" 405 "}\n"); 406 } 407 408 DEF_TEST(SkSLModifiersDeclaration, r) { 409 test(r, 410 "layout(blend_support_all_equations) out;" 411 "layout(blend_support_all_equations) out;" 412 "layout(blend_support_multiply) out;" 413 "layout(blend_support_screen) out;" 414 "layout(blend_support_overlay) out;" 415 "layout(blend_support_darken) out;" 416 "layout(blend_support_lighten) out;" 417 "layout(blend_support_colordodge) out;" 418 "layout(blend_support_colorburn) out;" 419 "layout(blend_support_hardlight) out;" 420 "layout(blend_support_softlight) out;" 421 "layout(blend_support_difference) out;" 422 "layout(blend_support_exclusion) out;" 423 "layout(blend_support_hsl_hue) out;" 424 "layout(blend_support_hsl_saturation) out;" 425 "layout(blend_support_hsl_color) out;" 426 "layout(blend_support_hsl_luminosity) out;" 427 "void main() { }", 428 *SkSL::ShaderCapsFactory::Default(), 429 "#version 400\n" 430 "out vec4 sk_FragColor;\n" 431 "layout (blend_support_all_equations) out ;\n" 432 "layout (blend_support_all_equations) out ;\n" 433 "layout (blend_support_multiply) out ;\n" 434 "layout (blend_support_screen) out ;\n" 435 "layout (blend_support_overlay) out ;\n" 436 "layout (blend_support_darken) out ;\n" 437 "layout (blend_support_lighten) out ;\n" 438 "layout (blend_support_colordodge) out ;\n" 439 "layout (blend_support_colorburn) out ;\n" 440 "layout (blend_support_hardlight) out ;\n" 441 "layout (blend_support_softlight) out ;\n" 442 "layout (blend_support_difference) out ;\n" 443 "layout (blend_support_exclusion) out ;\n" 444 "layout (blend_support_hsl_hue) out ;\n" 445 "layout (blend_support_hsl_saturation) out ;\n" 446 "layout (blend_support_hsl_color) out ;\n" 447 "layout (blend_support_hsl_luminosity) out ;\n" 448 "void main() {\n" 449 "}\n"); 450 } 451 452 DEF_TEST(SkSLHex, r) { 453 test(r, 454 "void main() {" 455 "int i1 = 0x0;" 456 "i1++;" 457 "int i2 = 0x1234abcd;" 458 "i2++;" 459 "int i3 = 0x7fffffff;" 460 "i3++;" 461 "int i4 = 0xffffffff;" 462 "i4++;" 463 "int i5 = -0xbeef;" 464 "i5++;" 465 "uint u1 = 0x0;" 466 "u1++;" 467 "uint u2 = 0x1234abcd;" 468 "u2++;" 469 "uint u3 = 0x7fffffff;" 470 "u3++;" 471 "uint u4 = 0xffffffff;" 472 "u4++;" 473 "ushort u5 = 0xffff;" 474 "u5++;" 475 "}", 476 *SkSL::ShaderCapsFactory::Default(), 477 "#version 400\n" 478 "out vec4 sk_FragColor;\n" 479 "void main() {\n" 480 " int i1 = 0;\n" 481 " i1++;\n" 482 " int i2 = 305441741;\n" 483 " i2++;\n" 484 " int i3 = 2147483647;\n" 485 " i3++;\n" 486 " int i4 = -1;\n" 487 " i4++;\n" 488 " int i5 = -48879;\n" 489 " i5++;\n" 490 " uint u1 = 0u;\n" 491 " u1++;\n" 492 " uint u2 = 305441741u;\n" 493 " u2++;\n" 494 " uint u3 = 2147483647u;\n" 495 " u3++;\n" 496 " uint u4 = 4294967295u;\n" 497 " u4++;\n" 498 " uint u5 = 65535u;\n" 499 " u5++;\n" 500 "}\n"); 501 } 502 503 DEF_TEST(SkSLVectorConstructors, r) { 504 test(r, 505 "float2 v1 = float2(1);" 506 "float2 v2 = float2(1, 2);" 507 "float2 v3 = float2(float2(1));" 508 "float3 v4 = float3(float2(1), 1.0);" 509 "int2 v5 = int2(1);" 510 "int2 v6 = int2(float2(1, 2));" 511 "float2 v7 = float2(int2(1, 2));", 512 *SkSL::ShaderCapsFactory::Default(), 513 "#version 400\n" 514 "out vec4 sk_FragColor;\n" 515 "vec2 v1 = vec2(1.0);\n" 516 "vec2 v2 = vec2(1.0, 2.0);\n" 517 "vec2 v3 = vec2(1.0);\n" 518 "vec3 v4 = vec3(vec2(1.0), 1.0);\n" 519 "ivec2 v5 = ivec2(1);\n" 520 "ivec2 v6 = ivec2(vec2(1.0, 2.0));\n" 521 "vec2 v7 = vec2(ivec2(1, 2));\n"); 522 } 523 524 DEF_TEST(SkSLArrayConstructors, r) { 525 test(r, 526 "float test1[] = float[](1, 2, 3, 4);" 527 "float2 test2[] = float2[](float2(1, 2), float2(3, 4));" 528 "float4x4 test3[] = float4x4[]();", 529 *SkSL::ShaderCapsFactory::Default(), 530 "#version 400\n" 531 "out vec4 sk_FragColor;\n" 532 "float test1[] = float[](1.0, 2.0, 3.0, 4.0);\n" 533 "vec2 test2[] = vec2[](vec2(1.0, 2.0), vec2(3.0, 4.0));\n" 534 "mat4 test3[] = mat4[]();\n"); 535 } 536 537 DEF_TEST(SkSLDerivatives, r) { 538 test(r, 539 "void main() { sk_FragColor.r = dFdx(1); }", 540 *SkSL::ShaderCapsFactory::Default(), 541 "#version 400\n" 542 "out vec4 sk_FragColor;\n" 543 "void main() {\n" 544 " sk_FragColor.x = dFdx(1.0);\n" 545 "}\n"); 546 test(r, 547 "void main() { sk_FragColor.r = 1; }", 548 *SkSL::ShaderCapsFactory::ShaderDerivativeExtensionString(), 549 "#version 400\n" 550 "precision mediump float;\n" 551 "out mediump vec4 sk_FragColor;\n" 552 "void main() {\n" 553 " sk_FragColor.x = 1.0;\n" 554 "}\n"); 555 test(r, 556 "void main() { sk_FragColor.r = dFdx(1); }", 557 *SkSL::ShaderCapsFactory::ShaderDerivativeExtensionString(), 558 "#version 400\n" 559 "#extension GL_OES_standard_derivatives : require\n" 560 "precision mediump float;\n" 561 "out mediump vec4 sk_FragColor;\n" 562 "void main() {\n" 563 " sk_FragColor.x = dFdx(1.0);\n" 564 "}\n"); 565 } 566 567 568 DEF_TEST(SkSLIntFolding, r) { 569 test(r, 570 "void main() {" 571 "sk_FragColor.r = 32 + 2;" 572 "sk_FragColor.r = 32 - 2;" 573 "sk_FragColor.r = 32 * 2;" 574 "sk_FragColor.r = 32 / 2;" 575 "sk_FragColor.r = 12 | 6;" 576 "sk_FragColor.r = 254 & 7;" 577 "sk_FragColor.r = 2 ^ 7;" 578 "sk_FragColor.r = 1 << 4;" 579 "sk_FragColor.r = 128 >> 2;" 580 "sk_FragColor.r = -1 == -1 ? 1 : -1;" 581 "sk_FragColor.r = -1 == -2 ? 2 : -2;" 582 "sk_FragColor.r = 0 != 1 ? 3 : -3;" 583 "sk_FragColor.r = 0 != 0 ? 4 : -4;" 584 "sk_FragColor.r = 6 > 5 ? 5 : -5;" 585 "sk_FragColor.r = 6 > 6 ? 6 : -6;" 586 "sk_FragColor.r = -1 < 0 ? 7 : -7;" 587 "sk_FragColor.r = 1 < 0 ? 8 : -8;" 588 "sk_FragColor.r = 6 >= 6 ? 9 : -9;" 589 "sk_FragColor.r = 6 >= 7 ? 10 : -10;" 590 "sk_FragColor.r = 6 <= 6 ? 11 : -11;" 591 "sk_FragColor.r = 6 <= 5 ? 12 : -12;" 592 "sk_FragColor.r = int(sqrt(1)) + 0;" 593 "sk_FragColor.r = 0 + int(sqrt(2));" 594 "sk_FragColor.r = int(sqrt(3)) - 0;" 595 "sk_FragColor.r = int(sqrt(4)) * 0;" 596 "sk_FragColor.r = int(sqrt(5)) * 1;" 597 "sk_FragColor.r = 1 * int(sqrt(6));" 598 "sk_FragColor.r = 0 * int(sqrt(7));" 599 "sk_FragColor.r = int(sqrt(8)) / 1;" 600 "sk_FragColor.r = 0 / int(sqrt(9));" 601 "int x = int(sqrt(2));" 602 "x += 1;" 603 "x += 0;" 604 "x -= 1;" 605 "x -= 0;" 606 "x *= 1;" 607 "x *= 2;" 608 "x /= 1;" 609 "x /= 2;" 610 "sk_FragColor.r = x;" 611 "}", 612 *SkSL::ShaderCapsFactory::Default(), 613 "#version 400\n" 614 "out vec4 sk_FragColor;\n" 615 "void main() {\n" 616 " sk_FragColor.x = 34.0;\n" 617 " sk_FragColor.x = 30.0;\n" 618 " sk_FragColor.x = 64.0;\n" 619 " sk_FragColor.x = 16.0;\n" 620 " sk_FragColor.x = 14.0;\n" 621 " sk_FragColor.x = 6.0;\n" 622 " sk_FragColor.x = 5.0;\n" 623 " sk_FragColor.x = 16.0;\n" 624 " sk_FragColor.x = 32.0;\n" 625 " sk_FragColor.x = 1.0;\n" 626 " sk_FragColor.x = -2.0;\n" 627 " sk_FragColor.x = 3.0;\n" 628 " sk_FragColor.x = -4.0;\n" 629 " sk_FragColor.x = 5.0;\n" 630 " sk_FragColor.x = -6.0;\n" 631 " sk_FragColor.x = 7.0;\n" 632 " sk_FragColor.x = -8.0;\n" 633 " sk_FragColor.x = 9.0;\n" 634 " sk_FragColor.x = -10.0;\n" 635 " sk_FragColor.x = 11.0;\n" 636 " sk_FragColor.x = -12.0;\n" 637 " sk_FragColor.x = float(int(sqrt(1.0)));\n" 638 " sk_FragColor.x = float(int(sqrt(2.0)));\n" 639 " sk_FragColor.x = float(int(sqrt(3.0)));\n" 640 " sk_FragColor.x = 0.0;\n" 641 " sk_FragColor.x = float(int(sqrt(5.0)));\n" 642 " sk_FragColor.x = float(int(sqrt(6.0)));\n" 643 " sk_FragColor.x = 0.0;\n" 644 " sk_FragColor.x = float(int(sqrt(8.0)));\n" 645 " sk_FragColor.x = 0.0;\n" 646 " int x = int(sqrt(2.0));\n" 647 " x += 1;\n" 648 " x -= 1;\n" 649 " x *= 2;\n" 650 " x /= 2;\n" 651 " sk_FragColor.x = float(x);\n" 652 "}\n"); 653 } 654 655 DEF_TEST(SkSLFloatFolding, r) { 656 test(r, 657 "void main() {" 658 "sk_FragColor.r = 32.0 + 2.0;" 659 "sk_FragColor.r = 32.0 - 2.0;" 660 "sk_FragColor.r = 32.0 * 2.0;" 661 "sk_FragColor.r = 32.0 / 2.0;" 662 "sk_FragColor.r = (12 > 2.0) ? (10 * 2 / 5 + 18 - 3) : 0;" 663 "sk_FragColor.r = 0.0 == 0.0 ? 1 : -1;" 664 "sk_FragColor.r = 0.0 == 1.0 ? 2 : -2;" 665 "sk_FragColor.r = 0.0 != 1.0 ? 3 : -3;" 666 "sk_FragColor.r = 0.0 != 0.0 ? 4 : -4;" 667 "sk_FragColor.r = 6.0 > 5.0 ? 5 : -5;" 668 "sk_FragColor.r = 6.0 > 6.0 ? 6 : -6;" 669 "sk_FragColor.r = 6.0 >= 6.0 ? 7 : -7;" 670 "sk_FragColor.r = 6.0 >= 7.0 ? 8 : -8;" 671 "sk_FragColor.r = 5.0 < 6.0 ? 9 : -9;" 672 "sk_FragColor.r = 6.0 < 6.0 ? 10 : -10;" 673 "sk_FragColor.r = 6.0 <= 6.0 ? 11 : -11;" 674 "sk_FragColor.r = 6.0 <= 5.0 ? 12 : -12;" 675 "sk_FragColor.r = sqrt(1) + 0;" 676 "sk_FragColor.r = 0 + sqrt(2);" 677 "sk_FragColor.r = sqrt(3) - 0;" 678 "sk_FragColor.r = sqrt(4) * 0;" 679 "sk_FragColor.r = sqrt(5) * 1;" 680 "sk_FragColor.r = 1 * sqrt(6);" 681 "sk_FragColor.r = 0 * sqrt(7);" 682 "sk_FragColor.r = sqrt(8) / 1;" 683 "sk_FragColor.r = 0 / sqrt(9);" 684 "sk_FragColor.r += 1;" 685 "sk_FragColor.r += 0;" 686 "sk_FragColor.r -= 1;" 687 "sk_FragColor.r -= 0;" 688 "sk_FragColor.r *= 1;" 689 "sk_FragColor.r *= 2;" 690 "sk_FragColor.r /= 1;" 691 "sk_FragColor.r /= 2;" 692 "}", 693 *SkSL::ShaderCapsFactory::Default(), 694 "#version 400\n" 695 "out vec4 sk_FragColor;\n" 696 "void main() {\n" 697 " sk_FragColor.x = 34.0;\n" 698 " sk_FragColor.x = 30.0;\n" 699 " sk_FragColor.x = 64.0;\n" 700 " sk_FragColor.x = 16.0;\n" 701 " sk_FragColor.x = 19.0;\n" 702 " sk_FragColor.x = 1.0;\n" 703 " sk_FragColor.x = -2.0;\n" 704 " sk_FragColor.x = 3.0;\n" 705 " sk_FragColor.x = -4.0;\n" 706 " sk_FragColor.x = 5.0;\n" 707 " sk_FragColor.x = -6.0;\n" 708 " sk_FragColor.x = 7.0;\n" 709 " sk_FragColor.x = -8.0;\n" 710 " sk_FragColor.x = 9.0;\n" 711 " sk_FragColor.x = -10.0;\n" 712 " sk_FragColor.x = 11.0;\n" 713 " sk_FragColor.x = -12.0;\n" 714 " sk_FragColor.x = sqrt(1.0);\n" 715 " sk_FragColor.x = sqrt(2.0);\n" 716 " sk_FragColor.x = sqrt(3.0);\n" 717 " sk_FragColor.x = 0.0;\n" 718 " sk_FragColor.x = sqrt(5.0);\n" 719 " sk_FragColor.x = sqrt(6.0);\n" 720 " sk_FragColor.x = 0.0;\n" 721 " sk_FragColor.x = sqrt(8.0);\n" 722 " sk_FragColor.x = 0.0;\n" 723 " sk_FragColor.x += 1.0;\n" 724 " sk_FragColor.x -= 1.0;\n" 725 " sk_FragColor.x *= 2.0;\n" 726 " sk_FragColor.x /= 2.0;\n" 727 "}\n"); 728 } 729 730 DEF_TEST(SkSLBoolFolding, r) { 731 test(r, 732 "void main() {" 733 "sk_FragColor.r = 1 == 1 || 2 == 8 ? 1 : -1;" 734 "sk_FragColor.r = 1 > 1 || 2 == 8 ? 2 : -2;" 735 "sk_FragColor.r = 1 == 1 && 2 <= 8 ? 3 : -3;" 736 "sk_FragColor.r = 1 == 2 && 2 == 8 ? 4 : -4;" 737 "sk_FragColor.r = 1 == 1 ^^ 1 != 1 ? 5 : -5;" 738 "sk_FragColor.r = 1 == 1 ^^ 1 == 1 ? 6 : -6;" 739 "}", 740 *SkSL::ShaderCapsFactory::Default(), 741 "#version 400\n" 742 "out vec4 sk_FragColor;\n" 743 "void main() {\n" 744 " sk_FragColor.x = 1.0;\n" 745 " sk_FragColor.x = -2.0;\n" 746 " sk_FragColor.x = 3.0;\n" 747 " sk_FragColor.x = -4.0;\n" 748 " sk_FragColor.x = 5.0;\n" 749 " sk_FragColor.x = -6.0;\n" 750 "}\n"); 751 } 752 753 DEF_TEST(SkSLVecFolding, r) { 754 test(r, 755 "void main() {" 756 "sk_FragColor.r = float4(0.5, 1, 1, 1).x;" 757 "sk_FragColor = float4(float2(1), float2(2, 3)) + float4(5, 6, 7, 8);" 758 "sk_FragColor = float4(8, float3(10)) - float4(1);" 759 "sk_FragColor = float4(2) * float4(1, 2, 3, 4);" 760 "sk_FragColor = float4(12) / float4(1, 2, 3, 4);" 761 "sk_FragColor.r = (float4(12) / float4(1, 2, 3, 4)).y;" 762 "sk_FragColor.x = float4(1) == float4(1) ? 1.0 : -1.0;" 763 "sk_FragColor.x = float4(1) == float4(2) ? 2.0 : -2.0;" 764 "sk_FragColor.x = float2(1) == float2(1, 1) ? 3.0 : -3.0;" 765 "sk_FragColor.x = float2(1, 1) == float2(1, 1) ? 4.0 : -4.0;" 766 "sk_FragColor.x = float2(1) == float2(1, 0) ? 5.0 : -5.0;" 767 "sk_FragColor.x = float4(1) == float4(float2(1), float2(1)) ? 6.0 : -6.0;" 768 "sk_FragColor.x = float4(float3(1), 1) == float4(float2(1), float2(1)) ? 7.0 : -7.0;" 769 "sk_FragColor.x = float4(float3(1), 1) == float4(float2(1), 1, 0) ? 8.0 : -8.0;" 770 "sk_FragColor.x = float2(1) != float2(1, 0) ? 9.0 : -9.0;" 771 "sk_FragColor.x = float4(1) != float4(float2(1), float2(1)) ? 10.0 : -10.0;" 772 "sk_FragColor = float4(sqrt(1)) * float4(1);" 773 "sk_FragColor = float4(1) * float4(sqrt(2));" 774 "sk_FragColor = float4(0) * float4(sqrt(3));" 775 "sk_FragColor = float4(sqrt(4)) * float4(0);" 776 "sk_FragColor = float4(0) / float4(sqrt(5));" 777 "sk_FragColor = float4(0) + float4(sqrt(6));" 778 "sk_FragColor = float4(sqrt(7)) + float4(0);" 779 "sk_FragColor = float4(sqrt(8)) - float4(0);" 780 "sk_FragColor = float4(0) + sqrt(9);" 781 "sk_FragColor = float4(0) * sqrt(10);" 782 "sk_FragColor = float4(0) / sqrt(11);" 783 "sk_FragColor = float4(1) * sqrt(12);" 784 "sk_FragColor = 0 + float4(sqrt(13));" 785 "sk_FragColor = 0 * float4(sqrt(14));" 786 "sk_FragColor = 0 / float4(sqrt(15));" 787 "sk_FragColor = 1 * float4(sqrt(16));" 788 "sk_FragColor = float4(sqrt(17)) + 0;" 789 "sk_FragColor = float4(sqrt(18)) * 0;" 790 "sk_FragColor = float4(sqrt(19)) * 1;" 791 "sk_FragColor = float4(sqrt(19.5)) - 0;" 792 "sk_FragColor = sqrt(20) * float4(1);" 793 "sk_FragColor = sqrt(21) + float4(0);" 794 "sk_FragColor = sqrt(22) - float4(0);" 795 "sk_FragColor = sqrt(23) / float4(1);" 796 "sk_FragColor = float4(sqrt(24)) / 1;" 797 "sk_FragColor += float4(1);" 798 "sk_FragColor += float4(0);" 799 "sk_FragColor -= float4(1);" 800 "sk_FragColor -= float4(0);" 801 "sk_FragColor *= float4(1);" 802 "sk_FragColor *= float4(2);" 803 "sk_FragColor /= float4(1);" 804 "sk_FragColor /= float4(2);" 805 "}", 806 *SkSL::ShaderCapsFactory::Default(), 807 "#version 400\n" 808 "out vec4 sk_FragColor;\n" 809 "void main() {\n" 810 " sk_FragColor.x = 0.5;\n" 811 " sk_FragColor = vec4(6.0, 7.0, 9.0, 11.0);\n" 812 " sk_FragColor = vec4(7.0, 9.0, 9.0, 9.0);\n" 813 " sk_FragColor = vec4(2.0, 4.0, 6.0, 8.0);\n" 814 " sk_FragColor = vec4(12.0, 6.0, 4.0, 3.0);\n" 815 " sk_FragColor.x = 6.0;\n" 816 " sk_FragColor.x = 1.0;\n" 817 " sk_FragColor.x = -2.0;\n" 818 " sk_FragColor.x = 3.0;\n" 819 " sk_FragColor.x = 4.0;\n" 820 " sk_FragColor.x = -5.0;\n" 821 " sk_FragColor.x = 6.0;\n" 822 " sk_FragColor.x = 7.0;\n" 823 " sk_FragColor.x = -8.0;\n" 824 " sk_FragColor.x = 9.0;\n" 825 " sk_FragColor.x = -10.0;\n" 826 " sk_FragColor = vec4(sqrt(1.0));\n" 827 " sk_FragColor = vec4(sqrt(2.0));\n" 828 " sk_FragColor = vec4(0.0);\n" 829 " sk_FragColor = vec4(0.0);\n" 830 " sk_FragColor = vec4(0.0);\n" 831 " sk_FragColor = vec4(sqrt(6.0));\n" 832 " sk_FragColor = vec4(sqrt(7.0));\n" 833 " sk_FragColor = vec4(sqrt(8.0));\n" 834 " sk_FragColor = vec4(sqrt(9.0));\n" 835 " sk_FragColor = vec4(0.0);\n" 836 " sk_FragColor = vec4(0.0);\n" 837 " sk_FragColor = vec4(sqrt(12.0));\n" 838 " sk_FragColor = vec4(sqrt(13.0));\n" 839 " sk_FragColor = vec4(0.0);\n" 840 " sk_FragColor = vec4(0.0);\n" 841 " sk_FragColor = vec4(sqrt(16.0));\n" 842 " sk_FragColor = vec4(sqrt(17.0));\n" 843 " sk_FragColor = vec4(0.0);\n" 844 " sk_FragColor = vec4(sqrt(19.0));\n" 845 " sk_FragColor = vec4(sqrt(19.5));\n" 846 " sk_FragColor = vec4(sqrt(20.0));\n" 847 " sk_FragColor = vec4(sqrt(21.0));\n" 848 " sk_FragColor = vec4(sqrt(22.0));\n" 849 " sk_FragColor = vec4(sqrt(23.0));\n" 850 " sk_FragColor = vec4(sqrt(24.0));\n" 851 " sk_FragColor += vec4(1.0);\n" 852 " sk_FragColor -= vec4(1.0);\n" 853 " sk_FragColor *= vec4(2.0);\n" 854 " sk_FragColor /= vec4(2.0);\n" 855 "}\n"); 856 } 857 858 DEF_TEST(SkSLMatFolding, r) { 859 test(r, 860 "void main() {" 861 "sk_FragColor.x = float2x2(float2(1.0, 0.0), float2(0.0, 1.0)) == " 862 "float2x2(float2(1.0, 0.0), float2(0.0, 1.0)) ? 1 : -1;" 863 "sk_FragColor.x = float2x2(float2(1.0, 0.0), float2(1.0, 1.0)) == " 864 "float2x2(float2(1.0, 0.0), float2(0.0, 1.0)) ? 2 : -2;" 865 "sk_FragColor.x = float2x2(1) == float2x2(1) ? 3 : -3;" 866 "sk_FragColor.x = float2x2(1) == float2x2(0) ? 4 : -4;" 867 "sk_FragColor.x = float2x2(1) == float2x2(float2(1.0, 0.0), float2(0.0, 1.0)) ? 5 : -5;" 868 "sk_FragColor.x = float2x2(2) == float2x2(float2(1.0, 0.0), float2(0.0, 1.0)) ? 6 : -6;" 869 "sk_FragColor.x = float3x2(2) == float3x2(float2(2.0, 0.0), float2(0.0, 2.0), float2(0.0))" 870 "? 7 : -7;" 871 "sk_FragColor.x = float2x2(1) != float2x2(1) ? 8 : -8;" 872 "sk_FragColor.x = float2x2(1) != float2x2(0) ? 9 : -9;" 873 "sk_FragColor.x = float3x3(float3(1.0, 0.0, 0.0), float3(0.0, 1.0, 0.0), " 874 "float3(0.0, 0.0, 0.0)) == float3x3(float2x2(1.0)) ? 10 : -10;" 875 "sk_FragColor.x = float2x2(float3x3(1.0)) == float2x2(1.0) ? 11 : -11;" 876 "sk_FragColor.x = float2x2(float4(1.0, 0.0, 0.0, 1.0)) == float2x2(1.0) ? 12 : -12;" 877 "sk_FragColor.x = float2x2(1.0, 0.0, float2(0.0, 1.0)) == float2x2(1.0) ? 13 : -13;" 878 "sk_FragColor.x = float2x2(float2(1.0, 0.0), 0.0, 1.0) == float2x2(1.0) ? 14 : -14;" 879 "}", 880 *SkSL::ShaderCapsFactory::Default(), 881 "#version 400\n" 882 "out vec4 sk_FragColor;\n" 883 "void main() {\n" 884 " sk_FragColor.x = 1.0;\n" 885 " sk_FragColor.x = -2.0;\n" 886 " sk_FragColor.x = 3.0;\n" 887 " sk_FragColor.x = -4.0;\n" 888 " sk_FragColor.x = 5.0;\n" 889 " sk_FragColor.x = -6.0;\n" 890 " sk_FragColor.x = 7.0;\n" 891 " sk_FragColor.x = -8.0;\n" 892 " sk_FragColor.x = 9.0;\n" 893 " sk_FragColor.x = 10.0;\n" 894 " sk_FragColor.x = 11.0;\n" 895 " sk_FragColor.x = 12.0;\n" 896 " sk_FragColor.x = 13.0;\n" 897 " sk_FragColor.x = 14.0;\n" 898 "}\n"); 899 } 900 901 DEF_TEST(SkSLConstantIf, r) { 902 test(r, 903 "void main() {" 904 "int x;" 905 "if (true) x = 1;" 906 "if (2 > 1) x = 2; else x = 3;" 907 "if (1 > 2) x = 4; else x = 5;" 908 "if (false) x = 6;" 909 "sk_FragColor.r = x;" 910 "}", 911 *SkSL::ShaderCapsFactory::Default(), 912 "#version 400\n" 913 "out vec4 sk_FragColor;\n" 914 "void main() {\n" 915 " sk_FragColor.x = 5.0;\n" 916 "}\n"); 917 } 918 919 DEF_TEST(SkSLCaps, r) { 920 test(r, 921 "void main() {" 922 "int x = 0;" 923 "int y = 0;" 924 "int z = 0;" 925 "int w = 0;" 926 "if (sk_Caps.externalTextureSupport) x = 1;" 927 "if (sk_Caps.fbFetchSupport) y = 1;" 928 "if (sk_Caps.dropsTileOnZeroDivide && sk_Caps.texelFetchSupport) z = 1;" 929 "if (sk_Caps.dropsTileOnZeroDivide && sk_Caps.canUseAnyFunctionInShader) w = 1;" 930 "sk_FragColor = half4(x, y, z, w);" 931 "}", 932 *SkSL::ShaderCapsFactory::VariousCaps(), 933 "#version 400\n" 934 "out vec4 sk_FragColor;\n" 935 "void main() {\n" 936 " sk_FragColor = vec4(1.0, 0.0, 1.0, 0.0);\n" 937 "}\n"); 938 } 939 940 DEF_TEST(SkSLTexture, r) { 941 test(r, 942 "uniform sampler1D one;" 943 "uniform sampler2D two;" 944 "void main() {" 945 "float4 a = texture(one, 0);" 946 "float4 b = texture(two, float2(0));" 947 "float4 c = texture(one, float2(0));" 948 "float4 d = texture(two, float3(0));" 949 "sk_FragColor = half4(a.x, b.x, c.x, d.x);" 950 "}", 951 *SkSL::ShaderCapsFactory::Default(), 952 "#version 400\n" 953 "out vec4 sk_FragColor;\n" 954 "uniform sampler1D one;\n" 955 "uniform sampler2D two;\n" 956 "void main() {\n" 957 " vec4 a = texture(one, 0.0);\n" 958 " vec4 b = texture(two, vec2(0.0));\n" 959 " vec4 c = textureProj(one, vec2(0.0));\n" 960 " vec4 d = textureProj(two, vec3(0.0));\n" 961 " sk_FragColor = vec4(a.x, b.x, c.x, d.x);\n" 962 "}\n"); 963 test(r, 964 "uniform sampler1D one;" 965 "uniform sampler2D two;" 966 "void main() {" 967 "float4 a = texture(one, 0);" 968 "float4 b = texture(two, float2(0));" 969 "float4 c = texture(one, float2(0));" 970 "float4 d = texture(two, float3(0));" 971 "sk_FragColor = half4(a.x, b.x, c.x, d.x);" 972 "}", 973 *SkSL::ShaderCapsFactory::Version110(), 974 "#version 110\n" 975 "uniform sampler1D one;\n" 976 "uniform sampler2D two;\n" 977 "void main() {\n" 978 " vec4 a = texture1D(one, 0.0);\n" 979 " vec4 b = texture2D(two, vec2(0.0));\n" 980 " vec4 c = texture1DProj(one, vec2(0.0));\n" 981 " vec4 d = texture2DProj(two, vec3(0.0));\n" 982 " gl_FragColor = vec4(a.x, b.x, c.x, d.x);\n" 983 "}\n"); 984 } 985 986 DEF_TEST(SkSLOffset, r) { 987 test(r, 988 "struct Test {" 989 "layout(offset = 0) int x;" 990 "layout(offset = 4) int y;" 991 "int z;" 992 "} test;", 993 *SkSL::ShaderCapsFactory::Default(), 994 "#version 400\n" 995 "out vec4 sk_FragColor;\n" 996 "struct Test {\n" 997 " layout (offset = 0) int x;\n" 998 " layout (offset = 4) int y;\n" 999 " int z;\n" 1000 "} test;\n"); 1001 } 1002 1003 DEF_TEST(SkSLFragCoord, r) { 1004 SkSL::Program::Settings settings; 1005 settings.fFlipY = true; 1006 sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::FragCoordsOld(); 1007 settings.fCaps = caps.get(); 1008 SkSL::Program::Inputs inputs; 1009 test(r, 1010 "void main() { sk_FragColor.xy = sk_FragCoord.xy; }", 1011 settings, 1012 "#version 110\n" 1013 "#extension GL_ARB_fragment_coord_conventions : require\n" 1014 "layout(origin_upper_left) in vec4 gl_FragCoord;\n" 1015 "void main() {\n" 1016 " gl_FragColor.xy = gl_FragCoord.xy;\n" 1017 "}\n", 1018 &inputs); 1019 REPORTER_ASSERT(r, !inputs.fRTHeight); 1020 1021 caps = SkSL::ShaderCapsFactory::FragCoordsNew(); 1022 settings.fCaps = caps.get(); 1023 test(r, 1024 "void main() { sk_FragColor.xy = sk_FragCoord.xy; }", 1025 settings, 1026 "#version 400\n" 1027 "layout(origin_upper_left) in vec4 gl_FragCoord;\n" 1028 "out vec4 sk_FragColor;\n" 1029 "void main() {\n" 1030 " sk_FragColor.xy = gl_FragCoord.xy;\n" 1031 "}\n", 1032 &inputs); 1033 REPORTER_ASSERT(r, !inputs.fRTHeight); 1034 1035 caps = SkSL::ShaderCapsFactory::Default(); 1036 settings.fCaps = caps.get(); 1037 test(r, 1038 "void main() { sk_FragColor.xy = sk_FragCoord.xy; }", 1039 settings, 1040 "#version 400\n" 1041 "uniform float u_skRTHeight;\n" 1042 "out vec4 sk_FragColor;\n" 1043 "void main() {\n" 1044 " vec2 _sktmpCoord = gl_FragCoord.xy;\n" 1045 " vec4 sk_FragCoord = vec4(_sktmpCoord.x, u_skRTHeight - _sktmpCoord.y, 1.0, 1.0);\n" 1046 " sk_FragColor.xy = sk_FragCoord.xy;\n" 1047 "}\n", 1048 &inputs); 1049 REPORTER_ASSERT(r, inputs.fRTHeight); 1050 1051 settings.fFlipY = false; 1052 test(r, 1053 "void main() { sk_FragColor.xy = sk_FragCoord.xy; }", 1054 settings, 1055 "#version 400\n" 1056 "out vec4 sk_FragColor;\n" 1057 "void main() {\n" 1058 " sk_FragColor.xy = gl_FragCoord.xy;\n" 1059 "}\n", 1060 &inputs); 1061 REPORTER_ASSERT(r, !inputs.fRTHeight); 1062 1063 test(r, 1064 "in float4 pos; void main() { sk_Position = pos; }", 1065 *SkSL::ShaderCapsFactory::CannotUseFragCoord(), 1066 "#version 400\n" 1067 "out vec4 sk_FragCoord_Workaround;\n" 1068 "in vec4 pos;\n" 1069 "void main() {\n" 1070 " sk_FragCoord_Workaround = (gl_Position = pos);\n" 1071 "}\n", 1072 SkSL::Program::kVertex_Kind); 1073 1074 test(r, 1075 "in uniform float4 sk_RTAdjust; in float4 pos; void main() { sk_Position = pos; }", 1076 *SkSL::ShaderCapsFactory::CannotUseFragCoord(), 1077 "#version 400\n" 1078 "out vec4 sk_FragCoord_Workaround;\n" 1079 "in uniform vec4 sk_RTAdjust;\n" 1080 "in vec4 pos;\n" 1081 "void main() {\n" 1082 " sk_FragCoord_Workaround = (gl_Position = pos);\n" 1083 " gl_Position = vec4(gl_Position.x * sk_RTAdjust.x + gl_Position.w * sk_RTAdjust.y, " 1084 "gl_Position.y * sk_RTAdjust.z + gl_Position.w * sk_RTAdjust.w, 0.0, " 1085 "gl_Position.w);\n" 1086 "}\n", 1087 SkSL::Program::kVertex_Kind); 1088 1089 test(r, 1090 "void main() { sk_FragColor.xy = sk_FragCoord.xy; }", 1091 *SkSL::ShaderCapsFactory::CannotUseFragCoord(), 1092 "#version 400\n" 1093 "in vec4 sk_FragCoord_Workaround;\n" 1094 "out vec4 sk_FragColor;\n" 1095 "void main() {\n" 1096 " float sk_FragCoord_InvW = 1. / sk_FragCoord_Workaround.w;\n" 1097 " vec4 sk_FragCoord_Resolved = vec4(sk_FragCoord_Workaround.xyz * " 1098 "sk_FragCoord_InvW, sk_FragCoord_InvW);\n" 1099 " sk_FragCoord_Resolved.xy = floor(sk_FragCoord_Resolved.xy) + vec2(.5);\n" 1100 " sk_FragColor.xy = sk_FragCoord_Resolved.xy;\n" 1101 "}\n"); 1102 } 1103 1104 DEF_TEST(SkSLVertexID, r) { 1105 test(r, 1106 "out int id; void main() { id = sk_VertexID; }", 1107 *SkSL::ShaderCapsFactory::Default(), 1108 "#version 400\n" 1109 "out int id;\n" 1110 "void main() {\n" 1111 " id = gl_VertexID;\n" 1112 "}\n", 1113 SkSL::Program::kVertex_Kind); 1114 } 1115 1116 DEF_TEST(SkSLInstanceID, r) { 1117 test(r, 1118 "out int id; void main() { id = sk_InstanceID; }", 1119 *SkSL::ShaderCapsFactory::Default(), 1120 "#version 400\n" 1121 "out int id;\n" 1122 "void main() {\n" 1123 " id = gl_InstanceID;\n" 1124 "}\n", 1125 SkSL::Program::kVertex_Kind); 1126 } 1127 1128 DEF_TEST(SkSLClipDistance, r) { 1129 test(r, 1130 "void main() { sk_ClipDistance[0] = 0; }", 1131 *SkSL::ShaderCapsFactory::Default(), 1132 "#version 400\n" 1133 "void main() {\n" 1134 " gl_ClipDistance[0] = 0.0;\n" 1135 "}\n", 1136 SkSL::Program::kVertex_Kind); 1137 test(r, 1138 "void main() { sk_FragColor = half4(sk_ClipDistance[0]); }", 1139 *SkSL::ShaderCapsFactory::Default(), 1140 "#version 400\n" 1141 "out vec4 sk_FragColor;\n" 1142 "void main() {\n" 1143 " sk_FragColor = vec4(gl_ClipDistance[0]);\n" 1144 "}\n"); 1145 } 1146 1147 DEF_TEST(SkSLArrayTypes, r) { 1148 test(r, 1149 "void main() { float2 x[2] = float2[2](float2(1), float2(2));" 1150 "float2[2] y = float2[2](float2(3), float2(4));" 1151 "sk_FragColor = float4(x[0], y[1]); }", 1152 *SkSL::ShaderCapsFactory::Default(), 1153 "#version 400\n" 1154 "out vec4 sk_FragColor;\n" 1155 "void main() {\n" 1156 " sk_FragColor = vec4(vec2[2](vec2(1.0), vec2(2.0))[0], " 1157 "vec2[2](vec2(3.0), vec2(4.0))[1]);\n" 1158 "}\n"); 1159 } 1160 1161 DEF_TEST(SkSLArrayIndexTypes, r) { 1162 test(r, 1163 "void main() { float array[4] = float[4](1, 2, 3, 4);" 1164 "short x = 0; ushort y = 1; int z = 2; uint w = 3;" 1165 "sk_FragColor = float4(array[x], array[y], array[z], array[w]); }", 1166 *SkSL::ShaderCapsFactory::Default(), 1167 "#version 400\n" 1168 "out vec4 sk_FragColor;\n" 1169 "void main() {\n" 1170 " sk_FragColor = vec4(float[4](1.0, 2.0, 3.0, 4.0)[0], " 1171 "float[4](1.0, 2.0, 3.0, 4.0)[1], " 1172 "float[4](1.0, 2.0, 3.0, 4.0)[2], " 1173 "float[4](1.0, 2.0, 3.0, 4.0)[3]);\n" 1174 "}\n"); 1175 } 1176 1177 DEF_TEST(SkSLGeometry, r) { 1178 test(r, 1179 "layout(points) in;" 1180 "layout(invocations = 2) in;" 1181 "layout(line_strip, max_vertices = 2) out;" 1182 "void main() {" 1183 "sk_Position = sk_in[0].sk_Position + float4(-0.5, 0, 0, sk_InvocationID);" 1184 "EmitVertex();" 1185 "sk_Position = sk_in[0].sk_Position + float4(0.5, 0, 0, sk_InvocationID);" 1186 "EmitVertex();" 1187 "EndPrimitive();" 1188 "}", 1189 *SkSL::ShaderCapsFactory::GeometryShaderSupport(), 1190 "#version 400\n" 1191 "layout (points) in ;\n" 1192 "layout (invocations = 2) in ;\n" 1193 "layout (line_strip, max_vertices = 2) out ;\n" 1194 "void main() {\n" 1195 " gl_Position = gl_in[0].gl_Position + vec4(-0.5, 0.0, 0.0, float(gl_InvocationID));\n" 1196 " EmitVertex();\n" 1197 " gl_Position = gl_in[0].gl_Position + vec4(0.5, 0.0, 0.0, float(gl_InvocationID));\n" 1198 " EmitVertex();\n" 1199 " EndPrimitive();\n" 1200 "}\n", 1201 SkSL::Program::kGeometry_Kind); 1202 } 1203 1204 DEF_TEST(SkSLSwitch, r) { 1205 // basic "does a switch even work" test 1206 test(r, 1207 "void main() {" 1208 " float x;" 1209 " switch (int(sqrt(1))) {" 1210 " case 0:" 1211 " x = 0.0;" 1212 " break;" 1213 " case 1:" 1214 " x = 1.0;" 1215 " break;" 1216 " default:" 1217 " x = 2.0;" 1218 " }" 1219 " sk_FragColor = float4(x);" 1220 "}", 1221 *SkSL::ShaderCapsFactory::Default(), 1222 "#version 400\n" 1223 "out vec4 sk_FragColor;\n" 1224 "void main() {\n" 1225 " float x;\n" 1226 " switch (int(sqrt(1.0))) {\n" 1227 " case 0:\n" 1228 " x = 0.0;\n" 1229 " break;\n" 1230 " case 1:\n" 1231 " x = 1.0;\n" 1232 " break;\n" 1233 " default:\n" 1234 " x = 2.0;\n" 1235 " }\n" 1236 " sk_FragColor = vec4(x);\n" 1237 "}\n"); 1238 // dead code inside of switch 1239 test(r, 1240 "void main() {" 1241 " float x;" 1242 " switch (int(sqrt(2))) {" 1243 " case 0:" 1244 " x = 0.0;" 1245 " case 1:" 1246 " x = 1.0;" 1247 " default:" 1248 " x = 2.0;" 1249 " }" 1250 " sk_FragColor = half4(x);" 1251 "}", 1252 *SkSL::ShaderCapsFactory::Default(), 1253 "#version 400\n" 1254 "out vec4 sk_FragColor;\n" 1255 "void main() {\n" 1256 " switch (int(sqrt(2.0))) {\n" 1257 " case 0:\n" 1258 " ;\n" 1259 " case 1:\n" 1260 " ;\n" 1261 " default:\n" 1262 " ;\n" 1263 " }\n" 1264 " sk_FragColor = vec4(2.0);\n" 1265 "}\n"); 1266 // non-static test w/ fallthrough 1267 test(r, 1268 "void main() {" 1269 " float x = 0.0;" 1270 " switch (int(sqrt(3))) {" 1271 " case 0:" 1272 " x = 0.0;" 1273 " case 1:" 1274 " x = 1.0;" 1275 " }" 1276 " sk_FragColor = half4(x);" 1277 "}", 1278 *SkSL::ShaderCapsFactory::Default(), 1279 "#version 400\n" 1280 "out vec4 sk_FragColor;\n" 1281 "void main() {\n" 1282 " float x = 0.0;\n" 1283 " switch (int(sqrt(3.0))) {\n" 1284 " case 0:\n" 1285 " x = 0.0;\n" 1286 " case 1:\n" 1287 " x = 1.0;\n" 1288 " }\n" 1289 " sk_FragColor = vec4(x);\n" 1290 "}\n"); 1291 // static test w/ fallthrough 1292 test(r, 1293 "void main() {" 1294 " float x = 0.0;" 1295 " switch (0) {" 1296 " case 0:" 1297 " x = 0.0;" 1298 " case 1:" 1299 " x = 1.0;" 1300 " }" 1301 " sk_FragColor = half4(x);" 1302 "}", 1303 *SkSL::ShaderCapsFactory::Default(), 1304 "#version 400\n" 1305 "out vec4 sk_FragColor;\n" 1306 "void main() {\n" 1307 " sk_FragColor = vec4(1.0);\n" 1308 "}\n"); 1309 // static test w/ fallthrough, different entry point 1310 test(r, 1311 "void main() {" 1312 " float x = 0.0;" 1313 " switch (1) {" 1314 " case 0:" 1315 " x = 0.0;" 1316 " case 1:" 1317 " x = 1.0;" 1318 " }" 1319 " sk_FragColor = half4(x);" 1320 "}", 1321 *SkSL::ShaderCapsFactory::Default(), 1322 "#version 400\n" 1323 "out vec4 sk_FragColor;\n" 1324 "void main() {\n" 1325 " sk_FragColor = vec4(1.0);\n" 1326 "}\n"); 1327 // static test w/ break 1328 test(r, 1329 "void main() {" 1330 " float x = 0.0;" 1331 " switch (0) {" 1332 " case 0:" 1333 " x = 0.0;" 1334 " break;" 1335 " case 1:" 1336 " x = 1.0;" 1337 " }" 1338 " sk_FragColor = half4(x);" 1339 "}", 1340 *SkSL::ShaderCapsFactory::Default(), 1341 "#version 400\n" 1342 "out vec4 sk_FragColor;\n" 1343 "void main() {\n" 1344 " sk_FragColor = vec4(0.0);\n" 1345 "}\n"); 1346 // static test w/ static conditional break 1347 test(r, 1348 "void main() {" 1349 " float x = 0.0;" 1350 " switch (0) {" 1351 " case 0:" 1352 " x = 0.0;" 1353 " if (x < 1) break;" 1354 " case 1:" 1355 " x = 1.0;" 1356 " }" 1357 " sk_FragColor = half4(x);" 1358 "}", 1359 *SkSL::ShaderCapsFactory::Default(), 1360 "#version 400\n" 1361 "out vec4 sk_FragColor;\n" 1362 "void main() {\n" 1363 " sk_FragColor = vec4(0.0);\n" 1364 "}\n"); 1365 // static test w/ non-static conditional break 1366 test(r, 1367 "void main() {" 1368 " float x = 0.0;" 1369 " switch (0) {" 1370 " case 0:" 1371 " x = 0.0;" 1372 " if (x < sqrt(1)) break;" 1373 " case 1:" 1374 " x = 1.0;" 1375 " }" 1376 " sk_FragColor = half4(x);" 1377 "}", 1378 *SkSL::ShaderCapsFactory::Default(), 1379 "#version 400\n" 1380 "out vec4 sk_FragColor;\n" 1381 "void main() {\n" 1382 " float x = 0.0;\n" 1383 " switch (0) {\n" 1384 " case 0:\n" 1385 " x = 0.0;\n" 1386 " if (0.0 < sqrt(1.0)) break;\n" 1387 " case 1:\n" 1388 " x = 1.0;\n" 1389 " }\n" 1390 " sk_FragColor = vec4(x);\n" 1391 "}\n"); 1392 } 1393 1394 DEF_TEST(SkSLRectangleTexture, r) { 1395 test(r, 1396 "uniform sampler2D test;" 1397 "void main() {" 1398 " sk_FragColor = texture(test, float2(0.5));" 1399 "}", 1400 *SkSL::ShaderCapsFactory::Default(), 1401 "#version 400\n" 1402 "out vec4 sk_FragColor;\n" 1403 "uniform sampler2D test;\n" 1404 "void main() {\n" 1405 " sk_FragColor = texture(test, vec2(0.5));\n" 1406 "}\n"); 1407 test(r, 1408 "uniform sampler2DRect test;" 1409 "void main() {" 1410 " sk_FragColor = texture(test, float2(0.5));" 1411 "}", 1412 *SkSL::ShaderCapsFactory::Default(), 1413 "#version 400\n" 1414 "out vec4 sk_FragColor;\n" 1415 "uniform sampler2DRect test;\n" 1416 "void main() {\n" 1417 " sk_FragColor = texture(test, textureSize(test) * vec2(0.5));\n" 1418 "}\n"); 1419 test(r, 1420 "uniform sampler2DRect test;" 1421 "void main() {" 1422 " sk_FragColor = texture(test, float3(0.5));" 1423 "}", 1424 *SkSL::ShaderCapsFactory::Default(), 1425 "#version 400\n" 1426 "out vec4 sk_FragColor;\n" 1427 "uniform sampler2DRect test;\n" 1428 "void main() {\n" 1429 " sk_FragColor = texture(test, vec3(textureSize(test), 1.0) * vec3(0.5));\n" 1430 "}\n"); 1431 } 1432 1433 DEF_TEST(SkSLUnusedVars, r) { 1434 test(r, 1435 "void main() {" 1436 "float a = 1, b = 2, c = 3;" 1437 "float d = c;" 1438 "float e = d;" 1439 "b++;" 1440 "d++;" 1441 "sk_FragColor = float4(b, b, d, d);" 1442 "}", 1443 *SkSL::ShaderCapsFactory::Default(), 1444 "#version 400\n" 1445 "out vec4 sk_FragColor;\n" 1446 "void main() {\n" 1447 " float b = 2.0;\n" 1448 " float d = 3.0;\n" 1449 " b++;\n" 1450 " d++;\n" 1451 " sk_FragColor = vec4(b, b, d, d);\n" 1452 "}\n"); 1453 } 1454 1455 DEF_TEST(SkSLMultipleAssignments, r) { 1456 test(r, 1457 "void main() {" 1458 "float x;" 1459 "float y;" 1460 "int z;" 1461 "x = y = z = 1;" 1462 "sk_FragColor = float4(z);" 1463 "}", 1464 *SkSL::ShaderCapsFactory::Default(), 1465 "#version 400\n" 1466 "out vec4 sk_FragColor;\n" 1467 "void main() {\n" 1468 " sk_FragColor = vec4(1.0);\n" 1469 "}\n"); 1470 } 1471 1472 DEF_TEST(SkSLComplexDelete, r) { 1473 test(r, 1474 "uniform float4x4 colorXform;" 1475 "uniform sampler2D sampler;" 1476 "void main() {" 1477 "float4 tmpColor;" 1478 "sk_FragColor = float4(1.0) * (tmpColor = texture(sampler, float2(1)) , " 1479 "colorXform != float4x4(1.0) ? float4(clamp((float4x4(colorXform) * " 1480 "float4(tmpColor.xyz, 1.0)).xyz, " 1481 "0.0, tmpColor.w), tmpColor.w) : tmpColor);" 1482 "}", 1483 *SkSL::ShaderCapsFactory::Default(), 1484 "#version 400\n" 1485 "out vec4 sk_FragColor;\n" 1486 "uniform mat4 colorXform;\n" 1487 "uniform sampler2D sampler;\n" 1488 "void main() {\n" 1489 " vec4 tmpColor;\n" 1490 " sk_FragColor = (tmpColor = texture(sampler, vec2(1.0)) , colorXform != mat4(1.0) ? " 1491 "vec4(clamp((colorXform * vec4(tmpColor.xyz, 1.0)).xyz, 0.0, tmpColor.w), tmpColor.w) : " 1492 "tmpColor);\n" 1493 "}\n"); 1494 } 1495 1496 DEF_TEST(SkSLDependentInitializers, r) { 1497 test(r, 1498 "void main() {" 1499 "float x = 0.5, y = x * 2;" 1500 "sk_FragColor = float4(y);" 1501 "}", 1502 *SkSL::ShaderCapsFactory::Default(), 1503 "#version 400\n" 1504 "out vec4 sk_FragColor;\n" 1505 "void main() {\n" 1506 " sk_FragColor = vec4(1.0);\n" 1507 "}\n"); 1508 } 1509 1510 DEF_TEST(SkSLDeadLoopVar, r) { 1511 test(r, 1512 "void main() {" 1513 "for (int x = 0; x < 4; ) {" 1514 "break;" 1515 "}" 1516 "}", 1517 *SkSL::ShaderCapsFactory::Default(), 1518 "#version 400\n" 1519 "out vec4 sk_FragColor;\n" 1520 "void main() {\n" 1521 " for (; true; ) {\n" 1522 " break;\n" 1523 " }\n" 1524 "}\n" 1525 ); 1526 } 1527 1528 DEF_TEST(SkSLGeometryShaders, r) { 1529 test(r, 1530 "layout(points) in;" 1531 "layout(invocations = 2) in;" 1532 "layout(line_strip, max_vertices = 2) out;" 1533 "void test() {" 1534 "sk_Position = sk_in[0].sk_Position + float4(0.5, 0, 0, sk_InvocationID);" 1535 "EmitVertex();" 1536 "}" 1537 "void main() {" 1538 "sk_Position = sk_in[0].sk_Position + float4(-0.5, 0, 0, sk_InvocationID);" 1539 "EmitVertex();" 1540 "}", 1541 *SkSL::ShaderCapsFactory::NoGSInvocationsSupport(), 1542 "#version 400\n" 1543 "int sk_InvocationID;\n" 1544 "layout (points) in ;\n" 1545 "layout (line_strip, max_vertices = 4) out ;\n" 1546 "void test() {\n" 1547 " gl_Position = gl_in[0].gl_Position + vec4(0.5, 0.0, 0.0, float(sk_InvocationID));\n" 1548 " EmitVertex();\n" 1549 "}\n" 1550 "void _invoke() {\n" 1551 " gl_Position = gl_in[0].gl_Position + vec4(-0.5, 0.0, 0.0, float(sk_InvocationID));\n" 1552 " EmitVertex();\n" 1553 "}\n" 1554 "void main() {\n" 1555 " for (sk_InvocationID = 0;sk_InvocationID < 2; sk_InvocationID++) {\n" 1556 " _invoke();\n" 1557 " EndPrimitive();\n" 1558 " }\n" 1559 "}\n", 1560 SkSL::Program::kGeometry_Kind); 1561 test(r, 1562 "layout(points, invocations = 2) in;" 1563 "layout(invocations = 3) in;" 1564 "layout(line_strip, max_vertices = 2) out;" 1565 "void main() {" 1566 "sk_Position = sk_in[0].sk_Position + float4(-0.5, 0, 0, sk_InvocationID);" 1567 "EmitVertex();" 1568 "EndPrimitive();" 1569 "}", 1570 *SkSL::ShaderCapsFactory::GSInvocationsExtensionString(), 1571 "#version 400\n" 1572 "#extension GL_ARB_gpu_shader5 : require\n" 1573 "layout (points, invocations = 2) in ;\n" 1574 "layout (invocations = 3) in ;\n" 1575 "layout (line_strip, max_vertices = 2) out ;\n" 1576 "void main() {\n" 1577 " gl_Position = gl_in[0].gl_Position + vec4(-0.5, 0.0, 0.0, float(gl_InvocationID));\n" 1578 " EmitVertex();\n" 1579 " EndPrimitive();\n" 1580 "}\n", 1581 SkSL::Program::kGeometry_Kind); 1582 test(r, 1583 "layout(points, invocations = 2) in;" 1584 "layout(invocations = 3) in;" 1585 "layout(line_strip, max_vertices = 2) out;" 1586 "void main() {" 1587 "sk_Position = sk_in[0].sk_Position + float4(-0.5, 0, 0, sk_InvocationID);" 1588 "EmitVertex();" 1589 "EndPrimitive();" 1590 "}", 1591 *SkSL::ShaderCapsFactory::GeometryShaderExtensionString(), 1592 "#version 310es\n" 1593 "#extension GL_EXT_geometry_shader : require\n" 1594 "layout (points, invocations = 2) in ;\n" 1595 "layout (invocations = 3) in ;\n" 1596 "layout (line_strip, max_vertices = 2) out ;\n" 1597 "void main() {\n" 1598 " gl_Position = gl_in[0].gl_Position + vec4(-0.5, 0.0, 0.0, float(gl_InvocationID));\n" 1599 " EmitVertex();\n" 1600 " EndPrimitive();\n" 1601 "}\n", 1602 SkSL::Program::kGeometry_Kind); 1603 } 1604 1605 DEF_TEST(SkSLTypePrecision, r) { 1606 test(r, 1607 "float f = 1;" 1608 "half h = 2;" 1609 "double d = 3;" 1610 "float2 f2 = float2(1, 2);" 1611 "half3 h3 = half3(1, 2, 3);" 1612 "double4 d4 = double4(1, 2, 3, 4);" 1613 "float2x2 f22 = float2x2(1, 2, 3, 4);" 1614 "half2x4 h24 = half2x4(1, 2, 3, 4, 5, 6, 7, 8);" 1615 "double4x2 d42 = double4x2(1, 2, 3, 4, 5, 6, 7, 8);", 1616 *SkSL::ShaderCapsFactory::Default(), 1617 "#version 400\n" 1618 "out vec4 sk_FragColor;\n" 1619 "float f = 1.0;\n" 1620 "float h = 2.0;\n" 1621 "double d = 3.0;\n" 1622 "vec2 f2 = vec2(1.0, 2.0);\n" 1623 "vec3 h3 = vec3(1.0, 2.0, 3.0);\n" 1624 "dvec4 d4 = dvec4(1.0, 2.0, 3.0, 4.0);\n" 1625 "mat2 f22 = mat2(1.0, 2.0, 3.0, 4.0);\n" 1626 "mat2x4 h24 = mat2x4(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0);\n" 1627 "dmat4x2 d42 = dmat4x2(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0);\n"); 1628 test(r, 1629 "float f = 1;" 1630 "half h = 2;" 1631 "float2 f2 = float2(1, 2);" 1632 "half3 h3 = half3(1, 2, 3);" 1633 "float2x2 f22 = float2x2(1, 2, 3, 4);" 1634 "half2x4 h24 = half2x4(1, 2, 3, 4, 5, 6, 7, 8);", 1635 *SkSL::ShaderCapsFactory::UsesPrecisionModifiers(), 1636 "#version 400\n" 1637 "precision mediump float;\n" 1638 "out mediump vec4 sk_FragColor;\n" 1639 "highp float f = 1.0;\n" 1640 "mediump float h = 2.0;\n" 1641 "highp vec2 f2 = vec2(1.0, 2.0);\n" 1642 "mediump vec3 h3 = vec3(1.0, 2.0, 3.0);\n" 1643 "highp mat2 f22 = mat2(1.0, 2.0, 3.0, 4.0);\n" 1644 "mediump mat2x4 h24 = mat2x4(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0);\n"); 1645 } 1646 1647 DEF_TEST(SkSLNumberConversions, r) { 1648 test(r, 1649 "short s = short(sqrt(1));" 1650 "int i = int(sqrt(1));" 1651 "ushort us = ushort(sqrt(1));" 1652 "uint ui = uint(sqrt(1));" 1653 "half h = sqrt(1);" 1654 "float f = sqrt(1);" 1655 "short s2s = s;" 1656 "short i2s = i;" 1657 "short us2s = short(us);" 1658 "short ui2s = short(ui);" 1659 "short h2s = short(h);" 1660 "short f2s = short(f);" 1661 "int s2i = s;" 1662 "int i2i = i;" 1663 "int us2i = int(us);" 1664 "int ui2i = int(ui);" 1665 "int h2i = int(h);" 1666 "int f2i = int(f);" 1667 "ushort s2us = ushort(s);" 1668 "ushort i2us = ushort(i);" 1669 "ushort us2us = us;" 1670 "ushort ui2us = ui;" 1671 "ushort h2us = ushort(h);" 1672 "ushort f2us = ushort(f);" 1673 "uint s2ui = uint(s);" 1674 "uint i2ui = uint(i);" 1675 "uint us2ui = us;" 1676 "uint ui2ui = ui;" 1677 "uint h2ui = uint(h);" 1678 "uint f2ui = uint(f);" 1679 "float s2f = s;" 1680 "float i2f = i;" 1681 "float us2f = us;" 1682 "float ui2f = ui;" 1683 "float h2f = h;" 1684 "float f2f = f;", 1685 *SkSL::ShaderCapsFactory::Default(), 1686 "#version 400\n" 1687 "out vec4 sk_FragColor;\n" 1688 "int s = int(sqrt(1.0));\n" 1689 "int i = int(sqrt(1.0));\n" 1690 "uint us = uint(sqrt(1.0));\n" 1691 "uint ui = uint(sqrt(1.0));\n" 1692 "float h = sqrt(1.0);\n" 1693 "float f = sqrt(1.0);\n" 1694 "int s2s = s;\n" 1695 "int i2s = i;\n" 1696 "int us2s = int(us);\n" 1697 "int ui2s = int(ui);\n" 1698 "int h2s = int(h);\n" 1699 "int f2s = int(f);\n" 1700 "int s2i = s;\n" 1701 "int i2i = i;\n" 1702 "int us2i = int(us);\n" 1703 "int ui2i = int(ui);\n" 1704 "int h2i = int(h);\n" 1705 "int f2i = int(f);\n" 1706 "uint s2us = uint(s);\n" 1707 "uint i2us = uint(i);\n" 1708 "uint us2us = us;\n" 1709 "uint ui2us = ui;\n" 1710 "uint h2us = uint(h);\n" 1711 "uint f2us = uint(f);\n" 1712 "uint s2ui = uint(s);\n" 1713 "uint i2ui = uint(i);\n" 1714 "uint us2ui = us;\n" 1715 "uint ui2ui = ui;\n" 1716 "uint h2ui = uint(h);\n" 1717 "uint f2ui = uint(f);\n" 1718 "float s2f = float(s);\n" 1719 "float i2f = float(i);\n" 1720 "float us2f = float(us);\n" 1721 "float ui2f = float(ui);\n" 1722 "float h2f = h;\n" 1723 "float f2f = f;\n"); 1724 } 1725 1726 DEF_TEST(SkSLForceHighPrecision, r) { 1727 test(r, 1728 "void main() { half x = sqrt(1); half4 y = half4(x); sk_FragColor = y; }", 1729 *SkSL::ShaderCapsFactory::UsesPrecisionModifiers(), 1730 "#version 400\n" 1731 "precision mediump float;\n" 1732 "out mediump vec4 sk_FragColor;\n" 1733 "void main() {\n" 1734 " mediump float x = sqrt(1.0);\n" 1735 " mediump vec4 y = vec4(x);\n" 1736 " sk_FragColor = y;\n" 1737 "}\n"); 1738 SkSL::Program::Settings settings; 1739 settings.fForceHighPrecision = true; 1740 sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::UsesPrecisionModifiers(); 1741 settings.fCaps = caps.get(); 1742 SkSL::Program::Inputs inputs; 1743 test(r, 1744 "void main() { half x = sqrt(1); half4 y = half4(x); sk_FragColor = y; }", 1745 settings, 1746 "#version 400\n" 1747 "precision mediump float;\n" 1748 "out mediump vec4 sk_FragColor;\n" 1749 "void main() {\n" 1750 " highp float x = sqrt(1.0);\n" 1751 " highp vec4 y = vec4(x);\n" 1752 " sk_FragColor = y;\n" 1753 "}\n", 1754 &inputs); 1755 } 1756 1757 DEF_TEST(SkSLNormalization, r) { 1758 test(r, 1759 "uniform float4 sk_RTAdjust; void main() { sk_Position = half4(1); }", 1760 *SkSL::ShaderCapsFactory::Default(), 1761 "#version 400\n" 1762 "uniform vec4 sk_RTAdjust;\n" 1763 "void main() {\n" 1764 " gl_Position = vec4(1.0);\n" 1765 " gl_Position = vec4(gl_Position.x * sk_RTAdjust.x + gl_Position.w * sk_RTAdjust.y, " 1766 "gl_Position.y * sk_RTAdjust.z + gl_Position.w * sk_RTAdjust.w, " 1767 "0.0, " 1768 "gl_Position.w);\n" 1769 "}\n", 1770 SkSL::Program::kVertex_Kind); 1771 test(r, 1772 "uniform float4 sk_RTAdjust;" 1773 "layout(points) in;" 1774 "layout(invocations = 2) in;" 1775 "layout(line_strip, max_vertices = 2) out;" 1776 "void main() {" 1777 "sk_Position = sk_in[0].sk_Position + float4(-0.5, 0, 0, sk_InvocationID);" 1778 "EmitVertex();" 1779 "sk_Position = sk_in[0].sk_Position + float4(0.5, 0, 0, sk_InvocationID);" 1780 "EmitVertex();" 1781 "EndPrimitive();" 1782 "}", 1783 *SkSL::ShaderCapsFactory::GeometryShaderSupport(), 1784 "#version 400\n" 1785 "uniform vec4 sk_RTAdjust;\n" 1786 "layout (points) in ;\n" 1787 "layout (invocations = 2) in ;\n" 1788 "layout (line_strip, max_vertices = 2) out ;\n" 1789 "void main() {\n" 1790 " gl_Position = gl_in[0].gl_Position + vec4(-0.5, 0.0, 0.0, float(gl_InvocationID));\n" 1791 " {\n" 1792 " gl_Position = vec4(gl_Position.x * sk_RTAdjust.x + gl_Position.w * " 1793 "sk_RTAdjust.y, gl_Position.y * sk_RTAdjust.z + gl_Position.w * " 1794 "sk_RTAdjust.w, 0.0, gl_Position.w);\n" 1795 " EmitVertex();\n" 1796 " }\n" 1797 " gl_Position = gl_in[0].gl_Position + vec4(0.5, 0.0, 0.0, float(gl_InvocationID));\n" 1798 " {\n" 1799 " gl_Position = vec4(gl_Position.x * sk_RTAdjust.x + gl_Position.w * " 1800 "sk_RTAdjust.y, gl_Position.y * sk_RTAdjust.z + gl_Position.w * " 1801 "sk_RTAdjust.w, 0.0, gl_Position.w);\n" 1802 " EmitVertex();\n" 1803 " }\n" 1804 " EndPrimitive();\n" 1805 "}\n", 1806 SkSL::Program::kGeometry_Kind); 1807 } 1808 1809 DEF_TEST(SkSLTernaryLValue, r) { 1810 test(r, 1811 "void main() { half r, g; (true ? r : g) = 1; (false ? r : g) = 0; " 1812 "sk_FragColor = half4(r, g, 1, 1); }", 1813 *SkSL::ShaderCapsFactory::Default(), 1814 "#version 400\n" 1815 "out vec4 sk_FragColor;\n" 1816 "void main() {\n" 1817 " sk_FragColor = vec4(1.0, 0.0, 1.0, 1.0);\n" 1818 "}\n"); 1819 test(r, 1820 "void main() { half r, g; (true ? r : g) = sqrt(1); (false ? r : g) = sqrt(0); " 1821 "sk_FragColor = half4(r, g, 1, 1); }", 1822 *SkSL::ShaderCapsFactory::Default(), 1823 "#version 400\n" 1824 "out vec4 sk_FragColor;\n" 1825 "void main() {\n" 1826 " float r, g;\n" 1827 " r = sqrt(1.0);\n" 1828 " g = sqrt(0.0);\n" 1829 " sk_FragColor = vec4(r, g, 1.0, 1.0);\n" 1830 "}\n"); 1831 test(r, 1832 "void main() {" 1833 "half r, g;" 1834 "(sqrt(1) > 0 ? r : g) = sqrt(1);" 1835 "(sqrt(0) > 0 ? r : g) = sqrt(0);" 1836 "sk_FragColor = half4(r, g, 1, 1);" 1837 "}", 1838 *SkSL::ShaderCapsFactory::Default(), 1839 "#version 400\n" 1840 "out vec4 sk_FragColor;\n" 1841 "void main() {\n" 1842 " float r, g;\n" 1843 " sqrt(1.0) > 0.0 ? r : g = sqrt(1.0);\n" 1844 " sqrt(0.0) > 0.0 ? r : g = sqrt(0.0);\n" 1845 " sk_FragColor = vec4(r, g, 1.0, 1.0);\n" 1846 "}\n"); 1847 } 1848 1849 #endif 1850