From d538c2c935953cd2d087cff5fd4b1fcd8abf5746 Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Thu, 6 Jun 2024 14:58:30 -0700 Subject: [PATCH] RenderTexture2DExample --- Examples/RenderTexture2DArrayExample.cs | 2 + Examples/RenderTexture2DExample.cs | 118 ++++++++++++++++++++++ Examples/RenderTexture2DGame.cs | 129 ------------------------ MoonWorksGraphicsTests.csproj | 1 + Program.cs | 3 +- 5 files changed, 123 insertions(+), 130 deletions(-) create mode 100644 Examples/RenderTexture2DExample.cs delete mode 100644 Examples/RenderTexture2DGame.cs diff --git a/Examples/RenderTexture2DArrayExample.cs b/Examples/RenderTexture2DArrayExample.cs index c42a9df..322eb73 100644 --- a/Examples/RenderTexture2DArrayExample.cs +++ b/Examples/RenderTexture2DArrayExample.cs @@ -22,6 +22,8 @@ class RenderTexture2DArrayExample : Example Window = window; GraphicsDevice = graphicsDevice; + Window.SetTitle("RenderTexture2DArray"); + RenderTarget = Texture.CreateTexture2DArray( GraphicsDevice, 16, diff --git a/Examples/RenderTexture2DExample.cs b/Examples/RenderTexture2DExample.cs new file mode 100644 index 0000000..1904ce3 --- /dev/null +++ b/Examples/RenderTexture2DExample.cs @@ -0,0 +1,118 @@ +using System; +using MoonWorks; +using MoonWorks.Graphics; +using MoonWorks.Input; + +namespace MoonWorksGraphicsTests; + +class RenderTexture2DExample : Example +{ + private Texture[] textures = new Texture[4]; + + public override void Init(Window window, GraphicsDevice graphicsDevice, Inputs inputs) + { + Window = window; + GraphicsDevice = graphicsDevice; + + Window.SetTitle("RenderTexture2D"); + + for (int i = 0; i < textures.Length; i += 1) + { + textures[i] = Texture.CreateTexture2D( + GraphicsDevice, + Window.Width / 4, + Window.Height / 4, + TextureFormat.R8G8B8A8, + TextureUsageFlags.ColorTarget | TextureUsageFlags.Sampler + ); + } + } + + public override void Update(System.TimeSpan delta) { } + + public override void Draw(double alpha) + { + CommandBuffer cmdbuf = GraphicsDevice.AcquireCommandBuffer(); + Texture swapchainTexture = cmdbuf.AcquireSwapchainTexture(Window); + if (swapchainTexture != null) + { + var renderPass = cmdbuf.BeginRenderPass(new ColorAttachmentInfo(textures[0], false, Color.Red)); + cmdbuf.EndRenderPass(renderPass); + + renderPass = cmdbuf.BeginRenderPass(new ColorAttachmentInfo(textures[1], false, Color.Blue)); + cmdbuf.EndRenderPass(renderPass); + + renderPass = cmdbuf.BeginRenderPass(new ColorAttachmentInfo(textures[2], false, Color.Green)); + cmdbuf.EndRenderPass(renderPass); + + renderPass = cmdbuf.BeginRenderPass(new ColorAttachmentInfo(textures[3], false, Color.Yellow)); + cmdbuf.EndRenderPass(renderPass); + + cmdbuf.Blit( + textures[0], + new TextureRegion + { + TextureSlice = swapchainTexture, + Width = swapchainTexture.Width / 2, + Height = swapchainTexture.Height / 2, + Depth = 1 + }, + Filter.Nearest, + false + ); + + cmdbuf.Blit( + textures[1], + new TextureRegion + { + TextureSlice = swapchainTexture, + X = swapchainTexture.Width / 2, + Width = swapchainTexture.Width / 2, + Height = swapchainTexture.Height / 2, + Depth = 1 + }, + Filter.Nearest, + false + ); + + cmdbuf.Blit( + textures[2], + new TextureRegion + { + TextureSlice = swapchainTexture, + Y = swapchainTexture.Height / 2, + Width = swapchainTexture.Width / 2, + Height = swapchainTexture.Height / 2, + Depth = 1 + }, + Filter.Nearest, + false + ); + + cmdbuf.Blit( + textures[3], + new TextureRegion + { + TextureSlice = swapchainTexture, + X = swapchainTexture.Width / 2, + Y = swapchainTexture.Height / 2, + Width = swapchainTexture.Width / 2, + Height = swapchainTexture.Height / 2, + Depth = 1 + }, + Filter.Nearest, + false + ); + } + + GraphicsDevice.Submit(cmdbuf); + } + + public override void Destroy() + { + for (var i = 0; i < 4; i += 1) + { + textures[i].Dispose(); + } + } +} diff --git a/Examples/RenderTexture2DGame.cs b/Examples/RenderTexture2DGame.cs deleted file mode 100644 index 9b667a0..0000000 --- a/Examples/RenderTexture2DGame.cs +++ /dev/null @@ -1,129 +0,0 @@ -using MoonWorks; -using MoonWorks.Graphics; -using MoonWorks.Math.Float; - -namespace MoonWorks.Test -{ - class RenderTexture2DGame : Game - { - private GraphicsPipeline pipeline; - private GpuBuffer vertexBuffer; - private GpuBuffer indexBuffer; - private Texture[] textures = new Texture[4]; - private Sampler sampler; - - public RenderTexture2DGame() : 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("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 - var resourceUploader = new ResourceUploader(GraphicsDevice); - - vertexBuffer = resourceUploader.CreateBuffer( - [ - 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)), - ], - BufferUsageFlags.Vertex - ); - - indexBuffer = resourceUploader.CreateBuffer( - [ - 0, 1, 2, - 0, 2, 3, - ], - BufferUsageFlags.Index - ); - - resourceUploader.Upload(); - resourceUploader.Dispose(); - - 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 - ); - } - } - - 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], WriteOptions.Cycle, Color.Red)); - cmdbuf.EndRenderPass(); - - cmdbuf.BeginRenderPass(new ColorAttachmentInfo(textures[1], WriteOptions.Cycle, Color.Blue)); - cmdbuf.EndRenderPass(); - - cmdbuf.BeginRenderPass(new ColorAttachmentInfo(textures[2], WriteOptions.Cycle, Color.Green)); - cmdbuf.EndRenderPass(); - - cmdbuf.BeginRenderPass(new ColorAttachmentInfo(textures[3], WriteOptions.Cycle, Color.Yellow)); - cmdbuf.EndRenderPass(); - - cmdbuf.BeginRenderPass(new ColorAttachmentInfo(backbuffer, WriteOptions.Cycle, 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); - } - - cmdbuf.EndRenderPass(); - } - GraphicsDevice.Submit(cmdbuf); - } - - public static void Main(string[] args) - { - RenderTexture2DGame game = new RenderTexture2DGame(); - game.Run(); - } - } -} diff --git a/MoonWorksGraphicsTests.csproj b/MoonWorksGraphicsTests.csproj index 6aa57cf..5af8e50 100644 --- a/MoonWorksGraphicsTests.csproj +++ b/MoonWorksGraphicsTests.csproj @@ -41,6 +41,7 @@ + diff --git a/Program.cs b/Program.cs index 5c0d9e6..b20ac81 100644 --- a/Program.cs +++ b/Program.cs @@ -24,7 +24,8 @@ class Program : Game new InstancingAndOffsetsExample(), new MSAACubeExample(), new MSAAExample(), - new RenderTexture2DArrayExample() + new RenderTexture2DArrayExample(), + new RenderTexture2DExample() ]; int ExampleIndex = 0;