SOURCE DUMP v1

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

View file

@ -0,0 +1,57 @@
namespace Brutal.VulkanApi.Abstractions;
/// <summary>
/// Allocation information for a partition of a buffer allocation (portion of a larger buffer)
/// </summary>
/// <param name="Partitioner">Buffer partitioner that created this partition</param>
/// <param name="BufferInfo">Information about this allocation within the buffer</param>
/// <param name="PartitionOffset">Offset within owning buffer where this partition starts</param>
/// <param name="PartitionSize">Size of this partition</param>
public readonly record struct BufferPartitionInfo(
IBufferPartitioner? Partitioner,
BufferAllocationInfo BufferInfo,
ByteSize PartitionOffset,
ByteSize PartitionSize
) : IMappable, IBindBuffer
{
public record struct CreateInfo(ByteSize Size);
/// <summary>
/// Reference to the parent Vulkan buffer
/// </summary>
public VkBuffer VkBuffer => BufferInfo.VkBuffer;
/// <summary>
/// Offset within the parent buffer where this partition starts
/// </summary>
public ByteSize BindOffset => BufferInfo.BufferOffset + PartitionOffset;
/// <summary>
/// Offset within the owning device memory allocation where this partition starts
/// </summary>
public ByteSize MemoryOffset => BufferInfo.AllocationInfo.AllocOffset + BindOffset;
/// <summary>
/// If true, this partition is a full buffer allocation
/// </summary>
public bool IsFullPartition => Partitioner is null;
/// <summary>
/// Convenience function to create a partition info object for a full buffer allocation
/// </summary>
/// <param name="bufferAllocationInfo">BufferAllocationInfo object</param>
/// <returns>BufferAllocationInfo as BufferPartitionInfo, with the 'partition' covering the entire buffer</returns>
public static BufferPartitionInfo FullPartition(BufferAllocationInfo bufferAllocationInfo)
{
return new BufferPartitionInfo(
Partitioner: null,
BufferInfo: bufferAllocationInfo,
PartitionSize: bufferAllocationInfo.BufferSize,
PartitionOffset: ByteSize.Zero
);
}
AllocationInfo IMappable.AllocationInfo => BufferInfo.AllocationInfo;
ByteSize IMappable.MapSize => PartitionSize;
ByteSize IMappable.MapOffset => PartitionOffset;
}

View file

@ -0,0 +1,56 @@
namespace Brutal.VulkanApi.Abstractions;
/// <summary>
/// Interface for a buffer partitioner object capable of allocating and freeing partitions of a buffer
/// </summary>
public interface IBufferPartitioner
{
BufferPartitionInfo BackingPartition { get; protected set; }
/// <summary>
/// Reset the partitioner to its initial state with the given partition info. Invalidates all current allocations.
/// </summary>
/// <param name="info"></param>
public void Reset(BufferPartitionInfo info);
/// <summary>
/// Allocate a buffer partition with the given required size
/// </summary>
/// <param name="createInfo">Struct containing all relevant information about the allocation</param>
/// <param name="partition">The resulting partition</param>
/// <returns>True if the allocation was successful</returns>
public bool TryCreatePartition(BufferPartitionInfo.CreateInfo createInfo, out BufferPartitionInfo partition);
}
/// <summary>
/// Interface for a buffer partitioner object capable of allocating and freeing partitions of a buffer
/// </summary>
public interface IRecycleBufferPartitioner : IBufferPartitioner
{
/// <summary>
/// Free a buffer partition
/// </summary>
/// <param name="inAllocation">Struct containing all relevant information about the allocation</param>
public void FreePartition(in BufferPartitionInfo inAllocation);
}
public static class BufferPartitionerExtensions
{
public static BufferPartitionInfo CreatePartition<T>(
this T partitioner,
in BufferPartitionInfo.CreateInfo createInfo
)
where T : IBufferPartitioner
{
if (!partitioner.TryCreatePartition(createInfo, out var partition))
throw new OutOfMemoryException("Failed to create partition");
return partition;
}
/// <inheritdoc cref="IBufferPartitioner.Reset"/>
public static void Reset(this IBufferPartitioner partitioner)
{
partitioner.Reset(partitioner.BackingPartition);
}
}

View file

@ -0,0 +1,94 @@
namespace Brutal.VulkanApi.Abstractions;
/// <summary>
/// Basic linear allocator/partitioner, allocates memory/partitions in a linear fashion
/// Very simple design, doesn't support freeing any space except for a full clear
/// Provides optional alignment for buffers that require a minimum alignment offset
/// </summary>
public class LinearBufferPartitioner : IBufferPartitioner, ISize
{
/// <summary>
/// Basic linear allocator/partitioner, allocates memory/partitions in a linear fashion
/// Very simple design, doesn't support freeing any space except for a full clear
/// Provides optional alignment for buffers that require a minimum alignment offset
/// </summary>
public LinearBufferPartitioner(BufferPartitionInfo backingPartition, ByteSize alignment = default)
{
Alignment = alignment;
BackingPartition = backingPartition;
if (GetAlignedOffset(Alignment, backingPartition.BindOffset) != backingPartition.BindOffset)
throw new Exception("Invalid alignment of backing buffer partition");
}
/// <summary>
/// Backing partition this partitioner is slicing up buffer portions of
/// </summary>
public BufferPartitionInfo BackingPartition { get; set; }
/// <summary>
/// Capacity of this partitioner
/// </summary>
public ByteSize Capacity => BackingPartition.PartitionSize;
/// <summary>
/// Current offset from the start of the backing partition
/// </summary>
public ByteSize CurrentOffset { get; private set; }
/// <summary>
/// Alignment of allocations: if zero, no alignment applied
/// </summary>
public readonly ByteSize Alignment;
/// <inheritdoc />
int ISize.Size => (int)Capacity;
/// <summary>
/// Number of bytes remaining in the partitioner
/// </summary>
public ByteSize FreeSpace => Capacity - CurrentOffset;
/// <inheritdoc />
public bool TryCreatePartition(BufferPartitionInfo.CreateInfo info, out BufferPartitionInfo partition)
{
var size = Alignment > 0 ? GetAlignedOffset(Alignment, info.Size) : info.Size;
// Check we haven't exceeded the capacity of the partitioner
if (CurrentOffset + size > Capacity)
{
partition = default;
return false;
}
// Claim the space
var offset = CurrentOffset;
CurrentOffset += size;
// Create the partition info object
partition = new BufferPartitionInfo(
Partitioner: this,
BufferInfo: BackingPartition.BufferInfo,
PartitionOffset: offset + BackingPartition.PartitionOffset,
PartitionSize: size
);
return true;
}
/// <inheritdoc />
public void Reset(BufferPartitionInfo info)
{
BackingPartition = info;
CurrentOffset = ByteSize.Zero;
}
/// <summary>
/// Get an offset (or size of an object) fitting correct minimum memory alignment
/// </summary>
/// <param name="alignment">Min Alignment</param>
/// <param name="offset">Required offset</param>
/// <returns>Aligned offset</returns>
public static ByteSize GetAlignedOffset(ByteSize alignment, ByteSize offset)
{
return ByteSize.Of((offset + alignment - 1) & ~(alignment - 1));
}
}