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
|
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#include <dawn/native/DawnNative.h>
#include "build/build_config.h"
#include "components/viz/test/test_gpu_service_holder.h"
#include "gpu/command_buffer/client/client_shared_image.h"
#include "gpu/command_buffer/client/shared_image_interface.h"
#include "gpu/command_buffer/client/webgpu_implementation.h"
#include "gpu/command_buffer/common/mailbox.h"
#include "gpu/command_buffer/common/shared_image_usage.h"
#include "gpu/command_buffer/service/shared_image/shared_image_format_service_utils.h"
#include "gpu/command_buffer/service/webgpu_decoder.h"
#include "gpu/command_buffer/tests/webgpu_test.h"
#include "gpu/config/gpu_finch_features.h"
#include "gpu/config/gpu_test_config.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/color_space.h"
#include "ui/gl/gl_context.h"
#include "ui/gl/gl_utils.h"
#include "ui/gl/init/gl_factory.h"
#define SKIP_TEST_IF(condition) \
if (condition) \
GTEST_SKIP() << #condition
namespace gpu {
namespace {
class MockBufferMapCallback {
public:
MOCK_METHOD(void,
Call,
(wgpu::MapAsyncStatus status, wgpu::StringView message));
};
std::unique_ptr<testing::StrictMock<MockBufferMapCallback>>
mock_buffer_map_callback;
void ToMockBufferMapCallback(wgpu::MapAsyncStatus status,
wgpu::StringView message) {
mock_buffer_map_callback->Call(status, message);
}
struct WebGPUMailboxTextureTestParams : WebGPUTest::Options {
viz::SharedImageFormat format;
};
std::ostream& operator<<(std::ostream& os,
const WebGPUMailboxTextureTestParams& options) {
DCHECK(options.format == viz::SinglePlaneFormat::kRGBA_8888 ||
options.format == viz::SinglePlaneFormat::kBGRA_8888 ||
options.format == viz::SinglePlaneFormat::kRGBA_F16);
os << options.format.ToTestParamString();
if (options.use_skia_graphite) {
os << "_SkiaGraphite";
}
if (options.enable_unsafe_webgpu) {
os << "_UnsafeWebGPU";
}
if (options.force_fallback_adapter) {
os << "_FallbackAdapter";
}
return os;
}
uint32_t BytesPerTexel(viz::SharedImageFormat format) {
if ((format == viz::SinglePlaneFormat::kRGBA_8888) ||
(format == viz::SinglePlaneFormat::kBGRA_8888)) {
return 4;
}
if (format == viz::SinglePlaneFormat::kRGBA_F16) {
return 8;
}
NOTREACHED();
}
} // namespace
class WebGPUMailboxTestBase : public WebGPUTest {
protected:
template <typename T>
static error::Error ExecuteCmd(webgpu::WebGPUDecoder* decoder, const T& cmd) {
static_assert(T::kArgFlags == cmd::kFixed,
"T::kArgFlags should equal cmd::kFixed");
int entries_processed = 0;
return decoder->DoCommands(1, (const void*)&cmd,
ComputeNumEntries(sizeof(cmd)),
&entries_processed);
}
template <typename T>
static error::Error ExecuteImmediateCmd(webgpu::WebGPUDecoder* decoder,
const T& cmd,
size_t data_size) {
static_assert(T::kArgFlags == cmd::kAtLeastN,
"T::kArgFlags should equal cmd::kAtLeastN");
int entries_processed = 0;
return decoder->DoCommands(1, (const void*)&cmd,
ComputeNumEntries(sizeof(cmd) + data_size),
&entries_processed);
}
};
class WebGPUMailboxTextureTest
: public WebGPUMailboxTestBase,
public testing::WithParamInterface<WebGPUMailboxTextureTestParams> {
public:
static std::vector<WebGPUMailboxTextureTestParams> TestParams() {
WebGPUMailboxTextureTestParams options = {};
WebGPUMailboxTextureTestParams fallback_options = {};
fallback_options.force_fallback_adapter = true;
// Unsafe WebGPU currently required for SwiftShader fallback.
fallback_options.enable_unsafe_webgpu = true;
std::vector<WebGPUMailboxTextureTestParams> params;
for (viz::SharedImageFormat format : {
// TODO(crbug.com/40823053): Support "rgba8unorm" canvas context format on Mac
#if !BUILDFLAG(IS_MAC)
viz::SinglePlaneFormat::kRGBA_8888,
#endif // !BUILDFLAG(IS_MAC)
viz::SinglePlaneFormat::kBGRA_8888,
viz::SinglePlaneFormat::kRGBA_F16,
}) {
WebGPUMailboxTextureTestParams o = options;
o.format = format;
#if BUILDFLAG(IS_LINUX)
// Linux does not support creation of RGBA_F16 GpuMemoryBuffers, which
// causes SharedImage creation in these tests to fail.
if (o.format != viz::SinglePlaneFormat::kRGBA_F16) {
params.push_back(o);
}
#else
params.push_back(o);
#endif
// Test SwiftShader fallback both with and without SkiaGraphite
o = fallback_options;
o.format = format;
o.use_skia_graphite = false;
params.push_back(o);
// Note: Only windows & Mac have Graphite supported for now.
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC)
o.use_skia_graphite = true;
params.push_back(o);
#endif
}
return params;
}
protected:
void SetUp() override {
SKIP_TEST_IF(!WebGPUSupported());
SKIP_TEST_IF(!WebGPUSharedImageSupported());
WebGPUTest::SetUp();
Initialize(GetParam());
device_ = GetNewDevice();
mock_buffer_map_callback =
std::make_unique<testing::StrictMock<MockBufferMapCallback>>();
}
void TearDown() override {
mock_buffer_map_callback = nullptr;
// Wait for all operations to catch any validation or device lost errors.
PollUntilIdle();
device_ = nullptr;
WebGPUTest::TearDown();
}
struct AssociateMailboxCmdStorage {
webgpu::cmds::AssociateMailboxImmediate cmd;
// Immediate data is copied into the space immediately following `cmd`.
// Allocate space to hold up to 1 mailbox and 2 view formats.
GLbyte data[GL_MAILBOX_SIZE_CHROMIUM];
std::array<WGPUTextureFormat, 2u> view_formats;
};
enum class AccessType { Read, Write, ReadWrite };
SharedImageUsageSet GetSharedImageUsage(AccessType access_type) {
SharedImageUsageSet webgpu_usage;
// With the fallback adapter, reading/writing from the SharedImage will
// occur via Skia.
SharedImageUsageSet fallback_usage;
switch (access_type) {
case AccessType::Read:
webgpu_usage = SHARED_IMAGE_USAGE_WEBGPU_READ;
fallback_usage = SHARED_IMAGE_USAGE_RASTER_READ;
break;
case AccessType::Write:
webgpu_usage = SHARED_IMAGE_USAGE_WEBGPU_WRITE;
fallback_usage = SHARED_IMAGE_USAGE_RASTER_WRITE;
break;
case AccessType::ReadWrite:
webgpu_usage =
SHARED_IMAGE_USAGE_WEBGPU_READ | SHARED_IMAGE_USAGE_WEBGPU_WRITE;
fallback_usage =
SHARED_IMAGE_USAGE_RASTER_READ | SHARED_IMAGE_USAGE_RASTER_WRITE;
break;
}
auto si_usage = webgpu_usage;
if (IsUsingFallbackAdapter()) {
si_usage |= fallback_usage;
}
return si_usage;
}
void InitializeTextureColor(wgpu::Device device,
const Mailbox& mailbox,
wgpu::Color clearValue) {
gpu::webgpu::ReservedTexture reservation =
webgpu()->ReserveTexture(device.Get());
webgpu()->AssociateMailbox(
reservation.deviceId, reservation.deviceGeneration, reservation.id,
reservation.generation, WGPUTextureUsage_RenderAttachment,
webgpu::WEBGPU_MAILBOX_NONE, mailbox);
wgpu::Texture texture = wgpu::Texture::Acquire(reservation.texture);
// Clear the texture using a render pass.
wgpu::RenderPassColorAttachment color_desc = {};
color_desc.view = texture.CreateView();
color_desc.loadOp = wgpu::LoadOp::Clear;
color_desc.storeOp = wgpu::StoreOp::Store;
color_desc.clearValue = clearValue;
wgpu::RenderPassDescriptor render_pass_desc = {};
render_pass_desc.colorAttachmentCount = 1;
render_pass_desc.colorAttachments = &color_desc;
wgpu::CommandEncoder encoder = device.CreateCommandEncoder();
wgpu::RenderPassEncoder pass = encoder.BeginRenderPass(&render_pass_desc);
pass.End();
wgpu::CommandBuffer commands = encoder.Finish();
wgpu::Queue queue = device.GetQueue();
queue.Submit(1, &commands);
webgpu()->DissociateMailbox(reservation.id, reservation.generation);
}
void UninitializeTexture(wgpu::Device device, wgpu::Texture texture) {
wgpu::RenderPassColorAttachment color_desc = {};
color_desc.view = texture.CreateView();
color_desc.loadOp = wgpu::LoadOp::Load;
color_desc.storeOp = wgpu::StoreOp::Discard;
wgpu::RenderPassDescriptor render_pass_desc = {};
render_pass_desc.colorAttachmentCount = 1;
render_pass_desc.colorAttachments = &color_desc;
wgpu::CommandEncoder encoder = device.CreateCommandEncoder();
wgpu::RenderPassEncoder pass = encoder.BeginRenderPass(&render_pass_desc);
pass.End();
wgpu::CommandBuffer commands = encoder.Finish();
wgpu::Queue queue = device.GetQueue();
queue.Submit(1, &commands);
}
wgpu::Device device_;
};
TEST_P(WebGPUMailboxTextureTest, AssociateMailboxCmd) {
// Create the shared image
SharedImageInterface* sii = GetSharedImageInterface();
scoped_refptr<gpu::ClientSharedImage> shared_image =
sii->CreateSharedImage({GetParam().format,
{1, 1},
gfx::ColorSpace::CreateSRGB(),
GetSharedImageUsage(AccessType::Read),
"TestLabel"},
kNullSurfaceHandle);
webgpu::ReservedTexture reservation = webgpu()->ReserveTexture(device_.Get());
GetGpuServiceHolder()->ScheduleGpuMainTask(base::BindOnce(
[](webgpu::WebGPUDecoder* decoder, webgpu::ReservedTexture reservation,
scoped_refptr<gpu::ClientSharedImage> shared_image) {
const gpu::Mailbox& mailbox = shared_image->mailbox();
// Error case: device client id doesn't exist.
{
AssociateMailboxCmdStorage cmd;
cmd.cmd.Init(reservation.deviceId + 1, reservation.deviceGeneration,
reservation.id, reservation.generation,
WGPUTextureUsage_TextureBinding, 0u,
webgpu::WEBGPU_MAILBOX_NONE, 0u,
ComputeNumEntries(sizeof(mailbox.name)),
reinterpret_cast<const GLuint*>(&mailbox.name));
EXPECT_EQ(
error::kInvalidArguments,
ExecuteImmediateCmd(decoder, cmd.cmd, sizeof(mailbox.name)));
}
// Error case: device generation is invalid.
{
AssociateMailboxCmdStorage cmd;
cmd.cmd.Init(reservation.deviceId, reservation.deviceGeneration + 1,
reservation.id, reservation.generation,
WGPUTextureUsage_TextureBinding, 0u,
webgpu::WEBGPU_MAILBOX_NONE, 0u,
ComputeNumEntries(sizeof(mailbox.name)),
reinterpret_cast<const GLuint*>(&mailbox.name));
EXPECT_EQ(
error::kInvalidArguments,
ExecuteImmediateCmd(decoder, cmd.cmd, sizeof(mailbox.name)));
}
// Error case: texture ID invalid for the wire server.
{
AssociateMailboxCmdStorage cmd;
cmd.cmd.Init(reservation.deviceId, reservation.deviceGeneration,
reservation.id + 1, reservation.generation,
WGPUTextureUsage_TextureBinding, 0u,
webgpu::WEBGPU_MAILBOX_NONE, 0u,
ComputeNumEntries(sizeof(mailbox.name)),
reinterpret_cast<const GLuint*>(&mailbox.name));
EXPECT_EQ(
error::kInvalidArguments,
ExecuteImmediateCmd(decoder, cmd.cmd, sizeof(mailbox.name)));
}
// Error case: invalid texture usage.
{
AssociateMailboxCmdStorage cmd;
cmd.cmd.Init(reservation.deviceId, reservation.deviceGeneration,
reservation.id, reservation.generation,
UINT64_MAX, 0u,
webgpu::WEBGPU_MAILBOX_NONE, 0u,
ComputeNumEntries(sizeof(mailbox.name)),
reinterpret_cast<const GLuint*>(&mailbox.name));
EXPECT_EQ(
error::kInvalidArguments,
ExecuteImmediateCmd(decoder, cmd.cmd, sizeof(mailbox.name)));
}
// Error case: invalid internal texture usage.
{
AssociateMailboxCmdStorage cmd;
cmd.cmd.Init(reservation.deviceId, reservation.deviceGeneration,
reservation.id, reservation.generation,
WGPUTextureUsage_TextureBinding,
UINT64_MAX, webgpu::WEBGPU_MAILBOX_NONE,
0u, ComputeNumEntries(sizeof(mailbox.name)),
reinterpret_cast<const GLuint*>(&mailbox.name));
EXPECT_EQ(
error::kInvalidArguments,
ExecuteImmediateCmd(decoder, cmd.cmd, sizeof(mailbox.name)));
}
// Prep packed data for packing view formats and the mailbox.
std::vector<GLuint> packed_data;
packed_data.resize(sizeof(mailbox.name) / sizeof(uint32_t));
memcpy(reinterpret_cast<char*>(packed_data.data()), &mailbox.name,
sizeof(mailbox.name));
uint32_t view_format_count = 0u;
if (GetParam().format == viz::SinglePlaneFormat::kRGBA_F16) {
} else if (GetParam().format == viz::SinglePlaneFormat::kRGBA_8888) {
view_format_count = 1u;
packed_data.push_back(
static_cast<uint32_t>(WGPUTextureFormat_RGBA8UnormSrgb));
} else if (GetParam().format == viz::SinglePlaneFormat::kBGRA_8888) {
view_format_count = 2u;
packed_data.push_back(
static_cast<uint32_t>(WGPUTextureFormat_BGRA8UnormSrgb));
packed_data.push_back(
static_cast<uint32_t>(WGPUTextureFormat_BGRA8Unorm));
} else {
NOTREACHED();
}
// Error case: packed data empty.
{
AssociateMailboxCmdStorage cmd;
cmd.cmd.Init(reservation.deviceId, reservation.deviceGeneration,
reservation.id, reservation.generation, UINT64_MAX, 0u,
webgpu::WEBGPU_MAILBOX_NONE, 0u, 0u, packed_data.data());
EXPECT_EQ(error::kOutOfBounds,
ExecuteImmediateCmd(decoder, cmd.cmd, 0u));
}
// Error case: packed data smaller than mailbox.
{
AssociateMailboxCmdStorage cmd;
cmd.cmd.Init(reservation.deviceId, reservation.deviceGeneration,
reservation.id, reservation.generation, UINT64_MAX, 0u,
webgpu::WEBGPU_MAILBOX_NONE, view_format_count,
ComputeNumEntries(sizeof(mailbox.name)) - 1u,
packed_data.data());
EXPECT_EQ(error::kOutOfBounds,
ExecuteImmediateCmd(decoder, cmd.cmd,
sizeof(uint32_t) * packed_data.size()));
}
// Error case: packed data size incorrect.
for (int adjustment : {-1, -2}) {
AssociateMailboxCmdStorage cmd;
cmd.cmd.Init(reservation.deviceId, reservation.deviceGeneration,
reservation.id, reservation.generation,
WGPUTextureUsage_TextureBinding, 0u,
webgpu::WEBGPU_MAILBOX_NONE, view_format_count,
packed_data.size() + adjustment, packed_data.data());
EXPECT_EQ(error::kOutOfBounds,
ExecuteImmediateCmd(decoder, cmd.cmd,
sizeof(uint32_t) * packed_data.size()));
}
// Error case: view_format_count incorrect.
for (int adjustment : {-1, 1}) {
AssociateMailboxCmdStorage cmd;
cmd.cmd.Init(reservation.deviceId, reservation.deviceGeneration,
reservation.id, reservation.generation,
WGPUTextureUsage_TextureBinding, 0u,
webgpu::WEBGPU_MAILBOX_NONE,
view_format_count + adjustment, packed_data.size(),
packed_data.data());
EXPECT_EQ(error::kOutOfBounds,
ExecuteImmediateCmd(decoder, cmd.cmd,
sizeof(uint32_t) * packed_data.size()));
}
// Control case: test a successful call to AssociateMailbox.
// The control case is not put first because it modifies the internal
// state of the Dawn wire server and would make calls with the same
// texture ID and generation invalid.
{
AssociateMailboxCmdStorage cmd;
cmd.cmd.Init(reservation.deviceId, reservation.deviceGeneration,
reservation.id, reservation.generation,
WGPUTextureUsage_TextureBinding, 0u,
webgpu::WEBGPU_MAILBOX_NONE, view_format_count,
packed_data.size(), packed_data.data());
EXPECT_EQ(error::kNoError,
ExecuteImmediateCmd(decoder, cmd.cmd,
sizeof(uint32_t) * packed_data.size()));
}
// Error case: associated to an already associated texture.
{
AssociateMailboxCmdStorage cmd;
cmd.cmd.Init(reservation.deviceId, reservation.deviceGeneration,
reservation.id, reservation.generation,
WGPUTextureUsage_TextureBinding, 0u,
webgpu::WEBGPU_MAILBOX_NONE, 0u,
ComputeNumEntries(sizeof(mailbox.name)),
reinterpret_cast<const GLuint*>(&mailbox.name));
EXPECT_EQ(
error::kInvalidArguments,
ExecuteImmediateCmd(decoder, cmd.cmd, sizeof(mailbox.name)));
}
// Dissociate the image from the control case to remove its reference.
{
webgpu::cmds::DissociateMailbox cmd;
cmd.Init(reservation.id, reservation.generation);
EXPECT_EQ(error::kNoError, ExecuteCmd(decoder, cmd));
}
},
GetDecoder(), reservation, std::move(shared_image)));
GetGpuServiceHolder()
->gpu_main_thread_task_runner()
->RunsTasksInCurrentSequence();
}
// Test that AssociateMailbox with a bad mailbox produces an error texture.
TEST_P(WebGPUMailboxTextureTest,
AssociateMailboxCmdBadMailboxMakesErrorTexture) {
// Create the shared image
SharedImageInterface* sii = GetSharedImageInterface();
scoped_refptr<gpu::ClientSharedImage> shared_image =
sii->CreateSharedImage({GetParam().format,
{1, 1},
gfx::ColorSpace::CreateSRGB(),
GetSharedImageUsage(AccessType::Read),
"TestLabel"},
kNullSurfaceHandle);
webgpu::ReservedTexture reservation = webgpu()->ReserveTexture(device_.Get());
GetGpuServiceHolder()->ScheduleGpuMainTask(base::BindOnce(
[](webgpu::WebGPUDecoder* decoder, webgpu::ReservedTexture reservation,
scoped_refptr<gpu::ClientSharedImage> shared_image) {
// Error case: invalid mailbox
{
gpu::Mailbox bad_mailbox;
AssociateMailboxCmdStorage cmd;
cmd.cmd.Init(reservation.deviceId, reservation.deviceGeneration,
reservation.id, reservation.generation,
WGPUTextureUsage_TextureBinding, 0u,
webgpu::WEBGPU_MAILBOX_NONE, 0u,
ComputeNumEntries(sizeof(bad_mailbox.name)),
reinterpret_cast<const GLuint*>(&bad_mailbox.name));
EXPECT_EQ(
error::kNoError,
ExecuteImmediateCmd(decoder, cmd.cmd, sizeof(bad_mailbox.name)));
}
},
GetDecoder(), reservation, std::move(shared_image)));
wgpu::Texture texture = wgpu::Texture::Acquire(reservation.texture);
// Expect an error when creating a view since the texture is an error.
EXPECT_WEBGPU_ERROR(device_, wgpu::ErrorType::Validation,
texture.CreateView());
}
TEST_P(WebGPUMailboxTextureTest, DissociateMailboxCmd) {
// Create the shared image
SharedImageInterface* sii = GetSharedImageInterface();
scoped_refptr<gpu::ClientSharedImage> shared_image =
sii->CreateSharedImage({GetParam().format,
{1, 1},
gfx::ColorSpace::CreateSRGB(),
GetSharedImageUsage(AccessType::Read),
"TestLabel"},
kNullSurfaceHandle);
webgpu::ReservedTexture reservation = webgpu()->ReserveTexture(device_.Get());
GetGpuServiceHolder()->ScheduleGpuMainTask(base::BindOnce(
[](webgpu::WebGPUDecoder* decoder, webgpu::ReservedTexture reservation,
scoped_refptr<gpu::ClientSharedImage> shared_image) {
const gpu::Mailbox& mailbox = shared_image->mailbox();
// Associate a mailbox so we can later dissociate it.
{
AssociateMailboxCmdStorage cmd;
cmd.cmd.Init(reservation.deviceId, reservation.deviceGeneration,
reservation.id, reservation.generation,
WGPUTextureUsage_TextureBinding, 0u,
webgpu::WEBGPU_MAILBOX_NONE, 0u,
ComputeNumEntries(sizeof(mailbox.name)),
reinterpret_cast<const GLuint*>(&mailbox.name));
EXPECT_EQ(error::kNoError, ExecuteImmediateCmd(decoder, cmd.cmd,
sizeof(mailbox.name)));
}
// Error case: wrong texture ID
{
webgpu::cmds::DissociateMailbox cmd;
cmd.Init(reservation.id + 1, reservation.generation);
EXPECT_EQ(error::kInvalidArguments, ExecuteCmd(decoder, cmd));
}
// Error case: wrong texture generation
{
webgpu::cmds::DissociateMailbox cmd;
cmd.Init(reservation.id, reservation.generation + 1);
EXPECT_EQ(error::kInvalidArguments, ExecuteCmd(decoder, cmd));
}
// Success case
{
webgpu::cmds::DissociateMailbox cmd;
cmd.Init(reservation.id, reservation.generation);
EXPECT_EQ(error::kNoError, ExecuteCmd(decoder, cmd));
}
// Error case: dissociate an already dissociated mailbox
{
webgpu::cmds::DissociateMailbox cmd;
cmd.Init(reservation.id, reservation.generation);
EXPECT_EQ(error::kInvalidArguments, ExecuteCmd(decoder, cmd));
}
},
GetDecoder(), reservation, std::move(shared_image)));
GetGpuServiceHolder()
->gpu_main_thread_task_runner()
->RunsTasksInCurrentSequence();
}
// Tests using Associate/DissociateMailbox to share an image with Dawn.
// For simplicity of the test the image is shared between a Dawn device and
// itself: we render to it using the Dawn device, then re-associate it to a
// Dawn texture and read back the values that were written.
TEST_P(WebGPUMailboxTextureTest, WriteToMailboxThenReadFromIt) {
SKIP_TEST_IF(GetParam().format == viz::SinglePlaneFormat::kRGBA_F16);
// Create the shared image
SharedImageInterface* sii = GetSharedImageInterface();
scoped_refptr<gpu::ClientSharedImage> shared_image =
sii->CreateSharedImage({GetParam().format,
{1, 1},
gfx::ColorSpace::CreateSRGB(),
GetSharedImageUsage(AccessType::ReadWrite),
"TestLabel"},
kNullSurfaceHandle);
SyncToken mailbox_produced_token = sii->GenVerifiedSyncToken();
webgpu()->WaitSyncTokenCHROMIUM(mailbox_produced_token.GetConstData());
// Part 1: Write to the texture using Dawn
InitializeTextureColor(device_, shared_image->mailbox(),
{0.0, 0.0, 1.0, 1.0});
// Part 2: Read back the texture using Dawn
{
// Register the shared image as a Dawn texture in the wire.
gpu::webgpu::ReservedTexture reservation =
webgpu()->ReserveTexture(device_.Get());
webgpu()->AssociateMailbox(
reservation.deviceId, reservation.deviceGeneration, reservation.id,
reservation.generation, WGPUTextureUsage_CopySrc,
webgpu::WEBGPU_MAILBOX_NONE, shared_image->mailbox());
wgpu::Texture texture = wgpu::Texture::Acquire(reservation.texture);
// Copy the texture in a mappable buffer.
wgpu::BufferDescriptor buffer_desc;
buffer_desc.size = 4;
buffer_desc.usage = wgpu::BufferUsage::MapRead | wgpu::BufferUsage::CopyDst;
wgpu::Buffer readback_buffer = device_.CreateBuffer(&buffer_desc);
wgpu::TexelCopyTextureInfo copy_src = {};
copy_src.texture = texture;
copy_src.mipLevel = 0;
copy_src.origin = {0, 0, 0};
wgpu::TexelCopyBufferInfo copy_dst = {};
copy_dst.buffer = readback_buffer;
copy_dst.layout.offset = 0;
copy_dst.layout.bytesPerRow = 256;
wgpu::Extent3D copy_size = {1, 1, 1};
wgpu::CommandEncoder encoder = device_.CreateCommandEncoder();
encoder.CopyTextureToBuffer(©_src, ©_dst, ©_size);
wgpu::CommandBuffer commands = encoder.Finish();
wgpu::Queue queue = device_.GetQueue();
queue.Submit(1, &commands);
webgpu()->DissociateMailbox(reservation.id, reservation.generation);
// Map the buffer and assert the pixel is the correct value.
readback_buffer.MapAsync(wgpu::MapMode::Read, 0, 4,
wgpu::CallbackMode::AllowSpontaneous,
ToMockBufferMapCallback);
EXPECT_CALL(*mock_buffer_map_callback,
Call(wgpu::MapAsyncStatus::Success, testing::_))
.Times(1);
WaitForCompletion(device_);
const void* data = readback_buffer.GetConstMappedRange();
if (GetParam().format == viz::SinglePlaneFormat::kRGBA_8888) {
EXPECT_EQ(0xFFFF0000u, *static_cast<const uint32_t*>(data));
} else if (GetParam().format == viz::SinglePlaneFormat::kBGRA_8888) {
EXPECT_EQ(0xFF0000FFu, *static_cast<const uint32_t*>(data));
} else {
NOTREACHED();
}
}
}
// Test that passing write usages when associating a mailbox fails if
// the SharedImage associated with the mailbox doesn't have WEBGPU_WRITE access.
TEST_P(WebGPUMailboxTextureTest,
PassWriteUsagesWhenAssociatingReadOnlyMailbox) {
// Create the shared image.
SharedImageInterface* sii = GetSharedImageInterface();
scoped_refptr<gpu::ClientSharedImage> shared_image =
sii->CreateSharedImage({GetParam().format,
{1, 1},
gfx::ColorSpace::CreateSRGB(),
GetSharedImageUsage(AccessType::Read),
"TestLabel"},
kNullSurfaceHandle);
SyncToken mailbox_produced_token = sii->GenVerifiedSyncToken();
webgpu()->WaitSyncTokenCHROMIUM(mailbox_produced_token.GetConstData());
// Register the shared image as a Dawn texture in the wire.
gpu::webgpu::ReservedTexture reservation =
webgpu()->ReserveTexture(device_.Get());
// Create a texture for the mailbox, passing CopyDst as a usage.
webgpu()->AssociateMailbox(
reservation.deviceId, reservation.deviceGeneration, reservation.id,
reservation.generation,
WGPUTextureUsage_CopySrc | WGPUTextureUsage_CopyDst,
webgpu::WEBGPU_MAILBOX_NONE, shared_image->mailbox());
wgpu::Texture texture = wgpu::Texture::Acquire(reservation.texture);
// Copy the texture in a mappable buffer.
wgpu::BufferDescriptor buffer_desc;
buffer_desc.size = BytesPerTexel(GetParam().format);
buffer_desc.usage = wgpu::BufferUsage::MapRead | wgpu::BufferUsage::CopyDst;
wgpu::Buffer readback_buffer = device_.CreateBuffer(&buffer_desc);
wgpu::TexelCopyTextureInfo copy_src = {};
copy_src.texture = texture;
copy_src.mipLevel = 0;
copy_src.origin = {0, 0, 0};
wgpu::TexelCopyBufferInfo copy_dst = {};
copy_dst.buffer = readback_buffer;
copy_dst.layout.offset = 0;
copy_dst.layout.bytesPerRow = 256;
wgpu::Extent3D copy_size = {1, 1, 1};
wgpu::CommandEncoder encoder = device_.CreateCommandEncoder();
encoder.CopyTextureToBuffer(©_src, ©_dst, ©_size);
EXPECT_WEBGPU_ERROR(device_, wgpu::ErrorType::Validation, encoder.Finish());
WaitForCompletion(device_);
}
// Test that passing internal write usages when associating a mailbox fails if
// the SharedImage associated with the mailbox doesn't have WEBGPU_WRITE access.
TEST_P(WebGPUMailboxTextureTest,
PassInternalWriteUsagesWhenAssociatingReadOnlyMailbox) {
// Create the shared image.
SharedImageInterface* sii = GetSharedImageInterface();
scoped_refptr<gpu::ClientSharedImage> shared_image =
sii->CreateSharedImage({GetParam().format,
{1, 1},
gfx::ColorSpace::CreateSRGB(),
GetSharedImageUsage(AccessType::Read),
"TestLabel"},
kNullSurfaceHandle);
SyncToken mailbox_produced_token = sii->GenVerifiedSyncToken();
webgpu()->WaitSyncTokenCHROMIUM(mailbox_produced_token.GetConstData());
// Register the shared image as a Dawn texture in the wire.
gpu::webgpu::ReservedTexture reservation =
webgpu()->ReserveTexture(device_.Get());
// Create a texture for the mailbox, passing CopyDst as an internal usage.
webgpu()->AssociateMailbox(reservation.deviceId, reservation.deviceGeneration,
reservation.id, reservation.generation,
WGPUTextureUsage_CopySrc, WGPUTextureUsage_CopyDst,
webgpu::WEBGPU_MAILBOX_NONE,
shared_image->mailbox());
wgpu::Texture texture = wgpu::Texture::Acquire(reservation.texture);
// Copy the texture in a mappable buffer.
wgpu::BufferDescriptor buffer_desc;
buffer_desc.size = BytesPerTexel(GetParam().format);
buffer_desc.usage = wgpu::BufferUsage::MapRead | wgpu::BufferUsage::CopyDst;
wgpu::Buffer readback_buffer = device_.CreateBuffer(&buffer_desc);
wgpu::TexelCopyTextureInfo copy_src = {};
copy_src.texture = texture;
copy_src.mipLevel = 0;
copy_src.origin = {0, 0, 0};
wgpu::TexelCopyBufferInfo copy_dst = {};
copy_dst.buffer = readback_buffer;
copy_dst.layout.offset = 0;
copy_dst.layout.bytesPerRow = 256;
wgpu::Extent3D copy_size = {1, 1, 1};
wgpu::CommandEncoder encoder = device_.CreateCommandEncoder();
encoder.CopyTextureToBuffer(©_src, ©_dst, ©_size);
EXPECT_WEBGPU_ERROR(device_, wgpu::ErrorType::Validation, encoder.Finish());
WaitForCompletion(device_);
}
// Test that passing WEBGPU_MAILBOX_DISCARD when associating a mailbox fails if
// the SharedImage associated with the mailbox doesn't have WEBGPU_WRITE access.
TEST_P(WebGPUMailboxTextureTest, PassDiscardWhenAssociatingReadOnlyMailbox) {
// Create the shared image.
SharedImageInterface* sii = GetSharedImageInterface();
scoped_refptr<gpu::ClientSharedImage> shared_image =
sii->CreateSharedImage({GetParam().format,
{1, 1},
gfx::ColorSpace::CreateSRGB(),
GetSharedImageUsage(AccessType::Read),
"TestLabel"},
kNullSurfaceHandle);
SyncToken mailbox_produced_token = sii->GenVerifiedSyncToken();
webgpu()->WaitSyncTokenCHROMIUM(mailbox_produced_token.GetConstData());
// Register the shared image as a Dawn texture in the wire.
gpu::webgpu::ReservedTexture reservation =
webgpu()->ReserveTexture(device_.Get());
// Associate the mailbox. Using WEBGPU_MAILBOX_DISCARD will set the contents
// to uncleared.
webgpu()->AssociateMailbox(
reservation.deviceId, reservation.deviceGeneration, reservation.id,
reservation.generation, WGPUTextureUsage_CopySrc,
webgpu::WEBGPU_MAILBOX_DISCARD, shared_image->mailbox());
wgpu::Texture texture = wgpu::Texture::Acquire(reservation.texture);
// Copy the texture in a mappable buffer.
wgpu::BufferDescriptor buffer_desc;
buffer_desc.size = BytesPerTexel(GetParam().format);
buffer_desc.usage = wgpu::BufferUsage::MapRead | wgpu::BufferUsage::CopyDst;
wgpu::Buffer readback_buffer = device_.CreateBuffer(&buffer_desc);
wgpu::TexelCopyTextureInfo copy_src = {};
copy_src.texture = texture;
copy_src.mipLevel = 0;
copy_src.origin = {0, 0, 0};
wgpu::TexelCopyBufferInfo copy_dst = {};
copy_dst.buffer = readback_buffer;
copy_dst.layout.offset = 0;
copy_dst.layout.bytesPerRow = 256;
wgpu::Extent3D copy_size = {1, 1, 1};
wgpu::CommandEncoder encoder = device_.CreateCommandEncoder();
encoder.CopyTextureToBuffer(©_src, ©_dst, ©_size);
EXPECT_WEBGPU_ERROR(device_, wgpu::ErrorType::Validation, encoder.Finish());
WaitForCompletion(device_);
}
// Test that passing WEBGPU_MAILBOX_DISCARD when associating a mailbox fails if
// the client doesn't pass a usage supporting lazy clearing.
TEST_P(WebGPUMailboxTextureTest,
PassDiscardWhenAssociatingMailboxWithoutUsageSupportingClearing) {
// Create the shared image.
SharedImageInterface* sii = GetSharedImageInterface();
scoped_refptr<gpu::ClientSharedImage> shared_image =
sii->CreateSharedImage({GetParam().format,
{1, 1},
gfx::ColorSpace::CreateSRGB(),
GetSharedImageUsage(AccessType::ReadWrite),
"TestLabel"},
kNullSurfaceHandle);
SyncToken mailbox_produced_token = sii->GenVerifiedSyncToken();
webgpu()->WaitSyncTokenCHROMIUM(mailbox_produced_token.GetConstData());
// Register the shared image as a Dawn texture in the wire.
gpu::webgpu::ReservedTexture reservation =
webgpu()->ReserveTexture(device_.Get());
// Associate the mailbox without passing any usages or internal usages
// supporting lazy clearing.
webgpu()->AssociateMailbox(
reservation.deviceId, reservation.deviceGeneration, reservation.id,
reservation.generation, WGPUTextureUsage_CopySrc,
webgpu::WEBGPU_MAILBOX_DISCARD, shared_image->mailbox());
wgpu::Texture texture = wgpu::Texture::Acquire(reservation.texture);
// Copy the texture in a mappable buffer.
wgpu::BufferDescriptor buffer_desc;
buffer_desc.size = BytesPerTexel(GetParam().format);
buffer_desc.usage = wgpu::BufferUsage::MapRead | wgpu::BufferUsage::CopyDst;
wgpu::Buffer readback_buffer = device_.CreateBuffer(&buffer_desc);
wgpu::TexelCopyTextureInfo copy_src = {};
copy_src.texture = texture;
copy_src.mipLevel = 0;
copy_src.origin = {0, 0, 0};
wgpu::TexelCopyBufferInfo copy_dst = {};
copy_dst.buffer = readback_buffer;
copy_dst.layout.offset = 0;
copy_dst.layout.bytesPerRow = 256;
wgpu::Extent3D copy_size = {1, 1, 1};
wgpu::CommandEncoder encoder = device_.CreateCommandEncoder();
encoder.CopyTextureToBuffer(©_src, ©_dst, ©_size);
EXPECT_WEBGPU_ERROR(device_, wgpu::ErrorType::Validation, encoder.Finish());
WaitForCompletion(device_);
}
// Test that an uninitialized writable shared image is lazily cleared by Dawn
// when it is accessed with an internal write usage supporting lazy clearing.
TEST_P(WebGPUMailboxTextureTest,
ReadWritableUninitializedSharedImageWhenAccessedWithInternalWriteUsage) {
// Create the shared image. Note that it is uncleared by default.
SharedImageInterface* sii = GetSharedImageInterface();
scoped_refptr<gpu::ClientSharedImage> shared_image =
sii->CreateSharedImage({GetParam().format,
{1, 1},
gfx::ColorSpace::CreateSRGB(),
GetSharedImageUsage(AccessType::ReadWrite),
"TestLabel"},
kNullSurfaceHandle);
SyncToken mailbox_produced_token = sii->GenVerifiedSyncToken();
webgpu()->WaitSyncTokenCHROMIUM(mailbox_produced_token.GetConstData());
// Register the shared image as a Dawn texture in the wire.
gpu::webgpu::ReservedTexture reservation =
webgpu()->ReserveTexture(device_.Get());
// Associate the mailbox, passing a read-only usage but RenderAttachment as an
// internal usage.
webgpu()->AssociateMailbox(
reservation.deviceId, reservation.deviceGeneration, reservation.id,
reservation.generation, WGPUTextureUsage_CopySrc,
WGPUTextureUsage_RenderAttachment, webgpu::WEBGPU_MAILBOX_NONE,
shared_image->mailbox());
wgpu::Texture texture = wgpu::Texture::Acquire(reservation.texture);
wgpu::CommandEncoder encoder = device_.CreateCommandEncoder();
// Copy the texture in a mappable buffer.
wgpu::BufferDescriptor buffer_desc;
buffer_desc.size = BytesPerTexel(GetParam().format);
buffer_desc.usage = wgpu::BufferUsage::MapRead | wgpu::BufferUsage::CopyDst;
wgpu::Buffer readback_buffer = device_.CreateBuffer(&buffer_desc);
wgpu::TexelCopyTextureInfo copy_src = {};
copy_src.texture = texture;
copy_src.mipLevel = 0;
copy_src.origin = {0, 0, 0};
wgpu::TexelCopyBufferInfo copy_dst = {};
copy_dst.buffer = readback_buffer;
copy_dst.layout.offset = 0;
copy_dst.layout.bytesPerRow = 256;
wgpu::Extent3D copy_size = {1, 1, 1};
encoder.CopyTextureToBuffer(©_src, ©_dst, ©_size);
wgpu::CommandBuffer commands = encoder.Finish();
wgpu::Queue queue = device_.GetQueue();
queue.Submit(1, &commands);
webgpu()->DissociateMailbox(reservation.id, reservation.generation);
// Map the buffer and assert the pixel is the correct value.
readback_buffer.MapAsync(wgpu::MapMode::Read, 0, buffer_desc.size,
wgpu::CallbackMode::AllowSpontaneous,
ToMockBufferMapCallback);
EXPECT_CALL(*mock_buffer_map_callback,
Call(wgpu::MapAsyncStatus::Success, testing::_))
.Times(1);
WaitForCompletion(device_);
const uint8_t* data = static_cast<const uint8_t*>(
readback_buffer.GetConstMappedRange(0, buffer_desc.size));
// Contents should be black because the texture was lazily cleared.
for (uint32_t i = 0; i < buffer_desc.size; ++i) {
EXPECT_EQ(data[i], uint8_t(0));
}
// Associate the SharedImage with a new Dawn texture in a read-only access.
// The SharedImage should have been cleared at the end of the previous access,
// and hence this read access should succeed.
gpu::webgpu::ReservedTexture reservation2 =
webgpu()->ReserveTexture(device_.Get());
webgpu()->AssociateMailbox(
reservation2.deviceId, reservation2.deviceGeneration, reservation2.id,
reservation2.generation, WGPUTextureUsage_CopySrc,
webgpu::WEBGPU_MAILBOX_NONE, shared_image->mailbox());
wgpu::Texture texture2 = wgpu::Texture::Acquire(reservation2.texture);
copy_src.texture = texture2;
wgpu::Buffer readback_buffer2 = device_.CreateBuffer(&buffer_desc);
copy_dst.buffer = readback_buffer2;
wgpu::CommandEncoder encoder2 = device_.CreateCommandEncoder();
encoder2.CopyTextureToBuffer(©_src, ©_dst, ©_size);
commands = encoder2.Finish();
queue.Submit(1, &commands);
webgpu()->DissociateMailbox(reservation2.id, reservation2.generation);
// Map the buffer.
readback_buffer2.MapAsync(wgpu::MapMode::Read, 0, buffer_desc.size,
wgpu::CallbackMode::AllowSpontaneous,
ToMockBufferMapCallback);
EXPECT_CALL(*mock_buffer_map_callback,
Call(wgpu::MapAsyncStatus::Success, testing::_))
.Times(1);
WaitForCompletion(device_);
}
// Test that an uninitialized writable shared image is lazily cleared by Dawn
// when it is read if a usage supporting lazy clearing is passed.
TEST_P(WebGPUMailboxTextureTest,
ReadWritableUninitializedSharedImageWithUsageSupportingLazyClearing) {
// Create the shared image.
SharedImageInterface* sii = GetSharedImageInterface();
scoped_refptr<gpu::ClientSharedImage> shared_image =
sii->CreateSharedImage({GetParam().format,
{1, 1},
gfx::ColorSpace::CreateSRGB(),
GetSharedImageUsage(AccessType::ReadWrite),
"TestLabel"},
kNullSurfaceHandle);
SyncToken mailbox_produced_token = sii->GenVerifiedSyncToken();
webgpu()->WaitSyncTokenCHROMIUM(mailbox_produced_token.GetConstData());
// Set the texture contents to non-zero so we can test a lazy clear occurs.
InitializeTextureColor(device_, shared_image->mailbox(), {1.0, 0, 0, 1.0});
// Register the shared image as a Dawn texture in the wire.
gpu::webgpu::ReservedTexture reservation =
webgpu()->ReserveTexture(device_.Get());
// Associate the mailbox. Using WEBGPU_MAILBOX_DISCARD will set the contents
// to uncleared.
webgpu()->AssociateMailbox(
reservation.deviceId, reservation.deviceGeneration, reservation.id,
reservation.generation,
WGPUTextureUsage_CopySrc | WGPUTextureUsage_RenderAttachment,
webgpu::WEBGPU_MAILBOX_DISCARD, shared_image->mailbox());
wgpu::Texture texture = wgpu::Texture::Acquire(reservation.texture);
// Read the texture using a render pass. Load+Store the contents.
// Uninitialized contents should not be loaded.
wgpu::RenderPassColorAttachment color_desc = {};
color_desc.view = texture.CreateView();
color_desc.loadOp = wgpu::LoadOp::Load;
color_desc.storeOp = wgpu::StoreOp::Store;
wgpu::RenderPassDescriptor render_pass_desc = {};
render_pass_desc.colorAttachmentCount = 1;
render_pass_desc.colorAttachments = &color_desc;
wgpu::CommandEncoder encoder = device_.CreateCommandEncoder();
wgpu::RenderPassEncoder pass = encoder.BeginRenderPass(&render_pass_desc);
pass.End();
// Copy the texture in a mappable buffer.
wgpu::BufferDescriptor buffer_desc;
buffer_desc.size = BytesPerTexel(GetParam().format);
buffer_desc.usage = wgpu::BufferUsage::MapRead | wgpu::BufferUsage::CopyDst;
wgpu::Buffer readback_buffer = device_.CreateBuffer(&buffer_desc);
wgpu::TexelCopyTextureInfo copy_src = {};
copy_src.texture = texture;
copy_src.mipLevel = 0;
copy_src.origin = {0, 0, 0};
wgpu::TexelCopyBufferInfo copy_dst = {};
copy_dst.buffer = readback_buffer;
copy_dst.layout.offset = 0;
copy_dst.layout.bytesPerRow = 256;
wgpu::Extent3D copy_size = {1, 1, 1};
encoder.CopyTextureToBuffer(©_src, ©_dst, ©_size);
wgpu::CommandBuffer commands = encoder.Finish();
wgpu::Queue queue = device_.GetQueue();
queue.Submit(1, &commands);
webgpu()->DissociateMailbox(reservation.id, reservation.generation);
// Map the buffer and assert the pixel is the correct value.
readback_buffer.MapAsync(wgpu::MapMode::Read, 0, buffer_desc.size,
wgpu::CallbackMode::AllowSpontaneous,
ToMockBufferMapCallback);
EXPECT_CALL(*mock_buffer_map_callback,
Call(wgpu::MapAsyncStatus::Success, testing::_))
.Times(1);
WaitForCompletion(device_);
const uint8_t* data = static_cast<const uint8_t*>(
readback_buffer.GetConstMappedRange(0, buffer_desc.size));
// Contents should be black because the texture was lazily cleared.
for (uint32_t i = 0; i < buffer_desc.size; ++i) {
EXPECT_EQ(data[i], uint8_t(0));
}
}
// Test that an uninitialized writable shared image is lazily cleared by Dawn
// when it is read if an internal usage supporting lazy clearing is passed.
TEST_P(
WebGPUMailboxTextureTest,
ReadWritableUninitializedSharedImageWithInternalUsageSupportingLazyClearing) {
// Create the shared image.
SharedImageInterface* sii = GetSharedImageInterface();
scoped_refptr<gpu::ClientSharedImage> shared_image =
sii->CreateSharedImage({GetParam().format,
{1, 1},
gfx::ColorSpace::CreateSRGB(),
GetSharedImageUsage(AccessType::ReadWrite),
"TestLabel"},
kNullSurfaceHandle);
SyncToken mailbox_produced_token = sii->GenVerifiedSyncToken();
webgpu()->WaitSyncTokenCHROMIUM(mailbox_produced_token.GetConstData());
// Set the texture contents to non-zero so we can test a lazy clear occurs.
InitializeTextureColor(device_, shared_image->mailbox(), {1.0, 0, 0, 1.0});
// Register the shared image as a Dawn texture in the wire.
gpu::webgpu::ReservedTexture reservation =
webgpu()->ReserveTexture(device_.Get());
// Associate the mailbox. Using WEBGPU_MAILBOX_DISCARD will set the contents
// to uncleared.
webgpu()->AssociateMailbox(
reservation.deviceId, reservation.deviceGeneration, reservation.id,
reservation.generation, WGPUTextureUsage_CopySrc,
WGPUTextureUsage_RenderAttachment, webgpu::WEBGPU_MAILBOX_DISCARD,
shared_image->mailbox());
wgpu::Texture texture = wgpu::Texture::Acquire(reservation.texture);
// Copy the texture in a mappable buffer.
wgpu::BufferDescriptor buffer_desc;
buffer_desc.size = BytesPerTexel(GetParam().format);
buffer_desc.usage = wgpu::BufferUsage::MapRead | wgpu::BufferUsage::CopyDst;
wgpu::Buffer readback_buffer = device_.CreateBuffer(&buffer_desc);
wgpu::TexelCopyTextureInfo copy_src = {};
copy_src.texture = texture;
copy_src.mipLevel = 0;
copy_src.origin = {0, 0, 0};
wgpu::TexelCopyBufferInfo copy_dst = {};
copy_dst.buffer = readback_buffer;
copy_dst.layout.offset = 0;
copy_dst.layout.bytesPerRow = 256;
wgpu::Extent3D copy_size = {1, 1, 1};
wgpu::CommandEncoder encoder = device_.CreateCommandEncoder();
encoder.CopyTextureToBuffer(©_src, ©_dst, ©_size);
wgpu::CommandBuffer commands = encoder.Finish();
wgpu::Queue queue = device_.GetQueue();
queue.Submit(1, &commands);
webgpu()->DissociateMailbox(reservation.id, reservation.generation);
// Map the buffer and assert the pixel is the correct value.
readback_buffer.MapAsync(wgpu::MapMode::Read, 0, buffer_desc.size,
wgpu::CallbackMode::AllowSpontaneous,
ToMockBufferMapCallback);
EXPECT_CALL(*mock_buffer_map_callback,
Call(wgpu::MapAsyncStatus::Success, testing::_))
.Times(1);
WaitForCompletion(device_);
const uint8_t* data = static_cast<const uint8_t*>(
readback_buffer.GetConstMappedRange(0, buffer_desc.size));
// Contents should be black because the texture was lazily cleared.
for (uint32_t i = 0; i < buffer_desc.size; ++i) {
EXPECT_EQ(data[i], uint8_t(0));
}
}
// Tests that using a shared image aftr it is dissociated produces an error.
TEST_P(WebGPUMailboxTextureTest, ErrorWhenUsingTextureAfterDissociate) {
// Create the shared image.
// NOTE: It's necessary to add WEBGPU_WRITE access as the created SharedImage
// will be uncleared and hence require lazy clearing on access.
// WebGPUDecoderImpl might also need to fall back to using Skia to read and
// write, making it necessary to add those usages as well.
SharedImageInterface* sii = GetSharedImageInterface();
scoped_refptr<gpu::ClientSharedImage> shared_image =
sii->CreateSharedImage({GetParam().format,
{1, 1},
gfx::ColorSpace::CreateSRGB(),
GetSharedImageUsage(AccessType::ReadWrite),
"TestLabel"},
kNullSurfaceHandle);
SyncToken mailbox_produced_token = sii->GenVerifiedSyncToken();
webgpu()->WaitSyncTokenCHROMIUM(mailbox_produced_token.GetConstData());
// Associate and immediately dissociate the image.
gpu::webgpu::ReservedTexture reservation =
webgpu()->ReserveTexture(device_.Get());
wgpu::Texture texture = wgpu::Texture::Acquire(reservation.texture);
// NOTE: Accessing an uncleared Dawn texture requires passing a usage that
// supports lazy clearing (otherwise AssociateMailbox() will generate an
// error, which is not the error case that this test is looking to test).
webgpu()->AssociateMailbox(
reservation.deviceId, reservation.deviceGeneration, reservation.id,
reservation.generation,
WGPUTextureUsage_CopySrc | WGPUTextureUsage_RenderAttachment,
webgpu::WEBGPU_MAILBOX_NONE, shared_image->mailbox());
webgpu()->DissociateMailbox(reservation.id, reservation.generation);
wgpu::TextureDescriptor dst_desc = {};
dst_desc.size = {1, 1};
dst_desc.usage = wgpu::TextureUsage::CopyDst;
DCHECK(GetParam().format == viz::SinglePlaneFormat::kRGBA_8888 ||
GetParam().format == viz::SinglePlaneFormat::kBGRA_8888 ||
GetParam().format == viz::SinglePlaneFormat::kRGBA_F16);
dst_desc.format = ToDawnFormat(GetParam().format);
wgpu::TexelCopyTextureInfo src_image = {};
src_image.texture = texture;
wgpu::TexelCopyTextureInfo dst_image = {};
dst_image.texture = device_.CreateTexture(&dst_desc);
wgpu::Extent3D extent = {1, 1};
// Try using the texture in a copy command; it should produce a validation
// error.
wgpu::CommandEncoder encoder = device_.CreateCommandEncoder();
encoder.CopyTextureToTexture(&src_image, &dst_image, &extent);
wgpu::CommandBuffer commandBuffer = encoder.Finish();
// Wait so it's clear the validation error after this when we call Submit.
WaitForCompletion(device_);
EXPECT_WEBGPU_ERROR(device_, wgpu::ErrorType::Validation,
device_.GetQueue().Submit(1, &commandBuffer));
}
// This is a regression test for an issue when using multiple shared images
// where a `ScopedAccess` was destroyed after it's `SharedImageRepresentation`.
// The code was similar to the following.
//
// struct Pair {
// unique_ptr<Representation> representation;
// unique_ptr<Access> access;
// };
//
// base::flat_map<Key, Pair> map;
// map.erase(some_iterator);
//
// In the Pair destructor C++ guarantees that `access` is destroyed before
// `representation` but `erase` can move one element over another, causing
// the move-assignment operator to be called. In this case the defaulted
// move-assignment would first move `representation` then `access`. Causing
// incorrect member destruction order for the move-to object.
TEST_P(WebGPUMailboxTextureTest, UseA_UseB_DestroyA_DestroyB) {
// Create a the shared images.
SharedImageInterface* sii = GetSharedImageInterface();
scoped_refptr<gpu::ClientSharedImage> shared_image_a =
sii->CreateSharedImage({GetParam().format,
{1, 1},
gfx::ColorSpace::CreateSRGB(),
GetSharedImageUsage(AccessType::ReadWrite),
"TestLabel"},
kNullSurfaceHandle);
scoped_refptr<gpu::ClientSharedImage> shared_image_b =
sii->CreateSharedImage({GetParam().format,
{1, 1},
gfx::ColorSpace::CreateSRGB(),
GetSharedImageUsage(AccessType::ReadWrite),
"TestLabel"},
kNullSurfaceHandle);
// Associate both mailboxes
gpu::webgpu::ReservedTexture reservation_a =
webgpu()->ReserveTexture(device_.Get());
webgpu()->AssociateMailbox(
reservation_a.deviceId, reservation_a.deviceGeneration, reservation_a.id,
reservation_a.generation, WGPUTextureUsage_RenderAttachment,
webgpu::WEBGPU_MAILBOX_NONE, shared_image_a->mailbox());
gpu::webgpu::ReservedTexture reservation_b =
webgpu()->ReserveTexture(device_.Get());
webgpu()->AssociateMailbox(
reservation_b.deviceId, reservation_b.deviceGeneration, reservation_b.id,
reservation_b.generation, WGPUTextureUsage_RenderAttachment,
webgpu::WEBGPU_MAILBOX_NONE, shared_image_b->mailbox());
// Dissociate both mailboxes in the same order.
webgpu()->DissociateMailbox(reservation_a.id, reservation_a.generation);
webgpu()->DissociateMailbox(reservation_b.id, reservation_b.generation);
// Send all the previous commands to the WebGPU decoder.
webgpu()->FlushCommands();
}
// Regression test for a bug where the (id, generation) for associated shared
// images was stored globally instead of per-device. This meant that of two
// devices tried to create shared images with the same (id, generation) (which
// is possible because they can be on different Dawn wires) they would conflict.
TEST_P(WebGPUMailboxTextureTest, AssociateOnTwoDevicesAtTheSameTime) {
// Create a the shared images.
SharedImageInterface* sii = GetSharedImageInterface();
scoped_refptr<gpu::ClientSharedImage> shared_image_a =
sii->CreateSharedImage({GetParam().format,
{1, 1},
gfx::ColorSpace::CreateSRGB(),
GetSharedImageUsage(AccessType::ReadWrite),
"TestLabel"},
kNullSurfaceHandle);
scoped_refptr<gpu::ClientSharedImage> shared_image_b =
sii->CreateSharedImage({GetParam().format,
{1, 1},
gfx::ColorSpace::CreateSRGB(),
GetSharedImageUsage(AccessType::ReadWrite),
"TestLabel"},
kNullSurfaceHandle);
// Two WebGPU devices to associate the shared images to.
wgpu::Device device_a = GetNewDevice();
wgpu::Device device_b = GetNewDevice();
// Associate both mailboxes
gpu::webgpu::ReservedTexture reservation_a =
webgpu()->ReserveTexture(device_a.Get());
webgpu()->AssociateMailbox(
reservation_a.deviceId, reservation_a.deviceGeneration, reservation_a.id,
reservation_a.generation, WGPUTextureUsage_RenderAttachment,
webgpu::WEBGPU_MAILBOX_NONE, shared_image_a->mailbox());
gpu::webgpu::ReservedTexture reservation_b =
webgpu()->ReserveTexture(device_b.Get());
webgpu()->AssociateMailbox(
reservation_b.deviceId, reservation_b.deviceGeneration, reservation_b.id,
reservation_b.generation, WGPUTextureUsage_RenderAttachment,
webgpu::WEBGPU_MAILBOX_NONE, shared_image_b->mailbox());
// Dissociate both mailboxes in the same order.
webgpu()->DissociateMailbox(reservation_a.id, reservation_a.generation);
webgpu()->DissociateMailbox(reservation_b.id, reservation_b.generation);
// Send all the previous commands to the WebGPU decoder.
webgpu()->FlushCommands();
}
// Test that passing a descriptor to ReserveTexture produces a client-side
// WGPUTexture that correctly reflects said descriptor.
TEST_P(WebGPUMailboxTextureTest, ReflectionOfDescriptor) {
// Check that reserving a texture with a full descriptor give the same data
// back through reflection.
wgpu::TextureDescriptor desc1 = {};
desc1.size = {1, 2, 3};
desc1.format = wgpu::TextureFormat::R32Float;
desc1.usage = wgpu::TextureUsage::CopyDst;
desc1.dimension = wgpu::TextureDimension::e2D;
desc1.sampleCount = 1;
desc1.mipLevelCount = 1;
gpu::webgpu::ReservedTexture reservation1 = webgpu()->ReserveTexture(
device_.Get(), reinterpret_cast<const WGPUTextureDescriptor*>(&desc1));
wgpu::Texture texture1 = wgpu::Texture::Acquire(reservation1.texture);
ASSERT_EQ(desc1.size.width, texture1.GetWidth());
ASSERT_EQ(desc1.size.height, texture1.GetHeight());
ASSERT_EQ(desc1.size.depthOrArrayLayers, texture1.GetDepthOrArrayLayers());
ASSERT_EQ(desc1.format, texture1.GetFormat());
ASSERT_EQ(desc1.usage, texture1.GetUsage());
ASSERT_EQ(desc1.dimension, texture1.GetDimension());
ASSERT_EQ(desc1.sampleCount, texture1.GetSampleCount());
ASSERT_EQ(desc1.mipLevelCount, texture1.GetMipLevelCount());
// Test with a different descriptor to check data is not hardcoded. Not that
// this is actually not a valid descriptor (diimension == 1D with height !=
// 1), but that it should still be reflected exactly.
wgpu::TextureDescriptor desc2 = {};
desc2.size = {4, 5, 6};
desc2.format = wgpu::TextureFormat::RGBA8Unorm;
desc2.usage = wgpu::TextureUsage::CopySrc;
desc2.dimension = wgpu::TextureDimension::e1D;
desc2.sampleCount = 4;
desc2.mipLevelCount = 3;
gpu::webgpu::ReservedTexture reservation2 = webgpu()->ReserveTexture(
device_.Get(), reinterpret_cast<const WGPUTextureDescriptor*>(&desc2));
wgpu::Texture texture2 = wgpu::Texture::Acquire(reservation2.texture);
ASSERT_EQ(desc2.size.width, texture2.GetWidth());
ASSERT_EQ(desc2.size.height, texture2.GetHeight());
ASSERT_EQ(desc2.size.depthOrArrayLayers, texture2.GetDepthOrArrayLayers());
ASSERT_EQ(desc2.format, texture2.GetFormat());
ASSERT_EQ(desc2.usage, texture2.GetUsage());
ASSERT_EQ(desc2.dimension, texture2.GetDimension());
ASSERT_EQ(desc2.sampleCount, texture2.GetSampleCount());
ASSERT_EQ(desc2.mipLevelCount, texture2.GetMipLevelCount());
// Associate mailboxes so that releasing the reserved wgpu::Textures does not
// fail. Note that these texture parameters do not match. It doesn't matter
// since the textures are not used in this test except for frontend
// reflection.
SharedImageInterface* sii = GetSharedImageInterface();
scoped_refptr<gpu::ClientSharedImage> shared_image1 =
sii->CreateSharedImage({GetParam().format,
{1, 1},
gfx::ColorSpace::CreateSRGB(),
GetSharedImageUsage(AccessType::ReadWrite),
"TestLabel"},
kNullSurfaceHandle);
scoped_refptr<gpu::ClientSharedImage> shared_image2 =
sii->CreateSharedImage({GetParam().format,
{1, 1},
gfx::ColorSpace::CreateSRGB(),
GetSharedImageUsage(AccessType::Read),
"TestLabel"},
kNullSurfaceHandle);
webgpu()->AssociateMailbox(
reservation1.deviceId, reservation1.deviceGeneration, reservation1.id,
reservation1.generation, static_cast<WGPUTextureUsage>(desc1.usage),
webgpu::WEBGPU_MAILBOX_NONE, shared_image1->mailbox());
webgpu()->AssociateMailbox(
reservation2.deviceId, reservation2.deviceGeneration, reservation2.id,
reservation2.generation, static_cast<WGPUTextureUsage>(desc2.usage),
webgpu::WEBGPU_MAILBOX_NONE, shared_image2->mailbox());
}
// Test that passing a texture with invalid view formats to AssociateMailbox
// does not cause WebGPU validation errors on a later call to DissociateMailbox.
TEST_P(WebGPUMailboxTextureTest, AssociateInvalidViewFormats) {
wgpu::TextureDescriptor desc = {};
desc.size = {1, 1, 1};
desc.format = ToDawnFormat(GetParam().format);
desc.usage = wgpu::TextureUsage::RenderAttachment;
desc.dimension = wgpu::TextureDimension::e2D;
desc.sampleCount = 1;
desc.mipLevelCount = 1;
gpu::webgpu::ReservedTexture reservation = webgpu()->ReserveTexture(
device_.Get(), reinterpret_cast<const WGPUTextureDescriptor*>(&desc));
wgpu::Texture texture = wgpu::Texture::Acquire(reservation.texture);
SharedImageInterface* sii = GetSharedImageInterface();
scoped_refptr<gpu::ClientSharedImage> shared_image =
sii->CreateSharedImage({GetParam().format,
{1, 1},
gfx::ColorSpace::CreateSRGB(),
GetSharedImageUsage(AccessType::ReadWrite),
"TestLabel"},
kNullSurfaceHandle);
WGPUTextureFormat view_formats = {
WGPUTextureFormat_R8Unorm,
};
// AssociateMailbox may cause validation errors, given the invalid
// viewFormats, so wrap it in an error scope.
device_.PushErrorScope(wgpu::ErrorFilter::Validation);
webgpu()->AssociateMailbox(
reservation.deviceId, reservation.deviceGeneration, reservation.id,
reservation.generation, static_cast<WGPUTextureUsage>(desc.usage),
&view_formats, 1, webgpu::WEBGPU_MAILBOX_NONE, shared_image->mailbox());
device_.PopErrorScope(
wgpu::CallbackMode::AllowSpontaneous,
[](wgpu::PopErrorScopeStatus, wgpu::ErrorType, wgpu::StringView) {});
// DissociateMailbox should NOT cause validation errors.
webgpu()->DissociateMailbox(reservation.id, reservation.generation);
}
// Test that if some other GL context is current when
// Associate/DissociateMailbox occurs, the operations do not fail. Some WebGPU
// shared image backings rely on GL and need to be responsible for making the
// context current.
TEST_P(WebGPUMailboxTextureTest, AssociateDissociateMailboxWhenNotCurrent) {
// Create the shared image
SharedImageInterface* sii = GetSharedImageInterface();
scoped_refptr<gpu::ClientSharedImage> shared_image =
sii->CreateSharedImage({GetParam().format,
{1, 1},
gfx::ColorSpace::CreateSRGB(),
GetSharedImageUsage(AccessType::ReadWrite),
"TestLabel"},
kNullSurfaceHandle);
scoped_refptr<gl::GLContext> gl_context1;
scoped_refptr<gl::GLContext> gl_context2;
scoped_refptr<gl::GLSurface> gl_surface1;
scoped_refptr<gl::GLSurface> gl_surface2;
// Create and make a new gl context current.
// Contexts must be created on the GPU thread, so this creates it on the GPU
// thread and sets a scoped_refptr on the main thread.
auto CreateAndMakeGLContextCurrent =
[&](scoped_refptr<gl::GLContext>* gl_context_out,
scoped_refptr<gl::GLSurface>* gl_surface_out) {
GetGpuServiceHolder()->ScheduleGpuMainTask(base::BindOnce(
[](scoped_refptr<gl::GLContext>* gl_context_out,
scoped_refptr<gl::GLSurface>* gl_surface_out) {
auto gl_surface = gl::init::CreateOffscreenGLSurface(
gl::GetDefaultDisplay(), gfx::Size(4, 4));
auto gl_context = gl::init::CreateGLContext(
nullptr, gl_surface.get(), gl::GLContextAttribs());
EXPECT_TRUE(gl_context->MakeCurrent(gl_surface.get()))
<< "Failed to make GL context current";
*gl_context_out = std::move(gl_context);
*gl_surface_out = std::move(gl_surface);
},
gl_context_out, gl_surface_out));
GetGpuServiceHolder()
->gpu_main_thread_task_runner()
->RunsTasksInCurrentSequence();
};
webgpu::ReservedTexture reservation = webgpu()->ReserveTexture(device_.Get());
// Create a GL context and make it current.
CreateAndMakeGLContextCurrent(&gl_context1, &gl_surface1);
webgpu()->AssociateMailbox(
reservation.deviceId, reservation.deviceGeneration, reservation.id,
reservation.generation, WGPUTextureUsage_RenderAttachment,
webgpu::WEBGPU_MAILBOX_NONE, shared_image->mailbox());
wgpu::Texture texture = wgpu::Texture::Acquire(reservation.texture);
// Clear the texture using a render pass.
wgpu::RenderPassColorAttachment color_desc = {};
color_desc.view = texture.CreateView();
color_desc.loadOp = wgpu::LoadOp::Clear;
color_desc.storeOp = wgpu::StoreOp::Store;
color_desc.clearValue = {0.0, 1.0, 0.0, 1.0};
wgpu::RenderPassDescriptor render_pass_desc = {};
render_pass_desc.colorAttachmentCount = 1;
render_pass_desc.colorAttachments = &color_desc;
wgpu::CommandEncoder encoder = device_.CreateCommandEncoder();
wgpu::RenderPassEncoder pass = encoder.BeginRenderPass(&render_pass_desc);
pass.End();
wgpu::CommandBuffer commands = encoder.Finish();
wgpu::Queue queue = device_.GetQueue();
queue.Submit(1, &commands);
WaitForCompletion(device_);
// Create another context and make it current.
// This is a distinct context to catch errors where Associate/Dissociate
// always use the current context, and in the test, these just so happen to be
// identical.
CreateAndMakeGLContextCurrent(&gl_context2, &gl_surface2);
webgpu()->DissociateMailbox(reservation.id, reservation.generation);
WaitForCompletion(device_);
// Delete the GL contexts on the GPU thread.
GetGpuServiceHolder()->ScheduleGpuMainTask(
base::BindOnce([](scoped_refptr<gl::GLContext> gl_context1,
scoped_refptr<gl::GLContext> gl_context2,
scoped_refptr<gl::GLSurface> gl_surface1,
scoped_refptr<gl::GLSurface> gl_surface2) {},
std::move(gl_context1), std::move(gl_context2),
std::move(gl_surface1), std::move(gl_surface2)));
}
INSTANTIATE_TEST_SUITE_P(
,
WebGPUMailboxTextureTest,
::testing::ValuesIn(WebGPUMailboxTextureTest::TestParams()),
::testing::PrintToStringParamName());
class WebGPUMailboxBufferTest : public WebGPUMailboxTestBase {
public:
void SetUp() override {
SKIP_TEST_IF(!WebGPUSupported());
SKIP_TEST_IF(!WebGPUSharedImageSupported());
WebGPUTest::SetUp();
WebGPUTest::Options options = {};
options.enable_unsafe_webgpu = true;
Initialize(options);
wgpu::AdapterInfo info;
adapter_.GetInfo(&info);
// Buffer-backed SharedImages are only supported on D3D12 right now.
SKIP_TEST_IF(info.backendType != wgpu::BackendType::D3D12);
device_ = GetNewDevice(kRequiredFeatures);
mock_buffer_map_callback =
std::make_unique<testing::StrictMock<MockBufferMapCallback>>();
SharedImageInterface* sii = GetSharedImageInterface();
shared_image_ = sii->CreateSharedImage(
{viz::SharedImageFormat(),
{kBufferSize, 1},
gfx::ColorSpace(),
kTopLeft_GrSurfaceOrigin,
kUnknown_SkAlphaType,
gpu::SHARED_IMAGE_USAGE_WEBGPU_READ |
gpu::SHARED_IMAGE_USAGE_WEBGPU_WRITE |
gpu::SHARED_IMAGE_USAGE_WEBGPU_SHARED_BUFFER,
"TestLabel"},
kNullSurfaceHandle);
}
void TearDown() override {
mock_buffer_map_callback = nullptr;
// Wait for all operations to catch any validation or device lost errors.
PollUntilIdle();
device_ = nullptr;
WebGPUTest::TearDown();
}
struct AssociateMailboxForBufferCmdStorage {
webgpu::cmds::AssociateMailboxForBufferImmediate cmd;
// Immediate data is copied into the space immediately following `cmd`.
// Allocate space to hold 1 mailbox.
GLbyte data[GL_MAILBOX_SIZE_CHROMIUM];
};
protected:
std::vector<wgpu::FeatureName> kRequiredFeatures = {
wgpu::FeatureName::SharedBufferMemoryD3D12Resource};
const int kBufferSize = 4;
const uint32_t kBufferData = 0x12345678;
wgpu::Device device_;
scoped_refptr<gpu::ClientSharedImage> shared_image_;
};
// Test that AssociateMailboxForBuffer works as expected.
TEST_F(WebGPUMailboxBufferTest, AssociateMailboxForBufferCmd) {
webgpu::ReservedBuffer reservation = webgpu()->ReserveBuffer(device_.Get());
GetGpuServiceHolder()->ScheduleGpuMainTask(base::BindOnce(
[](webgpu::WebGPUDecoder* decoder, webgpu::ReservedBuffer reservation,
scoped_refptr<gpu::ClientSharedImage> shared_image) {
const gpu::Mailbox& mailbox = shared_image->mailbox();
// Error case: device client id doesn't exist.
{
AssociateMailboxForBufferCmdStorage cmd;
cmd.cmd.Init(reservation.deviceId + 1, reservation.deviceGeneration,
reservation.id, reservation.generation,
WGPUBufferUsage_Storage,
reinterpret_cast<const GLuint*>(&mailbox.name));
EXPECT_EQ(
error::kInvalidArguments,
ExecuteImmediateCmd(decoder, cmd.cmd, sizeof(mailbox.name)));
}
// Error case: device generation is invalid.
{
AssociateMailboxForBufferCmdStorage cmd;
cmd.cmd.Init(reservation.deviceId, reservation.deviceGeneration + 1,
reservation.id, reservation.generation,
WGPUBufferUsage_Storage,
reinterpret_cast<const GLuint*>(&mailbox.name));
EXPECT_EQ(
error::kInvalidArguments,
ExecuteImmediateCmd(decoder, cmd.cmd, sizeof(mailbox.name)));
}
// Error case: buffer ID invalid for the wire server.
{
AssociateMailboxForBufferCmdStorage cmd;
cmd.cmd.Init(reservation.deviceId, reservation.deviceGeneration,
reservation.id + 1, reservation.generation,
WGPUBufferUsage_Storage,
reinterpret_cast<const GLuint*>(&mailbox.name));
EXPECT_EQ(
error::kInvalidArguments,
ExecuteImmediateCmd(decoder, cmd.cmd, sizeof(mailbox.name)));
}
// Error case: packed data empty.
{
AssociateMailboxForBufferCmdStorage cmd;
cmd.cmd.Init(reservation.deviceId, reservation.deviceGeneration,
reservation.id, reservation.generation,
WGPUBufferUsage_Storage,
reinterpret_cast<const GLuint*>(&mailbox.name));
EXPECT_EQ(error::kOutOfBounds,
ExecuteImmediateCmd(decoder, cmd.cmd, 0u));
}
// Control case: test a successful call to AssociateMailboxForBuffer.
// The control case is not put first because it modifies the internal
// state of the Dawn wire server and would make calls with the same
// buffer ID and generation invalid.
{
AssociateMailboxForBufferCmdStorage cmd;
cmd.cmd.Init(reservation.deviceId, reservation.deviceGeneration,
reservation.id, reservation.generation,
WGPUBufferUsage_Storage,
reinterpret_cast<const GLuint*>(&mailbox.name));
EXPECT_EQ(error::kNoError, ExecuteImmediateCmd(decoder, cmd.cmd,
sizeof(mailbox.name)));
}
// Error case: associated to an already associated buffer.
{
AssociateMailboxForBufferCmdStorage cmd;
cmd.cmd.Init(reservation.deviceId, reservation.deviceGeneration,
reservation.id, reservation.generation,
WGPUBufferUsage_Storage,
reinterpret_cast<const GLuint*>(&mailbox.name));
EXPECT_EQ(
error::kInvalidArguments,
ExecuteImmediateCmd(decoder, cmd.cmd, sizeof(mailbox.name)));
}
// Dissociate the buffer from the control case to remove its reference.
{
webgpu::cmds::DissociateMailboxForBuffer cmd;
cmd.Init(reservation.id, reservation.generation);
EXPECT_EQ(error::kNoError, ExecuteCmd(decoder, cmd));
}
},
GetDecoder(), reservation, std::move(shared_image_)));
GetGpuServiceHolder()
->gpu_main_thread_task_runner()
->RunsTasksInCurrentSequence();
}
// Test that AssociateMailboxForBuffer with a bad mailbox produces an error
// buffer.
TEST_F(WebGPUMailboxBufferTest,
AssociateMailboxForBufferCmdBadMailboxMakesErrorBuffer) {
webgpu::ReservedBuffer reservation = webgpu()->ReserveBuffer(device_.Get());
GetGpuServiceHolder()->ScheduleGpuMainTask(base::BindOnce(
[](webgpu::WebGPUDecoder* decoder, webgpu::ReservedBuffer reservation,
scoped_refptr<gpu::ClientSharedImage> shared_image) {
// Calling AssociateMailboxForBuffer with an invalid Mailbox should
// return an error buffer.
{
gpu::Mailbox bad_mailbox;
AssociateMailboxForBufferCmdStorage cmd;
cmd.cmd.Init(reservation.deviceId, reservation.deviceGeneration,
reservation.id, reservation.generation,
WGPUBufferUsage_CopyDst,
reinterpret_cast<const GLuint*>(&bad_mailbox.name));
EXPECT_EQ(
error::kNoError,
ExecuteImmediateCmd(decoder, cmd.cmd, sizeof(bad_mailbox.name)));
}
},
GetDecoder(), reservation, std::move(shared_image_)));
wgpu::Buffer buffer = wgpu::Buffer::Acquire(reservation.buffer);
wgpu::CommandEncoder encoder = device_.CreateCommandEncoder();
encoder.ClearBuffer(buffer, 0, kBufferSize);
// Expect an error when finishing encoding because the buffer is an error.
EXPECT_WEBGPU_ERROR(device_, wgpu::ErrorType::Validation,
wgpu::CommandBuffer commands = encoder.Finish());
}
// Test that DissociateMailboxForBuffer works as expected.
TEST_F(WebGPUMailboxBufferTest, DissociateMailboxForBufferCmd) {
webgpu::ReservedBuffer reservation = webgpu()->ReserveBuffer(device_.Get());
GetGpuServiceHolder()->ScheduleGpuMainTask(base::BindOnce(
[](webgpu::WebGPUDecoder* decoder, webgpu::ReservedBuffer reservation,
scoped_refptr<gpu::ClientSharedImage> shared_image) {
const gpu::Mailbox& mailbox = shared_image->mailbox();
// Associate a mailbox so we can later dissociate it.
{
AssociateMailboxForBufferCmdStorage cmd;
cmd.cmd.Init(reservation.deviceId, reservation.deviceGeneration,
reservation.id, reservation.generation,
WGPUBufferUsage_Storage,
reinterpret_cast<const GLuint*>(&mailbox.name));
EXPECT_EQ(error::kNoError, ExecuteImmediateCmd(decoder, cmd.cmd,
sizeof(mailbox.name)));
}
// Error case: wrong buffer ID
{
webgpu::cmds::DissociateMailboxForBuffer cmd;
cmd.Init(reservation.id + 1, reservation.generation);
EXPECT_EQ(error::kInvalidArguments, ExecuteCmd(decoder, cmd));
}
// Error case: wrong buffer generation
{
webgpu::cmds::DissociateMailboxForBuffer cmd;
cmd.Init(reservation.id, reservation.generation + 1);
EXPECT_EQ(error::kInvalidArguments, ExecuteCmd(decoder, cmd));
}
// Success case
{
webgpu::cmds::DissociateMailboxForBuffer cmd;
cmd.Init(reservation.id, reservation.generation);
EXPECT_EQ(error::kNoError, ExecuteCmd(decoder, cmd));
}
// Error case: dissociate an already dissociated mailbox
{
webgpu::cmds::DissociateMailboxForBuffer cmd;
cmd.Init(reservation.id, reservation.generation);
EXPECT_EQ(error::kInvalidArguments, ExecuteCmd(decoder, cmd));
}
},
GetDecoder(), reservation, std::move(shared_image_)));
GetGpuServiceHolder()
->gpu_main_thread_task_runner()
->RunsTasksInCurrentSequence();
}
// Tests using Associate/DissociateMailbox to share a buffer with Dawn.
TEST_F(WebGPUMailboxBufferTest, WriteToMailboxThenReadFromIt) {
webgpu::ReservedBuffer reservation = webgpu()->ReserveBuffer(device_.Get());
SharedImageInterface* sii = GetSharedImageInterface();
SyncToken mailbox_produced_token = sii->GenVerifiedSyncToken();
webgpu()->WaitSyncTokenCHROMIUM(mailbox_produced_token.GetConstData());
webgpu()->AssociateMailboxForBuffer(
reservation.deviceId, reservation.deviceGeneration, reservation.id,
reservation.generation,
WGPUBufferUsage_Storage | WGPUBufferUsage_CopySrc |
WGPUBufferUsage_CopyDst,
shared_image_->mailbox());
wgpu::Buffer mailbox_buffer = wgpu::Buffer::Acquire(reservation.buffer);
// Create an buffer and write data to it.
wgpu::BufferDescriptor data_buffer_desc;
data_buffer_desc.size = kBufferSize;
data_buffer_desc.usage =
wgpu::BufferUsage::CopyDst | wgpu::BufferUsage::CopySrc;
wgpu::Buffer data_buffer = device_.CreateBuffer(&data_buffer_desc);
wgpu::Queue queue = device_.GetQueue();
queue.WriteBuffer(data_buffer, 0, &kBufferData, kBufferSize);
// Create a readback buffer to store result data.
wgpu::BufferDescriptor readback_buffer_desc;
readback_buffer_desc.size = kBufferSize;
readback_buffer_desc.usage =
wgpu::BufferUsage::MapRead | wgpu::BufferUsage::CopyDst;
wgpu::Buffer readback_buffer = device_.CreateBuffer(&readback_buffer_desc);
// Copy data from the upload buffer to the mailbox buffer, then from the
// mailbox buffer to the readback buffer.
wgpu::CommandEncoder encoder = device_.CreateCommandEncoder();
encoder.CopyBufferToBuffer(data_buffer, 0, mailbox_buffer, 0, kBufferSize);
encoder.CopyBufferToBuffer(mailbox_buffer, 0, readback_buffer, 0,
kBufferSize);
wgpu::CommandBuffer commands = encoder.Finish();
queue = device_.GetQueue();
queue.Submit(1, &commands);
webgpu()->DissociateMailboxForBuffer(reservation.id, reservation.generation);
// Map the readback buffer and check that it contains the correct value.
readback_buffer.MapAsync(wgpu::MapMode::Read, 0, 4,
wgpu::CallbackMode::AllowSpontaneous,
ToMockBufferMapCallback);
EXPECT_CALL(*mock_buffer_map_callback,
Call(wgpu::MapAsyncStatus::Success, testing::_))
.Times(1);
WaitForCompletion(device_);
const void* readback_data = readback_buffer.GetConstMappedRange();
EXPECT_EQ(kBufferData, *static_cast<const uint32_t*>(readback_data));
}
// Tests that using a mailbox buffer after it is dissociated produces an
// error.
TEST_F(WebGPUMailboxBufferTest, ErrorWhenUsingBufferAfterDissociate) {
webgpu::ReservedBuffer reservation = webgpu()->ReserveBuffer(device_.Get());
SharedImageInterface* sii = GetSharedImageInterface();
SyncToken mailbox_produced_token = sii->GenVerifiedSyncToken();
webgpu()->WaitSyncTokenCHROMIUM(mailbox_produced_token.GetConstData());
wgpu::Buffer mailbox_buffer = wgpu::Buffer::Acquire(reservation.buffer);
webgpu()->AssociateMailboxForBuffer(
reservation.deviceId, reservation.deviceGeneration, reservation.id,
reservation.generation,
WGPUBufferUsage_Storage | WGPUBufferUsage_CopySrc |
WGPUBufferUsage_CopyDst,
shared_image_->mailbox());
webgpu()->DissociateMailboxForBuffer(reservation.id, reservation.generation);
wgpu::CommandEncoder encoder = device_.CreateCommandEncoder();
encoder.ClearBuffer(mailbox_buffer, 0, kBufferSize);
wgpu::CommandBuffer commands = encoder.Finish();
// Wait so it's clear the validation error after this when we call Submit.
WaitForCompletion(device_);
EXPECT_WEBGPU_ERROR(device_, wgpu::ErrorType::Validation,
device_.GetQueue().Submit(1, &commands));
WaitForCompletion(device_);
}
} // namespace gpu
|