Skip to content

Commit

Permalink
set up for viewport debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
pkdawson committed Nov 29, 2022
1 parent 3e8008f commit c5ddc08
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 29 deletions.
3 changes: 2 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ csharp_style_namespace_declarations = file_scoped
dotnet_style_allow_multiple_blank_lines_experimental = false
csharp_style_allow_blank_lines_between_consecutive_braces_experimental = false

dotnet_analyzer_diagnostic.severity = warning

# don't add _ discards for return values
dotnet_diagnostic.IDE0058.severity = none

Expand All @@ -28,7 +30,6 @@ dotnet_diagnostic.IDE0022.severity = none
dotnet_diagnostic.IDE0010.severity = none
dotnet_diagnostic.IDE0072.severity = none

dotnet_analyzer_diagnostic.severity = warning
dotnet_diagnostic.CA1805.severity = none
dotnet_diagnostic.CA1051.severity = none
dotnet_diagnostic.CA1001.severity = none
Expand Down
25 changes: 6 additions & 19 deletions addons/imgui-godot/ImGuiGodot/ImGuiLayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
using ImGuiNET;
using System;
using System.Collections.Generic;
#if IMGUI_GODOT_DEV
using System.Runtime.InteropServices;
#endif

namespace ImGuiGodot;

Expand Down Expand Up @@ -111,27 +114,9 @@ public override void _EnterTree()
}
ImGuiGD.RebuildFontAtlas();

_subViewportContainer = new SubViewportContainer
{
Name = "ImGuiLayer_SubViewportContainer",
AnchorsPreset = 15,
MouseFilter = Control.MouseFilterEnum.Ignore,
Stretch = true
};

_subViewport = new SubViewport
{
Name = "ImGuiLayer_SubViewport",
TransparentBg = true,
HandleInputLocally = false,
GuiDisableInput = true,
RenderTargetUpdateMode = SubViewport.UpdateMode.Always
};

_subViewportContainer.AddChild(_subViewport);
Internal.AddLayerSubViewport(this, out _subViewportContainer, out _subViewport);

Internal.Renderer.InitViewport(_subViewport);
AddChild(_subViewportContainer);

_updateFirst = new UpdateFirst
{
Expand Down Expand Up @@ -230,6 +215,7 @@ public static void Connect(ImGuiLayoutEventHandler d)
}

#if IMGUI_GODOT_DEV
#pragma warning disable CA1822 // Mark members as static
// WIP, this will probably be changed or moved
public long[] GetImGuiPtrs(string version, int ioSize, int vertSize, int idxSize)
{
Expand All @@ -255,6 +241,7 @@ public long[] GetImGuiPtrs(string version, int ioSize, int vertSize, int idxSize
(long)mem_free
};
}
#pragma warning restore CA1822 // Mark members as static
#endif

private static void OnNodeRemoved(Node node)
Expand Down
23 changes: 23 additions & 0 deletions addons/imgui-godot/ImGuiGodot/Internal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,29 @@ public static void ProcessNotification(long what)
};
}

public static void AddLayerSubViewport(Node parent, out SubViewportContainer subViewportContainer, out SubViewport subViewport)
{
subViewportContainer = new SubViewportContainer
{
Name = "ImGuiLayer_SubViewportContainer",
AnchorsPreset = 15,
MouseFilter = Control.MouseFilterEnum.Ignore,
Stretch = true
};

subViewport = new SubViewport
{
Name = "ImGuiLayer_SubViewport",
TransparentBg = true,
HandleInputLocally = false,
GuiDisableInput = true,
RenderTargetUpdateMode = SubViewport.UpdateMode.Always
};

subViewportContainer.AddChild(subViewport);
parent.AddChild(subViewportContainer);
}

