SOURCE DUMP v1

This commit is contained in:
Rian Drake 2026-10-15 14:09:11 +13:00
commit fb20299e9d
44 changed files with 768360 additions and 0 deletions

View file

@ -0,0 +1,709 @@
namespace Brutal.TextureApi.Abstractions;
/// <summary>
/// Static class for utility functions relating to a TextureFormat.
/// </summary>
public static class TextureFormatExtensions
{
/// <param name="format">Format to get the <see cref="FormatDescriptor"/> for</param>
extension(TextureFormat format)
{
/// <summary>
/// Returns a descriptor detailing information about a TextureFormat.
/// </summary>
/// <returns>The Descriptor</returns>
/// <exception cref="NotImplementedException">If the properties of the format could not be determined</exception>
public FormatDescriptor Descriptor()
{
var minBlocksX = 1u;
var minBlocksY = 1u;
var flags = FormatDescriptor.DescriptorFlags.None;
var blockSizeInBits = 0u;
var blockWidth = 0u;
var blockHeight = 0u;
var blockDepth = 0u;
switch (format)
{
case TextureFormat.R4G4_UNorm_Pack8:
flags = FormatDescriptor.DescriptorFlags.PackedBit;
blockSizeInBits = 1 * 8;
blockWidth = 1;
blockHeight = 1;
blockDepth = 1;
break;
case TextureFormat.A8_UNormKHR:
flags = 0;
blockSizeInBits = 1 * 8;
blockWidth = 1;
blockHeight = 1;
blockDepth = 1;
break;
case TextureFormat.R4G4B4A4_UNorm_Pack16:
case TextureFormat.B4G4R4A4_UNorm_Pack16:
case TextureFormat.R5G6B5_UNorm_Pack16:
case TextureFormat.B5G6R5_UNorm_Pack16:
case TextureFormat.R5G5B5A1_UNorm_Pack16:
case TextureFormat.B5G5R5A1_UNorm_Pack16:
case TextureFormat.A1R5G5B5_UNorm_Pack16:
case TextureFormat.A1B5G5R5_UNorm_Pack16KHR:
flags = FormatDescriptor.DescriptorFlags.PackedBit;
blockSizeInBits = 2 * 8;
blockWidth = 1;
blockHeight = 1;
blockDepth = 1;
break;
case TextureFormat.R8_UNorm:
case TextureFormat.R8_SNorm:
case TextureFormat.R8_UScaled:
case TextureFormat.R8_SScaled:
case TextureFormat.R8_UInt:
case TextureFormat.R8_SInt:
case TextureFormat.R8_SRGB:
flags = 0;
blockSizeInBits = 1 * 8;
blockWidth = 1;
blockHeight = 1;
blockDepth = 1;
break;
case TextureFormat.R8G8_UNorm:
case TextureFormat.R8G8_SNorm:
case TextureFormat.R8G8_UScaled:
case TextureFormat.R8G8_SScaled:
case TextureFormat.R8G8_UInt:
case TextureFormat.R8G8_SInt:
case TextureFormat.R8G8_SRGB:
flags = 0;
blockSizeInBits = 2 * 8;
blockWidth = 1;
blockHeight = 1;
blockDepth = 1;
break;
case TextureFormat.R8G8B8_UNorm:
case TextureFormat.R8G8B8_SNorm:
case TextureFormat.R8G8B8_UScaled:
case TextureFormat.R8G8B8_SScaled:
case TextureFormat.R8G8B8_UInt:
case TextureFormat.R8G8B8_SInt:
case TextureFormat.R8G8B8_SRGB:
case TextureFormat.B8G8R8_UNorm:
case TextureFormat.B8G8R8_SNorm:
case TextureFormat.B8G8R8_UScaled:
case TextureFormat.B8G8R8_SScaled:
case TextureFormat.B8G8R8_UInt:
case TextureFormat.B8G8R8_SInt:
case TextureFormat.B8G8R8_SRGB:
flags = 0;
blockSizeInBits = 3 * 8;
blockWidth = 1;
blockHeight = 1;
blockDepth = 1;
break;
case TextureFormat.R8G8B8A8_UNorm:
case TextureFormat.R8G8B8A8_SNorm:
case TextureFormat.R8G8B8A8_UScaled:
case TextureFormat.R8G8B8A8_SScaled:
case TextureFormat.R8G8B8A8_UInt:
case TextureFormat.R8G8B8A8_SInt:
case TextureFormat.R8G8B8A8_SRGB:
case TextureFormat.B8G8R8A8_UNorm:
case TextureFormat.B8G8R8A8_SNorm:
case TextureFormat.B8G8R8A8_UScaled:
case TextureFormat.B8G8R8A8_SScaled:
case TextureFormat.B8G8R8A8_UInt:
case TextureFormat.B8G8R8A8_SInt:
case TextureFormat.B8G8R8A8_SRGB:
flags = 0;
blockSizeInBits = 4 * 8;
blockWidth = 1;
blockHeight = 1;
blockDepth = 1;
break;
case TextureFormat.A8B8G8R8_UNorm_Pack32:
case TextureFormat.A8B8G8R8_SNorm_Pack32:
case TextureFormat.A8B8G8R8_UScaled_Pack32:
case TextureFormat.A8B8G8R8_SScaled_Pack32:
case TextureFormat.A8B8G8R8_UInt_Pack32:
case TextureFormat.A8B8G8R8_SInt_Pack32:
case TextureFormat.A8B8G8R8_SRGB_Pack32:
flags = FormatDescriptor.DescriptorFlags.PackedBit;
blockSizeInBits = 4 * 8;
blockWidth = 1;
blockHeight = 1;
blockDepth = 1;
break;
case TextureFormat.A2R10G10B10_UNorm_Pack32:
case TextureFormat.A2R10G10B10_SNorm_Pack32:
case TextureFormat.A2R10G10B10_UScaled_Pack32:
case TextureFormat.A2R10G10B10_SScaled_Pack32:
case TextureFormat.A2R10G10B10_UInt_Pack32:
case TextureFormat.A2R10G10B10_SInt_Pack32:
case TextureFormat.A2B10G10R10_UNorm_Pack32:
case TextureFormat.A2B10G10R10_SNorm_Pack32:
case TextureFormat.A2B10G10R10_UScaled_Pack32:
case TextureFormat.A2B10G10R10_SScaled_Pack32:
case TextureFormat.A2B10G10R10_UInt_Pack32:
case TextureFormat.A2B10G10R10_SInt_Pack32:
flags = FormatDescriptor.DescriptorFlags.PackedBit;
blockSizeInBits = 4 * 8;
blockWidth = 1;
blockHeight = 1;
blockDepth = 1;
break;
case TextureFormat.R16_UNorm:
case TextureFormat.R16_SNorm:
case TextureFormat.R16_UScaled:
case TextureFormat.R16_SScaled:
case TextureFormat.R16_UInt:
case TextureFormat.R16_SInt:
case TextureFormat.R16_SFloat:
flags = 0;
blockSizeInBits = 2 * 8;
blockWidth = 1;
blockHeight = 1;
blockDepth = 1;
break;
case TextureFormat.R16G16_UNorm:
case TextureFormat.R16G16_SNorm:
case TextureFormat.R16G16_UScaled:
case TextureFormat.R16G16_SScaled:
case TextureFormat.R16G16_UInt:
case TextureFormat.R16G16_SInt:
case TextureFormat.R16G16_SFloat:
case TextureFormat.R16G16S105NV: // Actualy is VK_FORMAT_R16G16_SFIXED5_NV
flags = 0;
blockSizeInBits = 4 * 8;
blockWidth = 1;
blockHeight = 1;
blockDepth = 1;
break;
case TextureFormat.R16G16B16_UNorm:
case TextureFormat.R16G16B16_SNorm:
case TextureFormat.R16G16B16_UScaled:
case TextureFormat.R16G16B16_SScaled:
case TextureFormat.R16G16B16_UInt:
case TextureFormat.R16G16B16_SInt:
case TextureFormat.R16G16B16_SFloat:
flags = 0;
blockSizeInBits = 6 * 8;
blockWidth = 1;
blockHeight = 1;
blockDepth = 1;
break;
case TextureFormat.R16G16B16A16_UNorm:
case TextureFormat.R16G16B16A16_SNorm:
case TextureFormat.R16G16B16A16_UScaled:
case TextureFormat.R16G16B16A16_SScaled:
case TextureFormat.R16G16B16A16_UInt:
case TextureFormat.R16G16B16A16_SInt:
case TextureFormat.R16G16B16A16_SFloat:
flags = 0;
blockSizeInBits = 8 * 8;
blockWidth = 1;
blockHeight = 1;
blockDepth = 1;
break;
case TextureFormat.R32_UInt:
case TextureFormat.R32_SInt:
case TextureFormat.R32_SFloat:
flags = 0;
blockSizeInBits = 4 * 8;
blockWidth = 1;
blockHeight = 1;
blockDepth = 1;
break;
case TextureFormat.R32G32_UInt:
case TextureFormat.R32G32_SInt:
case TextureFormat.R32G32_SFloat:
flags = 0;
blockSizeInBits = 8 * 8;
blockWidth = 1;
blockHeight = 1;
blockDepth = 1;
break;
case TextureFormat.R32G32B32_UInt:
case TextureFormat.R32G32B32_SInt:
case TextureFormat.R32G32B32_SFloat:
flags = 0;
blockSizeInBits = 12 * 8;
blockWidth = 1;
blockHeight = 1;
blockDepth = 1;
break;
case TextureFormat.R32G32B32A32_UInt:
case TextureFormat.R32G32B32A32_SInt:
case TextureFormat.R32G32B32A32_SFloat:
flags = 0;
blockSizeInBits = 16 * 8;
blockWidth = 1;
blockHeight = 1;
blockDepth = 1;
break;
case TextureFormat.R64G64_UInt:
case TextureFormat.R64G64_SInt:
case TextureFormat.R64G64_SFloat:
flags = 0;
blockSizeInBits = 16 * 8;
blockWidth = 1;
blockHeight = 1;
blockDepth = 1;
break;
case TextureFormat.R64_UInt:
case TextureFormat.R64_SInt:
case TextureFormat.R64_SFloat:
flags = 0;
blockSizeInBits = 8 * 8;
blockWidth = 1;
blockHeight = 1;
blockDepth = 1;
break;
case TextureFormat.R64G64B64_UInt:
case TextureFormat.R64G64B64_SInt:
case TextureFormat.R64G64B64_SFloat:
flags = 0;
blockSizeInBits = 24 * 8;
blockWidth = 1;
blockHeight = 1;
blockDepth = 1;
break;
case TextureFormat.R64G64B64A64_UInt:
case TextureFormat.R64G64B64A64_SInt:
case TextureFormat.R64G64B64A64_SFloat:
flags = 0;
blockSizeInBits = 32 * 8;
blockWidth = 1;
blockHeight = 1;
blockDepth = 1;
break;
case TextureFormat.B10G11R11_UFloat_Pack32:
case TextureFormat.E5B9G9R9_UFloat_Pack32:
flags = FormatDescriptor.DescriptorFlags.PackedBit;
blockSizeInBits = 4 * 8;
blockWidth = 1;
blockHeight = 1;
blockDepth = 1;
break;
case TextureFormat.D16_UNorm:
flags = FormatDescriptor.DescriptorFlags.DepthBit;
blockSizeInBits = 2 * 8;
blockWidth = 1;
blockHeight = 1;
blockDepth = 1;
break;
case TextureFormat.X8D24_UNorm_Pack32:
flags = FormatDescriptor.DescriptorFlags.PackedBit | FormatDescriptor.DescriptorFlags.DepthBit;
blockSizeInBits = 4 * 8;
blockWidth = 1;
blockHeight = 1;
blockDepth = 1;
break;
case TextureFormat.D32_SFloat:
flags = FormatDescriptor.DescriptorFlags.DepthBit;
blockSizeInBits = 4 * 8;
blockWidth = 1;
blockHeight = 1;
blockDepth = 1;
break;
case TextureFormat.S8_UInt:
flags = FormatDescriptor.DescriptorFlags.StencilBit;
blockSizeInBits = 1 * 8;
blockWidth = 1;
blockHeight = 1;
blockDepth = 1;
break;
case TextureFormat.D16_UNormS8_UInt:
flags = FormatDescriptor.DescriptorFlags.DepthBit | FormatDescriptor.DescriptorFlags.StencilBit;
blockSizeInBits = 3 * 8;
blockWidth = 1;
blockHeight = 1;
blockDepth = 1;
break;
case TextureFormat.D24_UNormS8_UInt:
flags = FormatDescriptor.DescriptorFlags.DepthBit | FormatDescriptor.DescriptorFlags.StencilBit;
blockSizeInBits = 4 * 8;
blockWidth = 1;
blockHeight = 1;
blockDepth = 1;
break;
case TextureFormat.D32_SFloatS8_UInt:
flags = FormatDescriptor.DescriptorFlags.DepthBit | FormatDescriptor.DescriptorFlags.StencilBit;
blockSizeInBits = 8 * 8;
blockWidth = 1;
blockHeight = 1;
blockDepth = 1;
break;
case TextureFormat.BC1Rgb_UNorm_Block:
case TextureFormat.BC1Rgb_SRGB_Block:
case TextureFormat.BC1Rgba_UNorm_Block:
case TextureFormat.BC1Rgba_SRGB_Block:
case TextureFormat.BC4_UNorm_Block:
case TextureFormat.BC4_SNorm_Block:
flags = FormatDescriptor.DescriptorFlags.CompressedBit;
blockSizeInBits = 8 * 8;
blockWidth = 4;
blockHeight = 4;
blockDepth = 1;
break;
case TextureFormat.BC2_UNorm_Block:
case TextureFormat.BC2_SRGB_Block:
case TextureFormat.BC3_UNorm_Block:
case TextureFormat.BC3_SRGB_Block:
case TextureFormat.BC5_UNorm_Block:
case TextureFormat.BC5_SNorm_Block:
case TextureFormat.BC6HUFloat_Block:
case TextureFormat.BC6H_SFloat_Block:
case TextureFormat.BC7_UNorm_Block:
case TextureFormat.BC7_SRGB_Block:
flags = FormatDescriptor.DescriptorFlags.CompressedBit;
blockSizeInBits = 16 * 8;
blockWidth = 4;
blockHeight = 4;
blockDepth = 1;
break;
case TextureFormat.ETC2R8G8B8_UNorm_Block:
case TextureFormat.ETC2R8G8B8_SRGB_Block:
case TextureFormat.ETC2R8G8B8A1_UNorm_Block:
case TextureFormat.ETC2R8G8B8A1_SRGB_Block:
flags = FormatDescriptor.DescriptorFlags.CompressedBit;
blockSizeInBits = 8 * 8;
blockWidth = 4;
blockHeight = 4;
blockDepth = 1;
break;
case TextureFormat.EACR11_UNorm_Block:
case TextureFormat.EACR11_SNorm_Block:
flags = FormatDescriptor.DescriptorFlags.CompressedBit;
blockSizeInBits = 8 * 8;
blockWidth = 4;
blockHeight = 4;
blockDepth = 1;
break;
case TextureFormat.ETC2R8G8B8A8_UNorm_Block:
case TextureFormat.ETC2R8G8B8A8_SRGB_Block:
case TextureFormat.EACR11G11_UNorm_Block:
case TextureFormat.EACR11G11_SNorm_Block:
flags = FormatDescriptor.DescriptorFlags.CompressedBit;
blockSizeInBits = 16 * 8;
blockWidth = 4;
blockHeight = 4;
blockDepth = 1;
break;
case TextureFormat.Pvrtc12Bpp_SRGB_BlockIMG:
case TextureFormat.Pvrtc12Bpp_UNorm_BlockIMG:
flags = FormatDescriptor.DescriptorFlags.CompressedBit;
blockSizeInBits = 8 * 8;
blockWidth = 8;
blockHeight = 4;
blockDepth = 1;
minBlocksX = 2;
minBlocksY = 2;
break;
case TextureFormat.Pvrtc22Bpp_SRGB_BlockIMG:
case TextureFormat.Pvrtc22Bpp_UNorm_BlockIMG:
flags = FormatDescriptor.DescriptorFlags.CompressedBit;
blockSizeInBits = 8 * 8;
blockWidth = 8;
blockHeight = 4;
blockDepth = 1;
break;
case TextureFormat.Pvrtc14Bpp_SRGB_BlockIMG:
case TextureFormat.Pvrtc14Bpp_UNorm_BlockIMG:
flags = FormatDescriptor.DescriptorFlags.CompressedBit;
blockSizeInBits = 8 * 8;
blockWidth = 4;
blockHeight = 4;
blockDepth = 1;
minBlocksX = 2;
minBlocksY = 2;
break;
case TextureFormat.Pvrtc24Bpp_SRGB_BlockIMG:
case TextureFormat.Pvrtc24Bpp_UNorm_BlockIMG:
flags = FormatDescriptor.DescriptorFlags.CompressedBit;
blockSizeInBits = 8 * 8;
blockWidth = 4;
blockHeight = 4;
blockDepth = 1;
break;
case TextureFormat.ASTC4X4_SFloat_Block:
case TextureFormat.ASTC4X4_UNorm_Block:
case TextureFormat.ASTC4X4_SRGB_Block:
flags = FormatDescriptor.DescriptorFlags.CompressedBit;
blockSizeInBits = 16 * 8;
blockWidth = 4;
blockHeight = 4;
blockDepth = 1;
break;
case TextureFormat.ASTC5X4_SFloat_Block:
case TextureFormat.ASTC5X4_UNorm_Block:
case TextureFormat.ASTC5X4_SRGB_Block:
flags = FormatDescriptor.DescriptorFlags.CompressedBit;
blockSizeInBits = 16 * 8;
blockWidth = 5;
blockHeight = 4;
blockDepth = 1;
break;
case TextureFormat.ASTC5X5_SFloat_Block:
case TextureFormat.ASTC5X5_UNorm_Block:
case TextureFormat.ASTC5X5_SRGB_Block:
flags = FormatDescriptor.DescriptorFlags.CompressedBit;
blockSizeInBits = 16 * 8;
blockWidth = 5;
blockHeight = 5;
blockDepth = 1;
break;
case TextureFormat.ASTC6X5_SFloat_Block:
case TextureFormat.ASTC6X5_UNorm_Block:
case TextureFormat.ASTC6X5_SRGB_Block:
flags = FormatDescriptor.DescriptorFlags.CompressedBit;
blockSizeInBits = 16 * 8;
blockWidth = 6;
blockHeight = 5;
blockDepth = 1;
break;
case TextureFormat.ASTC6X6_SFloat_Block:
case TextureFormat.ASTC6X6_UNorm_Block:
case TextureFormat.ASTC6X6_SRGB_Block:
flags = FormatDescriptor.DescriptorFlags.CompressedBit;
blockSizeInBits = 16 * 8;
blockWidth = 6;
blockHeight = 6;
blockDepth = 1;
break;
case TextureFormat.ASTC8X5_SFloat_Block:
case TextureFormat.ASTC8X5_UNorm_Block:
case TextureFormat.ASTC8X5_SRGB_Block:
flags = FormatDescriptor.DescriptorFlags.CompressedBit;
blockSizeInBits = 16 * 8;
blockWidth = 8;
blockHeight = 5;
blockDepth = 1;
break;
case TextureFormat.ASTC8X6_SFloat_Block:
case TextureFormat.ASTC8X6_UNorm_Block:
case TextureFormat.ASTC8X6_SRGB_Block:
flags = FormatDescriptor.DescriptorFlags.CompressedBit;
blockSizeInBits = 16 * 8;
blockWidth = 8;
blockHeight = 6;
blockDepth = 1;
break;
case TextureFormat.ASTC8X8_SFloat_Block:
case TextureFormat.ASTC8X8_UNorm_Block:
case TextureFormat.ASTC8X8_SRGB_Block:
flags = FormatDescriptor.DescriptorFlags.CompressedBit;
blockSizeInBits = 16 * 8;
blockWidth = 8;
blockHeight = 8;
blockDepth = 1;
break;
case TextureFormat.ASTC10X5_SFloat_Block:
case TextureFormat.ASTC10X5_UNorm_Block:
case TextureFormat.ASTC10X5_SRGB_Block:
flags = FormatDescriptor.DescriptorFlags.CompressedBit;
blockSizeInBits = 16 * 8;
blockWidth = 10;
blockHeight = 5;
blockDepth = 1;
break;
case TextureFormat.ASTC10X6_SFloat_Block:
case TextureFormat.ASTC10X6_UNorm_Block:
case TextureFormat.ASTC10X6_SRGB_Block:
flags = FormatDescriptor.DescriptorFlags.CompressedBit;
blockSizeInBits = 16 * 8;
blockWidth = 10;
blockHeight = 6;
blockDepth = 1;
break;
case TextureFormat.ASTC10X8_SFloat_Block:
case TextureFormat.ASTC10X8_UNorm_Block:
case TextureFormat.ASTC10X8_SRGB_Block:
flags = FormatDescriptor.DescriptorFlags.CompressedBit;
blockSizeInBits = 16 * 8;
blockWidth = 10;
blockHeight = 8;
blockDepth = 1;
break;
case TextureFormat.ASTC10X10_SFloat_Block:
case TextureFormat.ASTC10X10_UNorm_Block:
case TextureFormat.ASTC10X10_SRGB_Block:
flags = FormatDescriptor.DescriptorFlags.CompressedBit;
blockSizeInBits = 16 * 8;
blockWidth = 10;
blockHeight = 10;
blockDepth = 1;
break;
case TextureFormat.ASTC12X10_SFloat_Block:
case TextureFormat.ASTC12X10_UNorm_Block:
case TextureFormat.ASTC12X10_SRGB_Block:
flags = FormatDescriptor.DescriptorFlags.CompressedBit;
blockSizeInBits = 16 * 8;
blockWidth = 12;
blockHeight = 10;
blockDepth = 1;
break;
case TextureFormat.ASTC12X12_SFloat_Block:
case TextureFormat.ASTC12X12_UNorm_Block:
case TextureFormat.ASTC12X12_SRGB_Block:
flags = FormatDescriptor.DescriptorFlags.CompressedBit;
blockSizeInBits = 16 * 8;
blockWidth = 12;
blockHeight = 12;
blockDepth = 1;
break;
case TextureFormat.B8G8R8G8422_UNorm:
case TextureFormat.G8B8G8R8422_UNorm:
flags = 0;
blockSizeInBits = 4 * 8;
blockWidth = 2;
blockHeight = 1;
blockDepth = 1;
break;
// Plane and Scaled formats are prohibited by libktx.
case TextureFormat.G8B8R82_Plane420_UNorm:
case TextureFormat.G8B8R83_Plane420_UNorm:
flags = FormatDescriptor.DescriptorFlags.PlanarBit;
blockSizeInBits = 3 * 8;
blockWidth = 1;
blockHeight = 1;
blockDepth = 1;
break;
case TextureFormat.G8B8R83_Plane422_UNorm:
case TextureFormat.G8B8R82_Plane422_UNorm:
flags = FormatDescriptor.DescriptorFlags.PlanarBit;
blockSizeInBits = 3 * 8;
blockWidth = 1;
blockHeight = 1;
blockDepth = 1;
break;
case TextureFormat.G8B8R83_Plane444_UNorm:
case TextureFormat.G8B8R82_Plane444_UNorm:
flags = FormatDescriptor.DescriptorFlags.PlanarBit;
blockSizeInBits = 3 * 8;
blockWidth = 1;
blockHeight = 1;
blockDepth = 1;
break;
case TextureFormat.R10X6G10X6B10X6A10X6_UNorm4_Pack16:
//case TextureFormat.R10X6G10X6B10X6A10X6_UInt4_Pack16_Arm:
flags = FormatDescriptor.DescriptorFlags.PackedBit;
blockSizeInBits = 8 * 8;
blockWidth = 1;
blockHeight = 1;
blockDepth = 1;
break;
case TextureFormat.G10X6B10X6G10X6R10X6422_UNorm4_Pack16:
case TextureFormat.B10X6G10X6R10X6G10X6422_UNorm4_Pack16:
flags = FormatDescriptor.DescriptorFlags.PackedBit;
blockSizeInBits = 8 * 8;
blockWidth = 2;
blockHeight = 1;
blockDepth = 1;
break;
case TextureFormat.G10X6B10X6R10X62_Plane420_UNorm3_Pack16:
case TextureFormat.G10X6B10X6R10X62_Plane422_UNorm3_Pack16:
case TextureFormat.G10X6B10X6R10X62_Plane444_UNorm3_Pack16:
case TextureFormat.G10X6B10X6R10X63_Plane420_UNorm3_Pack16:
case TextureFormat.G10X6B10X6R10X63_Plane422_UNorm3_Pack16:
case TextureFormat.G10X6B10X6R10X63_Plane444_UNorm3_Pack16:
flags = FormatDescriptor.DescriptorFlags.PackedBit | FormatDescriptor.DescriptorFlags.PlanarBit;
blockSizeInBits = 6 * 8;
blockWidth = 1;
blockHeight = 1;
blockDepth = 1;
break;
case TextureFormat.R12X4G12X4B12X4A12X4_UNorm4_Pack16:
//case TextureFormat.R12X4G12X4B12X4A12X4_UInt4_Pack16_Arm:
flags = FormatDescriptor.DescriptorFlags.PackedBit;
blockSizeInBits = 8 * 8;
blockWidth = 1;
blockHeight = 1;
blockDepth = 1;
break;
case TextureFormat.G12X4B12X4G12X4R12X4422_UNorm4_Pack16:
case TextureFormat.B12X4G12X4R12X4G12X4422_UNorm4_Pack16:
flags = FormatDescriptor.DescriptorFlags.PackedBit;
blockSizeInBits = 8 * 8;
blockWidth = 2;
blockHeight = 1;
blockDepth = 1;
break;
case TextureFormat.G12X4B12X4R12X42_Plane420_UNorm3_Pack16:
case TextureFormat.G12X4B12X4R12X42_Plane422_UNorm3_Pack16:
case TextureFormat.G12X4B12X4R12X42_Plane444_UNorm3_Pack16:
case TextureFormat.G12X4B12X4R12X43_Plane420_UNorm3_Pack16:
case TextureFormat.G12X4B12X4R12X43_Plane422_UNorm3_Pack16:
case TextureFormat.G12X4B12X4R12X43_Plane444_UNorm3_Pack16:
flags = FormatDescriptor.DescriptorFlags.PackedBit | FormatDescriptor.DescriptorFlags.PlanarBit;
blockSizeInBits = 6 * 8;
blockWidth = 1;
blockHeight = 1;
blockDepth = 1;
break;
case TextureFormat.G16B16G16R16422_UNorm:
case TextureFormat.B16G16R16G16422_UNorm:
flags = 0;
blockSizeInBits = 8 * 8;
blockWidth = 2;
blockHeight = 1;
blockDepth = 1;
break;
case TextureFormat.G16B16R163_Plane420_UNorm:
case TextureFormat.G16B16R162_Plane420_UNorm:
case TextureFormat.G16B16R163_Plane422_UNorm:
case TextureFormat.G16B16R162_Plane422_UNorm:
case TextureFormat.G16B16R162_Plane444_UNorm:
flags = FormatDescriptor.DescriptorFlags.PlanarBit;
blockSizeInBits = 6 * 8;
blockWidth = 1;
blockHeight = 1;
blockDepth = 1;
break;
case TextureFormat.R10X6_UNorm_Pack16:
case TextureFormat.R12X4_UNorm_Pack16:
case TextureFormat.A4R4G4B4_UNorm_Pack16:
case TextureFormat.A4B4G4R4_UNorm_Pack16:
flags = FormatDescriptor.DescriptorFlags.PackedBit;
blockSizeInBits = 2 * 8;
blockWidth = 1;
blockHeight = 1;
blockDepth = 1;
break;
case TextureFormat.R10X6G10X6_UNorm2_Pack16:
case TextureFormat.R12X4G12X4_UNorm2_Pack16:
flags = FormatDescriptor.DescriptorFlags.PackedBit;
blockSizeInBits = 4 * 8;
blockWidth = 1;
blockHeight = 1;
blockDepth = 1;
break;
case TextureFormat.G16B16R163_Plane444_UNorm:
flags = FormatDescriptor.DescriptorFlags.PlanarBit;
blockSizeInBits = 6 * 8;
blockWidth = 1;
blockHeight = 1;
blockDepth = 1;
break;
case TextureFormat.Undefined:
flags = 0;
blockSizeInBits = 0 * 8;
blockWidth = 1;
blockHeight = 1;
blockDepth = 1;
break;
default:
throw new NotImplementedException("FormatDescriptor undefined for TextureFormat " + format);
}
return new FormatDescriptor(
flags,
blockSizeInBits,
blockWidth,
blockHeight,
blockDepth,
minBlocksX,
minBlocksY
);
}
}
}

