using System; using System.Diagnostics.Contracts; using ComputeSharp.__Internals; using ComputeSharp.Resources; #pragma warning disable CS0618 namespace ComputeSharp.Tests.Extensions; /// /// A helper class for testing resource types. /// 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 length of the buffer to create. /// The allocation mode to use for the new resource. /// A instance of the requested size. [Pure] public static Buffer AllocateBuffer(this GraphicsDevice device, Type type, int length, AllocationMode allocationMode = AllocationMode.Default) where T : unmanaged { return type switch { _ when type == typeof(ConstantBuffer<>) => device.AllocateConstantBuffer(length, allocationMode), _ when type == typeof(ReadOnlyBuffer<>) => device.AllocateReadOnlyBuffer(length, allocationMode), _ when type == typeof(ReadWriteBuffer<>) => device.AllocateReadWriteBuffer(length, allocationMode), _ => throw new ArgumentException($"Invalid type: {type}", nameof(type)) }; } /// /// Allocates a new instance of the specified type. /// /// 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 allocation mode to use for the new resource. /// A instance of the requested size. [Pure] public static Texture2D AllocateTexture2D(this GraphicsDevice device, Type type, int width, int height, AllocationMode allocationMode = AllocationMode.Default) where T : unmanaged { return type switch { _ when type == typeof(ReadOnlyTexture2D<>) => device.AllocateReadOnlyTexture2D(width, height, allocationMode), _ when type == typeof(ReadWriteTexture2D<>) => device.AllocateReadWriteTexture2D(width, height, allocationMode), _ => throw new ArgumentException($"Invalid type: {type}", nameof(type)) }; } /// /// 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 allocation mode to use for the new resource. /// A instance of the requested size. [Pure] public static Texture2D AllocateTexture2D(this GraphicsDevice device, Type type, int width, int height, AllocationMode allocationMode = AllocationMode.Default) where T : unmanaged, IUnorm where TPixel : unmanaged { return type switch { _ when type == typeof(ReadOnlyTexture2D<,>) => device.AllocateReadOnlyTexture2D(width, height, allocationMode), _ when type == typeof(ReadWriteTexture2D<,>) => device.AllocateReadWriteTexture2D(width, height, allocationMode), _ => throw new ArgumentException($"Invalid type: {type}", nameof(type)) }; } /// /// Allocates a new instance of the specified type. /// /// 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. /// The allocation mode to use for the new resource. /// A instance of the requested size. [Pure] public static Texture3D AllocateTexture3D(this GraphicsDevice device, Type type, int width, int height, int depth, AllocationMode allocationMode = AllocationMode.Default) where T : unmanaged { return type switch { _ when type == typeof(ReadOnlyTexture3D<>) => device.AllocateReadOnlyTexture3D(width, height, depth, allocationMode), _ when type == typeof(ReadWriteTexture3D<>) => device.AllocateReadWriteTexture3D(width, height, depth, allocationMode), _ => throw new ArgumentException($"Invalid type: {type}", nameof(type)) }; } /// /// 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. /// The allocation mode to use for the new resource. /// A instance of the requested size. [Pure] public static Texture3D AllocateTexture3D(this GraphicsDevice device, Type type, int width, int height, int depth, AllocationMode allocationMode = AllocationMode.Default) where T : unmanaged, IUnorm where TPixel : unmanaged { return type switch { _ when type == typeof(ReadOnlyTexture3D<,>) => device.AllocateReadOnlyTexture3D(width, height, depth, allocationMode), _ when type == typeof(ReadWriteTexture3D<,>) => device.AllocateReadWriteTexture3D(width, height, depth, allocationMode), _ => throw new ArgumentException($"Invalid type: {type}", nameof(type)) }; } /// /// 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 length of the buffer to create. /// The allocation mode to use for the new resource. /// A instance of the requested size. [Pure] public static TransferBuffer AllocateTransferBuffer(this GraphicsDevice device, Type type, int length, AllocationMode allocationMode = AllocationMode.Default) where T : unmanaged { return type switch { _ when type == typeof(UploadBuffer<>) => device.AllocateUploadBuffer(length, allocationMode), _ when type == typeof(ReadBackBuffer<>) => device.AllocateReadBackBuffer(length, allocationMode), _ => throw new ArgumentException($"Invalid type: {type}", nameof(type)) }; } /// /// 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 width of the texture to create. /// The height of the texture to create. /// The allocation mode to use for the new resource. /// A instance of the requested size. [Pure] public static TransferTexture2D AllocateTransferTexture2D(this GraphicsDevice device, Type type, int width, int height, AllocationMode allocationMode = AllocationMode.Default) where T : unmanaged { return type switch { _ when type == typeof(UploadTexture2D<>) => device.AllocateUploadTexture2D(width, height, allocationMode), _ when type == typeof(ReadBackTexture2D<>) => device.AllocateReadBackTexture2D(width, height, allocationMode), _ => throw new ArgumentException($"Invalid type: {type}", nameof(type)) }; } /// /// 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 width of the texture to create. /// The height of the texture to create. /// The depth of the texture to create. /// The allocation mode to use for the new resource. /// A instance of the requested size. [Pure] public static TransferTexture3D AllocateTransferTexture3D(this GraphicsDevice device, Type type, int width, int height, int depth, AllocationMode allocationMode = AllocationMode.Default) where T : unmanaged { return type switch { _ when type == typeof(UploadTexture3D<>) => device.AllocateUploadTexture3D(width, height, depth, allocationMode), _ when type == typeof(ReadBackTexture3D<>) => device.AllocateReadBackTexture3D(width, height, depth, allocationMode), _ => throw new ArgumentException($"Invalid type: {type}", nameof(type)) }; } /// /// 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 data to load on the buffer. /// A instance of the requested type. [Pure] public static Buffer AllocateBuffer(this GraphicsDevice device, Type type, T[] data) where T : unmanaged { return type switch { _ when type == typeof(ConstantBuffer<>) => device.AllocateConstantBuffer(data), _ when type == typeof(ReadOnlyBuffer<>) => device.AllocateReadOnlyBuffer(data), _ when type == typeof(ReadWriteBuffer<>) => device.AllocateReadWriteBuffer(data), _ => throw new ArgumentException($"Invalid type: {type}", nameof(type)) }; } /// /// Allocates a new instance of the specified type. /// /// The type of items in the texture. /// The target instance to allocate the texture for. /// The type of buffer to allocate. /// The data to load on the texture. /// The width of the texture to create. /// The height of the texture to create. /// A instance of the requested type. [Pure] public static Texture2D AllocateTexture2D(this GraphicsDevice device, Type type, T[] data, int width, int height) where T : unmanaged { return type switch { _ when type == typeof(ReadOnlyTexture2D<>) => device.AllocateReadOnlyTexture2D(data, width, height), _ when type == typeof(ReadWriteTexture2D<>) => device.AllocateReadWriteTexture2D(data, width, height), _ => throw new ArgumentException($"Invalid type: {type}", nameof(type)) }; } /// /// Allocates a new instance of the specified type. /// /// The type of items in the texture. /// The target instance to allocate the texture for. /// The type of buffer to allocate. /// The data to load on the texture. /// 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 type. [Pure] public static Texture3D AllocateTexture3D(this GraphicsDevice device, Type type, T[] data, int width, int height, int depth) where T : unmanaged { return type switch { _ when type == typeof(ReadOnlyTexture3D<>) => device.AllocateReadOnlyTexture3D(data, width, height, depth), _ when type == typeof(ReadWriteTexture3D<>) => device.AllocateReadWriteTexture3D(data, width, height, depth), _ => throw new ArgumentException($"Invalid type: {type}", nameof(type)) }; } /// /// Allocates a new instance of the specified type. /// /// The type of items in the texture. /// The target instance to allocate the texture for. /// The type of texture to allocate. /// A instance of the requested size. [Pure] public static Texture3D AllocateTexture3D(this GraphicsDevice device, Type type, T[,,] data) where T : unmanaged { return type switch { _ when type == typeof(ReadOnlyTexture3D<>) => device.AllocateReadOnlyTexture3D(data), _ when type == typeof(ReadWriteTexture3D<>) => device.AllocateReadWriteTexture3D(data), _ => throw new ArgumentException($"Invalid type: {type}", nameof(type)) }; } /// /// 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 data to load on the buffer. /// A instance of the requested type. [Pure] public static Buffer AllocateBuffer(this GraphicsDevice device, Type type, Buffer data) where T : unmanaged { return type switch { _ when type == typeof(ConstantBuffer<>) => device.AllocateConstantBuffer(data), _ when type == typeof(ReadOnlyBuffer<>) => device.AllocateReadOnlyBuffer(data), _ when type == typeof(ReadWriteBuffer<>) => device.AllocateReadWriteBuffer(data), _ => throw new ArgumentException($"Invalid type: {type}", nameof(type)) }; } }