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..5dd2acb4 100644 --- a/samples/ComputeSharp.SwapChain.Uwp/App.xaml.cs +++ b/samples/ComputeSharp.SwapChain.Uwp/App.xaml.cs @@ -1,13 +1,7 @@ -#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; @@ -31,15 +25,22 @@ sealed partial class App : Application /// public App() { - this.InitializeComponent(); + 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(); @@ -55,26 +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(); -#if !DEBUG - if (Guid.TryParse(AppServiceSecret, out _) && !Debugger.IsAttached) - { - services.AddSingleton(new AppCenterService(AppServiceSecret)); - } - else - { - services.AddSingleton(); - } -#else - services.AddSingleton(); -#endif + _ = services.AddAnalyticsService(); + _ = services.AddTransient(static services => new MainViewModel(services.GetRequiredService())); - services.AddTransient(); - - Ioc.Default.ConfigureServices(services.BuildServiceProvider()); + return 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..c1bdecc9 --- /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(new DebugAnalyticsService()); + } + + return services; + } +} diff --git a/samples/ComputeSharp.SwapChain.Uwp/Views/MainView.xaml.cs b/samples/ComputeSharp.SwapChain.Uwp/Views/MainView.xaml.cs index 9973a613..0b1cc07d 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; @@ -17,8 +17,9 @@ public sealed partial class MainView : UserControl { public MainView() { - this.InitializeComponent(); - this.DataContext = Ioc.Default.GetRequiredService(); + InitializeComponent(); + + DataContext = App.Current.Services.GetRequiredService(); } /// @@ -29,16 +30,17 @@ 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(nameof(this.ShaderSelectionPanel)); - _ = this.Root.Resources.Remove("ShaderSelectionPanel"); this.Root.Children.Add(this.ShaderSelectionPanel); } // 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 +54,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 +62,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; } 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); }