using System;
using System.IO;
using ComputeSharp.Resources;
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.
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 allocation mode to use for the new resource.
/// A instance of the requested size.
public static Texture1D AllocateTexture1D(this GraphicsDevice device, Type type, int width, AllocationMode allocationMode = AllocationMode.Default)
where T : unmanaged
{
return type switch
{
_ when type == typeof(ReadOnlyTexture1D<>) => device.AllocateReadOnlyTexture1D(width, allocationMode),
_ when type == typeof(ReadWriteTexture1D<>) => device.AllocateReadWriteTexture1D(width, 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 allocation mode to use for the new resource.
/// A instance of the requested size.
public static Texture1D AllocateTexture1D(this GraphicsDevice device, Type type, int width, AllocationMode allocationMode = AllocationMode.Default)
where T : unmanaged, IPixel
where TPixel : unmanaged
{
return type switch
{
_ when type == typeof(ReadOnlyTexture1D<,>) => device.AllocateReadOnlyTexture1D(width, allocationMode),
_ when type == typeof(ReadWriteTexture1D<,>) => device.AllocateReadWriteTexture1D(width, 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.
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.
public static Texture2D AllocateTexture2D(this GraphicsDevice device, Type type, int width, int height, AllocationMode allocationMode = AllocationMode.Default)
where T : unmanaged, IPixel
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.
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.
public static Texture3D AllocateTexture3D(this GraphicsDevice device, Type type, int width, int height, int depth, AllocationMode allocationMode = AllocationMode.Default)
where T : unmanaged, IPixel
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.
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 allocation mode to use for the new resource.
/// A instance of the requested size.
public static TransferTexture1D AllocateTransferTexture1D(this GraphicsDevice device, Type type, int width, AllocationMode allocationMode = AllocationMode.Default)
where T : unmanaged
{
return type switch
{
_ when type == typeof(UploadTexture1D<>) => device.AllocateUploadTexture1D(width, allocationMode),
_ when type == typeof(ReadBackTexture1D<>) => device.AllocateReadBackTexture1D(width, 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.
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 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.
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.
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 texture to allocate.
/// The data to load on the texture.
/// A instance of the requested type.
public static Texture1D AllocateTexture1D(this GraphicsDevice device, Type type, T[] data)
where T : unmanaged
{
return type switch
{
_ when type == typeof(ReadOnlyTexture1D<>) => device.AllocateReadOnlyTexture1D(data),
_ when type == typeof(ReadWriteTexture1D<>) => device.AllocateReadWriteTexture1D(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 texture 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.
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 texture 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.
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.
/// The input 3D array to use as source data for the texture.
/// A instance of the requested size.
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.
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))
};
}
///
/// Loads an image from a specific file into a target instance.
///
/// The type of items to store in the texture.
/// The target instance.
/// The type of filepath to use.
/// The filename of the image file to load and decode into the texture.
public static void Load(this UploadTexture2D texture, Type inputType, string filename)
where T : unmanaged
{
switch (inputType)
{
case var _ when inputType == typeof(string):
texture.Load(filename);
break;
case var _ when inputType == typeof(ReadOnlySpan):
texture.Load(filename);
break;
default:
throw new ArgumentException($"Invalid input type: {inputType}", nameof(inputType));
}
}
///
/// 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 type of filepath to use.
/// The filename of the image file to load and decode into the texture.
/// A instance with the contents of the specified file.
public static Texture2D LoadTexture2D(this GraphicsDevice device, Type textureType, Type inputType, string filename)
where T : unmanaged, IPixel
where TPixel : unmanaged
{
return textureType switch
{
_ when textureType == typeof(ReadOnlyTexture2D<,>) => inputType switch
{
_ when inputType == typeof(string) => device.LoadReadOnlyTexture2D(filename),
_ when inputType == typeof(ReadOnlySpan) => device.LoadReadOnlyTexture2D(filename.AsSpan()),
_ => throw new ArgumentException($"Invalid input type: {inputType}", nameof(inputType))
},
_ when textureType == typeof(ReadWriteTexture2D<,>) => inputType switch
{
_ when inputType == typeof(string) => device.LoadReadWriteTexture2D(filename),
_ when inputType == typeof(ReadOnlySpan) => device.LoadReadWriteTexture2D(filename.AsSpan()),
_ => throw new ArgumentException($"Invalid input type: {inputType}", nameof(inputType))
},
_ => throw new ArgumentException($"Invalid texture type: {textureType}", nameof(textureType))
};
}
///
/// Loads a new readonly 2D texture with the contents of the specified buffer.
///
/// 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 buffer.
public static Texture2D LoadTexture2D(this GraphicsDevice device, Type type, byte[] buffer)
where T : unmanaged, IPixel
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))
};
}
///
/// Loads a new readonly 2D texture with the contents of the specified stream.
///
/// 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 stream with the image data to load and decode into the texture.
/// A instance with the contents of the specified stream.
public static Texture2D LoadTexture2D(this GraphicsDevice device, Type type, Stream stream)
where T : unmanaged, IPixel
where TPixel : unmanaged
{
return type switch
{
_ when type == typeof(ReadOnlyTexture2D<,>) => device.LoadReadOnlyTexture2D(stream),
_ when type == typeof(ReadWriteTexture2D<,>) => device.LoadReadWriteTexture2D(stream),
_ => throw new ArgumentException($"Invalid type: {type}", nameof(type))
};
}
///
/// Saves a texture to a specified file.
///
/// The type of items to store in the texture.
/// The texture to save to an image.
/// The type of filepath to use.
/// The filename of the image file to save.
public static void Save(this Texture2D texture, Type inputType, string filename)
where T : unmanaged
{
switch (inputType)
{
case var _ when inputType == typeof(string):
texture.Save(filename);
break;
case var _ when inputType == typeof(ReadOnlySpan):
texture.Save(filename.AsSpan());
break;
default:
throw new ArgumentException($"Invalid input type: {inputType}", nameof(inputType));
}
}
///
/// Saves a texture to a specified file.
///
/// The type of items to store in the texture.
/// The texture to save to an image.
/// The type of filepath to use.
/// The filename of the image file to save.
public static void Save(this ReadBackTexture2D texture, Type inputType, string filename)
where T : unmanaged
{
switch (inputType)
{
case var _ when inputType == typeof(string):
texture.Save(filename);
break;
case var _ when inputType == typeof(ReadOnlySpan):
texture.Save(filename.AsSpan());
break;
default:
throw new ArgumentException($"Invalid input type: {inputType}", nameof(inputType));
}
}
}