Vulkan-Guide icon indicating copy to clipboard operation
Vulkan-Guide copied to clipboard

Document how to do D3D12-style command buffer management in Vulkan

Open Tobski opened this issue 6 years ago • 4 comments

There's a particular usage pattern for dynamically recorded command buffers which DX12 applications make use of DX12's ability to free-associate command buffers with command allocators, which is not how things naturally work in Vulkan.

Whilst this might not be the ideal method for managing command buffers in all cases, it works, and is natural for anyone porting D3D12 content.

After discussion in the Vulkan WG, we agreed that the following advice is the right way to achieve the same in Vulkan, with no expected perf implications vs DX12:

  • Allocate command pools with VK_COMMAND_POOL_CREATE_TRANSIENT_BIT
  • Treat VkCommandPool objects the same as ID3D12CommandAllocator objects
    • I.e. they are externally synchronized command allocators
  • Don't pre-allocate command buffers
  • Allocate command buffers when you want to begin recording, then begin them
  • When the command buffer is no longer in use, free the command buffer
    • Ideally along with all others associated with the same pool

It would be useful to turn this porting advice into a section of the Vulkan-Guide, possibly alongside other porting advice.

Tobski avatar Jan 08 '20 16:01 Tobski

I would also note that there's interaction between command buffer management and semaphore management here - timeline semaphores kind of round out the whole package if you want to truly emulate the same style of command buffer management developers are used to in D3D12. These pieces can be written separately, but eventually should be part of the same guide section.

Tobski avatar Jan 08 '20 16:01 Tobski

One additional thing to mention which I kind of didn't call out explicitly, in the same way that in D3D12 you would need to call ID3D12CommandAllocator::Reset to reclaim memory, you should be calling vkResetCommandPool - ideally once per frame. If we're going to document this properly as command buffer management advice, we should be clear to include that. Not doing this can result in an eventual OOM condition.

See https://docs.microsoft.com/en-us/windows/win32/direct3d12/recording-command-lists-and-bundles for useful info on how d3d12 command lists are used.

Tobski avatar Jan 10 '20 10:01 Tobski

One other comment is that this doesn't represent the only way you might use command lists in D3D12 - it seems perfectly possible to use them in the same way as Vulkan. This is specifically addressing a common pattern in D3D12 applications where the command lists are created and submitted on a per-frame basis, but then they are individually reset to use a new command allocator - this indicates the best way to accomplish that.

Tobski avatar Jan 10 '20 11:01 Tobski

Might be worth a whole chapter on moving to Vulkan if you're coming form D3D12?

marty-johnson59 avatar Jan 25 '24 23:01 marty-johnson59