mirror of
https://github.com/ahwootech/BRUTAL.git
synced 2026-11-26 13:08:24 +01:00
first
This commit is contained in:
parent
bd62d51ec9
commit
b578433e37
33 changed files with 784842 additions and 0 deletions
56
Brutal.Texture/DerivedTexturePropertiesExtensions.cs
Normal file
56
Brutal.Texture/DerivedTexturePropertiesExtensions.cs
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
namespace Brutal.TextureApi;
|
||||
|
||||
/// <summary>
|
||||
/// Extension methods library for ITexture
|
||||
/// </summary>
|
||||
public static class DerivedTexturePropertiesExtensions
|
||||
{
|
||||
extension(ITexture texture)
|
||||
{
|
||||
/// <summary>
|
||||
/// Determines whether the specified texture represents a 3-dimensional texture.
|
||||
/// </summary>
|
||||
/// <value>true if the texture has three dimensions; otherwise, false.</value>
|
||||
public bool Is3d => texture.DimensionCount == 3;
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether the specified texture represents a two-dimensional texture.
|
||||
/// </summary>
|
||||
/// <value>true if the texture has exactly two dimensions; otherwise, false.</value>
|
||||
public bool Is2d => texture.DimensionCount == 2;
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether the specified texture is one-dimensional.
|
||||
/// </summary>
|
||||
/// <value>true if the texture has a single dimension; otherwise, false.</value>
|
||||
public bool Is1d => texture.DimensionCount == 1;
|
||||
|
||||
/// <summary>
|
||||
/// The width of this texture (in texels)
|
||||
/// </summary>
|
||||
public int Width => texture.Extent.X;
|
||||
|
||||
/// <summary>
|
||||
/// The height of this texture (in texels)
|
||||
/// </summary>
|
||||
public int Height => texture.Extent.Y;
|
||||
|
||||
/// <summary>
|
||||
/// The depth of this texture (in texels)
|
||||
/// </summary>
|
||||
public int Depth => texture.Extent.Z;
|
||||
|
||||
/// <summary>
|
||||
/// Calculates the dimensions of a texture at the specified mipmap level.
|
||||
/// </summary>
|
||||
/// <param name="level">The mipmap level for which to calculate the extent. Must be greater than or equal to zero.</param>
|
||||
/// <returns>A TexelCount3 containing the width, height, and depth of the texture at the specified mipmap level. Each
|
||||
/// dimension will be at least 1.</returns>
|
||||
public TexelCount3 Extent(int level) =>
|
||||
new(
|
||||
Math.Max(1, texture.Width >> level),
|
||||
Math.Max(1, texture.Height >> level),
|
||||
Math.Max(1, texture.Depth >> level)
|
||||
);
|
||||
}
|
||||
}
|
||||
50
Brutal.Texture/Gli/GliConversionUtils.cs
Normal file
50
Brutal.Texture/Gli/GliConversionUtils.cs
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
namespace Brutal.TextureApi.Gli;
|
||||
|
||||
public static class GliConversionUtils
|
||||
{
|
||||
public static GliTexture ToGliTexture(this ITexture texture)
|
||||
{
|
||||
var gliTarget = GetTarget(texture);
|
||||
var gliNative = new Brutal.GliApi.Gli.Texture(
|
||||
gliTarget,
|
||||
texture.Format.ToGliFormat(),
|
||||
new(texture.Extent.X, texture.Extent.Y, texture.Extent.Z),
|
||||
texture.LayerCount,
|
||||
texture.FaceCount,
|
||||
texture.LevelCount,
|
||||
GliApi.Gli.Swizzles.Identity
|
||||
);
|
||||
var outTexture = new GliTexture(gliNative);
|
||||
texture.Data.CopyTo(outTexture.Data);
|
||||
return outTexture;
|
||||
}
|
||||
|
||||
public static Brutal.GliApi.Gli.Target GetTarget(ITexture texture)
|
||||
{
|
||||
return GetTarget(texture.Extent, texture.LayerCount, texture.IsCubemap);
|
||||
}
|
||||
|
||||
public static GliApi.Gli.Target GetTarget(TexelCount3 extent, int layerCount, bool isCubemap)
|
||||
{
|
||||
var isArray = layerCount > 1;
|
||||
|
||||
if (isCubemap)
|
||||
{
|
||||
return isArray ? GliApi.Gli.Target.CubeArray : GliApi.Gli.Target.Cube;
|
||||
}
|
||||
if (extent.Y is 1)
|
||||
{
|
||||
return isArray ? GliApi.Gli.Target._1DArray : GliApi.Gli.Target._1D;
|
||||
}
|
||||
|
||||
if (extent.Z is 1)
|
||||
{
|
||||
return isArray ? GliApi.Gli.Target._2DArray : GliApi.Gli.Target._2D;
|
||||
}
|
||||
|
||||
if (isArray)
|
||||
throw new NotImplementedException("Didn't anticipate Gli compatibility with 3d array textures");
|
||||
|
||||
return GliApi.Gli.Target._3D;
|
||||
}
|
||||
}
|
||||
430
Brutal.Texture/Gli/GliFormatExtensions.cs
Normal file
430
Brutal.Texture/Gli/GliFormatExtensions.cs
Normal file
|
|
@ -0,0 +1,430 @@
|
|||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Brutal.TextureApi.Gli;
|
||||
|
||||
public static class GliFormatExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Convert a GliFormat to a TextureFormat
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Not all GliFormats are currently supported by this function due to ambiguity about whether they will convert correctly, and will throw a NotImplementedException if encountered
|
||||
/// </remarks>
|
||||
/// <param name="gliFormat">The GliFormat to convert</param>
|
||||
/// <returns>A corresponding TextureFormat</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static TextureFormat ToTextureFormat(this GliApi.Gli.Format gliFormat)
|
||||
{
|
||||
return gliFormat switch
|
||||
{
|
||||
GliApi.Gli.Format.Undefined => TextureFormat.Undefined,
|
||||
GliApi.Gli.Format.RG4_UNorm_Pack8 => TextureFormat.R4G4_UNorm_Pack8,
|
||||
GliApi.Gli.Format.RGBA4_UNorm_Pack16 => TextureFormat.R4G4B4A4_UNorm_Pack16,
|
||||
GliApi.Gli.Format.BGRA4_UNorm_Pack16 => TextureFormat.B4G4R4A4_UNorm_Pack16,
|
||||
GliApi.Gli.Format.R5G6B5_UNorm_Pack16 => TextureFormat.R5G6B5_UNorm_Pack16,
|
||||
GliApi.Gli.Format.B5G6R5_UNorm_Pack16 => TextureFormat.B5G6R5_UNorm_Pack16,
|
||||
GliApi.Gli.Format.RGB5A1_UNorm_Pack16 => TextureFormat.R5G5B5A1_UNorm_Pack16,
|
||||
GliApi.Gli.Format.BGR5A1_UNorm_Pack16 => TextureFormat.B5G5R5A1_UNorm_Pack16,
|
||||
GliApi.Gli.Format.A1RGB5_UNorm_Pack16 => TextureFormat.A1R5G5B5_UNorm_Pack16,
|
||||
GliApi.Gli.Format.R8_UNorm_Pack8 => TextureFormat.R8_UNorm,
|
||||
GliApi.Gli.Format.R8_SNorm_Pack8 => TextureFormat.R8_SNorm,
|
||||
GliApi.Gli.Format.R8_UScaled_Pack8 => TextureFormat.R8_UScaled,
|
||||
GliApi.Gli.Format.R8_SScaled_Pack8 => TextureFormat.R8_SScaled,
|
||||
GliApi.Gli.Format.R8_UInt_Pack8 => TextureFormat.R8_UInt,
|
||||
GliApi.Gli.Format.R8_SInt_Pack8 => TextureFormat.R8_SInt,
|
||||
GliApi.Gli.Format.R8_SRGB_Pack8 => TextureFormat.R8_SRGB,
|
||||
GliApi.Gli.Format.RG8_UNorm_Pack8 => TextureFormat.R8G8_UNorm,
|
||||
GliApi.Gli.Format.RG8_SNorm_Pack8 => TextureFormat.R8G8_SNorm,
|
||||
GliApi.Gli.Format.RG8_UScaled_Pack8 => TextureFormat.R8G8_UScaled,
|
||||
GliApi.Gli.Format.RG8_SScaled_Pack8 => TextureFormat.R8G8_SScaled,
|
||||
GliApi.Gli.Format.RG8_UInt_Pack8 => TextureFormat.R8G8_UInt,
|
||||
GliApi.Gli.Format.RG8_SInt_Pack8 => TextureFormat.R8G8_SInt,
|
||||
GliApi.Gli.Format.RG8_SRGB_Pack8 => TextureFormat.R8G8_SRGB,
|
||||
GliApi.Gli.Format.RGB8_UNorm_Pack8 => TextureFormat.R8G8B8_UNorm,
|
||||
GliApi.Gli.Format.RGB8_SNorm_Pack8 => TextureFormat.R8G8B8_SNorm,
|
||||
GliApi.Gli.Format.RGB8_UScaled_Pack8 => TextureFormat.R8G8B8_UScaled,
|
||||
GliApi.Gli.Format.RGB8_SScaled_Pack8 => TextureFormat.R8G8B8_SScaled,
|
||||
GliApi.Gli.Format.RGB8_UInt_Pack8 => TextureFormat.R8G8B8_UInt,
|
||||
GliApi.Gli.Format.RGB8_SInt_Pack8 => TextureFormat.R8G8B8_SInt,
|
||||
GliApi.Gli.Format.RGB8_SRGB_Pack8 => TextureFormat.R8G8B8_SRGB,
|
||||
GliApi.Gli.Format.BGR8_UNorm_Pack8 => TextureFormat.B8G8R8_UNorm,
|
||||
GliApi.Gli.Format.BGR8_SNorm_Pack8 => TextureFormat.B8G8R8_SNorm,
|
||||
GliApi.Gli.Format.BGR8_UScaled_Pack8 => TextureFormat.B8G8R8_UScaled,
|
||||
GliApi.Gli.Format.BGR8_SScaled_Pack8 => TextureFormat.B8G8R8_SScaled,
|
||||
GliApi.Gli.Format.BGR8_UInt_Pack8 => TextureFormat.B8G8R8_UInt,
|
||||
GliApi.Gli.Format.BGR8_SInt_Pack8 => TextureFormat.B8G8R8_SInt,
|
||||
GliApi.Gli.Format.BGR8_SRGB_Pack8 => TextureFormat.B8G8R8_SRGB,
|
||||
GliApi.Gli.Format.RGBA8_UNorm_Pack8 => TextureFormat.R8G8B8A8_UNorm,
|
||||
GliApi.Gli.Format.RGBA8_SNorm_Pack8 => TextureFormat.R8G8B8A8_SNorm,
|
||||
GliApi.Gli.Format.RGBA8_UScaled_Pack8 => TextureFormat.R8G8B8A8_UScaled,
|
||||
GliApi.Gli.Format.RGBA8_SScaled_Pack8 => TextureFormat.R8G8B8A8_SScaled,
|
||||
GliApi.Gli.Format.RGBA8_UInt_Pack8 => TextureFormat.R8G8B8A8_UInt,
|
||||
GliApi.Gli.Format.RGBA8_SInt_Pack8 => TextureFormat.R8G8B8A8_SInt,
|
||||
GliApi.Gli.Format.RGBA8_SRGB_Pack8 => TextureFormat.R8G8B8A8_SRGB,
|
||||
GliApi.Gli.Format.BGRA8_UNorm_Pack8 => TextureFormat.B8G8R8A8_UNorm,
|
||||
GliApi.Gli.Format.BGRA8_SNorm_Pack8 => TextureFormat.B8G8R8A8_SNorm,
|
||||
GliApi.Gli.Format.BGRA8_UScaled_Pack8 => TextureFormat.B8G8R8A8_UScaled,
|
||||
GliApi.Gli.Format.BGRA8_SScaled_Pack8 => TextureFormat.B8G8R8A8_SScaled,
|
||||
GliApi.Gli.Format.BGRA8_UInt_Pack8 => TextureFormat.B8G8R8A8_UInt,
|
||||
GliApi.Gli.Format.BGRA8_SInt_Pack8 => TextureFormat.B8G8R8A8_SInt,
|
||||
GliApi.Gli.Format.BGRA8_SRGB_Pack8 => TextureFormat.B8G8R8A8_SRGB,
|
||||
// Note: Vulkan seems to order the naming here for 32 bit packed formats from the least significant byte to the most significant byte, rather the memory layout order
|
||||
// See Enums.cs docstrings on the Format enum for more information
|
||||
GliApi.Gli.Format.RGBA8_UNorm_Pack32 => TextureFormat.A8B8G8R8_UNorm_Pack32,
|
||||
GliApi.Gli.Format.RGBA8_SNorm_Pack32 => TextureFormat.A8B8G8R8_SNorm_Pack32,
|
||||
GliApi.Gli.Format.RGBA8_UScaled_Pack32 => TextureFormat.A8B8G8R8_UScaled_Pack32,
|
||||
GliApi.Gli.Format.RGBA8_SScaled_Pack32 => TextureFormat.A8B8G8R8_SScaled_Pack32,
|
||||
GliApi.Gli.Format.RGBA8_UInt_Pack32 => TextureFormat.A8B8G8R8_UInt_Pack32,
|
||||
GliApi.Gli.Format.RGBA8_SInt_Pack32 => TextureFormat.A8B8G8R8_SInt_Pack32,
|
||||
GliApi.Gli.Format.RGBA8_SRGB_Pack32 => TextureFormat.A8B8G8R8_SRGB_Pack32,
|
||||
// These formats aren't named consistently between Gli and Vulkan, have removed support until we can confirm the correct mapping
|
||||
GliApi.Gli.Format.RGB10A2_UNorm_Pack32 => throw new NotImplementedException(),
|
||||
GliApi.Gli.Format.RGB10A2_SNorm_Pack32 => throw new NotImplementedException(),
|
||||
GliApi.Gli.Format.RGB10A2_UScaled_Pack32 => throw new NotImplementedException(),
|
||||
GliApi.Gli.Format.RGB10A2_SScaled_Pack32 => throw new NotImplementedException(),
|
||||
GliApi.Gli.Format.RGB10A2_UInt_Pack32 => throw new NotImplementedException(),
|
||||
GliApi.Gli.Format.RGB10A2_SInt_Pack32 => throw new NotImplementedException(),
|
||||
GliApi.Gli.Format.BGR10A2_UNorm_Pack32 => throw new NotImplementedException(),
|
||||
GliApi.Gli.Format.BGR10A2_SNorm_Pack32 => throw new NotImplementedException(),
|
||||
GliApi.Gli.Format.BGR10A2_UScaled_Pack32 => throw new NotImplementedException(),
|
||||
GliApi.Gli.Format.BGR10A2_SScaled_Pack32 => throw new NotImplementedException(),
|
||||
GliApi.Gli.Format.BGR10A2_UInt_Pack32 => throw new NotImplementedException(),
|
||||
GliApi.Gli.Format.BGR10A2_SInt_Pack32 => throw new NotImplementedException(),
|
||||
GliApi.Gli.Format.R16_UNorm_Pack16 => TextureFormat.R16_UNorm,
|
||||
GliApi.Gli.Format.R16_SNorm_Pack16 => TextureFormat.R16_SNorm,
|
||||
GliApi.Gli.Format.R16_UScaled_Pack16 => TextureFormat.R16_UScaled,
|
||||
GliApi.Gli.Format.R16_SScaled_Pack16 => TextureFormat.R16_SScaled,
|
||||
GliApi.Gli.Format.R16_UInt_Pack16 => TextureFormat.R16_UInt,
|
||||
GliApi.Gli.Format.R16_SInt_Pack16 => TextureFormat.R16_SInt,
|
||||
GliApi.Gli.Format.R16_SFloat_Pack16 => TextureFormat.R16_SFloat,
|
||||
GliApi.Gli.Format.RG16_UNorm_Pack16 => TextureFormat.R16G16_UNorm,
|
||||
GliApi.Gli.Format.RG16_SNorm_Pack16 => TextureFormat.R16G16_SNorm,
|
||||
GliApi.Gli.Format.RG16_UScaled_Pack16 => TextureFormat.R16G16_UScaled,
|
||||
GliApi.Gli.Format.RG16_SScaled_Pack16 => TextureFormat.R16G16_SScaled,
|
||||
GliApi.Gli.Format.RG16_UInt_Pack16 => TextureFormat.R16G16_UInt,
|
||||
GliApi.Gli.Format.RG16_SInt_Pack16 => TextureFormat.R16G16_SInt,
|
||||
GliApi.Gli.Format.RG16_SFloat_Pack16 => TextureFormat.R16G16_SFloat,
|
||||
GliApi.Gli.Format.RGB16_UNorm_Pack16 => TextureFormat.R16G16B16_UNorm,
|
||||
GliApi.Gli.Format.RGB16_SNorm_Pack16 => TextureFormat.R16G16B16_SNorm,
|
||||
GliApi.Gli.Format.RGB16_UScaled_Pack16 => TextureFormat.R16G16B16_UScaled,
|
||||
GliApi.Gli.Format.RGB16_SScaled_Pack16 => TextureFormat.R16G16B16_SScaled,
|
||||
GliApi.Gli.Format.RGB16_UInt_Pack16 => TextureFormat.R16G16B16_UInt,
|
||||
GliApi.Gli.Format.RGB16_SInt_Pack16 => TextureFormat.R16G16B16_SInt,
|
||||
GliApi.Gli.Format.RGB16_SFloat_Pack16 => TextureFormat.R16G16B16_SFloat,
|
||||
GliApi.Gli.Format.RGBA16_UNorm_Pack16 => TextureFormat.R16G16B16A16_UNorm,
|
||||
GliApi.Gli.Format.RGBA16_SNorm_Pack16 => TextureFormat.R16G16B16A16_SNorm,
|
||||
GliApi.Gli.Format.RGBA16_UScaled_Pack16 => TextureFormat.R16G16B16A16_UScaled,
|
||||
GliApi.Gli.Format.RGBA16_SScaled_Pack16 => TextureFormat.R16G16B16A16_SScaled,
|
||||
GliApi.Gli.Format.RGBA16_UInt_Pack16 => TextureFormat.R16G16B16A16_UInt,
|
||||
GliApi.Gli.Format.RGBA16_SInt_Pack16 => TextureFormat.R16G16B16A16_SInt,
|
||||
GliApi.Gli.Format.RGBA16_SFloat_Pack16 => TextureFormat.R16G16B16A16_SFloat,
|
||||
GliApi.Gli.Format.R32_UInt_Pack32 => TextureFormat.R32_UInt,
|
||||
GliApi.Gli.Format.R32_SInt_Pack32 => TextureFormat.R32_SInt,
|
||||
GliApi.Gli.Format.R32_SFloat_Pack32 => TextureFormat.R32_SFloat,
|
||||
GliApi.Gli.Format.RG32_UInt_Pack32 => TextureFormat.R32G32_UInt,
|
||||
GliApi.Gli.Format.RG32_SInt_Pack32 => TextureFormat.R32G32_SInt,
|
||||
GliApi.Gli.Format.RG32_SFloat_Pack32 => TextureFormat.R32G32_SFloat,
|
||||
GliApi.Gli.Format.RGB32_UInt_Pack32 => TextureFormat.R32G32B32_UInt,
|
||||
GliApi.Gli.Format.RGB32_SInt_Pack32 => TextureFormat.R32G32B32_SInt,
|
||||
GliApi.Gli.Format.RGB32_SFloat_Pack32 => TextureFormat.R32G32B32_SFloat,
|
||||
GliApi.Gli.Format.RGBA32_UInt_Pack32 => TextureFormat.R32G32B32A32_UInt,
|
||||
GliApi.Gli.Format.RGBA32_SInt_Pack32 => TextureFormat.R32G32B32A32_SInt,
|
||||
GliApi.Gli.Format.RGBA32_SFloat_Pack32 => TextureFormat.R32G32B32A32_SFloat,
|
||||
GliApi.Gli.Format.R64_UInt_Pack64 => TextureFormat.R64_UInt,
|
||||
GliApi.Gli.Format.R64_SInt_Pack64 => TextureFormat.R64_SInt,
|
||||
GliApi.Gli.Format.R64_SFloat_Pack64 => TextureFormat.R64_SFloat,
|
||||
GliApi.Gli.Format.RG64_UInt_Pack64 => TextureFormat.R64G64_UInt,
|
||||
GliApi.Gli.Format.RG64_SInt_Pack64 => TextureFormat.R64G64_SInt,
|
||||
GliApi.Gli.Format.RG64_SFloat_Pack64 => TextureFormat.R64G64_SFloat,
|
||||
GliApi.Gli.Format.RGB64_UInt_Pack64 => TextureFormat.R64G64B64_UInt,
|
||||
GliApi.Gli.Format.RGB64_SInt_Pack64 => TextureFormat.R64G64B64_SInt,
|
||||
GliApi.Gli.Format.RGB64_SFloat_Pack64 => TextureFormat.R64G64B64_SFloat,
|
||||
GliApi.Gli.Format.RGBA64_UInt_Pack64 => TextureFormat.R64G64B64A64_UInt,
|
||||
GliApi.Gli.Format.RGBA64_SInt_Pack64 => TextureFormat.R64G64B64A64_SInt,
|
||||
GliApi.Gli.Format.RGBA64_SFloat_Pack64 => TextureFormat.R64G64B64A64_SFloat,
|
||||
GliApi.Gli.Format.RG11B10_UFloat_Pack32 => TextureFormat.B10G11R11_UFloat_Pack32,
|
||||
GliApi.Gli.Format.RGB9E5_UFloat_Pack32 => TextureFormat.E5B9G9R9_UFloat_Pack32,
|
||||
GliApi.Gli.Format.D16_UNorm_Pack16 => TextureFormat.D16_UNorm,
|
||||
GliApi.Gli.Format.D24_UNorm_Pack32 => TextureFormat.X8D24_UNorm_Pack32,
|
||||
GliApi.Gli.Format.D32_SFloat_Pack32 => TextureFormat.D32_SFloat,
|
||||
GliApi.Gli.Format.S8_UInt_Pack8 => TextureFormat.S8_UInt,
|
||||
GliApi.Gli.Format.D16_UNorm_S8_UInt_Pack32 => TextureFormat.D16_UNormS8_UInt,
|
||||
GliApi.Gli.Format.D24_UNorm_S8_UInt_Pack32 => TextureFormat.D24_UNormS8_UInt,
|
||||
GliApi.Gli.Format.D32_SFloat_S8_UInt_Pack64 => TextureFormat.D32_SFloatS8_UInt,
|
||||
// Note: DXT1 is equivalent to BC1
|
||||
GliApi.Gli.Format.RGB_DXT1_UNorm_Block8 => TextureFormat.BC1Rgb_UNorm_Block,
|
||||
GliApi.Gli.Format.RGB_DXT1_SRGB_Block8 => TextureFormat.BC1Rgb_SRGB_Block,
|
||||
GliApi.Gli.Format.RGBA_DXT1_UNorm_Block8 => TextureFormat.BC1Rgba_UNorm_Block,
|
||||
GliApi.Gli.Format.RGBA_DXT1_SRGB_Block8 => TextureFormat.BC1Rgba_SRGB_Block,
|
||||
// Note: DXT2/3 is equivalent to BC2
|
||||
GliApi.Gli.Format.RGBA_DXT3_UNorm_Block16 => TextureFormat.BC2_UNorm_Block,
|
||||
GliApi.Gli.Format.RGBA_DXT3_SRGB_Block16 => TextureFormat.BC2_SRGB_Block,
|
||||
// Note: DXT4/5 is equivalent to BC3
|
||||
GliApi.Gli.Format.RGBA_DXT5_UNorm_Block16 => TextureFormat.BC3_UNorm_Block,
|
||||
GliApi.Gli.Format.RGBA_DXT5_SRGB_Block16 => TextureFormat.BC3_SRGB_Block,
|
||||
// Note: ATI1 is equivalent to BC4
|
||||
GliApi.Gli.Format.R_ATI1N_UNorm_Block8 => TextureFormat.BC4_UNorm_Block,
|
||||
GliApi.Gli.Format.R_ATI1N_SNorm_Block8 => TextureFormat.BC4_SNorm_Block,
|
||||
// Note: ATI2 is equivalent to BC5
|
||||
GliApi.Gli.Format.RG_ATI2N_UNorm_Block16 => TextureFormat.BC5_UNorm_Block,
|
||||
GliApi.Gli.Format.RG_ATI2N_SNorm_Block16 => TextureFormat.BC5_SNorm_Block,
|
||||
// It's not clear what the Vulkan equivalent of these formats are
|
||||
GliApi.Gli.Format.RGB_BP_UFloat_Block16 => throw new NotImplementedException(),
|
||||
GliApi.Gli.Format.RGB_BP_SFloat_Block16 => throw new NotImplementedException(),
|
||||
GliApi.Gli.Format.RGBA_BP_UNorm_Block16 => throw new NotImplementedException(),
|
||||
GliApi.Gli.Format.RGBA_BP_SRGB_Block16 => throw new NotImplementedException(),
|
||||
// ETC formats
|
||||
GliApi.Gli.Format.RGB_ETC2_UNorm_Block8 => TextureFormat.ETC2R8G8B8_UNorm_Block,
|
||||
GliApi.Gli.Format.RGB_ETC2_SRGB_Block8 => TextureFormat.ETC2R8G8B8_SRGB_Block,
|
||||
GliApi.Gli.Format.RGBA_ETC2_UNorm_Block8 => throw new NotImplementedException(), // It's not clear what the Vulkan equivalent of this format is
|
||||
GliApi.Gli.Format.RGBA_ETC2_SRGB_Block8 => throw new NotImplementedException(), // It's not clear what the Vulkan equivalent of this format is
|
||||
GliApi.Gli.Format.RGBA_ETC2_UNorm_Block16 => TextureFormat.ETC2R8G8B8A8_UNorm_Block,
|
||||
GliApi.Gli.Format.RGBA_ETC2_SRGB_Block16 => TextureFormat.ETC2R8G8B8A8_SRGB_Block,
|
||||
GliApi.Gli.Format.R_EAC_UNorm_Block8 => TextureFormat.EACR11_UNorm_Block,
|
||||
GliApi.Gli.Format.R_EAC_SNorm_Block8 => TextureFormat.EACR11_SNorm_Block,
|
||||
GliApi.Gli.Format.RG_EAC_UNorm_Block16 => TextureFormat.EACR11G11_UNorm_Block,
|
||||
GliApi.Gli.Format.RG_EAC_SNorm_Block16 => TextureFormat.EACR11G11_SNorm_Block,
|
||||
// ASTC formats
|
||||
GliApi.Gli.Format.RGBA_ASTC_4X4_UNorm_Block16 => TextureFormat.ASTC4X4_UNorm_Block,
|
||||
GliApi.Gli.Format.RGBA_ASTC_4X4_SRGB_Block16 => TextureFormat.ASTC4X4_SRGB_Block,
|
||||
GliApi.Gli.Format.RGBA_ASTC_5X4_UNorm_Block16 => TextureFormat.ASTC5X4_UNorm_Block,
|
||||
GliApi.Gli.Format.RGBA_ASTC_5X4_SRGB_Block16 => TextureFormat.ASTC5X4_SRGB_Block,
|
||||
GliApi.Gli.Format.RGBA_ASTC_5X5_UNorm_Block16 => TextureFormat.ASTC5X5_UNorm_Block,
|
||||
GliApi.Gli.Format.RGBA_ASTC_5X5_SRGB_Block16 => TextureFormat.ASTC5X5_SRGB_Block,
|
||||
GliApi.Gli.Format.RGBA_ASTC_6X5_UNorm_Block16 => TextureFormat.ASTC6X5_UNorm_Block,
|
||||
GliApi.Gli.Format.RGBA_ASTC_6X5_SRGB_Block16 => TextureFormat.ASTC6X5_SRGB_Block,
|
||||
GliApi.Gli.Format.RGBA_ASTC_6X6_UNorm_Block16 => TextureFormat.ASTC6X6_UNorm_Block,
|
||||
GliApi.Gli.Format.RGBA_ASTC_6X6_SRGB_Block16 => TextureFormat.ASTC6X6_SRGB_Block,
|
||||
GliApi.Gli.Format.RGBA_ASTC_8X5_UNorm_Block16 => TextureFormat.ASTC8X5_UNorm_Block,
|
||||
GliApi.Gli.Format.RGBA_ASTC_8X5_SRGB_Block16 => TextureFormat.ASTC8X5_SRGB_Block,
|
||||
GliApi.Gli.Format.RGBA_ASTC_8X6_UNorm_Block16 => TextureFormat.ASTC8X6_UNorm_Block,
|
||||
GliApi.Gli.Format.RGBA_ASTC_8X6_SRGB_Block16 => TextureFormat.ASTC8X6_SRGB_Block,
|
||||
GliApi.Gli.Format.RGBA_ASTC_8X8_UNorm_Block16 => TextureFormat.ASTC8X8_UNorm_Block,
|
||||
GliApi.Gli.Format.RGBA_ASTC_8X8_SRGB_Block16 => TextureFormat.ASTC8X8_SRGB_Block,
|
||||
GliApi.Gli.Format.RGBA_ASTC_10X5_UNorm_Block16 => TextureFormat.ASTC10X5_UNorm_Block,
|
||||
GliApi.Gli.Format.RGBA_ASTC_10X5_SRGB_Block16 => TextureFormat.ASTC10X5_SRGB_Block,
|
||||
GliApi.Gli.Format.RGBA_ASTC_10X6_UNorm_Block16 => TextureFormat.ASTC10X6_UNorm_Block,
|
||||
GliApi.Gli.Format.RGBA_ASTC_10X6_SRGB_Block16 => TextureFormat.ASTC10X6_SRGB_Block,
|
||||
GliApi.Gli.Format.RGBA_ASTC_10X8_UNorm_Block16 => TextureFormat.ASTC10X8_UNorm_Block,
|
||||
GliApi.Gli.Format.RGBA_ASTC_10X8_SRGB_Block16 => TextureFormat.ASTC10X8_SRGB_Block,
|
||||
GliApi.Gli.Format.RGBA_ASTC_10X10_UNorm_Block16 => TextureFormat.ASTC10X10_UNorm_Block,
|
||||
GliApi.Gli.Format.RGBA_ASTC_10X10_SRGB_Block16 => TextureFormat.ASTC10X10_SRGB_Block,
|
||||
GliApi.Gli.Format.RGBA_ASTC_12X10_UNorm_Block16 => TextureFormat.ASTC12X10_UNorm_Block,
|
||||
GliApi.Gli.Format.RGBA_ASTC_12X10_SRGB_Block16 => TextureFormat.ASTC12X10_SRGB_Block,
|
||||
GliApi.Gli.Format.RGBA_ASTC_12X12_UNorm_Block16 => TextureFormat.ASTC12X12_UNorm_Block,
|
||||
GliApi.Gli.Format.RGBA_ASTC_12X12_SRGB_Block16 => TextureFormat.ASTC12X12_SRGB_Block,
|
||||
// PVRTC formats - requires VK_IMG_format_pvrtc extension, not currently exposed
|
||||
GliApi.Gli.Format.RGB_PVRTC1_8X8_UNorm_Block32 => throw new NotImplementedException(),
|
||||
GliApi.Gli.Format.RGB_PVRTC1_8X8_SRGB_Block32 => throw new NotImplementedException(),
|
||||
GliApi.Gli.Format.RGB_PVRTC1_16X8_UNorm_Block32 => throw new NotImplementedException(),
|
||||
GliApi.Gli.Format.RGB_PVRTC1_16X8_SRGB_Block32 => throw new NotImplementedException(),
|
||||
GliApi.Gli.Format.RGBA_PVRTC1_8X8_UNorm_Block32 => throw new NotImplementedException(),
|
||||
GliApi.Gli.Format.RGBA_PVRTC1_8X8_SRGB_Block32 => throw new NotImplementedException(),
|
||||
GliApi.Gli.Format.RGBA_PVRTC1_16X8_UNorm_Block32 => throw new NotImplementedException(),
|
||||
GliApi.Gli.Format.RGBA_PVRTC1_16X8_SRGB_Block32 => throw new NotImplementedException(),
|
||||
GliApi.Gli.Format.RGBA_PVRTC2_4X4_UNorm_Block8 => throw new NotImplementedException(),
|
||||
GliApi.Gli.Format.RGBA_PVRTC2_4X4_SRGB_Block8 => throw new NotImplementedException(),
|
||||
GliApi.Gli.Format.RGBA_PVRTC2_8X4_UNorm_Block8 => throw new NotImplementedException(),
|
||||
GliApi.Gli.Format.RGBA_PVRTC2_8X4_SRGB_Block8 => throw new NotImplementedException(),
|
||||
// Unknown whether these formats are supported by Vulkan
|
||||
GliApi.Gli.Format.RGB_ETC_UNorm_Block8 => throw new NotImplementedException(),
|
||||
GliApi.Gli.Format.RGB_ATC_UNorm_Block8 => throw new NotImplementedException(),
|
||||
GliApi.Gli.Format.RGBA_ATCA_UNorm_Block16 => throw new NotImplementedException(),
|
||||
GliApi.Gli.Format.RGBA_ATCI_UNorm_Block16 => throw new NotImplementedException(),
|
||||
GliApi.Gli.Format.L8_UNorm_Pack8 => throw new NotImplementedException(),
|
||||
GliApi.Gli.Format.A8_UNorm_Pack8 => throw new NotImplementedException(),
|
||||
GliApi.Gli.Format.LA8_UNorm_Pack8 => throw new NotImplementedException(),
|
||||
GliApi.Gli.Format.L16_UNorm_Pack16 => throw new NotImplementedException(),
|
||||
GliApi.Gli.Format.A16_UNorm_Pack16 => throw new NotImplementedException(),
|
||||
GliApi.Gli.Format.LA16_UNorm_Pack16 => throw new NotImplementedException(),
|
||||
GliApi.Gli.Format.BGR8_UNorm_Pack32 => throw new NotImplementedException(),
|
||||
GliApi.Gli.Format.BGR8_SRGB_Pack32 => throw new NotImplementedException(),
|
||||
GliApi.Gli.Format.RG3B2_UNorm_Pack8 => throw new NotImplementedException(),
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(gliFormat), gliFormat, null),
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert a TextureFormat to a GliFormat
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Not all formats are currently supported by this function due to ambiguity about whether they will convert correctly
|
||||
/// </remarks>
|
||||
/// <param name="format">The TextureFormat to convert</param>
|
||||
/// <returns>A corresponding GliFormat</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static GliApi.Gli.Format ToGliFormat(this TextureFormat format)
|
||||
{
|
||||
return format switch
|
||||
{
|
||||
TextureFormat.Undefined => GliApi.Gli.Format.Undefined,
|
||||
TextureFormat.R4G4_UNorm_Pack8 => GliApi.Gli.Format.RG4_UNorm_Pack8,
|
||||
TextureFormat.R4G4B4A4_UNorm_Pack16 => GliApi.Gli.Format.RGBA4_UNorm_Pack16,
|
||||
TextureFormat.B4G4R4A4_UNorm_Pack16 => GliApi.Gli.Format.BGRA4_UNorm_Pack16,
|
||||
TextureFormat.R5G6B5_UNorm_Pack16 => GliApi.Gli.Format.R5G6B5_UNorm_Pack16,
|
||||
TextureFormat.B5G6R5_UNorm_Pack16 => GliApi.Gli.Format.B5G6R5_UNorm_Pack16,
|
||||
TextureFormat.R5G5B5A1_UNorm_Pack16 => GliApi.Gli.Format.RGB5A1_UNorm_Pack16,
|
||||
TextureFormat.B5G5R5A1_UNorm_Pack16 => GliApi.Gli.Format.BGR5A1_UNorm_Pack16,
|
||||
TextureFormat.A1R5G5B5_UNorm_Pack16 => GliApi.Gli.Format.A1RGB5_UNorm_Pack16,
|
||||
TextureFormat.R8_UNorm => GliApi.Gli.Format.R8_UNorm_Pack8,
|
||||
TextureFormat.R8_SNorm => GliApi.Gli.Format.R8_SNorm_Pack8,
|
||||
TextureFormat.R8_UScaled => GliApi.Gli.Format.R8_UScaled_Pack8,
|
||||
TextureFormat.R8_SScaled => GliApi.Gli.Format.R8_SScaled_Pack8,
|
||||
TextureFormat.R8_UInt => GliApi.Gli.Format.R8_UInt_Pack8,
|
||||
TextureFormat.R8_SInt => GliApi.Gli.Format.R8_SInt_Pack8,
|
||||
TextureFormat.R8_SRGB => GliApi.Gli.Format.R8_SRGB_Pack8,
|
||||
TextureFormat.R8G8_UNorm => GliApi.Gli.Format.RG8_UNorm_Pack8,
|
||||
TextureFormat.R8G8_SNorm => GliApi.Gli.Format.RG8_SNorm_Pack8,
|
||||
TextureFormat.R8G8_UScaled => GliApi.Gli.Format.RG8_UScaled_Pack8,
|
||||
TextureFormat.R8G8_SScaled => GliApi.Gli.Format.RG8_SScaled_Pack8,
|
||||
TextureFormat.R8G8_UInt => GliApi.Gli.Format.RG8_UInt_Pack8,
|
||||
TextureFormat.R8G8_SInt => GliApi.Gli.Format.RG8_SInt_Pack8,
|
||||
TextureFormat.R8G8_SRGB => GliApi.Gli.Format.RG8_SRGB_Pack8,
|
||||
TextureFormat.R8G8B8_UNorm => GliApi.Gli.Format.RGB8_UNorm_Pack8,
|
||||
TextureFormat.R8G8B8_SNorm => GliApi.Gli.Format.RGB8_SNorm_Pack8,
|
||||
TextureFormat.R8G8B8_UScaled => GliApi.Gli.Format.RGB8_UScaled_Pack8,
|
||||
TextureFormat.R8G8B8_SScaled => GliApi.Gli.Format.RGB8_SScaled_Pack8,
|
||||
TextureFormat.R8G8B8_UInt => GliApi.Gli.Format.RGB8_UInt_Pack8,
|
||||
TextureFormat.R8G8B8_SInt => GliApi.Gli.Format.RGB8_SInt_Pack8,
|
||||
TextureFormat.R8G8B8_SRGB => GliApi.Gli.Format.RGB8_SRGB_Pack8,
|
||||
TextureFormat.B8G8R8_UNorm => GliApi.Gli.Format.BGR8_UNorm_Pack8,
|
||||
TextureFormat.B8G8R8_SNorm => GliApi.Gli.Format.BGR8_SNorm_Pack8,
|
||||
TextureFormat.B8G8R8_UScaled => GliApi.Gli.Format.BGR8_UScaled_Pack8,
|
||||
TextureFormat.B8G8R8_SScaled => GliApi.Gli.Format.BGR8_SScaled_Pack8,
|
||||
TextureFormat.B8G8R8_UInt => GliApi.Gli.Format.BGR8_UInt_Pack8,
|
||||
TextureFormat.B8G8R8_SInt => GliApi.Gli.Format.BGR8_SInt_Pack8,
|
||||
TextureFormat.B8G8R8_SRGB => GliApi.Gli.Format.BGR8_SRGB_Pack8,
|
||||
TextureFormat.R8G8B8A8_UNorm => GliApi.Gli.Format.RGBA8_UNorm_Pack8,
|
||||
TextureFormat.R8G8B8A8_SNorm => GliApi.Gli.Format.RGBA8_SNorm_Pack8,
|
||||
TextureFormat.R8G8B8A8_UScaled => GliApi.Gli.Format.RGBA8_UScaled_Pack8,
|
||||
TextureFormat.R8G8B8A8_SScaled => GliApi.Gli.Format.RGBA8_SScaled_Pack8,
|
||||
TextureFormat.R8G8B8A8_UInt => GliApi.Gli.Format.RGBA8_UInt_Pack8,
|
||||
TextureFormat.R8G8B8A8_SInt => GliApi.Gli.Format.RGBA8_SInt_Pack8,
|
||||
TextureFormat.R8G8B8A8_SRGB => GliApi.Gli.Format.RGBA8_SRGB_Pack8,
|
||||
TextureFormat.B8G8R8A8_UNorm => GliApi.Gli.Format.BGRA8_UNorm_Pack8,
|
||||
TextureFormat.B8G8R8A8_SNorm => GliApi.Gli.Format.BGRA8_SNorm_Pack8,
|
||||
TextureFormat.B8G8R8A8_UScaled => GliApi.Gli.Format.BGRA8_UScaled_Pack8,
|
||||
TextureFormat.B8G8R8A8_SScaled => GliApi.Gli.Format.BGRA8_SScaled_Pack8,
|
||||
TextureFormat.B8G8R8A8_UInt => GliApi.Gli.Format.BGRA8_UInt_Pack8,
|
||||
TextureFormat.B8G8R8A8_SInt => GliApi.Gli.Format.BGRA8_SInt_Pack8,
|
||||
TextureFormat.B8G8R8A8_SRGB => GliApi.Gli.Format.BGRA8_SRGB_Pack8,
|
||||
TextureFormat.A8B8G8R8_UNorm_Pack32 => GliApi.Gli.Format.RGBA8_UNorm_Pack32,
|
||||
TextureFormat.A8B8G8R8_SNorm_Pack32 => GliApi.Gli.Format.RGBA8_SNorm_Pack32,
|
||||
TextureFormat.A8B8G8R8_UScaled_Pack32 => GliApi.Gli.Format.RGBA8_UScaled_Pack32,
|
||||
TextureFormat.A8B8G8R8_SScaled_Pack32 => GliApi.Gli.Format.RGBA8_SScaled_Pack32,
|
||||
TextureFormat.A8B8G8R8_UInt_Pack32 => GliApi.Gli.Format.RGBA8_UInt_Pack32,
|
||||
TextureFormat.A8B8G8R8_SInt_Pack32 => GliApi.Gli.Format.RGBA8_SInt_Pack32,
|
||||
TextureFormat.A8B8G8R8_SRGB_Pack32 => GliApi.Gli.Format.RGBA8_SRGB_Pack32,
|
||||
TextureFormat.R16_UNorm => GliApi.Gli.Format.R16_UNorm_Pack16,
|
||||
TextureFormat.R16_SNorm => GliApi.Gli.Format.R16_SNorm_Pack16,
|
||||
TextureFormat.R16_UScaled => GliApi.Gli.Format.R16_UScaled_Pack16,
|
||||
TextureFormat.R16_SScaled => GliApi.Gli.Format.R16_SScaled_Pack16,
|
||||
TextureFormat.R16_UInt => GliApi.Gli.Format.R16_UInt_Pack16,
|
||||
TextureFormat.R16_SInt => GliApi.Gli.Format.R16_SInt_Pack16,
|
||||
TextureFormat.R16_SFloat => GliApi.Gli.Format.R16_SFloat_Pack16,
|
||||
TextureFormat.R16G16_UNorm => GliApi.Gli.Format.RG16_UNorm_Pack16,
|
||||
TextureFormat.R16G16_SNorm => GliApi.Gli.Format.RG16_SNorm_Pack16,
|
||||
TextureFormat.R16G16_UScaled => GliApi.Gli.Format.RG16_UScaled_Pack16,
|
||||
TextureFormat.R16G16_SScaled => GliApi.Gli.Format.RG16_SScaled_Pack16,
|
||||
TextureFormat.R16G16_UInt => GliApi.Gli.Format.RG16_UInt_Pack16,
|
||||
TextureFormat.R16G16_SInt => GliApi.Gli.Format.RG16_SInt_Pack16,
|
||||
TextureFormat.R16G16_SFloat => GliApi.Gli.Format.RG16_SFloat_Pack16,
|
||||
TextureFormat.R16G16B16_UNorm => GliApi.Gli.Format.RGB16_UNorm_Pack16,
|
||||
TextureFormat.R16G16B16_SNorm => GliApi.Gli.Format.RGB16_SNorm_Pack16,
|
||||
TextureFormat.R16G16B16_UScaled => GliApi.Gli.Format.RGB16_UScaled_Pack16,
|
||||
TextureFormat.R16G16B16_SScaled => GliApi.Gli.Format.RGB16_SScaled_Pack16,
|
||||
TextureFormat.R16G16B16_UInt => GliApi.Gli.Format.RGB16_UInt_Pack16,
|
||||
TextureFormat.R16G16B16_SInt => GliApi.Gli.Format.RGB16_SInt_Pack16,
|
||||
TextureFormat.R16G16B16_SFloat => GliApi.Gli.Format.RGB16_SFloat_Pack16,
|
||||
TextureFormat.R16G16B16A16_UNorm => GliApi.Gli.Format.RGBA16_UNorm_Pack16,
|
||||
TextureFormat.R16G16B16A16_SNorm => GliApi.Gli.Format.RGBA16_SNorm_Pack16,
|
||||
TextureFormat.R16G16B16A16_UScaled => GliApi.Gli.Format.RGBA16_UScaled_Pack16,
|
||||
TextureFormat.R16G16B16A16_SScaled => GliApi.Gli.Format.RGBA16_SScaled_Pack16,
|
||||
TextureFormat.R16G16B16A16_UInt => GliApi.Gli.Format.RGBA16_UInt_Pack16,
|
||||
TextureFormat.R16G16B16A16_SInt => GliApi.Gli.Format.RGBA16_SInt_Pack16,
|
||||
TextureFormat.R16G16B16A16_SFloat => GliApi.Gli.Format.RGBA16_SFloat_Pack16,
|
||||
TextureFormat.R32_UInt => GliApi.Gli.Format.R32_UInt_Pack32,
|
||||
TextureFormat.R32_SInt => GliApi.Gli.Format.R32_SInt_Pack32,
|
||||
TextureFormat.R32_SFloat => GliApi.Gli.Format.R32_SFloat_Pack32,
|
||||
TextureFormat.R32G32_UInt => GliApi.Gli.Format.RG32_UInt_Pack32,
|
||||
TextureFormat.R32G32_SInt => GliApi.Gli.Format.RG32_SInt_Pack32,
|
||||
TextureFormat.R32G32_SFloat => GliApi.Gli.Format.RG32_SFloat_Pack32,
|
||||
TextureFormat.R32G32B32_UInt => GliApi.Gli.Format.RGB32_UInt_Pack32,
|
||||
TextureFormat.R32G32B32_SInt => GliApi.Gli.Format.RGB32_SInt_Pack32,
|
||||
TextureFormat.R32G32B32_SFloat => GliApi.Gli.Format.RGB32_SFloat_Pack32,
|
||||
TextureFormat.R32G32B32A32_UInt => GliApi.Gli.Format.RGBA32_UInt_Pack32,
|
||||
TextureFormat.R32G32B32A32_SInt => GliApi.Gli.Format.RGBA32_SInt_Pack32,
|
||||
TextureFormat.R32G32B32A32_SFloat => GliApi.Gli.Format.RGBA32_SFloat_Pack32,
|
||||
TextureFormat.R64_UInt => GliApi.Gli.Format.R64_UInt_Pack64,
|
||||
TextureFormat.R64_SInt => GliApi.Gli.Format.R64_SInt_Pack64,
|
||||
TextureFormat.R64_SFloat => GliApi.Gli.Format.R64_SFloat_Pack64,
|
||||
TextureFormat.R64G64_UInt => GliApi.Gli.Format.RG64_UInt_Pack64,
|
||||
TextureFormat.R64G64_SInt => GliApi.Gli.Format.RG64_SInt_Pack64,
|
||||
TextureFormat.R64G64_SFloat => GliApi.Gli.Format.RG64_SFloat_Pack64,
|
||||
TextureFormat.R64G64B64_UInt => GliApi.Gli.Format.RGB64_UInt_Pack64,
|
||||
TextureFormat.R64G64B64_SInt => GliApi.Gli.Format.RGB64_SInt_Pack64,
|
||||
TextureFormat.R64G64B64_SFloat => GliApi.Gli.Format.RGB64_SFloat_Pack64,
|
||||
TextureFormat.R64G64B64A64_UInt => GliApi.Gli.Format.RGBA64_UInt_Pack64,
|
||||
TextureFormat.R64G64B64A64_SInt => GliApi.Gli.Format.RGBA64_SInt_Pack64,
|
||||
TextureFormat.R64G64B64A64_SFloat => GliApi.Gli.Format.RGBA64_SFloat_Pack64,
|
||||
TextureFormat.B10G11R11_UFloat_Pack32 => GliApi.Gli.Format.RG11B10_UFloat_Pack32,
|
||||
TextureFormat.E5B9G9R9_UFloat_Pack32 => GliApi.Gli.Format.RGB9E5_UFloat_Pack32,
|
||||
TextureFormat.D16_UNorm => GliApi.Gli.Format.D16_UNorm_Pack16,
|
||||
TextureFormat.X8D24_UNorm_Pack32 => GliApi.Gli.Format.D24_UNorm_Pack32,
|
||||
TextureFormat.D32_SFloat => GliApi.Gli.Format.D32_SFloat_Pack32,
|
||||
TextureFormat.S8_UInt => GliApi.Gli.Format.S8_UInt_Pack8,
|
||||
TextureFormat.D16_UNormS8_UInt => GliApi.Gli.Format.D16_UNorm_S8_UInt_Pack32,
|
||||
TextureFormat.D24_UNormS8_UInt => GliApi.Gli.Format.D24_UNorm_S8_UInt_Pack32,
|
||||
TextureFormat.D32_SFloatS8_UInt => GliApi.Gli.Format.D32_SFloat_S8_UInt_Pack64,
|
||||
TextureFormat.BC1Rgb_UNorm_Block => GliApi.Gli.Format.RGB_DXT1_UNorm_Block8,
|
||||
TextureFormat.BC1Rgb_SRGB_Block => GliApi.Gli.Format.RGB_DXT1_SRGB_Block8,
|
||||
TextureFormat.BC1Rgba_UNorm_Block => GliApi.Gli.Format.RGBA_DXT1_UNorm_Block8,
|
||||
TextureFormat.BC1Rgba_SRGB_Block => GliApi.Gli.Format.RGBA_DXT1_SRGB_Block8,
|
||||
TextureFormat.BC2_UNorm_Block => GliApi.Gli.Format.RGBA_DXT3_UNorm_Block16,
|
||||
TextureFormat.BC2_SRGB_Block => GliApi.Gli.Format.RGBA_DXT3_SRGB_Block16,
|
||||
TextureFormat.BC3_UNorm_Block => GliApi.Gli.Format.RGBA_DXT5_UNorm_Block16,
|
||||
TextureFormat.BC3_SRGB_Block => GliApi.Gli.Format.RGBA_DXT5_SRGB_Block16,
|
||||
TextureFormat.BC4_UNorm_Block => GliApi.Gli.Format.R_ATI1N_UNorm_Block8,
|
||||
TextureFormat.BC4_SNorm_Block => GliApi.Gli.Format.R_ATI1N_SNorm_Block8,
|
||||
TextureFormat.BC5_UNorm_Block => GliApi.Gli.Format.RG_ATI2N_UNorm_Block16,
|
||||
TextureFormat.BC5_SNorm_Block => GliApi.Gli.Format.RG_ATI2N_SNorm_Block16,
|
||||
TextureFormat.ETC2R8G8B8_UNorm_Block => GliApi.Gli.Format.RGB_ETC2_UNorm_Block8,
|
||||
TextureFormat.ETC2R8G8B8_SRGB_Block => GliApi.Gli.Format.RGB_ETC2_SRGB_Block8,
|
||||
TextureFormat.ETC2R8G8B8A8_UNorm_Block => GliApi.Gli.Format.RGBA_ETC2_UNorm_Block16,
|
||||
TextureFormat.ETC2R8G8B8A8_SRGB_Block => GliApi.Gli.Format.RGBA_ETC2_SRGB_Block16,
|
||||
TextureFormat.EACR11_UNorm_Block => GliApi.Gli.Format.R_EAC_UNorm_Block8,
|
||||
TextureFormat.EACR11_SNorm_Block => GliApi.Gli.Format.R_EAC_SNorm_Block8,
|
||||
TextureFormat.EACR11G11_UNorm_Block => GliApi.Gli.Format.RG_EAC_UNorm_Block16,
|
||||
TextureFormat.EACR11G11_SNorm_Block => GliApi.Gli.Format.RG_EAC_SNorm_Block16,
|
||||
TextureFormat.ASTC4X4_UNorm_Block => GliApi.Gli.Format.RGBA_ASTC_4X4_UNorm_Block16,
|
||||
TextureFormat.ASTC4X4_SRGB_Block => GliApi.Gli.Format.RGBA_ASTC_4X4_SRGB_Block16,
|
||||
TextureFormat.ASTC5X4_UNorm_Block => GliApi.Gli.Format.RGBA_ASTC_5X4_UNorm_Block16,
|
||||
TextureFormat.ASTC5X4_SRGB_Block => GliApi.Gli.Format.RGBA_ASTC_5X4_SRGB_Block16,
|
||||
TextureFormat.ASTC5X5_UNorm_Block => GliApi.Gli.Format.RGBA_ASTC_5X5_UNorm_Block16,
|
||||
TextureFormat.ASTC5X5_SRGB_Block => GliApi.Gli.Format.RGBA_ASTC_5X5_SRGB_Block16,
|
||||
TextureFormat.ASTC6X5_UNorm_Block => GliApi.Gli.Format.RGBA_ASTC_6X5_UNorm_Block16,
|
||||
TextureFormat.ASTC6X5_SRGB_Block => GliApi.Gli.Format.RGBA_ASTC_6X5_SRGB_Block16,
|
||||
TextureFormat.ASTC6X6_UNorm_Block => GliApi.Gli.Format.RGBA_ASTC_6X6_UNorm_Block16,
|
||||
TextureFormat.ASTC6X6_SRGB_Block => GliApi.Gli.Format.RGBA_ASTC_6X6_SRGB_Block16,
|
||||
TextureFormat.ASTC8X5_UNorm_Block => GliApi.Gli.Format.RGBA_ASTC_8X5_UNorm_Block16,
|
||||
TextureFormat.ASTC8X5_SRGB_Block => GliApi.Gli.Format.RGBA_ASTC_8X5_SRGB_Block16,
|
||||
TextureFormat.ASTC8X6_UNorm_Block => GliApi.Gli.Format.RGBA_ASTC_8X6_UNorm_Block16,
|
||||
TextureFormat.ASTC8X6_SRGB_Block => GliApi.Gli.Format.RGBA_ASTC_8X6_SRGB_Block16,
|
||||
TextureFormat.ASTC8X8_UNorm_Block => GliApi.Gli.Format.RGBA_ASTC_8X8_UNorm_Block16,
|
||||
TextureFormat.ASTC8X8_SRGB_Block => GliApi.Gli.Format.RGBA_ASTC_8X8_SRGB_Block16,
|
||||
TextureFormat.ASTC10X5_UNorm_Block => GliApi.Gli.Format.RGBA_ASTC_10X5_UNorm_Block16,
|
||||
TextureFormat.ASTC10X5_SRGB_Block => GliApi.Gli.Format.RGBA_ASTC_10X5_SRGB_Block16,
|
||||
TextureFormat.ASTC10X6_UNorm_Block => GliApi.Gli.Format.RGBA_ASTC_10X6_UNorm_Block16,
|
||||
TextureFormat.ASTC10X6_SRGB_Block => GliApi.Gli.Format.RGBA_ASTC_10X6_SRGB_Block16,
|
||||
TextureFormat.ASTC10X8_UNorm_Block => GliApi.Gli.Format.RGBA_ASTC_10X8_UNorm_Block16,
|
||||
TextureFormat.ASTC10X8_SRGB_Block => GliApi.Gli.Format.RGBA_ASTC_10X8_SRGB_Block16,
|
||||
TextureFormat.ASTC10X10_UNorm_Block => GliApi.Gli.Format.RGBA_ASTC_10X10_UNorm_Block16,
|
||||
TextureFormat.ASTC10X10_SRGB_Block => GliApi.Gli.Format.RGBA_ASTC_10X10_SRGB_Block16,
|
||||
TextureFormat.ASTC12X10_UNorm_Block => GliApi.Gli.Format.RGBA_ASTC_12X10_UNorm_Block16,
|
||||
TextureFormat.ASTC12X10_SRGB_Block => GliApi.Gli.Format.RGBA_ASTC_12X10_SRGB_Block16,
|
||||
TextureFormat.ASTC12X12_UNorm_Block => GliApi.Gli.Format.RGBA_ASTC_12X12_UNorm_Block16,
|
||||
TextureFormat.ASTC12X12_SRGB_Block => GliApi.Gli.Format.RGBA_ASTC_12X12_SRGB_Block16,
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(format), format, null),
|
||||
};
|
||||
}
|
||||
}
|
||||
42
Brutal.Texture/Gli/GliTexture.cs
Normal file
42
Brutal.Texture/Gli/GliTexture.cs
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
using Brutal.GliApi;
|
||||
|
||||
namespace Brutal.TextureApi.Gli;
|
||||
|
||||
public class GliTexture(Brutal.GliApi.Gli.Texture nativeTexture) : ITexture
|
||||
{
|
||||
public unsafe Span<byte> ImageData(int level, int layer, int faceOrSlice)
|
||||
{
|
||||
var sliceSize = _impl.ImageSize(level) / Extent.Z;
|
||||
|
||||
if (_impl.Target == Brutal.GliApi.Gli.Target._3D)
|
||||
return new(_impl.ImageData(level, layer, 0) + sliceSize * faceOrSlice, (int)sliceSize);
|
||||
|
||||
return new(_impl.ImageData(level, layer, faceOrSlice), (int)sliceSize);
|
||||
}
|
||||
|
||||
public unsafe Span<byte> Data => new(_impl.Data, (int)_impl.Size);
|
||||
public TexelCount3 Extent => new(_impl.Extent.X, _impl.Extent.Y, _impl.Extent.Z);
|
||||
public TextureFormat Format => _impl.Format.ToTextureFormat();
|
||||
public int LayerCount => (int)_impl.Layers;
|
||||
public int LevelCount => (int)_impl.Levels;
|
||||
public int FaceCount => (int)_impl.Faces;
|
||||
public bool IsCubemap => _impl.Target.IsCube();
|
||||
public bool IsArray => _impl.Target.IsArray();
|
||||
public int DimensionCount => _impl.Target.DimensionCount();
|
||||
|
||||
public ByteSize RowPitch(int level)
|
||||
{
|
||||
if (level < 0 || level >= LevelCount)
|
||||
throw new ArgumentOutOfRangeException(nameof(level), level, $"Level must be in range [0, {LevelCount})");
|
||||
return ByteSize.Of(_impl.ImageRowPitch(level));
|
||||
}
|
||||
|
||||
public static implicit operator Brutal.GliApi.Gli.Texture(GliTexture texture) => texture._impl;
|
||||
|
||||
private readonly GliApi.Gli.Texture _impl = nativeTexture;
|
||||
|
||||
public void Destroy()
|
||||
{
|
||||
_impl.Dispose();
|
||||
}
|
||||
}
|
||||
9
Brutal.Texture/Gli/GliTextureException.cs
Normal file
9
Brutal.Texture/Gli/GliTextureException.cs
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
using Brutal.TextureApi;
|
||||
|
||||
namespace Brutal.TextureApi.Gli;
|
||||
|
||||
public class GliTextureException : TextureException
|
||||
{
|
||||
public GliTextureException(string message)
|
||||
: base(message) { }
|
||||
}
|
||||
36
Brutal.Texture/Gli/GliTextureLoader.cs
Normal file
36
Brutal.Texture/Gli/GliTextureLoader.cs
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
using System.Collections.Frozen;
|
||||
using Brutal.Pointers.Extensions;
|
||||
|
||||
namespace Brutal.TextureApi.Gli;
|
||||
|
||||
public static partial class Loader
|
||||
{
|
||||
public static GliTexture LoadFromFile(string filePath)
|
||||
{
|
||||
var nativeTexture = new GliApi.Gli.Texture(filePath);
|
||||
|
||||
if (nativeTexture.IsNull())
|
||||
{
|
||||
nativeTexture.Dispose();
|
||||
throw new GliTextureException($"Failed to load texture from filePath \"{filePath}\"");
|
||||
}
|
||||
|
||||
return new GliTexture(nativeTexture);
|
||||
}
|
||||
|
||||
public static unsafe GliTexture LoadFromMemory(ReadOnlySpan<byte> data)
|
||||
{
|
||||
var nativeTexture = new GliApi.Gli.Texture(data);
|
||||
|
||||
if (nativeTexture.IsNull())
|
||||
{
|
||||
nativeTexture.Dispose();
|
||||
throw new GliTextureException($"Failed to load texture from data at 0x{(nint)data.AsPointer()}");
|
||||
}
|
||||
|
||||
return new GliTexture(nativeTexture);
|
||||
}
|
||||
|
||||
public static FrozenSet<string> SupportedExtensions { get; } =
|
||||
FrozenSet.Create(StringComparer.OrdinalIgnoreCase, [".dds", ".ktx", ".kmg"]);
|
||||
}
|
||||
3
Brutal.Texture/Gli/GliTextureSaveException.cs
Normal file
3
Brutal.Texture/Gli/GliTextureSaveException.cs
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
namespace Brutal.TextureApi.Gli;
|
||||
|
||||
public class GliTextureSaveException(string message) : GliTextureException(message);
|
||||
59
Brutal.Texture/Gli/GliTextureWriter.cs
Normal file
59
Brutal.Texture/Gli/GliTextureWriter.cs
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
namespace Brutal.TextureApi.Gli;
|
||||
|
||||
public static partial class Writer
|
||||
{
|
||||
public static void SaveToFile(ITexture texture, string filePath)
|
||||
{
|
||||
if (texture is GliTexture gliTexture)
|
||||
{
|
||||
SaveToFileImpl(gliTexture, filePath);
|
||||
return;
|
||||
}
|
||||
|
||||
SaveToFile(
|
||||
new TextureDescriptor
|
||||
{
|
||||
Data = texture.Data,
|
||||
Extent = texture.Extent,
|
||||
Format = texture.Format,
|
||||
LayerCount = texture.LayerCount,
|
||||
FaceCount = texture.FaceCount,
|
||||
MipmapCount = texture.LevelCount,
|
||||
IsCubemap = texture.IsCubemap,
|
||||
},
|
||||
filePath
|
||||
);
|
||||
}
|
||||
|
||||
public static void SaveToFile(TextureDescriptor descriptor, string filePath)
|
||||
{
|
||||
var gliTarget = GliConversionUtils.GetTarget(descriptor.Extent, descriptor.LayerCount, descriptor.IsCubemap);
|
||||
|
||||
using var gliNative = new GliApi.Gli.Texture(
|
||||
gliTarget,
|
||||
descriptor.Format.ToGliFormat(),
|
||||
new(descriptor.Extent.X, descriptor.Extent.Y, descriptor.Extent.Z),
|
||||
descriptor.LayerCount,
|
||||
descriptor.FaceCount,
|
||||
descriptor.MipmapCount,
|
||||
GliApi.Gli.Swizzles.Identity
|
||||
);
|
||||
|
||||
unsafe
|
||||
{
|
||||
descriptor.Data.CopyTo(new Span<byte>(gliNative.Data, descriptor.Data.Length));
|
||||
}
|
||||
|
||||
SaveToFileImpl(gliNative, filePath);
|
||||
}
|
||||
|
||||
private static void SaveToFileImpl(GliApi.Gli.Texture texture, string filePath)
|
||||
{
|
||||
var result = texture.Save(filePath);
|
||||
|
||||
if (!result)
|
||||
{
|
||||
throw new GliTextureSaveException($"Failed to save texture to file at path: \"{filePath}\".");
|
||||
}
|
||||
}
|
||||
}
|
||||
75
Brutal.Texture/ITexture.cs
Normal file
75
Brutal.Texture/ITexture.cs
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
namespace Brutal.TextureApi;
|
||||
|
||||
/// <summary>
|
||||
/// Interface representing a texture.
|
||||
/// </summary>
|
||||
public interface ITexture
|
||||
{
|
||||
/// <summary>
|
||||
/// The texel data for this texture
|
||||
/// </summary>
|
||||
public Span<byte> Data { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The data for one image of the texture.
|
||||
/// </summary>
|
||||
/// <remarks>You cannot have 3d cubemaps, therefore the third parameter
|
||||
/// is used for face or slice depending on the type of texture.</remarks>
|
||||
/// <param name="level">The mip level of data to get</param>
|
||||
/// <param name="layer">The array layer of data to get</param>
|
||||
/// <param name="faceOrSlice">The face (If a cube map) or slice (if a depth texture) of data to get.</param>
|
||||
/// <exception cref="TextureException">Thrown if data cannot be retrieved</exception>
|
||||
public Span<byte> ImageData(int level, int layer, int faceOrSlice);
|
||||
|
||||
/// <summary>
|
||||
/// The extent of this texture (in texels). This is equal to the extent of the largest
|
||||
/// mip level of the texture
|
||||
/// </summary>
|
||||
public TexelCount3 Extent { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="TextureFormat" /> of this texture. <para />You can convert this to a Vulkan
|
||||
/// texture format with a call to ToVkFormat(). e.g.
|
||||
/// myITexture.Format.ToVkFormat();
|
||||
/// </summary>
|
||||
public TextureFormat Format { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The number of array layers in this texture
|
||||
/// </summary>
|
||||
public int LayerCount { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The number of levels (mipmaps) in this texture
|
||||
/// </summary>
|
||||
public int LevelCount { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The number of faces in this texture. This is typically 1 unless IsCubemap == true
|
||||
/// in which it could be up to 6
|
||||
/// </summary>
|
||||
public int FaceCount { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Whether this texture is defined as a cubemap
|
||||
/// </summary>
|
||||
public bool IsCubemap { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns whether this ITexture is an array texture.
|
||||
/// </summary>
|
||||
public bool IsArray { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns the number of dimensions of this ITexture.
|
||||
/// </summary>
|
||||
public int DimensionCount { get; } // 1, 2 or 3d
|
||||
|
||||
/// <summary>
|
||||
/// Byte count from start of one row of the texture to the next
|
||||
/// </summary>
|
||||
/// <param name="level">Mip level to get the pitch for</param>
|
||||
/// <returns>The number of bytes from start of one row to start of the next row</returns>
|
||||
/// <exception cref="ArgumentOutOfRangeException">Thrown if level is less than 0 or greater than mip levels - 1</exception>
|
||||
public ByteSize RowPitch(int level);
|
||||
}
|
||||
113
Brutal.Texture/Ktx/KtxConverter.cs
Normal file
113
Brutal.Texture/Ktx/KtxConverter.cs
Normal file
|
|
@ -0,0 +1,113 @@
|
|||
using Brutal.KtxApi;
|
||||
|
||||
namespace Brutal.TextureApi.Ktx;
|
||||
|
||||
/// <summary>
|
||||
/// Static class that provides conversion functions from other formats to a KtxTexture.
|
||||
/// </summary>
|
||||
public static partial class Converter
|
||||
{
|
||||
/// <summary>
|
||||
/// Takes any ITexture and creates a KTX version 2 ITexture from it. The original
|
||||
/// texture is kept as is and it is up to the caller to unload it if it is no longer
|
||||
/// used.
|
||||
/// </summary>
|
||||
/// <param name="texture">The ITexture to use as the source</param>
|
||||
/// <returns>The KTX Version 2 texture created from the source texture</returns>
|
||||
public static KtxTexture ToV2KtxTexture(ITexture texture)
|
||||
{
|
||||
var saveData = new TextureDescriptor
|
||||
{
|
||||
Data = texture.Data,
|
||||
Extent = texture.Extent,
|
||||
Format = texture.Format,
|
||||
LayerCount = texture.LayerCount,
|
||||
FaceCount = texture.FaceCount,
|
||||
MipmapCount = texture.LevelCount,
|
||||
IsCubemap = texture.IsCubemap,
|
||||
};
|
||||
|
||||
return new KtxTexture(ToKtxTexture2Handle(saveData));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a KTX version 2 texture handle from a TextureSaveInfo structure.
|
||||
/// It is your responsibility to free the texture handle.
|
||||
/// </summary>
|
||||
/// <param name="descriptor">The structure describing the texture to build</param>
|
||||
/// <returns>KtxTexture2Handle of the created texture</returns>
|
||||
/// <exception cref="KtxTextureException">Thrown if the texture could not be created, or
|
||||
/// the new texture data size is different to the source data size</exception>
|
||||
internal static unsafe KtxTexture2Handle ToKtxTexture2Handle(TextureDescriptor descriptor)
|
||||
{
|
||||
ValidateSaveInfo(descriptor);
|
||||
|
||||
var createInfo = new KtxTextureCreateInfo()
|
||||
{
|
||||
BaseWidth = (uint)descriptor.Extent.X,
|
||||
BaseHeight = (uint)descriptor.Extent.Y,
|
||||
BaseDepth = (uint)descriptor.Extent.Z,
|
||||
GenerateMipmaps = false,
|
||||
NumLevels = (uint)descriptor.MipmapCount,
|
||||
NumLayers = (uint)descriptor.LayerCount,
|
||||
NumDimensions = CalculateDimensions(descriptor),
|
||||
NumFaces = (uint)descriptor.FaceCount,
|
||||
IsArray =
|
||||
descriptor.Extent.X > 0
|
||||
&& descriptor.LayerCount > 1
|
||||
&& (descriptor.FaceCount == 1 || descriptor.IsCubemap),
|
||||
PDfd = null,
|
||||
VkFormat = (uint)descriptor.Format,
|
||||
GlInternalformat = 0, // Ignored
|
||||
};
|
||||
|
||||
var createResult = KtxTexture2.Create(
|
||||
ref createInfo,
|
||||
KtxTextureCreateStorageEnum.TextureCreateAllocStorage,
|
||||
out var texture2Handle
|
||||
);
|
||||
if (createResult != KtxErrorCode.Success)
|
||||
throw new KtxTextureException(
|
||||
$"Failed to create texture to save. KtxErrorCode:{createResult}",
|
||||
createResult
|
||||
);
|
||||
|
||||
if (descriptor.Data.Length != (int)texture2Handle.GetDataSize())
|
||||
throw new KtxTextureException(
|
||||
$"Size mismatch. Brutal.Ktx allocated {texture2Handle.GetDataSize()} bytes but the save data had {descriptor.Data.Length}"
|
||||
);
|
||||
|
||||
descriptor.Data.CopyTo(new Span<byte>(texture2Handle.GetData(), (int)texture2Handle.GetDataSize()));
|
||||
|
||||
return texture2Handle;
|
||||
}
|
||||
|
||||
private static void ValidateSaveInfo(TextureDescriptor descriptor)
|
||||
{
|
||||
if (descriptor.Extent.X == 0)
|
||||
throw new KtxTextureException("Texture width cannot be 0");
|
||||
|
||||
if (descriptor.FaceCount == 6 && (descriptor.Extent.Y != descriptor.Extent.X || descriptor.Extent.Z != 1))
|
||||
throw new KtxTextureException(
|
||||
"Cubemap textures must have 6 faces, have the same width and height, and have a depth of 1"
|
||||
);
|
||||
}
|
||||
|
||||
private static uint CalculateDimensions(TextureDescriptor descriptor)
|
||||
{
|
||||
if (descriptor.IsCubemap)
|
||||
return 2;
|
||||
|
||||
var saveDataExtent = descriptor.Extent;
|
||||
// A depth of 1 is a 2D texture, not a 3D one. Only Z > 1 denotes a true 3D (volume) texture.
|
||||
if (saveDataExtent is { X: > 0, Y: > 0, Z: > 1 })
|
||||
return 3;
|
||||
if (saveDataExtent is { X: > 0, Y: > 0, Z: 1 })
|
||||
return 2;
|
||||
if (saveDataExtent is { X: > 0, Y: 1, Z: 1 })
|
||||
return 1;
|
||||
throw new KtxTextureException(
|
||||
$"Invalid extent for dimension calculation. extent: {saveDataExtent.X}, {saveDataExtent.Y}, {saveDataExtent.Z}"
|
||||
);
|
||||
}
|
||||
}
|
||||
196
Brutal.Texture/Ktx/KtxTexture.cs
Normal file
196
Brutal.Texture/Ktx/KtxTexture.cs
Normal file
|
|
@ -0,0 +1,196 @@
|
|||
using System.Text;
|
||||
using Brutal.KtxApi;
|
||||
using Brutal.KtxApi.Abstractions;
|
||||
|
||||
namespace Brutal.TextureApi.Ktx;
|
||||
|
||||
/*
|
||||
* If facecount == 6 (IsCubemap == true) then width and height must be the same and depth must be 0
|
||||
* pixelHeight must not be 0 for block-compressed formats, including BasisLZ/ETC1S and UASTC.
|
||||
* pixelDepth must not be 0 for block formats that have block depth > 1
|
||||
* pixelDepth must be 0 for depth or stencil formats.
|
||||
*
|
||||
* layercount is number of array elements. If not an array then layercount must be 0.
|
||||
*
|
||||
* faceCount specifies the number of cubemap faces.
|
||||
* For cubemaps and cubemap arrays this must be 6. For non cubemaps this must be 1.
|
||||
* Cubemap faces are stored in the order: +X, -X, +Y, -Y, +Z, -Z in a left-handed coordinate system with
|
||||
* +Y up and, with the +Z face forward, +X on the on the right.
|
||||
* All faces must have the same orientation which must be rd (top-left origin) which is assumed in the absence of
|
||||
* Section 5.2, “KTXorientation” metadata. See Appendix A, Cubemap Orientation for details.
|
||||
* Applications wanting to store incomplete cubemaps should flatten faces into a 2D array and use the metadata described in
|
||||
* Section 5.1, “KTXcubemapIncomplete” to signal which faces are present.
|
||||
*
|
||||
* Type pixelWidth pixelHeight pixelDepth layerCount faceCount
|
||||
* 1D > 0 0 0 0 1
|
||||
* 2D > 0 > 0 0 0 1
|
||||
* 3D > 0 > 0 >0 0 1
|
||||
* Cubemap > 0 > 0 0 0 6
|
||||
* 1D Array > 0 0 0 >0 1
|
||||
* 2d Array > 0 > 0 0 >0 1
|
||||
* 3d Array > 0 > 0 >0 >0 1
|
||||
* Cubemap Arr > 0 > 0 0 >0 6
|
||||
*/
|
||||
|
||||
public class KtxTexture : ITexture
|
||||
{
|
||||
/// <summary>
|
||||
/// Create a KtxTexture from an existing KtxTextureHandle.
|
||||
/// </summary>
|
||||
/// <remarks> The commonTextureHandle must not currently be supercompressed.</remarks>
|
||||
/// <param name="commonTextureHandle">A native handle to a KTX V1 or V2 texture</param>
|
||||
/// <exception cref="TextureException">Thrown if the texture referenced by commonTextureHandle is super compressed</exception>
|
||||
public KtxTexture(KtxTextureHandle commonTextureHandle)
|
||||
{
|
||||
_commonTextureHandle = commonTextureHandle;
|
||||
if (_commonTextureHandle.NeedsTranscoding())
|
||||
throw new TextureException("Texture is supercompressed.");
|
||||
}
|
||||
|
||||
#region ITexture Implementation
|
||||
|
||||
/// <inheritdoc cref="ITexture.Data"/>
|
||||
public unsafe Span<byte> Data
|
||||
{
|
||||
get
|
||||
{
|
||||
EnsureLoaded();
|
||||
return new Span<byte>(_commonTextureHandle.GetData(), (int)_commonTextureHandle.GetDataSize());
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="ITexture.Extent"/>
|
||||
public TexelCount3 Extent =>
|
||||
new(
|
||||
(int)_commonTextureHandle.BaseWidth,
|
||||
(int)_commonTextureHandle.BaseHeight,
|
||||
(int)_commonTextureHandle.BaseDepth
|
||||
);
|
||||
|
||||
/// <inheritdoc cref="ITexture.Format"/>
|
||||
public TextureFormat Format => (TextureFormat)_commonTextureHandle.GetVkFormat();
|
||||
|
||||
/// <inheritdoc cref="ITexture.LayerCount"/>
|
||||
public int LayerCount => (int)_commonTextureHandle.NumLayers;
|
||||
|
||||
/// <inheritdoc cref="ITexture.LevelCount"/>
|
||||
public int LevelCount => (int)_commonTextureHandle.NumLevels;
|
||||
|
||||
/// <inheritdoc cref="ITexture.FaceCount"/>
|
||||
public int FaceCount => (int)_commonTextureHandle.NumFaces;
|
||||
|
||||
/// <inheritdoc cref="ITexture.IsCubemap"/>
|
||||
public bool IsCubemap => _commonTextureHandle.IsCubemap;
|
||||
|
||||
/// <inheritdoc cref="ITexture.IsArray"/>
|
||||
public bool IsArray => _commonTextureHandle.IsArray;
|
||||
|
||||
/// <inheritdoc cref="ITexture.DimensionCount"/>
|
||||
public int DimensionCount => (int)_commonTextureHandle.NumDimensions;
|
||||
|
||||
/// <inheritdoc cref="ITexture.RowPitch"/>
|
||||
public unsafe ByteSize RowPitch(int level)
|
||||
{
|
||||
if (level < 0 || level >= LevelCount)
|
||||
throw new ArgumentOutOfRangeException(nameof(level), level, $"Level must be in range [0, {LevelCount})");
|
||||
|
||||
_rowPitch ??= new ByteSize[LevelCount];
|
||||
if (_rowPitch[level] != 0)
|
||||
return _rowPitch[level];
|
||||
|
||||
if (_commonTextureHandle.ClassId == ClassId.Texture2C)
|
||||
{
|
||||
var formatSize = _commonTextureHandle.Protected->FormatSize;
|
||||
var blocksWide = Math.Max(1, (_commonTextureHandle.BaseWidth / formatSize.BlockWidth) >> level);
|
||||
_rowPitch[level] = ByteSize.Of(blocksWide * ((int)formatSize.BlockSizeInBits >> 3));
|
||||
return _rowPitch[level];
|
||||
}
|
||||
|
||||
_rowPitch[level] = (ByteSize)_commonTextureHandle.GetRowPitch((uint)level);
|
||||
return _rowPitch[level];
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="ITexture.ImageData"/>
|
||||
public Span<byte> ImageData(int level, int layer, int face)
|
||||
{
|
||||
EnsureLoaded();
|
||||
|
||||
var getResult = _commonTextureHandle.GetImageData((uint)level, (uint)layer, (uint)face, out var data);
|
||||
if (getResult != KtxErrorCode.Success)
|
||||
throw new TextureException(
|
||||
$"Cannot get image data for KtxTexture. level:{level} layer:{layer} face:{face} error:{getResult}"
|
||||
);
|
||||
return data;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Returns a native handle to the KtxTexture
|
||||
/// </summary>
|
||||
/// <value></value>
|
||||
public KtxTextureHandle UnderlyingKtxTextureHandle => _commonTextureHandle;
|
||||
|
||||
/// <summary>
|
||||
/// Frees all resources held by the the KtxTexture. After calling Destroy() the
|
||||
/// KtxTexture object is unusable.
|
||||
/// </summary>
|
||||
public void Destroy()
|
||||
{
|
||||
if (_commonTextureHandle.IsNotNull())
|
||||
{
|
||||
_commonTextureHandle.Destroy();
|
||||
_commonTextureHandle = default;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Saves a KtxTexture to a file on disk.
|
||||
/// </summary>
|
||||
/// <param name="filePath"></param>
|
||||
/// <returns></returns>
|
||||
public KtxErrorCode SaveToFile(string filePath)
|
||||
{
|
||||
EnsureLoaded();
|
||||
return _commonTextureHandle.WriteToNamedFile(Encoding.UTF8.GetBytes(filePath));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns whether the row pitch for the texture has been padded to 4 bytes.
|
||||
/// <remarks>
|
||||
/// This is a special requirement for V1 Ktx files. The spec required the alignment of rows of uncompresed textures to 4 bytes.
|
||||
/// This requirement was removed for V2 textures but we do need to know if packing is occurring for V1 if using OpenGl,
|
||||
/// so we can set GL_UNPACK_ALIGNMENT.
|
||||
/// If this function returns true, then set GL_UNPACK_ALIGNMENT to 4 before uploading the texture otherwise
|
||||
/// set it to 1.</remarks>
|
||||
/// </summary>
|
||||
/// <param name="level">What level to check for padding</param>
|
||||
/// <returns>True if row pitch was padded to 4 bytes, false if not.</returns>
|
||||
/// <exception cref="TextureException">Thrown if the image is supercompressed</exception>
|
||||
public bool IsRow4BytePadded(int level)
|
||||
{
|
||||
if (_commonTextureHandle.ClassId == ClassId.Texture2C)
|
||||
return false;
|
||||
|
||||
// Catches compressed textures (All multiple of 4 in element size), and uncompressed with 4 byte elements
|
||||
if (_commonTextureHandle.GetElementSize() % 4 == 0)
|
||||
return false;
|
||||
|
||||
return _commonTextureHandle.GetRowPitch((uint)level)
|
||||
!= _commonTextureHandle.GetElementSize() * (_commonTextureHandle.BaseWidth >> level);
|
||||
}
|
||||
|
||||
#region Private
|
||||
private unsafe void EnsureLoaded()
|
||||
{
|
||||
if (_commonTextureHandle.GetData() != null)
|
||||
return;
|
||||
var loadResult = _commonTextureHandle.LoadImageData(null);
|
||||
if (loadResult != KtxErrorCode.Success)
|
||||
throw new TextureException($"Cannot load image data for KtxTexture. KtxErrorCode:{loadResult}");
|
||||
}
|
||||
|
||||
private KtxTextureHandle _commonTextureHandle;
|
||||
private ByteSize[]? _rowPitch;
|
||||
#endregion
|
||||
}
|
||||
15
Brutal.Texture/Ktx/KtxTextureException.cs
Normal file
15
Brutal.Texture/Ktx/KtxTextureException.cs
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
using Brutal.KtxApi;
|
||||
using Brutal.TextureApi;
|
||||
|
||||
namespace Brutal.TextureApi.Ktx;
|
||||
|
||||
public class KtxTextureException : TextureException
|
||||
{
|
||||
public KtxTextureException(string message, KtxErrorCode? errorCode = null)
|
||||
: base(message)
|
||||
{
|
||||
ErrorCode = errorCode;
|
||||
}
|
||||
|
||||
public KtxErrorCode? ErrorCode { get; }
|
||||
}
|
||||
94
Brutal.Texture/Ktx/KtxTextureLoader.cs
Normal file
94
Brutal.Texture/Ktx/KtxTextureLoader.cs
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
using System.Collections.Frozen;
|
||||
using System.Text;
|
||||
using Brutal.KtxApi;
|
||||
using Brutal.KtxApi.Abstractions;
|
||||
|
||||
namespace Brutal.TextureApi.Ktx;
|
||||
|
||||
public static partial class Loader
|
||||
{
|
||||
/// <summary>
|
||||
/// Loads a ktx texture from a file.
|
||||
/// </summary>
|
||||
/// <param name="filePath">Path to file</param>
|
||||
/// <param name="loadOptions">Options for the load</param>
|
||||
/// <returns>Texture that was loaded</returns>
|
||||
/// <exception cref="KtxTextureException">If the texture could not be loaded.
|
||||
/// e.g The SuperCompressionFamily was not compatible with the texture's data,
|
||||
/// The file could not be found etc.
|
||||
/// </exception>
|
||||
public static KtxTexture LoadFromFile(string filePath, LoadSettings? loadOptions = null)
|
||||
{
|
||||
var loadSettings = loadOptions ?? LoadSettings.Default;
|
||||
|
||||
var loadResult = KtxApi.KtxTexture.CreateFromNamedFile(
|
||||
Encoding.UTF8.GetBytes(filePath),
|
||||
LoadFlags(loadSettings),
|
||||
out var textureHandle
|
||||
);
|
||||
|
||||
if (loadResult != KtxErrorCode.Success)
|
||||
{
|
||||
textureHandle.Dispose();
|
||||
throw new KtxTextureException(
|
||||
$"Failed to load texture from filePath \"{filePath}\". error:{loadResult}",
|
||||
loadResult
|
||||
);
|
||||
}
|
||||
|
||||
if (!textureHandle.NeedsTranscoding())
|
||||
return new KtxTexture(textureHandle);
|
||||
|
||||
var result = Transcode(textureHandle, loadSettings.SuperCompressionTranscodeFormat);
|
||||
if (result == KtxErrorCode.Success)
|
||||
return new KtxTexture(textureHandle);
|
||||
|
||||
textureHandle.Dispose();
|
||||
throw new KtxTextureException($"Failed to transcode supercompressed texture. KtxErrorCode:{result}");
|
||||
}
|
||||
|
||||
public static KtxTexture LoadFromMemory(ReadOnlySpan<byte> data, LoadSettings? loadData = null)
|
||||
{
|
||||
var loadSettings = loadData ?? new LoadSettings();
|
||||
|
||||
var createResult = KtxTexture2.CreateFromMemory(data, LoadFlags(loadSettings), out var texture2Handle);
|
||||
if (createResult != KtxErrorCode.Success)
|
||||
throw new KtxTextureException("Failed to create texture from memory", createResult);
|
||||
|
||||
return new KtxTexture(texture2Handle);
|
||||
}
|
||||
|
||||
public class LoadSettings : TextureLoadSettings
|
||||
{
|
||||
public static LoadSettings Default { get; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// If the texture is supercompressed, this indicates the format to transcode it to on load.
|
||||
/// </summary>
|
||||
/// <value>Defaults to KtxTranscodeFmt.Bc7Rgba</value>
|
||||
public KtxTranscodeFmt SuperCompressionTranscodeFormat = KtxTranscodeFmt.Bc7Rgba;
|
||||
|
||||
/// <summary>
|
||||
/// If true, does not immediately load the texture data.
|
||||
/// Data is loaded when accessed.
|
||||
/// This flag is ignored if the texture is super compressed and needs transcoding because transcoding
|
||||
/// will happen when loading the file.
|
||||
/// </summary>
|
||||
public bool LazyLoad = false;
|
||||
}
|
||||
|
||||
private static KtxTextureCreateFlagBits LoadFlags(LoadSettings loadSettings)
|
||||
{
|
||||
return loadSettings.LazyLoad ? KtxTextureCreateFlagBits.NoFlags : KtxTextureCreateFlagBits.LoadImageDataBit;
|
||||
}
|
||||
|
||||
private static KtxErrorCode Transcode(KtxTextureHandle textureHandle, KtxTranscodeFmt transcodeFormat)
|
||||
{
|
||||
var ktxTexture2Handle = textureHandle.AsKtxTexture2Handle();
|
||||
var result = ktxTexture2Handle.TranscodeBasis(transcodeFormat, KtxTranscodeFlagBits.HighQuality);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static FrozenSet<string> SupportedExtensions { get; } =
|
||||
FrozenSet.Create(StringComparer.OrdinalIgnoreCase, [".ktx", ".ktx2"]);
|
||||
}
|
||||
34
Brutal.Texture/Ktx/KtxTextureWriter.cs
Normal file
34
Brutal.Texture/Ktx/KtxTextureWriter.cs
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
using Brutal.KtxApi;
|
||||
|
||||
namespace Brutal.TextureApi.Ktx;
|
||||
|
||||
public static partial class Writer
|
||||
{
|
||||
/// <summary>
|
||||
/// Writes an ITexture
|
||||
/// </summary>
|
||||
/// <param name="texture"></param>
|
||||
/// <param name="filePath"></param>
|
||||
/// <exception cref="KtxTextureException"></exception>
|
||||
public static void SaveToFile(ITexture texture, string filePath)
|
||||
{
|
||||
var ktxTexture = texture as KtxTexture ?? Converter.ToV2KtxTexture(texture);
|
||||
try
|
||||
{
|
||||
var saveResult = ktxTexture.SaveToFile(filePath);
|
||||
if (saveResult != KtxErrorCode.Success)
|
||||
throw new KtxTextureException($"Failed to save texture KtxErrorCode:{saveResult}", saveResult);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (texture is not KtxTexture)
|
||||
ktxTexture.Destroy();
|
||||
}
|
||||
}
|
||||
|
||||
public static void SaveToFile(TextureDescriptor descriptor, string filePath)
|
||||
{
|
||||
var texture2Handle = Converter.ToKtxTexture2Handle(descriptor);
|
||||
SaveToFile(new KtxTexture(texture2Handle), filePath);
|
||||
}
|
||||
}
|
||||
141
Brutal.Texture/Resources/ResourceTracker.cs
Normal file
141
Brutal.Texture/Resources/ResourceTracker.cs
Normal file
|
|
@ -0,0 +1,141 @@
|
|||
using System.Collections.Concurrent;
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.ConstrainedExecution;
|
||||
|
||||
namespace Brutal.TextureApi;
|
||||
|
||||
/// <summary>
|
||||
/// Provides basic functionality for tracking resources and their disposal. Call Track() to record an instance
|
||||
/// of a resource and Untrack() to stop tracking.
|
||||
/// <para/>
|
||||
/// If you wish to see what resources are still being tracked, along with a formatted stacktrace of when they
|
||||
/// were tracked you can call <see cref="CurrentResources"/>.
|
||||
/// <para/>
|
||||
/// Tracking and untracking resource is threadsafe and can be done from different threads. ResourceTracker uses a
|
||||
/// <see cref="ConcurrentDictionary{TKey, TValue}" /> underneath and Interlocked calls to manage internal counts.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// ARKB: This is a very rudimentary implementation just so I could see the usage of textures in KSA. This class is internal
|
||||
/// for a reason and not directly usable outside Brutal for now.
|
||||
/// Making this a CriticalFinalizerObject means its finaliser will run after normal finalisers, giving
|
||||
/// a chance for things to Untrack first.</remarks>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
internal class ResourceTracker<T> : CriticalFinalizerObject
|
||||
where T : notnull
|
||||
{
|
||||
public readonly struct TrackedResource
|
||||
{
|
||||
public T Instance { get; init; }
|
||||
public StackTrace? StackTrace { get; init; }
|
||||
}
|
||||
|
||||
public ResourceTracker()
|
||||
{
|
||||
_trackedInstances = new(new Comparer());
|
||||
#if RecordTrackingOverhead
|
||||
_stopwatch = new Stopwatch();
|
||||
#endif
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
Interlocked.Exchange(ref _totalResourcesTracked, 0);
|
||||
_trackedInstances.Clear();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The total number of resource items tracked since the tracker was
|
||||
/// created.
|
||||
/// </summary>
|
||||
public ulong TotalTrackedCount => Interlocked.Read(ref _totalResourcesTracked);
|
||||
|
||||
/// <summary>
|
||||
/// The number of resources currently being tracked. This is the difference of
|
||||
/// TotalTrackedCount and the number of successfully Untrack()'ed resources.
|
||||
/// </summary>
|
||||
public ulong CurrentTrackedCount => (ulong)_trackedInstances.Count;
|
||||
|
||||
/// <summary>
|
||||
/// Track a new resource instance. The resource must NOT already be tracked.
|
||||
/// </summary>
|
||||
/// <param name="resource">The object to track</param>
|
||||
/// <exception cref="InvalidOperationException">Thrown if the resource is already being tracked</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
|
||||
public void Track(in T resource)
|
||||
{
|
||||
#if RecordTrackingOverhead
|
||||
var start = Stopwatch.GetTimestamp();
|
||||
#endif
|
||||
if (!_trackedInstances.TryAdd(new() { Instance = resource, StackTrace = new StackTrace(1, true) }, 0))
|
||||
throw new InvalidOperationException($"Object of type {typeof(T).Name} already being tracked");
|
||||
Interlocked.Increment(ref _totalResourcesTracked);
|
||||
#if RecordTrackingOverhead
|
||||
_totalTimeSpentTracking += Stopwatch.GetTimestamp() - start;
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stop tracking a resource.
|
||||
/// </summary>
|
||||
/// <param name="resource">The resource to stop tracking</param>
|
||||
/// <exception cref="InvalidOperationException">Thrown if the resource isn't being tracked</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
|
||||
public void Untrack(in T resource)
|
||||
{
|
||||
#if RecordTrackingOverhead
|
||||
var start = Stopwatch.GetTimestamp();
|
||||
#endif
|
||||
if (!_trackedInstances.TryRemove(new() { Instance = resource }, out _))
|
||||
throw new InvalidOperationException($"Object {resource} not found in tracked instances.");
|
||||
#if RecordTrackingOverhead
|
||||
_totalTimeSpentTracking += Stopwatch.GetTimestamp() - start;
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Return a collection of TrackedResource objects.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public IReadOnlyCollection<TrackedResource> CurrentResources()
|
||||
{
|
||||
return _trackedInstances.AsReadOnly().Keys;
|
||||
}
|
||||
|
||||
public class Comparer : IEqualityComparer<TrackedResource>
|
||||
{
|
||||
public bool Equals(TrackedResource x, TrackedResource y)
|
||||
{
|
||||
return ReferenceEquals(x.Instance, y.Instance);
|
||||
}
|
||||
|
||||
public int GetHashCode(TrackedResource obj)
|
||||
{
|
||||
return RuntimeHelpers.GetHashCode(obj.Instance);
|
||||
}
|
||||
}
|
||||
|
||||
#if RecordTrackingOverhead
|
||||
public void LogUnreleasedResources(int stackFrameCount)
|
||||
{
|
||||
Console.WriteLine(
|
||||
$"Overhead: {_totalTimeSpentTracking / (float)Stopwatch.Frequency / _totalResourcesTracked * 1000000}us"
|
||||
);
|
||||
|
||||
if (_trackedInstances.IsEmpty)
|
||||
return;
|
||||
foreach (var instance in _trackedInstances.Keys)
|
||||
Console.WriteLine(
|
||||
$"Allocated here: {(instance.StackTrace is not null ? StackTraceToString(instance.StackTrace, stackFrameCount) : "no trace")}"
|
||||
);
|
||||
}
|
||||
#endif
|
||||
|
||||
private ulong _totalResourcesTracked = 0;
|
||||
private readonly ConcurrentDictionary<TrackedResource, byte> _trackedInstances;
|
||||
|
||||
#if RecordTrackingOverhead
|
||||
private readonly Stopwatch _stopwatch;
|
||||
public long _totalTimeSpentTracking = 0;
|
||||
#endif
|
||||
}
|
||||
72
Brutal.Texture/Resources/StackTraceFormatting.cs
Normal file
72
Brutal.Texture/Resources/StackTraceFormatting.cs
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
using System.Diagnostics;
|
||||
|
||||
namespace Brutal.TextureApi
|
||||
{
|
||||
internal static class StackTraceFormatting
|
||||
{
|
||||
/// <summary>
|
||||
/// Takes a StackTrace and generates a string of the last X number of relevant stack frames.
|
||||
/// <para />
|
||||
/// The output starts at the first non-Brutal namespace stack frame.
|
||||
/// System functions (Those in System or Microsoft namespaces) are removed.
|
||||
/// </summary>
|
||||
/// <param name="stackTrace">The StackTrace to format</param>
|
||||
/// <param name="stackFrameCount">The number of relevant StackFrames to return in the string.</param>
|
||||
/// <returns></returns>
|
||||
public static IList<string> FriendlyStackTraceStrings(StackTrace? stackTrace, int stackFrameCount)
|
||||
{
|
||||
if (stackTrace is null)
|
||||
return ["No stack trace"];
|
||||
|
||||
var stackIndent = "";
|
||||
var stackTraceEntries = new List<string>();
|
||||
var startStackFrameIndex = GetFirstClientStackFrameIndex(stackTrace);
|
||||
|
||||
for (var i = startStackFrameIndex; i < stackTrace.FrameCount; i++)
|
||||
{
|
||||
var stackFrame = stackTrace.GetFrame(i)!;
|
||||
if (IsIgnoredStackFrame(stackFrame))
|
||||
continue;
|
||||
|
||||
stackTraceEntries.Add(BuildStackFrameString(stackIndent, stackFrame));
|
||||
if (--stackFrameCount == 0)
|
||||
break;
|
||||
|
||||
stackIndent += " ";
|
||||
}
|
||||
return stackTraceEntries;
|
||||
}
|
||||
|
||||
#region Private
|
||||
|
||||
private static string BuildStackFrameString(string stackIndent, StackFrame stackFrame)
|
||||
{
|
||||
return $"{stackIndent}{stackFrame.GetMethod()!.Name} : {stackFrame.GetFileName()} : {stackFrame.GetFileLineNumber()}";
|
||||
}
|
||||
|
||||
private static int GetFirstClientStackFrameIndex(StackTrace stackTrace)
|
||||
{
|
||||
for (var i = 0; i < stackTrace.FrameCount; i++)
|
||||
{
|
||||
var stackFrameNamespace = stackTrace.GetFrame(i)?.GetMethod()?.DeclaringType?.Namespace;
|
||||
if (stackFrameNamespace is null)
|
||||
continue;
|
||||
|
||||
if (!stackFrameNamespace.StartsWith("Brutal")) // This is a temporary lame filter
|
||||
return i;
|
||||
}
|
||||
return stackTrace.FrameCount;
|
||||
}
|
||||
|
||||
private static bool IsIgnoredStackFrame(StackFrame stackFrame)
|
||||
{
|
||||
if (!stackFrame.HasSource() || !stackFrame.HasMethod())
|
||||
return true;
|
||||
|
||||
var name = stackFrame.GetMethod()?.DeclaringType?.Assembly.FullName;
|
||||
return name is null || name.StartsWith("Microsoft") || name.StartsWith("System");
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
40
Brutal.Texture/ScopedTexture.cs
Normal file
40
Brutal.Texture/ScopedTexture.cs
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
namespace Brutal.TextureApi
|
||||
{
|
||||
public static partial class ITextureExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// A wrapper class for an ITexture that can be used where IDisposables can; e.g. in
|
||||
/// a using statement.
|
||||
/// When Dispose() is called on an instance of ScopedTexture the ITexture
|
||||
/// is unloaded through the <see cref="TextureLoader" /> static class.
|
||||
/// </summary>
|
||||
public ref struct ScopedTexture : IDisposable
|
||||
{
|
||||
public ScopedTexture(ITexture texture)
|
||||
{
|
||||
Texture = texture;
|
||||
}
|
||||
|
||||
public ITexture? Texture { get; private set; }
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (Texture is null)
|
||||
return;
|
||||
|
||||
TextureLoader.Unload(Texture);
|
||||
Texture = null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Wraps an ITexture in an IDisposable
|
||||
/// </summary>
|
||||
/// <param name="texture">The ITexture to wrap</param>
|
||||
/// <returns>The ScopedTexture wrapper</returns>
|
||||
public static ScopedTexture Using(this ITexture texture)
|
||||
{
|
||||
return new ScopedTexture(texture);
|
||||
}
|
||||
}
|
||||
}
|
||||
30
Brutal.Texture/Stb/StbDataTypeExtensions.cs
Normal file
30
Brutal.Texture/Stb/StbDataTypeExtensions.cs
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
using Brutal.TextureApi;
|
||||
|
||||
namespace Brutal.TextureApi.Stb;
|
||||
|
||||
public static class StbDataTypeExtensions
|
||||
{
|
||||
public static StbApi.Stb.DataType ToStbDataType(this TextureFormat format)
|
||||
{
|
||||
switch (format)
|
||||
{
|
||||
case TextureFormat.R8_UNorm:
|
||||
case TextureFormat.R8G8_UNorm:
|
||||
case TextureFormat.R8G8B8_UNorm:
|
||||
case TextureFormat.R8G8B8A8_UNorm:
|
||||
return StbApi.Stb.DataType._8Bit;
|
||||
case TextureFormat.R16_UNorm:
|
||||
case TextureFormat.R16G16_UNorm:
|
||||
case TextureFormat.R16G16B16_UNorm:
|
||||
case TextureFormat.R16G16B16A16_UNorm:
|
||||
return StbApi.Stb.DataType._16Bit;
|
||||
case TextureFormat.R32_SFloat:
|
||||
case TextureFormat.R32G32_SFloat:
|
||||
case TextureFormat.R32G32B32_SFloat:
|
||||
case TextureFormat.R32G32B32A32_SFloat:
|
||||
return StbApi.Stb.DataType.Float;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException(nameof(format), format, "TextureFormat not supported by Stb");
|
||||
}
|
||||
}
|
||||
}
|
||||
73
Brutal.Texture/Stb/StbTexture.cs
Normal file
73
Brutal.Texture/Stb/StbTexture.cs
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
using System.Diagnostics;
|
||||
using Brutal.Numerics;
|
||||
using Brutal.Pointers.Extensions;
|
||||
|
||||
namespace Brutal.TextureApi.Stb;
|
||||
|
||||
public abstract partial class StbTexture : ITexture
|
||||
{
|
||||
internal struct CreateInfo
|
||||
{
|
||||
internal required int2 Extent;
|
||||
internal required int Channels;
|
||||
internal required int BytesPerChannel;
|
||||
}
|
||||
|
||||
#region ITexture
|
||||
public unsafe Span<byte> Data => new(GetDataPointer(), ElementCount.Of(_dataSize));
|
||||
public TexelCount3 Extent => new(NativeInfo.Extent.X, NativeInfo.Extent.Y, 1);
|
||||
public abstract TextureFormat Format { get; }
|
||||
public int LayerCount => 1;
|
||||
public int LevelCount => 1;
|
||||
public int FaceCount => 1;
|
||||
public bool IsCubemap => false;
|
||||
public bool IsArray => false;
|
||||
public int DimensionCount => 2;
|
||||
|
||||
public ByteSize RowPitch(int level)
|
||||
{
|
||||
if (level < 0 || level >= LevelCount)
|
||||
throw new ArgumentOutOfRangeException(nameof(level), level, $"Level must be in range [0, {LevelCount})");
|
||||
return _rowPitch;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the bytes that the image takes up.
|
||||
/// </summary>
|
||||
/// <param name="level">Must be 0</param>
|
||||
/// <param name="layer">Must be 0</param>
|
||||
/// <param name="face">Must be 0</param>
|
||||
/// <returns></returns>
|
||||
public unsafe Span<byte> ImageData(int level, int layer, int face)
|
||||
{
|
||||
if (level != 0 || layer != 0 || face != 0)
|
||||
throw new TextureException($"level, layer and face must all be 0 for STB textures");
|
||||
return new Span<byte>(Data.AsPointer(), Data.Length);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
public int Channels => NativeInfo.Channels;
|
||||
|
||||
public void Destroy()
|
||||
{
|
||||
FreeNativeResources();
|
||||
}
|
||||
|
||||
internal readonly CreateInfo NativeInfo;
|
||||
protected abstract unsafe byte* GetDataPointer();
|
||||
private readonly ByteSize _dataSize;
|
||||
private readonly ByteSize _rowPitch;
|
||||
|
||||
internal StbTexture(CreateInfo info)
|
||||
{
|
||||
Debug.Assert(FaceCount == 1 && LevelCount == 1 && LayerCount == 1);
|
||||
NativeInfo = info;
|
||||
_dataSize = ByteSize.Of(
|
||||
NativeInfo.Extent.X * NativeInfo.Extent.Y * NativeInfo.Channels * NativeInfo.BytesPerChannel
|
||||
);
|
||||
_rowPitch = ByteSize.Of(NativeInfo.Extent.X * NativeInfo.Channels * NativeInfo.BytesPerChannel);
|
||||
}
|
||||
|
||||
protected abstract void FreeNativeResources();
|
||||
}
|
||||
52
Brutal.Texture/Stb/StbTexture16.cs
Normal file
52
Brutal.Texture/Stb/StbTexture16.cs
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
using Brutal.Numerics;
|
||||
using Brutal.StbApi;
|
||||
using Brutal.TextureApi;
|
||||
|
||||
namespace Brutal.TextureApi.Stb;
|
||||
|
||||
public class StbTexture16 : StbTexture
|
||||
{
|
||||
internal StbTexture16(StbImage16 image)
|
||||
: base(
|
||||
new CreateInfo
|
||||
{
|
||||
Extent = new int2(image.Width, image.Height),
|
||||
Channels = image.Channels,
|
||||
BytesPerChannel = 2,
|
||||
}
|
||||
)
|
||||
{
|
||||
_image = image;
|
||||
}
|
||||
|
||||
public override TextureFormat Format
|
||||
{
|
||||
get
|
||||
{
|
||||
return _image.Channels switch
|
||||
{
|
||||
1 => TextureFormat.R16_UNorm,
|
||||
2 => TextureFormat.R16G16_UNorm,
|
||||
3 => TextureFormat.R16G16B16_UNorm,
|
||||
4 => TextureFormat.R16G16B16A16_UNorm,
|
||||
_ => throw new ArgumentOutOfRangeException(),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
protected override unsafe byte* GetDataPointer()
|
||||
{
|
||||
return (byte*)_image.Data;
|
||||
}
|
||||
|
||||
protected override void FreeNativeResources()
|
||||
{
|
||||
if (_image.IsNotNull())
|
||||
{
|
||||
_image.Dispose();
|
||||
_image = default;
|
||||
}
|
||||
}
|
||||
|
||||
private StbImage16 _image;
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue