Hash :
9e8b104e
Author :
Date :
2024-10-14T00:00:00
Do not test OpenGL backend on iOS Added Metal platform to tests that require instantiation. Bug: angleproject:40050022 Bug: angleproject:42264029 Bug: angleproject:42266119 Bug: angleproject:42266226 Bug: angleproject:42266239 Bug: angleproject:42266249 Bug: angleproject:359136169 Fixed: angleproject:373478551 Change-Id: I915f09c7f24acce27bf0d489932645338ac3fbe8 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/5932659 Reviewed-by: Geoff Lang <geofflang@chromium.org> Commit-Queue: Alexey Knyazev <lexa.knyazev@gmail.com>
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 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535
//
// 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 "test_utils/gl_raii.h"
#include "common/mathutil.h"
#include "platform/autogen/FeaturesD3D_autogen.h"
using namespace angle;
struct ReadbackTestParam
{
GLuint attachment;
GLuint format;
GLuint type;
void *data;
int depthBits;
int stencilBits;
};
class DepthStencilFormatsTestBase : public ANGLETest<>
{
protected:
DepthStencilFormatsTestBase()
{
setWindowWidth(128);
setWindowHeight(128);
setConfigRedBits(8);
setConfigGreenBits(8);
setConfigBlueBits(8);
setConfigAlphaBits(8);
}
bool checkTexImageFormatSupport(GLenum format, GLenum type)
{
EXPECT_GL_NO_ERROR();
GLuint tex = 0;
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glTexImage2D(GL_TEXTURE_2D, 0, format, 1, 1, 0, format, type, nullptr);
glDeleteTextures(1, &tex);
return (glGetError() == GL_NO_ERROR);
}
bool checkTexStorageFormatSupport(GLenum internalFormat)
{
EXPECT_GL_NO_ERROR();
GLuint tex = 0;
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glTexStorage2DEXT(GL_TEXTURE_2D, 1, internalFormat, 1, 1);
glDeleteTextures(1, &tex);
return (glGetError() == GL_NO_ERROR);
}
bool checkRenderbufferFormatSupport(GLenum internalFormat)
{
EXPECT_GL_NO_ERROR();
GLuint rb = 0;
glGenRenderbuffers(1, &rb);
glBindRenderbuffer(GL_RENDERBUFFER, rb);
glRenderbufferStorage(GL_RENDERBUFFER, internalFormat, 1, 1);
glDeleteRenderbuffers(1, &rb);
return (glGetError() == GL_NO_ERROR);
}
void verifyDepthRenderBuffer(GLenum internalFormat)
{
GLTexture tex;
glBindTexture(GL_TEXTURE_2D, tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
ASSERT_GL_NO_ERROR();
GLRenderbuffer rbDepth;
glBindRenderbuffer(GL_RENDERBUFFER, rbDepth);
glRenderbufferStorage(GL_RENDERBUFFER, internalFormat, 1, 1);
ASSERT_GL_NO_ERROR();
GLFramebuffer fbo;
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex, 0);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rbDepth);
EXPECT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
ASSERT_GL_NO_ERROR();
ANGLE_GL_PROGRAM(programRed, essl1_shaders::vs::Simple(), essl1_shaders::fs::Red());
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_GEQUAL);
glClearDepthf(0.99f);
glClearColor(0, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Pass Depth Test and draw red
float depthValue = 1.0f;
drawQuad(programRed, essl1_shaders::PositionAttrib(), depthValue * 2 - 1);
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::red);
ASSERT_GL_NO_ERROR();
ANGLE_GL_PROGRAM(programGreen, essl1_shaders::vs::Simple(), essl1_shaders::fs::Green());
// Fail Depth Test and color buffer is unchanged
depthValue = 0.98f;
drawQuad(programGreen, essl1_shaders::PositionAttrib(), depthValue * 2 - 1);
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::red);
ASSERT_GL_NO_ERROR();
ANGLE_GL_PROGRAM(programBlue, essl1_shaders::vs::Simple(), essl1_shaders::fs::Blue());
glClearDepthf(0.0f);
glClear(GL_DEPTH_BUFFER_BIT);
// Pass Depth Test and draw blue
depthValue = 0.01f;
drawQuad(programBlue, essl1_shaders::PositionAttrib(), depthValue * 2 - 1);
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::blue);
glDisable(GL_DEPTH_TEST);
ASSERT_GL_NO_ERROR();
}
void testSetUp() override
{
constexpr char kVS[] = R"(precision highp float;
attribute vec4 position;
varying vec2 texcoord;
void main()
{
gl_Position = position;
texcoord = (position.xy * 0.5) + 0.5;
})";
constexpr char kFS[] = R"(precision highp float;
uniform sampler2D tex;
varying vec2 texcoord;
void main()
{
gl_FragColor = texture2D(tex, texcoord);
})";
mProgram = CompileProgram(kVS, kFS);
if (mProgram == 0)
{
FAIL() << "shader compilation failed.";
}
mTextureUniformLocation = glGetUniformLocation(mProgram, "tex");
EXPECT_NE(-1, mTextureUniformLocation);
glGenTextures(1, &mTexture);
ASSERT_GL_NO_ERROR();
}
void testTearDown() override
{
glDeleteProgram(mProgram);
glDeleteTextures(1, &mTexture);
}
bool hasReadDepthSupport() const { return IsGLExtensionEnabled("GL_NV_read_depth"); }
bool hasReadDepthStencilSupport() const
{
return IsGLExtensionEnabled("GL_NV_read_depth_stencil");
}
bool hasReadStencilSupport() const { return IsGLExtensionEnabled("GL_NV_read_stencil"); }
bool hasFloatDepthSupport() const { return IsGLExtensionEnabled("GL_NV_depth_buffer_float2"); }
void depthStencilReadbackCase(const ReadbackTestParam &type);
GLuint mProgram;
GLuint mTexture;
GLint mTextureUniformLocation;
};
class DepthStencilFormatsTest : public DepthStencilFormatsTestBase
{};
class DepthStencilFormatsTestES3 : public DepthStencilFormatsTestBase
{};
TEST_P(DepthStencilFormatsTest, DepthTexture)
{
bool shouldHaveTextureSupport = (IsGLExtensionEnabled("GL_ANGLE_depth_texture") ||
IsGLExtensionEnabled("GL_OES_depth_texture"));
EXPECT_EQ(shouldHaveTextureSupport,
checkTexImageFormatSupport(GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT));
EXPECT_EQ(shouldHaveTextureSupport,
checkTexImageFormatSupport(GL_DEPTH_COMPONENT, GL_UNSIGNED_INT));
if (IsGLExtensionEnabled("GL_EXT_texture_storage"))
{
EXPECT_EQ(shouldHaveTextureSupport, checkTexStorageFormatSupport(GL_DEPTH_COMPONENT16));
EXPECT_EQ(shouldHaveTextureSupport, checkTexStorageFormatSupport(GL_DEPTH_COMPONENT32_OES));
}
}
TEST_P(DepthStencilFormatsTest, PackedDepthStencil)
{
// Expected to fail in D3D9 if GL_OES_packed_depth_stencil is not present.
// Expected to fail in D3D11 if GL_OES_packed_depth_stencil or GL_ANGLE_depth_texture is not
// present.
bool shouldHaveRenderbufferSupport = IsGLExtensionEnabled("GL_OES_packed_depth_stencil");
EXPECT_EQ(shouldHaveRenderbufferSupport,
checkRenderbufferFormatSupport(GL_DEPTH24_STENCIL8_OES));
bool shouldHaveTextureSupport = ((IsGLExtensionEnabled("GL_OES_packed_depth_stencil") ||
IsGLExtensionEnabled("GL_OES_depth_texture_cube_map")) &&
IsGLExtensionEnabled("GL_OES_depth_texture")) ||
IsGLExtensionEnabled("GL_ANGLE_depth_texture");
EXPECT_EQ(shouldHaveTextureSupport,
checkTexImageFormatSupport(GL_DEPTH_STENCIL_OES, GL_UNSIGNED_INT_24_8_OES));
if (IsGLExtensionEnabled("GL_EXT_texture_storage"))
{
bool shouldHaveTexStorageSupport = IsGLExtensionEnabled("GL_OES_packed_depth_stencil") ||
IsGLExtensionEnabled("GL_ANGLE_depth_texture");
EXPECT_EQ(shouldHaveTexStorageSupport,
checkTexStorageFormatSupport(GL_DEPTH24_STENCIL8_OES));
}
}
void DepthStencilFormatsTestBase::depthStencilReadbackCase(const ReadbackTestParam &type)
{
ANGLE_SKIP_TEST_IF(!IsGLExtensionEnabled("GL_OES_depth_texture"));
const bool hasFloatDepth = (type.type == GL_FLOAT);
ANGLE_SKIP_TEST_IF(hasFloatDepth && !hasFloatDepthSupport());
const bool hasStencil = (type.format != GL_DEPTH_COMPONENT);
const bool supportPackedDepthStencilFramebuffer = getClientMajorVersion() >= 3;
const int res = 2;
const int destRes = 4;
GLTexture tex;
glBindTexture(GL_TEXTURE_2D, tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// test level > 0
glTexImage2D(GL_TEXTURE_2D, 1, type.format, 1, 1, 0, type.format, type.type, nullptr);
EXPECT_GL_NO_ERROR();
// test with data
glTexImage2D(GL_TEXTURE_2D, 0, type.format, 1, 1, 0, type.format, type.type, type.data);
EXPECT_GL_NO_ERROR();
// test real thing
glTexImage2D(GL_TEXTURE_2D, 0, type.format, res, res, 0, type.format, type.type, nullptr);
EXPECT_GL_NO_ERROR();
// test texSubImage2D
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 1, 1, type.format, type.type, type.data);
EXPECT_GL_NO_ERROR();
GLuint fbo = 0;
glGenFramebuffers(1, &fbo);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
if (type.depthBits > 0 && type.stencilBits > 0 && !supportPackedDepthStencilFramebuffer)
{
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, tex, 0);
EXPECT_GL_NO_ERROR();
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, tex, 0);
EXPECT_GL_NO_ERROR();
}
else
{
glFramebufferTexture2D(GL_FRAMEBUFFER, type.attachment, GL_TEXTURE_2D, tex, 0);
EXPECT_GL_NO_ERROR();
}
// Ensure DEPTH_BITS returns >= 16 bits for UNSIGNED_SHORT and UNSIGNED_INT, >= 24
// UNSIGNED_INT_24_8_WEBGL. If there is stencil, ensure STENCIL_BITS reports >= 8 for
// UNSIGNED_INT_24_8_WEBGL.
GLint depthBits = 0;
glGetIntegerv(GL_DEPTH_BITS, &depthBits);
EXPECT_GE(depthBits, type.depthBits);
GLint stencilBits = 0;
glGetIntegerv(GL_STENCIL_BITS, &stencilBits);
EXPECT_GE(stencilBits, type.stencilBits);
// TODO: remove this check if the spec is updated to require these combinations to work.
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
{
// try adding a color buffer.
GLuint colorTex = 0;
glGenTextures(1, &colorTex);
glBindTexture(GL_TEXTURE_2D, colorTex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, res, res, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, colorTex, 0);
EXPECT_GLENUM_EQ(GL_FRAMEBUFFER_COMPLETE, glCheckFramebufferStatus(GL_FRAMEBUFFER));
}
EXPECT_GLENUM_EQ(GL_FRAMEBUFFER_COMPLETE, glCheckFramebufferStatus(GL_FRAMEBUFFER));
// use the default texture to render with while we return to the depth texture.
glBindTexture(GL_TEXTURE_2D, 0);
/* Setup 2x2 depth texture:
* 1 0.6 0.8
* |
* 0 0.2 0.4
* 0---1
*/
GLbitfield clearBits = GL_DEPTH_BUFFER_BIT;
if (hasStencil)
{
clearBits |= GL_STENCIL_BUFFER_BIT;
}
const GLfloat d00 = 0.2;
const GLfloat d01 = 0.4;
const GLfloat d10 = 0.6;
const GLfloat d11 = 0.8;
glEnable(GL_SCISSOR_TEST);
glScissor(0, 0, 1, 1);
glClearDepthf(d00);
glClearStencil(1);
glClear(clearBits);
glScissor(1, 0, 1, 1);
glClearDepthf(d10);
glClearStencil(2);
glClear(clearBits);
glScissor(0, 1, 1, 1);
glClearDepthf(d01);
glClearStencil(3);
glClear(clearBits);
glScissor(1, 1, 1, 1);
glClearDepthf(d11);
glClearStencil(4);
glClear(clearBits);
glDisable(GL_SCISSOR_TEST);
GLubyte actualPixels[destRes * destRes * 8];
glReadPixels(0, 0, destRes, destRes, GL_DEPTH_COMPONENT,
hasFloatDepth ? GL_FLOAT : GL_UNSIGNED_SHORT, actualPixels);
if (hasReadDepthSupport())
{
EXPECT_GL_NO_ERROR();
if (hasFloatDepth)
{
constexpr float kEpsilon = 0.002f;
const float *pixels = reinterpret_cast<const float *>(actualPixels);
ASSERT_NEAR(pixels[0], d00, kEpsilon);
ASSERT_NEAR(pixels[0 + destRes], d01, kEpsilon);
ASSERT_NEAR(pixels[1], d10, kEpsilon);
ASSERT_NEAR(pixels[1 + destRes], d11, kEpsilon);
}
else
{
constexpr unsigned short kEpsilon = 2;
const unsigned short *pixels = reinterpret_cast<const unsigned short *>(actualPixels);
ASSERT_NEAR(pixels[0], gl::unorm<16>(d00), kEpsilon);
ASSERT_NEAR(pixels[0 + destRes], gl::unorm<16>(d01), kEpsilon);
ASSERT_NEAR(pixels[1], gl::unorm<16>(d10), kEpsilon);
ASSERT_NEAR(pixels[1 + destRes], gl::unorm<16>(d11), kEpsilon);
}
}
else
{
EXPECT_GL_ERROR(GL_INVALID_OPERATION);
}
if (hasStencil)
{
glReadPixels(0, 0, destRes, destRes, GL_STENCIL_INDEX_OES, GL_UNSIGNED_BYTE, actualPixels);
if (hasReadStencilSupport())
{
EXPECT_GL_NO_ERROR();
ASSERT_TRUE((actualPixels[0] == 1) && (actualPixels[1] == 2) &&
(actualPixels[0 + destRes] == 3) && (actualPixels[1 + destRes] == 4));
}
else
{
EXPECT_GL_ERROR(GL_INVALID_OPERATION);
}
ASSERT(!hasFloatDepth);
glReadPixels(0, 0, destRes, destRes, GL_DEPTH_STENCIL_OES, GL_UNSIGNED_INT_24_8_OES,
actualPixels);
if (hasReadDepthStencilSupport())
{
EXPECT_GL_NO_ERROR();
struct Pixel
{
uint32_t x;
uint32_t d24() const { return x >> 8; }
uint8_t s8() const { return x & 0xff; }
};
constexpr unsigned short kEpsilon = 2;
const Pixel *pixels = reinterpret_cast<const Pixel *>(actualPixels);
ASSERT_NEAR(pixels[0].d24(), gl::unorm<24>(d00), kEpsilon);
ASSERT_NEAR(pixels[0 + destRes].d24(), gl::unorm<24>(d01), kEpsilon);
ASSERT_NEAR(pixels[1].d24(), gl::unorm<24>(d10), kEpsilon);
ASSERT_NEAR(pixels[1 + destRes].d24(), gl::unorm<24>(d11), kEpsilon);
ASSERT_TRUE((pixels[0].s8() == 1) && (pixels[1].s8() == 2) &&
(pixels[0 + destRes].s8() == 3) && (pixels[1 + destRes].s8() == 4));
}
else
{
EXPECT_GL_ERROR(GL_INVALID_OPERATION);
}
}
}
// This test will initialize a depth texture, clear it and read it back, if possible
TEST_P(DepthStencilFormatsTest, DepthStencilReadback_UShort)
{
GLuint fakeData[10] = {0};
ReadbackTestParam type = {
GL_DEPTH_ATTACHMENT, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, fakeData, 16, 0};
depthStencilReadbackCase(type);
}
// This test will initialize a depth texture, clear it and read it back, if possible
TEST_P(DepthStencilFormatsTest, DepthStencilReadback_UInt)
{
// http://anglebug.com/40644772
ANGLE_SKIP_TEST_IF(IsMac() && IsIntelUHD630Mobile() && IsDesktopOpenGL());
GLuint fakeData[10] = {0};
ReadbackTestParam type = {
GL_DEPTH_ATTACHMENT, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, fakeData, 16, 0};
depthStencilReadbackCase(type);
}
// This test will initialize a depth texture, clear it and read it back, if possible
TEST_P(DepthStencilFormatsTest, DepthStencilReadback_Float)
{
// http://anglebug.com/40644772
ANGLE_SKIP_TEST_IF(IsMac() && IsIntelUHD630Mobile() && IsDesktopOpenGL());
GLuint fakeData[10] = {0};
ReadbackTestParam type = {GL_DEPTH_ATTACHMENT, GL_DEPTH_COMPONENT, GL_FLOAT, fakeData, 32, 0};
depthStencilReadbackCase(type);
}
// This test will initialize a depth texture, clear it and read it back, if possible
TEST_P(DepthStencilFormatsTest, DepthStencilReadback_DepthStencil)
{
GLuint fakeData[10] = {0};
ReadbackTestParam type = {
GL_DEPTH_STENCIL_ATTACHMENT, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8_OES, fakeData, 24, 8};
depthStencilReadbackCase(type);
}
// Verify that packed D/S readPixels with a D32_FLOAT_S8X24_UINT attachment
TEST_P(DepthStencilFormatsTestES3, DepthStencilReadback_DepthFloatStencil)
{
ANGLE_SKIP_TEST_IF(!IsGLExtensionEnabled("GL_OES_packed_depth_stencil") ||
!IsGLExtensionEnabled("GL_NV_depth_buffer_float2") ||
!IsGLExtensionEnabled("GL_NV_read_depth") ||
!IsGLExtensionEnabled("GL_NV_read_depth_stencil") ||
!IsGLExtensionEnabled("GL_NV_read_stencil"));
GLFramebuffer FBO;
glBindFramebuffer(GL_FRAMEBUFFER, FBO);
ASSERT_GL_NO_ERROR();
GLRenderbuffer RBO;
glBindRenderbuffer(GL_RENDERBUFFER, RBO);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH32F_STENCIL8, getWindowWidth(),
getWindowHeight());
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, RBO);
ASSERT_GL_NO_ERROR();
constexpr float kDepthClearValue = 0.123f;
constexpr uint8_t kStencilClearValue = 0x42;
glClearDepthf(kDepthClearValue);
glClearStencil(kStencilClearValue);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
ASSERT_GL_NO_ERROR();
struct
{
float depth;
uint8_t stencil;
char unused[3];
} pixel = {};
glReadPixels(0, 0, 1, 1, GL_DEPTH_STENCIL_OES, GL_FLOAT_32_UNSIGNED_INT_24_8_REV, &pixel);
EXPECT_GL_NO_ERROR();
EXPECT_FLOAT_EQ(pixel.depth, kDepthClearValue);
EXPECT_EQ(pixel.stencil, kStencilClearValue);
}
// This test will initialize a depth texture and then render with it and verify
// pixel correctness.
// This is modeled after webgl-depth-texture.html
TEST_P(DepthStencilFormatsTest, DepthTextureRender)
{
constexpr char kVS[] = R"(attribute vec4 a_position;
void main()
{
gl_Position = a_position;
})";
constexpr char kFS[] = R"(precision mediump float;
uniform sampler2D u_texture;
uniform vec2 u_resolution;
void main()
{
vec2 texcoord = (gl_FragCoord.xy - vec2(0.5)) / (u_resolution - vec2(1.0));
gl_FragColor = texture2D(u_texture, texcoord);
})";
ANGLE_SKIP_TEST_IF(!IsGLExtensionEnabled("GL_OES_depth_texture") &&
!IsGLExtensionEnabled("GL_ANGLE_depth_texture"));
bool depthTextureCubeSupport = IsGLExtensionEnabled("GL_OES_depth_texture_cube_map");
bool textureSrgbDecodeSupport = IsGLExtensionEnabled("GL_EXT_texture_sRGB_decode");
// http://anglebug.com/42262117
ANGLE_SKIP_TEST_IF(IsIntel() && IsWindows() && IsD3D9());
const int res = 2;
const int destRes = 4;
GLint resolution;
ANGLE_GL_PROGRAM(program, kVS, kFS);
glUseProgram(program);
resolution = glGetUniformLocation(program, "u_resolution");
ASSERT_NE(-1, resolution);
glUniform2f(resolution, static_cast<float>(destRes), static_cast<float>(destRes));
GLuint buffer;
glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
float verts[] = {
1, 1, 1, -1, 1, 0, -1, -1, -1, 1, 1, 1, -1, -1, -1, 1, -1, 0,
};
glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
// OpenGL ES does not have a FLIPY PixelStore attribute
// glPixelStorei(GL_UNPACK_FLIP)
struct TypeInfo
{
GLuint attachment;
GLuint format;
GLuint type;
void *data;
int depthBits;
int stencilBits;
};
GLuint fakeData[10] = {0};
std::vector<TypeInfo> types = {
{GL_DEPTH_ATTACHMENT, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, fakeData, 16, 0},
{GL_DEPTH_ATTACHMENT, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, fakeData, 16, 0},
{GL_DEPTH_STENCIL_ATTACHMENT, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8_OES, fakeData, 24, 8},
};
for (const TypeInfo &type : types)
{
GLTexture cubeTex;
glBindTexture(GL_TEXTURE_CUBE_MAP, cubeTex);
ASSERT_GL_NO_ERROR();
std::vector<GLuint> targets{GL_TEXTURE_CUBE_MAP_POSITIVE_X, GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
GL_TEXTURE_CUBE_MAP_POSITIVE_Y, GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
GL_TEXTURE_CUBE_MAP_POSITIVE_Z, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z};
for (const GLuint target : targets)
{
glTexImage2D(target, 0, type.format, 1, 1, 0, type.format, type.type, nullptr);
if (depthTextureCubeSupport)
{
ASSERT_GL_NO_ERROR();
}
else
{
EXPECT_GL_ERROR(GL_INVALID_OPERATION);
}
}
std::vector<GLuint> filterModes = {GL_LINEAR, GL_NEAREST};
const bool supportPackedDepthStencilFramebuffer = getClientMajorVersion() >= 3;
for (const GLuint filterMode : filterModes)
{
GLTexture tex;
glBindTexture(GL_TEXTURE_2D, tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filterMode);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filterMode);
if (textureSrgbDecodeSupport)
{
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SRGB_DECODE_EXT, GL_SKIP_DECODE_EXT);
}
// test level > 0
glTexImage2D(GL_TEXTURE_2D, 1, type.format, 1, 1, 0, type.format, type.type, nullptr);
if (IsGLExtensionEnabled("GL_OES_depth_texture"))
{
EXPECT_GL_NO_ERROR();
}
else
{
EXPECT_GL_ERROR(GL_INVALID_OPERATION);
}
// test with data
glTexImage2D(GL_TEXTURE_2D, 0, type.format, 1, 1, 0, type.format, type.type, type.data);
if (IsGLExtensionEnabled("GL_OES_depth_texture"))
{
EXPECT_GL_NO_ERROR();
}
else
{
EXPECT_GL_ERROR(GL_INVALID_OPERATION);
}
// test copyTexImage2D
glCopyTexImage2D(GL_TEXTURE_2D, 0, type.format, 0, 0, 1, 1, 0);
GLuint error = glGetError();
ASSERT_TRUE(error == GL_INVALID_ENUM || error == GL_INVALID_OPERATION);
// test real thing
glTexImage2D(GL_TEXTURE_2D, 0, type.format, res, res, 0, type.format, type.type,
nullptr);
EXPECT_GL_NO_ERROR();
// test texSubImage2D
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 1, 1, type.format, type.type, type.data);
if (IsGLExtensionEnabled("GL_OES_depth_texture"))
{
EXPECT_GL_NO_ERROR();
}
else
{
EXPECT_GL_ERROR(GL_INVALID_OPERATION);
}
// test copyTexSubImage2D
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, 1, 1);
EXPECT_GL_ERROR(GL_INVALID_OPERATION);
// test generateMipmap
glGenerateMipmap(GL_TEXTURE_2D);
EXPECT_GL_ERROR(GL_INVALID_OPERATION);
GLuint fbo = 0;
glGenFramebuffers(1, &fbo);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
if (type.depthBits > 0 && type.stencilBits > 0 && !supportPackedDepthStencilFramebuffer)
{
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, tex, 0);
EXPECT_GL_NO_ERROR();
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, tex,
0);
EXPECT_GL_NO_ERROR();
}
else
{
glFramebufferTexture2D(GL_FRAMEBUFFER, type.attachment, GL_TEXTURE_2D, tex, 0);
EXPECT_GL_NO_ERROR();
}
// Ensure DEPTH_BITS returns >= 16 bits for UNSIGNED_SHORT and UNSIGNED_INT, >= 24
// UNSIGNED_INT_24_8_WEBGL. If there is stencil, ensure STENCIL_BITS reports >= 8 for
// UNSIGNED_INT_24_8_WEBGL.
GLint depthBits = 0;
glGetIntegerv(GL_DEPTH_BITS, &depthBits);
EXPECT_GE(depthBits, type.depthBits);
GLint stencilBits = 0;
glGetIntegerv(GL_STENCIL_BITS, &stencilBits);
EXPECT_GE(stencilBits, type.stencilBits);
// TODO: remove this check if the spec is updated to require these combinations to work.
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
{
// try adding a color buffer.
GLuint colorTex = 0;
glGenTextures(1, &colorTex);
glBindTexture(GL_TEXTURE_2D, colorTex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, res, res, 0, GL_RGBA, GL_UNSIGNED_BYTE,
nullptr);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
colorTex, 0);
EXPECT_GLENUM_EQ(GL_FRAMEBUFFER_COMPLETE, glCheckFramebufferStatus(GL_FRAMEBUFFER));
}
EXPECT_GLENUM_EQ(GL_FRAMEBUFFER_COMPLETE, glCheckFramebufferStatus(GL_FRAMEBUFFER));
// use the default texture to render with while we return to the depth texture.
glBindTexture(GL_TEXTURE_2D, 0);
/* Setup 2x2 depth texture:
* 1 0.6 0.8
* |
* 0 0.2 0.4
* 0---1
*/
const GLfloat d00 = 0.2;
const GLfloat d01 = 0.4;
const GLfloat d10 = 0.6;
const GLfloat d11 = 0.8;
glEnable(GL_SCISSOR_TEST);
glScissor(0, 0, 1, 1);
glClearDepthf(d00);
glClear(GL_DEPTH_BUFFER_BIT);
glScissor(1, 0, 1, 1);
glClearDepthf(d10);
glClear(GL_DEPTH_BUFFER_BIT);
glScissor(0, 1, 1, 1);
glClearDepthf(d01);
glClear(GL_DEPTH_BUFFER_BIT);
glScissor(1, 1, 1, 1);
glClearDepthf(d11);
glClear(GL_DEPTH_BUFFER_BIT);
glDisable(GL_SCISSOR_TEST);
// render the depth texture.
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0, 0, destRes, destRes);
glBindTexture(GL_TEXTURE_2D, tex);
glDisable(GL_DITHER);
glEnable(GL_DEPTH_TEST);
glClearColor(1, 0, 0, 1);
glClearDepthf(1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0, 6);
GLubyte actualPixels[destRes * destRes * 4];
glReadPixels(0, 0, destRes, destRes, GL_RGBA, GL_UNSIGNED_BYTE, actualPixels);
const GLfloat eps = 0.002;
std::vector<GLfloat> expectedMin;
std::vector<GLfloat> expectedMax;
if (filterMode == GL_NEAREST)
{
GLfloat init[] = {d00, d00, d10, d10, d00, d00, d10, d10,
d01, d01, d11, d11, d01, d01, d11, d11};
expectedMin.insert(expectedMin.begin(), init, init + 16);
expectedMax.insert(expectedMax.begin(), init, init + 16);
for (int i = 0; i < 16; i++)
{
expectedMin[i] = expectedMin[i] - eps;
expectedMax[i] = expectedMax[i] + eps;
}
}
else
{
GLfloat initMin[] = {
d00 - eps, d00, d00, d10 - eps, d00, d00, d00, d10,
d00, d00, d00, d10, d01 - eps, d01, d01, d11 - eps,
};
GLfloat initMax[] = {
d00 + eps, d10, d10, d10 + eps, d01, d11, d11, d11,
d01, d11, d11, d11, d01 + eps, d11, d11, d11 + eps,
};
expectedMin.insert(expectedMin.begin(), initMin, initMin + 16);
expectedMax.insert(expectedMax.begin(), initMax, initMax + 16);
}
for (int yy = 0; yy < destRes; ++yy)
{
for (int xx = 0; xx < destRes; ++xx)
{
const int t = xx + destRes * yy;
const GLfloat was = (GLfloat)(actualPixels[4 * t] / 255.0); // 4bpp
const GLfloat eMin = expectedMin[t];
const GLfloat eMax = expectedMax[t];
EXPECT_TRUE(was >= eMin && was <= eMax)
<< "At " << xx << ", " << yy << ", expected within [" << eMin << ", "
<< eMax << "] was " << was;
}
}
// check limitations
// Note: What happens if current attachment type is GL_DEPTH_STENCIL_ATTACHMENT
// and you try to call glFramebufferTexture2D with GL_DEPTH_ATTACHMENT?
// The webGL test this code came from expected that to fail.
// I think due to this line in ES3 spec:
// GL_INVALID_OPERATION is generated if textarget and texture are not compatible
// However, that's not the behavior I'm seeing, nor does it seem that a depth_stencil
// buffer isn't compatible with a depth attachment (e.g. stencil is unused).
if (type.attachment == GL_DEPTH_ATTACHMENT)
{
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glFramebufferTexture2D(GL_FRAMEBUFFER, type.attachment, GL_TEXTURE_2D, 0, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D,
tex, 0);
EXPECT_GLENUM_NE(GL_NO_ERROR, glGetError());
EXPECT_GLENUM_NE(GL_FRAMEBUFFER_COMPLETE, glCheckFramebufferStatus(GL_FRAMEBUFFER));
glClear(GL_DEPTH_BUFFER_BIT);
EXPECT_GL_ERROR(GL_INVALID_FRAMEBUFFER_OPERATION);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
EXPECT_GL_NO_ERROR();
}
}
}
}
// This test will initialize a frame buffer, attaching a color and 16-bit depth buffer,
// render to it with depth testing, and verify pixel correctness.
TEST_P(DepthStencilFormatsTest, DepthBuffer16)
{
verifyDepthRenderBuffer(GL_DEPTH_COMPONENT16);
}
// This test will initialize a frame buffer, attaching a color and 24-bit depth buffer,
// render to it with depth testing, and verify pixel correctness.
TEST_P(DepthStencilFormatsTest, DepthBuffer24)
{
bool shouldHaveRenderbufferSupport = IsGLExtensionEnabled("GL_OES_depth24");
EXPECT_EQ(shouldHaveRenderbufferSupport,
checkRenderbufferFormatSupport(GL_DEPTH_COMPONENT24_OES));
if (shouldHaveRenderbufferSupport)
{
verifyDepthRenderBuffer(GL_DEPTH_COMPONENT24_OES);
}
}
TEST_P(DepthStencilFormatsTestES3, DrawWithDepth16)
{
GLushort data[16];
for (unsigned int i = 0; i < 16; i++)
{
data[i] = std::numeric_limits<GLushort>::max();
}
glBindTexture(GL_TEXTURE_2D, mTexture);
glTexStorage2D(GL_TEXTURE_2D, 1, GL_DEPTH_COMPONENT16, 4, 4);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 4, 4, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glUseProgram(mProgram);
glUniform1i(mTextureUniformLocation, 0);
glClear(GL_COLOR_BUFFER_BIT);
drawQuad(mProgram, "position", 0.5f);
ASSERT_GL_NO_ERROR();
GLubyte pixel[4];
glReadPixels(0, 0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &pixel);
// Only require the red and alpha channels have the correct values, the depth texture extensions
// leave the green and blue channels undefined
ASSERT_NEAR(255, pixel[0], 2.0);
ASSERT_EQ(255, pixel[3]);
}
// This test reproduces a driver bug on Intel windows platforms on driver version
// from 4815 to 4901.
// When rendering with Stencil buffer enabled and depth buffer disabled, large
// viewport will lead to memory leak and driver crash. And the pixel result
// is a random value.
TEST_P(DepthStencilFormatsTestES3, DrawWithLargeViewport)
{
ANGLE_SKIP_TEST_IF(IsIntel() && IsWindows());
ANGLE_GL_PROGRAM(program, essl1_shaders::vs::Simple(), essl1_shaders::fs::Red());
glEnable(GL_STENCIL_TEST);
glDisable(GL_DEPTH_TEST);
// The iteration is to reproduce memory leak when rendering several times.
for (int i = 0; i < 10; ++i)
{
// Create offscreen fbo and its color attachment and depth stencil attachment.
GLTexture framebufferColorTexture;
glBindTexture(GL_TEXTURE_2D, framebufferColorTexture);
glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, getWindowWidth(), getWindowHeight());
ASSERT_GL_NO_ERROR();
GLTexture framebufferStencilTexture;
glBindTexture(GL_TEXTURE_2D, framebufferStencilTexture);
glTexStorage2D(GL_TEXTURE_2D, 1, GL_DEPTH24_STENCIL8, getWindowWidth(), getWindowHeight());
ASSERT_GL_NO_ERROR();
GLFramebuffer fb;
glBindFramebuffer(GL_FRAMEBUFFER, fb);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
framebufferColorTexture, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D,
framebufferStencilTexture, 0);
EXPECT_GLENUM_EQ(GL_FRAMEBUFFER_COMPLETE, glCheckFramebufferStatus(GL_FRAMEBUFFER));
ASSERT_GL_NO_ERROR();
GLint kStencilRef = 4;
glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
glStencilFunc(GL_ALWAYS, kStencilRef, 0xFF);
float viewport[2];
glGetFloatv(GL_MAX_VIEWPORT_DIMS, viewport);
glViewport(0, 0, static_cast<GLsizei>(viewport[0]), static_cast<GLsizei>(viewport[1]));
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fb);
drawQuad(program, essl1_shaders::PositionAttrib(), 0.0f);
ASSERT_GL_NO_ERROR();
glBindFramebuffer(GL_READ_FRAMEBUFFER, fb);
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::red);
ASSERT_GL_NO_ERROR();
}
}
// Verify that stencil component of depth texture is uploaded
TEST_P(DepthStencilFormatsTest, VerifyDepthStencilUploadData)
{
// http://anglebug.com/42262342
// When bug is resolved we can remove this skip.
ANGLE_SKIP_TEST_IF(IsVulkan() && IsAndroid());
// http://anglebug.com/42262345
ANGLE_SKIP_TEST_IF(IsWindows() && IsVulkan() && IsAMD());
bool shouldHaveTextureSupport = (IsGLExtensionEnabled("GL_OES_packed_depth_stencil") &&
IsGLExtensionEnabled("GL_OES_depth_texture")) ||
(getClientMajorVersion() >= 3);
ANGLE_SKIP_TEST_IF(!shouldHaveTextureSupport ||
!checkTexImageFormatSupport(GL_DEPTH_STENCIL_OES, GL_UNSIGNED_INT_24_8_OES));
ANGLE_GL_PROGRAM(program, essl1_shaders::vs::Simple(), essl1_shaders::fs::Red());
glEnable(GL_STENCIL_TEST);
glDisable(GL_DEPTH_TEST);
glDepthMask(GL_FALSE);
glViewport(0, 0, getWindowWidth(), getWindowHeight());
glClearColor(0, 0, 0, 1);
// Create offscreen fbo and its color attachment and depth stencil attachment.
GLTexture framebufferColorTexture;
glBindTexture(GL_TEXTURE_2D, framebufferColorTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, getWindowWidth(), getWindowHeight(), 0, GL_RGBA,
GL_UNSIGNED_BYTE, nullptr);
ASSERT_GL_NO_ERROR();
// drawQuad's depth range is -1.0 to 1.0, so a depth value of 0.5 (0x7fffff) matches a drawQuad
// depth of 0.0.
std::vector<GLuint> depthStencilData(getWindowWidth() * getWindowHeight(), 0x7fffffA9);
GLTexture framebufferStencilTexture;
glBindTexture(GL_TEXTURE_2D, framebufferStencilTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_STENCIL, getWindowWidth(), getWindowHeight(), 0,
GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8_OES, depthStencilData.data());
ASSERT_GL_NO_ERROR();
GLFramebuffer fb;
glBindFramebuffer(GL_FRAMEBUFFER, fb);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
framebufferColorTexture, 0);
if (getClientMajorVersion() >= 3)
{
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D,
framebufferStencilTexture, 0);
ASSERT_GL_NO_ERROR();
}
else
{
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D,
framebufferStencilTexture, 0);
ASSERT_GL_NO_ERROR();
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D,
framebufferStencilTexture, 0);
ASSERT_GL_NO_ERROR();
}
ASSERT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
GLint kStencilRef = 0xA9;
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
glStencilFunc(GL_EQUAL, kStencilRef, 0xFF);
glBindFramebuffer(GL_FRAMEBUFFER, fb);
glClear(GL_COLOR_BUFFER_BIT);
drawQuad(program, essl1_shaders::PositionAttrib(), 1.0f);
ASSERT_GL_NO_ERROR();
glBindFramebuffer(GL_FRAMEBUFFER, fb);
EXPECT_PIXEL_RECT_EQ(0, 0, getWindowWidth(), getWindowHeight(), GLColor::red);
ASSERT_GL_NO_ERROR();
// Check Z values
glClear(GL_COLOR_BUFFER_BIT);
glDisable(GL_STENCIL_TEST);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
drawQuad(program, essl1_shaders::PositionAttrib(), -0.1f);
ASSERT_GL_NO_ERROR();
EXPECT_PIXEL_RECT_EQ(0, 0, getWindowWidth(), getWindowHeight(), GLColor::red);
glClear(GL_COLOR_BUFFER_BIT);
drawQuad(program, essl1_shaders::PositionAttrib(), 0.1f);
ASSERT_GL_NO_ERROR();
EXPECT_PIXEL_RECT_EQ(0, 0, getWindowWidth(), getWindowHeight(), GLColor::black);
}
// Verify that depth texture's data can be uploaded correctly
TEST_P(DepthStencilFormatsTest, VerifyDepth32UploadData)
{
ANGLE_SKIP_TEST_IF(!IsGLExtensionEnabled("GL_OES_depth_texture"));
GLTexture tex;
glBindTexture(GL_TEXTURE_2D, tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
ASSERT_GL_NO_ERROR();
// normalized 0.99 = 0xfd70a3d6
std::vector<GLuint> depthData(1, 0xfd70a3d6);
GLTexture rbDepth;
glBindTexture(GL_TEXTURE_2D, rbDepth);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, 1, 1, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT,
depthData.data());
ASSERT_GL_NO_ERROR();
GLFramebuffer fbo;
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, rbDepth, 0);
EXPECT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
ASSERT_GL_NO_ERROR();
ANGLE_GL_PROGRAM(programRed, essl1_shaders::vs::Simple(), essl1_shaders::fs::Red());
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_GEQUAL);
glClearColor(0, 1, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);
// Fail Depth Test and color buffer is unchanged
float depthValue = 0.98f;
drawQuad(programRed, essl1_shaders::PositionAttrib(), depthValue * 2 - 1);
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
// Pass Depth Test and draw red
depthValue = 1.0f;
drawQuad(programRed, essl1_shaders::PositionAttrib(), depthValue * 2 - 1);
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::red);
ASSERT_GL_NO_ERROR();
// Change depth texture data
glBindTexture(GL_TEXTURE_2D, rbDepth);
depthData[0] = 0x7fffffff; // 0.5
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 1, 1, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT,
depthData.data());
ASSERT_GL_NO_ERROR();
ANGLE_GL_PROGRAM(programGreen, essl1_shaders::vs::Simple(), essl1_shaders::fs::Green());
// Fail Depth Test and color buffer is unchanged
depthValue = 0.48f;
drawQuad(programGreen, essl1_shaders::PositionAttrib(), depthValue * 2 - 1);
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::red);
ASSERT_GL_NO_ERROR();
ANGLE_GL_PROGRAM(programBlue, essl1_shaders::vs::Simple(), essl1_shaders::fs::Blue());
glClearDepthf(0.0f);
glClear(GL_DEPTH_BUFFER_BIT);
// Pass Depth Test and draw blue
depthValue = 0.01f;
drawQuad(programBlue, essl1_shaders::PositionAttrib(), depthValue * 2 - 1);
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::blue);
glDisable(GL_DEPTH_TEST);
ASSERT_GL_NO_ERROR();
}
// Verify that 16 bits depth texture's data can be uploaded correctly
TEST_P(DepthStencilFormatsTest, VerifyDepth16UploadData)
{
ANGLE_SKIP_TEST_IF(!IsGLExtensionEnabled("GL_OES_depth_texture"));
GLTexture tex;
glBindTexture(GL_TEXTURE_2D, tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
ASSERT_GL_NO_ERROR();
// normalized 0.99 = 0xfd6f
std::vector<GLushort> depthData(1, 0xfd6f);
GLTexture rbDepth;
glBindTexture(GL_TEXTURE_2D, rbDepth);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, 1, 1, 0, GL_DEPTH_COMPONENT,
GL_UNSIGNED_SHORT, depthData.data());
ASSERT_GL_NO_ERROR();
GLFramebuffer fbo;
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, rbDepth, 0);
EXPECT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
ASSERT_GL_NO_ERROR();
ANGLE_GL_PROGRAM(programRed, essl1_shaders::vs::Simple(), essl1_shaders::fs::Red());
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_GEQUAL);
glClearColor(0, 1, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);
// Fail Depth Test and color buffer is unchanged
float depthValue = 0.98f;
drawQuad(programRed, essl1_shaders::PositionAttrib(), depthValue * 2 - 1);
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
// Pass Depth Test and draw red
depthValue = 1.0f;
drawQuad(programRed, essl1_shaders::PositionAttrib(), depthValue * 2 - 1);
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::red);
ASSERT_GL_NO_ERROR();
// Change depth texture data
glBindTexture(GL_TEXTURE_2D, rbDepth);
depthData[0] = 0x7fff; // 0.5
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 1, 1, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT,
depthData.data());
ASSERT_GL_NO_ERROR();
ANGLE_GL_PROGRAM(programGreen, essl1_shaders::vs::Simple(), essl1_shaders::fs::Green());
// Fail Depth Test and color buffer is unchanged
depthValue = 0.48f;
drawQuad(programGreen, essl1_shaders::PositionAttrib(), depthValue * 2 - 1);
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::red);
ASSERT_GL_NO_ERROR();
ANGLE_GL_PROGRAM(programBlue, essl1_shaders::vs::Simple(), essl1_shaders::fs::Blue());
glClearDepthf(0.0f);
glClear(GL_DEPTH_BUFFER_BIT);
// Pass Depth Test and draw blue
depthValue = 0.01f;
drawQuad(programBlue, essl1_shaders::PositionAttrib(), depthValue * 2 - 1);
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::blue);
glDisable(GL_DEPTH_TEST);
ASSERT_GL_NO_ERROR();
}
class DepthStencilFormatsTestES31 : public DepthStencilFormatsTestBase
{};
// Test depth24 texture sample from fragment shader and then read access from compute
TEST_P(DepthStencilFormatsTestES31, DrawReadDrawDispatch)
{
ANGLE_SKIP_TEST_IF(!IsGLExtensionEnabled("GL_OES_depth24") ||
!IsGLExtensionEnabled("GL_NV_read_depth"));
const char kVS[] = R"(#version 310 es
layout (location = 0) in vec3 pos;
void main(void) {
gl_Position = vec4(pos, 1.0);
})";
const char kFS[] = R"(#version 310 es
precision highp float;
uniform sampler2D tex;
out vec4 fragColor;
void main(void) {
float d = texture(tex,vec2(0,0)).x;
fragColor = vec4(d, 0, 0, 1);
})";
// Create depth texture
GLTexture depthTexture;
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, depthTexture);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexStorage2D(GL_TEXTURE_2D, 1, GL_DEPTH_COMPONENT24, 1, 1);
// Clear depth texture to 0.51f
GLFramebuffer framebuffer;
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthTexture, 0);
ASSERT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
GLfloat depthValue = 0.51f;
glClearDepthf(depthValue);
glClear(GL_DEPTH_BUFFER_BIT);
GLuint actualDepthValue;
GLfloat depthErrorTolerance = 0.001f;
glReadPixels(0, 0, 1, 1, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, &actualDepthValue);
EXPECT_NEAR(depthValue, actualDepthValue / (float)UINT_MAX, depthErrorTolerance);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
// Render to surface with texture sample from depth texture
ANGLE_GL_PROGRAM(graphicsProgram, kVS, kFS);
glUseProgram(graphicsProgram);
const auto &quadVertices = GetQuadVertices();
GLBuffer arrayBuffer;
glBindBuffer(GL_ARRAY_BUFFER, arrayBuffer);
glBufferData(GL_ARRAY_BUFFER, quadVertices.size() * sizeof(Vector3), quadVertices.data(),
GL_STATIC_DRAW);
GLint positionAttributeLocation = 0;
glBindAttribLocation(graphicsProgram, positionAttributeLocation, "pos");
glVertexAttribPointer(positionAttributeLocation, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(positionAttributeLocation);
GLint uTextureLocation = glGetUniformLocation(graphicsProgram, "tex");
ASSERT_NE(-1, uTextureLocation);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, depthTexture);
glUniform1i(uTextureLocation, 0);
// Sample the depth texture from fragment shader and verify. This flushes out commands which
// ensures there will be no layout transition in next renderPass.
glDepthMask(false);
glDisable(GL_DEPTH_TEST);
glDrawArrays(GL_TRIANGLES, 0, 6);
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor((GLubyte)(depthValue * 255), 0u, 0u, 255u));
// Sample the depth texture from fragment shader. No image layout transition expected here.
glDrawArrays(GL_TRIANGLES, 0, 6);
// Sample it from compute shader while the renderPass also sample from the same texture
constexpr char kCS[] = R"(#version 310 es
layout(local_size_x=1, local_size_y=1, local_size_z=1) in;
layout(std140, binding=0) buffer buf {
vec4 outData;
};
uniform sampler2D u_tex2D;
void main()
{
outData = texture(u_tex2D, vec2(gl_LocalInvocationID.xy));
})";
ANGLE_GL_COMPUTE_PROGRAM(computeProgram, kCS);
glUseProgram(computeProgram);
GLBuffer ssbo;
const std::vector<GLfloat> initialData(16, 0.0f);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo);
glBufferData(GL_SHADER_STORAGE_BUFFER, 16, initialData.data(), GL_STATIC_DRAW);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, ssbo);
glDispatchCompute(1, 1, 1);
glMemoryBarrier(GL_BUFFER_UPDATE_BARRIER_BIT);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo);
const GLfloat *ptr = reinterpret_cast<const GLfloat *>(
glMapBufferRange(GL_SHADER_STORAGE_BUFFER, 0, 16, GL_MAP_READ_BIT));
EXPECT_NEAR(depthValue, ptr[0], depthErrorTolerance);
EXPECT_GL_NO_ERROR();
}
ANGLE_INSTANTIATE_TEST_ES2(DepthStencilFormatsTest);
GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(DepthStencilFormatsTestES3);
ANGLE_INSTANTIATE_TEST_ES3(DepthStencilFormatsTestES3);
GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(DepthStencilFormatsTestES31);
ANGLE_INSTANTIATE_TEST_ES31(DepthStencilFormatsTestES31);
class TinyDepthStencilWorkaroundTest : public ANGLETest<>
{
public:
TinyDepthStencilWorkaroundTest()
{
setWindowWidth(512);
setWindowHeight(512);
setConfigRedBits(8);
setConfigGreenBits(8);
setConfigBlueBits(8);
setConfigAlphaBits(8);
}
};
// Tests that the tiny depth stencil textures workaround does not "stick" depth textures.
// http://anglebug.com/40644610
TEST_P(TinyDepthStencilWorkaroundTest, DepthTexturesStick)
{
// http://anglebug.com/40096654
ANGLE_SKIP_TEST_IF((IsAndroid() && IsOpenGLES()) || (IsLinux() && IsVulkan()));
constexpr char kDrawVS[] =
"#version 100\n"
"attribute vec3 vertex;\n"
"void main () {\n"
" gl_Position = vec4(vertex.x, vertex.y, vertex.z * 2.0 - 1.0, 1);\n"
"}\n";
ANGLE_GL_PROGRAM(drawProgram, kDrawVS, essl1_shaders::fs::Red());
constexpr char kBlitVS[] =
"#version 100\n"
"attribute vec2 vertex;\n"
"varying vec2 position;\n"
"void main () {\n"
" position = vertex * .5 + .5;\n"
" gl_Position = vec4(vertex, 0, 1);\n"
"}\n";
constexpr char kBlitFS[] =
"#version 100\n"
"precision mediump float;\n"
"uniform sampler2D texture;\n"
"varying vec2 position;\n"
"void main () {\n"
" gl_FragColor = vec4 (texture2D (texture, position).rrr, 1.);\n"
"}\n";
ANGLE_GL_PROGRAM(blitProgram, kBlitVS, kBlitFS);
GLint blitTextureLocation = glGetUniformLocation(blitProgram, "texture");
ASSERT_NE(-1, blitTextureLocation);
GLTexture colorTex;
glBindTexture(GL_TEXTURE_2D, colorTex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, getWindowWidth(), getWindowHeight(), 0, GL_RGBA,
GL_UNSIGNED_BYTE, nullptr);
glBindTexture(GL_TEXTURE_2D, 0);
GLTexture depthTex;
glBindTexture(GL_TEXTURE_2D, depthTex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
ASSERT_EQ(getWindowWidth(), getWindowHeight());
int levels = gl::log2(getWindowWidth());
for (int mipLevel = 0; mipLevel <= levels; ++mipLevel)
{
int size = getWindowWidth() >> mipLevel;
glTexImage2D(GL_TEXTURE_2D, mipLevel, GL_DEPTH_STENCIL, size, size, 0, GL_DEPTH_STENCIL,
GL_UNSIGNED_INT_24_8_OES, nullptr);
}
glBindTexture(GL_TEXTURE_2D, 0);
ASSERT_GL_NO_ERROR();
GLFramebuffer framebuffer;
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, colorTex, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthTex, 0);
ASSERT_GL_NO_ERROR();
glDepthRangef(0.0f, 1.0f);
glViewport(0, 0, getWindowWidth(), getWindowHeight());
glClearColor(0, 0, 0, 1);
// Draw loop.
for (unsigned int frame = 0; frame < 3; ++frame)
{
// draw into FBO
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
float depth = ((frame % 2 == 0) ? 0.0f : 1.0f);
drawQuad(drawProgram, "vertex", depth);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
// blit FBO
glDisable(GL_DEPTH_TEST);
glUseProgram(blitProgram);
glUniform1i(blitTextureLocation, 0);
glBindTexture(GL_TEXTURE_2D, depthTex);
drawQuad(blitProgram, "vertex", 0.5f);
Vector4 depthVec(depth, depth, depth, 1);
GLColor depthColor(depthVec);
EXPECT_PIXEL_COLOR_NEAR(0, 0, depthColor, 1);
ASSERT_GL_NO_ERROR();
}
}
// Initialize a depth texture by writing to it in a fragment shader then attempt to read it from a
// compute shader. Regression test for D3D11 not unbinding the depth texture and the sampler binding
// failing.
TEST_P(DepthStencilFormatsTestES31, ReadDepthStencilInComputeShader)
{
constexpr char kTestVertexShader[] = R"(#version 310 es
void main() {
gl_PointSize = 1.0;
vec2 pos[6] = vec2[6](vec2(-1.0f), vec2(1.0f, -1.0f), vec2(-1.0f, 1.0f), vec2(-1.0f, 1.0f), vec2(1.0f, -1.0f), vec2(1.0f));
gl_Position = vec4(pos[uint(gl_VertexID)], 0.5f, 1.0f);
return;
}
)";
constexpr char kTestFragmentShader[] = R"(#version 310 es
precision highp float;
void main()
{
gl_FragDepth = 0.51f;
return;
}
)";
constexpr char kTestComputeShader[] = R"(#version 310 es
layout(binding = 0, std430) buffer dst_buf_block_ssbo {
float inner[];
} dst_buf;
uniform highp sampler2D tex;
void blit_depth_to_buffer(uvec3 id) {
uvec2 coord = (id.xy);
uint dstOffset = (id.x + (id.y * 1u));
dst_buf.inner[dstOffset] = texelFetch(tex, ivec2(coord), 0).x;
}
layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
void main() {
blit_depth_to_buffer(gl_GlobalInvocationID);
}
)";
GLfloat depthValue = 0.51f;
GLfloat depthErrorTolerance = 0.001f;
GLTexture depthTexture;
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, depthTexture);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexStorage2D(GL_TEXTURE_2D, 1, GL_DEPTH_COMPONENT32F, 1, 1);
// Clear depth texture to depthValue
GLFramebuffer framebuffer;
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthTexture, 0);
ASSERT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
// 2. Draw and write to gl_FragDepth
ANGLE_GL_PROGRAM(graphicsProgram, kTestVertexShader, kTestFragmentShader);
glUseProgram(graphicsProgram);
EXPECT_GL_NO_ERROR();
glDrawBuffers(0, nullptr);
EXPECT_GL_NO_ERROR();
glClearDepthf(0.8);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_ALWAYS);
glDisable(GL_CULL_FACE);
glDepthMask(true);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0, 6);
EXPECT_GL_NO_ERROR();
glBindFramebuffer(GL_FRAMEBUFFER, 0);
ANGLE_GL_COMPUTE_PROGRAM(computeProgram, kTestComputeShader);
glUseProgram(computeProgram);
GLint uTextureLocation = glGetUniformLocation(computeProgram, "tex");
ASSERT_NE(-1, uTextureLocation);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, depthTexture);
glUniform1i(uTextureLocation, 0);
GLBuffer ssbo;
std::array<GLfloat, 16> initialData{0};
glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo);
glBufferData(GL_SHADER_STORAGE_BUFFER, 16, initialData.data(), GL_STATIC_DRAW);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, ssbo);
glDispatchCompute(1, 1, 1);
glMemoryBarrier(GL_BUFFER_UPDATE_BARRIER_BIT);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo);
const GLfloat *ptr = reinterpret_cast<const GLfloat *>(
glMapBufferRange(GL_SHADER_STORAGE_BUFFER, 0, 16, GL_MAP_READ_BIT));
EXPECT_NEAR(depthValue, ptr[0], depthErrorTolerance);
EXPECT_GL_NO_ERROR();
}
GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(TinyDepthStencilWorkaroundTest);
ANGLE_INSTANTIATE_TEST_ES3_AND(TinyDepthStencilWorkaroundTest,
ES3_D3D11().enable(Feature::EmulateTinyStencilTextures));