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 APIs.
///
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.
/// A instance of the requested size.
[Pure]
public static Buffer AllocateBuffer(this GraphicsDevice device, Type type, int length)
where T : unmanaged
{
return type switch
{
_ when type == typeof(ConstantBuffer<>) => device.AllocateConstantBuffer(length),
_ when type == typeof(ReadOnlyBuffer<>) => device.AllocateReadOnlyBuffer(length),
_ when type == typeof(ReadWriteBuffer<>) => device.AllocateReadWriteBuffer(length),
_ => 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.
/// A instance of the requested size.
[Pure]
public static Texture2D AllocateTexture2D(this GraphicsDevice device, Type type, int width, int height)
where T : 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.
///
/// 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.
///
/// 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.
/// 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
{
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.
///
/// 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.
///
/// 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))
};
}
}
}