Merge pull request #123 from Sergio0694/feature/wic-decoder

WIC-powered image encoding/decoding
このコミットが含まれているのは:
Sergio Pedri authored and GitHub committed 2021-05-12 11:58:53 +02:00
コミット a5f77cbf79
17ファイル変更
+686 -82

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

-4
ファイルの表示
@@ -5,10 +5,6 @@
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="SixLabors.ImageSharp" Version="1.0.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\ComputeSharp\ComputeSharp.csproj" />
<ProjectReference Include="..\..\src\ComputeSharp.SourceGenerators\ComputeSharp.SourceGenerators.csproj"
+1 -12
ファイルの表示
@@ -3,11 +3,8 @@ using System.Diagnostics.Contracts;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using ComputeSharp.SwapChain.Backend;
using ComputeSharp.SwapChain.Shaders;
using SixLabors.ImageSharp;
using ImageSharpRgba32 = SixLabors.ImageSharp.PixelFormats.Rgba32;
namespace ComputeSharp.SwapChain
{
@@ -71,15 +68,7 @@ namespace ComputeSharp.SwapChain
{
string filename = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Textures", $"{name}.png");
using Image<ImageSharpRgba32> image = Image.Load<ImageSharpRgba32>(filename);
ReadOnlyTexture2D<Rgba32, Float4> texture = Gpu.Default.AllocateReadOnlyTexture2D<Rgba32, Float4>(image.Width, image.Height);
image.TryGetSinglePixelSpan(out Span<ImageSharpRgba32> span);
texture.CopyFrom(MemoryMarshal.Cast<ImageSharpRgba32, Rgba32>(span));
return texture;
return Gpu.Default.AllocateReadOnlyTexture2D<Rgba32, Float4>(filename);
}
}
}
+109 -20
ファイルの表示
@@ -1,6 +1,7 @@
using System;
using System.Diagnostics.Contracts;
using ComputeSharp.__Internals;
using ComputeSharp.Graphics.Helpers;
using ComputeSharp.Resources;
using ComputeSharp.Shaders;
@@ -239,7 +240,7 @@ namespace ComputeSharp
/// <param name="width">The width of the texture.</param>
/// <param name="height">The height of the texture.</param>
/// <param name="allocationMode">The allocation mode to use for the new resource.</param>
/// <returns>A zeroed <see cref="ReadOnlyTexture2D{T,TPixel}"/> instance of size [<paramref name="width"/>, <paramref name="height"/>].</returns>
/// <returns>A zeroed <see cref="ReadOnlyTexture2D{T, TPixel}"/> instance of size [<paramref name="width"/>, <paramref name="height"/>].</returns>
[Pure]
public static ReadOnlyTexture2D<T, TPixel> AllocateReadOnlyTexture2D<T, TPixel>(this GraphicsDevice device, int width, int height, AllocationMode allocationMode = AllocationMode.Default)
where T : unmanaged, IUnorm<TPixel>
@@ -257,7 +258,7 @@ namespace ComputeSharp
/// <param name="source">The input <typeparamref name="T"/> array with the data to copy on the allocated texture.</param>
/// <param name="width">The width of the texture.</param>
/// <param name="height">The height of the texture.</param>
/// <returns>A <see cref="ReadOnlyTexture2D{T,TPixel}"/> instance with the contents of the input array.</returns>
/// <returns>A <see cref="ReadOnlyTexture2D{T, TPixel}"/> instance with the contents of the input array.</returns>
[Pure]
public static ReadOnlyTexture2D<T, TPixel> AllocateReadOnlyTexture2D<T, TPixel>(this GraphicsDevice device, T[] source, int width, int height)
where T : unmanaged, IUnorm<TPixel>
@@ -276,7 +277,7 @@ namespace ComputeSharp
/// <param name="offset">The starting offset within <paramref name="source"/> to read data from.</param>
/// <param name="width">The width of the texture.</param>
/// <param name="height">The height of the texture.</param>
/// <returns>A <see cref="ReadOnlyTexture2D{T,TPixel}"/> instance with the contents of the input array.</returns>
/// <returns>A <see cref="ReadOnlyTexture2D{T, TPixel}"/> instance with the contents of the input array.</returns>
[Pure]
public static ReadOnlyTexture2D<T, TPixel> AllocateReadOnlyTexture2D<T, TPixel>(this GraphicsDevice device, T[] source, int offset, int width, int height)
where T : unmanaged, IUnorm<TPixel>
@@ -292,7 +293,7 @@ namespace ComputeSharp
/// <typeparam name="TPixel">The type of pixels used on the GPU side.</typeparam>
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the texture.</param>
/// <param name="source">The input <typeparamref name="T"/> array with the data to copy on the allocated texture.</param>
/// <returns>A read write <see cref="ReadOnlyTexture2D{T,TPixel}"/> instance with the contents of the input array.</returns>
/// <returns>A read write <see cref="ReadOnlyTexture2D{T, TPixel}"/> instance with the contents of the input array.</returns>
[Pure]
public static ReadOnlyTexture2D<T, TPixel> AllocateReadOnlyTexture2D<T, TPixel>(this GraphicsDevice device, T[,] source)
where T : unmanaged, IUnorm<TPixel>
@@ -314,7 +315,7 @@ namespace ComputeSharp
/// <param name="source">The input <see cref="ReadOnlySpan{T}"/> with the data to copy on the allocated texture.</param>
/// <param name="width">The width of the texture.</param>
/// <param name="height">The height of the texture.</param>
/// <returns>A <see cref="ReadOnlyTexture2D{T,TPixel}"/> instance with the contents of the input <see cref="ReadOnlySpan{T}"/>.</returns>
/// <returns>A <see cref="ReadOnlyTexture2D{T, TPixel}"/> instance with the contents of the input <see cref="ReadOnlySpan{T}"/>.</returns>
[Pure]
public static ReadOnlyTexture2D<T, TPixel> AllocateReadOnlyTexture2D<T, TPixel>(this GraphicsDevice device, ReadOnlySpan<T> source, int width, int height)
where T : unmanaged, IUnorm<TPixel>
@@ -327,6 +328,50 @@ namespace ComputeSharp
return texture;
}
/// <summary>
/// Allocates a new readonly 2D texture with the contents of the specified file.
/// </summary>
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
/// <typeparam name="TPixel">The type of pixels used on the GPU side.</typeparam>
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the texture.</param>
/// <param name="filename">The filename of the image file to load and decode into the texture.</param>
/// <returns>A <see cref="ReadOnlyTexture2D{T, TPixel}"/> instance with the contents of the specified file.</returns>
[Pure]
public static ReadOnlyTexture2D<T, TPixel> AllocateReadOnlyTexture2D<T, TPixel>(this GraphicsDevice device, ReadOnlySpan<char> filename)
where T : unmanaged, IUnorm<TPixel>
where TPixel : unmanaged
{
using UploadTexture2D<T> upload = WICHelper.Instance.LoadTexture<T>(device, filename);
ReadOnlyTexture2D<T, TPixel> texture = device.AllocateReadOnlyTexture2D<T, TPixel>(upload.Width, upload.Height);
upload.CopyTo(texture);
return texture;
}
/// <summary>
/// Allocates a new readonly 2D texture with the contents of the specified buffer.
/// </summary>
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
/// <typeparam name="TPixel">The type of pixels used on the GPU side.</typeparam>
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the texture.</param>
/// <param name="span">The buffer with the image data to load and decode into the texture.</param>
/// <returns>A <see cref="ReadOnlyTexture2D{T, TPixel}"/> instance with the contents of the specified file.</returns>
[Pure]
public static ReadOnlyTexture2D<T, TPixel> AllocateReadOnlyTexture2D<T, TPixel>(this GraphicsDevice device, ReadOnlySpan<byte> span)
where T : unmanaged, IUnorm<TPixel>
where TPixel : unmanaged
{
using UploadTexture2D<T> upload = WICHelper.Instance.LoadTexture<T>(device, span);
ReadOnlyTexture2D<T, TPixel> texture = device.AllocateReadOnlyTexture2D<T, TPixel>(upload.Width, upload.Height);
upload.CopyTo(texture);
return texture;
}
/// <summary>
/// Allocates a new readonly 3D texture with the specified parameters.
/// </summary>
@@ -432,7 +477,7 @@ namespace ComputeSharp
/// <param name="height">The height of the texture.</param>
/// <param name="depth">The depth of the texture.</param>
/// <param name="allocationMode">The allocation mode to use for the new resource.</param>
/// <returns>A zeroed <see cref="ReadOnlyTexture3D{T,TPixel}"/> instance of size [<paramref name="width"/>, <paramref name="height"/>, <paramref name="depth"/>].</returns>
/// <returns>A zeroed <see cref="ReadOnlyTexture3D{T, TPixel}"/> instance of size [<paramref name="width"/>, <paramref name="height"/>, <paramref name="depth"/>].</returns>
[Pure]
public static ReadOnlyTexture3D<T, TPixel> AllocateReadOnlyTexture3D<T, TPixel>(this GraphicsDevice device, int width, int height, int depth, AllocationMode allocationMode = AllocationMode.Default)
where T : unmanaged, IUnorm<TPixel>
@@ -451,7 +496,7 @@ namespace ComputeSharp
/// <param name="width">The width of the texture.</param>
/// <param name="height">The height of the texture.</param>
/// <param name="depth">The depth of the texture.</param>
/// <returns>A <see cref="ReadOnlyTexture3D{T,TPixel}"/> instance with the contents of the input array.</returns>
/// <returns>A <see cref="ReadOnlyTexture3D{T, TPixel}"/> instance with the contents of the input array.</returns>
[Pure]
public static ReadOnlyTexture3D<T, TPixel> AllocateReadOnlyTexture3D<T, TPixel>(this GraphicsDevice device, T[] source, int width, int height, int depth)
where T : unmanaged, IUnorm<TPixel>
@@ -471,7 +516,7 @@ namespace ComputeSharp
/// <param name="width">The width of the texture.</param>
/// <param name="height">The height of the texture.</param>
/// <param name="depth">The depth of the texture.</param>
/// <returns>A <see cref="ReadOnlyTexture3D{T,TPixel}"/> instance with the contents of the input array.</returns>
/// <returns>A <see cref="ReadOnlyTexture3D{T, TPixel}"/> instance with the contents of the input array.</returns>
[Pure]
public static ReadOnlyTexture3D<T, TPixel> AllocateReadOnlyTexture3D<T, TPixel>(this GraphicsDevice device, T[] source, int offset, int width, int height, int depth)
where T : unmanaged, IUnorm<TPixel>
@@ -487,7 +532,7 @@ namespace ComputeSharp
/// <typeparam name="TPixel">The type of pixels used on the GPU side.</typeparam>
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the texture.</param>
/// <param name="source">The input <typeparamref name="T"/> array with the data to copy on the allocated texture.</param>
/// <returns>A read write <see cref="ReadOnlyTexture3D{T,TPixel}"/> instance with the contents of the input array.</returns>
/// <returns>A read write <see cref="ReadOnlyTexture3D{T, TPixel}"/> instance with the contents of the input array.</returns>
/// <remarks>
/// The source 3D array needs to have each 2D plane stacked on the depth axis.
/// That is, the expected layout of the input array has to be [depth, height, width].
@@ -514,7 +559,7 @@ namespace ComputeSharp
/// <param name="width">The width of the texture.</param>
/// <param name="height">The height of the texture.</param>
/// <param name="depth">The depth of the texture.</param>
/// <returns>A <see cref="ReadOnlyTexture3D{T,TPixel}"/> instance with the contents of the input <see cref="ReadOnlySpan{T}"/>.</returns>
/// <returns>A <see cref="ReadOnlyTexture3D{T, TPixel}"/> instance with the contents of the input <see cref="ReadOnlySpan{T}"/>.</returns>
[Pure]
public static ReadOnlyTexture3D<T, TPixel> AllocateReadOnlyTexture3D<T, TPixel>(this GraphicsDevice device, ReadOnlySpan<T> source, int width, int height, int depth)
where T : unmanaged, IUnorm<TPixel>
@@ -688,7 +733,7 @@ namespace ComputeSharp
/// <param name="width">The width of the texture.</param>
/// <param name="height">The height of the texture.</param>
/// <param name="allocationMode">The allocation mode to use for the new resource.</param>
/// <returns>A zeroed <see cref="ReadWriteTexture2D{T,TPixel}"/> instance of size [<paramref name="width"/>, <paramref name="height"/>].</returns>
/// <returns>A zeroed <see cref="ReadWriteTexture2D{T, TPixel}"/> instance of size [<paramref name="width"/>, <paramref name="height"/>].</returns>
[Pure]
public static ReadWriteTexture2D<T, TPixel> AllocateReadWriteTexture2D<T, TPixel>(this GraphicsDevice device, int width, int height, AllocationMode allocationMode = AllocationMode.Default)
where T : unmanaged, IUnorm<TPixel>
@@ -706,7 +751,7 @@ namespace ComputeSharp
/// <param name="source">The input <typeparamref name="T"/> array with the data to copy on the allocated texture.</param>
/// <param name="width">The width of the texture.</param>
/// <param name="height">The height of the texture.</param>
/// <returns>A <see cref="ReadWriteTexture2D{T,TPixel}"/> instance with the contents of the input array.</returns>
/// <returns>A <see cref="ReadWriteTexture2D{T, TPixel}"/> instance with the contents of the input array.</returns>
[Pure]
public static ReadWriteTexture2D<T, TPixel> AllocateReadWriteTexture2D<T, TPixel>(this GraphicsDevice device, T[] source, int width, int height)
where T : unmanaged, IUnorm<TPixel>
@@ -725,7 +770,7 @@ namespace ComputeSharp
/// <param name="offset">The starting offset within <paramref name="source"/> to read data from.</param>
/// <param name="width">The width of the texture.</param>
/// <param name="height">The height of the texture.</param>
/// <returns>A <see cref="ReadWriteTexture2D{T,TPixel}"/> instance with the contents of the input array.</returns>
/// <returns>A <see cref="ReadWriteTexture2D{T, TPixel}"/> instance with the contents of the input array.</returns>
[Pure]
public static ReadWriteTexture2D<T, TPixel> AllocateReadWriteTexture2D<T, TPixel>(this GraphicsDevice device, T[] source, int offset, int width, int height)
where T : unmanaged, IUnorm<TPixel>
@@ -741,7 +786,7 @@ namespace ComputeSharp
/// <typeparam name="TPixel">The type of pixels used on the GPU side.</typeparam>
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the texture.</param>
/// <param name="source">The input <typeparamref name="T"/> array with the data to copy on the allocated texture.</param>
/// <returns>A read write <see cref="ReadWriteTexture2D{T,TPixel}"/> instance with the contents of the input array.</returns>
/// <returns>A read write <see cref="ReadWriteTexture2D{T, TPixel}"/> instance with the contents of the input array.</returns>
[Pure]
public static ReadWriteTexture2D<T, TPixel> AllocateReadWriteTexture2D<T, TPixel>(this GraphicsDevice device, T[,] source)
where T : unmanaged, IUnorm<TPixel>
@@ -763,7 +808,7 @@ namespace ComputeSharp
/// <param name="source">The input <see cref="ReadOnlySpan{T}"/> with the data to copy on the allocated texture.</param>
/// <param name="width">The width of the texture.</param>
/// <param name="height">The height of the texture.</param>
/// <returns>A <see cref="ReadWriteTexture2D{T,TPixel}"/> instance with the contents of the input <see cref="ReadOnlySpan{T}"/>.</returns>
/// <returns>A <see cref="ReadWriteTexture2D{T, TPixel}"/> instance with the contents of the input <see cref="ReadOnlySpan{T}"/>.</returns>
[Pure]
public static ReadWriteTexture2D<T, TPixel> AllocateReadWriteTexture2D<T, TPixel>(this GraphicsDevice device, ReadOnlySpan<T> source, int width, int height)
where T : unmanaged, IUnorm<TPixel>
@@ -776,6 +821,50 @@ namespace ComputeSharp
return texture;
}
/// <summary>
/// Allocates a new writeable 2D texture with the contents of the specified file.
/// </summary>
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
/// <typeparam name="TPixel">The type of pixels used on the GPU side.</typeparam>
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the texture.</param>
/// <param name="filename">The filename of the image file to load and decode into the texture.</param>
/// <returns>A <see cref="ReadWriteTexture2D{T, TPixel}"/> instance with the contents of the specified file.</returns>
[Pure]
public static ReadWriteTexture2D<T, TPixel> AllocateReadWriteTexture2D<T, TPixel>(this GraphicsDevice device, ReadOnlySpan<char> filename)
where T : unmanaged, IUnorm<TPixel>
where TPixel : unmanaged
{
using UploadTexture2D<T> upload = WICHelper.Instance.LoadTexture<T>(device, filename);
ReadWriteTexture2D<T, TPixel> texture = device.AllocateReadWriteTexture2D<T, TPixel>(upload.Width, upload.Height);
upload.CopyTo(texture);
return texture;
}
/// <summary>
/// Allocates a new writeable 2D texture with the contents of the specified buffer.
/// </summary>
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
/// <typeparam name="TPixel">The type of pixels used on the GPU side.</typeparam>
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the texture.</param>
/// <param name="span">The buffer with the image data to load and decode into the texture.</param>
/// <returns>A <see cref="ReadWriteTexture2D{T, TPixel}"/> instance with the contents of the specified file.</returns>
[Pure]
public static ReadWriteTexture2D<T, TPixel> AllocateReadWriteTexture2D<T, TPixel>(this GraphicsDevice device, ReadOnlySpan<byte> span)
where T : unmanaged, IUnorm<TPixel>
where TPixel : unmanaged
{
using UploadTexture2D<T> upload = WICHelper.Instance.LoadTexture<T>(device, span);
ReadWriteTexture2D<T, TPixel> texture = device.AllocateReadWriteTexture2D<T, TPixel>(upload.Width, upload.Height);
upload.CopyTo(texture);
return texture;
}
/// <summary>
/// Allocates a new writeable 3D texture with the specified parameters.
/// </summary>
@@ -881,7 +970,7 @@ namespace ComputeSharp
/// <param name="height">The height of the texture.</param>
/// <param name="depth">The depth of the texture.</param>
/// <param name="allocationMode">The allocation mode to use for the new resource.</param>
/// <returns>A <see cref="ReadWriteTexture3D{T,TPixel}"/> instance of size [<paramref name="width"/>, <paramref name="height"/>, <paramref name="depth"/>].</returns>
/// <returns>A <see cref="ReadWriteTexture3D{T, TPixel}"/> instance of size [<paramref name="width"/>, <paramref name="height"/>, <paramref name="depth"/>].</returns>
[Pure]
public static ReadWriteTexture3D<T, TPixel> AllocateReadWriteTexture3D<T, TPixel>(this GraphicsDevice device, int width, int height, int depth, AllocationMode allocationMode = AllocationMode.Default)
where T : unmanaged, IUnorm<TPixel>
@@ -900,7 +989,7 @@ namespace ComputeSharp
/// <param name="width">The width of the texture.</param>
/// <param name="height">The height of the texture.</param>
/// <param name="depth">The depth of the texture.</param>
/// <returns>A <see cref="ReadWriteTexture3D{T,TPixel}"/> instance with the contents of the input array.</returns>
/// <returns>A <see cref="ReadWriteTexture3D{T, TPixel}"/> instance with the contents of the input array.</returns>
[Pure]
public static ReadWriteTexture3D<T, TPixel> AllocateReadWriteTexture3D<T, TPixel>(this GraphicsDevice device, T[] source, int width, int height, int depth)
where T : unmanaged, IUnorm<TPixel>
@@ -920,7 +1009,7 @@ namespace ComputeSharp
/// <param name="width">The width of the texture.</param>
/// <param name="height">The height of the texture.</param>
/// <param name="depth">The depth of the texture.</param>
/// <returns>A <see cref="ReadWriteTexture3D{T,TPixel}"/> instance with the contents of the input array.</returns>
/// <returns>A <see cref="ReadWriteTexture3D{T, TPixel}"/> instance with the contents of the input array.</returns>
[Pure]
public static ReadWriteTexture3D<T, TPixel> AllocateReadWriteTexture3D<T, TPixel>(this GraphicsDevice device, T[] source, int offset, int width, int height, int depth)
where T : unmanaged, IUnorm<TPixel>
@@ -936,7 +1025,7 @@ namespace ComputeSharp
/// <typeparam name="TPixel">The type of pixels used on the GPU side.</typeparam>
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the texture.</param>
/// <param name="source">The input <typeparamref name="T"/> array with the data to copy on the allocated texture.</param>
/// <returns>A read write <see cref="ReadWriteTexture3D{T,TPixel}"/> instance with the contents of the input array.</returns>
/// <returns>A read write <see cref="ReadWriteTexture3D{T, TPixel}"/> instance with the contents of the input array.</returns>
/// <remarks>
/// The source 3D array needs to have each 2D plane stacked on the depth axis.
/// That is, the expected layout of the input array has to be [depth, height, width].
@@ -963,7 +1052,7 @@ namespace ComputeSharp
/// <param name="width">The width of the texture.</param>
/// <param name="height">The height of the texture.</param>
/// <param name="depth">The depth of the texture.</param>
/// <returns>A <see cref="ReadWriteTexture3D{T,TPixel}"/> instance with the contents of the input <see cref="ReadOnlySpan{T}"/>.</returns>
/// <returns>A <see cref="ReadWriteTexture3D{T, TPixel}"/> instance with the contents of the input <see cref="ReadOnlySpan{T}"/>.</returns>
[Pure]
public static ReadWriteTexture3D<T, TPixel> AllocateReadWriteTexture3D<T, TPixel>(this GraphicsDevice device, ReadOnlySpan<T> source, int width, int height, int depth)
where T : unmanaged, IUnorm<TPixel>
+23
ファイルの表示
@@ -0,0 +1,23 @@
using System;
using ComputeSharp.Graphics.Helpers;
namespace ComputeSharp
{
/// <summary>
/// A <see langword="class"/> that contains extension methods for the <see cref="ReadBackTexture2D{T}"/> type.
/// </summary>
public static class ReadBackTexture2DExtensions
{
/// <summary>
/// Saves a texture to a specified file.
/// </summary>
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
/// <param name="texture">The texture to save to an image.</param>
/// <param name="filename">The filename of the image file to save.</param>
public static void Save<T>(this ReadBackTexture2D<T> texture, ReadOnlySpan<char> filename)
where T : unmanaged
{
WICHelper.Instance.SaveTexture(texture.View, filename);
}
}
}
+17
ファイルの表示
@@ -1,6 +1,7 @@
using System;
using System.Diagnostics.Contracts;
using System.Runtime.InteropServices;
using ComputeSharp.Graphics.Helpers;
using ComputeSharp.Resources;
using Microsoft.Toolkit.Diagnostics;
@@ -338,5 +339,21 @@ namespace ComputeSharp
{
texture.CopyFrom(ref MemoryMarshal.GetReference(source), source.Length, x, y, width, height);
}
/// <summary>
/// Saves a texture to a specified file.
/// </summary>
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
/// <param name="texture">The texture to save to an image.</param>
/// <param name="filename">The filename of the image file to save.</param>
public static void Save<T>(this Texture2D<T> texture, ReadOnlySpan<char> filename)
where T : unmanaged
{
using ReadBackTexture2D<T> readback = texture.GraphicsDevice.AllocateReadBackTexture2D<T>(texture.Width, texture.Height);
texture.CopyTo(readback);
WICHelper.Instance.SaveTexture(readback.View, filename);
}
}
}
+132
ファイルの表示
@@ -0,0 +1,132 @@
using System;
using System.Diagnostics.Contracts;
using System.IO;
using System.Runtime.CompilerServices;
using Microsoft.Toolkit.Diagnostics;
using FX = TerraFX.Interop.Windows;
namespace ComputeSharp.Graphics.Helpers
{
/// <summary>
/// A helper type with utility methods for WIC formats.
/// </summary>
internal static class WICFormatHelper
{
/// <summary>
/// Gets the appropriate WIC format <see cref="Guid"/> value for the input type argument.
/// </summary>
/// <typeparam name="T">The input type argument to get the corresponding WIC format <see cref="Guid"/>.</typeparam>
/// <returns>The WIC format <see cref="Guid"/> value corresponding to <typeparamref name="T"/>.</returns>
/// <exception cref="ArgumentException">Thrown when the input type <typeparamref name="T"/> is not supported.</exception>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Guid GetForType<T>()
where T : unmanaged
{
if (typeof(T) == typeof(Bgra32)) return FX.GUID_WICPixelFormat32bppBGRA;
else if (typeof(T) == typeof(Rgba32)) return FX.GUID_WICPixelFormat32bppRGBA;
else if (typeof(T) == typeof(Rgba64)) return FX.GUID_WICPixelFormat64bppRGBA;
else if (typeof(T) == typeof(R8)) return FX.GUID_WICPixelFormat8bppGray;
else if (typeof(T) == typeof(R16)) return FX.GUID_WICPixelFormat16bppGray;
else return ThrowHelper.ThrowArgumentException<Guid>("Invalid texture type");
}
/// <summary>
/// Gets the appropriate WIC format <see cref="Guid"/> value for the input type argument.
/// </summary>
/// <typeparam name="T">The input type argument to get the corresponding WIC format <see cref="Guid"/>.</typeparam>
/// <returns>The WIC format <see cref="Guid"/> value corresponding to <typeparamref name="T"/>.</returns>
/// <exception cref="ArgumentException">Thrown when the input type <typeparamref name="T"/> is not supported.</exception>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool TryGetIntermediateFormatForType<T>(Guid containerFormat, out Guid intermediateFormat)
where T : unmanaged
{
if (containerFormat == FX.GUID_ContainerFormatBmp)
{
if (typeof(T) == typeof(Bgra32)) intermediateFormat = default;
else if (typeof(T) == typeof(Rgba32)) intermediateFormat = FX.GUID_WICPixelFormat32bppBGRA;
else if (typeof(T) == typeof(Rgba64)) intermediateFormat = FX.GUID_WICPixelFormat32bppBGRA;
else if (typeof(T) == typeof(R8)) intermediateFormat = FX.GUID_WICPixelFormat24bppBGR;
else if (typeof(T) == typeof(R16)) intermediateFormat = FX.GUID_WICPixelFormat24bppBGR;
else intermediateFormat = ThrowHelper.ThrowArgumentException<Guid>("Invalid texture type");
}
else if (containerFormat == FX.GUID_ContainerFormatPng)
{
if (typeof(T) == typeof(Bgra32)) intermediateFormat = default;
else if (typeof(T) == typeof(Rgba32)) intermediateFormat = FX.GUID_WICPixelFormat32bppBGRA;
else if (typeof(T) == typeof(Rgba64)) intermediateFormat = default;
else if (typeof(T) == typeof(R8)) intermediateFormat = default;
else if (typeof(T) == typeof(R16)) intermediateFormat = FX.GUID_WICPixelFormat8bppGray;
else intermediateFormat = ThrowHelper.ThrowArgumentException<Guid>("Invalid texture type");
}
else if (containerFormat == FX.GUID_ContainerFormatJpeg)
{
if (typeof(T) == typeof(Bgra32)) intermediateFormat = FX.GUID_WICPixelFormat24bppBGR;
else if (typeof(T) == typeof(Rgba32)) intermediateFormat = FX.GUID_WICPixelFormat24bppBGR;
else if (typeof(T) == typeof(Rgba64)) intermediateFormat = FX.GUID_WICPixelFormat24bppBGR;
else if (typeof(T) == typeof(R8)) intermediateFormat = default;
else if (typeof(T) == typeof(R16)) intermediateFormat = FX.GUID_WICPixelFormat8bppGray;
else intermediateFormat = ThrowHelper.ThrowArgumentException<Guid>("Invalid texture type");
}
else if (containerFormat == FX.GUID_ContainerFormatWmp)
{
if (typeof(T) == typeof(Bgra32)) intermediateFormat = default;
else if (typeof(T) == typeof(Rgba32)) intermediateFormat = FX.GUID_WICPixelFormat32bppBGRA;
else if (typeof(T) == typeof(Rgba64)) intermediateFormat = default;
else if (typeof(T) == typeof(R8)) intermediateFormat = default;
else if (typeof(T) == typeof(R16)) intermediateFormat = default;
else intermediateFormat = ThrowHelper.ThrowArgumentException<Guid>("Invalid texture type");
}
else if (containerFormat == FX.GUID_ContainerFormatTiff)
{
if (typeof(T) == typeof(Bgra32)) intermediateFormat = default;
else if (typeof(T) == typeof(Rgba32)) intermediateFormat = FX.GUID_WICPixelFormat32bppBGRA;
else if (typeof(T) == typeof(Rgba64)) intermediateFormat = default;
else if (typeof(T) == typeof(R8)) intermediateFormat = default;
else if (typeof(T) == typeof(R16)) intermediateFormat = default;
else intermediateFormat = ThrowHelper.ThrowArgumentException<Guid>("Invalid texture type");
}
else if (containerFormat == FX.GUID_ContainerFormatDds)
{
if (typeof(T) == typeof(Bgra32)) intermediateFormat = default;
else if (typeof(T) == typeof(Rgba32)) intermediateFormat = FX.GUID_WICPixelFormat32bppBGRA;
else if (typeof(T) == typeof(Rgba64)) intermediateFormat = FX.GUID_WICPixelFormat32bppBGRA;
else if (typeof(T) == typeof(R8)) intermediateFormat = FX.GUID_WICPixelFormat32bppBGRA;
else if (typeof(T) == typeof(R16)) intermediateFormat = FX.GUID_WICPixelFormat32bppBGRA;
else intermediateFormat = ThrowHelper.ThrowArgumentException<Guid>("Invalid texture type");
}
else
{
intermediateFormat = ThrowHelper.ThrowArgumentException<Guid>("Invalid container format");
}
return intermediateFormat != default;
}
/// <summary>
/// Gets the appropriate WIC container format <see cref="Guid"/> value for the input filename.
/// </summary>
/// <param name="filename">The target filename to get the container format for.</param>
/// <returns>The WIC format container <see cref="Guid"/> value matching <paramref name="filename"/>.</returns>
/// <exception cref="ArgumentException">Thrown when the input filename doesn't have a valid file extension.</exception>
[Pure]
public static Guid GetForFilename(ReadOnlySpan<char> filename)
{
switch (Path.GetExtension(filename).ToString())
{
case ".bmp": return FX.GUID_ContainerFormatBmp;
case ".png": return FX.GUID_ContainerFormatPng;
case ".jpg":
case ".jpeg": return FX.GUID_ContainerFormatJpeg;
case ".jxr":
case ".hdp":
case ".wdp":
case ".wmp": return FX.GUID_ContainerFormatWmp;
case ".tiff": return FX.GUID_ContainerFormatTiff;
case ".dds": return FX.GUID_ContainerFormatDds;
default: return ThrowHelper.ThrowArgumentException<Guid>("Invalid filename");
}
}
}
}
+258
ファイルの表示
@@ -0,0 +1,258 @@
using System;
using System.Diagnostics.Contracts;
using ComputeSharp.Core.Extensions;
using TerraFX.Interop;
using FX = TerraFX.Interop.Windows;
namespace ComputeSharp.Graphics.Helpers
{
/// <summary>
/// A <see langword="class"/> that uses the WIC APIs to load and decode images.
/// </summary>
internal sealed unsafe class WICHelper
{
/// <summary>
/// The <see cref="IWICImagingFactory2"/> instance to use to create decoders.
/// </summary>
private readonly ComPtr<IWICImagingFactory2> wicImagingFactory2;
/// <summary>
/// Creates a new <see cref="WICHelper"/> instance.
/// </summary>
private WICHelper()
{
using ComPtr<IWICImagingFactory2> wicImagingFactory2 = default;
Guid wicImagingFactor2Clsid = FX.CLSID_WICImagingFactory2;
FX.CoCreateInstance(
&wicImagingFactor2Clsid,
null,
(uint)CLSCTX.CLSCTX_INPROC_SERVER,
FX.__uuidof<IWICImagingFactory2>(),
wicImagingFactory2.GetVoidAddressOf()).Assert();
this.wicImagingFactory2 = wicImagingFactory2.Move();
}
/// <summary>
/// Destroys the current <see cref="WICHelper"/> instance.
/// </summary>
~WICHelper()
{
this.wicImagingFactory2.Dispose();
}
/// <summary>
/// Gets a <see cref="WICHelper"/> instance to use.
/// </summary>
public static WICHelper Instance { get; } = new();
/// <summary>
/// Loads an <see cref="UploadTexture2D{T}"/> from a specified file.
/// </summary>
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the texture.</param>
/// <param name="filename">The filename of the image file to load and decode into the texture.</param>
/// <returns>An <see cref="UploadTexture2D{T}"/> instance with the contents of the specified file.</returns>
[Pure]
public UploadTexture2D<T> LoadTexture<T>(GraphicsDevice device, ReadOnlySpan<char> filename)
where T : unmanaged
{
using ComPtr<IWICBitmapDecoder> wicBitmapDecoder = default;
// Get the bitmap decoder for the target file
fixed (char* p = filename)
{
this.wicImagingFactory2.Get()->CreateDecoderFromFilename(
(ushort*)p,
null,
FX.GENERIC_READ,
WICDecodeOptions.WICDecodeMetadataCacheOnDemand,
wicBitmapDecoder.GetAddressOf()).Assert();
}
return LoadTexture<T>(device, wicBitmapDecoder.Get());
}
/// <summary>
/// Loads an <see cref="UploadTexture2D{T}"/> from a specified buffer.
/// </summary>
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the texture.</param>
/// <param name="span">The buffer with the image data to load and decode into the texture.</param>
/// <returns>An <see cref="UploadTexture2D{T}"/> instance with the contents of the specified file.</returns>
[Pure]
public UploadTexture2D<T> LoadTexture<T>(GraphicsDevice device, ReadOnlySpan<byte> span)
where T : unmanaged
{
using ComPtr<IWICBitmapDecoder> wicBitmapDecoder = default;
using ComPtr<IWICStream> wicStream = default;
// Get the bitmap decoder for the target buffer
fixed (byte* p = span)
{
this.wicImagingFactory2.Get()->CreateStream(wicStream.GetAddressOf()).Assert();
wicStream.Get()->InitializeFromMemory(p, (uint)span.Length).Assert();
this.wicImagingFactory2.Get()->CreateDecoderFromStream(
(IStream*)wicStream.Get(),
null,
WICDecodeOptions.WICDecodeMetadataCacheOnDemand,
wicBitmapDecoder.GetAddressOf()).Assert();
return LoadTexture<T>(device, wicBitmapDecoder.Get());
}
}
/// <summary>
/// Saves a texture to a specified file.
/// </summary>
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
/// <param name="texture">The texture data to save to an image.</param>
/// <param name="filename">The filename of the image file to save.</param>
public void SaveTexture<T>(TextureView2D<T> texture, ReadOnlySpan<char> filename)
where T : unmanaged
{
using ComPtr<IWICBitmapEncoder> wicBitmapEncoder = default;
Guid containerGuid = WICFormatHelper.GetForFilename(filename);
// Create the image encoder
this.wicImagingFactory2.Get()->CreateEncoder(
&containerGuid,
null,
wicBitmapEncoder.GetAddressOf()).Assert();
using ComPtr<IWICStream> wicStream = default;
// Create and initialize a stream to the target file
this.wicImagingFactory2.Get()->CreateStream(wicStream.GetAddressOf()).Assert();
fixed (char* p = filename)
{
wicStream.Get()->InitializeFromFilename(
(ushort*)p,
FX.GENERIC_WRITE);
}
// Initialize the encoder
wicBitmapEncoder.Get()->Initialize(
(IStream*)wicStream.Get(),
WICBitmapEncoderCacheOption.WICBitmapEncoderNoCache).Assert();
using ComPtr<IWICBitmapFrameEncode> wicBitmapFrameEncode = default;
// Create the image frame and initialize it
wicBitmapEncoder.Get()->CreateNewFrame(wicBitmapFrameEncode.GetAddressOf(), null).Assert();
wicBitmapFrameEncode.Get()->Initialize(null).Assert();
wicBitmapFrameEncode.Get()->SetSize((uint)texture.Width, (uint)texture.Height).Assert();
// Depending on the target format and the current pixel type, we need to check whether
// an intermediate encoding step is necessary. This is because not all pixel formats
// are directly supported by the native image encoders present in Windows.
if (WICFormatHelper.TryGetIntermediateFormatForType<T>(containerGuid, out Guid intermediateGuid))
{
T* data = texture.DangerousGetAddressAndByteStride(out int strideInBytes);
using ComPtr<IWICBitmap> wicBitmap = default;
Guid initialGuid = WICFormatHelper.GetForType<T>();
// Create a bitmap wrapping the input texture
this.wicImagingFactory2.Get()->CreateBitmapFromMemory(
(uint)texture.Width,
(uint)texture.Height,
&initialGuid,
(uint)strideInBytes,
(uint)(strideInBytes * texture.Height),
(byte*)data,
wicBitmap.GetAddressOf()).Assert();
using ComPtr<IWICFormatConverter> wicFormatConverter = default;
this.wicImagingFactory2.Get()->CreateFormatConverter(wicFormatConverter.GetAddressOf()).Assert();
// Get a format converter to encode the pixel data
wicFormatConverter.Get()->Initialize(
(IWICBitmapSource*)wicBitmap.Get(),
&intermediateGuid,
WICBitmapDitherType.WICBitmapDitherTypeNone,
null,
0,
WICBitmapPaletteType.WICBitmapPaletteTypeMedianCut).Assert();
// Write the encoded image data to the frame
wicBitmapFrameEncode.Get()->SetPixelFormat(&intermediateGuid).Assert();
wicBitmapFrameEncode.Get()->WriteSource(
(IWICBitmapSource*)wicBitmap.Get(),
null).Assert();
}
else
{
T* data = texture.DangerousGetAddressAndByteStride(out int strideInBytes);
Guid initialGuid = WICFormatHelper.GetForType<T>();
// Write the image data to the frame
wicBitmapFrameEncode.Get()->SetPixelFormat(&initialGuid).Assert();
wicBitmapFrameEncode.Get()->WritePixels(
lineCount: (uint)texture.Height,
cbStride: (uint)strideInBytes,
cbBufferSize: (uint)(strideInBytes * texture.Height),
pbPixels: (byte*)data).Assert();
}
wicBitmapFrameEncode.Get()->Commit();
wicBitmapEncoder.Get()->Commit();
}
/// <summary>
/// Loads an <see cref="UploadTexture2D{T}"/> from a specified <see cref="IWICBitmapDecoder"/> object.
/// </summary>
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the texture.</param>
/// <param name="wicBitmapDecoder">The <see cref="IWICBitmapDecoder"/> object in use.</param>
/// <returns>An <see cref="UploadTexture2D{T}"/> instance with the contents of the specified file.</returns>
[Pure]
private UploadTexture2D<T> LoadTexture<T>(GraphicsDevice device, IWICBitmapDecoder* wicBitmapDecoder)
where T : unmanaged
{
using ComPtr<IWICBitmapFrameDecode> wicBitmapFrameDecode = default;
// Get the first frame of the loaded image (if more are present, they will be ignored)
wicBitmapDecoder->GetFrame(0, wicBitmapFrameDecode.GetAddressOf()).Assert();
using ComPtr<IWICFormatConverter> wicFormatConverter = default;
Guid wicPixelFormatGuid = WICFormatHelper.GetForType<T>();
this.wicImagingFactory2.Get()->CreateFormatConverter(wicFormatConverter.GetAddressOf()).Assert();
// Get a format converter to decode the pixel data
wicFormatConverter.Get()->Initialize(
(IWICBitmapSource*)wicBitmapFrameDecode.Get(),
&wicPixelFormatGuid,
WICBitmapDitherType.WICBitmapDitherTypeNone,
null,
0,
WICBitmapPaletteType.WICBitmapPaletteTypeMedianCut).Assert();
uint width, height;
// Extract the image size info
wicFormatConverter.Get()->GetSize(&width, &height).Assert();
// Allocate an upload texture to transfer the decoded pixel data
UploadTexture2D<T> upload = device.AllocateUploadTexture2D<T>((int)width, (int)height);
T* data = upload.View.DangerousGetAddressAndByteStride(out int strideInBytes);
// Decode the pixel data into the upload buffer
wicFormatConverter.Get()->CopyPixels(
prc: null,
cbStride: (uint)strideInBytes,
cbBufferSize: (uint)strideInBytes * height,
pbBuffer: (byte*)data).Assert();
return upload;
}
}
}
+1 -1
ファイルの表示
@@ -22,7 +22,7 @@ namespace ComputeSharp
where TPixel : unmanaged
{
/// <summary>
/// Creates a new <see cref="ReadOnlyTexture2D{T,TPixel}"/> instance with the specified parameters.
/// Creates a new <see cref="ReadOnlyTexture2D{T, TPixel}"/> instance with the specified parameters.
/// </summary>
/// <param name="device">The <see cref="GraphicsDevice"/> associated with the current instance.</param>
/// <param name="width">The width of the texture.</param>
+1 -1
ファイルの表示
@@ -22,7 +22,7 @@ namespace ComputeSharp
where TPixel : unmanaged
{
/// <summary>
/// Creates a new <see cref="ReadOnlyTexture3D{T,TPixel}"/> instance with the specified parameters.
/// Creates a new <see cref="ReadOnlyTexture3D{T, TPixel}"/> instance with the specified parameters.
/// </summary>
/// <param name="device">The <see cref="GraphicsDevice"/> associated with the current instance.</param>
/// <param name="width">The width of the texture.</param>
+1 -1
ファイルの表示
@@ -22,7 +22,7 @@ namespace ComputeSharp
where TPixel : unmanaged
{
/// <summary>
/// Creates a new <see cref="ReadWriteTexture2D{T,TPixel}"/> instance with the specified parameters.
/// Creates a new <see cref="ReadWriteTexture2D{T, TPixel}"/> instance with the specified parameters.
/// </summary>
/// <param name="device">The <see cref="GraphicsDevice"/> associated with the current instance.</param>
/// <param name="width">The width of the texture.</param>
+1 -1
ファイルの表示
@@ -22,7 +22,7 @@ namespace ComputeSharp
where TPixel : unmanaged
{
/// <summary>
/// Creates a new <see cref="ReadOnlyTexture2D{T,TPixel}"/> instance with the specified parameters.
/// Creates a new <see cref="ReadOnlyTexture2D{T, TPixel}"/> instance with the specified parameters.
/// </summary>
/// <param name="device">The <see cref="GraphicsDevice"/> associated with the current instance.</param>
/// <param name="width">The width of the texture.</param>
+1 -1
ファイルの表示
@@ -4,7 +4,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NuGetizer" Version="0.6.2" PrivateAssets="all" />
<PackageReference Include="NuGetizer" Version="0.7.0" PrivateAssets="all" />
</ItemGroup>
<Import Project="..\Directory.Build.props" />
+8 -7
ファイルの表示
@@ -13,21 +13,22 @@ namespace ComputeSharp.Tests.Extensions
public static class ImagingExtensions
{
/// <summary>
/// Allocates a new <see cref="ReadOnlyTexture2D{T, TPixel}"/> instance with the specified texture data.
/// Creates a new <see cref="Image{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>
/// <param name="texture">The source <see cref="ReadOnlyTexture2D{T, TPixel}"/> instance to read data from.</param>
/// <returns>An image with the data from the input texture.</returns>
[Pure]
public static ReadOnlyTexture2D<Rgba32, Float4> LoadTexture(this GraphicsDevice device, string path)
public static Image<ImageSharpRgba32> ToImage(this ReadOnlyTexture2D<Rgba32, Float4> texture)
{
using Image<ImageSharpRgba32> image = Image.Load<ImageSharpRgba32>(path);
Image<ImageSharpRgba32> image = new(texture.Width, texture.Height);
Assert.IsTrue(image.TryGetSinglePixelSpan(out Span<ImageSharpRgba32> span));
Span<Rgba32> pixels = MemoryMarshal.Cast<ImageSharpRgba32, Rgba32>(span);
return device.AllocateReadOnlyTexture2D<Rgba32, Float4>(pixels, image.Width, image.Height);
texture.CopyTo(pixels);
return image;
}
/// <summary>
+63
ファイルの表示
@@ -0,0 +1,63 @@
using System.IO;
using System.Reflection;
using ComputeSharp.BokehBlur.Processors;
using ComputeSharp.Tests.Attributes;
using ComputeSharp.Tests.Extensions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;
using ImageSharpRgba32 = SixLabors.ImageSharp.PixelFormats.Rgba32;
namespace ComputeSharp.Tests
{
[TestClass]
[TestCategory("ImageProcessing")]
public class ImageProcessingTests
{
[CombinatorialTestMethod]
[AllDevices]
public void BokehBlur(Device device)
{
// Early test to ensure the device is available. This saves time when running the
// unit test if the target device is not available, as we skip the preprocessing.
_ = device.Get();
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging");
using var original = Image.Load<ImageSharpRgba32>(Path.Combine(path, "city.jpg"));
using var cpu = original.Clone(c => c.BokehBlur(80, 2, 3));
using var gpu = original.Clone(c => c.ApplyProcessor(new HlslBokehBlurProcessor(device.Get(), 80, 2)));
string
expectedPath = Path.Combine(path, "city_bokeh_cpu.jpg"),
actualPath = Path.Combine(path, "city_bokeh_gpu.jpg");
cpu.Save(expectedPath);
gpu.Save(actualPath);
ImagingTests.TolerantImageComparer.AssertEqual(expectedPath, actualPath, 0.000009f);
}
[CombinatorialTestMethod]
[AllDevices]
public void GaussianBlur(Device device)
{
_ = device.Get();
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging");
using var original = Image.Load<ImageSharpRgba32>(Path.Combine(path, "city.jpg"));
using var cpu = original.Clone(c => c.GaussianBlur(30f));
using var gpu = original.Clone(c => c.ApplyProcessor(new HlslGaussianBlurProcessor(device.Get(), 90)));
string
expectedPath = Path.Combine(path, "city_gaussian_cpu.jpg"),
actualPath = Path.Combine(path, "city_gaussian_gpu.jpg");
cpu.Save(expectedPath);
gpu.Save(actualPath);
ImagingTests.TolerantImageComparer.AssertEqual(expectedPath, actualPath, 0.000003f);
}
}
}
+65 -29
ファイルの表示
@@ -4,13 +4,12 @@ using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using ComputeSharp.BokehBlur.Processors;
using ComputeSharp.Tests.Attributes;
using ComputeSharp.Tests.Extensions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;
using ImageSharpRgba32 = SixLabors.ImageSharp.PixelFormats.Rgba32;
using ImageSharpL8 = SixLabors.ImageSharp.PixelFormats.L8;
namespace ComputeSharp.Tests
{
@@ -20,48 +19,85 @@ namespace ComputeSharp.Tests
{
[CombinatorialTestMethod]
[AllDevices]
public void BokehBlur(Device device)
public void LoadAsRgba32FromFile(Device device)
{
// Early test to ensure the device is available. This saves time when running the
// unit test if the target device is not available, as we skip the preprocessing.
_ = device.Get();
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging", "city.jpg");
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging");
using ReadOnlyTexture2D<Rgba32, Float4> texture = device.Get().AllocateReadOnlyTexture2D<Rgba32, Float4>(path);
using var original = Image.Load<ImageSharpRgba32>(Path.Combine(path, "city.jpg"));
using var cpu = original.Clone(c => c.BokehBlur(80, 2, 3));
using var gpu = original.Clone(c => c.ApplyProcessor(new HlslBokehBlurProcessor(device.Get(), 80, 2)));
using Image<ImageSharpRgba32> loaded = texture.ToImage();
using Image<ImageSharpRgba32> original = Image.Load<ImageSharpRgba32>(path);
string
expectedPath = Path.Combine(path, "city_bokeh_cpu.jpg"),
actualPath = Path.Combine(path, "city_bokeh_gpu.jpg");
cpu.Save(expectedPath);
gpu.Save(actualPath);
TolerantImageComparer.AssertEqual(expectedPath, actualPath, 0.000009f);
TolerantImageComparer.AssertEqual(original, loaded, 0.0000032f);
}
[CombinatorialTestMethod]
[AllDevices]
public void GaussianBlur(Device device)
public void LoadAsRgba32FromBuffer(Device device)
{
_ = device.Get();
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging", "city.jpg");
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging");
byte[] buffer = File.ReadAllBytes(path);
using var original = Image.Load<ImageSharpRgba32>(Path.Combine(path, "city.jpg"));
using var cpu = original.Clone(c => c.GaussianBlur(30f));
using var gpu = original.Clone(c => c.ApplyProcessor(new HlslGaussianBlurProcessor(device.Get(), 90)));
using ReadOnlyTexture2D<Rgba32, Float4> texture = device.Get().AllocateReadOnlyTexture2D<Rgba32, Float4>(buffer);
using Image<ImageSharpRgba32> loaded = texture.ToImage();
using Image<ImageSharpRgba32> original = Image.Load<ImageSharpRgba32>(path);
TolerantImageComparer.AssertEqual(original, loaded, 0.0000032f);
}
[CombinatorialTestMethod]
[AllDevices]
public void SaveRgba32AsJpeg(Device device)
{
string
expectedPath = Path.Combine(path, "city_gaussian_cpu.jpg"),
actualPath = Path.Combine(path, "city_gaussian_gpu.jpg");
path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging"),
expectedPath = Path.Combine(path, "city.jpg"),
actualPath = Path.Combine(path, "city_rgba32_saved.jpg");
cpu.Save(expectedPath);
gpu.Save(actualPath);
using ReadOnlyTexture2D<Rgba32, Float4> texture = device.Get().AllocateReadOnlyTexture2D<Rgba32, Float4>(expectedPath);
TolerantImageComparer.AssertEqual(expectedPath, actualPath, 0.000003f);
texture.Save(actualPath);
TolerantImageComparer.AssertEqual(expectedPath, actualPath, 0.00001023f);
}
[CombinatorialTestMethod]
[AllDevices]
public void SaveBgra32AsJpeg(Device device)
{
string
path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging"),
expectedPath = Path.Combine(path, "city.jpg"),
actualPath = Path.Combine(path, "city_bgra32_saved.jpg");
using ReadOnlyTexture2D<Bgra32, Float4> texture = device.Get().AllocateReadOnlyTexture2D<Bgra32, Float4>(expectedPath);
texture.Save(actualPath);
TolerantImageComparer.AssertEqual(expectedPath, actualPath, 0.00001023f);
}
[CombinatorialTestMethod]
[AllDevices]
public void SaveR8AsJpeg(Device device)
{
string
path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging"),
sourcePath = Path.Combine(path, "city.jpg"),
expectedPath = Path.Combine(path, "city_r8_reference.jpg"),
actualPath = Path.Combine(path, "city_r8_saved.jpg");
using ReadOnlyTexture2D<R8, float> texture = device.Get().AllocateReadOnlyTexture2D<R8, float>(sourcePath);
texture.Save(actualPath);
using Image<ImageSharpL8> original = Image.Load<ImageSharpL8>(sourcePath);
original.Save(expectedPath);
TolerantImageComparer.AssertEqual(expectedPath, actualPath, 0.00004037f);
}
/// <summary>
+1 -1
ファイルの表示
@@ -105,7 +105,7 @@ namespace ComputeSharp.Tests
{
string filename = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Shaders", "Textures", "RustyMetal.png");
using ReadOnlyTexture2D<Rgba32, Float4> background = device.Get().LoadTexture(filename);
using ReadOnlyTexture2D<Rgba32, Float4> background = device.Get().AllocateReadOnlyTexture2D<Rgba32, Float4>(filename);
RunAndCompareShader(
device,
+4 -4
ファイルの表示
@@ -76,14 +76,14 @@ namespace ComputeSharp.Tests
using var sampled = Image.Load<ImageSharpRgba32>(Path.Combine(assetsPath, "CityAfter1024x1024Sampling.png"));
using ReadOnlyTexture2D<Rgba32, Float4> source = device.Get().LoadTexture(Path.Combine(imagingPath, "city.jpg"));
using ReadOnlyTexture2D<Rgba32, Float4> source = device.Get().AllocateReadOnlyTexture2D<Rgba32, Float4>(Path.Combine(imagingPath, "city.jpg"));
using ReadWriteTexture2D<Rgba32, Float4> destination = device.Get().AllocateReadWriteTexture2D<Rgba32, Float4>(sampled.Width, sampled.Height);
device.Get().For(sampled.Width, sampled.Height, new SamplingComputeShader(source, destination));
using var processed = destination.ToImage();
ImagingTests.TolerantImageComparer.AssertEqual(sampled, processed, 0.00000086f);
ImagingTests.TolerantImageComparer.AssertEqual(sampled, processed, 0.0000017f);
}
[AutoConstructor]
@@ -109,14 +109,14 @@ namespace ComputeSharp.Tests
using var sampled = Image.Load<ImageSharpRgba32>(Path.Combine(assetsPath, "CityAfter1024x1024Sampling.png"));
using ReadOnlyTexture2D<Rgba32, Float4> source = device.Get().LoadTexture(Path.Combine(imagingPath, "city.jpg"));
using ReadOnlyTexture2D<Rgba32, Float4> source = device.Get().AllocateReadOnlyTexture2D<Rgba32, Float4>(Path.Combine(imagingPath, "city.jpg"));
using ReadWriteTexture2D<Rgba32, Float4> destination = device.Get().AllocateReadWriteTexture2D<Rgba32, Float4>(sampled.Width, sampled.Height);
device.Get().ForEach(destination, new SamplingPixelShader(source));
using var processed = destination.ToImage();
ImagingTests.TolerantImageComparer.AssertEqual(sampled, processed, 0.00000086f);
ImagingTests.TolerantImageComparer.AssertEqual(sampled, processed, 0.0000017f);
}
[AutoConstructor]