Browse Source

Textures: Single Textures[] array allows backend to not have to care about atlases.

# Conflicts:
#	imgui.h
ocornut 7 months ago
parent
commit
a21a2e855b
2 changed files with 25 additions and 5 deletions
  1. 15 0
      imgui.cpp
  2. 10 5
      imgui.h

+ 15 - 0
imgui.cpp

@@ -1271,6 +1271,7 @@ static void             UpdateKeyRoutingTable(ImGuiKeyRoutingTable* rt);
 // Misc
 static void             UpdateFontsNewFrame();
 static void             UpdateTexturesNewFrame();
+static void             UpdateTexturesEndFrame();
 static void             UpdateSettings();
 static int              UpdateWindowManualResize(ImGuiWindow* window, const ImVec2& size_auto_fit, int* border_hovered, int* border_held, int resize_grip_count, ImU32 resize_grip_col[4], const ImRect& visibility_rect);
 static void             RenderWindowOuterBorders(ImGuiWindow* window);
@@ -5182,6 +5183,18 @@ static void ImGui::UpdateTexturesNewFrame()
     ImFontAtlasUpdateNewFrame(atlas);
 }
 
+// Build a single texture list
+// We want to avoid user reading from atlas->TexList[] in order to facilitate better support for multiple atlases.
+static void ImGui::UpdateTexturesEndFrame()
+{
+    ImGuiContext& g = *GImGui;
+    ImFontAtlas* atlas = g.IO.Fonts;
+    g.PlatformIO.Textures.resize(0);
+    g.PlatformIO.Textures.reserve(atlas->TexList.Size);
+    for (ImTextureData* tex : atlas->TexList)
+        g.PlatformIO.Textures.push_back(tex);
+}
+
 // Called once a frame. Followed by SetCurrentFont() which sets up the remaining data.
 // FIXME-VIEWPORT: the concept of a single ClipRectFullscreen is not ideal!
 static void SetupDrawListSharedData()
@@ -5759,6 +5772,8 @@ void ImGui::EndFrame()
     g.Windows.swap(g.WindowsTempSortBuffer);
     g.IO.MetricsActiveWindows = g.WindowsActiveCount;
 
+    UpdateTexturesEndFrame();
+
     // Unlock font atlas
     g.IO.Fonts->Locked = false;
 

+ 10 - 5
imgui.h

@@ -3496,7 +3496,7 @@ enum ImFontAtlasFlags_
 //  - Mouse cursor shapes for software cursor rendering (unless setting 'Flags |= ImFontAtlasFlags_NoMouseCursors' in the font atlas).
 //  - If you don't call any AddFont*** functions, the default font embedded in the code will be loaded for you.
 // It is the rendering backend responsibility to upload texture into your graphics API:
-//  - ImGui_ImplXXXX_RenderDrawData() functions generally iterate atlas->TexList[] to create/update/destroy each ImTextureData instance.
+//  - ImGui_ImplXXXX_RenderDrawData() functions generally iterate platform_io->Textures[] to create/update/destroy each ImTextureData instance.
 //  - Backend then set ImTextureData's TexID and BackendUserData.
 //  - Texture id are passed back to you during rendering to identify the texture. Read FAQ entry about ImTextureID for more details.
 // Legacy path:
@@ -3597,11 +3597,9 @@ struct ImFontAtlas
     int                         TexGlyphPadding;    // FIXME: Should be called "TexPackPadding". Padding between glyphs within texture in pixels. Defaults to 1. If your rendering method doesn't rely on bilinear filtering you may set this to 0 (will also need to set AntiAliasedLinesUseTex = false).
     void*                       UserData;           // Store your own atlas related user-data (if e.g. you have multiple font atlas).
 
-    // Output
-    ImTextureData*              TexData;            // Current texture
-    ImVector<ImTextureData*>    TexList;            // Texture list (most often TexList.Size == 1). TexData is always == TexList.back().
-
     // [Internal]
+    ImTextureData*              TexData;            // Current texture
+    ImVector<ImTextureData*>    TexList;            // Texture list (most often TexList.Size == 1). TexData is always == TexList.back(). DO NOT USE DIRECTLY, USE GetPlatformIO().Textures[] instead!
     bool                        Locked;             // Marked as Locked by ImGui::NewFrame() so attempt to modify the atlas will assert.
     bool                        TexIsBuilt;         // Set when texture was built matching current font input
     bool                        TexPixelsUseColors; // Tell whether our texture data is known to use colors (rather than just alpha channel), in order to help backend select a format or conversion process.
@@ -3788,6 +3786,13 @@ struct ImGuiPlatformIO
 
     // Written by some backends during ImGui_ImplXXXX_RenderDrawData() call to point backend_specific ImGui_ImplXXXX_RenderState* structure.
     void*       Renderer_RenderState;
+
+    //------------------------------------------------------------------
+    // Output
+    //------------------------------------------------------------------
+
+    // Textures list (the list is updated by calling ImGui::EndFrame or ImGui::Render)
+    ImVector<ImTextureData*>        Textures;           // Texture list (most often Textures.Size == 1).
 };
 
 // (Optional) Support for IME (Input Method Editor) via the platform_io.Platform_SetImeDataFn() function. Handler is called during EndFrame().