Added ContouredLayers shader to WinUI sample

このコミットが含まれているのは:
Sergio Pedri committed 2021-11-20 00:28:01 +01:00
1 親 1494e41a53
コミット a2d137d26b
7ファイル変更
+98 -3

検索条件に一致するファイルはありません

@@ -52,6 +52,9 @@
<Content Include="..\..\..\tests\ComputeSharp.Tests\Assets\ColorfulInfinity.png">
<Link>Images\Shaders\ColorfulInfinity.png</Link>
</Content>
<Content Include="..\..\..\tests\ComputeSharp.Tests\Assets\ContouredLayers.png">
<Link>Images\Shaders\ContouredLayers.png</Link>
</Content>
<Content Include="..\..\..\tests\ComputeSharp.Tests\Assets\ExtrudedTruchetPattern.png">
<Link>Images\Shaders\ExtrudedTruchetPattern.png</Link>
</Content>
@@ -76,8 +79,12 @@
<Content Include="..\..\..\tests\ComputeSharp.Tests\Assets\TriangleGridContouring.png">
<Link>Images\Shaders\TriangleGridContouring.png</Link>
</Content>
<Content Include="..\..\ComputeSharp.SwapChain\Textures\RustyMetal.png">
<Link>Images\Textures\RustyMetal.png</Link>
</Content>
<Content Include="Images\Misc\noise_low.png" />
<Content Include="Images\Shaders\Blurred\ColorfulInfinity.png" />
<Content Include="Images\Shaders\Blurred\ContouredLayers.png" />
<Content Include="Images\Shaders\Blurred\ExtrudedTruchetPattern.png" />
<Content Include="Images\Shaders\Blurred\FractalTiling.png" />
<Content Include="Images\Shaders\Blurred\MengerJourney.png" />
バイナリファイルは表示されません。

変更後

幅:  |  高さ:  |  サイズ: 351 KiB