public static void Render(Viewport vp)
{
ImGui.Render();
Expand Down
5 changes: 5 additions & 0 deletions addons/imgui-godot/ImGuiGodot/InternalCanvasRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ private class ViewportData
public void Init(ImGuiIOPtr io)
{
io.BackendFlags |= ImGuiBackendFlags.RendererHasVtxOffset;
#if IMGUI_GODOT_DEV
io.BackendFlags |= ImGuiBackendFlags.RendererHasViewports;
#endif
}

public void InitViewport(Viewport vp)
Expand All @@ -32,6 +35,8 @@ public void InitViewport(Viewport vp)
RenderingServer.ViewportAttachCanvas(vprid, canvas);
RenderingServer.CanvasItemSetParent(canvasItem, canvas);

RenderingServer.CanvasItemAddRect(canvasItem, new(0, 0, 32, 32), Colors.Red);

_vpData[vprid] = new ViewportData()
{
Canvas = canvas,
Expand Down
5 changes: 5 additions & 0 deletions addons/imgui-godot/ImGuiGodot/InternalRdRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public InternalRdRenderer()
_shader = RD.ShaderCreateFromSpirv(spirv);

#if IMGUI_GODOT_DEV
#pragma warning disable CA2201
using var src = new RDShaderSource()
{
SourceFragment = _fragmentShaderSource,
Expand All @@ -58,6 +59,7 @@ public InternalRdRenderer()
throw new Exception("fragment bytecode mismatch");
if (!System.Linq.Enumerable.SequenceEqual(spirv.BytecodeVertex, freshSpirv.BytecodeVertex))
throw new Exception("vertex bytecode mismatch");
#pragma warning restore CA2201
#endif

// create vertex format
Expand Down Expand Up @@ -146,6 +148,9 @@ public InternalRdRenderer()
public void Init(ImGuiIOPtr io)
{
io.BackendFlags |= ImGuiBackendFlags.RendererHasVtxOffset;
#if IMGUI_GODOT_DEV
io.BackendFlags |= ImGuiBackendFlags.RendererHasViewports;
#endif
}

public void InitViewport(Viewport vp)
Expand Down
17 changes: 8 additions & 9 deletions addons/imgui-godot/ImGuiGodot/InternalViewports.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ private class GodotImGuiWindow : IDisposable

private bool _focused;
public Window GodotWindow { get; init; }
public SubViewport LayerSvp { get; init; }

public GodotImGuiWindow(ImGuiViewportPtr vp)
{
Expand Down Expand Up @@ -110,10 +111,11 @@ public GodotImGuiWindow(ImGuiViewportPtr vp)
// need to do this after AddChild
GodotWindow.Transparent = true;

Internal.Renderer.InitViewport(GodotWindow);
RenderingServer.ViewportSetTransparentBackground(GodotWindow.GetViewportRid(), true);
Internal.AddLayerSubViewport(GodotWindow, out SubViewportContainer svpContainer, out SubViewport svp);
LayerSvp = svp;

//RenderingServer.CanvasItemAddRect(RootCanvasItem, new(0, 0, 100, 100), Colors.Red);
Internal.Renderer.InitViewport(LayerSvp);
RenderingServer.ViewportSetTransparentBackground(GodotWindow.GetViewportRid(), true);
}

public GodotImGuiWindow(ImGuiViewportPtr vp, Window gw)
Expand All @@ -128,8 +130,6 @@ public void Dispose()
{
if (GodotWindow.GetParent() != null)
{
//RenderingServer.FreeRid(RootCanvasItem);
//RenderingServer.FreeRid(CanvasRid);
GodotWindow.QueueFree();
}
_gcHandle.Free();
Expand Down Expand Up @@ -238,9 +238,8 @@ private static unsafe void InitPlatformInterface()
public static void Init(ImGuiIOPtr io)
{
io.BackendFlags |= ImGuiBackendFlags.PlatformHasViewports;
io.BackendFlags |= ImGuiBackendFlags.RendererHasViewports;

// io.ConfigFlags |= ImGuiConfigFlags.ViewportsEnable;
io.ConfigFlags |= ImGuiConfigFlags.ViewportsEnable;

_mainWindow = new(ImGui.GetMainViewport(), ImGuiLayer.Instance.GetViewport() as Window);

Expand All @@ -250,7 +249,7 @@ public static void Init(ImGuiIOPtr io)

private static void Godot_CreateWindow(ImGuiViewportPtr vp)
{
new GodotImGuiWindow(vp);
_ = new GodotImGuiWindow(vp);
}

private static void Godot_DestroyWindow(ImGuiViewportPtr vp)
Expand Down Expand Up @@ -324,7 +323,7 @@ public static void RenderViewports()
{
var vp = pio.Viewports[i];
var window = (GodotImGuiWindow)GCHandle.FromIntPtr(vp.PlatformHandle).Target;
Internal.Renderer.RenderDrawData(window.GodotWindow, vp.DrawData);
Internal.Renderer.RenderDrawData(window.LayerSvp, vp.DrawData);
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/MyNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ public override void _Ready()

public override void _Process(double delta)
{
#if IMGUI_GODOT_DEV
ImGui.SetNextWindowPos(new(100, 100));
ImGui.SetNextWindowSize(new(200, 200));
ImGui.Begin("test");
ImGui.Text("some text");
ImGui.End();
#endif

ImGui.ShowDemoWindow();
}

Expand Down

0 comments on commit c5ddc08

Please sign in to comment.