Improve tests for loading/saving images

このコミットが含まれているのは:
Sergio Pedri committed 2021-11-16 20:19:01 +01:00
1 親 a0ee841edb
コミット fcc99d20aa
2ファイル変更
+82 -21

検索条件に一致するファイルはありません

+49 -5
ファイルの表示
@@ -174,9 +174,9 @@ public static class GraphicsDeviceExtensions
/// <summary>
/// Allocates a new <see cref="TransferTexture3D{T}"/> instance of the specified type.
/// </summary>
/// <typeparam name="T">The type of items in the buffer.</typeparam>
/// <param name="device">The target <see cref="GraphicsDevice"/> instance to allocate the buffer for.</param>
/// <param name="type">The type of buffer to allocate.</param>
/// <typeparam name="T">The type of items in the texture.</typeparam>
/// <param name="device">The target <see cref="GraphicsDevice"/> instance to allocate the texture for.</param>
/// <param name="type">The type of texture to allocate.</param>
/// <param name="width">The width of the texture to create.</param>
/// <param name="height">The height of the texture to create.</param>
/// <param name="depth">The depth of the texture to create.</param>
@@ -220,7 +220,7 @@ public static class GraphicsDeviceExtensions
/// </summary>
/// <typeparam name="T">The type of items in the texture.</typeparam>
/// <param name="device">The target <see cref="GraphicsDevice"/> instance to allocate the texture for.</param>
/// <param name="type">The type of buffer to allocate.</param>
/// <param name="type">The type of texture to allocate.</param>
/// <param name="data">The data to load on the texture.</param>
/// <param name="width">The width of the texture to create.</param>
/// <param name="height">The height of the texture to create.</param>
@@ -242,7 +242,7 @@ public static class GraphicsDeviceExtensions
/// </summary>
/// <typeparam name="T">The type of items in the texture.</typeparam>
/// <param name="device">The target <see cref="GraphicsDevice"/> instance to allocate the texture for.</param>
/// <param name="type">The type of buffer to allocate.</param>
/// <param name="type">The type of texture to allocate.</param>
/// <param name="data">The data to load on the texture.</param>
/// <param name="width">The width of the texture to create.</param>
/// <param name="height">The height of the texture to create.</param>
@@ -299,4 +299,48 @@ public static class GraphicsDeviceExtensions
_ => throw new ArgumentException($"Invalid type: {type}", nameof(type))
};
}
/// <summary>
/// Loads a new readonly 2D texture with the contents of the specified file.
/// </summary>
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
/// <typeparam name="TPixel">The type of pixels used on the GPU side.</typeparam>
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the texture.</param>
/// <param name="type">The type of texture to allocate.</param>
/// <param name="filename">The filename of the image file to load and decode into the texture.</param>
/// <returns>A <see cref="Texture2D{T, TPixel}"/> instance with the contents of the specified file.</returns>
[Pure]
public static Texture2D<T> LoadTexture2D<T, TPixel>(this GraphicsDevice device, Type type, ReadOnlySpan<char> filename)
where T : unmanaged, IUnorm<TPixel>
where TPixel : unmanaged
{
return type switch
{
_ when type == typeof(ReadOnlyTexture2D<,>) => device.LoadReadOnlyTexture2D<T, TPixel>(filename),
_ when type == typeof(ReadWriteTexture2D<,>) => device.LoadReadWriteTexture2D<T, TPixel>(filename),
_ => throw new ArgumentException($"Invalid type: {type}", nameof(type))
};
}
/// <summary>
/// Loads a new readonly 2D texture with the contents of the specified file.
/// </summary>
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
/// <typeparam name="TPixel">The type of pixels used on the GPU side.</typeparam>
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the texture.</param>
/// <param name="type">The type of texture to allocate.</param>
/// <param name="buffer">The buffer with the image data to load and decode into the texture.</param>
/// <returns>A <see cref="Texture2D{T, TPixel}"/> instance with the contents of the specified file.</returns>
[Pure]
public static Texture2D<T> LoadTexture2D<T, TPixel>(this GraphicsDevice device, Type type, byte[] buffer)
where T : unmanaged, IUnorm<TPixel>
where TPixel : unmanaged
{
return type switch
{
_ when type == typeof(ReadOnlyTexture2D<,>) => device.LoadReadOnlyTexture2D<T, TPixel>(buffer),
_ when type == typeof(ReadWriteTexture2D<,>) => device.LoadReadWriteTexture2D<T, TPixel>(buffer),
_ => throw new ArgumentException($"Invalid type: {type}", nameof(type))
};
}
}