View file

@ -0,0 +1,91 @@
namespace Brutal.TextureApi.Abstractions;
/// <summary>
/// Holds information about a <see cref="TextureFormat" />.
/// </summary>
/// <remarks>
/// <para />
/// Planar formats are not represented well by a FormatDescriptor. While a FormatDescriptor might be instantiated for a planar
/// texture format, the properties of the FormatDescriptor should not be relied upon.
/// </remarks>
/// <remarks>
/// This structure is taken from libktx in the file KTX-SOFTWARE\lib\vk_format.h
/// </remarks>
public readonly record struct FormatDescriptor
{
[Flags]
public enum DescriptorFlags
{
None = 0,
PackedBit = 1,
CompressedBit = 2,
DepthBit = 4,
StencilBit = 8,
PlanarBit = 16,
}
public FormatDescriptor(
DescriptorFlags flags,
uint blockSizeInBits,
uint blockWidth,
uint blockHeight,
uint blockDepth,
uint minBlockX,
uint minBlockY
)
{
Flags = flags;
BlockSizeInBits = blockSizeInBits;
BlockWidth = blockWidth;
BlockHeight = blockHeight;
BlockDepth = blockDepth;
MinBlocksX = minBlockX;
MinBlocksY = minBlockY;
}
public bool IsBlockCompressed => (Flags & DescriptorFlags.CompressedBit) != 0;
public bool IsPacked => (Flags & DescriptorFlags.PackedBit) != 0;
public bool IsDepth => (Flags & DescriptorFlags.DepthBit) != 0;
public bool IsStencil => (Flags & DescriptorFlags.StencilBit) != 0;
public bool IsPlanar => (Flags & DescriptorFlags.PlanarBit) != 0;
/// <summary>
/// Returns the size of a block in bytes
/// </summary>
public ByteSize BlockSizeInBytes => (ByteSize)(BlockSizeInBits >> 3);
/// <summary>
/// Extent of a block. i.e how many texels wide, high and deep a block one block of this TextureFormat is.
/// For non block-compressed formats this will be equivalent to TexelCount(1,1,1)
/// </summary>
public TexelCount3 BlockExtent => new((int)BlockWidth, (int)BlockHeight, (int)BlockDepth);
public DescriptorFlags Flags { get; }
public uint BlockSizeInBits { get; }
/// <summary>
/// Width of block in texels. For compressed formats this is how many pixels wide the block represents.
/// </summary>
public uint BlockWidth { get; }
/// <summary>
/// Height of block in texels. For compressed formats this is how many pixels high the block represents.
/// </summary>
public uint BlockHeight { get; }
/// <summary>
/// Depth of the block in texels. For compressed formats this is how many pixels deep the block represents.
/// </summary>
public uint BlockDepth { get; }
/// <summary>
/// Minimum number of blocks wide the format must have
/// </summary>
public uint MinBlocksX { get; }
/// <summary>
/// Minimum number of blocks high the format must have
/// </summary>
public uint MinBlocksY { get; }
}

