diff options
| -rw-r--r-- | src/video_core/vulkan_common/vulkan_memory_allocator.cpp | 129 | ||||
| -rw-r--r-- | src/video_core/vulkan_common/vulkan_memory_allocator.h | 17 |
2 files changed, 91 insertions, 55 deletions
diff --git a/src/video_core/vulkan_common/vulkan_memory_allocator.cpp b/src/video_core/vulkan_common/vulkan_memory_allocator.cpp index 8bb15794d..f15061d0c 100644 --- a/src/video_core/vulkan_common/vulkan_memory_allocator.cpp +++ b/src/video_core/vulkan_common/vulkan_memory_allocator.cpp | |||
| @@ -5,7 +5,6 @@ | |||
| 5 | #include <algorithm> | 5 | #include <algorithm> |
| 6 | #include <bit> | 6 | #include <bit> |
| 7 | #include <optional> | 7 | #include <optional> |
| 8 | #include <tuple> | ||
| 9 | #include <vector> | 8 | #include <vector> |
| 10 | 9 | ||
| 11 | #include "common/alignment.h" | 10 | #include "common/alignment.h" |
| @@ -27,7 +26,7 @@ struct Range { | |||
| 27 | } | 26 | } |
| 28 | }; | 27 | }; |
| 29 | 28 | ||
| 30 | [[nodiscard]] u64 GetAllocationChunkSize(u64 required_size) { | 29 | [[nodiscard]] u64 AllocationChunkSize(u64 required_size) { |
| 31 | static constexpr std::array sizes{ | 30 | static constexpr std::array sizes{ |
| 32 | 0x1000ULL << 10, 0x1400ULL << 10, 0x1800ULL << 10, 0x1c00ULL << 10, 0x2000ULL << 10, | 31 | 0x1000ULL << 10, 0x1400ULL << 10, 0x1800ULL << 10, 0x1c00ULL << 10, 0x2000ULL << 10, |
| 33 | 0x3200ULL << 10, 0x4000ULL << 10, 0x6000ULL << 10, 0x8000ULL << 10, 0xA000ULL << 10, | 32 | 0x3200ULL << 10, 0x4000ULL << 10, 0x6000ULL << 10, 0x8000ULL << 10, 0xA000ULL << 10, |
| @@ -38,14 +37,28 @@ struct Range { | |||
| 38 | const auto it = std::ranges::lower_bound(sizes, required_size); | 37 | const auto it = std::ranges::lower_bound(sizes, required_size); |
| 39 | return it != sizes.end() ? *it : Common::AlignUp(required_size, 4ULL << 20); | 38 | return it != sizes.end() ? *it : Common::AlignUp(required_size, 4ULL << 20); |
| 40 | } | 39 | } |
| 40 | |||
| 41 | [[nodiscard]] VkMemoryPropertyFlags MemoryUsagePropertyFlags(MemoryUsage usage) { | ||
| 42 | switch (usage) { | ||
| 43 | case MemoryUsage::DeviceLocal: | ||
| 44 | return VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT; | ||
| 45 | case MemoryUsage::Upload: | ||
| 46 | return VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT; | ||
| 47 | case MemoryUsage::Download: | ||
| 48 | return VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT | | ||
| 49 | VK_MEMORY_PROPERTY_HOST_CACHED_BIT; | ||
| 50 | } | ||
| 51 | UNREACHABLE_MSG("Invalid memory usage={}", usage); | ||
| 52 | return VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT; | ||
| 53 | } | ||
| 41 | } // Anonymous namespace | 54 | } // Anonymous namespace |
| 42 | 55 | ||
| 43 | class MemoryAllocation { | 56 | class MemoryAllocation { |
| 44 | public: | 57 | public: |
| 45 | explicit MemoryAllocation(const Device& device_, vk::DeviceMemory memory_, | 58 | explicit MemoryAllocation(const Device& device_, vk::DeviceMemory memory_, |
| 46 | VkMemoryPropertyFlags properties_, u64 allocation_size_, u32 type_) | 59 | VkMemoryPropertyFlags properties, u64 allocation_size_, u32 type) |
| 47 | : device{device_}, memory{std::move(memory_)}, properties{properties_}, | 60 | : device{device_}, memory{std::move(memory_)}, allocation_size{allocation_size_}, |
| 48 | allocation_size{allocation_size_}, shifted_type{ShiftType(type_)} {} | 61 | property_flags{properties}, shifted_memory_type{1U << type} {} |
| 49 | 62 | ||
| 50 | [[nodiscard]] std::optional<MemoryCommit> Commit(VkDeviceSize size, VkDeviceSize alignment) { | 63 | [[nodiscard]] std::optional<MemoryCommit> Commit(VkDeviceSize size, VkDeviceSize alignment) { |
| 51 | const std::optional<u64> alloc = FindFreeRegion(size, alignment); | 64 | const std::optional<u64> alloc = FindFreeRegion(size, alignment); |
| @@ -68,17 +81,16 @@ public: | |||
| 68 | } | 81 | } |
| 69 | 82 | ||
| 70 | [[nodiscard]] std::span<u8> Map() { | 83 | [[nodiscard]] std::span<u8> Map() { |
| 71 | if (!memory_mapped_span.empty()) { | 84 | if (memory_mapped_span.empty()) { |
| 72 | return memory_mapped_span; | 85 | u8* const raw_pointer = memory.Map(0, allocation_size); |
| 86 | memory_mapped_span = std::span<u8>(raw_pointer, allocation_size); | ||
| 73 | } | 87 | } |
| 74 | u8* const raw_pointer = memory.Map(0, allocation_size); | ||
| 75 | memory_mapped_span = std::span<u8>(raw_pointer, allocation_size); | ||
| 76 | return memory_mapped_span; | 88 | return memory_mapped_span; |
| 77 | } | 89 | } |
| 78 | 90 | ||
| 79 | /// Returns whether this allocation is compatible with the arguments. | 91 | /// Returns whether this allocation is compatible with the arguments. |
| 80 | [[nodiscard]] bool IsCompatible(VkMemoryPropertyFlags wanted_properties, u32 type_mask) const { | 92 | [[nodiscard]] bool IsCompatible(VkMemoryPropertyFlags flags, u32 type_mask) const { |
| 81 | return (wanted_properties & properties) && (type_mask & shifted_type) != 0; | 93 | return (flags & property_flags) && (type_mask & shifted_memory_type) != 0; |
| 82 | } | 94 | } |
| 83 | 95 | ||
| 84 | private: | 96 | private: |
| @@ -106,13 +118,13 @@ private: | |||
| 106 | return candidate; | 118 | return candidate; |
| 107 | } | 119 | } |
| 108 | 120 | ||
| 109 | const Device& device; ///< Vulkan device. | 121 | const Device& device; ///< Vulkan device. |
| 110 | const vk::DeviceMemory memory; ///< Vulkan memory allocation handler. | 122 | const vk::DeviceMemory memory; ///< Vulkan memory allocation handler. |
| 111 | const VkMemoryPropertyFlags properties; ///< Vulkan properties. | 123 | const u64 allocation_size; ///< Size of this allocation. |
| 112 | const u64 allocation_size; ///< Size of this allocation. | 124 | const VkMemoryPropertyFlags property_flags; ///< Vulkan memory property flags. |
| 113 | const u32 shifted_type; ///< Stored Vulkan type of this allocation, shifted. | 125 | const u32 shifted_memory_type; ///< Shifted Vulkan memory type. |
| 114 | std::vector<Range> commits; ///< All commit ranges done from this allocation. | 126 | std::vector<Range> commits; ///< All commit ranges done from this allocation. |
| 115 | std::span<u8> memory_mapped_span; ///< Memory mapped span. Empty if not queried before. | 127 | std::span<u8> memory_mapped_span; ///< Memory mapped span. Empty if not queried before. |
| 116 | }; | 128 | }; |
| 117 | 129 | ||
| 118 | MemoryCommit::MemoryCommit(const Device& device_, MemoryAllocation* allocation_, | 130 | MemoryCommit::MemoryCommit(const Device& device_, MemoryAllocation* allocation_, |
| @@ -138,10 +150,9 @@ MemoryCommit::MemoryCommit(MemoryCommit&& rhs) noexcept | |||
| 138 | interval{rhs.interval}, span{std::exchange(rhs.span, std::span<u8>{})} {} | 150 | interval{rhs.interval}, span{std::exchange(rhs.span, std::span<u8>{})} {} |
| 139 | 151 | ||
| 140 | std::span<u8> MemoryCommit::Map() { | 152 | std::span<u8> MemoryCommit::Map() { |
| 141 | if (!span.empty()) { | 153 | if (span.empty()) { |
| 142 | return span; | 154 | span = allocation->Map().subspan(interval.first, interval.second - interval.first); |
| 143 | } | 155 | } |
| 144 | span = allocation->Map().subspan(interval.first, interval.second - interval.first); | ||
| 145 | return span; | 156 | return span; |
| 146 | } | 157 | } |
| 147 | 158 | ||
| @@ -157,25 +168,18 @@ MemoryAllocator::MemoryAllocator(const Device& device_) | |||
| 157 | MemoryAllocator::~MemoryAllocator() = default; | 168 | MemoryAllocator::~MemoryAllocator() = default; |
| 158 | 169 | ||
| 159 | MemoryCommit MemoryAllocator::Commit(const VkMemoryRequirements& requirements, MemoryUsage usage) { | 170 | MemoryCommit MemoryAllocator::Commit(const VkMemoryRequirements& requirements, MemoryUsage usage) { |
| 160 | const u64 chunk_size = GetAllocationChunkSize(requirements.size); | 171 | // Find the fastest memory flags we can afford with the current requirements |
| 161 | 172 | const VkMemoryPropertyFlags flags = MemoryPropertyFlags(requirements.memoryTypeBits, usage); | |
| 162 | // When a host visible commit is asked, search for host visible and coherent, otherwise search | 173 | if (std::optional<MemoryCommit> commit = TryCommit(requirements, flags)) { |
| 163 | // for a fast device local type. | ||
| 164 | // TODO: Deduce memory types from usage in a better way | ||
| 165 | const bool host_visible = IsHostVisible(usage); | ||
| 166 | const VkMemoryPropertyFlags wanted_properties = | ||
| 167 | host_visible ? VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT | ||
| 168 | : VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT; | ||
| 169 | if (std::optional<MemoryCommit> commit = TryAllocCommit(requirements, wanted_properties)) { | ||
| 170 | return std::move(*commit); | 174 | return std::move(*commit); |
| 171 | } | 175 | } |
| 172 | // Commit has failed, allocate more memory. | 176 | // Commit has failed, allocate more memory. |
| 173 | // TODO(Rodrigo): Handle out of memory situations in some way like flushing to guest memory. | 177 | // TODO(Rodrigo): Handle out of memory situations in some way like flushing to guest memory. |
| 174 | AllocMemory(wanted_properties, requirements.memoryTypeBits, chunk_size); | 178 | AllocMemory(flags, requirements.memoryTypeBits, AllocationChunkSize(requirements.size)); |
| 175 | 179 | ||
| 176 | // Commit again, this time it won't fail since there's a fresh allocation above. | 180 | // Commit again, this time it won't fail since there's a fresh allocation above. |
| 177 | // If it does, there's a bug. | 181 | // If it does, there's a bug. |
| 178 | return TryAllocCommit(requirements, wanted_properties).value(); | 182 | return TryCommit(requirements, flags).value(); |
| 179 | } | 183 | } |
| 180 | 184 | ||
| 181 | MemoryCommit MemoryAllocator::Commit(const vk::Buffer& buffer, MemoryUsage usage) { | 185 | MemoryCommit MemoryAllocator::Commit(const vk::Buffer& buffer, MemoryUsage usage) { |
| @@ -190,33 +194,22 @@ MemoryCommit MemoryAllocator::Commit(const vk::Image& image, MemoryUsage usage) | |||
| 190 | return commit; | 194 | return commit; |
| 191 | } | 195 | } |
| 192 | 196 | ||
| 193 | void MemoryAllocator::AllocMemory(VkMemoryPropertyFlags wanted_properties, u32 type_mask, | 197 | void MemoryAllocator::AllocMemory(VkMemoryPropertyFlags flags, u32 type_mask, u64 size) { |
| 194 | u64 size) { | 198 | const u32 type = FindType(flags, type_mask).value(); |
| 195 | const u32 type = [&] { | ||
| 196 | for (u32 type_index = 0; type_index < properties.memoryTypeCount; ++type_index) { | ||
| 197 | const auto flags = properties.memoryTypes[type_index].propertyFlags; | ||
| 198 | if ((type_mask & (1U << type_index)) && (flags & wanted_properties)) { | ||
| 199 | // The type matches in type and in the wanted properties. | ||
| 200 | return type_index; | ||
| 201 | } | ||
| 202 | } | ||
| 203 | UNREACHABLE_MSG("Couldn't find a compatible memory type!"); | ||
| 204 | return 0U; | ||
| 205 | }(); | ||
| 206 | vk::DeviceMemory memory = device.GetLogical().AllocateMemory({ | 199 | vk::DeviceMemory memory = device.GetLogical().AllocateMemory({ |
| 207 | .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, | 200 | .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, |
| 208 | .pNext = nullptr, | 201 | .pNext = nullptr, |
| 209 | .allocationSize = size, | 202 | .allocationSize = size, |
| 210 | .memoryTypeIndex = type, | 203 | .memoryTypeIndex = type, |
| 211 | }); | 204 | }); |
| 212 | allocations.push_back(std::make_unique<MemoryAllocation>(device, std::move(memory), | 205 | allocations.push_back( |
| 213 | wanted_properties, size, type)); | 206 | std::make_unique<MemoryAllocation>(device, std::move(memory), flags, size, type)); |
| 214 | } | 207 | } |
| 215 | 208 | ||
| 216 | std::optional<MemoryCommit> MemoryAllocator::TryAllocCommit( | 209 | std::optional<MemoryCommit> MemoryAllocator::TryCommit(const VkMemoryRequirements& requirements, |
| 217 | const VkMemoryRequirements& requirements, VkMemoryPropertyFlags wanted_properties) { | 210 | VkMemoryPropertyFlags flags) { |
| 218 | for (auto& allocation : allocations) { | 211 | for (auto& allocation : allocations) { |
| 219 | if (!allocation->IsCompatible(wanted_properties, requirements.memoryTypeBits)) { | 212 | if (!allocation->IsCompatible(flags, requirements.memoryTypeBits)) { |
| 220 | continue; | 213 | continue; |
| 221 | } | 214 | } |
| 222 | if (auto commit = allocation->Commit(requirements.size, requirements.alignment)) { | 215 | if (auto commit = allocation->Commit(requirements.size, requirements.alignment)) { |
| @@ -226,6 +219,40 @@ std::optional<MemoryCommit> MemoryAllocator::TryAllocCommit( | |||
| 226 | return std::nullopt; | 219 | return std::nullopt; |
| 227 | } | 220 | } |
| 228 | 221 | ||
| 222 | VkMemoryPropertyFlags MemoryAllocator::MemoryPropertyFlags(u32 type_mask, MemoryUsage usage) const { | ||
| 223 | return MemoryPropertyFlags(type_mask, MemoryUsagePropertyFlags(usage)); | ||
| 224 | } | ||
| 225 | |||
| 226 | VkMemoryPropertyFlags MemoryAllocator::MemoryPropertyFlags(u32 type_mask, | ||
| 227 | VkMemoryPropertyFlags flags) const { | ||
| 228 | if (FindType(flags, type_mask)) { | ||
| 229 | // Found a memory type with those requirements | ||
| 230 | return flags; | ||
| 231 | } | ||
| 232 | if (flags & VK_MEMORY_PROPERTY_HOST_CACHED_BIT) { | ||
| 233 | // Remove host cached bit in case it's not supported | ||
| 234 | return MemoryPropertyFlags(type_mask, flags & ~VK_MEMORY_PROPERTY_HOST_CACHED_BIT); | ||
| 235 | } | ||
| 236 | if (flags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) { | ||
| 237 | // Remove device local, if it's not supported by the requested resource | ||
| 238 | return MemoryPropertyFlags(type_mask, flags & ~VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT); | ||
| 239 | } | ||
| 240 | UNREACHABLE_MSG("No compatible memory types found"); | ||
| 241 | return 0; | ||
| 242 | } | ||
| 243 | |||
| 244 | std::optional<u32> MemoryAllocator::FindType(VkMemoryPropertyFlags flags, u32 type_mask) const { | ||
| 245 | for (u32 type_index = 0; type_index < properties.memoryTypeCount; ++type_index) { | ||
| 246 | const VkMemoryPropertyFlags type_flags = properties.memoryTypes[type_index].propertyFlags; | ||
| 247 | if ((type_mask & (1U << type_index)) && (type_flags & flags)) { | ||
| 248 | // The type matches in type and in the wanted properties. | ||
| 249 | return type_index; | ||
| 250 | } | ||
| 251 | } | ||
| 252 | // Failed to find index | ||
| 253 | return std::nullopt; | ||
| 254 | } | ||
| 255 | |||
| 229 | bool IsHostVisible(MemoryUsage usage) noexcept { | 256 | bool IsHostVisible(MemoryUsage usage) noexcept { |
| 230 | switch (usage) { | 257 | switch (usage) { |
| 231 | case MemoryUsage::DeviceLocal: | 258 | case MemoryUsage::DeviceLocal: |
diff --git a/src/video_core/vulkan_common/vulkan_memory_allocator.h b/src/video_core/vulkan_common/vulkan_memory_allocator.h index efb32167a..d4e34c843 100644 --- a/src/video_core/vulkan_common/vulkan_memory_allocator.h +++ b/src/video_core/vulkan_common/vulkan_memory_allocator.h | |||
| @@ -92,13 +92,22 @@ public: | |||
| 92 | 92 | ||
| 93 | private: | 93 | private: |
| 94 | /// Allocates a chunk of memory. | 94 | /// Allocates a chunk of memory. |
| 95 | void AllocMemory(VkMemoryPropertyFlags wanted_properties, u32 type_mask, u64 size); | 95 | void AllocMemory(VkMemoryPropertyFlags flags, u32 type_mask, u64 size); |
| 96 | 96 | ||
| 97 | /// Tries to allocate a memory commit. | 97 | /// Tries to allocate a memory commit. |
| 98 | std::optional<MemoryCommit> TryAllocCommit(const VkMemoryRequirements& requirements, | 98 | std::optional<MemoryCommit> TryCommit(const VkMemoryRequirements& requirements, |
| 99 | VkMemoryPropertyFlags wanted_properties); | 99 | VkMemoryPropertyFlags flags); |
| 100 | 100 | ||
| 101 | const Device& device; ///< Device handler. | 101 | /// Returns the fastest compatible memory property flags from a wanted usage. |
| 102 | VkMemoryPropertyFlags MemoryPropertyFlags(u32 type_mask, MemoryUsage usage) const; | ||
| 103 | |||
| 104 | /// Returns the fastest compatible memory property flags from the wanted flags. | ||
| 105 | VkMemoryPropertyFlags MemoryPropertyFlags(u32 type_mask, VkMemoryPropertyFlags flags) const; | ||
| 106 | |||
| 107 | /// Returns index to the fastest memory type compatible with the passed requirements. | ||
| 108 | std::optional<u32> FindType(VkMemoryPropertyFlags flags, u32 type_mask) const; | ||
| 109 | |||
| 110 | const Device& device; ///< Device handle. | ||
| 102 | const VkPhysicalDeviceMemoryProperties properties; ///< Physical device properties. | 111 | const VkPhysicalDeviceMemoryProperties properties; ///< Physical device properties. |
| 103 | std::vector<std::unique_ptr<MemoryAllocation>> allocations; ///< Current allocations. | 112 | std::vector<std::unique_ptr<MemoryAllocation>> allocations; ///< Current allocations. |
| 104 | }; | 113 | }; |