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:
committed by
Copybara-Service
parent
d597b23e5e
commit
1c2100e27c
@@ -49,9 +49,8 @@
|
|||||||
|
|
||||||
namespace mujoco {
|
namespace mujoco {
|
||||||
|
|
||||||
FilamentContext::FilamentContext(const mjrFilamentConfig* config,
|
FilamentContext::FilamentContext(const mjrFilamentConfig* config)
|
||||||
const mjModel* model)
|
: config_(*config) {
|
||||||
: config_(*config), model_(model) {
|
|
||||||
FilamentPlatformSetup setup = CreateFilamentPlatform(config_);
|
FilamentPlatformSetup setup = CreateFilamentPlatform(config_);
|
||||||
platform_ = std::move(setup.platform);
|
platform_ = std::move(setup.platform);
|
||||||
|
|
||||||
@@ -63,32 +62,16 @@ FilamentContext::FilamentContext(const mjrFilamentConfig* config,
|
|||||||
engine_ = engine_builder.build();
|
engine_ = engine_builder.build();
|
||||||
|
|
||||||
renderer_ = engine_->createRenderer();
|
renderer_ = engine_->createRenderer();
|
||||||
|
|
||||||
const int width = model_->vis.global.offwidth;
|
|
||||||
const int height = model_->vis.global.offheight;
|
|
||||||
#ifdef __EMSCRIPTEN__
|
#ifdef __EMSCRIPTEN__
|
||||||
window_swap_chain_ = engine_->createSwapChain(nullptr);
|
window_swap_chain_ = engine_->createSwapChain(nullptr);
|
||||||
#else
|
#else
|
||||||
if (config_.native_window) {
|
if (config_.native_window) {
|
||||||
window_swap_chain_ = engine_->createSwapChain(config_.native_window);
|
window_swap_chain_ = engine_->createSwapChain(config_.native_window);
|
||||||
} else {
|
} else {
|
||||||
window_swap_chain_ = engine_->createSwapChain(width, height);
|
window_swap_chain_ = engine_->createSwapChain(config_.width, config_.height);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
offscreen_swap_chain_ = engine_->createSwapChain(config_.width, config_.height);
|
||||||
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());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
FilamentContext::~FilamentContext() {
|
FilamentContext::~FilamentContext() {
|
||||||
@@ -102,6 +85,20 @@ FilamentContext::~FilamentContext() {
|
|||||||
filament::Engine::destroy(engine_);
|
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) {
|
void FilamentContext::Render(const mjrRect& viewport, const mjvScene* scene) {
|
||||||
// If we're rendering to the window, and the window size has changed, we need
|
// If we're rendering to the window, and the window size has changed, we need
|
||||||
// to reacquire the swap chain.
|
// to reacquire the swap chain.
|
||||||
|
|||||||
@@ -35,9 +35,11 @@ namespace mujoco {
|
|||||||
// Manages the filament renderer that is exposed via the mjr functions.
|
// Manages the filament renderer that is exposed via the mjr functions.
|
||||||
class FilamentContext {
|
class FilamentContext {
|
||||||
public:
|
public:
|
||||||
FilamentContext(const mjrFilamentConfig* config, const mjModel* model);
|
explicit FilamentContext(const mjrFilamentConfig* config);
|
||||||
~FilamentContext();
|
~FilamentContext();
|
||||||
|
|
||||||
|
void Init(const mjModel* model);
|
||||||
|
|
||||||
void Render(const mjrRect& viewport, const mjvScene* scene);
|
void Render(const mjrRect& viewport, const mjvScene* scene);
|
||||||
|
|
||||||
void SetFrameBuffer(int framebuffer);
|
void SetFrameBuffer(int framebuffer);
|
||||||
@@ -70,7 +72,6 @@ class FilamentContext {
|
|||||||
void DestroyRenderTargets();
|
void DestroyRenderTargets();
|
||||||
|
|
||||||
mjrFilamentConfig config_;
|
mjrFilamentConfig config_;
|
||||||
const mjModel* model_ = nullptr;
|
|
||||||
|
|
||||||
filament::Engine* engine_ = nullptr;
|
filament::Engine* engine_ = nullptr;
|
||||||
filament::Renderer* renderer_ = nullptr;
|
filament::Renderer* renderer_ = nullptr;
|
||||||
|
|||||||
@@ -49,7 +49,8 @@ void mjrf_makeFilamentContext(const mjModel* m, mjrContext* con,
|
|||||||
if (g_filament_context != nullptr) {
|
if (g_filament_context != nullptr) {
|
||||||
mju_error("Context already exists!");
|
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) {
|
void mjrf_defaultContext(mjrContext* con) {
|
||||||
@@ -60,6 +61,8 @@ void mjrf_makeContext(const mjModel* m, mjrContext* con, int fontscale) {
|
|||||||
mjr_freeContext(con);
|
mjr_freeContext(con);
|
||||||
mjrFilamentConfig cfg;
|
mjrFilamentConfig cfg;
|
||||||
mjrf_defaultFilamentConfig(&cfg);
|
mjrf_defaultFilamentConfig(&cfg);
|
||||||
|
cfg.width = m->vis.global.offwidth;
|
||||||
|
cfg.height = m->vis.global.offheight;
|
||||||
mjrf_makeFilamentContext(m, con, &cfg);
|
mjrf_makeFilamentContext(m, con, &cfg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -39,6 +39,10 @@ struct mjrFilamentConfig {
|
|||||||
// The native window handle into which we can render directly.
|
// The native window handle into which we can render directly.
|
||||||
void* native_window;
|
void* native_window;
|
||||||
|
|
||||||
|
// The initial width and height of the offscreen framebuffer.
|
||||||
|
int width;
|
||||||
|
int height;
|
||||||
|
|
||||||
// The backend graphics API to use.
|
// The backend graphics API to use.
|
||||||
int graphics_api;
|
int graphics_api;
|
||||||
|
|
||||||
|
|||||||
@@ -88,6 +88,8 @@ void Renderer::Init(const mjModel* model) {
|
|||||||
mjrFilamentConfig render_config;
|
mjrFilamentConfig render_config;
|
||||||
mjrf_defaultFilamentConfig(&render_config);
|
mjrf_defaultFilamentConfig(&render_config);
|
||||||
render_config.native_window = native_window_;
|
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.force_software_rendering = IsSoftware(gfx_);
|
||||||
render_config.graphics_api =
|
render_config.graphics_api =
|
||||||
IsOpenGl(gfx_) || IsWebGl(gfx_) ? mjGFX_OPENGL : mjGFX_VULKAN;
|
IsOpenGl(gfx_) || IsWebGl(gfx_) ? mjGFX_OPENGL : mjGFX_VULKAN;
|
||||||
|
|||||||
Reference in New Issue
Block a user