Skip to content

Commit

Permalink
Update WinUI sample XAML to be on par with UWP
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio0694 committed Feb 26, 2023
1 parent a022ead commit f5ac2cc
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 25 deletions.
2 changes: 1 addition & 1 deletion samples/ComputeSharp.SwapChain.Uwp/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
<ResourceDictionary Source="Controls/D2D1AnimatedPixelShaderPanel.xaml"/>
<ResourceDictionary Source="Controls/D2D1AnimatedPixelShaderPanel.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Expand Down
8 changes: 3 additions & 5 deletions samples/ComputeSharp.SwapChain.Uwp/Views/MainView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@

<!-- Shader switches (for DirectX 12 or Win2D) -->
<controls:SwitchPresenter Grid.Row="1" Value="{x:Bind ViewModel.SelectedRenderingMode, Mode=OneWay}">
<controls:Case Value="{x:Bind enums:RenderingMode.DirectX12}" IsDefault="True">
<controls:Case IsDefault="True" Value="{x:Bind enums:RenderingMode.DirectX12}">

<!-- DirectX 12 shader -->
<computesharp:AnimatedComputeShaderPanel
Expand All @@ -157,14 +157,12 @@
IsVerticalSyncEnabled="{x:Bind ViewModel.IsVerticalSyncEnabled, Mode=OneWay}"
RenderingFailed="MainShaderPanel_RenderingFailed"
ResolutionScale="{x:Bind converters:ResolutionScaleConverter.ConvertPercentageToScale(ViewModel.SelectedResolutionScale), Mode=OneWay}"
ShaderRunner="{x:Bind ViewModel.SelectedComputeShader.ShaderRunner, Mode=OneWay}"/>
ShaderRunner="{x:Bind ViewModel.SelectedComputeShader.ShaderRunner, Mode=OneWay}" />
</controls:Case>
<controls:Case Value="{x:Bind enums:RenderingMode.Win2D}">

<!-- Win2D shader -->
<views:D2D1AnimatedPixelShaderPanel
IsPaused="{x:Bind ViewModel.IsRenderingPaused, Mode=OneWay}"
ShaderRunner="{x:Bind ViewModel.SelectedComputeShader.D2D1ShaderRunner, Mode=OneWay}"/>
<views:D2D1AnimatedPixelShaderPanel IsPaused="{x:Bind ViewModel.IsRenderingPaused, Mode=OneWay}" ShaderRunner="{x:Bind ViewModel.SelectedComputeShader.D2D1ShaderRunner, Mode=OneWay}" />
</controls:Case>
</controls:SwitchPresenter>

Expand Down
1 change: 1 addition & 0 deletions samples/ComputeSharp.SwapChain.WinUI/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
<ResourceDictionary Source="Controls/D2D1AnimatedPixelShaderPanel.xaml" />
<ResourceDictionary>

<!-- Override the title bar colors to keep it transparent -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public CanvasAnimatedControl()
this.canvasControl.Draw += CanvasControl_Draw;

// Setup and start the render timer (24fps for now) and tracking timer
this.dispatcherQueueTimer.Interval = TimeSpan.FromMilliseconds(1 / 24.0);
this.dispatcherQueueTimer.Interval = TimeSpan.FromSeconds(1 / 24.0);
this.dispatcherQueueTimer.Tick += (s, e) => this.canvasControl.Invalidate();
this.dispatcherQueueTimer.Start();
this.stopwatch.Start();
Expand Down
89 changes: 73 additions & 16 deletions samples/ComputeSharp.SwapChain.WinUI/Views/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
xmlns:computesharp="using:ComputeSharp.WinUI"
xmlns:controls="using:CommunityToolkit.WinUI.UI.Controls"
xmlns:converters="using:ComputeSharp.SwapChain.Core.Converters"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:enums="using:ComputeSharp.SwapChain.Core.Enums"
xmlns:media="using:CommunityToolkit.WinUI.UI.Media"
xmlns:ui="using:CommunityToolkit.WinUI.UI"
xmlns:viewmodels="using:ComputeSharp.SwapChain.Core.ViewModels"
SizeChanged="UserControl_SizeChanged"
mc:Ignorable="d">
xmlns:views="using:ComputeSharp.SwapChain.WinUI.Views"
SizeChanged="Window_SizeChanged">

