using System; using Microsoft.UI.Xaml; using Windows.Foundation; namespace ComputeSharp.WinUI; /// partial class AnimatedComputeShaderPanel { /// /// Raised whenever rendering starts. /// public event TypedEventHandler? RenderingStarted { add => this.swapChainManager.RenderingStarted += value; remove => this.swapChainManager.RenderingStarted -= value; } /// /// Raised whenever rendering stops. /// public event TypedEventHandler? RenderingStopped { add => this.swapChainManager.RenderingStopped += value; remove => this.swapChainManager.RenderingStopped -= value; } /// /// Raised whenever rendering fails. /// public event TypedEventHandler? RenderingFailed { add => this.swapChainManager.RenderingFailed += value; remove => this.swapChainManager.RenderingFailed -= value; } /// /// Raised whenever the control is disposed and all underlying resources are released. /// public event TypedEventHandler? Disposed { add => this.swapChainManager.Disposed += value; remove => this.swapChainManager.Disposed -= value; } /// /// Gets or sets the instance to use to render content. /// public IShaderRunner? ShaderRunner { get => (IShaderRunner?)GetValue(ShaderRunnerProperty); set => SetValue(ShaderRunnerProperty, value); } /// /// The backing . /// public static readonly DependencyProperty ShaderRunnerProperty = DependencyProperty.Register( nameof(ShaderRunner), typeof(IShaderRunner), typeof(AnimatedComputeShaderPanel), new PropertyMetadata(null, OnShaderRunnerPropertyChanged)); /// private static void OnShaderRunnerPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { AnimatedComputeShaderPanel @this = (AnimatedComputeShaderPanel)d; if (@this.IsLoaded && !@this.IsPaused && e.NewValue is IShaderRunner shaderRunner) { @this.swapChainManager.StartRenderLoop(null, shaderRunner); } else { @this.swapChainManager.StopRenderLoop(); } } /// /// 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, exceeding values will be clamped). public double ResolutionScale { get => (double)GetValue(ResolutionScaleProperty); set => SetValue(ResolutionScaleProperty, Math.Clamp(value, 0.1, 1.0)); } /// /// The backing . /// public static readonly DependencyProperty ResolutionScaleProperty = DependencyProperty.Register( nameof(ResolutionScale), typeof(double), typeof(AnimatedComputeShaderPanel), new PropertyMetadata(1.0, OnResolutionScalePropertyChanged)); /// private static void OnResolutionScalePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { AnimatedComputeShaderPanel @this = (AnimatedComputeShaderPanel)d; double resolutionScale = (double)e.NewValue; @this.swapChainManager.QueueResolutionScaleChange(resolutionScale); } /// /// 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. /// The default value for this property is . /// public bool IsDynamicResolutionEnabled { get => (bool)GetValue(IsDynamicResolutionEnabledProperty); set => SetValue(IsDynamicResolutionEnabledProperty, value); } /// /// The backing . /// public static readonly DependencyProperty IsDynamicResolutionEnabledProperty = DependencyProperty.Register( nameof(IsDynamicResolutionEnabled), typeof(bool), typeof(AnimatedComputeShaderPanel), new PropertyMetadata(true, OnIsDynamicResolutionEnabledPropertyChanged)); /// private static void OnIsDynamicResolutionEnabledPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { AnimatedComputeShaderPanel @this = (AnimatedComputeShaderPanel)d; bool isDynamicResolutionEnabled = (bool)e.NewValue; @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) { AnimatedComputeShaderPanel @this = (AnimatedComputeShaderPanel)d; bool isVerticalSyncEnabled = (bool)e.NewValue; @this.swapChainManager.QueueVerticalSyncModeChange(isVerticalSyncEnabled); } /// /// Gets or sets whether or not the rendering is paused. /// public bool IsPaused { get => (bool)GetValue(IsPausedProperty); set => SetValue(IsPausedProperty, value); } /// /// The backing . /// public static readonly DependencyProperty IsPausedProperty = DependencyProperty.Register( nameof(IsPaused), typeof(bool), typeof(AnimatedComputeShaderPanel), new PropertyMetadata(false, OnIsPausedPropertyChanged)); /// private static void OnIsPausedPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { AnimatedComputeShaderPanel @this = (AnimatedComputeShaderPanel)d; if (@this.IsLoaded && !(bool)e.NewValue && @this.ShaderRunner is IShaderRunner shaderRunner) { @this.swapChainManager.StartRenderLoop(null, shaderRunner); } else { @this.swapChainManager.StopRenderLoop(); } } }