using System.Diagnostics; namespace Brutal.VulkanApi.Abstractions; /// /// Convenience struct for mapped memory /// public readonly struct MappedMemory : IDisposable, IEquatable { /// /// Internal allocator used to map and unmap memory /// private IAllocator Allocator => _allocationMapInfo.AllocationInfo.Allocator; /// /// Allocation info of the mapped memory, supplied by the allocator /// private readonly AllocationMapInfo _allocationMapInfo; public MapRegion Region => _allocationMapInfo.Region; public readonly Ptr MapPtr; /// /// Device memory handle /// public VkDeviceMemory DeviceMemory => _allocationMapInfo.AllocationInfo.Memory; /// /// Construct and map memory /// /// Memory allocation info of memory /// Pointer to existing mapped pointer internal MappedMemory(in AllocationMapInfo allocationAllocationMapInfo, Ptr existingPtr) { Debug.Assert(allocationAllocationMapInfo.Region.Size > 0); _allocationMapInfo = allocationAllocationMapInfo; MapPtr = existingPtr; if (existingPtr.IsNull()) MapPtr = allocationAllocationMapInfo.AllocationInfo.Allocator.MapMemory(_allocationMapInfo); } /// /// Create a view into a mapped memory region that is a subset of the original mapped region /// /// Thrown if the byte offset or size is invalid public MappedMemory Offset(MapRegion region) { if (region.Offset > Region.Size) throw new ArgumentException(@"Byte offset exceeds buffer size", nameof(region.Offset)); if (region.Size == VK.WHOLE_SIZE) { region = region with { Size = Region.Size - region.Offset }; } if (region.Offset + region.Size > Region.Size) throw new ArgumentException(@"Data exceeds buffer size", nameof(region.Size)); return new MappedMemory( _allocationMapInfo with { Region = region with { Offset = region.Offset + Region.Offset }, }, MapPtr.Offset(region.Offset) ); } public MappedMemory Offset(ByteSize bytes) { return Offset(new MapRegion { Offset = bytes }); } /// /// Unmap the memory region using source allocator /// public void Unmap() { Allocator.UnmapMemory(_allocationMapInfo); } /// /// Flush the mapped memory range /// public void Flush() { Allocator .GetDevice() .FlushMappedMemoryRanges( [ new VkMappedMemoryRange { Memory = DeviceMemory, Offset = Region.Offset, Size = Region.Size, }, ] ); } /// /// Invalidate the mapped memory range /// public void Invalidate() { Allocator .GetDevice() .InvalidateMappedMemoryRanges( [ new VkMappedMemoryRange { Memory = DeviceMemory, Offset = Region.Offset, Size = Region.Size, }, ] ); } /// /// Cast the mapped memory to a reference of type T /// /// Type to cast /// Cast object /// Data exceeds buffer size public unsafe ref T As() where T : unmanaged { if (ByteSize.Of() > Region.Size) throw new ArgumentException(@"Data exceeds buffer size", nameof(T)); var firstElement = (T*)MapPtr; return ref *firstElement; } /// /// Cast the mapped memory to a span of bytes /// /// Byte span public Span AsSpan() => AsSpan(); /// /// Cast the mapped memory to a span of type T /// /// Span element type /// Span of type T /// Requested data exceeds buffer size public Span AsSpan() where T : unmanaged { if (Region.Size < ByteSize.Of()) throw new ArgumentException(@"Requested data exceeds buffer size", nameof(T)); unsafe { return new Span(MapPtr, (int)Region.Size).Cast(); } } /// public void Dispose() { Unmap(); } public static MappedMemory Map(AllocationMapInfo allocationMapInfo) { return new MappedMemory(allocationMapInfo, null); } public bool Equals(MappedMemory other) { return _allocationMapInfo.Equals(other._allocationMapInfo) && MapPtr == other.MapPtr; } public override bool Equals(object? obj) { return obj is MappedMemory other && Equals(other); } public override int GetHashCode() { return _allocationMapInfo.GetHashCode(); } }