using System;
using System.Diagnostics.Contracts;
using ComputeSharp.Graphics;
using ComputeSharp.Graphics.Buffers.Abstract;
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 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 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))
};
}
}
}