MoonWorksGraphicsTests/Examples/BasicComputeExample.cs

197 lines
5.5 KiB
C#
Raw Permalink Normal View History

2024-06-05 21:19:43 +00:00
using MoonWorks;
using MoonWorks.Graphics;
2024-06-06 05:47:06 +00:00
using MoonWorks.Input;
2024-06-05 21:19:43 +00:00
using MoonWorks.Math.Float;
namespace MoonWorksGraphicsTests;
class BasicComputeExample : Example
{
private GraphicsPipeline DrawPipeline;
private Texture Texture;
private Sampler Sampler;
2024-06-11 17:08:37 +00:00
private Buffer VertexBuffer;
2024-06-05 21:19:43 +00:00
2024-06-06 05:47:06 +00:00
public override void Init(Window window, GraphicsDevice graphicsDevice, Inputs inputs)
2024-06-05 21:19:43 +00:00
{
Window = window;
GraphicsDevice = graphicsDevice;
2024-06-06 05:56:09 +00:00
Window.SetTitle("BasicCompute");
// Create the compute pipeline that writes texture data
ComputePipeline fillTextureComputePipeline = new ComputePipeline(
2024-06-05 21:19:43 +00:00
GraphicsDevice,
TestUtils.GetShaderPath("FillTexture.comp"),
"main",
new ComputePipelineCreateInfo
{
ShaderFormat = ShaderFormat.SPIRV,
ReadWriteStorageTextureCount = 1,
ThreadCountX = 8,
ThreadCountY = 8,
ThreadCountZ = 1
}
2024-06-05 21:19:43 +00:00
);
// Create the compute pipeline that calculates squares of numbers
ComputePipeline calculateSquaresComputePipeline = new ComputePipeline(
2024-06-05 21:19:43 +00:00
GraphicsDevice,
TestUtils.GetShaderPath("CalculateSquares.comp"),
"main",
new ComputePipelineCreateInfo
{
ShaderFormat = ShaderFormat.SPIRV,
ReadWriteStorageBufferCount = 1,
ThreadCountX = 8,
ThreadCountY = 1,
ThreadCountZ = 1
}
2024-06-05 21:19:43 +00:00
);
// Create the graphics pipeline
Shader vertShader = new Shader(
2024-06-05 21:19:43 +00:00
GraphicsDevice,
TestUtils.GetShaderPath("TexturedQuad.vert"),
"main",
new ShaderCreateInfo
{
ShaderStage = ShaderStage.Vertex,
ShaderFormat = ShaderFormat.SPIRV
}
2024-06-05 21:19:43 +00:00
);
Shader fragShader = new Shader(
2024-06-05 21:19:43 +00:00
GraphicsDevice,
TestUtils.GetShaderPath("TexturedQuad.frag"),
"main",
new ShaderCreateInfo
{
ShaderStage = ShaderStage.Fragment,
ShaderFormat = ShaderFormat.SPIRV,
SamplerCount = 1
}
2024-06-05 21:19:43 +00:00
);
GraphicsPipelineCreateInfo drawPipelineCreateInfo = TestUtils.GetStandardGraphicsPipelineCreateInfo(
Window.SwapchainFormat,
vertShader,
fragShader
2024-06-05 21:19:43 +00:00
);
drawPipelineCreateInfo.VertexInputState = VertexInputState.CreateSingleBinding<PositionTextureVertex>();
DrawPipeline = new GraphicsPipeline(
GraphicsDevice,
drawPipelineCreateInfo
);
// Create buffers and textures
uint[] squares = new uint[64];
2024-06-11 17:08:37 +00:00
Buffer squaresBuffer = Buffer.Create<uint>(
2024-06-05 21:19:43 +00:00
GraphicsDevice,
BufferUsageFlags.ComputeStorageWrite,
(uint) squares.Length
);
TransferBuffer transferBuffer = new TransferBuffer(
GraphicsDevice,
TransferUsage.Buffer,
TransferBufferMapFlags.Read,
squaresBuffer.Size
);
Texture = Texture.CreateTexture2D(
GraphicsDevice,
Window.Width,
Window.Height,
TextureFormat.R8G8B8A8,
TextureUsageFlags.ComputeStorageWrite | TextureUsageFlags.Sampler
);
Sampler = new Sampler(GraphicsDevice, new SamplerCreateInfo());
// Upload GPU resources and dispatch compute work
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, 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
);
resourceUploader.Upload();
resourceUploader.Dispose();
CommandBuffer cmdbuf = GraphicsDevice.AcquireCommandBuffer();
// This should result in a bright yellow texture!
var computePass = cmdbuf.BeginComputePass(new StorageTextureReadWriteBinding
{
TextureSlice = Texture,
Cycle = false
});
computePass.BindComputePipeline(fillTextureComputePipeline);
computePass.Dispatch(Texture.Width / 8, Texture.Height / 8, 1);
cmdbuf.EndComputePass(computePass);
// This calculates the squares of the first N integers!
computePass = cmdbuf.BeginComputePass(new StorageBufferReadWriteBinding
{
Buffer = squaresBuffer,
Cycle = false
});
computePass.BindComputePipeline(calculateSquaresComputePipeline);
computePass.Dispatch((uint) squares.Length / 8, 1, 1);
cmdbuf.EndComputePass(computePass);
var copyPass = cmdbuf.BeginCopyPass();
copyPass.DownloadFromBuffer(squaresBuffer, transferBuffer, new BufferCopy(0, 0, squaresBuffer.Size));
cmdbuf.EndCopyPass(copyPass);
var fence = GraphicsDevice.SubmitAndAcquireFence(cmdbuf);
2024-06-06 17:52:12 +00:00
GraphicsDevice.WaitForFence(fence);
2024-06-05 21:19:43 +00:00
GraphicsDevice.ReleaseFence(fence);
// Print the squares!
transferBuffer.GetData<uint>(squares, 0);
Logger.LogInfo("Squares of the first " + squares.Length + " integers: " + string.Join(", ", squares));
}
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.CornflowerBlue));
renderPass.BindGraphicsPipeline(DrawPipeline);
renderPass.BindFragmentSampler(new TextureSamplerBinding(Texture, Sampler));
renderPass.BindVertexBuffer(VertexBuffer);
renderPass.DrawPrimitives(0, 2);
cmdbuf.EndRenderPass(renderPass);
}
GraphicsDevice.Submit(cmdbuf);
}
public override void Destroy()
{
DrawPipeline.Dispose();
Texture.Dispose();
Sampler.Dispose();
VertexBuffer.Dispose();
}
}