Add LoadReadOnlyTexture2D overload for Stream

このコミットが含まれているのは:
Sergio Pedri committed 2021-12-08 19:57:28 +01:00
1 親 b93a4a2a5b
コミット 770ed134bf
4ファイル変更
+208 -79

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

+49
ファイルの表示
@@ -1,5 +1,6 @@
using System;
using System.Diagnostics.Contracts;
using System.IO;
using ComputeSharp.__Internals;
using ComputeSharp.Graphics.Helpers;
using ComputeSharp.Resources;
@@ -1133,6 +1134,30 @@ public static partial class GraphicsDeviceExtensions
return texture;
}
/// <summary>
/// Loads a new readonly 2D texture with the contents of the specified stream.
/// </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="stream">The stream 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> LoadReadOnlyTexture2D<T, TPixel>(this GraphicsDevice device, Stream stream)
where T : unmanaged, IUnorm<TPixel>
where TPixel : unmanaged
{
Guard.IsNotNull(stream, nameof(stream));
using UploadTexture2D<T> upload = WICHelper.Instance.LoadTexture<T>(device, stream);
ReadOnlyTexture2D<T, TPixel> texture = device.AllocateReadOnlyTexture2D<T, TPixel>(upload.Width, upload.Height);
upload.CopyTo(texture);
return texture;
}
/// <summary>
/// Loads a new writeable 2D texture with the contents of the specified file.
/// </summary>
@@ -1194,4 +1219,28 @@ public static partial class GraphicsDeviceExtensions
return texture;
}
/// <summary>
/// Loads a new writeable 2D texture with the contents of the specified stream.
/// </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="stream">The stream 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> LoadReadWriteTexture2D<T, TPixel>(this GraphicsDevice device, Stream stream)
where T : unmanaged, IUnorm<TPixel>
where TPixel : unmanaged
{
Guard.IsNotNull(stream, nameof(stream));
using UploadTexture2D<T> upload = WICHelper.Instance.LoadTexture<T>(device, stream);
ReadWriteTexture2D<T, TPixel> texture = device.AllocateReadWriteTexture2D<T, TPixel>(upload.Width, upload.Height);
upload.CopyTo(texture);
return texture;
}
}
+106 -77
ファイルの表示
@@ -1,7 +1,9 @@
using System;
using System.Diagnostics.Contracts;
using System.IO;
using System.Runtime.CompilerServices;
using ComputeSharp.Core.Extensions;
using ComputeSharp.Graphics.Extensions;
using TerraFX.Interop.Windows;
#pragma warning disable CA1416
@@ -106,6 +108,110 @@ internal sealed unsafe class WICHelper
}
}
/// <summary>
/// Loads an <see cref="UploadTexture2D{T}"/> from a specified stream.
/// </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="stream">The stream 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, 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();
return LoadTexture<T>(device, wicBitmapDecoder.Get());
}
/// <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();
uint width;
uint height;
// Extract the image size info
wicBitmapFrameDecode.Get()->GetSize(&width, &height).Assert();
Guid wicTargetPixelFormatGuid = WICFormatHelper.GetForType<T>();
Guid wicActualPixelFormatGuid;
// Get the current and target pixel format info
wicBitmapFrameDecode.Get()->GetPixelFormat(&wicActualPixelFormatGuid).Assert();
// If the current pixel format is the same as the target one, we can just read the
// decoded pixel data directly. Otherwise, we need a pixel format conversion step.
if (wicTargetPixelFormatGuid == wicActualPixelFormatGuid)
{
// 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);
// Copy the decoded pixels directly from the loaded image
wicBitmapFrameDecode.Get()->CopyPixels(
prc: null,
cbStride: (uint)strideInBytes,
cbBufferSize: (uint)strideInBytes * height,
pbBuffer: (byte*)data).Assert();
return upload;
}
else
{
using ComPtr<IWICFormatConverter> wicFormatConverter = default;
this.wicImagingFactory2.Get()->CreateFormatConverter(wicFormatConverter.GetAddressOf()).Assert();
// Get a format converter to decode the pixel data
wicFormatConverter.Get()->Initialize(
(IWICBitmapSource*)wicBitmapFrameDecode.Get(),
&wicTargetPixelFormatGuid,
WICBitmapDitherType.WICBitmapDitherTypeNone,
null,
0,
WICBitmapPaletteType.WICBitmapPaletteTypeMedianCut).Assert();
// Allocate an upload texture to transfer the converted 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;
}
}
/// <summary>
/// Saves a texture to a specified file.
/// </summary>
@@ -205,81 +311,4 @@ internal sealed unsafe class WICHelper
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();
uint width;
uint height;
// Extract the image size info
wicBitmapFrameDecode.Get()->GetSize(&width, &height).Assert();
Guid wicTargetPixelFormatGuid = WICFormatHelper.GetForType<T>();
Guid wicActualPixelFormatGuid;
// Get the current and target pixel format info
wicBitmapFrameDecode.Get()->GetPixelFormat(&wicActualPixelFormatGuid).Assert();
// If the current pixel format is the same as the target one, we can just read the
// decoded pixel data directly. Otherwise, we need a pixel format conversion step.
if (wicTargetPixelFormatGuid == wicActualPixelFormatGuid)
{
// 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);
// Copy the decoded pixels directly from the loaded image
wicBitmapFrameDecode.Get()->CopyPixels(
prc: null,
cbStride: (uint)strideInBytes,
cbBufferSize: (uint)strideInBytes * height,
pbBuffer: (byte*)data).Assert();
return upload;
}
else
{
using ComPtr<IWICFormatConverter> wicFormatConverter = default;
this.wicImagingFactory2.Get()->CreateFormatConverter(wicFormatConverter.GetAddressOf()).Assert();
// Get a format converter to decode the pixel data
wicFormatConverter.Get()->Initialize(
(IWICBitmapSource*)wicBitmapFrameDecode.Get(),
&wicTargetPixelFormatGuid,
WICBitmapDitherType.WICBitmapDitherTypeNone,
null,
0,
WICBitmapPaletteType.WICBitmapPaletteTypeMedianCut).Assert();
// Allocate an upload texture to transfer the converted 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;
}
}
}
+25 -2
ファイルの表示
@@ -1,5 +1,6 @@
using System;
using System.Diagnostics.Contracts;
using System.IO;
using ComputeSharp.__Internals;
using ComputeSharp.Resources;
@@ -335,14 +336,14 @@ public static class GraphicsDeviceExtensions
}
/// <summary>
/// Loads a new readonly 2D texture with the contents of the specified file.
/// Loads 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="type">The type of texture to allocate.</param>
/// <param name="buffer">The buffer with the image data to load and decode into the texture.</param>
/// <returns>A <see cref="Texture2D{T}"/> instance with the contents of the specified file.</returns>
/// <returns>A <see cref="Texture2D{T}"/> instance with the contents of the specified buffer.</returns>
[Pure]
public static Texture2D<T> LoadTexture2D<T, TPixel>(this GraphicsDevice device, Type type, byte[] buffer)
where T : unmanaged, IUnorm<TPixel>
@@ -356,6 +357,28 @@ public static class GraphicsDeviceExtensions
};
}
/// <summary>
/// Loads a new readonly 2D texture with the contents of the specified stream.
/// </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="type">The type of texture to allocate.</param>
/// <param name="stream">The stream with the image data to load and decode into the texture.</param>
/// <returns>A <see cref="Texture2D{T}"/> instance with the contents of the specified stream.</returns>
[Pure]
public static Texture2D<T> LoadTexture2D<T, TPixel>(this GraphicsDevice device, Type type, Stream stream)
where T : unmanaged, IUnorm<TPixel>
where TPixel : unmanaged
{
return type switch
{
_ when type == typeof(ReadOnlyTexture2D<,>) => device.LoadReadOnlyTexture2D<T, TPixel>(stream),
_ when type == typeof(ReadWriteTexture2D<,>) => device.LoadReadWriteTexture2D<T, TPixel>(stream),
_ => throw new ArgumentException($"Invalid type: {type}", nameof(type))
};
}
/// <summary>
/// Saves a texture to a specified file.
/// </summary>
+28
ファイルの表示
@@ -111,6 +111,24 @@ public class ImagingTests
TolerantImageComparer.AssertEqual(original, loaded, 0.0000032f);
}
[CombinatorialTestMethod]
[AllDevices]
[Resource(typeof(ReadOnlyTexture2D<,>))]
[Resource(typeof(ReadWriteTexture2D<,>))]
public void LoadAsRgba32FromStream(Device device, Type textureType)
{
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging", "city.jpg");
using Stream stream = File.OpenRead(path);
using Texture2D<Rgba32> texture = device.Get().LoadTexture2D<Rgba32, float4>(textureType, stream);
using Image<ImageSharpRgba32> loaded = texture.ToImage<Rgba32, ImageSharpRgba32>();
using Image<ImageSharpRgba32> original = Image.Load<ImageSharpRgba32>(path);
TolerantImageComparer.AssertEqual(original, loaded, 0.0000032f);
}
[CombinatorialTestMethod]
[Device(Device.Warp)]
[Resource(typeof(ReadOnlyTexture2D<,>))]
@@ -121,6 +139,16 @@ public class ImagingTests
using Texture2D<Rgba32> texture = device.Get().LoadTexture2D<Rgba32, float4>(textureType, typeof(string), null!);
}
[CombinatorialTestMethod]
[Device(Device.Warp)]
[Resource(typeof(ReadOnlyTexture2D<,>))]
[Resource(typeof(ReadWriteTexture2D<,>))]
[ExpectedException(typeof(ArgumentNullException))]
public void LoadAsRgba32_WithNullStream(Device device, Type textureType)
{
using Texture2D<Rgba32> texture = device.Get().LoadTexture2D<Rgba32, float4>(textureType, (Stream)null!);
}
[CombinatorialTestMethod]
[AllDevices]
[Resource(typeof(ReadOnlyTexture2D<,>))]