using System.Collections.Generic; using CommunityToolkit.Diagnostics; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using ComputeSharp.SwapChain.Core.Constants; using ComputeSharp.SwapChain.Core.Enums; 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.selectedRenderingMode = RenderingMode.DirectX12; this.isVerticalSyncEnabled = true; this.isDynamicResolutionEnabled = true; this.selectedResolutionScale = 100; this.selectedComputeShader = ComputeShaderOptions[0]; this.selectedComputeShader.IsSelected = true; } /// /// Gets or sets the selected rendering mode. /// [ObservableProperty] [NotifyPropertyChangedFor(nameof(IsResolutionScaleOptionEnabled))] private RenderingMode selectedRenderingMode; /// /// Gets or sets whether the vertical sync is enabled. /// [ObservableProperty] private bool isVerticalSyncEnabled; /// /// Gets or sets whether the dynamic resolution is enabled. /// [ObservableProperty] [NotifyPropertyChangedFor(nameof(IsResolutionScaleOptionEnabled))] private bool isDynamicResolutionEnabled; /// /// Gets the currently selected resolution scale setting (as percentage value). /// [ObservableProperty] private int selectedResolutionScale; /// /// Gets or sets the currently selected compute shader. /// [ObservableProperty] private ShaderRunnerViewModel selectedComputeShader; /// /// Gets or sets whether the rendering is currently paused. /// [ObservableProperty] private bool isRenderingPaused; /// /// Gets the available resolution scaling options (as percentage values). /// public IList ResolutionScaleOptions { get; } = new[] { 25, 50, 75, 100 }; /// /// Gets the collection of available compute shader. /// public IReadOnlyList ComputeShaderOptions { get; } = new ShaderRunnerViewModel[] { new( nameof(ColorfulInfinity), new ShaderRunner(static time => new((float)time.TotalSeconds)), new PixelShaderEffect.For(static (time, width, height) => new((float)time.TotalSeconds, new int2(width, height)))), new( nameof(FractalTiling), new ShaderRunner(static time => new((float)time.TotalSeconds)), new PixelShaderEffect.For(static (time, width, height) => new((float)time.TotalSeconds, new int2(width, height)))), new( nameof(MengerJourney), new ShaderRunner(static time => new((float)time.TotalSeconds)), new PixelShaderEffect.For(static (time, width, height) => new((float)time.TotalSeconds, new int2(width, height)))), new( nameof(Octagrams), new ShaderRunner(static time => new((float)time.TotalSeconds)), new PixelShaderEffect.For(static (time, width, height) => new((float)time.TotalSeconds, new int2(width, height)))), new( nameof(ProteanClouds), new ShaderRunner(static time => new((float)time.TotalSeconds)), new PixelShaderEffect.For(static (time, width, height) => new((float)time.TotalSeconds, new int2(width, height)))), new( nameof(TwoTiledTruchet), new ShaderRunner(static time => new((float)time.TotalSeconds)), new PixelShaderEffect.For(static (time, width, height) => new((float)time.TotalSeconds, new int2(width, height)))), new( nameof(PyramidPattern), new ShaderRunner(static time => new((float)time.TotalSeconds)), new PixelShaderEffect.For(static (time, width, height) => new((float)time.TotalSeconds, new int2(width, height)))), new( nameof(TriangleGridContouring), new ShaderRunner(static time => new((float)time.TotalSeconds)), new PixelShaderEffect.For(static (time, width, height) => new((float)time.TotalSeconds, new int2(width, height)))), new(nameof(ContouredLayers), new ContouredLayersRunner(), new D2D1ContouredLayersEffect()), new( nameof(TerracedHills), new ShaderRunner(static time => new((float)time.TotalSeconds)), new PixelShaderEffect.For(static (time, width, height) => new((float)time.TotalSeconds, new int2(width, height)))), }; /// /// Checks whether the resolution scale can currently be expliclty set. /// public bool IsResolutionScaleOptionEnabled => SelectedRenderingMode == RenderingMode.DirectX12 && !IsDynamicResolutionEnabled; /// /// Sets . /// [RelayCommand] private void SetRenderingMode(RenderingMode renderingMode) { SelectedRenderingMode = renderingMode; } /// /// Sets . /// [RelayCommand] private void SetResolutionScale(int resolutionScale) { SelectedResolutionScale = resolutionScale; } /// /// Toggles . /// [RelayCommand] private void ToggleRenderingPaused() { IsRenderingPaused = !IsRenderingPaused; } /// partial void OnSelectedRenderingModeChanged(RenderingMode value) { this.analyticsService.Log(Event.SelectedRenderingModeChanged, (nameof(value), value)); } /// partial void OnIsVerticalSyncEnabledChanged(bool value) { this.analyticsService.Log(Event.IsVerticalSyncEnabledChanged, (nameof(value), value)); } /// partial void OnIsDynamicResolutionEnabledChanged(bool value) { this.analyticsService.Log(Event.IsDynamicResolutionEnabledChanged, (nameof(value), value)); } /// partial void OnSelectedResolutionScaleChanged(int value) { this.analyticsService.Log(Event.SelectedResolutionScaleChanged, (nameof(value), value)); } /// partial void OnSelectedComputeShaderChanging(ShaderRunnerViewModel value) { SelectedComputeShader.IsSelected = false; value.IsSelected = true; } /// partial void OnSelectedComputeShaderChanged(ShaderRunnerViewModel value) { this.analyticsService.Log(Event.SelectedComputeShaderChanged, (nameof(value.ShaderType), value.ShaderType)); } /// partial void OnIsRenderingPausedChanged(bool value) { this.analyticsService.Log(Event.IsRenderingPausedChanged, (nameof(value), value)); } }