From fbab761627574855dd3a26b7e00a0a8b9cfbfa0e Mon Sep 17 00:00:00 2001 From: Haroon Qureshi Date: Tue, 7 Oct 2025 13:55:41 +0100 Subject: [PATCH] Cleanup how different rendering backends are selected. PiperOrigin-RevId: 816169098 Change-Id: I915aa45fa3a6d45a5178d3ff7bfdd0f1b1e01789 --- src/experimental/studio/app.cc | 46 +++++++++++++++++------------- src/experimental/toolbox/window.cc | 4 +-- src/experimental/toolbox/window.h | 2 +- 3 files changed, 29 insertions(+), 23 deletions(-) diff --git a/src/experimental/studio/app.cc b/src/experimental/studio/app.cc index 2a00e300..af48aaaf 100644 --- a/src/experimental/studio/app.cc +++ b/src/experimental/studio/app.cc @@ -14,10 +14,6 @@ #include "experimental/studio/app.h" -#ifndef USE_FILAMENT_RENDERING -#define USE_FILAMENT_RENDERING 0 -#endif - #include #include #include @@ -43,10 +39,12 @@ #include "experimental/toolbox/renderer.h" #include "experimental/toolbox/window.h" -#if USE_FILAMENT_RENDERING +#if defined(USE_FILAMENT_OPENGL) || defined(USE_FILAMENT_VULKAN) #include "third_party/mujoco/google/filament/render_context_filament.h" -#else +#elif defined(USE_CLASSIC_OPENGL) #include "third_party/dear_imgui/backends/imgui_impl_opengl2.h" +#else +#error No rendering mode defined. #endif namespace mujoco::studio { @@ -58,6 +56,16 @@ namespace mujoco::studio { // - "passive" mode // - async physics +static constexpr toolbox::Window::Config kWindowConfig = +#if defined(USE_FILAMENT_VULKAN) + toolbox::Window::Config::kFilamentVulkan; +#elif defined(USE_FILAMENT_OPENGL) + toolbox::Window::Config::kFilamentOpenGL; +#elif defined(USE_CLASSIC_OPENGL) + toolbox::Window::Config::kClassicOpenGL; +#endif + + static void ToggleWindow(bool& window) { window = !window; ImGui::GetIO().WantSaveIniSettings = true; @@ -93,27 +101,25 @@ using toolbox::ToggleKind; App::App(int width, int height, std::string ini_path, const toolbox::LoadAssetFn& load_asset_fn) : ini_path_(std::move(ini_path)), load_asset_fn_(load_asset_fn) { -#if USE_FILAMENT_RENDERING - const toolbox::Window::Config config = - toolbox::Window::Config::kFilamentVulkan; -#else - const toolbox::Window::Config config = toolbox::Window::Config::kMujocoOpenGL; -#endif - window_ = std::make_unique("MuJoCo Studio", width, height, - config, load_asset_fn); + kWindowConfig, load_asset_fn); auto make_context_fn = [&](const mjModel* m, mjrContext* con) { -#if USE_FILAMENT_RENDERING +#if defined(USE_CLASSIC_OPENGL) + mjr_makeContext(m, con, mjFONTSCALE_150); +#else mjrFilamentConfig render_config; mjr_defaultFilamentConfig(&render_config); render_config.native_window = window_->GetNativeWindowHandle(); render_config.load_asset = &App::LoadAssetCallback; render_config.load_asset_user_data = this; render_config.enable_gui = true; +#if defined(USE_FILAMENT_OPENGL) + render_config.graphics_api = mjGFX_OPENGL; +#elif defined(USE_FILAMENT_VULKAN) + render_config.graphics_api = mjGFX_VULKAN; +#endif mjr_makeFilamentContext(m, con, &render_config); -#else - mjr_makeContext(m, con, mjFONTSCALE_150); #endif }; renderer_ = std::make_unique(make_context_fn); @@ -128,7 +134,7 @@ App::App(int width, int height, std::string ini_path, LoadSettings(); ClearProfilerData(); -#if USE_FILAMENT_RENDERING == 0 +#ifdef USE_CLASSIC_OPENGL ImGui_ImplOpenGL2_Init(); #endif } @@ -153,7 +159,7 @@ void App::OnModelLoaded(std::string_view model_file) { bool App::Update() { const toolbox::Window::Status status = window_->NewFrame(); -#if USE_FILAMENT_RENDERING == 0 +#ifdef USE_CLASSIC_OPENGL ImGui_ImplOpenGL2_NewFrame(); #endif @@ -189,7 +195,7 @@ void App::Render() { renderer_->Render(Model(), Data(), &perturb_, &camera_, &vis_options_, width * scale, height * scale); -#if USE_FILAMENT_RENDERING == 0 +#ifdef USE_CLASSIC_OPENGL ImGui::Render(); ImGui_ImplOpenGL2_RenderDrawData(ImGui::GetDrawData()); #endif diff --git a/src/experimental/toolbox/window.cc b/src/experimental/toolbox/window.cc index 67ea5568..1092999b 100644 --- a/src/experimental/toolbox/window.cc +++ b/src/experimental/toolbox/window.cc @@ -85,7 +85,7 @@ Window::Window(std::string_view title, int width, int height, Config config, SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES); - } else if (config == kMujocoOpenGL || config == kFilamentOpenGL) { + } else if (config == kClassicOpenGL || config == kFilamentOpenGL) { window_flags |= SDL_WINDOW_OPENGL; SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); @@ -102,7 +102,7 @@ Window::Window(std::string_view title, int width, int height, Config config, InitImGui(sdl_window_, load_asset_fn); - if (config == kFilamentWebGL || config == kMujocoOpenGL) { + if (config == kFilamentWebGL || config == kClassicOpenGL) { SDL_GLContext gl_context = SDL_GL_CreateContext(sdl_window_); SDL_GL_MakeCurrent(sdl_window_, gl_context); } diff --git a/src/experimental/toolbox/window.h b/src/experimental/toolbox/window.h index e13771e4..d4b8d730 100644 --- a/src/experimental/toolbox/window.h +++ b/src/experimental/toolbox/window.h @@ -31,7 +31,7 @@ class Window { public: // Configures the window for the specified rendering backend. enum Config { - kMujocoOpenGL, + kClassicOpenGL, kFilamentVulkan, kFilamentOpenGL, kFilamentWebGL,