@@ -11,6 +11,7 @@
<ItemGroup>
<Compile Include="..\..\ComputeSharp.SwapChain\Shaders\ColorfulInfinity.cs" Link="Shaders\ColorfulInfinity.cs" />
<Compile Include="..\..\ComputeSharp.SwapChain\Shaders\ContouredLayers.cs" Link="Shaders\ContouredLayers.cs" />
<Compile Include="..\..\ComputeSharp.SwapChain\Shaders\ExtrudedTruchetPattern.cs" Link="Shaders\ExtrudedTruchetPattern.cs" />
<Compile Include="..\..\ComputeSharp.SwapChain\Shaders\FractalTiling.cs" Link="Shaders\FractalTiling.cs" />
<Compile Include="..\..\ComputeSharp.SwapChain\Shaders\MengerJourney.cs" Link="Shaders\MengerJourney.cs" />
@@ -34,6 +35,7 @@
<PackageReference Include="Microsoft.ProjectReunion" Version="0.5.6" />
<PackageReference Include="Microsoft.ProjectReunion.Foundation" Version="0.5.6" />
<PackageReference Include="Microsoft.ProjectReunion.WinUI" Version="0.5.6" />
<PackageReference Include="SixLabors.ImageSharp" Version="1.0.3" />
<Manifest Include="$(ApplicationManifest)" />
</ItemGroup>
@@ -1,5 +1,6 @@
using System;
using ComputeSharp.SwapChain.WinUI.Models;
using ComputeSharp.SwapChain.WinUI.Shaders.Runners;
namespace ComputeSharp.SwapChain.WinUI.Converters
{
@@ -15,7 +16,16 @@ namespace ComputeSharp.SwapChain.WinUI.Converters
/// <returns>A <see cref="Uri"/> with the thumbnail image for <paramref name="value"/>.</returns>
public static Uri ConvertComputeShaderToThumbnailUri(ComputeShader value)
{
return new($"ms-appx:///Images/Shaders/{value.ShaderRunner.GetType().GenericTypeArguments[0].Name}.png");
if (value.ShaderRunner.GetType().IsGenericType)
{
return new($"ms-appx:///Images/Shaders/{value.ShaderRunner.GetType().GenericTypeArguments[0].Name}.png");
}
return value.ShaderRunner.GetType() switch
{
Type t when t == typeof(ContouredLayersRunner) => new($"ms-appx:///Images/Shaders/ContouredLayers.png"),
_ => throw new ArgumentException($"Invalid shader type: {value.ShaderRunner.GetType()}")
};
}
/// <summary>
@@ -25,7 +35,16 @@ namespace ComputeSharp.SwapChain.WinUI.Converters
/// <returns>A <see cref="Uri"/> with the background image for <paramref name="value"/>.</returns>
public static Uri ConvertComputeShaderToBackgroundUri(ComputeShader value)
{
return new($"ms-appx:///Images/Shaders/Blurred/{value.ShaderRunner.GetType().GenericTypeArguments[0].Name}.png");
if (value.ShaderRunner.GetType().IsGenericType)
{
return new($"ms-appx:///Images/Shaders/Blurred/{value.ShaderRunner.GetType().GenericTypeArguments[0].Name}.png");
}
return value.ShaderRunner.GetType() switch
{
Type t when t == typeof(ContouredLayersRunner) => new($"ms-appx:///Images/Shaders/Blurred/ContouredLayers.png"),
_ => throw new ArgumentException($"Invalid shader type: {value.ShaderRunner.GetType()}")
};
}
}
}
@@ -0,0 +1,32 @@
using System;
using System.Diagnostics.Contracts;
using System.Runtime.InteropServices;
using SixLabors.ImageSharp;
using ImageSharpRgba32 = SixLabors.ImageSharp.PixelFormats.Rgba32;
namespace ComputeSharp.SwapChain.WinUI.Extensions
{
/// <summary>
/// A helper class for loading images into resources.
/// </summary>
public static class ImagingExtensions
{
/// <summary>
/// Allocates a new <see cref="ReadOnlyTexture2D{T, TPixel}"/> instance with the specified texture data.
/// </summary>
/// <param name="device">The target <see cref="GraphicsDevice"/> instance to allocate the texture for.</param>
/// <param name="path">The path of the texture to load.</param>
/// <returns>A texture with the data from the image at the specified path.</returns>
[Pure]
public static ReadOnlyTexture2D<Rgba32, Float4> LoadTexture(this GraphicsDevice device, string path)
{
using Image<ImageSharpRgba32> image = Image.Load<ImageSharpRgba32>(path);
_ = image.TryGetSinglePixelSpan(out Span<ImageSharpRgba32> span);
Span<Rgba32> pixels = MemoryMarshal.Cast<ImageSharpRgba32, Rgba32>(span);
return device.AllocateReadOnlyTexture2D<Rgba32, Float4>(pixels, image.Width, image.Height);
}
}
}
@@ -0,0 +1,33 @@
using System;
using System.IO;
using ComputeSharp.SwapChain.Shaders;
using ComputeSharp.SwapChain.WinUI.Extensions;
using ComputeSharp.WinUI;
using Windows.ApplicationModel;
namespace ComputeSharp.SwapChain.WinUI.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 void Execute(IReadWriteTexture2D<Float4> texture, TimeSpan timespan)
{
if (this.texture is null)
{
string filename = Path.Combine(Package.Current.InstalledLocation.Path, "Images", "Textures", "RustyMetal.png");
this.texture = Gpu.Default.LoadTexture(filename);
}
Gpu.Default.ForEach(texture, new ContouredLayers((float)timespan.TotalSeconds, this.texture));
}
}
}
@@ -4,6 +4,7 @@ using ComputeSharp.SwapChain.WinUI.Models;
using ComputeSharp.WinUI;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using ComputeSharp.SwapChain.WinUI.Shaders.Runners;
namespace ComputeSharp.SwapChain.WinUI.ViewModels
{
@@ -67,7 +68,8 @@ namespace ComputeSharp.SwapChain.WinUI.ViewModels
new("Protean clouds", new ShaderRunner<ProteanClouds>(static time => new((float)time.TotalSeconds))),
new("Two tiled truchet", new ShaderRunner<TwoTiledTruchet>(static time => new((float)time.TotalSeconds))),
new("Pyramid pattern", new ShaderRunner<PyramidPattern>(static time => new((float)time.TotalSeconds))),
new("Triangle grid contouring", new ShaderRunner<TriangleGridContouring>(static time => new((float)time.TotalSeconds)))
new("Triangle grid contouring", new ShaderRunner<TriangleGridContouring>(static time => new((float)time.TotalSeconds))),
new("Contoured layers", new ContouredLayersRunner())
};
private ComputeShader selectedComputeShader;