From 1568924f55ea5a6d5b5b7dc161f8639bc932cd39 Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Thu, 6 Jun 2024 14:47:06 -0700 Subject: [PATCH] RenderTexture2DArrayExample --- Examples/RenderTexture2DArrayExample.cs | 92 ++++++++++++++++ Examples/RenderTexture2DArrayGame.cs | 139 ------------------------ MoonWorksGraphicsTests.csproj | 1 + Program.cs | 3 +- 4 files changed, 95 insertions(+), 140 deletions(-) create mode 100644 Examples/RenderTexture2DArrayExample.cs delete mode 100644 Examples/RenderTexture2DArrayGame.cs diff --git a/Examples/RenderTexture2DArrayExample.cs b/Examples/RenderTexture2DArrayExample.cs new file mode 100644 index 0000000..c42a9df --- /dev/null +++ b/Examples/RenderTexture2DArrayExample.cs @@ -0,0 +1,92 @@ +using System; +using MoonWorks; +using MoonWorks.Graphics; +using MoonWorks.Input; + +namespace MoonWorksGraphicsTests; + +class RenderTexture2DArrayExample : Example +{ + private Texture RenderTarget; + + private float t; + private Color[] colors = + [ + Color.Red, + Color.Green, + Color.Blue, + ]; + + public override void Init(Window window, GraphicsDevice graphicsDevice, Inputs inputs) + { + Window = window; + GraphicsDevice = graphicsDevice; + + RenderTarget = Texture.CreateTexture2DArray( + GraphicsDevice, + 16, + 16, + (uint) colors.Length, + TextureFormat.R8G8B8A8, + TextureUsageFlags.ColorTarget | TextureUsageFlags.Sampler + ); + + CommandBuffer cmdbuf = GraphicsDevice.AcquireCommandBuffer(); + + // Clear each depth slice of the RT to a different color + for (uint i = 0; i < colors.Length; i += 1) + { + ColorAttachmentInfo attachmentInfo = new ColorAttachmentInfo + { + TextureSlice = new TextureSlice + { + Texture = RenderTarget, + 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) { } + + public override void Draw(double alpha) + { + t += 0.01f; + t %= 3; + + CommandBuffer cmdbuf = GraphicsDevice.AcquireCommandBuffer(); + Texture swapchainTexture = cmdbuf.AcquireSwapchainTexture(Window); + if (swapchainTexture != null) + { + cmdbuf.Blit( + new TextureRegion + { + TextureSlice = new TextureSlice + { + Texture = RenderTarget, + Layer = (uint) MathF.Floor(t) + }, + Depth = 1 + }, + swapchainTexture, + Filter.Nearest, + false + ); + } + GraphicsDevice.Submit(cmdbuf); + } + + public override void Destroy() + { + RenderTarget.Dispose(); + } +} diff --git a/Examples/RenderTexture2DArrayGame.cs b/Examples/RenderTexture2DArrayGame.cs deleted file mode 100644 index 789e4f3..0000000 --- a/Examples/RenderTexture2DArrayGame.cs +++ /dev/null @@ -1,139 +0,0 @@ -using System; -using MoonWorks.Graphics; -using MoonWorks.Math.Float; - -namespace MoonWorks.Test -{ - class RenderTexture2DArrayGame : Game - { - private GraphicsPipeline pipeline; - private GpuBuffer vertexBuffer; - private GpuBuffer indexBuffer; - private Texture rt; - private Sampler sampler; - - private float t; - private Color[] colors = new Color[] - { - Color.Red, - Color.Green, - Color.Blue, - }; - - struct FragUniform - { - public float Depth; - - public FragUniform(float depth) - { - Depth = depth; - } - } - - public RenderTexture2DArrayGame() : base(TestUtils.GetStandardWindowCreateInfo(), TestUtils.GetStandardFrameLimiterSettings(), TestUtils.PreferredBackends, 60, true) - { - // Load the shaders - ShaderModule vertShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("TexturedQuad.vert")); - ShaderModule fragShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("TexturedQuad2DArray.frag")); - - // Create the graphics pipeline - GraphicsPipelineCreateInfo pipelineCreateInfo = TestUtils.GetStandardGraphicsPipelineCreateInfo( - MainWindow.SwapchainFormat, - vertShaderModule, - fragShaderModule - ); - pipelineCreateInfo.VertexInputState = VertexInputState.CreateSingleBinding(); - pipelineCreateInfo.FragmentShaderInfo = GraphicsShaderInfo.Create(fragShaderModule, "main", 1); - pipeline = new GraphicsPipeline(GraphicsDevice, pipelineCreateInfo); - - // Create samplers - sampler = new Sampler(GraphicsDevice, SamplerCreateInfo.PointWrap); - - // Create and populate the GPU resources - var resourceUploader = new ResourceUploader(GraphicsDevice); - - vertexBuffer = resourceUploader.CreateBuffer( - [ - new PositionTextureVertex(new Vector3(-1, -1, 0), new Vector2(0, 0)), - 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)), - ], - BufferUsageFlags.Vertex - ); - - indexBuffer = resourceUploader.CreateBuffer( - [ - 0, 1, 2, - 0, 2, 3, - ], - BufferUsageFlags.Index - ); - - resourceUploader.Upload(); - resourceUploader.Dispose(); - - rt = Texture.CreateTexture2DArray( - GraphicsDevice, - 16, - 16, - (uint) colors.Length, - TextureFormat.R8G8B8A8, - TextureUsageFlags.ColorTarget | TextureUsageFlags.Sampler - ); - - CommandBuffer cmdbuf = GraphicsDevice.AcquireCommandBuffer(); - - // Clear each depth slice of the RT to a different color - for (uint i = 0; i < colors.Length; i += 1) - { - ColorAttachmentInfo attachmentInfo = new ColorAttachmentInfo - { - TextureSlice = new TextureSlice - { - Texture = rt, - 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) { } - - protected override void Draw(double alpha) - { - t += 0.01f; - t %= 3; - FragUniform fragUniform = new FragUniform(MathF.Floor(t)); - - 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(rt, sampler)); - cmdbuf.PushFragmentShaderUniforms(fragUniform); - cmdbuf.DrawIndexedPrimitives(0, 0, 2); - cmdbuf.EndRenderPass(); - } - GraphicsDevice.Submit(cmdbuf); - } - - public static void Main(string[] args) - { - RenderTexture2DArrayGame game = new RenderTexture2DArrayGame(); - game.Run(); - } - } -} diff --git a/MoonWorksGraphicsTests.csproj b/MoonWorksGraphicsTests.csproj index edbe6f9..6aa57cf 100644 --- a/MoonWorksGraphicsTests.csproj +++ b/MoonWorksGraphicsTests.csproj @@ -40,6 +40,7 @@ + diff --git a/Program.cs b/Program.cs index d1109c8..5c0d9e6 100644 --- a/Program.cs +++ b/Program.cs @@ -23,7 +23,8 @@ class Program : Game new GetBufferDataExample(), new InstancingAndOffsetsExample(), new MSAACubeExample(), - new MSAAExample() + new MSAAExample(), + new RenderTexture2DArrayExample() ]; int ExampleIndex = 0;