Frontends: Use DXGI_PRESENT_FLAG_ALLOW_TEARING for presentatation

This commit is contained in:
Connor McLaughlin
2020-01-18 18:12:09 +10:00
parent 159786c3f1
commit 46033898ba
6 changed files with 98 additions and 36 deletions

View File

@ -3,6 +3,7 @@
#include "common/d3d11/shader_compiler.h"
#include "common/log.h"
#include <array>
#include <dxgi1_5.h>
#include <imgui.h>
#include <imgui_impl_dx11.h>
Log_SetChannel(D3D11DisplayWindow);
@ -193,7 +194,7 @@ void D3D11DisplayWindow::onWindowResized(int width, int height)
bool D3D11DisplayWindow::createDeviceContext(QThread* worker_thread)
{
const bool debug = true;
const bool debug = false;
ComPtr<IDXGIFactory> dxgi_factory;
HRESULT hr = CreateDXGIFactory(IID_PPV_ARGS(dxgi_factory.GetAddressOf()));
@ -203,6 +204,18 @@ bool D3D11DisplayWindow::createDeviceContext(QThread* worker_thread)
return false;
}
m_allow_tearing_supported = false;
ComPtr<IDXGIFactory5> dxgi_factory5;
hr = dxgi_factory.As(&dxgi_factory5);
if (SUCCEEDED(hr))
{
BOOL allow_tearing_supported = false;
hr = dxgi_factory5->CheckFeatureSupport(DXGI_FEATURE_PRESENT_ALLOW_TEARING, &allow_tearing_supported,
sizeof(allow_tearing_supported));
if (SUCCEEDED(hr))
m_allow_tearing_supported = (allow_tearing_supported == TRUE);
}
UINT create_flags = 0;
if (debug)
create_flags |= D3D11_CREATE_DEVICE_DEBUG;
@ -227,11 +240,17 @@ bool D3D11DisplayWindow::createDeviceContext(QThread* worker_thread)
swap_chain_desc.Windowed = TRUE;
swap_chain_desc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD;
if (m_allow_tearing_supported)
swap_chain_desc.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING;
hr = dxgi_factory->CreateSwapChain(m_device.Get(), &swap_chain_desc, m_swap_chain.GetAddressOf());
if (FAILED(hr))
{
Log_WarningPrintf("Failed to create a flip-discard swap chain, trying discard.");
swap_chain_desc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
swap_chain_desc.Flags = 0;
m_allow_tearing_supported = false;
hr = dxgi_factory->CreateSwapChain(m_device.Get(), &swap_chain_desc, m_swap_chain.GetAddressOf());
if (FAILED(hr))
{
@ -240,15 +259,16 @@ bool D3D11DisplayWindow::createDeviceContext(QThread* worker_thread)
}
}
#if 1
ComPtr<ID3D11InfoQueue> info;
hr = m_device.As(&info);
if (SUCCEEDED(hr))
if (debug)
{
info->SetBreakOnSeverity(D3D11_MESSAGE_SEVERITY_ERROR, TRUE);
info->SetBreakOnSeverity(D3D11_MESSAGE_SEVERITY_WARNING, TRUE);
ComPtr<ID3D11InfoQueue> info;
hr = m_device.As(&info);
if (SUCCEEDED(hr))
{
info->SetBreakOnSeverity(D3D11_MESSAGE_SEVERITY_ERROR, TRUE);
info->SetBreakOnSeverity(D3D11_MESSAGE_SEVERITY_WARNING, TRUE);
}
}
#endif
if (!QtDisplayWindow::createDeviceContext(worker_thread))
{
@ -422,7 +442,10 @@ void D3D11DisplayWindow::Render()
ImGui::Render();
ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());
m_swap_chain->Present(BoolToUInt32(m_vsync), 0);
if (!m_vsync && m_allow_tearing_supported)
m_swap_chain->Present(0, DXGI_PRESENT_ALLOW_TEARING);
else
m_swap_chain->Present(BoolToUInt32(m_vsync), 0);
ImGui::NewFrame();
ImGui_ImplDX11_NewFrame();