Hash :
b3846714
Author :
Date :
2022-11-28T11:05:05
Vulkan: Feature to make async queue slow for testing Bug: angleproject:6746 Change-Id: I2573cae2dcf42d177168c55bc2a6d8bb012dde18 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/4227986 Auto-Submit: Shahbaz Youssefi <syoussefi@chromium.org> Commit-Queue: Shahbaz Youssefi <syoussefi@chromium.org> Reviewed-by: Charlie Lao <cclao@google.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 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997
//
// Copyright 2018 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.
//
// MulithreadingTest.cpp : Tests of multithreaded rendering
#include "test_utils/ANGLETest.h"
#include "test_utils/MultiThreadSteps.h"
#include "test_utils/gl_raii.h"
#include "util/EGLWindow.h"
#include "util/test_utils.h"
#include <atomic>
#include <mutex>
#include <thread>
namespace angle
{
class MultithreadingTest : public ANGLETest<>
{
public:
static constexpr uint32_t kSize = 512;
protected:
MultithreadingTest()
{
setWindowWidth(kSize);
setWindowHeight(kSize);
setConfigRedBits(8);
setConfigGreenBits(8);
setConfigBlueBits(8);
setConfigAlphaBits(8);
}
bool hasFenceSyncExtension() const
{
return IsEGLDisplayExtensionEnabled(getEGLWindow()->getDisplay(), "EGL_KHR_fence_sync");
}
bool hasGLSyncExtension() const { return IsGLExtensionEnabled("GL_OES_EGL_sync"); }
EGLContext createMultithreadedContext(EGLWindow *window, EGLContext shareCtx)
{
EGLint attribs[] = {EGL_CONTEXT_VIRTUALIZATION_GROUP_ANGLE, mVirtualizationGroup++,
EGL_NONE};
if (!IsEGLDisplayExtensionEnabled(getEGLWindow()->getDisplay(),
"EGL_ANGLE_context_virtualization"))
{
attribs[0] = EGL_NONE;
}
return window->createContext(shareCtx, attribs);
}
void runMultithreadedGLTest(
std::function<void(EGLSurface surface, size_t threadIndex)> testBody,
size_t threadCount)
{
std::mutex mutex;
EGLWindow *window = getEGLWindow();
EGLDisplay dpy = window->getDisplay();
EGLConfig config = window->getConfig();
constexpr EGLint kPBufferSize = 256;
std::vector<std::thread> threads(threadCount);
for (size_t threadIdx = 0; threadIdx < threadCount; threadIdx++)
{
threads[threadIdx] = std::thread([&, threadIdx]() {
EGLSurface surface = EGL_NO_SURFACE;
EGLContext ctx = EGL_NO_CONTEXT;
{
std::lock_guard<decltype(mutex)> lock(mutex);
// Initialize the pbuffer and context
EGLint pbufferAttributes[] = {
EGL_WIDTH, kPBufferSize, EGL_HEIGHT, kPBufferSize, EGL_NONE, EGL_NONE,
};
surface = eglCreatePbufferSurface(dpy, config, pbufferAttributes);
EXPECT_EGL_SUCCESS();
ctx = createMultithreadedContext(window, EGL_NO_CONTEXT);
EXPECT_NE(EGL_NO_CONTEXT, ctx);
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, surface, surface, ctx));
EXPECT_EGL_SUCCESS();
}
testBody(surface, threadIdx);
{
std::lock_guard<decltype(mutex)> lock(mutex);
// Clean up
EXPECT_EGL_TRUE(
eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
EXPECT_EGL_SUCCESS();
eglDestroySurface(dpy, surface);
eglDestroyContext(dpy, ctx);
}
});
}
for (std::thread &thread : threads)
{
thread.join();
}
}
std::atomic<EGLint> mVirtualizationGroup;
};
class MultithreadingTestES3 : public MultithreadingTest
{
public:
void textureThreadFunction(bool useDraw);
void mainThreadDraw(bool useDraw);
protected:
MultithreadingTestES3()
: mTexture2D(0), mExitThread(false), mMainThreadSyncObj(NULL), mSecondThreadSyncObj(NULL)
{
setWindowWidth(kSize);
setWindowHeight(kSize);
setConfigRedBits(8);
setConfigGreenBits(8);
setConfigBlueBits(8);
setConfigAlphaBits(8);
}
GLuint create2DTexture()
{
GLuint texture2D;
glGenTextures(1, &texture2D);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture2D);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, kSize, kSize, 0, GL_RGBA, GL_UNSIGNED_BYTE,
nullptr);
EXPECT_GL_NO_ERROR();
return texture2D;
}
void testSetUp() override { mTexture2D = create2DTexture(); }
void testTearDown() override
{
if (mTexture2D)
{
glDeleteTextures(1, &mTexture2D);
}
}
enum class FenceTest
{
ClientWait,
ServerWait,
GetStatus,
};
enum class FlushMethod
{
Flush,
Finish,
};
void testFenceWithOpenRenderPass(FenceTest test, FlushMethod flushMethod);
enum class DrawOrder
{
Before,
After,
};
void testFramebufferFetch(DrawOrder drawOrder);
std::mutex mMutex;
GLuint mTexture2D;
std::atomic<bool> mExitThread;
std::atomic<bool> mDrawGreen; // Toggle drawing green or red
std::atomic<GLsync> mMainThreadSyncObj;
std::atomic<GLsync> mSecondThreadSyncObj;
};
// Test that it's possible to make one context current on different threads
TEST_P(MultithreadingTest, MakeCurrentSingleContext)
{
ANGLE_SKIP_TEST_IF(!platformSupportsMultithreading());
std::mutex mutex;
EGLWindow *window = getEGLWindow();
EGLDisplay dpy = window->getDisplay();
EGLContext ctx = window->getContext();
EGLSurface surface = window->getSurface();
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
EXPECT_EGL_SUCCESS();
constexpr size_t kThreadCount = 16;
std::array<std::thread, kThreadCount> threads;
for (std::thread &thread : threads)
{
thread = std::thread([&]() {
std::lock_guard<decltype(mutex)> lock(mutex);
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, surface, surface, ctx));
EXPECT_EGL_SUCCESS();
EXPECT_EGL_TRUE(eglSwapBuffers(dpy, surface));
EXPECT_EGL_SUCCESS();
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
EXPECT_EGL_SUCCESS();
});
}
for (std::thread &thread : threads)
{
thread.join();
}
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, surface, surface, ctx));
EXPECT_EGL_SUCCESS();
}
// Test that multiple threads can clear and readback pixels successfully at the same time
TEST_P(MultithreadingTest, MultiContextClear)
{
ANGLE_SKIP_TEST_IF(!platformSupportsMultithreading());
auto testBody = [](EGLSurface surface, size_t thread) {
constexpr size_t kIterationsPerThread = 32;
for (size_t iteration = 0; iteration < kIterationsPerThread; iteration++)
{
// Base the clear color on the thread and iteration indexes so every clear color is
// unique
const GLColor color(static_cast<GLubyte>(thread % 255),
static_cast<GLubyte>(iteration % 255), 0, 255);
const angle::Vector4 floatColor = color.toNormalizedVector();
glClearColor(floatColor[0], floatColor[1], floatColor[2], floatColor[3]);
EXPECT_GL_NO_ERROR();
glClear(GL_COLOR_BUFFER_BIT);
EXPECT_GL_NO_ERROR();
EXPECT_PIXEL_COLOR_EQ(0, 0, color);
}
};
runMultithreadedGLTest(
testBody,
getEGLWindow()->isFeatureEnabled(Feature::SlowAsyncCommandQueueForTesting) ? 4 : 72);
}
// Verify that threads can interleave eglDestroyContext and draw calls without
// any crashes.
TEST_P(MultithreadingTest, MultiContextDeleteDraw)
{
// Skip this test on non-D3D11 backends, as it has the potential to time-out
// and this test was originally intended to catch a crash on the D3D11 backend.
ANGLE_SKIP_TEST_IF(!platformSupportsMultithreading());
ANGLE_SKIP_TEST_IF(!IsD3D11());
EGLWindow *window = getEGLWindow();
EGLDisplay dpy = window->getDisplay();
EGLConfig config = window->getConfig();
std::thread t1 = std::thread([&]() {
// 5000 is chosen here as it reliably reproduces the former crash.
for (int i = 0; i < 5000; i++)
{
EGLContext ctx1 = createMultithreadedContext(window, EGL_NO_CONTEXT);
EGLContext ctx2 = createMultithreadedContext(window, EGL_NO_CONTEXT);
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, ctx2));
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, ctx1));
EXPECT_EGL_TRUE(eglDestroyContext(dpy, ctx2));
EXPECT_EGL_TRUE(eglDestroyContext(dpy, ctx1));
}
});
std::thread t2 = std::thread([&]() {
EGLint pbufferAttributes[] = {
EGL_WIDTH, 256, EGL_HEIGHT, 256, EGL_NONE, EGL_NONE,
};
EGLSurface surface = eglCreatePbufferSurface(dpy, config, pbufferAttributes);
EXPECT_EGL_SUCCESS();
auto ctx = createMultithreadedContext(window, EGL_NO_CONTEXT);
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, surface, surface, ctx));
constexpr size_t kIterationsPerThread = 512;
constexpr size_t kDrawsPerIteration = 512;
ANGLE_GL_PROGRAM(program, essl1_shaders::vs::Simple(), essl1_shaders::fs::UniformColor());
glUseProgram(program);
GLint colorLocation = glGetUniformLocation(program, essl1_shaders::ColorUniform());
auto quadVertices = GetQuadVertices();
GLBuffer vertexBuffer;
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 3 * 6, quadVertices.data(), GL_STATIC_DRAW);
GLint positionLocation = glGetAttribLocation(program, essl1_shaders::PositionAttrib());
glEnableVertexAttribArray(positionLocation);
glVertexAttribPointer(positionLocation, 3, GL_FLOAT, GL_FALSE, 0, 0);
for (size_t iteration = 0; iteration < kIterationsPerThread; iteration++)
{
const GLColor color(static_cast<GLubyte>(15151 % 255),
static_cast<GLubyte>(iteration % 255), 0, 255);
const angle::Vector4 floatColor = color.toNormalizedVector();
glUniform4fv(colorLocation, 1, floatColor.data());
for (size_t draw = 0; draw < kDrawsPerIteration; draw++)
{
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, surface, surface, ctx));
glDrawArrays(GL_TRIANGLES, 0, 6);
}
}
});
t1.join();
t2.join();
}
// Test that multiple threads can draw and readback pixels successfully at the same time
TEST_P(MultithreadingTest, MultiContextDraw)
{
ANGLE_SKIP_TEST_IF(!platformSupportsMultithreading());
ANGLE_SKIP_TEST_IF(isSwiftshader());
auto testBody = [](EGLSurface surface, size_t thread) {
constexpr size_t kIterationsPerThread = 32;
constexpr size_t kDrawsPerIteration = 500;
ANGLE_GL_PROGRAM(program, essl1_shaders::vs::Simple(), essl1_shaders::fs::UniformColor());
glUseProgram(program);
GLint colorLocation = glGetUniformLocation(program, essl1_shaders::ColorUniform());
auto quadVertices = GetQuadVertices();
GLBuffer vertexBuffer;
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 3 * 6, quadVertices.data(), GL_STATIC_DRAW);
GLint positionLocation = glGetAttribLocation(program, essl1_shaders::PositionAttrib());
glEnableVertexAttribArray(positionLocation);
glVertexAttribPointer(positionLocation, 3, GL_FLOAT, GL_FALSE, 0, 0);
for (size_t iteration = 0; iteration < kIterationsPerThread; iteration++)
{
// Base the clear color on the thread and iteration indexes so every clear color is
// unique
const GLColor color(static_cast<GLubyte>(thread % 255),
static_cast<GLubyte>(iteration % 255), 0, 255);
const angle::Vector4 floatColor = color.toNormalizedVector();
glUniform4fv(colorLocation, 1, floatColor.data());
for (size_t draw = 0; draw < kDrawsPerIteration; draw++)
{
glDrawArrays(GL_TRIANGLES, 0, 6);
}
EXPECT_PIXEL_COLOR_EQ(0, 0, color);
}
};
runMultithreadedGLTest(testBody, 4);
}
// Test that multiple threads can draw and read back pixels correctly.
// Using eglSwapBuffers stresses race conditions around use of QueueSerials.
TEST_P(MultithreadingTest, MultiContextDrawWithSwapBuffers)
{
ANGLE_SKIP_TEST_IF(!platformSupportsMultithreading());
// http://anglebug.com/5099
ANGLE_SKIP_TEST_IF(IsAndroid() && IsOpenGLES());
EGLWindow *window = getEGLWindow();
EGLDisplay dpy = window->getDisplay();
auto testBody = [dpy](EGLSurface surface, size_t thread) {
constexpr size_t kIterationsPerThread = 100;
constexpr size_t kDrawsPerIteration = 10;
ANGLE_GL_PROGRAM(program, essl1_shaders::vs::Simple(), essl1_shaders::fs::UniformColor());
glUseProgram(program);
GLint colorLocation = glGetUniformLocation(program, essl1_shaders::ColorUniform());
auto quadVertices = GetQuadVertices();
GLBuffer vertexBuffer;
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 3 * 6, quadVertices.data(), GL_STATIC_DRAW);
GLint positionLocation = glGetAttribLocation(program, essl1_shaders::PositionAttrib());
glEnableVertexAttribArray(positionLocation);
glVertexAttribPointer(positionLocation, 3, GL_FLOAT, GL_FALSE, 0, 0);
for (size_t iteration = 0; iteration < kIterationsPerThread; iteration++)
{
// Base the clear color on the thread and iteration indexes so every clear color is
// unique
const GLColor color(static_cast<GLubyte>(thread % 255),
static_cast<GLubyte>(iteration % 255), 0, 255);
const angle::Vector4 floatColor = color.toNormalizedVector();
glUniform4fv(colorLocation, 1, floatColor.data());
for (size_t draw = 0; draw < kDrawsPerIteration; draw++)
{
glDrawArrays(GL_TRIANGLES, 0, 6);
}
EXPECT_EGL_TRUE(eglSwapBuffers(dpy, surface));
EXPECT_EGL_SUCCESS();
EXPECT_PIXEL_COLOR_EQ(0, 0, color);
}
};
runMultithreadedGLTest(
testBody,
getEGLWindow()->isFeatureEnabled(Feature::SlowAsyncCommandQueueForTesting) ? 4 : 32);
}
// Test that ANGLE handles multiple threads creating and destroying resources (vertex buffer in this
// case). Disable defer_flush_until_endrenderpass so that glFlush will issue work to GPU in order to
// maximize the chance we resources can be destroyed at the wrong time.
TEST_P(MultithreadingTest, MultiContextCreateAndDeleteResources)
{
ANGLE_SKIP_TEST_IF(!platformSupportsMultithreading());
EGLWindow *window = getEGLWindow();
EGLDisplay dpy = window->getDisplay();
auto testBody = [dpy](EGLSurface surface, size_t thread) {
constexpr size_t kIterationsPerThread = 32;
constexpr size_t kDrawsPerIteration = 1;
ANGLE_GL_PROGRAM(program, essl1_shaders::vs::Simple(), essl1_shaders::fs::UniformColor());
glUseProgram(program);
GLint colorLocation = glGetUniformLocation(program, essl1_shaders::ColorUniform());
auto quadVertices = GetQuadVertices();
for (size_t iteration = 0; iteration < kIterationsPerThread; iteration++)
{
GLBuffer vertexBuffer;
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 3 * 6, quadVertices.data(),
GL_STATIC_DRAW);
GLint positionLocation = glGetAttribLocation(program, essl1_shaders::PositionAttrib());
glEnableVertexAttribArray(positionLocation);
glVertexAttribPointer(positionLocation, 3, GL_FLOAT, GL_FALSE, 0, 0);
// Base the clear color on the thread and iteration indexes so every clear color is
// unique
const GLColor color(static_cast<GLubyte>(thread % 255),
static_cast<GLubyte>(iteration % 255), 0, 255);
const angle::Vector4 floatColor = color.toNormalizedVector();
glUniform4fv(colorLocation, 1, floatColor.data());
for (size_t draw = 0; draw < kDrawsPerIteration; draw++)
{
glDrawArrays(GL_TRIANGLES, 0, 6);
}
EXPECT_EGL_TRUE(eglSwapBuffers(dpy, surface));
EXPECT_EGL_SUCCESS();
EXPECT_PIXEL_COLOR_EQ(0, 0, color);
}
glFinish();
};
runMultithreadedGLTest(
testBody,
getEGLWindow()->isFeatureEnabled(Feature::SlowAsyncCommandQueueForTesting) ? 4 : 32);
}
TEST_P(MultithreadingTest, MultiCreateContext)
{
// Supported by CGL, GLX, and WGL (https://anglebug.com/4725)
// Not supported on Ozone (https://crbug.com/1103009)
ANGLE_SKIP_TEST_IF(!(IsWindows() || IsLinux() || IsOSX()) || IsOzone());
EGLWindow *window = getEGLWindow();
EGLDisplay dpy = window->getDisplay();
EGLContext ctx = window->getContext();
EGLSurface surface = window->getSurface();
// Un-makeCurrent the test window's context
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
EXPECT_EGL_SUCCESS();
constexpr size_t kThreadCount = 16;
std::atomic<uint32_t> barrier(0);
std::vector<std::thread> threads(kThreadCount);
std::vector<EGLContext> contexts(kThreadCount);
for (size_t threadIdx = 0; threadIdx < kThreadCount; threadIdx++)
{
threads[threadIdx] = std::thread([&, threadIdx]() {
contexts[threadIdx] = EGL_NO_CONTEXT;
{
contexts[threadIdx] = createMultithreadedContext(window, EGL_NO_CONTEXT);
EXPECT_NE(EGL_NO_CONTEXT, contexts[threadIdx]);
barrier++;
}
while (barrier < kThreadCount)
{
}
{
EXPECT_TRUE(eglDestroyContext(dpy, contexts[threadIdx]));
}
});
}
for (std::thread &thread : threads)
{
thread.join();
}
// Re-make current the test window's context for teardown.
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, surface, surface, ctx));
EXPECT_EGL_SUCCESS();
}
// Create multiple shared context and draw with shared vertex buffer simutanously
TEST_P(MultithreadingTest, CreateMultiSharedContextAndDraw)
{
// Supported by CGL, GLX, and WGL (https://anglebug.com/4725)
// Not supported on Ozone (https://crbug.com/1103009)
ANGLE_SKIP_TEST_IF(!(IsWindows() || IsLinux() || IsOSX()) || IsOzone());
EGLWindow *window = getEGLWindow();
EGLDisplay dpy = window->getDisplay();
EGLConfig config = window->getConfig();
constexpr EGLint kPBufferSize = 256;
// Initialize the pbuffer and context
EGLint pbufferAttributes[] = {
EGL_WIDTH, kPBufferSize, EGL_HEIGHT, kPBufferSize, EGL_NONE, EGL_NONE,
};
EGLSurface sharedSurface = eglCreatePbufferSurface(dpy, config, pbufferAttributes);
EXPECT_EGL_SUCCESS();
EGLContext sharedCtx = createMultithreadedContext(window, EGL_NO_CONTEXT);
EXPECT_NE(EGL_NO_CONTEXT, sharedCtx);
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, sharedSurface, sharedSurface, sharedCtx));
EXPECT_EGL_SUCCESS();
// Create a shared vertextBuffer
auto quadVertices = GetQuadVertices();
GLBuffer sharedVertexBuffer;
glBindBuffer(GL_ARRAY_BUFFER, sharedVertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 3 * 6, quadVertices.data(), GL_STATIC_DRAW);
ASSERT_GL_NO_ERROR();
// Now draw with the buffer and verify
{
ANGLE_GL_PROGRAM(sharedProgram, essl1_shaders::vs::Simple(),
essl1_shaders::fs::UniformColor());
glUseProgram(sharedProgram);
GLint colorLocation = glGetUniformLocation(sharedProgram, essl1_shaders::ColorUniform());
GLint positionLocation =
glGetAttribLocation(sharedProgram, essl1_shaders::PositionAttrib());
glEnableVertexAttribArray(positionLocation);
glVertexAttribPointer(positionLocation, 3, GL_FLOAT, GL_FALSE, 0, 0);
const GLColor color(0, 0, 0, 255);
const angle::Vector4 floatColor = color.toNormalizedVector();
glUniform4fv(colorLocation, 1, floatColor.data());
glDrawArrays(GL_TRIANGLES, 0, 6);
EXPECT_PIXEL_COLOR_EQ(0, 0, color);
}
// Create shared context in their own threads and draw with the shared vertex buffer at the same
// time.
size_t threadCount = 16;
constexpr size_t kIterationsPerThread = 3;
constexpr size_t kDrawsPerIteration = 50;
std::vector<std::thread> threads(threadCount);
std::atomic<uint32_t> numOfContextsCreated(0);
std::mutex mutex;
for (size_t threadIdx = 0; threadIdx < threadCount; threadIdx++)
{
threads[threadIdx] = std::thread([&, threadIdx]() {
EGLSurface surface = EGL_NO_SURFACE;
EGLContext ctx = EGL_NO_CONTEXT;
{
std::lock_guard<decltype(mutex)> lock(mutex);
// Initialize the pbuffer and context
surface = eglCreatePbufferSurface(dpy, config, pbufferAttributes);
EXPECT_EGL_SUCCESS();
ctx = createMultithreadedContext(window, /*EGL_NO_CONTEXT*/ sharedCtx);
EXPECT_NE(EGL_NO_CONTEXT, ctx);
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, surface, surface, ctx));
EXPECT_EGL_SUCCESS();
numOfContextsCreated++;
}
// Wait for all contexts created.
while (numOfContextsCreated < threadCount)
{
}
// Now draw with shared vertex buffer
{
ANGLE_GL_PROGRAM(program, essl1_shaders::vs::Simple(),
essl1_shaders::fs::UniformColor());
glUseProgram(program);
GLint colorLocation = glGetUniformLocation(program, essl1_shaders::ColorUniform());
GLint positionLocation =
glGetAttribLocation(program, essl1_shaders::PositionAttrib());
// Use sharedVertexBuffer
glBindBuffer(GL_ARRAY_BUFFER, sharedVertexBuffer);
glEnableVertexAttribArray(positionLocation);
glVertexAttribPointer(positionLocation, 3, GL_FLOAT, GL_FALSE, 0, 0);
for (size_t iteration = 0; iteration < kIterationsPerThread; iteration++)
{
// Base the clear color on the thread and iteration indexes so every clear color
// is unique
const GLColor color(static_cast<GLubyte>(threadIdx % 255),
static_cast<GLubyte>(iteration % 255), 0, 255);
const angle::Vector4 floatColor = color.toNormalizedVector();
glUniform4fv(colorLocation, 1, floatColor.data());
for (size_t draw = 0; draw < kDrawsPerIteration; draw++)
{
glDrawArrays(GL_TRIANGLES, 0, 6);
}
EXPECT_PIXEL_COLOR_EQ(0, 0, color);
}
}
// tear down shared context
{
std::lock_guard<decltype(mutex)> lock(mutex);
EXPECT_EGL_TRUE(
eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
EXPECT_EGL_SUCCESS();
eglDestroySurface(dpy, surface);
eglDestroyContext(dpy, ctx);
}
});
}
for (std::thread &thread : threads)
{
thread.join();
}
eglDestroySurface(dpy, sharedSurface);
eglDestroyContext(dpy, sharedCtx);
// Re-make current the test window's context for teardown.
EXPECT_EGL_TRUE(
eglMakeCurrent(dpy, window->getSurface(), window->getSurface(), window->getContext()));
EXPECT_EGL_SUCCESS();
}
void MultithreadingTestES3::textureThreadFunction(bool useDraw)
{
EGLWindow *window = getEGLWindow();
EGLDisplay dpy = window->getDisplay();
EGLConfig config = window->getConfig();
EGLSurface surface = EGL_NO_SURFACE;
EGLContext ctx = EGL_NO_CONTEXT;
// Initialize the pbuffer and context
EGLint pbufferAttributes[] = {
EGL_WIDTH, kSize, EGL_HEIGHT, kSize, EGL_NONE, EGL_NONE,
};
surface = eglCreatePbufferSurface(dpy, config, pbufferAttributes);
EXPECT_EGL_SUCCESS();
EXPECT_NE(EGL_NO_SURFACE, surface);
ctx = createMultithreadedContext(window, window->getContext());
EXPECT_NE(EGL_NO_CONTEXT, ctx);
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, surface, surface, ctx));
EXPECT_EGL_SUCCESS();
std::vector<GLColor> greenColor(kSize * kSize, GLColor::green);
std::vector<GLColor> redColor(kSize * kSize, GLColor::red);
ANGLE_GL_PROGRAM(greenProgram, essl1_shaders::vs::Simple(), essl1_shaders::fs::Green());
ANGLE_GL_PROGRAM(redProgram, essl1_shaders::vs::Simple(), essl1_shaders::fs::Red());
glBindTexture(GL_TEXTURE_2D, mTexture2D);
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, kSize, kSize, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
ASSERT_GL_NO_ERROR();
GLFramebuffer fbo;
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, mTexture2D, 0);
ASSERT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
mSecondThreadSyncObj = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
ASSERT_GL_NO_ERROR();
// Force the fence to be created
glFlush();
// Draw something
while (!mExitThread)
{
std::lock_guard<decltype(mMutex)> lock(mMutex);
if (mMainThreadSyncObj != nullptr)
{
glWaitSync(mMainThreadSyncObj, 0, GL_TIMEOUT_IGNORED);
ASSERT_GL_NO_ERROR();
glDeleteSync(mMainThreadSyncObj);
ASSERT_GL_NO_ERROR();
mMainThreadSyncObj = nullptr;
}
else
{
continue;
}
glBindTexture(GL_TEXTURE_2D, mTexture2D);
ASSERT_GL_NO_ERROR();
if (mDrawGreen)
{
if (useDraw)
{
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
drawQuad(greenProgram, essl1_shaders::PositionAttrib(), 0.0f);
}
else
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, kSize, kSize, 0, GL_RGBA, GL_UNSIGNED_BYTE,
greenColor.data());
}
ASSERT_GL_NO_ERROR();
}
else
{
if (useDraw)
{
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
drawQuad(redProgram, essl1_shaders::PositionAttrib(), 0.0f);
}
else
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, kSize, kSize, 0, GL_RGBA, GL_UNSIGNED_BYTE,
redColor.data());
}
ASSERT_GL_NO_ERROR();
}
ASSERT_EQ(mSecondThreadSyncObj.load(), nullptr);
mSecondThreadSyncObj = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
ASSERT_GL_NO_ERROR();
// Force the fence to be created
glFlush();
mDrawGreen = !mDrawGreen;
}
// Clean up
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
EXPECT_EGL_SUCCESS();
eglDestroySurface(dpy, surface);
eglDestroyContext(dpy, ctx);
}
// Test fence sync with multiple threads drawing
void MultithreadingTestES3::mainThreadDraw(bool useDraw)
{
ANGLE_SKIP_TEST_IF(!platformSupportsMultithreading());
EGLWindow *window = getEGLWindow();
EGLDisplay dpy = window->getDisplay();
EGLContext ctx = window->getContext();
EGLSurface surface = window->getSurface();
// Use odd numbers so we bounce between red and green in the final image
constexpr int kNumIterations = 5;
constexpr int kNumDraws = 5;
mDrawGreen = false;
std::thread textureThread(&MultithreadingTestES3::textureThreadFunction, this, true);
ANGLE_GL_PROGRAM(texProgram, essl1_shaders::vs::Texture2D(), essl1_shaders::fs::Texture2D());
for (int iterations = 0; iterations < kNumIterations; ++iterations)
{
for (int draws = 0; draws < kNumDraws;)
{
std::lock_guard<decltype(mMutex)> lock(mMutex);
if (mSecondThreadSyncObj != nullptr)
{
glWaitSync(mSecondThreadSyncObj, 0, GL_TIMEOUT_IGNORED);
ASSERT_GL_NO_ERROR();
glDeleteSync(mSecondThreadSyncObj);
ASSERT_GL_NO_ERROR();
mSecondThreadSyncObj = nullptr;
}
else
{
continue;
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glBindTexture(GL_TEXTURE_2D, mTexture2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glUseProgram(texProgram);
drawQuad(texProgram, essl1_shaders::PositionAttrib(), 0.0f);
ASSERT_EQ(mMainThreadSyncObj.load(), nullptr);
mMainThreadSyncObj = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
ASSERT_GL_NO_ERROR();
// Force the fence to be created
glFlush();
++draws;
}
ASSERT_GL_NO_ERROR();
swapBuffers();
}
mExitThread = true;
textureThread.join();
ASSERT_GL_NO_ERROR();
GLColor color;
if (mDrawGreen)
{
color = GLColor::green;
}
else
{
color = GLColor::red;
}
EXPECT_PIXEL_RECT_EQ(0, 0, kSize, kSize, color);
// Re-make current the test window's context for teardown.
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, surface, surface, ctx));
EXPECT_EGL_SUCCESS();
}
// Test that glFenceSync/glWaitSync works correctly with multithreading.
// Main thread: Samples from the shared texture to draw to the default FBO.
// Secondary (Texture) thread: Draws to the shared texture, which the Main thread samples from.
// The overall execution flow is:
// Main Thread:
// 1. Wait for the mSecondThreadSyncObj fence object to be created.
// - This fence object is used by synchronize access to the shared texture by indicating that the
// Secondary thread's draws to the texture have all completed and it's now safe to sample from
// it.
// 2. Once the fence is created, add a glWaitSync(mSecondThreadSyncObj) to the command stream and
// then delete it.
// 3. Draw, sampling from the shared texture.
// 4. Create a new mMainThreadSyncObj.
// - This fence object is used to synchronize access to the shared texture by indicating that the
// Main thread's draws are no longer sampling from the texture, so it's now safe for the
// Secondary thread to draw to it again with a new color.
// Secondary (Texture) Thread:
// 1. Wait for the mMainThreadSyncObj fence object to be created.
// 2. Once the fence is created, add a glWaitSync(mMainThreadSyncObj) to the command stream and then
// delete it.
// 3. Draw/Fill the texture.
// 4. Create a new mSecondThreadSyncObj.
//
// These threads loop for the specified number of iterations, drawing/sampling the shared texture
// with the necessary glFlush()s and occasional eglSwapBuffers() to mimic a real multithreaded GLES
// application.
TEST_P(MultithreadingTestES3, MultithreadFenceDraw)
{
// http://anglebug.com/5418
ANGLE_SKIP_TEST_IF(IsLinux() && IsVulkan() && (IsIntel() || isSwiftshader()));
// Have the secondary thread use glDrawArrays()
mainThreadDraw(true);
}
// Same as MultithreadFenceDraw, but with the secondary thread using glTexImage2D rather than
// glDrawArrays.
TEST_P(MultithreadingTestES3, MultithreadFenceTexImage)
{
// http://anglebug.com/5418
ANGLE_SKIP_TEST_IF(IsLinux() && IsIntel() && IsVulkan());
// http://anglebug.com/5439
ANGLE_SKIP_TEST_IF(IsLinux() && isSwiftshader());
// Have the secondary thread use glTexImage2D()
mainThreadDraw(false);
}
// Test that waiting on a sync object that hasn't been flushed and without a current context returns
// TIMEOUT_EXPIRED or CONDITION_SATISFIED, but doesn't generate an error or crash.
TEST_P(MultithreadingTest, NoFlushNoContextReturnsTimeout)
{
ANGLE_SKIP_TEST_IF(!platformSupportsMultithreading());
ANGLE_SKIP_TEST_IF(!hasFenceSyncExtension() || !hasGLSyncExtension());
std::mutex mutex;
EGLWindow *window = getEGLWindow();
EGLDisplay dpy = window->getDisplay();
glClearColor(1.0f, 0.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
EGLSyncKHR sync = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, nullptr);
EXPECT_NE(sync, EGL_NO_SYNC_KHR);
std::thread thread = std::thread([&]() {
std::lock_guard<decltype(mutex)> lock(mutex);
// Make sure there is no active context on this thread.
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
EXPECT_EGL_SUCCESS();
// Don't wait forever to make sure the test terminates
constexpr GLuint64 kTimeout = 1'000'000'000; // 1 second
int result = eglClientWaitSyncKHR(dpy, sync, 0, kTimeout);
// We typically expect to get back TIMEOUT_EXPIRED since the sync object was never flushed.
// However, the OpenGL ES backend returns CONDITION_SATISFIED, which is also a passing
// result.
ASSERT_TRUE(result == EGL_TIMEOUT_EXPIRED_KHR || result == EGL_CONDITION_SATISFIED_KHR);
});
thread.join();
EXPECT_EGL_TRUE(eglDestroySyncKHR(dpy, sync));
}
// Test that waiting on sync object that hasn't been flushed yet, but is later flushed by another
// thread, correctly returns when the fence is signalled without a timeout.
TEST_P(MultithreadingTest, CreateFenceThreadAClientWaitSyncThreadBDelayedFlush)
{
ANGLE_SKIP_TEST_IF(!platformSupportsMultithreading());
ANGLE_SKIP_TEST_IF(!hasFenceSyncExtension() || !hasGLSyncExtension());
EGLSyncKHR sync = EGL_NO_SYNC_KHR;
std::mutex mutex;
std::condition_variable condVar;
enum class Step
{
Start,
Thread0Clear,
Thread1CreateFence,
Thread0ClientWaitSync,
Thread1Flush,
Finish,
Abort,
};
Step currentStep = Step::Start;
auto thread0 = [&](EGLDisplay dpy, EGLSurface surface, EGLContext context) {
ThreadSynchronization<Step> threadSynchronization(¤tStep, &mutex, &condVar);
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Start));
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, surface, surface, context));
// Do work.
glClearColor(1.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
// Wait for thread 1 to clear.
threadSynchronization.nextStep(Step::Thread0Clear);
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Thread1CreateFence));
// Wait on the sync object, but do *not* flush it, since the other thread will flush.
constexpr GLuint64 kTimeout = 2'000'000'000; // 2 seconds
threadSynchronization.nextStep(Step::Thread0ClientWaitSync);
ASSERT_EQ(EGL_CONDITION_SATISFIED_KHR, eglClientWaitSyncKHR(dpy, sync, 0, kTimeout));
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Finish));
};
auto thread1 = [&](EGLDisplay dpy, EGLSurface surface, EGLContext context) {
ThreadSynchronization<Step> threadSynchronization(¤tStep, &mutex, &condVar);
// Wait for thread 0 to clear.
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Thread0Clear));
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, surface, surface, context));
// Do work.
glClearColor(0.0, 1.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
sync = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, nullptr);
EXPECT_NE(sync, EGL_NO_SYNC_KHR);
// Wait for the thread 0 to eglClientWaitSyncKHR().
threadSynchronization.nextStep(Step::Thread1CreateFence);
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Thread0ClientWaitSync));
// Wait a little to give thread 1 time to wait on the sync object before flushing it.
angle::Sleep(500);
glFlush();
// Clean up
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
threadSynchronization.nextStep(Step::Finish);
};
std::array<LockStepThreadFunc, 2> threadFuncs = {
std::move(thread0),
std::move(thread1),
};
RunLockStepThreads(getEGLWindow(), threadFuncs.size(), threadFuncs.data());
ASSERT_NE(currentStep, Step::Abort);
}
// Test that thread B can wait on thread A's sync before thread A flushes it, and wakes up after
// that.
void MultithreadingTestES3::testFenceWithOpenRenderPass(FenceTest test, FlushMethod flushMethod)
{
ANGLE_SKIP_TEST_IF(!platformSupportsMultithreading());
ANGLE_SKIP_TEST_IF(!hasFenceSyncExtension() || !hasGLSyncExtension());
constexpr uint32_t kWidth = 100;
constexpr uint32_t kHeight = 200;
GLsync sync = 0;
GLuint texture = 0;
std::mutex mutex;
std::condition_variable condVar;
enum class Step
{
Start,
Thread0CreateFence,
Thread1WaitFence,
Finish,
Abort,
};
Step currentStep = Step::Start;
auto thread0 = [&](EGLDisplay dpy, EGLSurface surface, EGLContext context) {
ThreadSynchronization<Step> threadSynchronization(¤tStep, &mutex, &condVar);
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Start));
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, surface, surface, context));
// Create a shared texture to test synchronization
GLTexture color;
texture = color;
glBindTexture(GL_TEXTURE_2D, texture);
glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, kWidth, kHeight);
GLFramebuffer fbo;
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
// Draw to shared texture.
ANGLE_GL_PROGRAM(program, essl1_shaders::vs::Simple(), essl1_shaders::fs::Red());
drawQuad(program, essl1_shaders::PositionAttrib(), 0.0f);
ASSERT_GL_NO_ERROR();
// Issue a fence. A render pass is currently open, so the fence is not actually submitted
// in the Vulkan backend.
sync = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
ASSERT_NE(sync, nullptr);
// Wait for thread 1 to wait on it.
threadSynchronization.nextStep(Step::Thread0CreateFence);
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Thread1WaitFence));
// Wait a little to give thread 1 time to wait on the sync object before flushing it.
angle::Sleep(500);
switch (flushMethod)
{
case FlushMethod::Flush:
glFlush();
break;
case FlushMethod::Finish:
glFinish();
break;
}
// Clean up
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Finish));
};
auto thread1 = [&](EGLDisplay dpy, EGLSurface surface, EGLContext context) {
ThreadSynchronization<Step> threadSynchronization(¤tStep, &mutex, &condVar);
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, surface, surface, context));
// Wait for thread 0 to create the fence object.
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Thread0CreateFence));
// Test access to the fence object
threadSynchronization.nextStep(Step::Thread1WaitFence);
constexpr GLuint64 kTimeout = 2'000'000'000; // 2 seconds
GLenum result = GL_CONDITION_SATISFIED;
switch (test)
{
case FenceTest::ClientWait:
result = glClientWaitSync(sync, 0, kTimeout);
break;
case FenceTest::ServerWait:
glWaitSync(sync, 0, GL_TIMEOUT_IGNORED);
break;
case FenceTest::GetStatus:
{
GLint value;
glGetSynciv(sync, GL_SYNC_STATUS, 1, nullptr, &value);
if (value != GL_SIGNALED)
{
result = glClientWaitSync(sync, 0, kTimeout);
}
break;
}
}
ASSERT_TRUE(result == GL_CONDITION_SATISFIED || result == GL_ALREADY_SIGNALED);
// Verify the shared texture is drawn to.
glBindTexture(GL_TEXTURE_2D, texture);
GLFramebuffer fbo;
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
EXPECT_PIXEL_RECT_EQ(0, 0, kWidth, kHeight, GLColor::red);
// Clean up
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
threadSynchronization.nextStep(Step::Finish);
};
std::array<LockStepThreadFunc, 2> threadFuncs = {
std::move(thread0),
std::move(thread1),
};
RunLockStepThreads(getEGLWindow(), threadFuncs.size(), threadFuncs.data());
ASSERT_NE(currentStep, Step::Abort);
}
// Test that thread B can wait on thread A's sync before thread A flushes it, and wakes up after
// that.
TEST_P(MultithreadingTestES3, ThreadBClientWaitBeforeThreadASyncFlush)
{
testFenceWithOpenRenderPass(FenceTest::ClientWait, FlushMethod::Flush);
}
// Test that thread B can wait on thread A's sync before thread A flushes it, and wakes up after
// that.
TEST_P(MultithreadingTestES3, ThreadBServerWaitBeforeThreadASyncFlush)
{
testFenceWithOpenRenderPass(FenceTest::ServerWait, FlushMethod::Flush);
}
// Test that thread B can wait on thread A's sync before thread A flushes it, and wakes up after
// that.
TEST_P(MultithreadingTestES3, ThreadBGetStatusBeforeThreadASyncFlush)
{
testFenceWithOpenRenderPass(FenceTest::GetStatus, FlushMethod::Flush);
}
// Test that thread B can wait on thread A's sync before thread A flushes it, and wakes up after
// that.
TEST_P(MultithreadingTestES3, ThreadBClientWaitBeforeThreadASyncFinish)
{
testFenceWithOpenRenderPass(FenceTest::ClientWait, FlushMethod::Finish);
}
// Test that thread B can wait on thread A's sync before thread A flushes it, and wakes up after
// that.
TEST_P(MultithreadingTestES3, ThreadBServerWaitBeforeThreadASyncFinish)
{
testFenceWithOpenRenderPass(FenceTest::ServerWait, FlushMethod::Finish);
}
// Test that thread B can wait on thread A's sync before thread A flushes it, and wakes up after
// that.
TEST_P(MultithreadingTestES3, ThreadBGetStatusBeforeThreadASyncFinish)
{
testFenceWithOpenRenderPass(FenceTest::GetStatus, FlushMethod::Finish);
}
// Test the following scenario:
//
// - Thread A opens a render pass, and flushes it. In the Vulkan backend, this may make the flush
// deferred.
// - Thread B opens a render pass and creates a fence. In the Vulkan backend, this also defers the
// flush.
// - Thread C waits on fence
//
// In the Vulkan backend, submission of the fence is implied by thread C's wait, and thread A may
// also be flushed as collateral. If the fence's serial is updated based on thread A's submission,
// synchronization between B and C would be broken.
TEST_P(MultithreadingTestES3, ThreadCWaitBeforeThreadBSyncFinish)
{
ANGLE_SKIP_TEST_IF(!platformSupportsMultithreading());
ANGLE_SKIP_TEST_IF(!hasFenceSyncExtension() || !hasGLSyncExtension());
constexpr uint32_t kWidth = 100;
constexpr uint32_t kHeight = 200;
GLsync sync = 0;
GLuint texture = 0;
std::mutex mutex;
std::condition_variable condVar;
enum class Step
{
Start,
Thread0DrawAndFlush,
Thread1CreateFence,
Thread2WaitFence,
Finish,
Abort,
};
Step currentStep = Step::Start;
auto thread0 = [&](EGLDisplay dpy, EGLSurface surface, EGLContext context) {
ThreadSynchronization<Step> threadSynchronization(¤tStep, &mutex, &condVar);
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Start));
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, surface, surface, context));
// Open a render pass and flush it.
ANGLE_GL_PROGRAM(program, essl1_shaders::vs::Simple(), essl1_shaders::fs::Green());
drawQuad(program, essl1_shaders::PositionAttrib(), 0.0f);
glFlush();
ASSERT_GL_NO_ERROR();
threadSynchronization.nextStep(Step::Thread0DrawAndFlush);
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Finish));
// Clean up
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
};
auto thread1 = [&](EGLDisplay dpy, EGLSurface surface, EGLContext context) {
ThreadSynchronization<Step> threadSynchronization(¤tStep, &mutex, &condVar);
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Start));
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, surface, surface, context));
// Wait for thread 0 to set up
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Thread0DrawAndFlush));
// Create a shared texture to test synchronization
GLTexture color;
texture = color;
glBindTexture(GL_TEXTURE_2D, texture);
glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, kWidth, kHeight);
GLFramebuffer fbo;
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
// Draw to shared texture.
ANGLE_GL_PROGRAM(program, essl1_shaders::vs::Simple(), essl1_shaders::fs::Red());
drawQuad(program, essl1_shaders::PositionAttrib(), 0.0f);
ASSERT_GL_NO_ERROR();
// Issue a fence. A render pass is currently open, so the fence is not actually submitted
// in the Vulkan backend.
sync = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
ASSERT_NE(sync, nullptr);
// Wait for thread 1 to wait on it.
threadSynchronization.nextStep(Step::Thread1CreateFence);
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Thread2WaitFence));
// Wait a little to give thread 1 time to wait on the sync object before flushing it.
angle::Sleep(500);
glFlush();
// Clean up
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Finish));
};
auto thread2 = [&](EGLDisplay dpy, EGLSurface surface, EGLContext context) {
ThreadSynchronization<Step> threadSynchronization(¤tStep, &mutex, &condVar);
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, surface, surface, context));
// Wait for thread 0 to create the fence object.
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Thread1CreateFence));
// Test access to the fence object
threadSynchronization.nextStep(Step::Thread2WaitFence);
constexpr GLuint64 kTimeout = 2'000'000'000; // 2 seconds
GLenum result = glClientWaitSync(sync, 0, kTimeout);
ASSERT_TRUE(result == GL_CONDITION_SATISFIED || result == GL_ALREADY_SIGNALED);
// Verify the shared texture is drawn to.
glBindTexture(GL_TEXTURE_2D, texture);
GLFramebuffer fbo;
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
EXPECT_PIXEL_RECT_EQ(0, 0, kWidth, kHeight, GLColor::red);
// Clean up
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
threadSynchronization.nextStep(Step::Finish);
};
std::array<LockStepThreadFunc, 3> threadFuncs = {
std::move(thread0),
std::move(thread1),
std::move(thread2),
};
RunLockStepThreads(getEGLWindow(), threadFuncs.size(), threadFuncs.data());
ASSERT_NE(currentStep, Step::Abort);
}
// Test that having commands recorded but not submitted on one thread using a texture, does not
// interfere with similar commands on another thread using the same texture. Regression test for a
// bug in the Vulkan backend where the first thread would batch updates to a descriptor set not
// visible to the other thread, while the other thread picks up the (unupdated) descriptor set from
// a shared cache.
TEST_P(MultithreadingTestES3, UnsynchronizedTextureReads)
{
ANGLE_SKIP_TEST_IF(!platformSupportsMultithreading());
ANGLE_SKIP_TEST_IF(!hasFenceSyncExtension() || !hasGLSyncExtension());
GLsync sync = 0;
GLuint texture = 0;
constexpr GLubyte kInitialData[4] = {127, 63, 191, 255};
std::mutex mutex;
std::condition_variable condVar;
enum class Step
{
Start,
Thread0CreateTextureAndDraw,
Thread1DrawAndFlush,
Finish,
Abort,
};
Step currentStep = Step::Start;
auto thread0 = [&](EGLDisplay dpy, EGLSurface surface, EGLContext context) {
ThreadSynchronization<Step> threadSynchronization(¤tStep, &mutex, &condVar);
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Start));
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, surface, surface, context));
// Create a texture, and record a command that draws into it.
GLTexture color;
texture = color;
glBindTexture(GL_TEXTURE_2D, texture);
glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, 1, 1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, kInitialData);
sync = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
ASSERT_NE(sync, nullptr);
ANGLE_GL_PROGRAM(drawTexture, essl1_shaders::vs::Texture2D(),
essl1_shaders::fs::Texture2D());
drawQuad(drawTexture, essl1_shaders::PositionAttrib(), 0.0f);
// Don't flush yet; this leaves the descriptor set updates to the texture pending in the
// Vulkan backend.
threadSynchronization.nextStep(Step::Thread0CreateTextureAndDraw);
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Thread1DrawAndFlush));
// Flush after thread 1
EXPECT_PIXEL_COLOR_NEAR(
0, 0, GLColor(kInitialData[0], kInitialData[1], kInitialData[2], kInitialData[3]), 1);
// Clean up
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
threadSynchronization.nextStep(Step::Finish);
};
auto thread1 = [&](EGLDisplay dpy, EGLSurface surface, EGLContext context) {
ThreadSynchronization<Step> threadSynchronization(¤tStep, &mutex, &condVar);
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Start));
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, surface, surface, context));
// Wait for thread 0 to set up
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Thread0CreateTextureAndDraw));
// Synchronize with the texture upload (but not the concurrent read)
glWaitSync(sync, 0, GL_TIMEOUT_IGNORED);
// Draw with the same texture, in the same way as thread 0. This ensures that the
// descriptor sets used in the Vulkan backend are identical.
glBindTexture(GL_TEXTURE_2D, texture);
ANGLE_GL_PROGRAM(drawTexture, essl1_shaders::vs::Texture2D(),
essl1_shaders::fs::Texture2D());
drawQuad(drawTexture, essl1_shaders::PositionAttrib(), 0.0f);
// Flush
EXPECT_PIXEL_COLOR_NEAR(
0, 0, GLColor(kInitialData[0], kInitialData[1], kInitialData[2], kInitialData[3]), 1);
threadSynchronization.nextStep(Step::Thread1DrawAndFlush);
// Clean up
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Finish));
};
std::array<LockStepThreadFunc, 2> threadFuncs = {
std::move(thread0),
std::move(thread1),
};
RunLockStepThreads(getEGLWindow(), threadFuncs.size(), threadFuncs.data());
ASSERT_NE(currentStep, Step::Abort);
}
// Similar to UnsynchronizedTextureReads, but the texture update is done through framebuffer write.
TEST_P(MultithreadingTestES3, UnsynchronizedTextureReads2)
{
ANGLE_SKIP_TEST_IF(!platformSupportsMultithreading());
ANGLE_SKIP_TEST_IF(!hasFenceSyncExtension() || !hasGLSyncExtension());
GLsync sync = 0;
GLuint texture = 0;
std::mutex mutex;
std::condition_variable condVar;
enum class Step
{
Start,
Thread0CreateTextureAndDraw,
Thread1DrawAndFlush,
Finish,
Abort,
};
Step currentStep = Step::Start;
auto thread0 = [&](EGLDisplay dpy, EGLSurface surface, EGLContext context) {
ThreadSynchronization<Step> threadSynchronization(¤tStep, &mutex, &condVar);
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Start));
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, surface, surface, context));
// Create a texture, and record a command that draws into it.
GLTexture color;
texture = color;
glBindTexture(GL_TEXTURE_2D, texture);
glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, 1, 1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
GLFramebuffer fbo;
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
glClearColor(1, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::red);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
sync = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
ASSERT_NE(sync, nullptr);
ANGLE_GL_PROGRAM(drawTexture, essl1_shaders::vs::Texture2D(),
essl1_shaders::fs::Texture2D());
drawQuad(drawTexture, essl1_shaders::PositionAttrib(), 0.0f);
// Don't flush yet; this leaves the descriptor set updates to the texture pending in the
// Vulkan backend.
threadSynchronization.nextStep(Step::Thread0CreateTextureAndDraw);
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Thread1DrawAndFlush));
// Flush after thread 1
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::red);
// Clean up
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
threadSynchronization.nextStep(Step::Finish);
};
auto thread1 = [&](EGLDisplay dpy, EGLSurface surface, EGLContext context) {
ThreadSynchronization<Step> threadSynchronization(¤tStep, &mutex, &condVar);
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Start));
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, surface, surface, context));
// Wait for thread 0 to set up
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Thread0CreateTextureAndDraw));
// Synchronize with the texture update (but not the concurrent read)
glWaitSync(sync, 0, GL_TIMEOUT_IGNORED);
// Draw with the same texture, in the same way as thread 0. This ensures that the
// descriptor sets used in the Vulkan backend are identical.
glBindTexture(GL_TEXTURE_2D, texture);
ANGLE_GL_PROGRAM(drawTexture, essl1_shaders::vs::Texture2D(),
essl1_shaders::fs::Texture2D());
drawQuad(drawTexture, essl1_shaders::PositionAttrib(), 0.0f);
// Flush
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::red);
threadSynchronization.nextStep(Step::Thread1DrawAndFlush);
// Clean up
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Finish));
};
std::array<LockStepThreadFunc, 2> threadFuncs = {
std::move(thread0),
std::move(thread1),
};
RunLockStepThreads(getEGLWindow(), threadFuncs.size(), threadFuncs.data());
ASSERT_NE(currentStep, Step::Abort);
}
// Similar to UnsynchronizedTextureReads, but the texture is used once. This is because
// UnsynchronizedTextureRead hits a different bug than it intends to test. This test makes sure the
// image is put in the right layout, by using it together with another texture (i.e. a different
// descriptor set).
TEST_P(MultithreadingTestES3, UnsynchronizedTextureReads3)
{
ANGLE_SKIP_TEST_IF(!platformSupportsMultithreading());
constexpr GLubyte kInitialData[4] = {127, 63, 191, 255};
GLuint texture = 0;
std::mutex mutex;
std::condition_variable condVar;
enum class Step
{
Start,
Thread0CreateTextureAndDraw,
Thread1DrawAndFlush,
Finish,
Abort,
};
Step currentStep = Step::Start;
auto thread0 = [&](EGLDisplay dpy, EGLSurface surface, EGLContext context) {
ThreadSynchronization<Step> threadSynchronization(¤tStep, &mutex, &condVar);
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Start));
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, surface, surface, context));
// Create a texture, and record a command that draws into it.
GLTexture color;
texture = color;
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, 1, 1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, kInitialData);
glActiveTexture(GL_TEXTURE1);
GLTexture color2;
glBindTexture(GL_TEXTURE_2D, color2);
glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, 1, 1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, kInitialData);
ANGLE_GL_PROGRAM(setupTexture, essl1_shaders::vs::Texture2D(),
R"(precision mediump float;
uniform sampler2D tex2D;
uniform sampler2D tex2D2;
varying vec2 v_texCoord;
void main()
{
gl_FragColor = texture2D(tex2D, v_texCoord) + texture2D(tex2D2, v_texCoord);
})");
drawQuad(setupTexture, essl1_shaders::PositionAttrib(), 0.0f);
ASSERT_GL_NO_ERROR();
glFinish();
ANGLE_GL_PROGRAM(drawTexture, essl1_shaders::vs::Texture2D(),
essl1_shaders::fs::Texture2D());
drawQuad(drawTexture, essl1_shaders::PositionAttrib(), 0.0f);
ASSERT_GL_NO_ERROR();
// Don't flush yet; this leaves the descriptor set updates to the texture pending in the
// Vulkan backend.
threadSynchronization.nextStep(Step::Thread0CreateTextureAndDraw);
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Thread1DrawAndFlush));
// Flush after thread 1
EXPECT_PIXEL_COLOR_NEAR(
0, 0, GLColor(kInitialData[0], kInitialData[1], kInitialData[2], kInitialData[3]), 1);
// Clean up
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
threadSynchronization.nextStep(Step::Finish);
};
auto thread1 = [&](EGLDisplay dpy, EGLSurface surface, EGLContext context) {
ThreadSynchronization<Step> threadSynchronization(¤tStep, &mutex, &condVar);
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Start));
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, surface, surface, context));
// Wait for thread 0 to set up
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Thread0CreateTextureAndDraw));
// Draw with the same texture, in the same way as thread 0. This ensures that the
// descriptor sets used in the Vulkan backend are identical.
glBindTexture(GL_TEXTURE_2D, texture);
ANGLE_GL_PROGRAM(drawTexture, essl1_shaders::vs::Texture2D(),
essl1_shaders::fs::Texture2D());
drawQuad(drawTexture, essl1_shaders::PositionAttrib(), 0.0f);
ASSERT_GL_NO_ERROR();
// Flush
EXPECT_PIXEL_COLOR_NEAR(
0, 0, GLColor(kInitialData[0], kInitialData[1], kInitialData[2], kInitialData[3]), 1);
threadSynchronization.nextStep(Step::Thread1DrawAndFlush);
// Clean up
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Finish));
};
std::array<LockStepThreadFunc, 2> threadFuncs = {
std::move(thread0),
std::move(thread1),
};
RunLockStepThreads(getEGLWindow(), threadFuncs.size(), threadFuncs.data());
ASSERT_NE(currentStep, Step::Abort);
}
// Test framebuffer fetch program used between share groups.
void MultithreadingTestES3::testFramebufferFetch(DrawOrder drawOrder)
{
ANGLE_SKIP_TEST_IF(!platformSupportsMultithreading());
ANGLE_SKIP_TEST_IF(!IsGLExtensionEnabled("GL_EXT_shader_framebuffer_fetch_non_coherent"));
GLProgram framebufferFetchProgram;
constexpr char kFS[] = R"(#version 300 es
#extension GL_EXT_shader_framebuffer_fetch_non_coherent : require
layout(noncoherent, location = 0) inout highp vec4 o_color;
uniform highp vec4 u_color;
void main (void)
{
o_color += u_color;
})";
std::mutex mutex;
std::condition_variable condVar;
enum class Step
{
Start,
Thread0PreCreateProgram,
Thread1CreateProgram,
Finish,
Abort,
};
Step currentStep = Step::Start;
auto thread0 = [&](EGLDisplay dpy, EGLSurface surface, EGLContext context) {
ThreadSynchronization<Step> threadSynchronization(¤tStep, &mutex, &condVar);
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Start));
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, surface, surface, context));
// Open a render pass, if requested.
if (drawOrder == DrawOrder::Before)
{
ANGLE_GL_PROGRAM(program, essl1_shaders::vs::Simple(), essl1_shaders::fs::Green());
drawQuad(program, essl1_shaders::PositionAttrib(), 0.0f);
ASSERT_GL_NO_ERROR();
}
threadSynchronization.nextStep(Step::Thread0PreCreateProgram);
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Thread1CreateProgram));
// Render using the framebuffer fetch program
if (drawOrder == DrawOrder::After)
{
ANGLE_GL_PROGRAM(program, essl1_shaders::vs::Simple(), essl1_shaders::fs::Green());
drawQuad(program, essl1_shaders::PositionAttrib(), 0.0f);
ASSERT_GL_NO_ERROR();
}
glFramebufferFetchBarrierEXT();
glUseProgram(framebufferFetchProgram);
GLint colorLocation = glGetUniformLocation(framebufferFetchProgram, "u_color");
glUniform4f(colorLocation, 1, 0, 0, 0);
drawQuad(framebufferFetchProgram, essl1_shaders::PositionAttrib(), 0.0f);
ASSERT_GL_NO_ERROR();
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::yellow);
threadSynchronization.nextStep(Step::Finish);
// Clean up
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
};
auto thread1 = [&](EGLDisplay dpy, EGLSurface surface, EGLContext context) {
ThreadSynchronization<Step> threadSynchronization(¤tStep, &mutex, &condVar);
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Start));
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, surface, surface, context));
// Wait for thread 0 to set up
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Thread0PreCreateProgram));
// Create the framebuffer fetch program
framebufferFetchProgram.makeRaster(essl3_shaders::vs::Simple(), kFS);
glUseProgram(framebufferFetchProgram);
// Notify the other thread to use it
threadSynchronization.nextStep(Step::Thread1CreateProgram);
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Finish));
glClearColor(0, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);
glFramebufferFetchBarrierEXT();
glUseProgram(framebufferFetchProgram);
GLint colorLocation = glGetUniformLocation(framebufferFetchProgram, "u_color");
glUniform4f(colorLocation, 0, 0, 1, 0);
drawQuad(framebufferFetchProgram, essl1_shaders::PositionAttrib(), 0.0f);
ASSERT_GL_NO_ERROR();
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::blue);
// Clean up
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
};
std::array<LockStepThreadFunc, 2> threadFuncs = {
std::move(thread0),
std::move(thread1),
};
RunLockStepThreads(getEGLWindow(), threadFuncs.size(), threadFuncs.data());
ASSERT_NE(currentStep, Step::Abort);
}
// Thread 1 creates the framebuffer fetch program. Thread 0 proceeds to use it.
TEST_P(MultithreadingTestES3, CreateFramebufferFetchBeforeRenderPass)
{
testFramebufferFetch(DrawOrder::After);
}
// Thread 1 creates the framebuffer fetch program while thread 0 is mid render pass. Thread 0
// proceeds to use the framebuffer fetch program in the rest of its render pass.
TEST_P(MultithreadingTestES3, CreateFramebufferFetchMidRenderPass)
{
testFramebufferFetch(DrawOrder::Before);
}
// Test async monolithic pipeline creation in the Vulkan backend vs shared programs. This test
// makes one context/thread create a set of programs, then has another context/thread use them a few
// times, and then the original context destroys them.
TEST_P(MultithreadingTestES3, ProgramUseAndDestroyInTwoContexts)
{
ANGLE_SKIP_TEST_IF(!platformSupportsMultithreading());
GLProgram programs[6];
GLsync sync = 0;
std::mutex mutex;
std::condition_variable condVar;
enum class Step
{
Start,
Thread0CreatePrograms,
Thread1UsePrograms,
Finish,
Abort,
};
Step currentStep = Step::Start;
auto thread0 = [&](EGLDisplay dpy, EGLSurface surface, EGLContext context) {
ThreadSynchronization<Step> threadSynchronization(¤tStep, &mutex, &condVar);
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Start));
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, surface, surface, context));
// Create the programs
programs[0].makeRaster(essl1_shaders::vs::Simple(), essl1_shaders::fs::Red());
programs[1].makeRaster(essl1_shaders::vs::Simple(), essl1_shaders::fs::Green());
programs[2].makeRaster(essl1_shaders::vs::Simple(), essl1_shaders::fs::Blue());
programs[3].makeRaster(essl1_shaders::vs::Passthrough(), essl1_shaders::fs::Checkered());
programs[4].makeRaster(essl1_shaders::vs::Simple(), essl1_shaders::fs::UniformColor());
programs[5].makeRaster(essl1_shaders::vs::Texture2D(), essl1_shaders::fs::Texture2D());
EXPECT_TRUE(programs[0].valid());
EXPECT_TRUE(programs[1].valid());
EXPECT_TRUE(programs[2].valid());
EXPECT_TRUE(programs[3].valid());
EXPECT_TRUE(programs[4].valid());
EXPECT_TRUE(programs[5].valid());
// Wait for the other thread to use the programs
threadSynchronization.nextStep(Step::Thread0CreatePrograms);
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Thread1UsePrograms));
// Destroy them
glWaitSync(sync, 0, GL_TIMEOUT_IGNORED);
programs[0].reset();
programs[1].reset();
programs[2].reset();
programs[3].reset();
programs[4].reset();
programs[5].reset();
threadSynchronization.nextStep(Step::Finish);
// Clean up
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
};
auto thread1 = [&](EGLDisplay dpy, EGLSurface surface, EGLContext context) {
ThreadSynchronization<Step> threadSynchronization(¤tStep, &mutex, &condVar);
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Start));
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, surface, surface, context));
// Wait for thread 0 to create the programs
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Thread0CreatePrograms));
// Use them a few times.
drawQuad(programs[0], essl1_shaders::PositionAttrib(), 0.0f);
drawQuad(programs[1], essl1_shaders::PositionAttrib(), 0.0f);
drawQuad(programs[2], essl1_shaders::PositionAttrib(), 0.0f);
drawQuad(programs[3], essl1_shaders::PositionAttrib(), 0.0f);
drawQuad(programs[4], essl1_shaders::PositionAttrib(), 0.0f);
drawQuad(programs[0], essl1_shaders::PositionAttrib(), 0.0f);
drawQuad(programs[1], essl1_shaders::PositionAttrib(), 0.0f);
drawQuad(programs[2], essl1_shaders::PositionAttrib(), 0.0f);
drawQuad(programs[0], essl1_shaders::PositionAttrib(), 0.0f);
sync = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
ASSERT_NE(sync, nullptr);
// Notify the other thread to destroy the programs.
threadSynchronization.nextStep(Step::Thread1UsePrograms);
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Finish));
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::red);
// Clean up
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
};
std::array<LockStepThreadFunc, 2> threadFuncs = {
std::move(thread0),
std::move(thread1),
};
RunLockStepThreads(getEGLWindow(), threadFuncs.size(), threadFuncs.data());
ASSERT_NE(currentStep, Step::Abort);
}
ANGLE_INSTANTIATE_TEST(
MultithreadingTest,
ES2_OPENGL(),
ES3_OPENGL(),
ES2_OPENGLES(),
ES3_OPENGLES(),
ES3_VULKAN(),
ES3_VULKAN_SWIFTSHADER().enable(Feature::AsyncCommandQueue),
ES3_VULKAN_SWIFTSHADER()
.enable(Feature::AsyncCommandQueue)
.enable(Feature::SlowAsyncCommandQueueForTesting),
ES3_VULKAN_SWIFTSHADER().disable(Feature::PreferMonolithicPipelinesOverLibraries),
ES3_VULKAN_SWIFTSHADER().enable(Feature::PreferMonolithicPipelinesOverLibraries),
ES3_VULKAN_SWIFTSHADER()
.enable(Feature::PreferMonolithicPipelinesOverLibraries)
.enable(Feature::SlowDownMonolithicPipelineCreationForTesting),
ES3_VULKAN_SWIFTSHADER()
.enable(Feature::PreferMonolithicPipelinesOverLibraries)
.disable(Feature::MergeProgramPipelineCachesToGlobalCache),
ES3_VULKAN_SWIFTSHADER().enable(Feature::PermanentlySwitchToFramebufferFetchMode),
ES3_VULKAN_SWIFTSHADER()
.enable(Feature::PermanentlySwitchToFramebufferFetchMode)
.enable(Feature::PreferMonolithicPipelinesOverLibraries),
ES3_VULKAN_SWIFTSHADER()
.enable(Feature::PermanentlySwitchToFramebufferFetchMode)
.enable(Feature::PreferMonolithicPipelinesOverLibraries)
.enable(Feature::SlowDownMonolithicPipelineCreationForTesting),
ES2_D3D11(),
ES3_D3D11());
GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(MultithreadingTestES3);
ANGLE_INSTANTIATE_TEST(
MultithreadingTestES3,
ES3_OPENGL(),
ES3_OPENGLES(),
ES3_VULKAN(),
ES3_VULKAN_SWIFTSHADER().enable(Feature::AsyncCommandQueue),
ES3_VULKAN_SWIFTSHADER()
.enable(Feature::AsyncCommandQueue)
.enable(Feature::SlowAsyncCommandQueueForTesting),
ES3_VULKAN_SWIFTSHADER().disable(Feature::PreferMonolithicPipelinesOverLibraries),
ES3_VULKAN_SWIFTSHADER().enable(Feature::PreferMonolithicPipelinesOverLibraries),
ES3_VULKAN_SWIFTSHADER()
.enable(Feature::PreferMonolithicPipelinesOverLibraries)
.enable(Feature::SlowDownMonolithicPipelineCreationForTesting),
ES3_VULKAN_SWIFTSHADER()
.enable(Feature::PreferMonolithicPipelinesOverLibraries)
.disable(Feature::MergeProgramPipelineCachesToGlobalCache),
ES3_VULKAN_SWIFTSHADER().enable(Feature::PermanentlySwitchToFramebufferFetchMode),
ES3_VULKAN_SWIFTSHADER()
.enable(Feature::PermanentlySwitchToFramebufferFetchMode)
.enable(Feature::PreferMonolithicPipelinesOverLibraries),
ES3_VULKAN_SWIFTSHADER()
.enable(Feature::PermanentlySwitchToFramebufferFetchMode)
.enable(Feature::PreferMonolithicPipelinesOverLibraries)
.enable(Feature::SlowDownMonolithicPipelineCreationForTesting),
ES3_D3D11());
} // namespace angle