Skip to content

Commit

Permalink
renderer fix: handle empty draw commands
Browse files Browse the repository at this point in the history
  • Loading branch information
pkdawson committed Sep 19, 2022
1 parent c9f1ac0 commit 8ebb284
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions addons/imgui-godot/ImGuiGD.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
Expand Down Expand Up @@ -324,18 +330,23 @@ 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)
{
indices[j] = cmdList.IdxBuffer[i];
}

RID child = _children[nodeN];
RID child = _children[nodeN++];

Texture2D tex = GetTexture(drawCmd.GetTexID());
RenderingServer.CanvasItemClear(child);
Expand Down

0 comments on commit 8ebb284

Please sign in to comment.