Fonts: avoid both ImTextureRef fields being set simultaneously.

This commit is contained in:
ocornut
2025-03-19 15:00:13 +01:00
parent 0fff7ceda4
commit a548cd9934
2 changed files with 5 additions and 6 deletions

View File

@@ -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;

View File

@@ -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