diff --git a/ComputeSharp.sln b/ComputeSharp.sln index 0313b614..dc3dc4b7 100644 --- a/ComputeSharp.sln +++ b/ComputeSharp.sln @@ -130,6 +130,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{B86A48A7-18B EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.D2D1.Uwp", "src\ComputeSharp.D2D1.Uwp\ComputeSharp.D2D1.Uwp.csproj", "{E60A3B1E-FC5D-41AC-ACF0-C2D75B6590D2}" EndProject +Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "ComputeSharp.SwapChain.Shaders.D2D1.Shared", "samples\ComputeSharp.SwapChain.Shaders.D2D1.Shared\ComputeSharp.SwapChain.Shaders.D2D1.Shared.shproj", "{F8BF7666-7CCF-4FE4-B055-2EBA4BCD4564}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|x64 = Debug|x64 @@ -411,16 +413,19 @@ Global {5DE90C85-ED26-4B5A-A0D7-2FF966A210AC} = {F8EFBB27-4EE2-4463-A75B-7EFDFB55D0F7} {669A414F-3B46-4B55-B587-061AF63C3067} = {F8EFBB27-4EE2-4463-A75B-7EFDFB55D0F7} {B86A48A7-18BD-4946-B995-3CE26B092DDF} = {1A9C4108-F61A-4ABE-B5CF-F524521EAE2D} + {F8BF7666-7CCF-4FE4-B055-2EBA4BCD4564} = {0ED8F632-5E17-46BE-8CC3-B14A82D4AEB1} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {4664C5E3-0340-4E22-BCFD-98AAEDF5F2DC} EndGlobalSection GlobalSection(SharedMSBuildProjectFiles) = preSolution samples\ComputeSharp.SwapChain.Core.Shared\ComputeSharp.SwapChain.Core.Shared.projitems*{0071701e-b9c5-4098-be5f-ef556c4553c6}*SharedItemsImports = 5 + samples\ComputeSharp.SwapChain.Shaders.D2D1.Shared\ComputeSharp.SwapChain.Shaders.D2D1.Shared.projitems*{0071701e-b9c5-4098-be5f-ef556c4553c6}*SharedItemsImports = 5 samples\ComputeSharp.SwapChain.Shaders.Shared\ComputeSharp.SwapChain.Shaders.Shared.projitems*{0071701e-b9c5-4098-be5f-ef556c4553c6}*SharedItemsImports = 5 samples\ComputeSharp.SwapChain.Shaders.Shared\ComputeSharp.SwapChain.Shaders.Shared.projitems*{0800bd51-499d-44c2-9417-fd1f7fdfe9c4}*SharedItemsImports = 13 src\ComputeSharp.UI\ComputeSharp.UI.projitems*{0c82ede1-5b8a-4df3-87da-700b29d5e265}*SharedItemsImports = 13 src\TerraFX.Interop.Windows.Dynamic\TerraFX.Interop.Windows.Dynamic.projitems*{158f984d-cd96-4062-bd02-2268946031a0}*SharedItemsImports = 13 + samples\ComputeSharp.SwapChain.Shaders.D2D1.Shared\ComputeSharp.SwapChain.Shaders.D2D1.Shared.projitems*{209c95a3-fa53-431b-b688-3299ca6c29d2}*SharedItemsImports = 5 src\TerraFX.Interop.Windows\TerraFX.Interop.Windows.projitems*{314eb353-e7e3-4146-8111-685cc83ec1de}*SharedItemsImports = 5 src\TerraFX.Interop.Windows.D2D1\TerraFX.Interop.Windows.D2D1.projitems*{4d7d9ba8-a821-4b1c-9ee2-cbcf5849906e}*SharedItemsImports = 5 src\TerraFX.Interop.Windows\TerraFX.Interop.Windows.projitems*{514ff00e-bbf0-4d55-a47c-eeeb6208adce}*SharedItemsImports = 13 @@ -446,8 +451,10 @@ Global src\ComputeSharp.SourceGeneration\ComputeSharp.SourceGeneration.projitems*{eb52cb67-4162-4722-874b-44ea8da5450e}*SharedItemsImports = 5 src\TerraFX.Interop.Windows.Dynamic\TerraFX.Interop.Windows.Dynamic.projitems*{eb52cb67-4162-4722-874b-44ea8da5450e}*SharedItemsImports = 5 src\TerraFX.Interop.Windows\TerraFX.Interop.Windows.projitems*{eb52cb67-4162-4722-874b-44ea8da5450e}*SharedItemsImports = 5 + samples\ComputeSharp.SwapChain.Shaders.D2D1.Shared\ComputeSharp.SwapChain.Shaders.D2D1.Shared.projitems*{f8bf7666-7ccf-4fe4-b055-2eba4bcd4564}*SharedItemsImports = 13 src\ComputeSharp.UI\ComputeSharp.UI.projitems*{fa18ba9f-76bf-4de0-8d24-02fc7657e6cb}*SharedItemsImports = 5 samples\ComputeSharp.SwapChain.Core.Shared\ComputeSharp.SwapChain.Core.Shared.projitems*{fbd388a5-7855-4408-98ff-e689f509db5a}*SharedItemsImports = 4 + samples\ComputeSharp.SwapChain.Shaders.D2D1.Shared\ComputeSharp.SwapChain.Shaders.D2D1.Shared.projitems*{fbd388a5-7855-4408-98ff-e689f509db5a}*SharedItemsImports = 4 samples\ComputeSharp.SwapChain.Shaders.Shared\ComputeSharp.SwapChain.Shaders.Shared.projitems*{fbd388a5-7855-4408-98ff-e689f509db5a}*SharedItemsImports = 4 EndGlobalSection EndGlobal diff --git a/samples/ComputeSharp.Sample.Shared/ComputeSharp.Sample.Shared.shproj b/samples/ComputeSharp.Sample.Shared/ComputeSharp.Sample.Shared.shproj index 51735cd3..d04a8bda 100644 --- a/samples/ComputeSharp.Sample.Shared/ComputeSharp.Sample.Shared.shproj +++ b/samples/ComputeSharp.Sample.Shared/ComputeSharp.Sample.Shared.shproj @@ -7,7 +7,6 @@ - diff --git a/samples/ComputeSharp.SwapChain.Core.Shared/ComputeSharp.SwapChain.Core.Shared.projitems b/samples/ComputeSharp.SwapChain.Core.Shared/ComputeSharp.SwapChain.Core.Shared.projitems index bc2b2625..35a54357 100644 --- a/samples/ComputeSharp.SwapChain.Core.Shared/ComputeSharp.SwapChain.Core.Shared.projitems +++ b/samples/ComputeSharp.SwapChain.Core.Shared/ComputeSharp.SwapChain.Core.Shared.projitems @@ -11,11 +11,14 @@ - + + + + diff --git a/samples/ComputeSharp.SwapChain.Core.Shared/ComputeSharp.SwapChain.Core.Shared.shproj b/samples/ComputeSharp.SwapChain.Core.Shared/ComputeSharp.SwapChain.Core.Shared.shproj index db365a5b..038557e4 100644 --- a/samples/ComputeSharp.SwapChain.Core.Shared/ComputeSharp.SwapChain.Core.Shared.shproj +++ b/samples/ComputeSharp.SwapChain.Core.Shared/ComputeSharp.SwapChain.Core.Shared.shproj @@ -7,7 +7,6 @@ - diff --git a/samples/ComputeSharp.SwapChain.Core.Shared/Constants/Event.cs b/samples/ComputeSharp.SwapChain.Core.Shared/Constants/Event.cs index 67d2b89d..6e4b8268 100644 --- a/samples/ComputeSharp.SwapChain.Core.Shared/Constants/Event.cs +++ b/samples/ComputeSharp.SwapChain.Core.Shared/Constants/Event.cs @@ -5,6 +5,7 @@ namespace ComputeSharp.SwapChain.Core.Constants; /// public static class Event { + public const string SelectedRenderingModeChanged = "[SETTINGS] SelectedRenderingMode"; public const string IsVerticalSyncEnabledChanged = "[SETTINGS] IsVerticalSyncEnabled"; public const string IsDynamicResolutionEnabledChanged = "[SETTINGS] IsDynamicResolutionEnabled"; public const string SelectedResolutionScaleChanged = "[SETTINGS] SelectedResolutionScale"; diff --git a/samples/ComputeSharp.SwapChain.Core.Shared/Converters/BoolConverter.cs b/samples/ComputeSharp.SwapChain.Core.Shared/Converters/BoolConverter.cs deleted file mode 100644 index 2d491fcd..00000000 --- a/samples/ComputeSharp.SwapChain.Core.Shared/Converters/BoolConverter.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System.Runtime.CompilerServices; - -namespace ComputeSharp.SwapChain.Core.Converters; - -/// -/// A class with some static converters for values. -/// -public static class BoolConverter -{ - /// - /// Negates an input value. - /// - /// The value to negate. - /// The negated value of . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static bool Not(bool value) - { - return !value; - } -} \ No newline at end of file diff --git a/samples/ComputeSharp.SwapChain.Core.Shared/Enums/RenderingMode.cs b/samples/ComputeSharp.SwapChain.Core.Shared/Enums/RenderingMode.cs new file mode 100644 index 00000000..fd33ab59 --- /dev/null +++ b/samples/ComputeSharp.SwapChain.Core.Shared/Enums/RenderingMode.cs @@ -0,0 +1,17 @@ +namespace ComputeSharp.SwapChain.Core.Enums; + +/// +/// Indicates the rendering mode being used. +/// +public enum RenderingMode +{ + /// + /// Render using DirectX 12 APIs. + /// + DirectX12, + + /// + /// Render using Win2D (ie. Direct2D behind the scenes). + /// + Win2D +} diff --git a/samples/ComputeSharp.SwapChain.Core.Shared/Shaders/D2D1ShaderRunner{T}.cs b/samples/ComputeSharp.SwapChain.Core.Shared/Shaders/D2D1ShaderRunner{T}.cs new file mode 100644 index 00000000..7a01a45c --- /dev/null +++ b/samples/ComputeSharp.SwapChain.Core.Shared/Shaders/D2D1ShaderRunner{T}.cs @@ -0,0 +1,59 @@ +using System; +using CommunityToolkit.Diagnostics; +using ComputeSharp.D2D1; +#if WINDOWS_UWP +using ComputeSharp.D2D1.Uwp; +using Microsoft.Graphics.Canvas; +using Microsoft.Graphics.Canvas.UI.Xaml; +#endif + +namespace ComputeSharp.SwapChain.Core.Shaders; + +/// +/// A simple implementation powered by a supplied shader type. +/// +/// The type of shader to use to render frames. +public sealed class D2D1ShaderRunner : ID2D1ShaderRunner + where T : unmanaged, ID2D1PixelShader +{ +#if WINDOWS_UWP + /// + /// The instance used to create shaders to run. + /// + private readonly Func shaderFactory; + + /// + /// The reusable instance to use to render frames. + /// + private readonly PixelShaderEffect pixelShaderEffect; +#endif + + /// + /// Creates a new instance. + /// + /// The instance used to create shaders to run. + public D2D1ShaderRunner(Func shaderFactory) + { + Guard.IsNotNull(shaderFactory); + +#if WINDOWS_UWP + this.shaderFactory = shaderFactory; + this.pixelShaderEffect = new PixelShaderEffect(); +#endif + } + +#if WINDOWS_UWP + /// + public void Execute(ICanvasAnimatedControl sender, CanvasAnimatedDrawEventArgs args) + { + int widthInPixels = sender.ConvertDipsToPixels((float)sender.Size.Width, CanvasDpiRounding.Round); + int heightInPixels = sender.ConvertDipsToPixels((float)sender.Size.Height, CanvasDpiRounding.Round); + + // Set the constant buffer + this.pixelShaderEffect.ConstantBuffer = this.shaderFactory(args.Timing.TotalTime, widthInPixels, heightInPixels); + + // Draw the shader + args.DrawingSession.DrawImage(this.pixelShaderEffect); + } +#endif +} \ No newline at end of file diff --git a/samples/ComputeSharp.SwapChain.Core.Shared/Shaders/ID2D1ShaderRunner.cs b/samples/ComputeSharp.SwapChain.Core.Shared/Shaders/ID2D1ShaderRunner.cs new file mode 100644 index 00000000..cb1a3bb4 --- /dev/null +++ b/samples/ComputeSharp.SwapChain.Core.Shared/Shaders/ID2D1ShaderRunner.cs @@ -0,0 +1,20 @@ +#if WINDOWS_UWP +using Microsoft.Graphics.Canvas.UI.Xaml; +#endif + +namespace ComputeSharp.SwapChain.Core.Shaders; + +/// +/// An interface for a shader runner to be used with a Win2D renderer. +/// +public interface ID2D1ShaderRunner +{ +#if WINDOWS_UWP + /// + /// Draws a new frame on a target instance. + /// + /// The target instance. + /// The instance to use to draw the new frame. + void Execute(ICanvasAnimatedControl sender, CanvasAnimatedDrawEventArgs args); +#endif +} \ No newline at end of file diff --git a/samples/ComputeSharp.SwapChain.Core.Shared/Shaders/Runners/D2D1ContouredLayersRunner.cs b/samples/ComputeSharp.SwapChain.Core.Shared/Shaders/Runners/D2D1ContouredLayersRunner.cs new file mode 100644 index 00000000..225da1b1 --- /dev/null +++ b/samples/ComputeSharp.SwapChain.Core.Shared/Shaders/Runners/D2D1ContouredLayersRunner.cs @@ -0,0 +1,76 @@ +#if WINDOWS_UWP +using System; +using System.IO; +using ComputeSharp.D2D1; +using ComputeSharp.D2D1.Interop; +using ComputeSharp.D2D1.Uwp; +#endif +using ComputeSharp.SwapChain.Shaders.D2D1; +#if WINDOWS_UWP +using Microsoft.Graphics.Canvas; +using Microsoft.Graphics.Canvas.UI.Xaml; +using Windows.ApplicationModel; +#endif + +#nullable enable + +namespace ComputeSharp.SwapChain.Core.Shaders.Runners; + +/// +/// A specialized for . +/// +public sealed class D2D1ContouredLayersRunner : ID2D1ShaderRunner +{ +#if WINDOWS_UWP + /// + /// The reusable instance to use to render frames. + /// + private PixelShaderEffect? pixelShaderEffect; + + /// + public unsafe void Execute(ICanvasAnimatedControl sender, CanvasAnimatedDrawEventArgs args) + { + if (this.pixelShaderEffect is null) + { + string filename = Path.Combine(Package.Current.InstalledLocation.Path, "Assets", "Textures", "RustyMetal.png"); + + // As a temporary workaround for the lack of image decoding helpers for D2D1ResourceTextureManager, and in order to + // keep the code here synchronous and simple, we can just leverage the DirectX 12 APIs to load textures. That is, we + // first load a texture (which will use WIC behind the scenes) to get the decoded image data in GPU memory, and then + // copy the data in a readback texture we can read from on the CPU. We can't just load an upload texture and read + // from it, as that type of texture can only be written to from the CPU. From there, we create a D2D resource texture. + using ReadOnlyTexture2D readOnlyTexture = GraphicsDevice.GetDefault().LoadReadOnlyTexture2D(filename); + using ReadBackTexture2D readBackTexture = GraphicsDevice.GetDefault().AllocateReadBackTexture2D(readOnlyTexture.Width, readOnlyTexture.Height); + + readOnlyTexture.CopyTo(readBackTexture); + + // Get the buffer pointer, the stride, and calculate the buffer size without the trailing padding. + // That is, the area between the end of the data in the last row and the stride is not included. + Rgba32* dataBuffer = readBackTexture.View.DangerousGetAddressAndByteStride(out int strideInBytes); + int bufferSize = ((readBackTexture.Height - 1) * strideInBytes) + (readBackTexture.View.Width * sizeof(Rgba32)); + + // Create the resource texture manager to use in the shader + D2D1ResourceTextureManager resourceTextureManager = new( + extents: stackalloc uint[] { (uint)readBackTexture.Height, (uint)readBackTexture.Width }, + bufferPrecision: D2D1BufferPrecision.UInt8Normalized, + channelDepth: D2D1ChannelDepth.Four, + filter: D2D1Filter.MinMagMipLinear, + extendModes: stackalloc D2D1ExtendMode[] { D2D1ExtendMode.Mirror, D2D1ExtendMode.Mirror }, + data: new ReadOnlySpan(dataBuffer, bufferSize), + strides: stackalloc uint[] { (uint)strideInBytes }); + + // Create the new pixel shader effect + this.pixelShaderEffect = new PixelShaderEffect() { ResourceTextureManagers = { [0] = resourceTextureManager } }; + } + + int widthInPixels = sender.ConvertDipsToPixels((float)sender.Size.Width, CanvasDpiRounding.Round); + int heightInPixels = sender.ConvertDipsToPixels((float)sender.Size.Height, CanvasDpiRounding.Round); + + // Set the constant buffer + this.pixelShaderEffect.ConstantBuffer = new ContouredLayers((float)args.Timing.TotalTime.TotalSeconds, new int2(widthInPixels, heightInPixels)); + + // Draw the shader + args.DrawingSession.DrawImage(this.pixelShaderEffect); + } +#endif +} \ No newline at end of file diff --git a/samples/ComputeSharp.SwapChain.Core.Shared/ViewModels/MainViewModel.cs b/samples/ComputeSharp.SwapChain.Core.Shared/ViewModels/MainViewModel.cs index 0719ea3c..e43331a4 100644 --- a/samples/ComputeSharp.SwapChain.Core.Shared/ViewModels/MainViewModel.cs +++ b/samples/ComputeSharp.SwapChain.Core.Shared/ViewModels/MainViewModel.cs @@ -3,7 +3,9 @@ using CommunityToolkit.Diagnostics; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using ComputeSharp.SwapChain.Core.Constants; +using ComputeSharp.SwapChain.Core.Enums; using ComputeSharp.SwapChain.Core.Services; +using ComputeSharp.SwapChain.Core.Shaders; using ComputeSharp.SwapChain.Core.Shaders.Runners; using ComputeSharp.SwapChain.Shaders; #if WINDOWS_UWP @@ -34,6 +36,7 @@ public sealed partial class MainViewModel : ObservableObject Guard.IsNotNull(analyticsService); this.analyticsService = analyticsService; + this.selectedRenderingMode = RenderingMode.DirectX12; this.isVerticalSyncEnabled = true; this.isDynamicResolutionEnabled = true; this.selectedResolutionScale = 100; @@ -42,116 +45,104 @@ public sealed partial class MainViewModel : ObservableObject } /// - /// Gets the available resolution scaling options (as percentage values). + /// Gets or sets the selected rendering mode. /// - public IList ResolutionScaleOptions { get; } = new[] { 25, 50, 75, 100 }; - - private bool isVerticalSyncEnabled; + [ObservableProperty] + [NotifyPropertyChangedFor(nameof(IsResolutionScaleOptionEnabled))] + private RenderingMode selectedRenderingMode; /// /// Gets or sets whether the vertical sync is enabled. /// - public bool IsVerticalSyncEnabled - { - get => this.isVerticalSyncEnabled; - set - { - if (SetProperty(ref this.isVerticalSyncEnabled, value)) - { - this.analyticsService.Log(Event.IsVerticalSyncEnabledChanged, (nameof(value), value)); - } - } - } - - private bool isDynamicResolutionEnabled; + [ObservableProperty] + private bool isVerticalSyncEnabled; /// /// Gets or sets whether the dynamic resolution is enabled. /// - public bool IsDynamicResolutionEnabled - { - get => this.isDynamicResolutionEnabled; - set - { - if (SetProperty(ref this.isDynamicResolutionEnabled, value)) - { - this.analyticsService.Log(Event.IsDynamicResolutionEnabledChanged, (nameof(value), value)); - } - } - } + [ObservableProperty] + [NotifyPropertyChangedFor(nameof(IsResolutionScaleOptionEnabled))] + private bool isDynamicResolutionEnabled; + /// + /// Gets the currently selected resolution scale setting (as percentage value). + /// + [ObservableProperty] private int selectedResolutionScale; /// - /// Gets the currently selected resolution scale setting (as percentage valaue). + /// Gets or sets the currently selected compute shader. /// - public int SelectedResolutionScale - { - get => this.selectedResolutionScale; - private set - { - if (SetProperty(ref this.selectedResolutionScale, value)) - { - this.analyticsService.Log(Event.SelectedResolutionScaleChanged, (nameof(value), value)); - } - } - } + [ObservableProperty] + private ShaderRunnerViewModel selectedComputeShader; + + /// + /// Gets or sets whether the rendering is currently paused. + /// + [ObservableProperty] + private bool isRenderingPaused; + + /// + /// Gets the available resolution scaling options (as percentage values). + /// + public IList ResolutionScaleOptions { get; } = new[] { 25, 50, 75, 100 }; /// /// Gets the collection of available compute shader. /// 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 D2D1ContouredLayersRunner()), + 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; + /// + /// Checks whether the resolution scale can currently be expliclty set. + /// + public bool IsResolutionScaleOptionEnabled => SelectedRenderingMode == RenderingMode.DirectX12 && !IsDynamicResolutionEnabled; /// - /// Gets or sets the currently selected compute shader. + /// Sets . /// - public ShaderRunnerViewModel SelectedComputeShader + [RelayCommand] + private void SetRenderingMode(RenderingMode renderingMode) { - get => this.selectedComputeShader; - set - { - this.selectedComputeShader.IsSelected = false; - - if (SetProperty(ref this.selectedComputeShader, value) && - value is not null) - { - this.analyticsService.Log(Event.SelectedComputeShaderChanged, (nameof(value.ShaderType), value.ShaderType.Name)); - - value.IsSelected = true; - } - } - } - - private bool isRenderingPaused; - - /// - /// Gets or sets whether the rendering is currently paused. - /// - public bool IsRenderingPaused - { - get => this.isRenderingPaused; - set - { - if (SetProperty(ref this.isRenderingPaused, value)) - { - this.analyticsService.Log(Event.IsRenderingPausedChanged, (nameof(value), value)); - } - } + SelectedRenderingMode = renderingMode; } /// @@ -171,4 +162,47 @@ public sealed partial class MainViewModel : ObservableObject { IsRenderingPaused = !IsRenderingPaused; } + + /// + partial void OnSelectedRenderingModeChanged(RenderingMode value) + { + this.analyticsService.Log(Event.SelectedRenderingModeChanged, (nameof(value), value)); + } + + /// + partial void OnIsVerticalSyncEnabledChanged(bool value) + { + this.analyticsService.Log(Event.IsVerticalSyncEnabledChanged, (nameof(value), value)); + } + + /// + partial void OnIsDynamicResolutionEnabledChanged(bool value) + { + this.analyticsService.Log(Event.IsDynamicResolutionEnabledChanged, (nameof(value), value)); + } + + /// + partial void OnSelectedResolutionScaleChanged(int value) + { + this.analyticsService.Log(Event.SelectedResolutionScaleChanged, (nameof(value), value)); + } + + /// + partial void OnSelectedComputeShaderChanging(ShaderRunnerViewModel value) + { + SelectedComputeShader.IsSelected = false; + value.IsSelected = true; + } + + /// + partial void OnSelectedComputeShaderChanged(ShaderRunnerViewModel value) + { + this.analyticsService.Log(Event.SelectedComputeShaderChanged, (nameof(value.ShaderType), value.ShaderType.Name)); + } + + /// + partial void OnIsRenderingPausedChanged(bool value) + { + this.analyticsService.Log(Event.IsRenderingPausedChanged, (nameof(value), value)); + } } \ No newline at end of file diff --git a/samples/ComputeSharp.SwapChain.Core.Shared/ViewModels/ShaderRunnerViewModel.cs b/samples/ComputeSharp.SwapChain.Core.Shared/ViewModels/ShaderRunnerViewModel.cs index 7931cbd8..c3ff0817 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,19 @@ namespace ComputeSharp.SwapChain.Core.ViewModels; /// /// A viewmodel for a compute shader. /// -public sealed class ShaderRunnerViewModel : ObservableObject +public sealed partial class ShaderRunnerViewModel : ObservableObject { /// /// 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 +37,14 @@ public sealed class ShaderRunnerViewModel : ObservableObject /// public IShaderRunner ShaderRunner { get; } - private bool isSelected; + /// + /// Gets the instance to execute. + /// + public ID2D1ShaderRunner D2D1ShaderRunner { get; } /// /// Gets or sets whether the current shader is selected. /// - public bool IsSelected - { - get => this.isSelected; - set => SetProperty(ref this.isSelected, value); - } + [ObservableProperty] + private bool isSelected; } \ No newline at end of file diff --git a/tests/ComputeSharp.D2D1.Tests/Shaders/ColorfulInfinity.cs b/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/ColorfulInfinity.cs similarity index 93% rename from tests/ComputeSharp.D2D1.Tests/Shaders/ColorfulInfinity.cs rename to samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/ColorfulInfinity.cs index 06a496f0..94ac59d2 100644 --- a/tests/ComputeSharp.D2D1.Tests/Shaders/ColorfulInfinity.cs +++ b/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/ColorfulInfinity.cs @@ -1,4 +1,6 @@ -namespace ComputeSharp.D2D1.Tests.Shaders; +using ComputeSharp.D2D1; + +namespace ComputeSharp.SwapChain.Shaders.D2D1; /// /// A shader creating an abstract and colorful animation. @@ -15,12 +17,12 @@ internal readonly partial struct ColorfulInfinity : ID2D1PixelShader /// /// The current time since the start of the application. /// - public readonly float time; + private readonly float time; /// /// The dispatch size for the current output. /// - public readonly int2 dispatchSize; + private readonly int2 dispatchSize; /// /// The total number of layers for the final animation. diff --git a/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/ComputeSharp.SwapChain.Shaders.D2D1.Shared.projitems b/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/ComputeSharp.SwapChain.Shaders.D2D1.Shared.projitems new file mode 100644 index 00000000..7c992834 --- /dev/null +++ b/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/ComputeSharp.SwapChain.Shaders.D2D1.Shared.projitems @@ -0,0 +1,24 @@ + + + + $(MSBuildAllProjects);$(MSBuildThisFileFullPath) + true + f8bf7666-7ccf-4fe4-b055-2eba4bcd4564 + + + ComputeSharp.SwapChain.Shaders.D2D1.Shared + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/ComputeSharp.SwapChain.Shaders.D2D1.Shared.shproj b/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/ComputeSharp.SwapChain.Shaders.D2D1.Shared.shproj new file mode 100644 index 00000000..4b0971f2 --- /dev/null +++ b/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/ComputeSharp.SwapChain.Shaders.D2D1.Shared.shproj @@ -0,0 +1,12 @@ + + + + f8bf7666-7ccf-4fe4-b055-2eba4bcd4564 + 14.0 + + + + + + + diff --git a/tests/ComputeSharp.D2D1.Tests/Shaders/ContouredLayers.cs b/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/ContouredLayers.cs similarity index 98% rename from tests/ComputeSharp.D2D1.Tests/Shaders/ContouredLayers.cs rename to samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/ContouredLayers.cs index c5af9d3a..63b01ac6 100644 --- a/tests/ComputeSharp.D2D1.Tests/Shaders/ContouredLayers.cs +++ b/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/ContouredLayers.cs @@ -1,4 +1,6 @@ -namespace ComputeSharp.D2D1.Tests.Shaders; +using ComputeSharp.D2D1; + +namespace ComputeSharp.SwapChain.Shaders.D2D1; /// /// Constructing some concise contoured layers, then applying various edge and shading effects to produce some faux depth. @@ -19,7 +21,7 @@ internal readonly partial struct ContouredLayers : ID2D1PixelShader /// /// The dispatch size for the current output. /// - public readonly int2 dispatchSize; + private readonly int2 dispatchSize; /// /// The background texture to sample. diff --git a/tests/ComputeSharp.D2D1.Tests/Shaders/FractalTiling.cs b/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/FractalTiling.cs similarity index 91% rename from tests/ComputeSharp.D2D1.Tests/Shaders/FractalTiling.cs rename to samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/FractalTiling.cs index 191a2d7e..db30a226 100644 --- a/tests/ComputeSharp.D2D1.Tests/Shaders/FractalTiling.cs +++ b/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/FractalTiling.cs @@ -1,4 +1,6 @@ -namespace ComputeSharp.D2D1.Tests.Shaders; +using ComputeSharp.D2D1; + +namespace ComputeSharp.SwapChain.Shaders.D2D1; /// /// A shader creating a fractal tiling animation. @@ -15,12 +17,12 @@ internal readonly partial struct FractalTiling : ID2D1PixelShader /// /// The current time since the start of the application. /// - public readonly float time; + private readonly float time; /// /// The dispatch size for the current output. /// - public readonly int2 dispatchSize; + private readonly int2 dispatchSize; /// public float4 Execute() diff --git a/tests/ComputeSharp.D2D1.Tests/Shaders/HelloWorld.cs b/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/HelloWorld.cs similarity index 86% rename from tests/ComputeSharp.D2D1.Tests/Shaders/HelloWorld.cs rename to samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/HelloWorld.cs index 96f0ed79..49727720 100644 --- a/tests/ComputeSharp.D2D1.Tests/Shaders/HelloWorld.cs +++ b/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/HelloWorld.cs @@ -1,4 +1,6 @@ -namespace ComputeSharp.D2D1.Tests.Shaders; +using ComputeSharp.D2D1; + +namespace ComputeSharp.SwapChain.Shaders.D2D1; /// /// A simple shader to get started with based on shadertoy new shader template. @@ -13,12 +15,12 @@ internal readonly partial struct HelloWorld : ID2D1PixelShader /// /// The current time since the start of the application. /// - public readonly float time; + private readonly float time; /// /// The dispatch size for the current output. /// - public readonly int2 dispatchSize; + private readonly int2 dispatchSize; /// public float4 Execute() diff --git a/tests/ComputeSharp.D2D1.Tests/Shaders/MengerJourney.cs b/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/MengerJourney.cs similarity index 97% rename from tests/ComputeSharp.D2D1.Tests/Shaders/MengerJourney.cs rename to samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/MengerJourney.cs index b079eb1e..398e276d 100644 --- a/tests/ComputeSharp.D2D1.Tests/Shaders/MengerJourney.cs +++ b/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/MengerJourney.cs @@ -1,4 +1,6 @@ -namespace ComputeSharp.D2D1.Tests.Shaders; +using ComputeSharp.D2D1; + +namespace ComputeSharp.SwapChain.Shaders.D2D1; /// /// A shader creating a flythrough in a Menger system. @@ -14,12 +16,12 @@ internal readonly partial struct MengerJourney : ID2D1PixelShader /// /// The current time since the start of the application. /// - public readonly float time; + private readonly float time; /// /// The dispatch size for the current output. /// - public readonly int2 dispatchSize; + private readonly int2 dispatchSize; private const int MaxSteps = 30; private const float MinimumDistance = 0.0009f; diff --git a/tests/ComputeSharp.D2D1.Tests/Shaders/Octagrams.cs b/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/Octagrams.cs similarity index 96% rename from tests/ComputeSharp.D2D1.Tests/Shaders/Octagrams.cs rename to samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/Octagrams.cs index e1c1bc27..aa16a6bb 100644 --- a/tests/ComputeSharp.D2D1.Tests/Shaders/Octagrams.cs +++ b/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/Octagrams.cs @@ -1,4 +1,6 @@ -namespace ComputeSharp.D2D1.Tests.Shaders; +using ComputeSharp.D2D1; + +namespace ComputeSharp.SwapChain.Shaders.D2D1; /// /// A shader creating an octagram animation, inspired by arabesque. @@ -14,12 +16,12 @@ internal readonly partial struct Octagrams : ID2D1PixelShader /// /// The current time since the start of the application. /// - public readonly float time; + private readonly float time; /// /// The dispatch size for the current output. /// - public readonly int2 dispatchSize; + private readonly int2 dispatchSize; private static float2x2 Rotate(float a) { diff --git a/tests/ComputeSharp.D2D1.Tests/Shaders/ProteanClouds.cs b/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/ProteanClouds.cs similarity index 96% rename from tests/ComputeSharp.D2D1.Tests/Shaders/ProteanClouds.cs rename to samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/ProteanClouds.cs index 0eeabef3..762bf810 100644 --- a/tests/ComputeSharp.D2D1.Tests/Shaders/ProteanClouds.cs +++ b/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/ProteanClouds.cs @@ -1,4 +1,6 @@ -namespace ComputeSharp.D2D1.Tests.Shaders; +using ComputeSharp.D2D1; + +namespace ComputeSharp.SwapChain.Shaders.D2D1; /// /// Fully procedural 3D animated volume with three evaluations per step (for shading). @@ -15,14 +17,14 @@ internal readonly partial struct ProteanClouds : ID2D1PixelShader /// /// The current time Hlsl.Since the start of the application. /// - public readonly float time; + private readonly float time; /// /// The dispatch size for the current output. /// - public readonly int2 dispatchSize; + private readonly int2 dispatchSize; - private static readonly float3x3 m3 = new float3x3(0.33338f, 0.56034f, -0.71817f, -0.87887f, 0.32651f, -0.15323f, 0.15162f, 0.69596f, 0.61339f) * 1.93f; + private static readonly float3x3 M3 = new float3x3(0.33338f, 0.56034f, -0.71817f, -0.87887f, 0.32651f, -0.15323f, 0.15162f, 0.69596f, 0.61339f) * 1.93f; private static float2x2 Rotate(in float a) { @@ -65,7 +67,7 @@ internal readonly partial struct ProteanClouds : ID2D1PixelShader z *= 0.57f; trk *= 1.4f; - p = Hlsl.Mul(p, m3); + p = Hlsl.Mul(p, M3); } d = Hlsl.Abs(d + (prm1 * 3.0f)) + (prm1 * 0.3f) - 2.5f + bsMo.Y; diff --git a/tests/ComputeSharp.D2D1.Tests/Shaders/PyramidPattern.cs b/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/PyramidPattern.cs similarity index 97% rename from tests/ComputeSharp.D2D1.Tests/Shaders/PyramidPattern.cs rename to samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/PyramidPattern.cs index c680c786..a272f95c 100644 --- a/tests/ComputeSharp.D2D1.Tests/Shaders/PyramidPattern.cs +++ b/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/PyramidPattern.cs @@ -1,4 +1,6 @@ -namespace ComputeSharp.D2D1.Tests.Shaders; +using ComputeSharp.D2D1; + +namespace ComputeSharp.SwapChain.Shaders.D2D1; /// /// An offset grid of square-based pyramids whose tips have been offset according to an underlying directional noise field. @@ -15,12 +17,12 @@ internal readonly partial struct PyramidPattern : ID2D1PixelShader /// /// The current time Hlsl.Since the start of the application. /// - public readonly float time; + private readonly float time; /// /// The dispatch size for the current output. /// - public readonly int2 dispatchSize; + private readonly int2 dispatchSize; // Standard 2D rotation formula. private static float2x2 Rotate2x2(in float a) diff --git a/tests/ComputeSharp.D2D1.Tests/Shaders/TerracedHills.cs b/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/TerracedHills.cs similarity index 97% rename from tests/ComputeSharp.D2D1.Tests/Shaders/TerracedHills.cs rename to samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/TerracedHills.cs index 19090ae2..6669651c 100644 --- a/tests/ComputeSharp.D2D1.Tests/Shaders/TerracedHills.cs +++ b/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/TerracedHills.cs @@ -1,4 +1,6 @@ -namespace ComputeSharp.D2D1.Tests.Shaders; +using ComputeSharp.D2D1; + +namespace ComputeSharp.SwapChain.Shaders.D2D1; /// /// An abstract representation of some terraced hills. In essence, just a very basic terrain layout with a small code footprint. @@ -19,7 +21,7 @@ internal readonly partial struct TerracedHills : ID2D1PixelShader /// /// The dispatch size for the current output. /// - public readonly int2 dispatchSize; + private readonly int2 dispatchSize; // 2x2 matrix rotation. Angle vector, courtesy of Fabrice private static float2x2 Rot2(float th) diff --git a/tests/ComputeSharp.D2D1.Tests/Shaders/TriangleGridContouring.cs b/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/TriangleGridContouring.cs similarity index 98% rename from tests/ComputeSharp.D2D1.Tests/Shaders/TriangleGridContouring.cs rename to samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/TriangleGridContouring.cs index 3c38a8e3..d472f8e6 100644 --- a/tests/ComputeSharp.D2D1.Tests/Shaders/TriangleGridContouring.cs +++ b/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/TriangleGridContouring.cs @@ -1,4 +1,6 @@ -namespace ComputeSharp.D2D1.Tests.Shaders; +using ComputeSharp.D2D1; + +namespace ComputeSharp.SwapChain.Shaders.D2D1; /// /// Utilizing a 2D simplex grid to construct the isolines of a noise function. @@ -14,12 +16,12 @@ internal readonly partial struct TriangleGridContouring : ID2D1PixelShader /// /// The current time Hlsl.Since the start of the application. /// - public readonly float time; + private readonly float time; /// /// The dispatch size for the current output. /// - public readonly int2 dispatchSize; + private readonly int2 dispatchSize; // Standard 2D rotation formula. private static float2x2 Rotate2x2(in float a) diff --git a/tests/ComputeSharp.D2D1.Tests/Shaders/TwoTiledTruchet.cs b/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/TwoTiledTruchet.cs similarity index 96% rename from tests/ComputeSharp.D2D1.Tests/Shaders/TwoTiledTruchet.cs rename to samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/TwoTiledTruchet.cs index f2cf9655..2710b331 100644 --- a/tests/ComputeSharp.D2D1.Tests/Shaders/TwoTiledTruchet.cs +++ b/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/TwoTiledTruchet.cs @@ -1,4 +1,6 @@ -namespace ComputeSharp.D2D1.Tests.Shaders; +using ComputeSharp.D2D1; + +namespace ComputeSharp.SwapChain.Shaders.D2D1; /// /// A shader creating an animated two-tiles Truchet arrangement. @@ -14,12 +16,12 @@ internal readonly partial struct TwoTiledTruchet : ID2D1PixelShader /// /// The current time since the start of the application. /// - public readonly float time; + private readonly float time; /// /// The dispatch size for the current output. /// - public readonly int2 dispatchSize; + private readonly int2 dispatchSize; /// /// Calculates the Truchet distance field. diff --git a/samples/ComputeSharp.SwapChain.Shaders.Shared/ComputeSharp.SwapChain.Shaders.Shared.shproj b/samples/ComputeSharp.SwapChain.Shaders.Shared/ComputeSharp.SwapChain.Shaders.Shared.shproj index 293130fe..ea8e8c68 100644 --- a/samples/ComputeSharp.SwapChain.Shaders.Shared/ComputeSharp.SwapChain.Shaders.Shared.shproj +++ b/samples/ComputeSharp.SwapChain.Shaders.Shared/ComputeSharp.SwapChain.Shaders.Shared.shproj @@ -7,7 +7,6 @@ - diff --git a/samples/ComputeSharp.SwapChain.Uwp/App.xaml b/samples/ComputeSharp.SwapChain.Uwp/App.xaml index b203f6c3..8d04ab03 100644 --- a/samples/ComputeSharp.SwapChain.Uwp/App.xaml +++ b/samples/ComputeSharp.SwapChain.Uwp/App.xaml @@ -3,6 +3,11 @@ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> - + + + + + + diff --git a/samples/ComputeSharp.SwapChain.Uwp/ComputeSharp.SwapChain.Uwp.csproj b/samples/ComputeSharp.SwapChain.Uwp/ComputeSharp.SwapChain.Uwp.csproj index 26263f0b..8be7ced6 100644 --- a/samples/ComputeSharp.SwapChain.Uwp/ComputeSharp.SwapChain.Uwp.csproj +++ b/samples/ComputeSharp.SwapChain.Uwp/ComputeSharp.SwapChain.Uwp.csproj @@ -79,6 +79,7 @@ App.xaml + @@ -192,17 +193,24 @@ + + + - + + MSBuild:Compile + Designer + Designer MSBuild:Compile + diff --git a/samples/ComputeSharp.SwapChain.Uwp/Controls/D2D1AnimatedPixelShaderPanel.cs b/samples/ComputeSharp.SwapChain.Uwp/Controls/D2D1AnimatedPixelShaderPanel.cs new file mode 100644 index 00000000..cf7bbeba --- /dev/null +++ b/samples/ComputeSharp.SwapChain.Uwp/Controls/D2D1AnimatedPixelShaderPanel.cs @@ -0,0 +1,142 @@ +using ComputeSharp.SwapChain.Core.Shaders; +using Microsoft.Graphics.Canvas.UI.Xaml; +using Windows.UI.Xaml; +using Windows.UI.Xaml.Controls; + +#nullable enable + +namespace ComputeSharp.SwapChain.Uwp.Views; + +/// +/// A custom that can be used to render animated backgrounds via Win2D. +/// +[TemplatePart(Name = "PART_CanvasAnimatedControl", Type = typeof(CanvasAnimatedControl))] +public sealed class D2D1AnimatedPixelShaderPanel : Control +{ + /// + /// The wrapped instance used to render frames. + /// + private CanvasAnimatedControl? canvasAnimatedControl; + + /// + /// The instance currently in use, if any. + /// + private volatile ID2D1ShaderRunner? shaderRunner; + + /// + /// Creates a new instance. + /// + public D2D1AnimatedPixelShaderPanel() + { + DefaultStyleKey = typeof(D2D1AnimatedPixelShaderPanel); + + Loaded += D2D1AnimatedPixelShaderPanel_Loaded; + Unloaded += D2D1AnimatedPixelShaderPanel_Unloaded; + } + + /// + protected override void OnApplyTemplate() + { + base.OnApplyTemplate(); + + this.canvasAnimatedControl = (CanvasAnimatedControl)GetTemplateChild("PART_CanvasAnimatedControl")!; + this.canvasAnimatedControl.Draw += CanvasAnimatedControl_Draw; + } + + // Ensure the panel is not paused when the control is loaded + private void D2D1AnimatedPixelShaderPanel_Loaded(object sender, RoutedEventArgs e) + { + if (this.canvasAnimatedControl is { } canvasAnimatedControl && + ShaderRunner is not null && + !IsPaused) + { + canvasAnimatedControl.Paused = false; + } + } + + // Always pause rendering when the control is unloaded + private void D2D1AnimatedPixelShaderPanel_Unloaded(object sender, RoutedEventArgs e) + { + if (this.canvasAnimatedControl is { } canvasAnimatedControl) + { + canvasAnimatedControl.Paused = true; + } + } + + /// + /// Draws a new frame on the wrapped instance. + /// + /// + private void CanvasAnimatedControl_Draw(ICanvasAnimatedControl sender, CanvasAnimatedDrawEventArgs args) + { + this.shaderRunner?.Execute(sender, args); + } + + /// + /// Gets or sets the instance to use to render content. + /// + public ID2D1ShaderRunner? ShaderRunner + { + get => (ID2D1ShaderRunner?)GetValue(ShaderRunnerProperty); + set => SetValue(ShaderRunnerProperty, value); + } + + /// + /// The backing . + /// + public static readonly DependencyProperty ShaderRunnerProperty = DependencyProperty.Register( + nameof(ShaderRunner), + typeof(ID2D1ShaderRunner), + typeof(D2D1AnimatedPixelShaderPanel), + new PropertyMetadata(null, OnShaderRunnerPropertyChanged)); + + /// + private static void OnShaderRunnerPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + D2D1AnimatedPixelShaderPanel @this = (D2D1AnimatedPixelShaderPanel)d; + ID2D1ShaderRunner? shaderRunner = (ID2D1ShaderRunner?)e.NewValue; + + // Save the new shader runner for later (as the dependency property cannot be accessed by the render thread) + @this.shaderRunner = shaderRunner; + + // Pause or start the render thread if a runner is available + if (@this.canvasAnimatedControl is { } canvasAnimatedControl) + { + bool shouldRender = @this.IsLoaded && !@this.IsPaused && shaderRunner is not null; + + canvasAnimatedControl.Paused = !shouldRender; + } + } + + /// + /// Gets or sets whether or not the rendering is paused. + /// + public bool IsPaused + { + get => (bool)GetValue(IsPausedProperty); + set => SetValue(IsPausedProperty, value); + } + + /// + /// The backing . + /// + public static readonly DependencyProperty IsPausedProperty = DependencyProperty.Register( + nameof(IsPaused), + typeof(bool), + typeof(D2D1AnimatedPixelShaderPanel), + new PropertyMetadata(false, OnIsPausedPropertyChanged)); + + /// + private static void OnIsPausedPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + D2D1AnimatedPixelShaderPanel @this = (D2D1AnimatedPixelShaderPanel)d; + bool isPaused = (bool)e.NewValue; + + if (@this.canvasAnimatedControl is { } canvasAnimatedControl) + { + bool shouldRender = @this.IsLoaded && !isPaused && @this.ShaderRunner is ID2D1ShaderRunner; + + canvasAnimatedControl.Paused = !shouldRender; + } + } +} \ No newline at end of file diff --git a/samples/ComputeSharp.SwapChain.Uwp/Controls/D2D1AnimatedPixelShaderPanel.xaml b/samples/ComputeSharp.SwapChain.Uwp/Controls/D2D1AnimatedPixelShaderPanel.xaml new file mode 100644 index 00000000..3ca0e723 --- /dev/null +++ b/samples/ComputeSharp.SwapChain.Uwp/Controls/D2D1AnimatedPixelShaderPanel.xaml @@ -0,0 +1,16 @@ + + + + diff --git a/samples/ComputeSharp.SwapChain.Uwp/Views/MainView.xaml b/samples/ComputeSharp.SwapChain.Uwp/Views/MainView.xaml index 4d9ebc58..31029efd 100644 --- a/samples/ComputeSharp.SwapChain.Uwp/Views/MainView.xaml +++ b/samples/ComputeSharp.SwapChain.Uwp/Views/MainView.xaml @@ -6,10 +6,12 @@ xmlns:computesharp="using:ComputeSharp.Uwp" xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls" xmlns:converters="using:ComputeSharp.SwapChain.Core.Converters" + xmlns:enums="using:ComputeSharp.SwapChain.Core.Enums" xmlns:media="using:Microsoft.Toolkit.Uwp.UI.Media" 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"> @@ -144,16 +146,27 @@ - - + + + + + + + + + + + + + - - + + + + + diff --git a/samples/ComputeSharp.SwapChain.WinUI/ComputeSharp.SwapChain.WinUI.csproj b/samples/ComputeSharp.SwapChain.WinUI/ComputeSharp.SwapChain.WinUI.csproj index d05797e8..b6fbd2bc 100644 --- a/samples/ComputeSharp.SwapChain.WinUI/ComputeSharp.SwapChain.WinUI.csproj +++ b/samples/ComputeSharp.SwapChain.WinUI/ComputeSharp.SwapChain.WinUI.csproj @@ -55,10 +55,13 @@ + + - + + diff --git a/samples/ComputeSharp.SwapChain.WinUI/Views/MainWindow.xaml b/samples/ComputeSharp.SwapChain.WinUI/Views/MainWindow.xaml index 770daeb4..9d8692d9 100644 --- a/samples/ComputeSharp.SwapChain.WinUI/Views/MainWindow.xaml +++ b/samples/ComputeSharp.SwapChain.WinUI/Views/MainWindow.xaml @@ -196,25 +196,25 @@ Command="{x:Bind ViewModel.SetResolutionScaleCommand}" CommandParameter="{x:Bind ViewModel.ResolutionScaleOptions[0]}" IsChecked="{x:Bind ViewModel.SelectedResolutionScale.Equals(ViewModel.ResolutionScaleOptions[0]), Mode=OneWay}" - IsEnabled="{x:Bind converters:BoolConverter.Not(ViewModel.IsDynamicResolutionEnabled), Mode=OneWay}" + IsEnabled="{x:Bind ViewModel.IsResolutionScaleOptionEnabled, Mode=OneWay}" Text="{x:Bind converters:ResolutionScaleConverter.ConvertPercentageToFormattedString(ViewModel.ResolutionScaleOptions[0])}" /> diff --git a/src/ComputeSharp.NetStandard/ComputeSharp.NetStandard.shproj b/src/ComputeSharp.NetStandard/ComputeSharp.NetStandard.shproj index 1ab4d44d..975577c8 100644 --- a/src/ComputeSharp.NetStandard/ComputeSharp.NetStandard.shproj +++ b/src/ComputeSharp.NetStandard/ComputeSharp.NetStandard.shproj @@ -7,7 +7,6 @@ - diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/ComputeSharp.SourceGeneration.Hlsl.shproj b/src/ComputeSharp.SourceGeneration.Hlsl/ComputeSharp.SourceGeneration.Hlsl.shproj index 4c441b94..0f33740e 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/ComputeSharp.SourceGeneration.Hlsl.shproj +++ b/src/ComputeSharp.SourceGeneration.Hlsl/ComputeSharp.SourceGeneration.Hlsl.shproj @@ -7,7 +7,6 @@ - diff --git a/src/ComputeSharp.SourceGeneration/ComputeSharp.SourceGeneration.shproj b/src/ComputeSharp.SourceGeneration/ComputeSharp.SourceGeneration.shproj index 00042f22..585a1858 100644 --- a/src/ComputeSharp.SourceGeneration/ComputeSharp.SourceGeneration.shproj +++ b/src/ComputeSharp.SourceGeneration/ComputeSharp.SourceGeneration.shproj @@ -7,7 +7,6 @@ - diff --git a/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj b/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj index 25b0121e..af709bd0 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj +++ b/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj @@ -14,8 +14,6 @@ - - diff --git a/src/ComputeSharp.UI/ComputeSharp.UI.shproj b/src/ComputeSharp.UI/ComputeSharp.UI.shproj index dfd65f65..4196b150 100644 --- a/src/ComputeSharp.UI/ComputeSharp.UI.shproj +++ b/src/ComputeSharp.UI/ComputeSharp.UI.shproj @@ -7,7 +7,6 @@ - diff --git a/src/TerraFX.Interop.Windows.D2D1/TerraFX.Interop.Windows.D2D1.shproj b/src/TerraFX.Interop.Windows.D2D1/TerraFX.Interop.Windows.D2D1.shproj index 4ebe595b..36dcdc72 100644 --- a/src/TerraFX.Interop.Windows.D2D1/TerraFX.Interop.Windows.D2D1.shproj +++ b/src/TerraFX.Interop.Windows.D2D1/TerraFX.Interop.Windows.D2D1.shproj @@ -7,7 +7,6 @@ - diff --git a/src/TerraFX.Interop.Windows.Dynamic/TerraFX.Interop.Windows.Dynamic.shproj b/src/TerraFX.Interop.Windows.Dynamic/TerraFX.Interop.Windows.Dynamic.shproj index 04db5f31..09d48832 100644 --- a/src/TerraFX.Interop.Windows.Dynamic/TerraFX.Interop.Windows.Dynamic.shproj +++ b/src/TerraFX.Interop.Windows.Dynamic/TerraFX.Interop.Windows.Dynamic.shproj @@ -7,7 +7,6 @@ - diff --git a/src/TerraFX.Interop.Windows/TerraFX.Interop.Windows.shproj b/src/TerraFX.Interop.Windows/TerraFX.Interop.Windows.shproj index 567d0dbd..79ff33ca 100644 --- a/src/TerraFX.Interop.Windows/TerraFX.Interop.Windows.shproj +++ b/src/TerraFX.Interop.Windows/TerraFX.Interop.Windows.shproj @@ -7,7 +7,6 @@ - diff --git a/tests/ComputeSharp.D2D1.Tests/ComputeSharp.D2D1.Tests.csproj b/tests/ComputeSharp.D2D1.Tests/ComputeSharp.D2D1.Tests.csproj index 9f61c43a..a3818860 100644 --- a/tests/ComputeSharp.D2D1.Tests/ComputeSharp.D2D1.Tests.csproj +++ b/tests/ComputeSharp.D2D1.Tests/ComputeSharp.D2D1.Tests.csproj @@ -100,4 +100,6 @@ + + diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1TransformMapperTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1TransformMapperTests.cs index edf50028..b7ee61ac 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1TransformMapperTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1TransformMapperTests.cs @@ -4,7 +4,7 @@ using System.Reflection; using System.Runtime.InteropServices; using ComputeSharp.D2D1.Interop; using ComputeSharp.D2D1.Tests.Helpers; -using ComputeSharp.D2D1.Tests.Shaders; +using ComputeSharp.SwapChain.Shaders.D2D1; using ComputeSharp.Tests.Helpers; using Microsoft.VisualStudio.TestTools.UnitTesting; using Win32; diff --git a/tests/ComputeSharp.D2D1.Tests/ShadersTests.cs b/tests/ComputeSharp.D2D1.Tests/ShadersTests.cs index 2dd70375..2c05cc1c 100644 --- a/tests/ComputeSharp.D2D1.Tests/ShadersTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/ShadersTests.cs @@ -4,6 +4,7 @@ using System.Reflection; using System.Runtime.InteropServices; using ComputeSharp.D2D1.Interop; using ComputeSharp.D2D1.Tests.Helpers; +using ComputeSharp.SwapChain.Shaders.D2D1; using Microsoft.VisualStudio.TestTools.UnitTesting; using SixLabors.ImageSharp; using SixLabors.ImageSharp.PixelFormats; @@ -17,25 +18,25 @@ public class ShadersTests [TestMethod] public void HelloWorld() { - RunTest(); + RunTest(); } [TestMethod] public void ColorfulInfinity() { - RunTest(); + RunTest(); } [TestMethod] public void FractalTiling() { - RunTest(); + RunTest(); } [TestMethod] public void MengerJourney() { - RunTest(0.000011f); + RunTest(0.000011f); } // This test and the other 3 below are skipped because they do produce valid result, @@ -46,33 +47,33 @@ public class ShadersTests [Ignore] public void TwoTiledTruchet() { - RunTest(); + RunTest(); } [TestMethod] public void Octagrams() { - RunTest(); + RunTest(); } [TestMethod] public void ProteanClouds() { - RunTest(); + RunTest(); } [TestMethod] [Ignore] public void PyramidPattern() { - RunTest(); + RunTest(); } [TestMethod] [Ignore] public void TriangleGridContouring() { - RunTest(); + RunTest(); } [TestMethod] @@ -101,15 +102,15 @@ public class ShadersTests strides: stackalloc uint[] { (uint)(texture.Width * sizeof(Rgba32)) }); } - Shaders.ContouredLayers shader = new(0f, new int2(1280, 720)); + ContouredLayers shader = new(0f, new int2(1280, 720)); - D2D1TestRunner.RunAndCompareShader(in shader, 1280, 720, $"{nameof(Shaders.ContouredLayers)}.png", nameof(Shaders.ContouredLayers), resourceTextures: (0, resourceTextureManager)); + D2D1TestRunner.RunAndCompareShader(in shader, 1280, 720, $"{nameof(ContouredLayers)}.png", nameof(ContouredLayers), resourceTextures: (0, resourceTextureManager)); } [TestMethod] public void TerracedHills() { - RunTest(0.000026f); + RunTest(0.000026f); } private static void RunTest(float threshold = 0.00001f)