Switch D2D1 UWP/WinUI samples to use CanvasEffect
This replaces the ID2D1ShaderRunner interface in the sample apps
このコミットが含まれているのは:
1 親
2578cf3028
コミット
a291685aff
12ファイル変更
+219
-166
検索条件に一致するファイルはありません
@@ -1,61 +0,0 @@
|
||||
using System;
|
||||
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;
|
||||
|
||||
namespace ComputeSharp.SwapChain.Core.Shaders;
|
||||
|
||||
/// <summary>
|
||||
/// 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, IDisposable
|
||||
where T : unmanaged, ID2D1PixelShader
|
||||
{
|
||||
/// <summary>
|
||||
/// The <see cref="Func{T1,T2,T3TResult}"/> instance used to create shaders to run.
|
||||
/// </summary>
|
||||
private readonly Func<TimeSpan, int, int, T> shaderFactory;
|
||||
|
||||
/// <summary>
|
||||
/// The reusable <see cref="PixelShaderEffect{T}"/> instance to use to render frames.
|
||||
/// </summary>
|
||||
private readonly PixelShaderEffect<T> pixelShaderEffect;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="D2D1ShaderRunner{T}"/> instance.
|
||||
/// </summary>
|
||||
/// <param name="shaderFactory">The <see cref="Func{T1,TResult}"/> instance used to create shaders to run.</param>
|
||||
public D2D1ShaderRunner(Func<TimeSpan, int, int, T> shaderFactory)
|
||||
{
|
||||
Guard.IsNotNull(shaderFactory);
|
||||
|
||||
this.shaderFactory = shaderFactory;
|
||||
this.pixelShaderEffect = new PixelShaderEffect<T>();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Execute(ICanvasAnimatedControl sender, CanvasAnimatedDrawEventArgs args)
|
||||
{
|
||||
int widthInPixels = sender.ConvertDipsToPixels((float)sender.Size.Width, CanvasDpiRounding.Round);
|
||||
int heightInPixels = sender.ConvertDipsToPixels((float)sender.Size.Height, CanvasDpiRounding.Round);
|
||||
|
||||
// Set the constant buffer
|
||||
this.pixelShaderEffect.ConstantBuffer = this.shaderFactory(args.Timing.TotalTime, widthInPixels, heightInPixels);
|
||||
|
||||
// Draw the shader
|
||||
args.DrawingSession.DrawImage(this.pixelShaderEffect);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Dispose()
|
||||
{
|
||||
this.pixelShaderEffect.Dispose();
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
using Microsoft.Graphics.Canvas.UI.Xaml;
|
||||
|
||||
namespace ComputeSharp.SwapChain.Core.Shaders;
|
||||
|
||||
/// <summary>
|
||||
/// An interface for a shader runner to be used with a Win2D renderer.
|
||||
/// </summary>
|
||||
public interface ID2D1ShaderRunner
|
||||
{
|
||||
/// <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);
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
using System;
|
||||
using ComputeSharp.D2D1;
|
||||
#if WINDOWS_UWP
|
||||
using ComputeSharp.D2D1.Uwp;
|
||||
#else
|
||||
using ComputeSharp.D2D1.WinUI;
|
||||
#endif
|
||||
using Microsoft.Graphics.Canvas;
|
||||
|
||||
#nullable enable
|
||||
|
||||
namespace ComputeSharp.SwapChain.Core.Shaders;
|
||||
|
||||
/// <summary>
|
||||
/// An base effect for an animated pixel shader.
|
||||
/// </summary>
|
||||
public abstract class PixelShaderEffect : CanvasEffect
|
||||
{
|
||||
/// <summary>
|
||||
/// The current elapsed time.
|
||||
/// </summary>
|
||||
private TimeSpan elapsedTime;
|
||||
|
||||
/// <summary>
|
||||
/// The current screen width in raw pixels.
|
||||
/// </summary>
|
||||
private int screenWidth;
|
||||
|
||||
/// <summary>
|
||||
/// The current screen height in raw pixels.
|
||||
/// </summary>
|
||||
private int screenHeight;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the total elapsed time.
|
||||
/// </summary>
|
||||
public TimeSpan ElapsedTime
|
||||
{
|
||||
get => this.elapsedTime;
|
||||
set => SetAndInvalidateEffectGraph(ref this.elapsedTime, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the screen width in raw pixels.
|
||||
/// </summary>
|
||||
public int ScreenWidth
|
||||
{
|
||||
get => this.screenWidth;
|
||||
set => SetAndInvalidateEffectGraph(ref this.screenWidth, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the screen height in raw pixels.
|
||||
/// </summary>
|
||||
public int ScreenHeight
|
||||
{
|
||||
get => this.screenHeight;
|
||||
set => SetAndInvalidateEffectGraph(ref this.screenHeight, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// An effect for an animated pixel shader.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of pixel shader to render.</typeparam>
|
||||
public sealed class For<T> : PixelShaderEffect
|
||||
where T : unmanaged, ID2D1PixelShader
|
||||
{
|
||||
/// <summary>
|
||||
/// The <typeparamref name="T"/> factory to use.
|
||||
/// </summary>
|
||||
private readonly Factory factory;
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="PixelShaderEffect{T}"/> instance in use.
|
||||
/// </summary>
|
||||
private PixelShaderEffect<T>? effect;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="For{T}"/> instance with the specified parameters.
|
||||
/// </summary>
|
||||
/// <param name="factory">The input <typeparamref name="T"/> factory.</param>
|
||||
public For(Factory factory)
|
||||
{
|
||||
this.factory = factory;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override ICanvasImage BuildEffectGraph()
|
||||
{
|
||||
this.effect = new PixelShaderEffect<T>();
|
||||
|
||||
return this.effect;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override void ConfigureEffectGraph()
|
||||
{
|
||||
this.effect!.ConstantBuffer = this.factory(ElapsedTime, ScreenWidth, ScreenHeight);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
base.Dispose(disposing);
|
||||
|
||||
if (disposing)
|
||||
{
|
||||
this.effect?.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A factory of a given shader instance.
|
||||
/// </summary>
|
||||
/// <param name="elapsedTime">The total elapsed time.</param>
|
||||
/// <param name="screenWidth">The screen width in raw pixels.</param>
|
||||
/// <param name="screenHeight">The screen height in raw pixels.</param>
|
||||
/// <returns>A shader instance to render.</returns>
|
||||
public delegate T Factory(TimeSpan elapsedTime, int screenWidth, int screenHeight);
|
||||
}
|
||||
}
|
||||
+41
-41
@@ -9,7 +9,6 @@ using ComputeSharp.D2D1.WinUI;
|
||||
#endif
|
||||
using ComputeSharp.SwapChain.Shaders.D2D1;
|
||||
using Microsoft.Graphics.Canvas;
|
||||
using Microsoft.Graphics.Canvas.UI.Xaml;
|
||||
using Windows.ApplicationModel;
|
||||
|
||||
#nullable enable
|
||||
@@ -17,9 +16,9 @@ using Windows.ApplicationModel;
|
||||
namespace ComputeSharp.SwapChain.Core.Shaders.Runners;
|
||||
|
||||
/// <summary>
|
||||
/// A specialized <see cref="ID2D1ShaderRunner"/> for <see cref="ContouredLayers"/>.
|
||||
/// A specialized <see cref="PixelShaderEffect"/> for <see cref="ContouredLayers"/>.
|
||||
/// </summary>
|
||||
public sealed class D2D1ContouredLayersRunner : ID2D1ShaderRunner, IDisposable
|
||||
public sealed class D2D1ContouredLayersEffect : PixelShaderEffect
|
||||
{
|
||||
/// <summary>
|
||||
/// The reusable <see cref="PixelShaderEffect{T}"/> instance to use to render frames.
|
||||
@@ -27,54 +26,55 @@ public sealed class D2D1ContouredLayersRunner : ID2D1ShaderRunner, IDisposable
|
||||
private PixelShaderEffect<ContouredLayers>? pixelShaderEffect;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public unsafe void Execute(ICanvasAnimatedControl sender, CanvasAnimatedDrawEventArgs args)
|
||||
protected override unsafe ICanvasImage BuildEffectGraph()
|
||||
{
|
||||
if (this.pixelShaderEffect is null)
|
||||
{
|
||||
string filename = Path.Combine(Package.Current.InstalledLocation.Path, "Assets", "Textures", "RustyMetal.png");
|
||||
string filename = Path.Combine(Package.Current.InstalledLocation.Path, "Assets", "Textures", "RustyMetal.png");
|
||||
|
||||
// As a temporary workaround for the lack of image decoding helpers for D2D1ResourceTextureManager, and in order to
|
||||
// keep the code here synchronous and simple, we can just leverage the DirectX 12 APIs to load textures. That is, we
|
||||
// first load a texture (which will use WIC behind the scenes) to get the decoded image data in GPU memory, and then
|
||||
// copy the data in a readback texture we can read from on the CPU. We can't just load an upload texture and read
|
||||
// from it, as that type of texture can only be written to from the CPU. From there, we create a D2D resource texture.
|
||||
using ReadOnlyTexture2D<Rgba32, float4> readOnlyTexture = GraphicsDevice.GetDefault().LoadReadOnlyTexture2D<Rgba32, float4>(filename);
|
||||
using ReadBackTexture2D<Rgba32> readBackTexture = GraphicsDevice.GetDefault().AllocateReadBackTexture2D<Rgba32>(readOnlyTexture.Width, readOnlyTexture.Height);
|
||||
// As a temporary workaround for the lack of image decoding helpers for D2D1ResourceTextureManager, and in order to
|
||||
// keep the code here synchronous and simple, we can just leverage the DirectX 12 APIs to load textures. That is, we
|
||||
// first load a texture (which will use WIC behind the scenes) to get the decoded image data in GPU memory, and then
|
||||
// copy the data in a readback texture we can read from on the CPU. We can't just load an upload texture and read
|
||||
// from it, as that type of texture can only be written to from the CPU. From there, we create a D2D resource texture.
|
||||
using ReadOnlyTexture2D<Rgba32, float4> readOnlyTexture = GraphicsDevice.GetDefault().LoadReadOnlyTexture2D<Rgba32, float4>(filename);
|
||||
using ReadBackTexture2D<Rgba32> readBackTexture = GraphicsDevice.GetDefault().AllocateReadBackTexture2D<Rgba32>(readOnlyTexture.Width, readOnlyTexture.Height);
|
||||
|
||||
readOnlyTexture.CopyTo(readBackTexture);
|
||||
readOnlyTexture.CopyTo(readBackTexture);
|
||||
|
||||
// Get the buffer pointer, the stride, and calculate the buffer size without the trailing padding.
|
||||
// That is, the area between the end of the data in the last row and the stride is not included.
|
||||
Rgba32* dataBuffer = readBackTexture.View.DangerousGetAddressAndByteStride(out int strideInBytes);
|
||||
int bufferSize = ((readBackTexture.Height - 1) * strideInBytes) + (readBackTexture.View.Width * sizeof(Rgba32));
|
||||
// Get the buffer pointer, the stride, and calculate the buffer size without the trailing padding.
|
||||
// That is, the area between the end of the data in the last row and the stride is not included.
|
||||
Rgba32* dataBuffer = readBackTexture.View.DangerousGetAddressAndByteStride(out int strideInBytes);
|
||||
int bufferSize = ((readBackTexture.Height - 1) * strideInBytes) + (readBackTexture.View.Width * sizeof(Rgba32));
|
||||
|
||||
// Create the resource texture manager to use in the shader
|
||||
D2D1ResourceTextureManager resourceTextureManager = new(
|
||||
extents: stackalloc uint[] { (uint)readBackTexture.Height, (uint)readBackTexture.Width },
|
||||
bufferPrecision: D2D1BufferPrecision.UInt8Normalized,
|
||||
channelDepth: D2D1ChannelDepth.Four,
|
||||
filter: D2D1Filter.MinMagMipLinear,
|
||||
extendModes: stackalloc D2D1ExtendMode[] { D2D1ExtendMode.Mirror, D2D1ExtendMode.Mirror },
|
||||
data: new ReadOnlySpan<byte>(dataBuffer, bufferSize),
|
||||
strides: stackalloc uint[] { (uint)strideInBytes });
|
||||
// Create the resource texture manager to use in the shader
|
||||
D2D1ResourceTextureManager resourceTextureManager = new(
|
||||
extents: stackalloc uint[] { (uint)readBackTexture.Height, (uint)readBackTexture.Width },
|
||||
bufferPrecision: D2D1BufferPrecision.UInt8Normalized,
|
||||
channelDepth: D2D1ChannelDepth.Four,
|
||||
filter: D2D1Filter.MinMagMipLinear,
|
||||
extendModes: stackalloc D2D1ExtendMode[] { D2D1ExtendMode.Mirror, D2D1ExtendMode.Mirror },
|
||||
data: new ReadOnlySpan<byte>(dataBuffer, bufferSize),
|
||||
strides: stackalloc uint[] { (uint)strideInBytes });
|
||||
|
||||
// Create the new pixel shader effect
|
||||
this.pixelShaderEffect = new PixelShaderEffect<ContouredLayers>() { ResourceTextureManagers = { [0] = resourceTextureManager } };
|
||||
}
|
||||
// Create the new pixel shader effect
|
||||
this.pixelShaderEffect = new PixelShaderEffect<ContouredLayers>() { ResourceTextureManagers = { [0] = resourceTextureManager } };
|
||||
|
||||
int widthInPixels = sender.ConvertDipsToPixels((float)sender.Size.Width, CanvasDpiRounding.Round);
|
||||
int heightInPixels = sender.ConvertDipsToPixels((float)sender.Size.Height, CanvasDpiRounding.Round);
|
||||
|
||||
// Set the constant buffer
|
||||
this.pixelShaderEffect.ConstantBuffer = new ContouredLayers((float)args.Timing.TotalTime.TotalSeconds, new int2(widthInPixels, heightInPixels));
|
||||
|
||||
// Draw the shader
|
||||
args.DrawingSession.DrawImage(this.pixelShaderEffect);
|
||||
return this.pixelShaderEffect;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Dispose()
|
||||
protected override void ConfigureEffectGraph()
|
||||
{
|
||||
this.pixelShaderEffect?.Dispose();
|
||||
this.pixelShaderEffect!.ConstantBuffer = new ContouredLayers((float)ElapsedTime.TotalSeconds, new int2(ScreenWidth, ScreenHeight));
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
base.Dispose(disposing);
|
||||
|
||||
if (disposing)
|
||||
{
|
||||
this.pixelShaderEffect?.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする