update BasicCompute

refresh2
cosmonaut 2024-02-23 10:57:54 -08:00
parent d8f402646a
commit d6ec93525e
1 changed files with 32 additions and 19 deletions

View File

@ -9,7 +9,7 @@ namespace MoonWorks.Test
private GraphicsPipeline drawPipeline; private GraphicsPipeline drawPipeline;
private Texture texture; private Texture texture;
private Sampler sampler; private Sampler sampler;
private Buffer vertexBuffer; private GpuBuffer vertexBuffer;
public BasicComputeGame() : base(TestUtils.GetStandardWindowCreateInfo(), TestUtils.GetStandardFrameLimiterSettings(), 60, true) public BasicComputeGame() : base(TestUtils.GetStandardWindowCreateInfo(), TestUtils.GetStandardFrameLimiterSettings(), 60, true)
{ {
@ -61,16 +61,15 @@ namespace MoonWorks.Test
// Create buffers and textures // Create buffers and textures
uint[] squares = new uint[64]; uint[] squares = new uint[64];
Buffer squaresBuffer = Buffer.Create<uint>( GpuBuffer squaresBuffer = GpuBuffer.Create<uint>(
GraphicsDevice, GraphicsDevice,
BufferUsageFlags.Compute, BufferUsageFlags.Compute,
(uint) squares.Length (uint) squares.Length
); );
vertexBuffer = Buffer.Create<PositionTextureVertex>( TransferBuffer transferBuffer = new TransferBuffer(
GraphicsDevice, GraphicsDevice,
BufferUsageFlags.Vertex, squaresBuffer.Size
6
); );
texture = Texture.CreateTexture2D( texture = Texture.CreateTexture2D(
@ -84,34 +83,48 @@ namespace MoonWorks.Test
sampler = new Sampler(GraphicsDevice, new SamplerCreateInfo()); sampler = new Sampler(GraphicsDevice, new SamplerCreateInfo());
// Upload GPU resources and dispatch compute work // 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(); CommandBuffer cmdbuf = GraphicsDevice.AcquireCommandBuffer();
cmdbuf.SetBufferData(vertexBuffer, new PositionTextureVertex[] cmdbuf.BeginComputePass();
{
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)),
});
// This should result in a bright yellow texture! // This should result in a bright yellow texture!
cmdbuf.BindComputePipeline(fillTextureComputePipeline); cmdbuf.BindComputePipeline(fillTextureComputePipeline);
cmdbuf.BindComputeTextures(texture); cmdbuf.BindComputeTextures(new TextureLevelBinding(texture, 0));
cmdbuf.DispatchCompute(texture.Width / 8, texture.Height / 8, 1, 0); cmdbuf.DispatchCompute(texture.Width / 8, texture.Height / 8, 1);
// This calculates the squares of the first N integers! // This calculates the squares of the first N integers!
cmdbuf.BindComputePipeline(calculateSquaresComputePipeline); cmdbuf.BindComputePipeline(calculateSquaresComputePipeline);
cmdbuf.BindComputeBuffers(squaresBuffer); 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); var fence = GraphicsDevice.SubmitAndAcquireFence(cmdbuf);
GraphicsDevice.WaitForFences(fence); GraphicsDevice.WaitForFences(fence);
GraphicsDevice.ReleaseFence(fence); GraphicsDevice.ReleaseFence(fence);
// Print the squares! // Print the squares!
squaresBuffer.GetData(squares); transferBuffer.GetData<uint>(squares, 0);
Logger.LogInfo("Squares of the first " + squares.Length + " integers: " + string.Join(", ", squares)); Logger.LogInfo("Squares of the first " + squares.Length + " integers: " + string.Join(", ", squares));
} }
@ -127,7 +140,7 @@ namespace MoonWorks.Test
cmdbuf.BindGraphicsPipeline(drawPipeline); cmdbuf.BindGraphicsPipeline(drawPipeline);
cmdbuf.BindFragmentSamplers(new TextureSamplerBinding(texture, sampler)); cmdbuf.BindFragmentSamplers(new TextureSamplerBinding(texture, sampler));
cmdbuf.BindVertexBuffers(vertexBuffer); cmdbuf.BindVertexBuffers(vertexBuffer);
cmdbuf.DrawPrimitives(0, 2, 0, 0); cmdbuf.DrawPrimitives(0, 2);
cmdbuf.EndRenderPass(); cmdbuf.EndRenderPass();
} }
GraphicsDevice.Submit(cmdbuf); GraphicsDevice.Submit(cmdbuf);