View file

@ -0,0 +1,343 @@
using Brutal.Memory;
using Brutal.Numerics;
using Brutal.Pointers.Extensions;
namespace Brutal.TextureApi.Abstractions;
/// <summary>
/// A generic implementation of ITexture which can be used to hold user-defined texture data.
/// </summary>
/// <remarks>
/// Allocates its own memory for texture data. Must call Destroy() or use.
/// Has only a single mip level.
/// Has no row padding.
/// </remarks>
public class GenericTexture : ITexture
{
/// <summary>
/// Info required to create a GenericTexture
/// </summary>
public struct CreateInfo
{
public required TextureFormat Format;
public required TexelCount3 Extent;
public required int LayerCount;
public required int LevelCount;
public required int FaceCount;
public required bool IsCubemap;
public required bool IsArray;
public required int DimensionCount;
}
/// <summary>
/// Describes the starting offset and byte length of an image.
/// Used when creating a GenericTexture from a CreateInfo and memory size.
/// </summary>
public struct CreateImageRange
{
public required ByteSize StartByteOffset;
public required ByteSize ByteLength;
public required ByteSize RowPitch;
}
/// <summary>
/// Create a new generic texture with the given properties and allocation size.
/// </summary>
/// <param name="info">Properties describing the texture</param>
/// <param name="memAllocSize">The required allocation size for texture data, in bytes, no row padding</param>
/// <param name="imageRanges">The set of CreateImageRange objects describing where and how big the mip levels are.
/// MUST be supplied with a valid set of ranges if any of info.LevelCount, info.LayerCount or info.FaceCount is greater than 1.
/// If all of those properties are 1 then you can omit this argument which will treat all memory allocated as the memory
/// of the one and only mip level, layer and face</param>
/// <exception cref="TextureException">Thrown if memAllocSize is not valid for the supplied <paramref name="info" /> and <paramref name="imageRanges" /></exception>
/// <remarks>
/// Will zero-initialize the texture data memory.
/// It is assumed you will fill the texture data in a valid manner for the info specified.
/// </remarks>
public unsafe GenericTexture(
CreateInfo info,
ByteSize memAllocSize,
ReadOnlySpan<CreateImageRange> imageRanges = new()
)
{
if (info.LevelCount == 0 || info.LayerCount == 0 || info.FaceCount == 0)
throw new TextureException("LevelCount, LayerCount and FaceCount must all be greater than 0");
_data = Mem.AllocZeroed(memAllocSize).Cast<byte>();
_dataSize = memAllocSize;
_info = info;
_imageMemory = BuildImageMemoryRanges(info, memAllocSize, imageRanges);
_rowPitches = new ByteSize[info.LevelCount];
var descriptor = info.Format.Descriptor();
var minimumMemoryRequired = ByteSize.Zero;
for (var level = 0; level < info.LevelCount; ++level)
{
// Extent in # blocks wide, high, deep this image should be based on its texel extents.
var levelExtentInBlocks = new ImageExtent(info.Extent, level, descriptor.BlockExtent).Blocks;
var levelMinimumMemoryRequired =
(levelExtentInBlocks.X * levelExtentInBlocks.Y * levelExtentInBlocks.Z) * descriptor.BlockSizeInBytes;
minimumMemoryRequired += levelMinimumMemoryRequired;
_rowPitches[level] = GetLevelInfo(level, 0, 0).ByteRowPitch;
}
if (memAllocSize < minimumMemoryRequired)
throw new TextureException(
$"memAllocSize {memAllocSize} is too small for the given extent {info.Extent} and format {info.Format}. Minimum size is {minimumMemoryRequired}"
);
}
private Dictionary<long, ImageMemory> BuildImageMemoryRanges(
CreateInfo info,
ByteSize memAllocSize,
ReadOnlySpan<CreateImageRange> levelRanges
)
{
if (info is { LevelCount: 1, LayerCount: 1, FaceCount: 1 } && levelRanges.IsEmpty)
{
if (memAllocSize.Bytes % ((nuint)info.Extent.X * (nuint)info.Extent.Y * (nuint)info.Extent.Z) != 0)
throw new TextureException(
$"Memory byte size ({memAllocSize.Bytes} is not a multiple of (X * Y * Z extents)"
);
var key = Key(0, 0, 0);
return new Dictionary<long, ImageMemory>
{
[key] = new(
key,
ByteSize.Zero,
memAllocSize,
ByteSize.Of(memAllocSize.Bytes / (nuint)info.Extent.Z / (nuint)info.Extent.Y)
),
};
}
var expectedRangeCount = info.LevelCount * info.LayerCount * info.FaceCount;
if (levelRanges.Length != expectedRangeCount)
throw new TextureException(
$"Wrong levelRanges length. Expected {expectedRangeCount}, received {levelRanges.Length}"
);
var imageMemory = new Dictionary<long, ImageMemory>(levelRanges.Length);
var index = 0;
for (var level = 0; level < info.LevelCount; ++level)
for (var layer = 0; layer < info.LayerCount; ++layer)
for (var face = 0; face < info.FaceCount; ++face)
{
var key = Key(level, layer, face);
imageMemory[key] = new ImageMemory(
key,
levelRanges[index].StartByteOffset,
levelRanges[index].ByteLength,
levelRanges[index].RowPitch
);
++index;
}
return imageMemory;
}
/// <summary>
/// Create a new generic texture, copying the properties and data of an existing ITexture instance
/// </summary>
/// <param name="texture">The texture to copy</param>
public unsafe GenericTexture(ITexture texture)
{
var data = Mem.AllocAndCopy(texture.Data);
_data = data.AsPointer();
_dataSize = ByteSize.Of(data);
_imageMemory = BuildImageMemoryRangesFromTextureMips(texture);
_rowPitches = new ByteSize[texture.LevelCount];
for (var level = 0; level < texture.LevelCount; ++level)
_rowPitches[level] = texture.RowPitch(level);
_info = new CreateInfo
{
Format = texture.Format,
Extent = texture.Extent,
LayerCount = texture.LayerCount,
LevelCount = texture.LevelCount,
FaceCount = texture.FaceCount,
IsCubemap = texture.IsCubemap,
IsArray = texture.IsArray,
DimensionCount = texture.DimensionCount,
};
}
private unsafe Dictionary<long, ImageMemory> BuildImageMemoryRangesFromTextureMips(ITexture texture)
{
var imageMemory = new Dictionary<long, ImageMemory>();
for (var level = 0; level < texture.LevelCount; ++level)
for (var layer = 0; layer < texture.LayerCount; ++layer)
for (var face = 0; face < texture.FaceCount; ++face)
{
var data = texture.ImageData(level, layer, face);
var imageDataOffset = ByteSize.Of(data.AsPointer() - texture.Data.AsPointer());
var key = Key(level, layer, face);
imageMemory[key] = new ImageMemory(key, imageDataOffset, ByteSize.Of(data), texture.RowPitch(level));
}
return imageMemory;
}
public static class Defaults
{
public static GenericTexture Uncompressed(
TextureFormat format,
int2 extent,
int levels = 1,
int layers = 1,
int faces = 1
)
{
var createInfo = MakeInfo(format, extent);
createInfo.IsArray = layers > 1;
createInfo.IsCubemap = faces == 6;
var descriptor = format.Descriptor();
if (descriptor.IsBlockCompressed)
throw new ArgumentException($"Texture format is a block compressed format");
var memAllocSize = ByteSize.Zero;
var offset = ByteSize.Zero;
var ranges = new CreateImageRange[levels];
for (var level = 0; level < levels; ++level)
{
var bytesForLevel = (ByteSize)
Math.Max(
1,
(createInfo.Extent.X >> level)
* Math.Max(1, createInfo.Extent.Y >> level)
* descriptor.BlockSizeInBytes
);
memAllocSize += bytesForLevel;
ranges[level] = new()
{
StartByteOffset = offset,
ByteLength = bytesForLevel,
RowPitch = bytesForLevel,
};
offset += bytesForLevel;
}
return new GenericTexture(createInfo, memAllocSize, ranges);
}
/// <summary>
/// Creates a generic 2D, single layer, single level texture with R8G8B8_UNorm format
/// </summary>
/// <remarks>There is no row padding.</remarks>
/// <param name="extent">The required extent of the texture</param>
/// <returns>A new GenericTexture</returns>
public static GenericTexture RGB8UNorm(int2 extent)
{
var createInfo = MakeInfo(TextureFormat.R8G8B8_UNorm, extent);
var memAllocSize =
createInfo.Extent.X * createInfo.Extent.Y * createInfo.Format.Descriptor().BlockSizeInBytes;
return new GenericTexture(createInfo, memAllocSize);
}
/// <summary>
/// Creates a generic 2D, single layer, single level texture with R8G8B8A8_UNorm format
/// </summary>
/// <remarks>There is no row padding.</remarks>
/// <param name="extent">The required extent of the texture</param>
/// <returns>A new GenericTexture</returns>
public static GenericTexture RGBA8UNorm(int2 extent)
{
var createInfo = MakeInfo(TextureFormat.R8G8B8A8_UNorm, extent);
var memAllocSize =
createInfo.Extent.X * createInfo.Extent.Y * createInfo.Format.Descriptor().BlockSizeInBytes;
return new GenericTexture(createInfo, memAllocSize);
}
private static CreateInfo MakeInfo(TextureFormat format, int2 extent)
{
return new CreateInfo
{
Format = format,
Extent = new TexelCount3(extent.X, extent.Y, 1),
LayerCount = 1,
LevelCount = 1,
FaceCount = 1,
IsCubemap = false,
IsArray = false,
DimensionCount = 2,
};
}
}
public unsafe Span<byte> Data => new(_data, ElementCount.Of(_dataSize));
public unsafe Span<byte> ImageData(int level, int layer, int face)
{
var levelInfo = GetLevelInfo(level, layer, face);
return new Span<byte>(_data + levelInfo.StartByte, ElementCount.Of(levelInfo.ByteLength));
}
public TexelCount3 Extent => _info.Extent;
public int Width => Extent.X;
public int Height => Extent.Y;
public int Depth => Extent.Z;
public TextureFormat Format => _info.Format;
public int LayerCount => _info.LayerCount;
public int LevelCount => _info.LevelCount;
public int FaceCount => _info.FaceCount;
public bool IsCubemap => _info.IsCubemap;
public bool IsArray => _info.IsArray;
public int DimensionCount => _info.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 _rowPitches[level];
}
public void Destroy()
{
FreeData();
}
private unsafe void FreeData()
{
Mem.Free(_data);
}
private struct ImageMemory
{
public ImageMemory(long key, ByteSize startByte, ByteSize byteLength, ByteSize byteRowPitch)
{
Key = key;
StartByte = startByte;
ByteLength = byteLength;
ByteRowPitch = byteRowPitch;
}
public ByteSize ByteRowPitch { get; init; }
public long Key { get; init; }
public ByteSize StartByte { get; init; }
public ByteSize ByteLength { get; init; }
}
private long Key(int level, int layer, int face)
{
return (_info.LayerCount * _info.FaceCount) * level + _info.FaceCount * layer + face;
}
private ImageMemory GetLevelInfo(int level, int layer, int face)
{
return _imageMemory[Key(level, layer, face)];
}
private readonly unsafe byte* _data;
private readonly ByteSize _dataSize;
private readonly CreateInfo _info;
private readonly Dictionary<long, ImageMemory> _imageMemory;
private readonly ByteSize[] _rowPitches;
}

