From d6ec93525ed139984453162b39dba5f4d546ade5 Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Fri, 23 Feb 2024 10:57:54 -0800 Subject: [PATCH] update BasicCompute --- BasicCompute/BasicComputeGame.cs | 51 ++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 19 deletions(-) diff --git a/BasicCompute/BasicComputeGame.cs b/BasicCompute/BasicComputeGame.cs index 17f9306..3f30641 100644 --- a/BasicCompute/BasicComputeGame.cs +++ b/BasicCompute/BasicComputeGame.cs @@ -9,7 +9,7 @@ namespace MoonWorks.Test private GraphicsPipeline drawPipeline; private Texture texture; private Sampler sampler; - private Buffer vertexBuffer; + private GpuBuffer vertexBuffer; public BasicComputeGame() : base(TestUtils.GetStandardWindowCreateInfo(), TestUtils.GetStandardFrameLimiterSettings(), 60, true) { @@ -61,16 +61,15 @@ namespace MoonWorks.Test // Create buffers and textures uint[] squares = new uint[64]; - Buffer squaresBuffer = Buffer.Create( + GpuBuffer squaresBuffer = GpuBuffer.Create( GraphicsDevice, BufferUsageFlags.Compute, (uint) squares.Length ); - vertexBuffer = Buffer.Create( + TransferBuffer transferBuffer = new TransferBuffer( GraphicsDevice, - BufferUsageFlags.Vertex, - 6 + squaresBuffer.Size ); texture = Texture.CreateTexture2D( @@ -84,34 +83,48 @@ namespace MoonWorks.Test sampler = new Sampler(GraphicsDevice, new SamplerCreateInfo()); // Upload GPU resources and dispatch compute work + var resourceInitializer = new ResourceInitializer(GraphicsDevice); + vertexBuffer = resourceInitializer.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, 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 + ); + + resourceInitializer.Upload(); + resourceInitializer.Dispose(); + CommandBuffer cmdbuf = GraphicsDevice.AcquireCommandBuffer(); - cmdbuf.SetBufferData(vertexBuffer, new PositionTextureVertex[] - { - 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, 0)), - new PositionTextureVertex(new Vector3(1, 1, 0), new Vector2(1, 1)), - new PositionTextureVertex(new Vector3(-1, 1, 0), new Vector2(0, 1)), - }); + cmdbuf.BeginComputePass(); // This should result in a bright yellow texture! cmdbuf.BindComputePipeline(fillTextureComputePipeline); - cmdbuf.BindComputeTextures(texture); - cmdbuf.DispatchCompute(texture.Width / 8, texture.Height / 8, 1, 0); + cmdbuf.BindComputeTextures(new TextureLevelBinding(texture, 0)); + cmdbuf.DispatchCompute(texture.Width / 8, texture.Height / 8, 1); // This calculates the squares of the first N integers! cmdbuf.BindComputePipeline(calculateSquaresComputePipeline); cmdbuf.BindComputeBuffers(squaresBuffer); - cmdbuf.DispatchCompute((uint) squares.Length / 8, 1, 1, 0); + cmdbuf.DispatchCompute((uint) squares.Length / 8, 1, 1); + + cmdbuf.EndComputePass(); + + cmdbuf.BeginCopyPass(); + cmdbuf.DownloadFromBuffer(squaresBuffer, transferBuffer); + cmdbuf.EndCopyPass(); var fence = GraphicsDevice.SubmitAndAcquireFence(cmdbuf); GraphicsDevice.WaitForFences(fence); GraphicsDevice.ReleaseFence(fence); // Print the squares! - squaresBuffer.GetData(squares); + transferBuffer.GetData(squares, 0); Logger.LogInfo("Squares of the first " + squares.Length + " integers: " + string.Join(", ", squares)); } @@ -127,7 +140,7 @@ namespace MoonWorks.Test cmdbuf.BindGraphicsPipeline(drawPipeline); cmdbuf.BindFragmentSamplers(new TextureSamplerBinding(texture, sampler)); cmdbuf.BindVertexBuffers(vertexBuffer); - cmdbuf.DrawPrimitives(0, 2, 0, 0); + cmdbuf.DrawPrimitives(0, 2); cmdbuf.EndRenderPass(); } GraphicsDevice.Submit(cmdbuf);