Skip to content

Commit

Permalink
Backport compositor vsync by @TerminalJack
Browse files Browse the repository at this point in the history
  • Loading branch information
Kleadron committed Oct 31, 2024
1 parent 918c6c4 commit 1fada50
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 2 deletions.
12 changes: 12 additions & 0 deletions core/bind/core_bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,15 @@ bool _OS::is_vsync_enabled() const {
return OS::get_singleton()->is_vsync_enabled();
}

void _OS::set_vsync_via_compositor(bool p_enable) {
OS::get_singleton()->set_vsync_via_compositor(p_enable);
}

bool _OS::is_vsync_via_compositor_enabled() const {

return OS::get_singleton()->is_vsync_via_compositor_enabled();
}

/*
enum Weekday {
DAY_SUNDAY,
Expand Down Expand Up @@ -1179,6 +1188,9 @@ void _OS::_bind_methods() {
ObjectTypeDB::bind_method(_MD("set_use_vsync", "enable"), &_OS::set_use_vsync);
ObjectTypeDB::bind_method(_MD("is_vsync_enabled"), &_OS::is_vsync_enabled);

ObjectTypeDB::bind_method(_MD("set_vsync_via_compositor", "enable"), &_OS::set_vsync_via_compositor);
ObjectTypeDB::bind_method(_MD("is_vsync_via_compositor_enabled"), &_OS::is_vsync_via_compositor_enabled);

ObjectTypeDB::bind_method(_MD("get_engine_version"), &_OS::get_engine_version);

BIND_CONSTANT(DAY_SUNDAY);
Expand Down
3 changes: 3 additions & 0 deletions core/bind/core_bind.h
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,9 @@ class _OS : public Object {
void set_use_vsync(bool p_enable);
bool is_vsync_enabled() const;

void set_vsync_via_compositor(bool p_enable);
bool is_vsync_via_compositor_enabled() const;

Dictionary get_engine_version() const;

static _OS *get_singleton() { return singleton; }
Expand Down
12 changes: 11 additions & 1 deletion core/os/os.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -518,11 +518,19 @@ String OS::get_joy_guid(int p_device) const {
void OS::set_context(int p_context) {
}
void OS::set_use_vsync(bool p_enable) {
_use_vsync = p_enable;
}

bool OS::is_vsync_enabled() const {
return _use_vsync;
}

return true;
void OS::set_vsync_via_compositor(bool p_enable) {
_vsync_via_compositor = p_enable;
}

bool OS::is_vsync_via_compositor_enabled() const {
return _vsync_via_compositor;
}

Dictionary OS::get_engine_version() const {
Expand Down Expand Up @@ -576,6 +584,8 @@ OS::OS() {
_time_scale = 1.0;
_pixel_snap = false;
_allow_hidpi = true;
_use_vsync = true;
_vsync_via_compositor = false;
Math::seed(1234567);
}

Expand Down
5 changes: 5 additions & 0 deletions core/os/os.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ class OS {
float _time_scale;
bool _pixel_snap;
bool _allow_hidpi;
bool _use_vsync;
bool _vsync_via_compositor;

char *last_error;

Expand Down Expand Up @@ -435,6 +437,9 @@ class OS {
virtual void set_use_vsync(bool p_enable);
virtual bool is_vsync_enabled() const;

virtual void set_vsync_via_compositor(bool p_enable);
virtual bool is_vsync_via_compositor_enabled() const;

Dictionary get_engine_version() const;

bool is_hidpi_allowed() const { return _allow_hidpi; }
Expand Down
3 changes: 3 additions & 0 deletions main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ static bool use_debug_profiler = false;
static bool force_lowdpi = false;
static int init_screen = -1;
static bool use_vsync = true;
static bool vsync_via_compositor = false;
static bool editor = false;

static OS::ProcessID allow_focus_steal_pid = 0;
Expand Down Expand Up @@ -725,6 +726,7 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph
GLOBAL_DEF("display/borderless_window", video_mode.borderless_window);
GLOBAL_DEF("display/always_on_top", video_mode.always_on_top);
use_vsync = GLOBAL_DEF("display/use_vsync", use_vsync);
vsync_via_compositor = GLOBAL_DEF("display/vsync_via_compositor", vsync_via_compositor);
GLOBAL_DEF("display/test_width", 0);
GLOBAL_DEF("display/test_height", 0);
OS::get_singleton()->_pixel_snap = GLOBAL_DEF("display/use_2d_pixel_snap", false);
Expand Down Expand Up @@ -880,6 +882,7 @@ Error Main::setup2(Thread::ID p_main_tid_override) {
}

OS::get_singleton()->set_use_vsync(use_vsync);
OS::get_singleton()->set_vsync_via_compositor(vsync_via_compositor);

register_core_singletons();

Expand Down
51 changes: 50 additions & 1 deletion platform/windows/context_gl_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@

#include "context_gl_win.h"

#include <dwmapi.h>

//#include "drivers/opengl/glwrapper.h"
//#include "ctxgl_procaddr.h"

Expand Down Expand Up @@ -74,9 +76,41 @@ int ContextGL_Win::get_window_height() {
return OS::get_singleton()->get_video_mode().height;
}

bool ContextGL_Win::should_vsync_via_compositor() {

if (OS::get_singleton()->is_window_fullscreen() || !OS::get_singleton()->is_vsync_via_compositor_enabled()) {
return false;
}

// Note: All Windows versions supported by Godot have a compositor.
// It can be disabled on earlier Windows versions.
BOOL dwm_enabled;

if (dwmFlushFunc && dwmIsCompositionEnabledFunc && SUCCEEDED(dwmIsCompositionEnabledFunc(&dwm_enabled))) {
return dwm_enabled;
}

return false;
}

void ContextGL_Win::swap_buffers() {

SwapBuffers(hDC);

if (use_vsync) {
bool vsync_via_compositor_now = should_vsync_via_compositor();

if (vsync_via_compositor_now) {
dwmFlushFunc();
}

if (vsync_via_compositor_now != vsync_via_compositor) {
// The previous frame had a different operating mode than this
// frame. Set the 'vsync_via_compositor' member variable and the
// OpenGL swap interval to their proper values.
set_use_vsync(true);
}
}
}

/*
Expand All @@ -95,9 +129,13 @@ static GLWrapperFuncPtr wrapper_get_proc_address(const char* p_function) {

void ContextGL_Win::set_use_vsync(bool p_use) {

vsync_via_compositor = p_use && should_vsync_via_compositor();

if (wglSwapIntervalEXT) {
wglSwapIntervalEXT(p_use ? 1 : 0);
int swap_interval = (p_use && !vsync_via_compositor) ? 1 : 0;
wglSwapIntervalEXT(swap_interval);
}

use_vsync = p_use;
}

Expand Down Expand Up @@ -196,6 +234,16 @@ Error ContextGL_Win::initialize() {
wglSwapIntervalEXT = (PFNWGLSWAPINTERVALEXTPROC)wglGetProcAddress("wglSwapIntervalEXT");
// glWrapperInit(wrapper_get_proc_address);

HMODULE dwmapi = LoadLibraryW(L"dwmapi.dll");
if (dwmapi) {
dwmFlushFunc = (DwmFlushProc)GetProcAddress(dwmapi, "DwmFlush");
dwmIsCompositionEnabledFunc = (DwmIsCompositionEnabledProc)GetProcAddress(dwmapi, "DwmIsCompositionEnabled");
}
else {
dwmFlushFunc = NULL;
dwmIsCompositionEnabledFunc = NULL;
}

return OK;
}

Expand All @@ -204,6 +252,7 @@ ContextGL_Win::ContextGL_Win(HWND hwnd, bool p_opengl_3_context) {
opengl_3_context = p_opengl_3_context;
hWnd = hwnd;
use_vsync = false;
vsync_via_compositor = false;
}

ContextGL_Win::~ContextGL_Win() {
Expand Down
7 changes: 7 additions & 0 deletions platform/windows/context_gl_win.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
#include <windows.h>

typedef bool(APIENTRY *PFNWGLSWAPINTERVALEXTPROC)(int interval);
typedef HRESULT(APIENTRY *DwmFlushProc)();
typedef HRESULT(APIENTRY *DwmIsCompositionEnabledProc)(BOOL *pfEnabled);

class ContextGL_Win : public ContextGL {

Expand All @@ -60,8 +62,13 @@ class ContextGL_Win : public ContextGL {
HWND hWnd;
bool opengl_3_context;
bool use_vsync;
bool vsync_via_compositor;

PFNWGLSWAPINTERVALEXTPROC wglSwapIntervalEXT;
DwmFlushProc dwmFlushFunc;
DwmIsCompositionEnabledProc dwmIsCompositionEnabledFunc;

bool should_vsync_via_compositor();

public:
virtual void release_current();
Expand Down
1 change: 1 addition & 0 deletions xp_changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- Added gizmo for concave collision shapes.
- RoomInstance node and Room resource have been named back to their original names. (from Room and RoomBounds)
- Fixed chibi tracker module player's panning so your left ear can also enjoy the music.
- Backported sync_via_compositor from Godot 3.


## Godot 2.1.7 RC interim version
Expand Down

0 comments on commit 1fada50

Please sign in to comment.