Move DrawMode into its own header file.

PiperOrigin-RevId: 900656695
Change-Id: I4db6f0f74c2109752efe40bc37c7104505139049
This commit is contained in:
Haroon Qureshi
2026-04-16 04:43:39 -07:00
committed by Copybara-Service
parent 56e98cc16c
commit 1117e7db39
10 changed files with 78 additions and 47 deletions
+1
View File
@@ -27,6 +27,7 @@ target_sources(${MUJOCO_FILAMENT_TARGET_NAME}
filament/builtins.h
filament/color_grading_options.cc
filament/color_grading_options.h
filament/draw_mode.h
filament/filament_context.cc
filament/filament_context.h
filament/filament_platform_factory.cc
@@ -0,0 +1,36 @@
// Copyright 2026 DeepMind Technologies Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef MUJOCO_SRC_EXPERIMENTAL_FILAMENT_FILAMENT_DRAW_MODE_H_
#define MUJOCO_SRC_EXPERIMENTAL_FILAMENT_FILAMENT_DRAW_MODE_H_
namespace mujoco {
// The different modes that can be used to render the scene.
enum class DrawMode {
// Render the scene with "normal" colors and lighting.
Color,
// Render the scene as a grayscale depth map.
Depth,
// Render each object with a unique, uniform (flat) color regardless of
// lighting and texture.
Segmentation,
};
static constexpr int kNumDrawModes = 3;
} // namespace mujoco
#endif // MUJOCO_SRC_EXPERIMENTAL_FILAMENT_FILAMENT_DRAW_MODE_H_
@@ -38,6 +38,7 @@
#include <mujoco/mjmodel.h>
#include <mujoco/mjvisualize.h>
#include <mujoco/mujoco.h>
#include "experimental/filament/filament/draw_mode.h"
#include "experimental/filament/filament/filament_platform_factory.h"
#include "experimental/filament/filament/imgui_bridge.h"
#include "experimental/filament/filament/imgui_editor.h"
@@ -134,11 +135,11 @@ void FilamentContext::Render(const mjrRect& viewport, const mjvScene* scene) {
imgui_bridge_->Update();
}
last_render_mode_ = SceneView::DrawMode::kNormal;
last_render_mode_ = DrawMode::Color;
if (scene->flags[mjRND_SEGMENT]) {
last_render_mode_ = SceneView::DrawMode::kSegmentation;
last_render_mode_ = DrawMode::Segmentation;
} else if (scene->flags[mjRND_DEPTH]) {
last_render_mode_ = SceneView::DrawMode::kDepth;
last_render_mode_ = DrawMode::Depth;
}
last_camera_ = mjv_averageCamera(scene->camera, scene->camera + 1);
@@ -242,7 +243,7 @@ void FilamentContext::ReadPixels(mjrRect viewport, unsigned char* rgb,
if (depth) {
if (renderer_->beginFrame(offscreen_swap_chain_)) {
SceneView::RenderRequest request;
request.draw_mode = SceneView::DrawMode::kDepth;
request.draw_mode = DrawMode::Depth;
request.viewport = viewport;
request.target = depth_target_.get();
request.camera = last_camera_;
@@ -24,6 +24,7 @@
#include <mujoco/mjmodel.h>
#include <mujoco/mjrender.h>
#include <mujoco/mjvisualize.h>
#include "experimental/filament/filament/draw_mode.h"
#include "experimental/filament/filament/imgui_bridge.h"
#include "experimental/filament/filament/object_manager.h"
#include "experimental/filament/filament/render_target.h"
@@ -80,7 +81,7 @@ class FilamentContext {
filament::SwapChain* offscreen_swap_chain_ = nullptr;
std::unique_ptr<filament::backend::Platform> platform_;
SceneView::DrawMode last_render_mode_ = SceneView::DrawMode::kNormal;
DrawMode last_render_mode_ = DrawMode::Color;
mjvGLCamera last_camera_;
SwapChainType scene_swap_chain_target_ = kWindowSwapChain;
SwapChainType gui_swap_chain_target_ = kWindowSwapChain;
@@ -23,6 +23,7 @@
#include <imgui.h>
#include <math/vec4.h>
#include <mujoco/mujoco.h>
#include "experimental/filament/filament/draw_mode.h"
#include "experimental/filament/filament/material.h"
#include "experimental/filament/filament/mesh.h"
#include "experimental/filament/filament/renderable.h"
@@ -276,7 +277,7 @@ void ImguiBridge::PrepareRenderables(int count) {
r->SetBlendOrder(static_cast<std::uint16_t>(renderables_.size()));
Material& material = r->GetMaterial();
Material::DrawMode mode = Material::DrawMode::kNormal;
DrawMode mode = DrawMode::Color;
material.SetMaterial(mode, object_mgr_->GetMaterial(ObjectManager::kUnlitUi));
r->SetMaterialInstance(material.GetMaterialInstance(mode));
scene_view_->AddToUxScene(r.get());
+17 -9
View File
@@ -20,6 +20,7 @@
#include <filament/RenderableManager.h>
#include <filament/TextureSampler.h>
#include <mujoco/mujoco.h>
#include "experimental/filament/filament/draw_mode.h"
#include "experimental/filament/filament/texture.h"
#include "experimental/filament/filament/object_manager.h"
@@ -38,22 +39,27 @@ Material::~Material() noexcept {
}
void Material::SetMaterial(DrawMode mode, filament::Material* material) {
if (instances_[mode]) {
const int index = static_cast<int>(mode);
if (instances_[index]) {
const filament::Material* current_material =
instances_[mode]->getMaterial();
instances_[index]->getMaterial();
if (current_material == material) {
return;
}
GetEngine()->destroy(instances_[mode]);
instances_[mode] = nullptr;
GetEngine()->destroy(instances_[index]);
instances_[index] = nullptr;
}
if (material) {
instances_[mode] = material->createInstance();
instances_[index] = material->createInstance();
UpdateMaterialInstances();
}
}
filament::MaterialInstance* Material::GetMaterialInstance(DrawMode mode) {
return instances_[static_cast<int>(mode)];
}
void Material::UpdateParams(const Params& params) {
params_ = params;
UpdateMaterialInstances();
@@ -65,7 +71,8 @@ void Material::UpdateTextures(const Textures& textures) {
}
void Material::UpdateMaterialInstances() {
filament::MaterialInstance* instance = instances_[DrawMode::kNormal];
filament::MaterialInstance* instance =
instances_[static_cast<int>(DrawMode::Color)];
if (instance == nullptr) {
return;
}
@@ -107,9 +114,10 @@ void Material::UpdateMaterialInstances() {
instance->setParameter("Reflectance", params_.reflectance);
}
if (instances_[DrawMode::kSegmentation]) {
instances_[DrawMode::kSegmentation]->setParameter(
"BaseColorFactor", params_.segmentation_color);
const int segmentation_index = static_cast<int>(DrawMode::Segmentation);
if (instances_[segmentation_index]) {
instances_[segmentation_index]->setParameter("BaseColorFactor",
params_.segmentation_color);
}
// All textures use the same default sampler.
+2 -13
View File
@@ -20,6 +20,7 @@
#include <math/vec2.h>
#include <math/vec3.h>
#include <math/vec4.h>
#include "experimental/filament/filament/draw_mode.h"
#include "experimental/filament/filament/texture.h"
#include "experimental/filament/filament/object_manager.h"
@@ -27,16 +28,6 @@ namespace mujoco {
class Material {
public:
// The different methods for rendering objects. Each mode uses a different
// material, but all materials "share" the same textures and parameters
// (unless specifically noted otherwise).
enum DrawMode {
kNormal,
kDepth,
kSegmentation,
kNumDrawModes,
};
// The textures that can be assigned to the drawable's material.
struct Textures {
const Texture* color = nullptr;
@@ -89,9 +80,7 @@ class Material {
const Textures& GetTextures() const { return textures_; }
// Returns the material instance assigned to the draw mode.
filament::MaterialInstance* GetMaterialInstance(DrawMode mode) {
return instances_[mode];
}
filament::MaterialInstance* GetMaterialInstance(DrawMode mode);
// Returns the filament Engine managing the material.
filament::Engine* GetEngine() const { return object_mgr_->GetEngine(); }
@@ -31,6 +31,7 @@
#include <utils/Entity.h>
#include <mujoco/mjvisualize.h>
#include <mujoco/mujoco.h>
#include "experimental/filament/filament/draw_mode.h"
#include "experimental/filament/filament/material.h"
#include "experimental/filament/filament/math_util.h"
#include "experimental/filament/filament/mesh.h"
@@ -533,14 +534,11 @@ static void UpdateGeomMaterial(Renderable& renderable, const mjvGeom& geom,
params.glossiness *= model_objs->GetShininessMultiplier();
material.UpdateParams(params);
material.SetMaterial(DrawMode::Color, object_mgr->GetMaterial(material_type));
material.SetMaterial(DrawMode::Depth,
object_mgr->GetMaterial(ObjectManager::kUnlitDepth));
material.SetMaterial(
Material::DrawMode::kNormal,
object_mgr->GetMaterial(material_type));
material.SetMaterial(
Material::DrawMode::kDepth,
object_mgr->GetMaterial(ObjectManager::kUnlitDepth));
material.SetMaterial(
Material::DrawMode::kSegmentation,
DrawMode::Segmentation,
object_mgr->GetMaterial(ObjectManager::kUnlitSegmentation));
}
@@ -40,6 +40,7 @@
#include <utils/EntityManager.h>
#include <mujoco/mujoco.h>
#include "experimental/filament/filament/color_grading_options.h"
#include "experimental/filament/filament/draw_mode.h"
#include "experimental/filament/filament/light.h"
#include "experimental/filament/filament/material.h"
#include "experimental/filament/filament/math_util.h"
@@ -53,12 +54,9 @@ using filament::math::float3;
using filament::math::float4;
using filament::math::mat4;
static constexpr int kNormalIndex =
static_cast<int>(Material::DrawMode::kNormal);
static constexpr int kDepthIndex =
static_cast<int>(Material::DrawMode::kDepth);
static constexpr int kSegmentIndex =
static_cast<int>(Material::DrawMode::kSegmentation);
static constexpr int kNormalIndex = static_cast<int>(DrawMode::Color);
static constexpr int kDepthIndex = static_cast<int>(DrawMode::Depth);
static constexpr int kSegmentIndex = static_cast<int>(DrawMode::Segmentation);
static filament::ColorGrading::Builder ToBuilder(
const ColorGradingOptions& opts) {
@@ -279,7 +277,7 @@ void SceneView::Render(filament::Renderer* renderer,
}
// Render reflection passes.
if (request.draw_mode == DrawMode::kNormal) {
if (request.draw_mode == DrawMode::Color) {
filament::TransformManager& tm = engine_->getTransformManager();
for (size_t i = 0; i < reflectives_.size(); ++i) {
Renderable* renderable = reflectives_[i];
@@ -27,8 +27,8 @@
#include <filament/View.h>
#include <mujoco/mujoco.h>
#include "experimental/filament/filament/color_grading_options.h"
#include "experimental/filament/filament/draw_mode.h"
#include "experimental/filament/filament/light.h"
#include "experimental/filament/filament/material.h"
#include "experimental/filament/filament/renderable.h"
#include "experimental/filament/filament/render_target.h"
@@ -58,10 +58,9 @@ class SceneView {
void RemoveFromUxScene(Renderable* renderable);
// Parameters for rendering the scene.
using DrawMode = Material::DrawMode;
struct RenderRequest {
// The draw mode (e.g. normal, depth, segmentation) to render.
DrawMode draw_mode = DrawMode::kNormal;
DrawMode draw_mode = DrawMode::Color;
// The target viewport for the rendered image.
mjrRect viewport;
// The camera from which to render the scene.
@@ -102,8 +101,7 @@ class SceneView {
filament::Camera* camera_ = nullptr;
filament::ColorGrading* color_grading_ = nullptr;
ColorGradingOptions color_grading_options_;
std::array<filament::View*, DrawMode::kNumDrawModes> views_;
DrawMode active_mode_ = DrawMode::kNumDrawModes;
std::array<filament::View*, kNumDrawModes> views_;
// Scene objects.
std::unordered_set<Light*> lights_;