MoonWorksGraphicsTests/Examples/ComputeUniformsGame.cs

133 lines
4.1 KiB
C#
Raw Normal View History

using MoonWorks;
using MoonWorks.Graphics;
using MoonWorks.Math.Float;
namespace MoonWorks.Test
{
2024-02-07 15:27:55 +00:00
class ComputeUniformsGame : Game
{
private GraphicsPipeline drawPipeline;
private Texture texture;
private Sampler sampler;
2024-02-23 20:03:46 +00:00
private GpuBuffer vertexBuffer;
2024-02-07 15:27:55 +00:00
struct GradientTextureComputeUniforms
{
public uint groupCountX;
public uint groupCountY;
public GradientTextureComputeUniforms(uint w, uint h)
{
groupCountX = w;
groupCountY = h;
}
}
2024-03-07 18:35:12 +00:00
public ComputeUniformsGame() : 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 gradientTextureComputeShaderModule = new ShaderModule(
GraphicsDevice,
TestUtils.GetShaderPath("GradientTexture.comp")
);
ComputePipeline gradientTextureComputePipeline = new ComputePipeline(
GraphicsDevice,
ComputeShaderInfo.Create<GradientTextureComputeUniforms>(gradientTextureComputeShaderModule, "main", 0, 1)
);
// 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(
MainWindow.SwapchainFormat,
vertShaderModule,
fragShaderModule
);
drawPipelineCreateInfo.VertexInputState = VertexInputState.CreateSingleBinding<PositionTextureVertex>();
drawPipelineCreateInfo.FragmentShaderInfo.SamplerBindingCount = 1;
drawPipeline = new GraphicsPipeline(
GraphicsDevice,
drawPipelineCreateInfo
);
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
2024-02-23 20:03:46 +00:00
var resourceUploader = new ResourceUploader(GraphicsDevice);
2024-02-23 20:03:46 +00:00
vertexBuffer = resourceUploader.CreateBuffer(
2024-02-23 20:03:46 +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-07 15:27:55 +00:00
CommandBuffer cmdbuf = GraphicsDevice.AcquireCommandBuffer();
GradientTextureComputeUniforms gradientUniforms = new GradientTextureComputeUniforms(
texture.Width / 8,
texture.Height / 8
);
2024-02-23 20:03:46 +00:00
cmdbuf.BeginComputePass();
2024-02-07 15:27:55 +00:00
cmdbuf.BindComputePipeline(gradientTextureComputePipeline);
2024-03-01 23:03:29 +00:00
cmdbuf.BindComputeTextures(new ComputeTextureBinding(texture, 0));
2024-02-23 20:03:46 +00:00
cmdbuf.PushComputeShaderUniforms(gradientUniforms);
cmdbuf.DispatchCompute(gradientUniforms.groupCountX, gradientUniforms.groupCountY, 1);
cmdbuf.EndComputePass();
2024-02-07 15:27:55 +00:00
GraphicsDevice.Submit(cmdbuf);
}
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 20:03:46 +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)
{
ComputeUniformsGame game = new ComputeUniformsGame();
game.Run();
}
}
}