namespace Brutal.VulkanApi.Abstractions;
///
/// Extends the type to provide additional functionality.
///
public readonly struct BufferEx : IDisposable, INullable, IMappable, IBindBuffer
{
public ref struct CreateInfo
{
/// The name to apply to the buffer and allocation
public required string Name;
///
public VkBufferCreateFlags BufferFlags;
///
public ByteSize BufferSize;
///
public VkBufferUsageFlags BufferUsage;
///
public VkSharingMode BufferSharingMode;
///
public int BufferQueueFamilyIndexCount;
///
public unsafe int* BufferQueueFamilyIndices;
///
public Ptr BufferNext { get; set; }
///
public MemoryPreference AllocPreference;
///
public VkMemoryPropertyFlags AllocRequiredProperties;
///
public VkMemoryPropertyFlags AllocPreferredProperties;
///
public float AllocPriority;
///
public Ptr AllocNext { get; set; }
///
public VkMemoryAllocateFlags AllocFlags;
///
/// Extract or populate this struct with a VkBufferCreateInfo
///
public unsafe VkBufferCreateInfo BufferCreateInfo
{
get =>
new()
{
Flags = BufferFlags,
Size = BufferSize,
Usage = BufferUsage,
SharingMode = BufferSharingMode,
QueueFamilyIndexCount = BufferQueueFamilyIndexCount,
QueueFamilyIndices = BufferQueueFamilyIndices,
Next = BufferNext,
};
set
{
BufferFlags = value.Flags;
BufferSize = value.Size;
BufferUsage = value.Usage;
BufferSharingMode = value.SharingMode;
BufferQueueFamilyIndexCount = value.QueueFamilyIndexCount;
BufferQueueFamilyIndices = value.QueueFamilyIndices;
BufferNext = value.Next;
}
}
///
/// Extract or populate this struct with an AllocationInfo.CreateInfo
///
public AllocationInfo.CreateInfo AllocationInfo
{
get =>
new()
{
Preference = AllocPreference,
RequiredProperties = AllocRequiredProperties,
PreferredProperties = AllocPreferredProperties,
Priority = AllocPriority,
Next = AllocNext,
AllocateFlags = AllocFlags,
};
set
{
AllocPreference = value.Preference;
AllocRequiredProperties = value.RequiredProperties;
AllocPreferredProperties = value.PreferredProperties;
AllocPriority = value.Priority;
AllocNext = value.Next;
AllocFlags = value.AllocateFlags;
}
}
}
///
/// Source of the buffer allocation
///
public readonly IBufferAllocator Allocator;
///
/// Allocation info for the buffer
///
public readonly BufferAllocationInfo BufferAllocationInfo;
///
/// The size of the buffer allocation
///
public ByteSize BufferSize => BufferAllocationInfo.BufferSize;
///
/// Offset within the owning buffer where this buffer starts
///
public ByteSize BindOffset => BufferAllocationInfo.BufferOffset;
///
/// Usage flags this buffer supports
///
public VkBufferUsageFlags Usage => BufferAllocationInfo.Usage;
internal BufferEx(IBufferAllocator allocator, CreateInfo createInfo)
{
Allocator = allocator;
BufferAllocationInfo = allocator.AllocateBuffer(
createInfo.Name,
createInfo.BufferCreateInfo,
createInfo.AllocationInfo
);
}
///
/// Construct a BufferEx from an existing buffer allocation
///
/// Pre-allocated buffer sourced from an IBufferAllocator
public BufferEx(BufferAllocationInfo allocationInfo)
{
BufferAllocationInfo = allocationInfo;
Allocator = (IBufferAllocator)allocationInfo.AllocationInfo.Allocator;
}
///
public void Dispose()
{
Allocator.FreeAllocation(BufferAllocationInfo);
}
///
/// Vulkan buffer handle
///
public VkBuffer VkBuffer => BufferAllocationInfo.VkBuffer;
///
/// Vulkan device memory handle
///
public VkDeviceMemory VkDeviceMemory => BufferAllocationInfo.AllocationInfo.Memory;
///
/// Query the buffer's GPU virtual address via
/// vkGetBufferDeviceAddress.
/// The buffer must have been created with and its memory
/// allocated with (see ).
///
public VkDeviceAddress GetDeviceAddress()
{
return Allocator.GetDevice().GetBufferDeviceAddress(new VkBufferDeviceAddressInfo { Buffer = VkBuffer });
}
public BufferPartitionInfo AsPartition()
{
if (IsNull())
throw new NullReferenceException(nameof(VkBuffer));
return BufferPartitionInfo.FullPartition(BufferAllocationInfo);
}
public static implicit operator BufferPartitionInfo(BufferEx buffer) => buffer.AsPartition();
public bool IsNull()
{
return VkBuffer.IsNull();
}
AllocationInfo IMappable.AllocationInfo => BufferAllocationInfo.AllocationInfo;
ByteSize IMappable.MapSize => BufferSize;
ByteSize IMappable.MapOffset => ByteSize.Zero; // BufferOffset should not be used here
}
public static class BufferExExtensions
{
public static BufferEx CreateBuffer(this IBufferAllocator allocator, BufferEx.CreateInfo createInfo)
{
return new BufferEx(allocator, createInfo);
}
}