Skip to content

Commit

Permalink
Acceleration structures with Levels
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
AntarticCoder committed Jul 18, 2023
1 parent 89a92fb commit c6d97c2
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 15 deletions.
3 changes: 2 additions & 1 deletion MoltenVK/MoltenVK/Commands/MVKCmdAccelerationStructure.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class MVKCmdBuildAccelerationStructure : public MVKCommand {
protected:
MVKCommandTypePool<MVKCommand>* getTypePool(MVKCommandPool* cmdPool) override;

MVKDevice* _mvkDevice;
uint32_t _infoCount;
VkAccelerationStructureBuildGeometryInfoKHR* _geometryInfos;
VkAccelerationStructureBuildRangeInfoKHR const* _buildRangeInfos;
Expand All @@ -59,7 +60,7 @@ class MVKCmdCopyAccelerationStructure : public MVKCommand {
void encode(MVKCommandEncoder* cmdEncoder) override;
protected:
MVKCommandTypePool<MVKCommand>* getTypePool(MVKCommandPool* cmdPool) override;

id<MTLAccelerationStructure> _srcAccelerationStructure;
id<MTLAccelerationStructure> _dstAccelerationStructure;

Expand Down
118 changes: 105 additions & 13 deletions MoltenVK/MoltenVK/Commands/MVKCmdAccelerationStructure.mm
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
const VkAccelerationStructureBuildRangeInfoKHR* const* ppBuildRangeInfos) {
VkAccelerationStructureBuildGeometryInfoKHR geoInfo = *pInfos;

_mvkDevice = cmdBuff->getDevice();
_infoCount = infoCount;
_geometryInfos = &geoInfo;
_buildRangeInfos = *ppBuildRangeInfos;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -151,7 +244,6 @@
}

void MVKCmdCopyAccelerationStructureToMemory::encode(MVKCommandEncoder* cmdEncoder) {
id<MTLAccelerationStructureCommandEncoder> accStructEncoder = cmdEncoder->getMTLAccelerationStructureEncoder(kMVKCommandUseNone);
_mvkDevice = cmdEncoder->getDevice();

if(_copyMode != VK_COPY_ACCELERATION_STRUCTURE_MODE_SERIALIZE_KHR || !_dstBuffer->getDeviceMemory()->isMemoryHostAccessible()){
Expand Down
1 change: 0 additions & 1 deletion MoltenVK/MoltenVK/GPUObjects/MVKAccelerationStructure.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
vkGetAccelerationStructureBuildSizesKHR - DONE
vkGetAccelerationStructureDeviceAddressKHR - DONE
vkGetDeviceAccelerationStructureCompatibilityKHR - DONE
vkWriteAccelerationStructuresPropertiesKHR
*/

#pragma once
Expand Down

0 comments on commit c6d97c2

Please sign in to comment.