diff --git a/ComputeSharp.sln b/ComputeSharp.sln
index ac5b7e06..c82125bb 100644
--- a/ComputeSharp.sln
+++ b/ComputeSharp.sln
@@ -54,6 +54,8 @@ Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "ComputeSharp.NetStandard",
EndProject
Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "TerraFX.Interop.Windows", "src\TerraFX.Interop.Windows\TerraFX.Interop.Windows.shproj", "{514FF00E-BBF0-4D55-A47C-EEEB6208ADCE}"
EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ComputeSharp.WinUI", "src\ComputeSharp.WinUI\ComputeSharp.WinUI.csproj", "{FA18BA9F-76BF-4DE0-8D24-02FC7657E6CB}"
+EndProject
Global
GlobalSection(SharedMSBuildProjectFiles) = preSolution
src\ComputeSharp.NetStandard\ComputeSharp.NetStandard.projitems*{314eb353-e7e3-4146-8111-685cc83ec1de}*SharedItemsImports = 5
@@ -216,6 +218,14 @@ Global
{1F37A704-3CDF-45BB-8114-ED9137E5F2CF}.Release|ARM64.Build.0 = Release|Any CPU
{1F37A704-3CDF-45BB-8114-ED9137E5F2CF}.Release|x64.ActiveCfg = Release|Any CPU
{1F37A704-3CDF-45BB-8114-ED9137E5F2CF}.Release|x64.Build.0 = Release|Any CPU
+ {FA18BA9F-76BF-4DE0-8D24-02FC7657E6CB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {FA18BA9F-76BF-4DE0-8D24-02FC7657E6CB}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {FA18BA9F-76BF-4DE0-8D24-02FC7657E6CB}.Debug|x64.ActiveCfg = Debug|Any CPU
+ {FA18BA9F-76BF-4DE0-8D24-02FC7657E6CB}.Debug|x64.Build.0 = Debug|Any CPU
+ {FA18BA9F-76BF-4DE0-8D24-02FC7657E6CB}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {FA18BA9F-76BF-4DE0-8D24-02FC7657E6CB}.Release|Any CPU.Build.0 = Release|Any CPU
+ {FA18BA9F-76BF-4DE0-8D24-02FC7657E6CB}.Release|x64.ActiveCfg = Release|Any CPU
+ {FA18BA9F-76BF-4DE0-8D24-02FC7657E6CB}.Release|x64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
diff --git a/src/ComputeSharp.WinUI/ComputeShaderPanel.Properties.cs b/src/ComputeSharp.WinUI/ComputeShaderPanel.Properties.cs
new file mode 100644
index 00000000..66bd8ccf
--- /dev/null
+++ b/src/ComputeSharp.WinUI/ComputeShaderPanel.Properties.cs
@@ -0,0 +1,27 @@
+using Microsoft.UI.Xaml;
+using Microsoft.UI.Xaml.Controls;
+
+namespace ComputeSharp.WinUI
+{
+ ///
+ public sealed unsafe partial class ComputeShaderPanel : SwapChainPanel
+ {
+ ///
+ /// Gets or sets the instance to use to render content.
+ ///
+ public IShaderRunner? ShaderRunner
+ {
+ get => (IShaderRunner?)GetValue(ShaderFactoryProperty);
+ set => SetValue(ShaderFactoryProperty, value);
+ }
+
+ ///
+ /// The backing .
+ ///
+ public static readonly DependencyProperty ShaderFactoryProperty = DependencyProperty.Register(
+ nameof(ShaderRunner),
+ typeof(IShaderRunner),
+ typeof(ComputeShaderPanel),
+ new PropertyMetadata(null, static (d, e) => ((ComputeShaderPanel)d).shaderRunner = (IShaderRunner?)e.NewValue));
+ }
+}
diff --git a/src/ComputeSharp.WinUI/ComputeShaderPanel.Rendering.cs b/src/ComputeSharp.WinUI/ComputeShaderPanel.Rendering.cs
new file mode 100644
index 00000000..d995783d
--- /dev/null
+++ b/src/ComputeSharp.WinUI/ComputeShaderPanel.Rendering.cs
@@ -0,0 +1,309 @@
+using System;
+using ComputeSharp.Interop;
+using Microsoft.UI.Xaml.Controls;
+using TerraFX.Interop;
+using FX = TerraFX.Interop.Windows;
+
+namespace ComputeSharp.WinUI
+{
+ ///
+ public sealed partial class ComputeShaderPanel : SwapChainPanel
+ {
+ ///
+ /// The instance used to create shaders to run.
+ ///
+ private IShaderRunner? shaderRunner;
+
+ ///
+ /// The pointer for the device currently in use.
+ ///
+ private ComPtr d3D12Device;
+
+ ///
+ /// The instance to use for graphics operations.
+ ///
+ private ComPtr d3D12CommandQueue;
+
+ ///
+ /// The instance used for graphics operations.
+ ///
+ private ComPtr d3D12Fence;
+
+ ///
+ /// The next fence value for graphics operations using .
+ ///
+ private ulong nextD3D12FenceValue = 1;
+
+ ///
+ /// The object to create command lists.
+ ///
+ private ComPtr d3D12CommandAllocator;
+
+ ///
+ /// The instance used to copy data to the back buffers.
+ ///
+ private ComPtr d3D12GraphicsCommandList;
+
+ ///
+ /// The instance used to display content onto the target window.
+ ///
+ public ComPtr dxgiSwapChain1;
+
+ ///
+ /// The first buffer within .
+ ///
+ private ComPtr d3D12Resource0;
+
+ ///
+ /// The second buffer within .
+ ///
+ private ComPtr d3D12Resource1;
+
+ ///
+ /// The index of the next buffer that can be used to present content.
+ ///
+ private uint currentBufferIndex;
+
+ ///
+ /// The instance used to prepare frames to display.
+ ///
+ private ReadWriteTexture2D? texture;
+
+ ///
+ /// Whether or not the window has been resized and requires the buffers to be updated.
+ ///
+ private bool isResizePending;
+
+ ///
+ /// Initializes the current application.
+ ///
+ /// The handle for the window.
+ private unsafe void OnInitialize(HWND hwnd)
+ {
+ // Get the underlying ID3D12Device in use
+ fixed (ID3D12Device** d3D12Device = this.d3D12Device)
+ {
+ _ = InteropServices.TryGetID3D12Device(Gpu.Default, FX.__uuidof(), (void**)d3D12Device);
+ }
+
+ // Create the direct command queue to use
+ fixed (ID3D12CommandQueue** d3D12CommandQueue = this.d3D12CommandQueue)
+ {
+ D3D12_COMMAND_QUEUE_DESC d3D12CommandQueueDesc;
+ d3D12CommandQueueDesc.Type = D3D12_COMMAND_LIST_TYPE.D3D12_COMMAND_LIST_TYPE_DIRECT;
+ d3D12CommandQueueDesc.Priority = (int)D3D12_COMMAND_QUEUE_PRIORITY.D3D12_COMMAND_QUEUE_PRIORITY_NORMAL;
+ d3D12CommandQueueDesc.Flags = D3D12_COMMAND_QUEUE_FLAGS.D3D12_COMMAND_QUEUE_FLAG_NONE;
+ d3D12CommandQueueDesc.NodeMask = 0;
+
+ _ = d3D12Device.Get()->CreateCommandQueue(
+ &d3D12CommandQueueDesc,
+ FX.__uuidof(),
+ (void**)d3D12CommandQueue);
+ }
+
+ // Create the direct fence
+ fixed (ID3D12Fence** d3D12Fence = this.d3D12Fence)
+ {
+ _ = this.d3D12Device.Get()->CreateFence(
+ 0,
+ D3D12_FENCE_FLAGS.D3D12_FENCE_FLAG_NONE,
+ FX.__uuidof(),
+ (void**)d3D12Fence);
+ }
+
+ // Create the swap chain to display frames
+ fixed (IDXGISwapChain1** dxgiSwapChain1 = this.dxgiSwapChain1)
+ {
+ using ComPtr dxgiFactory2 = default;
+
+ _ = FX.CreateDXGIFactory2(FX.DXGI_CREATE_FACTORY_DEBUG, FX.__uuidof(), (void**)dxgiFactory2.GetAddressOf());
+
+ DXGI_SWAP_CHAIN_DESC1 dxgiSwapChainDesc1 = default;
+ dxgiSwapChainDesc1.AlphaMode = DXGI_ALPHA_MODE.DXGI_ALPHA_MODE_IGNORE;
+ dxgiSwapChainDesc1.BufferCount = 2;
+ dxgiSwapChainDesc1.Flags = 0;
+ dxgiSwapChainDesc1.Format = DXGI_FORMAT.DXGI_FORMAT_R8G8B8A8_UNORM;
+ dxgiSwapChainDesc1.Width = 0;
+ dxgiSwapChainDesc1.Height = 0;
+ dxgiSwapChainDesc1.SampleDesc = new DXGI_SAMPLE_DESC(count: 1, quality: 0);
+ dxgiSwapChainDesc1.Scaling = DXGI_SCALING.DXGI_SCALING_STRETCH;
+ dxgiSwapChainDesc1.Stereo = 0;
+ dxgiSwapChainDesc1.SwapEffect = DXGI_SWAP_EFFECT.DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL;
+
+ _ = dxgiFactory2.Get()->CreateSwapChainForHwnd(
+ (IUnknown*)d3D12CommandQueue.Get(),
+ hwnd,
+ &dxgiSwapChainDesc1,
+ null,
+ null,
+ dxgiSwapChain1);
+ }
+
+ // Create the command allocator to use
+ fixed (ID3D12CommandAllocator** d3D12CommandAllocator = this.d3D12CommandAllocator)
+ {
+ this.d3D12Device.Get()->CreateCommandAllocator(
+ D3D12_COMMAND_LIST_TYPE.D3D12_COMMAND_LIST_TYPE_DIRECT,
+ FX.__uuidof(),
+ (void**)d3D12CommandAllocator);
+ }
+
+ // Create the reusable command list to copy data to the back buffers
+ fixed (ID3D12GraphicsCommandList** d3D12GraphicsCommandList = this.d3D12GraphicsCommandList)
+ {
+ this.d3D12Device.Get()->CreateCommandList(
+ 0,
+ D3D12_COMMAND_LIST_TYPE.D3D12_COMMAND_LIST_TYPE_DIRECT,
+ d3D12CommandAllocator,
+ null,
+ FX.__uuidof(),
+ (void**)d3D12GraphicsCommandList);
+ }
+
+ // Close the command list to prepare it for future use
+ this.d3D12GraphicsCommandList.Get()->Close();
+ }
+
+ ///
+ /// Resizes the current application.
+ ///
+ private void OnResize()
+ {
+ this.isResizePending = true;
+ }
+
+ ///
+ /// Applies the actual resize logic that was scheduled from .
+ ///
+ private unsafe void ApplyResize()
+ {
+ this.d3D12CommandQueue.Get()->Signal(this.d3D12Fence.Get(), this.nextD3D12FenceValue);
+
+ // Wait for the fence again to ensure there are no pending operations
+ this.d3D12Fence.Get()->SetEventOnCompletion(this.nextD3D12FenceValue, default);
+
+ this.nextD3D12FenceValue++;
+
+ // Dispose the old buffers before resizing the buffer
+ this.d3D12Resource0.Dispose();
+ this.d3D12Resource1.Dispose();
+
+ // Resize the swap chain buffers
+ this.dxgiSwapChain1.Get()->ResizeBuffers(0, 0, 0, DXGI_FORMAT.DXGI_FORMAT_UNKNOWN, 0);
+
+ // Get the index of the initial back buffer
+ using (ComPtr dxgiSwapChain3 = default)
+ {
+ _ = this.dxgiSwapChain1.CopyTo(dxgiSwapChain3.GetAddressOf());
+
+ this.currentBufferIndex = dxgiSwapChain3.Get()->GetCurrentBackBufferIndex();
+ }
+
+ // Retrieve the back buffers for the swap chain
+ fixed (ID3D12Resource** d3D12Resource0 = this.d3D12Resource0)
+ fixed (ID3D12Resource** d3D12Resource1 = this.d3D12Resource1)
+ {
+ _ = dxgiSwapChain1.Get()->GetBuffer(0, FX.__uuidof(), (void**)d3D12Resource0);
+ _ = dxgiSwapChain1.Get()->GetBuffer(1, FX.__uuidof(), (void**)d3D12Resource1);
+ }
+
+ this.texture?.Dispose();
+
+ D3D12_RESOURCE_DESC d3D12Resource0Description = this.d3D12Resource0.Get()->GetDesc();
+
+ // Create the 2D texture to use to generate frames to display
+ this.texture = Gpu.Default.AllocateReadWriteTexture2D(
+ (int)d3D12Resource0Description.Width,
+ (int)d3D12Resource0Description.Height);
+ }
+
+ ///
+ /// Updates the current application.
+ ///
+ /// The current time since the start of the application.
+ private unsafe void OnUpdate(TimeSpan time)
+ {
+ if (this.isResizePending)
+ {
+ ApplyResize();
+
+ this.isResizePending = false;
+ }
+
+ // Skip if no factory is available
+ if (this.shaderRunner is null)
+ {
+ return;
+ }
+
+ // Generate the new frame
+ this.shaderRunner.Execute(this.texture!, time);
+
+ using ComPtr d3D12Resource = default;
+
+ // Get the underlying ID3D12Resource pointer for the texture
+ _ = InteropServices.TryGetID3D12Resource(this.texture!, FX.__uuidof(), (void**)d3D12Resource.GetAddressOf());
+
+ // Get the target back buffer to update
+ ID3D12Resource* d3D12ResourceBackBuffer = this.currentBufferIndex switch
+ {
+ 0 => this.d3D12Resource0.Get(),
+ 1 => this.d3D12Resource1.Get(),
+ _ => null
+ };
+
+ this.currentBufferIndex ^= 1;
+
+ // Reset the command list and command allocator
+ this.d3D12CommandAllocator.Get()->Reset();
+ this.d3D12GraphicsCommandList.Get()->Reset(this.d3D12CommandAllocator.Get(), null);
+
+ D3D12_RESOURCE_BARRIER* d3D12ResourceBarriers = stackalloc D3D12_RESOURCE_BARRIER[]
+ {
+ D3D12_RESOURCE_BARRIER.InitTransition(
+ d3D12Resource.Get(),
+ D3D12_RESOURCE_STATES.D3D12_RESOURCE_STATE_UNORDERED_ACCESS,
+ D3D12_RESOURCE_STATES.D3D12_RESOURCE_STATE_COPY_SOURCE),
+ D3D12_RESOURCE_BARRIER.InitTransition(
+ d3D12ResourceBackBuffer,
+ D3D12_RESOURCE_STATES.D3D12_RESOURCE_STATE_COMMON,
+ D3D12_RESOURCE_STATES.D3D12_RESOURCE_STATE_COPY_DEST)
+ };
+
+ // Transition the resources to COPY_DEST and COPY_SOURCE respectively
+ d3D12GraphicsCommandList.Get()->ResourceBarrier(2, d3D12ResourceBarriers);
+
+ // Copy the generated frame to the target back buffer
+ d3D12GraphicsCommandList.Get()->CopyResource(d3D12ResourceBackBuffer, d3D12Resource.Get());
+
+ d3D12ResourceBarriers[0] = D3D12_RESOURCE_BARRIER.InitTransition(
+ d3D12Resource.Get(),
+ D3D12_RESOURCE_STATES.D3D12_RESOURCE_STATE_COPY_SOURCE,
+ D3D12_RESOURCE_STATES.D3D12_RESOURCE_STATE_UNORDERED_ACCESS);
+
+ d3D12ResourceBarriers[1] = D3D12_RESOURCE_BARRIER.InitTransition(
+ d3D12ResourceBackBuffer,
+ D3D12_RESOURCE_STATES.D3D12_RESOURCE_STATE_COPY_DEST,
+ D3D12_RESOURCE_STATES.D3D12_RESOURCE_STATE_COMMON);
+
+ // Transition the resources back to COMMON and UNORDERED_ACCESS respectively
+ d3D12GraphicsCommandList.Get()->ResourceBarrier(2, d3D12ResourceBarriers);
+
+ d3D12GraphicsCommandList.Get()->Close();
+
+ // Execute the command list to perform the copy
+ this.d3D12CommandQueue.Get()->ExecuteCommandLists(1, (ID3D12CommandList**)d3D12GraphicsCommandList.GetAddressOf());
+ this.d3D12CommandQueue.Get()->Signal(this.d3D12Fence.Get(), this.nextD3D12FenceValue);
+
+ // Present the new frame
+ this.dxgiSwapChain1.Get()->Present(0, 0);
+
+ if (this.nextD3D12FenceValue > this.d3D12Fence.Get()->GetCompletedValue())
+ {
+ this.d3D12Fence.Get()->SetEventOnCompletion(this.nextD3D12FenceValue, default);
+ }
+
+ this.nextD3D12FenceValue++;
+ }
+ }
+}
diff --git a/src/ComputeSharp.WinUI/ComputeShaderPanel.cs b/src/ComputeSharp.WinUI/ComputeShaderPanel.cs
new file mode 100644
index 00000000..0ffd8344
--- /dev/null
+++ b/src/ComputeSharp.WinUI/ComputeShaderPanel.cs
@@ -0,0 +1,113 @@
+using System;
+using System.Diagnostics;
+using System.Diagnostics.Contracts;
+using System.Runtime.InteropServices;
+using System.Threading;
+using Microsoft.UI.Xaml;
+using Microsoft.UI.Xaml.Controls;
+using TerraFX.Interop;
+using WinRT;
+
+namespace ComputeSharp.WinUI
+{
+ ///
+ /// A custom that can be used to render animated backgrounds.
+ /// It is powered by ComputeSharp and leverages compute shaders to create frames to display.
+ ///
+ public sealed unsafe partial class ComputeShaderPanel : SwapChainPanel
+ {
+ private readonly ISwapChainPanelNative* swapChainPanelNative;
+
+ private Thread? renderThread;
+
+ ///
+ /// Creates a new instance.
+ ///
+ public ComputeShaderPanel()
+ {
+ this.swapChainPanelNative = GetISwapChainPanelNative();
+
+ this.Loaded += ComputeShaderPanel_Loaded;
+ this.SizeChanged += ComputeShaderPanel_SizeChanged;
+ }
+
+ private void ComputeShaderPanel_Loaded(object sender, RoutedEventArgs e)
+ {
+ IntPtr hwnd = XamlRoot.As().HostWindow;
+
+ OnInitialize(hwnd);
+ OnResize();
+
+ using ComPtr idxgiSwapChain = default;
+
+ _ = this.dxgiSwapChain1.CopyTo(&idxgiSwapChain);
+
+ this.swapChainPanelNative->SetSwapChain(idxgiSwapChain.Get());
+
+ this.renderThread = new Thread(static args =>
+ {
+ var panel = (ComputeShaderPanel)args!;
+
+ Stopwatch
+ startStopwatch = Stopwatch.StartNew(),
+ frameStopwatch = Stopwatch.StartNew();
+
+ const long targetFrameTimeInTicksFor60fps = 166666;
+
+ panel.OnUpdate(TimeSpan.Zero);
+
+ while (true)
+ {
+ if (frameStopwatch.ElapsedTicks >= targetFrameTimeInTicksFor60fps)
+ {
+ frameStopwatch.Restart();
+
+ panel.OnUpdate(startStopwatch.Elapsed);
+ }
+ }
+ });
+
+ this.renderThread.Start(this);
+ }
+
+ private void ComputeShaderPanel_SizeChanged(object sender, SizeChangedEventArgs e)
+ {
+ OnResize();
+ }
+
+ ///
+ /// Gets a pointer to the underlying object.
+ ///
+ /// The pointer for the object in use.
+ [Pure]
+ private ISwapChainPanelNative* GetISwapChainPanelNative()
+ {
+ IObjectReference swapChainAsIUnknown = ((IWinRTObject)this).NativeObject;
+
+ IUnknown* iUnknown = (IUnknown*)swapChainAsIUnknown.ThisPtr;
+ ISwapChainPanelNative* iSwapChainPanelNative = default;
+
+ Guid iSwapChainPanelNativeUuid = Guid.Parse("63AAD0B8-7C24-40FF-85A8-640D944CC325");
+
+ _ = iUnknown->QueryInterface(
+ &iSwapChainPanelNativeUuid,
+ (void**)&iSwapChainPanelNative);
+
+ return iSwapChainPanelNative;
+ }
+
+ ///
+ /// An interface for the COM object behind .
+ ///
+ [ComImport]
+ [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
+ [Guid("4CCD7521-9C08-41AD-A5BD-B263EF64C9E7")]
+ private interface IXamlRootNative
+ {
+ ///
+ /// Gets the handle for the host window containing the current instance.
+ ///
+ IntPtr HostWindow { get; }
+ }
+ }
+}
diff --git a/src/ComputeSharp.WinUI/ComputeSharp.WinUI.csproj b/src/ComputeSharp.WinUI/ComputeSharp.WinUI.csproj
new file mode 100644
index 00000000..1ac64a9c
--- /dev/null
+++ b/src/ComputeSharp.WinUI/ComputeSharp.WinUI.csproj
@@ -0,0 +1,25 @@
+
+
+ net5.0-windows10.0.19041.0
+ 10.0.17763.0
+ ComputeSharp.WinUI
+ win10-x86;win10-x64;win10-arm64
+ true
+ enable
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/src/ComputeSharp.WinUI/IShaderRunner.cs b/src/ComputeSharp.WinUI/IShaderRunner.cs
new file mode 100644
index 00000000..ed633df9
--- /dev/null
+++ b/src/ComputeSharp.WinUI/IShaderRunner.cs
@@ -0,0 +1,17 @@
+using System;
+
+namespace ComputeSharp.WinUI
+{
+ ///
+ /// An interface for a shader runner to be used with .
+ ///
+ public interface IShaderRunner
+ {
+ ///
+ /// Renders a single frame to a texture.
+ ///
+ /// The target texture to render the frame to.
+ /// The timespan for the current frame.
+ public void Execute(IReadWriteTexture2D texture, TimeSpan timespan);
+ }
+}
diff --git a/src/ComputeSharp.WinUI/ShaderRunner{T}.cs b/src/ComputeSharp.WinUI/ShaderRunner{T}.cs
new file mode 100644
index 00000000..54d8ca6f
--- /dev/null
+++ b/src/ComputeSharp.WinUI/ShaderRunner{T}.cs
@@ -0,0 +1,32 @@
+using System;
+
+namespace ComputeSharp.WinUI
+{
+ ///
+ /// An implementation powered by a supplied shader type.
+ ///
+ /// The type of shader to use to render frames.
+ public sealed class ShaderRunner : IShaderRunner
+ where T : struct, IComputeShader
+ {
+ ///
+ /// The instance used to create shaders to run.
+ ///
+ private readonly Func, TimeSpan, T> shaderFactory;
+
+ ///
+ /// Creates a new instance.
+ ///
+ /// The instance used to create shaders to run.
+ public ShaderRunner(Func, TimeSpan, T> shaderFactory)
+ {
+ this.shaderFactory = shaderFactory;
+ }
+
+ ///
+ public void Execute(IReadWriteTexture2D texture, TimeSpan time)
+ {
+ Gpu.Default.For(texture.Width, texture.Height, this.shaderFactory(texture, time));
+ }
+ }
+}