Skip to content

Commit

Permalink
Merge pull request #68700 from clayjohn/GLES3-vsync
Browse files Browse the repository at this point in the history
Set vsync on window creation when using GLES3
  • Loading branch information
akien-mga committed Nov 15, 2022
2 parents 29e2aa4 + 58a9cfe commit 5993209
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 45 deletions.
3 changes: 2 additions & 1 deletion platform/linuxbsd/x11/display_server_x11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4414,7 +4414,7 @@ void DisplayServerX11::window_set_vsync_mode(DisplayServer::VSyncMode p_vsync_mo

#if defined(GLES3_ENABLED)
if (gl_manager) {
gl_manager->set_use_vsync(p_vsync_mode == DisplayServer::VSYNC_ENABLED);
gl_manager->set_use_vsync(p_vsync_mode != DisplayServer::VSYNC_DISABLED);
}
#endif
}
Expand Down Expand Up @@ -4675,6 +4675,7 @@ DisplayServerX11::WindowID DisplayServerX11::_create_window(WindowMode p_mode, V
if (gl_manager) {
Error err = gl_manager->window_create(id, wd.x11_window, x11_display, p_rect.size.width, p_rect.size.height);
ERR_FAIL_COND_V_MSG(err != OK, INVALID_WINDOW_ID, "Can't create an OpenGL window");
window_set_vsync_mode(p_vsync_mode, id);
}
#endif

Expand Down
3 changes: 2 additions & 1 deletion platform/macos/display_server_macos.mm
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@
Error err = gl_manager->window_create(window_id_counter, wd.window_view, p_rect.size.width, p_rect.size.height);
ERR_FAIL_COND_V_MSG(err != OK, INVALID_WINDOW_ID, "Can't create an OpenGL context");
}
window_set_vsync_mode(p_vsync_mode, window_id_counter);
#endif
[wd.window_view updateLayerDelegate];
id = window_id_counter++;
Expand Down Expand Up @@ -2999,7 +3000,7 @@
_THREAD_SAFE_METHOD_
#if defined(GLES3_ENABLED)
if (gl_manager) {
gl_manager->set_use_vsync(p_vsync_mode);
gl_manager->set_use_vsync(p_vsync_mode != DisplayServer::VSYNC_DISABLED);
}
#endif
#if defined(VULKAN_ENABLED)
Expand Down
14 changes: 14 additions & 0 deletions platform/windows/display_server_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2023,6 +2023,12 @@ void DisplayServerWindows::window_set_vsync_mode(DisplayServer::VSyncMode p_vsyn
context_vulkan->set_vsync_mode(p_window, p_vsync_mode);
}
#endif

#if defined(GLES3_ENABLED)
if (gl_manager) {
gl_manager->set_use_vsync(p_window, p_vsync_mode != DisplayServer::VSYNC_DISABLED);
}
#endif
}

DisplayServer::VSyncMode DisplayServerWindows::window_get_vsync_mode(WindowID p_window) const {
Expand All @@ -2032,6 +2038,13 @@ DisplayServer::VSyncMode DisplayServerWindows::window_get_vsync_mode(WindowID p_
return context_vulkan->get_vsync_mode(p_window);
}
#endif

#if defined(GLES3_ENABLED)
if (gl_manager) {
return gl_manager->is_using_vsync(p_window) ? DisplayServer::VSYNC_ENABLED : DisplayServer::VSYNC_DISABLED;
}
#endif

return DisplayServer::VSYNC_ENABLED;
}

Expand Down Expand Up @@ -3601,6 +3614,7 @@ DisplayServer::WindowID DisplayServerWindows::_create_window(WindowMode p_mode,
windows.erase(id);
ERR_FAIL_V_MSG(INVALID_WINDOW_ID, "Failed to create an OpenGL window.");
}
window_set_vsync_mode(p_vsync_mode, id);
}
#endif

Expand Down
59 changes: 21 additions & 38 deletions platform/windows/gl_manager_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ Error GLManager_Windows::_create_context(GLWindow &win, GLDisplay &gl_display) {
return ERR_CANT_CREATE;
}

if (!wglSwapIntervalEXT) {
wglSwapIntervalEXT = (PFNWGLSWAPINTERVALEXTPROC)wglGetProcAddress("wglSwapIntervalEXT");
}

return OK;
}

Expand Down Expand Up @@ -293,50 +297,30 @@ void GLManager_Windows::swap_buffers() {
}

