Cleanup how different rendering backends are selected.
PiperOrigin-RevId: 816169098 Change-Id: I915aa45fa3a6d45a5178d3ff7bfdd0f1b1e01789
This commit is contained in:
committed by
Saran Tunyasuvunakool
parent
fd9b989bf8
commit
fbab761627
@@ -14,10 +14,6 @@
|
|||||||
|
|
||||||
#include "experimental/studio/app.h"
|
#include "experimental/studio/app.h"
|
||||||
|
|
||||||
#ifndef USE_FILAMENT_RENDERING
|
|
||||||
#define USE_FILAMENT_RENDERING 0
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
@@ -43,10 +39,12 @@
|
|||||||
#include "experimental/toolbox/renderer.h"
|
#include "experimental/toolbox/renderer.h"
|
||||||
#include "experimental/toolbox/window.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"
|
#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"
|
#include "third_party/dear_imgui/backends/imgui_impl_opengl2.h"
|
||||||
|
#else
|
||||||
|
#error No rendering mode defined.
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
namespace mujoco::studio {
|
namespace mujoco::studio {
|
||||||
@@ -58,6 +56,16 @@ namespace mujoco::studio {
|
|||||||
// - "passive" mode
|
// - "passive" mode
|
||||||
// - async physics
|
// - 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) {
|
static void ToggleWindow(bool& window) {
|
||||||
window = !window;
|
window = !window;
|
||||||
ImGui::GetIO().WantSaveIniSettings = true;
|
ImGui::GetIO().WantSaveIniSettings = true;
|
||||||
@@ -93,27 +101,25 @@ using toolbox::ToggleKind;
|
|||||||
App::App(int width, int height, std::string ini_path,
|
App::App(int width, int height, std::string ini_path,
|
||||||
const toolbox::LoadAssetFn& load_asset_fn)
|
const toolbox::LoadAssetFn& load_asset_fn)
|
||||||
: ini_path_(std::move(ini_path)), load_asset_fn_(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<toolbox::Window>("MuJoCo Studio", width, height,
|
window_ = std::make_unique<toolbox::Window>("MuJoCo Studio", width, height,
|
||||||
config, load_asset_fn);
|
kWindowConfig, load_asset_fn);
|
||||||
|
|
||||||
auto make_context_fn = [&](const mjModel* m, mjrContext* con) {
|
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;
|
mjrFilamentConfig render_config;
|
||||||
mjr_defaultFilamentConfig(&render_config);
|
mjr_defaultFilamentConfig(&render_config);
|
||||||
render_config.native_window = window_->GetNativeWindowHandle();
|
render_config.native_window = window_->GetNativeWindowHandle();
|
||||||
render_config.load_asset = &App::LoadAssetCallback;
|
render_config.load_asset = &App::LoadAssetCallback;
|
||||||
render_config.load_asset_user_data = this;
|
render_config.load_asset_user_data = this;
|
||||||
render_config.enable_gui = true;
|
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);
|
mjr_makeFilamentContext(m, con, &render_config);
|
||||||
#else
|
|
||||||
mjr_makeContext(m, con, mjFONTSCALE_150);
|
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
renderer_ = std::make_unique<toolbox::Renderer>(make_context_fn);
|
renderer_ = std::make_unique<toolbox::Renderer>(make_context_fn);
|
||||||
@@ -128,7 +134,7 @@ App::App(int width, int height, std::string ini_path,
|
|||||||
LoadSettings();
|
LoadSettings();
|
||||||
ClearProfilerData();
|
ClearProfilerData();
|
||||||
|
|
||||||
#if USE_FILAMENT_RENDERING == 0
|
#ifdef USE_CLASSIC_OPENGL
|
||||||
ImGui_ImplOpenGL2_Init();
|
ImGui_ImplOpenGL2_Init();
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
@@ -153,7 +159,7 @@ void App::OnModelLoaded(std::string_view model_file) {
|
|||||||
bool App::Update() {
|
bool App::Update() {
|
||||||
const toolbox::Window::Status status = window_->NewFrame();
|
const toolbox::Window::Status status = window_->NewFrame();
|
||||||
|
|
||||||
#if USE_FILAMENT_RENDERING == 0
|
#ifdef USE_CLASSIC_OPENGL
|
||||||
ImGui_ImplOpenGL2_NewFrame();
|
ImGui_ImplOpenGL2_NewFrame();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -189,7 +195,7 @@ void App::Render() {
|
|||||||
renderer_->Render(Model(), Data(), &perturb_, &camera_, &vis_options_,
|
renderer_->Render(Model(), Data(), &perturb_, &camera_, &vis_options_,
|
||||||
width * scale, height * scale);
|
width * scale, height * scale);
|
||||||
|
|
||||||
#if USE_FILAMENT_RENDERING == 0
|
#ifdef USE_CLASSIC_OPENGL
|
||||||
ImGui::Render();
|
ImGui::Render();
|
||||||
ImGui_ImplOpenGL2_RenderDrawData(ImGui::GetDrawData());
|
ImGui_ImplOpenGL2_RenderDrawData(ImGui::GetDrawData());
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -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_MAJOR_VERSION, 3);
|
||||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
|
||||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
|
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;
|
window_flags |= SDL_WINDOW_OPENGL;
|
||||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
|
||||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
|
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);
|
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_GLContext gl_context = SDL_GL_CreateContext(sdl_window_);
|
||||||
SDL_GL_MakeCurrent(sdl_window_, gl_context);
|
SDL_GL_MakeCurrent(sdl_window_, gl_context);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ class Window {
|
|||||||
public:
|
public:
|
||||||
// Configures the window for the specified rendering backend.
|
// Configures the window for the specified rendering backend.
|
||||||
enum Config {
|
enum Config {
|
||||||
kMujocoOpenGL,
|
kClassicOpenGL,
|
||||||
kFilamentVulkan,
|
kFilamentVulkan,
|
||||||
kFilamentOpenGL,
|
kFilamentOpenGL,
|
||||||
kFilamentWebGL,
|
kFilamentWebGL,
|
||||||
|
|||||||
Reference in New Issue
Block a user