From 83bdf69e64311cb1623195daf64b805dd9c462be Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Thu, 6 Jun 2024 13:09:45 -0700 Subject: [PATCH] DrawIndirectExample --- Examples/DrawIndirectExample.cs | 105 ++++++++++++++++++++++++++++++++ Examples/DrawIndirectGame.cs | 79 ------------------------ MoonWorksGraphicsTests.csproj | 1 + Program.cs | 3 +- 4 files changed, 108 insertions(+), 80 deletions(-) create mode 100644 Examples/DrawIndirectExample.cs delete mode 100644 Examples/DrawIndirectGame.cs diff --git a/Examples/DrawIndirectExample.cs b/Examples/DrawIndirectExample.cs new file mode 100644 index 0000000..68f9553 --- /dev/null +++ b/Examples/DrawIndirectExample.cs @@ -0,0 +1,105 @@ +using MoonWorks; +using MoonWorks.Graphics; +using MoonWorks.Input; +using MoonWorks.Math.Float; +using System.Runtime.InteropServices; + +namespace MoonWorksGraphicsTests; + +class DrawIndirectExample : Example +{ + private GraphicsPipeline GraphicsPipeline; + private GpuBuffer VertexBuffer; + private GpuBuffer DrawBuffer; + + public override void Init(Window window, GraphicsDevice graphicsDevice, Inputs inputs) + { + Window = window; + GraphicsDevice = graphicsDevice; + + Window.SetTitle("DrawIndirect"); + + // Load the shaders + Shader vertShader = new Shader( + GraphicsDevice, + TestUtils.GetShaderPath("PositionColor.vert"), + "main", + ShaderStage.Vertex, + ShaderFormat.SPIRV + ); + + Shader fragShader = new Shader( + GraphicsDevice, + TestUtils.GetShaderPath("SolidColor.frag"), + "main", + ShaderStage.Fragment, + ShaderFormat.SPIRV + ); + + // Create the graphics pipeline + GraphicsPipelineCreateInfo pipelineCreateInfo = TestUtils.GetStandardGraphicsPipelineCreateInfo( + Window.SwapchainFormat, + vertShader, + fragShader + ); + pipelineCreateInfo.VertexInputState = VertexInputState.CreateSingleBinding(); + GraphicsPipeline = new GraphicsPipeline(GraphicsDevice, pipelineCreateInfo); + + // Create and populate the vertex buffer + var resourceUploader = new ResourceUploader(GraphicsDevice); + + VertexBuffer = resourceUploader.CreateBuffer( + [ + new PositionColorVertex(new Vector3(-0.5f, 1, 0), Color.Blue), + new PositionColorVertex(new Vector3( -1f, -1, 0), Color.Green), + new PositionColorVertex(new Vector3( 0f, -1, 0), Color.Red), + + new PositionColorVertex(new Vector3(0.5f, 1, 0), Color.Blue), + new PositionColorVertex(new Vector3( 1f, -1, 0), Color.Green), + new PositionColorVertex(new Vector3( 0f, -1, 0), Color.Red), + ], + BufferUsageFlags.Vertex + ); + + DrawBuffer = resourceUploader.CreateBuffer( + [ + new IndirectDrawCommand(3, 1, 3, 0), + new IndirectDrawCommand(3, 1, 0, 0), + ], + BufferUsageFlags.Indirect + ); + + resourceUploader.Upload(); + resourceUploader.Dispose(); + } + + public override void Update(System.TimeSpan delta) { } + + public override void Draw(double alpha) + { + CommandBuffer cmdbuf = GraphicsDevice.AcquireCommandBuffer(); + Texture swapchainTexture = cmdbuf.AcquireSwapchainTexture(Window); + if (swapchainTexture != null) + { + var renderPass = cmdbuf.BeginRenderPass( + new ColorAttachmentInfo( + swapchainTexture, + false, + Color.Black + ) + ); + renderPass.BindGraphicsPipeline(GraphicsPipeline); + renderPass.BindVertexBuffer(VertexBuffer); + renderPass.DrawPrimitivesIndirect(DrawBuffer, 0, 2, (uint) Marshal.SizeOf()); + cmdbuf.EndRenderPass(renderPass); + } + GraphicsDevice.Submit(cmdbuf); + } + + public override void Destroy() + { + GraphicsPipeline.Dispose(); + VertexBuffer.Dispose(); + DrawBuffer.Dispose(); + } +} diff --git a/Examples/DrawIndirectGame.cs b/Examples/DrawIndirectGame.cs deleted file mode 100644 index b6fd8e0..0000000 --- a/Examples/DrawIndirectGame.cs +++ /dev/null @@ -1,79 +0,0 @@ -using MoonWorks.Graphics; -using MoonWorks.Math.Float; -using System.Runtime.InteropServices; - -namespace MoonWorks.Test -{ - class DrawIndirectGame : Game - { - private GraphicsPipeline graphicsPipeline; - private GpuBuffer vertexBuffer; - private GpuBuffer drawBuffer; - - public DrawIndirectGame() : base(TestUtils.GetStandardWindowCreateInfo(), TestUtils.GetStandardFrameLimiterSettings(), TestUtils.PreferredBackends, 60, true) - { - // Load the shaders - ShaderModule vertShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("PositionColor.vert")); - ShaderModule fragShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("SolidColor.frag")); - - // Create the graphics pipeline - GraphicsPipelineCreateInfo pipelineCreateInfo = TestUtils.GetStandardGraphicsPipelineCreateInfo( - MainWindow.SwapchainFormat, - vertShaderModule, - fragShaderModule - ); - pipelineCreateInfo.VertexInputState = VertexInputState.CreateSingleBinding(); - graphicsPipeline = new GraphicsPipeline(GraphicsDevice, pipelineCreateInfo); - - // Create and populate the vertex buffer - var resourceUploader = new ResourceUploader(GraphicsDevice); - - vertexBuffer = resourceUploader.CreateBuffer( - [ - new PositionColorVertex(new Vector3(-0.5f, -1, 0), Color.Blue), - new PositionColorVertex(new Vector3(-1f, 1, 0), Color.Green), - new PositionColorVertex(new Vector3(0f, 1, 0), Color.Red), - - new PositionColorVertex(new Vector3(.5f, -1, 0), Color.Blue), - new PositionColorVertex(new Vector3(1f, 1, 0), Color.Green), - new PositionColorVertex(new Vector3(0f, 1, 0), Color.Red), - ], - BufferUsageFlags.Vertex - ); - - drawBuffer = resourceUploader.CreateBuffer( - [ - new IndirectDrawCommand(3, 1, 3, 0), - new IndirectDrawCommand(3, 1, 0, 0), - ], - BufferUsageFlags.Indirect - ); - - resourceUploader.Upload(); - resourceUploader.Dispose(); - } - - protected override void Update(System.TimeSpan delta) { } - - protected override void Draw(double alpha) - { - CommandBuffer cmdbuf = GraphicsDevice.AcquireCommandBuffer(); - Texture? backbuffer = cmdbuf.AcquireSwapchainTexture(MainWindow); - if (backbuffer != null) - { - cmdbuf.BeginRenderPass(new ColorAttachmentInfo(backbuffer, WriteOptions.Cycle, Color.CornflowerBlue)); - cmdbuf.BindGraphicsPipeline(graphicsPipeline); - cmdbuf.BindVertexBuffers(new BufferBinding(vertexBuffer, 0)); - cmdbuf.DrawPrimitivesIndirect(drawBuffer, 0, 2, (uint) Marshal.SizeOf()); - cmdbuf.EndRenderPass(); - } - GraphicsDevice.Submit(cmdbuf); - } - - public static void Main(string[] args) - { - DrawIndirectGame game = new DrawIndirectGame(); - game.Run(); - } - } -} diff --git a/MoonWorksGraphicsTests.csproj b/MoonWorksGraphicsTests.csproj index f95f752..dad5679 100644 --- a/MoonWorksGraphicsTests.csproj +++ b/MoonWorksGraphicsTests.csproj @@ -35,6 +35,7 @@ + diff --git a/Program.cs b/Program.cs index c44c680..a6f2039 100644 --- a/Program.cs +++ b/Program.cs @@ -18,7 +18,8 @@ class Program : Game new CopyTextureExample(), new CubeExample(), new CullFaceExample(), - new DepthMSAAExample() + new DepthMSAAExample(), + new DrawIndirectExample() ]; int ExampleIndex = 0;