Hash :
beae1b4a
Author :
Date :
2025-06-19T00:00:00
Metal: Remove obsolete workarounds Removed Intel-specific Metal workarounds that are not used on macOS 12 and later. Removed a redundant version check. Bug: angleproject:427600175 Change-Id: I34c7e53108f7e030512c9436ab2b9ae38ad17946 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/6672312 Commit-Queue: Alexey Knyazev <lexa.knyazev@gmail.com> Reviewed-by: Geoff Lang <geofflang@chromium.org>
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 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
//
// Copyright 2017 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// MSLOutput_test.cpp:
// Tests for MSL output.
//
#include <regex>
#include "GLSLANG/ShaderLang.h"
#include "angle_gl.h"
#include "gtest/gtest.h"
#include "tests/test_utils/compiler_test.h"
using namespace sh;
class MSLOutputTestBase : public MatchOutputCodeTest
{
public:
MSLOutputTestBase(GLenum shaderType) : MatchOutputCodeTest(shaderType, SH_MSL_METAL_OUTPUT)
{
setDefaultCompileOptions(defaultOptions());
}
static ShCompileOptions defaultOptions()
{
ShCompileOptions options = {};
// Default options that are forced for MSL output.
options.rescopeGlobalVariables = true;
options.simplifyLoopConditions = true;
options.initializeUninitializedLocals = true;
options.separateCompoundStructDeclarations = true;
options.removeInactiveVariables = true;
// The tests also test that validation succeeds. This should be also the
// default forced option, but currently MSL backend does not generate
// valid trees. Once validateAST is forced, move to above hunk.
options.validateAST = true;
return options;
}
};
class MSLOutputTest : public MSLOutputTestBase
{
public:
MSLOutputTest() : MSLOutputTestBase(GL_FRAGMENT_SHADER) {}
};
class MSLVertexOutputTest : public MSLOutputTestBase
{
public:
MSLVertexOutputTest() : MSLOutputTestBase(GL_VERTEX_SHADER) {}
};
// Test that having dynamic indexing of a vector inside the right hand side of logical or doesn't
// trigger asserts in MSL output.
TEST_F(MSLOutputTest, DynamicIndexingOfVectorOnRightSideOfLogicalOr)
{
const std::string &shaderString =
"#version 300 es\n"
"precision highp float;\n"
"out vec4 my_FragColor;\n"
"uniform int u1;\n"
"void main() {\n"
" bvec4 v = bvec4(true, true, true, false);\n"
" my_FragColor = vec4(v[u1 + 1] || v[u1]);\n"
"}\n";
compile(shaderString);
}
// Test that having an array constructor as a statement doesn't trigger an assert in MSL output.
TEST_F(MSLOutputTest, ArrayConstructorStatement)
{
const std::string &shaderString =
R"(#version 300 es
precision mediump float;
out vec4 outColor;
void main()
{
outColor = vec4(0.0, 0.0, 0.0, 1.0);
float[1](outColor[1]++);
})";
compile(shaderString);
}
// Test an array of arrays constructor as a statement.
TEST_F(MSLOutputTest, ArrayOfArraysStatement)
{
const std::string &shaderString =
R"(#version 310 es
precision mediump float;
out vec4 outColor;
void main()
{
outColor = vec4(0.0, 0.0, 0.0, 1.0);
float[2][2](float[2](outColor[1]++, 0.0), float[2](1.0, 2.0));
})";
compile(shaderString);
}
// Test dynamic indexing of a vector. This makes sure that helper functions added for dynamic
// indexing have correct data that subsequent traversal steps rely on.
TEST_F(MSLOutputTest, VectorDynamicIndexing)
{
const std::string &shaderString =
R"(#version 300 es
precision mediump float;
out vec4 outColor;
uniform int i;
void main()
{
vec4 foo = vec4(0.0, 0.0, 0.0, 1.0);
foo[i] = foo[i + 1];
outColor = foo;
})";
compile(shaderString);
}
// Test returning an array from a user-defined function. This makes sure that function symbols are
// changed consistently when the user-defined function is changed to have an array out parameter.
TEST_F(MSLOutputTest, ArrayReturnValue)
{
const std::string &shaderString =
R"(#version 300 es
precision mediump float;
uniform float u;
out vec4 outColor;
float[2] getArray(float f)
{
return float[2](f, f + 1.0);
}
void main()
{
float[2] arr = getArray(u);
outColor = vec4(arr[0], arr[1], 0.0, 1.0);
})";
compile(shaderString);
}
// Test that writing parameters without a name doesn't assert.
TEST_F(MSLOutputTest, ParameterWithNoName)
{
const std::string &shaderString =
R"(precision mediump float;
uniform vec4 v;
vec4 s(vec4)
{
return v;
}
void main()
{
gl_FragColor = s(v);
})";
compile(shaderString);
}
TEST_F(MSLOutputTest, Macro)
{
const std::string &shaderString =
R"(#version 300 es
precision highp float;
#define FOO vec4
out vec4 outColor;
void main()
{
outColor = FOO(1.0, 2.0, 3.0, 4.0);
})";
compile(shaderString);
}
TEST_F(MSLOutputTest, UniformSimple)
{
const std::string &shaderString =
R"(#version 300 es
precision highp float;
out vec4 outColor;
uniform float x;
void main()
{
outColor = vec4(x, x, x, x);
})";
compile(shaderString);
}
TEST_F(MSLOutputTest, FragmentOutSimple)
{
const std::string &shaderString =
R"(#version 300 es
precision highp float;
out vec4 outColor;
void main()
{
outColor = vec4(1.0, 2.0, 3.0, 4.0);
})";
compile(shaderString);
}
TEST_F(MSLOutputTest, FragmentOutIndirect1)
{
const std::string &shaderString =
R"(#version 300 es
precision highp float;
out vec4 outColor;
void foo()
{
outColor = vec4(1.0, 2.0, 3.0, 4.0);
}
void bar()
{
foo();
}
void main()
{
bar();
})";
compile(shaderString);
}
TEST_F(MSLOutputTest, FragmentOutIndirect2)
{
const std::string &shaderString =
R"(#version 300 es
precision highp float;
out vec4 outColor;
void foo();
void bar()
{
foo();
}
void foo()
{
outColor = vec4(1.0, 2.0, 3.0, 4.0);
}
void main()
{
bar();
})";
compile(shaderString);
}
TEST_F(MSLOutputTest, FragmentOutIndirect3)
{
const std::string &shaderString =
R"(#version 300 es
precision highp float;
out vec4 outColor;
float foo(float x, float y)
{
outColor = vec4(x, y, 3.0, 4.0);
return 7.0;
}
float bar(float x)
{
return foo(x, 2.0);
}
float baz()
{
return 13.0;
}
float identity(float x)
{
return x;
}
void main()
{
identity(bar(baz()));
})";
compile(shaderString);
}
TEST_F(MSLOutputTest, VertexInOut)
{
const std::string &shaderString =
R"(#version 300 es
precision highp float;
in float in0;
out float out0;
void main()
{
out0 = in0;
})";
compile(shaderString);
}
TEST_F(MSLOutputTest, SymbolSharing)
{
const std::string &shaderString =
R"(#version 300 es
precision highp float;
out vec4 outColor;
struct Foo {
float x;
float y;
};
void doFoo(Foo foo, float zw);
void doFoo(Foo foo, float zw)
{
foo.x = foo.y;
outColor = vec4(foo.x, foo.y, zw, zw);
}
void main()
{
Foo foo;
foo.x = 2.0;
foo.y = 2.0;
doFoo(foo, 3.0);
})";
compile(shaderString);
}
TEST_F(MSLOutputTest, StructDecl)
{
const std::string &shaderString =
R"(#version 300 es
precision highp float;
out float out0;
struct Foo {
float value;
};
void main()
{
Foo foo;
out0 = foo.value;
}
)";
compile(shaderString);
}
TEST_F(MSLOutputTest, Structs)
{
const std::string &shaderString =
R"(#version 300 es
precision highp float;
struct Foo {
float value;
};
out vec4 out0;
struct Bar {
Foo foo;
};
void go();
uniform UniInstance {
Bar bar;
float instance;
} uniInstance;
uniform UniGlobal {
Foo foo;
float global;
};
void main()
{
go();
}
struct Baz {
Bar bar;
} baz;
void go()
{
out0.x = baz.bar.foo.value;
out0.y = global;
out0.z = uniInstance.instance;
out0.w = 0.0;
}
)";
compile(shaderString);
}
TEST_F(MSLOutputTest, KeywordConflict)
{
const std::string &shaderString =
R"(#version 300 es
precision highp float;
struct fragment {
float kernel;
} device;
struct Foo {
fragment frag;
} foo;
out float vertex;
float kernel;
float stage_in(float x)
{
return x;
}
void metal(float metal, float fragment);
void metal(float metal, float fragment)
{
vertex = metal * fragment * foo.frag.kernel;
}
void main()
{
metal(stage_in(stage_in(kernel * device.kernel)), foo.frag.kernel);
})";
compile(shaderString);
}
TEST_F(MSLVertexOutputTest, Vertex)
{
const std::string &shaderString =
R"(#version 300 es
precision highp float;
void main()
{
gl_Position = vec4(1.0,1.0,1.0,1.0);
})";
compile(shaderString);
}
TEST_F(MSLVertexOutputTest, LastReturn)
{
const std::string &shaderString =
R"(#version 300 es
in highp vec4 a_position;
in highp vec4 a_coords;
out highp vec4 v_color;
void main (void)
{
gl_Position = a_position;
v_color = vec4(a_coords.xyz, 1.0);
return;
})";
compile(shaderString);
}
TEST_F(MSLOutputTest, LastReturn)
{
const std::string &shaderString =
R"(#version 300 es
in mediump vec4 v_coords;
layout(location = 0) out mediump vec4 o_color;
void main (void)
{
o_color = vec4(v_coords.xyz, 1.0);
return;
})";
compile(shaderString);
}
TEST_F(MSLOutputTest, FragColor)
{
const std::string &shaderString = R"(
void main ()
{
gl_FragColor = vec4(1.0, 0.0, 1.0, 1.0);
})";
compile(shaderString);
}
TEST_F(MSLOutputTest, MatrixIn)
{
const std::string &shaderString =
R"(#version 300 es
precision highp float;
in mat4 mat;
out float out0;
void main()
{
out0 = mat[0][0];
}
)";
compile(shaderString);
}
TEST_F(MSLOutputTest, WhileTrue)
{
const std::string &shaderString =
R"(#version 300 es
precision mediump float;
uniform float uf;
out vec4 my_FragColor;
void main()
{
my_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
while (true)
{
break;
}
})";
compile(shaderString);
}
TEST_F(MSLOutputTest, ForTrue)
{
const std::string &shaderString =
R"(#version 300 es
precision mediump float;
uniform float uf;
out vec4 my_FragColor;
void main()
{
my_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
for (;true;)
{
break;
}
})";
compile(shaderString);
}
TEST_F(MSLOutputTest, ForEmpty)
{
const std::string &shaderString =
R"(#version 300 es
precision mediump float;
uniform float uf;
out vec4 my_FragColor;
void main()
{
my_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
for (;;)
{
break;
}
})";
compile(shaderString);
}
TEST_F(MSLOutputTest, ForComplex)
{
const std::string &shaderString =
R"(#version 300 es
precision mediump float;
uniform float uf;
out vec4 my_FragColor;
void main()
{
my_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
for (int i = 0, j = 2; i < j; ++i) {
if (i == 0) continue;
if (i == 42) break;
my_FragColor.x += float(i);
}
})";
compile(shaderString);
}
TEST_F(MSLOutputTest, ForSymbol)
{
const std::string &shaderString =
R"(#version 300 es
precision mediump float;
uniform float uf;
out vec4 my_FragColor;
void main()
{
bool cond = true;
for (;cond;)
{
my_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
cond = false;
}
})";
compile(shaderString);
}
TEST_F(MSLOutputTest, DoWhileSymbol)
{
const std::string &shaderString =
R"(#version 300 es
precision mediump float;
uniform float uf;
out vec4 my_FragColor;
void main()
{
bool cond = false;
do
{
my_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
} while (cond);
})";
compile(shaderString);
}
TEST_F(MSLOutputTest, AnonymousStruct)
{
const std::string &shaderString =
R"(
precision mediump float;
struct { vec4 v; } anonStruct;
void main() {
anonStruct.v = vec4(0.0,1.0,0.0,1.0);
gl_FragColor = anonStruct.v;
})";
compile(shaderString);
// TODO(anglebug.com/42264909): This success condition is expected to fail now.
// When WebKit build is able to run the tests, this should be changed to something else.
// ASSERT_TRUE(foundInCode(SH_MSL_METAL_OUTPUT, "__unnamed"));
}
TEST_F(MSLOutputTest, GlobalRescopingSimple)
{
const std::string &shaderString =
R"(#version 300 es
precision mediump float;
// Should rescope uf into main
float uf;
out vec4 my_FragColor;
void main()
{
uf += 1.0f;
my_FragColor = vec4(uf, 0.0, 0.0, 1.0);
})";
compile(shaderString);
}
TEST_F(MSLOutputTest, GlobalRescopingNoRescope)
{
const std::string &shaderString =
R"(#version 300 es
precision mediump float;
// Should not rescope any variable
float uf;
out vec4 my_FragColor;
void modifyGlobal()
{
uf = 1.0f;
}
void main()
{
modifyGlobal();
my_FragColor = vec4(uf, 0.0, 0.0, 1.0);
})";
compile(shaderString);
}
TEST_F(MSLOutputTest, GlobalRescopingInitializer)
{
const std::string &shaderString =
R"(#version 300 es
precision mediump float;
// Should rescope uf into main
float uf = 1.0f;
out vec4 my_FragColor;
void main()
{
uf += 1.0;
my_FragColor = vec4(uf, 0.0, 0.0, 1.0);
})";
compile(shaderString);
}
TEST_F(MSLOutputTest, GlobalRescopingInitializerNoRescope)
{
const std::string &shaderString =
R"(#version 300 es
precision mediump float;
// Should not rescope any variable
float uf = 1.0f;
out vec4 my_FragColor;
void modifyGlobal()
{
uf =+ 1.0f;
}
void main()
{
modifyGlobal();
my_FragColor = vec4(uf, 0.0, 0.0, 1.0);
})";
compile(shaderString);
}
TEST_F(MSLOutputTest, GlobalRescopingNestedFunction)
{
const std::string &shaderString =
R"(#version 300 es
precision mediump float;
// Should rescope a info modifyGlobal
float a = 1.0f;
float uf = 1.0f;
out vec4 my_FragColor;
void modifyGlobal()
{
uf =+ a;
}
void main()
{
modifyGlobal();
my_FragColor = vec4(uf, 0.0, 0.0, 1.0);
})";
compile(shaderString);
}
TEST_F(MSLOutputTest, GlobalRescopingMultipleUses)
{
const std::string &shaderString =
R"(#version 300 es
precision mediump float;
// Should rescope uf into main
float uf = 1.0f;
out vec4 my_FragColor;
void main()
{
uf =+ 1.0;
if (uf > 0.0)
{
uf =- 0.5;
}
my_FragColor = vec4(uf, 0.0, 0.0, 1.0);
})";
compile(shaderString);
}
TEST_F(MSLOutputTest, GlobalRescopingGloballyReferencedVar)
{
const std::string &shaderString =
R"(#version 300 es
precision mediump float;
// Should rescope uf into main
const float a = 1.0f;
float uf = a;
out vec4 my_FragColor;
void main()
{
my_FragColor = vec4(uf, 0.0, a, 0.0);
})";
compile(shaderString);
}
TEST_F(MSLOutputTest, GlobalRescopingDeclarationAfterFunction)
{
const std::string &shaderString =
R"(#version 300 es
precision mediump float;
// Should rescope c and b into main
float a = 1.0f;
float c = 1.0f;
out vec4 my_FragColor;
void modifyGlobal()
{
a =+ 1.0f;
}
float b = 1.0f;
void main()
{
modifyGlobal();
my_FragColor = vec4(a, b, c, 0.0);
}
)";
compile(shaderString);
}
TEST_F(MSLOutputTest, ReusedOutVarName)
{
const std::string &shaderString =
R"(#version 300 es
precision mediump float;
out vec4 my_FragColor;
void funcWith1Out(
out float outC) {
outC = 1.0;
}
void funcWith4Outs(
out float outA,
out float outB,
out float outC,
out float outD) {
outA = 1.0;
outB = 1.0;
outD = 1.0;
}
void main()
{
funcWith1Out(my_FragColor.g);
funcWith4Outs(my_FragColor.r, my_FragColor.g, my_FragColor.b, my_FragColor.a);
}
)";
compile(shaderString);
}
// Test that for loops without body do not crash. At the time of writing, constant hoisting would
// traverse such ASTs and crash when loop bodies were not present.
TEST_F(MSLOutputTest, RemovedForBodyNoCrash)
{
const char kShader[] = R"(#version 310 es
void main() {
for(;;)if(2==0);
})";
compile(kShader);
}
// Test that accessing array element of array of anonymous struct instances does not fail
// validation.
TEST_F(MSLOutputTest, AnonymousStructArrayValidationNoCrash)
{
const char kShader[] = R"(
precision mediump float;
void main() {
struct { vec4 field; } s1[1];
gl_FragColor = s1[0].field;
})";
compile(kShader);
}
// Tests that rewriting varyings for per-element element access does not cause crash.
// At the time of writing a_ would be confused with a due to matrixes being flattened
// for fragment inputs, and the new variables would be given semantic names separated
// with _. This would cause confusion because semantic naming would filter underscores.
TEST_F(MSLOutputTest, VaryingRewriteUnderscoreNoCrash)
{
const char kShader[] = R"(precision mediump float;
varying mat2 a_;
varying mat3 a;
void main(){
gl_FragColor = vec4(a_) + vec4(a);
})";
compile(kShader);
}
// Tests that rewriting varyings for per-element element access does not cause crash.
// Test for a clash between a[0] and a_0. Both could be clashing at a_0.
TEST_F(MSLVertexOutputTest, VaryingRewriteUnderscoreNoCrash)
{
const char kShader[] = R"(precision mediump float;
varying mat2 a_0;
varying mat3 a[1];
void main(){
a_0 = mat2(0,1,2,3);
a[0] = mat3(0,1,2,3,4,5,6,7,8);
gl_Position = vec4(1);
})";
compile(kShader);
}
// Tests that rewriting varyings for per-element element access does not cause crash.
// ES3 variant.
// Test for a clash between a[0] and a_0. Both could be clashing at a_0.
TEST_F(MSLVertexOutputTest, VaryingRewriteUnderscoreNoCrash2)
{
const char kShader[] = R"(#version 300 es
precision mediump float;
out mat2 a_0;
out mat3 a[1];
void main(){
a_0 = mat2(0,1,2,3);
a[0] = mat3(0,1,2,3,4,5,6,7,8);
})";
compile(kShader);
}
// Tests that rewriting varyings for per-element element access does not cause crash.
// Test for a clash between a_[0] and a._0. Both could be clashing at a__0.
TEST_F(MSLVertexOutputTest, VaryingRewriteUnderscoreNoCrash3)
{
const char kShader[] = R"(#version 300 es
precision mediump float;
out mat3 a_[1];
struct s {
mat2 _0;
};
out s a;
void main(){
a._0 = mat2(0,1,2,3);
a_[0] = mat3(0,1,2,3,4,5,6,7,8);
})";
compile(kShader);
}
// Tests that rewriting attributes for per-element element access does not cause crash.
// At the time of writing a_ would be confused with a due to matrixes being flattened
// for fragment inputs, and the new variables would be given semantic names separated
// with _. This would cause confusion because semantic naming would filter underscores.
TEST_F(MSLVertexOutputTest, AttributeRewriteUnderscoreNoCrash)
{
const char kShader[] = R"(precision mediump float;
attribute mat2 a_;
attribute mat3 a;
void main(){
gl_Position = vec4(a_) + vec4(a);
})";
compile(kShader);
}
// Test that emulated clip distance varying passes AST validation
TEST_F(MSLVertexOutputTest, ClipDistanceVarying)
{
getResources()->ANGLE_clip_cull_distance = 1;
const char kShader[] = R"(#version 300 es
#extension GL_ANGLE_clip_cull_distance:require
void main(){gl_ClipDistance[0];})";
compile(kShader);
}
TEST_F(MSLVertexOutputTest, VertexIDIvecNoCrash)
{
const char kShader[] = R"(#version 300 es
void main(){ivec2 xy=ivec2((+gl_VertexID));gl_Position=vec4((xy), 0,1);})";
compile(kShader);
}
TEST_F(MSLVertexOutputTest, StructEqualityNoCrash)
{
const char kShader[] = R"(#version 300 es
struct S{mediump vec2 i;};S a,b;void main(){if (a==b){}})";
compile(kShader);
}
TEST_F(MSLOutputTest, StructAndVarDeclarationNoCrash)
{
const char kShader[] = R"(#version 300 es
void main(){struct S{mediump vec4 v;};S a;a=a,1;})";
compile(kShader);
}
TEST_F(MSLOutputTest, StructAndVarDeclarationSeparationNoCrash)
{
const char kShader[] = R"(#version 300 es
void main(){struct S{mediump vec4 v;}a;a=a,1;})";
compile(kShader);
}
TEST_F(MSLOutputTest, StructAndVarDeclarationSeparationNoCrash2)
{
const char kShader[] = R"(#version 300 es
void main(){struct S{mediump vec4 v;}a,b;a=b,1;})";
compile(kShader);
}
TEST_F(MSLOutputTest, StructAndVarDeclarationSeparationNoCrash3)
{
const char kShader[] = R"(#version 300 es
void main(){struct S1{mediump vec4 v;}l;struct S2{S1 s1;}s2;s2=s2,l=l,1;})";
compile(kShader);
}
TEST_F(MSLOutputTest, MultisampleInterpolationNoCrash)
{
getResources()->OES_shader_multisample_interpolation = 1;
const char kShader[] = R"(#version 300 es
#extension GL_OES_shader_multisample_interpolation : require
precision highp float;
in float i; out vec4 c; void main() { c = vec4(interpolateAtOffset(i, vec2(i))); })";
compile(kShader);
}
TEST_F(MSLVertexOutputTest, ClipCullDistanceNoCrash)
{
getResources()->ANGLE_clip_cull_distance = 1;
const char kShader[] = R"(#version 300 es
#extension GL_ANGLE_clip_cull_distance : require
void main() { gl_Position = vec4(0.0, 0.0, 0.0, 1.0); gl_ClipDistance[1] = 1.0;})";
compile(kShader);
}
TEST_F(MSLOutputTest, UnnamedOutParameterNoCrash)
{
const char kShader[] = R"(void f(out int){}void main(){int a;f(a);})";
compile(kShader);
}
TEST_F(MSLOutputTest, ExplicitBoolCastsNoCrash)
{
ShCompileOptions options = defaultOptions();
const char kShader[] = R"(
precision mediump float;
void main(){vec2 c;bvec2 U=bvec2(c.xx);if (U.x) gl_FragColor = vec4(1);})";
compile(kShader, options);
}
// The following tests check that the SeparateCompoundExpressions step during MSL shader translation
// handles comma expressions correctly when at least one of the operands is a function call.
TEST_F(MSLOutputTest, CommaOpTwoFunctionCallsWithGlobalsNoCrash)
{
ShCompileOptions options = defaultOptions();
const char kShader[] = R"(
int g;
void F(int v) { g = v; }
void main() { F(g), F(g); })";
compile(kShader, options);
}
TEST_F(MSLOutputTest, CommaOpLeftFunctionCallWithGlobalsNoCrash)
{
ShCompileOptions options = defaultOptions();
const char kShader[] = R"(
int g;
void F(int v) { g = v; }
void main() { F(g), F(1); })";
compile(kShader, options);
}
TEST_F(MSLOutputTest, CommaOpRightFunctionCallWithGlobalsNoCrash)
{
ShCompileOptions options = defaultOptions();
const char kShader[] = R"(
int g;
void F(int v) { g = v; }
void main() { F(1), F(g); })";
compile(kShader, options);
}
TEST_F(MSLOutputTest, EnsureLoopForwardProgressInfinite)
{
ShCompileOptions options = defaultOptions();
options.ensureLoopForwardProgress = 1;
const std::string &shaderString =
R"(
precision mediump float;
void main() {
for (int i = 0; i < i + 1; ++i) { }
gl_FragColor = vec4(1);
})";
compile(shaderString, options);
ASSERT_TRUE(foundInCode(SH_MSL_METAL_OUTPUT, "loopForwardProgress();"));
ASSERT_TRUE(foundInCode(SH_MSL_METAL_OUTPUT, "volatile bool p = true;"));
}
TEST_F(MSLOutputTest, EnsureLoopForwardProgressFinite)
{
ShCompileOptions options = defaultOptions();
options.ensureLoopForwardProgress = 1;
const std::string &shaderString =
R"(
precision mediump float;
void main() {
for (int i = 0; i < 1; ++i) { }
gl_FragColor = vec4(1);
})";
compile(shaderString, options);
ASSERT_FALSE(foundInCode(SH_MSL_METAL_OUTPUT, "loopForwardProgress();"));
}