diff --git a/Examples/TriangleVertexBufferExample.cs b/Examples/TriangleVertexBufferExample.cs new file mode 100644 index 0000000..c74d479 --- /dev/null +++ b/Examples/TriangleVertexBufferExample.cs @@ -0,0 +1,90 @@ +using MoonWorks; +using MoonWorks.Graphics; +using MoonWorks.Input; +using MoonWorks.Math.Float; + +namespace MoonWorksGraphicsTests; + +class TriangleVertexBufferExample : Example +{ + private GraphicsPipeline Pipeline; + private GpuBuffer VertexBuffer; + + public override void Init(Window window, GraphicsDevice graphicsDevice, Inputs inputs) + { + Window = window; + GraphicsDevice = graphicsDevice; + + Window.SetTitle("TriangleVertexBuffer"); + + // Load the shaders + Shader vertShader = new Shader( + GraphicsDevice, + TestUtils.GetShaderPath("PositionColor.vert"), + "main", + ShaderStage.Vertex, + ShaderFormat.SPIRV + ); + Shader fragShader = new Shader( + GraphicsDevice, + TestUtils.GetShaderPath("SolidColor.frag"), + "main", + ShaderStage.Fragment, + ShaderFormat.SPIRV + ); + + // Create the graphics pipeline + GraphicsPipelineCreateInfo pipelineCreateInfo = TestUtils.GetStandardGraphicsPipelineCreateInfo( + Window.SwapchainFormat, + vertShader, + fragShader + ); + pipelineCreateInfo.VertexInputState = VertexInputState.CreateSingleBinding(); + Pipeline = new GraphicsPipeline(GraphicsDevice, pipelineCreateInfo); + + // Create and populate the vertex buffer + var resourceUploader = new ResourceUploader(GraphicsDevice); + + VertexBuffer = resourceUploader.CreateBuffer( + [ + new PositionColorVertex(new Vector3(-1, -1, 0), Color.Red), + new PositionColorVertex(new Vector3( 1, -1, 0), Color.Lime), + new PositionColorVertex(new Vector3( 0, 1, 0), Color.Blue), + ], + BufferUsageFlags.Vertex + ); + + 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.DrawPrimitives(0, 1); + cmdbuf.EndRenderPass(renderPass); + } + + GraphicsDevice.Submit(cmdbuf); + } + + public override void Destroy() + { + Pipeline.Dispose(); + VertexBuffer.Dispose(); + } +} diff --git a/Examples/TriangleVertexBufferGame.cs b/Examples/TriangleVertexBufferGame.cs deleted file mode 100644 index 39ed4b8..0000000 --- a/Examples/TriangleVertexBufferGame.cs +++ /dev/null @@ -1,66 +0,0 @@ -using MoonWorks; -using MoonWorks.Graphics; -using MoonWorks.Math.Float; - -namespace MoonWorks.Test -{ - class TriangleVertexBufferGame : Game - { - private GraphicsPipeline pipeline; - private GpuBuffer vertexBuffer; - - public TriangleVertexBufferGame() : base(TestUtils.GetStandardWindowCreateInfo(), TestUtils.GetStandardFrameLimiterSettings(), TestUtils.PreferredBackends, 60, true) - { - // Load the shaders - ShaderModule vertShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("PositionColor.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(); - pipeline = new GraphicsPipeline(GraphicsDevice, pipelineCreateInfo); - - // Create and populate the vertex buffer - var resourceUploader = new ResourceUploader(GraphicsDevice); - - vertexBuffer = resourceUploader.CreateBuffer( - [ - new PositionColorVertex(new Vector3(-1, 1, 0), Color.Red), - new PositionColorVertex(new Vector3(1, 1, 0), Color.Lime), - new PositionColorVertex(new Vector3(0, -1, 0), Color.Blue), - ], - BufferUsageFlags.Vertex - ); - - 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.DrawPrimitives(0, 1); - cmdbuf.EndRenderPass(); - } - GraphicsDevice.Submit(cmdbuf); - } - - public static void Main(string[] args) - { - TriangleVertexBufferGame p = new TriangleVertexBufferGame(); - p.Run(); - } - } -} diff --git a/MoonWorksGraphicsTests.csproj b/MoonWorksGraphicsTests.csproj index e3e0583..17d6e0b 100644 --- a/MoonWorksGraphicsTests.csproj +++ b/MoonWorksGraphicsTests.csproj @@ -50,6 +50,7 @@ + diff --git a/Program.cs b/Program.cs index ba5e24a..38c8277 100644 --- a/Program.cs +++ b/Program.cs @@ -33,7 +33,8 @@ class Program : Game new Texture3DExample(), new TexturedAnimatedQuadExample(), new TexturedQuadExample(), - new TextureMipmapsExample() + new TextureMipmapsExample(), + new TriangleVertexBufferExample() ]; int ExampleIndex = 0;