MoonWorksGraphicsTests/BasicCompute/BasicComputeGame.cs

153 lines
5.0 KiB
C#
Raw Normal View History

2022-11-12 23:20:02 +00:00
using MoonWorks;
using MoonWorks.Graphics;
using MoonWorks.Math.Float;
namespace MoonWorks.Test
{
2024-02-07 15:27:55 +00:00
class BasicComputeGame : Game
{
private GraphicsPipeline drawPipeline;
private Texture texture;
private Sampler sampler;
2024-02-23 18:57:54 +00:00
private GpuBuffer vertexBuffer;
2024-02-07 15:27:55 +00:00
2024-03-07 18:35:12 +00:00
public BasicComputeGame() : base(TestUtils.GetStandardWindowCreateInfo(), TestUtils.GetStandardFrameLimiterSettings(), TestUtils.PreferredBackends, 60, true)
2024-02-07 15:27:55 +00:00
{
// Create the compute pipeline that writes texture data
ShaderModule fillTextureComputeShaderModule = new ShaderModule(
GraphicsDevice,
TestUtils.GetShaderPath("FillTexture.comp")
);
ComputePipeline fillTextureComputePipeline = new ComputePipeline(
GraphicsDevice,
ComputeShaderInfo.Create(fillTextureComputeShaderModule, "main", 0, 1)
);
// Create the compute pipeline that calculates squares of numbers
ShaderModule calculateSquaresComputeShaderModule = new ShaderModule(
GraphicsDevice,
TestUtils.GetShaderPath("CalculateSquares.comp")
);
ComputePipeline calculateSquaresComputePipeline = new ComputePipeline(
GraphicsDevice,
ComputeShaderInfo.Create(calculateSquaresComputeShaderModule, "main", 1, 0)
);
// Create the graphics pipeline
ShaderModule vertShaderModule = new ShaderModule(
GraphicsDevice,
TestUtils.GetShaderPath("TexturedQuad.vert")
);
ShaderModule fragShaderModule = new ShaderModule(
GraphicsDevice,
TestUtils.GetShaderPath("TexturedQuad.frag")
);
GraphicsPipelineCreateInfo drawPipelineCreateInfo = TestUtils.GetStandardGraphicsPipelineCreateInfo(
2022-11-17 20:33:58 +00:00
MainWindow.SwapchainFormat,
2024-02-07 15:27:55 +00:00
vertShaderModule,
fragShaderModule
);
drawPipelineCreateInfo.VertexInputState = VertexInputState.CreateSingleBinding<PositionTextureVertex>();
drawPipelineCreateInfo.FragmentShaderInfo.SamplerBindingCount = 1;
drawPipeline = new GraphicsPipeline(
GraphicsDevice,
drawPipelineCreateInfo
);
// Create buffers and textures
uint[] squares = new uint[64];
2024-02-23 18:57:54 +00:00
GpuBuffer squaresBuffer = GpuBuffer.Create<uint>(
2024-02-07 15:27:55 +00:00
GraphicsDevice,
BufferUsageFlags.Compute,
(uint) squares.Length
);
2024-02-23 18:57:54 +00:00
TransferBuffer transferBuffer = new TransferBuffer(
2024-02-07 15:27:55 +00:00
GraphicsDevice,
2024-02-23 18:57:54 +00:00
squaresBuffer.Size
2024-02-07 15:27:55 +00:00
);
texture = Texture.CreateTexture2D(
GraphicsDevice,
MainWindow.Width,
MainWindow.Height,
TextureFormat.R8G8B8A8,
TextureUsageFlags.Compute | TextureUsageFlags.Sampler
);
sampler = new Sampler(GraphicsDevice, new SamplerCreateInfo());
// Upload GPU resources and dispatch compute work
var resourceUploader = new ResourceUploader(GraphicsDevice);
vertexBuffer = resourceUploader.CreateBuffer(
2024-02-23 18:57:54 +00:00
[
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();
2024-02-23 18:57:54 +00:00
2024-02-07 15:27:55 +00:00
CommandBuffer cmdbuf = GraphicsDevice.AcquireCommandBuffer();
2024-02-23 18:57:54 +00:00
cmdbuf.BeginComputePass();
2024-02-07 15:27:55 +00:00
// This should result in a bright yellow texture!
cmdbuf.BindComputePipeline(fillTextureComputePipeline);
2024-03-01 23:03:29 +00:00
cmdbuf.BindComputeTextures(new ComputeTextureBinding(texture, WriteOptions.SafeOverwrite));
2024-02-23 18:57:54 +00:00
cmdbuf.DispatchCompute(texture.Width / 8, texture.Height / 8, 1);
2024-02-07 15:27:55 +00:00
// This calculates the squares of the first N integers!
cmdbuf.BindComputePipeline(calculateSquaresComputePipeline);
2024-03-01 23:03:29 +00:00
cmdbuf.BindComputeBuffers(new ComputeBufferBinding(squaresBuffer, WriteOptions.SafeOverwrite));
2024-02-23 18:57:54 +00:00
cmdbuf.DispatchCompute((uint) squares.Length / 8, 1, 1);
cmdbuf.EndComputePass();
2024-02-07 15:27:55 +00:00
var fence = GraphicsDevice.SubmitAndAcquireFence(cmdbuf);
GraphicsDevice.WaitForFences(fence);
2023-09-19 06:28:32 +00:00
GraphicsDevice.ReleaseFence(fence);
2024-02-07 15:27:55 +00:00
// Print the squares!
GraphicsDevice.DownloadFromBuffer(squaresBuffer, transferBuffer, TransferOptions.Overwrite);
2024-02-23 18:57:54 +00:00
transferBuffer.GetData<uint>(squares, 0);
2024-02-07 15:27:55 +00:00
Logger.LogInfo("Squares of the first " + squares.Length + " integers: " + string.Join(", ", squares));
}
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)
{
2024-03-07 22:24:54 +00:00
cmdbuf.BeginRenderPass(new ColorAttachmentInfo(backbuffer, WriteOptions.Cycle, Color.CornflowerBlue));
2024-02-07 15:27:55 +00:00
cmdbuf.BindGraphicsPipeline(drawPipeline);
cmdbuf.BindFragmentSamplers(new TextureSamplerBinding(texture, sampler));
cmdbuf.BindVertexBuffers(vertexBuffer);
2024-02-23 18:57:54 +00:00
cmdbuf.DrawPrimitives(0, 2);
2024-02-07 15:27:55 +00:00
cmdbuf.EndRenderPass();
}
GraphicsDevice.Submit(cmdbuf);
}
public static void Main(string[] args)
{
BasicComputeGame game = new BasicComputeGame();
game.Run();
}
}
2022-11-12 23:20:02 +00:00
}