Add UploadTexture2D<T>.Load APIs

このコミットが含まれているのは:
Sergio Pedri committed 2021-12-08 23:47:16 +01:00
1 親 5d5dc541bc
コミット 48cb1b21d7
4ファイル変更
+314 -1

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

+58 -1
ファイルの表示
@@ -1,4 +1,8 @@
using ComputeSharp.Resources;
using System;
using System.IO;
using ComputeSharp.Graphics.Helpers;
using ComputeSharp.Resources;
using Microsoft.Toolkit.Diagnostics;
namespace ComputeSharp;
@@ -52,4 +56,57 @@ public static class UploadTexture2DExtensions
{
destination.CopyFrom(source, sourceOffsetX, sourceOffsetY, destinationOffsetX, destinationOffsetY, width, height);
}
/// <summary>
/// Loads a texture from a specified file.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="texture">The texture to decode the image into.</param>
/// <param name="filename">The filename of the image file to load.</param>
public static void Load<T>(this UploadTexture2D<T> texture, string filename)
where T : unmanaged
{
Guard.IsNotNull(filename, nameof(filename));
texture. Load(filename.AsSpan());
}
/// <summary>
/// Loads a texture from a specified file.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="texture">The texture to decode the image into.</param>
/// <param name="filename">The filename of the image file to load.</param>
public static void Load<T>(this UploadTexture2D<T> texture, ReadOnlySpan<char> filename)
where T : unmanaged
{
WICHelper.Instance.LoadTexture(texture.View, filename);
}
/// <summary>
/// LLoads a texture from a specified buffer.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="texture">The texture to decode the image into.</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>
public static void Load<T>(this UploadTexture2D<T> texture, ReadOnlySpan<byte> span)
where T : unmanaged
{
WICHelper.Instance.LoadTexture(texture.View, span);
}
/// <summary>
/// Loads a texture from a specified stream.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="texture">The texture to decode the image into.</param>
/// <param name="stream">The stream to load the image from.</param>
public static void Load<T>(this UploadTexture2D<T> texture, Stream stream)
where T : unmanaged
{
Guard.IsNotNull(stream, nameof(stream));
WICHelper.Instance.LoadTexture(texture.View, stream);
}
}
+145
ファイルの表示
@@ -4,6 +4,7 @@ using System.IO;
using System.Runtime.CompilerServices;
using ComputeSharp.Core.Extensions;
using ComputeSharp.Graphics.Extensions;
using Microsoft.Toolkit.Diagnostics;
using TerraFX.Interop.Windows;
#pragma warning disable CA1416
@@ -212,6 +213,150 @@ internal sealed unsafe class WICHelper
}
}
/// <summary>
/// Loads an image from a specified file.
/// </summary>
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
/// <param name="texture">The target <see cref="TextureView2D{T}"/> instance to write data to.</param>
/// <param name="filename">The filename of the image file to load and decode into the texture.</param>
[Pure]
public void LoadTexture<T>(TextureView2D<T> texture, ReadOnlySpan<char> filename)
where T : unmanaged
{
using ComPtr<IWICBitmapDecoder> wicBitmapDecoder = default;
fixed (char* p = filename)
{
this.wicImagingFactory2.Get()->CreateDecoderFromFilename(
(ushort*)p,
null,
Windows.GENERIC_READ,
WICDecodeOptions.WICDecodeMetadataCacheOnDemand,
wicBitmapDecoder.GetAddressOf()).Assert();
}
LoadTexture(texture, wicBitmapDecoder.Get());
}
/// <summary>
/// Loads an image from a specified buffer.
/// </summary>
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
/// <param name="texture">The target <see cref="TextureView2D{T}"/> instance to write data to.</param>
/// <param name="span">The buffer with the image data to load and decode into the texture.</param>
[Pure]
public void LoadTexture<T>(TextureView2D<T> texture, 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();
LoadTexture(texture, wicBitmapDecoder.Get());
}
}
/// <summary>
/// Loads an image from a specified stream.
/// </summary>
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
/// <param name="texture">The target <see cref="TextureView2D{T}"/> instance to write data to.</param>
/// <param name="stream">The stream with the image data to load and decode into the texture.</param>
[Pure]
public void LoadTexture<T>(TextureView2D<T> texture, Stream stream)
where T : unmanaged
{
using ComPtr<IWICBitmapDecoder> wicBitmapDecoder = default;
using ComPtr<IWICStream> wicStream = default;
this.wicImagingFactory2.Get()->CreateStream(wicStream.GetAddressOf()).Assert();
wicStream.Get()->InitializeFromStream(stream).Assert();
this.wicImagingFactory2.Get()->CreateDecoderFromStream(
(IStream*)wicStream.Get(),
null,
WICDecodeOptions.WICDecodeMetadataCacheOnDemand,
wicBitmapDecoder.GetAddressOf()).Assert();
LoadTexture(texture, wicBitmapDecoder.Get());
}
/// <summary>
/// Loads image data from a specified <see cref="IWICBitmapDecoder"/> object into a target texture.
/// </summary>
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
/// <param name="texture">The target <see cref="TextureView2D{T}"/> instance to write data to.</param>
/// <param name="wicBitmapDecoder">The <see cref="IWICBitmapDecoder"/> object in use.</param>
[Pure]
private void LoadTexture<T>(TextureView2D<T> texture, IWICBitmapDecoder* wicBitmapDecoder)
where T : unmanaged
{
using ComPtr<IWICBitmapFrameDecode> wicBitmapFrameDecode = default;
wicBitmapDecoder->GetFrame(0, wicBitmapFrameDecode.GetAddressOf()).Assert();
uint width;
uint height;
// Extract the image size info
wicBitmapFrameDecode.Get()->GetSize(&width, &height).Assert();
// Validate the target texture has the right size
Guard.IsEqualTo((uint)texture.Width, width, nameof(texture.Width));
Guard.IsEqualTo((uint)texture.Height, height, nameof(texture.Height));
Guid wicTargetPixelFormatGuid = WICFormatHelper.GetForType<T>();
Guid wicActualPixelFormatGuid;
wicBitmapFrameDecode.Get()->GetPixelFormat(&wicActualPixelFormatGuid).Assert();
T* data = texture.DangerousGetAddressAndByteStride(out int strideInBytes);
// Copy the decoded pixels directly from the loaded image
if (wicTargetPixelFormatGuid == wicActualPixelFormatGuid)
{
wicBitmapFrameDecode.Get()->CopyPixels(
prc: null,
cbStride: (uint)strideInBytes,
cbBufferSize: (uint)strideInBytes * height,
pbBuffer: (byte*)data).Assert();
}
else
{
using ComPtr<IWICFormatConverter> wicFormatConverter = default;
this.wicImagingFactory2.Get()->CreateFormatConverter(wicFormatConverter.GetAddressOf()).Assert();
wicFormatConverter.Get()->Initialize(
(IWICBitmapSource*)wicBitmapFrameDecode.Get(),
&wicTargetPixelFormatGuid,
WICBitmapDitherType.WICBitmapDitherTypeNone,
null,
0,
WICBitmapPaletteType.WICBitmapPaletteTypeMedianCut).Assert();
// Decode the pixel data into the upload buffer
wicFormatConverter.Get()->CopyPixels(
prc: null,
cbStride: (uint)strideInBytes,
cbBufferSize: (uint)strideInBytes * height,
pbBuffer: (byte*)data).Assert();
}
}
/// <summary>
/// Saves a texture to a specified file.
/// </summary>
+24
ファイルの表示
@@ -302,6 +302,30 @@ public static class GraphicsDeviceExtensions
};
}
/// <summary>
/// Loads an image from a specific file into a target <see cref="UploadTexture2D{T}"/> instance.
/// </summary>
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
/// <param name="texture">The target <see cref="UploadTexture2D{T}"/> instance.</param>
/// <param name="inputType">The type of filepath to use.</param>
/// <param name="filename">The filename of the image file to load and decode into the texture.</param>
[Pure]
public static void Load<T>(this UploadTexture2D<T> texture, Type inputType, string filename)
where T : unmanaged
{
switch (inputType)
{
case var _ when inputType == typeof(string):
texture.Load(filename);
break;
case var _ when inputType == typeof(ReadOnlySpan<char>):
texture.Load(filename);
break;
default:
throw new ArgumentException($"Invalid input type: {inputType}", nameof(inputType));
}
}
/// <summary>
/// Loads a new readonly 2D texture with the contents of the specified file.
/// </summary>
+87
ファイルの表示
@@ -39,6 +39,30 @@ public class ImagingTests
TolerantImageComparer.AssertEqual(original, loaded, 0.0000032f);
}
[CombinatorialTestMethod]
[AllDevices]
[Resource(typeof(ReadOnlyTexture2D<>))]
[Resource(typeof(ReadWriteTexture2D<>))]
[Data(typeof(string))]
[Data(typeof(ReadOnlySpan<char>))]
public void LoadAsRgba32_FromFile_WithUploadTexture(Device device, Type textureType, Type inputType)
{
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging", "city.jpg");
IImageInfo imageInfo = Image.Identify(path);
using Texture2D<Rgba32> texture = device.Get().AllocateTexture2D<Rgba32>(textureType, imageInfo.Width, imageInfo.Height);
using UploadTexture2D<Rgba32> upload = device.Get().AllocateUploadTexture2D<Rgba32>(imageInfo.Width, imageInfo.Height);
upload.Load(inputType, path);
upload.CopyTo(texture);
using Image<ImageSharpRgba32> loaded = texture.ToImage<Rgba32, ImageSharpRgba32>();
using Image<ImageSharpRgba32> original = Image.Load<ImageSharpRgba32>(path);
TolerantImageComparer.AssertEqual(original, loaded, 0.0000032f);
}
[CombinatorialTestMethod]
[AllDevices]
[Resource(typeof(ReadOnlyTexture2D<,>))]
@@ -57,6 +81,30 @@ public class ImagingTests
TolerantImageComparer.AssertEqual(original, loaded, 0.0000032f);
}
[CombinatorialTestMethod]
[AllDevices]
[Resource(typeof(ReadOnlyTexture2D<>))]
[Resource(typeof(ReadWriteTexture2D<>))]
public void LoadAsRgba32_FromBuffer_WithUploadTexture(Device device, Type textureType)
{
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging", "city.jpg");
IImageInfo imageInfo = Image.Identify(path);
using Texture2D<Rgba32> texture = device.Get().AllocateTexture2D<Rgba32>(textureType, imageInfo.Width, imageInfo.Height);
using UploadTexture2D<Rgba32> upload = device.Get().AllocateUploadTexture2D<Rgba32>(imageInfo.Width, imageInfo.Height);
byte[] buffer = File.ReadAllBytes(path);
upload.Load(buffer);
upload.CopyTo(texture);
using Image<ImageSharpRgba32> loaded = texture.ToImage<Rgba32, ImageSharpRgba32>();
using Image<ImageSharpRgba32> original = Image.Load<ImageSharpRgba32>(path);
TolerantImageComparer.AssertEqual(original, loaded, 0.0000032f);
}
[CombinatorialTestMethod]
[AllDevices]
[Resource(typeof(ReadOnlyTexture2D<,>))]
@@ -75,6 +123,45 @@ public class ImagingTests
TolerantImageComparer.AssertEqual(original, loaded, 0.0000032f);
}
[CombinatorialTestMethod]
[AllDevices]
[Resource(typeof(ReadOnlyTexture2D<>))]
[Resource(typeof(ReadWriteTexture2D<>))]
public void LoadAsRgba32_FromStream_WithUploadTexture(Device device, Type textureType)
{
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging", "city.jpg");
IImageInfo imageInfo = Image.Identify(path);
using Texture2D<Rgba32> texture = device.Get().AllocateTexture2D<Rgba32>(textureType, imageInfo.Width, imageInfo.Height);
using UploadTexture2D<Rgba32> upload = device.Get().AllocateUploadTexture2D<Rgba32>(imageInfo.Width, imageInfo.Height);
using Stream stream = File.OpenRead(path);
upload.Load(stream);
upload.CopyTo(texture);
using Image<ImageSharpRgba32> loaded = texture.ToImage<Rgba32, ImageSharpRgba32>();
using Image<ImageSharpRgba32> original = Image.Load<ImageSharpRgba32>(path);
TolerantImageComparer.AssertEqual(original, loaded, 0.0000032f);
}
[CombinatorialTestMethod]
[AllDevices]
[Data(3840, 2562)]
[Data(3839, 2560)]
[Data(3844, 2564)]
[ExpectedException(typeof(ArgumentException))]
public void LoadAsRgba32_FromStream_WithUploadTexture_SizeMismatch(Device device, int width, int height)
{
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging", "city.jpg");
using UploadTexture2D<Rgba32> upload = device.Get().AllocateUploadTexture2D<Rgba32>(width, height);
upload.Load(path);
}
[CombinatorialTestMethod]
[AllDevices]
[Resource(typeof(ReadOnlyTexture2D<,>))]