Skip to content

Commit

Permalink
Merge pull request #842 from Sergio0694/dev/aot-winui-sample
Browse files Browse the repository at this point in the history
Add NativeAOT support to WinUI 3 sample app
  • Loading branch information
Sergio0694 authored Aug 25, 2024
2 parents 92214f7 + 6a040d8 commit 72e8930
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 41 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,13 @@ jobs:
run: >
msbuild samples\ComputeSharp.NativeLibrary.WinRT\ComputeSharp.NativeLibrary.WinRT.csproj /restore -t:publish
/p:Configuration=Release /p:Platform=${{matrix.platform}} /p:RuntimeIdentifier=win-${{matrix.platform}}
# Publish the WinUI 3 sample app with NativeAOT
- if: matrix.platform == 'x64'
name: Publish ComputeSharp.SwapChain.WinUI
run: >
msbuild samples\ComputeSharp.SwapChain.WinUI\ComputeSharp.SwapChain.WinUI.csproj /restore -t:publish
/p:Configuration=Release /p:Platform=${{matrix.platform}} /p:RuntimeIdentifier=win-${{matrix.platform}}
# Download the NuGet packages generated in the previous job and use them
# to build and run the sample project referencing them. This is used as
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,68 @@
<PublishProfile>win-$(Platform).pubxml</PublishProfile>
<UseWinUI>true</UseWinUI>
<EnableMsixTooling>true</EnableMsixTooling>
<RestoreSources>
https://api.nuget.org/v3/index.json;
https://pkgs.dev.azure.com/dotnet/CommunityToolkit/_packaging/CommunityToolkit-MainLatest/nuget/v3/index.json;
</RestoreSources>
</PropertyGroup>

<PropertyGroup>

<!--
Temporary workaround: don't fail builds for 'IL2059' errors, like this:
"XamlTypeInfo.g.cs(495): Trim analysis warning IL2059: ComputeSharp.SwapChain.WinUI.ComputeSharp_SwapChain_WinUI_XamlTypeInfo.XamlTypeInfoProvider.
StaticInitializer_38_Nullable(): Unrecognized value passed to the parameter 'type' of method 'System.Runtime. CompilerServices.RuntimeHelpers
.RunClassConstructor(RuntimeTypeHandle)'. It's not possible to guarantee the availability of the target static constructor."
This is due to a bug in the .NET trim analyzer and in ILC, see: https://github.com/dotnet/runtime/issues/106939.
The best fix would also be to update the XAML compiler in WinAppSDK to skip generating unnecessary static constructor stubs.
-->
<TrimmerSingleWarn>false</TrimmerSingleWarn>
<WarningsNotAsErrors>$(WarningsNotAsErrors);IL2059</WarningsNotAsErrors>
</PropertyGroup>

<!-- Additional CsWinRT properties (temporary, until the .NET SDK is updated) -->
<PropertyGroup>

