From 2cfc8fa51234f1504b039f21fb3823880453ab6f Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Thu, 6 Jun 2024 16:05:36 -0700 Subject: [PATCH] VertexSamplerExample --- Examples/VertexSamplerExample.cs | 108 +++++++++++++++++++++++++++++++ Examples/VertexSamplerGame.cs | 79 ---------------------- MoonWorksGraphicsTests.csproj | 1 + Program.cs | 3 +- 4 files changed, 111 insertions(+), 80 deletions(-) create mode 100644 Examples/VertexSamplerExample.cs delete mode 100644 Examples/VertexSamplerGame.cs diff --git a/Examples/VertexSamplerExample.cs b/Examples/VertexSamplerExample.cs new file mode 100644 index 0000000..c4e0748 --- /dev/null +++ b/Examples/VertexSamplerExample.cs @@ -0,0 +1,108 @@ +using System; +using MoonWorks; +using MoonWorks.Graphics; +using MoonWorks.Input; +using MoonWorks.Math.Float; + +namespace MoonWorksGraphicsTests; + +class VertexSamplerExample : Example +{ + private GraphicsPipeline Pipeline; + private GpuBuffer VertexBuffer; + private Texture Texture; + private Sampler Sampler; + + public override void Init(Window window, GraphicsDevice graphicsDevice, Inputs inputs) + { + Window = window; + GraphicsDevice = graphicsDevice; + + Window.SetTitle("VertexSampler"); + + // Load the shaders + Shader vertShaderModule = new Shader( + GraphicsDevice, + TestUtils.GetShaderPath("PositionSampler.vert"), + "main", + ShaderStage.Vertex, + ShaderFormat.SPIRV + ); + + Shader fragShaderModule = new Shader( + GraphicsDevice, + TestUtils.GetShaderPath("SolidColor.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 + { + SamplerCount = 1 + }; + Pipeline = new GraphicsPipeline(GraphicsDevice, pipelineCreateInfo); + + // Create and populate the GPU resources + Sampler = new Sampler(GraphicsDevice, SamplerCreateInfo.PointClamp); + + 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(0.334f, 0)), + new PositionTextureVertex(new Vector3( 0, 1, 0), new Vector2(0.667f, 0)), + ], + BufferUsageFlags.Vertex + ); + + Texture = resourceUploader.CreateTexture2D( + new Span([Color.Yellow, Color.Indigo, Color.HotPink]), + 3, + 1 + ); + + resourceUploader.Upload(); + resourceUploader.Dispose(); + } + + 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( + swapchainTexture, + false, + Color.Black + ) + ); + renderPass.BindGraphicsPipeline(Pipeline); + renderPass.BindVertexBuffer(VertexBuffer); + renderPass.BindVertexSampler(new TextureSamplerBinding(Texture, Sampler)); + renderPass.DrawPrimitives(0, 1); + cmdbuf.EndRenderPass(renderPass); + } + GraphicsDevice.Submit(cmdbuf); + } + + public override void Destroy() + { + Pipeline.Dispose(); + VertexBuffer.Dispose(); + Texture.Dispose(); + Sampler.Dispose(); + } +} diff --git a/Examples/VertexSamplerGame.cs b/Examples/VertexSamplerGame.cs deleted file mode 100644 index 888899b..0000000 --- a/Examples/VertexSamplerGame.cs +++ /dev/null @@ -1,79 +0,0 @@ -using System; -using MoonWorks; -using MoonWorks.Graphics; -using MoonWorks.Math.Float; - -namespace MoonWorks.Test -{ - class VertexSamplerGame : Game - { - private GraphicsPipeline pipeline; - private GpuBuffer vertexBuffer; - private Texture texture; - private Sampler sampler; - - public VertexSamplerGame() : base(TestUtils.GetStandardWindowCreateInfo(), TestUtils.GetStandardFrameLimiterSettings(), TestUtils.PreferredBackends, 60, true) - { - // Load the shaders - ShaderModule vertShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("PositionSampler.vert")); - ShaderModule fragShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("SolidColor.frag")); - - // Create the graphics pipeline - GraphicsPipelineCreateInfo pipelineCreateInfo = TestUtils.GetStandardGraphicsPipelineCreateInfo( - MainWindow.SwapchainFormat, - vertShaderModule, - fragShaderModule - ); - pipelineCreateInfo.VertexInputState = VertexInputState.CreateSingleBinding(); - pipelineCreateInfo.VertexShaderInfo.SamplerBindingCount = 1; - pipeline = new GraphicsPipeline(GraphicsDevice, pipelineCreateInfo); - - // Create and populate the GPU resources - sampler = new Sampler(GraphicsDevice, SamplerCreateInfo.PointClamp); - - 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(0.334f, 0)), - new PositionTextureVertex(new Vector3(0, -1, 0), new Vector2(0.667f, 0)), - ], - BufferUsageFlags.Vertex - ); - - texture = resourceUploader.CreateTexture2D( - new Span([Color.Yellow, Color.Indigo, Color.HotPink]), - 3, - 1 - ); - - resourceUploader.Upload(); - resourceUploader.Dispose(); - } - - 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(backbuffer, WriteOptions.Cycle, Color.Black)); - cmdbuf.BindGraphicsPipeline(pipeline); - cmdbuf.BindVertexBuffers(vertexBuffer); - cmdbuf.BindVertexSamplers(new TextureSamplerBinding(texture, sampler)); - cmdbuf.DrawPrimitives(0, 1); - cmdbuf.EndRenderPass(); - } - GraphicsDevice.Submit(cmdbuf); - } - - public static void Main(string[] args) - { - VertexSamplerGame p = new VertexSamplerGame(); - p.Run(); - } - } -} diff --git a/MoonWorksGraphicsTests.csproj b/MoonWorksGraphicsTests.csproj index 17d6e0b..fefc2d7 100644 --- a/MoonWorksGraphicsTests.csproj +++ b/MoonWorksGraphicsTests.csproj @@ -51,6 +51,7 @@ + diff --git a/Program.cs b/Program.cs index 38c8277..2faf4d4 100644 --- a/Program.cs +++ b/Program.cs @@ -34,7 +34,8 @@ class Program : Game new TexturedAnimatedQuadExample(), new TexturedQuadExample(), new TextureMipmapsExample(), - new TriangleVertexBufferExample() + new TriangleVertexBufferExample(), + new VertexSamplerExample() ]; int ExampleIndex = 0;