From ab796356b9e1107b1199f8f4579edb119a2afe02 Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Thu, 6 Jun 2024 14:11:14 -0700 Subject: [PATCH] InstancingAndOffsetsExample --- Examples/InstancingAndOffsetsExample.cs | 131 ++++++++++++++++++++++++ Examples/InstancingAndOffsetsGame.cs | 105 ------------------- MoonWorksGraphicsTests.csproj | 1 + Program.cs | 4 +- 4 files changed, 135 insertions(+), 106 deletions(-) create mode 100644 Examples/InstancingAndOffsetsExample.cs delete mode 100644 Examples/InstancingAndOffsetsGame.cs diff --git a/Examples/InstancingAndOffsetsExample.cs b/Examples/InstancingAndOffsetsExample.cs new file mode 100644 index 0000000..86cab61 --- /dev/null +++ b/Examples/InstancingAndOffsetsExample.cs @@ -0,0 +1,131 @@ +using MoonWorks; +using MoonWorks.Graphics; +using MoonWorks.Input; +using MoonWorks.Math.Float; + +namespace MoonWorksGraphicsTests; + +class InstancingAndOffsetsExample : Example +{ + private GraphicsPipeline Pipeline; + private GpuBuffer VertexBuffer; + private GpuBuffer IndexBuffer; + + private bool useVertexOffset; + private bool useIndexOffset; + + public override void Init(Window window, GraphicsDevice graphicsDevice, Inputs inputs) + { + Window = window; + GraphicsDevice = graphicsDevice; + Inputs = inputs; + + Window.SetTitle("InstancingAndOffsets"); + + Logger.LogInfo("Press Left to toggle vertex offset\nPress Right to toggle index offset"); + + // Load the shaders + Shader vertShader = new Shader( + GraphicsDevice, + TestUtils.GetShaderPath("PositionColorInstanced.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(); + Pipeline = new GraphicsPipeline(GraphicsDevice, pipelineCreateInfo); + + // Create and populate the vertex and index buffers + var resourceUploader = new ResourceUploader(GraphicsDevice); + + VertexBuffer = resourceUploader.CreateBuffer( + [ + new PositionColorVertex(new Vector3(-1, -1, 0), Color.Red), + new PositionColorVertex(new Vector3( 1, -1, 0), Color.Lime), + new PositionColorVertex(new Vector3( 0, 1, 0), Color.Blue), + + new PositionColorVertex(new Vector3(-1, -1, 0), Color.Orange), + new PositionColorVertex(new Vector3( 1, -1, 0), Color.Green), + new PositionColorVertex(new Vector3( 0, 1, 0), Color.Aqua), + + new PositionColorVertex(new Vector3(-1, -1, 0), Color.White), + new PositionColorVertex(new Vector3( 1, -1, 0), Color.White), + new PositionColorVertex(new Vector3( 0, 1, 0), Color.White), + ], + BufferUsageFlags.Vertex + ); + + IndexBuffer = resourceUploader.CreateBuffer( + [ + 0, 1, 2, + 3, 4, 5, + ], + BufferUsageFlags.Index + ); + + resourceUploader.Upload(); + resourceUploader.Dispose(); + } + + public override void Update(System.TimeSpan delta) + { + if (TestUtils.CheckButtonPressed(Inputs, TestUtils.ButtonType.Left)) + { + useVertexOffset = !useVertexOffset; + Logger.LogInfo("Using vertex offset: " + useVertexOffset); + } + + if (TestUtils.CheckButtonPressed(Inputs, TestUtils.ButtonType.Right)) + { + useIndexOffset = !useIndexOffset; + Logger.LogInfo("Using index offset: " + useIndexOffset); + } + } + + public override void Draw(double alpha) + { + uint vertexOffset = useVertexOffset ? 3u : 0; + uint indexOffset = useIndexOffset ? 3u : 0; + + CommandBuffer cmdbuf = GraphicsDevice.AcquireCommandBuffer(); + Texture swapchainTexture = cmdbuf.AcquireSwapchainTexture(Window); + if (swapchainTexture != null) + { + var renderPass = cmdbuf.BeginRenderPass( + new ColorAttachmentInfo( + swapchainTexture, + false, + Color.Black + ) + ); + renderPass.BindGraphicsPipeline(Pipeline); + renderPass.BindVertexBuffer(VertexBuffer); + renderPass.BindIndexBuffer(IndexBuffer, IndexElementSize.Sixteen); + renderPass.DrawIndexedPrimitives(vertexOffset, indexOffset, 1, 16); + cmdbuf.EndRenderPass(renderPass); + } + GraphicsDevice.Submit(cmdbuf); + } + + public override void Destroy() + { + Pipeline.Dispose(); + VertexBuffer.Dispose(); + IndexBuffer.Dispose(); + } +} diff --git a/Examples/InstancingAndOffsetsGame.cs b/Examples/InstancingAndOffsetsGame.cs deleted file mode 100644 index 7fc2dd7..0000000 --- a/Examples/InstancingAndOffsetsGame.cs +++ /dev/null @@ -1,105 +0,0 @@ -using MoonWorks; -using MoonWorks.Graphics; -using MoonWorks.Math.Float; - -namespace MoonWorks.Test -{ - class InstancingAndOffsetsGame : Game - { - private GraphicsPipeline pipeline; - private GpuBuffer vertexBuffer; - private GpuBuffer indexBuffer; - - private bool useVertexOffset; - private bool useIndexOffset; - - public InstancingAndOffsetsGame() : base(TestUtils.GetStandardWindowCreateInfo(), TestUtils.GetStandardFrameLimiterSettings(), TestUtils.PreferredBackends, 60, true) - { - Logger.LogInfo("Press Left to toggle vertex offset\nPress Right to toggle index offset"); - - // Load the shaders - ShaderModule vertShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("PositionColorInstanced.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(); - pipeline = new GraphicsPipeline(GraphicsDevice, pipelineCreateInfo); - - // Create and populate the vertex and index buffers - var resourceUploader = new ResourceUploader(GraphicsDevice); - - vertexBuffer = resourceUploader.CreateBuffer( - [ - new PositionColorVertex(new Vector3(-1, 1, 0), Color.Red), - new PositionColorVertex(new Vector3(1, 1, 0), Color.Lime), - new PositionColorVertex(new Vector3(0, -1, 0), Color.Blue), - - new PositionColorVertex(new Vector3(-1, 1, 0), Color.Orange), - new PositionColorVertex(new Vector3(1, 1, 0), Color.Green), - new PositionColorVertex(new Vector3(0, -1, 0), Color.Aqua), - - new PositionColorVertex(new Vector3(-1, 1, 0), Color.White), - new PositionColorVertex(new Vector3(1, 1, 0), Color.White), - new PositionColorVertex(new Vector3(0, -1, 0), Color.White), - ], - BufferUsageFlags.Vertex - ); - - indexBuffer = resourceUploader.CreateBuffer( - [ - 0, 1, 2, - 3, 4, 5, - ], - BufferUsageFlags.Index - ); - - resourceUploader.Upload(); - resourceUploader.Dispose(); - } - - protected override void Update(System.TimeSpan delta) - { - if (TestUtils.CheckButtonPressed(Inputs, TestUtils.ButtonType.Left)) - { - useVertexOffset = !useVertexOffset; - Logger.LogInfo("Using vertex offset: " + useVertexOffset); - } - - if (TestUtils.CheckButtonPressed(Inputs, TestUtils.ButtonType.Right)) - { - useIndexOffset = !useIndexOffset; - Logger.LogInfo("Using index offset: " + useIndexOffset); - } - } - - protected override void Draw(double alpha) - { - uint vertexOffset = useVertexOffset ? 3u : 0; - uint indexOffset = useIndexOffset ? 3u : 0; - - CommandBuffer cmdbuf = GraphicsDevice.AcquireCommandBuffer(); - Texture? backbuffer = cmdbuf.AcquireSwapchainTexture(MainWindow); - if (backbuffer != null) - { - cmdbuf.BeginRenderPass(new ColorAttachmentInfo(backbuffer, WriteOptions.Cycle, Color.Black)); - cmdbuf.BindGraphicsPipeline(pipeline); - cmdbuf.BindVertexBuffers(vertexBuffer); - cmdbuf.BindIndexBuffer(indexBuffer, IndexElementSize.Sixteen); - cmdbuf.DrawInstancedPrimitives(vertexOffset, indexOffset, 1, 16); - cmdbuf.EndRenderPass(); - } - GraphicsDevice.Submit(cmdbuf); - } - - public static void Main(string[] args) - { - InstancingAndOffsetsGame p = new InstancingAndOffsetsGame(); - p.Run(); - } - } -} diff --git a/MoonWorksGraphicsTests.csproj b/MoonWorksGraphicsTests.csproj index 3891787..daab783 100644 --- a/MoonWorksGraphicsTests.csproj +++ b/MoonWorksGraphicsTests.csproj @@ -37,6 +37,7 @@ + diff --git a/Program.cs b/Program.cs index 299af2a..0db2320 100644 --- a/Program.cs +++ b/Program.cs @@ -20,7 +20,8 @@ class Program : Game new CullFaceExample(), new DepthMSAAExample(), new DrawIndirectExample(), - new GetBufferDataExample() + new GetBufferDataExample(), + new InstancingAndOffsetsExample() ]; int ExampleIndex = 0; @@ -33,6 +34,7 @@ class Program : Game bool debugMode = false ) : base(windowCreateInfo, frameLimiterSettings, preferredBackends, targetTimestep, debugMode) { + Logger.LogInfo("Welcome to the MoonWorks Graphics Tests program! Press Q and E to cycle through examples!"); Examples[ExampleIndex].Init(MainWindow, GraphicsDevice, Inputs); }