namespace Brutal.VulkanApi.Abstractions;
///
/// Allocation information for a partition of a buffer allocation (portion of a larger buffer)
///
/// Buffer partitioner that created this partition
/// Information about this allocation within the buffer
/// Offset within owning buffer where this partition starts
/// Size of this partition
public readonly record struct BufferPartitionInfo(
IBufferPartitioner? Partitioner,
BufferAllocationInfo BufferInfo,
ByteSize PartitionOffset,
ByteSize PartitionSize
) : IMappable, IBindBuffer
{
public record struct CreateInfo(ByteSize Size);
///
/// Reference to the parent Vulkan buffer
///
public VkBuffer VkBuffer => BufferInfo.VkBuffer;
///
/// Offset within the parent buffer where this partition starts
///
public ByteSize BindOffset => BufferInfo.BufferOffset + PartitionOffset;
///
/// Offset within the owning device memory allocation where this partition starts
///
public ByteSize MemoryOffset => BufferInfo.AllocationInfo.AllocOffset + BindOffset;
///
/// If true, this partition is a full buffer allocation
///
public bool IsFullPartition => Partitioner is null;
///
/// Convenience function to create a partition info object for a full buffer allocation
///
/// BufferAllocationInfo object
/// BufferAllocationInfo as BufferPartitionInfo, with the 'partition' covering the entire buffer
public static BufferPartitionInfo FullPartition(BufferAllocationInfo bufferAllocationInfo)
{
return new BufferPartitionInfo(
Partitioner: null,
BufferInfo: bufferAllocationInfo,
PartitionSize: bufferAllocationInfo.BufferSize,
PartitionOffset: ByteSize.Zero
);
}
///
/// Return this partition to wherever it came from: a recycling partitioner gets it back via
/// , and a full partition frees the
/// underlying buffer allocation. Partitions from a non-recycling partitioner (e.g.
/// ) are a no-op - their space is reclaimed only by
/// resetting the partitioner.
///
public void Free()
{
if (Partitioner is IRecycleBufferPartitioner recycler)
recycler.FreePartition(in this);
else if (Partitioner is null)
((IBufferAllocator)BufferInfo.AllocationInfo.Allocator).FreeAllocation(BufferInfo);
}
AllocationInfo IMappable.AllocationInfo => BufferInfo.AllocationInfo;
ByteSize IMappable.MapSize => PartitionSize;
ByteSize IMappable.MapOffset => PartitionOffset;
}