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

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