diff --git a/DrawIndirect/DrawIndirect.csproj b/DrawIndirect/DrawIndirect.csproj new file mode 100644 index 0000000..3d96325 --- /dev/null +++ b/DrawIndirect/DrawIndirect.csproj @@ -0,0 +1,15 @@ + + + + + + + + + Exe + net6.0 + enable + x64 + + + diff --git a/DrawIndirect/DrawIndirectGame.cs b/DrawIndirect/DrawIndirectGame.cs new file mode 100644 index 0000000..aaa6fbf --- /dev/null +++ b/DrawIndirect/DrawIndirectGame.cs @@ -0,0 +1,86 @@ +using MoonWorks; +using MoonWorks.Graphics; +using MoonWorks.Math.Float; +using System.Runtime.InteropServices; + +namespace MoonWorks.Test +{ + class DrawIndirectGame : Game + { + private GraphicsPipeline graphicsPipeline; + private Buffer vertexBuffer; + private Buffer drawBuffer; + + public DrawIndirectGame() : base(TestUtils.GetStandardWindowCreateInfo(), TestUtils.GetStandardFrameLimiterSettings(), 60, true) + { + // Load the shaders + ShaderModule vertShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("PositionColorVert.spv")); + ShaderModule fragShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("SolidColor.spv")); + + // Create the graphics pipeline + GraphicsPipelineCreateInfo pipelineCreateInfo = TestUtils.GetStandardGraphicsPipelineCreateInfo( + MainWindow.SwapchainFormat, + vertShaderModule, + fragShaderModule + ); + pipelineCreateInfo.VertexInputState = new VertexInputState( + VertexBinding.Create(), + VertexAttribute.Create("Position", 0), + VertexAttribute.Create("Color", 1) + ); + graphicsPipeline = new GraphicsPipeline(GraphicsDevice, pipelineCreateInfo); + + // Create and populate the vertex buffer + vertexBuffer = Buffer.Create(GraphicsDevice, BufferUsageFlags.Vertex, 6); + drawBuffer = Buffer.Create(GraphicsDevice, BufferUsageFlags.Indirect, 2); + + CommandBuffer cmdbuf = GraphicsDevice.AcquireCommandBuffer(); + cmdbuf.SetBufferData( + vertexBuffer, + new PositionColorVertex[] + { + 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), + } + ); + cmdbuf.SetBufferData( + drawBuffer, + new DrawIndirectCommand[] + { + new DrawIndirectCommand(3, 1, 3, 0), + new DrawIndirectCommand(3, 1, 0, 0), + } + ); + GraphicsDevice.Submit(cmdbuf); + GraphicsDevice.Wait(); + } + + 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, Color.CornflowerBlue)); + cmdbuf.BindGraphicsPipeline(graphicsPipeline); + cmdbuf.BindVertexBuffers(new BufferBinding(vertexBuffer, 0)); + cmdbuf.DrawPrimitivesIndirect(drawBuffer, 0, 2, (uint) Marshal.SizeOf(), 0, 0); + cmdbuf.EndRenderPass(); + } + GraphicsDevice.Submit(cmdbuf); + } + + public static void Main(string[] args) + { + DrawIndirectGame game = new DrawIndirectGame(); + game.Run(); + } + } +} diff --git a/MoonWorksGraphicsTests.sln b/MoonWorksGraphicsTests.sln index 53dd780..c499ce9 100644 --- a/MoonWorksGraphicsTests.sln +++ b/MoonWorksGraphicsTests.sln @@ -29,7 +29,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Cube", "Cube\Cube.csproj", EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BasicCompute", "BasicCompute\BasicCompute.csproj", "{68D47057-BBCB-4F86-9C0A-D6D730B18D9E}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ComputeUniforms", "ComputeUniforms\ComputeUniforms.csproj", "{5F8BBD49-6C39-4186-A4A3-91D6662D3ABD}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeUniforms", "ComputeUniforms\ComputeUniforms.csproj", "{5F8BBD49-6C39-4186-A4A3-91D6662D3ABD}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DrawIndirect", "DrawIndirect\DrawIndirect.csproj", "{CA6E0ACF-3698-452F-B71F-51286EB5E1B2}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -93,6 +95,10 @@ Global {5F8BBD49-6C39-4186-A4A3-91D6662D3ABD}.Debug|x64.Build.0 = Debug|x64 {5F8BBD49-6C39-4186-A4A3-91D6662D3ABD}.Release|x64.ActiveCfg = Release|Any CPU {5F8BBD49-6C39-4186-A4A3-91D6662D3ABD}.Release|x64.Build.0 = Release|Any CPU + {CA6E0ACF-3698-452F-B71F-51286EB5E1B2}.Debug|x64.ActiveCfg = Debug|x64 + {CA6E0ACF-3698-452F-B71F-51286EB5E1B2}.Debug|x64.Build.0 = Debug|x64 + {CA6E0ACF-3698-452F-B71F-51286EB5E1B2}.Release|x64.ActiveCfg = Release|Any CPU + {CA6E0ACF-3698-452F-B71F-51286EB5E1B2}.Release|x64.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/README.md b/README.md index 4611abd..2a56b56 100644 --- a/README.md +++ b/README.md @@ -47,3 +47,7 @@ Uses compute pipelines to (1) fill a texture with yellow pixels then display it **ComputeUniforms** Uses a compute pipeline to fill a texture with a color gradient. Tests compute uniforms. + +**DrawIndirect** + +Draws two triangles via indirect commands. Tests DrawPrimitivesIndirect.