Hash :
9fc3682c
Author :
Date :
2015-11-18T13:08:07
D3D: Rework varying packing code. In D3D we pack varyings by making a register map, and using the recommended GLSL ES algorithm to reserve register space. We use this map to assign row and column slots to each varying and then produce a semantic index value. The existing scheme had a number of bugs, and was failing several angle_end2end_tests. The new design cleans up the code somewhat and uses a different counting scheme for the semantic indexes: just sort the varyings in packing order and use a simple incrementing semantic index per varying. In SM4+, the HLSL compiler sorts and packs the varyings correctly itself, and in SM3, handle the cases we don't support by returning an error instead of a D3D compiler link error. Also refactor how we store varying information for TF Feedback/ StreamOut. Only store the necessary D3D information, instead of extra information like the name and type. This fixes several tests in GLSLTest/*. This also will allow us to fix interpolation qualifier packing and the structure packing in HLSL, which seems to work differently than the rest of the varying types. BUG=angleproject:1202 TEST=bots,dEQP-GLES3.functional.transform_feedback.* Change-Id: Ie5bfbb4f71d8bf97f39115fc46d2e61b131df639 Reviewed-on: https://chromium-review.googlesource.com/311241 Reviewed-by: Geoff Lang <geofflang@chromium.org> Tested-by: Jamie Madill <jmadill@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 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322
//
// Copyright 2015 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.
//
#include "test_utils/ANGLETest.h"
#include "libANGLE/Context.h"
#include "libANGLE/Program.h"
using namespace angle;
class GLSLTest : public ANGLETest
{
protected:
GLSLTest()
{
setWindowWidth(128);
setWindowHeight(128);
setConfigRedBits(8);
setConfigGreenBits(8);
setConfigBlueBits(8);
setConfigAlphaBits(8);
}
virtual void SetUp()
{
ANGLETest::SetUp();
mSimpleVSSource = SHADER_SOURCE
(
attribute vec4 inputAttribute;
void main()
{
gl_Position = inputAttribute;
}
);
}
std::string GenerateVaryingType(GLint vectorSize)
{
char varyingType[10];
if (vectorSize == 1)
{
sprintf(varyingType, "float");
}
else
{
sprintf(varyingType, "vec%d", vectorSize);
}
return std::string(varyingType);
}
std::string GenerateVectorVaryingDeclaration(GLint vectorSize, GLint arraySize, GLint id)
{
char buff[100];
if (arraySize == 1)
{
sprintf(buff, "varying %s v%d;\n", GenerateVaryingType(vectorSize).c_str(), id);
}
else
{
sprintf(buff, "varying %s v%d[%d];\n", GenerateVaryingType(vectorSize).c_str(), id, arraySize);
}
return std::string(buff);
}
std::string GenerateVectorVaryingSettingCode(GLint vectorSize, GLint arraySize, GLint id)
{
std::string returnString;
char buff[100];
if (arraySize == 1)
{
sprintf(buff, "\t v%d = %s(1.0);\n", id, GenerateVaryingType(vectorSize).c_str());
returnString += buff;
}
else
{
for (int i = 0; i < arraySize; i++)
{
sprintf(buff, "\t v%d[%d] = %s(1.0);\n", id, i, GenerateVaryingType(vectorSize).c_str());
returnString += buff;
}
}
return returnString;
}
std::string GenerateVectorVaryingUseCode(GLint arraySize, GLint id)
{
if (arraySize == 1)
{
char buff[100];
sprintf(buff, "v%d + ", id);
return std::string(buff);
}
else
{
std::string returnString;
for (int i = 0; i < arraySize; i++)
{
char buff[100];
sprintf(buff, "v%d[%d] + ", id, i);
returnString += buff;
}
return returnString;
}
}
void GenerateGLSLWithVaryings(GLint floatCount, GLint floatArrayCount, GLint vec2Count, GLint vec2ArrayCount, GLint vec3Count, GLint vec3ArrayCount,
GLint vec4Count, GLint vec4ArrayCount, bool useFragCoord, bool usePointCoord, bool usePointSize,
std::string* fragmentShader, std::string* vertexShader)
{
// Generate a string declaring the varyings, to share between the fragment shader and the vertex shader.
std::string varyingDeclaration;
unsigned int varyingCount = 0;
for (GLint i = 0; i < floatCount; i++)
{
varyingDeclaration += GenerateVectorVaryingDeclaration(1, 1, varyingCount);
varyingCount += 1;
}
for (GLint i = 0; i < floatArrayCount; i++)
{
varyingDeclaration += GenerateVectorVaryingDeclaration(1, 2, varyingCount);
varyingCount += 1;
}
for (GLint i = 0; i < vec2Count; i++)
{
varyingDeclaration += GenerateVectorVaryingDeclaration(2, 1, varyingCount);
varyingCount += 1;
}
for (GLint i = 0; i < vec2ArrayCount; i++)
{
varyingDeclaration += GenerateVectorVaryingDeclaration(2, 2, varyingCount);
varyingCount += 1;
}
for (GLint i = 0; i < vec3Count; i++)
{
varyingDeclaration += GenerateVectorVaryingDeclaration(3, 1, varyingCount);
varyingCount += 1;
}
for (GLint i = 0; i < vec3ArrayCount; i++)
{
varyingDeclaration += GenerateVectorVaryingDeclaration(3, 2, varyingCount);
varyingCount += 1;
}
for (GLint i = 0; i < vec4Count; i++)
{
varyingDeclaration += GenerateVectorVaryingDeclaration(4, 1, varyingCount);
varyingCount += 1;
}
for (GLint i = 0; i < vec4ArrayCount; i++)
{
varyingDeclaration += GenerateVectorVaryingDeclaration(4, 2, varyingCount);
varyingCount += 1;
}
// Generate the vertex shader
vertexShader->clear();
vertexShader->append(varyingDeclaration);
vertexShader->append("\nvoid main()\n{\n");
unsigned int currentVSVarying = 0;
for (GLint i = 0; i < floatCount; i++)
{
vertexShader->append(GenerateVectorVaryingSettingCode(1, 1, currentVSVarying));
currentVSVarying += 1;
}
for (GLint i = 0; i < floatArrayCount; i++)
{
vertexShader->append(GenerateVectorVaryingSettingCode(1, 2, currentVSVarying));
currentVSVarying += 1;
}
for (GLint i = 0; i < vec2Count; i++)
{
vertexShader->append(GenerateVectorVaryingSettingCode(2, 1, currentVSVarying));
currentVSVarying += 1;
}
for (GLint i = 0; i < vec2ArrayCount; i++)
{
vertexShader->append(GenerateVectorVaryingSettingCode(2, 2, currentVSVarying));
currentVSVarying += 1;
}
for (GLint i = 0; i < vec3Count; i++)
{
vertexShader->append(GenerateVectorVaryingSettingCode(3, 1, currentVSVarying));
currentVSVarying += 1;
}
for (GLint i = 0; i < vec3ArrayCount; i++)
{
vertexShader->append(GenerateVectorVaryingSettingCode(3, 2, currentVSVarying));
currentVSVarying += 1;
}
for (GLint i = 0; i < vec4Count; i++)
{
vertexShader->append(GenerateVectorVaryingSettingCode(4, 1, currentVSVarying));
currentVSVarying += 1;
}
for (GLint i = 0; i < vec4ArrayCount; i++)
{
vertexShader->append(GenerateVectorVaryingSettingCode(4, 2, currentVSVarying));
currentVSVarying += 1;
}
if (usePointSize)
{
vertexShader->append("gl_PointSize = 1.0;\n");
}
vertexShader->append("}\n");
// Generate the fragment shader
fragmentShader->clear();
fragmentShader->append("precision highp float;\n");
fragmentShader->append(varyingDeclaration);
fragmentShader->append("\nvoid main() \n{ \n\tvec4 retColor = vec4(0,0,0,0);\n");
unsigned int currentFSVarying = 0;
// Make use of the float varyings
fragmentShader->append("\tretColor += vec4(");
for (GLint i = 0; i < floatCount; i++)
{
fragmentShader->append(GenerateVectorVaryingUseCode(1, currentFSVarying));
currentFSVarying += 1;
}
for (GLint i = 0; i < floatArrayCount; i++)
{
fragmentShader->append(GenerateVectorVaryingUseCode(2, currentFSVarying));
currentFSVarying += 1;
}
fragmentShader->append("0.0, 0.0, 0.0, 0.0);\n");
// Make use of the vec2 varyings
fragmentShader->append("\tretColor += vec4(");
for (GLint i = 0; i < vec2Count; i++)
{
fragmentShader->append(GenerateVectorVaryingUseCode(1, currentFSVarying));
currentFSVarying += 1;
}
for (GLint i = 0; i < vec2ArrayCount; i++)
{
fragmentShader->append(GenerateVectorVaryingUseCode(2, currentFSVarying));
currentFSVarying += 1;
}
fragmentShader->append("vec2(0.0, 0.0), 0.0, 0.0);\n");
// Make use of the vec3 varyings
fragmentShader->append("\tretColor += vec4(");
for (GLint i = 0; i < vec3Count; i++)
{
fragmentShader->append(GenerateVectorVaryingUseCode(1, currentFSVarying));
currentFSVarying += 1;
}
for (GLint i = 0; i < vec3ArrayCount; i++)
{
fragmentShader->append(GenerateVectorVaryingUseCode(2, currentFSVarying));
currentFSVarying += 1;
}
fragmentShader->append("vec3(0.0, 0.0, 0.0), 0.0);\n");
// Make use of the vec4 varyings
fragmentShader->append("\tretColor += ");
for (GLint i = 0; i < vec4Count; i++)
{
fragmentShader->append(GenerateVectorVaryingUseCode(1, currentFSVarying));
currentFSVarying += 1;
}
for (GLint i = 0; i < vec4ArrayCount; i++)
{
fragmentShader->append(GenerateVectorVaryingUseCode(2, currentFSVarying));
currentFSVarying += 1;
}
fragmentShader->append("vec4(0.0, 0.0, 0.0, 0.0);\n");
// Set gl_FragColor, and use special variables if requested
fragmentShader->append("\tgl_FragColor = retColor");
if (useFragCoord)
{
fragmentShader->append(" + gl_FragCoord");
}
if (usePointCoord)
{
fragmentShader->append(" + vec4(gl_PointCoord, 0.0, 0.0)");
}
fragmentShader->append(";\n}");
}
void VaryingTestBase(GLint floatCount, GLint floatArrayCount, GLint vec2Count, GLint vec2ArrayCount, GLint vec3Count, GLint vec3ArrayCount,
GLint vec4Count, GLint vec4ArrayCount, bool useFragCoord, bool usePointCoord, bool usePointSize, bool expectSuccess)
{
std::string fragmentShaderSource;
std::string vertexShaderSource;
GenerateGLSLWithVaryings(floatCount, floatArrayCount, vec2Count, vec2ArrayCount, vec3Count, vec3ArrayCount,
vec4Count, vec4ArrayCount, useFragCoord, usePointCoord, usePointSize,
&fragmentShaderSource, &vertexShaderSource);
GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
if (expectSuccess)
{
EXPECT_NE(0u, program);
}
else
{
EXPECT_EQ(0u, program);
}
}
void CompileGLSLWithUniformsAndSamplers(GLint vertexUniformCount,
GLint fragmentUniformCount,
GLint vertexSamplersCount,
GLint fragmentSamplersCount,
bool expectSuccess)
{
std::stringstream vertexShader;
std::stringstream fragmentShader;
// Generate the vertex shader
vertexShader << "precision mediump float;\n";
for (int i = 0; i < vertexUniformCount; i++)
{
vertexShader << "uniform vec4 v" << i << ";\n";
}
for (int i = 0; i < vertexSamplersCount; i++)
{
vertexShader << "uniform sampler2D s" << i << ";\n";
}
vertexShader << "void main()\n{\n";
for (int i = 0; i < vertexUniformCount; i++)
{
vertexShader << " gl_Position += v" << i << ";\n";
}
for (int i = 0; i < vertexSamplersCount; i++)
{
vertexShader << " gl_Position += texture2D(s" << i << ", vec2(0.0, 0.0));\n";
}
if (vertexUniformCount == 0 && vertexSamplersCount == 0)
{
vertexShader << " gl_Position = vec4(0.0);\n";
}
vertexShader << "}\n";
// Generate the fragment shader
fragmentShader << "precision mediump float;\n";
for (int i = 0; i < fragmentUniformCount; i++)
{
fragmentShader << "uniform vec4 v" << i << ";\n";
}
for (int i = 0; i < fragmentSamplersCount; i++)
{
fragmentShader << "uniform sampler2D s" << i << ";\n";
}
fragmentShader << "void main()\n{\n";
for (int i = 0; i < fragmentUniformCount; i++)
{
fragmentShader << " gl_FragColor += v" << i << ";\n";
}
for (int i = 0; i < fragmentSamplersCount; i++)
{
fragmentShader << " gl_FragColor += texture2D(s" << i << ", vec2(0.0, 0.0));\n";
}
if (fragmentUniformCount == 0 && fragmentSamplersCount == 0)
{
fragmentShader << " gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);\n";
}
fragmentShader << "}\n";
GLuint program = CompileProgram(vertexShader.str(), fragmentShader.str());
if (expectSuccess)
{
EXPECT_NE(0u, program);
}
else
{
EXPECT_EQ(0u, program);
}
}
std::string mSimpleVSSource;
};
class GLSLTest_ES3 : public GLSLTest
{
};
TEST_P(GLSLTest, NamelessScopedStructs)
{
const std::string fragmentShaderSource = SHADER_SOURCE
(
precision mediump float;
void main()
{
struct
{
float q;
} b;
gl_FragColor = vec4(1, 0, 0, 1);
gl_FragColor.a += b.q;
}
);
GLuint program = CompileProgram(mSimpleVSSource, fragmentShaderSource);
EXPECT_NE(0u, program);
}
TEST_P(GLSLTest, ScopedStructsOrderBug)
{
const std::string fragmentShaderSource = SHADER_SOURCE
(
precision mediump float;
struct T
{
float f;
};
void main()
{
T a;
struct T
{
float q;
};
T b;
gl_FragColor = vec4(1, 0, 0, 1);
gl_FragColor.a += a.f;
gl_FragColor.a += b.q;
}
);
GLuint program = CompileProgram(mSimpleVSSource, fragmentShaderSource);
EXPECT_NE(0u, program);
}
TEST_P(GLSLTest, ScopedStructsBug)
{
const std::string fragmentShaderSource = SHADER_SOURCE
(
precision mediump float;
struct T_0
{
float f;
};
void main()
{
gl_FragColor = vec4(1, 0, 0, 1);
struct T
{
vec2 v;
};
T_0 a;
T b;
gl_FragColor.a += a.f;
gl_FragColor.a += b.v.x;
}
);
GLuint program = CompileProgram(mSimpleVSSource, fragmentShaderSource);
EXPECT_NE(0u, program);
}
TEST_P(GLSLTest, DxPositionBug)
{
const std::string &vertexShaderSource = SHADER_SOURCE
(
attribute vec4 inputAttribute;
varying float dx_Position;
void main()
{
gl_Position = vec4(inputAttribute);
dx_Position = 0.0;
}
);
const std::string &fragmentShaderSource = SHADER_SOURCE
(
precision mediump float;
varying float dx_Position;
void main()
{
gl_FragColor = vec4(dx_Position, 0, 0, 1);
}
);
GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
EXPECT_NE(0u, program);
}
TEST_P(GLSLTest, ElseIfRewriting)
{
const std::string &vertexShaderSource =
"attribute vec4 a_position;\n"
"varying float v;\n"
"void main() {\n"
" gl_Position = a_position;\n"
" v = 1.0;\n"
" if (a_position.x <= 0.5) {\n"
" v = 0.0;\n"
" } else if (a_position.x >= 0.5) {\n"
" v = 2.0;\n"
" }\n"
"}\n";
const std::string &fragmentShaderSource =
"precision highp float;\n"
"varying float v;\n"
"void main() {\n"
" vec4 color = vec4(1.0, 0.0, 0.0, 1.0);\n"
" if (v >= 1.0) color = vec4(0.0, 1.0, 0.0, 1.0);\n"
" if (v >= 2.0) color = vec4(0.0, 0.0, 1.0, 1.0);\n"
" gl_FragColor = color;\n"
"}\n";
GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
ASSERT_NE(0u, program);
drawQuad(program, "a_position", 0.5f);
EXPECT_PIXEL_EQ(0, 0, 255, 0, 0, 255);
EXPECT_PIXEL_EQ(getWindowWidth()-1, 0, 0, 255, 0, 255);
}
TEST_P(GLSLTest, TwoElseIfRewriting)
{
const std::string &vertexShaderSource =
"attribute vec4 a_position;\n"
"varying float v;\n"
"void main() {\n"
" gl_Position = a_position;\n"
" if (a_position.x == 0.0) {\n"
" v = 1.0;\n"
" } else if (a_position.x > 0.5) {\n"
" v = 0.0;\n"
" } else if (a_position.x > 0.75) {\n"
" v = 0.5;\n"
" }\n"
"}\n";
const std::string &fragmentShaderSource =
"precision highp float;\n"
"varying float v;\n"
"void main() {\n"
" gl_FragColor = vec4(v, 0.0, 0.0, 1.0);\n"
"}\n";
GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
EXPECT_NE(0u, program);
}
TEST_P(GLSLTest, InvariantVaryingOut)
{
const std::string fragmentShaderSource = SHADER_SOURCE
(
precision mediump float;
varying float v_varying;
void main() { gl_FragColor = vec4(v_varying, 0, 0, 1.0); }
);
const std::string vertexShaderSource = SHADER_SOURCE
(
attribute vec4 a_position;
invariant varying float v_varying;
void main() { v_varying = a_position.x; gl_Position = a_position; }
);
GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
EXPECT_NE(0u, program);
}
TEST_P(GLSLTest, FrontFacingAndVarying)
{
EGLPlatformParameters platform = GetParam().eglParameters;
const std::string vertexShaderSource = SHADER_SOURCE
(
attribute vec4 a_position;
varying float v_varying;
void main()
{
v_varying = a_position.x;
gl_Position = a_position;
}
);
const std::string fragmentShaderSource = SHADER_SOURCE
(
precision mediump float;
varying float v_varying;
void main()
{
vec4 c;
if (gl_FrontFacing)
{
c = vec4(v_varying, 0, 0, 1.0);
}
else
{
c = vec4(0, v_varying, 0, 1.0);
}
gl_FragColor = c;
}
);
GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
// Compilation should fail on D3D11 feature level 9_3, since gl_FrontFacing isn't supported.
if (platform.renderer == EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE)
{
if (platform.majorVersion == 9 && platform.minorVersion == 3)
{
EXPECT_EQ(0u, program);
return;
}
}
// Otherwise, compilation should succeed
EXPECT_NE(0u, program);
}
TEST_P(GLSLTest, InvariantVaryingIn)
{
const std::string fragmentShaderSource = SHADER_SOURCE
(
precision mediump float;
invariant varying float v_varying;
void main() { gl_FragColor = vec4(v_varying, 0, 0, 1.0); }
);
const std::string vertexShaderSource = SHADER_SOURCE
(
attribute vec4 a_position;
varying float v_varying;
void main() { v_varying = a_position.x; gl_Position = a_position; }
);
GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
EXPECT_NE(0u, program);
}
TEST_P(GLSLTest, InvariantVaryingBoth)
{
const std::string fragmentShaderSource = SHADER_SOURCE
(
precision mediump float;
invariant varying float v_varying;
void main() { gl_FragColor = vec4(v_varying, 0, 0, 1.0); }
);
const std::string vertexShaderSource = SHADER_SOURCE
(
attribute vec4 a_position;
invariant varying float v_varying;
void main() { v_varying = a_position.x; gl_Position = a_position; }
);
GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
EXPECT_NE(0u, program);
}
TEST_P(GLSLTest, InvariantGLPosition)
{
const std::string fragmentShaderSource = SHADER_SOURCE
(
precision mediump float;
varying float v_varying;
void main() { gl_FragColor = vec4(v_varying, 0, 0, 1.0); }
);
const std::string vertexShaderSource = SHADER_SOURCE
(
attribute vec4 a_position;
invariant gl_Position;
varying float v_varying;
void main() { v_varying = a_position.x; gl_Position = a_position; }
);
GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
EXPECT_NE(0u, program);
}
TEST_P(GLSLTest, InvariantAll)
{
const std::string fragmentShaderSource = SHADER_SOURCE
(
precision mediump float;
varying float v_varying;
void main() { gl_FragColor = vec4(v_varying, 0, 0, 1.0); }
);
const std::string vertexShaderSource =
"#pragma STDGL invariant(all)\n"
"attribute vec4 a_position;\n"
"varying float v_varying;\n"
"void main() { v_varying = a_position.x; gl_Position = a_position; }\n";
GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
EXPECT_NE(0u, program);
}
TEST_P(GLSLTest, MaxVaryingVec4)
{
GLint maxVaryings = 0;
glGetIntegerv(GL_MAX_VARYING_VECTORS, &maxVaryings);
VaryingTestBase(0, 0, 0, 0, 0, 0, maxVaryings, 0, false, false, false, true);
}
TEST_P(GLSLTest, MaxMinusTwoVaryingVec4PlusTwoSpecialVariables)
{
GLint maxVaryings = 0;
glGetIntegerv(GL_MAX_VARYING_VECTORS, &maxVaryings);
// Generate shader code that uses gl_FragCoord and gl_PointCoord, two special fragment shader variables.
VaryingTestBase(0, 0, 0, 0, 0, 0, maxVaryings - 2, 0, true, true, false, true);
}
TEST_P(GLSLTest, MaxMinusTwoVaryingVec4PlusThreeSpecialVariables)
{
GLint maxVaryings = 0;
glGetIntegerv(GL_MAX_VARYING_VECTORS, &maxVaryings);
// Generate shader code that uses gl_FragCoord, gl_PointCoord and gl_PointSize.
VaryingTestBase(0, 0, 0, 0, 0, 0, maxVaryings - 2, 0, true, true, true, true);
}
TEST_P(GLSLTest, MaxVaryingVec4PlusFragCoord)
{
GLint maxVaryings = 0;
glGetIntegerv(GL_MAX_VARYING_VECTORS, &maxVaryings);
// Generate shader code that uses gl_FragCoord, a special fragment shader variables.
// This test should fail, since we are really using (maxVaryings + 1) varyings.
VaryingTestBase(0, 0, 0, 0, 0, 0, maxVaryings, 0, true, false, false, false);
}
TEST_P(GLSLTest, MaxVaryingVec4PlusPointCoord)
{
GLint maxVaryings = 0;
glGetIntegerv(GL_MAX_VARYING_VECTORS, &maxVaryings);
// Generate shader code that uses gl_FragCoord, a special fragment shader variables.
// This test should fail, since we are really using (maxVaryings + 1) varyings.
VaryingTestBase(0, 0, 0, 0, 0, 0, maxVaryings, 0, false, true, false, false);
}
TEST_P(GLSLTest, MaxVaryingVec3)
{
GLint maxVaryings = 0;
glGetIntegerv(GL_MAX_VARYING_VECTORS, &maxVaryings);
VaryingTestBase(0, 0, 0, 0, maxVaryings, 0, 0, 0, false, false, false, true);
}
TEST_P(GLSLTest, MaxVaryingVec3Array)
{
GLint maxVaryings = 0;
glGetIntegerv(GL_MAX_VARYING_VECTORS, &maxVaryings);
VaryingTestBase(0, 0, 0, 0, 0, maxVaryings / 2, 0, 0, false, false, false, true);
}
// Disabled because of a failure in D3D9
TEST_P(GLSLTest, MaxVaryingVec3AndOneFloat)
{
if (isD3D9())
{
std::cout << "Test disabled on D3D9." << std::endl;
return;
}
GLint maxVaryings = 0;
glGetIntegerv(GL_MAX_VARYING_VECTORS, &maxVaryings);
VaryingTestBase(1, 0, 0, 0, maxVaryings, 0, 0, 0, false, false, false, true);
}
// Disabled because of a failure in D3D9
TEST_P(GLSLTest, MaxVaryingVec3ArrayAndOneFloatArray)
{
if (isD3D9())
{
std::cout << "Test disabled on D3D9." << std::endl;
return;
}
GLint maxVaryings = 0;
glGetIntegerv(GL_MAX_VARYING_VECTORS, &maxVaryings);
VaryingTestBase(0, 1, 0, 0, 0, maxVaryings / 2, 0, 0, false, false, false, true);
}
// Disabled because of a failure in D3D9
TEST_P(GLSLTest, TwiceMaxVaryingVec2)
{
if (isD3D9())
{
std::cout << "Test disabled on D3D9." << std::endl;
return;
}
GLint maxVaryings = 0;
glGetIntegerv(GL_MAX_VARYING_VECTORS, &maxVaryings);
VaryingTestBase(0, 0, 2 * maxVaryings, 0, 0, 0, 0, 0, false, false, false, true);
}
// Disabled because of a failure in D3D9
TEST_P(GLSLTest, MaxVaryingVec2Arrays)
{
if (isD3DSM3())
{
std::cout << "Test disabled on SM3." << std::endl;
return;
}
GLint maxVaryings = 0;
glGetIntegerv(GL_MAX_VARYING_VECTORS, &maxVaryings);
VaryingTestBase(0, 0, 0, maxVaryings, 0, 0, 0, 0, false, false, false, true);
}
TEST_P(GLSLTest, MaxPlusOneVaryingVec3)
{
GLint maxVaryings = 0;
glGetIntegerv(GL_MAX_VARYING_VECTORS, &maxVaryings);
VaryingTestBase(0, 0, 0, 0, maxVaryings + 1, 0, 0, 0, false, false, false, false);
}
TEST_P(GLSLTest, MaxPlusOneVaryingVec3Array)
{
GLint maxVaryings = 0;
glGetIntegerv(GL_MAX_VARYING_VECTORS, &maxVaryings);
VaryingTestBase(0, 0, 0, 0, 0, maxVaryings / 2 + 1, 0, 0, false, false, false, false);
}
TEST_P(GLSLTest, MaxVaryingVec3AndOneVec2)
{
GLint maxVaryings = 0;
glGetIntegerv(GL_MAX_VARYING_VECTORS, &maxVaryings);
VaryingTestBase(0, 0, 1, 0, maxVaryings, 0, 0, 0, false, false, false, false);
}
TEST_P(GLSLTest, MaxPlusOneVaryingVec2)
{
GLint maxVaryings = 0;
glGetIntegerv(GL_MAX_VARYING_VECTORS, &maxVaryings);
VaryingTestBase(0, 0, 2 * maxVaryings + 1, 0, 0, 0, 0, 0, false, false, false, false);
}
TEST_P(GLSLTest, MaxVaryingVec3ArrayAndMaxPlusOneFloatArray)
{
GLint maxVaryings = 0;
glGetIntegerv(GL_MAX_VARYING_VECTORS, &maxVaryings);
VaryingTestBase(0, maxVaryings / 2 + 1, 0, 0, 0, 0, 0, maxVaryings / 2, false, false, false, false);
}
// Verify shader source with a fixed length that is less than the null-terminated length will compile.
TEST_P(GLSLTest, FixedShaderLength)
{
GLuint shader = glCreateShader(GL_FRAGMENT_SHADER);
const std::string appendGarbage = "abcasdfasdfasdfasdfasdf";
const std::string source = "void main() { gl_FragColor = vec4(0, 0, 0, 0); }" + appendGarbage;
const char *sourceArray[1] = { source.c_str() };
GLint lengths[1] = { static_cast<GLint>(source.length() - appendGarbage.length()) };
glShaderSource(shader, static_cast<GLsizei>(ArraySize(sourceArray)), sourceArray, lengths);
glCompileShader(shader);
GLint compileResult;
glGetShaderiv(shader, GL_COMPILE_STATUS, &compileResult);
EXPECT_NE(compileResult, 0);
}
// Verify that a negative shader source length is treated as a null-terminated length.
TEST_P(GLSLTest, NegativeShaderLength)
{
GLuint shader = glCreateShader(GL_FRAGMENT_SHADER);
const char *sourceArray[1] = { "void main() { gl_FragColor = vec4(0, 0, 0, 0); }" };
GLint lengths[1] = { -10 };
glShaderSource(shader, static_cast<GLsizei>(ArraySize(sourceArray)), sourceArray, lengths);
glCompileShader(shader);
GLint compileResult;
glGetShaderiv(shader, GL_COMPILE_STATUS, &compileResult);
EXPECT_NE(compileResult, 0);
}
// Verify that a length array with mixed positive and negative values compiles.
TEST_P(GLSLTest, MixedShaderLengths)
{
GLuint shader = glCreateShader(GL_FRAGMENT_SHADER);
const char *sourceArray[] =
{
"void main()",
"{",
" gl_FragColor = vec4(0, 0, 0, 0);",
"}",
};
GLint lengths[] =
{
-10,
1,
static_cast<GLint>(strlen(sourceArray[2])),
-1,
};
ASSERT_EQ(ArraySize(sourceArray), ArraySize(lengths));
glShaderSource(shader, static_cast<GLsizei>(ArraySize(sourceArray)), sourceArray, lengths);
glCompileShader(shader);
GLint compileResult;
glGetShaderiv(shader, GL_COMPILE_STATUS, &compileResult);
EXPECT_NE(compileResult, 0);
}
// Verify that zero-length shader source does not affect shader compilation.
TEST_P(GLSLTest, ZeroShaderLength)
{
GLuint shader = glCreateShader(GL_FRAGMENT_SHADER);
const char *sourceArray[] =
{
"adfasdf",
"34534",
"void main() { gl_FragColor = vec4(0, 0, 0, 0); }",
"",
"asdfasdfsdsdf",
};
GLint lengths[] =
{
0,
0,
-1,
0,
0,
};
ASSERT_EQ(ArraySize(sourceArray), ArraySize(lengths));
glShaderSource(shader, static_cast<GLsizei>(ArraySize(sourceArray)), sourceArray, lengths);
glCompileShader(shader);
GLint compileResult;
glGetShaderiv(shader, GL_COMPILE_STATUS, &compileResult);
EXPECT_NE(compileResult, 0);
}
// Tests that bad index expressions don't crash ANGLE's translator.
// https://code.google.com/p/angleproject/issues/detail?id=857
TEST_P(GLSLTest, BadIndexBug)
{
const std::string &fragmentShaderSourceVec =
"precision mediump float;\n"
"uniform vec4 uniformVec;\n"
"void main()\n"
"{\n"
" gl_FragColor = vec4(uniformVec[int()]);\n"
"}";
GLuint shader = CompileShader(GL_FRAGMENT_SHADER, fragmentShaderSourceVec);
EXPECT_EQ(0u, shader);
if (shader != 0)
{
glDeleteShader(shader);
}
const std::string &fragmentShaderSourceMat =
"precision mediump float;\n"
"uniform mat4 uniformMat;\n"
"void main()\n"
"{\n"
" gl_FragColor = vec4(uniformMat[int()]);\n"
"}";
shader = CompileShader(GL_FRAGMENT_SHADER, fragmentShaderSourceMat);
EXPECT_EQ(0u, shader);
if (shader != 0)
{
glDeleteShader(shader);
}
const std::string &fragmentShaderSourceArray =
"precision mediump float;\n"
"uniform vec4 uniformArray;\n"
"void main()\n"
"{\n"
" gl_FragColor = vec4(uniformArray[int()]);\n"
"}";
shader = CompileShader(GL_FRAGMENT_SHADER, fragmentShaderSourceArray);
EXPECT_EQ(0u, shader);
if (shader != 0)
{
glDeleteShader(shader);
}
}
// Test that structs defined in uniforms are translated correctly.
TEST_P(GLSLTest, StructSpecifiersUniforms)
{
const std::string fragmentShaderSource = SHADER_SOURCE
(
precision mediump float;
uniform struct S { float field;} s;
void main()
{
gl_FragColor = vec4(1, 0, 0, 1);
gl_FragColor.a += s.field;
}
);
GLuint program = CompileProgram(mSimpleVSSource, fragmentShaderSource);
EXPECT_NE(0u, program);
}
// Test that gl_DepthRange is not stored as a uniform location. Since uniforms
// beginning with "gl_" are filtered out by our validation logic, we must
// bypass the validation to test the behaviour of the implementation.
// (note this test is still Impl-independent)
TEST_P(GLSLTest, DepthRangeUniforms)
{
const std::string fragmentShaderSource = SHADER_SOURCE
(
precision mediump float;
void main()
{
gl_FragColor = vec4(gl_DepthRange.near, gl_DepthRange.far, gl_DepthRange.diff, 1);
}
);
GLuint program = CompileProgram(mSimpleVSSource, fragmentShaderSource);
EXPECT_NE(0u, program);
// dive into the ANGLE internals, so we can bypass validation.
gl::Context *context = reinterpret_cast<gl::Context *>(getEGLWindow()->getContext());
gl::Program *glProgram = context->getProgram(program);
GLint nearIndex = glProgram->getUniformLocation("gl_DepthRange.near");
EXPECT_EQ(-1, nearIndex);
// Test drawing does not throw an exception.
drawQuad(program, "inputAttribute", 0.5f);
EXPECT_GL_NO_ERROR();
glDeleteProgram(program);
}
// Covers the WebGL test 'glsl/bugs/pow-of-small-constant-in-user-defined-function'
// See https://code.google.com/p/angleproject/issues/detail?id=851
// TODO(jmadill): ANGLE constant folding can fix this
TEST_P(GLSLTest, DISABLED_PowOfSmallConstant)
{
const std::string &fragmentShaderSource = SHADER_SOURCE
(
precision highp float;
float fun(float arg)
{
// These values are still easily within the highp range.
// The minimum range in terms of 10's exponent is around -19 to 19, and IEEE-754 single precision range is higher than that.
return pow(arg, 2.0);
}
void main()
{
// Note that the bug did not reproduce if an uniform was passed to the function instead of a constant,
// or if the expression was moved outside the user-defined function.
const float a = 1.0e-6;
float b = 1.0e12 * fun(a);
if (abs(b - 1.0) < 0.01)
{
gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0); // green
}
else
{
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); // red
}
}
);
GLuint program = CompileProgram(mSimpleVSSource, fragmentShaderSource);
EXPECT_NE(0u, program);
drawQuad(program, "inputAttribute", 0.5f);
EXPECT_PIXEL_EQ(0, 0, 0, 255, 0, 255);
EXPECT_GL_NO_ERROR();
}
// Test that fragment shaders which contain non-constant loop indexers and compiled for FL9_3 and
// below
// fail with a specific error message.
// Additionally test that the same fragment shader compiles successfully with feature levels greater
// than FL9_3.
TEST_P(GLSLTest, LoopIndexingValidation)
{
const std::string fragmentShaderSource = SHADER_SOURCE
(
precision mediump float;
uniform float loopMax;
void main()
{
gl_FragColor = vec4(1, 0, 0, 1);
for (float l = 0.0; l < loopMax; l++)
{
if (loopMax > 3.0)
{
gl_FragColor.a += 0.1;
}
}
}
);
GLuint shader = glCreateShader(GL_FRAGMENT_SHADER);
const char *sourceArray[1] = {fragmentShaderSource.c_str()};
glShaderSource(shader, 1, sourceArray, nullptr);
glCompileShader(shader);
GLint compileResult;
glGetShaderiv(shader, GL_COMPILE_STATUS, &compileResult);
// If the test is configured to run limited to Feature Level 9_3, then it is
// assumed that shader compilation will fail with an expected error message containing
// "Loop index cannot be compared with non-constant expression"
if ((GetParam() == ES2_D3D11_FL9_3() || GetParam() == ES2_D3D9()))
{
if (compileResult != 0)
{
FAIL() << "Shader compilation succeeded, expected failure";
}
else
{
GLint infoLogLength;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLogLength);
std::string infoLog;
infoLog.resize(infoLogLength);
glGetShaderInfoLog(shader, static_cast<GLsizei>(infoLog.size()), NULL, &infoLog[0]);
if (infoLog.find("Loop index cannot be compared with non-constant expression") ==
std::string::npos)
{
FAIL() << "Shader compilation failed with unexpected error message";
}
}
}
else
{
EXPECT_NE(0, compileResult);
}
if (shader != 0)
{
glDeleteShader(shader);
}
}
// Tests that the maximum uniforms count returned from querying GL_MAX_VERTEX_UNIFORM_VECTORS
// can actually be used.
TEST_P(GLSLTest, VerifyMaxVertexUniformVectors)
{
int maxUniforms = 10000;
glGetIntegerv(GL_MAX_VERTEX_UNIFORM_VECTORS, &maxUniforms);
EXPECT_GL_NO_ERROR();
std::cout << "Validating GL_MAX_VERTEX_UNIFORM_VECTORS = " << maxUniforms << std::endl;
CompileGLSLWithUniformsAndSamplers(maxUniforms, 0, 0, 0, true);
}
// Tests that the maximum uniforms count returned from querying GL_MAX_VERTEX_UNIFORM_VECTORS
// can actually be used along with the maximum number of texture samplers.
TEST_P(GLSLTest, VerifyMaxVertexUniformVectorsWithSamplers)
{
int maxUniforms = 10000;
glGetIntegerv(GL_MAX_VERTEX_UNIFORM_VECTORS, &maxUniforms);
EXPECT_GL_NO_ERROR();
std::cout << "Validating GL_MAX_VERTEX_UNIFORM_VECTORS = " << maxUniforms << std::endl;
int maxTextureImageUnits = 0;
glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, &maxTextureImageUnits);
CompileGLSLWithUniformsAndSamplers(maxUniforms, 0, maxTextureImageUnits, 0, true);
}
// Tests that the maximum uniforms count + 1 from querying GL_MAX_VERTEX_UNIFORM_VECTORS
// fails shader compilation.
TEST_P(GLSLTest, VerifyMaxVertexUniformVectorsExceeded)
{
int maxUniforms = 10000;
glGetIntegerv(GL_MAX_VERTEX_UNIFORM_VECTORS, &maxUniforms);
EXPECT_GL_NO_ERROR();
std::cout << "Validating GL_MAX_VERTEX_UNIFORM_VECTORS + 1 = " << maxUniforms + 1 << std::endl;
CompileGLSLWithUniformsAndSamplers(maxUniforms + 1, 0, 0, 0, false);
}
// Tests that the maximum uniforms count returned from querying GL_MAX_FRAGMENT_UNIFORM_VECTORS
// can actually be used.
TEST_P(GLSLTest, VerifyMaxFragmentUniformVectors)
{
int maxUniforms = 10000;
glGetIntegerv(GL_MAX_FRAGMENT_UNIFORM_VECTORS, &maxUniforms);
EXPECT_GL_NO_ERROR();
std::cout << "Validating GL_MAX_FRAGMENT_UNIFORM_VECTORS = " << maxUniforms << std::endl;
CompileGLSLWithUniformsAndSamplers(0, maxUniforms, 0, 0, true);
}
// Tests that the maximum uniforms count returned from querying GL_MAX_FRAGMENT_UNIFORM_VECTORS
// can actually be used along with the maximum number of texture samplers.
TEST_P(GLSLTest, VerifyMaxFragmentUniformVectorsWithSamplers)
{
int maxUniforms = 10000;
glGetIntegerv(GL_MAX_FRAGMENT_UNIFORM_VECTORS, &maxUniforms);
EXPECT_GL_NO_ERROR();
int maxTextureImageUnits = 0;
glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureImageUnits);
CompileGLSLWithUniformsAndSamplers(0, maxUniforms, 0, maxTextureImageUnits, true);
}
// Tests that the maximum uniforms count + 1 from querying GL_MAX_FRAGMENT_UNIFORM_VECTORS
// fails shader compilation.
TEST_P(GLSLTest, VerifyMaxFragmentUniformVectorsExceeded)
{
int maxUniforms = 10000;
glGetIntegerv(GL_MAX_FRAGMENT_UNIFORM_VECTORS, &maxUniforms);
EXPECT_GL_NO_ERROR();
std::cout << "Validating GL_MAX_FRAGMENT_UNIFORM_VECTORS + 1 = " << maxUniforms + 1
<< std::endl;
CompileGLSLWithUniformsAndSamplers(0, maxUniforms + 1, 0, 0, false);
}
// Use this to select which configurations (e.g. which renderer, which GLES major version) these tests should be run against.
ANGLE_INSTANTIATE_TEST(GLSLTest, ES2_D3D9(), ES2_D3D11(), ES2_D3D11_FL9_3());
// Use this to select which configurations (e.g. which renderer, which GLES major version) these tests should be run against.
ANGLE_INSTANTIATE_TEST(GLSLTest_ES3, ES3_D3D11());