directx-12direct3d12

IDXGISwapChain::GetCurrentBackBufferIndex returns wrong index


I am getting the error

D3D12 ERROR: ID3D12CommandQueue::ExecuteCommandLists: A command list, which writes to a swapchain back buffer, may only be executed when that back buffer is the back buffer that will be presented during the next call to Present*. Such a back buffer is also referred to as the "current back buffer"

However, I am using the index returned from IDXGISwapChain::GetCurrentBackBufferIndex? So what's going wrong?


Solution

  • I was calling IDXGISwapChain1::Present before calling ID3D12CommandQueue::ExecuteCommandLists:

    auto index = IDXGISwapChain::GetCurrentBackBufferIndex() // gets index
    IDXGISwapChain::Present() // increments backbuffer, makes index stale
    ID3D12CommandQueue::ExecuteCommandLists() // BOOM, commandList used stale index