using System;
using Microsoft.Toolkit.Diagnostics;
#if WINDOWS_UWP
namespace ComputeSharp.Uwp;
#else
namespace ComputeSharp.WinUI;
#endif
///
/// A simple implementation powered by a supplied shader type.
/// All rendered frames produced by this type will always be presented.
///
/// The type of shader to use to render frames.
public sealed class ShaderRunner : IShaderRunner
where T : struct, IPixelShader
{
///
/// The instance used to create shaders to run.
///
private readonly Func? shaderFactory;
///
/// The instance used to create shaders to run.
///
private readonly Func? statefulShaderFactory;
///
/// Creates a new instance that will create shader instances with
/// the default constructor. Only use this constructor if the shader doesn't require any additional
/// resources or other parameters being initialized before being dispatched on the GPU.
///
public ShaderRunner()
{
}
///
/// Creates a new instance.
///
/// The instance used to create shaders to run.
public ShaderRunner(Func shaderFactory)
{
Guard.IsNotNull(shaderFactory, nameof(shaderFactory));
this.shaderFactory = shaderFactory;
}
///
/// Creates a new instance.
///
/// The instance used to create shaders to run.
public ShaderRunner(Func shaderFactory)
{
Guard.IsNotNull(shaderFactory, nameof(shaderFactory));
this.statefulShaderFactory = shaderFactory;
}
///
public bool TryExecute(IReadWriteTexture2D texture, TimeSpan time, object? parameter)
{
if (this.shaderFactory is Func shaderFactory)
{
GraphicsDevice.Default.ForEach(texture, this.shaderFactory(time));
}
else
{
GraphicsDevice.Default.ForEach(texture, this.statefulShaderFactory!(time, parameter));
}
return true;
}
}