diff --git a/DeviceResources.cpp b/DeviceResources.cpp index 218f386..141278d 100644 --- a/DeviceResources.cpp +++ b/DeviceResources.cpp @@ -55,6 +55,7 @@ DX::DeviceResources::DeviceResources(DXGI_FORMAT backBufferFormat, , m_dpi(-1.0f) , // Require DPI to be explicitly set. m_vTotalMode(false) + , m_tearingSupport(false) { UNREFERENCED_PARAMETER(minFeatureLevel); @@ -233,9 +234,7 @@ void DX::DeviceResources::CreateWindowSizeDependentResources() } else { - swapFlags = 0; // default is vsync on, not tearing -// swapFlags = DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING; // It is recommended to always use the tearing flag when it is available. -// swapChainDesc.Flags = m_tearingSupport ? DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING : 0; TODO make this not hard-coded -then older systems may work + swapFlags = m_tearingSupport ? DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING : 0; } // If the swap chain already exists, resize it. HRESULT hr = m_swapChain->ResizeBuffers(m_backBufferCount, backBufferWidth, backBufferHeight, m_backBufferFormat, swapFlags); @@ -294,9 +293,7 @@ void DX::DeviceResources::CreateWindowSizeDependentResources() } else { - swapFlags = 0; // default is vsync enabled, not tearing -// swapFlags = DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING; // It is recommended to always use the tearing flag when it is available. -// swapChainDesc.Flags = m_tearingSupport ? DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING : 0; TODO make this not hard-coded -then older systems may work + swapFlags = m_tearingSupport ? DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING : 0; } swapChainDesc.Flags = swapFlags; @@ -465,8 +462,13 @@ HRESULT DX::DeviceResources::Present(UINT syncInterval, UINT flags) // flag when it is supported, even when presenting in windowed mode. // However, this flag cannot be used if the app is in fullscreen mode as a // result of calling SetFullscreenState. - // UINT presentFlags = (m_tearingSupport && m_windowedMode) ? DXGI_PRESENT_ALLOW_TEARING : 0; - HRESULT hr = m_swapChain->Present(syncInterval, flags); + UINT presentFlags = flags; + if (m_tearingSupport && syncInterval == 0) + { + presentFlags |= DXGI_PRESENT_ALLOW_TEARING; + } + + HRESULT hr = m_swapChain->Present(syncInterval, presentFlags); if (m_d3dContext) { diff --git a/DeviceResources.h b/DeviceResources.h index 4343c31..1d369a9 100644 --- a/DeviceResources.h +++ b/DeviceResources.h @@ -50,6 +50,10 @@ namespace DX { m_vTotalMode = vTotalMode; } // Tell DevResources object when we are requesting v-total Fixed. + void SetTearingSupport(bool tearingSupport) + { + m_tearingSupport = tearingSupport; + } // The size of the render target, in pixels. RECT GetOutputSize() const @@ -185,6 +189,7 @@ namespace DX Microsoft::WRL::ComPtr m_d3dAnnotation; HANDLE m_frameLatencyHandle; BOOL m_vTotalMode; // True for a FIXED v-total average swapchain + bool m_tearingSupport; // Direct3D rendering objects. Required for 3D. Microsoft::WRL::ComPtr m_renderTarget; diff --git a/Game.cpp b/Game.cpp index 4be55fb..8aec5fc 100644 --- a/Game.cpp +++ b/Game.cpp @@ -214,6 +214,9 @@ void Game::RotateFrameLog() // Initialize the Direct3D resources required to run. void Game::Initialize(HWND window, int width, int height) { + CheckTearingSupport(); + m_deviceResources->SetTearingSupport(m_tearingSupport); + m_deviceResources->SetWindow(window, width, height); m_deviceResources->CreateDeviceResources(); m_deviceResources->SetDpi(96.0f); // TODO: using default 96 DPI for now