From 0846c09f53ff9fee0238c9899df176e40baa8224 Mon Sep 17 00:00:00 2001 From: Caleb Cornett Date: Fri, 9 Feb 2024 11:47:55 -0600 Subject: [PATCH] Add RenderTexture2D test + README update for StoreLoad --- MoonWorksGraphicsTests.sln | 6 ++ README.md | 8 ++ RenderTexture2D/RenderTexture2D.csproj | 16 +++ RenderTexture2D/RenderTexture2DGame.cs | 131 +++++++++++++++++++++++++ 4 files changed, 161 insertions(+) create mode 100644 RenderTexture2D/RenderTexture2D.csproj create mode 100644 RenderTexture2D/RenderTexture2DGame.cs diff --git a/MoonWorksGraphicsTests.sln b/MoonWorksGraphicsTests.sln index f5e6619..f3b6948 100644 --- a/MoonWorksGraphicsTests.sln +++ b/MoonWorksGraphicsTests.sln @@ -61,6 +61,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WindowResizing", "WindowRes EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StoreLoad", "StoreLoad\StoreLoad.csproj", "{CD31F1B5-C76A-428A-A812-8DFD6CAB20A9}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RenderTexture2D", "RenderTexture2D\RenderTexture2D.csproj", "{F9C9E15D-1000-46DA-BA39-1D4C0D43F023}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -183,6 +185,10 @@ Global {CD31F1B5-C76A-428A-A812-8DFD6CAB20A9}.Debug|Any CPU.Build.0 = Debug|Any CPU {CD31F1B5-C76A-428A-A812-8DFD6CAB20A9}.Release|Any CPU.ActiveCfg = Release|Any CPU {CD31F1B5-C76A-428A-A812-8DFD6CAB20A9}.Release|Any CPU.Build.0 = Release|Any CPU + {F9C9E15D-1000-46DA-BA39-1D4C0D43F023}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F9C9E15D-1000-46DA-BA39-1D4C0D43F023}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F9C9E15D-1000-46DA-BA39-1D4C0D43F023}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F9C9E15D-1000-46DA-BA39-1D4C0D43F023}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/README.md b/README.md index 43b6904..1cfd774 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,10 @@ Sets up a graphics pipeline and draws a triangle without vertex buffers. (The ve Similar to above, but using a MoonWorks vertex buffer and custom vertex structs. +**StoreLoad** + +Draws a triangle to the screen in one render pass, stores the attachment, and then loads the attachment again. Tests that load/store operations work as expected. + **TexturedQuad** Draws a textured quad to the screen. Tests texture binding, index buffers, and sampler states. @@ -80,6 +84,10 @@ Displays a quad that can be scaled to reveal various mip levels. The mips are ge Displays a triangle whose colors are driven by sampling a texture in the vertex shader. +**RenderTexture2D** + +Clears and draws 4 render textures to the screen. Tests binding and sampling 2D render textures. + **RenderTexture3D** Fades through 2D slices of a 3D render texture. Tests binding and sampling 3D render textures. diff --git a/RenderTexture2D/RenderTexture2D.csproj b/RenderTexture2D/RenderTexture2D.csproj new file mode 100644 index 0000000..7ce1c70 --- /dev/null +++ b/RenderTexture2D/RenderTexture2D.csproj @@ -0,0 +1,16 @@ + + + + + + + + + Exe + net8.0 + enable + + + + + diff --git a/RenderTexture2D/RenderTexture2DGame.cs b/RenderTexture2D/RenderTexture2DGame.cs new file mode 100644 index 0000000..00ea043 --- /dev/null +++ b/RenderTexture2D/RenderTexture2DGame.cs @@ -0,0 +1,131 @@ +using MoonWorks; +using MoonWorks.Graphics; +using MoonWorks.Math.Float; + +namespace MoonWorks.Test +{ + class RenderTexture2DGame : Game + { + private GraphicsPipeline pipeline; + private Buffer vertexBuffer; + private Buffer indexBuffer; + private Texture[] textures = new Texture[4]; + private Sampler sampler; + + public RenderTexture2DGame() : base(TestUtils.GetStandardWindowCreateInfo(), TestUtils.GetStandardFrameLimiterSettings(), 60, true) + { + // Load the shaders + ShaderModule vertShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("TexturedQuad.vert")); + ShaderModule fragShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("TexturedQuad.frag")); + + // Create the graphics pipeline + GraphicsPipelineCreateInfo pipelineCreateInfo = TestUtils.GetStandardGraphicsPipelineCreateInfo( + MainWindow.SwapchainFormat, + vertShaderModule, + fragShaderModule + ); + pipelineCreateInfo.VertexInputState = VertexInputState.CreateSingleBinding(); + pipelineCreateInfo.VertexShaderInfo = GraphicsShaderInfo.Create(vertShaderModule, "main", 0); + pipelineCreateInfo.FragmentShaderInfo.SamplerBindingCount = 1; + pipeline = new GraphicsPipeline(GraphicsDevice, pipelineCreateInfo); + + // Create sampler + SamplerCreateInfo samplerCreateInfo = SamplerCreateInfo.PointClamp; + sampler = new Sampler(GraphicsDevice, samplerCreateInfo); + + // Create and populate the GPU resources + vertexBuffer = Buffer.Create(GraphicsDevice, BufferUsageFlags.Vertex, 16); + indexBuffer = Buffer.Create(GraphicsDevice, BufferUsageFlags.Index, 6); + + for (int i = 0; i < textures.Length; i += 1) + { + textures[i] = Texture.CreateTexture2D( + GraphicsDevice, + MainWindow.Width / 4, + MainWindow.Height / 4, + TextureFormat.R8G8B8A8, + TextureUsageFlags.ColorTarget | TextureUsageFlags.Sampler + ); + } + + CommandBuffer cmdbuf = GraphicsDevice.AcquireCommandBuffer(); + cmdbuf.SetBufferData( + vertexBuffer, + new PositionTextureVertex[] + { + new PositionTextureVertex(new Vector3(-1, -1, 0), new Vector2(0, 0)), + new PositionTextureVertex(new Vector3(0, -1, 0), new Vector2(1, 0)), + new PositionTextureVertex(new Vector3(0, 0, 0), new Vector2(1, 1)), + new PositionTextureVertex(new Vector3(-1, 0, 0), new Vector2(0, 1)), + + new PositionTextureVertex(new Vector3(0, -1, 0), new Vector2(0, 0)), + new PositionTextureVertex(new Vector3(1, -1, 0), new Vector2(1, 0)), + new PositionTextureVertex(new Vector3(1, 0, 0), new Vector2(1, 1)), + new PositionTextureVertex(new Vector3(0, 0, 0), new Vector2(0, 1)), + + new PositionTextureVertex(new Vector3(-1, 0, 0), new Vector2(0, 0)), + new PositionTextureVertex(new Vector3(0, 0, 0), new Vector2(1, 0)), + new PositionTextureVertex(new Vector3(0, 1, 0), new Vector2(1, 1)), + new PositionTextureVertex(new Vector3(-1, 1, 0), new Vector2(0, 1)), + + new PositionTextureVertex(new Vector3(0, 0, 0), new Vector2(0, 0)), + new PositionTextureVertex(new Vector3(1, 0, 0), new Vector2(1, 0)), + new PositionTextureVertex(new Vector3(1, 1, 0), new Vector2(1, 1)), + new PositionTextureVertex(new Vector3(0, 1, 0), new Vector2(0, 1)), + } + ); + cmdbuf.SetBufferData( + indexBuffer, + new ushort[] + { + 0, 1, 2, + 0, 2, 3, + } + ); + + GraphicsDevice.Submit(cmdbuf); + } + + 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(textures[0], Color.Red)); + cmdbuf.EndRenderPass(); + + cmdbuf.BeginRenderPass(new ColorAttachmentInfo(textures[1], Color.Blue)); + cmdbuf.EndRenderPass(); + + cmdbuf.BeginRenderPass(new ColorAttachmentInfo(textures[2], Color.Green)); + cmdbuf.EndRenderPass(); + + cmdbuf.BeginRenderPass(new ColorAttachmentInfo(textures[3], Color.Yellow)); + cmdbuf.EndRenderPass(); + + cmdbuf.BeginRenderPass(new ColorAttachmentInfo(backbuffer, Color.Black)); + cmdbuf.BindGraphicsPipeline(pipeline); + cmdbuf.BindVertexBuffers(vertexBuffer); + cmdbuf.BindIndexBuffer(indexBuffer, IndexElementSize.Sixteen); + + for (uint i = 0; i < textures.Length; i += 1) + { + cmdbuf.BindFragmentSamplers(new TextureSamplerBinding(textures[i], sampler)); + cmdbuf.DrawIndexedPrimitives(4 * i, 0, 2, 0, 0); + } + + cmdbuf.EndRenderPass(); + } + GraphicsDevice.Submit(cmdbuf); + } + + public static void Main(string[] args) + { + RenderTexture2DGame game = new RenderTexture2DGame(); + game.Run(); + } + } +}