diff --git a/Content/woodgrain.png b/Content/woodgrain.png new file mode 100644 index 0000000..8f2aa11 Binary files /dev/null and b/Content/woodgrain.png differ diff --git a/lib/MoonWorks b/lib/MoonWorks index c4d2e3b..40d9cdd 160000 --- a/lib/MoonWorks +++ b/lib/MoonWorks @@ -1 +1 @@ -Subproject commit c4d2e3b8eef88d2f90a12f3fa7a33e50a8fb0b8c +Subproject commit 40d9cdd33a3d5bb0a9d2947c77c4d6c5b864b78b diff --git a/src/MoonWorksImguiExampleGame.cs b/src/MoonWorksImguiExampleGame.cs index 8e4962e..ac826c0 100644 --- a/src/MoonWorksImguiExampleGame.cs +++ b/src/MoonWorksImguiExampleGame.cs @@ -28,27 +28,17 @@ namespace MoonWorksImGuiExample private ShaderModule ImGuiFragmentShader; private Sampler ImGuiSampler; + private Texture WoodGrainTexture; + private System.IntPtr WoodGrainTexturePtr; + + private System.Numerics.Vector3 ExampleColor; + public MoonWorksImGuiExampleGame( WindowCreateInfo windowCreateInfo, PresentMode presentMode, bool 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")); ImGuiFragmentShader = new ShaderModule(GraphicsDevice, Path.Combine(ContentPath, "ImGuiFrag.spv")); ImGuiSampler = new Sampler(GraphicsDevice, SamplerCreateInfo.PointClamp); @@ -78,6 +68,27 @@ namespace MoonWorksImGuiExample 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) @@ -90,6 +101,7 @@ namespace MoonWorksImGuiExample var id = new System.IntPtr(TextureID); LoadedTextures.Add(id, texture); TextureHandles.Add(texture, id); + TextureID += 1; return id; } @@ -175,18 +187,18 @@ namespace MoonWorksImGuiExample { var cmdList = drawDataPtr.CmdListsRange[n]; - commandBuffer.SetBufferData( + commandBuffer.SetBufferData( ImGuiVertexBuffer, cmdList.VtxBuffer.Data, vertexOffset, - (uint) (cmdList.VtxBuffer.Size * vertexSize) + (uint) cmdList.VtxBuffer.Size ); - commandBuffer.SetBufferData( + commandBuffer.SetBufferData( ImGuiIndexBuffer, cmdList.IdxBuffer.Data, indexOffset, - (uint) cmdList.IdxBuffer.Size * sizeof(ushort) + (uint) cmdList.IdxBuffer.Size ); vertexOffset += (uint) cmdList.VtxBuffer.Size; @@ -196,6 +208,63 @@ namespace MoonWorksImGuiExample 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) { // Replace this with your own drawing code. @@ -242,64 +311,19 @@ namespace MoonWorksImGuiExample 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(); var drawDataPtr = ImGui.GetDrawData(); UpdateImGuiBuffers(drawDataPtr); - - 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); + RenderCommandLists(drawDataPtr, io); } protected override void OnDestroy()