Move files from ComputeSharp.SwapChain.Core to WinUI project
このコミットが含まれているのは:
1 親
7f7e084974
コミット
cb0977c0ea
17ファイル変更
+1
-52
検索条件に一致するファイルはありません
@@ -0,0 +1,108 @@
|
||||
using System;
|
||||
using ComputeSharp.D2D1;
|
||||
using ComputeSharp.D2D1.Descriptors;
|
||||
#if WINDOWS_UWP
|
||||
using ComputeSharp.D2D1.Uwp;
|
||||
#else
|
||||
using ComputeSharp.D2D1.WinUI;
|
||||
#endif
|
||||
|
||||
#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, ID2D1PixelShaderDescriptor<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// The <see cref="PixelShaderEffect{T}"/> node in use.
|
||||
/// </summary>
|
||||
private static readonly EffectNode<PixelShaderEffect<T>> Effect = new();
|
||||
|
||||
/// <summary>
|
||||
/// The <typeparamref name="T"/> factory to use.
|
||||
/// </summary>
|
||||
private readonly Factory factory;
|
||||
|
||||
/// <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 void BuildEffectGraph(EffectGraph effectGraph)
|
||||
{
|
||||
effectGraph.RegisterOutputNode(Effect, new PixelShaderEffect<T>());
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override void ConfigureEffectGraph(EffectGraph effectGraph)
|
||||
{
|
||||
effectGraph.GetNode(Effect).ConstantBuffer = this.factory(ElapsedTime, ScreenWidth, ScreenHeight);
|
||||
}
|
||||
|
||||
/// <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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using ComputeSharp.SwapChain.Shaders;
|
||||
#if WINDOWS_UWP
|
||||
using ComputeSharp.Uwp;
|
||||
#else
|
||||
using ComputeSharp.WinUI;
|
||||
#endif
|
||||
using Windows.ApplicationModel;
|
||||
|
||||
#nullable enable
|
||||
|
||||
namespace ComputeSharp.SwapChain.Core.Shaders.Runners;
|
||||
|
||||
/// <summary>
|
||||
/// A specialized <see cref="IShaderRunner"/> for <see cref="ContouredLayers"/>.
|
||||
/// </summary>
|
||||
public sealed class ContouredLayersRunner : IShaderRunner
|
||||
{
|
||||
/// <summary>
|
||||
/// A texture for <c>\Textures\RustyMetal.png</c>.
|
||||
/// </summary>
|
||||
private ReadOnlyTexture2D<Rgba32, Float4>? texture;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public bool TryExecute(IReadWriteNormalizedTexture2D<Float4> texture, TimeSpan timespan, object? parameter)
|
||||
{
|
||||
if (this.texture is null ||
|
||||
this.texture.GraphicsDevice != texture.GraphicsDevice)
|
||||
{
|
||||
string filename = Path.Combine(Package.Current.InstalledLocation.Path, "Assets", "Textures", "RustyMetal.png");
|
||||
|
||||
this.texture?.Dispose();
|
||||
|
||||
this.texture = texture.GraphicsDevice.LoadReadOnlyTexture2D<Rgba32, Float4>(filename);
|
||||
}
|
||||
|
||||
texture.GraphicsDevice.ForEach(texture, new ContouredLayers((float)timespan.TotalSeconds, this.texture));
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
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;
|
||||
using Windows.ApplicationModel;
|
||||
|
||||
#nullable enable
|
||||
|
||||
namespace ComputeSharp.SwapChain.Core.Shaders.Runners;
|
||||
|
||||
/// <summary>
|
||||
/// A specialized <see cref="PixelShaderEffect"/> for <see cref="ContouredLayers"/>.
|
||||
/// </summary>
|
||||
public sealed class D2D1ContouredLayersEffect : PixelShaderEffect
|
||||
{
|
||||
/// <summary>
|
||||
/// The reusable <see cref="PixelShaderEffect{T}"/> node to use to render frames.
|
||||
/// </summary>
|
||||
private static readonly EffectNode<PixelShaderEffect<ContouredLayers>> PixelShaderEffect = new();
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override unsafe void BuildEffectGraph(EffectGraph effectGraph)
|
||||
{
|
||||
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);
|
||||
|
||||
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));
|
||||
|
||||
// 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
|
||||
PixelShaderEffect<ContouredLayers> pixelShaderEffect = new() { ResourceTextureManagers = { [0] = resourceTextureManager } };
|
||||
|
||||
// Register the pixel shader effect as the output node in the effect graph
|
||||
effectGraph.RegisterOutputNode(PixelShaderEffect, pixelShaderEffect);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override void ConfigureEffectGraph(EffectGraph effectGraph)
|
||||
{
|
||||
effectGraph.GetNode(PixelShaderEffect).ConstantBuffer = new ContouredLayers((float)ElapsedTime.TotalSeconds, new int2(ScreenWidth, ScreenHeight));
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする