using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using ComputeSharp.SwapChain.Core.Enums;
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
#pragma warning disable IDE0300 // CsWinRT does not support marshalling collection expression (https://github.com/microsoft/CsWinRT/issues/1661)
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
{
///
/// Creates a new instance.
///
public MainViewModel()
{
SelectedComputeShader = ComputeShaderOptions[0];
}
///
/// Gets or sets the selected rendering mode.
///
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(IsResolutionScaleOptionEnabled))]
public partial RenderingMode SelectedRenderingMode { get; set; } = RenderingMode.DirectX12;
///
/// Gets or sets whether the vertical sync is enabled.
///
[ObservableProperty]
public partial bool IsVerticalSyncEnabled { get; set; } = true;
///
/// Gets or sets whether the dynamic resolution is enabled.
///
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(IsResolutionScaleOptionEnabled))]
public partial bool IsDynamicResolutionEnabled { get; set; } = true;
///
/// Gets the currently selected resolution scale setting (as percentage value).
///
[ObservableProperty]
public partial int SelectedResolutionScale { get; set; } = 100;
///
/// Gets or sets the currently selected compute shader.
///
[ObservableProperty]
public partial ShaderRunnerViewModel SelectedComputeShader { get; set; }
///
/// Gets or sets whether the rendering is currently paused.
///
[ObservableProperty]
public partial bool IsRenderingPaused { get; set; }
///
/// Gets the available resolution scaling options (as percentage values).
///
public ReadOnlyCollection ResolutionScaleOptions { get; } = new((int[])[25, 50, 75, 100]);
///
/// Gets the collection of available compute shader.
///
public ReadOnlyCollection 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 OnSelectedComputeShaderChanging(ShaderRunnerViewModel oldValue, ShaderRunnerViewModel newValue)
{
// Known issue with partial properties: while the property is marked as not nullable, assigning it in the
// constructor will suppress the warning but cause the setter to be invoked, with 'field' being 'null'.
// As such, as need to check for that scenario here to avoid an exception. Possibly fixed in C# 14.
if (oldValue is not null)
{
oldValue.IsSelected = false;
}
newValue.IsSelected = true;
}
}