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" />
|
||||
|
||||
@@ -0,0 +1,151 @@
|
||||
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.
|
||||
/// </summary>
|
||||
[TemplatePart(Name = "PART_CanvasAnimatedControl", Type = typeof(CanvasAnimatedControl))]
|
||||
public sealed class D2D1AnimatedPixelShaderPanel : Control
|
||||
{
|
||||
/// <summary>
|
||||
/// The wrapped <see cref="CanvasAnimatedControl"/> instance used to render frames.
|
||||
/// </summary>
|
||||
private CanvasAnimatedControl? canvasAnimatedControl;
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="ID2D1ShaderRunner"/> instance currently in use, if any.
|
||||
/// </summary>
|
||||
private volatile ID2D1ShaderRunner? shaderRunner;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="D2D1AnimatedPixelShaderPanel"/> instance.
|
||||
/// </summary>
|
||||
public D2D1AnimatedPixelShaderPanel()
|
||||
{
|
||||
DefaultStyleKey = typeof(D2D1AnimatedPixelShaderPanel);
|
||||
|
||||
Loaded += D2D1AnimatedPixelShaderPanel_Loaded;
|
||||
Unloaded += D2D1AnimatedPixelShaderPanel_Unloaded;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override void OnApplyTemplate()
|
||||
{
|
||||
base.OnApplyTemplate();
|
||||
|
||||
this.canvasAnimatedControl = (CanvasAnimatedControl)GetTemplateChild("PART_CanvasAnimatedControl")!;
|
||||
this.canvasAnimatedControl.Draw += CanvasAnimatedControl_Draw;
|
||||
}
|
||||
|
||||
// Ensure the panel is not paused when the control is loaded
|
||||
private void D2D1AnimatedPixelShaderPanel_Loaded(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (this.canvasAnimatedControl is { } canvasAnimatedControl &&
|
||||
ShaderRunner is not null &&
|
||||
!IsPaused)
|
||||
{
|
||||
canvasAnimatedControl.Paused = false;
|
||||
}
|
||||
}
|
||||
|
||||
// Always pause rendering when the control is unloaded
|
||||
private void D2D1AnimatedPixelShaderPanel_Unloaded(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (this.canvasAnimatedControl is { } canvasAnimatedControl)
|
||||
{
|
||||
canvasAnimatedControl.Paused = true;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Draws a new frame on the wrapped <see cref="CanvasAnimatedControl"/> instance.
|
||||
/// </summary>
|
||||
/// <inheritdoc cref="ID2D1ShaderRunner.Execute"/>
|
||||
private void CanvasAnimatedControl_Draw(ICanvasAnimatedControl sender, CanvasAnimatedDrawEventArgs args)
|
||||
{
|
||||
this.shaderRunner?.Execute(sender, args);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the <see cref="ID2D1ShaderRunner"/> instance to use to render content.
|
||||
/// </summary>
|
||||
public ID2D1ShaderRunner? ShaderRunner
|
||||
{
|
||||
get => (ID2D1ShaderRunner?)GetValue(ShaderRunnerProperty);
|
||||
set => SetValue(ShaderRunnerProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="DependencyProperty"/> backing <see cref="ShaderRunner"/>.
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty ShaderRunnerProperty = DependencyProperty.Register(
|
||||
nameof(ShaderRunner),
|
||||
typeof(ID2D1ShaderRunner),
|
||||
typeof(D2D1AnimatedPixelShaderPanel),
|
||||
new PropertyMetadata(null, OnShaderRunnerPropertyChanged));
|
||||
|
||||
/// <inheritdoc cref="DependencyPropertyChangedCallback"/>
|
||||
private static void OnShaderRunnerPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
D2D1AnimatedPixelShaderPanel @this = (D2D1AnimatedPixelShaderPanel)d;
|
||||
ID2D1ShaderRunner? shaderRunner = (ID2D1ShaderRunner?)e.NewValue;
|
||||
|
||||
// Save the new shader runner for later (as the dependency property cannot be accessed by the render thread)
|
||||
@this.shaderRunner = shaderRunner;
|
||||
|
||||
// Pause or start the render thread if a runner is available
|
||||
if (@this.canvasAnimatedControl is { } canvasAnimatedControl)
|
||||
{
|
||||
bool shouldRender = @this.IsLoaded && !@this.IsPaused && shaderRunner is not null;
|
||||
|
||||
canvasAnimatedControl.Paused = !shouldRender;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets whether or not the rendering is paused.
|
||||
/// </summary>
|
||||
public bool IsPaused
|
||||
{
|
||||
get => (bool)GetValue(IsPausedProperty);
|
||||
set => SetValue(IsPausedProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="DependencyProperty"/> backing <see cref="IsPaused"/>.
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty IsPausedProperty = DependencyProperty.Register(
|
||||
nameof(IsPaused),
|
||||
typeof(bool),
|
||||
typeof(D2D1AnimatedPixelShaderPanel),
|
||||
new PropertyMetadata(false, OnIsPausedPropertyChanged));
|
||||
|
||||
/// <inheritdoc cref="DependencyPropertyChangedCallback"/>
|
||||
private static void OnIsPausedPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
D2D1AnimatedPixelShaderPanel @this = (D2D1AnimatedPixelShaderPanel)d;
|
||||
bool isPaused = (bool)e.NewValue;
|
||||
|
||||
if (@this.canvasAnimatedControl is { } canvasAnimatedControl)
|
||||
{
|
||||
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();
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする