From fcc99d20aac8d1333ac6e30be5536d14d5d6e6c9 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 16 Nov 2021 20:19:01 +0100 Subject: [PATCH] Improve tests for loading/saving images --- .../Extensions/GraphicsDeviceExtensions.cs | 54 +++++++++++++++++-- tests/ComputeSharp.Tests/ImagingTests.cs | 49 +++++++++++------ 2 files changed, 82 insertions(+), 21 deletions(-) diff --git a/tests/ComputeSharp.Tests/Extensions/GraphicsDeviceExtensions.cs b/tests/ComputeSharp.Tests/Extensions/GraphicsDeviceExtensions.cs index bd2c2c39..ff225a90 100644 --- a/tests/ComputeSharp.Tests/Extensions/GraphicsDeviceExtensions.cs +++ b/tests/ComputeSharp.Tests/Extensions/GraphicsDeviceExtensions.cs @@ -174,9 +174,9 @@ public static class GraphicsDeviceExtensions /// /// Allocates a new instance of the specified type. /// - /// The type of items in the buffer. - /// The target instance to allocate the buffer for. - /// The type of buffer to allocate. + /// The type of items in the texture. + /// The target instance to allocate the texture for. + /// The type of texture to allocate. /// The width of the texture to create. /// The height of the texture to create. /// The depth of the texture to create. @@ -220,7 +220,7 @@ public static class GraphicsDeviceExtensions /// /// The type of items in the texture. /// The target instance to allocate the texture for. - /// The type of buffer to allocate. + /// The type of texture to allocate. /// The data to load on the texture. /// The width of the texture to create. /// The height of the texture to create. @@ -242,7 +242,7 @@ public static class GraphicsDeviceExtensions /// /// The type of items in the texture. /// The target instance to allocate the texture for. - /// The type of buffer to allocate. + /// The type of texture to allocate. /// The data to load on the texture. /// The width of the texture to create. /// The height of the texture to create. @@ -299,4 +299,48 @@ public static class GraphicsDeviceExtensions _ => throw new ArgumentException($"Invalid type: {type}", nameof(type)) }; } + + /// + /// 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 type of texture to allocate. + /// 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) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + return type switch + { + _ when type == typeof(ReadOnlyTexture2D<,>) => device.LoadReadOnlyTexture2D(filename), + _ when type == typeof(ReadWriteTexture2D<,>) => device.LoadReadWriteTexture2D(filename), + _ => throw new ArgumentException($"Invalid type: {type}", nameof(type)) + }; + } + + /// + /// 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 type of texture to allocate. + /// The buffer with the image data 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, byte[] buffer) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + return type switch + { + _ when type == typeof(ReadOnlyTexture2D<,>) => device.LoadReadOnlyTexture2D(buffer), + _ when type == typeof(ReadWriteTexture2D<,>) => device.LoadReadWriteTexture2D(buffer), + _ => throw new ArgumentException($"Invalid type: {type}", nameof(type)) + }; + } } diff --git a/tests/ComputeSharp.Tests/ImagingTests.cs b/tests/ComputeSharp.Tests/ImagingTests.cs index 54b2cef1..40774b90 100644 --- a/tests/ComputeSharp.Tests/ImagingTests.cs +++ b/tests/ComputeSharp.Tests/ImagingTests.cs @@ -4,6 +4,7 @@ using System.IO; using System.Reflection; using System.Runtime.CompilerServices; using System.Text; +using ComputeSharp.Resources; using ComputeSharp.Tests.Attributes; using ComputeSharp.Tests.Extensions; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -22,11 +23,13 @@ public class ImagingTests { [CombinatorialTestMethod] [AllDevices] - public void LoadAsRgba32FromFile(Device device) + [Resource(typeof(ReadOnlyTexture2D<,>))] + [Resource(typeof(ReadWriteTexture2D<,>))] + public void LoadAsRgba32FromFile(Device device, Type textureType) { string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging", "city.jpg"); - using ReadOnlyTexture2D texture = device.Get().LoadReadOnlyTexture2D(path); + using Texture2D texture = device.Get().LoadTexture2D(textureType, path); using Image loaded = texture.ToImage(); using Image original = Image.Load(path); @@ -36,11 +39,13 @@ public class ImagingTests [CombinatorialTestMethod] [AllDevices] - public void LoadAsBgra32FromFile(Device device) + [Resource(typeof(ReadOnlyTexture2D<,>))] + [Resource(typeof(ReadWriteTexture2D<,>))] + public void LoadAsBgra32FromFile(Device device, Type textureType) { string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging", "city.jpg"); - using ReadOnlyTexture2D texture = device.Get().LoadReadOnlyTexture2D(path); + using Texture2D texture = device.Get().LoadTexture2D(textureType, path); using Image loaded = texture.ToImage(); using Image original = Image.Load(path); @@ -50,11 +55,13 @@ public class ImagingTests [CombinatorialTestMethod] [AllDevices] - public void LoadAsBgra32FromFileWithSameFormat(Device device) + [Resource(typeof(ReadOnlyTexture2D<,>))] + [Resource(typeof(ReadWriteTexture2D<,>))] + public void LoadAsBgra32FromFileWithSameFormat(Device device, Type textureType) { string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Assets", "CityAfter1024x1024Sampling.png"); - using ReadOnlyTexture2D texture = device.Get().LoadReadOnlyTexture2D(path); + using Texture2D texture = device.Get().LoadTexture2D(textureType, path); using Image loaded = texture.ToImage(); using Image original = Image.Load(path); @@ -64,11 +71,13 @@ public class ImagingTests [CombinatorialTestMethod] [AllDevices] - public void LoadAsR8FromFile(Device device) + [Resource(typeof(ReadOnlyTexture2D<,>))] + [Resource(typeof(ReadWriteTexture2D<,>))] + public void LoadAsR8FromFile(Device device, Type textureType) { string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging", "city.jpg"); - using ReadOnlyTexture2D texture = device.Get().LoadReadOnlyTexture2D(path); + using Texture2D texture = device.Get().LoadTexture2D(textureType, path); using Image loaded = texture.ToImage(); using Image original = Image.Load(path); @@ -78,13 +87,15 @@ public class ImagingTests [CombinatorialTestMethod] [AllDevices] - public void LoadAsRgba32FromBuffer(Device device) + [Resource(typeof(ReadOnlyTexture2D<,>))] + [Resource(typeof(ReadWriteTexture2D<,>))] + public void LoadAsRgba32FromBuffer(Device device, Type textureType) { string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging", "city.jpg"); byte[] buffer = File.ReadAllBytes(path); - using ReadOnlyTexture2D texture = device.Get().LoadReadOnlyTexture2D(buffer); + using Texture2D texture = device.Get().LoadTexture2D(textureType, buffer); using Image loaded = texture.ToImage(); using Image original = Image.Load(path); @@ -94,13 +105,15 @@ public class ImagingTests [CombinatorialTestMethod] [AllDevices] - public void SaveRgba32AsJpeg(Device device) + [Resource(typeof(ReadOnlyTexture2D<,>))] + [Resource(typeof(ReadWriteTexture2D<,>))] + public void SaveRgba32AsJpeg(Device device, Type textureType) { 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 ReadOnlyTexture2D texture = device.Get().LoadReadOnlyTexture2D(expectedPath); + using Texture2D texture = device.Get().LoadTexture2D(textureType, path); texture.Save(actualPath); @@ -109,13 +122,15 @@ public class ImagingTests [CombinatorialTestMethod] [AllDevices] - public void SaveBgra32AsJpeg(Device device) + [Resource(typeof(ReadOnlyTexture2D<,>))] + [Resource(typeof(ReadWriteTexture2D<,>))] + public void SaveBgra32AsJpeg(Device device, Type textureType) { 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 ReadOnlyTexture2D texture = device.Get().LoadReadOnlyTexture2D(expectedPath); + using Texture2D texture = device.Get().LoadTexture2D(textureType, path); texture.Save(actualPath); @@ -124,14 +139,16 @@ public class ImagingTests [CombinatorialTestMethod] [AllDevices] - public void SaveR8AsJpeg(Device device) + [Resource(typeof(ReadOnlyTexture2D<,>))] + [Resource(typeof(ReadWriteTexture2D<,>))] + public void SaveR8AsJpeg(Device device, Type textureType) { 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 ReadOnlyTexture2D texture = device.Get().LoadReadOnlyTexture2D(sourcePath); + using Texture2D texture = device.Get().LoadTexture2D(textureType, path); texture.Save(actualPath);