diff --git a/src/ComputeSharp/Graphics/Extensions/UploadTexture2DExtensions.cs b/src/ComputeSharp/Graphics/Extensions/UploadTexture2DExtensions.cs
index 675aab5d..6b97447c 100644
--- a/src/ComputeSharp/Graphics/Extensions/UploadTexture2DExtensions.cs
+++ b/src/ComputeSharp/Graphics/Extensions/UploadTexture2DExtensions.cs
@@ -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);
}
+
+ ///
+ /// Loads a texture from a specified file.
+ ///
+ /// The type of items stored on the texture.
+ /// The texture to decode the image into.
+ /// The filename of the image file to load.
+ public static void Load(this UploadTexture2D texture, string filename)
+ where T : unmanaged
+ {
+ Guard.IsNotNull(filename, nameof(filename));
+
+ texture. Load(filename.AsSpan());
+ }
+
+ ///
+ /// Loads a texture from a specified file.
+ ///
+ /// The type of items stored on the texture.
+ /// The texture to decode the image into.
+ /// The filename of the image file to load.
+ public static void Load(this UploadTexture2D texture, ReadOnlySpan filename)
+ where T : unmanaged
+ {
+ WICHelper.Instance.LoadTexture(texture.View, filename);
+ }
+
+ ///
+ /// LLoads a texture from a specified buffer.
+ ///
+ /// The type of items stored on the texture.
+ /// The texture to decode the image into.
+ /// The buffer with the image data to load and decode into the texture.
+ /// A instance with the contents of the specified file.
+ public static void Load(this UploadTexture2D texture, ReadOnlySpan span)
+ where T : unmanaged
+ {
+ WICHelper.Instance.LoadTexture(texture.View, span);
+ }
+
+ ///
+ /// Loads a texture from a specified stream.
+ ///
+ /// The type of items stored on the texture.
+ /// The texture to decode the image into.
+ /// The stream to load the image from.
+ public static void Load(this UploadTexture2D texture, Stream stream)
+ where T : unmanaged
+ {
+ Guard.IsNotNull(stream, nameof(stream));
+
+ WICHelper.Instance.LoadTexture(texture.View, stream);
+ }
}
diff --git a/src/ComputeSharp/Graphics/Helpers/WICHelper.cs b/src/ComputeSharp/Graphics/Helpers/WICHelper.cs
index d5e78467..d1d07e44 100644
--- a/src/ComputeSharp/Graphics/Helpers/WICHelper.cs
+++ b/src/ComputeSharp/Graphics/Helpers/WICHelper.cs
@@ -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
}
}
+ ///
+ /// Loads an image from a specified file.
+ ///
+ /// The type of items to store in the texture.
+ /// The target instance to write data to.
+ /// The filename of the image file to load and decode into the texture.
+ [Pure]
+ public void LoadTexture(TextureView2D texture, ReadOnlySpan filename)
+ where T : unmanaged
+ {
+ using ComPtr 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());
+ }
+
+ ///
+ /// Loads an image from a specified buffer.
+ ///
+ /// The type of items to store in the texture.
+ /// The target instance to write data to.
+ /// The buffer with the image data to load and decode into the texture.
+ [Pure]
+ public void LoadTexture(TextureView2D texture, ReadOnlySpan span)
+ where T : unmanaged
+ {
+ using ComPtr wicBitmapDecoder = default;
+ using ComPtr 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());
+ }
+ }
+
+ ///
+ /// Loads an image from a specified stream.
+ ///
+ /// The type of items to store in the texture.
+ /// The target instance to write data to.
+ /// The stream with the image data to load and decode into the texture.
+ [Pure]
+ public void LoadTexture(TextureView2D texture, Stream stream)
+ where T : unmanaged
+ {
+ using ComPtr wicBitmapDecoder = default;
+ using ComPtr 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());
+ }
+
+ ///
+ /// Loads image data from a specified object into a target texture.
+ ///
+ /// The type of items to store in the texture.
+ /// The target instance to write data to.
+ /// The object in use.
+ [Pure]
+ private void LoadTexture(TextureView2D texture, IWICBitmapDecoder* wicBitmapDecoder)
+ where T : unmanaged
+ {
+ using ComPtr 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();
+ 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 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();
+ }
+ }
+
///
/// Saves a texture to a specified file.
///
diff --git a/tests/ComputeSharp.Tests/Extensions/GraphicsDeviceExtensions.cs b/tests/ComputeSharp.Tests/Extensions/GraphicsDeviceExtensions.cs
index 4214b6aa..3048450d 100644
--- a/tests/ComputeSharp.Tests/Extensions/GraphicsDeviceExtensions.cs
+++ b/tests/ComputeSharp.Tests/Extensions/GraphicsDeviceExtensions.cs
@@ -302,6 +302,30 @@ public static class GraphicsDeviceExtensions
};
}
+ ///
+ /// Loads an image from a specific file into a target instance.
+ ///
+ /// The type of items to store in the texture.
+ /// The target instance.
+ /// The type of filepath to use.
+ /// The filename of the image file to load and decode into the texture.
+ [Pure]
+ public static void Load(this UploadTexture2D 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):
+ texture.Load(filename);
+ break;
+ default:
+ throw new ArgumentException($"Invalid input type: {inputType}", nameof(inputType));
+ }
+ }
+
///
/// Loads a new readonly 2D texture with the contents of the specified file.
///
diff --git a/tests/ComputeSharp.Tests/ImagingTests.cs b/tests/ComputeSharp.Tests/ImagingTests.cs
index cff15df5..216083bc 100644
--- a/tests/ComputeSharp.Tests/ImagingTests.cs
+++ b/tests/ComputeSharp.Tests/ImagingTests.cs
@@ -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))]
+ 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 texture = device.Get().AllocateTexture2D(textureType, imageInfo.Width, imageInfo.Height);
+ using UploadTexture2D upload = device.Get().AllocateUploadTexture2D(imageInfo.Width, imageInfo.Height);
+
+ upload.Load(inputType, path);
+ upload.CopyTo(texture);
+
+ using Image loaded = texture.ToImage();
+ using Image original = Image.Load(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 texture = device.Get().AllocateTexture2D(textureType, imageInfo.Width, imageInfo.Height);
+ using UploadTexture2D upload = device.Get().AllocateUploadTexture2D(imageInfo.Width, imageInfo.Height);
+
+ byte[] buffer = File.ReadAllBytes(path);
+
+ upload.Load(buffer);
+ upload.CopyTo(texture);
+
+ using Image loaded = texture.ToImage();
+ using Image original = Image.Load(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 texture = device.Get().AllocateTexture2D(textureType, imageInfo.Width, imageInfo.Height);
+ using UploadTexture2D upload = device.Get().AllocateUploadTexture2D(imageInfo.Width, imageInfo.Height);
+
+ using Stream stream = File.OpenRead(path);
+
+ upload.Load(stream);
+ upload.CopyTo(texture);
+
+ using Image loaded = texture.ToImage();
+ using Image original = Image.Load(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 upload = device.Get().AllocateUploadTexture2D(width, height);
+
+ upload.Load(path);
+ }
+
[CombinatorialTestMethod]
[AllDevices]
[Resource(typeof(ReadOnlyTexture2D<,>))]