<Grid x:Name="Root">
<Grid.Resources>
Expand Down Expand Up @@ -119,7 +118,10 @@
<Grid>

<!-- Shader preview -->
<computesharp:AnimatedComputeShaderPanel Loaded="AnimatedComputeShaderPanel_Loaded" ShaderRunner="{x:Bind ShaderRunner}" />
<computesharp:AnimatedComputeShaderPanel
Loaded="AnimatedComputeShaderPanel_Loaded"
RenderingFailed="SelectionShaderPanel_RenderingFailed"
ShaderRunner="{x:Bind ShaderRunner}" />

<!--
Note: here we have a selection property to work around {x:Bind} not working correctly outside of the
Expand All @@ -146,15 +148,26 @@
</Border>
</Grid.Resources>

<!-- Shader -->
<computesharp:AnimatedComputeShaderPanel
x:Name="ShaderPanel"
Grid.Row="1"
IsDynamicResolutionEnabled="{x:Bind ViewModel.IsDynamicResolutionEnabled, Mode=OneWay}"
IsPaused="{x:Bind ViewModel.IsRenderingPaused, Mode=OneWay}"
IsVerticalSyncEnabled="{x:Bind ViewModel.IsVerticalSyncEnabled, Mode=OneWay}"
ResolutionScale="{x:Bind converters:ResolutionScaleConverter.ConvertPercentageToScale(ViewModel.SelectedResolutionScale), Mode=OneWay}"
ShaderRunner="{x:Bind ViewModel.SelectedComputeShader.ShaderRunner, Mode=OneWay}" />
<!-- Shader switches (for DirectX 12 or Win2D) -->
<controls:SwitchPresenter Grid.Row="1" Value="{x:Bind ViewModel.SelectedRenderingMode, Mode=OneWay}">
<controls:Case IsDefault="True" Value="{x:Bind enums:RenderingMode.DirectX12}">

<!-- DirectX 12 shader -->
<computesharp:AnimatedComputeShaderPanel
x:Name="ShaderPanel"
IsDynamicResolutionEnabled="{x:Bind ViewModel.IsDynamicResolutionEnabled, Mode=OneWay}"
IsPaused="{x:Bind ViewModel.IsRenderingPaused, Mode=OneWay}"
IsVerticalSyncEnabled="{x:Bind ViewModel.IsVerticalSyncEnabled, Mode=OneWay}"
RenderingFailed="MainShaderPanel_RenderingFailed"
ResolutionScale="{x:Bind converters:ResolutionScaleConverter.ConvertPercentageToScale(ViewModel.SelectedResolutionScale), Mode=OneWay}"
ShaderRunner="{x:Bind ViewModel.SelectedComputeShader.ShaderRunner, Mode=OneWay}" />
</controls:Case>
<controls:Case Value="{x:Bind enums:RenderingMode.Win2D}">

<!-- Win2D shader -->
<views:D2D1AnimatedPixelShaderPanel IsPaused="{x:Bind ViewModel.IsRenderingPaused, Mode=OneWay}" ShaderRunner="{x:Bind ViewModel.SelectedComputeShader.D2D1ShaderRunner, Mode=OneWay}" />
</controls:Case>
</controls:SwitchPresenter>

