From 8ebb2845d6bf7f834284783359282f0237bf5595 Mon Sep 17 00:00:00 2001 From: Patrick Dawson Date: Mon, 19 Sep 2022 18:54:46 +0200 Subject: [PATCH] renderer fix: handle empty draw commands --- addons/imgui-godot/ImGuiGD.cs | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/addons/imgui-godot/ImGuiGD.cs b/addons/imgui-godot/ImGuiGD.cs index d6ed7e6..6bfd6e5 100644 --- a/addons/imgui-godot/ImGuiGD.cs +++ b/addons/imgui-godot/ImGuiGD.cs @@ -270,11 +270,17 @@ private static void UpdateKeyMods(ImGuiIOPtr io, InputEventKey k) private static void RenderDrawData(ImDrawDataPtr drawData, RID parent) { - // allocate and clear out our CanvasItem pool as needed + // allocate our CanvasItem pool as needed int neededNodes = 0; for (int i = 0; i < drawData.CmdListsCount; ++i) { - neededNodes += drawData.CmdListsRange[i].CmdBuffer.Size; + var cmdBuf = drawData.CmdListsRange[i].CmdBuffer; + neededNodes += cmdBuf.Size; + for (int j = 0; j < cmdBuf.Size; ++j) + { + if (cmdBuf[j].ElemCount == 0) + --neededNodes; + } } while (_children.Count < neededNodes) @@ -285,7 +291,7 @@ private static void RenderDrawData(ImDrawDataPtr drawData, RID parent) _children.Add(newChild); } - // trim unused nodes to reduce draw calls + // trim unused nodes while (_children.Count > neededNodes) { int idx = _children.Count - 1; @@ -324,10 +330,15 @@ private static void RenderDrawData(ImDrawDataPtr drawData, RID parent) uvs[i] = new Vector2(v.uv.X, v.uv.Y); } - for (int cmdi = 0; cmdi < cmdList.CmdBuffer.Size; ++cmdi, ++nodeN) + for (int cmdi = 0; cmdi < cmdList.CmdBuffer.Size; ++cmdi) { ImDrawCmdPtr drawCmd = cmdList.CmdBuffer[cmdi]; + if (drawCmd.ElemCount == 0) + { + continue; + } + var indices = new int[drawCmd.ElemCount]; int idxOffset = (int)drawCmd.IdxOffset; for (int i = idxOffset, j = 0; i < idxOffset + drawCmd.ElemCount; ++i, ++j) @@ -335,7 +346,7 @@ private static void RenderDrawData(ImDrawDataPtr drawData, RID parent) indices[j] = cmdList.IdxBuffer[i]; } - RID child = _children[nodeN]; + RID child = _children[nodeN++]; Texture2D tex = GetTexture(drawCmd.GetTexID()); RenderingServer.CanvasItemClear(child);