From 6e56626136f07dc2480034df3f0d80de18620874 Mon Sep 17 00:00:00 2001 From: Dylan Perks <11160611+Perksey@users.noreply.github.com> Date: Sun, 29 Aug 2021 16:47:32 +0100 Subject: [PATCH 1/4] Create VMASharp_API_For_Review.md --- src/Vulkan/VMASharp_API_For_Review.md | 307 ++++++++++++++++++++++++++ 1 file changed, 307 insertions(+) create mode 100644 src/Vulkan/VMASharp_API_For_Review.md diff --git a/src/Vulkan/VMASharp_API_For_Review.md b/src/Vulkan/VMASharp_API_For_Review.md new file mode 100644 index 0000000000..c44c8cbda2 --- /dev/null +++ b/src/Vulkan/VMASharp_API_For_Review.md @@ -0,0 +1,307 @@ +```cs +namespace VMASharp.Metadata +{ + public interface IBlockMetadata + { + long Size { get; } + int AllocationCount { get; } + long SumFreeSize { get; } + long UnusedRangeSizeMax { get; } + bool IsEmpty { get; } + void Validate(); + void CalcAllocationStatInfo(out StatInfo outInfo); + void AddPoolStats(ref PoolStats stats); + bool TryCreateAllocationRequest(in AllocationContext context, out AllocationRequest request); + bool MakeRequestedAllocationsLost( + int currentFrame, + int frameInUseCount, + ref AllocationRequest request); + int MakeAllocationsLost(int currentFrame, int frameInUseCount); + void CheckCorruption([NativeInteger] UIntPtr blockDataPointer); + void Alloc( + in AllocationRequest request, + SuballocationType type, + long allocSize, + BlockAllocation allocation); + void Free(BlockAllocation allocation); + void FreeAtOffset(long offset); + } +} +namespace VMASharp +{ + public abstract class Allocation : IDisposable + { + public long Size { get; } + public int MemoryTypeIndex { get; } + public abstract DeviceMemory DeviceMemory { get; } + public abstract long Offset { get; internal set; } + public object? UserData { get; set; } + public abstract IntPtr MappedData { get; } + public void Dispose(); + public Result BindBufferMemory(Buffer buffer); + public Result BindBufferMemory(Buffer buffer, long allocationLocalOffset, IntPtr pNext); + public Result BindBufferMemory(Buffer buffer, long allocationLocalOffset, void* pNext = null); + public Result BindImageMemory(Image image); + public Result BindImageMemory(Image image, long allocationLocalOffset, IntPtr pNext); + public Result BindImageMemory(Image image, long allocationLocalOffset, void* pNext = null); + public bool TouchAllocation(); + public Result Flush(long offset, long size); + public Result Invalidate(long offset, long size); + public abstract IntPtr Map(); + public abstract void Unmap(); + public bool TryGetMemory(out Memory memory) where T : unmanaged; + public bool TryGetSpan(out Span span) where T : unmanaged; + } + public struct AllocationBudget + { + public long BlockBytes; + public long AllocationBytes; + public long Usage; + public long Budget; + public AllocationBudget(long blockBytes, long allocationBytes, long usage, long budget); + } + public struct AllocationContext + { + public int CurrentFrame; + public int FrameInUseCount; + public long BufferImageGranularity; + public long AllocationSize; + public long AllocationAlignment; + public AllocationStrategyFlags Strategy; + public SuballocationType SuballocationType; + public bool CanMakeOtherLost; + public AllocationContext( + int currentFrame, + int framesInUse, + long bufferImageGranularity, + long allocationSize, + long allocationAlignment, + AllocationStrategyFlags strategy, + SuballocationType suballocType, + bool canMakeOtherLost); + } + [Flags] + public enum AllocationCreateFlags + { + DedicatedMemory = 1, + NeverAllocate = 2, + Mapped = 4, + CanBecomeLost = 8, + CanMakeOtherLost = 16, // 0x00000010 + UpperAddress = 64, // 0x00000040 + DontBind = 128, // 0x00000080 + WithinBudget = 256, // 0x00000100 + } + public struct AllocationCreateInfo + { + public AllocationCreateFlags Flags; + public AllocationStrategyFlags Strategy; + public MemoryUsage Usage; + public MemoryPropertyFlags? RequiredFlags; + public MemoryPropertyFlags? PreferredFlags; + public uint MemoryTypeBits; + public VulkanMemoryPool? Pool; + public object? UserData; + public AllocationCreateInfo( + AllocationCreateFlags flags = (AllocationCreateFlags) 0, + AllocationStrategyFlags strategy = (AllocationStrategyFlags) 0, + MemoryUsage usage = MemoryUsage.Unknown, + MemoryPropertyFlags? requiredFlags = 0, + MemoryPropertyFlags? preferredFlags = 0, + uint memoryTypeBits = 0, + VulkanMemoryPool? pool = null, + object? userData = null); + } + public class AllocationException : VulkanResultException + { + public AllocationException(string message); + public AllocationException(string message, Exception? innerException); + public AllocationException(Result res); + public AllocationException(string message, Result res); + } + public struct AllocationPoolCreateInfo + { + public int MemoryTypeIndex; + public PoolCreateFlags Flags; + public long BlockSize; + public int MinBlockCount; + public int MaxBlockCount; + public int FrameInUseCount; + public Func? AllocationAlgorithmCreate; + public AllocationPoolCreateInfo( + int memoryTypeIndex, + PoolCreateFlags flags = (PoolCreateFlags) 0, + long blockSize = 0, + int minBlockCount = 0, + int maxBlockCount = 0, + int frameInUseCount = 0, + Func? allocationAlgorithemCreate = null); + } + public struct AllocationRequest + { + public const long LostAllocationCost = 1048576; + public long Offset; + public long SumFreeSize; + public long SumItemSize; + public long ItemsToMakeLostCount; + public object Item; + public object CustomData; + public AllocationRequestType Type; + public readonly long CalcCost(); + } + public enum AllocationRequestType + { + Normal, + UpperAddress, + EndOfList1, + EndOfList2, + } + [Flags] + public enum AllocationStrategyFlags + { + BestFit = 1, + WorstFit = 2, + FirstFit = 4, + MinMemory = BestFit, // 0x00000001 + MinTime = FirstFit, // 0x00000004 + MinFragmentation = WorstFit, // 0x00000002 + } + [Flags] + public enum AllocatorCreateFlags + { + ExternallySyncronized = 1, + ExtMemoryBudget = 8, + AMDDeviceCoherentMemory = 16, // 0x00000010 + BufferDeviceAddress = 32, // 0x00000020 + } + public sealed class BlockAllocation : Allocation + { + public override DeviceMemory DeviceMemory { get; } + public override long Offset { get; internal set; } + public override IntPtr MappedData { get; } + public override IntPtr Map(); + public override void Unmap(); + } + public class MapMemoryException : VulkanResultException + { + public MapMemoryException(string message); + public MapMemoryException(Result res); + public MapMemoryException(string message, Result res); + } + public enum MemoryUsage + { + Unknown, + GPU_Only, + CPU_Only, + CPU_To_GPU, + GPU_To_CPU, + CPU_Copy, + GPU_LazilyAllocated, + } + [Flags] + public enum PoolCreateFlags + { + IgnoreBufferImageGranularity = 1, + LinearAlgorithm = 16, // 0x00000010 + BuddyAlgorithm = 32, // 0x00000020 + } + public struct PoolStats + { + public long Size; + public long UnusedSize; + public int AllocationCount; + public int UnusedRangeCount; + public long UnusedRangeSizeMax; + public int BlockCount; + } + public struct StatInfo + { + public int BlockCount; + public int AllocationCount; + public int UnusedRangeCount; + public long UsedBytes; + public long UnusedBytes; + public long AllocationSizeMin; + public long AllocationSizeAvg; + public long AllocationSizeMax; + public long UnusedRangeSizeMin; + public long UnusedRangeSizeAvg; + public long UnusedRangeSizeMax; + } + public class Stats + { + public readonly StatInfo[] MemoryType; + public readonly StatInfo[] MemoryHeap; + public StatInfo Total; + } + public enum SuballocationType + { + Free, + Unknown, + Buffer, + Image_Unknown, + Image_Linear, + Image_Optimal, + } + public class ValidationFailedException : ApplicationException + { + } + public sealed class VulkanMemoryAllocator : IDisposable + { + public Device Device { get; } + public VulkanMemoryAllocator(in VulkanMemoryAllocatorCreateInfo createInfo); + public int CurrentFrameIndex { get; set; } + public void Dispose(); + public ref readonly Silk.NET.Vulkan.PhysicalDeviceProperties PhysicalDeviceProperties { get; } + public ref readonly PhysicalDeviceMemoryProperties MemoryProperties { get; } + public MemoryPropertyFlags GetMemoryTypeProperties(int memoryTypeIndex); + public int? FindMemoryTypeIndex(uint memoryTypeBits, in AllocationCreateInfo allocInfo); + public int? FindMemoryTypeIndexForBufferInfo( + in BufferCreateInfo bufferInfo, + in AllocationCreateInfo allocInfo); + public int? FindMemoryTypeIndexForImageInfo( + in ImageCreateInfo imageInfo, + in AllocationCreateInfo allocInfo); + public Allocation AllocateMemory( + in MemoryRequirements requirements, + in AllocationCreateInfo createInfo); + public Allocation AllocateMemoryForBuffer( + Buffer buffer, + in AllocationCreateInfo createInfo, + bool BindToBuffer = false); + public Allocation AllocateMemoryForImage( + Image image, + in AllocationCreateInfo createInfo, + bool BindToImage = false); + public Result CheckCorruption(uint memoryTypeBits); + public Buffer CreateBuffer( + in BufferCreateInfo bufferInfo, + in AllocationCreateInfo allocInfo, + out Allocation allocation); + public Image CreateImage( + in ImageCreateInfo imageInfo, + in AllocationCreateInfo allocInfo, + out Allocation allocation); + public void FreeMemory(Allocation allocation); + public Stats CalculateStats(); + public VulkanMemoryPool CreatePool(in AllocationPoolCreateInfo createInfo); + } + public sealed class VulkanMemoryPool : IDisposable + { + public VulkanMemoryAllocator Allocator { get; } + public string Name { get; set; } + public void Dispose(); + public int MakeAllocationsLost(); + public Result CheckForCorruption(); + public void GetPoolStats(out PoolStats stats); + } + public class VulkanResultException : ApplicationException + { + public readonly Silk.NET.Vulkan.Result? Result; + public VulkanResultException(string message); + public VulkanResultException(string message, Exception? innerException); + public VulkanResultException(Silk.NET.Vulkan.Result res); + public VulkanResultException(string message, Silk.NET.Vulkan.Result res); + } +} +``` From db0c0c87a4f37edbc1c23e2aa7f95b88010128d2 Mon Sep 17 00:00:00 2001 From: Dylan Perks <11160611+Perksey@users.noreply.github.com> Date: Sun, 29 Aug 2021 16:51:19 +0100 Subject: [PATCH 2/4] Update VMASharp_API_For_Review.md --- src/Vulkan/VMASharp_API_For_Review.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Vulkan/VMASharp_API_For_Review.md b/src/Vulkan/VMASharp_API_For_Review.md index c44c8cbda2..8645f9c847 100644 --- a/src/Vulkan/VMASharp_API_For_Review.md +++ b/src/Vulkan/VMASharp_API_For_Review.md @@ -17,7 +17,7 @@ namespace VMASharp.Metadata int frameInUseCount, ref AllocationRequest request); int MakeAllocationsLost(int currentFrame, int frameInUseCount); - void CheckCorruption([NativeInteger] UIntPtr blockDataPointer); + void CheckCorruption(nuint blockDataPointer); void Alloc( in AllocationRequest request, SuballocationType type, From 7c72e278f10b33363408bf5be19d7a9142f263ae Mon Sep 17 00:00:00 2001 From: Dylan Perks <11160611+Perksey@users.noreply.github.com> Date: Sun, 3 Oct 2021 18:26:10 +0100 Subject: [PATCH 3/4] Update VMASharp_API_For_Review.md --- src/Vulkan/VMASharp_API_For_Review.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Vulkan/VMASharp_API_For_Review.md b/src/Vulkan/VMASharp_API_For_Review.md index 8645f9c847..2999732e3f 100644 --- a/src/Vulkan/VMASharp_API_For_Review.md +++ b/src/Vulkan/VMASharp_API_For_Review.md @@ -152,9 +152,7 @@ namespace VMASharp public enum AllocationRequestType { Normal, - UpperAddress, - EndOfList1, - EndOfList2, + UpperAddress } [Flags] public enum AllocationStrategyFlags From 4059d32c45a2e459e6d1f268cd5a6bb0aeaaa0f8 Mon Sep 17 00:00:00 2001 From: Beyley Thomas Date: Sat, 28 Jan 2023 13:30:31 -0800 Subject: [PATCH 4/4] Move VMASharp proposed API over to nint (#1260) --- src/Vulkan/VMASharp_API_For_Review.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Vulkan/VMASharp_API_For_Review.md b/src/Vulkan/VMASharp_API_For_Review.md index 2999732e3f..7f64fc47e2 100644 --- a/src/Vulkan/VMASharp_API_For_Review.md +++ b/src/Vulkan/VMASharp_API_For_Review.md @@ -36,18 +36,18 @@ namespace VMASharp public abstract DeviceMemory DeviceMemory { get; } public abstract long Offset { get; internal set; } public object? UserData { get; set; } - public abstract IntPtr MappedData { get; } + public abstract nint MappedData { get; } public void Dispose(); public Result BindBufferMemory(Buffer buffer); - public Result BindBufferMemory(Buffer buffer, long allocationLocalOffset, IntPtr pNext); + public Result BindBufferMemory(Buffer buffer, long allocationLocalOffset, nint pNext); public Result BindBufferMemory(Buffer buffer, long allocationLocalOffset, void* pNext = null); public Result BindImageMemory(Image image); - public Result BindImageMemory(Image image, long allocationLocalOffset, IntPtr pNext); + public Result BindImageMemory(Image image, long allocationLocalOffset, nint pNext); public Result BindImageMemory(Image image, long allocationLocalOffset, void* pNext = null); public bool TouchAllocation(); public Result Flush(long offset, long size); public Result Invalidate(long offset, long size); - public abstract IntPtr Map(); + public abstract nint Map(); public abstract void Unmap(); public bool TryGetMemory(out Memory memory) where T : unmanaged; public bool TryGetSpan(out Span span) where T : unmanaged; @@ -176,8 +176,8 @@ namespace VMASharp { public override DeviceMemory DeviceMemory { get; } public override long Offset { get; internal set; } - public override IntPtr MappedData { get; } - public override IntPtr Map(); + public override nint MappedData { get; } + public override nint Map(); public override void Unmap(); } public class MapMemoryException : VulkanResultException