using System; #if WINDOWS_UWP using Windows.UI.Xaml; #else using Microsoft.UI.Xaml; #endif using Windows.Foundation; #if WINDOWS_UWP namespace ComputeSharp.Uwp; #else namespace ComputeSharp.WinUI; #endif /// partial class ComputeShaderPanel { /// /// 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 request new frames. /// public IFrameRequestQueue? FrameRequestQueue { get => (IFrameRequestQueue)GetValue(FrameRequestQueueProperty); set => SetValue(FrameRequestQueueProperty, value); } /// /// The backing . /// public static readonly DependencyProperty FrameRequestQueueProperty = DependencyProperty.Register( nameof(FrameRequestQueue), typeof(IFrameRequestQueue), typeof(ComputeShaderPanel), new PropertyMetadata(null, OnFrameRequestQueueChanged)); /// private static void OnFrameRequestQueueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { ComputeShaderPanel @this = (ComputeShaderPanel)d; if (@this.IsLoaded && e.NewValue is IFrameRequestQueue frameRequestQueue && @this.ShaderRunner is IShaderRunner shaderRunner) { @this.swapChainManager.StartRenderLoop(frameRequestQueue, shaderRunner); } else { @this.swapChainManager.StopRenderLoop(); } } /// /// 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(ComputeShaderPanel), new PropertyMetadata(null, OnShaderRunnerPropertyChanged)); /// private static void OnShaderRunnerPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { ComputeShaderPanel @this = (ComputeShaderPanel)d; if (@this.IsLoaded && @this.FrameRequestQueue is IFrameRequestQueue frameRequestQueue && e.NewValue is IShaderRunner shaderRunner) { @this.swapChainManager.StartRenderLoop(frameRequestQueue, 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(ComputeShaderPanel), new PropertyMetadata(1.0, OnResolutionScalePropertyChanged)); /// private static void OnResolutionScalePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { ComputeShaderPanel @this = (ComputeShaderPanel)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(ComputeShaderPanel), new PropertyMetadata(true, OnIsDynamicResolutionEnabledPropertyChanged)); /// private static void OnIsDynamicResolutionEnabledPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { ComputeShaderPanel @this = (ComputeShaderPanel)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(ComputeShaderPanel), new PropertyMetadata(true, OnIsVerticalSyncEnabledPropertyChanged)); /// private static void OnIsVerticalSyncEnabledPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { ComputeShaderPanel @this = (ComputeShaderPanel)d; bool isVerticalSyncEnabled = (bool)e.NewValue; @this.swapChainManager.QueueVerticalSyncModeChange(isVerticalSyncEnabled); } }