<!-- Settings panel -->
<StackPanel
Expand Down Expand Up @@ -190,7 +203,25 @@
<SymbolIcon Symbol="Setting" />
<Button.Flyout>
<MenuFlyout>
<ToggleMenuFlyoutItem IsChecked="{x:Bind ViewModel.IsDynamicResolutionEnabled, Mode=TwoWay}" Text="Dynamic resolution" />
<ToggleMenuFlyoutItem
Command="{x:Bind ViewModel.SetRenderingModeCommand}"
CommandParameter="{x:Bind enums:RenderingMode.DirectX12}"
IsChecked="{x:Bind ViewModel.SelectedRenderingMode.Equals(enums:RenderingMode.DirectX12), Mode=OneWay}"
Text="DirectX 12" />
<ToggleMenuFlyoutItem
Command="{x:Bind ViewModel.SetRenderingModeCommand}"
CommandParameter="{x:Bind enums:RenderingMode.Win2D}"
IsChecked="{x:Bind ViewModel.SelectedRenderingMode.Equals(enums:RenderingMode.Win2D), Mode=OneWay}"
Text="Win2D (Direct2D)" />
<MenuFlyoutSeparator />
<ToggleMenuFlyoutItem
IsChecked="{x:Bind ViewModel.IsVerticalSyncEnabled, Mode=TwoWay}"
IsEnabled="{x:Bind ViewModel.SelectedRenderingMode.Equals(enums:RenderingMode.DirectX12), Mode=OneWay}"
Text="Vertical sync" />
<ToggleMenuFlyoutItem
IsChecked="{x:Bind ViewModel.IsDynamicResolutionEnabled, Mode=TwoWay}"
IsEnabled="{x:Bind ViewModel.SelectedRenderingMode.Equals(enums:RenderingMode.DirectX12), Mode=OneWay}"
Text="Dynamic resolution" />
<MenuFlyoutSeparator />
<ToggleMenuFlyoutItem
Command="{x:Bind ViewModel.SetResolutionScaleCommand}"
Expand Down Expand Up @@ -224,9 +255,35 @@
<!-- Invisible title bar -->
<Rectangle
x:Name="TitleBarRectangle"
Height="28"
Height="32"
HorizontalAlignment="Stretch"
VerticalAlignment="Top"
Fill="Transparent" />

<!-- Info bar -->
<InfoBar
x:Name="RenderingErrorInfoBar"
Title="Rendering error"
Margin="80"
Message="There was an error that caused the rendering to fail. Try pausing and starting the rendering again, or restarting the app. If the issue persists, please report it by opening an issue on GitHub."
Severity="Error">
<InfoBar.ActionButton>
<HyperlinkButton Content="Report the issue" NavigateUri="https://github.com/Sergio0694/ComputeSharp/issues" />
</InfoBar.ActionButton>
<animations:Implicit.ShowAnimations>
<animations:OpacityAnimation
From="0"
To="1.0"
Duration="0:0:0.5" />
<animations:TranslationAnimation
From="0,20,0"
To="0"
Duration="0:0:0.5" />
</animations:Implicit.ShowAnimations>
<animations:Implicit.HideAnimations>
<animations:OpacityAnimation To="0" Duration="0:0:0.5" />
<animations:TranslationAnimation To="0,20,0" Duration="0:0:0.5" />
</animations:Implicit.HideAnimations>
</InfoBar>
</Grid>
</Window>
16 changes: 14 additions & 2 deletions samples/ComputeSharp.SwapChain.WinUI/Views/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace ComputeSharp.SwapChain.WinUI.Views;

/// <summary>
/// A view for <see cref="Core.ViewModels.MainViewModel"/>.
/// A view for <see cref="MainViewModel"/>.
/// </summary>
public sealed partial class MainWindow : Window
{
Expand Down Expand Up @@ -62,7 +62,7 @@ private void ShaderSelectionPanel_Tapped(object sender, TappedRoutedEventArgs e)
}

// Updates the size of the shaders list panel
private void UserControl_SizeChanged(object sender, WindowSizeChangedEventArgs e)
private void Window_SizeChanged(object sender, WindowSizeChangedEventArgs e)
{
this.ShadersListContainerPanel.Height = Math.Round(e.Size.Height * 0.35);
}
Expand All @@ -72,4 +72,16 @@ private void AnimatedComputeShaderPanel_Loaded(object sender, RoutedEventArgs e)
{
this.shaderPanels.AddOrUpdate((AnimatedComputeShaderPanel)sender, null);
}

// Logs rendering failed in the main panel
private void MainShaderPanel_RenderingFailed(AnimatedComputeShaderPanel sender, RenderingFailedEventArgs args)
{
this.RenderingErrorInfoBar.IsOpen = true;
}

// Logs rendering failed in a secondary panel
private void SelectionShaderPanel_RenderingFailed(AnimatedComputeShaderPanel sender, RenderingFailedEventArgs args)
{
this.RenderingErrorInfoBar.IsOpen = true;
}
}

0 comments on commit f5ac2cc

Please sign in to comment.