From c6d97c29ba378cda38fc82a73dab431e6542cf96 Mon Sep 17 00:00:00 2001 From: Antarctic Coder Date: Tue, 18 Jul 2023 19:58:37 -0400 Subject: [PATCH] Acceleration structures with Levels This commit adds the functionality for a bottom and top level acceleration structure, which are not quite finished, but I'm pushing this because I'm unable to stash this. --- .../Commands/MVKCmdAccelerationStructure.h | 3 +- .../Commands/MVKCmdAccelerationStructure.mm | 118 ++++++++++++++++-- .../GPUObjects/MVKAccelerationStructure.h | 1 - 3 files changed, 107 insertions(+), 15 deletions(-) diff --git a/MoltenVK/MoltenVK/Commands/MVKCmdAccelerationStructure.h b/MoltenVK/MoltenVK/Commands/MVKCmdAccelerationStructure.h index db943199f..488f5332b 100644 --- a/MoltenVK/MoltenVK/Commands/MVKCmdAccelerationStructure.h +++ b/MoltenVK/MoltenVK/Commands/MVKCmdAccelerationStructure.h @@ -40,6 +40,7 @@ class MVKCmdBuildAccelerationStructure : public MVKCommand { protected: MVKCommandTypePool* getTypePool(MVKCommandPool* cmdPool) override; + MVKDevice* _mvkDevice; uint32_t _infoCount; VkAccelerationStructureBuildGeometryInfoKHR* _geometryInfos; VkAccelerationStructureBuildRangeInfoKHR const* _buildRangeInfos; @@ -59,7 +60,7 @@ class MVKCmdCopyAccelerationStructure : public MVKCommand { void encode(MVKCommandEncoder* cmdEncoder) override; protected: MVKCommandTypePool* getTypePool(MVKCommandPool* cmdPool) override; - + id _srcAccelerationStructure; id _dstAccelerationStructure; diff --git a/MoltenVK/MoltenVK/Commands/MVKCmdAccelerationStructure.mm b/MoltenVK/MoltenVK/Commands/MVKCmdAccelerationStructure.mm index df9a4cd98..f3837e80e 100644 --- a/MoltenVK/MoltenVK/Commands/MVKCmdAccelerationStructure.mm +++ b/MoltenVK/MoltenVK/Commands/MVKCmdAccelerationStructure.mm @@ -32,6 +32,7 @@ const VkAccelerationStructureBuildRangeInfoKHR* const* ppBuildRangeInfos) { VkAccelerationStructureBuildGeometryInfoKHR geoInfo = *pInfos; + _mvkDevice = cmdBuff->getDevice(); _infoCount = infoCount; _geometryInfos = &geoInfo; _buildRangeInfos = *ppBuildRangeInfos; @@ -63,21 +64,113 @@ if(_geometryInfos[i].mode == VK_BUILD_ACCELERATION_STRUCTURE_MODE_BUILD_KHR) { - MTLAccelerationStructureDescriptor* accStructBuildDescriptor = [MTLAccelerationStructureDescriptor new]; + if(_geometryInfos[i].type == VK_ACCELERATION_STRUCTURE_TYPE_GENERIC_KHR) + { + MTLAccelerationStructureDescriptor* accStructBuildDescriptor = [MTLAccelerationStructureDescriptor new]; + + if(mvkIsAnyFlagEnabled(_geometryInfos[i].flags, VK_BUILD_ACCELERATION_STRUCTURE_ALLOW_UPDATE_BIT_KHR)){ + accStructBuildDescriptor.usage += MTLAccelerationStructureUsageRefit; + mvkDstAccelerationStructure->setAllowUpdate(true); + }else if(mvkIsAnyFlagEnabled(_geometryInfos[i].flags, VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_BUILD_BIT_KHR)){ + accStructBuildDescriptor.usage += MTLAccelerationStructureUsagePreferFastBuild; + }else{ + accStructBuildDescriptor.usage = MTLAccelerationStructureUsageNone; + } + + [accStructEncoder buildAccelerationStructure:dstAccelerationStructure + descriptor:accStructBuildDescriptor + scratchBuffer:scratchBuffer + scratchBufferOffset:scratchBufferOffset]; + } - if(mvkIsAnyFlagEnabled(_geometryInfos[i].flags, VK_BUILD_ACCELERATION_STRUCTURE_ALLOW_UPDATE_BIT_KHR)){ - accStructBuildDescriptor.usage += MTLAccelerationStructureUsageRefit; - mvkDstAccelerationStructure->setAllowUpdate(true); - }else if(mvkIsAnyFlagEnabled(_geometryInfos[i].flags, VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_BUILD_BIT_KHR)){ - accStructBuildDescriptor.usage += MTLAccelerationStructureUsagePreferFastBuild; - }else{ - accStructBuildDescriptor.usage = MTLAccelerationStructureUsageNone; + if(_geometryInfos[i].type == VK_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL_KHR) + { + if(_geometryInfos[i].pGeometries->geometryType == VK_GEOMETRY_TYPE_INSTANCES_KHR) { return; } + + if(_geometryInfos[i].pGeometries->geometryType == VK_GEOMETRY_TYPE_TRIANGLES_KHR) + { + MTLPrimitiveAccelerationStructureDescriptor* accStructTriangleBuildDescriptor = [MTLPrimitiveAccelerationStructureDescriptor new]; + + if(mvkIsAnyFlagEnabled(_geometryInfos[i].flags, VK_BUILD_ACCELERATION_STRUCTURE_ALLOW_UPDATE_BIT_KHR)){ + accStructTriangleBuildDescriptor.usage += MTLAccelerationStructureUsageRefit; + mvkDstAccelerationStructure->setAllowUpdate(true); + }else if(mvkIsAnyFlagEnabled(_geometryInfos[i].flags, VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_BUILD_BIT_KHR)){ + accStructTriangleBuildDescriptor.usage += MTLAccelerationStructureUsagePreferFastBuild; + }else{ + accStructTriangleBuildDescriptor.usage = MTLAccelerationStructureUsageNone; + } + + VkAccelerationStructureGeometryTrianglesDataKHR triangleGeometryData = _geometryInfos[i].pGeometries->geometry.triangles; + uint64_t vertexBDA = triangleGeometryData.vertexData.deviceAddress; + uint64_t indexBDA = triangleGeometryData.indexData.deviceAddress; + MVKBuffer* mvkVertexBuffer = _mvkDevice->getBufferAtAddress(vertexBDA); + MVKBuffer* mvkIndexBuffer = _mvkDevice->getBufferAtAddress(indexBDA); + + MTLAccelerationStructureTriangleGeometryDescriptor* geometryTriangles = [MTLAccelerationStructureTriangleGeometryDescriptor new]; + geometryTriangles.triangleCount = _geometryInfos[i].geometryCount; + geometryTriangles.vertexBuffer = mvkVertexBuffer->getMTLBuffer(); + geometryTriangles.vertexBufferOffset = _buildRangeInfos[i].primitiveOffset; + + geometryTriangles.indexBuffer = mvkIndexBuffer->getMTLBuffer(); + geometryTriangles.indexBufferOffset = 0; // Need to get this value + geometryTriangles.indexType = mvkMTLIndexTypeFromVkIndexType(triangleGeometryData.indexType); + accStructTriangleBuildDescriptor.geometryDescriptors = @[geometryTriangles]; + + [accStructEncoder buildAccelerationStructure:dstAccelerationStructure + descriptor:accStructTriangleBuildDescriptor + scratchBuffer:scratchBuffer + scratchBufferOffset:scratchBufferOffset]; + } + // Need to implement AABBS + if(_geometryInfos[i].pGeometries->geometryType == VK_GEOMETRY_TYPE_AABBS_KHR) + { + MTLPrimitiveAccelerationStructureDescriptor* accStructTriangleBuildDescriptor = [MTLPrimitiveAccelerationStructureDescriptor new]; + + if(mvkIsAnyFlagEnabled(_geometryInfos[i].flags, VK_BUILD_ACCELERATION_STRUCTURE_ALLOW_UPDATE_BIT_KHR)){ + accStructTriangleBuildDescriptor.usage += MTLAccelerationStructureUsageRefit; + mvkDstAccelerationStructure->setAllowUpdate(true); + }else if(mvkIsAnyFlagEnabled(_geometryInfos[i].flags, VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_BUILD_BIT_KHR)){ + accStructTriangleBuildDescriptor.usage += MTLAccelerationStructureUsagePreferFastBuild; + }else{ + accStructTriangleBuildDescriptor.usage = MTLAccelerationStructureUsageNone; + } + + VkAccelerationStructureGeometryTrianglesDataKHR triangleGeometryData = _geometryInfos[i].pGeometries->geometry.triangles; + uint64_t vertexBDA = triangleGeometryData.vertexData.deviceAddress; + uint64_t indexBDA = triangleGeometryData.indexData.deviceAddress; + MVKBuffer* mvkVertexBuffer = _mvkDevice->getBufferAtAddress(vertexBDA); + MVKBuffer* mvkIndexBuffer = _mvkDevice->getBufferAtAddress(indexBDA); + + MTLAccelerationStructureTriangleGeometryDescriptor* geometryTriangles = [MTLAccelerationStructureTriangleGeometryDescriptor new]; + geometryTriangles.triangleCount = _geometryInfos[i].geometryCount; + geometryTriangles.vertexBuffer = mvkVertexBuffer->getMTLBuffer(); + geometryTriangles.vertexBufferOffset = _buildRangeInfos[i].primitiveOffset; + + geometryTriangles.indexBuffer = mvkIndexBuffer->getMTLBuffer(); + geometryTriangles.indexBufferOffset = 0; // Need to get this value + geometryTriangles.indexType = mvkMTLIndexTypeFromVkIndexType(triangleGeometryData.indexType); + accStructTriangleBuildDescriptor.geometryDescriptors = @[geometryTriangles]; + + [accStructEncoder buildAccelerationStructure:dstAccelerationStructure + descriptor:accStructTriangleBuildDescriptor + scratchBuffer:scratchBuffer + scratchBufferOffset:scratchBufferOffset]; + } } - [accStructEncoder buildAccelerationStructure:dstAccelerationStructure - descriptor:accStructBuildDescriptor - scratchBuffer:scratchBuffer - scratchBufferOffset:scratchBufferOffset]; + if(_geometryInfos[i].type == VK_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL_KHR) + { + MTLInstanceAccelerationStructureDescriptor* accStructInstanceBuildDescriptor = [MTLInstanceAccelerationStructureDescriptor new]; + + if(mvkIsAnyFlagEnabled(_geometryInfos[i].flags, VK_BUILD_ACCELERATION_STRUCTURE_ALLOW_UPDATE_BIT_KHR)){ + accStructInstanceBuildDescriptor.usage += MTLAccelerationStructureUsageRefit; + mvkDstAccelerationStructure->setAllowUpdate(true); + }else if(mvkIsAnyFlagEnabled(_geometryInfos[i].flags, VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_BUILD_BIT_KHR)){ + accStructInstanceBuildDescriptor.usage += MTLAccelerationStructureUsagePreferFastBuild; + }else{ + accStructInstanceBuildDescriptor.usage = MTLAccelerationStructureUsageNone; + } + } } if(_geometryInfos[i].mode == VK_BUILD_ACCELERATION_STRUCTURE_MODE_UPDATE_KHR) @@ -151,7 +244,6 @@ } void MVKCmdCopyAccelerationStructureToMemory::encode(MVKCommandEncoder* cmdEncoder) { - id accStructEncoder = cmdEncoder->getMTLAccelerationStructureEncoder(kMVKCommandUseNone); _mvkDevice = cmdEncoder->getDevice(); if(_copyMode != VK_COPY_ACCELERATION_STRUCTURE_MODE_SERIALIZE_KHR || !_dstBuffer->getDeviceMemory()->isMemoryHostAccessible()){ diff --git a/MoltenVK/MoltenVK/GPUObjects/MVKAccelerationStructure.h b/MoltenVK/MoltenVK/GPUObjects/MVKAccelerationStructure.h index 5f3cdc41a..d2078c4b9 100644 --- a/MoltenVK/MoltenVK/GPUObjects/MVKAccelerationStructure.h +++ b/MoltenVK/MoltenVK/GPUObjects/MVKAccelerationStructure.h @@ -30,7 +30,6 @@ vkGetAccelerationStructureBuildSizesKHR - DONE vkGetAccelerationStructureDeviceAddressKHR - DONE vkGetDeviceAccelerationStructureCompatibilityKHR - DONE - vkWriteAccelerationStructuresPropertiesKHR */ #pragma once