Skip to content

Commit

Permalink
Store scracth buffer for TLAS updating
Browse files Browse the repository at this point in the history
  • Loading branch information
rokuz committed Dec 27, 2024
1 parent c297911 commit d205380
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
39 changes: 27 additions & 12 deletions lvk/vulkan/VulkanClasses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,14 @@ VkSurfaceFormatKHR chooseSwapSurfaceFormat(const std::vector<VkSurfaceFormatKHR>
return formats[0];
}

VkDeviceSize bufferSize(lvk::VulkanContext& ctx, const lvk::Holder<lvk::BufferHandle>& handle) {
lvk::VulkanBuffer* buffer = ctx.buffersPool_.get(handle);
if (!buffer) {
return 0;
}
return buffer->bufferSize_;
}

} // namespace

namespace lvk {
Expand Down Expand Up @@ -2853,16 +2861,19 @@ void lvk::CommandBuffer::cmdUpdateTLAS(AccelStructHandle handle, BufferHandle in
&accelerationStructureBuildGeometryInfo,
&as->buildRangeInfo.primitiveCount,
&accelerationStructureBuildSizesInfo);

lvk::Holder<lvk::BufferHandle> scratchBuffer = ctx_->createBuffer(
lvk::BufferDesc{
.usage = lvk::BufferUsageBits_Storage,
.storage = lvk::StorageType_Device,
.size = accelerationStructureBuildSizesInfo.buildScratchSize,
.debugName = "scratchBuffer",
},
nullptr,
nullptr);

if (!as->scratchBuffer.valid() || bufferSize(*ctx_, as->scratchBuffer) < accelerationStructureBuildSizesInfo.buildScratchSize) {
LLOGW("Recreating scratch buffer for TLAS update");
as->scratchBuffer = ctx_->createBuffer(
lvk::BufferDesc{
.usage = lvk::BufferUsageBits_Storage,
.storage = lvk::StorageType_Device,
.size = accelerationStructureBuildSizesInfo.buildScratchSize,
.debugName = "scratchBuffer",
},
nullptr,
nullptr);
}

const VkAccelerationStructureBuildGeometryInfoKHR accelerationBuildGeometryInfo = {
.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_BUILD_GEOMETRY_INFO_KHR,
Expand All @@ -2873,7 +2884,7 @@ void lvk::CommandBuffer::cmdUpdateTLAS(AccelStructHandle handle, BufferHandle in
.dstAccelerationStructure = as->vkHandle,
.geometryCount = 1,
.pGeometries = &accelerationStructureGeometry,
.scratchData = {.deviceAddress = ctx_->gpuAddress(scratchBuffer)},
.scratchData = {.deviceAddress = ctx_->gpuAddress(as->scratchBuffer)},
};

const VkAccelerationStructureBuildRangeInfoKHR* accelerationBuildStructureRangeInfos[] = {&as->buildRangeInfo};
Expand Down Expand Up @@ -4359,7 +4370,7 @@ lvk::AccelStructHandle lvk::VulkanContext::createTLAS(const AccelStructDesc& des
},
nullptr,
outResult);

const VkAccelerationStructureBuildGeometryInfoKHR accelerationBuildGeometryInfo = {
.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_BUILD_GEOMETRY_INFO_KHR,
.type = VK_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL_KHR,
Expand All @@ -4370,6 +4381,10 @@ lvk::AccelStructHandle lvk::VulkanContext::createTLAS(const AccelStructDesc& des
.pGeometries = &accelerationStructureGeometry,
.scratchData = {.deviceAddress = gpuAddress(scratchBuffer)},
};
if (desc.buildFlags & lvk::AccelStructBuildFlagBits_AllowUpdate) {
// Store scracth buffer for future updates
accelStruct.scratchBuffer = std::move(scratchBuffer);
}

const VkAccelerationStructureBuildRangeInfoKHR* accelerationBuildStructureRangeInfos[] = {&accelStruct.buildRangeInfo};

Expand Down
1 change: 1 addition & 0 deletions lvk/vulkan/VulkanClasses.h
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ struct AccelerationStructure {
VkAccelerationStructureKHR vkHandle = VK_NULL_HANDLE;
uint64_t deviceAddress = 0;
lvk::Holder<lvk::BufferHandle> buffer;
lvk::Holder<lvk::BufferHandle> scratchBuffer; // Store only for TLAS
};

class CommandBuffer final : public ICommandBuffer {
Expand Down

0 comments on commit d205380

Please sign in to comment.