-
Notifications
You must be signed in to change notification settings - Fork 840
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Workaround for broken VSync in Simulate on macOS.
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
1 parent
3abf2ae
commit 2f0fb1e
Showing
9 changed files
with
195 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters