From 6d0add62304ab9c1f66e1fee5d19a35036b68d41 Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Thu, 6 Jun 2024 15:03:43 -0700 Subject: [PATCH] RenderTextureCubeExample --- Examples/RenderTextureCubeExample.cs | 209 +++++++++++++++++++++++++++ Examples/RenderTextureCubeGame.cs | 179 ----------------------- MoonWorksGraphicsTests.csproj | 1 + Program.cs | 3 +- 4 files changed, 212 insertions(+), 180 deletions(-) create mode 100644 Examples/RenderTextureCubeExample.cs delete mode 100644 Examples/RenderTextureCubeGame.cs diff --git a/Examples/RenderTextureCubeExample.cs b/Examples/RenderTextureCubeExample.cs new file mode 100644 index 0000000..096d339 --- /dev/null +++ b/Examples/RenderTextureCubeExample.cs @@ -0,0 +1,209 @@ +using MoonWorks.Graphics; +using MoonWorks.Math.Float; +using MoonWorks.Math; +using MoonWorks; +using MoonWorks.Input; + +namespace MoonWorksGraphicsTests; + +class RenderTextureCubeExample : Example +{ + private GraphicsPipeline pipeline; + private GpuBuffer vertexBuffer; + private GpuBuffer 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, + }; + + public override void Init(Window window, GraphicsDevice graphicsDevice, Inputs inputs) + { + Window = window; + GraphicsDevice = graphicsDevice; + Inputs = inputs; + + Window.SetTitle("RenderTextureCube"); + + Logger.LogInfo("Press Down to view the other side of the cubemap"); + + // Load the shaders + Shader vertShader = new Shader( + GraphicsDevice, + TestUtils.GetShaderPath("Skybox.vert"), + "main", + ShaderStage.Vertex, + ShaderFormat.SPIRV + ); + + Shader fragShader = new Shader( + GraphicsDevice, + TestUtils.GetShaderPath("Skybox.frag"), + "main", + ShaderStage.Fragment, + ShaderFormat.SPIRV + ); + + // Create the graphics pipeline + GraphicsPipelineCreateInfo pipelineCreateInfo = TestUtils.GetStandardGraphicsPipelineCreateInfo( + Window.SwapchainFormat, + vertShader, + fragShader + ); + pipelineCreateInfo.VertexInputState = VertexInputState.CreateSingleBinding(); + pipelineCreateInfo.VertexShaderResourceInfo = new GraphicsPipelineResourceInfo + { + UniformBufferCount = 1 + }; + pipelineCreateInfo.FragmentShaderResourceInfo = new GraphicsPipelineResourceInfo + { + SamplerCount = 1 + }; + pipeline = new GraphicsPipeline(GraphicsDevice, pipelineCreateInfo); + + // Create samplers + sampler = new Sampler(GraphicsDevice, SamplerCreateInfo.PointClamp); + + // Create and populate the GPU resources + var resourceUploader = new ResourceUploader(GraphicsDevice); + + vertexBuffer = resourceUploader.CreateBuffer( + [ + 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)) + ], + BufferUsageFlags.Vertex + ); + + indexBuffer = resourceUploader.CreateBuffer( + [ + 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 + ], + BufferUsageFlags.Index + ); + + resourceUploader.Upload(); + resourceUploader.Dispose(); + + cubemap = Texture.CreateTextureCube( + GraphicsDevice, + 16, + TextureFormat.R8G8B8A8, + TextureUsageFlags.ColorTarget | TextureUsageFlags.Sampler + ); + + CommandBuffer cmdbuf = GraphicsDevice.AcquireCommandBuffer(); + + // Clear each slice of the cubemap to a different color + for (uint i = 0; i < 6; i += 1) + { + ColorAttachmentInfo attachmentInfo = new ColorAttachmentInfo + { + TextureSlice = new TextureSlice + { + Texture = cubemap, + Layer = i, + MipLevel = 0 + }, + ClearColor = colors[i], + LoadOp = LoadOp.Clear, + StoreOp = StoreOp.Store + }; + var renderPass = cmdbuf.BeginRenderPass(attachmentInfo); + cmdbuf.EndRenderPass(renderPass); + } + + GraphicsDevice.Submit(cmdbuf); + } + + public override void Update(System.TimeSpan delta) + { + if (TestUtils.CheckButtonPressed(Inputs, TestUtils.ButtonType.Bottom)) + { + camPos.Z *= -1; + } + } + + public override void Draw(double alpha) + { + Matrix4x4 proj = Matrix4x4.CreatePerspectiveFieldOfView( + MathHelper.ToRadians(75f), + (float) Window.Width / Window.Height, + 0.01f, + 100f + ); + Matrix4x4 view = Matrix4x4.CreateLookAt( + camPos, + Vector3.Zero, + Vector3.Up + ); + TransformVertexUniform vertUniforms = new TransformVertexUniform(view * proj); + + 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.BindFragmentSampler(new TextureSamplerBinding(cubemap, sampler)); + renderPass.PushVertexUniformData(vertUniforms); + renderPass.DrawIndexedPrimitives(0, 0, 12); + cmdbuf.EndRenderPass(renderPass); + } + GraphicsDevice.Submit(cmdbuf); + } + + public override void Destroy() + { + + } +} diff --git a/Examples/RenderTextureCubeGame.cs b/Examples/RenderTextureCubeGame.cs deleted file mode 100644 index 215424e..0000000 --- a/Examples/RenderTextureCubeGame.cs +++ /dev/null @@ -1,179 +0,0 @@ -using MoonWorks.Graphics; -using MoonWorks.Math.Float; -using MoonWorks.Math; -using System.Runtime.InteropServices; - -namespace MoonWorks.Test -{ - class RenderTextureCubeGame : Game - { - private GraphicsPipeline pipeline; - private GpuBuffer vertexBuffer; - private GpuBuffer 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, - }; - - public RenderTextureCubeGame() : base(TestUtils.GetStandardWindowCreateInfo(), TestUtils.GetStandardFrameLimiterSettings(), TestUtils.PreferredBackends, 60, true) - { - Logger.LogInfo("Press Down to view the other side of the cubemap"); - - // Load the shaders - ShaderModule vertShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("Skybox.vert")); - ShaderModule fragShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("Skybox.frag")); - - // 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 - var resourceUploader = new ResourceUploader(GraphicsDevice); - - vertexBuffer = resourceUploader.CreateBuffer( - [ - 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)) - ], - BufferUsageFlags.Vertex - ); - - indexBuffer = resourceUploader.CreateBuffer( - [ - 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 - ], - BufferUsageFlags.Index - ); - - resourceUploader.Upload(); - resourceUploader.Dispose(); - - cubemap = Texture.CreateTextureCube( - GraphicsDevice, - 16, - TextureFormat.R8G8B8A8, - TextureUsageFlags.ColorTarget | TextureUsageFlags.Sampler - ); - - CommandBuffer cmdbuf = GraphicsDevice.AcquireCommandBuffer(); - - // Clear each slice of the cubemap to a different color - for (uint i = 0; i < 6; i += 1) - { - ColorAttachmentInfo attachmentInfo = new ColorAttachmentInfo - { - TextureSlice = new TextureSlice - { - Texture = cubemap, - Layer = i, - MipLevel = 0 - }, - ClearColor = colors[i], - LoadOp = LoadOp.Clear, - StoreOp = StoreOp.Store - }; - 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 - ); - TransformVertexUniform vertUniforms = new TransformVertexUniform(view * proj); - - 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.BindFragmentSamplers(new TextureSamplerBinding(cubemap, sampler)); - cmdbuf.PushVertexShaderUniforms(vertUniforms); - cmdbuf.DrawIndexedPrimitives(0, 0, 12); - cmdbuf.EndRenderPass(); - } - GraphicsDevice.Submit(cmdbuf); - } - - public static void Main(string[] args) - { - RenderTextureCubeGame game = new RenderTextureCubeGame(); - game.Run(); - } - } -} diff --git a/MoonWorksGraphicsTests.csproj b/MoonWorksGraphicsTests.csproj index 5af8e50..e47c7f3 100644 --- a/MoonWorksGraphicsTests.csproj +++ b/MoonWorksGraphicsTests.csproj @@ -42,6 +42,7 @@ + diff --git a/Program.cs b/Program.cs index b20ac81..13c29f9 100644 --- a/Program.cs +++ b/Program.cs @@ -25,7 +25,8 @@ class Program : Game new MSAACubeExample(), new MSAAExample(), new RenderTexture2DArrayExample(), - new RenderTexture2DExample() + new RenderTexture2DExample(), + new RenderTextureCubeExample() ]; int ExampleIndex = 0;