using System; namespace ComputeSharp.WinUI; /// /// 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, IComputeShader { /// /// 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) { default(ArgumentNullException).ThrowIfNull(shaderFactory); this.shaderFactory = shaderFactory; } /// /// Creates a new instance. /// /// The instance used to create shaders to run. public ShaderRunner(Func shaderFactory) { default(ArgumentNullException).ThrowIfNull(shaderFactory); this.statefulShaderFactory = shaderFactory; } /// public bool TryExecute(IReadWriteNormalizedTexture2D texture, TimeSpan time, object? parameter) { if (this.shaderFactory is Func shaderFactory) { texture.GraphicsDevice.ForEach(texture, this.shaderFactory(time)); } else { texture.GraphicsDevice.ForEach(texture, this.statefulShaderFactory!(time, parameter)); } return true; } }