diff --git a/src/ComputeSharp.WinUI/ComputeShaderPanel.Properties.cs b/src/ComputeSharp.WinUI/ComputeShaderPanel.Properties.cs
index 7329f7fb..88fe7e15 100644
--- a/src/ComputeSharp.WinUI/ComputeShaderPanel.Properties.cs
+++ b/src/ComputeSharp.WinUI/ComputeShaderPanel.Properties.cs
@@ -32,8 +32,8 @@ namespace ComputeSharp.WinUI
/// The accepted range is [0.1 and 1] (default is 1).
public double ResolutionScale
{
- get => (double)GetValue(ShaderFactoryProperty);
- set => SetValue(ShaderFactoryProperty, value);
+ get => (double)GetValue(ResolutionScaleProperty);
+ set => SetValue(ResolutionScaleProperty, value);
}
///
diff --git a/src/ComputeSharp.WinUI/ComputeShaderPanel.Rendering.cs b/src/ComputeSharp.WinUI/ComputeShaderPanel.Rendering.cs
index aedcd6b0..8e551488 100644
--- a/src/ComputeSharp.WinUI/ComputeShaderPanel.Rendering.cs
+++ b/src/ComputeSharp.WinUI/ComputeShaderPanel.Rendering.cs
@@ -79,11 +79,30 @@ namespace ComputeSharp.WinUI
///
private bool isResizePending;
+ ///
+ /// The backing store for for the render thread.
+ ///
+ private double width;
+
+ ///
+ /// The backing store for for the render thread.
+ ///
+ private double height;
+
+ ///
+ /// The backing store for for the render thread.
+ ///
+ private float compositionScaleX;
+
+ ///
+ /// The backing store for for the render thread.
+ ///
+ private float compositionScaleY;
+
///
/// Initializes the current application.
///
- /// The handle for the window.
- private unsafe void OnInitialize(HWND hwnd)
+ private unsafe void OnInitialize()
{
// Get the underlying ID3D12Device in use
fixed (ID3D12Device** d3D12Device = this.d3D12Device)
@@ -128,19 +147,18 @@ namespace ComputeSharp.WinUI
dxgiSwapChainDesc1.BufferCount = 2;
dxgiSwapChainDesc1.Flags = 0;
dxgiSwapChainDesc1.Format = DXGI_FORMAT.DXGI_FORMAT_R8G8B8A8_UNORM;
- dxgiSwapChainDesc1.Width = 0;
- dxgiSwapChainDesc1.Height = 0;
+ dxgiSwapChainDesc1.Width = (uint)Math.Max(Math.Ceiling(this.width * this.compositionScaleX * this.resolutionScale), 1.0);
+ dxgiSwapChainDesc1.Height = (uint)Math.Max(Math.Ceiling(this.height * this.compositionScaleY * this.resolutionScale), 1.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;
+ dxgiSwapChainDesc1.BufferUsage = FX.DXGI_USAGE_RENDER_TARGET_OUTPUT;
- _ = dxgiFactory2.Get()->CreateSwapChainForHwnd(
+ int h = dxgiFactory2.Get()->CreateSwapChainForComposition(
(IUnknown*)d3D12CommandQueue.Get(),
- hwnd,
&dxgiSwapChainDesc1,
null,
- null,
dxgiSwapChain1);
}
@@ -194,47 +212,20 @@ namespace ComputeSharp.WinUI
this.d3D12Resource1.Dispose();
// Resize the swap chain buffers
- this.dxgiSwapChain1.Get()->ResizeBuffers(0, 0, 0, DXGI_FORMAT.DXGI_FORMAT_UNKNOWN, 0);
+ _ = this.dxgiSwapChain1.Get()->ResizeBuffers(
+ 0,
+ (uint)Math.Max(Math.Ceiling(this.width * this.compositionScaleX * this.resolutionScale), 1.0),
+ (uint)Math.Max(Math.Ceiling(this.height * this.compositionScaleY * this.resolutionScale), 1.0),
+ DXGI_FORMAT.DXGI_FORMAT_UNKNOWN,
+ 0);
- if (this.resolutionScale == 1.0)
+ // Retrieve the back buffers for the swap chain
+ fixed (ID3D12Resource** d3D12Resource0 = this.d3D12Resource0)
+ fixed (ID3D12Resource** d3D12Resource1 = this.d3D12Resource1)
{
- // 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);
- }
+ _ = dxgiSwapChain1.Get()->GetBuffer(0, FX.__uuidof(), (void**)d3D12Resource0);
+ _ = dxgiSwapChain1.Get()->GetBuffer(1, FX.__uuidof(), (void**)d3D12Resource1);
}
- else
- {
- D3D12_RESOURCE_DESC d3D12ResourceDescription;
-
- // This is a workaround to detect the right scaled resolution for the back buffers.
- // First, IDXGISwapChain.Resize is called with (0, 0) as parameters for the size: this will cause
- // it to automatically resize to a 1:1 scaling factor with the available space. Then we get the
- // first backbuffer and use it to find the current resolution, scale it, and resize again to that.
- using (ComPtr d3D12Resource = default)
- {
- _ = dxgiSwapChain1.Get()->GetBuffer(0, FX.__uuidof(), (void**)&d3D12Resource);
-
- d3D12ResourceDescription = d3D12Resource.Get()->GetDesc();
- }
-
- uint
- scaledWidth = (uint)(d3D12ResourceDescription.Width * this.resolutionScale),
- scaledHeight = (uint)(d3D12ResourceDescription.Height * this.resolutionScale);
-
- this.dxgiSwapChain1.Get()->ResizeBuffers(0, scaledWidth, scaledHeight, DXGI_FORMAT.DXGI_FORMAT_UNKNOWN, 0);
-
- // Finally retrieve the scaled back buffers
- 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);
- }
- }
// Get the index of the initial back buffer
using (ComPtr dxgiSwapChain3 = default)
diff --git a/src/ComputeSharp.WinUI/ComputeShaderPanel.cs b/src/ComputeSharp.WinUI/ComputeShaderPanel.cs
index 0ffd8344..676e75e2 100644
--- a/src/ComputeSharp.WinUI/ComputeShaderPanel.cs
+++ b/src/ComputeSharp.WinUI/ComputeShaderPanel.cs
@@ -1,8 +1,7 @@
using System;
using System.Diagnostics;
-using System.Diagnostics.Contracts;
-using System.Runtime.InteropServices;
using System.Threading;
+using ComputeSharp.Core.Extensions;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using TerraFX.Interop;
@@ -16,8 +15,6 @@ namespace ComputeSharp.WinUI
///
public sealed unsafe partial class ComputeShaderPanel : SwapChainPanel
{
- private readonly ISwapChainPanelNative* swapChainPanelNative;
-
private Thread? renderThread;
///
@@ -25,24 +22,39 @@ namespace ComputeSharp.WinUI
///
public ComputeShaderPanel()
{
- this.swapChainPanelNative = GetISwapChainPanelNative();
-
this.Loaded += ComputeShaderPanel_Loaded;
this.SizeChanged += ComputeShaderPanel_SizeChanged;
+ this.CompositionScaleChanged += ComputeShaderPanel_CompositionScaleChanged;
}
+ // Initializes the swap chain and starts the render thread
private void ComputeShaderPanel_Loaded(object sender, RoutedEventArgs e)
{
- IntPtr hwnd = XamlRoot.As().HostWindow;
+ this.width = ActualWidth;
+ this.height = ActualHeight;
+ this.compositionScaleX = CompositionScaleX;
+ this.compositionScaleY = CompositionScaleY;
+ this.resolutionScale = ResolutionScale;
- OnInitialize(hwnd);
- OnResize();
+ OnInitialize();
- using ComPtr idxgiSwapChain = default;
+ // Extract the ISwapChainPanelNative reference from the current panel, then query the
+ // IDXGISwapChain reference just created and set that as the swap chain panel to use.
+ using (ComPtr swapChainPanelNative = default)
+ {
+ IUnknown* swapChainPanel = (IUnknown*)((IWinRTObject)this).NativeObject.ThisPtr;
+ Guid iSwapChainPanelNativeUuid = Guid.Parse("63AAD0B8-7C24-40FF-85A8-640D944CC325");
- _ = this.dxgiSwapChain1.CopyTo(&idxgiSwapChain);
+ swapChainPanel->QueryInterface(
+ &iSwapChainPanelNativeUuid,
+ (void**)&swapChainPanelNative).Assert();
- this.swapChainPanelNative->SetSwapChain(idxgiSwapChain.Get());
+ using ComPtr idxgiSwapChain = default;
+
+ this.dxgiSwapChain1.CopyTo(&idxgiSwapChain).Assert();
+
+ swapChainPanelNative.Get()->SetSwapChain(idxgiSwapChain.Get()).Assert();
+ }
this.renderThread = new Thread(static args =>
{
@@ -70,44 +82,22 @@ namespace ComputeSharp.WinUI
this.renderThread.Start(this);
}
+ // Updates the background store for the frame size factors used by the render thread
private void ComputeShaderPanel_SizeChanged(object sender, SizeChangedEventArgs e)
{
+ this.width = e.NewSize.Width;
+ this.height = e.NewSize.Height;
+
OnResize();
}
- ///
- /// Gets a pointer to the underlying object.
- ///
- /// The pointer for the object in use.
- [Pure]
- private ISwapChainPanelNative* GetISwapChainPanelNative()
+ // Updates the background store for the composition scale factors used by the render thread
+ private void ComputeShaderPanel_CompositionScaleChanged(SwapChainPanel sender, object args)
{
- IObjectReference swapChainAsIUnknown = ((IWinRTObject)this).NativeObject;
+ this.compositionScaleX = CompositionScaleX;
+ this.compositionScaleY = CompositionScaleY;
- 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; }
+ OnResize();
}
}
}
diff --git a/src/ComputeSharp/Properties/AssemblyInfo.cs b/src/ComputeSharp/Properties/AssemblyInfo.cs
index 0d511608..a671df7e 100644
--- a/src/ComputeSharp/Properties/AssemblyInfo.cs
+++ b/src/ComputeSharp/Properties/AssemblyInfo.cs
@@ -3,3 +3,4 @@
[module: SkipLocalsInit]
[assembly: InternalsVisibleTo("ComputeSharp.Tests.Internals")]
+[assembly: InternalsVisibleTo("ComputeSharp.WinUI")]