View file

@ -0,0 +1,6 @@
namespace Brutal.TextureApi.Abstractions;
/// <summary>
/// Represents a count of blocks in 3 dimensions.
/// </summary>
public record struct BlockCount3(int X, int Y, int Z) { }

View file

@ -0,0 +1,46 @@
namespace Brutal.TextureApi.Abstractions;
/// <summary>
/// Represents the 3 dimensional extents of one image (mip level) of a texture and includes
/// its dimensions in both texels and blocks.
/// </summary>
public readonly record struct ImageExtent
{
/// <summary>
/// Creates an ImageExtent representing the 3 dimensions of one mip level of a texture from the larges image dimension
/// of the texture and texel dimensions of one block of the image.
/// </summary>
/// <remarks>
/// <para />The Blocks property dimensions are rounded-up from Texels / blockTexelDimensions and will always be at least 1
/// </remarks>
/// <param name="level0ImageTexelDimensions">Dimensions of the largest mip level, in whole texels.</param>
/// <param name="blockTexelDimensions">The dimensions of one block, in whole texels</param>
/// <exception cref="ArgumentOutOfRangeException">Throws ArgumentOutOfRangeException if level is less than 0</exception>
public ImageExtent(TexelCount3 level0ImageTexelDimensions, int mipLevel, TexelCount3 blockTexelDimensions)
{
Level = mipLevel;
Texels = new TexelCount3(
level0ImageTexelDimensions.X >> mipLevel,
level0ImageTexelDimensions.Y >> mipLevel,
level0ImageTexelDimensions.Z >> mipLevel
);
Blocks = InBlocks(Texels, blockTexelDimensions);
}
/// <summary>
/// The dimensions of this texture in Blocks
/// </summary>
public BlockCount3 Blocks { get; }
public TexelCount3 Texels { get; }
public int Level { get; init; }
public static BlockCount3 InBlocks(TexelCount3 imageTexelDimensions, TexelCount3 blockTexelDimensions)
{
return new BlockCount3(
(int)MathF.Max(1, MathF.Ceiling(imageTexelDimensions.X / (float)blockTexelDimensions.X)),
(int)MathF.Max(1, MathF.Ceiling(imageTexelDimensions.Y / (float)blockTexelDimensions.Y)),
(int)MathF.Max(1, MathF.Ceiling(imageTexelDimensions.Z / (float)blockTexelDimensions.Z))
);
}
}

