From f0dbf5ce59818aee94245b6c4c90929c73cf5bdf Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 30 Apr 2021 14:29:34 +0200 Subject: [PATCH] Added ComputeShaderPanel.ResolutionScale property --- .../ComputeShaderPanel.Properties.cs | 35 +++++++++++- .../ComputeShaderPanel.Rendering.cs | 53 ++++++++++++++++--- 2 files changed, 79 insertions(+), 9 deletions(-) diff --git a/src/ComputeSharp.WinUI/ComputeShaderPanel.Properties.cs b/src/ComputeSharp.WinUI/ComputeShaderPanel.Properties.cs index 66bd8ccf..7329f7fb 100644 --- a/src/ComputeSharp.WinUI/ComputeShaderPanel.Properties.cs +++ b/src/ComputeSharp.WinUI/ComputeShaderPanel.Properties.cs @@ -1,4 +1,5 @@ -using Microsoft.UI.Xaml; +using Microsoft.Toolkit.Diagnostics; +using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; namespace ComputeSharp.WinUI @@ -23,5 +24,37 @@ namespace ComputeSharp.WinUI typeof(IShaderRunner), typeof(ComputeShaderPanel), new PropertyMetadata(null, static (d, e) => ((ComputeShaderPanel)d).shaderRunner = (IShaderRunner?)e.NewValue)); + + /// + /// Gets or sets the resolution scale to be used to render frames. + /// This can help achieve higher framerates with more complex shaders. + /// + /// The accepted range is [0.1 and 1] (default is 1). + public double ResolutionScale + { + get => (double)GetValue(ShaderFactoryProperty); + set => SetValue(ShaderFactoryProperty, value); + } + + /// + /// The backing . + /// + public static readonly DependencyProperty ResolutionScaleProperty = DependencyProperty.Register( + nameof(ResolutionScale), + typeof(double), + typeof(ComputeShaderPanel), + new PropertyMetadata(1.0, static (d, e) => ((ComputeShaderPanel)d).shaderRunner = (IShaderRunner?)e.NewValue)); + + /// + private static void OnResolutionScalePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + var @this = (ComputeShaderPanel)d; + var resolutionScale = (double)e.NewValue; + + Guard.IsBetweenOrEqualTo(resolutionScale, 0.1, 1.0, nameof(resolutionScale)); + + @this.resolutionScale = resolutionScale; + @this.OnResize(); + } } } diff --git a/src/ComputeSharp.WinUI/ComputeShaderPanel.Rendering.cs b/src/ComputeSharp.WinUI/ComputeShaderPanel.Rendering.cs index d995783d..aedcd6b0 100644 --- a/src/ComputeSharp.WinUI/ComputeShaderPanel.Rendering.cs +++ b/src/ComputeSharp.WinUI/ComputeShaderPanel.Rendering.cs @@ -14,6 +14,11 @@ namespace ComputeSharp.WinUI /// private IShaderRunner? shaderRunner; + /// + /// The resolution scale used to render frames. + /// + private double resolutionScale; + /// /// The pointer for the device currently in use. /// @@ -191,6 +196,46 @@ namespace ComputeSharp.WinUI // Resize the swap chain buffers this.dxgiSwapChain1.Get()->ResizeBuffers(0, 0, 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) + { + _ = 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) { @@ -199,14 +244,6 @@ namespace ComputeSharp.WinUI 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();