Remove ObjectManager dependency from GuiView.

PiperOrigin-RevId: 890407310
Change-Id: I9548ee698fbd02d967581200ba2f6ec31e0f7a7c
This commit is contained in:
Haroon Qureshi
2026-03-27 06:01:21 -07:00
committed by Copybara-Service
parent 1c2100e27c
commit b31e15fbb8
3 changed files with 13 additions and 20 deletions
@@ -88,7 +88,8 @@ FilamentContext::~FilamentContext() {
void FilamentContext::Init(const mjModel* model) { void FilamentContext::Init(const mjModel* model) {
object_manager_ = std::make_unique<ObjectManager>(model, engine_); object_manager_ = std::make_unique<ObjectManager>(model, engine_);
scene_view_ = std::make_unique<SceneView>(engine_, object_manager_.get()); scene_view_ = std::make_unique<SceneView>(engine_, object_manager_.get());
gui_view_ = std::make_unique<GuiView>(engine_, object_manager_.get()); gui_view_ = std::make_unique<GuiView>(
engine_, object_manager_->GetMaterial(ObjectManager::kUnlitUi));
// Set clear options. // Set clear options.
filament::Renderer::ClearOptions opts; filament::Renderer::ClearOptions opts;
+10 -16
View File
@@ -31,7 +31,6 @@
#include <mujoco/mjrender.h> #include <mujoco/mjrender.h>
#include <mujoco/mujoco.h> #include <mujoco/mujoco.h>
#include "experimental/filament/filament/buffer_util.h" #include "experimental/filament/filament/buffer_util.h"
#include "experimental/filament/filament/object_manager.h"
#include "experimental/filament/filament/vertex_util.h" #include "experimental/filament/filament/vertex_util.h"
namespace mujoco { namespace mujoco {
@@ -41,8 +40,8 @@ using filament::math::float4;
static constexpr auto kTriangles = static constexpr auto kTriangles =
filament::RenderableManager::PrimitiveType::TRIANGLES; filament::RenderableManager::PrimitiveType::TRIANGLES;
GuiView::GuiView(filament::Engine* engine, ObjectManager* object_mgr) GuiView::GuiView(filament::Engine* engine, filament::Material* ui_material)
: object_mgr_(object_mgr), engine_(engine) { : engine_(engine), material_(ui_material) {
auto& em = utils::EntityManager::get(); auto& em = utils::EntityManager::get();
scene_ = engine_->createScene(); scene_ = engine_->createScene();
camera_ = engine_->createCamera(em.create()); camera_ = engine_->createCamera(em.create());
@@ -51,8 +50,6 @@ GuiView::GuiView(filament::Engine* engine, ObjectManager* object_mgr)
view_->setScene(scene_); view_->setScene(scene_);
view_->setCamera(camera_); view_->setCamera(camera_);
view_->setPostProcessingEnabled(false); view_->setPostProcessingEnabled(false);
material_ = object_mgr_->GetMaterial(ObjectManager::kUnlitUi);
} }
GuiView::~GuiView() { GuiView::~GuiView() {
@@ -107,8 +104,6 @@ uintptr_t GuiView::UploadImage(uintptr_t tex_id, const uint8_t* pixels,
const auto texture_format = bpp == 4 ? filament::Texture::Format::RGBA const auto texture_format = bpp == 4 ? filament::Texture::Format::RGBA
: filament::Texture::Format::RGB; : filament::Texture::Format::RGB;
filament::Engine* engine = object_mgr_->GetEngine();
filament::Texture* texture = nullptr; filament::Texture* texture = nullptr;
if (tex_id == 0) { if (tex_id == 0) {
texture = filament::Texture::Builder() texture = filament::Texture::Builder()
@@ -117,7 +112,7 @@ uintptr_t GuiView::UploadImage(uintptr_t tex_id, const uint8_t* pixels,
.levels(1) .levels(1)
.format(internal_format) .format(internal_format)
.sampler(filament::Texture::Sampler::SAMPLER_2D) .sampler(filament::Texture::Sampler::SAMPLER_2D)
.build(*engine); .build(*engine_);
tex_id = textures_.size() + 1; tex_id = textures_.size() + 1;
textures_[tex_id] = texture; textures_[tex_id] = texture;
} else { } else {
@@ -129,19 +124,19 @@ uintptr_t GuiView::UploadImage(uintptr_t tex_id, const uint8_t* pixels,
if (pixels == nullptr) { if (pixels == nullptr) {
// A nullptr implies that the user wants to destroy the texture. // A nullptr implies that the user wants to destroy the texture.
engine->destroy(texture); engine_->destroy(texture);
textures_.erase(tex_id); textures_.erase(tex_id);
return 0; return 0;
} else if (texture->getWidth() != width || texture->getHeight() != height) { } else if (texture->getWidth() != width || texture->getHeight() != height) {
// Recreate the texture if the dimensions have changed. // Recreate the texture if the dimensions have changed.
engine->destroy(texture); engine_->destroy(texture);
texture = filament::Texture::Builder() texture = filament::Texture::Builder()
.width(width) .width(width)
.height(height) .height(height)
.levels(1) .levels(1)
.format(internal_format) .format(internal_format)
.sampler(filament::Texture::Sampler::SAMPLER_2D) .sampler(filament::Texture::Sampler::SAMPLER_2D)
.build(*engine); .build(*engine_);
textures_[tex_id] = texture; textures_[tex_id] = texture;
} }
} }
@@ -158,12 +153,11 @@ uintptr_t GuiView::UploadImage(uintptr_t tex_id, const uint8_t* pixels,
filament::Texture::PixelBufferDescriptor pb(bytes, num_bytes, texture_format, filament::Texture::PixelBufferDescriptor pb(bytes, num_bytes, texture_format,
filament::Texture::Type::UBYTE, filament::Texture::Type::UBYTE,
callback); callback);
texture->setImage(*engine, 0, std::move(pb)); texture->setImage(*engine_, 0, std::move(pb));
return tex_id; return tex_id;
} }
void GuiView::CreateTexture(ImTextureData* data) { void GuiView::CreateTexture(ImTextureData* data) {
filament::Engine* engine = object_mgr_->GetEngine();
if (data->Format != ImTextureFormat_RGBA32) { if (data->Format != ImTextureFormat_RGBA32) {
mju_error("Unsupported texture format."); mju_error("Unsupported texture format.");
} }
@@ -175,7 +169,7 @@ void GuiView::CreateTexture(ImTextureData* data) {
.levels(1) .levels(1)
.format(filament::Texture::InternalFormat::RGBA8) .format(filament::Texture::InternalFormat::RGBA8)
.sampler(filament::Texture::Sampler::SAMPLER_2D) .sampler(filament::Texture::Sampler::SAMPLER_2D)
.build(*engine); .build(*engine_);
const uintptr_t tex_id = textures_.size() + 1; const uintptr_t tex_id = textures_.size() + 1;
textures_[tex_id] = texture; textures_[tex_id] = texture;
@@ -194,14 +188,14 @@ void GuiView::UpdateTexture(ImTextureData* data) {
} }
filament::Texture* texture = iter->second; filament::Texture* texture = iter->second;
texture->setImage(*object_mgr_->GetEngine(), 0, std::move(pb)); texture->setImage(*engine_, 0, std::move(pb));
data->SetStatus(ImTextureStatus_OK); data->SetStatus(ImTextureStatus_OK);
} }
void GuiView::DestroyTexture(ImTextureData* data) { void GuiView::DestroyTexture(ImTextureData* data) {
auto iter = textures_.find(data->TexID); auto iter = textures_.find(data->TexID);
if (iter != textures_.end()) { if (iter != textures_.end()) {
object_mgr_->GetEngine()->destroy(iter->second); engine_->destroy(iter->second);
textures_.erase(data->TexID); textures_.erase(data->TexID);
data->SetTexID(ImTextureID_Invalid); data->SetTexID(ImTextureID_Invalid);
data->SetStatus(ImTextureStatus_Destroyed); data->SetStatus(ImTextureStatus_Destroyed);
@@ -29,14 +29,13 @@
#include <filament/View.h> #include <filament/View.h>
#include <mujoco/mjrender.h> #include <mujoco/mjrender.h>
#include "experimental/filament/filament/buffer_util.h" #include "experimental/filament/filament/buffer_util.h"
#include "experimental/filament/filament/object_manager.h"
namespace mujoco { namespace mujoco {
// A filament::View that contains a filament::Scene used for rendering the GUI. // A filament::View that contains a filament::Scene used for rendering the GUI.
class GuiView { class GuiView {
public: public:
GuiView(filament::Engine* engine, ObjectManager* object_mgr); GuiView(filament::Engine* engine, filament::Material* ui_material);
~GuiView(); ~GuiView();
// Prepares the UX scene renderable using data from the current ImGui state. // Prepares the UX scene renderable using data from the current ImGui state.
@@ -64,7 +63,6 @@ class GuiView {
// Clears the filament::Scene of the UX renderable and releases all buffers. // Clears the filament::Scene of the UX renderable and releases all buffers.
void ResetRenderable(); void ResetRenderable();
ObjectManager* object_mgr_ = nullptr;
filament::Engine* engine_ = nullptr; filament::Engine* engine_ = nullptr;
filament::Scene* scene_ = nullptr; filament::Scene* scene_ = nullptr;
filament::Camera* camera_ = nullptr; filament::Camera* camera_ = nullptr;