From 3a5a48b9f9450683f6574abe49e3f63c7f8fe61f Mon Sep 17 00:00:00 2001 From: Matija Kecman Date: Mon, 27 Apr 2026 01:58:43 -0700 Subject: [PATCH] Fix texture ID collisions in ImguiBridge Texture IDs were assigned using textures_.size() + 1, which is not monotonic: after erasures the map shrinks and previously used IDs can be reassigned to new textures. This causes multiple ImGui images to reference the same underlying Filament texture, rendering them all with the same pixel data. This fix replaces the size-based scheme with a monotonically increasing counter (next_tex_id_) that never reuses IDs. PiperOrigin-RevId: 906214496 Change-Id: I5a388861ad9565c8e00e27b39cc9c76c9750ca1e --- src/experimental/filament/compat/imgui_bridge.cc | 4 ++-- src/experimental/filament/compat/imgui_bridge.h | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/experimental/filament/compat/imgui_bridge.cc b/src/experimental/filament/compat/imgui_bridge.cc index 71c9da01..c7b83514 100644 --- a/src/experimental/filament/compat/imgui_bridge.cc +++ b/src/experimental/filament/compat/imgui_bridge.cc @@ -72,7 +72,7 @@ uintptr_t ImguiBridge::UploadImage(uintptr_t tex_id, const uint8_t* pixels, // Assign a new texture ID. if (tex_id == 0) { - tex_id = textures_.size() + 1; + tex_id = next_tex_id_++; } std::unique_ptr& texture = textures_[tex_id]; @@ -123,7 +123,7 @@ void ImguiBridge::CreateTexture(ImTextureData* data) { config.format = mjPIXEL_FORMAT_RGBA8; config.color_space = mjCOLORSPACE_LINEAR; - const uintptr_t tex_id = textures_.size() + 1; + const uintptr_t tex_id = next_tex_id_++; textures_[tex_id] = std::make_unique(scene_view_->GetEngine(), config); data->SetTexID((ImTextureID)tex_id); diff --git a/src/experimental/filament/compat/imgui_bridge.h b/src/experimental/filament/compat/imgui_bridge.h index 06807f68..953cbd1e 100644 --- a/src/experimental/filament/compat/imgui_bridge.h +++ b/src/experimental/filament/compat/imgui_bridge.h @@ -64,6 +64,7 @@ class ImguiBridge { std::vector> renderables_; std::vector> meshes_; std::unordered_map> textures_; + uintptr_t next_tex_id_ = 1; }; // Draws text at the given screen coordinates in clip space (i.e. [-1,-1,-1] to