Skip to content

Commit

Permalink
Workaround for broken VSync in Simulate on macOS.
Browse files Browse the repository at this point in the history
OpenGL VSync became broken again in macOS 13 (glfw/glfw#2249). This workaround is similar to glfw/glfw#2277 but is implemented entirely outside of GLFW.

PiperOrigin-RevId: 516320722
Change-Id: Ib8a651a942f592c74e00ad3c7b22f2dfd156be5f
  • Loading branch information
saran-t authored and Copybara-Service committed Mar 13, 2023
1 parent 3abf2ae commit 2f0fb1e
Show file tree
Hide file tree
Showing 9 changed files with 195 additions and 0 deletions.
4 changes: 4 additions & 0 deletions doc/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ Python bindings
Simulate
^^^^^^^^

- Implemented a workaround for `broken VSync <https://github.com/glfw/glfw/issues/2249>`_ on macOS so that the frame
rate is correctly capped when the Vertical Sync toggle is enabled.

.. image:: images/changelog/contactlabel.png
:align: right
:width: 400px
Expand All @@ -42,6 +45,7 @@ Simulate

|br|


Version 2.3.2 (February 7, 2023)
--------------------------------

Expand Down
4 changes: 4 additions & 0 deletions simulate/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ target_sources(
PRIVATE glfw_adapter.cc glfw_dispatch.cc platform_ui_adapter.cc
)
target_compile_options(platform_ui_adapter PUBLIC ${MUJOCO_SIMULATE_COMPILE_OPTIONS})
if(APPLE)
target_sources(platform_ui_adapter PUBLIC glfw_corevideo.h PRIVATE glfw_corevideo.mm)
target_link_libraries(platform_ui_adapter PUBLIC "-framework CoreVideo")
endif()
target_include_directories(
platform_ui_adapter PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}
$<TARGET_PROPERTY:glfw,INTERFACE_INCLUDE_DIRECTORIES>
Expand Down
28 changes: 28 additions & 0 deletions simulate/glfw_adapter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
#include <mujoco/mujoco.h>
#include "glfw_dispatch.h"

#ifdef __APPLE__
#include "glfw_corevideo.h"
#endif

namespace mujoco {
namespace {
int MaybeGlfwInit() {
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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) {
Expand All @@ -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() {
Expand Down
11 changes: 11 additions & 0 deletions simulate/glfw_adapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
#include <mujoco/mujoco.h>
#include "platform_ui_adapter.h"

#ifdef __APPLE__
#include <optional>
#include "glfw_corevideo.h"
#endif

namespace mujoco {
class GlfwAdapter : public PlatformUIAdapter {
public:
Expand Down Expand Up @@ -61,6 +66,12 @@ class GlfwAdapter : public PlatformUIAdapter {
// store last window information when going to full screen
std::pair<int, int> window_pos_;
std::pair<int, int> window_size_;

#ifdef __APPLE__
// Workaround for perpertually broken OpenGL VSync on macOS,
// most recently https://github.com/glfw/glfw/issues/2249.
std::optional<GlfwCoreVideo> core_video_;
#endif
};
} // namespace mujoco

Expand Down
56 changes: 56 additions & 0 deletions simulate/glfw_corevideo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2023 DeepMind Technologies Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef MUJOCO_SIMULATE_GLFW_COREVIDEO_H_
#define MUJOCO_SIMULATE_GLFW_COREVIDEO_H_

#ifndef __APPLE__
#error "This header only works on macOS."
#endif

#include <condition_variable>
#include <mutex>

#include "glfw_dispatch.h"

#ifdef __OBJC__
#import <CoreVideo/CoreVideo.h>
#else
typedef void* CVDisplayLinkRef;
#endif

// Workaround for perpertually broken OpenGL VSync on macOS,
// most recently https://github.com/glfw/glfw/issues/2249.
namespace mujoco {
class GlfwCoreVideo {
public:
GlfwCoreVideo(GLFWwindow* window);
~GlfwCoreVideo();
void EnqueueSwap();
void WaitForSwap();
int DisplayLinkCallback();
void UpdateDisplayLink();

private:
GLFWwindow* window_;
CVDisplayLinkRef display_link_;

bool second_buffer_has_content_;
std::mutex mu_;
std::condition_variable cond_;
};
} // namespace mujoco


#endif // MUJOCO_SIMULATE_GLFW_COREVIDEO_H_
75 changes: 75 additions & 0 deletions simulate/glfw_corevideo.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright 2023 DeepMind Technologies Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "glfw_corevideo.h"

#include <condition_variable>
#include <mutex>

#include "glfw_adapter.h"
#include "glfw_dispatch.h"

// Workaround for perpertually broken OpenGL VSync on macOS,
// most recently https://github.com/glfw/glfw/issues/2249.
namespace mujoco {
namespace {
static int DisplayLinkCallbackTrampoline(CVDisplayLinkRef display_link,
const CVTimeStamp* now,
const CVTimeStamp* output_time,
CVOptionFlags flags_in,
CVOptionFlags* flags_out,
void* user_context) {
return static_cast<GlfwCoreVideo*>(user_context)->DisplayLinkCallback();
}
} // namespace

GlfwCoreVideo::GlfwCoreVideo(GLFWwindow* window) : window_(window) {
CVDisplayLinkCreateWithActiveCGDisplays(&display_link_);
CVDisplayLinkSetOutputCallback(display_link_, &DisplayLinkCallbackTrampoline, this);
CVDisplayLinkStart(display_link_);
}

GlfwCoreVideo::~GlfwCoreVideo() {
CVDisplayLinkStop(display_link_);
CVDisplayLinkRelease(display_link_);
}

void GlfwCoreVideo::EnqueueSwap() {
std::unique_lock lock(mu_);
second_buffer_has_content_ = true;
}

void GlfwCoreVideo::WaitForSwap() {
if (second_buffer_has_content_) {
std::unique_lock lock(mu_);
cond_.wait(lock, [this]() { return !this->second_buffer_has_content_; });
}
}

int GlfwCoreVideo::DisplayLinkCallback() {
if (second_buffer_has_content_) {
std::unique_lock lock(mu_);
Glfw().glfwSwapBuffers(window_);
second_buffer_has_content_ = false;
cond_.notify_one();
}
return kCVReturnSuccess;
}

void GlfwCoreVideo::UpdateDisplayLink() {
NSOpenGLContext* nsgl = Glfw().glfwGetNSGLContext(window_);
CVDisplayLinkSetCurrentCGDisplayFromOpenGLContext(display_link_, [nsgl CGLContextObj],
[nsgl.pixelFormat CGLPixelFormatObj]);
}
} // namespace mujoco
4 changes: 4 additions & 0 deletions simulate/glfw_dispatch.cc
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ const struct Glfw& Glfw(void* dlhandle) {
mjGLFW_INITIALIZE_SYMBOL(glfwWindowShouldClose);
// go/keep-sorted end

#ifdef __APPLE__
mjGLFW_INITIALIZE_SYMBOL(glfwGetNSGLContext);
#endif

#undef mjGLFW_INITIALIZE_SYMBOL

return glfw;
Expand Down
10 changes: 10 additions & 0 deletions simulate/glfw_dispatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@

#include <GLFW/glfw3.h>

#ifdef __APPLE__
#define GLFW_EXPOSE_NATIVE_NSGL
#include <GLFW/glfw3native.h>
#endif

namespace mujoco {
// Dynamic dispatch table for GLFW functions required by Simulate.
// This allows us to use GLFW without introducing a link-time dependency on the
Expand Down Expand Up @@ -58,6 +63,11 @@ struct Glfw {
mjGLFW_DECLARE_SYMBOL(glfwWindowHint);
mjGLFW_DECLARE_SYMBOL(glfwWindowShouldClose);
// go/keep-sorted end

#ifdef __APPLE__
mjGLFW_DECLARE_SYMBOL(glfwGetNSGLContext);
#endif

#undef mjGLFW_DECLARE_SYMBOL
};

Expand Down
3 changes: 3 additions & 0 deletions simulate/simulate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1950,6 +1950,9 @@ void Simulate::renderloop() {
uiModify(&this->ui0, &this->uistate, &this->platform_ui->mjr_context());
uiModify(&this->ui1, &this->uistate, &this->platform_ui->mjr_context());

// set VSync to initial value
this->platform_ui->SetVSync(this->vsync);

// run event loop
while (!this->platform_ui->ShouldCloseWindow() && !this->exitrequest.load()) {
{
Expand Down

0 comments on commit 2f0fb1e

Please sign in to comment.