Skip to content

Commit

Permalink
better handling of empty draw data
Browse files Browse the repository at this point in the history
  • Loading branch information
pkdawson committed Nov 30, 2022
1 parent 152b9b1 commit 84ac175
Showing 1 changed file with 56 additions and 49 deletions.
105 changes: 56 additions & 49 deletions addons/imgui-godot/ImGuiGodot/InternalRdRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,55 +164,9 @@ public void CloseViewport(Viewport vp)
{
}

public void RenderDrawData(Viewport vp, ImDrawDataPtr drawData)
private void SetupBuffers(ImDrawDataPtr drawData)
{
#if IMGUI_GODOT_DEV
RD.DrawCommandBeginLabel("ImGui", Colors.Purple);
#endif
RID fb = GetFramebuffer(vp);

int vertSize = Marshal.SizeOf<ImDrawVert>();

var window = (GodotImGuiWindow)GCHandle.FromIntPtr(drawData.OwnerViewport.PlatformHandle).Target;
Transform2D transform = window.Xform;
if (transform == Transform2D.Identity)
transform = Transform2D.Identity.Translated(window.GetWindowPos()).Inverse(); ;

_scale[0] = 2.0f / drawData.DisplaySize.X;
_scale[1] = 2.0f / drawData.DisplaySize.Y;

_translate[0] = -1.0f - (drawData.DisplayPos.X * _scale[0]);
_translate[1] = -1.0f - (drawData.DisplayPos.Y * _scale[1]);

Buffer.BlockCopy(_scale, 0, _pcbuf, 0, 8);
Buffer.BlockCopy(_translate, 0, _pcbuf, 8, 8);

// allocate merged index and vertex buffers
if (_idxBufferSize < drawData.TotalIdxCount)
{
if (_idxBuffer.Id != 0)
RD.FreeRid(_idxBuffer);
_idxBuffer = RD.IndexBufferCreate((uint)drawData.TotalIdxCount, RenderingDevice.IndexBufferFormat.Uint16);
_idxBufferSize = drawData.TotalIdxCount;
}

if (_vtxBufferSize < drawData.TotalVtxCount)
{
if (_vtxBuffer.Id != 0)
RD.FreeRid(_vtxBuffer);
_vtxBuffer = RD.VertexBufferCreate((uint)(drawData.TotalVtxCount * vertSize));
_vtxBufferSize = drawData.TotalVtxCount;
}

_usedTextures.Clear();

// check if our font texture is still valid
foreach (var kv in _uniformSets)
{
if (!RD.UniformSetIsValid(kv.Value))
_uniformSets.Remove(kv.Key);
}

int globalIdxOffset = 0;
int globalVtxOffset = 0;

Expand Down Expand Up @@ -262,6 +216,59 @@ public void RenderDrawData(Viewport vp, ImDrawDataPtr drawData)
_bufPool.Return(idxBuf);
RD.BufferUpdate(_vtxBuffer, 0, (uint)vertBufSize, vertBuf);
_bufPool.Return(vertBuf);
}

public void RenderDrawData(Viewport vp, ImDrawDataPtr drawData)
{
#if IMGUI_GODOT_DEV
RD.DrawCommandBeginLabel("ImGui", Colors.Purple);
#endif
RID fb = GetFramebuffer(vp);

int vertSize = Marshal.SizeOf<ImDrawVert>();

var window = (GodotImGuiWindow)GCHandle.FromIntPtr(drawData.OwnerViewport.PlatformHandle).Target;
Transform2D transform = window.Xform;
if (transform == Transform2D.Identity)
transform = Transform2D.Identity.Translated(window.GetWindowPos()).Inverse(); ;

_scale[0] = 2.0f / drawData.DisplaySize.X;
_scale[1] = 2.0f / drawData.DisplaySize.Y;

_translate[0] = -1.0f - (drawData.DisplayPos.X * _scale[0]);
_translate[1] = -1.0f - (drawData.DisplayPos.Y * _scale[1]);

Buffer.BlockCopy(_scale, 0, _pcbuf, 0, 8);
Buffer.BlockCopy(_translate, 0, _pcbuf, 8, 8);

// allocate merged index and vertex buffers
if (_idxBufferSize < drawData.TotalIdxCount)
{
if (_idxBuffer.Id != 0)
RD.FreeRid(_idxBuffer);
_idxBuffer = RD.IndexBufferCreate((uint)drawData.TotalIdxCount, RenderingDevice.IndexBufferFormat.Uint16);
_idxBufferSize = drawData.TotalIdxCount;
}

if (_vtxBufferSize < drawData.TotalVtxCount)
{
if (_vtxBuffer.Id != 0)
RD.FreeRid(_vtxBuffer);
_vtxBuffer = RD.VertexBufferCreate((uint)(drawData.TotalVtxCount * vertSize));
_vtxBufferSize = drawData.TotalVtxCount;
}

_usedTextures.Clear();

// check if our font texture is still valid
foreach (var kv in _uniformSets)
{
if (!RD.UniformSetIsValid(kv.Value))
_uniformSets.Remove(kv.Key);
}

if (drawData.CmdListsCount > 0)
SetupBuffers(drawData);

// draw
var dl = RD.DrawListBegin(fb,
Expand All @@ -271,8 +278,8 @@ public void RenderDrawData(Viewport vp, ImDrawDataPtr drawData)
RD.DrawListBindRenderPipeline(dl, _pipeline);
RD.DrawListSetPushConstant(dl, _pcbuf, (uint)_pcbuf.Length);

globalIdxOffset = 0;
globalVtxOffset = 0;
int globalIdxOffset = 0;
int globalVtxOffset = 0;
for (int i = 0; i < drawData.CmdListsCount; ++i)
{
ImDrawListPtr cmdList = drawData.CmdListsRange[i];
Expand Down

0 comments on commit 84ac175

Please sign in to comment.