Refactor FilamentContext to prepare for upcoming changes.

Separate out the parts of the FilamentContext initialization that
depends on the mjModel into a separate Init function.

PiperOrigin-RevId: 890373341
Change-Id: I06b524668cd60dad804d437958f4f6606c46200b
This commit is contained in:
Haroon Qureshi
2026-03-27 04:25:34 -07:00
committed by Copybara-Service
parent d597b23e5e
commit 1c2100e27c
5 changed files with 31 additions and 24 deletions
@@ -49,9 +49,8 @@
namespace mujoco {
FilamentContext::FilamentContext(const mjrFilamentConfig* config,
const mjModel* model)
: config_(*config), model_(model) {
FilamentContext::FilamentContext(const mjrFilamentConfig* config)
: config_(*config) {
FilamentPlatformSetup setup = CreateFilamentPlatform(config_);
platform_ = std::move(setup.platform);
@@ -63,32 +62,16 @@ FilamentContext::FilamentContext(const mjrFilamentConfig* config,
engine_ = engine_builder.build();
renderer_ = engine_->createRenderer();
const int width = model_->vis.global.offwidth;
const int height = model_->vis.global.offheight;
#ifdef __EMSCRIPTEN__
window_swap_chain_ = engine_->createSwapChain(nullptr);
#else
if (config_.native_window) {
window_swap_chain_ = engine_->createSwapChain(config_.native_window);
} else {
window_swap_chain_ = engine_->createSwapChain(width, height);
window_swap_chain_ = engine_->createSwapChain(config_.width, config_.height);
}
#endif
offscreen_swap_chain_ = engine_->createSwapChain(width, height);
object_manager_ = std::make_unique<ObjectManager>(model, engine_);
// Set clear options.
filament::Renderer::ClearOptions opts;
opts.clear = true;
opts.discard = true;
opts.clearColor = ReadElement(model_, "filament.clearColor",
filament::math::float4(0, 0, 0, 1));
renderer_->setClearOptions(opts);
scene_view_ = std::make_unique<SceneView>(engine_, object_manager_.get());
gui_view_ = std::make_unique<GuiView>(engine_, object_manager_.get());
offscreen_swap_chain_ = engine_->createSwapChain(config_.width, config_.height);
}
FilamentContext::~FilamentContext() {
@@ -102,6 +85,20 @@ FilamentContext::~FilamentContext() {
filament::Engine::destroy(engine_);
}
void FilamentContext::Init(const mjModel* model) {
object_manager_ = std::make_unique<ObjectManager>(model, engine_);
scene_view_ = std::make_unique<SceneView>(engine_, object_manager_.get());
gui_view_ = std::make_unique<GuiView>(engine_, object_manager_.get());
// Set clear options.
filament::Renderer::ClearOptions opts;
opts.clear = true;
opts.discard = true;
opts.clearColor = ReadElement(model, "filament.clearColor",
filament::math::float4(0, 0, 0, 1));
renderer_->setClearOptions(opts);
}
void FilamentContext::Render(const mjrRect& viewport, const mjvScene* scene) {
// If we're rendering to the window, and the window size has changed, we need
// to reacquire the swap chain.
@@ -35,9 +35,11 @@ namespace mujoco {
// Manages the filament renderer that is exposed via the mjr functions.
class FilamentContext {
public:
FilamentContext(const mjrFilamentConfig* config, const mjModel* model);
explicit FilamentContext(const mjrFilamentConfig* config);
~FilamentContext();
void Init(const mjModel* model);
void Render(const mjrRect& viewport, const mjvScene* scene);
void SetFrameBuffer(int framebuffer);
@@ -70,7 +72,6 @@ class FilamentContext {
void DestroyRenderTargets();
mjrFilamentConfig config_;
const mjModel* model_ = nullptr;
filament::Engine* engine_ = nullptr;
filament::Renderer* renderer_ = nullptr;
@@ -49,7 +49,8 @@ void mjrf_makeFilamentContext(const mjModel* m, mjrContext* con,
if (g_filament_context != nullptr) {
mju_error("Context already exists!");
}
g_filament_context = new mujoco::FilamentContext(config, m);
g_filament_context = new mujoco::FilamentContext(config);
g_filament_context->Init(m);
}
void mjrf_defaultContext(mjrContext* con) {
@@ -60,6 +61,8 @@ void mjrf_makeContext(const mjModel* m, mjrContext* con, int fontscale) {
mjr_freeContext(con);
mjrFilamentConfig cfg;
mjrf_defaultFilamentConfig(&cfg);
cfg.width = m->vis.global.offwidth;
cfg.height = m->vis.global.offheight;
mjrf_makeFilamentContext(m, con, &cfg);
}
@@ -39,6 +39,10 @@ struct mjrFilamentConfig {
// The native window handle into which we can render directly.
void* native_window;
// The initial width and height of the offscreen framebuffer.
int width;
int height;
// The backend graphics API to use.
int graphics_api;
+2
View File
@@ -88,6 +88,8 @@ void Renderer::Init(const mjModel* model) {
mjrFilamentConfig render_config;
mjrf_defaultFilamentConfig(&render_config);
render_config.native_window = native_window_;
render_config.width = model->vis.global.offwidth;
render_config.height = model->vis.global.offheight;
render_config.force_software_rendering = IsSoftware(gfx_);
render_config.graphics_api =
IsOpenGl(gfx_) || IsWebGl(gfx_) ? mjGFX_OPENGL : mjGFX_VULKAN;