using System.Collections.Generic; using CommunityToolkit.Diagnostics; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using ComputeSharp.SwapChain.Core.Constants; using ComputeSharp.SwapChain.Core.Services; using ComputeSharp.SwapChain.Core.Shaders; using ComputeSharp.SwapChain.Core.Shaders.Runners; using ComputeSharp.SwapChain.Shaders; #if WINDOWS_UWP using ComputeSharp.Uwp; #else using ComputeSharp.WinUI; #endif #nullable enable namespace ComputeSharp.SwapChain.Core.ViewModels; /// /// The viewmodel for for a view that allows users to select shaders and customize settings. /// public sealed partial class MainViewModel : ObservableObject { /// /// The instance currently in use. /// private readonly IAnalyticsService analyticsService; /// /// Creates a new instance. /// public MainViewModel(IAnalyticsService analyticsService) { Guard.IsNotNull(analyticsService); this.analyticsService = analyticsService; this.isVerticalSyncEnabled = true; this.isDynamicResolutionEnabled = true; this.selectedResolutionScale = 100; this.selectedComputeShader = ComputeShaderOptions[0]; this.selectedComputeShader.IsSelected = true; } /// /// Gets the available resolution scaling options (as percentage values). /// public IList ResolutionScaleOptions { get; } = new[] { 25, 50, 75, 100 }; private bool isVerticalSyncEnabled; /// /// Gets or sets whether the vertical sync is enabled. /// public bool IsVerticalSyncEnabled { get => this.isVerticalSyncEnabled; set { if (SetProperty(ref this.isVerticalSyncEnabled, value)) { this.analyticsService.Log(Event.IsVerticalSyncEnabledChanged, (nameof(value), value)); } } } private bool isDynamicResolutionEnabled; /// /// Gets or sets whether the dynamic resolution is enabled. /// public bool IsDynamicResolutionEnabled { get => this.isDynamicResolutionEnabled; set { if (SetProperty(ref this.isDynamicResolutionEnabled, value)) { this.analyticsService.Log(Event.IsDynamicResolutionEnabledChanged, (nameof(value), value)); } } } private int selectedResolutionScale; /// /// Gets the currently selected resolution scale setting (as percentage valaue). /// public int SelectedResolutionScale { get => this.selectedResolutionScale; private set { if (SetProperty(ref this.selectedResolutionScale, value)) { this.analyticsService.Log(Event.SelectedResolutionScaleChanged, (nameof(value), value)); } } } /// /// Gets the collection of available compute shader. /// public IReadOnlyList ComputeShaderOptions { get; } = new ShaderRunnerViewModel[] { new( typeof(ColorfulInfinity), new ShaderRunner(static time => new((float)time.TotalSeconds)), new D2D1ShaderRunner(static (time, width, height) => new((float)time.TotalSeconds, new int2(width, height)))), new( typeof(FractalTiling), new ShaderRunner(static time => new((float)time.TotalSeconds)), new D2D1ShaderRunner(static (time, width, height) => new((float)time.TotalSeconds, new int2(width, height)))), new( typeof(MengerJourney), new ShaderRunner(static time => new((float)time.TotalSeconds)), new D2D1ShaderRunner(static (time, width, height) => new((float)time.TotalSeconds, new int2(width, height)))), new( typeof(Octagrams), new ShaderRunner(static time => new((float)time.TotalSeconds)), new D2D1ShaderRunner(static (time, width, height) => new((float)time.TotalSeconds, new int2(width, height)))), new( typeof(ProteanClouds), new ShaderRunner(static time => new((float)time.TotalSeconds)), new D2D1ShaderRunner(static (time, width, height) => new((float)time.TotalSeconds, new int2(width, height)))), new( typeof(TwoTiledTruchet), new ShaderRunner(static time => new((float)time.TotalSeconds)), new D2D1ShaderRunner(static (time, width, height) => new((float)time.TotalSeconds, new int2(width, height)))), new( typeof(PyramidPattern), new ShaderRunner(static time => new((float)time.TotalSeconds)), new D2D1ShaderRunner(static (time, width, height) => new((float)time.TotalSeconds, new int2(width, height)))), new( typeof(TriangleGridContouring), new ShaderRunner(static time => new((float)time.TotalSeconds)), new D2D1ShaderRunner(static (time, width, height) => new((float)time.TotalSeconds, new int2(width, height)))), new( typeof(ContouredLayers), new ContouredLayersRunner(), new D2D1ShaderRunner(static (time, width, height) => new((float)time.TotalSeconds, new int2(width, height)))), new( typeof(TerracedHills), new ShaderRunner(static time => new((float)time.TotalSeconds)), new D2D1ShaderRunner(static (time, width, height) => new((float)time.TotalSeconds, new int2(width, height)))), }; private ShaderRunnerViewModel selectedComputeShader; /// /// Gets or sets the currently selected compute shader. /// public ShaderRunnerViewModel SelectedComputeShader { get => this.selectedComputeShader; set { this.selectedComputeShader.IsSelected = false; if (SetProperty(ref this.selectedComputeShader, value) && value is not null) { this.analyticsService.Log(Event.SelectedComputeShaderChanged, (nameof(value.ShaderType), value.ShaderType.Name)); value.IsSelected = true; } } } private bool isRenderingPaused; /// /// Gets or sets whether the rendering is currently paused. /// public bool IsRenderingPaused { get => this.isRenderingPaused; set { if (SetProperty(ref this.isRenderingPaused, value)) { this.analyticsService.Log(Event.IsRenderingPausedChanged, (nameof(value), value)); } } } /// /// Sets . /// [RelayCommand] private void SetResolutionScale(int resolutionScale) { SelectedResolutionScale = resolutionScale; } /// /// Toggles . /// [RelayCommand] private void ToggleRenderingPaused() { IsRenderingPaused = !IsRenderingPaused; } }