namespace Brutal.VulkanApi.Abstractions;
///
/// Texture array slot partitioner
/// Creates and manages an image array, and this allocator will distribute slots from the array for users to stage/bind to
///
public class TextureArrayPartitioner : IRecycleImageViewPartitioner
{
public ImageViewPartitionInfo BackingPartition { get; set; }
///
/// Free list of texture array slots
///
private readonly FreeListIndexPool _textureArraySlots;
///
/// Total number of slots in the texture array
///
public int TotalSlots => BackingPartition.LocalSubresourceRange.LayerCount;
///
/// Construct a texture array partitioner from an existing image allocation
///
/// Backing image allocation
public TextureArrayPartitioner(ImageViewPartitionInfo backingPartition)
{
BackingPartition = backingPartition;
_textureArraySlots = new FreeListIndexPool(backingPartition.LocalSubresourceRange.LayerCount, false);
}
///
public bool TryCreatePartition(
in ImageViewPartitionInfo.CreateInfo createInfo,
out ImageViewPartitionInfo partition
)
{
if (
createInfo.Extent.Width > BackingPartition.Extent.Width
|| createInfo.Extent.Height > BackingPartition.Extent.Height
|| createInfo.Extent.Depth > BackingPartition.Extent.Depth
)
{
throw new ArgumentException("Extent mismatch");
}
if (createInfo.LayerCount > 1)
{
throw new ArgumentException("ArraySlots must be 1");
}
if (!TryAllocateSlot(out var slot))
{
partition = default;
return false;
}
partition = BackingPartition with
{
Partitioner = this,
Extent = createInfo.Extent,
ArrayCount = 1,
ArrayOffset = BackingPartition.ArrayOffset + slot,
};
return true;
}
///
/// Allocate a slot in the texture array and return the array index (slot)
/// Can call this directly if you don't need the full ImageSubAllocationInfo
///
/// Slot/array index allocated
public bool TryAllocateSlot(out int slot)
{
return _textureArraySlots.TryAdd(out slot);
}
///
public void FreePartition(in ImageViewPartitionInfo inAllocation)
{
FreeSlot(inAllocation.ArrayOffset - BackingPartition.LocalSubresourceRange.LayerCount);
}
///
/// Free up a slot in texture array
/// Can call this directly if you didn't need the full ImageSubAllocationInfo and just used the slot handle
///
/// Array index (aka slot handle)
public void FreeSlot(int slot)
{
_textureArraySlots.Remove(slot);
}
public void Reset(ImageViewPartitionInfo info)
{
BackingPartition = info;
_textureArraySlots.Clear();
_textureArraySlots.Capacity = BackingPartition.LocalSubresourceRange.LayerCount;
}
}