Enabled toggle for dynamic resolution mode

このコミットが含まれているのは:
Sergio Pedri committed 2021-11-20 00:28:07 +01:00
1 親 57866456af
コミット 227a1ffcc2
6ファイル変更
+115 -31

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

@@ -18,6 +18,7 @@ namespace ComputeSharp.SwapChain.WinUI.ViewModels
/// </summary>
public MainViewModel()
{
this.isDynamicResolutionEnabled = true;
this.selectedResolutionScale = 100;
this.selectedComputeShader = ComputeShaderOptions[0];
this.selectedComputeShader.IsSelected = true;
@@ -31,6 +32,17 @@ namespace ComputeSharp.SwapChain.WinUI.ViewModels
/// </summary>
public IList<int> ResolutionScaleOptions { get; } = new[] { 25, 50, 75, 100 };
private bool isDynamicResolutionEnabled;
/// <summary>
/// Gets or sets whether the dynamic resolution is enabled.
/// </summary>
public bool IsDynamicResolutionEnabled
{
get => this.isDynamicResolutionEnabled;
set => SetProperty(ref this.isDynamicResolutionEnabled, value);
}
private int selectedResolutionScale;
/// <summary>
@@ -134,6 +134,7 @@
x:Name="ShaderPanel"
Grid.Row="1"
ShaderRunner="{x:Bind ViewModel.SelectedComputeShader.ShaderRunner, Mode=OneWay}"
IsDynamicResolutionEnabled="{x:Bind ViewModel.IsDynamicResolutionEnabled, Mode=OneWay}"
ResolutionScale="{x:Bind converters:ResolutionScaleConverter.ConvertPercentageToScale(ViewModel.SelectedResolutionScale), Mode=OneWay}"
IsPaused="{x:Bind ViewModel.IsRenderingPaused, Mode=OneWay}"/>
@@ -171,6 +172,10 @@
<SymbolIcon Symbol="Setting"/>
<Button.Flyout>
<MenuFlyout>
<ToggleMenuFlyoutItem
Text="Dynamic resolution"
IsChecked="{x:Bind ViewModel.IsDynamicResolutionEnabled, Mode=TwoWay}"/>
<MenuFlyoutSeparator/>
<ToggleMenuFlyoutItem
Text="{x:Bind converters:ResolutionScaleConverter.ConvertPercentageToFormattedString(ViewModel.ResolutionScaleOptions[0])}"
Command="{x:Bind ViewModel.SetResolutionScaleCommand}"
+35 -2
ファイルの表示
@@ -50,8 +50,41 @@ namespace ComputeSharp.WinUI
var @this = (ComputeShaderPanel)d;
var resolutionScale = (double)e.NewValue;
@this.resolutionScale = resolutionScale;
@this.OnResize();
@this.targetResolutionScale = (float)resolutionScale;
if (!@this.isDynamicResolutionEnabled)
{
@this.OnResize();
}
}
/// <summary>
/// Gets or sets whether dynamic resolution is enabled. If it is, the internal render
/// resolution of the shader being run will be automatically adjusted to reach 60fps.
/// <para>The default value for this property is <see langword="true"/>.</para>
/// </summary>
public bool IsDynamicResolutionEnabled
{
get => (bool)GetValue(IsDynamicResolutionEnabledProperty);
set => SetValue(IsDynamicResolutionEnabledProperty, value);
}
/// <summary>
/// The <see cref="DependencyProperty"/> backing <see cref="IsDynamicResolutionEnabled"/>.
/// </summary>
public static readonly DependencyProperty IsDynamicResolutionEnabledProperty = DependencyProperty.Register(
nameof(IsDynamicResolutionEnabled),
typeof(bool),
typeof(ComputeShaderPanel),
new PropertyMetadata(true, OnIsDynamicResolutionEnabledPropertyChanged));
/// <inheritdoc cref="DependencyPropertyChangedCallback"/>
private static void OnIsDynamicResolutionEnabledPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var @this = (ComputeShaderPanel)d;
var isDynamicResolutionEnabled = (bool)e.NewValue;
@this.isDynamicResolutionEnabled = isDynamicResolutionEnabled;
}
/// <summary>
+47 -16
ファイルの表示
@@ -8,6 +8,8 @@ using TerraFX.Interop;
using WinRT;
using FX = TerraFX.Interop.Windows;
#pragma warning disable CS0420
namespace ComputeSharp.WinUI
{
/// <inheritdoc cref="ComputeShaderPanel"/>
@@ -86,32 +88,42 @@ namespace ComputeSharp.WinUI
/// <summary>
/// Whether or not the window has been resized and requires the buffers to be updated.
/// </summary>
private bool isResizePending;
private volatile bool isResizePending;
/// <summary>
/// The backing store for <see cref="ActualWidth"/> for the render thread.
/// </summary>
private double width;
private volatile float width;
/// <summary>
/// The backing store for <see cref="ActualHeight"/> for the render thread.
/// </summary>
private double height;
private volatile float height;
/// <summary>
/// The backing store for <see cref="CompositionScaleX"/> for the render thread.
/// </summary>
private float compositionScaleX;
private volatile float compositionScaleX;
/// <summary>
/// The backing store for <see cref="CompositionScaleY"/> for the render thread.
/// </summary>
private float compositionScaleY;
private volatile float compositionScaleY;
/// <summary>
/// The resolution scale used to render frames.
/// The resolution scale used to render frames when explicitly controlled by the user.
/// </summary>
private double resolutionScale;
private volatile float resolutionScale;
/// <summary>
/// The resolution scale used to render frames, in either render mode.
/// </summary>
private volatile float targetResolutionScale;
/// <summary>
/// Whether dynamic resolution is currently enabled.
/// </summary>
private volatile bool isDynamicResolutionEnabled;
/// <summary>
/// Indicates whether or not the rendering has been canceled.
@@ -181,8 +193,8 @@ namespace ComputeSharp.WinUI
dxgiSwapChainDesc1.BufferCount = 2;
dxgiSwapChainDesc1.Flags = (uint)DXGI_SWAP_CHAIN_FLAG.DXGI_SWAP_CHAIN_FLAG_FRAME_LATENCY_WAITABLE_OBJECT;
dxgiSwapChainDesc1.Format = DXGI_FORMAT.DXGI_FORMAT_R8G8B8A8_UNORM;
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.Width = (uint)Math.Max(Math.Ceiling(this.width * this.compositionScaleX * this.targetResolutionScale), 1.0);
dxgiSwapChainDesc1.Height = (uint)Math.Max(Math.Ceiling(this.height * this.compositionScaleY * this.targetResolutionScale), 1.0);
dxgiSwapChainDesc1.SampleDesc = new DXGI_SAMPLE_DESC(count: 1, quality: 0);
dxgiSwapChainDesc1.Scaling = DXGI_SCALING.DXGI_SCALING_STRETCH;
dxgiSwapChainDesc1.Stereo = 0;
@@ -277,15 +289,15 @@ namespace ComputeSharp.WinUI
// Resize the swap chain buffers
this.dxgiSwapChain2.Get()->ResizeBuffers(
2,
(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),
(uint)Math.Max(Math.Ceiling(this.width * this.compositionScaleX * this.targetResolutionScale), 1.0),
(uint)Math.Max(Math.Ceiling(this.height * this.compositionScaleY * this.targetResolutionScale), 1.0),
DXGI_FORMAT.DXGI_FORMAT_UNKNOWN,
(uint)DXGI_SWAP_CHAIN_FLAG.DXGI_SWAP_CHAIN_FLAG_FRAME_LATENCY_WAITABLE_OBJECT).Assert();
// Apply the necessary scale transform
DXGI_MATRIX_3X2_F transformMatrix = default;
transformMatrix._11 = (float)((1 / this.compositionScaleX) * (1 / this.resolutionScale));
transformMatrix._22 = (float)((1 / this.compositionScaleY) * (1 / this.resolutionScale));
transformMatrix._11 = (1 / this.compositionScaleX) * (1 / this.targetResolutionScale);
transformMatrix._22 = (1 / this.compositionScaleY) * (1 / this.targetResolutionScale);
this.dxgiSwapChain2.Get()->SetMatrixTransform(&transformMatrix).Assert();
@@ -420,25 +432,44 @@ namespace ComputeSharp.WinUI
DynamicResolutionManager.Create(out DynamicResolutionManager frameTimeWatcher);
bool isDynamicResolutionEnabled = @this.isDynamicResolutionEnabled;
// Start the initial frame separately, before the timer starts. This ensures that
// resuming after a pause correctly renders the first frame at the right time.
@this.OnUpdate(renderStopwatch.Elapsed);
renderStopwatch.Start();
const long targetFrameTimeInTicksFor60fps = 166666;
const long targetFrameTimeInTicksFor61fps = 163934;
// Main render loop, until cancellation is requested
while (!@this.isCancellationRequested)
{
_ = FX.WaitForSingleObjectEx(frameLatencyWaitableObject, 1000, 1);
if (frameTimeWatcher.Advance(frameStopwatch.ElapsedTicks, ref @this.resolutionScale))
// Update the resolution mode, if needed
if (isDynamicResolutionEnabled != @this.isDynamicResolutionEnabled)
{
isDynamicResolutionEnabled = !isDynamicResolutionEnabled;
if (isDynamicResolutionEnabled)
{
DynamicResolutionManager.Create(out frameTimeWatcher);
}
else
{
@this.targetResolutionScale = @this.resolutionScale;
}
}
// Evaluate the dynamic resolution frame time step, if the mode is enabled
if (isDynamicResolutionEnabled &&
frameTimeWatcher.Advance(frameStopwatch.ElapsedTicks, ref @this.targetResolutionScale))
{
@this.isResizePending = true;
}
while (frameStopwatch.ElapsedTicks < targetFrameTimeInTicksFor60fps)
while (frameStopwatch.ElapsedTicks < targetFrameTimeInTicksFor61fps)
{
}
+7 -5
ファイルの表示
@@ -31,11 +31,13 @@ namespace ComputeSharp.WinUI
// Initializes the swap chain and starts the render thread
private void ComputeShaderPanel_Loaded(object sender, RoutedEventArgs e)
{
this.width = ActualWidth;
this.height = ActualHeight;
this.width = (float)ActualWidth;
this.height = (float)ActualHeight;
this.compositionScaleX = CompositionScaleX;
this.compositionScaleY = CompositionScaleY;
this.resolutionScale = ResolutionScale;
this.resolutionScale = (float)ResolutionScale;
this.targetResolutionScale = 1.0f;
this.isDynamicResolutionEnabled = true;
OnInitialize();
OnStartRenderLoop();
@@ -50,8 +52,8 @@ namespace ComputeSharp.WinUI
// 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;
this.width = (float)e.NewSize.Width;
this.height = (float)e.NewSize.Height;
OnResize();
}
+9 -8
ファイルの表示
@@ -1,5 +1,6 @@
using System;
using System.Runtime.InteropServices;
using System.Threading;
namespace ComputeSharp.WinUI.Helpers
{
@@ -11,7 +12,7 @@ namespace ComputeSharp.WinUI.Helpers
/// <summary>
/// The target frame time in ticks to reach 60fps (set to 64fps).
/// </summary>
private const long TargetFrameTimeInTicksFor60fps = 156250;
public const long TargetFrameTimeInTicksFor60fps = 156250;
/// <summary>
/// The upper frame time threshold in ticks to reach 60fps.
@@ -31,12 +32,12 @@ namespace ComputeSharp.WinUI.Helpers
/// <summary>
/// The threshold for scale factor updates.
/// </summary>
private const double ScaleFactorDeltaThreshold = 0.005;
private const float ScaleFactorDeltaThreshold = 0.005f;
/// <summary>
/// The constant multiplicative factor for the scale factor.
/// </summary>
private const double ScaleFactorDeltaK = 0.8;
private const float ScaleFactorDeltaK = 0.8f;
/// <summary>
/// The window of registered frame times for previous frames.
@@ -76,7 +77,7 @@ namespace ComputeSharp.WinUI.Helpers
/// <param name="frameTimeInTicks">The frame time in ticks for the last rendered frame.</param>
/// <param name="scaleFactor">The current scale factor value being used to render frames.</param>
/// <returns>Whether or not <paramref name="scaleFactor"/> has been updated and a resize is needed.</returns>
public bool Advance(long frameTimeInTicks, ref double scaleFactor)
public bool Advance(long frameTimeInTicks, ref float scaleFactor)
{
// Ignore the current frame if a resize has just happened
if (this.frameTimeOffset == -1)
@@ -104,16 +105,16 @@ namespace ComputeSharp.WinUI.Helpers
if (averageFrameTimeInTicks is < LowerFrameTimeThresholdInTicksFor60fps or > UpperFrameTimeThresholdInTicksFor60fps)
{
double
frameTimeDelta = (TargetFrameTimeInTicksFor60fps - averageFrameTimeInTicks) / (double)averageFrameTimeInTicks,
float
frameTimeDelta = (TargetFrameTimeInTicksFor60fps - averageFrameTimeInTicks) / (float)averageFrameTimeInTicks,
scaleFactorDelta = scaleFactor * frameTimeDelta * ScaleFactorDeltaK,
updatedScaleFactor = Math.Clamp(scaleFactor + scaleFactorDelta, 0.10, 1.0);
updatedScaleFactor = Math.Clamp(scaleFactor + scaleFactorDelta, 0.10f, 1.0f);
// Apply the scale factor update if the target scale has changed enough. This helps to avoid
// too frequence resolution changes if the scale factor only changes very little every time.
if (Math.Abs(scaleFactor - updatedScaleFactor) >= ScaleFactorDeltaThreshold)
{
scaleFactor = updatedScaleFactor;
Volatile.Write(ref scaleFactor, updatedScaleFactor);
this.frameTimeOffset = -1;