Search code examples
vulkanraytracing

Vulkan, VK_KHR_ray_tracing: how do you rebuild a BLAS, as opposed to updating it?


I have an application (based on the vulkan-tutorial.com) in which I use the titular raytracing extension for vulkan. In it, an acceleration structure is created for some geometry. This geometry then changes (vertices are displaced dynamically, per frame), and thus I update the appropriate BLAS by calling vkCmdBuildAccelerationStructureKHR with VkAccelerationStructureBuildGeometryInfoKHR::update = VK_TRUE. This works fine (although the update ignores my changing the maxPrimitiveCount and similar parameters - It uses as many primitives as I specified during the first build; somewhat makes sense to me and is not part of my question).

I've researched a bit and came across some best practices here: https://developer.nvidia.com/blog/best-practices-using-nvidia-rtx-ray-tracing/ In there, they mention this: "Consider using only rebuilds [of the BLAS] with unpredictable deformations." This seems like something I want to try out, however, I can't find any sample code for rebuilding BLAS, and if I simply set update to VK_FALSE, I get massive amounts of validation layer errors and no image on screen. Specifically, I get a lot of "X was destroyed/freed but was still in use" where X is command buffers, VkBuffers, memory, fences, semaphores... My guess is the rebuild is trying to free the BLAS while it's still in use.

My question is therefore: How do you properly perform a "rebuild" of a BLAS, as mentioned in the above article?

I was considering using some std::shared_ptr to keep track of the BLAS being still in use by a given swapchain image but that seems excessively complicated and somewhat unclean, besides, I would need as many BLAS as I have swapchain images, multiplying required graphics memory by the swapchain size... that can't be practical in real life applications, right?


Solution

  • I cannot explain why, but I must've had an error in my code which resulted in the errors I described in my question.

    The correct way to rebuild instead of update an acceleration structure is indeed by setting the update parameter of VkAccelerationStructureBuildGeometryInfoKHR to VK_FALSE, that's all that needs to be done.