From 1494e41a53dfa2bb71ead787d22c0d9cbddf46ce Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 5 May 2021 19:35:54 +0200 Subject: [PATCH] Updated WinUI 3 sample to leverage IPixelShader --- .../ViewModels/MainViewModel.cs | 20 +++++++++---------- src/ComputeSharp.WinUI/ShaderRunner{T}.cs | 20 ++++++++++++++----- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/samples/ComputeSharp.SwapChain.WinUI/ComputeSharp.SwapChain.WinUI/ViewModels/MainViewModel.cs b/samples/ComputeSharp.SwapChain.WinUI/ComputeSharp.SwapChain.WinUI/ViewModels/MainViewModel.cs index 72838eb0..2bfe1dc7 100644 --- a/samples/ComputeSharp.SwapChain.WinUI/ComputeSharp.SwapChain.WinUI/ViewModels/MainViewModel.cs +++ b/samples/ComputeSharp.SwapChain.WinUI/ComputeSharp.SwapChain.WinUI/ViewModels/MainViewModel.cs @@ -1,8 +1,8 @@ using System.Collections.Generic; using ComputeSharp.SwapChain.Shaders; +using ComputeSharp.SwapChain.WinUI.Models; using ComputeSharp.WinUI; using CommunityToolkit.Mvvm.ComponentModel; -using ComputeSharp.SwapChain.WinUI.Models; using CommunityToolkit.Mvvm.Input; namespace ComputeSharp.SwapChain.WinUI.ViewModels @@ -59,15 +59,15 @@ namespace ComputeSharp.SwapChain.WinUI.ViewModels /// public IReadOnlyList ComputeShaderOptions { get; } = new ComputeShader[] { - new("Colorful infinity", new ShaderRunner(static (texture, time) => new(texture, (float)time.TotalSeconds))), - new("Extruded truchet", new ShaderRunner(static (texture, time) => new(texture, (float)time.TotalSeconds))), - new("Fractal tiling", new ShaderRunner(static (texture, time) => new(texture, (float)time.TotalSeconds))), - new("Menger Journey", new ShaderRunner(static (texture, time) => new(texture, (float)time.TotalSeconds))), - new("Octagrams", new ShaderRunner(static (texture, time) => new(texture, (float)time.TotalSeconds))), - new("Protean clouds", new ShaderRunner(static (texture, time) => new(texture, (float)time.TotalSeconds))), - new("Two tiled truchet", new ShaderRunner(static (texture, time) => new(texture, (float)time.TotalSeconds))), - new("Pyramid pattern", new ShaderRunner(static (texture, time) => new(texture, (float)time.TotalSeconds))), - new("Triangle grid contouring", new ShaderRunner(static (texture, time) => new(texture, (float)time.TotalSeconds))) + new("Colorful infinity", new ShaderRunner(static time => new((float)time.TotalSeconds))), + new("Extruded truchet", new ShaderRunner(static time => new((float)time.TotalSeconds))), + new("Fractal tiling", new ShaderRunner(static time => new((float)time.TotalSeconds))), + new("Menger Journey", new ShaderRunner(static time => new((float)time.TotalSeconds))), + new("Octagrams", new ShaderRunner(static time => new((float)time.TotalSeconds))), + new("Protean clouds", new ShaderRunner(static time => new((float)time.TotalSeconds))), + new("Two tiled truchet", new ShaderRunner(static time => new((float)time.TotalSeconds))), + new("Pyramid pattern", new ShaderRunner(static time => new((float)time.TotalSeconds))), + new("Triangle grid contouring", new ShaderRunner(static time => new((float)time.TotalSeconds))) }; private ComputeShader selectedComputeShader; diff --git a/src/ComputeSharp.WinUI/ShaderRunner{T}.cs b/src/ComputeSharp.WinUI/ShaderRunner{T}.cs index 54d8ca6f..c51232ff 100644 --- a/src/ComputeSharp.WinUI/ShaderRunner{T}.cs +++ b/src/ComputeSharp.WinUI/ShaderRunner{T}.cs @@ -7,18 +7,28 @@ namespace ComputeSharp.WinUI /// /// The type of shader to use to render frames. public sealed class ShaderRunner : IShaderRunner - where T : struct, IComputeShader + where T : struct, IPixelShader { /// - /// The instance used to create shaders to run. + /// The instance used to create shaders to run. /// - private readonly Func, TimeSpan, T> shaderFactory; + private readonly Func shaderFactory; + + /// + /// 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() + { + this.shaderFactory = static _ => default; + } /// /// Creates a new instance. /// /// The instance used to create shaders to run. - public ShaderRunner(Func, TimeSpan, T> shaderFactory) + public ShaderRunner(Func shaderFactory) { this.shaderFactory = shaderFactory; } @@ -26,7 +36,7 @@ namespace ComputeSharp.WinUI /// public void Execute(IReadWriteTexture2D texture, TimeSpan time) { - Gpu.Default.For(texture.Width, texture.Height, this.shaderFactory(texture, time)); + Gpu.Default.ForEach(texture, this.shaderFactory(time)); } } }