From a9e6592febd16aa89e124e0c7593be34b1f788d6 Mon Sep 17 00:00:00 2001 From: Magnus Norddahl Date: Mon, 2 Dec 2019 20:05:13 +0100 Subject: [PATCH] Switch to the D3DSWAPEFFECT_FLIPEX swap model --- .../polyrenderer/backend/poly_framebuffer.cpp | 8 +++--- .../polyrenderer/backend/poly_framebuffer.h | 2 ++ src/win32/win32polyvideo.cpp | 27 +++++++++---------- 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/src/rendering/polyrenderer/backend/poly_framebuffer.cpp b/src/rendering/polyrenderer/backend/poly_framebuffer.cpp index 023298806..8edca2e0e 100644 --- a/src/rendering/polyrenderer/backend/poly_framebuffer.cpp +++ b/src/rendering/polyrenderer/backend/poly_framebuffer.cpp @@ -49,12 +49,12 @@ extern FString gpuStatOutput; #ifdef WIN32 void I_PolyPresentInit(); -uint8_t *I_PolyPresentLock(int w, int h, int &pitch); +uint8_t *I_PolyPresentLock(int w, int h, bool vsync, int &pitch); void I_PolyPresentUnlock(int x, int y, int w, int h); void I_PolyPresentDeinit(); #else void I_PolyPresentInit() { } -uint8_t *I_PolyPresentLock(int w, int h, int &pitch) { pitch = 0; return nullptr; } +uint8_t *I_PolyPresentLock(int w, int h, bool vsync, int &pitch) { pitch = 0; return nullptr; } void I_PolyPresentUnlock(int x, int y, int w, int h) { } void I_PolyPresentDeinit() { } #endif @@ -157,7 +157,7 @@ void PolyFrameBuffer::Update() int pixelsize = 4; const uint8_t *src = (const uint8_t*)mCanvas->GetPixels(); int pitch = 0; - uint8_t *dst = I_PolyPresentLock(w, h, pitch); + uint8_t *dst = I_PolyPresentLock(w, h, cur_vsync, pitch); if (dst) { #if 1 @@ -444,7 +444,7 @@ uint32_t PolyFrameBuffer::GetCaps() void PolyFrameBuffer::SetVSync(bool vsync) { - // This is handled in PolySwapChain::AcquireImage. + cur_vsync = vsync; } void PolyFrameBuffer::CleanForRestart() diff --git a/src/rendering/polyrenderer/backend/poly_framebuffer.h b/src/rendering/polyrenderer/backend/poly_framebuffer.h index 08494a117..ba35b5a16 100644 --- a/src/rendering/polyrenderer/backend/poly_framebuffer.h +++ b/src/rendering/polyrenderer/backend/poly_framebuffer.h @@ -83,6 +83,8 @@ private: std::unique_ptr mDepthStencil; std::shared_ptr mDrawCommands; RenderMemory mFrameMemory; + + bool cur_vsync = false; }; inline PolyFrameBuffer *GetPolyFrameBuffer() { return static_cast(screen); } diff --git a/src/win32/win32polyvideo.cpp b/src/win32/win32polyvideo.cpp index a6e74218a..2c2111979 100644 --- a/src/win32/win32polyvideo.cpp +++ b/src/win32/win32polyvideo.cpp @@ -19,14 +19,14 @@ namespace int ClientHeight = 0; bool CurrentVSync = false; - IDirect3D9 *d3d9 = nullptr; - IDirect3DDevice9 *device = nullptr; - IDirect3DSurface9 *surface = nullptr; + IDirect3D9Ex *d3d9 = nullptr; + IDirect3DDevice9Ex *device = nullptr; + IDirect3DSurface9* surface = nullptr; } void I_PolyPresentInit() { - d3d9 = Direct3DCreate9(D3D_SDK_VERSION); + Direct3DCreate9Ex(D3D_SDK_VERSION, &d3d9); if (!d3d9) { I_FatalError("Direct3DCreate9 failed"); @@ -35,33 +35,32 @@ void I_PolyPresentInit() RECT rect = {}; GetClientRect(Window, &rect); - CurrentVSync = vid_vsync; ClientWidth = rect.right; ClientHeight = rect.bottom; D3DPRESENT_PARAMETERS pp = {}; pp.Windowed = true; - pp.SwapEffect = D3DSWAPEFFECT_DISCARD; + pp.SwapEffect = D3DSWAPEFFECT_FLIPEX; pp.BackBufferWidth = ClientWidth; pp.BackBufferHeight = ClientHeight; pp.BackBufferCount = 1; pp.hDeviceWindow = Window; - pp.PresentationInterval = CurrentVSync ? D3DPRESENT_INTERVAL_ONE : D3DPRESENT_INTERVAL_IMMEDIATE; + pp.PresentationInterval = CurrentVSync ? D3DPRESENT_INTERVAL_DEFAULT : D3DPRESENT_INTERVAL_IMMEDIATE; - HRESULT result = d3d9->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, Window, D3DCREATE_HARDWARE_VERTEXPROCESSING, &pp, &device); + HRESULT result = d3d9->CreateDeviceEx(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, Window, D3DCREATE_HARDWARE_VERTEXPROCESSING, &pp, nullptr, &device); if (FAILED(result)) { I_FatalError("IDirect3D9.CreateDevice failed"); } } -uint8_t *I_PolyPresentLock(int w, int h, int &pitch) +uint8_t *I_PolyPresentLock(int w, int h, bool vsync, int &pitch) { HRESULT result; RECT rect = {}; GetClientRect(Window, &rect); - if (rect.right != ClientWidth || rect.bottom != ClientHeight || CurrentVSync != vid_vsync) + if (rect.right != ClientWidth || rect.bottom != ClientHeight || CurrentVSync != vsync) { if (surface) { @@ -69,18 +68,18 @@ uint8_t *I_PolyPresentLock(int w, int h, int &pitch) surface = nullptr; } - CurrentVSync = vid_vsync; + CurrentVSync = vsync; ClientWidth = rect.right; ClientHeight = rect.bottom; D3DPRESENT_PARAMETERS pp = {}; pp.Windowed = true; - pp.SwapEffect = D3DSWAPEFFECT_DISCARD; + pp.SwapEffect = D3DSWAPEFFECT_FLIPEX; pp.BackBufferWidth = ClientWidth; pp.BackBufferHeight = ClientHeight; pp.BackBufferCount = 1; pp.hDeviceWindow = Window; - pp.PresentationInterval = CurrentVSync ? D3DPRESENT_INTERVAL_ONE : D3DPRESENT_INTERVAL_IMMEDIATE; + pp.PresentationInterval = CurrentVSync ? D3DPRESENT_INTERVAL_DEFAULT : D3DPRESENT_INTERVAL_IMMEDIATE; device->Reset(&pp); } @@ -173,7 +172,7 @@ void I_PolyPresentUnlock(int x, int y, int width, int height) result = device->EndScene(); if (SUCCEEDED(result)) - device->Present(nullptr, nullptr, 0, nullptr); + device->PresentEx(nullptr, nullptr, 0, nullptr, CurrentVSync ? 0 : D3DPRESENT_FORCEIMMEDIATE); } backbuffer->Release();