Should I use a fence for each Back buffer. Or just one that is shared for each backbuffer? DirectX 12

Started by
1 comment, last by MJP 2 years, 3 months ago

So I am attempting to dive into DirectX12 synchronization. And I am having a hard time trying to understand why some programs need multiple fences for GPU/CPU synchronization, instead of just one.

Here https://www.3dgep.com/learning-directx-12-1/, they have only a single fence and single fence value (that is stored per frame, so that the the value can be checked for a certain frame).

While here https://www.braynzarsoft.net/viewtutorial/q16390-03-initializing-directx-12, they have a a fence for each frame and an array of current values for each frame. (This guide doesn't explain its reasoning for have a fence for each frame)

What is the correct way to do this? (For simplicity just assume it is a single threaded program with 3 back buffers in the swap chain, but if you have the knowledge for a multithreaded program too, that additional information would be helpful). What are the pros and cons of each way if they are both valid approaches? (I assume both are valid, so what is the trade off?)

Also why is it that we use a single fence event instead of one for each frame as well? Like in the below:

if (fence->GetCompletedValue() < fenceValue) 
{
    ThrowIfFailed(fence->SetEventOnCompletion(fenceValue, fenceEvent));  
    ::WaitForSingleObject(fenceEvent, static_cast<DWORD>(duration.count()));
}

(I understand that the signals are appended to the end of command list in the command queues) So are fence events queued up the Windows OS? Is that why we can reuse it or is it because we will only be waiting on one event at a time ever, which is why we only need 1.

Thanks for the help in advanced. I am trying to get a strong understanding of the basics before i move on to more complex synchronization.

Advertisement

One fence is totally fine. You just need to increase the value of the fence for each frame. When you call SetEventOnCompletion it's saying “signal this event when the value of the fence is greater than or equal to the Value parameter”, so as long as the you keep increasing the fence value you can keep waiting on the same fence.

This topic is closed to new replies.

Advertisement