Error GLManager_Windows::initialize() {
wglSwapIntervalEXT = (PFNWGLSWAPINTERVALEXTPROC)wglGetProcAddress("wglSwapIntervalEXT");
wglGetSwapIntervalEXT = (PFNWGLGETSWAPINTERVALEXTPROC)wglGetProcAddress("wglGetSwapIntervalEXT");
//glWrapperInit(wrapper_get_proc_address);

return OK;
}

void GLManager_Windows::set_use_vsync(bool p_use) {
/*
static bool setup = false;
static PFNGLXSWAPINTERVALEXTPROC glXSwapIntervalEXT = nullptr;
static PFNGLXSWAPINTERVALSGIPROC glXSwapIntervalMESA = nullptr;
static PFNGLXSWAPINTERVALSGIPROC glXSwapIntervalSGI = nullptr;
if (!setup) {
setup = true;
String extensions = glXQueryExtensionsString(x11_display, DefaultScreen(x11_display));
if (extensions.find("GLX_EXT_swap_control") != -1) {
glXSwapIntervalEXT = (PFNGLXSWAPINTERVALEXTPROC)glXGetProcAddressARB((const GLubyte *)"glXSwapIntervalEXT");
}
if (extensions.find("GLX_MESA_swap_control") != -1) {
glXSwapIntervalMESA = (PFNGLXSWAPINTERVALSGIPROC)glXGetProcAddressARB((const GLubyte *)"glXSwapIntervalMESA");
}
if (extensions.find("GLX_SGI_swap_control") != -1) {
glXSwapIntervalSGI = (PFNGLXSWAPINTERVALSGIPROC)glXGetProcAddressARB((const GLubyte *)"glXSwapIntervalSGI");
}
void GLManager_Windows::set_use_vsync(DisplayServer::WindowID p_window_id, bool p_use) {
GLWindow &win = get_window(p_window_id);
GLWindow *current = _current_window;

if (&win != _current_window) {
window_make_current(p_window_id);
}
int val = p_use ? 1 : 0;
if (glXSwapIntervalMESA) {
glXSwapIntervalMESA(val);
} else if (glXSwapIntervalSGI) {
glXSwapIntervalSGI(val);
} else if (glXSwapIntervalEXT) {
GLXDrawable drawable = glXGetCurrentDrawable();
glXSwapIntervalEXT(x11_display, drawable, val);
} else {
return;

if (wglSwapIntervalEXT) {
win.use_vsync = p_use;
wglSwapIntervalEXT(p_use ? 1 : 0);
}

if (current != _current_window) {
_current_window = current;
make_current();
}
use_vsync = p_use;
*/
}

bool GLManager_Windows::is_using_vsync() const {
return use_vsync;
bool GLManager_Windows::is_using_vsync(DisplayServer::WindowID p_window_id) const {
return get_window(p_window_id).use_vsync;
}

HDC GLManager_Windows::get_hdc(DisplayServer::WindowID p_window_id) {
Expand All @@ -354,7 +338,6 @@ GLManager_Windows::GLManager_Windows(ContextType p_context_type) {

direct_render = false;
glx_minor = glx_major = 0;
use_vsync = false;
_current_window = nullptr;
}

Expand Down
10 changes: 5 additions & 5 deletions platform/windows/gl_manager_windows.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class GLManager_Windows {
struct GLWindow {
int width = 0;
int height = 0;
bool use_vsync = false;

// windows specific
HDC hDC;
Expand All @@ -72,8 +73,8 @@ class GLManager_Windows {

GLWindow *_current_window = nullptr;

PFNWGLSWAPINTERVALEXTPROC wglSwapIntervalEXT;
PFNWGLGETSWAPINTERVALEXTPROC wglGetSwapIntervalEXT;
PFNWGLSWAPINTERVALEXTPROC wglSwapIntervalEXT = nullptr;
PFNWGLGETSWAPINTERVALEXTPROC wglGetSwapIntervalEXT = nullptr;

// funcs
void _internal_set_current_window(GLWindow *p_win);
Expand All @@ -86,7 +87,6 @@ class GLManager_Windows {

bool direct_render;
int glx_minor, glx_major;
bool use_vsync;
ContextType context_type;

private:
Expand All @@ -110,8 +110,8 @@ class GLManager_Windows {

Error initialize();

void set_use_vsync(bool p_use);
bool is_using_vsync() const;
void set_use_vsync(DisplayServer::WindowID p_window_id, bool p_use);
bool is_using_vsync(DisplayServer::WindowID p_window_id) const;

HDC get_hdc(DisplayServer::WindowID p_window_id);
HGLRC get_hglrc(DisplayServer::WindowID p_window_id);
Expand Down

0 comments on commit 5993209

Please sign in to comment.