diff --git a/tests/ComputeSharp.Tests/Extensions/GraphicsDeviceExtensions.cs b/tests/ComputeSharp.Tests/Extensions/GraphicsDeviceExtensions.cs index ff9ba47e..f4005f85 100644 --- a/tests/ComputeSharp.Tests/Extensions/GraphicsDeviceExtensions.cs +++ b/tests/ComputeSharp.Tests/Extensions/GraphicsDeviceExtensions.cs @@ -1,8 +1,11 @@ using System; using System.Diagnostics.Contracts; +using ComputeSharp.__Internals; using ComputeSharp.Graphics; using ComputeSharp.Graphics.Buffers.Abstract; +#pragma warning disable CS0618 + namespace ComputeSharp.Tests.Extensions { /// @@ -52,6 +55,29 @@ namespace ComputeSharp.Tests.Extensions }; } + /// + /// Allocates a new instance of the specified type. + /// + /// The type of items in the texture. + /// The type of pixels used on the GPU side. + /// 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. + /// A instance of the requested size. + [Pure] + public static Texture2D AllocateTexture2D(this GraphicsDevice device, Type type, int width, int height) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + return type switch + { + _ when type == typeof(ReadOnlyTexture2D<,>) => device.AllocateReadOnlyTexture2D(width, height), + _ when type == typeof(ReadWriteTexture2D<,>) => device.AllocateReadWriteTexture2D(width, height), + _ => throw new ArgumentException($"Invalid type: {type}", nameof(type)) + }; + } + /// /// Allocates a new instance of the specified type. /// @@ -74,6 +100,30 @@ namespace ComputeSharp.Tests.Extensions }; } + /// + /// Allocates a new instance of the specified type. + /// + /// The type of items in the texture. + /// The type of pixels used on the GPU side. + /// 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. + /// A instance of the requested size. + [Pure] + public static Texture3D AllocateTexture3D(this GraphicsDevice device, Type type, int width, int height, int depth) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + return type switch + { + _ when type == typeof(ReadOnlyTexture3D<,>) => device.AllocateReadOnlyTexture3D(width, height, depth), + _ when type == typeof(ReadWriteTexture3D<,>) => device.AllocateReadWriteTexture3D(width, height, depth), + _ => throw new ArgumentException($"Invalid type: {type}", nameof(type)) + }; + } + /// /// Allocates a new instance of the specified type. /// diff --git a/tests/ComputeSharp.Tests/Texture2DTests.Pixels.cs b/tests/ComputeSharp.Tests/Texture2DTests.Pixels.cs new file mode 100644 index 00000000..1b0e1fb0 --- /dev/null +++ b/tests/ComputeSharp.Tests/Texture2DTests.Pixels.cs @@ -0,0 +1,87 @@ +using System; +using System.Numerics; +using System.Reflection; +using ComputeSharp.__Internals; +using ComputeSharp.Graphics.Buffers.Abstract; +using ComputeSharp.Tests.Extensions; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +#pragma warning disable CS0618 + +namespace ComputeSharp.Tests +{ + public partial class Texture2DTests + { + [TestMethod] + [DataRow(typeof(ReadOnlyTexture2D<,>), typeof(Bgra32), typeof(Vector4))] + [DataRow(typeof(ReadOnlyTexture2D<,>), typeof(Bgra32), typeof(Float4))] + [DataRow(typeof(ReadOnlyTexture2D<,>), typeof(Bgra32), typeof(Double4))] + [DataRow(typeof(ReadOnlyTexture2D<,>), typeof(R16), typeof(float))] + [DataRow(typeof(ReadOnlyTexture2D<,>), typeof(R16), typeof(double))] + [DataRow(typeof(ReadOnlyTexture2D<,>), typeof(R8), typeof(float))] + [DataRow(typeof(ReadOnlyTexture2D<,>), typeof(R8), typeof(double))] + [DataRow(typeof(ReadOnlyTexture2D<,>), typeof(Rg16), typeof(Vector2))] + [DataRow(typeof(ReadOnlyTexture2D<,>), typeof(Rg16), typeof(Float2))] + [DataRow(typeof(ReadOnlyTexture2D<,>), typeof(Rg16), typeof(Double2))] + [DataRow(typeof(ReadOnlyTexture2D<,>), typeof(Rg32), typeof(Vector2))] + [DataRow(typeof(ReadOnlyTexture2D<,>), typeof(Rg32), typeof(Float2))] + [DataRow(typeof(ReadOnlyTexture2D<,>), typeof(Rg32), typeof(Double2))] + [DataRow(typeof(ReadOnlyTexture2D<,>), typeof(Rgba32), typeof(Vector4))] + [DataRow(typeof(ReadOnlyTexture2D<,>), typeof(Rgba32), typeof(Float4))] + [DataRow(typeof(ReadOnlyTexture2D<,>), typeof(Rgba32), typeof(Double4))] + [DataRow(typeof(ReadOnlyTexture2D<,>), typeof(Rgba64), typeof(Vector4))] + [DataRow(typeof(ReadOnlyTexture2D<,>), typeof(Rgba64), typeof(Float4))] + [DataRow(typeof(ReadOnlyTexture2D<,>), typeof(Rgba64), typeof(Double4))] + [DataRow(typeof(ReadWriteTexture2D<,>), typeof(Bgra32), typeof(Vector4))] + [DataRow(typeof(ReadWriteTexture2D<,>), typeof(Bgra32), typeof(Float4))] + [DataRow(typeof(ReadWriteTexture2D<,>), typeof(Bgra32), typeof(Double4))] + [DataRow(typeof(ReadWriteTexture2D<,>), typeof(R16), typeof(float))] + [DataRow(typeof(ReadWriteTexture2D<,>), typeof(R16), typeof(double))] + [DataRow(typeof(ReadWriteTexture2D<,>), typeof(R8), typeof(float))] + [DataRow(typeof(ReadWriteTexture2D<,>), typeof(R8), typeof(double))] + [DataRow(typeof(ReadWriteTexture2D<,>), typeof(Rg16), typeof(Vector2))] + [DataRow(typeof(ReadWriteTexture2D<,>), typeof(Rg16), typeof(Float2))] + [DataRow(typeof(ReadWriteTexture2D<,>), typeof(Rg16), typeof(Double2))] + [DataRow(typeof(ReadWriteTexture2D<,>), typeof(Rg32), typeof(Vector2))] + [DataRow(typeof(ReadWriteTexture2D<,>), typeof(Rg32), typeof(Float2))] + [DataRow(typeof(ReadWriteTexture2D<,>), typeof(Rg32), typeof(Double2))] + [DataRow(typeof(ReadWriteTexture2D<,>), typeof(Rgba32), typeof(Vector4))] + [DataRow(typeof(ReadWriteTexture2D<,>), typeof(Rgba32), typeof(Float4))] + [DataRow(typeof(ReadWriteTexture2D<,>), typeof(Rgba32), typeof(Double4))] + [DataRow(typeof(ReadWriteTexture2D<,>), typeof(Rgba64), typeof(Vector4))] + [DataRow(typeof(ReadWriteTexture2D<,>), typeof(Rgba64), typeof(Float4))] + [DataRow(typeof(ReadWriteTexture2D<,>), typeof(Rgba64), typeof(Double4))] + public void Allocate_Uninitialized_Pixel_Ok(Type textureType, Type t, Type tPixel) + { + static void Test(Type textureType) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + using Texture2D texture = Gpu.Default.AllocateTexture2D(textureType, 128, 128); + + Assert.IsNotNull(texture); + Assert.AreEqual(texture.Width, 128); + Assert.AreEqual(texture.Height, 128); + Assert.AreSame(texture.GraphicsDevice, Gpu.Default); + + if (textureType == typeof(ReadOnlyTexture2D<,>)) + { + Assert.IsTrue(texture is ReadOnlyTexture2D); + } + else + { + Assert.IsTrue(texture is ReadWriteTexture2D); + } + } + + try + { + new Action(Test).Method.GetGenericMethodDefinition().MakeGenericMethod(t, tPixel).Invoke(null, new[] { textureType }); + } + catch (TargetInvocationException e) when (e.InnerException is not null) + { + throw e.InnerException; + } + } + } +} diff --git a/tests/ComputeSharp.Tests/Texture3DTests.Pixels.cs b/tests/ComputeSharp.Tests/Texture3DTests.Pixels.cs new file mode 100644 index 00000000..0057e024 --- /dev/null +++ b/tests/ComputeSharp.Tests/Texture3DTests.Pixels.cs @@ -0,0 +1,88 @@ +using System; +using System.Numerics; +using System.Reflection; +using ComputeSharp.__Internals; +using ComputeSharp.Graphics.Buffers.Abstract; +using ComputeSharp.Tests.Extensions; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +#pragma warning disable CS0618 + +namespace ComputeSharp.Tests +{ + public partial class Texture3DTests + { + [TestMethod] + [DataRow(typeof(ReadOnlyTexture3D<,>), typeof(Bgra32), typeof(Vector4))] + [DataRow(typeof(ReadOnlyTexture3D<,>), typeof(Bgra32), typeof(Float4))] + [DataRow(typeof(ReadOnlyTexture3D<,>), typeof(Bgra32), typeof(Double4))] + [DataRow(typeof(ReadOnlyTexture3D<,>), typeof(R16), typeof(float))] + [DataRow(typeof(ReadOnlyTexture3D<,>), typeof(R16), typeof(double))] + [DataRow(typeof(ReadOnlyTexture3D<,>), typeof(R8), typeof(float))] + [DataRow(typeof(ReadOnlyTexture3D<,>), typeof(R8), typeof(double))] + [DataRow(typeof(ReadOnlyTexture3D<,>), typeof(Rg16), typeof(Vector2))] + [DataRow(typeof(ReadOnlyTexture3D<,>), typeof(Rg16), typeof(Float2))] + [DataRow(typeof(ReadOnlyTexture3D<,>), typeof(Rg16), typeof(Double2))] + [DataRow(typeof(ReadOnlyTexture3D<,>), typeof(Rg32), typeof(Vector2))] + [DataRow(typeof(ReadOnlyTexture3D<,>), typeof(Rg32), typeof(Float2))] + [DataRow(typeof(ReadOnlyTexture3D<,>), typeof(Rg32), typeof(Double2))] + [DataRow(typeof(ReadOnlyTexture3D<,>), typeof(Rgba32), typeof(Vector4))] + [DataRow(typeof(ReadOnlyTexture3D<,>), typeof(Rgba32), typeof(Float4))] + [DataRow(typeof(ReadOnlyTexture3D<,>), typeof(Rgba32), typeof(Double4))] + [DataRow(typeof(ReadOnlyTexture3D<,>), typeof(Rgba64), typeof(Vector4))] + [DataRow(typeof(ReadOnlyTexture3D<,>), typeof(Rgba64), typeof(Float4))] + [DataRow(typeof(ReadOnlyTexture3D<,>), typeof(Rgba64), typeof(Double4))] + [DataRow(typeof(ReadWriteTexture3D<,>), typeof(Bgra32), typeof(Vector4))] + [DataRow(typeof(ReadWriteTexture3D<,>), typeof(Bgra32), typeof(Float4))] + [DataRow(typeof(ReadWriteTexture3D<,>), typeof(Bgra32), typeof(Double4))] + [DataRow(typeof(ReadWriteTexture3D<,>), typeof(R16), typeof(float))] + [DataRow(typeof(ReadWriteTexture3D<,>), typeof(R16), typeof(double))] + [DataRow(typeof(ReadWriteTexture3D<,>), typeof(R8), typeof(float))] + [DataRow(typeof(ReadWriteTexture3D<,>), typeof(R8), typeof(double))] + [DataRow(typeof(ReadWriteTexture3D<,>), typeof(Rg16), typeof(Vector2))] + [DataRow(typeof(ReadWriteTexture3D<,>), typeof(Rg16), typeof(Float2))] + [DataRow(typeof(ReadWriteTexture3D<,>), typeof(Rg16), typeof(Double2))] + [DataRow(typeof(ReadWriteTexture3D<,>), typeof(Rg32), typeof(Vector2))] + [DataRow(typeof(ReadWriteTexture3D<,>), typeof(Rg32), typeof(Float2))] + [DataRow(typeof(ReadWriteTexture3D<,>), typeof(Rg32), typeof(Double2))] + [DataRow(typeof(ReadWriteTexture3D<,>), typeof(Rgba32), typeof(Vector4))] + [DataRow(typeof(ReadWriteTexture3D<,>), typeof(Rgba32), typeof(Float4))] + [DataRow(typeof(ReadWriteTexture3D<,>), typeof(Rgba32), typeof(Double4))] + [DataRow(typeof(ReadWriteTexture3D<,>), typeof(Rgba64), typeof(Vector4))] + [DataRow(typeof(ReadWriteTexture3D<,>), typeof(Rgba64), typeof(Float4))] + [DataRow(typeof(ReadWriteTexture3D<,>), typeof(Rgba64), typeof(Double4))] + public void Allocate_Uninitialized_Pixel_Ok(Type textureType, Type t, Type tPixel) + { + static void Test(Type textureType) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + using Texture3D texture = Gpu.Default.AllocateTexture3D(textureType, 128, 128, 2); + + Assert.IsNotNull(texture); + Assert.AreEqual(texture.Width, 128); + Assert.AreEqual(texture.Height, 128); + Assert.AreEqual(texture.Depth, 2); + Assert.AreSame(texture.GraphicsDevice, Gpu.Default); + + if (textureType == typeof(ReadOnlyTexture3D<,>)) + { + Assert.IsTrue(texture is ReadOnlyTexture3D); + } + else + { + Assert.IsTrue(texture is ReadWriteTexture3D); + } + } + + try + { + new Action(Test).Method.GetGenericMethodDefinition().MakeGenericMethod(t, tPixel).Invoke(null, new[] { textureType }); + } + catch (TargetInvocationException e) when (e.InnerException is not null) + { + throw e.InnerException; + } + } + } +}