From 162b46ed419ca6c41c5331450344424daaa82ad6 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 31 Dec 2022 16:04:06 +0100 Subject: [PATCH] Wire up D2D1 frames renderer in UWP sample --- .../ViewModels/MainViewModel.cs | 52 +++++++++++++++---- .../ViewModels/ShaderRunnerViewModel.cs | 23 ++++---- .../Views/MainView.xaml | 6 ++- 3 files changed, 59 insertions(+), 22 deletions(-) diff --git a/samples/ComputeSharp.SwapChain.Core.Shared/ViewModels/MainViewModel.cs b/samples/ComputeSharp.SwapChain.Core.Shared/ViewModels/MainViewModel.cs index 0719ea3c..08731093 100644 --- a/samples/ComputeSharp.SwapChain.Core.Shared/ViewModels/MainViewModel.cs +++ b/samples/ComputeSharp.SwapChain.Core.Shared/ViewModels/MainViewModel.cs @@ -4,6 +4,7 @@ using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using ComputeSharp.SwapChain.Core.Constants; using ComputeSharp.SwapChain.Core.Services; +using ComputeSharp.SwapChain.Core.Shaders; using ComputeSharp.SwapChain.Core.Shaders.Runners; using ComputeSharp.SwapChain.Shaders; #if WINDOWS_UWP @@ -102,17 +103,46 @@ public sealed partial class MainViewModel : ObservableObject /// public IReadOnlyList ComputeShaderOptions { get; } = new ShaderRunnerViewModel[] { - new(typeof(ColorfulInfinity), new ShaderRunner(static time => new((float)time.TotalSeconds))), - new(typeof(ExtrudedTruchetPattern),new ShaderRunner(static time => new((float)time.TotalSeconds))), - new(typeof(FractalTiling),new ShaderRunner(static time => new((float)time.TotalSeconds))), - new(typeof(MengerJourney),new ShaderRunner(static time => new((float)time.TotalSeconds))), - new(typeof(Octagrams),new ShaderRunner(static time => new((float)time.TotalSeconds))), - new(typeof(ProteanClouds),new ShaderRunner(static time => new((float)time.TotalSeconds))), - new(typeof(TwoTiledTruchet),new ShaderRunner(static time => new((float)time.TotalSeconds))), - new(typeof(PyramidPattern),new ShaderRunner(static time => new((float)time.TotalSeconds))), - new(typeof(TriangleGridContouring),new ShaderRunner(static time => new((float)time.TotalSeconds))), - new(typeof(ContouredLayers),new ContouredLayersRunner()), - new(typeof(TerracedHills),new ShaderRunner(static time => new((float)time.TotalSeconds))), + new( + typeof(ColorfulInfinity), + new ShaderRunner(static time => new((float)time.TotalSeconds)), + new D2D1ShaderRunner(static (time, width, height) => new((float)time.TotalSeconds, new int2(width, height)))), + new( + typeof(FractalTiling), + new ShaderRunner(static time => new((float)time.TotalSeconds)), + new D2D1ShaderRunner(static (time, width, height) => new((float)time.TotalSeconds, new int2(width, height)))), + new( + typeof(MengerJourney), + new ShaderRunner(static time => new((float)time.TotalSeconds)), + new D2D1ShaderRunner(static (time, width, height) => new((float)time.TotalSeconds, new int2(width, height)))), + new( + typeof(Octagrams), + new ShaderRunner(static time => new((float)time.TotalSeconds)), + new D2D1ShaderRunner(static (time, width, height) => new((float)time.TotalSeconds, new int2(width, height)))), + new( + typeof(ProteanClouds), + new ShaderRunner(static time => new((float)time.TotalSeconds)), + new D2D1ShaderRunner(static (time, width, height) => new((float)time.TotalSeconds, new int2(width, height)))), + new( + typeof(TwoTiledTruchet), + new ShaderRunner(static time => new((float)time.TotalSeconds)), + new D2D1ShaderRunner(static (time, width, height) => new((float)time.TotalSeconds, new int2(width, height)))), + new( + typeof(PyramidPattern), + new ShaderRunner(static time => new((float)time.TotalSeconds)), + new D2D1ShaderRunner(static (time, width, height) => new((float)time.TotalSeconds, new int2(width, height)))), + new( + typeof(TriangleGridContouring), + new ShaderRunner(static time => new((float)time.TotalSeconds)), + new D2D1ShaderRunner(static (time, width, height) => new((float)time.TotalSeconds, new int2(width, height)))), + new( + typeof(ContouredLayers), + new ContouredLayersRunner(), + new D2D1ShaderRunner(static (time, width, height) => new((float)time.TotalSeconds, new int2(width, height)))), + new( + typeof(TerracedHills), + new ShaderRunner(static time => new((float)time.TotalSeconds)), + new D2D1ShaderRunner(static (time, width, height) => new((float)time.TotalSeconds, new int2(width, height)))), }; private ShaderRunnerViewModel selectedComputeShader; diff --git a/samples/ComputeSharp.SwapChain.Core.Shared/ViewModels/ShaderRunnerViewModel.cs b/samples/ComputeSharp.SwapChain.Core.Shared/ViewModels/ShaderRunnerViewModel.cs index 7931cbd8..a77299e0 100644 --- a/samples/ComputeSharp.SwapChain.Core.Shared/ViewModels/ShaderRunnerViewModel.cs +++ b/samples/ComputeSharp.SwapChain.Core.Shared/ViewModels/ShaderRunnerViewModel.cs @@ -1,5 +1,6 @@ using System; using CommunityToolkit.Mvvm.ComponentModel; +using ComputeSharp.SwapChain.Core.Shaders; #if WINDOWS_UWP using ComputeSharp.Uwp; #else @@ -11,17 +12,25 @@ namespace ComputeSharp.SwapChain.Core.ViewModels; /// /// A viewmodel for a compute shader. /// -public sealed class ShaderRunnerViewModel : ObservableObject +public sealed partial class ShaderRunnerViewModel : ObservableObject { + /// + /// Gets or sets whether the current shader is selected. + /// + [ObservableProperty] + private bool isSelected; + /// /// Creates a new instance. /// /// The instance for the actual shader to execute. /// The instance to execute. - public ShaderRunnerViewModel(Type shaderType, IShaderRunner shaderRunner) + /// The instance to execute. + public ShaderRunnerViewModel(Type shaderType, IShaderRunner shaderRunner, ID2D1ShaderRunner d2D1ShaderRunner) { ShaderType = shaderType; ShaderRunner = shaderRunner; + D2D1ShaderRunner = d2D1ShaderRunner; } /// @@ -34,14 +43,8 @@ public sealed class ShaderRunnerViewModel : ObservableObject /// public IShaderRunner ShaderRunner { get; } - private bool isSelected; - /// - /// Gets or sets whether the current shader is selected. + /// Gets the instance to execute. /// - public bool IsSelected - { - get => this.isSelected; - set => SetProperty(ref this.isSelected, value); - } + public ID2D1ShaderRunner D2D1ShaderRunner { get; } } \ No newline at end of file diff --git a/samples/ComputeSharp.SwapChain.Uwp/Views/MainView.xaml b/samples/ComputeSharp.SwapChain.Uwp/Views/MainView.xaml index 4d9ebc58..b964a769 100644 --- a/samples/ComputeSharp.SwapChain.Uwp/Views/MainView.xaml +++ b/samples/ComputeSharp.SwapChain.Uwp/Views/MainView.xaml @@ -10,6 +10,7 @@ xmlns:muxc="using:Microsoft.UI.Xaml.Controls" xmlns:ui="using:Microsoft.Toolkit.Uwp.UI" xmlns:viewmodels="using:ComputeSharp.SwapChain.Core.ViewModels" + xmlns:views="using:ComputeSharp.SwapChain.Uwp.Views" SizeChanged="UserControl_SizeChanged"> @@ -153,7 +154,10 @@ IsVerticalSyncEnabled="{x:Bind ViewModel.IsVerticalSyncEnabled, Mode=OneWay}" RenderingFailed="MainShaderPanel_RenderingFailed" ResolutionScale="{x:Bind converters:ResolutionScaleConverter.ConvertPercentageToScale(ViewModel.SelectedResolutionScale), Mode=OneWay}" - ShaderRunner="{x:Bind ViewModel.SelectedComputeShader.ShaderRunner, Mode=OneWay}" /> + ShaderRunner="{x:Bind ViewModel.SelectedComputeShader.ShaderRunner, Mode=OneWay}" + Visibility="Collapsed"/> + +