Switched to Composition swap chain, fixed resizing, code tweaks
このコミットが含まれているのは:
1 親
f0dbf5ce59
コミット
5cb70828ac
4ファイル変更
+72
-90
検索条件に一致するファイルはありません
@@ -32,8 +32,8 @@ namespace ComputeSharp.WinUI
|
||||
/// <remarks>The accepted range is [0.1 and 1] (default is 1).</remarks>
|
||||
public double ResolutionScale
|
||||
{
|
||||
get => (double)GetValue(ShaderFactoryProperty);
|
||||
set => SetValue(ShaderFactoryProperty, value);
|
||||
get => (double)GetValue(ResolutionScaleProperty);
|
||||
set => SetValue(ResolutionScaleProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -79,11 +79,30 @@ namespace ComputeSharp.WinUI
|
||||
/// </summary>
|
||||
private bool isResizePending;
|
||||
|
||||
/// <summary>
|
||||
/// The backing store for <see cref="ActualWidth"/> for the render thread.
|
||||
/// </summary>
|
||||
private double width;
|
||||
|
||||
/// <summary>
|
||||
/// The backing store for <see cref="ActualHeight"/> for the render thread.
|
||||
/// </summary>
|
||||
private double height;
|
||||
|
||||
/// <summary>
|
||||
/// The backing store for <see cref="CompositionScaleX"/> for the render thread.
|
||||
/// </summary>
|
||||
private float compositionScaleX;
|
||||
|
||||
/// <summary>
|
||||
/// The backing store for <see cref="CompositionScaleY"/> for the render thread.
|
||||
/// </summary>
|
||||
private float compositionScaleY;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the current application.
|
||||
/// </summary>
|
||||
/// <param name="hwnd">The handle for the window.</param>
|
||||
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<ID3D12Resource>(), (void**)d3D12Resource0);
|
||||
_ = dxgiSwapChain1.Get()->GetBuffer(1, FX.__uuidof<ID3D12Resource>(), (void**)d3D12Resource1);
|
||||
}
|
||||
_ = dxgiSwapChain1.Get()->GetBuffer(0, FX.__uuidof<ID3D12Resource>(), (void**)d3D12Resource0);
|
||||
_ = dxgiSwapChain1.Get()->GetBuffer(1, FX.__uuidof<ID3D12Resource>(), (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<ID3D12Resource> d3D12Resource = default)
|
||||
{
|
||||
_ = dxgiSwapChain1.Get()->GetBuffer(0, FX.__uuidof<ID3D12Resource>(), (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<ID3D12Resource>(), (void**)d3D12Resource0);
|
||||
_ = dxgiSwapChain1.Get()->GetBuffer(1, FX.__uuidof<ID3D12Resource>(), (void**)d3D12Resource1);
|
||||
}
|
||||
}
|
||||
|
||||
// Get the index of the initial back buffer
|
||||
using (ComPtr<IDXGISwapChain3> dxgiSwapChain3 = default)
|
||||
|
||||
@@ -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
|
||||
/// </summary>
|
||||
public sealed unsafe partial class ComputeShaderPanel : SwapChainPanel
|
||||
{
|
||||
private readonly ISwapChainPanelNative* swapChainPanelNative;
|
||||
|
||||
private Thread? renderThread;
|
||||
|
||||
/// <summary>
|
||||
@@ -25,24 +22,39 @@ namespace ComputeSharp.WinUI
|
||||
/// </summary>
|
||||
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<IXamlRootNative>().HostWindow;
|
||||
this.width = ActualWidth;
|
||||
this.height = ActualHeight;
|
||||
this.compositionScaleX = CompositionScaleX;
|
||||
this.compositionScaleY = CompositionScaleY;
|
||||
this.resolutionScale = ResolutionScale;
|
||||
|
||||
OnInitialize(hwnd);
|
||||
OnResize();
|
||||
OnInitialize();
|
||||
|
||||
using ComPtr<IDXGISwapChain> 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<ISwapChainPanelNative> 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> 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();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a pointer to the underlying <see cref="ISwapChainPanelNative"/> object.
|
||||
/// </summary>
|
||||
/// <returns>The <see cref="ISwapChainPanelNative"/> pointer for the object in use.</returns>
|
||||
[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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// An interface for the COM object behind <see cref="XamlRoot"/>.
|
||||
/// </summary>
|
||||
[ComImport]
|
||||
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
|
||||
[Guid("4CCD7521-9C08-41AD-A5BD-B263EF64C9E7")]
|
||||
private interface IXamlRootNative
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the handle for the host window containing the current instance.
|
||||
/// </summary>
|
||||
IntPtr HostWindow { get; }
|
||||
OnResize();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,3 +3,4 @@
|
||||
[module: SkipLocalsInit]
|
||||
|
||||
[assembly: InternalsVisibleTo("ComputeSharp.Tests.Internals")]
|
||||
[assembly: InternalsVisibleTo("ComputeSharp.WinUI")]
|
||||
新しいイシューから参照
ユーザーをブロックする