Merge renderer+offscreen mode into unified graphics mode.
Allow Studio app to switch between graphics modes dynamically. PiperOrigin-RevId: 878396966 Change-Id: I666fe3d2452de5404b9be2bd1d017eabec789aef
This commit is contained in:
committed by
Copybara-Service
parent
8ab791fdf7
commit
73f73d94f7
@@ -25,6 +25,7 @@
|
||||
#include <filesystem>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <span>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
@@ -35,6 +36,7 @@
|
||||
#include <implot.h>
|
||||
#include <mujoco/mujoco.h>
|
||||
#include "experimental/platform/file_dialog.h"
|
||||
#include "experimental/platform/graphics_mode.h"
|
||||
#include "experimental/platform/gui.h"
|
||||
#include "experimental/platform/gui_spec.h"
|
||||
#include "experimental/platform/helpers.h"
|
||||
@@ -111,14 +113,9 @@ static constexpr std::array<const char*, 31> kPercentRealTime = {
|
||||
};
|
||||
// clang-format on
|
||||
|
||||
App::App(Config config) : ini_path_(std::move(config.ini_path)) {
|
||||
platform::Window::Config window_config;
|
||||
window_config.renderer_backend = platform::Renderer::GetBackend();
|
||||
window_config.offscreen_mode = config.offscreen_mode;
|
||||
window_ = std::make_unique<platform::Window>("MuJoCo Studio", config.width,
|
||||
config.height, window_config);
|
||||
renderer_ =
|
||||
std::make_unique<platform::Renderer>(window_->GetNativeWindowHandle());
|
||||
App::App(Config config)
|
||||
: ini_path_(std::move(config.ini_path)), gfx_mode_(config.gfx_mode) {
|
||||
SwitchGraphicsMode(config.width, config.height, config.gfx_mode);
|
||||
|
||||
ImPlot::CreateContext();
|
||||
mjv_defaultPerturb(&perturb_);
|
||||
@@ -128,6 +125,20 @@ App::App(Config config) : ini_path_(std::move(config.ini_path)) {
|
||||
profiler_.Clear();
|
||||
}
|
||||
|
||||
void App::SwitchGraphicsMode(int width, int height,
|
||||
platform::GraphicsMode mode) {
|
||||
renderer_.reset();
|
||||
window_.reset();
|
||||
gfx_mode_ = mode;
|
||||
|
||||
platform::Window::Config window_config;
|
||||
window_config.gfx_mode = gfx_mode_;
|
||||
window_ = std::make_unique<platform::Window>("MuJoCo Studio", width, height,
|
||||
window_config);
|
||||
renderer_ = std::make_unique<platform::Renderer>(
|
||||
window_->GetNativeWindowHandle(), gfx_mode_);
|
||||
}
|
||||
|
||||
void App::ClearModel() {
|
||||
model_holder_.reset();
|
||||
window_->SetTitle("MuJoCo Studio");
|
||||
@@ -361,8 +372,7 @@ void App::Render() {
|
||||
const float width = window_->GetWidth();
|
||||
const float height = window_->GetHeight();
|
||||
const float scale = window_->GetScale();
|
||||
|
||||
if (window_->IsOffscreenMode()) {
|
||||
if (IsHeadless(window_->GetGraphicsMode())) {
|
||||
pixels_.resize(width * height * 3);
|
||||
} else {
|
||||
pixels_.clear();
|
||||
@@ -615,14 +625,14 @@ void App::HandleKeyboardEvents() {
|
||||
return;
|
||||
}
|
||||
|
||||
constexpr auto ImGuiMode_CtrlShift = ImGuiMod_Ctrl | ImGuiMod_Shift;
|
||||
constexpr auto ImGuiMod_CtrlShift = ImGuiMod_Ctrl | ImGuiMod_Shift;
|
||||
|
||||
bool is_freecam_wasd = ui_.camera_idx == platform::kFreeCameraIdx;
|
||||
|
||||
// Menu shortcuts.
|
||||
if (ImGui_IsChordJustPressed(ImGuiKey_O | ImGuiMod_Ctrl)) {
|
||||
tmp_.file_dialog = UiTempState::FileDialog_Load;
|
||||
} else if (ImGui_IsChordJustPressed(ImGuiKey_S | ImGuiMode_CtrlShift)) {
|
||||
} else if (ImGui_IsChordJustPressed(ImGuiKey_S | ImGuiMod_CtrlShift)) {
|
||||
tmp_.file_dialog = UiTempState::FileDialog_SaveMjb;
|
||||
} else if (ImGui_IsChordJustPressed(ImGuiKey_S | ImGuiMod_Ctrl)) {
|
||||
tmp_.file_dialog = UiTempState::FileDialog_SaveXml;
|
||||
@@ -1898,6 +1908,51 @@ void App::MainMenuGui() {
|
||||
if (ImGui::MenuItem("Picture-in-Picture")) {
|
||||
tmp_.picture_in_picture = !tmp_.picture_in_picture;
|
||||
}
|
||||
ImGui::Separator();
|
||||
|
||||
|
||||
if (ImGui::BeginMenu("Graphics Mode (Experimental)")) {
|
||||
std::optional<platform::GraphicsMode> mode;
|
||||
if (ImGui::MenuItem(
|
||||
"Classic OpenGL", nullptr,
|
||||
gfx_mode_ == platform::GraphicsMode::ClassicOpenGl)) {
|
||||
mode = platform::GraphicsMode::ClassicOpenGl;
|
||||
}
|
||||
if (ImGui::MenuItem(
|
||||
"Classic OpenGL Headless", nullptr,
|
||||
gfx_mode_ == platform::GraphicsMode::ClassicOpenGlHeadless)) {
|
||||
mode = platform::GraphicsMode::ClassicOpenGlHeadless;
|
||||
}
|
||||
if (ImGui::MenuItem(
|
||||
"Filament OpenGL", nullptr,
|
||||
gfx_mode_ == platform::GraphicsMode::FilamentOpenGl)) {
|
||||
mode = platform::GraphicsMode::FilamentOpenGl;
|
||||
}
|
||||
if (ImGui::MenuItem(
|
||||
"Filament OpenGL Headless", nullptr,
|
||||
gfx_mode_ == platform::GraphicsMode::FilamentOpenGlHeadless)) {
|
||||
mode = platform::GraphicsMode::FilamentOpenGlHeadless;
|
||||
}
|
||||
if (ImGui::MenuItem(
|
||||
"Filament Vulkan", nullptr,
|
||||
gfx_mode_ == platform::GraphicsMode::FilamentVulkan)) {
|
||||
mode = platform::GraphicsMode::FilamentVulkan;
|
||||
}
|
||||
if (mode.has_value()) {
|
||||
spec_op_ = [=, this]() {
|
||||
const int width = window_->GetWidth();
|
||||
const int height = window_->GetHeight();
|
||||
SwitchGraphicsMode(width, height, *mode);
|
||||
// TODO: figure out why ImGui doesn't work unless we do this twice.
|
||||
if (IsClassic(*mode)) {
|
||||
SwitchGraphicsMode(width, height, *mode);
|
||||
}
|
||||
renderer_->Init(model());
|
||||
};
|
||||
}
|
||||
ImGui::EndMenu();
|
||||
}
|
||||
|
||||
ImGui::EndMenu();
|
||||
}
|
||||
|
||||
|
||||
@@ -26,6 +26,7 @@
|
||||
#include <vector>
|
||||
|
||||
#include <mujoco/mujoco.h>
|
||||
#include "experimental/platform/graphics_mode.h"
|
||||
#include "experimental/platform/gui.h"
|
||||
#include "experimental/platform/gui_spec.h"
|
||||
#include "experimental/platform/interaction.h"
|
||||
@@ -51,10 +52,8 @@ class App {
|
||||
// The path to the ini file containing the user settings.
|
||||
std::string ini_path;
|
||||
|
||||
// By default, we render directly to the window surface. However, in some
|
||||
// cases, we may want to render to an (offscreen) texture and blit the
|
||||
// texture to the window surface.
|
||||
bool offscreen_mode = false;
|
||||
// The graphics configuration used for initializing the window.
|
||||
platform::GraphicsMode gfx_mode = platform::GraphicsMode::FilamentVulkan;
|
||||
};
|
||||
|
||||
explicit App(Config config);
|
||||
@@ -190,6 +189,8 @@ class App {
|
||||
// then compile the spec to a model.
|
||||
void OnModelLoaded(std::string filename, ModelKind model_kind);
|
||||
|
||||
void SwitchGraphicsMode(int width, int height, platform::GraphicsMode mode);
|
||||
|
||||
void SetLoadError(std::string error);
|
||||
void UpdateFilePaths(const std::string& resolved_path);
|
||||
|
||||
@@ -243,6 +244,7 @@ class App {
|
||||
std::optional<std::string> pending_load_;
|
||||
bool preserve_camera_on_load_ = false;
|
||||
ModelKind model_kind_ = kEmptyModel;
|
||||
platform::GraphicsMode gfx_mode_ = platform::GraphicsMode::FilamentVulkan;
|
||||
|
||||
std::unique_ptr<platform::Window> window_;
|
||||
std::unique_ptr<platform::Renderer> renderer_;
|
||||
|
||||
@@ -26,12 +26,13 @@
|
||||
|
||||
#include <absl/flags/flag.h>
|
||||
#include <mujoco/mujoco.h>
|
||||
#include "experimental/platform/graphics_mode.h"
|
||||
#include "experimental/studio/app.h"
|
||||
|
||||
ABSL_FLAG(int, window_width, 1400, "Window width");
|
||||
ABSL_FLAG(int, window_height, 720, "Window height");
|
||||
ABSL_FLAG(std::string, model_file, "", "MuJoCo model file.");
|
||||
ABSL_FLAG(bool, offscreen_mode, false, "Offscreen mode");
|
||||
ABSL_FLAG(std::string, gfx, "", "Graphics API");
|
||||
|
||||
std::string Resolve(std::string_view path) {
|
||||
std::string_view subpath = path.substr(path.find(':') + 1);
|
||||
@@ -99,14 +100,31 @@ int main(int argc, char** argv, char** envp) {
|
||||
resource_provider.prefix = "filament";
|
||||
mjp_registerResourceProvider(&resource_provider);
|
||||
|
||||
std::string gfx = absl::GetFlag(FLAGS_gfx);
|
||||
|
||||
mujoco::platform::GraphicsMode gfx_mode =
|
||||
mujoco::platform::GraphicsMode::FilamentVulkan;
|
||||
if (gfx == "classic") {
|
||||
gfx_mode = mujoco::platform::GraphicsMode::ClassicOpenGl;
|
||||
} else if (gfx == "classic_headless") {
|
||||
gfx_mode = mujoco::platform::GraphicsMode::ClassicOpenGlHeadless;
|
||||
} else if (gfx == "opengl") {
|
||||
gfx_mode = mujoco::platform::GraphicsMode::FilamentOpenGl;
|
||||
} else if (gfx == "opengl_headless") {
|
||||
gfx_mode = mujoco::platform::GraphicsMode::FilamentOpenGlHeadless;
|
||||
} else if (gfx == "vulkan" || gfx.empty()) {
|
||||
gfx_mode = mujoco::platform::GraphicsMode::FilamentVulkan;
|
||||
} else {
|
||||
mju_error("Unsupported graphics API: %s", gfx.c_str());
|
||||
}
|
||||
|
||||
const int width = absl::GetFlag(FLAGS_window_width);
|
||||
const int height = absl::GetFlag(FLAGS_window_height);
|
||||
const bool offscreen_mode = absl::GetFlag(FLAGS_offscreen_mode);
|
||||
mujoco::studio::App app({
|
||||
.width = width,
|
||||
.height = height,
|
||||
.ini_path = ini_path,
|
||||
.offscreen_mode = offscreen_mode,
|
||||
.gfx_mode = gfx_mode,
|
||||
});
|
||||
|
||||
// If the model file is not specified, try to load it from the first argument
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
#include <utility>
|
||||
|
||||
#include <mujoco/mujoco.h>
|
||||
#include "experimental/platform/graphics_mode.h"
|
||||
#include "experimental/studio/app.h"
|
||||
|
||||
// Global app instance. Lifetime is controlled by Init/Deinit calls which are
|
||||
@@ -95,6 +96,7 @@ void Init() {
|
||||
.width = width,
|
||||
.height = height,
|
||||
.ini_path = ini_path,
|
||||
.gfx_mode = mujoco::platform::GraphicsMode::FilamentWebGl,
|
||||
});
|
||||
g_app->InitEmptyModel();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user