diff --git a/src/ComputeSharp.UI/Controls/AnimatedComputeShaderPanel.Properties.cs b/src/ComputeSharp.UI/Controls/AnimatedComputeShaderPanel.Properties.cs index 6e5bc36e..912de608 100644 --- a/src/ComputeSharp.UI/Controls/AnimatedComputeShaderPanel.Properties.cs +++ b/src/ComputeSharp.UI/Controls/AnimatedComputeShaderPanel.Properties.cs @@ -135,6 +135,34 @@ partial class AnimatedComputeShaderPanel @this.swapChainManager.QueueDynamicResolutionModeChange(isDynamicResolutionEnabled); } + /// + /// Gets or sets whether vertical sync is enabled. + /// The default value for this property is . + /// + public bool IsVerticalSyncEnabled + { + get => (bool)GetValue(IsVerticalSyncEnabledProperty); + set => SetValue(IsVerticalSyncEnabledProperty, value); + } + + /// + /// The backing . + /// + public static readonly DependencyProperty IsVerticalSyncEnabledProperty = DependencyProperty.Register( + nameof(IsVerticalSyncEnabled), + typeof(bool), + typeof(AnimatedComputeShaderPanel), + new PropertyMetadata(true, OnIsVerticalSyncEnabledPropertyChanged)); + + /// + private static void OnIsVerticalSyncEnabledPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + var @this = (AnimatedComputeShaderPanel)d; + var isVerticalSyncEnabled = (bool)e.NewValue; + + @this.swapChainManager.QueueVerticalSyncModeChange(isVerticalSyncEnabled); + } + /// /// Gets or sets whether or not the rendering is paused. /// diff --git a/src/ComputeSharp.UI/Controls/AnimatedComputeShaderPanel.cs b/src/ComputeSharp.UI/Controls/AnimatedComputeShaderPanel.cs index 27b77bc1..7fb592ae 100644 --- a/src/ComputeSharp.UI/Controls/AnimatedComputeShaderPanel.cs +++ b/src/ComputeSharp.UI/Controls/AnimatedComputeShaderPanel.cs @@ -50,6 +50,7 @@ public sealed partial class AnimatedComputeShaderPanel : SwapChainPanel this.swapChainManager.QueueCompositionScaleChange(CompositionScaleX, CompositionScaleY); this.swapChainManager.QueueResolutionScaleChange(ResolutionScale); this.swapChainManager.QueueDynamicResolutionModeChange(IsDynamicResolutionEnabled); + this.swapChainManager.QueueVerticalSyncModeChange(IsVerticalSyncEnabled); if (!IsPaused && ShaderRunner is IShaderRunner shaderRunner) diff --git a/src/ComputeSharp.UI/Controls/ComputeShaderPanel.Properties.cs b/src/ComputeSharp.UI/Controls/ComputeShaderPanel.Properties.cs index 7e351bc0..7c76dc3c 100644 --- a/src/ComputeSharp.UI/Controls/ComputeShaderPanel.Properties.cs +++ b/src/ComputeSharp.UI/Controls/ComputeShaderPanel.Properties.cs @@ -169,4 +169,32 @@ partial class ComputeShaderPanel @this.swapChainManager.QueueDynamicResolutionModeChange(isDynamicResolutionEnabled); } + + /// + /// Gets or sets whether vertical sync is enabled. + /// The default value for this property is . + /// + public bool IsVerticalSyncEnabled + { + get => (bool)GetValue(IsVerticalSyncEnabledProperty); + set => SetValue(IsVerticalSyncEnabledProperty, value); + } + + /// + /// The backing . + /// + public static readonly DependencyProperty IsVerticalSyncEnabledProperty = DependencyProperty.Register( + nameof(IsVerticalSyncEnabled), + typeof(bool), + typeof(ComputeShaderPanel), + new PropertyMetadata(true, OnIsVerticalSyncEnabledPropertyChanged)); + + /// + private static void OnIsVerticalSyncEnabledPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + var @this = (ComputeShaderPanel)d; + var isVerticalSyncEnabled = (bool)e.NewValue; + + @this.swapChainManager.QueueVerticalSyncModeChange(isVerticalSyncEnabled); + } } diff --git a/src/ComputeSharp.UI/Controls/ComputeShaderPanel.cs b/src/ComputeSharp.UI/Controls/ComputeShaderPanel.cs index 8750e3dc..560f1950 100644 --- a/src/ComputeSharp.UI/Controls/ComputeShaderPanel.cs +++ b/src/ComputeSharp.UI/Controls/ComputeShaderPanel.cs @@ -50,6 +50,7 @@ public sealed partial class ComputeShaderPanel : SwapChainPanel this.swapChainManager.QueueCompositionScaleChange(CompositionScaleX, CompositionScaleY); this.swapChainManager.QueueResolutionScaleChange(ResolutionScale); this.swapChainManager.QueueDynamicResolutionModeChange(IsDynamicResolutionEnabled); + this.swapChainManager.QueueVerticalSyncModeChange(IsVerticalSyncEnabled); if (FrameRequestQueue is IFrameRequestQueue frameRequestQueue && ShaderRunner is IShaderRunner shaderRunner) diff --git a/src/ComputeSharp.UI/Helpers/SwapChainManager.Rendering.cs b/src/ComputeSharp.UI/Helpers/SwapChainManager.Rendering.cs index e99c28d4..ae1a25f6 100644 --- a/src/ComputeSharp.UI/Helpers/SwapChainManager.Rendering.cs +++ b/src/ComputeSharp.UI/Helpers/SwapChainManager.Rendering.cs @@ -97,6 +97,17 @@ partial class SwapChainManager } } + /// + /// Queues a change in the vertical sync mode. + /// + /// Whether or not to use vertical sync. + public void QueueVerticalSyncModeChange(bool isVerticalSyncEnabled) + { + ThrowIfDisposed(); + + this.syncInterval = isVerticalSyncEnabled ? 1u : 0u; + } + /// /// Queues a resize operation. /// diff --git a/src/ComputeSharp.UI/Helpers/SwapChainManager.cs b/src/ComputeSharp.UI/Helpers/SwapChainManager.cs index 4021ed1f..c50db485 100644 --- a/src/ComputeSharp.UI/Helpers/SwapChainManager.cs +++ b/src/ComputeSharp.UI/Helpers/SwapChainManager.cs @@ -175,6 +175,11 @@ internal sealed unsafe partial class SwapChainManager : NativeObject /// private volatile bool isDynamicResolutionEnabled; + /// + /// The flags to enable or disable vertical sync. + /// + private volatile uint syncInterval; + /// /// The instance tracking time since the first rendered frame. /// @@ -388,7 +393,7 @@ internal sealed unsafe partial class SwapChainManager : NativeObject private unsafe partial void OnPresent() { // Wait for the swap chain to be ready for presenting - _ = Win32.WaitForSingleObjectEx(frameLatencyWaitableObject, 1000, true); + _ = Win32.WaitForSingleObjectEx(frameLatencyWaitableObject, Win32.INFINITE, true); using ComPtr d3D12Resource = default; @@ -455,7 +460,7 @@ internal sealed unsafe partial class SwapChainManager : NativeObject this.d3D12CommandQueue.Get()->Signal(this.d3D12Fence.Get(), updatedFenceValue).Assert(); // Present the new frame - this.dxgiSwapChain3.Get()->Present(0, 0).Assert(); + this.dxgiSwapChain3.Get()->Present(SyncInterval: this.syncInterval, 0).Assert(); if (updatedFenceValue > this.d3D12Fence.Get()->GetCompletedValue()) {