From b2aa5adc3b4ba012cc46af17138876762dbb0a52 Mon Sep 17 00:00:00 2001 From: Caleb Cornett Date: Thu, 26 Jan 2023 22:22:38 -0500 Subject: [PATCH] Add BasicStencil test + set default rasterizer to CCW_CullNone --- BasicStencil/BasicStencil.csproj | 17 +++++ BasicStencil/BasicStencilGame.cs | 111 +++++++++++++++++++++++++++++ MoonWorks.Test.Common/TestUtils.cs | 2 +- MoonWorksGraphicsTests.sln | 6 ++ README.md | 4 ++ 5 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 BasicStencil/BasicStencil.csproj create mode 100644 BasicStencil/BasicStencilGame.cs diff --git a/BasicStencil/BasicStencil.csproj b/BasicStencil/BasicStencil.csproj new file mode 100644 index 0000000..7becfeb --- /dev/null +++ b/BasicStencil/BasicStencil.csproj @@ -0,0 +1,17 @@ + + + + + + + + + Exe + net7.0 + enable + x64 + + + + + diff --git a/BasicStencil/BasicStencilGame.cs b/BasicStencil/BasicStencilGame.cs new file mode 100644 index 0000000..c362253 --- /dev/null +++ b/BasicStencil/BasicStencilGame.cs @@ -0,0 +1,111 @@ +using MoonWorks; +using MoonWorks.Graphics; +using MoonWorks.Math.Float; + +namespace MoonWorks.Test +{ + class BasicStencilGame : Game + { + private GraphicsPipeline maskerPipeline; + private GraphicsPipeline maskeePipeline; + private Buffer vertexBuffer; + private Texture depthStencilTexture; + + public BasicStencilGame() : base(TestUtils.GetStandardWindowCreateInfo(), TestUtils.GetStandardFrameLimiterSettings(), 60, true) + { + // Load the shaders + ShaderModule vertShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("PositionColorVert")); + ShaderModule fragShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("SolidColor")); + + // Create the graphics pipelines + GraphicsPipelineCreateInfo pipelineCreateInfo = TestUtils.GetStandardGraphicsPipelineCreateInfo( + MainWindow.SwapchainFormat, + vertShaderModule, + fragShaderModule + ); + pipelineCreateInfo.AttachmentInfo.HasDepthStencilAttachment = true; + pipelineCreateInfo.AttachmentInfo.DepthStencilFormat = TextureFormat.D16S8; + pipelineCreateInfo.VertexInputState = VertexInputState.CreateSingleBinding(); + pipelineCreateInfo.DepthStencilState = new DepthStencilState + { + StencilTestEnable = true, + FrontStencilState = new StencilOpState + { + Reference = 1, + WriteMask = 0xFFFFFFFF, + CompareOp = CompareOp.Never, + FailOp = StencilOp.Replace, + } + }; + maskerPipeline = new GraphicsPipeline(GraphicsDevice, pipelineCreateInfo); + + pipelineCreateInfo.DepthStencilState = new DepthStencilState + { + StencilTestEnable = true, + FrontStencilState = new StencilOpState + { + Reference = 0, + CompareMask = 0xFFFFFFFF, + WriteMask = 0, + CompareOp = CompareOp.Equal, + } + }; + maskeePipeline = new GraphicsPipeline(GraphicsDevice, pipelineCreateInfo); + + // Create and populate the GPU resources + depthStencilTexture = Texture.CreateTexture2D( + GraphicsDevice, + MainWindow.Width, + MainWindow.Height, + TextureFormat.D16S8, + TextureUsageFlags.DepthStencilTarget + ); + vertexBuffer = Buffer.Create(GraphicsDevice, BufferUsageFlags.Vertex, 6); + + CommandBuffer cmdbuf = GraphicsDevice.AcquireCommandBuffer(); + cmdbuf.SetBufferData( + vertexBuffer, + new PositionColorVertex[] + { + new PositionColorVertex(new Vector3(-0.5f, 0.5f, 0), Color.Yellow), + new PositionColorVertex(new Vector3(0.5f, 0.5f, 0), Color.Yellow), + new PositionColorVertex(new Vector3(0, -0.5f, 0), Color.Yellow), + + 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), + } + ); + 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 DepthStencilAttachmentInfo(depthStencilTexture, new DepthStencilValue(0, 0), StoreOp.DontCare, StoreOp.DontCare), + new ColorAttachmentInfo(backbuffer, Color.Black) + ); + cmdbuf.BindGraphicsPipeline(maskerPipeline); + cmdbuf.BindVertexBuffers(vertexBuffer); + cmdbuf.DrawPrimitives(0, 1, 0, 0); + cmdbuf.BindGraphicsPipeline(maskeePipeline); + cmdbuf.DrawPrimitives(3, 1, 0, 0); + cmdbuf.EndRenderPass(); + } + GraphicsDevice.Submit(cmdbuf); + } + + public static void Main(string[] args) + { + BasicStencilGame p = new BasicStencilGame(); + p.Run(); + } + } +} diff --git a/MoonWorks.Test.Common/TestUtils.cs b/MoonWorks.Test.Common/TestUtils.cs index f190a0d..62e920b 100644 --- a/MoonWorks.Test.Common/TestUtils.cs +++ b/MoonWorks.Test.Common/TestUtils.cs @@ -39,7 +39,7 @@ namespace MoonWorks.Test DepthStencilState = DepthStencilState.Disable, MultisampleState = MultisampleState.None, PrimitiveType = PrimitiveType.TriangleList, - RasterizerState = RasterizerState.CW_CullNone, + RasterizerState = RasterizerState.CCW_CullNone, VertexInputState = VertexInputState.Empty, VertexShaderInfo = GraphicsShaderInfo.Create(vertShaderModule, "main", 0), FragmentShaderInfo = GraphicsShaderInfo.Create(fragShaderModule, "main", 0) diff --git a/MoonWorksGraphicsTests.sln b/MoonWorksGraphicsTests.sln index d2b285c..e400f38 100644 --- a/MoonWorksGraphicsTests.sln +++ b/MoonWorksGraphicsTests.sln @@ -53,6 +53,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RenderTextureMipmaps", "Ren EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TextureMipmaps", "TextureMipmaps\TextureMipmaps.csproj", "{5A1AC35B-EF18-426D-A633-D4899E84EAA7}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BasicStencil", "Stencil\BasicStencil.csproj", "{E3FCEDC2-BAE3-4E21-AEBA-2AC3504526CD}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|x64 = Debug|x64 @@ -159,6 +161,10 @@ Global {5A1AC35B-EF18-426D-A633-D4899E84EAA7}.Debug|x64.Build.0 = Debug|x64 {5A1AC35B-EF18-426D-A633-D4899E84EAA7}.Release|x64.ActiveCfg = Release|x64 {5A1AC35B-EF18-426D-A633-D4899E84EAA7}.Release|x64.Build.0 = Release|x64 + {E3FCEDC2-BAE3-4E21-AEBA-2AC3504526CD}.Debug|x64.ActiveCfg = Debug|x64 + {E3FCEDC2-BAE3-4E21-AEBA-2AC3504526CD}.Debug|x64.Build.0 = Debug|x64 + {E3FCEDC2-BAE3-4E21-AEBA-2AC3504526CD}.Release|x64.ActiveCfg = Release|x64 + {E3FCEDC2-BAE3-4E21-AEBA-2AC3504526CD}.Release|x64.Build.0 = Release|x64 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/README.md b/README.md index 7b56fe7..2593385 100644 --- a/README.md +++ b/README.md @@ -91,3 +91,7 @@ Displays a cubemap generated by rendering to a cube render target. Tests binding **RenderTextureMipmaps** Displays a quad that can be scaled to reveal various mip levels. The mips are solid colors, generated by clearing various levels of the texture in a render pass. Tests rendering to mipmap levels of a render texture, mip LOD bias, min/max LODs, and mip filter modes. + +**BasicStencil** + +Demonstrates stencil masking using two triangles. Tests stencil buffers and basic stencil operations.