Hash :
76e6565e
Author :
Date :
2017-03-27T14:58:02
Validate clear attachment formats match color clear types. TEST=conformance2/rendering/clear-func-buffer-type-match.html BUG=angleproject:1954 Change-Id: Iefeb38041608f11781f87aadb8611737ba2ee96f Reviewed-on: https://chromium-review.googlesource.com/461270 Commit-Queue: Geoff Lang <geofflang@chromium.org> Reviewed-by: Corentin Wallez <cwallez@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 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392
//
// 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.
//
// WebGLCompatibilityTest.cpp : Tests of the GL_ANGLE_webgl_compatibility extension.
#include "test_utils/ANGLETest.h"
#include "test_utils/gl_raii.h"
namespace
{
bool ConstantColorAndAlphaBlendFunctions(GLenum first, GLenum second)
{
return (first == GL_CONSTANT_COLOR || first == GL_ONE_MINUS_CONSTANT_COLOR) &&
(second == GL_CONSTANT_ALPHA || second == GL_ONE_MINUS_CONSTANT_ALPHA);
}
void CheckBlendFunctions(GLenum src, GLenum dst)
{
if (ConstantColorAndAlphaBlendFunctions(src, dst) ||
ConstantColorAndAlphaBlendFunctions(dst, src))
{
EXPECT_GL_ERROR(GL_INVALID_OPERATION);
}
else
{
ASSERT_GL_NO_ERROR();
}
}
} // namespace
namespace angle
{
class WebGLCompatibilityTest : public ANGLETest
{
protected:
WebGLCompatibilityTest()
{
setWindowWidth(128);
setWindowHeight(128);
setConfigRedBits(8);
setConfigGreenBits(8);
setConfigBlueBits(8);
setConfigAlphaBits(8);
setWebGLCompatibilityEnabled(true);
}
void SetUp() override
{
ANGLETest::SetUp();
glRequestExtensionANGLE = reinterpret_cast<PFNGLREQUESTEXTENSIONANGLEPROC>(
eglGetProcAddress("glRequestExtensionANGLE"));
}
// Called from RenderingFeedbackLoopWithDrawBuffersEXT.
void drawBuffersEXTFeedbackLoop(GLuint program,
const std::array<GLenum, 2> &drawBuffers,
GLenum expectedError);
// Called from RenderingFeedbackLoopWithDrawBuffers.
void drawBuffersFeedbackLoop(GLuint program,
const std::array<GLenum, 2> &drawBuffers,
GLenum expectedError);
PFNGLREQUESTEXTENSIONANGLEPROC glRequestExtensionANGLE = nullptr;
};
class WebGL2CompatibilityTest : public WebGLCompatibilityTest
{
};
// Context creation would fail if EGL_ANGLE_create_context_webgl_compatibility was not available so
// the GL extension should always be present
TEST_P(WebGLCompatibilityTest, ExtensionStringExposed)
{
EXPECT_TRUE(extensionEnabled("GL_ANGLE_webgl_compatibility"));
}
// Verify that all extension entry points are available
TEST_P(WebGLCompatibilityTest, EntryPoints)
{
if (extensionEnabled("GL_ANGLE_request_extension"))
{
EXPECT_NE(nullptr, eglGetProcAddress("glRequestExtensionANGLE"));
}
}
// WebGL 1 allows GL_DEPTH_STENCIL_ATTACHMENT as a valid binding point. Make sure it is usable,
// even in ES2 contexts.
TEST_P(WebGLCompatibilityTest, DepthStencilBindingPoint)
{
GLRenderbuffer renderbuffer;
glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer.get());
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, 32, 32);
GLFramebuffer framebuffer;
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer.get());
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER,
renderbuffer.get());
EXPECT_GL_NO_ERROR();
}
// Test that attempting to enable an extension that doesn't exist generates GL_INVALID_OPERATION
TEST_P(WebGLCompatibilityTest, EnableExtensionValidation)
{
glRequestExtensionANGLE("invalid_extension_string");
EXPECT_GL_ERROR(GL_INVALID_OPERATION);
}
// Test enabling the GL_OES_element_index_uint extension
TEST_P(WebGLCompatibilityTest, EnableExtensionUintIndices)
{
if (getClientMajorVersion() != 2)
{
// This test only works on ES2 where uint indices are not available by default
return;
}
EXPECT_FALSE(extensionEnabled("GL_OES_element_index_uint"));
GLBuffer indexBuffer;
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer.get());
GLuint data[] = {0, 1, 2, 1, 3, 2};
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(data), data, GL_STATIC_DRAW);
ANGLE_GL_PROGRAM(program, "void main() { gl_Position = vec4(0, 0, 0, 1); }",
"void main() { gl_FragColor = vec4(0, 1, 0, 1); }")
glUseProgram(program.get());
glDrawElements(GL_TRIANGLES, 2, GL_UNSIGNED_INT, nullptr);
EXPECT_GL_ERROR(GL_INVALID_ENUM);
if (extensionRequestable("GL_OES_element_index_uint"))
{
glRequestExtensionANGLE("GL_OES_element_index_uint");
EXPECT_GL_NO_ERROR();
EXPECT_TRUE(extensionEnabled("GL_OES_element_index_uint"));
glDrawElements(GL_TRIANGLES, 2, GL_UNSIGNED_INT, nullptr);
EXPECT_GL_NO_ERROR();
}
}
// Test enabling the GL_EXT_texture_filter_anisotropic extension
TEST_P(WebGLCompatibilityTest, EnableExtensionTextureFilterAnisotropic)
{
EXPECT_FALSE(extensionEnabled("GL_EXT_texture_filter_anisotropic"));
GLfloat maxAnisotropy = 0.0f;
glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &maxAnisotropy);
EXPECT_GL_ERROR(GL_INVALID_ENUM);
GLTexture texture;
glBindTexture(GL_TEXTURE_2D, texture.get());
ASSERT_GL_NO_ERROR();
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 1.0f);
EXPECT_GL_ERROR(GL_INVALID_ENUM);
GLfloat currentAnisotropy = 0.0f;
glGetTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, ¤tAnisotropy);
EXPECT_GL_ERROR(GL_INVALID_ENUM);
if (extensionRequestable("GL_EXT_texture_filter_anisotropic"))
{
glRequestExtensionANGLE("GL_EXT_texture_filter_anisotropic");
EXPECT_GL_NO_ERROR();
EXPECT_TRUE(extensionEnabled("GL_EXT_texture_filter_anisotropic"));
glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &maxAnisotropy);
ASSERT_GL_NO_ERROR();
EXPECT_GE(maxAnisotropy, 2.0f);
glGetTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, ¤tAnisotropy);
ASSERT_GL_NO_ERROR();
EXPECT_EQ(1.0f, currentAnisotropy);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 2.0f);
ASSERT_GL_NO_ERROR();
}
}
// Verify that shaders are of a compatible spec when the extension is enabled.
TEST_P(WebGLCompatibilityTest, ExtensionCompilerSpec)
{
EXPECT_TRUE(extensionEnabled("GL_ANGLE_webgl_compatibility"));
// Use of reserved _webgl prefix should fail when the shader specification is for WebGL.
const std::string &vert =
"struct Foo {\n"
" int _webgl_bar;\n"
"};\n"
"void main()\n"
"{\n"
" Foo foo = Foo(1);\n"
"}";
// Default fragement shader.
const std::string &frag =
"void main()\n"
"{\n"
" gl_FragColor = vec4(1.0,0.0,0.0,1.0);\n"
"}";
GLuint program = CompileProgram(vert, frag);
EXPECT_EQ(0u, program);
glDeleteProgram(program);
}
// Test that client-side array buffers are forbidden in WebGL mode
TEST_P(WebGLCompatibilityTest, ForbidsClientSideArrayBuffer)
{
const std::string &vert =
"attribute vec3 a_pos;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(a_pos, 1.0);\n"
"}\n";
const std::string &frag =
"precision highp float;\n"
"void main()\n"
"{\n"
" gl_FragColor = vec4(1.0);\n"
"}\n";
ANGLE_GL_PROGRAM(program, vert, frag);
GLint posLocation = glGetAttribLocation(program.get(), "a_pos");
ASSERT_NE(-1, posLocation);
glUseProgram(program.get());
const auto &vertices = GetQuadVertices();
glVertexAttribPointer(posLocation, 3, GL_FLOAT, GL_FALSE, 4, vertices.data());
glEnableVertexAttribArray(posLocation);
ASSERT_GL_NO_ERROR();
glDrawArrays(GL_TRIANGLES, 0, 6);
EXPECT_GL_ERROR(GL_INVALID_OPERATION);
}
// Test that client-side element array buffers are forbidden in WebGL mode
TEST_P(WebGLCompatibilityTest, ForbidsClientSideElementBuffer)
{
const std::string &vert =
"attribute vec3 a_pos;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(a_pos, 1.0);\n"
"}\n";
const std::string &frag =
"precision highp float;\n"
"void main()\n"
"{\n"
" gl_FragColor = vec4(1.0);\n"
"}\n";
ANGLE_GL_PROGRAM(program, vert, frag);
GLint posLocation = glGetAttribLocation(program.get(), "a_pos");
ASSERT_NE(-1, posLocation);
glUseProgram(program.get());
const auto &vertices = GetQuadVertices();
GLBuffer vertexBuffer;
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer.get());
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices[0]) * vertices.size(), vertices.data(),
GL_STATIC_DRAW);
glVertexAttribPointer(posLocation, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(posLocation);
ASSERT_GL_NO_ERROR();
// Use the pointer with value of 1 for indices instead of an actual pointer because WebGL also
// enforces that the top bit of indices must be 0 (i.e. offset >= 0) and would generate
// GL_INVALID_VALUE in that case. Using a null pointer gets caught by another check.
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, reinterpret_cast<const void*>(intptr_t(1)));
EXPECT_GL_ERROR(GL_INVALID_OPERATION);
}
// Tests the WebGL requirement of having the same stencil mask, writemask and ref for fron and back
TEST_P(WebGLCompatibilityTest, RequiresSameStencilMaskAndRef)
{
// Run the test in an FBO to make sure we have some stencil bits.
GLRenderbuffer renderbuffer;
glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer.get());
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, 32, 32);
GLFramebuffer framebuffer;
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer.get());
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER,
renderbuffer.get());
ANGLE_GL_PROGRAM(program, "void main() { gl_Position = vec4(0, 0, 0, 1); }",
"void main() { gl_FragColor = vec4(0, 1, 0, 1); }")
glUseProgram(program.get());
ASSERT_GL_NO_ERROR();
// Having ref and mask the same for front and back is valid.
glStencilMask(255);
glStencilFunc(GL_ALWAYS, 0, 255);
glDrawArrays(GL_TRIANGLES, 0, 6);
ASSERT_GL_NO_ERROR();
// Having a different front - back write mask generates an error.
glStencilMaskSeparate(GL_FRONT, 1);
glDrawArrays(GL_TRIANGLES, 0, 6);
EXPECT_GL_ERROR(GL_INVALID_OPERATION);
// Setting both write masks separately to the same value is valid.
glStencilMaskSeparate(GL_BACK, 1);
glDrawArrays(GL_TRIANGLES, 0, 6);
ASSERT_GL_NO_ERROR();
// Having a different stencil front - back mask generates an error
glStencilFuncSeparate(GL_FRONT, GL_ALWAYS, 0, 1);
glDrawArrays(GL_TRIANGLES, 0, 6);
EXPECT_GL_ERROR(GL_INVALID_OPERATION);
// Setting both masks separately to the same value is valid.
glStencilFuncSeparate(GL_BACK, GL_ALWAYS, 0, 1);
glDrawArrays(GL_TRIANGLES, 0, 6);
ASSERT_GL_NO_ERROR();
// Having a different stencil front - back reference generates an error
glStencilFuncSeparate(GL_FRONT, GL_ALWAYS, 255, 1);
glDrawArrays(GL_TRIANGLES, 0, 6);
EXPECT_GL_ERROR(GL_INVALID_OPERATION);
// Setting both references separately to the same value is valid.
glStencilFuncSeparate(GL_BACK, GL_ALWAYS, 255, 1);
glDrawArrays(GL_TRIANGLES, 0, 6);
ASSERT_GL_NO_ERROR();
// Using different stencil funcs, everything being equal is valid.
glStencilFuncSeparate(GL_BACK, GL_NEVER, 255, 1);
glDrawArrays(GL_TRIANGLES, 0, 6);
ASSERT_GL_NO_ERROR();
}
// Test that GL_FIXED is forbidden
TEST_P(WebGLCompatibilityTest, ForbidsGLFixed)
{
GLBuffer buffer;
glBindBuffer(GL_ARRAY_BUFFER, buffer.get());
glBufferData(GL_ARRAY_BUFFER, 16, nullptr, GL_STATIC_DRAW);
glVertexAttribPointer(0, 1, GL_FLOAT, GL_FALSE, 0, nullptr);
ASSERT_GL_NO_ERROR();
glVertexAttribPointer(0, 1, GL_FIXED, GL_FALSE, 0, nullptr);
EXPECT_GL_ERROR(GL_INVALID_ENUM);
}
// Test the WebGL limit of 255 for the attribute stride
TEST_P(WebGLCompatibilityTest, MaxStride)
{
GLBuffer buffer;
glBindBuffer(GL_ARRAY_BUFFER, buffer.get());
glBufferData(GL_ARRAY_BUFFER, 1024, nullptr, GL_STATIC_DRAW);
glVertexAttribPointer(0, 1, GL_UNSIGNED_BYTE, GL_FALSE, 255, nullptr);
ASSERT_GL_NO_ERROR();
glVertexAttribPointer(0, 1, GL_UNSIGNED_BYTE, GL_FALSE, 256, nullptr);
EXPECT_GL_ERROR(GL_INVALID_VALUE);
}
// Test the checks for OOB reads in the vertex buffers, non-instanced version
TEST_P(WebGLCompatibilityTest, DrawArraysBufferOutOfBoundsNonInstanced)
{
const std::string &vert =
"attribute float a_pos;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(a_pos, a_pos, a_pos, 1.0);\n"
"}\n";
const std::string &frag =
"precision highp float;\n"
"void main()\n"
"{\n"
" gl_FragColor = vec4(1.0);\n"
"}\n";
ANGLE_GL_PROGRAM(program, vert, frag);
GLint posLocation = glGetAttribLocation(program.get(), "a_pos");
ASSERT_NE(-1, posLocation);
glUseProgram(program.get());
GLBuffer buffer;
glBindBuffer(GL_ARRAY_BUFFER, buffer.get());
glBufferData(GL_ARRAY_BUFFER, 16, nullptr, GL_STATIC_DRAW);
glEnableVertexAttribArray(posLocation);
const uint8_t* zeroOffset = nullptr;
// Test touching the last element is valid.
glVertexAttribPointer(0, 1, GL_UNSIGNED_BYTE, GL_FALSE, 0, zeroOffset + 12);
glDrawArrays(GL_POINTS, 0, 4);
ASSERT_GL_NO_ERROR();
// Test touching the last element + 1 is invalid.
glVertexAttribPointer(0, 1, GL_UNSIGNED_BYTE, GL_FALSE, 0, zeroOffset + 13);
glDrawArrays(GL_POINTS, 0, 4);
EXPECT_GL_ERROR(GL_INVALID_OPERATION);
// Test touching the last element is valid, using a stride.
glVertexAttribPointer(0, 1, GL_UNSIGNED_BYTE, GL_FALSE, 2, zeroOffset + 9);
glDrawArrays(GL_POINTS, 0, 4);
ASSERT_GL_NO_ERROR();
// Test touching the last element + 1 is invalid, using a stride.
glVertexAttribPointer(0, 1, GL_UNSIGNED_BYTE, GL_FALSE, 2, zeroOffset + 10);
glDrawArrays(GL_POINTS, 0, 4);
EXPECT_GL_ERROR(GL_INVALID_OPERATION);
// Test any offset is valid if no vertices are drawn.
glVertexAttribPointer(0, 1, GL_UNSIGNED_BYTE, GL_FALSE, 0, zeroOffset + 32);
glDrawArrays(GL_POINTS, 0, 0);
ASSERT_GL_NO_ERROR();
}
// Test the checks for OOB reads in the index buffer
TEST_P(WebGLCompatibilityTest, DrawElementsBufferOutOfBoundsInIndexBuffer)
{
const std::string &vert =
"attribute float a_pos;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(a_pos, a_pos, a_pos, 1.0);\n"
"}\n";
const std::string &frag =
"precision highp float;\n"
"void main()\n"
"{\n"
" gl_FragColor = vec4(1.0);\n"
"}\n";
ANGLE_GL_PROGRAM(program, vert, frag);
GLint posLocation = glGetAttribLocation(program.get(), "a_pos");
ASSERT_NE(-1, posLocation);
glUseProgram(program.get());
GLBuffer vertexBuffer;
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer.get());
glBufferData(GL_ARRAY_BUFFER, 16, nullptr, GL_STATIC_DRAW);
glEnableVertexAttribArray(posLocation);
const uint8_t *zeroOffset = nullptr;
const uint8_t zeroIndices[] = {0, 0, 0, 0, 0, 0, 0, 0};
glVertexAttribPointer(0, 1, GL_UNSIGNED_BYTE, GL_FALSE, 0, zeroOffset);
GLBuffer indexBuffer;
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer.get());
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(zeroIndices), zeroIndices, GL_STATIC_DRAW);
ASSERT_GL_NO_ERROR();
// Test touching the last index is valid
glDrawElements(GL_POINTS, 4, GL_UNSIGNED_BYTE, zeroOffset + 4);
ASSERT_GL_NO_ERROR();
// Test touching the last + 1 element is invalid
glDrawElements(GL_POINTS, 4, GL_UNSIGNED_BYTE, zeroOffset + 5);
EXPECT_GL_ERROR(GL_INVALID_OPERATION);
// Test any offset if valid if count is zero
glDrawElements(GL_POINTS, 0, GL_UNSIGNED_BYTE, zeroOffset + 42);
ASSERT_GL_NO_ERROR();
// Test touching the first index is valid
glDrawElements(GL_POINTS, 4, GL_UNSIGNED_BYTE, zeroOffset + 4);
ASSERT_GL_NO_ERROR();
// Test touching the first - 1 index is invalid
// The error ha been specified to be INVALID_VALUE instead of INVALID_OPERATION because it was
// the historic behavior of WebGL implementations
glDrawElements(GL_POINTS, 4, GL_UNSIGNED_BYTE, zeroOffset - 1);
EXPECT_GL_ERROR(GL_INVALID_VALUE);
}
// Test depth range with 'near' more or less than 'far.'
TEST_P(WebGLCompatibilityTest, DepthRange)
{
glDepthRangef(0, 1);
ASSERT_GL_NO_ERROR();
glDepthRangef(.5, .5);
ASSERT_GL_NO_ERROR();
glDepthRangef(1, 0);
EXPECT_GL_ERROR(GL_INVALID_OPERATION);
}
// Test all blend function combinations.
// In WebGL it is invalid to combine constant color with constant alpha.
TEST_P(WebGLCompatibilityTest, BlendWithConstantColor)
{
constexpr GLenum srcFunc[] = {
GL_ZERO,
GL_ONE,
GL_SRC_COLOR,
GL_ONE_MINUS_SRC_COLOR,
GL_DST_COLOR,
GL_ONE_MINUS_DST_COLOR,
GL_SRC_ALPHA,
GL_ONE_MINUS_SRC_ALPHA,
GL_DST_ALPHA,
GL_ONE_MINUS_DST_ALPHA,
GL_CONSTANT_COLOR,
GL_ONE_MINUS_CONSTANT_COLOR,
GL_CONSTANT_ALPHA,
GL_ONE_MINUS_CONSTANT_ALPHA,
GL_SRC_ALPHA_SATURATE,
};
constexpr GLenum dstFunc[] = {
GL_ZERO, GL_ONE,
GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR,
GL_DST_COLOR, GL_ONE_MINUS_DST_COLOR,
GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA,
GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA,
GL_CONSTANT_COLOR, GL_ONE_MINUS_CONSTANT_COLOR,
GL_CONSTANT_ALPHA, GL_ONE_MINUS_CONSTANT_ALPHA,
};
for (GLenum src : srcFunc)
{
for (GLenum dst : dstFunc)
{
glBlendFunc(src, dst);
CheckBlendFunctions(src, dst);
glBlendFuncSeparate(src, dst, GL_ONE, GL_ONE);
CheckBlendFunctions(src, dst);
}
}
}
// Test the checks for OOB reads in the vertex buffers, instanced version
TEST_P(WebGL2CompatibilityTest, DrawArraysBufferOutOfBoundsInstanced)
{
const std::string &vert =
"attribute float a_pos;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(a_pos, a_pos, a_pos, 1.0);\n"
"}\n";
const std::string &frag =
"precision highp float;\n"
"void main()\n"
"{\n"
" gl_FragColor = vec4(1.0);\n"
"}\n";
ANGLE_GL_PROGRAM(program, vert, frag);
GLint posLocation = glGetAttribLocation(program.get(), "a_pos");
ASSERT_NE(-1, posLocation);
glUseProgram(program.get());
GLBuffer buffer;
glBindBuffer(GL_ARRAY_BUFFER, buffer.get());
glBufferData(GL_ARRAY_BUFFER, 16, nullptr, GL_STATIC_DRAW);
glEnableVertexAttribArray(posLocation);
glVertexAttribDivisor(posLocation, 1);
const uint8_t* zeroOffset = nullptr;
// Test touching the last element is valid.
glVertexAttribPointer(0, 1, GL_UNSIGNED_BYTE, GL_FALSE, 0, zeroOffset + 12);
glDrawArraysInstanced(GL_POINTS, 0, 1, 4);
ASSERT_GL_NO_ERROR();
// Test touching the last element + 1 is invalid.
glVertexAttribPointer(0, 1, GL_UNSIGNED_BYTE, GL_FALSE, 0, zeroOffset + 13);
glDrawArraysInstanced(GL_POINTS, 0, 1, 4);
EXPECT_GL_ERROR(GL_INVALID_OPERATION);
// Test touching the last element is valid, using a stride.
glVertexAttribPointer(0, 1, GL_UNSIGNED_BYTE, GL_FALSE, 2, zeroOffset + 9);
glDrawArraysInstanced(GL_POINTS, 0, 1, 4);
ASSERT_GL_NO_ERROR();
// Test touching the last element + 1 is invalid, using a stride.
glVertexAttribPointer(0, 1, GL_UNSIGNED_BYTE, GL_FALSE, 2, zeroOffset + 10);
glDrawArraysInstanced(GL_POINTS, 0, 1, 4);
EXPECT_GL_ERROR(GL_INVALID_OPERATION);
// Test any offset is valid if no vertices are drawn.
glVertexAttribPointer(0, 1, GL_UNSIGNED_BYTE, GL_FALSE, 0, zeroOffset + 32);
glDrawArraysInstanced(GL_POINTS, 0, 1, 0);
ASSERT_GL_NO_ERROR();
}
// Tests that NPOT is not enabled by default in WebGL 1 and that it can be enabled
TEST_P(WebGLCompatibilityTest, NPOT)
{
EXPECT_FALSE(extensionEnabled("GL_OES_texture_npot"));
// Create a texture and set an NPOT mip 0, should always be acceptable.
GLTexture texture;
glBindTexture(GL_TEXTURE_2D, texture.get());
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 10, 10, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
ASSERT_GL_NO_ERROR();
// Try setting an NPOT mip 1 and verify the error if WebGL 1
glTexImage2D(GL_TEXTURE_2D, 1, GL_RGBA, 5, 5, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
if (getClientMajorVersion() < 3)
{
ASSERT_GL_ERROR(GL_INVALID_VALUE);
}
else
{
ASSERT_GL_NO_ERROR();
}
if (extensionRequestable("GL_OES_texture_npot"))
{
glRequestExtensionANGLE("GL_OES_texture_npot");
ASSERT_GL_NO_ERROR();
// Try again to set NPOT mip 1
glTexImage2D(GL_TEXTURE_2D, 1, GL_RGBA, 5, 5, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
ASSERT_GL_NO_ERROR();
}
}
template <typename T>
void FillTexture2D(GLuint texture,
GLsizei width,
GLsizei height,
const T &onePixelData,
GLint level,
GLint internalFormat,
GLenum format,
GLenum type)
{
std::vector<T> allPixelsData(width * height, onePixelData);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, level, internalFormat, width, height, 0, format, type,
allPixelsData.data());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
}
// Test that unset gl_Position defaults to (0,0,0,0).
TEST_P(WebGLCompatibilityTest, DefaultPosition)
{
// Draw a quad where each vertex is red if gl_Position is (0,0,0,0) before it is set,
// and green otherwise. The center of each quadrant will be red if and only if all
// four corners are red.
const std::string vertexShader =
"attribute vec3 pos;\n"
"varying vec4 color;\n"
"void main() {\n"
" if (gl_Position == vec4(0,0,0,0)) {\n"
" color = vec4(1,0,0,1);\n"
" } else {\n"
" color = vec4(0,1,0,1);\n"
" }\n"
" gl_Position = vec4(pos,1);\n"
"}\n";
const std::string fragmentShader =
"precision mediump float;\n"
"varying vec4 color;\n"
"void main() {\n"
" gl_FragColor = color;\n"
"}\n";
ANGLE_GL_PROGRAM(program, vertexShader, fragmentShader);
drawQuad(program.get(), "pos", 0.0f, 1.0f, true);
EXPECT_PIXEL_COLOR_EQ(getWindowWidth() * 1 / 4, getWindowHeight() * 1 / 4, GLColor::red);
EXPECT_PIXEL_COLOR_EQ(getWindowWidth() * 1 / 4, getWindowHeight() * 3 / 4, GLColor::red);
EXPECT_PIXEL_COLOR_EQ(getWindowWidth() * 3 / 4, getWindowHeight() * 1 / 4, GLColor::red);
EXPECT_PIXEL_COLOR_EQ(getWindowWidth() * 3 / 4, getWindowHeight() * 3 / 4, GLColor::red);
}
// Tests that a rendering feedback loop triggers a GL error under WebGL.
// Based on WebGL test conformance/renderbuffers/feedback-loop.html.
TEST_P(WebGLCompatibilityTest, RenderingFeedbackLoop)
{
const std::string vertexShader =
"attribute vec4 a_position;\n"
"varying vec2 v_texCoord;\n"
"void main() {\n"
" gl_Position = a_position;\n"
" v_texCoord = (a_position.xy * 0.5) + 0.5;\n"
"}\n";
const std::string fragmentShader =
"precision mediump float;\n"
"varying vec2 v_texCoord;\n"
"uniform sampler2D u_texture;\n"
"void main() {\n"
" // Shader swizzles color channels so we can tell if the draw succeeded.\n"
" gl_FragColor = texture2D(u_texture, v_texCoord).gbra;\n"
"}\n";
GLTexture texture;
FillTexture2D(texture.get(), 1, 1, GLColor::red, 0, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE);
ASSERT_GL_NO_ERROR();
GLFramebuffer framebuffer;
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer.get());
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture.get(), 0);
ASSERT_GLENUM_EQ(GL_FRAMEBUFFER_COMPLETE, glCheckFramebufferStatus(GL_FRAMEBUFFER));
ANGLE_GL_PROGRAM(program, vertexShader, fragmentShader);
GLint uniformLoc = glGetUniformLocation(program.get(), "u_texture");
ASSERT_NE(-1, uniformLoc);
glUseProgram(program.get());
glUniform1i(uniformLoc, 0);
glDisable(GL_BLEND);
glDisable(GL_DEPTH_TEST);
ASSERT_GL_NO_ERROR();
// Drawing with a texture that is also bound to the current framebuffer should fail
glBindTexture(GL_TEXTURE_2D, texture.get());
drawQuad(program.get(), "a_position", 0.5f, 1.0f, true);
EXPECT_GL_ERROR(GL_INVALID_OPERATION);
// Ensure that the texture contents did not change after the previous render
glBindFramebuffer(GL_FRAMEBUFFER, 0);
drawQuad(program.get(), "a_position", 0.5f, 1.0f, true);
ASSERT_GL_NO_ERROR();
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::blue);
// Drawing when texture is bound to an inactive uniform should succeed
GLTexture texture2;
FillTexture2D(texture2.get(), 1, 1, GLColor::green, 0, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE);
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer.get());
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texture.get());
drawQuad(program.get(), "a_position", 0.5f, 1.0f, true);
ASSERT_GL_NO_ERROR();
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::red);
}
// Test for the max draw buffers and color attachments.
TEST_P(WebGLCompatibilityTest, MaxDrawBuffersAttachmentPoints)
{
// This test only applies to ES2.
if (getClientMajorVersion() != 2)
{
return;
}
GLFramebuffer fbo[2];
glBindFramebuffer(GL_FRAMEBUFFER, fbo[0].get());
// Test that is valid when we bind with a single attachment point.
GLTexture texture;
glBindTexture(GL_TEXTURE_2D, texture.get());
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture.get(), 0);
ASSERT_GL_NO_ERROR();
// Test that enabling the draw buffers extension will allow us to bind with a non-zero
// attachment point.
if (extensionRequestable("GL_EXT_draw_buffers"))
{
glRequestExtensionANGLE("GL_EXT_draw_buffers");
EXPECT_GL_NO_ERROR();
EXPECT_TRUE(extensionEnabled("GL_EXT_draw_buffers"));
glBindFramebuffer(GL_FRAMEBUFFER, fbo[1].get());
GLTexture texture2;
glBindTexture(GL_TEXTURE_2D, texture2.get());
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, texture2.get(),
0);
ASSERT_GL_NO_ERROR();
}
}
// Test that the offset in the index buffer is forced to be a multiple of the element size
TEST_P(WebGLCompatibilityTest, DrawElementsOffsetRestriction)
{
const std::string &vert =
"attribute vec3 a_pos;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(a_pos, 1.0);\n"
"}\n";
const std::string &frag =
"precision highp float;\n"
"void main()\n"
"{\n"
" gl_FragColor = vec4(1.0);\n"
"}\n";
ANGLE_GL_PROGRAM(program, vert, frag);
GLint posLocation = glGetAttribLocation(program.get(), "a_pos");
ASSERT_NE(-1, posLocation);
glUseProgram(program.get());
const auto &vertices = GetQuadVertices();
GLBuffer vertexBuffer;
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer.get());
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices[0]) * vertices.size(), vertices.data(),
GL_STATIC_DRAW);
glVertexAttribPointer(posLocation, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(posLocation);
GLBuffer indexBuffer;
const GLubyte indices[] = {0, 0, 0, 0, 0, 0, 0};
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer.get());
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
ASSERT_GL_NO_ERROR();
const char *zeroIndices = nullptr;
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, zeroIndices);
ASSERT_GL_NO_ERROR();
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_SHORT, zeroIndices);
ASSERT_GL_NO_ERROR();
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_SHORT, zeroIndices + 1);
EXPECT_GL_ERROR(GL_INVALID_OPERATION);
}
// Test that the offset and stride in the vertex buffer is forced to be a multiple of the element
// size
TEST_P(WebGLCompatibilityTest, VertexAttribPointerOffsetRestriction)
{
const char *zeroOffset = nullptr;
// Base case, vector of two floats
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, zeroOffset);
ASSERT_GL_NO_ERROR();
// Test setting a non-multiple offset
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, zeroOffset + 1);
EXPECT_GL_ERROR(GL_INVALID_OPERATION);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, zeroOffset + 2);
EXPECT_GL_ERROR(GL_INVALID_OPERATION);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, zeroOffset + 3);
EXPECT_GL_ERROR(GL_INVALID_OPERATION);
// Test setting a non-multiple stride
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 1, zeroOffset);
EXPECT_GL_ERROR(GL_INVALID_OPERATION);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2, zeroOffset);
EXPECT_GL_ERROR(GL_INVALID_OPERATION);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 3, zeroOffset);
EXPECT_GL_ERROR(GL_INVALID_OPERATION);
}
void WebGLCompatibilityTest::drawBuffersEXTFeedbackLoop(GLuint program,
const std::array<GLenum, 2> &drawBuffers,
GLenum expectedError)
{
glDrawBuffersEXT(2, drawBuffers.data());
// Make sure framebuffer is complete before feedback loop detection
ASSERT_GLENUM_EQ(GL_FRAMEBUFFER_COMPLETE, glCheckFramebufferStatus(GL_FRAMEBUFFER));
drawQuad(program, "aPosition", 0.5f, 1.0f, true);
// "Rendering to a texture where it samples from should geneates INVALID_OPERATION. Otherwise,
// it should be NO_ERROR"
EXPECT_GL_ERROR(expectedError);
}
// This tests that rendering feedback loops works as expected with GL_EXT_draw_buffers.
// Based on WebGL test conformance/extensions/webgl-draw-buffers-feedback-loop.html
TEST_P(WebGLCompatibilityTest, RenderingFeedbackLoopWithDrawBuffersEXT)
{
const std::string vertexShader =
"attribute vec4 aPosition;\n"
"varying vec2 texCoord;\n"
"void main() {\n"
" gl_Position = aPosition;\n"
" texCoord = (aPosition.xy * 0.5) + 0.5;\n"
"}\n";
const std::string fragmentShader =
"#extension GL_EXT_draw_buffers : require\n"
"precision mediump float;\n"
"uniform sampler2D tex;\n"
"varying vec2 texCoord;\n"
"void main() {\n"
" gl_FragData[0] = texture2D(tex, texCoord);\n"
" gl_FragData[1] = texture2D(tex, texCoord);\n"
"}\n";
GLsizei width = 8;
GLsizei height = 8;
// This shader cannot be run in ES3, because WebGL 2 does not expose the draw buffers
// extension and gl_FragData semantics are changed to enforce indexing by zero always.
// TODO(jmadill): This extension should be disabled in WebGL 2 contexts.
if (/*!extensionEnabled("GL_EXT_draw_buffers")*/ getClientMajorVersion() != 2)
{
// No WEBGL_draw_buffers support -- this is legal.
return;
}
GLint maxDrawBuffers = 0;
glGetIntegerv(GL_MAX_DRAW_BUFFERS, &maxDrawBuffers);
if (maxDrawBuffers < 2)
{
std::cout << "Test skipped because MAX_DRAW_BUFFERS is too small." << std::endl;
return;
}
ANGLE_GL_PROGRAM(program, vertexShader, fragmentShader);
glUseProgram(program.get());
glViewport(0, 0, width, height);
GLTexture tex0;
GLTexture tex1;
GLFramebuffer fbo;
FillTexture2D(tex0.get(), width, height, GLColor::red, 0, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE);
FillTexture2D(tex1.get(), width, height, GLColor::green, 0, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE);
ASSERT_GL_NO_ERROR();
glBindTexture(GL_TEXTURE_2D, tex1.get());
GLint texLoc = glGetUniformLocation(program.get(), "tex");
ASSERT_NE(-1, texLoc);
glUniform1i(texLoc, 0);
ASSERT_GL_NO_ERROR();
// The sampling texture is bound to COLOR_ATTACHMENT1 during resource allocation
glBindFramebuffer(GL_FRAMEBUFFER, fbo.get());
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex0.get(), 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, tex1.get(), 0);
drawBuffersEXTFeedbackLoop(program.get(), {{GL_NONE, GL_COLOR_ATTACHMENT1}},
GL_INVALID_OPERATION);
drawBuffersEXTFeedbackLoop(program.get(), {{GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1}},
GL_INVALID_OPERATION);
drawBuffersEXTFeedbackLoop(program.get(), {{GL_COLOR_ATTACHMENT0, GL_NONE}}, GL_NO_ERROR);
}
// Test tests that texture copying feedback loops are properly rejected in WebGL.
// Based on the WebGL test conformance/textures/misc/texture-copying-feedback-loops.html
TEST_P(WebGLCompatibilityTest, TextureCopyingFeedbackLoops)
{
GLTexture texture;
glBindTexture(GL_TEXTURE_2D, texture.get());
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
GLTexture texture2;
glBindTexture(GL_TEXTURE_2D, texture2.get());
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
GLFramebuffer framebuffer;
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer.get());
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture.get(), 0);
// framebuffer should be FRAMEBUFFER_COMPLETE.
ASSERT_GLENUM_EQ(GL_FRAMEBUFFER_COMPLETE, glCheckFramebufferStatus(GL_FRAMEBUFFER));
ASSERT_GL_NO_ERROR();
// testing copyTexImage2D
// copyTexImage2D to same texture but different level
glBindTexture(GL_TEXTURE_2D, texture.get());
glCopyTexImage2D(GL_TEXTURE_2D, 1, GL_RGBA, 0, 0, 2, 2, 0);
EXPECT_GL_NO_ERROR();
// copyTexImage2D to same texture same level, invalid feedback loop
glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, 2, 2, 0);
EXPECT_GL_ERROR(GL_INVALID_OPERATION);
// copyTexImage2D to different texture
glBindTexture(GL_TEXTURE_2D, texture2.get());
glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, 2, 2, 0);
EXPECT_GL_NO_ERROR();
// testing copyTexSubImage2D
// copyTexSubImage2D to same texture but different level
glBindTexture(GL_TEXTURE_2D, texture.get());
glCopyTexSubImage2D(GL_TEXTURE_2D, 1, 0, 0, 0, 0, 1, 1);
EXPECT_GL_NO_ERROR();
// copyTexSubImage2D to same texture same level, invalid feedback loop
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, 1, 1);
EXPECT_GL_ERROR(GL_INVALID_OPERATION);
// copyTexSubImage2D to different texture
glBindTexture(GL_TEXTURE_2D, texture2.get());
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, 1, 1);
EXPECT_GL_NO_ERROR();
}
void WebGLCompatibilityTest::drawBuffersFeedbackLoop(GLuint program,
const std::array<GLenum, 2> &drawBuffers,
GLenum expectedError)
{
glDrawBuffers(2, drawBuffers.data());
// Make sure framebuffer is complete before feedback loop detection
ASSERT_GLENUM_EQ(GL_FRAMEBUFFER_COMPLETE, glCheckFramebufferStatus(GL_FRAMEBUFFER));
drawQuad(program, "aPosition", 0.5f, 1.0f, true);
// "Rendering to a texture where it samples from should geneates INVALID_OPERATION. Otherwise,
// it should be NO_ERROR"
EXPECT_GL_ERROR(expectedError);
}
// Tests invariance matching rules between built in varyings.
// Based on WebGL test conformance/glsl/misc/shaders-with-invariance.html.
TEST_P(WebGLCompatibilityTest, BuiltInInvariant)
{
const std::string vertexShaderVariant =
"varying vec4 v_varying;\n"
"void main()\n"
"{\n"
" gl_PointSize = 1.0;\n"
" gl_Position = v_varying;\n"
"}";
const std::string fragmentShaderInvariantGlFragCoord =
"invariant gl_FragCoord;\n"
"void main()\n"
"{\n"
" gl_FragColor = gl_FragCoord;\n"
"}";
const std::string fragmentShaderInvariantGlPointCoord =
"invariant gl_PointCoord;\n"
"void main()\n"
"{\n"
" gl_FragColor = vec4(gl_PointCoord, 0.0, 0.0);\n"
"}";
GLuint program = CompileProgram(vertexShaderVariant, fragmentShaderInvariantGlFragCoord);
EXPECT_EQ(0u, program);
program = CompileProgram(vertexShaderVariant, fragmentShaderInvariantGlPointCoord);
EXPECT_EQ(0u, program);
}
// This tests that rendering feedback loops works as expected with WebGL 2.
// Based on WebGL test conformance2/rendering/rendering-sampling-feedback-loop.html
TEST_P(WebGL2CompatibilityTest, RenderingFeedbackLoopWithDrawBuffers)
{
const std::string vertexShader =
"#version 300 es\n"
"in vec4 aPosition;\n"
"out vec2 texCoord;\n"
"void main() {\n"
" gl_Position = aPosition;\n"
" texCoord = (aPosition.xy * 0.5) + 0.5;\n"
"}\n";
const std::string fragmentShader =
"#version 300 es\n"
"precision mediump float;\n"
"uniform sampler2D tex;\n"
"in vec2 texCoord;\n"
"out vec4 oColor;\n"
"void main() {\n"
" oColor = texture(tex, texCoord);\n"
"}\n";
GLsizei width = 8;
GLsizei height = 8;
GLint maxDrawBuffers = 0;
glGetIntegerv(GL_MAX_DRAW_BUFFERS, &maxDrawBuffers);
// ES3 requires a minimum value of 4 for MAX_DRAW_BUFFERS.
ASSERT_GE(maxDrawBuffers, 2);
ANGLE_GL_PROGRAM(program, vertexShader, fragmentShader);
glUseProgram(program.get());
glViewport(0, 0, width, height);
GLTexture tex0;
GLTexture tex1;
GLFramebuffer fbo;
FillTexture2D(tex0.get(), width, height, GLColor::red, 0, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE);
FillTexture2D(tex1.get(), width, height, GLColor::green, 0, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE);
ASSERT_GL_NO_ERROR();
glBindTexture(GL_TEXTURE_2D, tex1.get());
GLint texLoc = glGetUniformLocation(program.get(), "tex");
ASSERT_NE(-1, texLoc);
glUniform1i(texLoc, 0);
// The sampling texture is bound to COLOR_ATTACHMENT1 during resource allocation
glBindFramebuffer(GL_FRAMEBUFFER, fbo.get());
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex0.get(), 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, tex1.get(), 0);
ASSERT_GL_NO_ERROR();
drawBuffersFeedbackLoop(program.get(), {{GL_NONE, GL_COLOR_ATTACHMENT1}}, GL_INVALID_OPERATION);
drawBuffersFeedbackLoop(program.get(), {{GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1}},
GL_INVALID_OPERATION);
drawBuffersFeedbackLoop(program.get(), {{GL_COLOR_ATTACHMENT0, GL_NONE}}, GL_NO_ERROR);
}
// This test covers detection of rendering feedback loops between the FBO and a depth Texture.
// Based on WebGL test conformance2/rendering/depth-stencil-feedback-loop.html
TEST_P(WebGL2CompatibilityTest, RenderingFeedbackLoopWithDepthStencil)
{
const std::string vertexShader =
"#version 300 es\n"
"in vec4 aPosition;\n"
"out vec2 texCoord;\n"
"void main() {\n"
" gl_Position = aPosition;\n"
" texCoord = (aPosition.xy * 0.5) + 0.5;\n"
"}\n";
const std::string fragmentShader =
"#version 300 es\n"
"precision mediump float;\n"
"uniform sampler2D tex;\n"
"in vec2 texCoord;\n"
"out vec4 oColor;\n"
"void main() {\n"
" oColor = texture(tex, texCoord);\n"
"}\n";
GLsizei width = 8;
GLsizei height = 8;
ANGLE_GL_PROGRAM(program, vertexShader, fragmentShader);
glUseProgram(program.get());
glViewport(0, 0, width, height);
GLint texLoc = glGetUniformLocation(program.get(), "tex");
glUniform1i(texLoc, 0);
// Create textures and allocate storage
GLTexture tex0;
GLTexture tex1;
GLRenderbuffer rb;
FillTexture2D(tex0.get(), width, height, GLColor::black, 0, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE);
FillTexture2D(tex1.get(), width, height, 0x80, 0, GL_DEPTH_COMPONENT16, GL_DEPTH_COMPONENT,
GL_UNSIGNED_INT);
glBindRenderbuffer(GL_RENDERBUFFER, rb.get());
glRenderbufferStorage(GL_RENDERBUFFER, GL_STENCIL_INDEX8, width, height);
ASSERT_GL_NO_ERROR();
GLFramebuffer fbo;
glBindFramebuffer(GL_FRAMEBUFFER, fbo.get());
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex0.get(), 0);
// Test rendering and sampling feedback loop for depth buffer
glBindTexture(GL_TEXTURE_2D, tex1.get());
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, tex1.get(), 0);
ASSERT_GLENUM_EQ(GL_FRAMEBUFFER_COMPLETE, glCheckFramebufferStatus(GL_FRAMEBUFFER));
// The same image is used as depth buffer during rendering.
glEnable(GL_DEPTH_TEST);
drawQuad(program.get(), "aPosition", 0.5f, 1.0f, true);
EXPECT_GL_ERROR(GL_INVALID_OPERATION);
// The same image is used as depth buffer. But depth mask is false.
glDepthMask(GL_FALSE);
drawQuad(program.get(), "aPosition", 0.5f, 1.0f, true);
EXPECT_GL_NO_ERROR();
// The same image is used as depth buffer. But depth test is not enabled during rendering.
glDepthMask(GL_TRUE);
glDisable(GL_DEPTH_TEST);
drawQuad(program.get(), "aPosition", 0.5f, 1.0f, true);
EXPECT_GL_NO_ERROR();
// Test rendering and sampling feedback loop for stencil buffer
glBindTexture(GL_RENDERBUFFER, rb.get());
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, rb.get());
ASSERT_GLENUM_EQ(GL_FRAMEBUFFER_COMPLETE, glCheckFramebufferStatus(GL_FRAMEBUFFER));
constexpr GLint stencilClearValue = 0x40;
glClearBufferiv(GL_STENCIL, 0, &stencilClearValue);
// The same image is used as stencil buffer during rendering.
glEnable(GL_STENCIL_TEST);
drawQuad(program.get(), "aPosition", 0.5f, 1.0f, true);
EXPECT_GL_ERROR(GL_INVALID_OPERATION);
// The same image is used as stencil buffer. But stencil mask is zero.
glStencilMask(0x0);
drawQuad(program.get(), "aPosition", 0.5f, 1.0f, true);
EXPECT_GL_NO_ERROR();
// The same image is used as stencil buffer. But stencil test is not enabled during rendering.
glStencilMask(0xffff);
glDisable(GL_STENCIL_TEST);
drawQuad(program.get(), "aPosition", 0.5f, 1.0f, true);
EXPECT_GL_NO_ERROR();
}
// The source and the target for CopyTexSubImage3D are the same 3D texture.
// But the level of the 3D texture != the level of the read attachment.
TEST_P(WebGL2CompatibilityTest, NoTextureCopyingFeedbackLoopBetween3DLevels)
{
GLTexture texture;
GLFramebuffer framebuffer;
glBindTexture(GL_TEXTURE_3D, texture.get());
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer.get());
glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA8, 2, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
glTexImage3D(GL_TEXTURE_3D, 1, GL_RGBA8, 2, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
glFramebufferTextureLayer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, texture.get(), 0, 0);
ASSERT_GL_NO_ERROR();
glCopyTexSubImage3D(GL_TEXTURE_3D, 1, 0, 0, 0, 0, 0, 2, 2);
EXPECT_GL_NO_ERROR();
}
// The source and the target for CopyTexSubImage3D are the same 3D texture.
// But the zoffset of the 3D texture != the layer of the read attachment.
TEST_P(WebGL2CompatibilityTest, NoTextureCopyingFeedbackLoopBetween3DLayers)
{
GLTexture texture;
GLFramebuffer framebuffer;
glBindTexture(GL_TEXTURE_3D, texture.get());
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer.get());
glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA8, 2, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
glFramebufferTextureLayer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, texture.get(), 0, 1);
ASSERT_GL_NO_ERROR();
glCopyTexSubImage3D(GL_TEXTURE_3D, 0, 0, 0, 0, 0, 0, 2, 2);
EXPECT_GL_NO_ERROR();
}
// The source and the target for CopyTexSubImage3D are the same 3D texture.
// And the level / zoffset of the 3D texture is equal to the level / layer of the read attachment.
TEST_P(WebGL2CompatibilityTest, TextureCopyingFeedbackLoop3D)
{
GLTexture texture;
GLFramebuffer framebuffer;
glBindTexture(GL_TEXTURE_3D, texture.get());
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer.get());
glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA8, 4, 4, 4, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
glTexImage3D(GL_TEXTURE_3D, 1, GL_RGBA8, 2, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
glTexImage3D(GL_TEXTURE_3D, 2, GL_RGBA8, 1, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
glFramebufferTextureLayer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, texture.get(), 1, 0);
ASSERT_GL_NO_ERROR();
glCopyTexSubImage3D(GL_TEXTURE_3D, 1, 0, 0, 0, 0, 0, 2, 2);
EXPECT_GL_ERROR(GL_INVALID_OPERATION);
}
// Verify that errors are generated when there isn not a defined conversion between the clear type
// and the buffer type.
TEST_P(WebGL2CompatibilityTest, ClearBufferTypeCompatibity)
{
if (IsD3D11())
{
std::cout << "Test skipped because it generates D3D11 runtime warnings." << std::endl;
return;
}
constexpr float clearFloat[] = {0.0f, 0.0f, 0.0f, 0.0f};
constexpr int clearInt[] = {0, 0, 0, 0};
constexpr unsigned int clearUint[] = {0, 0, 0, 0};
GLTexture texture;
GLFramebuffer framebuffer;
glBindTexture(GL_TEXTURE_2D, texture.get());
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer.get());
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture.get(), 0);
ASSERT_GL_NO_ERROR();
// Unsigned integer buffer
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32UI, 1, 1, 0, GL_RGBA_INTEGER, GL_UNSIGNED_INT, nullptr);
ASSERT_GL_NO_ERROR();
glClearBufferfv(GL_COLOR, 0, clearFloat);
EXPECT_GL_ERROR(GL_INVALID_OPERATION);
glClearBufferiv(GL_COLOR, 0, clearInt);
EXPECT_GL_ERROR(GL_INVALID_OPERATION);
glClearBufferuiv(GL_COLOR, 0, clearUint);
EXPECT_GL_NO_ERROR();
glClear(GL_COLOR_BUFFER_BIT);
EXPECT_GL_ERROR(GL_INVALID_OPERATION);
// Integer buffer
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32I, 1, 1, 0, GL_RGBA_INTEGER, GL_INT, nullptr);
ASSERT_GL_NO_ERROR();
glClearBufferfv(GL_COLOR, 0, clearFloat);
EXPECT_GL_ERROR(GL_INVALID_OPERATION);
glClearBufferiv(GL_COLOR, 0, clearInt);
EXPECT_GL_NO_ERROR();
glClearBufferuiv(GL_COLOR, 0, clearUint);
EXPECT_GL_ERROR(GL_INVALID_OPERATION);
glClear(GL_COLOR_BUFFER_BIT);
EXPECT_GL_ERROR(GL_INVALID_OPERATION);
// Float buffer
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, 1, 1, 0, GL_RGBA, GL_FLOAT, nullptr);
ASSERT_GL_NO_ERROR();
glClearBufferfv(GL_COLOR, 0, clearFloat);
EXPECT_GL_NO_ERROR();
glClearBufferiv(GL_COLOR, 0, clearInt);
EXPECT_GL_ERROR(GL_INVALID_OPERATION);
glClearBufferuiv(GL_COLOR, 0, clearUint);
EXPECT_GL_ERROR(GL_INVALID_OPERATION);
glClear(GL_COLOR_BUFFER_BIT);
EXPECT_GL_NO_ERROR();
// Normalized uint buffer
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
ASSERT_GL_NO_ERROR();
glClearBufferfv(GL_COLOR, 0, clearFloat);
EXPECT_GL_NO_ERROR();
glClearBufferiv(GL_COLOR, 0, clearInt);
EXPECT_GL_ERROR(GL_INVALID_OPERATION);
glClearBufferuiv(GL_COLOR, 0, clearUint);
EXPECT_GL_ERROR(GL_INVALID_OPERATION);
glClear(GL_COLOR_BUFFER_BIT);
EXPECT_GL_NO_ERROR();
}
// Use this to select which configurations (e.g. which renderer, which GLES major version) these
// tests should be run against.
ANGLE_INSTANTIATE_TEST(WebGLCompatibilityTest,
ES2_D3D9(),
ES2_D3D11(),
ES3_D3D11(),
ES2_D3D11_FL9_3(),
ES2_OPENGL(),
ES3_OPENGL(),
ES2_OPENGLES(),
ES3_OPENGLES());
ANGLE_INSTANTIATE_TEST(WebGL2CompatibilityTest, ES3_D3D11(), ES3_OPENGL(), ES3_OPENGLES());
} // namespace