View file

@ -0,0 +1,48 @@
namespace Brutal.TextureApi.Abstractions;
/// <summary>
/// Describes how one image (mip level) of a texture is laid out in memory.
/// </summary>
public class TextureImageMemoryLayout
{
/// <summary>
/// Create a TextureImageMemoryLayout from a set of texture properties.
/// </summary>
/// <param name="extent">The ImageExtent of the image</param>
/// <param name="rowPitch">The rowpitch in bytes of the image</param>
/// <param name="blockSizeInBytes">The number of bytes a single block takes up.</param>
public TextureImageMemoryLayout(ImageExtent extent, ByteSize rowPitch, ByteSize blockSizeInBytes)
{
Extent = extent;
RowPitch = rowPitch;
RowPadBytes = RowPitch - Extent.Blocks.X * blockSizeInBytes;
}
/// <summary>
/// Gets the ImageExtent of this TextureImageMemoryLayout. This includes both the extents in texels
/// and in blocks.
/// </summary>
public ImageExtent Extent { get; }
/// <summary>
/// Gets the number of bytes between the start of consecutive rows in the buffer.
/// </summary>
public ByteSize RowPitch { get; }
/// <summary>
/// Gets the number of padding bytes added to each row in the data structure.
/// </summary>
public ByteSize RowPadBytes { get; }
/// <summary>
/// The number of bytes the x extent texel blocks take up.
/// For uncompressed formats this is the extent texel width * bytes per texel.
/// For compressed formats this is the extent in whole blocks * bytes per block.
/// </summary>
public ByteSize RowExtentBytes => RowPitch - RowPadBytes;
/// <summary>
/// Does this layout have padding at the end of each row?
/// </summary>
public bool IsRowPadded => RowPadBytes > 0;
}

