From 6dbcd65fbeaec8295782a6eb0f1df79f4b3229e5 Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Thu, 6 Jun 2024 15:56:42 -0700 Subject: [PATCH] TextureMipmapsExample --- Examples/TextureMipmapsExample.cs | 173 ++++++++++++++++++++++++++++++ Examples/TextureMipmapsGame.cs | 137 ----------------------- MoonWorksGraphicsTests.csproj | 1 + Program.cs | 3 +- 4 files changed, 176 insertions(+), 138 deletions(-) create mode 100644 Examples/TextureMipmapsExample.cs delete mode 100644 Examples/TextureMipmapsGame.cs diff --git a/Examples/TextureMipmapsExample.cs b/Examples/TextureMipmapsExample.cs new file mode 100644 index 0000000..e8ad0f3 --- /dev/null +++ b/Examples/TextureMipmapsExample.cs @@ -0,0 +1,173 @@ +using MoonWorks; +using MoonWorks.Graphics; +using MoonWorks.Input; +using MoonWorks.Math.Float; + +namespace MoonWorksGraphicsTests; + +class TextureMipmapsExample : Example +{ + private GraphicsPipeline Pipeline; + private GpuBuffer VertexBuffer; + private GpuBuffer IndexBuffer; + private Texture Texture; + private Sampler Sampler; + + private float scale = 0.5f; + + public override void Init(Window window, GraphicsDevice graphicsDevice, Inputs inputs) + { + Window = window; + GraphicsDevice = graphicsDevice; + Inputs = inputs; + + Window.SetTitle("TextureMipmaps"); + + Logger.LogInfo("Press Left and Right to shrink/expand the scale of the quad"); + + // Load the shaders + Shader vertShaderModule = new Shader( + GraphicsDevice, + TestUtils.GetShaderPath("TexturedQuadWithMatrix.vert"), + "main", + ShaderStage.Vertex, + ShaderFormat.SPIRV + ); + + Shader fragShaderModule = new Shader( + GraphicsDevice, + TestUtils.GetShaderPath("TexturedQuad.frag"), + "main", + ShaderStage.Fragment, + ShaderFormat.SPIRV + ); + + // Create the graphics pipeline + GraphicsPipelineCreateInfo pipelineCreateInfo = TestUtils.GetStandardGraphicsPipelineCreateInfo( + Window.SwapchainFormat, + vertShaderModule, + fragShaderModule + ); + pipelineCreateInfo.VertexInputState = VertexInputState.CreateSingleBinding(); + pipelineCreateInfo.VertexShaderResourceInfo = new GraphicsPipelineResourceInfo + { + UniformBufferCount = 1 + }; + pipelineCreateInfo.FragmentShaderResourceInfo = new GraphicsPipelineResourceInfo + { + SamplerCount = 1 + }; + Pipeline = new GraphicsPipeline(GraphicsDevice, pipelineCreateInfo); + + Sampler = new Sampler(GraphicsDevice, SamplerCreateInfo.PointClamp); + + // Create and populate the GPU resources + Texture = Texture.CreateTexture2D( + GraphicsDevice, + 256, + 256, + TextureFormat.R8G8B8A8, + TextureUsageFlags.Sampler, + 4 + ); + + 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 + ); + + // Set the various mip levels + for (uint i = 0; i < Texture.LevelCount; i += 1) + { + var w = Texture.Width >> (int) i; + var h = Texture.Height >> (int) i; + var region = new TextureRegion + { + TextureSlice = new TextureSlice + { + Texture = Texture, + Layer = 0, + MipLevel = i + }, + X = 0, + Y = 0, + Z = 0, + Width = w, + Height = h, + Depth = 1 + }; + + resourceUploader.SetTextureDataFromCompressed( + region, + TestUtils.GetTexturePath($"mip{i}.png") + ); + } + + resourceUploader.Upload(); + resourceUploader.Dispose(); + } + + public override void Update(System.TimeSpan delta) + { + if (TestUtils.CheckButtonDown(Inputs, TestUtils.ButtonType.Left)) + { + scale = System.MathF.Max(0.01f, scale - 0.01f); + } + + if (TestUtils.CheckButtonDown(Inputs, TestUtils.ButtonType.Right)) + { + scale = System.MathF.Min(1f, scale + 0.01f); + } + } + + public override void Draw(double alpha) + { + TransformVertexUniform vertUniforms = new TransformVertexUniform(Matrix4x4.CreateScale(scale, scale, 1)); + + 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(Texture, Sampler)); + renderPass.PushVertexUniformData(vertUniforms); + renderPass.DrawIndexedPrimitives(0, 0, 2); + cmdbuf.EndRenderPass(renderPass); + } + + GraphicsDevice.Submit(cmdbuf); + } + + public override void Destroy() + { + Pipeline.Dispose(); + VertexBuffer.Dispose(); + IndexBuffer.Dispose(); + Texture.Dispose(); + Sampler.Dispose(); + } +} diff --git a/Examples/TextureMipmapsGame.cs b/Examples/TextureMipmapsGame.cs deleted file mode 100644 index 42cd27b..0000000 --- a/Examples/TextureMipmapsGame.cs +++ /dev/null @@ -1,137 +0,0 @@ -using MoonWorks.Graphics; -using MoonWorks.Math.Float; - -namespace MoonWorks.Test -{ - class TextureMipmapsGame : Game - { - private GraphicsPipeline pipeline; - private GpuBuffer vertexBuffer; - private GpuBuffer indexBuffer; - private Texture texture; - private Sampler sampler; - - private float scale = 0.5f; - - public TextureMipmapsGame() : base(TestUtils.GetStandardWindowCreateInfo(), TestUtils.GetStandardFrameLimiterSettings(), TestUtils.PreferredBackends, 60, true) - { - Logger.LogInfo("Press Left and Right to shrink/expand the scale of the quad"); - - // Load the shaders - ShaderModule vertShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("TexturedQuadWithMatrix.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); - - sampler = new Sampler(GraphicsDevice, SamplerCreateInfo.PointClamp); - - // Create and populate the GPU resources - texture = Texture.CreateTexture2D( - GraphicsDevice, - 256, - 256, - TextureFormat.R8G8B8A8, - TextureUsageFlags.Sampler, - 4 - ); - - 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 - ); - - // Set the various mip levels - for (uint i = 0; i < texture.LevelCount; i += 1) - { - var w = texture.Width >> (int) i; - var h = texture.Height >> (int) i; - var region = new TextureRegion - { - TextureSlice = new TextureSlice - { - Texture = texture, - Layer = 0, - MipLevel = i - }, - X = 0, - Y = 0, - Z = 0, - Width = w, - Height = h, - Depth = 1 - }; - - resourceUploader.SetTextureDataFromCompressed( - region, - TestUtils.GetTexturePath($"mip{i}.png") - ); - } - - resourceUploader.Upload(); - resourceUploader.Dispose(); - } - - protected override void Update(System.TimeSpan delta) - { - if (TestUtils.CheckButtonDown(Inputs, TestUtils.ButtonType.Left)) - { - scale = System.MathF.Max(0.01f, scale - 0.01f); - } - - if (TestUtils.CheckButtonDown(Inputs, TestUtils.ButtonType.Right)) - { - scale = System.MathF.Min(1f, scale + 0.01f); - } - } - - protected override void Draw(double alpha) - { - TransformVertexUniform vertUniforms = new TransformVertexUniform(Matrix4x4.CreateScale(scale, scale, 1)); - - 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(texture, sampler)); - cmdbuf.PushVertexShaderUniforms(vertUniforms); - cmdbuf.DrawIndexedPrimitives(0, 0, 2); - cmdbuf.EndRenderPass(); - } - GraphicsDevice.Submit(cmdbuf); - } - - public static void Main(string[] args) - { - TextureMipmapsGame game = new TextureMipmapsGame(); - game.Run(); - } - } -} diff --git a/MoonWorksGraphicsTests.csproj b/MoonWorksGraphicsTests.csproj index 629316a..e3e0583 100644 --- a/MoonWorksGraphicsTests.csproj +++ b/MoonWorksGraphicsTests.csproj @@ -49,6 +49,7 @@ + diff --git a/Program.cs b/Program.cs index 3c4bd97..ba5e24a 100644 --- a/Program.cs +++ b/Program.cs @@ -32,7 +32,8 @@ class Program : Game new Texture3DCopyExample(), new Texture3DExample(), new TexturedAnimatedQuadExample(), - new TexturedQuadExample() + new TexturedQuadExample(), + new TextureMipmapsExample() ]; int ExampleIndex = 0;