From a548cd9934e4064b9eb4c7565973499fc5357fa0 Mon Sep 17 00:00:00 2001 From: ocornut Date: Wed, 19 Mar 2025 15:00:13 +0100 Subject: [PATCH] Fonts: avoid both ImTextureRef fields being set simultaneously. --- imgui.h | 5 +++-- imgui_draw.cpp | 6 ++---- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/imgui.h b/imgui.h index f8e5b6c55..320f4f1c1 100644 --- a/imgui.h +++ b/imgui.h @@ -3426,7 +3426,7 @@ struct IMGUI_API ImTextureData unsigned char* GetPixelsAt(int x, int y) { IM_ASSERT(Pixels != NULL); return Pixels + (x + y * Width) * BytesPerPixel; } int GetSizeInBytes() const { return Width * Height * BytesPerPixel; } int GetPitch() const { return Width * BytesPerPixel; } - ImTextureRef GetTexRef() { ImTextureRef tex_ref; tex_ref._TexData = this; tex_ref._TexID = TexID; return tex_ref; } // FIXME-TEXREF + ImTextureRef GetTexRef() { ImTextureRef tex_ref; tex_ref._TexData = this; tex_ref._TexID = ImTextureID_Invalid; return tex_ref; } ImTextureID GetTexID() const { return TexID; } // Called by Renderer backend @@ -3761,6 +3761,7 @@ struct ImFont // We added an indirection to avoid patching ImDrawCmd after texture updates but this could be a solution too. inline ImTextureID ImTextureRef::GetTexID() const { + IM_ASSERT(!(_TexData != NULL && _TexID != ImTextureID_Invalid)); return _TexData ? _TexData->TexID : _TexID; } @@ -3768,7 +3769,7 @@ inline ImTextureID ImDrawCmd::GetTexID() const { // If you are getting this assert: A renderer backend with support for ImGuiBackendFlags_RendererHasTextures (1.92) // must iterate and handle ImTextureData requests stored in ImDrawData::Textures[]. - ImTextureID tex_id = TexRef._TexData ? TexRef._TexData->TexID : TexRef._TexID; + ImTextureID tex_id = TexRef._TexData ? TexRef._TexData->TexID : TexRef._TexID; // == TexRef.GetTexID() above. if (TexRef._TexData != NULL) IM_ASSERT(tex_id != ImTextureID_Invalid && "ImDrawCmd is referring to ImTextureData that wasn't uploaded to graphics system. Backend must call ImTextureData::SetTexID() after handling ImTextureStatus_WantCreate request!"); return tex_id; diff --git a/imgui_draw.cpp b/imgui_draw.cpp index fa15be25e..8ecc2ecdb 100644 --- a/imgui_draw.cpp +++ b/imgui_draw.cpp @@ -2615,7 +2615,6 @@ ImFontAtlas::ImFontAtlas() TexMaxWidth = 8192; TexMaxHeight = 8192; RendererHasTextures = false; // Assumed false by default, as apps can call e.g Atlas::Build() after backend init and before ImGui can update. - TexRef._TexData = NULL;// this; TexNextUniqueID = 1; FontNextUniqueID = 1; Builder = NULL; @@ -3882,9 +3881,8 @@ static void ImFontAtlasBuildSetTexture(ImFontAtlas* atlas, ImTextureData* tex) atlas->TexData = tex; atlas->TexUvScale = ImVec2(1.0f / tex->Width, 1.0f / tex->Height); atlas->TexRef._TexData = tex; - //atlas->TexID._TexID = tex->TexID; // <-- We intentionally don't do that and leave it 0, to allow late upload. - ImTextureRef new_tex_ref = atlas->TexRef; - ImFontAtlasUpdateDrawListsTextures(atlas, old_tex_ref, new_tex_ref); + //atlas->TexRef._TexID = tex->TexID; // <-- We intentionally don't do that. It would be misleading and betray promise that both fields aren't set. + ImFontAtlasUpdateDrawListsTextures(atlas, old_tex_ref, atlas->TexRef); } // Create a new texture, discard previous one