From eb09cf3bb9a21226186b4382e00f416ec7ab1664 Mon Sep 17 00:00:00 2001 From: Caleb Cornett Date: Sun, 22 Jan 2023 15:14:23 -0500 Subject: [PATCH] Add InstancingAndOffsets test --- CopyTexture/CopyTextureGame.cs | 6 +- Instancing/InstancingAndOffsets.csproj | 17 +++ Instancing/InstancingAndOffsetsGame.cs | 108 ++++++++++++++++++ .../PositionColorVertInstanced.refresh | Bin 0 -> 1733 bytes .../Source/PositionColorVertInstanced.vert | 16 +++ MoonWorksGraphicsTests.sln | 36 +++--- README.md | 6 +- 7 files changed, 168 insertions(+), 21 deletions(-) create mode 100644 Instancing/InstancingAndOffsets.csproj create mode 100644 Instancing/InstancingAndOffsetsGame.cs create mode 100644 MoonWorks.Test.Common/Content/Shaders/Compiled/PositionColorVertInstanced.refresh create mode 100644 MoonWorks.Test.Common/Content/Shaders/Source/PositionColorVertInstanced.vert diff --git a/CopyTexture/CopyTextureGame.cs b/CopyTexture/CopyTextureGame.cs index 6607dc0..20bb368 100644 --- a/CopyTexture/CopyTextureGame.cs +++ b/CopyTexture/CopyTextureGame.cs @@ -66,10 +66,6 @@ namespace MoonWorks.Test { 0, 1, 2, 0, 2, 3, - - // For testing index offsets - 8, 9, 10, - 8, 10, 11, } ); @@ -165,7 +161,7 @@ namespace MoonWorks.Test cmdbuf.BindFragmentSamplers(new TextureSamplerBinding(textureCopy, sampler)); cmdbuf.DrawIndexedPrimitives(4, 0, 2, 0, 0); cmdbuf.BindFragmentSamplers(new TextureSamplerBinding(textureSmallCopy, sampler)); - cmdbuf.DrawIndexedPrimitives(0, 6, 2, 0, 0); + cmdbuf.DrawIndexedPrimitives(8, 0, 2, 0, 0); cmdbuf.EndRenderPass(); } GraphicsDevice.Submit(cmdbuf); diff --git a/Instancing/InstancingAndOffsets.csproj b/Instancing/InstancingAndOffsets.csproj new file mode 100644 index 0000000..2868e63 --- /dev/null +++ b/Instancing/InstancingAndOffsets.csproj @@ -0,0 +1,17 @@ + + + + + + + + + Exe + net7.0 + enable + x64 + + + + + diff --git a/Instancing/InstancingAndOffsetsGame.cs b/Instancing/InstancingAndOffsetsGame.cs new file mode 100644 index 0000000..92a939c --- /dev/null +++ b/Instancing/InstancingAndOffsetsGame.cs @@ -0,0 +1,108 @@ +using MoonWorks; +using MoonWorks.Graphics; +using MoonWorks.Math.Float; + +namespace MoonWorks.Test +{ + class InstancingAndOffsetsGame : Game + { + private GraphicsPipeline pipeline; + private Buffer vertexBuffer; + private Buffer indexBuffer; + + private bool useVertexOffset; + private bool useIndexOffset; + + public InstancingAndOffsetsGame() : base(TestUtils.GetStandardWindowCreateInfo(), TestUtils.GetStandardFrameLimiterSettings(), 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("PositionColorVertInstanced")); + ShaderModule fragShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("SolidColor")); + + // 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 + vertexBuffer = Buffer.Create(GraphicsDevice, BufferUsageFlags.Vertex, 9); + indexBuffer = Buffer.Create(GraphicsDevice, BufferUsageFlags.Index, 6); + + CommandBuffer cmdbuf = GraphicsDevice.AcquireCommandBuffer(); + cmdbuf.SetBufferData( + vertexBuffer, + new PositionColorVertex[] + { + 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), + } + ); + cmdbuf.SetBufferData( + indexBuffer, + new ushort[] + { + 0, 1, 2, + 3, 4, 5, + } + ); + GraphicsDevice.Submit(cmdbuf); + GraphicsDevice.Wait(); + } + + 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, Color.Black)); + cmdbuf.BindGraphicsPipeline(pipeline); + cmdbuf.BindVertexBuffers(vertexBuffer); + cmdbuf.BindIndexBuffer(indexBuffer, IndexElementSize.Sixteen); + cmdbuf.DrawInstancedPrimitives(vertexOffset, indexOffset, 1, 16, 0, 0); + cmdbuf.EndRenderPass(); + } + GraphicsDevice.Submit(cmdbuf); + } + + public static void Main(string[] args) + { + InstancingAndOffsetsGame p = new InstancingAndOffsetsGame(); + p.Run(); + } + } +} + diff --git a/MoonWorks.Test.Common/Content/Shaders/Compiled/PositionColorVertInstanced.refresh b/MoonWorks.Test.Common/Content/Shaders/Compiled/PositionColorVertInstanced.refresh new file mode 100644 index 0000000000000000000000000000000000000000..bc78019c9b56303fc648c6a638e75e967b497f8b GIT binary patch literal 1733 zcmYk6+fP$L5XP6wDJVA)0WXL}yn}cHL=mb%EhJ4qNqlWcdty#MN^j_wg8a1Ktsz33uA-gZlk&lr#XRBqNeZ$tB6UWI%uY+W#nMP%-67wY%&Nz>OJxglTk`?R{(=wwHYTD;e&$Di_C1-B|iwu89+ zE^cQzUr?XmS=cOHLAl`RljoC0c9?w8&72}~8$FS2Crw%}ll;6B8(qfMP16$__0vyQ zS$Ct?^B(1xzJoavJ2U9DGY@>0B)hu5)eXloj(9kB@1;d=PBY%5y<`Q9@qMy%=czb4&!Y4#OyD zoPWFdxzzbs!m>J_cywG^*IQ!7+<`@&zhWZoZ-F}>cxFHj^23pXTIVG6`KSEBd5JzvX zOUO;n$&KF)3H;X(cQbIrTih+_7Ds(x{H(s)(y8x9sPB$+xZe7fgyBbZD(5asm@D53 zHmm!tbZkY5oqta{=QDS!n}6W&R`&zxaKu~OL+KVr-C+F4!7Mg{{Y|+0)4*wS#%%_U pT8(=aINr&DUO@g9oFg88?7S!H;C|Z@-XCv{I>1|!f9k-7