Fix computation of compositor refresh rate

Cannot use DwmGetCompositionTimingInfo as it fails WACK
このコミットが含まれているのは:
Sergio Pedri committed 2022-01-29 17:11:58 +01:00
1 親 ccc491d0e6
コミット 276f9da199
12ファイル変更
+199 -21

検索条件に一致するファイルはありません

+1
ファイルの表示
@@ -13,6 +13,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Controls\AnimatedComputeShaderPanel.Properties.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\ComputeShaderPanel.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\ComputeShaderPanel.Properties.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\IDXGIAdapterExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\SemaphoreSlimExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\SwapChainManager.Rendering.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\SwapChainManager.cs" />
+55
ファイルの表示
@@ -0,0 +1,55 @@
using System;
using ComputeSharp.Core.Extensions;
using TerraFX.Interop.DirectX;
using TerraFX.Interop.Windows;
using Win32 = TerraFX.Interop.Windows.Windows;
#if WINDOWS_UWP
namespace ComputeSharp.Uwp.Extensions;
#else
namespace ComputeSharp.WinUI.Extensions;
#endif
/// <summary>
/// A <see langword="class"/> with extensions for the <see cref="IDXGIAdapter"/> type.
/// </summary>
internal static unsafe class IDXGIAdapterExtensions
{
/// <summary>
/// Calculates the refresh rate for a given adapter.
/// </summary>
/// <param name="dxgiAdapter">The input adapter to query.</param>
/// <param name="dxgiFormat">The <see cref="DXGI_FORMAT"/> in use to render content.</param>
/// <returns>The current composition refresh rate.</returns>
public static int GetCompositionRefreshRate(this ref IDXGIAdapter dxgiAdapter, DXGI_FORMAT dxgiFormat)
{
using ComPtr<IDXGIAdapter1> dxgiAdapter1 = default;
dxgiAdapter.QueryInterface(Win32.__uuidof<IDXGIAdapter1>(), dxgiAdapter1.GetVoidAddressOf()).Assert();
using ComPtr<IDXGIOutput> dxgiOutput = default;
dxgiAdapter1.Get()->EnumOutputs(0, dxgiOutput.GetAddressOf()).Assert();
using ComPtr<IDXGIOutput1> dxgiOutput1 = default;
dxgiOutput.CopyTo(dxgiOutput1.GetAddressOf()).Assert();
DXGI_MODE_DESC1 dxgiModeDescIn = default;
dxgiModeDescIn.Format = dxgiFormat;
DXGI_MODE_DESC1 dxgiModeDescOut = default;
dxgiOutput1.Get()->FindClosestMatchingMode1(&dxgiModeDescIn, &dxgiModeDescOut, null).Assert();
// Calculates the best target framerate based on monitor refresh rate. Even with v-sync disabled, the target framerate
// should always match the refresh rate in use, instead of being capped to 60fps (as users could have a 120Hz screen
// and disabled v-sync). Here the refresh rate is retrieved, then the target framerate is calculated as being the
// rounding up of this value to the next integer, and then clamped between 30 and 360fps.
double refreshRateInHz = dxgiModeDescOut.RefreshRate.Numerator / (double)dxgiModeDescOut.RefreshRate.Denominator;
int refreshRate = Math.Clamp((int)Math.Ceiling(refreshRateInHz), 30, 360);
return refreshRate;
}
}
+2 -18
ファイルの表示
@@ -1,9 +1,6 @@
using System;
using System.Runtime.CompilerServices;
using System.Threading;
using ComputeSharp.Core.Extensions;
using TerraFX.Interop.Windows;
using Win32 = TerraFX.Interop.Windows.Windows;
#if WINDOWS_UWP
namespace ComputeSharp.Uwp.Helpers;
@@ -69,22 +66,9 @@ internal unsafe ref struct DynamicResolutionManager
/// <summary>
/// Initializes a new <see cref="DynamicResolutionManager"/> instance.
/// </summary>
public DynamicResolutionManager()
/// <param name="targetFramerate">The target framerate to use.</param>
public DynamicResolutionManager(int targetFramerate)
{
DWM_TIMING_INFO dwmTimingInfo = default;
dwmTimingInfo.cbSize = (uint)sizeof(DWM_TIMING_INFO);
// Get the composition timing info (the refresh rate depends on the compositor)
Win32.DwmGetCompositionTimingInfo(HWND.NULL, &dwmTimingInfo).Assert();
// Calculates the best target framerate based on monitor refresh rate. Even with v-sync disabled,
// the target framerate should always match the refresh rate in use, instead of being capped to
// 60fps (as users could have a 120Hz screen and disabled v-sync). Here the refresh rate is retrieved,
// then the target framerate is calculated as being the rounding of this value to the next integer,
// then rounded up to the next multiple of 30, and then clamped between 30 and 240fps.
double refreshRateInHz = dwmTimingInfo.rateRefresh.uiNumerator / (double)dwmTimingInfo.rateRefresh.uiDenominator;
int targetFramerate = Math.Clamp((((int)Math.Ceiling(refreshRateInHz) + 29) / 30) * 30, 30, 240);
long targetFrameTimeInTicks = TimeSpan.FromSeconds(1).Ticks / targetFramerate;
long upperFrameTimeThresholdInTicks = TimeSpan.FromSeconds(1).Ticks / (targetFramerate - 2);
long lowerFrameTimeThresholdInTicks = TimeSpan.FromSeconds(1).Ticks / (targetFramerate + 2);
+10 -3
ファイルの表示
@@ -254,16 +254,23 @@ partial class SwapChainManager<TOwner>
}
}
/// <summary>
/// Gets the <see cref="DynamicResolutionManager"/> instance to use in <see cref="RenderLoopWithDynamicResolution"/>.
/// </summary>
/// <param name="resolutionManager">The <see cref="DynamicResolutionManager"/> instance to use.</param>
private unsafe partial void OnGetDynamicResolutionManager(out DynamicResolutionManager resolutionManager);
/// <summary>
/// The core render loop with dynamic resolution.
/// </summary>
[MethodImpl(MethodImplOptions.NoInlining)]
private void RenderLoopWithDynamicResolution()
private unsafe void RenderLoopWithDynamicResolution()
{
Stopwatch renderStopwatch = this.renderStopwatch ??= new();
Stopwatch frameStopwatch = new();
CancellationToken cancellationToken = this.renderCancellationTokenSource!.Token;
DynamicResolutionManager frameTimeWatcher = new();
OnGetDynamicResolutionManager(out DynamicResolutionManager resolutionManager);
if (!OnFrameRequest(out object? parameter, cancellationToken))
{
@@ -308,7 +315,7 @@ partial class SwapChainManager<TOwner>
OnPresent();
// Evaluate the dynamic resolution frame time step
if (frameTimeWatcher.Advance(frameStopwatch.ElapsedTicks, ref this.dynamicResolutionScale))
if (resolutionManager.Advance(frameStopwatch.ElapsedTicks, ref this.dynamicResolutionScale))
{
this.isResizePending = true;
}
+13
ファイルの表示
@@ -4,6 +4,11 @@ using System.Threading;
using ComputeSharp.Core.Extensions;
using ComputeSharp.Graphics.Helpers;
using ComputeSharp.Interop;
#if WINDOWS_UWP
using ComputeSharp.Uwp.Extensions;
#else
using ComputeSharp.WinUI.Extensions;
#endif
#if !WINDOWS_UWP
using Microsoft.UI.Dispatching;
using Microsoft.UI.Xaml.Controls;
@@ -471,6 +476,14 @@ internal sealed unsafe partial class SwapChainManager<TOwner> : NativeObject
}
}
/// <inheritdoc/>
private unsafe partial void OnGetDynamicResolutionManager(out DynamicResolutionManager resolutionManager)
{
int targetFramerate = GraphicsDevice.Default.DXGIAdapter->GetCompositionRefreshRate(DXGI_FORMAT.DXGI_FORMAT_R8G8B8A8_UNORM);
resolutionManager = new DynamicResolutionManager(targetFramerate);
}
/// <inheritdoc/>
protected override void OnDispose()
{
+12
ファイルの表示
@@ -27,6 +27,11 @@ public sealed unsafe partial class GraphicsDevice : NativeObject
/// </summary>
private ComPtr<ID3D12Device> d3D12Device;
/// <summary>
/// The <see cref="IDXGIAdapter"/> that <see cref="d3D12Device"/> was created from.
/// </summary>
private ComPtr<IDXGIAdapter> dxgiAdapter;
/// <summary>
/// The <see cref="ID3D12CommandQueue"/> instance to use for compute operations.
/// </summary>
@@ -93,6 +98,7 @@ public sealed unsafe partial class GraphicsDevice : NativeObject
internal GraphicsDevice(ID3D12Device* d3D12Device, IDXGIAdapter* dxgiAdapter, DXGI_ADAPTER_DESC1* dxgiDescription1)
{
this.d3D12Device = new ComPtr<ID3D12Device>(d3D12Device);
this.dxgiAdapter = new ComPtr<IDXGIAdapter>(dxgiAdapter);
this.d3D12ComputeCommandQueue = d3D12Device->CreateCommandQueue(D3D12_COMMAND_LIST_TYPE_COMPUTE);
this.d3D12CopyCommandQueue = d3D12Device->CreateCommandQueue(D3D12_COMMAND_LIST_TYPE_COPY);
@@ -169,6 +175,11 @@ public sealed unsafe partial class GraphicsDevice : NativeObject
/// </summary>
internal ID3D12Device* D3D12Device => this.d3D12Device;
/// <summary>
/// Gets the underlying <see cref="IDXGIAdapter"/> wrapped by the current instance.
/// </summary>
internal IDXGIAdapter* DXGIAdapter => this.dxgiAdapter;
#if NET6_0_OR_GREATER
/// <summary>
/// Gets the underlying <see cref="D3D12MA_Allocator"/> wrapped by the current instance.
@@ -352,6 +363,7 @@ public sealed unsafe partial class GraphicsDevice : NativeObject
DeviceHelper.NotifyDisposedDevice(this);
this.d3D12Device.Dispose();
this.dxgiAdapter.Dispose();
this.d3D12ComputeCommandQueue.Dispose();
this.d3D12CopyCommandQueue.Dispose();
this.d3D12ComputeFence.Dispose();
+27
ファイルの表示
@@ -0,0 +1,27 @@
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.
// Ported from shared/dxgi1_2.h in the Windows SDK for Windows 10.0.22000.0
// Original source is Copyright © Microsoft. All rights reserved.
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using TerraFX.Interop.Windows;
namespace TerraFX.Interop.DirectX
{
[Guid("00CDDEA8-939B-4B83-A340-A685226666CC")]
[NativeTypeName("struct IDXGIOutput1 : IDXGIOutput")]
[NativeInheritance("IDXGIOutput")]
internal unsafe partial struct IDXGIOutput1
{
public void** lpVtbl;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[VtblIndex(20)]
public HRESULT FindClosestMatchingMode1([NativeTypeName("const DXGI_MODE_DESC1 *")] DXGI_MODE_DESC1* pModeToMatch, DXGI_MODE_DESC1* pClosestMatch, IUnknown* pConcernedDevice)
{
return ((delegate* unmanaged[Stdcall]<IDXGIOutput1*, DXGI_MODE_DESC1*, DXGI_MODE_DESC1*, IUnknown*, int>)(lpVtbl[20]))((IDXGIOutput1*)Unsafe.AsPointer(ref this), pModeToMatch, pClosestMatch, pConcernedDevice);
}
}
}
+26
ファイルの表示
@@ -0,0 +1,26 @@
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.
// Ported from shared/dxgi1_2.h in the Windows SDK for Windows 10.0.22000.0
// Original source is Copyright © Microsoft. All rights reserved.
using TerraFX.Interop.Windows;
namespace TerraFX.Interop.DirectX
{
internal partial struct DXGI_MODE_DESC1
{
public uint Width;
public uint Height;
public DXGI_RATIONAL RefreshRate;
public DXGI_FORMAT Format;
public DXGI_MODE_SCANLINE_ORDER ScanlineOrdering;
public DXGI_MODE_SCALING Scaling;
public BOOL Stereo;
}
}
+14
ファイルの表示
@@ -0,0 +1,14 @@
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.
// Ported from shared/dxgicommon.h in the Windows SDK for Windows 10.0.22000.0
// Original source is Copyright © Microsoft. All rights reserved.
namespace TerraFX.Interop.DirectX
{
internal partial struct DXGI_RATIONAL
{
public uint Numerator;
public uint Denominator;
}
}
+16
ファイルの表示
@@ -0,0 +1,16 @@
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.
// Ported from shared/dxgitype.h in the Windows SDK for Windows 10.0.22000.0
// Original source is Copyright © Microsoft. All rights reserved.
namespace TerraFX.Interop.DirectX
{
internal enum DXGI_MODE_SCALING
{
DXGI_MODE_SCALING_UNSPECIFIED = 0,
DXGI_MODE_SCALING_CENTERED = 1,
DXGI_MODE_SCALING_STRETCHED = 2,
}
}
+18
ファイルの表示
@@ -0,0 +1,18 @@
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.
// Ported from shared/dxgitype.h in the Windows SDK for Windows 10.0.22000.0
// Original source is Copyright © Microsoft. All rights reserved.
namespace TerraFX.Interop.DirectX
{
internal enum DXGI_MODE_SCANLINE_ORDER
{
DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED = 0,
DXGI_MODE_SCANLINE_ORDER_PROGRESSIVE = 1,
DXGI_MODE_SCANLINE_ORDER_UPPER_FIELD_FIRST = 2,
DXGI_MODE_SCANLINE_ORDER_LOWER_FIELD_FIRST = 3,
}
}
+5
ファイルの表示
@@ -25,7 +25,9 @@
<Compile Include="$(MSBuildThisFileDirectory)DirectX\other\d3dx12\D3D12_STATIC_SAMPLER_DESC.Manual.cs" />
<Compile Include="$(MSBuildThisFileDirectory)DirectX\other\d3dx12\D3D12_TEXTURE_COPY_LOCATION.Manual.cs" />
<Compile Include="$(MSBuildThisFileDirectory)DirectX\other\d3dx12\D3D12_VERSIONED_ROOT_SIGNATURE_DESC.Manual.cs" />
<Compile Include="$(MSBuildThisFileDirectory)DirectX\shared\dxgi1_2\DXGIOutput1.cs" />
<Compile Include="$(MSBuildThisFileDirectory)DirectX\shared\dxgi1_2\DXGI_ALPHA_MODE.cs" />
<Compile Include="$(MSBuildThisFileDirectory)DirectX\shared\dxgi1_2\DXGI_MODE_DESC1.cs" />
<Compile Include="$(MSBuildThisFileDirectory)DirectX\shared\dxgi1_2\DXGI_SCALING.cs" />
<Compile Include="$(MSBuildThisFileDirectory)DirectX\shared\dxgi1_2\DXGI_SWAP_CHAIN_DESC1.cs" />
<Compile Include="$(MSBuildThisFileDirectory)DirectX\shared\dxgi1_2\IDXGIFactory2.cs" />
@@ -38,9 +40,12 @@
<Compile Include="$(MSBuildThisFileDirectory)DirectX\shared\dxgi1_5\DXGI_FEATURE.cs" />
<Compile Include="$(MSBuildThisFileDirectory)DirectX\shared\dxgi1_6\DXGI_GPU_PREFERENCE.cs" />
<Compile Include="$(MSBuildThisFileDirectory)DirectX\shared\dxgi1_6\IDXGIFactory6.cs" />
<Compile Include="$(MSBuildThisFileDirectory)DirectX\shared\dxgicommon\DXGI_RATIONAL.cs" />
<Compile Include="$(MSBuildThisFileDirectory)DirectX\shared\dxgicommon\DXGI_SAMPLE_DESC.cs" />
<Compile Include="$(MSBuildThisFileDirectory)DirectX\shared\dxgicommon\DXGI_SAMPLE_DESC.Manual.cs" />
<Compile Include="$(MSBuildThisFileDirectory)DirectX\shared\dxgiformat\DXGI_FORMAT.cs" />
<Compile Include="$(MSBuildThisFileDirectory)DirectX\shared\dxgitype\DXGI_MODE_SCALING.cs" />
<Compile Include="$(MSBuildThisFileDirectory)DirectX\shared\dxgitype\DXGI_MODE_SCANLINE_ORDER.cs" />
<Compile Include="$(MSBuildThisFileDirectory)DirectX\shared\dxgi\DXGI.cs" />
<Compile Include="$(MSBuildThisFileDirectory)DirectX\shared\dxgi\DXGI_ADAPTER_DESC.cs" />
<Compile Include="$(MSBuildThisFileDirectory)DirectX\shared\dxgi\DXGI_ADAPTER_DESC1.cs" />