From 271012dc515f8299d9d2947ffd1a9a8e60b9b351 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 17 Jan 2023 00:28:53 +0100 Subject: [PATCH 1/5] Refactor build script for AppService tokens --- build/CreateBlankAppCenterToken.ps1 | 34 ---------------- build/CreateBlankServiceTokens.ps1 | 40 +++++++++++++++++++ .../ComputeSharp.SwapChain.Uwp/App.xaml.cs | 23 +---------- .../Assets/Generated/ServiceTokens.g.cs | 20 ++++++++++ .../ComputeSharp.SwapChain.Uwp.csproj | 5 ++- .../Services/ServiceCollectionExtensions.cs | 31 ++++++++++++++ 6 files changed, 96 insertions(+), 57 deletions(-) delete mode 100644 build/CreateBlankAppCenterToken.ps1 create mode 100644 build/CreateBlankServiceTokens.ps1 create mode 100644 samples/ComputeSharp.SwapChain.Uwp/Assets/Generated/ServiceTokens.g.cs create mode 100644 samples/ComputeSharp.SwapChain.Uwp/Services/ServiceCollectionExtensions.cs diff --git a/build/CreateBlankAppCenterToken.ps1 b/build/CreateBlankAppCenterToken.ps1 deleted file mode 100644 index ec116dad..00000000 --- a/build/CreateBlankAppCenterToken.ps1 +++ /dev/null @@ -1,34 +0,0 @@ -# Create the tokens folder -$TOKENS_FOLDER_RELATIVE_PATH = Join-Path -Path $PSScriptRoot -ChildPath "..\samples\ComputeSharp.SwapChain.Uwp\Assets\ServiceTokens" -if(!(Test-Path -Path $TOKENS_FOLDER_RELATIVE_PATH)) { - New-Item -ItemType directory -Path $TOKENS_FOLDER_RELATIVE_PATH -} -$TOKENS_FOLDER_PATH = Resolve-Path -Path $TOKENS_FOLDER_RELATIVE_PATH - -# AppCenter tokens -$APPCENTER_TOKENS_NAME = "App.AppCenter.cs" -$APPCENTER_TOKENS_PATH = Join-Path -Path $TOKENS_FOLDER_PATH -ChildPath $APPCENTER_TOKENS_NAME -if (!(Test-Path -Path $APPCENTER_TOKENS_PATH)) -{ - # Prepare the generated source - $APP_APPCENTER_TEXT = -@" -// -#pragma warning disable -#nullable enable - -namespace ComputeSharp.SwapChain.Uwp; - -/// -partial class App -{ - /// - /// The secret token for the AppService connection. - /// - private const string AppServiceSecret = ""; -} -"@.Trim() - - # Output the source to the target file - Add-Content $APP_APPCENTER_TEXT -Path $APPCENTER_TOKENS_PATH -NoNewline -} \ No newline at end of file diff --git a/build/CreateBlankServiceTokens.ps1 b/build/CreateBlankServiceTokens.ps1 new file mode 100644 index 00000000..63cd06bc --- /dev/null +++ b/build/CreateBlankServiceTokens.ps1 @@ -0,0 +1,40 @@ +# Create the service tokens folder +$SERVICE_TOKENS_FOLDER_RELATIVE_PATH = Join-Path -Path $PSScriptRoot -ChildPath "..\samples\ComputeSharp.SwapChain.Uwp\Assets\Generated" +if(!(Test-Path -Path $SERVICE_TOKENS_FOLDER_RELATIVE_PATH)) { + New-Item -ItemType directory -Path $SERVICE_TOKENS_FOLDER_RELATIVE_PATH +} +$SERVICE_TOKENS_FOLDER_PATH = Resolve-Path -Path $SERVICE_TOKENS_FOLDER_RELATIVE_PATH + +# Service tokens +$SERVICE_TOKENS_NAME = "ServiceTokens.g.cs" +$SERVICE_TOKENS_PATH = Join-Path -Path $SERVICE_TOKENS_FOLDER_PATH -ChildPath $SERVICE_TOKENS_NAME +if (!(Test-Path -Path $SERVICE_TOKENS_PATH)) +{ + # Prepare the generated source + $SERVICE_TOKENS_TEXT = +@" +// +#pragma warning disable +#nullable enable + +namespace ComputeSharp.SwapChain.Uwp; + +/// +/// A with some secret service tokens. +/// +public static class ServiceTokens +{ + /// + /// The secret token for the AppService connection. + /// +#if RELEASE + internal const string AppService = ""; +#else + internal const string AppService = ""; +#endif +} +"@.Trim() + + # Output the source to the target file + Add-Content $SERVICE_TOKENS_TEXT -Path $SERVICE_TOKENS_PATH -NoNewline +} \ No newline at end of file diff --git a/samples/ComputeSharp.SwapChain.Uwp/App.xaml.cs b/samples/ComputeSharp.SwapChain.Uwp/App.xaml.cs index 906de4fa..db63ae84 100644 --- a/samples/ComputeSharp.SwapChain.Uwp/App.xaml.cs +++ b/samples/ComputeSharp.SwapChain.Uwp/App.xaml.cs @@ -1,13 +1,6 @@ -#if !DEBUG -using System; -using System.Diagnostics; -#endif using CommunityToolkit.Mvvm.DependencyInjection; -using ComputeSharp.SwapChain.Core.Services; using ComputeSharp.SwapChain.Core.ViewModels; -#if !DEBUG using ComputeSharp.SwapChain.Uwp.Services; -#endif using ComputeSharp.SwapChain.Uwp.Views; using Microsoft.Extensions.DependencyInjection; using Windows.ApplicationModel.Activation; @@ -59,20 +52,8 @@ sealed partial class App : Application { ServiceCollection services = new(); -#if !DEBUG - if (Guid.TryParse(AppServiceSecret, out _) && !Debugger.IsAttached) - { - services.AddSingleton(new AppCenterService(AppServiceSecret)); - } - else - { - services.AddSingleton(); - } -#else - services.AddSingleton(); -#endif - - services.AddTransient(); + _ = services.AddAnalyticsService(); + _ = services.AddTransient(); Ioc.Default.ConfigureServices(services.BuildServiceProvider()); } diff --git a/samples/ComputeSharp.SwapChain.Uwp/Assets/Generated/ServiceTokens.g.cs b/samples/ComputeSharp.SwapChain.Uwp/Assets/Generated/ServiceTokens.g.cs new file mode 100644 index 00000000..ba2fc97b --- /dev/null +++ b/samples/ComputeSharp.SwapChain.Uwp/Assets/Generated/ServiceTokens.g.cs @@ -0,0 +1,20 @@ +// +#pragma warning disable +#nullable enable + +namespace ComputeSharp.SwapChain.Uwp; + +/// +/// A with some secret service tokens. +/// +public static class ServiceTokens +{ + /// + /// The secret token for the AppService connection. + /// +#if RELEASE + internal const string AppService = ""; +#else + internal const string AppService = ""; +#endif +} \ No newline at end of file diff --git a/samples/ComputeSharp.SwapChain.Uwp/ComputeSharp.SwapChain.Uwp.csproj b/samples/ComputeSharp.SwapChain.Uwp/ComputeSharp.SwapChain.Uwp.csproj index 1877ab6d..cbeb3220 100644 --- a/samples/ComputeSharp.SwapChain.Uwp/ComputeSharp.SwapChain.Uwp.csproj +++ b/samples/ComputeSharp.SwapChain.Uwp/ComputeSharp.SwapChain.Uwp.csproj @@ -78,9 +78,10 @@ App.xaml - + + MainView.xaml @@ -209,6 +210,6 @@ - powershell.exe -ExecutionPolicy Bypass -NoProfile -NonInteractive -File $(SolutionDir)\build\CreateBlankAppCenterToken.ps1 + powershell.exe -ExecutionPolicy Bypass -NoProfile -NonInteractive -File $(SolutionDir)\build\CreateBlankServiceTokens.ps1 \ No newline at end of file diff --git a/samples/ComputeSharp.SwapChain.Uwp/Services/ServiceCollectionExtensions.cs b/samples/ComputeSharp.SwapChain.Uwp/Services/ServiceCollectionExtensions.cs new file mode 100644 index 00000000..17096d43 --- /dev/null +++ b/samples/ComputeSharp.SwapChain.Uwp/Services/ServiceCollectionExtensions.cs @@ -0,0 +1,31 @@ +using System; +using System.Diagnostics; +using ComputeSharp.SwapChain.Core.Services; +using Microsoft.Extensions.DependencyInjection; + +namespace ComputeSharp.SwapChain.Uwp.Services; + +/// +/// Extensions for the type. +/// +public static class ServiceCollectionExtensions +{ + /// + /// Adds the correct registration to the given instance. + /// + /// The input instance to use to register services. + /// The same instance as . + public static IServiceCollection AddAnalyticsService(this IServiceCollection services) + { + if (Guid.TryParse(ServiceTokens.AppService, out _) && !Debugger.IsAttached) + { + _ = services.AddSingleton(new AppCenterService(ServiceTokens.AppService)); + } + else + { + _ = services.AddSingleton(); + } + + return services; + } +} From d5d42d960c541cda6d329be8441f8b73bf6a3809 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 17 Jan 2023 00:30:06 +0100 Subject: [PATCH 2/5] Remove reflection from DI registration --- samples/ComputeSharp.SwapChain.Uwp/App.xaml.cs | 3 ++- .../Services/ServiceCollectionExtensions.cs | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/samples/ComputeSharp.SwapChain.Uwp/App.xaml.cs b/samples/ComputeSharp.SwapChain.Uwp/App.xaml.cs index db63ae84..57495de3 100644 --- a/samples/ComputeSharp.SwapChain.Uwp/App.xaml.cs +++ b/samples/ComputeSharp.SwapChain.Uwp/App.xaml.cs @@ -1,4 +1,5 @@ using CommunityToolkit.Mvvm.DependencyInjection; +using ComputeSharp.SwapChain.Core.Services; using ComputeSharp.SwapChain.Core.ViewModels; using ComputeSharp.SwapChain.Uwp.Services; using ComputeSharp.SwapChain.Uwp.Views; @@ -53,7 +54,7 @@ sealed partial class App : Application ServiceCollection services = new(); _ = services.AddAnalyticsService(); - _ = services.AddTransient(); + _ = services.AddTransient(static services => new MainViewModel(services.GetRequiredService())); Ioc.Default.ConfigureServices(services.BuildServiceProvider()); } diff --git a/samples/ComputeSharp.SwapChain.Uwp/Services/ServiceCollectionExtensions.cs b/samples/ComputeSharp.SwapChain.Uwp/Services/ServiceCollectionExtensions.cs index 17096d43..c1bdecc9 100644 --- a/samples/ComputeSharp.SwapChain.Uwp/Services/ServiceCollectionExtensions.cs +++ b/samples/ComputeSharp.SwapChain.Uwp/Services/ServiceCollectionExtensions.cs @@ -23,7 +23,7 @@ public static class ServiceCollectionExtensions } else { - _ = services.AddSingleton(); + _ = services.AddSingleton(new DebugAnalyticsService()); } return services; From f3d4820de0da3c565bc107eb8b0e2db5d81398af Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 17 Jan 2023 00:36:40 +0100 Subject: [PATCH 3/5] Stop using Ioc type as service locator --- samples/ComputeSharp.SwapChain.Uwp/App.xaml.cs | 18 +++++++++++++----- .../Views/MainView.xaml.cs | 12 ++++++------ 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/samples/ComputeSharp.SwapChain.Uwp/App.xaml.cs b/samples/ComputeSharp.SwapChain.Uwp/App.xaml.cs index 57495de3..fe9bf060 100644 --- a/samples/ComputeSharp.SwapChain.Uwp/App.xaml.cs +++ b/samples/ComputeSharp.SwapChain.Uwp/App.xaml.cs @@ -1,4 +1,4 @@ -using CommunityToolkit.Mvvm.DependencyInjection; +using System; using ComputeSharp.SwapChain.Core.Services; using ComputeSharp.SwapChain.Core.ViewModels; using ComputeSharp.SwapChain.Uwp.Services; @@ -28,12 +28,19 @@ sealed partial class App : Application this.InitializeComponent(); } + /// + public static new App Current => (App)Application.Current; + + /// + /// Gets the current instance. + /// + public IServiceProvider Services { get; } = ConfigureServices(); + /// protected override void OnLaunched(LaunchActivatedEventArgs e) { if (Window.Current.Content is not MainView) { - ConfigureServices(); StyleTitleBar(); ExpandViewIntoTitleBar(); @@ -49,14 +56,15 @@ sealed partial class App : Application /// /// Configures the services for the application. /// - private static void ConfigureServices() + /// The instance to use. + private static IServiceProvider ConfigureServices() { ServiceCollection services = new(); _ = services.AddAnalyticsService(); - _ = services.AddTransient(static services => new MainViewModel(services.GetRequiredService())); + _ = services.AddTransient(static services => new MainViewModel(services.GetRequiredService())); - Ioc.Default.ConfigureServices(services.BuildServiceProvider()); + return services.BuildServiceProvider(); } /// diff --git a/samples/ComputeSharp.SwapChain.Uwp/Views/MainView.xaml.cs b/samples/ComputeSharp.SwapChain.Uwp/Views/MainView.xaml.cs index 9973a613..dac017c5 100644 --- a/samples/ComputeSharp.SwapChain.Uwp/Views/MainView.xaml.cs +++ b/samples/ComputeSharp.SwapChain.Uwp/Views/MainView.xaml.cs @@ -1,9 +1,9 @@ using System; -using CommunityToolkit.Mvvm.DependencyInjection; using ComputeSharp.SwapChain.Core.Constants; using ComputeSharp.SwapChain.Core.Services; using ComputeSharp.SwapChain.Core.ViewModels; using ComputeSharp.Uwp; +using Microsoft.Extensions.DependencyInjection; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Input; @@ -18,7 +18,7 @@ public sealed partial class MainView : UserControl public MainView() { this.InitializeComponent(); - this.DataContext = Ioc.Default.GetRequiredService(); + this.DataContext = App.Current.Services.GetRequiredService(); } /// @@ -29,7 +29,7 @@ public sealed partial class MainView : UserControl // Opens the shader selection panel private void OpenShaderSelectionPanelButton_Click(object sender, RoutedEventArgs e) { - Ioc.Default.GetRequiredService().Log(Event.OpenShaderSelectionPanel); + App.Current.Services.GetRequiredService().Log(Event.OpenShaderSelectionPanel); _ = this.Root.Resources.Remove("ShaderSelectionPanel"); this.Root.Children.Add(this.ShaderSelectionPanel); @@ -38,7 +38,7 @@ public sealed partial class MainView : UserControl // Hides the shader selection panel private void ShaderSelectionPanel_Tapped(object sender, TappedRoutedEventArgs e) { - Ioc.Default.GetRequiredService().Log(Event.CloseShaderSelectionPanel); + App.Current.Services.GetRequiredService().Log(Event.CloseShaderSelectionPanel); _ = this.Root.Children.Remove(this.ShaderSelectionPanel); } @@ -52,7 +52,7 @@ public sealed partial class MainView : UserControl // Logs rendering failed in the main panel private void MainShaderPanel_RenderingFailed(AnimatedComputeShaderPanel sender, RenderingFailedEventArgs args) { - Ioc.Default.GetRequiredService().Log(args.Exception, (nameof(Error), Error.RenderingFailedOnMainPanel)); + App.Current.Services.GetRequiredService().Log(args.Exception, (nameof(Error), Error.RenderingFailedOnMainPanel)); this.RenderingErrorInfoBar.IsOpen = true; } @@ -60,7 +60,7 @@ public sealed partial class MainView : UserControl // Logs rendering failed in a secondary panel private void SelectionShaderPanel_RenderingFailed(AnimatedComputeShaderPanel sender, RenderingFailedEventArgs args) { - Ioc.Default.GetRequiredService().Log(args.Exception, (nameof(Error), Error.RenderingFailedOnSelectionPanel)); + App.Current.Services.GetRequiredService().Log(args.Exception, (nameof(Error), Error.RenderingFailedOnSelectionPanel)); this.RenderingErrorInfoBar.IsOpen = true; } From 1b77d4fe510defd9ed9c811a03b845ee47b034c6 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 17 Jan 2023 00:40:35 +0100 Subject: [PATCH 4/5] Apply IDE0003 codefix to solution (simply names) --- samples/ComputeSharp.SwapChain.Uwp/App.xaml.cs | 2 +- samples/ComputeSharp.SwapChain.Uwp/Views/MainView.xaml.cs | 6 ++++-- .../ComputeSharp.SwapChain.WinUI/Views/MainWindow.xaml.cs | 6 ++++-- .../Controls/AnimatedComputeShaderPanel.cs | 8 ++++---- src/ComputeSharp.UI/Controls/ComputeShaderPanel.cs | 8 ++++---- .../Graphics/Resources/Abstract/TransferTexture2D{T}.cs | 2 +- 6 files changed, 18 insertions(+), 14 deletions(-) diff --git a/samples/ComputeSharp.SwapChain.Uwp/App.xaml.cs b/samples/ComputeSharp.SwapChain.Uwp/App.xaml.cs index fe9bf060..5dd2acb4 100644 --- a/samples/ComputeSharp.SwapChain.Uwp/App.xaml.cs +++ b/samples/ComputeSharp.SwapChain.Uwp/App.xaml.cs @@ -25,7 +25,7 @@ sealed partial class App : Application /// public App() { - this.InitializeComponent(); + InitializeComponent(); } /// diff --git a/samples/ComputeSharp.SwapChain.Uwp/Views/MainView.xaml.cs b/samples/ComputeSharp.SwapChain.Uwp/Views/MainView.xaml.cs index dac017c5..489cf3ee 100644 --- a/samples/ComputeSharp.SwapChain.Uwp/Views/MainView.xaml.cs +++ b/samples/ComputeSharp.SwapChain.Uwp/Views/MainView.xaml.cs @@ -17,8 +17,9 @@ public sealed partial class MainView : UserControl { public MainView() { - this.InitializeComponent(); - this.DataContext = App.Current.Services.GetRequiredService(); + InitializeComponent(); + + DataContext = App.Current.Services.GetRequiredService(); } /// @@ -32,6 +33,7 @@ public sealed partial class MainView : UserControl App.Current.Services.GetRequiredService().Log(Event.OpenShaderSelectionPanel); _ = this.Root.Resources.Remove("ShaderSelectionPanel"); + this.Root.Children.Add(this.ShaderSelectionPanel); } diff --git a/samples/ComputeSharp.SwapChain.WinUI/Views/MainWindow.xaml.cs b/samples/ComputeSharp.SwapChain.WinUI/Views/MainWindow.xaml.cs index 0d03b5a1..59f54119 100644 --- a/samples/ComputeSharp.SwapChain.WinUI/Views/MainWindow.xaml.cs +++ b/samples/ComputeSharp.SwapChain.WinUI/Views/MainWindow.xaml.cs @@ -20,8 +20,9 @@ public sealed partial class MainWindow : Window public MainWindow() { - this.InitializeComponent(); - this.ExtendsContentIntoTitleBar = true; + InitializeComponent(); + + ExtendsContentIntoTitleBar = true; SetTitleBar(this.TitleBarRectangle); @@ -50,6 +51,7 @@ public sealed partial class MainWindow : Window private void OpenShaderSelectionPanelButton_Click(object sender, RoutedEventArgs e) { _ = this.Root.Resources.Remove("ShaderSelectionPanel"); + this.Root.Children.Add(this.ShaderSelectionPanel); } diff --git a/src/ComputeSharp.UI/Controls/AnimatedComputeShaderPanel.cs b/src/ComputeSharp.UI/Controls/AnimatedComputeShaderPanel.cs index ab611e3c..d486235a 100644 --- a/src/ComputeSharp.UI/Controls/AnimatedComputeShaderPanel.cs +++ b/src/ComputeSharp.UI/Controls/AnimatedComputeShaderPanel.cs @@ -47,10 +47,10 @@ public sealed partial class AnimatedComputeShaderPanel : SwapChainPanel, IDispos { this.swapChainManager = new SwapChainManager(this, device); - this.Loaded += AnimatedComputeShaderPanel_Loaded; - this.Unloaded += AnimatedComputeShaderPanel_Unloaded; - this.SizeChanged += AnimatedComputeShaderPanel_SizeChanged; - this.CompositionScaleChanged += AnimatedComputeShaderPanel_CompositionScaleChanged; + Loaded += AnimatedComputeShaderPanel_Loaded; + Unloaded += AnimatedComputeShaderPanel_Unloaded; + SizeChanged += AnimatedComputeShaderPanel_SizeChanged; + CompositionScaleChanged += AnimatedComputeShaderPanel_CompositionScaleChanged; } // Initializes the swap chain and starts the render thread diff --git a/src/ComputeSharp.UI/Controls/ComputeShaderPanel.cs b/src/ComputeSharp.UI/Controls/ComputeShaderPanel.cs index a0edfa80..dfeb8554 100644 --- a/src/ComputeSharp.UI/Controls/ComputeShaderPanel.cs +++ b/src/ComputeSharp.UI/Controls/ComputeShaderPanel.cs @@ -47,10 +47,10 @@ public sealed partial class ComputeShaderPanel : SwapChainPanel, IDisposable { this.swapChainManager = new SwapChainManager(this, device); - this.Loaded += ComputeShaderPanel_Loaded; - this.Unloaded += ComputeShaderPanel_Unloaded; - this.SizeChanged += ComputeShaderPanel_SizeChanged; - this.CompositionScaleChanged += ComputeShaderPanel_CompositionScaleChanged; + Loaded += ComputeShaderPanel_Loaded; + Unloaded += ComputeShaderPanel_Unloaded; + SizeChanged += ComputeShaderPanel_SizeChanged; + CompositionScaleChanged += ComputeShaderPanel_CompositionScaleChanged; } // Initializes the swap chain and starts the render thread diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs index 179b93a4..7dd4d2a3 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs @@ -124,7 +124,7 @@ public abstract unsafe partial class TransferTexture2D : IReferenceTrackedObj [MethodImpl(MethodImplOptions.AggressiveInlining)] get { - using ReferenceTracker.Lease _0 = this.GetReferenceTracker().GetLease(); + using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease(); return new(this.mappedData, Width, Height, (int)this.d3D12PlacedSubresourceFootprint.Footprint.RowPitch); } From 73bda514de6f606ed65cb604c149bdce253f1c4f Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 17 Jan 2023 01:06:26 +0100 Subject: [PATCH 5/5] Replace string literal with nameof() expression --- samples/ComputeSharp.SwapChain.Uwp/Views/MainView.xaml.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/ComputeSharp.SwapChain.Uwp/Views/MainView.xaml.cs b/samples/ComputeSharp.SwapChain.Uwp/Views/MainView.xaml.cs index 489cf3ee..0b1cc07d 100644 --- a/samples/ComputeSharp.SwapChain.Uwp/Views/MainView.xaml.cs +++ b/samples/ComputeSharp.SwapChain.Uwp/Views/MainView.xaml.cs @@ -32,7 +32,7 @@ public sealed partial class MainView : UserControl { App.Current.Services.GetRequiredService().Log(Event.OpenShaderSelectionPanel); - _ = this.Root.Resources.Remove("ShaderSelectionPanel"); + _ = this.Root.Resources.Remove(nameof(this.ShaderSelectionPanel)); this.Root.Children.Add(this.ShaderSelectionPanel); }