Move all texture creation functions into texture_util.

PiperOrigin-RevId: 892838123
Change-Id: I84a536b111c3d6b5391513d6623c2e9a4d02f3df
This commit is contained in:
Haroon Qureshi
2026-04-01 03:47:08 -07:00
committed by Copybara-Service
parent 0b54638df4
commit 6d32038417
8 changed files with 99 additions and 87 deletions
@@ -45,6 +45,7 @@
#include "experimental/filament/filament/object_manager.h"
#include "experimental/filament/filament/render_target_util.h"
#include "experimental/filament/filament/scene_view.h"
#include "experimental/filament/filament/texture_util.h"
#include "experimental/filament/render_context_filament.h"
namespace mujoco {
@@ -62,16 +63,18 @@ FilamentContext::FilamentContext(const mjrFilamentConfig* config)
engine_ = engine_builder.build();
renderer_ = engine_->createRenderer();
#ifdef __EMSCRIPTEN__
window_swap_chain_ = engine_->createSwapChain(nullptr);
#else
#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(config_.width, config_.height);
window_swap_chain_ =
engine_->createSwapChain(config_.width, config_.height);
}
#endif
offscreen_swap_chain_ = engine_->createSwapChain(config_.width, config_.height);
#endif
offscreen_swap_chain_ =
engine_->createSwapChain(config_.width, config_.height);
object_manager_ = std::make_unique<ObjectManager>(engine_);
}
@@ -183,11 +186,13 @@ void FilamentContext::SetFrameBuffer(int framebuffer) {
void FilamentContext::PrepareRenderTargets(int width, int height) {
color_target_ = std::make_unique<RenderTargetAndTextures>(
engine_, kRenderTargetColor, kRenderTargetDepth);
engine_, RenderTargetTextureType::kColor,
RenderTargetTextureType::kDepth);
color_target_->Prepare(width, height);
depth_target_ = std::make_unique<RenderTargetAndTextures>(
engine_, kRenderTargetDepthColor, kRenderTargetDepth);
engine_, RenderTargetTextureType::kDepthColor,
RenderTargetTextureType::kDepth);
depth_target_->Prepare(width, height);
}
@@ -310,8 +315,6 @@ double FilamentContext::GetFrameRate() const {
return 1.0e9 / static_cast<double>(ns);
}
void FilamentContext::UpdateGui() {
DrawGui(scene_view_.get());
}
void FilamentContext::UpdateGui() { DrawGui(scene_view_.get()); }
} // namespace mujoco
@@ -486,23 +486,4 @@ filament::IndexBuffer* CreateIndexBuffer(filament::Engine* engine,
FillSequence<uint16_t>);
}
}
filament::Texture* CreateTexture(filament::Engine* engine, const mjModel* model,
int id, TextureType texture_type) {
if (id < 0 || id >= model->ntex) {
mju_error("Invalid texture index %d", id);
}
const int width = model->tex_width[id];
const int height = model->tex_height[id];
const bool is_srgb = model->tex_colorspace[id] == mjCOLORSPACE_SRGB;
const int num_channels = model->tex_nchannel[id];
const mjtByte* data = model->tex_data + model->tex_adr[id];
filament::Texture* texture =
texture_type == TextureType::kNormal2d
? Create2dTexture(engine, width, height, num_channels, data, is_srgb)
: CreateCubeTexture(engine, width, height, num_channels, data,
is_srgb);
return texture;
}
} // namespace mujoco
@@ -37,12 +37,6 @@ enum class MeshType {
kHeightField,
};
// The types of textures stored in the mjModel.
enum class TextureType {
kNormal2d,
kCube,
};
// Generates a filament VertexBuffer for a given mesh in the mjModel.
filament::VertexBuffer* CreateVertexBuffer(filament::Engine* engine,
const mjModel* model, int id,
@@ -54,10 +48,6 @@ filament::IndexBuffer* CreateIndexBuffer(filament::Engine* engine,
const mjModel* model, int id,
MeshType mesh_type);
// Generates a filament Texture for a given 2D texture in the mjModel.
filament::Texture* CreateTexture(filament::Engine* engine, const mjModel* model,
int id, TextureType texture_type);
// Reads a value with the given name from the mjModel's data sections. The
// default_value is returned if the named element is not found.
template <typename T>
@@ -21,39 +21,7 @@
namespace mujoco {
static filament::Texture* CreateRenderTargetTexture(
filament::Engine* engine, int width, int height,
RenderTargetTextureType type) {
filament::Texture::Builder builder;
builder.width(width);
builder.height(height);
switch (type) {
case kRenderTargetColor:
builder.usage(filament::Texture::Usage::COLOR_ATTACHMENT |
filament::Texture::Usage::BLIT_SRC);
builder.format(filament::Texture::InternalFormat::RGB8);
break;
case kRenderTargetDepth:
builder.usage(filament::Texture::Usage::DEPTH_ATTACHMENT |
filament::Texture::Usage::SAMPLEABLE);
builder.format(filament::Texture::InternalFormat::DEPTH32F);
break;
case kRenderTargetDepthColor:
builder.usage(filament::Texture::Usage::COLOR_ATTACHMENT |
filament::Texture::Usage::BLIT_SRC);
builder.format(filament::Texture::InternalFormat::R32F);
break;
case kRenderTargetReflectionColor:
builder.usage(filament::Texture::Usage::COLOR_ATTACHMENT |
filament::Texture::Usage::BLIT_SRC |
filament::Texture::Usage::SAMPLEABLE);
builder.format(filament::Texture::InternalFormat::RGBA8);
break;
default:
mju_error("Unknown type: %d", static_cast<int>(type));
}
return builder.build(*engine);
}
RenderTargetAndTextures::RenderTargetAndTextures(filament::Engine* engine,
RenderTargetTextureType color,
@@ -17,19 +17,10 @@
#include <filament/Engine.h>
#include <filament/Texture.h>
#include "experimental/filament/filament/texture_util.h"
namespace mujoco {
// The different types of textures we can create for a render target.
enum RenderTargetTextureType {
kRenderTargetNone,
kRenderTargetColor,
kRenderTargetDepth,
kRenderTargetDepthColor,
kRenderTargetReflectionColor,
kNumRenderTargetTextureTypes,
};
// Manages a filament RenderTarget and the textures which are bound to it.
class RenderTargetAndTextures {
public:
@@ -63,8 +54,8 @@ class RenderTargetAndTextures {
filament::Texture* color_texture_ = nullptr;
filament::Texture* depth_texture_ = nullptr;
filament::RenderTarget* render_target_ = nullptr;
RenderTargetTextureType color_type_ = kRenderTargetNone;
RenderTargetTextureType depth_type_ = kRenderTargetNone;
RenderTargetTextureType color_type_;
RenderTargetTextureType depth_type_;
int width_ = 0;
int height_ = 0;
};
@@ -50,6 +50,7 @@
#include "experimental/filament/filament/model_util.h"
#include "experimental/filament/filament/object_manager.h"
#include "experimental/filament/filament/render_target_util.h"
#include "experimental/filament/filament/texture_util.h"
namespace mujoco {
@@ -516,7 +517,8 @@ void SceneView::AddReflectiveDrawable(Drawable* drawable) {
filament::Engine* engine = object_mgr_->GetEngine();
while (reflect_targets_.size() < reflectives_.size()) {
reflect_targets_.push_back(std::make_unique<RenderTargetAndTextures>(
engine, kRenderTargetReflectionColor, kRenderTargetDepth));
engine, RenderTargetTextureType::kReflectionColor,
RenderTargetTextureType::kDepth));
}
// Prepare a render target for the reflective drawable.
@@ -26,7 +26,6 @@
#include <math/vec3.h>
#include <mujoco/mujoco.h>
namespace mujoco {
static filament::Texture::Format GetTextureFormat(int num_channels) {
@@ -168,6 +167,40 @@ filament::Texture* CreateCubeTexture(filament::Engine* engine, int width,
return texture;
}
filament::Texture* CreateRenderTargetTexture(
filament::Engine* engine, int width, int height,
RenderTargetTextureType type) {
filament::Texture::Builder builder;
builder.width(width);
builder.height(height);
switch (type) {
case RenderTargetTextureType::kColor:
builder.usage(filament::Texture::Usage::COLOR_ATTACHMENT |
filament::Texture::Usage::BLIT_SRC);
builder.format(filament::Texture::InternalFormat::RGB8);
break;
case RenderTargetTextureType::kDepth:
builder.usage(filament::Texture::Usage::DEPTH_ATTACHMENT |
filament::Texture::Usage::SAMPLEABLE);
builder.format(filament::Texture::InternalFormat::DEPTH32F);
break;
case RenderTargetTextureType::kDepthColor:
builder.usage(filament::Texture::Usage::COLOR_ATTACHMENT |
filament::Texture::Usage::BLIT_SRC);
builder.format(filament::Texture::InternalFormat::R32F);
break;
case RenderTargetTextureType::kReflectionColor:
builder.usage(filament::Texture::Usage::COLOR_ATTACHMENT |
filament::Texture::Usage::BLIT_SRC |
filament::Texture::Usage::SAMPLEABLE);
builder.format(filament::Texture::InternalFormat::RGBA8);
break;
default:
mju_error("Unknown type: %d", static_cast<int>(type));
}
return builder.build(*engine);
}
filament::Texture* CreateKtxTexture(
filament::Engine* engine, const uint8_t* data, int size,
filament::math::float3* spherical_harmonics_out) {
@@ -178,4 +211,23 @@ filament::Texture* CreateKtxTexture(
const bool is_srgb = false;
return ktxreader::Ktx1Reader::createTexture(engine, bundle, is_srgb);
}
filament::Texture* CreateTexture(filament::Engine* engine, const mjModel* model,
int id, TextureType texture_type) {
if (id < 0 || id >= model->ntex) {
mju_error("Invalid texture index %d", id);
}
const int width = model->tex_width[id];
const int height = model->tex_height[id];
const bool is_srgb = model->tex_colorspace[id] == mjCOLORSPACE_SRGB;
const int num_channels = model->tex_nchannel[id];
const mjtByte* data = model->tex_data + model->tex_adr[id];
filament::Texture* texture =
texture_type == TextureType::kNormal2d
? Create2dTexture(engine, width, height, num_channels, data, is_srgb)
: CreateCubeTexture(engine, width, height, num_channels, data,
is_srgb);
return texture;
}
} // namespace mujoco
@@ -20,10 +20,26 @@
#include <filament/Engine.h>
#include <filament/Texture.h>
#include <math/vec3.h>
#include <mujoco/mjmodel.h>
// Functions for creating filament textures.
namespace mujoco {
// The types of textures we can create.
enum class TextureType {
kNormal2d,
kCube,
kKtx,
};
// The different types of textures we can create for a render target.
enum class RenderTargetTextureType {
kColor,
kDepth,
kDepthColor,
kReflectionColor,
};
// Creates a filament Texture for the given 2D texture.
filament::Texture* Create2dTexture(filament::Engine* engine, int width,
int height, int num_channels,
@@ -39,6 +55,15 @@ filament::Texture* CreateKtxTexture(
filament::Engine* engine, const uint8_t* data, int size,
filament::math::float3* spherical_harmonics_out);
// Creates a filament Texture for the given texture in the mjModel.
filament::Texture* CreateTexture(filament::Engine* engine, const mjModel* model,
int id, TextureType texture_type);
// Creates a filament Texture for the given render target.
filament::Texture* CreateRenderTargetTexture(filament::Engine* engine,
int width, int height,
RenderTargetTextureType type);
} // namespace mujoco
#endif // MUJOCO_SRC_EXPERIMENTAL_FILAMENT_FILAMENT_TEXTURE_UTIL_H_