diff --git a/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Resources.cs b/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Resources.cs
index 8ff10a5d..18c54fb3 100644
--- a/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Resources.cs
+++ b/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Resources.cs
@@ -3,6 +3,7 @@ using System.Diagnostics.Contracts;
using ComputeSharp.__Internals;
using ComputeSharp.Graphics.Helpers;
using ComputeSharp.Resources;
+using Microsoft.Toolkit.Diagnostics;
#pragma warning disable CS0618
@@ -1070,6 +1071,24 @@ public static partial class GraphicsDeviceExtensions
return new(device, width, height, depth, allocationMode);
}
+ ///
+ /// Loads a new readonly 2D texture with the contents of the specified file.
+ ///
+ /// The type of items to store in the texture.
+ /// The type of pixels used on the GPU side.
+ /// The instance to use to allocate the texture.
+ /// The filename of the image file to load and decode into the texture.
+ /// A instance with the contents of the specified file.
+ [Pure]
+ public static ReadOnlyTexture2D LoadReadOnlyTexture2D(this GraphicsDevice device, string filename)
+ where T : unmanaged, IUnorm
+ where TPixel : unmanaged
+ {
+ Guard.IsNotNull(filename, nameof(filename));
+
+ return device.LoadReadOnlyTexture2D(filename.AsSpan());
+ }
+
///
/// Loads a new readonly 2D texture with the contents of the specified file.
///
@@ -1114,6 +1133,24 @@ public static partial class GraphicsDeviceExtensions
return texture;
}
+ ///
+ /// Loads a new writeable 2D texture with the contents of the specified file.
+ ///
+ /// The type of items to store in the texture.
+ /// The type of pixels used on the GPU side.
+ /// The instance to use to allocate the texture.
+ /// The filename of the image file to load and decode into the texture.
+ /// A instance with the contents of the specified file.
+ [Pure]
+ public static ReadWriteTexture2D LoadReadWriteTexture2D(this GraphicsDevice device, string filename)
+ where T : unmanaged, IUnorm
+ where TPixel : unmanaged
+ {
+ Guard.IsNotNull(filename, nameof(filename));
+
+ return device.LoadReadWriteTexture2D(filename.AsSpan());
+ }
+
///
/// Loads a new writeable 2D texture with the contents of the specified file.
///
diff --git a/src/ComputeSharp/Graphics/Extensions/ReadBackTexture2DExtensions.cs b/src/ComputeSharp/Graphics/Extensions/ReadBackTexture2DExtensions.cs
index 8d855bfb..7a4b51cd 100644
--- a/src/ComputeSharp/Graphics/Extensions/ReadBackTexture2DExtensions.cs
+++ b/src/ComputeSharp/Graphics/Extensions/ReadBackTexture2DExtensions.cs
@@ -1,6 +1,7 @@
using System;
using ComputeSharp.Graphics.Helpers;
using ComputeSharp.Resources;
+using Microsoft.Toolkit.Diagnostics;
namespace ComputeSharp;
@@ -55,6 +56,20 @@ public static class ReadBackTexture2DExtensions
source.CopyTo(destination, sourceOffsetX, sourceOffsetY, destinationOffsetX, destinationOffsetY, width, height);
}
+ ///
+ /// Saves a texture to a specified file.
+ ///
+ /// The type of items to store in the texture.
+ /// The texture to save to an image.
+ /// The filename of the image file to save.
+ public static void Save(this ReadBackTexture2D texture, string filename)
+ where T : unmanaged
+ {
+ Guard.IsNotNull(filename, nameof(filename));
+
+ WICHelper.Instance.SaveTexture(texture.View, filename.AsSpan());
+ }
+
///
/// Saves a texture to a specified file.
///
diff --git a/src/ComputeSharp/Graphics/Extensions/Texture2DExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Texture2DExtensions.cs
index 3e651ab8..887659dd 100644
--- a/src/ComputeSharp/Graphics/Extensions/Texture2DExtensions.cs
+++ b/src/ComputeSharp/Graphics/Extensions/Texture2DExtensions.cs
@@ -535,6 +535,20 @@ public static class Texture2DExtensions
destination.CopyFrom(source, sourceOffsetX, sourceOffsetY, destinationOffsetX, destinationOffsetY, width, height);
}
+ ///
+ /// Saves a texture to a specified file.
+ ///
+ /// The type of items to store in the texture.
+ /// The texture to save to an image.
+ /// The filename of the image file to save.
+ public static void Save(this Texture2D texture, string filename)
+ where T : unmanaged
+ {
+ Guard.IsNotNull(filename, nameof(filename));
+
+ texture.Save(filename.AsSpan());
+ }
+
///
/// Saves a texture to a specified file.
///
diff --git a/tests/ComputeSharp.Tests/Extensions/GraphicsDeviceExtensions.cs b/tests/ComputeSharp.Tests/Extensions/GraphicsDeviceExtensions.cs
index ff225a90..0491aa04 100644
--- a/tests/ComputeSharp.Tests/Extensions/GraphicsDeviceExtensions.cs
+++ b/tests/ComputeSharp.Tests/Extensions/GraphicsDeviceExtensions.cs
@@ -306,19 +306,30 @@ public static class GraphicsDeviceExtensions
/// The type of items to store in the texture.
/// The type of pixels used on the GPU side.
/// The instance to use to allocate the texture.
- /// The type of texture to allocate.
+ /// The type of texture to allocate.
+ /// The type of filepath to use.
/// The filename of the image file to load and decode into the texture.
/// A instance with the contents of the specified file.
[Pure]
- public static Texture2D LoadTexture2D(this GraphicsDevice device, Type type, ReadOnlySpan filename)
+ public static Texture2D LoadTexture2D(this GraphicsDevice device, Type textureType, Type inputType, string filename)
where T : unmanaged, IUnorm
where TPixel : unmanaged
{
- return type switch
+ return textureType switch
{
- _ when type == typeof(ReadOnlyTexture2D<,>) => device.LoadReadOnlyTexture2D(filename),
- _ when type == typeof(ReadWriteTexture2D<,>) => device.LoadReadWriteTexture2D(filename),
- _ => throw new ArgumentException($"Invalid type: {type}", nameof(type))
+ _ when textureType == typeof(ReadOnlyTexture2D<,>) => inputType switch
+ {
+ _ when inputType == typeof(string) => device.LoadReadOnlyTexture2D(filename),
+ _ when inputType == typeof(ReadOnlySpan) => device.LoadReadOnlyTexture2D(filename.AsSpan()),
+ _ => throw new ArgumentException($"Invalid input type: {inputType}", nameof(inputType))
+ },
+ _ when textureType == typeof(ReadWriteTexture2D<,>) => inputType switch
+ {
+ _ when inputType == typeof(string) => device.LoadReadWriteTexture2D(filename),
+ _ when inputType == typeof(ReadOnlySpan) => device.LoadReadWriteTexture2D(filename.AsSpan()),
+ _ => throw new ArgumentException($"Invalid input type: {inputType}", nameof(inputType))
+ },
+ _ => throw new ArgumentException($"Invalid texture type: {textureType}", nameof(textureType))
};
}
@@ -343,4 +354,50 @@ public static class GraphicsDeviceExtensions
_ => throw new ArgumentException($"Invalid type: {type}", nameof(type))
};
}
+
+ ///
+ /// Saves a texture to a specified file.
+ ///
+ /// The type of items to store in the texture.
+ /// The texture to save to an image.
+ /// The type of filepath to use.
+ /// The filename of the image file to save.
+ public static void Save(this Texture2D texture, Type inputType, string filename)
+ where T : unmanaged
+ {
+ switch (inputType)
+ {
+ case var _ when inputType == typeof(string):
+ texture.Save(filename);
+ break;
+ case var _ when inputType == typeof(ReadOnlySpan):
+ texture.Save(filename.AsSpan());
+ break;
+ default:
+ throw new ArgumentException($"Invalid input type: {inputType}", nameof(inputType));
+ }
+ }
+
+ ///
+ /// Saves a texture to a specified file.
+ ///
+ /// The type of items to store in the texture.
+ /// The texture to save to an image.
+ /// The type of filepath to use.
+ /// The filename of the image file to save.
+ public static void Save(this ReadBackTexture2D texture, Type inputType, string filename)
+ where T : unmanaged
+ {
+ switch (inputType)
+ {
+ case var _ when inputType == typeof(string):
+ texture.Save(filename);
+ break;
+ case var _ when inputType == typeof(ReadOnlySpan):
+ texture.Save(filename.AsSpan());
+ break;
+ default:
+ throw new ArgumentException($"Invalid input type: {inputType}", nameof(inputType));
+ }
+ }
}
diff --git a/tests/ComputeSharp.Tests/ImagingTests.cs b/tests/ComputeSharp.Tests/ImagingTests.cs
index 40774b90..dc42f0a7 100644
--- a/tests/ComputeSharp.Tests/ImagingTests.cs
+++ b/tests/ComputeSharp.Tests/ImagingTests.cs
@@ -25,11 +25,13 @@ public class ImagingTests
[AllDevices]
[Resource(typeof(ReadOnlyTexture2D<,>))]
[Resource(typeof(ReadWriteTexture2D<,>))]
- public void LoadAsRgba32FromFile(Device device, Type textureType)
+ [Data(typeof(string))]
+ [Data(typeof(ReadOnlySpan))]
+ public void LoadAsRgba32FromFile(Device device, Type textureType, Type inputType)
{
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging", "city.jpg");
- using Texture2D texture = device.Get().LoadTexture2D(textureType, path);
+ using Texture2D texture = device.Get().LoadTexture2D(textureType, inputType, path);
using Image loaded = texture.ToImage();
using Image original = Image.Load(path);
@@ -41,11 +43,13 @@ public class ImagingTests
[AllDevices]
[Resource(typeof(ReadOnlyTexture2D<,>))]
[Resource(typeof(ReadWriteTexture2D<,>))]
- public void LoadAsBgra32FromFile(Device device, Type textureType)
+ [Data(typeof(string))]
+ [Data(typeof(ReadOnlySpan))]
+ public void LoadAsBgra32FromFile(Device device, Type textureType, Type inputType)
{
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging", "city.jpg");
- using Texture2D texture = device.Get().LoadTexture2D(textureType, path);
+ using Texture2D texture = device.Get().LoadTexture2D(textureType, inputType, path);
using Image loaded = texture.ToImage();
using Image original = Image.Load(path);
@@ -57,11 +61,13 @@ public class ImagingTests
[AllDevices]
[Resource(typeof(ReadOnlyTexture2D<,>))]
[Resource(typeof(ReadWriteTexture2D<,>))]
- public void LoadAsBgra32FromFileWithSameFormat(Device device, Type textureType)
+ [Data(typeof(string))]
+ [Data(typeof(ReadOnlySpan))]
+ public void LoadAsBgra32FromFileWithSameFormat(Device device, Type textureType, Type inputType)
{
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Assets", "CityAfter1024x1024Sampling.png");
- using Texture2D texture = device.Get().LoadTexture2D(textureType, path);
+ using Texture2D texture = device.Get().LoadTexture2D(textureType, inputType, path);
using Image loaded = texture.ToImage();
using Image original = Image.Load(path);
@@ -73,11 +79,13 @@ public class ImagingTests
[AllDevices]
[Resource(typeof(ReadOnlyTexture2D<,>))]
[Resource(typeof(ReadWriteTexture2D<,>))]
- public void LoadAsR8FromFile(Device device, Type textureType)
+ [Data(typeof(string))]
+ [Data(typeof(ReadOnlySpan))]
+ public void LoadAsR8FromFile(Device device, Type textureType, Type inputType)
{
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging", "city.jpg");
- using Texture2D texture = device.Get().LoadTexture2D(textureType, path);
+ using Texture2D texture = device.Get().LoadTexture2D(textureType, inputType, path);
using Image loaded = texture.ToImage();
using Image original = Image.Load(path);
@@ -103,19 +111,31 @@ public class ImagingTests
TolerantImageComparer.AssertEqual(original, loaded, 0.0000032f);
}
+ [CombinatorialTestMethod]
+ [Device(Device.Warp)]
+ [Resource(typeof(ReadOnlyTexture2D<,>))]
+ [Resource(typeof(ReadWriteTexture2D<,>))]
+ [ExpectedException(typeof(ArgumentNullException))]
+ public void LoadAsRgba32_WithNullPath(Device device, Type textureType)
+ {
+ using Texture2D texture = device.Get().LoadTexture2D(textureType, typeof(string), null!);
+ }
+
[CombinatorialTestMethod]
[AllDevices]
[Resource(typeof(ReadOnlyTexture2D<,>))]
[Resource(typeof(ReadWriteTexture2D<,>))]
- public void SaveRgba32AsJpeg(Device device, Type textureType)
+ [Data(typeof(string))]
+ [Data(typeof(ReadOnlySpan))]
+ public void SaveRgba32AsJpeg(Device device, Type textureType, Type inputType)
{
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging");
string expectedPath = Path.Combine(path, "city.jpg");
string actualPath = Path.Combine(path, "city_rgba32_saved.jpg");
- using Texture2D texture = device.Get().LoadTexture2D(textureType, path);
+ using Texture2D texture = device.Get().LoadTexture2D(textureType, inputType, expectedPath);
- texture.Save(actualPath);
+ texture.Save(inputType, actualPath);
TolerantImageComparer.AssertEqual(expectedPath, actualPath, 0.00001023f);
}
@@ -124,15 +144,56 @@ public class ImagingTests
[AllDevices]
[Resource(typeof(ReadOnlyTexture2D<,>))]
[Resource(typeof(ReadWriteTexture2D<,>))]
- public void SaveBgra32AsJpeg(Device device, Type textureType)
+ [Data(typeof(string))]
+ [Data(typeof(ReadOnlySpan))]
+ public void SaveRgba32AsJpeg_WithReadBackTexture(Device device, Type textureType, Type inputType)
+ {
+ string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging");
+ string expectedPath = Path.Combine(path, "city.jpg");
+ string actualPath = Path.Combine(path, "city_rgba32_saved.jpg");
+
+ using Texture2D texture = device.Get().LoadTexture2D(textureType, inputType, expectedPath);
+ using ReadBackTexture2D readback = device.Get().AllocateReadBackTexture2D(texture.Width, texture.Height);
+
+ texture.CopyTo(readback);
+
+ readback.Save(inputType, actualPath);
+
+ TolerantImageComparer.AssertEqual(expectedPath, actualPath, 0.00001023f);
+ }
+
+ [CombinatorialTestMethod]
+ [Device(Device.Warp)]
+ [Resource(typeof(ReadOnlyTexture2D<,>))]
+ [Resource(typeof(ReadWriteTexture2D<,>))]
+ [ExpectedException(typeof(ArgumentNullException))]
+ public void SaveRgba32AsJpeg_WithReadBackTexture_WithNullPath(Device device, Type textureType)
+ {
+ string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging", "city.jpg");
+
+ using Texture2D texture = device.Get().LoadTexture2D(textureType, typeof(string), path);
+ using ReadBackTexture2D readback = device.Get().AllocateReadBackTexture2D(texture.Width, texture.Height);
+
+ texture.CopyTo(readback);
+
+ readback.Save(typeof(string), null!);
+ }
+
+ [CombinatorialTestMethod]
+ [AllDevices]
+ [Resource(typeof(ReadOnlyTexture2D<,>))]
+ [Resource(typeof(ReadWriteTexture2D<,>))]
+ [Data(typeof(string))]
+ [Data(typeof(ReadOnlySpan))]
+ public void SaveBgra32AsJpeg(Device device, Type textureType, Type inputType)
{
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging");
string expectedPath = Path.Combine(path, "city.jpg");
string actualPath = Path.Combine(path, "city_bgra32_saved.jpg");
- using Texture2D texture = device.Get().LoadTexture2D(textureType, path);
+ using Texture2D texture = device.Get().LoadTexture2D(textureType, inputType, expectedPath);
- texture.Save(actualPath);
+ texture.Save(inputType, actualPath);
TolerantImageComparer.AssertEqual(expectedPath, actualPath, 0.00001023f);
}
@@ -141,16 +202,18 @@ public class ImagingTests
[AllDevices]
[Resource(typeof(ReadOnlyTexture2D<,>))]
[Resource(typeof(ReadWriteTexture2D<,>))]
- public void SaveR8AsJpeg(Device device, Type textureType)
+ [Data(typeof(string))]
+ [Data(typeof(ReadOnlySpan))]
+ public void SaveR8AsJpeg(Device device, Type textureType, Type inputType)
{
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging");
string sourcePath = Path.Combine(path, "city.jpg");
string expectedPath = Path.Combine(path, "city_r8_reference.jpg");
string actualPath = Path.Combine(path, "city_r8_saved.jpg");
- using Texture2D texture = device.Get().LoadTexture2D(textureType, path);
+ using Texture2D texture = device.Get().LoadTexture2D(textureType, inputType, sourcePath);
- texture.Save(actualPath);
+ texture.Save(inputType, actualPath);
using Image original = Image.Load(sourcePath);
@@ -159,6 +222,20 @@ public class ImagingTests
TolerantImageComparer.AssertEqual(expectedPath, actualPath, 0.00004037f);
}
+ [CombinatorialTestMethod]
+ [Device(Device.Warp)]
+ [Resource(typeof(ReadOnlyTexture2D<,>))]
+ [Resource(typeof(ReadWriteTexture2D<,>))]
+ [ExpectedException(typeof(ArgumentNullException))]
+ public void SaveRgba32AsJpeg_WithNullPath(Device device, Type textureType)
+ {
+ string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging", "city.jpg");
+
+ using Texture2D texture = device.Get().LoadTexture2D(textureType, typeof(string), path);
+
+ texture.Save(typeof(string), null!);
+ }
+
///
/// An tolerant image comparer type, from ImageSharp.
///