mirror of
https://github.com/ahwootech/BRUTAL.git
synced 2026-11-26 04:58:24 +01:00
SOURCE DUMP v1
This commit is contained in:
parent
bd62d51ec9
commit
fb20299e9d
11 changed files with 768360 additions and 0 deletions
|
|
@ -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
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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; }
|
||||
}
|
||||
|
|
@ -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;
|
||||
}
|
||||
|
|
@ -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) { }
|
||||
|
|
@ -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))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -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;
|
||||
}
|
||||
|
|
@ -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;
|
||||
}
|
||||
|
|
@ -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;
|
||||
}
|
||||
|
|
@ -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;
|
||||
}
|
||||
|
|
@ -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; }
|
||||
}
|
||||
|
|
@ -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 <= 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 = [];
|
||||
}
|
||||
Loading…
Reference in a new issue