<!--
We're only referencing CsWinRT for the source generators, we don't need to read any WinRT metadata.
CsWinRT is in projection mode by default, so we want to disable it (or it'd need the Windows SDK).
-->
<CsWinRTGenerateProjection>false</CsWinRTGenerateProjection>

<!-- We'll never have any IIDs to patch in this library -->
<CsWinRTIIDOptimizerOptOut>true</CsWinRTIIDOptimizerOptOut>

<!-- Enable AOT warnings for types implementing projected WinRT interfaces mapped to built-in types (eg. 'INotifyPropertyChanged') -->
<CsWinRTAotWarningLevel>2</CsWinRTAotWarningLevel>
</PropertyGroup>

<!-- CsWinRT size saving options -->
<PropertyGroup>
<CsWinRTEnableDynamicObjectsSupport>false</CsWinRTEnableDynamicObjectsSupport>
<CsWinRTUseExceptionResourceKeys>true</CsWinRTUseExceptionResourceKeys>
</PropertyGroup>

<!-- NativeAOT options -->
<PropertyGroup>
<PublishAot>true</PublishAot>
<UseSystemResourceKeys>true</UseSystemResourceKeys>
<InvariantGlobalization>true</InvariantGlobalization>
<StackTraceSupport>false</StackTraceSupport>
<OptimizationPreference>Speed</OptimizationPreference>
</PropertyGroup>

<ItemGroup>
<Content Include="Assets\SplashScreen.scale-200.png" />
<Content Include="Assets\LockScreenLogo.scale-200.png" />
<Content Include="Assets\Square150x150Logo.scale-200.png" />
<Content Include="Assets\Square44x44Logo.scale-200.png" />
<Content Include="Assets\Square44x44Logo.targetsize-24_altform-unplated.png" />
<Content Include="Assets\Wide310x150Logo.scale-200.png" />
<Content Include="Assets\Misc\noise_low.png" />
<Content Include="..\ComputeSharp.SwapChain\Textures\RustyMetal.png">
<Link>Assets\Textures\RustyMetal.png</Link>
</Content>
</ItemGroup>

<ItemGroup>
<Manifest Include="$(ApplicationManifest)" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
<PackageReference Include="CommunityToolkit.WinUI.UI.Controls.Core" Version="7.1.2" />
<PackageReference Include="CommunityToolkit.WinUI.UI.Media" Version="7.1.2" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.3.0-build.5" />
<PackageReference Include="CommunityToolkit.WinUI.Controls.Primitives" Version="8.2.240824-build.1155" />
<PackageReference Include="CommunityToolkit.WinUI.Media" Version="8.2.240824-build.1155" />
<PackageReference Include="Microsoft.Windows.CsWinRT" Version="2.1.1" />
</ItemGroup>

<!--
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace ComputeSharp.SwapChain.WinUI.Views;
/// A custom <see cref="Control"/> that can be used to render animated backgrounds via Win2D.
/// </summary>
[TemplatePart(Name = "PART_CanvasAnimatedControl", Type = typeof(CanvasAnimatedControl))]
public sealed class D2D1AnimatedPixelShaderPanel : Control
public sealed partial class D2D1AnimatedPixelShaderPanel : Control
{
/// <summary>
/// The <see cref="DependencyProperty"/> backing <see cref="PixelShaderEffect"/>.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,7 @@
<Platform>arm64</Platform>
<RuntimeIdentifier>win-arm64</RuntimeIdentifier>
<PublishDir>bin\$(Configuration)\$(TargetFramework)\$(RuntimeIdentifier)\publish\</PublishDir>
<PublishAot>true</PublishAot>
<SelfContained>true</SelfContained>
<PublishSingleFile>False</PublishSingleFile>
<PublishReadyToRun Condition="'$(Configuration)' == 'Debug'">False</PublishReadyToRun>
<PublishReadyToRun Condition="'$(Configuration)' != 'Debug'">True</PublishReadyToRun>
<!--
See https://github.com/microsoft/CsWinRT/issues/373
<PublishTrimmed>True</PublishTrimmed>
-->
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,7 @@
<Platform>x64</Platform>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<PublishDir>bin\$(Configuration)\$(TargetFramework)\$(RuntimeIdentifier)\publish\</PublishDir>
<PublishAot>true</PublishAot>
<SelfContained>true</SelfContained>
<PublishSingleFile>False</PublishSingleFile>
<PublishReadyToRun Condition="'$(Configuration)' == 'Debug'">False</PublishReadyToRun>
<PublishReadyToRun Condition="'$(Configuration)' != 'Debug'">True</PublishReadyToRun>
<!--
See https://github.com/microsoft/CsWinRT/issues/373
<PublishTrimmed>True</PublishTrimmed>
-->
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace ComputeSharp.SwapChain.Core.Shaders;
/// <summary>
/// An base effect for an animated pixel shader.
/// </summary>
public abstract class PixelShaderEffect : CanvasEffect
public abstract partial class PixelShaderEffect : CanvasEffect
{
/// <summary>
/// The current elapsed time.
Expand Down Expand Up @@ -57,7 +57,7 @@ public int ScreenHeight
/// </summary>
/// <typeparam name="T">The type of pixel shader to render.</typeparam>
/// <param name="factory">The input <typeparamref name="T"/> factory.</param>
public sealed class For<T>(For<T>.Factory factory) : PixelShaderEffect
public sealed partial class For<T>(For<T>.Factory factory) : PixelShaderEffect
where T : unmanaged, ID2D1PixelShader, ID2D1PixelShaderDescriptor<T>
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace ComputeSharp.SwapChain.Core.Shaders.Runners;
/// <summary>
/// A specialized <see cref="PixelShaderEffect"/> for <see cref="ContouredLayers"/>.
/// </summary>
public sealed class D2D1ContouredLayersEffect : PixelShaderEffect
public sealed partial class D2D1ContouredLayersEffect : PixelShaderEffect
{
/// <summary>
/// The reusable <see cref="PixelShaderEffect{T}"/> node to use to render frames.
Expand Down
10 changes: 6 additions & 4 deletions samples/ComputeSharp.SwapChain.WinUI/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using ComputeSharp.SwapChain.Shaders;
using ComputeSharp.WinUI;

#pragma warning disable IDE0300 // CsWinRT does not support marshalling collection expression (https://github.com/microsoft/CsWinRT/issues/1661)

namespace ComputeSharp.SwapChain.Core.ViewModels;

/// <summary>
Expand Down Expand Up @@ -68,13 +70,13 @@ public MainViewModel()
/// <summary>
/// Gets the available resolution scaling options (as percentage values).
/// </summary>
public IList<int> ResolutionScaleOptions { get; } = [25, 50, 75, 100];
public IList<int> ResolutionScaleOptions { get; } = new int[] { 25, 50, 75, 100 };

/// <summary>
/// Gets the collection of available compute shader.
/// </summary>
public IReadOnlyList<ShaderRunnerViewModel> ComputeShaderOptions { get; } =
[
public IReadOnlyList<ShaderRunnerViewModel> ComputeShaderOptions { get; } = new ShaderRunnerViewModel[]
{
new(
nameof(ColorfulInfinity),
new ShaderRunner<ColorfulInfinity>(static time => new((float)time.TotalSeconds)),
Expand Down Expand Up @@ -112,7 +114,7 @@ public MainViewModel()
nameof(TerracedHills),
new ShaderRunner<TerracedHills>(static time => new((float)time.TotalSeconds)),
new PixelShaderEffect.For<SwapChain.Shaders.D2D1.TerracedHills>(static (time, width, height) => new((float)time.TotalSeconds, new int2(width, height)))),
];
};

/// <summary>
/// Checks whether the resolution scale can currently be expliclty set.
Expand Down
8 changes: 4 additions & 4 deletions samples/ComputeSharp.SwapChain.WinUI/Views/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
x:Class="ComputeSharp.SwapChain.WinUI.Views.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:animations="using:CommunityToolkit.WinUI.UI.Animations"
xmlns:animations="using:CommunityToolkit.WinUI.Animations"
xmlns:computesharp="using:ComputeSharp.WinUI"
xmlns:controls="using:CommunityToolkit.WinUI.UI.Controls"
xmlns:controls="using:CommunityToolkit.WinUI.Controls"
xmlns:converters="using:ComputeSharp.SwapChain.Core.Converters"
xmlns:enums="using:ComputeSharp.SwapChain.Core.Enums"
xmlns:media="using:CommunityToolkit.WinUI.UI.Media"
xmlns:ui="using:CommunityToolkit.WinUI.UI"
xmlns:media="using:CommunityToolkit.WinUI.Media"
xmlns:ui="using:CommunityToolkit.WinUI"
xmlns:viewModels="using:ComputeSharp.SwapChain.Core.ViewModels"
xmlns:views="using:ComputeSharp.SwapChain.WinUI.Views"
SizeChanged="Window_SizeChanged">
Expand Down
2 changes: 1 addition & 1 deletion src/ComputeSharp.WinUI/ComputeSharp.WinUI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.6.240807006-preview1" />
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.6.240821007-preview2" />

<!-- Reference CsWinRT locally for the source generators (see comments in ComputeSharp.D2D1.WinUI) -->
<PackageReference Include="Microsoft.Windows.CsWinRT" Version="2.1.1" PrivateAssets="all" />
Expand Down

0 comments on commit 72e8930

Please sign in to comment.