From 018fbc32233ea7d4b7e22430465077f08826bb95 Mon Sep 17 00:00:00 2001 From: Matija Kecman Date: Thu, 11 Jun 2026 06:11:37 -0700 Subject: [PATCH] Release GIL in native viewer. Adds py::gil_scoped_release no_gil; to mujoco::studio::Viewer methods to release the Python GIL during C++ execution. PiperOrigin-RevId: 930482271 Change-Id: I590794109418516ec5d9e8b536085560a1d36314 --- python/mujoco/experimental/studio/native_viewer.cc | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/python/mujoco/experimental/studio/native_viewer.cc b/python/mujoco/experimental/studio/native_viewer.cc index 3cc587d4..a0558bdb 100644 --- a/python/mujoco/experimental/studio/native_viewer.cc +++ b/python/mujoco/experimental/studio/native_viewer.cc @@ -72,6 +72,7 @@ class Viewer { public: Viewer(const std::string& title, int width, int height, std::string graphics_mode_str) { + py::gil_scoped_release no_gil; // Register resource providers for font and filament assets. mjpResourceProvider resource_provider; mjp_defaultResourceProvider(&resource_provider); @@ -113,16 +114,19 @@ class Viewer { } void InitRenderer(const mujoco::python::MjModelWrapper& model) { + py::gil_scoped_release no_gil; renderer_->Init(model.get()); } bool NewFrame() { + py::gil_scoped_release no_gil; const mujoco::platform::Window::Status status = window_->NewFrame(); return status == mujoco::platform::Window::Status::kRunning; } intptr_t UploadImage(intptr_t tex_id, const std::string img, int width, int height, int bpp) { + py::gil_scoped_release no_gil; return renderer_->UploadImage(tex_id, (const std::byte*)img.data(), width, height, bpp); } @@ -131,6 +135,7 @@ class Viewer { mujoco::python::MjDataWrapper& data, mujoco::python::MjvCameraWrapper& cam, int width, int height, int tex_id) { + py::gil_scoped_release no_gil; const int bytes_per_pixel = 3; std::vector bytes(width * height * bytes_per_pixel); renderer_->RenderToTexture(model.get(), data.get(), cam.get(), width, @@ -139,7 +144,9 @@ class Viewer { bytes_per_pixel); } - std::string GetDropFile() { return window_->GetDropFile(); } + std::string GetDropFile() { + return window_->GetDropFile(); + } void Present(const mujoco::python::MjModelWrapper& model, mujoco::python::MjDataWrapper& data, @@ -147,6 +154,8 @@ class Viewer { mujoco::python::MjvCameraWrapper& camera, mujoco::python::MjvOptionWrapper& vis_options, const std::vector& render_flags) { + py::gil_scoped_release no_gil; + const float width = window_->GetWidth(); const float height = window_->GetHeight(); const float scale = window_->GetScale();