View file

@ -0,0 +1,69 @@
using System.Diagnostics;
namespace Brutal.TextureApi.Abstractions;
/// <summary>
/// Describes how a texture is laid out in memory. Provides the FormatDescriptor for a texture
/// as well as a TextureImageMemoryLayout object for each level of the texture.
/// </summary>
/// <remarks>
/// <para />Each level's data is calculated and cached on first use so multiple calls to query the same
/// mip level layout does not incur extra cost.<br/>
/// </remarks>
public class TextureMemoryLayout
{
public TextureMemoryLayout(ITexture texture)
{
_texture = texture;
_descriptor = texture.Format.Descriptor();
_imageLayouts = new Lazy<TextureImageMemoryLayout>[texture.LevelCount];
for (var mipLevelIndex = 0; mipLevelIndex < texture.LevelCount; mipLevelIndex++)
{
var mipLevel = mipLevelIndex;
_imageLayouts[mipLevelIndex] = new Lazy<TextureImageMemoryLayout>(
() => ImageLayoutForLevel(mipLevel),
true
);
}
}
/// <summary>
/// Returns the FormatDescriptor for the texture.
/// </summary>
public FormatDescriptor Descriptor => _descriptor;
/// <summary>
/// Returns the TextureImageMemoryLayout object for a single level of a texture.
/// </summary>
/// <param name="level">The mip level to retrieve the TextureImageMemoryLayout for.</param>
/// <returns cref="TextureImageMemoryLayout">TextureImageMemoryLayout for the mip level</returns>
/// <exception cref="ArgumentOutOfRangeException">throws ArgumentOutOfRangeException if level is not within the valid range of mip levels (0 based) for the texture</exception>
public TextureImageMemoryLayout ImageLayout(int level)
{
Debug.Assert(_texture != null);
Debug.Assert(_imageLayouts.Length == _texture.LevelCount);
if (level < 0 || level >= _texture.LevelCount)
throw new ArgumentOutOfRangeException(
nameof(level),
level,
$"Level must be in range [0, {_texture.LevelCount})"
);
return _imageLayouts[level].Value;
}
private TextureImageMemoryLayout ImageLayoutForLevel(int level)
{
return new TextureImageMemoryLayout(
new ImageExtent(_texture.Extent, level, _descriptor.BlockExtent),
_texture.RowPitch(level),
_descriptor.BlockSizeInBytes
);
}
private readonly ITexture _texture;
private readonly FormatDescriptor _descriptor;
private readonly Lazy<TextureImageMemoryLayout>[] _imageLayouts;
}

