From 4118538299c0d2dbcae6a712f5f89f182746ca0c Mon Sep 17 00:00:00 2001 From: Spodi Date: Sun, 9 Feb 2025 12:16:46 +0100 Subject: [PATCH 1/4] Allow switching V-Sync in OpenGL --- src/graphic/Fast3D/gfx_sdl2.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/graphic/Fast3D/gfx_sdl2.cpp b/src/graphic/Fast3D/gfx_sdl2.cpp index a9869c081..b03b2b4e8 100644 --- a/src/graphic/Fast3D/gfx_sdl2.cpp +++ b/src/graphic/Fast3D/gfx_sdl2.cpp @@ -662,6 +662,13 @@ static inline void sync_framerate_with_timer() { } static void gfx_sdl_swap_buffers_begin() { + if (vsync_enabled != + Ship::Context::GetInstance()->GetConsoleVariables()->GetInteger(CVAR_VSYNC_ENABLED, 1)) { + // Make sure only 0 or 1 is set. + vsync_enabled = + !!Ship::Context::GetInstance()->GetConsoleVariables()->GetInteger(CVAR_VSYNC_ENABLED, 1); + } + SDL_GL_SetSwapInterval(vsync_enabled); sync_framerate_with_timer(); SDL_GL_SwapWindow(wnd); } @@ -686,7 +693,7 @@ static const char* gfx_sdl_get_key_name(int scancode) { } bool gfx_sdl_can_disable_vsync() { - return false; + return true; } bool gfx_sdl_is_running() { From 40e7257e71a690693d24a58e41094d77829bd1b5 Mon Sep 17 00:00:00 2001 From: Spodi Date: Tue, 11 Feb 2025 23:37:21 +0100 Subject: [PATCH 2/4] V-sync Metal --- src/graphic/Fast3D/gfx_sdl2.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/graphic/Fast3D/gfx_sdl2.cpp b/src/graphic/Fast3D/gfx_sdl2.cpp index b03b2b4e8..26709b83c 100644 --- a/src/graphic/Fast3D/gfx_sdl2.cpp +++ b/src/graphic/Fast3D/gfx_sdl2.cpp @@ -662,13 +662,13 @@ static inline void sync_framerate_with_timer() { } static void gfx_sdl_swap_buffers_begin() { - if (vsync_enabled != - Ship::Context::GetInstance()->GetConsoleVariables()->GetInteger(CVAR_VSYNC_ENABLED, 1)) { + if (vsync_enabled != Ship::Context::GetInstance()->GetConsoleVariables()->GetInteger(CVAR_VSYNC_ENABLED, 1)) { // Make sure only 0 or 1 is set. - vsync_enabled = - !!Ship::Context::GetInstance()->GetConsoleVariables()->GetInteger(CVAR_VSYNC_ENABLED, 1); + vsync_enabled = !!Ship::Context::GetInstance()->GetConsoleVariables()->GetInteger(CVAR_VSYNC_ENABLED, 1); } SDL_GL_SetSwapInterval(vsync_enabled); + SDL_RenderSetVSync(renderer, vsync_enabled); + sync_framerate_with_timer(); SDL_GL_SwapWindow(wnd); } From 2b41d668caa98b2748934a7991c22707538b3a02 Mon Sep 17 00:00:00 2001 From: Spodi Date: Sun, 16 Feb 2025 21:33:10 +0100 Subject: [PATCH 3/4] Only call SetSwapInterval when cvar changes --- src/graphic/Fast3D/gfx_sdl2.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/graphic/Fast3D/gfx_sdl2.cpp b/src/graphic/Fast3D/gfx_sdl2.cpp index 26709b83c..97783ea4f 100644 --- a/src/graphic/Fast3D/gfx_sdl2.cpp +++ b/src/graphic/Fast3D/gfx_sdl2.cpp @@ -664,10 +664,11 @@ static inline void sync_framerate_with_timer() { static void gfx_sdl_swap_buffers_begin() { if (vsync_enabled != Ship::Context::GetInstance()->GetConsoleVariables()->GetInteger(CVAR_VSYNC_ENABLED, 1)) { // Make sure only 0 or 1 is set. - vsync_enabled = !!Ship::Context::GetInstance()->GetConsoleVariables()->GetInteger(CVAR_VSYNC_ENABLED, 1); + vsync_enabled = + (Ship::Context::GetInstance()->GetConsoleVariables()->GetInteger(CVAR_VSYNC_ENABLED, 1) ? 1 : 0); + SDL_GL_SetSwapInterval(vsync_enabled); + SDL_RenderSetVSync(renderer, vsync_enabled); } - SDL_GL_SetSwapInterval(vsync_enabled); - SDL_RenderSetVSync(renderer, vsync_enabled); sync_framerate_with_timer(); SDL_GL_SwapWindow(wnd); From 484b528ab5e2239686fb3893a73cbbe37ee1706f Mon Sep 17 00:00:00 2001 From: Spodi Date: Tue, 18 Feb 2025 23:51:50 +0100 Subject: [PATCH 4/4] Only read cvar once per frame --- src/graphic/Fast3D/gfx_dxgi.cpp | 8 ++------ src/graphic/Fast3D/gfx_sdl2.cpp | 8 ++++---- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/graphic/Fast3D/gfx_dxgi.cpp b/src/graphic/Fast3D/gfx_dxgi.cpp index f997df40c..29d50bd87 100644 --- a/src/graphic/Fast3D/gfx_dxgi.cpp +++ b/src/graphic/Fast3D/gfx_dxgi.cpp @@ -801,12 +801,8 @@ static bool gfx_dxgi_is_frame_ready() { // dxgi.length_in_vsync_frames is used as present interval. Present interval >1 (aka fractional V-Sync) // breaks VRR and introduces even more input lag than capping via normal V-Sync does. // Get the present interval the user wants instead (V-Sync toggle). - if (dxgi.is_vsync_enabled != - Ship::Context::GetInstance()->GetConsoleVariables()->GetInteger(CVAR_VSYNC_ENABLED, 1)) { - // Make sure only 0 or 1 is set, as present interval technically accepts a range from 0 to 4. - dxgi.is_vsync_enabled = - !!Ship::Context::GetInstance()->GetConsoleVariables()->GetInteger(CVAR_VSYNC_ENABLED, 1); - } + dxgi.is_vsync_enabled = + (Ship::Context::GetInstance()->GetConsoleVariables()->GetInteger(CVAR_VSYNC_ENABLED, 1) ? 1 : 0); dxgi.length_in_vsync_frames = dxgi.is_vsync_enabled; return true; } diff --git a/src/graphic/Fast3D/gfx_sdl2.cpp b/src/graphic/Fast3D/gfx_sdl2.cpp index 97783ea4f..59bb70c0c 100644 --- a/src/graphic/Fast3D/gfx_sdl2.cpp +++ b/src/graphic/Fast3D/gfx_sdl2.cpp @@ -662,10 +662,10 @@ static inline void sync_framerate_with_timer() { } static void gfx_sdl_swap_buffers_begin() { - if (vsync_enabled != Ship::Context::GetInstance()->GetConsoleVariables()->GetInteger(CVAR_VSYNC_ENABLED, 1)) { - // Make sure only 0 or 1 is set. - vsync_enabled = - (Ship::Context::GetInstance()->GetConsoleVariables()->GetInteger(CVAR_VSYNC_ENABLED, 1) ? 1 : 0); + // Make sure only 0 or 1 is set. + if (vsync_enabled = + !(Ship::Context::GetInstance()->GetConsoleVariables()->GetInteger(CVAR_VSYNC_ENABLED, 1) ? 1 : 0)) { + vsync_enabled = !vsync_enabled; SDL_GL_SetSwapInterval(vsync_enabled); SDL_RenderSetVSync(renderer, vsync_enabled); }