Workaround for broken VSync in Simulate on macOS.

OpenGL VSync became broken again in macOS 13 (https://github.com/glfw/glfw/issues/2249). This workaround is similar to https://github.com/glfw/glfw/pull/2277 but is implemented entirely outside of GLFW.

PiperOrigin-RevId: 516320722
Change-Id: Ib8a651a942f592c74e00ad3c7b22f2dfd156be5f
This commit is contained in:
Saran Tunyasuvunakool
2023-03-13 14:18:37 -07:00
committed by Copybara-Service
parent 3abf2ae2cd
commit 2f0fb1e4ef
9 changed files with 195 additions and 0 deletions
+28
View File
@@ -22,6 +22,10 @@
#include <mujoco/mujoco.h>
#include "glfw_dispatch.h"
#ifdef __APPLE__
#include "glfw_corevideo.h"
#endif
namespace mujoco {
namespace {
int MaybeGlfwInit() {
@@ -88,6 +92,12 @@ GlfwAdapter::GlfwAdapter() {
});
Glfw().glfwSetWindowRefreshCallback(
window_, +[](GLFWwindow* window) {
#ifdef __APPLE__
auto& core_video = GlfwAdapterFromWindow(window).core_video_;
if (core_video.has_value()) {
core_video->UpdateDisplayLink();
}
#endif
GlfwAdapterFromWindow(window).OnWindowRefresh();
});
Glfw().glfwSetWindowSizeCallback(
@@ -142,7 +152,16 @@ void GlfwAdapter::SetClipboardString(const char* text) {
}
void GlfwAdapter::SetVSync(bool enabled){
#ifdef __APPLE__
Glfw().glfwSwapInterval(0);
if (enabled && !core_video_.has_value()) {
core_video_.emplace(window_);
} else if (!enabled && core_video_.has_value()) {
core_video_.reset();
}
#else
Glfw().glfwSwapInterval(enabled);
#endif
}
void GlfwAdapter::SetWindowTitle(const char* title) {
@@ -154,7 +173,16 @@ bool GlfwAdapter::ShouldCloseWindow() const {
}
void GlfwAdapter::SwapBuffers() {
#ifdef __APPLE__
if (core_video_.has_value()) {
core_video_->EnqueueSwap();
core_video_->WaitForSwap();
} else {
Glfw().glfwSwapBuffers(window_);
}
#else
Glfw().glfwSwapBuffers(window_);
#endif
}
void GlfwAdapter::ToggleFullscreen() {