View file

@ -0,0 +1,25 @@
namespace Brutal.TextureApi.Abstractions;
/// <summary>
/// Holds a collection of regions for copying texture data from a texture to a staging buffer,
/// and from the staging buffer to an image.
/// </summary>
public class StageUploadRegions
{
/// <summary>
/// The regions in the source texture's memory to copy
/// </summary>
public List<StagingRegion> StageBufferRegions = [];
/// <summary>
/// The regions in a staging buffer copied using the StageBufferRegions
/// that can be copied to the images on a GPU using a GPU API.
/// </summary>
public List<UploadRegion> CopyToImageRegions = [];
/// <summary>
/// The minimum number of bytes that your staging buffer needs to be
/// to accept the copied StageBufferRegions
/// </summary>
public ByteSize RequiredBufferSizeBytes;
}

View file

@ -0,0 +1,44 @@
namespace Brutal.TextureApi.Abstractions;
/// <summary>
/// Represents a region of texture data to be copied to a staging buffer.
/// </summary>
public class StagingRegion
{
/// <summary>
/// Offset from the start of the texture data to start copying from.
/// </summary>
public ByteSize SourceStartOffset;
/// <summary>
/// Offset within the destination staging buffer to start copying data to.
/// </summary>
public ByteSize DestinationOffset;
/// <summary>
/// How many bytes of data to copy.
/// </summary>
public ByteSize SizeInBytes;
/// <summary>
/// How many blocks wide is a single row of image data.
/// </summary>
public int RowWidthInBlocks;
/// <summary>
/// How many bytes does a block take up. This is directly related
/// to the <see cref="TextureFormat" /> of the texture data that's being staged.
/// </summary>
public ByteSize BlockSizeBytes;
/// <summary>
/// How many bytes from the start of one one row of the texture to the start
/// of the next row.
///
/// This can be > BlockSizeBytes * RowWidthInBlocks. Specifically this is the case
/// for some KTX version 1 textures because they are padded to support OpenGL texture uploading.
/// Almost all other formats will have RowPitch == RowwidthInBlocks * BlockSizeBytes but
/// do not just assume that is the case and use this field for memory calculations on texture data.
/// </summary>
public required ByteSize RowPitch;
}

View file

@ -0,0 +1,28 @@
namespace Brutal.TextureApi.Abstractions;
/// <summary>
/// A region of data for uploading to a range of layers within a single level of an image.
/// </summary>
public class UploadRegion
{
/// <summary>
/// Offset in the staging buffer where this region starts.
/// </summary>
public ByteSize BufferOffset;
/// <summary>
/// The array (or faces if this is a non-array cube map) layer index this region starts at.
/// </summary>
public int BaseLayer { get; set; }
/// <summary>
/// The number of array layers (or faces if this is a non-array cube map)
/// this region encompasses.
/// </summary>
public int LayerCount { get; set; }
/// <summary>
/// The mip level that this UploadRegion starts at.
/// </summary>
public int BaseLevel { get; set; }
}

View file

