a bit more example usage

main
cosmonaut 2022-03-04 10:03:41 -08:00
parent c899d6d976
commit 319498cd5d
3 changed files with 98 additions and 74 deletions

BIN
Content/woodgrain.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 MiB

@ -1 +1 @@
Subproject commit c4d2e3b8eef88d2f90a12f3fa7a33e50a8fb0b8c Subproject commit 40d9cdd33a3d5bb0a9d2947c77c4d6c5b864b78b

View File

@ -28,27 +28,17 @@ namespace MoonWorksImGuiExample
private ShaderModule ImGuiFragmentShader; private ShaderModule ImGuiFragmentShader;
private Sampler ImGuiSampler; private Sampler ImGuiSampler;
private Texture WoodGrainTexture;
private System.IntPtr WoodGrainTexturePtr;
private System.Numerics.Vector3 ExampleColor;
public MoonWorksImGuiExampleGame( public MoonWorksImGuiExampleGame(
WindowCreateInfo windowCreateInfo, WindowCreateInfo windowCreateInfo,
PresentMode presentMode, PresentMode presentMode,
bool debugMode bool debugMode
) : base(windowCreateInfo, presentMode, 60, debugMode) ) : base(windowCreateInfo, presentMode, 60, debugMode)
{ {
var context = ImGui.CreateContext();
ImGui.SetCurrentContext(context);
var io = ImGui.GetIO();
io.Fonts.AddFontDefault();
RebuildFontAtlas();
io.DisplaySize = new System.Numerics.Vector2(
windowCreateInfo.WindowWidth,
windowCreateInfo.WindowHeight
);
io.DisplayFramebufferScale = System.Numerics.Vector2.One;
ImGuiVertexShader = new ShaderModule(GraphicsDevice, Path.Combine(ContentPath, "ImGuiVert.spv")); ImGuiVertexShader = new ShaderModule(GraphicsDevice, Path.Combine(ContentPath, "ImGuiVert.spv"));
ImGuiFragmentShader = new ShaderModule(GraphicsDevice, Path.Combine(ContentPath, "ImGuiFrag.spv")); ImGuiFragmentShader = new ShaderModule(GraphicsDevice, Path.Combine(ContentPath, "ImGuiFrag.spv"));
ImGuiSampler = new Sampler(GraphicsDevice, SamplerCreateInfo.PointClamp); ImGuiSampler = new Sampler(GraphicsDevice, SamplerCreateInfo.PointClamp);
@ -78,6 +68,27 @@ namespace MoonWorksImGuiExample
MultisampleState = MultisampleState.None MultisampleState = MultisampleState.None
} }
); );
var commandBuffer = GraphicsDevice.AcquireCommandBuffer();
WoodGrainTexture = Texture.LoadPNG(GraphicsDevice, commandBuffer, Path.Combine(ContentPath, "woodgrain.png"));
GraphicsDevice.Submit(commandBuffer);
var context = ImGui.CreateContext();
ImGui.SetCurrentContext(context);
var io = ImGui.GetIO();
io.Fonts.AddFontDefault();
RebuildFontAtlas();
WoodGrainTexturePtr = BindTexture(WoodGrainTexture);
io.DisplaySize = new System.Numerics.Vector2(
windowCreateInfo.WindowWidth,
windowCreateInfo.WindowHeight
);
io.DisplayFramebufferScale = System.Numerics.Vector2.One;
} }
protected override void Update(System.TimeSpan dt) protected override void Update(System.TimeSpan dt)
@ -90,6 +101,7 @@ namespace MoonWorksImGuiExample
var id = new System.IntPtr(TextureID); var id = new System.IntPtr(TextureID);
LoadedTextures.Add(id, texture); LoadedTextures.Add(id, texture);
TextureHandles.Add(texture, id); TextureHandles.Add(texture, id);
TextureID += 1;
return id; return id;
} }
@ -175,18 +187,18 @@ namespace MoonWorksImGuiExample
{ {
var cmdList = drawDataPtr.CmdListsRange[n]; var cmdList = drawDataPtr.CmdListsRange[n];
commandBuffer.SetBufferData( commandBuffer.SetBufferData<VertexPositionTextureColor>(
ImGuiVertexBuffer, ImGuiVertexBuffer,
cmdList.VtxBuffer.Data, cmdList.VtxBuffer.Data,
vertexOffset, vertexOffset,
(uint) (cmdList.VtxBuffer.Size * vertexSize) (uint) cmdList.VtxBuffer.Size
); );
commandBuffer.SetBufferData( commandBuffer.SetBufferData<ushort>(
ImGuiIndexBuffer, ImGuiIndexBuffer,
cmdList.IdxBuffer.Data, cmdList.IdxBuffer.Data,
indexOffset, indexOffset,
(uint) cmdList.IdxBuffer.Size * sizeof(ushort) (uint) cmdList.IdxBuffer.Size
); );
vertexOffset += (uint) cmdList.VtxBuffer.Size; vertexOffset += (uint) cmdList.VtxBuffer.Size;
@ -196,6 +208,63 @@ namespace MoonWorksImGuiExample
GraphicsDevice.Submit(commandBuffer); GraphicsDevice.Submit(commandBuffer);
} }
private void RenderCommandLists(ImDrawDataPtr drawDataPtr, ImGuiIOPtr ioPtr)
{
if (ImGuiVertexBuffer == null) { return; }
var commandBuffer = GraphicsDevice.AcquireCommandBuffer();
var swapchainTexture = commandBuffer.AcquireSwapchainTexture(Window);
if (swapchainTexture != null)
{
commandBuffer.BeginRenderPass(
new ColorAttachmentInfo(swapchainTexture, Color.Transparent)
);
commandBuffer.BindGraphicsPipeline(ImGuiPipeline);
var vertexUniformOffset = commandBuffer.PushVertexShaderUniforms(
Matrix4x4.CreateOrthographicOffCenter(0, ioPtr.DisplaySize.X, ioPtr.DisplaySize.Y, 0, -1, 1)
);
commandBuffer.BindVertexBuffers(ImGuiVertexBuffer);
commandBuffer.BindIndexBuffer(ImGuiIndexBuffer, IndexElementSize.Sixteen);
uint vertexOffset = 0;
uint indexOffset = 0;
for (int n = 0; n < drawDataPtr.CmdListsCount; n += 1)
{
var cmdList = drawDataPtr.CmdListsRange[n];
for (int cmdIndex = 0; cmdIndex < cmdList.CmdBuffer.Size; cmdIndex += 1)
{
var drawCmd = cmdList.CmdBuffer[cmdIndex];
commandBuffer.BindFragmentSamplers(
new TextureSamplerBinding(LoadedTextures[drawCmd.TextureId], ImGuiSampler)
);
commandBuffer.DrawIndexedPrimitives(
vertexOffset,
indexOffset,
drawCmd.ElemCount / 3,
vertexUniformOffset,
0
);
indexOffset += drawCmd.ElemCount;
}
vertexOffset += (uint) cmdList.VtxBuffer.Size;
}
commandBuffer.EndRenderPass();
}
GraphicsDevice.Submit(commandBuffer);
}
protected override void Draw(System.TimeSpan dt, double alpha) protected override void Draw(System.TimeSpan dt, double alpha)
{ {
// Replace this with your own drawing code. // Replace this with your own drawing code.
@ -242,64 +311,19 @@ namespace MoonWorksImGuiExample
ImGui.End(); ImGui.End();
ImGui.Begin("Texture", ImGuiWindowFlags.AlwaysAutoResize);
ImGui.Image(WoodGrainTexturePtr, new System.Numerics.Vector2(400, 400));
ImGui.End();
ImGui.Begin("Color Picker", ImGuiWindowFlags.AlwaysAutoResize);
ImGui.ColorPicker3("Color", ref ExampleColor);
ImGui.End();
ImGui.Render(); ImGui.Render();
var drawDataPtr = ImGui.GetDrawData(); var drawDataPtr = ImGui.GetDrawData();
UpdateImGuiBuffers(drawDataPtr); UpdateImGuiBuffers(drawDataPtr);
RenderCommandLists(drawDataPtr, io);
if (ImGuiVertexBuffer == null) { return; }
var commandBuffer = GraphicsDevice.AcquireCommandBuffer();
var swapchainTexture = commandBuffer.AcquireSwapchainTexture(Window);
if (swapchainTexture != null)
{
commandBuffer.BeginRenderPass(
new ColorAttachmentInfo(swapchainTexture, Color.Transparent)
);
commandBuffer.BindGraphicsPipeline(ImGuiPipeline);
var vertexUniformOffset = commandBuffer.PushVertexShaderUniforms(
Matrix4x4.CreateOrthographicOffCenter(0, io.DisplaySize.X, io.DisplaySize.Y, 0, -1, 1)
);
commandBuffer.BindVertexBuffers(ImGuiVertexBuffer);
commandBuffer.BindIndexBuffer(ImGuiIndexBuffer, IndexElementSize.Sixteen);
uint vertexOffset = 0;
uint indexOffset = 0;
for (int n = 0; n < drawDataPtr.CmdListsCount; n += 1)
{
var cmdList = drawDataPtr.CmdListsRange[n];
for (int cmdIndex = 0; cmdIndex < cmdList.CmdBuffer.Size; cmdIndex += 1)
{
var drawCmd = cmdList.CmdBuffer[cmdIndex];
commandBuffer.BindFragmentSamplers(
new TextureSamplerBinding(LoadedTextures[drawCmd.TextureId], ImGuiSampler)
);
commandBuffer.DrawIndexedPrimitives(
vertexOffset,
indexOffset,
drawCmd.ElemCount / 3,
vertexUniformOffset,
0
);
indexOffset += drawCmd.ElemCount;
}
vertexOffset += (uint) cmdList.VtxBuffer.Size;
}
commandBuffer.EndRenderPass();
}
GraphicsDevice.Submit(commandBuffer);
} }
protected override void OnDestroy() protected override void OnDestroy()