Move files from ComputeSharp.SwapChain.Core to WinUI project
このコミットが含まれているのは:
1 親
7f7e084974
コミット
cb0977c0ea
17ファイル変更
+1
-52
検索条件に一致するファイルはありません
@@ -63,15 +63,7 @@
|
||||
<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" />
|
||||
<!-- Shared projects for the DX12/D2D1 shaders -->
|
||||
<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" />
|
||||
</Project>
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace ComputeSharp.SwapChain.Core.Constants;
|
||||
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> with the collection of tracked errors for analytics.
|
||||
/// </summary>
|
||||
public static class Error
|
||||
{
|
||||
public const string RenderingFailedOnMainPanel = "[SHELL] RenderingFailed (main panel)";
|
||||
public const string RenderingFailedOnSelectionPanel = "[SHELL] RenderingFailed (selection panel)";
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
namespace ComputeSharp.SwapChain.Core.Constants;
|
||||
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> with the collection of tracked events for analytics.
|
||||
/// </summary>
|
||||
public static class Event
|
||||
{
|
||||
public const string SelectedRenderingModeChanged = "[SETTINGS] SelectedRenderingMode";
|
||||
public const string IsVerticalSyncEnabledChanged = "[SETTINGS] IsVerticalSyncEnabled";
|
||||
public const string IsDynamicResolutionEnabledChanged = "[SETTINGS] IsDynamicResolutionEnabled";
|
||||
public const string SelectedResolutionScaleChanged = "[SETTINGS] SelectedResolutionScale";
|
||||
public const string SelectedComputeShaderChanged = "[SETTINGS] SelectedComputeShader";
|
||||
public const string IsRenderingPausedChanged = "[SETTINGS] IsRenderingPaused";
|
||||
public const string OpenShaderSelectionPanel = "[SHELL] Open ShaderSelectionPanel";
|
||||
public const string CloseShaderSelectionPanel = "[SHELL] Close ShaderSelectionPanel";
|
||||
}
|
||||
@@ -0,0 +1,159 @@
|
||||
using ComputeSharp.SwapChain.Core.Shaders;
|
||||
using Microsoft.Graphics.Canvas;
|
||||
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="PixelShaderEffect"/> instance currently in use, if any.
|
||||
/// </summary>
|
||||
private volatile PixelShaderEffect? pixelShaderEffect;
|
||||
|
||||
/// <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 &&
|
||||
PixelShaderEffect 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>
|
||||
/// <param name="sender">The source <see cref="ICanvasAnimatedControl"/> instance.</param>
|
||||
/// <param name="args">The <see cref="CanvasAnimatedDrawEventArgs"/> object to perform drawing with.</param>
|
||||
private void CanvasAnimatedControl_Draw(ICanvasAnimatedControl sender, CanvasAnimatedDrawEventArgs args)
|
||||
{
|
||||
// Set the effect properties
|
||||
this.pixelShaderEffect!.ElapsedTime = args.Timing.TotalTime;
|
||||
this.pixelShaderEffect.ScreenWidth = sender.ConvertDipsToPixels((float)sender.Size.Width, CanvasDpiRounding.Round);
|
||||
this.pixelShaderEffect.ScreenHeight = sender.ConvertDipsToPixels((float)sender.Size.Height, CanvasDpiRounding.Round);
|
||||
|
||||
// Draw the effect
|
||||
args.DrawingSession.DrawImage(this.pixelShaderEffect);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the <see cref="Core.Shaders.PixelShaderEffect"/> instance to use to render content.
|
||||
/// </summary>
|
||||
public PixelShaderEffect? PixelShaderEffect
|
||||
{
|
||||
get => (PixelShaderEffect?)GetValue(PixelShaderEffectProperty);
|
||||
set => SetValue(PixelShaderEffectProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="DependencyProperty"/> backing <see cref="PixelShaderEffect"/>.
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty PixelShaderEffectProperty = DependencyProperty.Register(
|
||||
nameof(PixelShaderEffect),
|
||||
typeof(PixelShaderEffect),
|
||||
typeof(D2D1AnimatedPixelShaderPanel),
|
||||
new PropertyMetadata(null, OnPixelShaderEffectPropertyChanged));
|
||||
|
||||
/// <inheritdoc cref="DependencyPropertyChangedCallback"/>
|
||||
private static void OnPixelShaderEffectPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
D2D1AnimatedPixelShaderPanel @this = (D2D1AnimatedPixelShaderPanel)d;
|
||||
PixelShaderEffect? pixelShaderEffect = (PixelShaderEffect?)e.NewValue;
|
||||
|
||||
// Save the new pixel shader effect for later (as the dependency property cannot be accessed by the render thread)
|
||||
@this.pixelShaderEffect = pixelShaderEffect;
|
||||
|
||||
// Pause or start the render thread if an effect is available
|
||||
if (@this.canvasAnimatedControl is { } canvasAnimatedControl)
|
||||
{
|
||||
bool shouldRender = @this.IsLoaded && !@this.IsPaused && pixelShaderEffect 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.PixelShaderEffect is not null;
|
||||
|
||||
canvasAnimatedControl.Paused = !shouldRender;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
#if WINDOWS_UWP
|
||||
using Windows.UI.Xaml.Controls;
|
||||
#else
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
#endif
|
||||
|
||||
namespace ComputeSharp.SwapChain.Core.Converters;
|
||||
|
||||
/// <summary>
|
||||
/// A class with some static converters for rendering state.
|
||||
/// </summary>
|
||||
public static class RenderingPauseConverter
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets a symbol for an input rendering state.
|
||||
/// </summary>
|
||||
/// <param name="value">Whether or not the rendering is currently paused.</param>
|
||||
/// <returns>A symbol representing the next action for the rendering.</returns>
|
||||
public static Symbol ConvertPausedToSymbol(bool value)
|
||||
{
|
||||
return value ? Symbol.Play : Symbol.Pause;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
namespace ComputeSharp.SwapChain.Core.Converters;
|
||||
|
||||
/// <summary>
|
||||
/// A class with some static converters for resolution scale settings.
|
||||
/// </summary>
|
||||
public static class ResolutionScaleConverter
|
||||
{
|
||||
/// <summary>
|
||||
/// Converts a resolution scale to a formatted string.
|
||||
/// </summary>
|
||||
/// <param name="value">The input resolution scale (in percentage).</param>
|
||||
/// <returns>A formatted percentage string for <paramref name="value"/>.</returns>
|
||||
public static string ConvertPercentageToFormattedString(int value)
|
||||
{
|
||||
return $"{value}%";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a percentage value to a scaled value in the [0, 1] range.
|
||||
/// </summary>
|
||||
/// <param name="value">The input percentage value to scale.</param>
|
||||
/// <returns>The input percentage scaled to the [0, 1] range.</returns>
|
||||
public static double ConvertPercentageToScale(int value)
|
||||
{
|
||||
return value / 100.0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
namespace ComputeSharp.SwapChain.Core.Enums;
|
||||
|
||||
/// <summary>
|
||||
/// Indicates the rendering mode being used.
|
||||
/// </summary>
|
||||
public enum RenderingMode
|
||||
{
|
||||
/// <summary>
|
||||
/// Render using DirectX 12 APIs.
|
||||
/// </summary>
|
||||
DirectX12,
|
||||
|
||||
/// <summary>
|
||||
/// Render using Win2D (ie. Direct2D behind the scenes).
|
||||
/// </summary>
|
||||
Win2D
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Text;
|
||||
|
||||
#nullable enable
|
||||
|
||||
namespace ComputeSharp.SwapChain.Core.Services;
|
||||
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> that manages the analytics service in a test environment.
|
||||
/// </summary>
|
||||
public sealed class DebugAnalyticsService : IAnalyticsService
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public void Log(string title, params (string Property, object? Value)[]? data)
|
||||
{
|
||||
StringBuilder builder = new();
|
||||
|
||||
_ = builder.AppendLine($"[EVENT]: \"{title}\"");
|
||||
|
||||
if (data is not null)
|
||||
{
|
||||
foreach ((string Property, object? Value) info in data)
|
||||
{
|
||||
_ = builder.AppendLine($">> {info.Property}: \"{info.Value ?? "<NULL>"}\"");
|
||||
}
|
||||
}
|
||||
|
||||
Trace.Write(builder);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Log(Exception exception, params (string Property, object? Value)[]? data)
|
||||
{
|
||||
StringBuilder builder = new();
|
||||
|
||||
_ = builder.AppendLine($"[EXCEPTION]: \"{exception.GetType()}\"");
|
||||
_ = builder.AppendLine(">> Stack trace");
|
||||
_ = builder.AppendLine(exception.StackTrace);
|
||||
|
||||
if (data is not null)
|
||||
{
|
||||
foreach ((string Property, object? Value) info in data)
|
||||
{
|
||||
_ = builder.AppendLine($">> {info.Property}: \"{info.Value ?? "<NULL>"}\"");
|
||||
}
|
||||
}
|
||||
|
||||
Trace.Write(builder);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
|
||||
#nullable enable
|
||||
|
||||
namespace ComputeSharp.SwapChain.Core.Services;
|
||||
|
||||
/// <summary>
|
||||
/// The default <see langword="interface"/> for an analytics service.
|
||||
/// </summary>
|
||||
public interface IAnalyticsService
|
||||
{
|
||||
/// <summary>
|
||||
/// Logs an event with a specified title and optional properties.
|
||||
/// </summary>
|
||||
/// <param name="title">The title of the event to track.</param>
|
||||
/// <param name="data">The optional event properties.</param>
|
||||
void Log(string title, params (string Property, object? Value)[]? data);
|
||||
|
||||
/// <summary>
|
||||
/// Logs an exception with optional properties.
|
||||
/// </summary>
|
||||
/// <param name="exception">The exception that has been thrown.</param>
|
||||
/// <param name="data">The optional event properties.</param>
|
||||
void Log(Exception exception, params (string Property, object? Value)[]? data);
|
||||
}
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,208 @@
|
||||
using System.Collections.Generic;
|
||||
using CommunityToolkit.Diagnostics;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using ComputeSharp.SwapChain.Core.Constants;
|
||||
using ComputeSharp.SwapChain.Core.Enums;
|
||||
using ComputeSharp.SwapChain.Core.Services;
|
||||
using ComputeSharp.SwapChain.Core.Shaders;
|
||||
using ComputeSharp.SwapChain.Core.Shaders.Runners;
|
||||
using ComputeSharp.SwapChain.Shaders;
|
||||
#if WINDOWS_UWP
|
||||
using ComputeSharp.Uwp;
|
||||
#else
|
||||
using ComputeSharp.WinUI;
|
||||
#endif
|
||||
|
||||
#nullable enable
|
||||
|
||||
namespace ComputeSharp.SwapChain.Core.ViewModels;
|
||||
|
||||
/// <summary>
|
||||
/// The viewmodel for for a view that allows users to select shaders and customize settings.
|
||||
/// </summary>
|
||||
public sealed partial class MainViewModel : ObservableObject
|
||||
{
|
||||
/// <summary>
|
||||
/// The <see cref="IAnalyticsService"/> instance currently in use.
|
||||
/// </summary>
|
||||
private readonly IAnalyticsService analyticsService;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="MainViewModel"/> instance.
|
||||
/// </summary>
|
||||
public MainViewModel(IAnalyticsService analyticsService)
|
||||
{
|
||||
Guard.IsNotNull(analyticsService);
|
||||
|
||||
this.analyticsService = analyticsService;
|
||||
this.selectedRenderingMode = RenderingMode.DirectX12;
|
||||
this.isVerticalSyncEnabled = true;
|
||||
this.isDynamicResolutionEnabled = true;
|
||||
this.selectedResolutionScale = 100;
|
||||
this.selectedComputeShader = ComputeShaderOptions[0];
|
||||
this.selectedComputeShader.IsSelected = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the selected rendering mode.
|
||||
/// </summary>
|
||||
[ObservableProperty]
|
||||
[NotifyPropertyChangedFor(nameof(IsResolutionScaleOptionEnabled))]
|
||||
private RenderingMode selectedRenderingMode;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets whether the vertical sync is enabled.
|
||||
/// </summary>
|
||||
[ObservableProperty]
|
||||
private bool isVerticalSyncEnabled;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets whether the dynamic resolution is enabled.
|
||||
/// </summary>
|
||||
[ObservableProperty]
|
||||
[NotifyPropertyChangedFor(nameof(IsResolutionScaleOptionEnabled))]
|
||||
private bool isDynamicResolutionEnabled;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the currently selected resolution scale setting (as percentage value).
|
||||
/// </summary>
|
||||
[ObservableProperty]
|
||||
private int selectedResolutionScale;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the currently selected compute shader.
|
||||
/// </summary>
|
||||
[ObservableProperty]
|
||||
private ShaderRunnerViewModel selectedComputeShader;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets whether the rendering is currently paused.
|
||||
/// </summary>
|
||||
[ObservableProperty]
|
||||
private bool isRenderingPaused;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the available resolution scaling options (as percentage values).
|
||||
/// </summary>
|
||||
public IList<int> ResolutionScaleOptions { get; } = new[] { 25, 50, 75, 100 };
|
||||
|
||||
/// <summary>
|
||||
/// Gets the collection of available compute shader.
|
||||
/// </summary>
|
||||
public IReadOnlyList<ShaderRunnerViewModel> ComputeShaderOptions { get; } = new ShaderRunnerViewModel[]
|
||||
{
|
||||
new(
|
||||
nameof(ColorfulInfinity),
|
||||
new ShaderRunner<ColorfulInfinity>(static time => new((float)time.TotalSeconds)),
|
||||
new PixelShaderEffect.For<SwapChain.Shaders.D2D1.ColorfulInfinity>(static (time, width, height) => new((float)time.TotalSeconds, new int2(width, height)))),
|
||||
new(
|
||||
nameof(FractalTiling),
|
||||
new ShaderRunner<FractalTiling>(static time => new((float)time.TotalSeconds)),
|
||||
new PixelShaderEffect.For<SwapChain.Shaders.D2D1.FractalTiling>(static (time, width, height) => new((float)time.TotalSeconds, new int2(width, height)))),
|
||||
new(
|
||||
nameof(MengerJourney),
|
||||
new ShaderRunner<MengerJourney>(static time => new((float)time.TotalSeconds)),
|
||||
new PixelShaderEffect.For<SwapChain.Shaders.D2D1.MengerJourney>(static (time, width, height) => new((float)time.TotalSeconds, new int2(width, height)))),
|
||||
new(
|
||||
nameof(Octagrams),
|
||||
new ShaderRunner<Octagrams>(static time => new((float)time.TotalSeconds)),
|
||||
new PixelShaderEffect.For<SwapChain.Shaders.D2D1.Octagrams>(static (time, width, height) => new((float)time.TotalSeconds, new int2(width, height)))),
|
||||
new(
|
||||
nameof(ProteanClouds),
|
||||
new ShaderRunner<ProteanClouds>(static time => new((float)time.TotalSeconds)),
|
||||
new PixelShaderEffect.For<SwapChain.Shaders.D2D1.ProteanClouds>(static (time, width, height) => new((float)time.TotalSeconds, new int2(width, height)))),
|
||||
new(
|
||||
nameof(TwoTiledTruchet),
|
||||
new ShaderRunner<TwoTiledTruchet>(static time => new((float)time.TotalSeconds)),
|
||||
new PixelShaderEffect.For<SwapChain.Shaders.D2D1.TwoTiledTruchet>(static (time, width, height) => new((float)time.TotalSeconds, new int2(width, height)))),
|
||||
new(
|
||||
nameof(PyramidPattern),
|
||||
new ShaderRunner<PyramidPattern>(static time => new((float)time.TotalSeconds)),
|
||||
new PixelShaderEffect.For<SwapChain.Shaders.D2D1.PyramidPattern>(static (time, width, height) => new((float)time.TotalSeconds, new int2(width, height)))),
|
||||
new(
|
||||
nameof(TriangleGridContouring),
|
||||
new ShaderRunner<TriangleGridContouring>(static time => new((float)time.TotalSeconds)),
|
||||
new PixelShaderEffect.For<SwapChain.Shaders.D2D1.TriangleGridContouring>(static (time, width, height) => new((float)time.TotalSeconds, new int2(width, height)))),
|
||||
new(nameof(ContouredLayers), new ContouredLayersRunner(), new D2D1ContouredLayersEffect()),
|
||||
new(
|
||||
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.
|
||||
/// </summary>
|
||||
public bool IsResolutionScaleOptionEnabled => SelectedRenderingMode == RenderingMode.DirectX12 && !IsDynamicResolutionEnabled;
|
||||
|
||||
/// <summary>
|
||||
/// Sets <see cref="SelectedRenderingMode"/>.
|
||||
/// </summary>
|
||||
[RelayCommand]
|
||||
private void SetRenderingMode(RenderingMode renderingMode)
|
||||
{
|
||||
SelectedRenderingMode = renderingMode;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets <see cref="SelectedResolutionScale"/>.
|
||||
/// </summary>
|
||||
[RelayCommand]
|
||||
private void SetResolutionScale(int resolutionScale)
|
||||
{
|
||||
SelectedResolutionScale = resolutionScale;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Toggles <see cref="IsRenderingPaused"/>.
|
||||
/// </summary>
|
||||
[RelayCommand]
|
||||
private void ToggleRenderingPaused()
|
||||
{
|
||||
IsRenderingPaused = !IsRenderingPaused;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
partial void OnSelectedRenderingModeChanged(RenderingMode value)
|
||||
{
|
||||
this.analyticsService.Log(Event.SelectedRenderingModeChanged, (nameof(value), value));
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
partial void OnIsVerticalSyncEnabledChanged(bool value)
|
||||
{
|
||||
this.analyticsService.Log(Event.IsVerticalSyncEnabledChanged, (nameof(value), value));
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
partial void OnIsDynamicResolutionEnabledChanged(bool value)
|
||||
{
|
||||
this.analyticsService.Log(Event.IsDynamicResolutionEnabledChanged, (nameof(value), value));
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
partial void OnSelectedResolutionScaleChanged(int value)
|
||||
{
|
||||
this.analyticsService.Log(Event.SelectedResolutionScaleChanged, (nameof(value), value));
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
partial void OnSelectedComputeShaderChanging(ShaderRunnerViewModel? oldValue, ShaderRunnerViewModel newValue)
|
||||
{
|
||||
oldValue!.IsSelected = false;
|
||||
newValue.IsSelected = true;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
partial void OnSelectedComputeShaderChanged(ShaderRunnerViewModel value)
|
||||
{
|
||||
this.analyticsService.Log(Event.SelectedComputeShaderChanged, (nameof(value.ShaderType), value.ShaderType));
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
partial void OnIsRenderingPausedChanged(bool value)
|
||||
{
|
||||
this.analyticsService.Log(Event.IsRenderingPausedChanged, (nameof(value), value));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using ComputeSharp.SwapChain.Core.Shaders;
|
||||
#if WINDOWS_UWP
|
||||
using ComputeSharp.Uwp;
|
||||
#else
|
||||
using ComputeSharp.WinUI;
|
||||
#endif
|
||||
|
||||
namespace ComputeSharp.SwapChain.Core.ViewModels;
|
||||
|
||||
/// <summary>
|
||||
/// A viewmodel for a compute shader.
|
||||
/// </summary>
|
||||
public sealed partial class ShaderRunnerViewModel : ObservableObject
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="ShaderRunnerViewModel"/> instance.
|
||||
/// </summary>
|
||||
/// <param name="shaderType">The name of the shader type to execute.</param>
|
||||
/// <param name="shaderRunner">The <see cref="IShaderRunner"/> instance to execute.</param>
|
||||
/// <param name="pixelShaderEffect">The <see cref="Shaders.PixelShaderEffect"/> instance to execute.</param>
|
||||
public ShaderRunnerViewModel(string shaderType, IShaderRunner shaderRunner, PixelShaderEffect pixelShaderEffect)
|
||||
{
|
||||
ShaderType = shaderType;
|
||||
ShaderRunner = shaderRunner;
|
||||
PixelShaderEffect = pixelShaderEffect;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the name of the shader type to execute.
|
||||
/// </summary>
|
||||
public string ShaderType { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="IShaderRunner"/> instance to execute.
|
||||
/// </summary>
|
||||
public IShaderRunner ShaderRunner { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="PixelShaderEffect"/> instance to execute.
|
||||
/// </summary>
|
||||
public PixelShaderEffect PixelShaderEffect { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets whether the current shader is selected.
|
||||
/// </summary>
|
||||
[ObservableProperty]
|
||||
private bool isSelected;
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする