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
|
/*
* Copyright (c) 2015-2025 The Khronos Group Inc.
* Copyright (c) 2015-2025 Valve Corporation
* Copyright (c) 2015-2025 LunarG, Inc.
* Copyright (c) 2015-2025 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*/
#include "../framework/layer_validation_tests.h"
#include "../framework/ray_tracing_objects.h"
#include "../framework/feature_requirements.h"
#include "../framework/descriptor_helper.h"
#include "../framework/pipeline_helper.h"
#include "utils/math_utils.h"
#include <algorithm>
void RayTracingTest::InitFrameworkForRayTracingTest(VkValidationFeaturesEXT* enabled_features /*= nullptr*/) {
AddRequiredExtensions(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME);
AddRequiredExtensions(VK_KHR_GET_MEMORY_REQUIREMENTS_2_EXTENSION_NAME);
AddRequiredExtensions(VK_KHR_ACCELERATION_STRUCTURE_EXTENSION_NAME);
AddRequiredExtensions(VK_KHR_RAY_TRACING_PIPELINE_EXTENSION_NAME);
AddRequiredExtensions(VK_KHR_RAY_QUERY_EXTENSION_NAME);
AddRequiredExtensions(VK_EXT_DESCRIPTOR_INDEXING_EXTENSION_NAME);
AddRequiredExtensions(VK_KHR_BUFFER_DEVICE_ADDRESS_EXTENSION_NAME);
AddRequiredExtensions(VK_KHR_SPIRV_1_4_EXTENSION_NAME);
AddRequiredExtensions(VK_KHR_DEFERRED_HOST_OPERATIONS_EXTENSION_NAME);
AddRequiredExtensions(VK_KHR_PIPELINE_LIBRARY_EXTENSION_NAME);
RETURN_IF_SKIP(InitFramework(enabled_features));
}
class PositiveRayTracing : public RayTracingTest {};
TEST_F(PositiveRayTracing, GetAccelerationStructureBuildSizes) {
TEST_DESCRIPTION("Test enabled features for GetAccelerationStructureBuildSizes");
SetTargetApiVersion(VK_API_VERSION_1_1);
AddRequiredExtensions(VK_KHR_ACCELERATION_STRUCTURE_EXTENSION_NAME);
AddRequiredFeature(vkt::Feature::accelerationStructure);
RETURN_IF_SKIP(InitFrameworkForRayTracingTest());
RETURN_IF_SKIP(InitState());
VkAccelerationStructureBuildGeometryInfoKHR build_info = vku::InitStructHelper();
build_info.type = VK_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL_KHR;
uint32_t max_primitives_count = 0;
VkAccelerationStructureBuildSizesInfoKHR build_sizes_info = vku::InitStructHelper();
vk::GetAccelerationStructureBuildSizesKHR(device(), VK_ACCELERATION_STRUCTURE_BUILD_TYPE_HOST_OR_DEVICE_KHR, &build_info,
&max_primitives_count, &build_sizes_info);
}
TEST_F(PositiveRayTracing, AccelerationStructureReference) {
TEST_DESCRIPTION("Test device side accelerationStructureReference");
SetTargetApiVersion(VK_API_VERSION_1_1);
AddRequiredExtensions(VK_KHR_BUFFER_DEVICE_ADDRESS_EXTENSION_NAME);
AddRequiredExtensions(VK_KHR_RAY_QUERY_EXTENSION_NAME);
AddRequiredExtensions(VK_KHR_ACCELERATION_STRUCTURE_EXTENSION_NAME);
AddRequiredFeature(vkt::Feature::bufferDeviceAddress);
AddRequiredFeature(vkt::Feature::accelerationStructure);
AddRequiredFeature(vkt::Feature::rayQuery);
RETURN_IF_SKIP(Init());
m_command_buffer.Begin();
// Build Bottom Level Acceleration Structure
vkt::as::BuildGeometryInfoKHR blas = vkt::as::blueprint::BuildGeometryInfoSimpleOnDeviceBottomLevel(*m_device);
blas.BuildCmdBuffer(m_command_buffer);
m_command_buffer.End();
m_default_queue->Submit(m_command_buffer);
m_device->Wait();
m_command_buffer.Begin();
// Build Top Level Acceleration Structure
// ---
vkt::as::BuildGeometryInfoKHR tlas = vkt::as::blueprint::BuildGeometryInfoSimpleOnDeviceTopLevel(*m_device, *blas.GetDstAS());
tlas.BuildCmdBuffer(m_command_buffer);
m_command_buffer.End();
m_default_queue->Submit(m_command_buffer);
m_device->Wait();
}
TEST_F(PositiveRayTracing, HostAccelerationStructureReference) {
TEST_DESCRIPTION("Test host side accelerationStructureReference");
SetTargetApiVersion(VK_API_VERSION_1_1);
AddRequiredExtensions(VK_KHR_ACCELERATION_STRUCTURE_EXTENSION_NAME);
AddRequiredFeature(vkt::Feature::accelerationStructure);
AddRequiredFeature(vkt::Feature::accelerationStructureHostCommands);
RETURN_IF_SKIP(Init());
// Build Bottom Level Acceleration Structure
auto blas =
std::make_shared<vkt::as::BuildGeometryInfoKHR>(vkt::as::blueprint::BuildGeometryInfoSimpleOnHostBottomLevel(*m_device));
blas->BuildHost();
// Build Top Level Acceleration Structure
vkt::as::BuildGeometryInfoKHR tlas = vkt::as::blueprint::BuildGeometryInfoSimpleOnHostTopLevel(*m_device, blas);
tlas.BuildHost();
}
TEST_F(PositiveRayTracing, CreateAccelerationStructureKHR) {
TEST_DESCRIPTION("Validate acceleration structure creation.");
SetTargetApiVersion(VK_API_VERSION_1_1);
AddRequiredFeature(vkt::Feature::accelerationStructure);
RETURN_IF_SKIP(InitFrameworkForRayTracingTest());
RETURN_IF_SKIP(InitState());
vkt::Buffer buffer(*m_device, 4096, VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_STORAGE_BIT_KHR);
VkAccelerationStructureKHR as;
VkAccelerationStructureCreateInfoKHR as_create_info = vku::InitStructHelper();
as_create_info.buffer = buffer;
as_create_info.size = 4096;
as_create_info.type = VK_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL_KHR;
vk::CreateAccelerationStructureKHR(device(), &as_create_info, nullptr, &as);
vk::DestroyAccelerationStructureKHR(device(), as, nullptr);
}
TEST_F(PositiveRayTracing, StridedDeviceAddressRegion) {
TEST_DESCRIPTION("Test different valid VkStridedDeviceAddressRegionKHR");
SetTargetApiVersion(VK_API_VERSION_1_2);
AddRequiredFeature(vkt::Feature::bufferDeviceAddress);
AddRequiredFeature(vkt::Feature::accelerationStructure);
AddRequiredFeature(vkt::Feature::rayTracingPipeline);
AddRequiredFeature(vkt::Feature::rayQuery);
RETURN_IF_SKIP(InitFrameworkForRayTracingTest());
RETURN_IF_SKIP(InitState());
vkt::rt::Pipeline rt_pipeline(*this, m_device);
rt_pipeline.SetGlslRayGenShader(kRayTracingMinimalGlsl);
rt_pipeline.AddGlslMissShader(kRayTracingPayloadMinimalGlsl);
rt_pipeline.AddGlslClosestHitShader(kRayTracingPayloadMinimalGlsl);
rt_pipeline.AddBinding(VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR, 0);
rt_pipeline.CreateDescriptorSet();
vkt::as::BuildGeometryInfoKHR tlas(vkt::as::blueprint::BuildOnDeviceTopLevel(*m_device, *m_default_queue, m_command_buffer));
rt_pipeline.GetDescriptorSet().WriteDescriptorAccelStruct(0, 1, &tlas.GetDstAS()->handle());
rt_pipeline.GetDescriptorSet().UpdateDescriptorSets();
rt_pipeline.Build();
vkt::rt::TraceRaysSbt sbt = rt_pipeline.GetTraceRaysSbt();
m_command_buffer.Begin();
vk::CmdBindPipeline(m_command_buffer, VK_PIPELINE_BIND_POINT_RAY_TRACING_KHR, rt_pipeline);
vk::CmdTraceRaysKHR(m_command_buffer, &sbt.ray_gen_sbt, &sbt.miss_sbt, &sbt.hit_sbt, &sbt.callable_sbt, 100, 100, 1);
// pMissShaderBindingTable->deviceAddress == 0
{
VkStridedDeviceAddressRegionKHR null_addr_miss_sbt = sbt.miss_sbt;
null_addr_miss_sbt.deviceAddress = 0;
vk::CmdTraceRaysKHR(m_command_buffer, &sbt.ray_gen_sbt, &null_addr_miss_sbt, &sbt.hit_sbt, &sbt.callable_sbt, 100, 100, 1);
}
// pMissShaderBindingTable->size == 0 => region is considered unused so no error
{
VkStridedDeviceAddressRegionKHR null_addr_miss_sbt = sbt.miss_sbt;
null_addr_miss_sbt.size = 0;
null_addr_miss_sbt.stride = 0;
vk::CmdTraceRaysKHR(m_command_buffer, &sbt.ray_gen_sbt, &null_addr_miss_sbt, &sbt.hit_sbt, &sbt.callable_sbt, 100, 100, 1);
}
m_command_buffer.End();
m_default_queue->Submit(m_command_buffer);
m_device->Wait();
}
TEST_F(PositiveRayTracing, BarrierAccessMaskAccelerationStructureRayQueryEnabledRTXDisabled) {
TEST_DESCRIPTION(
"Test barrier with access ACCELERATION_STRUCTURE bit."
"Ray query extension is enabled, as well as feature."
"RTX extensions are disabled.");
SetTargetApiVersion(VK_API_VERSION_1_1);
AddRequiredExtensions(VK_KHR_SYNCHRONIZATION_2_EXTENSION_NAME);
AddRequiredExtensions(VK_KHR_ACCELERATION_STRUCTURE_EXTENSION_NAME);
AddRequiredExtensions(VK_KHR_RAY_QUERY_EXTENSION_NAME);
AddRequiredFeature(vkt::Feature::synchronization2);
AddRequiredFeature(vkt::Feature::rayQuery);
RETURN_IF_SKIP(Init());
VkMemoryBarrier2 mem_barrier = vku::InitStructHelper();
mem_barrier.dstAccessMask = VK_ACCESS_2_SHADER_READ_BIT;
mem_barrier.dstStageMask = VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT;
vkt::Buffer buffer(*m_device, 32, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT);
VkBufferMemoryBarrier2 buffer_barrier = vku::InitStructHelper();
buffer_barrier.dstAccessMask = VK_ACCESS_2_SHADER_READ_BIT;
buffer_barrier.dstStageMask = VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT;
buffer_barrier.buffer = buffer;
buffer_barrier.size = 32;
vkt::Image image(*m_device, 128, 128, VK_FORMAT_B8G8R8A8_UNORM, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT);
VkImageMemoryBarrier2 image_barrier = vku::InitStructHelper();
image_barrier.dstAccessMask = VK_ACCESS_2_SHADER_READ_BIT;
image_barrier.dstStageMask = VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT;
image_barrier.image = image;
image_barrier.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1};
VkDependencyInfo dependency_info = vku::InitStructHelper();
dependency_info.memoryBarrierCount = 1;
dependency_info.pMemoryBarriers = &mem_barrier;
dependency_info.bufferMemoryBarrierCount = 1;
dependency_info.pBufferMemoryBarriers = &buffer_barrier;
dependency_info.imageMemoryBarrierCount = 1;
dependency_info.pImageMemoryBarriers = &image_barrier;
m_command_buffer.Begin();
mem_barrier.srcAccessMask = VK_ACCESS_2_ACCELERATION_STRUCTURE_READ_BIT_KHR;
mem_barrier.srcStageMask = VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT;
buffer_barrier.srcAccessMask = VK_ACCESS_2_ACCELERATION_STRUCTURE_READ_BIT_KHR;
buffer_barrier.srcStageMask = VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT;
image_barrier.srcAccessMask = VK_ACCESS_2_ACCELERATION_STRUCTURE_READ_BIT_KHR;
image_barrier.srcStageMask = VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT;
mem_barrier.dstAccessMask = VK_ACCESS_2_ACCELERATION_STRUCTURE_READ_BIT_KHR;
mem_barrier.dstStageMask = VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT;
buffer_barrier.dstAccessMask = VK_ACCESS_2_ACCELERATION_STRUCTURE_READ_BIT_KHR;
buffer_barrier.dstStageMask = VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT;
image_barrier.dstAccessMask = VK_ACCESS_2_ACCELERATION_STRUCTURE_READ_BIT_KHR;
image_barrier.dstStageMask = VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT;
vk::CmdPipelineBarrier2KHR(m_command_buffer, &dependency_info);
m_command_buffer.End();
}
TEST_F(PositiveRayTracing, BarrierAccessMaskAccelerationStructureRayQueryEnabledRTXEnabled) {
TEST_DESCRIPTION(
"Test barrier with access ACCELERATION_STRUCTURE bit."
"Ray query extension is enabled, as well as feature."
"RTX extensions are enabled.");
SetTargetApiVersion(VK_API_VERSION_1_1);
AddRequiredExtensions(VK_KHR_SYNCHRONIZATION_2_EXTENSION_NAME);
AddRequiredExtensions(VK_KHR_ACCELERATION_STRUCTURE_EXTENSION_NAME);
AddRequiredExtensions(VK_KHR_RAY_QUERY_EXTENSION_NAME);
AddRequiredExtensions(VK_KHR_RAY_TRACING_PIPELINE_EXTENSION_NAME);
AddRequiredFeature(vkt::Feature::rayTracingPipeline);
AddRequiredFeature(vkt::Feature::synchronization2);
AddRequiredFeature(vkt::Feature::rayQuery);
RETURN_IF_SKIP(Init());
VkMemoryBarrier2 mem_barrier = vku::InitStructHelper();
mem_barrier.dstAccessMask = VK_ACCESS_2_SHADER_READ_BIT;
mem_barrier.dstStageMask = VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT;
vkt::Buffer buffer(*m_device, 32, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT);
VkBufferMemoryBarrier2 buffer_barrier = vku::InitStructHelper();
buffer_barrier.dstAccessMask = VK_ACCESS_2_SHADER_READ_BIT;
buffer_barrier.dstStageMask = VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT;
buffer_barrier.buffer = buffer;
buffer_barrier.size = 32;
vkt::Image image(*m_device, 128, 128, VK_FORMAT_B8G8R8A8_UNORM, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT);
VkImageMemoryBarrier2 image_barrier = vku::InitStructHelper();
image_barrier.dstAccessMask = VK_ACCESS_2_SHADER_READ_BIT;
image_barrier.dstStageMask = VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT;
image_barrier.image = image;
image_barrier.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1};
VkDependencyInfo dependency_info = vku::InitStructHelper();
dependency_info.memoryBarrierCount = 1;
dependency_info.pMemoryBarriers = &mem_barrier;
dependency_info.bufferMemoryBarrierCount = 1;
dependency_info.pBufferMemoryBarriers = &buffer_barrier;
dependency_info.imageMemoryBarrierCount = 1;
dependency_info.pImageMemoryBarriers = &image_barrier;
m_command_buffer.Begin();
// specify VK_PIPELINE_STAGE_2_RAY_TRACING_SHADER_BIT_KHR as srcStageMask and dstStageMask
mem_barrier.srcAccessMask = VK_ACCESS_2_ACCELERATION_STRUCTURE_READ_BIT_KHR;
mem_barrier.srcStageMask = VK_PIPELINE_STAGE_2_RAY_TRACING_SHADER_BIT_KHR;
buffer_barrier.srcAccessMask = VK_ACCESS_2_ACCELERATION_STRUCTURE_READ_BIT_KHR;
buffer_barrier.srcStageMask = VK_PIPELINE_STAGE_2_RAY_TRACING_SHADER_BIT_KHR;
image_barrier.srcAccessMask = VK_ACCESS_2_ACCELERATION_STRUCTURE_READ_BIT_KHR;
image_barrier.srcStageMask = VK_PIPELINE_STAGE_2_RAY_TRACING_SHADER_BIT_KHR;
mem_barrier.dstAccessMask = VK_ACCESS_2_ACCELERATION_STRUCTURE_READ_BIT_KHR;
mem_barrier.dstStageMask = VK_PIPELINE_STAGE_2_RAY_TRACING_SHADER_BIT_KHR;
buffer_barrier.dstAccessMask = VK_ACCESS_2_ACCELERATION_STRUCTURE_READ_BIT_KHR;
buffer_barrier.dstStageMask = VK_PIPELINE_STAGE_2_RAY_TRACING_SHADER_BIT_KHR;
image_barrier.dstAccessMask = VK_ACCESS_2_ACCELERATION_STRUCTURE_READ_BIT_KHR;
image_barrier.dstStageMask = VK_PIPELINE_STAGE_2_RAY_TRACING_SHADER_BIT_KHR;
vk::CmdPipelineBarrier2KHR(m_command_buffer, &dependency_info);
m_command_buffer.End();
}
TEST_F(PositiveRayTracing, BarrierSync1NoCrash) {
TEST_DESCRIPTION("Regression test for nullptr crash when Sync1 barrier API is used for acceleration structure accesses");
SetTargetApiVersion(VK_API_VERSION_1_1);
AddRequiredExtensions(VK_KHR_ACCELERATION_STRUCTURE_EXTENSION_NAME);
RETURN_IF_SKIP(Init());
// This stage can not be used with ACCELERATION_STRUCTURE_READ access when ray query is disabled, but VVL also should not crash.
constexpr VkPipelineStageFlags invalid_src_stage = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
VkMemoryBarrier barrier = vku::InitStructHelper();
barrier.srcAccessMask = VK_ACCESS_ACCELERATION_STRUCTURE_READ_BIT_KHR;
m_errorMonitor->SetUnexpectedError("VUID-vkCmdPipelineBarrier-srcAccessMask-06257");
m_command_buffer.Begin();
vk::CmdPipelineBarrier(m_command_buffer, invalid_src_stage, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, 0, 1, &barrier, 0, nullptr, 0,
nullptr);
m_command_buffer.End();
}
TEST_F(PositiveRayTracing, BuildAccelerationStructuresList) {
TEST_DESCRIPTION("Build a list of destination acceleration structures, then do an update build on that same list");
SetTargetApiVersion(VK_API_VERSION_1_1);
AddRequiredFeature(vkt::Feature::accelerationStructure);
AddRequiredFeature(vkt::Feature::bufferDeviceAddress);
AddRequiredFeature(vkt::Feature::rayQuery);
RETURN_IF_SKIP(InitFrameworkForRayTracingTest());
RETURN_IF_SKIP(InitState());
constexpr size_t blas_count = 10;
std::vector<vkt::as::BuildGeometryInfoKHR> blas_vec;
for (size_t i = 0; i < blas_count; ++i) {
auto blas = vkt::as::blueprint::BuildGeometryInfoSimpleOnDeviceBottomLevel(*m_device);
blas.AddFlags(VK_BUILD_ACCELERATION_STRUCTURE_ALLOW_UPDATE_BIT_KHR);
blas_vec.emplace_back(std::move(blas));
}
m_command_buffer.Begin();
vkt::as::BuildAccelerationStructuresKHR(m_command_buffer, blas_vec);
m_command_buffer.End();
m_default_queue->Submit(m_command_buffer);
m_device->Wait();
for (auto& blas : blas_vec) {
blas.SetSrcAS(blas.GetDstAS());
blas.SetMode(VK_BUILD_ACCELERATION_STRUCTURE_MODE_UPDATE_KHR);
blas.SetDstAS(vkt::as::blueprint::AccelStructSimpleOnDeviceBottomLevel(*m_device, 4096));
}
m_command_buffer.Begin();
vkt::as::BuildAccelerationStructuresKHR(m_command_buffer, blas_vec);
m_command_buffer.End();
m_default_queue->Submit(m_command_buffer);
m_device->Wait();
}
TEST_F(PositiveRayTracing, BuildAccelerationStructuresList2) {
TEST_DESCRIPTION(
"Build a list of destination acceleration structures, with first build having a bigger build range than second.");
SetTargetApiVersion(VK_API_VERSION_1_1);
AddRequiredFeature(vkt::Feature::accelerationStructure);
AddRequiredFeature(vkt::Feature::bufferDeviceAddress);
AddRequiredFeature(vkt::Feature::rayQuery);
RETURN_IF_SKIP(InitFrameworkForRayTracingTest());
RETURN_IF_SKIP(InitState());
if (IsPlatformMockICD()) {
GTEST_SKIP() << "Test not supported by MockICD";
}
VkPhysicalDeviceAccelerationStructurePropertiesKHR as_props = vku::InitStructHelper();
VkPhysicalDeviceProperties2 phys_dev_props = vku::InitStructHelper(&as_props);
vk::GetPhysicalDeviceProperties2(m_device->Physical(), &phys_dev_props);
VkMemoryAllocateFlagsInfo alloc_flags = vku::InitStructHelper();
alloc_flags.flags = VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT;
auto scratch_buffer = std::make_shared<vkt::Buffer>(
*m_device, 4 * 1024 * 1024, VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, &alloc_flags);
std::vector<vkt::as::BuildGeometryInfoKHR> blas_vec;
auto blas_0 = vkt::as::blueprint::BuildGeometryInfoSimpleOnDeviceBottomLevel(*m_device);
std::vector<vkt::as::GeometryKHR> geometries;
geometries.emplace_back(vkt::as::blueprint::GeometrySimpleOnDeviceIndexedTriangleInfo(*m_device, 1000));
blas_0.SetGeometries(std::move(geometries));
blas_0.SetScratchBuffer(scratch_buffer);
auto blas_1 = vkt::as::blueprint::BuildGeometryInfoSimpleOnDeviceBottomLevel(*m_device);
blas_1.SetScratchBuffer(scratch_buffer);
auto size_info_1 = blas_1.GetSizeInfo();
// Scratch buffer used ranges:
// buffer start --> | blas_1 | <pad for alignment> | blas_0 |
// If scratch size if computed incorrectly, an overlap with scratch memory for blas_0 will be detected for blas_1
blas_0.SetDeviceScratchOffset(
Align<VkDeviceAddress>(size_info_1.buildScratchSize, as_props.minAccelerationStructureScratchOffsetAlignment));
blas_vec.emplace_back(std::move(blas_0));
blas_vec.emplace_back(std::move(blas_1));
m_command_buffer.Begin();
vkt::as::BuildAccelerationStructuresKHR(m_command_buffer, blas_vec);
m_command_buffer.End();
m_default_queue->Submit(m_command_buffer);
m_device->Wait();
}
TEST_F(PositiveRayTracing, AccelerationStructuresOverlappingMemory) {
TEST_DESCRIPTION(
"Validate acceleration structure building when source/destination acceleration structures and scratch buffers may "
"overlap.");
SetTargetApiVersion(VK_API_VERSION_1_1);
AddRequiredFeature(vkt::Feature::accelerationStructure);
AddRequiredFeature(vkt::Feature::bufferDeviceAddress);
AddRequiredFeature(vkt::Feature::rayQuery);
RETURN_IF_SKIP(InitFrameworkForRayTracingTest());
RETURN_IF_SKIP(InitState());
constexpr size_t blas_count = 3;
VkMemoryAllocateFlagsInfo alloc_flags = vku::InitStructHelper();
alloc_flags.flags = VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT;
VkMemoryAllocateInfo alloc_info = vku::InitStructHelper(&alloc_flags);
alloc_info.allocationSize = (1u << 18) * blas_count;
vkt::DeviceMemory buffer_memory(*m_device, alloc_info);
// Test using non overlapping memory chunks from the same buffer in multiple builds
// The scratch buffer is used in multiple builds but bound at different offsets, so no validation error should be issued
{
VkBufferCreateInfo scratch_buffer_ci = vku::InitStructHelper();
scratch_buffer_ci.size = alloc_info.allocationSize;
scratch_buffer_ci.usage = VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR |
VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT;
auto scratch_buffer = std::make_shared<vkt::Buffer>(*m_device, scratch_buffer_ci, vkt::no_mem);
vk::BindBufferMemory(device(), scratch_buffer->handle(), buffer_memory, 0);
std::vector<vkt::as::BuildGeometryInfoKHR> blas_vec;
VkDeviceSize consumed_buffer_size = 0;
for (size_t i = 0; i < blas_count; ++i) {
auto blas = vkt::as::blueprint::BuildGeometryInfoSimpleOnDeviceBottomLevel(*m_device);
blas.SetScratchBuffer(scratch_buffer);
blas.SetDeviceScratchOffset(consumed_buffer_size);
consumed_buffer_size += blas.GetSizeInfo().buildScratchSize;
consumed_buffer_size = Align<VkDeviceSize>(consumed_buffer_size, 4096);
blas_vec.emplace_back(std::move(blas));
}
m_command_buffer.Begin();
vkt::as::BuildAccelerationStructuresKHR(m_command_buffer, blas_vec);
m_command_buffer.End();
m_default_queue->Submit(m_command_buffer);
m_device->Wait();
}
}
TEST_F(PositiveRayTracing, AccelerationStructuresReuseScratchMemory) {
TEST_DESCRIPTION("Repro https://github.com/KhronosGroup/Vulkan-ValidationLayers/issues/6461");
SetTargetApiVersion(VK_API_VERSION_1_1);
AddRequiredFeature(vkt::Feature::accelerationStructure);
AddRequiredFeature(vkt::Feature::bufferDeviceAddress);
AddRequiredFeature(vkt::Feature::rayQuery);
RETURN_IF_SKIP(InitFrameworkForRayTracingTest());
RETURN_IF_SKIP(InitState());
// Allocate a memory chunk that will be used as backing memory for scratch buffer
VkMemoryAllocateFlagsInfo alloc_flags = vku::InitStructHelper();
alloc_flags.flags = VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT;
VkMemoryAllocateInfo alloc_info = vku::InitStructHelper(&alloc_flags);
alloc_info.allocationSize = 1u << 18;
vkt::DeviceMemory common_scratch_memory(*m_device, alloc_info);
vkt::CommandBuffer cmd_buffer_frame_0(*m_device, m_command_pool);
vkt::CommandBuffer cmd_buffer_frame_1(*m_device, m_command_pool);
vkt::CommandBuffer cmd_buffer_frame_2(*m_device, m_command_pool);
std::vector<vkt::as::BuildGeometryInfoKHR> blas_vec_frame_0;
std::vector<vkt::as::BuildGeometryInfoKHR> blas_vec_frame_1;
std::vector<vkt::as::BuildGeometryInfoKHR> blas_vec_frame_2;
auto scratch_buffer_frame_0 = std::make_shared<vkt::Buffer>();
auto scratch_buffer_frame_1 = std::make_shared<vkt::Buffer>();
auto scratch_buffer_frame_2 = std::make_shared<vkt::Buffer>();
vkt::Fence fence_frame_0(*m_device);
vkt::Fence fence_frame_1(*m_device);
vkt::Fence fence_frame_2(*m_device);
// Frame 0
{
// Nothing to wait for, resources used in frame 0 will be released in frame 2
// Create scratch buffer
VkBufferCreateInfo scratch_buffer_ci = vku::InitStructHelper();
scratch_buffer_ci.size = alloc_info.allocationSize;
scratch_buffer_ci.usage = VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR |
VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT;
scratch_buffer_frame_0->InitNoMemory(*m_device, scratch_buffer_ci);
// Bind memory to scratch buffer
vk::BindBufferMemory(device(), scratch_buffer_frame_0->handle(), common_scratch_memory, 0);
// Build a dummy acceleration structure
auto blas = vkt::as::blueprint::BuildGeometryInfoSimpleOnDeviceBottomLevel(*m_device);
blas.SetScratchBuffer(scratch_buffer_frame_0);
blas_vec_frame_0.emplace_back(std::move(blas));
cmd_buffer_frame_0.Begin();
vkt::as::BuildAccelerationStructuresKHR(cmd_buffer_frame_0, blas_vec_frame_0);
// Synchronize accesses to scratch buffer memory: next op will be a new acceleration structure build
VkBufferMemoryBarrier barrier = vku::InitStructHelper();
barrier.buffer = scratch_buffer_frame_0->handle();
barrier.size = scratch_buffer_ci.size;
barrier.srcAccessMask = VK_ACCESS_ACCELERATION_STRUCTURE_WRITE_BIT_KHR | VK_ACCESS_ACCELERATION_STRUCTURE_READ_BIT_KHR;
barrier.dstAccessMask = VK_ACCESS_ACCELERATION_STRUCTURE_WRITE_BIT_KHR | VK_ACCESS_ACCELERATION_STRUCTURE_READ_BIT_KHR;
vk::CmdPipelineBarrier(cmd_buffer_frame_0, VK_PIPELINE_STAGE_ACCELERATION_STRUCTURE_BUILD_BIT_KHR,
VK_PIPELINE_STAGE_ACCELERATION_STRUCTURE_BUILD_BIT_KHR, 0, 0, nullptr, 1, &barrier, 0, nullptr);
cmd_buffer_frame_0.End();
m_default_queue->Submit(cmd_buffer_frame_0, fence_frame_0);
}
// Frame 1
{
// Still nothing to wait for
// Create scratch buffer
VkBufferCreateInfo scratch_buffer_ci = vku::InitStructHelper();
scratch_buffer_ci.size = alloc_info.allocationSize;
scratch_buffer_ci.usage = VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR |
VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT;
scratch_buffer_frame_1->InitNoMemory(*m_device, scratch_buffer_ci);
// Bind memory to scratch buffer
vk::BindBufferMemory(device(), scratch_buffer_frame_1->handle(), common_scratch_memory, 0);
// Build a dummy acceleration structure
auto blas = vkt::as::blueprint::BuildGeometryInfoSimpleOnDeviceBottomLevel(*m_device);
blas.SetScratchBuffer(scratch_buffer_frame_1);
blas_vec_frame_1.emplace_back(std::move(blas));
cmd_buffer_frame_1.Begin();
vkt::as::BuildAccelerationStructuresKHR(cmd_buffer_frame_1, blas_vec_frame_1);
// Synchronize accesses to scratch buffer memory: next op will be a new acceleration structure build
VkBufferMemoryBarrier barrier = vku::InitStructHelper();
barrier.buffer = scratch_buffer_frame_1->handle();
barrier.size = scratch_buffer_ci.size;
barrier.srcAccessMask = VK_ACCESS_ACCELERATION_STRUCTURE_WRITE_BIT_KHR | VK_ACCESS_ACCELERATION_STRUCTURE_READ_BIT_KHR;
barrier.dstAccessMask = VK_ACCESS_ACCELERATION_STRUCTURE_WRITE_BIT_KHR | VK_ACCESS_ACCELERATION_STRUCTURE_READ_BIT_KHR;
vk::CmdPipelineBarrier(cmd_buffer_frame_1, VK_PIPELINE_STAGE_ACCELERATION_STRUCTURE_BUILD_BIT_KHR,
VK_PIPELINE_STAGE_ACCELERATION_STRUCTURE_BUILD_BIT_KHR, 0, 0, nullptr, 1, &barrier, 0, nullptr);
cmd_buffer_frame_1.End();
m_default_queue->Submit(cmd_buffer_frame_1, fence_frame_1);
}
// Frame 2
{
// Free resources from frame 0
fence_frame_0.Wait(kWaitTimeout);
// Destroying buffer triggers VUID-vkDestroyBuffer-buffer-00922, it is still considered in use by cmd_buffer_frame_0 this
// should not happen assuming synchronization is correct
// Adding "fence_frame_1.Wait(kWaitTimeout);" used to solve this issue.
// Using a dedicated memory chunk for each scratch buffer also used to solve it.
// The issue was that when recording a acceleration structure build command,
// any buffer indirectly mentioned through a device address used to be added using a call to GetBuffersByAddress.
// So when recording the build happening on frame 1, given that all scratch buffers have the same base device address,
// scratch_buffer_frame_0 was *also* be added as a child to cmd_buffer_frame_1.
// So when destroying it hereinafter, since frame 1 is still in flight, scratch_buffer_frame_0 is still
// considered in use, so 00922 is triggered.
// => Solution: buffers obtained through a call to GetBuffersByAddress should not get added as children,
// since there is no 1 to 1 mapping between a device address and a buffer.
scratch_buffer_frame_0 = nullptr; // Remove reference
blas_vec_frame_0.clear(); // scratch_buffer_frame_0 will be destroyed in this call
// Create scratch buffer
VkBufferCreateInfo scratch_buffer_ci = vku::InitStructHelper();
scratch_buffer_ci.size = alloc_info.allocationSize;
scratch_buffer_ci.usage = VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR |
VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT;
scratch_buffer_frame_2->InitNoMemory(*m_device, scratch_buffer_ci);
// Bind memory to scratch buffer
vk::BindBufferMemory(device(), scratch_buffer_frame_2->handle(), common_scratch_memory, 0);
// Build a dummy acceleration structure
auto blas = vkt::as::blueprint::BuildGeometryInfoSimpleOnDeviceBottomLevel(*m_device);
blas.SetScratchBuffer(scratch_buffer_frame_2);
blas_vec_frame_2.emplace_back(std::move(blas));
cmd_buffer_frame_2.Begin();
vkt::as::BuildAccelerationStructuresKHR(cmd_buffer_frame_2, blas_vec_frame_2);
// Synchronize accesses to scratch buffer memory: next op will be a new acceleration structure build
VkBufferMemoryBarrier barrier = vku::InitStructHelper();
barrier.buffer = scratch_buffer_frame_2->handle();
barrier.size = scratch_buffer_ci.size;
barrier.srcAccessMask = VK_ACCESS_ACCELERATION_STRUCTURE_WRITE_BIT_KHR | VK_ACCESS_ACCELERATION_STRUCTURE_READ_BIT_KHR;
barrier.dstAccessMask = VK_ACCESS_ACCELERATION_STRUCTURE_WRITE_BIT_KHR | VK_ACCESS_ACCELERATION_STRUCTURE_READ_BIT_KHR;
vk::CmdPipelineBarrier(cmd_buffer_frame_2, VK_PIPELINE_STAGE_ACCELERATION_STRUCTURE_BUILD_BIT_KHR,
VK_PIPELINE_STAGE_ACCELERATION_STRUCTURE_BUILD_BIT_KHR, 0, 0, nullptr, 1, &barrier, 0, nullptr);
cmd_buffer_frame_2.End();
m_default_queue->Submit(cmd_buffer_frame_2, fence_frame_2);
}
fence_frame_1.Wait(kWaitTimeout);
fence_frame_2.Wait(kWaitTimeout);
}
TEST_F(PositiveRayTracing, AccelerationStructuresDedicatedScratchMemory) {
TEST_DESCRIPTION(
"Repro https://github.com/KhronosGroup/Vulkan-ValidationLayers/issues/6461"
"This time, each scratch buffer has its own memory");
SetTargetApiVersion(VK_API_VERSION_1_1);
AddRequiredFeature(vkt::Feature::accelerationStructure);
AddRequiredFeature(vkt::Feature::bufferDeviceAddress);
AddRequiredFeature(vkt::Feature::rayQuery);
RETURN_IF_SKIP(InitFrameworkForRayTracingTest());
RETURN_IF_SKIP(InitState());
vkt::CommandBuffer cmd_buffer_frame_0(*m_device, m_command_pool);
vkt::CommandBuffer cmd_buffer_frame_1(*m_device, m_command_pool);
vkt::CommandBuffer cmd_buffer_frame_2(*m_device, m_command_pool);
std::vector<vkt::as::BuildGeometryInfoKHR> blas_vec_frame_0;
std::vector<vkt::as::BuildGeometryInfoKHR> blas_vec_frame_1;
std::vector<vkt::as::BuildGeometryInfoKHR> blas_vec_frame_2;
vkt::Fence fence_frame_0(*m_device);
vkt::Fence fence_frame_1(*m_device);
vkt::Fence fence_frame_2(*m_device);
// Frame 0
{
// Nothing to wait for, resources used in frame 0 will be released in frame 2
// Build a dummy acceleration structure
auto blas = vkt::as::blueprint::BuildGeometryInfoSimpleOnDeviceBottomLevel(*m_device);
blas_vec_frame_0.emplace_back(std::move(blas));
cmd_buffer_frame_0.Begin();
vkt::as::BuildAccelerationStructuresKHR(cmd_buffer_frame_0, blas_vec_frame_0);
// Synchronize accesses to scratch buffer memory: next op will be a new acceleration structure build
VkBufferMemoryBarrier barrier = vku::InitStructHelper();
barrier.buffer = blas_vec_frame_0[0].GetScratchBuffer()->handle();
barrier.size = blas_vec_frame_0[0].GetScratchBuffer()->CreateInfo().size;
barrier.srcAccessMask = VK_ACCESS_ACCELERATION_STRUCTURE_WRITE_BIT_KHR | VK_ACCESS_ACCELERATION_STRUCTURE_READ_BIT_KHR;
barrier.dstAccessMask = VK_ACCESS_ACCELERATION_STRUCTURE_WRITE_BIT_KHR | VK_ACCESS_ACCELERATION_STRUCTURE_READ_BIT_KHR;
vk::CmdPipelineBarrier(cmd_buffer_frame_0, VK_PIPELINE_STAGE_ACCELERATION_STRUCTURE_BUILD_BIT_KHR,
VK_PIPELINE_STAGE_ACCELERATION_STRUCTURE_BUILD_BIT_KHR, 0, 0, nullptr, 1, &barrier, 0, nullptr);
cmd_buffer_frame_0.End();
m_default_queue->Submit(cmd_buffer_frame_0, fence_frame_0);
}
// Frame 1
{
// Still nothing to wait for
// Build a dummy acceleration structure
auto blas = vkt::as::blueprint::BuildGeometryInfoSimpleOnDeviceBottomLevel(*m_device);
blas_vec_frame_1.emplace_back(std::move(blas));
cmd_buffer_frame_1.Begin();
vkt::as::BuildAccelerationStructuresKHR(cmd_buffer_frame_1, blas_vec_frame_1);
// Synchronize accesses to scratch buffer memory: next op will be a new acceleration structure build
VkBufferMemoryBarrier barrier = vku::InitStructHelper();
barrier.buffer = blas_vec_frame_1[0].GetScratchBuffer()->handle();
barrier.size = blas_vec_frame_1[0].GetScratchBuffer()->CreateInfo().size;
barrier.srcAccessMask = VK_ACCESS_ACCELERATION_STRUCTURE_WRITE_BIT_KHR | VK_ACCESS_ACCELERATION_STRUCTURE_READ_BIT_KHR;
barrier.dstAccessMask = VK_ACCESS_ACCELERATION_STRUCTURE_WRITE_BIT_KHR | VK_ACCESS_ACCELERATION_STRUCTURE_READ_BIT_KHR;
vk::CmdPipelineBarrier(cmd_buffer_frame_1, VK_PIPELINE_STAGE_ACCELERATION_STRUCTURE_BUILD_BIT_KHR,
VK_PIPELINE_STAGE_ACCELERATION_STRUCTURE_BUILD_BIT_KHR, 0, 0, nullptr, 1, &barrier, 0, nullptr);
cmd_buffer_frame_1.End();
m_default_queue->Submit(cmd_buffer_frame_1, fence_frame_1);
}
// Frame 2
{
// Free resources from frame 0
fence_frame_0.Wait(kWaitTimeout);
blas_vec_frame_0.clear(); // No validation error
// Build a dummy acceleration structure
auto blas = vkt::as::blueprint::BuildGeometryInfoSimpleOnDeviceBottomLevel(*m_device);
blas_vec_frame_2.emplace_back(std::move(blas));
cmd_buffer_frame_2.Begin();
vkt::as::BuildAccelerationStructuresKHR(cmd_buffer_frame_2, blas_vec_frame_2);
// Synchronize accesses to scratch buffer memory: next op will be a new acceleration structure build
VkBufferMemoryBarrier barrier = vku::InitStructHelper();
barrier.buffer = blas_vec_frame_2[0].GetScratchBuffer()->handle();
barrier.size = blas_vec_frame_2[0].GetScratchBuffer()->CreateInfo().size;
barrier.srcAccessMask = VK_ACCESS_ACCELERATION_STRUCTURE_WRITE_BIT_KHR | VK_ACCESS_ACCELERATION_STRUCTURE_READ_BIT_KHR;
barrier.dstAccessMask = VK_ACCESS_ACCELERATION_STRUCTURE_WRITE_BIT_KHR | VK_ACCESS_ACCELERATION_STRUCTURE_READ_BIT_KHR;
vk::CmdPipelineBarrier(cmd_buffer_frame_2, VK_PIPELINE_STAGE_ACCELERATION_STRUCTURE_BUILD_BIT_KHR,
VK_PIPELINE_STAGE_ACCELERATION_STRUCTURE_BUILD_BIT_KHR, 0, 0, nullptr, 1, &barrier, 0, nullptr);
cmd_buffer_frame_2.End();
m_default_queue->Submit(cmd_buffer_frame_2, fence_frame_2);
}
fence_frame_1.Wait(kWaitTimeout);
fence_frame_2.Wait(kWaitTimeout);
}
TEST_F(PositiveRayTracing, CmdBuildAccelerationStructuresIndirect) {
TEST_DESCRIPTION("basic usage of vkCmdBuildAccelerationStructuresIndirectKHR.");
SetTargetApiVersion(VK_API_VERSION_1_1);
AddRequiredFeature(vkt::Feature::bufferDeviceAddress);
AddRequiredFeature(vkt::Feature::accelerationStructure);
AddRequiredFeature(vkt::Feature::accelerationStructureIndirectBuild);
AddRequiredFeature(vkt::Feature::rayQuery);
RETURN_IF_SKIP(InitFrameworkForRayTracingTest());
RETURN_IF_SKIP(InitState());
auto blas = vkt::as::blueprint::BuildGeometryInfoSimpleOnDeviceBottomLevel(*m_device);
m_command_buffer.Begin();
blas.BuildCmdBufferIndirect(m_command_buffer);
m_command_buffer.End();
}
TEST_F(PositiveRayTracing, ScratchBufferCorrectAddressSpaceOpBuild) {
TEST_DESCRIPTION(
"Have two scratch buffers bound to the same memory, with one of them being not big enough for an acceleration structure "
"build, but the other one is. If the buffer addresses of those buffers are the same, 03671 should not fire");
SetTargetApiVersion(VK_API_VERSION_1_2);
AddRequiredFeature(vkt::Feature::bufferDeviceAddress);
AddRequiredFeature(vkt::Feature::accelerationStructure);
AddRequiredFeature(vkt::Feature::rayTracingPipeline);
RETURN_IF_SKIP(InitFrameworkForRayTracingTest());
RETURN_IF_SKIP(InitState());
auto blas = vkt::as::blueprint::BuildGeometryInfoSimpleOnDeviceBottomLevel(*m_device);
auto size_info = blas.GetSizeInfo();
if (size_info.buildScratchSize <= 64) {
GTEST_SKIP() << "Need a big scratch size, skipping test.";
}
VkPhysicalDeviceAccelerationStructurePropertiesKHR acc_struct_properties = vku::InitStructHelper();
GetPhysicalDeviceProperties2(acc_struct_properties);
VkDeviceSize scratch_size = size_info.buildScratchSize + acc_struct_properties.minAccelerationStructureScratchOffsetAlignment;
scratch_size = Align<VkDeviceSize>(scratch_size, acc_struct_properties.minAccelerationStructureScratchOffsetAlignment);
// Allocate buffer memory separately so that it can be large enough. Scratch buffer size will be smaller.
VkMemoryAllocateFlagsInfo alloc_flags = vku::InitStructHelper();
alloc_flags.flags = VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT;
VkMemoryAllocateInfo alloc_info = vku::InitStructHelper(&alloc_flags);
alloc_info.allocationSize = scratch_size;
vkt::DeviceMemory buffer_memory(*m_device, alloc_info);
VkBufferCreateInfo small_buffer_ci = vku::InitStructHelper();
small_buffer_ci.size = 64;
small_buffer_ci.usage = VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT;
auto small_scratch_buffer = std::make_shared<vkt::Buffer>(*m_device, small_buffer_ci, vkt::no_mem);
small_scratch_buffer->BindMemory(buffer_memory, 0);
small_buffer_ci.size = alloc_info.allocationSize;
auto big_scratch_buffer = std::make_shared<vkt::Buffer>(*m_device, small_buffer_ci, vkt::no_mem);
big_scratch_buffer->BindMemory(buffer_memory, 0);
const VkDeviceAddress big_scratch_address = big_scratch_buffer->Address();
if (big_scratch_address != small_scratch_buffer->Address()) {
GTEST_SKIP() << "Binding two buffers to the same memory does not yield identical buffer addresses, skipping test.";
}
m_command_buffer.Begin();
blas.SetScratchBuffer(small_scratch_buffer);
blas.BuildCmdBuffer(m_command_buffer);
m_command_buffer.End();
}
TEST_F(PositiveRayTracing, BasicTraceRays) {
TEST_DESCRIPTION(
"Setup a ray tracing pipeline (ray generation, miss and closest hit shaders) and acceleration structure, and trace one "
"ray. Only call traceRay in the ray generation shader");
SetTargetApiVersion(VK_API_VERSION_1_2);
AddRequiredFeature(vkt::Feature::rayTracingPipeline);
AddRequiredFeature(vkt::Feature::accelerationStructure);
AddRequiredFeature(vkt::Feature::bufferDeviceAddress);
RETURN_IF_SKIP(InitFrameworkForRayTracingTest());
RETURN_IF_SKIP(InitState());
vkt::rt::Pipeline pipeline(*this, m_device);
// Set shaders
const char* ray_gen = R"glsl(
#version 460
#extension GL_EXT_ray_tracing : require // Requires SPIR-V 1.5 (Vulkan 1.2)
layout(binding = 0, set = 0) uniform accelerationStructureEXT tlas;
layout(location = 0) rayPayloadEXT vec3 hit;
void main() {
traceRayEXT(tlas, gl_RayFlagsOpaqueEXT, 0xff, 0, 0, 0, vec3(0,0,1), 0.1, vec3(0,0,1), 1000.0, 0);
}
)glsl";
pipeline.SetGlslRayGenShader(ray_gen);
const char* miss = R"glsl(
#version 460
#extension GL_EXT_ray_tracing : require
layout(location = 0) rayPayloadInEXT vec3 hit;
void main() {
hit = vec3(0.1, 0.2, 0.3);
}
)glsl";
pipeline.AddGlslMissShader(miss);
const char* closest_hit = R"glsl(
#version 460
#extension GL_EXT_ray_tracing : require
layout(location = 0) rayPayloadInEXT vec3 hit;
hitAttributeEXT vec2 baryCoord;
void main() {
const vec3 barycentricCoords = vec3(1.0f - baryCoord.x - baryCoord.y, baryCoord.x, baryCoord.y);
hit = barycentricCoords;
}
)glsl";
pipeline.AddGlslClosestHitShader(closest_hit);
pipeline.AddBinding(VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR, 0);
pipeline.CreateDescriptorSet();
vkt::as::BuildGeometryInfoKHR tlas(vkt::as::blueprint::BuildOnDeviceTopLevel(*m_device, *m_default_queue, m_command_buffer));
pipeline.GetDescriptorSet().WriteDescriptorAccelStruct(0, 1, &tlas.GetDstAS()->handle());
pipeline.GetDescriptorSet().UpdateDescriptorSets();
// Build pipeline
pipeline.Build();
// Bind descriptor set, pipeline, and trace rays
m_command_buffer.Begin();
vk::CmdBindDescriptorSets(m_command_buffer, VK_PIPELINE_BIND_POINT_RAY_TRACING_KHR, pipeline.GetPipelineLayout(), 0, 1,
&pipeline.GetDescriptorSet().set_, 0, nullptr);
vk::CmdBindPipeline(m_command_buffer, VK_PIPELINE_BIND_POINT_RAY_TRACING_KHR, pipeline);
vkt::rt::TraceRaysSbt trace_rays_sbt = pipeline.GetTraceRaysSbt();
vk::CmdTraceRaysKHR(m_command_buffer, &trace_rays_sbt.ray_gen_sbt, &trace_rays_sbt.miss_sbt, &trace_rays_sbt.hit_sbt,
&trace_rays_sbt.callable_sbt, 1, 1, 1);
m_command_buffer.End();
m_default_queue->Submit(m_command_buffer);
m_device->Wait();
}
TEST_F(PositiveRayTracing, BasicTraceRaysDeferredBuild) {
TEST_DESCRIPTION(
"Setup a ray tracing pipeline (ray generation, miss and closest hit shaders, and deferred build) and acceleration "
"structure, and trace one "
"ray. Only call traceRay in the ray generation shader");
SetTargetApiVersion(VK_API_VERSION_1_2);
AddRequiredFeature(vkt::Feature::rayTracingPipeline);
AddRequiredFeature(vkt::Feature::accelerationStructure);
AddRequiredFeature(vkt::Feature::bufferDeviceAddress);
RETURN_IF_SKIP(InitFrameworkForRayTracingTest());
RETURN_IF_SKIP(InitState());
vkt::rt::Pipeline pipeline(*this, m_device);
// Set shaders
const char* ray_gen = R"glsl(
#version 460
#extension GL_EXT_ray_tracing : require // Requires SPIR-V 1.5 (Vulkan 1.2)
layout(binding = 0, set = 0) uniform accelerationStructureEXT tlas;
layout(location = 0) rayPayloadEXT vec3 hit;
void main() {
traceRayEXT(tlas, gl_RayFlagsOpaqueEXT, 0xff, 0, 0, 0, vec3(0,0,1), 0.1, vec3(0,0,1), 1000.0, 0);
}
)glsl";
pipeline.SetGlslRayGenShader(ray_gen);
const char* miss = R"glsl(
#version 460
#extension GL_EXT_ray_tracing : require
layout(location = 0) rayPayloadInEXT vec3 hit;
void main() {
hit = vec3(0.1, 0.2, 0.3);
}
)glsl";
pipeline.AddGlslMissShader(miss);
const char* closest_hit = R"glsl(
#version 460
#extension GL_EXT_ray_tracing : require
layout(location = 0) rayPayloadInEXT vec3 hit;
hitAttributeEXT vec2 baryCoord;
void main() {
const vec3 barycentricCoords = vec3(1.0f - baryCoord.x - baryCoord.y, baryCoord.x, baryCoord.y);
hit = barycentricCoords;
}
)glsl";
pipeline.AddGlslClosestHitShader(closest_hit);
pipeline.AddBinding(VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR, 0);
pipeline.CreateDescriptorSet();
vkt::as::BuildGeometryInfoKHR tlas(vkt::as::blueprint::BuildOnDeviceTopLevel(*m_device, *m_default_queue, m_command_buffer));
pipeline.GetDescriptorSet().WriteDescriptorAccelStruct(0, 1, &tlas.GetDstAS()->handle());
pipeline.GetDescriptorSet().UpdateDescriptorSets();
// Deferred pipeline build
RETURN_IF_SKIP(pipeline.DeferBuild());
RETURN_IF_SKIP(pipeline.Build());
// Bind descriptor set, pipeline, and trace rays
m_command_buffer.Begin();
vk::CmdBindDescriptorSets(m_command_buffer, VK_PIPELINE_BIND_POINT_RAY_TRACING_KHR, pipeline.GetPipelineLayout(), 0, 1,
&pipeline.GetDescriptorSet().set_, 0, nullptr);
vk::CmdBindPipeline(m_command_buffer, VK_PIPELINE_BIND_POINT_RAY_TRACING_KHR, pipeline);
vkt::rt::TraceRaysSbt trace_rays_sbt = pipeline.GetTraceRaysSbt();
vk::CmdTraceRaysKHR(m_command_buffer, &trace_rays_sbt.ray_gen_sbt, &trace_rays_sbt.miss_sbt, &trace_rays_sbt.hit_sbt,
&trace_rays_sbt.callable_sbt, 1, 1, 1);
m_command_buffer.End();
m_default_queue->Submit(m_command_buffer);
m_device->Wait();
}
TEST_F(PositiveRayTracing, GetAccelerationStructureAddressBadBuffer) {
TEST_DESCRIPTION(
"Call vkGetAccelerationStructureDeviceAddressKHR on an acceleration structure whose buffer is missing usage "
"VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT, and whose memory has been destroyed");
SetTargetApiVersion(VK_API_VERSION_1_1);
AddRequiredExtensions(VK_KHR_MAINTENANCE_5_EXTENSION_NAME);
AddRequiredFeature(vkt::Feature::accelerationStructure);
AddRequiredFeature(vkt::Feature::bufferDeviceAddress);
AddRequiredFeature(vkt::Feature::rayQuery);
AddRequiredFeature(vkt::Feature::maintenance5);
RETURN_IF_SKIP(InitFrameworkForRayTracingTest());
RETURN_IF_SKIP(InitState());
VkBufferUsageFlags2CreateInfo buffer_usage = vku::InitStructHelper();
buffer_usage.usage = VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_STORAGE_BIT_KHR | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT;
VkBufferCreateInfo buffer_ci = vku::InitStructHelper(&buffer_usage);
buffer_ci.size = 4096;
vkt::Buffer buffer(*m_device, buffer_ci, vkt::no_mem);
VkMemoryRequirements mem_reqs;
vk::GetBufferMemoryRequirements(device(), buffer, &mem_reqs);
VkMemoryAllocateFlagsInfo alloc_flags = vku::InitStructHelper();
alloc_flags.flags = VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT;
VkMemoryAllocateInfo alloc_info = vku::InitStructHelper(&alloc_flags);
alloc_info.allocationSize = 4096;
vkt::DeviceMemory mem(*m_device, alloc_info);
vk::BindBufferMemory(device(), buffer, mem, 0);
VkAccelerationStructureKHR as;
VkAccelerationStructureCreateInfoKHR as_create_info = vku::InitStructHelper();
as_create_info.buffer = buffer;
as_create_info.size = 4096;
as_create_info.type = VK_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL_KHR;
vk::CreateAccelerationStructureKHR(device(), &as_create_info, nullptr, &as);
VkAccelerationStructureDeviceAddressInfoKHR as_address_info = vku::InitStructHelper();
as_address_info.accelerationStructure = as;
vk::GetAccelerationStructureDeviceAddressKHR(device(), &as_address_info);
vk::DestroyAccelerationStructureKHR(device(), as, nullptr);
}
// Use to be invalid, but VUID-vkCmdBuildAccelerationStructuresKHR-firstVertex-03770 was removed in
// https://gitlab.khronos.org/vulkan/vulkan/-/merge_requests/6733
TEST_F(PositiveRayTracing, UpdatedFirstVertex) {
TEST_DESCRIPTION(
"Build a list of destination acceleration structures, then do an update build on that same list but with a different "
"VkAccelerationStructureBuildRangeInfoKHR::firstVertex");
SetTargetApiVersion(VK_API_VERSION_1_1);
AddRequiredFeature(vkt::Feature::bufferDeviceAddress);
AddRequiredFeature(vkt::Feature::accelerationStructure);
AddRequiredFeature(vkt::Feature::rayQuery);
RETURN_IF_SKIP(InitFrameworkForRayTracingTest());
RETURN_IF_SKIP(InitState());
m_command_buffer.Begin();
auto blas = vkt::as::blueprint::BuildGeometryInfoSimpleOnDeviceBottomLevel(*m_device);
blas.AddFlags(VK_BUILD_ACCELERATION_STRUCTURE_ALLOW_UPDATE_BIT_KHR);
blas.BuildCmdBuffer(m_command_buffer);
m_command_buffer.End();
m_default_queue->Submit(m_command_buffer);
m_device->Wait();
m_command_buffer.Begin();
blas.SetMode(VK_BUILD_ACCELERATION_STRUCTURE_MODE_UPDATE_KHR);
blas.SetSrcAS(blas.GetDstAS());
// Create custom build ranges, with the default valid as a template, then somehow supply it?
auto build_range_infos = blas.GetBuildRangeInfosFromGeometries();
build_range_infos[0].firstVertex = 666;
blas.SetBuildRanges(build_range_infos);
blas.BuildCmdBuffer(m_command_buffer);
m_command_buffer.End();
}
TEST_F(PositiveRayTracing, BindGraphicsPipelineAfterRayTracingPipeline) {
TEST_DESCRIPTION("Bind a graphics pipeline width dynamic line width state after binding ray tracing pipeline");
SetTargetApiVersion(VK_API_VERSION_1_2);
AddRequiredFeature(vkt::Feature::rayTracingPipeline);
AddRequiredFeature(vkt::Feature::accelerationStructure);
AddRequiredFeature(vkt::Feature::bufferDeviceAddress);
RETURN_IF_SKIP(InitFrameworkForRayTracingTest());
RETURN_IF_SKIP(InitState());
InitRenderTarget();
vkt::rt::Pipeline pipeline(*this, m_device);
// Set shaders
const char* ray_gen = R"glsl(
#version 460
#extension GL_EXT_ray_tracing : require // Requires SPIR-V 1.5 (Vulkan 1.2)
layout(binding = 0, set = 0) uniform accelerationStructureEXT tlas;
layout(location = 0) rayPayloadEXT vec3 hit;
void main() {
traceRayEXT(tlas, gl_RayFlagsOpaqueEXT, 0xff, 0, 0, 0, vec3(0,0,1), 0.1, vec3(0,0,1), 1000.0, 0);
}
)glsl";
pipeline.SetGlslRayGenShader(ray_gen);
const char* miss = R"glsl(
#version 460
#extension GL_EXT_ray_tracing : require
layout(location = 0) rayPayloadInEXT vec3 hit;
void main() {
hit = vec3(0.1, 0.2, 0.3);
}
)glsl";
pipeline.AddGlslMissShader(miss);
const char* closest_hit = R"glsl(
#version 460
#extension GL_EXT_ray_tracing : require
layout(location = 0) rayPayloadInEXT vec3 hit;
hitAttributeEXT vec2 baryCoord;
void main() {
const vec3 barycentricCoords = vec3(1.0f - baryCoord.x - baryCoord.y, baryCoord.x, baryCoord.y);
hit = barycentricCoords;
}
)glsl";
pipeline.AddGlslClosestHitShader(closest_hit);
pipeline.AddBinding(VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR, 0);
pipeline.CreateDescriptorSet();
vkt::as::BuildGeometryInfoKHR tlas(vkt::as::blueprint::BuildOnDeviceTopLevel(*m_device, *m_default_queue, m_command_buffer));
pipeline.GetDescriptorSet().WriteDescriptorAccelStruct(0, 1, &tlas.GetDstAS()->handle());
pipeline.GetDescriptorSet().UpdateDescriptorSets();
// Build pipeline
pipeline.Build();
CreatePipelineHelper graphics_pipeline(*this);
graphics_pipeline.AddDynamicState(VK_DYNAMIC_STATE_LINE_WIDTH);
graphics_pipeline.ia_ci_.topology = VK_PRIMITIVE_TOPOLOGY_LINE_LIST;
graphics_pipeline.CreateGraphicsPipeline();
// Bind descriptor set, pipeline, and trace rays
m_command_buffer.Begin();
vk::CmdBindDescriptorSets(m_command_buffer, VK_PIPELINE_BIND_POINT_RAY_TRACING_KHR, pipeline.GetPipelineLayout(), 0, 1,
&pipeline.GetDescriptorSet().set_, 0, nullptr);
vk::CmdBindPipeline(m_command_buffer, VK_PIPELINE_BIND_POINT_RAY_TRACING_KHR, pipeline);
vk::CmdBindPipeline(m_command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, graphics_pipeline);
vk::CmdSetLineWidth(m_command_buffer, 1.0f);
vkt::rt::TraceRaysSbt trace_rays_sbt = pipeline.GetTraceRaysSbt();
vk::CmdTraceRaysKHR(m_command_buffer, &trace_rays_sbt.ray_gen_sbt, &trace_rays_sbt.miss_sbt, &trace_rays_sbt.hit_sbt,
&trace_rays_sbt.callable_sbt, 1, 1, 1);
m_command_buffer.End();
m_default_queue->Submit(m_command_buffer);
m_device->Wait();
}
TEST_F(PositiveRayTracing, InstanceBufferBadAddress) {
TEST_DESCRIPTION("Use an invalid address for an instance buffer, but also specify a primitiveCount of 0 => no errors");
SetTargetApiVersion(VK_API_VERSION_1_2);
AddRequiredFeature(vkt::Feature::bufferDeviceAddress);
AddRequiredFeature(vkt::Feature::accelerationStructure);
AddRequiredFeature(vkt::Feature::rayTracingPipeline);
RETURN_IF_SKIP(InitFrameworkForRayTracingTest());
RETURN_IF_SKIP(InitState());
vkt::as::BuildGeometryInfoKHR blas = vkt::as::blueprint::BuildGeometryInfoSimpleOnDeviceBottomLevel(*m_device);
m_command_buffer.Begin();
blas.BuildCmdBuffer(m_command_buffer);
m_command_buffer.End();
m_default_queue->Submit(m_command_buffer);
m_device->Wait();
auto tlas = vkt::as::blueprint::BuildGeometryInfoSimpleOnDeviceTopLevel(*m_device, *blas.GetDstAS());
m_command_buffer.Begin();
tlas.SetupBuild(*m_device, true);
auto build_range_infos = tlas.GetBuildRangeInfosFromGeometries();
build_range_infos[0].primitiveCount = 0;
tlas.SetBuildRanges(build_range_infos);
tlas.GetGeometries()[0].SetInstancesDeviceAddress(0);
tlas.VkCmdBuildAccelerationStructuresKHR(m_command_buffer);
m_command_buffer.End();
}
TEST_F(PositiveRayTracing, WriteAccelerationStructuresPropertiesDevice) {
TEST_DESCRIPTION("Test getting query results from vkCmdWriteAccelerationStructuresPropertiesKHR");
SetTargetApiVersion(VK_API_VERSION_1_1);
AddRequiredExtensions(VK_KHR_ACCELERATION_STRUCTURE_EXTENSION_NAME);
AddRequiredExtensions(VK_KHR_BUFFER_DEVICE_ADDRESS_EXTENSION_NAME);
AddRequiredExtensions(VK_KHR_RAY_TRACING_MAINTENANCE_1_EXTENSION_NAME);
AddRequiredFeature(vkt::Feature::accelerationStructure);
AddRequiredFeature(vkt::Feature::bufferDeviceAddress);
AddRequiredFeature(vkt::Feature::rayTracingMaintenance1);
RETURN_IF_SKIP(InitFrameworkForRayTracingTest());
RETURN_IF_SKIP(InitState());
vkt::Buffer buffer(*m_device, 4 * sizeof(uint64_t), VK_BUFFER_USAGE_TRANSFER_DST_BIT);
vkt::as::BuildGeometryInfoKHR blas = vkt::as::blueprint::BuildGeometryInfoSimpleOnDeviceBottomLevel(*m_device);
blas.SetFlags(VK_BUILD_ACCELERATION_STRUCTURE_ALLOW_COMPACTION_BIT_KHR);
vkt::QueryPool query_pool(*m_device, VK_QUERY_TYPE_ACCELERATION_STRUCTURE_SIZE_KHR, 1);
m_command_buffer.Begin();
blas.BuildCmdBuffer(m_command_buffer);
m_command_buffer.End();
m_default_queue->Submit(m_command_buffer);
m_device->Wait();
m_command_buffer.Begin();
vk::CmdResetQueryPool(m_command_buffer, query_pool, 0u, 1u);
vk::CmdWriteAccelerationStructuresPropertiesKHR(m_command_buffer, 1, &blas.GetDstAS()->handle(),
VK_QUERY_TYPE_ACCELERATION_STRUCTURE_SIZE_KHR, query_pool, 0);
vk::CmdCopyQueryPoolResults(m_command_buffer, query_pool, 0u, 1u, buffer, 0u, sizeof(uint64_t),
VK_QUERY_RESULT_64_BIT | VK_QUERY_RESULT_WAIT_BIT);
m_command_buffer.End();
m_default_queue->Submit(m_command_buffer);
m_device->Wait();
}
TEST_F(PositiveRayTracing, BasicOpacityMicromapBuild) {
TEST_DESCRIPTION("Test building an opacity micromap then building an acceleration structure with that");
// Mask data for 2 levels of subdivision. Middle triangle is index 1, so drop that one out.
// Bit string for middle missing is '1011' (0 on the left). In number form, that's 0xd.
// Extending the Sierpinski-esque pattern out one level is 0xdd0d
uint32_t testMask = 0xdd0d;
SetTargetApiVersion(VK_API_VERSION_1_1);
AddRequiredExtensions(VK_KHR_SYNCHRONIZATION_2_EXTENSION_NAME);
AddRequiredExtensions(VK_EXT_OPACITY_MICROMAP_EXTENSION_NAME);
AddRequiredFeature(vkt::Feature::bufferDeviceAddress);
AddRequiredFeature(vkt::Feature::accelerationStructure);
AddRequiredFeature(vkt::Feature::synchronization2);
AddRequiredFeature(vkt::Feature::rayTracingPipeline);
AddRequiredFeature(vkt::Feature::micromap);
AddRequiredFeature(vkt::Feature::micromapHostCommands);
RETURN_IF_SKIP(InitFrameworkForRayTracingTest());
RETURN_IF_SKIP(InitState());
if (IsPlatformMockICD()) {
GTEST_SKIP() << "Test not supported by MockICD";
}
VkMemoryAllocateFlagsInfo allocate_da_flag_info = vku::InitStructHelper();
allocate_da_flag_info.flags = VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT;
// Create a buffer with the mask and index data
vkt::Buffer micromapDataBuffer(*m_device, 2*1048576 /*XXX*/,
VK_BUFFER_USAGE_MICROMAP_BUILD_INPUT_READ_ONLY_BIT_EXT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT,
vkt::device_address);
VkDeviceAddress micromapAddress = micromapDataBuffer.Address();
// Fill out VkMicromapUsageEXT with size information
VkMicromapUsageEXT mmUsage = { };
mmUsage.count = 1;
const int TriangleOffset = 0;
const int IndexOffset = 256;
const int DataOffset = 512;
mmUsage.subdivisionLevel = 2;
mmUsage.format = VK_OPACITY_MICROMAP_FORMAT_2_STATE_EXT;
{
uint32_t* data = (uint32_t*)micromapDataBuffer.Memory().Map();
VkMicromapTriangleEXT* tri = (VkMicromapTriangleEXT*)&data[TriangleOffset/4];
tri->dataOffset = 0;
tri->subdivisionLevel = uint16_t(mmUsage.subdivisionLevel);
tri->format = uint16_t(mmUsage.format);
// Micromap data
// Just replicate for testing higher subdivision
{
uint32_t maskWord = testMask | (testMask << 16);
int words = ((1 << (2*mmUsage.subdivisionLevel)) + 31) / 32;
for (int i = 0; i<words; i++) {
data[DataOffset / 4 + i] = maskWord;
}
}
// Index information
data[IndexOffset / 4] = 0;
}
VkMicromapBuildInfoEXT mmBuildInfo = { VK_STRUCTURE_TYPE_MICROMAP_BUILD_INFO_EXT };
mmBuildInfo.type = VK_MICROMAP_TYPE_OPACITY_MICROMAP_EXT;
mmBuildInfo.flags = 0;
mmBuildInfo.mode = VK_BUILD_MICROMAP_MODE_BUILD_EXT;
mmBuildInfo.dstMicromap = VK_NULL_HANDLE;
mmBuildInfo.usageCountsCount = 1;
mmBuildInfo.pUsageCounts = &mmUsage;
mmBuildInfo.data.deviceAddress = 0ull;
mmBuildInfo.triangleArray.deviceAddress = 0ull;
mmBuildInfo.triangleArrayStride = 0;
VkMicromapBuildSizesInfoEXT sizeInfo = { VK_STRUCTURE_TYPE_MICROMAP_BUILD_SIZES_INFO_EXT };
vk::GetMicromapBuildSizesEXT(device(), VK_ACCELERATION_STRUCTURE_BUILD_TYPE_DEVICE_KHR, &mmBuildInfo, &sizeInfo);
// Create a buffer and micromap on top from the size
vkt::Buffer micromapBuffer(*m_device, sizeInfo.micromapSize, VK_BUFFER_USAGE_MICROMAP_STORAGE_BIT_EXT);
// Scratch buffer
vkt::Buffer msBuffer(*m_device, sizeInfo.buildScratchSize > 4 ? sizeInfo.buildScratchSize : 4,
VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT,
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
&allocate_da_flag_info);
VkDeviceAddress msAddress = msBuffer.Address();
VkMicromapEXT micromap;
VkMicromapCreateInfoEXT maCreateInfo = { VK_STRUCTURE_TYPE_MICROMAP_CREATE_INFO_EXT };
maCreateInfo.createFlags = 0;
maCreateInfo.buffer = micromapBuffer;
maCreateInfo.offset = 0;
maCreateInfo.size = sizeInfo.micromapSize;
maCreateInfo.type = VK_MICROMAP_TYPE_OPACITY_MICROMAP_EXT;
maCreateInfo.deviceAddress = 0ull;
VkResult result = vk::CreateMicromapEXT(device(), &maCreateInfo, nullptr, µmap);
ASSERT_EQ(VK_SUCCESS, result);
// Build the array with vkBuildmicromapsEXT
{
// Fill in the pointers we didn't have at size query
mmBuildInfo.dstMicromap = micromap;
mmBuildInfo.data.deviceAddress = micromapAddress+DataOffset;
mmBuildInfo.triangleArray.deviceAddress = micromapAddress+TriangleOffset;
mmBuildInfo.scratchData.deviceAddress = msAddress;
m_command_buffer.Begin();
vk::CmdBuildMicromapsEXT(m_command_buffer, 1, &mmBuildInfo);
{
VkMemoryBarrier2 memoryBarrier = {VK_STRUCTURE_TYPE_MEMORY_BARRIER_2,
NULL,
VK_PIPELINE_STAGE_2_MICROMAP_BUILD_BIT_EXT,
VK_ACCESS_2_MICROMAP_WRITE_BIT_EXT,
VK_PIPELINE_STAGE_2_ACCELERATION_STRUCTURE_BUILD_BIT_KHR,
VK_ACCESS_2_MICROMAP_READ_BIT_EXT};
m_command_buffer.BarrierKHR(memoryBarrier);
}
m_command_buffer.End();
m_default_queue->Submit(m_command_buffer);
m_device->Wait();
}
// Create a buffer with the triangle data in it
static float const vertexData[6*2] = {
0.25, 0.75,
0.5, 0.25,
0.75, 0.75,
};
static uint32_t const indexData[6] = { 0, 1, 2 };
vkt::Buffer vertexBuffer(*m_device, sizeof(vertexData) + sizeof(indexData),
VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT, vkt::device_address);
VkDeviceAddress vertexAddress = vertexBuffer.Address();
// Upload data to the vertex buffer.
{
char* ptr;
vk::MapMemory(device(), vertexBuffer.Memory(), 0, VK_WHOLE_SIZE, 0, (void**)&ptr);
memcpy(ptr, &vertexData[0], sizeof(vertexData));
memcpy(ptr+sizeof(vertexData), &indexData[0], sizeof(indexData));
vk::UnmapMemory(device(), vertexBuffer.Memory());
}
VkAccelerationStructureBuildSizesInfoKHR bottomASBuildSizesInfo{VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_BUILD_SIZES_INFO_KHR};
VkAccelerationStructureBuildSizesInfoKHR topASBuildSizesInfo{VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_BUILD_SIZES_INFO_KHR};
// Create a bottom-level acceleration structure with one triangle
VkAccelerationStructureGeometryKHR bottomASGeometry = { VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_GEOMETRY_KHR };
bottomASGeometry.geometryType = VK_GEOMETRY_TYPE_TRIANGLES_KHR;
bottomASGeometry.geometry.triangles = vku::InitStructHelper();
bottomASGeometry.geometry.triangles.vertexFormat = VK_FORMAT_R32G32_SFLOAT;
bottomASGeometry.geometry.triangles.vertexData.deviceAddress = vertexAddress;
bottomASGeometry.geometry.triangles.vertexStride = 8;
bottomASGeometry.geometry.triangles.maxVertex = 3;
bottomASGeometry.geometry.triangles.indexType = VK_INDEX_TYPE_UINT32;
bottomASGeometry.geometry.triangles.indexData.deviceAddress = vertexAddress + sizeof(vertexData);
bottomASGeometry.geometry.triangles.transformData.deviceAddress = 0;
bottomASGeometry.flags = 0;
VkAccelerationStructureTrianglesOpacityMicromapEXT opacityGeometryMicromap = { VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_TRIANGLES_OPACITY_MICROMAP_EXT };
opacityGeometryMicromap.indexType = VK_INDEX_TYPE_UINT32;
opacityGeometryMicromap.indexBuffer.deviceAddress = micromapAddress + IndexOffset;
opacityGeometryMicromap.indexStride = 0;
opacityGeometryMicromap.baseTriangle = 0;
opacityGeometryMicromap.micromap = micromap;
bottomASGeometry.geometry.triangles.pNext = &opacityGeometryMicromap;
VkAccelerationStructureBuildGeometryInfoKHR bottomASInfo = { VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_BUILD_GEOMETRY_INFO_KHR };
bottomASInfo.type = VK_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL_KHR;
bottomASInfo.flags = 0;
bottomASInfo.mode = VK_BUILD_ACCELERATION_STRUCTURE_MODE_BUILD_KHR;
bottomASInfo.srcAccelerationStructure = VK_NULL_HANDLE;
bottomASInfo.dstAccelerationStructure = VK_NULL_HANDLE;
bottomASInfo.geometryCount = 1;
bottomASInfo.pGeometries = &bottomASGeometry;
bottomASInfo.ppGeometries = NULL;
bottomASInfo.scratchData.deviceAddress = 0;
uint32_t bottomMaxPrimitiveCounts = 1;
vk::GetAccelerationStructureBuildSizesKHR(*m_device, VK_ACCELERATION_STRUCTURE_BUILD_TYPE_DEVICE_KHR, &bottomASInfo, &bottomMaxPrimitiveCounts, &bottomASBuildSizesInfo);
vkt::Buffer bottomASBuffer(*m_device, bottomASBuildSizesInfo.accelerationStructureSize,
VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_STORAGE_BIT_KHR | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT,
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
&allocate_da_flag_info);
VkAccelerationStructureCreateInfoKHR asCreateInfo = { VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_CREATE_INFO_KHR };
asCreateInfo.createFlags = 0;
asCreateInfo.buffer = bottomASBuffer;
asCreateInfo.offset = 0;
asCreateInfo.size = bottomASBuildSizesInfo.accelerationStructureSize;
asCreateInfo.type = VK_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL_KHR;
asCreateInfo.deviceAddress = 0;
VkAccelerationStructureKHR bottomAS, topAS;
result = vk::CreateAccelerationStructureKHR(*m_device, &asCreateInfo, NULL, &bottomAS);
ASSERT_EQ(VK_SUCCESS, result);
vkt::Buffer instanceBuffer(*m_device, 2 * sizeof(VkAccelerationStructureInstanceKHR),
VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT, vkt::device_address);
VkDeviceAddress instanceAddress = instanceBuffer.Address();
{
VkAccelerationStructureInstanceKHR* instance = (VkAccelerationStructureInstanceKHR*)instanceBuffer.Memory().Map();
memset(instance, 0, 2 * sizeof(VkAccelerationStructureInstanceKHR));
instance[0].transform.matrix[0][0] = 1;
instance[0].transform.matrix[0][1] = 0;
instance[0].transform.matrix[0][2] = 0;
instance[0].transform.matrix[0][3] = 0;
instance[0].transform.matrix[1][0] = 0;
instance[0].transform.matrix[1][1] = 1;
instance[0].transform.matrix[1][2] = 0;
instance[0].transform.matrix[1][3] = 0;
instance[0].transform.matrix[2][0] = 0;
instance[0].transform.matrix[2][1] = 0;
instance[0].transform.matrix[2][2] = 1;
instance[0].transform.matrix[2][3] = 0;
instance[0].instanceCustomIndex = 0xdeadfe;
instance[0].mask = 0xff;
instance[0].instanceShaderBindingTableRecordOffset = 0;
instance[0].flags = 0;
VkAccelerationStructureDeviceAddressInfoKHR asDeviceAddressInfo = { VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_DEVICE_ADDRESS_INFO_KHR };
asDeviceAddressInfo.accelerationStructure = bottomAS;
instance[0].accelerationStructureReference = vk::GetAccelerationStructureDeviceAddressKHR(device(), &asDeviceAddressInfo);
}
VkAccelerationStructureGeometryKHR topASGeometry = { VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_GEOMETRY_KHR };
topASGeometry.geometryType = VK_GEOMETRY_TYPE_INSTANCES_KHR;
topASGeometry.geometry.instances = vku::InitStructHelper();
topASGeometry.geometry.instances.pNext = NULL;
topASGeometry.geometry.instances.arrayOfPointers = VK_FALSE;
topASGeometry.geometry.instances.data.deviceAddress = instanceAddress;
topASGeometry.flags = 0;
VkAccelerationStructureBuildGeometryInfoKHR topASInfo = { VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_BUILD_GEOMETRY_INFO_KHR };
topASInfo.type = VK_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL_KHR;
topASInfo.flags = 0;
topASInfo.mode = VK_BUILD_ACCELERATION_STRUCTURE_MODE_BUILD_KHR;
topASInfo.srcAccelerationStructure = VK_NULL_HANDLE;
topASInfo.dstAccelerationStructure = VK_NULL_HANDLE;
topASInfo.geometryCount = 1;
topASInfo.pGeometries = &topASGeometry;
topASInfo.ppGeometries = NULL;
topASInfo.scratchData.deviceAddress = 0;
uint32_t topMaxPrimitiveCounts = 1;
vk::GetAccelerationStructureBuildSizesKHR(device(), VK_ACCELERATION_STRUCTURE_BUILD_TYPE_DEVICE_KHR, &topASInfo, &topMaxPrimitiveCounts, &topASBuildSizesInfo);
vkt::Buffer topASBuffer(*m_device, topASBuildSizesInfo.accelerationStructureSize,
VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_STORAGE_BIT_KHR | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT,
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
&allocate_da_flag_info);
asCreateInfo.createFlags = 0;
asCreateInfo.buffer = topASBuffer;
asCreateInfo.offset = 0;
asCreateInfo.size = topASBuildSizesInfo.accelerationStructureSize;
asCreateInfo.type = VK_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL_KHR;
asCreateInfo.deviceAddress = 0;
result = vk::CreateAccelerationStructureKHR(device(), &asCreateInfo, NULL, &topAS);
ASSERT_EQ(VK_SUCCESS, result);
vkt::Buffer scratchBuffer(*m_device, std::max(bottomASBuildSizesInfo.buildScratchSize, topASBuildSizesInfo.buildScratchSize),
VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT,
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
&allocate_da_flag_info);
VkDeviceAddress scratchAddress = scratchBuffer.Address();
{
bottomASInfo.dstAccelerationStructure = bottomAS;
bottomASInfo.scratchData.deviceAddress = scratchAddress;
VkAccelerationStructureBuildRangeInfoKHR buildRangeInfo = {
1, 0, 0, 0,
};
const VkAccelerationStructureBuildRangeInfoKHR *pBuildRangeInfo = &buildRangeInfo;
// Build the bottom-level acceleration structure
m_command_buffer.Begin();
vk::CmdBuildAccelerationStructuresKHR(m_command_buffer, 1, &bottomASInfo, &pBuildRangeInfo);
VkMemoryBarrier memoryBarrier = { VK_STRUCTURE_TYPE_MEMORY_BARRIER, NULL,
VK_ACCESS_ACCELERATION_STRUCTURE_WRITE_BIT_KHR, VK_ACCESS_ACCELERATION_STRUCTURE_WRITE_BIT_KHR };
vk::CmdPipelineBarrier(m_command_buffer, VK_PIPELINE_STAGE_ACCELERATION_STRUCTURE_BUILD_BIT_KHR,
VK_PIPELINE_STAGE_ACCELERATION_STRUCTURE_BUILD_BIT_KHR, 0, 1, &memoryBarrier, 0, 0, 0, 0);
m_command_buffer.End();
m_default_queue->Submit(m_command_buffer);
m_device->Wait();
}
{
topASInfo.dstAccelerationStructure = topAS;
topASInfo.scratchData.deviceAddress = scratchAddress;
VkAccelerationStructureBuildRangeInfoKHR buildRangeInfo = {
1, 0, 0, 0,
};
const VkAccelerationStructureBuildRangeInfoKHR *pBuildRangeInfo = &buildRangeInfo;
// Build the top-level acceleration structure
m_command_buffer.Begin();
vk::CmdBuildAccelerationStructuresKHR(m_command_buffer, 1, &topASInfo, &pBuildRangeInfo);
VkMemoryBarrier memoryBarrier = { VK_STRUCTURE_TYPE_MEMORY_BARRIER, NULL,
VK_ACCESS_ACCELERATION_STRUCTURE_WRITE_BIT_KHR, VK_ACCESS_ACCELERATION_STRUCTURE_READ_BIT_KHR };
vk::CmdPipelineBarrier(m_command_buffer, VK_PIPELINE_STAGE_ACCELERATION_STRUCTURE_BUILD_BIT_KHR,
VK_PIPELINE_STAGE_RAY_TRACING_SHADER_BIT_KHR, 0, 1, &memoryBarrier, 0, 0, 0, 0);
m_command_buffer.End();
m_default_queue->Submit(m_command_buffer);
m_device->Wait();
}
vk::DestroyAccelerationStructureKHR(*m_device, topAS, NULL);
vk::DestroyAccelerationStructureKHR(*m_device, bottomAS, NULL);
vk::DestroyMicromapEXT(*m_device, micromap, NULL);
}
TEST_F(PositiveRayTracing, SerializeAccelerationStructure) {
TEST_DESCRIPTION("Build an acceleration structure, serialize then deserialize it");
SetTargetApiVersion(VK_API_VERSION_1_1);
AddRequiredFeature(vkt::Feature::accelerationStructure);
AddRequiredFeature(vkt::Feature::bufferDeviceAddress);
AddRequiredFeature(vkt::Feature::rayQuery);
RETURN_IF_SKIP(InitFrameworkForRayTracingTest());
RETURN_IF_SKIP(InitState());
if (IsPlatformMockICD()) {
GTEST_SKIP() << "Test not supported by MockICD: base buffer device addresses must be aligned";
}
auto blas = vkt::as::blueprint::BuildGeometryInfoSimpleOnDeviceBottomLevel(*m_device);
blas.AddFlags(VK_BUILD_ACCELERATION_STRUCTURE_ALLOW_UPDATE_BIT_KHR);
m_command_buffer.Begin();
blas.BuildCmdBuffer(m_command_buffer);
m_command_buffer.End();
m_default_queue->Submit(m_command_buffer);
m_device->Wait();
vkt::QueryPool serialization_query_pool(*m_device, VK_QUERY_TYPE_ACCELERATION_STRUCTURE_SERIALIZATION_SIZE_KHR, 1);
m_command_buffer.Begin();
vk::CmdResetQueryPool(m_command_buffer, serialization_query_pool, 0, 1);
vk::CmdWriteAccelerationStructuresPropertiesKHR(m_command_buffer, 1, &blas.GetDstAS()->handle(),
VK_QUERY_TYPE_ACCELERATION_STRUCTURE_SERIALIZATION_SIZE_KHR,
serialization_query_pool, 0);
m_command_buffer.End();
m_default_queue->Submit(m_command_buffer);
m_device->Wait();
uint32_t accel_struct_serialization_size = 0;
serialization_query_pool.Results(0, 1, sizeof(uint32_t), &accel_struct_serialization_size, 0);
vkt::Buffer serialized_accel_struct_buffer(
*m_device, accel_struct_serialization_size,
VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT,
vkt::device_address);
VkCopyAccelerationStructureToMemoryInfoKHR copy_accel_struct_to_memory_info = vku::InitStructHelper();
copy_accel_struct_to_memory_info.src = blas.GetDstAS()->handle();
copy_accel_struct_to_memory_info.dst.deviceAddress = serialized_accel_struct_buffer.Address();
copy_accel_struct_to_memory_info.mode = VK_COPY_ACCELERATION_STRUCTURE_MODE_SERIALIZE_KHR;
m_command_buffer.Begin();
vk::CmdCopyAccelerationStructureToMemoryKHR(m_command_buffer, ©_accel_struct_to_memory_info);
m_command_buffer.End();
m_default_queue->Submit(m_command_buffer);
m_device->Wait();
vkt::Buffer de_serialized_accel_struct_buffer(
*m_device, accel_struct_serialization_size,
VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT,
vkt::device_address);
auto deserialized_blas = vkt::as::blueprint::BuildGeometryInfoSimpleOnDeviceBottomLevel(*m_device);
deserialized_blas.AddFlags(
VK_BUILD_ACCELERATION_STRUCTURE_ALLOW_UPDATE_BIT_KHR /*| VK_BUILD_ACCELERATION_STRUCTURE_ALLOW_COMPACTION_BIT_KHR*/);
deserialized_blas.GetDstAS()->SetSize(accel_struct_serialization_size);
deserialized_blas.GetDstAS()->Create();
VkCopyMemoryToAccelerationStructureInfoKHR copy_memory_to_accel_struct_info = vku::InitStructHelper();
copy_memory_to_accel_struct_info.src.deviceAddress = serialized_accel_struct_buffer.Address();
copy_memory_to_accel_struct_info.dst = deserialized_blas.GetDstAS()->handle();
copy_memory_to_accel_struct_info.mode = VK_COPY_ACCELERATION_STRUCTURE_MODE_DESERIALIZE_KHR;
m_command_buffer.Begin();
vk::CmdCopyMemoryToAccelerationStructureKHR(m_command_buffer, ©_memory_to_accel_struct_info);
m_command_buffer.End();
m_default_queue->Submit(m_command_buffer);
m_device->Wait();
}
TEST_F(PositiveRayTracing, AccelerationStructuresAndScratchBuffersAddressSharing) {
TEST_DESCRIPTION(
"Make sure that buffers backing acceleration structures and scratch buffers backed by the same VkDeviceMemory share the "
"same base address");
SetTargetApiVersion(VK_API_VERSION_1_1);
AddRequiredFeature(vkt::Feature::bufferDeviceAddress);
AddRequiredFeature(vkt::Feature::accelerationStructure);
AddRequiredFeature(vkt::Feature::rayQuery);
RETURN_IF_SKIP(InitFrameworkForRayTracingTest());
RETURN_IF_SKIP(InitState());
if (IsPlatformMockICD()) {
GTEST_SKIP() << "Only need to test on a real driver";
}
constexpr size_t build_info_count = 3;
// All buffers used to back destination acceleration struct and scratch will be bound to this memory chunk
VkMemoryAllocateFlagsInfo alloc_flags = vku::InitStructHelper();
alloc_flags.flags = VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT;
VkMemoryAllocateInfo alloc_info = vku::InitStructHelper(&alloc_flags);
alloc_info.allocationSize = 1u << 17;
vkt::DeviceMemory buffer_memory(*m_device, alloc_info);
// Test overlapping destination acceleration structure and scratch buffer
{
VkBufferCreateInfo dst_blas_buffer_ci = vku::InitStructHelper();
dst_blas_buffer_ci.size = 4096;
dst_blas_buffer_ci.usage = VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR |
VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_STORAGE_BIT_KHR |
VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT;
VkBufferCreateInfo scratch_buffer_ci = vku::InitStructHelper();
scratch_buffer_ci.size = alloc_info.allocationSize;
scratch_buffer_ci.usage = VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR |
VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT;
std::vector<vkt::Buffer> dst_blas_buffers(build_info_count);
std::vector<std::shared_ptr<vkt::Buffer>> scratch_buffers(build_info_count);
std::vector<vkt::as::BuildGeometryInfoKHR> blas_vec;
VkDeviceAddress ref_address = 0;
for (size_t i = 0; i < build_info_count; ++i) {
dst_blas_buffers[i].InitNoMemory(*m_device, dst_blas_buffer_ci);
vk::BindBufferMemory(device(), dst_blas_buffers[i], buffer_memory, 0);
scratch_buffers[i] = std::make_shared<vkt::Buffer>();
scratch_buffers[i]->InitNoMemory(*m_device, scratch_buffer_ci);
vk::BindBufferMemory(device(), scratch_buffers[i]->handle(), buffer_memory, 0);
const VkDeviceAddress scratch_address = scratch_buffers[i]->Address();
if (ref_address == 0) {
ref_address = scratch_address;
} else if (scratch_address != ref_address) {
ADD_FAILURE() << "Scratch buffer does not have the expected base address";
}
auto blas = vkt::as::blueprint::BuildGeometryInfoSimpleOnDeviceBottomLevel(*m_device);
blas.GetDstAS()->SetDeviceBuffer(std::move(dst_blas_buffers[i]));
blas.GetDstAS()->SetSize(4096);
blas.SetScratchBuffer(std::move(scratch_buffers[i]));
blas.SetupBuild(true);
const VkDeviceAddress dst_as_address = blas.GetDstAS()->GetBuffer().Address();
if (dst_as_address != ref_address) {
ADD_FAILURE() << "Buffer backing destination acceleration structure does not have the expected base address";
}
blas_vec.emplace_back(std::move(blas));
}
}
}
TEST_F(PositiveRayTracing, ZeroPrimitiveCount) {
TEST_DESCRIPTION("https://gitlab.khronos.org/vulkan/vulkan/-/issues/4203");
SetTargetApiVersion(VK_API_VERSION_1_1);
AddRequiredFeature(vkt::Feature::accelerationStructure);
AddRequiredFeature(vkt::Feature::bufferDeviceAddress);
RETURN_IF_SKIP(InitFrameworkForRayTracingTest());
RETURN_IF_SKIP(InitState());
auto blas =
std::make_shared<vkt::as::BuildGeometryInfoKHR>(vkt::as::blueprint::BuildGeometryInfoSimpleOnDeviceBottomLevel(*m_device));
m_command_buffer.Begin();
blas->SetupBuild(true);
const VkAccelerationStructureGeometryKHR* pGeometries = &blas->GetGeometries()[0].GetVkObj();
VkAccelerationStructureBuildGeometryInfoKHR build_geometry_info = vku::InitStructHelper();
build_geometry_info.type = VK_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL_KHR;
build_geometry_info.dstAccelerationStructure = *blas->GetDstAS();
build_geometry_info.geometryCount = 1u;
build_geometry_info.ppGeometries = &pGeometries;
build_geometry_info.scratchData = blas->GetInfo().scratchData;
VkAccelerationStructureBuildRangeInfoKHR build_range_info;
build_range_info.primitiveCount = 0u;
build_range_info.primitiveOffset = 0u;
build_range_info.firstVertex = 0u;
build_range_info.transformOffset = 0u;
const VkAccelerationStructureBuildRangeInfoKHR* p_build_range_info = &build_range_info;
vk::CmdBuildAccelerationStructuresKHR(m_command_buffer, 1u, &build_geometry_info, &p_build_range_info);
m_command_buffer.End();
m_default_queue->Submit(m_command_buffer);
m_device->Wait();
}
TEST_F(PositiveRayTracing, ZeroPrimitiveCountIndirect) {
TEST_DESCRIPTION("https://gitlab.khronos.org/vulkan/vulkan/-/issues/4203");
SetTargetApiVersion(VK_API_VERSION_1_1);
AddRequiredFeature(vkt::Feature::accelerationStructure);
AddRequiredFeature(vkt::Feature::bufferDeviceAddress);
AddRequiredFeature(vkt::Feature::accelerationStructureIndirectBuild);
RETURN_IF_SKIP(InitFrameworkForRayTracingTest());
RETURN_IF_SKIP(InitState());
auto blas =
std::make_shared<vkt::as::BuildGeometryInfoKHR>(vkt::as::blueprint::BuildGeometryInfoSimpleOnDeviceBottomLevel(*m_device));
m_command_buffer.Begin();
blas->SetupBuild(true);
VkMemoryAllocateFlagsInfo alloc_flags = vku::InitStructHelper();
alloc_flags.flags = VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT;
vkt::Buffer indirect_buffer_(*m_device, sizeof(VkAccelerationStructureBuildRangeInfoKHR),
VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
kHostVisibleMemProps, &alloc_flags);
auto* ranges_info = static_cast<VkAccelerationStructureBuildRangeInfoKHR*>(indirect_buffer_.Memory().Map());
const VkAccelerationStructureGeometryKHR* pGeometries;
pGeometries = &blas->GetGeometries()[0].GetVkObj();
*ranges_info = blas->GetGeometries()[0].GetFullBuildRange();
VkAccelerationStructureBuildGeometryInfoKHR build_geometry_info = blas->GetInfo();
build_geometry_info.geometryCount = 1u;
build_geometry_info.ppGeometries = &pGeometries;
const VkDeviceAddress indirect_address = indirect_buffer_.Address();
uint32_t stride = 0u;
uint32_t max_primitive_count = 1u;
const uint32_t* p_max_primitive_counts = &max_primitive_count;
vk::CmdBuildAccelerationStructuresIndirectKHR(m_command_buffer, 1u, &build_geometry_info, &indirect_address, &stride,
&p_max_primitive_counts);
m_command_buffer.End();
m_default_queue->Submit(m_command_buffer);
m_device->Wait();
}
TEST_F(PositiveRayTracing, BuildIndirectWithoutIndexBuffer) {
SetTargetApiVersion(VK_API_VERSION_1_1);
AddRequiredFeature(vkt::Feature::bufferDeviceAddress);
AddRequiredFeature(vkt::Feature::accelerationStructure);
AddRequiredFeature(vkt::Feature::accelerationStructureIndirectBuild);
AddRequiredFeature(vkt::Feature::rayQuery);
RETURN_IF_SKIP(InitFrameworkForRayTracingTest());
RETURN_IF_SKIP(InitState());
vkt::as::GeometryKHR triangle_geometry;
triangle_geometry.SetType(vkt::as::GeometryKHR::Type::Triangle);
VkMemoryAllocateFlagsInfo alloc_flags = vku::InitStructHelper();
alloc_flags.flags = VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT;
const VkBufferUsageFlags buffer_usage = VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR |
VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_STORAGE_BIT_KHR |
VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT;
vkt::Buffer vertex_buffer(*m_device, 1024, buffer_usage, kHostVisibleMemProps, &alloc_flags);
vkt::Buffer transform_buffer(*m_device, sizeof(VkTransformMatrixKHR), buffer_usage, kHostVisibleMemProps, &alloc_flags);
triangle_geometry.SetPrimitiveCount(1);
constexpr std::array vertices = {10.0f, 10.0f, 0.0f, -10.0f, 10.0f, 0.0f, 0.0f, -10.0f, 0.0f};
auto vertex_buffer_ptr = static_cast<float*>(vertex_buffer.Memory().Map());
std::copy(vertices.begin(), vertices.end(), vertex_buffer_ptr);
VkTransformMatrixKHR transform_matrix = {{
{1.0f, 0.0f, 0.0f, 0.0f},
{0.0f, 1.0f, 0.0f, 0.0f},
{0.0f, 0.0f, 1.0f, 0.0f},
}};
auto transform_buffer_ptr = static_cast<VkTransformMatrixKHR*>(transform_buffer.Memory().Map());
std::memcpy(transform_buffer_ptr, &transform_matrix, sizeof(transform_matrix));
triangle_geometry.SetTrianglesDeviceVertexBuffer(std::move(vertex_buffer), uint32_t(vertices.size() / 3) - 1);
triangle_geometry.SetTrianglesIndexType(VK_INDEX_TYPE_UINT32);
triangle_geometry.SetTrianglesTransformBuffer(std::move(transform_buffer));
triangle_geometry.SetFlags(VK_GEOMETRY_OPAQUE_BIT_KHR);
vkt::as::BuildGeometryInfoKHR out_build_info(m_device);
out_build_info.SetType(VK_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL_KHR);
out_build_info.SetBuildType(VK_ACCELERATION_STRUCTURE_BUILD_TYPE_DEVICE_KHR);
out_build_info.SetMode(VK_BUILD_ACCELERATION_STRUCTURE_MODE_BUILD_KHR);
std::vector<vkt::as::GeometryKHR> geometries;
geometries.emplace_back(std::move(triangle_geometry));
out_build_info.SetGeometries(std::move(geometries));
out_build_info.SetBuildRanges(out_build_info.GetBuildRangeInfosFromGeometries());
auto null_as = std::make_shared<vkt::as::AccelerationStructureKHR>(m_device);
null_as->SetNull(true);
out_build_info.SetSrcAS(null_as);
auto dstAsSize = out_build_info.GetSizeInfo().accelerationStructureSize;
auto as = std::make_shared<vkt::as::AccelerationStructureKHR>(m_device);
as->SetSize(dstAsSize);
as->SetType(VK_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL_KHR);
as->SetDeviceBufferMemoryAllocateFlags(VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT);
as->SetDeviceBufferMemoryPropertyFlags(VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
as->SetBufferUsageFlags(VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_STORAGE_BIT_KHR | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT |
VK_BUFFER_USAGE_STORAGE_BUFFER_BIT);
as->SetDeviceBufferInitNoMem(false);
out_build_info.SetDstAS(as);
out_build_info.SetUpdateDstAccelStructSizeBeforeBuild(true);
out_build_info.SetInfoCount(1);
out_build_info.SetNullInfos(false);
out_build_info.SetNullBuildRangeInfos(false);
m_command_buffer.Begin();
out_build_info.BuildCmdBufferIndirect(m_command_buffer);
m_command_buffer.End();
}
TEST_F(PositiveRayTracing, MultipleGeometries) {
SetTargetApiVersion(VK_API_VERSION_1_1);
AddRequiredFeature(vkt::Feature::bufferDeviceAddress);
AddRequiredFeature(vkt::Feature::accelerationStructure);
AddRequiredFeature(vkt::Feature::rayQuery);
RETURN_IF_SKIP(InitFrameworkForRayTracingTest());
RETURN_IF_SKIP(InitState());
if (IsPlatformMockICD()) {
GTEST_SKIP() << "Test not supported by MockICD: sometimes falil to get valid buffer device address";
}
m_command_buffer.Begin();
auto blas = vkt::as::blueprint::BuildGeometryInfoSimpleOnDeviceBottomLevel(*m_device);
blas.AddFlags(VK_BUILD_ACCELERATION_STRUCTURE_ALLOW_UPDATE_BIT_KHR);
std::vector<vkt::as::GeometryKHR> geometries;
geometries.emplace_back(vkt::as::blueprint::GeometrySimpleOnDeviceIndexedTriangleInfo(*m_device, 1));
geometries.emplace_back(vkt::as::blueprint::GeometrySimpleOnDeviceIndexedTriangleInfo(*m_device, 2));
geometries[0].SetTrianglesIndexType(VK_INDEX_TYPE_NONE_KHR);
geometries[1].SetTrianglesIndexType(VK_INDEX_TYPE_NONE_KHR);
geometries[0].SetTrianglesMaxVertex(5);
geometries[1].SetTrianglesMaxVertex(5);
blas.SetGeometries(std::move(geometries));
auto build_range_infos = blas.GetBuildRangeInfosFromGeometries();
blas.SetBuildRanges(build_range_infos);
blas.SetupBuild(true);
std::vector<const VkAccelerationStructureGeometryKHR*> pGeometries;
pGeometries.resize(2);
std::vector<const VkAccelerationStructureBuildRangeInfoKHR*> pRange_infos(1);
VkAccelerationStructureBuildRangeInfoKHR range_infos[2];
range_infos[0].primitiveCount = 1u;
range_infos[0].primitiveOffset = 0u;
range_infos[0].firstVertex = 0u;
range_infos[0].transformOffset = 0u;
range_infos[1].primitiveCount = 1u;
range_infos[1].primitiveOffset = 0u;
range_infos[1].firstVertex = 0u;
range_infos[1].transformOffset = 0u;
pRange_infos[0] = range_infos;
for (size_t i = 0; i < 2; ++i) {
const auto& geometry = blas.GetGeometries()[i];
pGeometries[i] = &geometry.GetVkObj();
}
// Need bigger scratch buffer since there are more geometries to build
auto scratch_buffer = std::make_shared<vkt::Buffer>(
*m_device, 4 * 1024 * 1024, VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
vkt::device_address);
blas.SetScratchBuffer(scratch_buffer);
blas.GetInfo().scratchData.deviceAddress = scratch_buffer->Address();
VkAccelerationStructureBuildGeometryInfoKHR vk_info = vku::InitStructHelper();
vk_info.type = VK_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL_KHR;
vk_info.dstAccelerationStructure = *blas.GetDstAS();
vk_info.geometryCount = 2u;
vk_info.ppGeometries = pGeometries.data();
vk_info.scratchData = blas.GetInfo().scratchData;
// Build acceleration structure
const VkAccelerationStructureBuildGeometryInfoKHR* pInfos = &vk_info;
const VkAccelerationStructureBuildRangeInfoKHR* const* ppBuildRangeInfos = pRange_infos.data();
vk::CmdBuildAccelerationStructuresKHR(m_command_buffer, 1u, pInfos, ppBuildRangeInfos);
m_command_buffer.End();
}
|