MoonWorksGraphicsTests/Examples/ComputeUniformsExample.cs

81 lines
2.0 KiB
C#
Raw Normal View History

2024-06-06 05:47:06 +00:00
using MoonWorks;
using MoonWorks.Graphics;
using MoonWorks.Input;
namespace MoonWorksGraphicsTests;
class ComputeUniformsExample : Example
{
private ComputePipeline GradientPipeline;
private Texture RenderTexture;
record struct GradientTextureComputeUniforms(float Time);
private GradientTextureComputeUniforms Uniforms;
public override void Init(Window window, GraphicsDevice graphicsDevice, Inputs inputs)
{
Window = window;
GraphicsDevice = graphicsDevice;
Window.SetTitle("ComputeUniforms");
Uniforms.Time = 0;
// Create the compute pipeline that writes texture data
GradientPipeline = new ComputePipeline(
2024-06-06 05:47:06 +00:00
GraphicsDevice,
TestUtils.GetShaderPath("GradientTexture.comp"),
"main",
new ComputePipelineCreateInfo
{
ShaderFormat = ShaderFormat.SPIRV,
2024-06-06 05:47:06 +00:00
ReadWriteStorageTextureCount = 1,
UniformBufferCount = 1,
ThreadCountX = 8,
ThreadCountY = 8,
ThreadCountZ = 1
2024-06-06 05:47:06 +00:00
}
);
RenderTexture = Texture.CreateTexture2D(
GraphicsDevice,
Window.Width,
Window.Height,
TextureFormat.R8G8B8A8,
TextureUsageFlags.ComputeStorageWrite | TextureUsageFlags.Sampler
);
}
public override void Update(System.TimeSpan delta)
{
Uniforms.Time += (float) delta.TotalSeconds;
}
public override void Draw(double alpha)
{
CommandBuffer cmdbuf = GraphicsDevice.AcquireCommandBuffer();
Texture swapchainTexture = cmdbuf.AcquireSwapchainTexture(Window);
if (swapchainTexture != null)
{
var computePass = cmdbuf.BeginComputePass(new StorageTextureReadWriteBinding
{
TextureSlice = RenderTexture,
Cycle = true
});
computePass.BindComputePipeline(GradientPipeline);
computePass.PushUniformData(Uniforms);
computePass.Dispatch(RenderTexture.Width / 8, RenderTexture.Height / 8, 1);
cmdbuf.EndComputePass(computePass);
cmdbuf.Blit(RenderTexture, swapchainTexture, Filter.Linear, false);
}
GraphicsDevice.Submit(cmdbuf);
}
public override void Destroy()
{
GradientPipeline.Dispose();
RenderTexture.Dispose();
}
}