using System; #if WINDOWS_UWP using ComputeSharp.Uwp.Helpers; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; #else using ComputeSharp.WinUI.Helpers; using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; #endif #if WINDOWS_UWP namespace ComputeSharp.Uwp; #else namespace ComputeSharp.WinUI; #endif /// /// /// A custom that can be used to render animated backgrounds. /// It is powered by ComputeSharp and leverages compute shaders to create frames to display. /// /// /// This panel accepts a custom instance to customize frame requests. /// /// public sealed partial class ComputeShaderPanel : SwapChainPanel, IDisposable { /// /// The instance handling rendering. /// private readonly SwapChainManager swapChainManager; /// /// Creates a new instance. /// public ComputeShaderPanel() : this(GraphicsDevice.GetDefault()) { } /// /// Creates a new instance. /// /// The instance to use to render frames. public ComputeShaderPanel(GraphicsDevice device) { this.swapChainManager = new SwapChainManager(this, device); this.Loaded += ComputeShaderPanel_Loaded; this.Unloaded += ComputeShaderPanel_Unloaded; 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) { this.swapChainManager.QueueResize(ActualWidth, ActualHeight); 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) { this.swapChainManager.StartRenderLoop(frameRequestQueue, shaderRunner); } } // Requests a cancellation for the render thread private void ComputeShaderPanel_Unloaded(object sender, RoutedEventArgs e) { this.swapChainManager.StopRenderLoop(); } // Updates the background store for the frame size factors used by the render thread private void ComputeShaderPanel_SizeChanged(object sender, SizeChangedEventArgs e) { this.swapChainManager.QueueResize(e.NewSize.Width, e.NewSize.Height); } // Updates the background store for the composition scale factors used by the render thread private void ComputeShaderPanel_CompositionScaleChanged(SwapChainPanel sender, object args) { this.swapChainManager.QueueCompositionScaleChange(CompositionScaleX, CompositionScaleY); } /// public void Dispose() { this.swapChainManager.Dispose(); } }