Share D2D1 rendering code between UWP/WinUI samples
このコミットが含まれているのは:
1 親
63ce21aa06
コミット
7254f1774c
10ファイル変更
+64
-28
検索条件に一致するファイルはありません
@@ -11,6 +11,7 @@
|
||||
<ItemGroup>
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Constants\Error.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Constants\Event.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Controls\D2D1AnimatedPixelShaderPanel.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Converters\RenderingPauseConverter.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Converters\ResolutionScaleConverter.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Enums\RenderingMode.cs" />
|
||||
|
||||
+10
-1
@@ -1,11 +1,20 @@
|
||||
using ComputeSharp.SwapChain.Core.Shaders;
|
||||
using Microsoft.Graphics.Canvas.UI.Xaml;
|
||||
#if WINDOWS_UWP
|
||||
using Windows.UI.Xaml;
|
||||
using Windows.UI.Xaml.Controls;
|
||||
#else
|
||||
using Microsoft.UI.Xaml;
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
#endif
|
||||
|
||||
#nullable enable
|
||||
|
||||
#if WINDOWS_UWP
|
||||
namespace ComputeSharp.SwapChain.Uwp.Views;
|
||||
#else
|
||||
namespace ComputeSharp.SwapChain.WinUI.Views;
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// A custom <see cref="Control"/> that can be used to render animated backgrounds via Win2D.
|
||||
@@ -134,7 +143,7 @@ public sealed class D2D1AnimatedPixelShaderPanel : Control
|
||||
|
||||
if (@this.canvasAnimatedControl is { } canvasAnimatedControl)
|
||||
{
|
||||
bool shouldRender = @this.IsLoaded && !isPaused && @this.ShaderRunner is ID2D1ShaderRunner;
|
||||
bool shouldRender = @this.IsLoaded && !isPaused && @this.ShaderRunner is not null;
|
||||
|
||||
canvasAnimatedControl.Paused = !shouldRender;
|
||||
}
|
||||
@@ -3,9 +3,11 @@ using CommunityToolkit.Diagnostics;
|
||||
using ComputeSharp.D2D1;
|
||||
#if WINDOWS_UWP
|
||||
using ComputeSharp.D2D1.Uwp;
|
||||
#else
|
||||
using ComputeSharp.D2D1.WinUI;
|
||||
#endif
|
||||
using Microsoft.Graphics.Canvas;
|
||||
using Microsoft.Graphics.Canvas.UI.Xaml;
|
||||
#endif
|
||||
|
||||
namespace ComputeSharp.SwapChain.Core.Shaders;
|
||||
|
||||
@@ -13,10 +15,9 @@ namespace ComputeSharp.SwapChain.Core.Shaders;
|
||||
/// A simple <see cref="ID2D1ShaderRunner"/> implementation powered by a supplied shader type.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of shader to use to render frames.</typeparam>
|
||||
public sealed class D2D1ShaderRunner<T> : ID2D1ShaderRunner
|
||||
public sealed class D2D1ShaderRunner<T> : ID2D1ShaderRunner, IDisposable
|
||||
where T : unmanaged, ID2D1PixelShader
|
||||
{
|
||||
#if WINDOWS_UWP
|
||||
/// <summary>
|
||||
/// The <see cref="Func{T1,T2,T3TResult}"/> instance used to create shaders to run.
|
||||
/// </summary>
|
||||
@@ -26,7 +27,6 @@ public sealed class D2D1ShaderRunner<T> : ID2D1ShaderRunner
|
||||
/// The reusable <see cref="PixelShaderEffect{T}"/> instance to use to render frames.
|
||||
/// </summary>
|
||||
private readonly PixelShaderEffect<T> pixelShaderEffect;
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="D2D1ShaderRunner{T}"/> instance.
|
||||
@@ -36,13 +36,10 @@ public sealed class D2D1ShaderRunner<T> : ID2D1ShaderRunner
|
||||
{
|
||||
Guard.IsNotNull(shaderFactory);
|
||||
|
||||
#if WINDOWS_UWP
|
||||
this.shaderFactory = shaderFactory;
|
||||
this.pixelShaderEffect = new PixelShaderEffect<T>();
|
||||
#endif
|
||||
}
|
||||
|
||||
#if WINDOWS_UWP
|
||||
/// <inheritdoc/>
|
||||
public void Execute(ICanvasAnimatedControl sender, CanvasAnimatedDrawEventArgs args)
|
||||
{
|
||||
@@ -55,5 +52,10 @@ public sealed class D2D1ShaderRunner<T> : ID2D1ShaderRunner
|
||||
// Draw the shader
|
||||
args.DrawingSession.DrawImage(this.pixelShaderEffect);
|
||||
}
|
||||
#endif
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Dispose()
|
||||
{
|
||||
this.pixelShaderEffect.Dispose();
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,4 @@
|
||||
#if WINDOWS_UWP
|
||||
using Microsoft.Graphics.Canvas.UI.Xaml;
|
||||
#endif
|
||||
|
||||
namespace ComputeSharp.SwapChain.Core.Shaders;
|
||||
|
||||
@@ -9,12 +7,10 @@ namespace ComputeSharp.SwapChain.Core.Shaders;
|
||||
/// </summary>
|
||||
public interface ID2D1ShaderRunner
|
||||
{
|
||||
#if WINDOWS_UWP
|
||||
/// <summary>
|
||||
/// Draws a new frame on a target <see cref="ICanvasAnimatedControl"/> instance.
|
||||
/// </summary>
|
||||
/// <param name="sender">The target <see cref="ICanvasAnimatedControl"/> instance.</param>
|
||||
/// <param name="args">The <see cref="CanvasAnimatedDrawEventArgs"/> instance to use to draw the new frame.</param>
|
||||
void Execute(ICanvasAnimatedControl sender, CanvasAnimatedDrawEventArgs args);
|
||||
#endif
|
||||
}
|
||||
+10
-6
@@ -1,16 +1,16 @@
|
||||
#if WINDOWS_UWP
|
||||
using System;
|
||||
using System.IO;
|
||||
using ComputeSharp.D2D1;
|
||||
using ComputeSharp.D2D1.Interop;
|
||||
#if WINDOWS_UWP
|
||||
using ComputeSharp.D2D1.Uwp;
|
||||
#else
|
||||
using ComputeSharp.D2D1.WinUI;
|
||||
#endif
|
||||
using ComputeSharp.SwapChain.Shaders.D2D1;
|
||||
#if WINDOWS_UWP
|
||||
using Microsoft.Graphics.Canvas;
|
||||
using Microsoft.Graphics.Canvas.UI.Xaml;
|
||||
using Windows.ApplicationModel;
|
||||
#endif
|
||||
|
||||
#nullable enable
|
||||
|
||||
@@ -19,9 +19,8 @@ namespace ComputeSharp.SwapChain.Core.Shaders.Runners;
|
||||
/// <summary>
|
||||
/// A specialized <see cref="ID2D1ShaderRunner"/> for <see cref="ContouredLayers"/>.
|
||||
/// </summary>
|
||||
public sealed class D2D1ContouredLayersRunner : ID2D1ShaderRunner
|
||||
public sealed class D2D1ContouredLayersRunner : ID2D1ShaderRunner, IDisposable
|
||||
{
|
||||
#if WINDOWS_UWP
|
||||
/// <summary>
|
||||
/// The reusable <see cref="PixelShaderEffect{T}"/> instance to use to render frames.
|
||||
/// </summary>
|
||||
@@ -72,5 +71,10 @@ public sealed class D2D1ContouredLayersRunner : ID2D1ShaderRunner
|
||||
// Draw the shader
|
||||
args.DrawingSession.DrawImage(this.pixelShaderEffect);
|
||||
}
|
||||
#endif
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Dispose()
|
||||
{
|
||||
this.pixelShaderEffect?.Dispose();
|
||||
}
|
||||
}
|
||||
@@ -79,7 +79,6 @@
|
||||
<DependentUpon>App.xaml</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="Assets\Generated\ServiceTokens.g.cs" />
|
||||
<Compile Include="Controls\D2D1AnimatedPixelShaderPanel.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Services\AppCenterService.cs" />
|
||||
<Compile Include="Services\ServiceCollectionExtensions.cs" />
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
<ResourceDictionary
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:local="using:ComputeSharp.SwapChain.Uwp.Views"
|
||||
xmlns:win2d="using:Microsoft.Graphics.Canvas.UI.Xaml"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
|
||||
xmlns:win2d="using:Microsoft.Graphics.Canvas.UI.Xaml">
|
||||
|
||||
<Style TargetType="local:D2D1AnimatedPixelShaderPanel">
|
||||
<Setter Property="Template">
|
||||
<Setter.Value>
|
||||
<ControlTemplate TargetType="local:D2D1AnimatedPixelShaderPanel">
|
||||
<win2d:CanvasAnimatedControl x:Name="PART_CanvasAnimatedControl"/>
|
||||
<win2d:CanvasAnimatedControl x:Name="PART_CanvasAnimatedControl" />
|
||||
</ControlTemplate>
|
||||
</Setter.Value>
|
||||
</Setter>
|
||||
|
||||
@@ -57,12 +57,21 @@
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\ComputeSharp.Core.SourceGenerators\ComputeSharp.Core.SourceGenerators.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" PrivateAssets="contentfiles;build" />
|
||||
<ProjectReference Include="..\..\src\ComputeSharp.D2D1.WinUI\ComputeSharp.D2D1.WinUI.csproj" />
|
||||
<ProjectReference Include="..\..\src\ComputeSharp.D2D1\ComputeSharp.D2D1.csproj" />
|
||||
<ProjectReference Include="..\..\src\ComputeSharp.D2D1.SourceGenerators\ComputeSharp.D2D1.SourceGenerators.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" PrivateAssets="contentfiles;build" />
|
||||
<ProjectReference Include="..\..\src\ComputeSharp.WinUI\ComputeSharp.WinUI.csproj" />
|
||||
<ProjectReference Include="..\..\src\ComputeSharp.SourceGenerators\ComputeSharp.SourceGenerators.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" PrivateAssets="contentfiles;build" />
|
||||
</ItemGroup>
|
||||
|
||||
<!-- Resource dictionary for D2D1AnimatedPixelShaderPanel -->
|
||||
<ItemGroup>
|
||||
<None Remove="Controls\D2D1AnimatedPixelShaderPanel.xaml" />
|
||||
<Page Update="Controls\D2D1AnimatedPixelShaderPanel.xaml">
|
||||
<SubType>Designer</SubType>
|
||||
</Page>
|
||||
</ItemGroup>
|
||||
|
||||
<Import Project="..\ComputeSharp.SwapChain.Core.Shared\ComputeSharp.SwapChain.Core.Shared.projitems" Label="Shared" />
|
||||
<Import Project="..\ComputeSharp.SwapChain.Shaders.Shared\ComputeSharp.SwapChain.Shaders.Shared.projitems" Label="Shared" />
|
||||
<Import Project="..\ComputeSharp.SwapChain.Shaders.D2D1.Shared\ComputeSharp.SwapChain.Shaders.D2D1.Shared.projitems" Label="Shared" />
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
<ResourceDictionary
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:local="using:ComputeSharp.SwapChain.WinUI.Views"
|
||||
xmlns:win2d="using:Microsoft.Graphics.Canvas.UI.Xaml">
|
||||
|
||||
<Style TargetType="local:D2D1AnimatedPixelShaderPanel">
|
||||
<Setter Property="Template">
|
||||
<Setter.Value>
|
||||
<ControlTemplate TargetType="local:D2D1AnimatedPixelShaderPanel">
|
||||
<win2d:CanvasAnimatedControl x:Name="PART_CanvasAnimatedControl" />
|
||||
</ControlTemplate>
|
||||
</Setter.Value>
|
||||
</Setter>
|
||||
</Style>
|
||||
</ResourceDictionary>
|
||||
+5
-5
@@ -29,7 +29,7 @@ public sealed class CanvasAnimatedControl : Grid, ICanvasAnimatedControl, IDispo
|
||||
/// <summary>
|
||||
/// Indicates whether rendering is paused.
|
||||
/// </summary>
|
||||
private bool isPaused;
|
||||
private bool paused;
|
||||
|
||||
/// <summary>
|
||||
/// Indicates whether a new frame is currently being drawn.
|
||||
@@ -67,14 +67,14 @@ public sealed class CanvasAnimatedControl : Grid, ICanvasAnimatedControl, IDispo
|
||||
/// <summary>
|
||||
/// Gets or sets whether rendering has paused
|
||||
/// </summary>
|
||||
public bool IsPaused
|
||||
public bool Paused
|
||||
{
|
||||
get => this.isPaused;
|
||||
get => this.paused;
|
||||
set
|
||||
{
|
||||
if (this.isPaused != value)
|
||||
if (this.paused != value)
|
||||
{
|
||||
this.isPaused = value;
|
||||
this.paused = value;
|
||||
|
||||
// Start or stop the timers as needed
|
||||
if (value)
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする