ソースを参照

Fonts: detect if ImFontAtlasUpdateNewFrame() is not being called.

ocornut 3 ヶ月 前
コミット
5926c877a1
3 ファイル変更16 行追加5 行削除
  1. 10 2
      imgui.cpp
  2. 5 2
      imgui_draw.cpp
  3. 1 1
      imgui_internal.h

+ 10 - 2
imgui.cpp

@@ -5211,16 +5211,24 @@ void ImGui::UpdateHoveredWindowAndCaptureFlags(const ImVec2& mouse_pos)
     io.WantTextInput = (g.WantTextInputNextFrame != -1) ? (g.WantTextInputNextFrame != 0) : false;
     io.WantTextInput = (g.WantTextInputNextFrame != -1) ? (g.WantTextInputNextFrame != 0) : false;
 }
 }
 
 
-// FIXME-NEWATLAS-V2: If we aim to support multiple atlases used by same context: how to reach/target all atlases?
 static void ImGui::UpdateTexturesNewFrame()
 static void ImGui::UpdateTexturesNewFrame()
 {
 {
+    // Cannot update every atlases based on atlas's FrameCount < g.FrameCount, because an atlas may be shared by multiple contexts with different frame count.
     ImGuiContext& g = *GImGui;
     ImGuiContext& g = *GImGui;
+    const bool has_textures = (g.IO.BackendFlags & ImGuiBackendFlags_RendererHasTextures) != 0;
     for (ImFontAtlas* atlas : g.FontAtlases)
     for (ImFontAtlas* atlas : g.FontAtlases)
+    {
         if (atlas->OwnerContext == &g)
         if (atlas->OwnerContext == &g)
         {
         {
-            atlas->RendererHasTextures = (g.IO.BackendFlags & ImGuiBackendFlags_RendererHasTextures) != 0;
+            atlas->RendererHasTextures = has_textures;
             ImFontAtlasUpdateNewFrame(atlas, g.FrameCount);
             ImFontAtlasUpdateNewFrame(atlas, g.FrameCount);
         }
         }
+        else
+        {
+            IM_ASSERT(atlas->Builder != NULL && atlas->Builder->FrameCount != -1 && "If you manage font atlases yourself you need to call ImFontAtlasUpdateNewFrame() on it.");
+            IM_ASSERT(atlas->RendererHasTextures == has_textures && "If you manage font atlases yourself make sure atlas->RendererHasTextures is set consistently with all contexts using it.");
+        }
+    }
 }
 }
 
 
 // Build a single texture list
 // Build a single texture list

+ 5 - 2
imgui_draw.cpp

@@ -2715,11 +2715,14 @@ static void ImFontAtlasBuildUpdateRendererHasTexturesFromContext(ImFontAtlas* at
         }
         }
 }
 }
 
 
-// Called by NewFrame(). When multiple context own the atlas, only the first one calls this.
-// If you are calling this yourself, ensure atlas->RendererHasTextures is set.
+// Called by NewFrame() for atlases owned by a context.
+// If you manually manage font atlases, you'll need to call this yourself + ensure atlas->RendererHasTextures is set.
 // 'frame_count' needs to be provided because we can gc/prioritize baked fonts based on their age.
 // 'frame_count' needs to be provided because we can gc/prioritize baked fonts based on their age.
+// 'frame_count' may not match those of imgui contexts using this atlas, as contexts may be updated as different frequencies.
 void ImFontAtlasUpdateNewFrame(ImFontAtlas* atlas, int frame_count)
 void ImFontAtlasUpdateNewFrame(ImFontAtlas* atlas, int frame_count)
 {
 {
+    IM_ASSERT(atlas->Builder == NULL || atlas->Builder->FrameCount < frame_count); // Protection against being called twice?
+
     // Check that font atlas was built or backend support texture reload in which case we can build now
     // Check that font atlas was built or backend support texture reload in which case we can build now
     if (atlas->RendererHasTextures)
     if (atlas->RendererHasTextures)
     {
     {

+ 1 - 1
imgui_internal.h

@@ -3787,7 +3787,7 @@ struct ImFontAtlasBuilder
     ImFontAtlasRectId           PackIdMouseCursors;     // White pixel + mouse cursors. Also happen to be fallback in case of packing failure.
     ImFontAtlasRectId           PackIdMouseCursors;     // White pixel + mouse cursors. Also happen to be fallback in case of packing failure.
     ImFontAtlasRectId           PackIdLinesTexData;
     ImFontAtlasRectId           PackIdLinesTexData;
 
 
-    ImFontAtlasBuilder()        { memset(this, 0, sizeof(*this)); RectsIndexFreeListStart = -1; PackIdMouseCursors = PackIdLinesTexData = -1; }
+    ImFontAtlasBuilder()        { memset(this, 0, sizeof(*this)); FrameCount = -1; RectsIndexFreeListStart = -1; PackIdMouseCursors = PackIdLinesTexData = -1; }
 };
 };
 
 
 IMGUI_API void              ImFontAtlasBuildInit(ImFontAtlas* atlas);
 IMGUI_API void              ImFontAtlasBuildInit(ImFontAtlas* atlas);