From 9cead194d9709c08fd183603f8625955c31faaa9 Mon Sep 17 00:00:00 2001 From: Caleb Cornett Date: Sun, 22 Jan 2023 20:50:35 -0500 Subject: [PATCH] Add RenderTextureCube test + clean up copy-paste msitakes from Texture3DGame tests --- MoonWorksGraphicsTests.sln | 8 +- README.md | 8 +- RenderTexture3D/RenderTexture3DGame.cs | 6 +- RenderTextureCube/RenderTextureCube.csproj | 17 ++ RenderTextureCube/RenderTextureCubeGame.cs | 187 +++++++++++++++++++++ Texture3D/Texture3DGame.cs | 6 +- 6 files changed, 223 insertions(+), 9 deletions(-) create mode 100644 RenderTextureCube/RenderTextureCube.csproj create mode 100644 RenderTextureCube/RenderTextureCubeGame.cs diff --git a/MoonWorksGraphicsTests.sln b/MoonWorksGraphicsTests.sln index f8d2716..917fe76 100644 --- a/MoonWorksGraphicsTests.sln +++ b/MoonWorksGraphicsTests.sln @@ -45,7 +45,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "InstancingAndOffsets", "Ins EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "VertexSampler", "VertexSampler\VertexSampler.csproj", "{C525B6DE-3003-45D5-BB83-89679B108C08}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RenderTexture3D", "RenderTexture3D\RenderTexture3D.csproj", "{6D625A4C-8618-4DFC-A6AD-AA3BE3488D70}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RenderTexture3D", "RenderTexture3D\RenderTexture3D.csproj", "{6D625A4C-8618-4DFC-A6AD-AA3BE3488D70}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RenderTextureCube", "RenderTextureCube\RenderTextureCube.csproj", "{D7A8452F-123F-4965-8716-9E39F677A831}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -141,6 +143,10 @@ Global {6D625A4C-8618-4DFC-A6AD-AA3BE3488D70}.Debug|x64.Build.0 = Debug|x64 {6D625A4C-8618-4DFC-A6AD-AA3BE3488D70}.Release|x64.ActiveCfg = Release|Any CPU {6D625A4C-8618-4DFC-A6AD-AA3BE3488D70}.Release|x64.Build.0 = Release|Any CPU + {D7A8452F-123F-4965-8716-9E39F677A831}.Debug|x64.ActiveCfg = Debug|x64 + {D7A8452F-123F-4965-8716-9E39F677A831}.Debug|x64.Build.0 = Debug|x64 + {D7A8452F-123F-4965-8716-9E39F677A831}.Release|x64.ActiveCfg = Release|Any CPU + {D7A8452F-123F-4965-8716-9E39F677A831}.Release|x64.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/README.md b/README.md index c94d015..395ab35 100644 --- a/README.md +++ b/README.md @@ -72,10 +72,14 @@ Plays a sample Ogg Theora video file. Tests YUV textures and video rendering. Displays 2D slices of a 3D texture. Tests creating and drawing 3D textures. +**VertexSampler** + +Displays a triangle whose colors are driven by sampling a texture in the vertex shader. + **RenderTexture3D** Fades through 2D slices of a 3D render texture. Tests binding and sampling 3D render textures. -**VertexSampler** +**RenderTextureCube** -Displays a triangle whose colors are driven by sampling a texture in the vertex shader. +Displays a cubemap generated by rendering to a cube render target. Tests binding and sampling cube render textures. diff --git a/RenderTexture3D/RenderTexture3DGame.cs b/RenderTexture3D/RenderTexture3DGame.cs index d8f03fc..abc6b2a 100644 --- a/RenderTexture3D/RenderTexture3DGame.cs +++ b/RenderTexture3D/RenderTexture3DGame.cs @@ -68,9 +68,9 @@ namespace MoonWorks.Test new PositionTextureVertex[] { new PositionTextureVertex(new Vector3(-1, -1, 0), new Vector2(0, 0)), - new PositionTextureVertex(new Vector3(1, -1, 0), new Vector2(4, 0)), - new PositionTextureVertex(new Vector3(1, 1, 0), new Vector2(4, 4)), - new PositionTextureVertex(new Vector3(-1, 1, 0), new Vector2(0, 4)), + new PositionTextureVertex(new Vector3(1, -1, 0), new Vector2(1, 0)), + new PositionTextureVertex(new Vector3(1, 1, 0), new Vector2(1, 1)), + new PositionTextureVertex(new Vector3(-1, 1, 0), new Vector2(0, 1)), } ); cmdbuf.SetBufferData( diff --git a/RenderTextureCube/RenderTextureCube.csproj b/RenderTextureCube/RenderTextureCube.csproj new file mode 100644 index 0000000..2868e63 --- /dev/null +++ b/RenderTextureCube/RenderTextureCube.csproj @@ -0,0 +1,17 @@ + + + + + + + + + Exe + net7.0 + enable + x64 + + + + + diff --git a/RenderTextureCube/RenderTextureCubeGame.cs b/RenderTextureCube/RenderTextureCubeGame.cs new file mode 100644 index 0000000..9137ed0 --- /dev/null +++ b/RenderTextureCube/RenderTextureCubeGame.cs @@ -0,0 +1,187 @@ +using MoonWorks; +using MoonWorks.Graphics; +using MoonWorks.Math.Float; +using MoonWorks.Math; +using System.Runtime.InteropServices; + +namespace MoonWorks.Test +{ + class RenderTextureCubeGame : Game + { + private GraphicsPipeline pipeline; + private Buffer vertexBuffer; + private Buffer indexBuffer; + private Texture cubemap; + private Sampler sampler; + + private Vector3 camPos = new Vector3(0, 0, 4f); + + private Color[] colors = new Color[] + { + Color.Red, + Color.Green, + Color.Blue, + Color.Orange, + Color.Yellow, + Color.Purple, + }; + + struct ViewProjectionUniforms + { + public Matrix4x4 ViewProjection; + + public ViewProjectionUniforms(Matrix4x4 viewProjection) + { + ViewProjection = viewProjection; + } + } + + public RenderTextureCubeGame() : base(TestUtils.GetStandardWindowCreateInfo(), TestUtils.GetStandardFrameLimiterSettings(), 60, true) + { + Logger.LogInfo("Press Down to view the other side of the cubemap"); + + // Load the shaders + ShaderModule vertShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("SkyboxVert")); + ShaderModule fragShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("SkyboxFrag")); + + // Create the graphics pipeline + GraphicsPipelineCreateInfo pipelineCreateInfo = TestUtils.GetStandardGraphicsPipelineCreateInfo( + MainWindow.SwapchainFormat, + vertShaderModule, + fragShaderModule + ); + pipelineCreateInfo.VertexInputState = VertexInputState.CreateSingleBinding(); + pipelineCreateInfo.VertexShaderInfo.UniformBufferSize = (uint) Marshal.SizeOf(); + pipelineCreateInfo.FragmentShaderInfo.SamplerBindingCount = 1; + pipeline = new GraphicsPipeline(GraphicsDevice, pipelineCreateInfo); + + // Create samplers + sampler = new Sampler(GraphicsDevice, SamplerCreateInfo.PointClamp); + + // Create and populate the GPU resources + vertexBuffer = Buffer.Create(GraphicsDevice, BufferUsageFlags.Vertex, 24); + indexBuffer = Buffer.Create(GraphicsDevice, BufferUsageFlags.Index, 36); + cubemap = Texture.CreateTextureCube( + GraphicsDevice, + 16, + TextureFormat.R8G8B8A8, + TextureUsageFlags.ColorTarget | TextureUsageFlags.Sampler + ); + + CommandBuffer cmdbuf = GraphicsDevice.AcquireCommandBuffer(); + cmdbuf.SetBufferData( + vertexBuffer, + new PositionVertex[] + { + new PositionVertex(new Vector3(-10, -10, -10)), + new PositionVertex(new Vector3(10, -10, -10)), + new PositionVertex(new Vector3(10, 10, -10)), + new PositionVertex(new Vector3(-10, 10, -10)), + + new PositionVertex(new Vector3(-10, -10, 10)), + new PositionVertex(new Vector3(10, -10, 10)), + new PositionVertex(new Vector3(10, 10, 10)), + new PositionVertex(new Vector3(-10, 10, 10)), + + new PositionVertex(new Vector3(-10, -10, -10)), + new PositionVertex(new Vector3(-10, 10, -10)), + new PositionVertex(new Vector3(-10, 10, 10)), + new PositionVertex(new Vector3(-10, -10, 10)), + + new PositionVertex(new Vector3(10, -10, -10)), + new PositionVertex(new Vector3(10, 10, -10)), + new PositionVertex(new Vector3(10, 10, 10)), + new PositionVertex(new Vector3(10, -10, 10)), + + new PositionVertex(new Vector3(-10, -10, -10)), + new PositionVertex(new Vector3(-10, -10, 10)), + new PositionVertex(new Vector3(10, -10, 10)), + new PositionVertex(new Vector3(10, -10, -10)), + + new PositionVertex(new Vector3(-10, 10, -10)), + new PositionVertex(new Vector3(-10, 10, 10)), + new PositionVertex(new Vector3(10, 10, 10)), + new PositionVertex(new Vector3(10, 10, -10)) + } + ); + + cmdbuf.SetBufferData( + indexBuffer, + new ushort[] + { + 0, 1, 2, 0, 2, 3, + 6, 5, 4, 7, 6, 4, + 8, 9, 10, 8, 10, 11, + 14, 13, 12, 15, 14, 12, + 16, 17, 18, 16, 18, 19, + 22, 21, 20, 23, 22, 20 + } + ); + + // Clear each slice of the cubemap to a different color + for (uint i = 0; i < 6; i += 1) + { + ColorAttachmentInfo attachmentInfo = new ColorAttachmentInfo + { + Texture = cubemap, + ClearColor = colors[i], + Depth = 0, + Layer = i, + Level = 0, + LoadOp = LoadOp.Clear, + StoreOp = StoreOp.Store, + SampleCount = SampleCount.One, + }; + cmdbuf.BeginRenderPass(attachmentInfo); + cmdbuf.EndRenderPass(); + } + + GraphicsDevice.Submit(cmdbuf); + } + + protected override void Update(System.TimeSpan delta) + { + if (TestUtils.CheckButtonPressed(Inputs, TestUtils.ButtonType.Bottom)) + { + camPos.Z *= -1; + } + } + + protected override void Draw(double alpha) + { + Matrix4x4 proj = Matrix4x4.CreatePerspectiveFieldOfView( + MathHelper.ToRadians(75f), + (float) MainWindow.Width / MainWindow.Height, + 0.01f, + 100f + ); + Matrix4x4 view = Matrix4x4.CreateLookAt( + camPos, + Vector3.Zero, + Vector3.Up + ); + ViewProjectionUniforms vertUniforms = new ViewProjectionUniforms(view * proj); + + 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.BindFragmentSamplers(new TextureSamplerBinding(cubemap, sampler)); + uint vertexUniformOffset = cmdbuf.PushVertexShaderUniforms(vertUniforms); + cmdbuf.DrawIndexedPrimitives(0, 0, 12, vertexUniformOffset, 0); + cmdbuf.EndRenderPass(); + } + GraphicsDevice.Submit(cmdbuf); + } + + public static void Main(string[] args) + { + RenderTextureCubeGame game = new RenderTextureCubeGame(); + game.Run(); + } + } +} diff --git a/Texture3D/Texture3DGame.cs b/Texture3D/Texture3DGame.cs index b955629..0a60b50 100644 --- a/Texture3D/Texture3DGame.cs +++ b/Texture3D/Texture3DGame.cs @@ -57,9 +57,9 @@ namespace MoonWorks.Test new PositionTextureVertex[] { new PositionTextureVertex(new Vector3(-1, -1, 0), new Vector2(0, 0)), - new PositionTextureVertex(new Vector3(1, -1, 0), new Vector2(4, 0)), - new PositionTextureVertex(new Vector3(1, 1, 0), new Vector2(4, 4)), - new PositionTextureVertex(new Vector3(-1, 1, 0), new Vector2(0, 4)), + new PositionTextureVertex(new Vector3(1, -1, 0), new Vector2(1, 0)), + new PositionTextureVertex(new Vector3(1, 1, 0), new Vector2(1, 1)), + new PositionTextureVertex(new Vector3(-1, 1, 0), new Vector2(0, 1)), } ); cmdbuf.SetBufferData(