Implement m_frameLatencyWaitableObject sync for reduced DX12 frame latency

This commit is contained in:
Stephen Saunders 2023-05-23 08:48:56 -04:00
parent 1800986ebe
commit 34332dbeeb

View file

@ -47,6 +47,7 @@ using nvrhi::RefCountPtr;
#define HR_RETURN(hr) if(FAILED(hr)) return false
idCVar r_graphicsAdapter( "r_graphicsAdapter", "", CVAR_RENDERER | CVAR_INIT | CVAR_ARCHIVE, "Substring in the name the DXGI graphics adapter to select a certain GPU" );
idCVar r_dx12FrameLatency( "r_dx12FrameLatency", "0", CVAR_INTEGER | CVAR_RENDERER | CVAR_INIT, "DX12 maximum frame latency using DXGI swap chain waitable object" );
class DeviceManager_DX12 : public DeviceManager
{
@ -58,6 +59,7 @@ class DeviceManager_DX12 : public DeviceManager
DXGI_SWAP_CHAIN_DESC1 m_SwapChainDesc{};
DXGI_SWAP_CHAIN_FULLSCREEN_DESC m_FullScreenDesc{};
RefCountPtr<IDXGIAdapter> m_DxgiAdapter;
HANDLE m_frameLatencyWaitableObject = nullptr;
bool m_TearingSupported = false;
std::vector<RefCountPtr<ID3D12Resource>> m_SwapChainBuffers;
@ -310,7 +312,8 @@ bool DeviceManager_DX12::CreateDeviceAndSwapChain()
m_SwapChainDesc.BufferUsage = m_DeviceParams.swapChainUsage;
m_SwapChainDesc.BufferCount = m_DeviceParams.swapChainBufferCount;
m_SwapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD;
m_SwapChainDesc.Flags = m_DeviceParams.allowModeSwitch ? DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH : 0;
m_SwapChainDesc.Flags = ( m_DeviceParams.allowModeSwitch ? DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH : 0 ) |
( r_dx12FrameLatency.GetInteger() > 0 ? DXGI_SWAP_CHAIN_FLAG_FRAME_LATENCY_WAITABLE_OBJECT : 0 );
// Special processing for sRGB swap chain formats.
// DXGI will not create a swap chain with an sRGB format, but its contents will be interpreted as sRGB.
@ -429,6 +432,14 @@ bool DeviceManager_DX12::CreateDeviceAndSwapChain()
hr = pSwapChain1->QueryInterface( IID_PPV_ARGS( &m_SwapChain ) );
HR_RETURN( hr );
if( r_dx12FrameLatency.GetInteger() > 0 )
{
hr = m_SwapChain->SetMaximumFrameLatency( r_dx12FrameLatency.GetInteger() );
HR_RETURN( hr );
m_frameLatencyWaitableObject = m_SwapChain->GetFrameLatencyWaitableObject();
}
nvrhi::d3d12::DeviceDesc deviceDesc;
deviceDesc.errorCB = &DefaultMessageCallback::GetInstance();
deviceDesc.pDevice = m_Device12;
@ -616,6 +627,14 @@ void DeviceManager_DX12::Present()
// SRS - Don't change m_DeviceParams.vsyncEnabled here, simply test for vsync mode 2 to set DXGI SyncInterval
m_SwapChain->Present( m_DeviceParams.vsyncEnabled == 2 ? 1 : 0, presentFlags );
if( m_frameLatencyWaitableObject )
{
OPTICK_CATEGORY("DX12_Sync1", Optick::Category::Wait);
// SRS - When m_frameLatencyWaitableObject active, sync first on earlier present
DWORD result = WaitForSingleObjectEx( m_frameLatencyWaitableObject, INFINITE, true );
}
if constexpr( NUM_FRAME_DATA > 2 )
{
OPTICK_CATEGORY( "DX12_Sync3", Optick::Category::Wait );