@ -0,0 +1,266 @@
using System.Diagnostics;
using Brutal.Pointers.Extensions;
using Brutal.TextureApi.Abstractions;
namespace Brutal.TextureApi.Abstractions;
/// <summary>
/// Builds a set of information to describe how to stage an ITexture's data to a staging buffer
/// and upload it to a GPU.
/// </summary>
/// <remarks>
/// <list type="bullet">Removes row padding from KTX v1 textures.<br/></list>
/// <list type="bullet">Aligning data to lcm(4, texel block size)<br/></list>
/// <list type="bullet">Reducing number of copy commands where possible.<br/></list>
/// <list type="bullet">Currently does NOT squash all mips into one copy for staging even if it would be valid.<br/></list>
/// </remarks>
public class UploadRegions
{
public enum Mode
{
BatchUploadRegions, // Try to put as many images into as few copy regions as possible
UploadRegionPerImage, // Each image gets its own copy region
}
private UploadRegions()
{
_currentStageRegion = null;
_currentUploadRegion = null;
}
/// <summary>
/// Builds optimal staging and upload regions for entire mip range
/// of a texture.
/// </summary>
/// <param name="texture"></param>
/// <returns></returns>
public static StageUploadRegions Build(ITexture texture)
{
var textureUpload = new UploadRegions();
return textureUpload.BuildInternal(texture, 0, texture.LevelCount, Mode.BatchUploadRegions);
}
/// <summary>
/// Builds optimal staging and upload regions for a selected mip range
/// of a texture.
/// </summary>
/// <param name="texture">Texture to build regions for.</param>
/// <param name="firstLevel">First level to build regions for. This level will become level 0 in the output.</param>
/// <param name="maximumLevelCount">Maximum # of levels to build. Must be > 0.</param>
/// <param name="mode">How to build the regions.</param>
/// <returns>The <see cref="StageUploadRegions" /> that were built.</returns>
/// <exception cref="ArgumentOutOfRangeException">Thrown if firstLevel >= the level count of texture or maximumLevelCount &lt;= 0.</exception>
public static StageUploadRegions Build(
ITexture texture,
int firstLevel,
int maximumLevelCount,
Mode mode = Mode.BatchUploadRegions
)
{
if (firstLevel >= texture.LevelCount)
throw new ArgumentOutOfRangeException(
nameof(firstLevel),
$"Level out of range. Expected 0-{texture.LevelCount - 1}, received {firstLevel}."
);
if (maximumLevelCount <= 0)
throw new ArgumentOutOfRangeException(
nameof(maximumLevelCount),
"Maximum level count must be greater than 0."
);
var textureUpload = new UploadRegions();
return textureUpload.BuildInternal(texture, firstLevel, maximumLevelCount, mode);
}
/// <summary>
/// Builds a datastructure describing how to upload an ITexture's images to a buffer and then to
/// a GPU.
/// Goes through the texture by level, layer, face and slice order, building the minimal
/// set of copies required to upload the texture, removing any row padding and also
/// aligning as needed.
/// </summary>
private unsafe StageUploadRegions BuildInternal(ITexture texture, int firstLevel, int levelCount, Mode mode)
{
var formatDescriptor = texture.Format.Descriptor();
levelCount = Math.Min(levelCount, texture.LevelCount - firstLevel);
_uploadRegions = new List<UploadRegion>(texture.LevelCount);
_copyRegions = new List<StagingRegion>(texture.LevelCount * texture.LayerCount * texture.FaceCount);
_lcm = CalculateLcm4AndTexelBlockSize(formatDescriptor.BlockSizeInBytes);
_stageBufferDestinationOffset = ByteSize.Zero;
var layouts = new TextureMemoryLayout(texture);
var blockSizeBytes = formatDescriptor.BlockSizeInBytes;
for (var level = firstLevel; level < firstLevel + levelCount; ++level)
{
// Output levels are re-based so the requested firstLevel becomes level 0. Batch-mode's
// "start a new region when the level changes" check must compare against this re-based
// level, otherwise any firstLevel > 0 never matches BaseLevel and every layer is split
// into its own region.
var outputLevel = level - firstLevel;
var layout = layouts.ImageLayout(level);
var rowWidthInBlocks = layout.Extent.Blocks.X;
var rowPitch = layout.RowPitch;
var depthSlices = texture.Is3d ? layout.Extent.Texels.Z : 1;
for (var layer = 0; layer < texture.LayerCount; ++layer)
{
for (var face = 0; face < texture.FaceCount; ++face)
{
var sourceImageData = texture.ImageData(level, layer, face);
var sourceImageOffset = ByteSize.Of(sourceImageData.AsPointer() - texture.Data.AsPointer());
if (
_currentUploadRegion == null
|| mode == Mode.UploadRegionPerImage
|| (mode == Mode.BatchUploadRegions && outputLevel != _currentUploadRegion.BaseLevel)
)
NewUploadRegion(outputLevel, layer * texture.FaceCount + face);
for (var depthSlice = 0; depthSlice < depthSlices; ++depthSlice)
{
AddImageData(
sourceImageOffset,
ByteSize.Of(sourceImageData),
rowWidthInBlocks,
blockSizeBytes,
rowPitch
);
sourceImageOffset += ByteSize.Of(sourceImageData);
}
++_currentUploadRegion!.LayerCount;
}
}
}
var minimumRequiredBytes =
_copyRegions.Count == 0
? ByteSize.Of(0)
: _copyRegions[^1].DestinationOffset + _copyRegions[^1].SizeInBytes;
return new StageUploadRegions()
{
StageBufferRegions = _copyRegions,
CopyToImageRegions = _uploadRegions,
RequiredBufferSizeBytes = minimumRequiredBytes,
};
}
private void AddImageData(
ByteSize sourceImageOffset,
ByteSize length,
int rowWidthInBlocks,
ByteSize blockSizeBytes,
ByteSize rowPitch
)
{
UpdateStageRegion(sourceImageOffset, rowWidthInBlocks, blockSizeBytes, rowPitch);
ExtendStageRegion(length);
}
private bool ContinuesCurrentStageRegionSource(ByteSize sourceImageOffset)
{
return sourceImageOffset == _currentStageRegion!.SourceStartOffset + _currentStageRegion.SizeInBytes;
}
private bool ContinuesCurrentStageRegionDestination(ByteSize destinationOffset)
{
return destinationOffset == _currentStageRegion!.DestinationOffset + _currentStageRegion.SizeInBytes;
}
private void ExtendStageRegion(ByteSize sizeInBytes)
{
Debug.Assert(_currentStageRegion != null);
_currentStageRegion.SizeInBytes += sizeInBytes;
_stageBufferDestinationOffset += sizeInBytes;
}
private static uint CalculateLcm4AndTexelBlockSize(ByteSize texelBlocksize)
{
if (texelBlocksize == 0)
return 0;
var num1 = ByteSize.Of(4u);
var num2 = texelBlocksize;
while (num1 != num2)
{
if (num1 > num2)
{
num1 -= num2;
}
else
{
num2 -= num1;
}
}
return (uint)(4 * texelBlocksize / num1);
}
private void NewUploadRegion(int baseLevel, int baseLayer)
{
Debug.Assert(!_uploadRegions.Any(r => r.BaseLayer == baseLayer && r.BaseLevel == baseLevel));
_stageBufferDestinationOffset = AlignOffsetToNextLcm(_stageBufferDestinationOffset);
_currentUploadRegion = new UploadRegion()
{
BaseLevel = baseLevel,
BaseLayer = baseLayer,
LayerCount = 0,
BufferOffset = _stageBufferDestinationOffset,
};
_uploadRegions.Add(_currentUploadRegion);
}
private void UpdateStageRegion(
ByteSize startOffset,
int rowWidthInBlocks,
ByteSize blockSizeBytes,
ByteSize rowPitch
)
{
Debug.Assert(
_stageBufferDestinationOffset == 0
|| _currentStageRegion == null
|| _stageBufferDestinationOffset
>= _currentStageRegion.DestinationOffset + _currentStageRegion.SizeInBytes
);
if (_currentStageRegion != null)
{
if (
ContinuesCurrentStageRegionSource(startOffset)
&& ContinuesCurrentStageRegionDestination(_stageBufferDestinationOffset)
)
return;
}
_currentStageRegion = new StagingRegion
{
DestinationOffset = _stageBufferDestinationOffset,
SourceStartOffset = startOffset,
SizeInBytes = ByteSize.Zero,
RowWidthInBlocks = rowWidthInBlocks,
BlockSizeBytes = blockSizeBytes,
RowPitch = rowPitch,
};
_copyRegions.Add(_currentStageRegion);
}
private ByteSize AlignOffsetToNextLcm(ByteSize offset)
{
return ByteSize.Of((offset.Bytes + _lcm - 1) / _lcm * _lcm);
}
private ByteSize _stageBufferDestinationOffset;
private uint _lcm;
private StagingRegion? _currentStageRegion;
private UploadRegion? _currentUploadRegion;
private List<StagingRegion> _copyRegions = [];
private List<UploadRegion> _uploadRegions = [];
}

View 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)
);
}
}

View 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;
}
}

View 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),
};
}
}

View 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();
}
}

View file

@ -0,0 +1,9 @@
using Brutal.TextureApi;
namespace Brutal.TextureApi.Gli;
public class GliTextureException : TextureException
{
public GliTextureException(string message)
: base(message) { }
}

View 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"]);
}

View file

@ -0,0 +1,3 @@
namespace Brutal.TextureApi.Gli;
public class GliTextureSaveException(string message) : GliTextureException(message);

View 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}\".");
}
}
}

View 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);
}

Some files were not shown because too many files have changed in this diff Show more