summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/video_core/vulkan_common/vulkan_memory_allocator.cpp22
-rw-r--r--src/video_core/vulkan_common/vulkan_memory_allocator.h5
2 files changed, 22 insertions, 5 deletions
diff --git a/src/video_core/vulkan_common/vulkan_memory_allocator.cpp b/src/video_core/vulkan_common/vulkan_memory_allocator.cpp
index 5edd06ebc..aa173d19e 100644
--- a/src/video_core/vulkan_common/vulkan_memory_allocator.cpp
+++ b/src/video_core/vulkan_common/vulkan_memory_allocator.cpp
@@ -69,10 +69,10 @@ constexpr VkExportMemoryAllocateInfo EXPORT_ALLOCATE_INFO{
69 69
70class MemoryAllocation { 70class MemoryAllocation {
71public: 71public:
72 explicit MemoryAllocation(vk::DeviceMemory memory_, VkMemoryPropertyFlags properties, 72 explicit MemoryAllocation(MemoryAllocator* const allocator_, vk::DeviceMemory memory_,
73 u64 allocation_size_, u32 type) 73 VkMemoryPropertyFlags properties, u64 allocation_size_, u32 type)
74 : memory{std::move(memory_)}, allocation_size{allocation_size_}, property_flags{properties}, 74 : allocator{allocator_}, memory{std::move(memory_)}, allocation_size{allocation_size_},
75 shifted_memory_type{1U << type} {} 75 property_flags{properties}, shifted_memory_type{1U << type} {}
76 76
77#if defined(_WIN32) || defined(__unix__) 77#if defined(_WIN32) || defined(__unix__)
78 ~MemoryAllocation() { 78 ~MemoryAllocation() {
@@ -106,6 +106,10 @@ public:
106 const auto it = std::ranges::find(commits, begin, &Range::begin); 106 const auto it = std::ranges::find(commits, begin, &Range::begin);
107 ASSERT_MSG(it != commits.end(), "Invalid commit"); 107 ASSERT_MSG(it != commits.end(), "Invalid commit");
108 commits.erase(it); 108 commits.erase(it);
109 if (commits.empty()) {
110 // Do not call any code involving 'this' after this call, the object will be destroyed
111 allocator->ReleaseMemory(this);
112 }
109 } 113 }
110 114
111 [[nodiscard]] std::span<u8> Map() { 115 [[nodiscard]] std::span<u8> Map() {
@@ -171,6 +175,7 @@ private:
171 return candidate; 175 return candidate;
172 } 176 }
173 177
178 MemoryAllocator* const allocator; ///< Parent memory allocation.
174 const vk::DeviceMemory memory; ///< Vulkan memory allocation handler. 179 const vk::DeviceMemory memory; ///< Vulkan memory allocation handler.
175 const u64 allocation_size; ///< Size of this allocation. 180 const u64 allocation_size; ///< Size of this allocation.
176 const VkMemoryPropertyFlags property_flags; ///< Vulkan memory property flags. 181 const VkMemoryPropertyFlags property_flags; ///< Vulkan memory property flags.
@@ -275,10 +280,17 @@ bool MemoryAllocator::TryAllocMemory(VkMemoryPropertyFlags flags, u32 type_mask,
275 return false; 280 return false;
276 } 281 }
277 } 282 }
278 allocations.push_back(std::make_unique<MemoryAllocation>(std::move(memory), flags, size, type)); 283 allocations.push_back(
284 std::make_unique<MemoryAllocation>(this, std::move(memory), flags, size, type));
279 return true; 285 return true;
280} 286}
281 287
288void MemoryAllocator::ReleaseMemory(MemoryAllocation* alloc) {
289 const auto it = std::ranges::find(allocations, alloc, &std::unique_ptr<MemoryAllocation>::get);
290 ASSERT(it != allocations.end());
291 allocations.erase(it);
292}
293
282std::optional<MemoryCommit> MemoryAllocator::TryCommit(const VkMemoryRequirements& requirements, 294std::optional<MemoryCommit> MemoryAllocator::TryCommit(const VkMemoryRequirements& requirements,
283 VkMemoryPropertyFlags flags) { 295 VkMemoryPropertyFlags flags) {
284 for (auto& allocation : allocations) { 296 for (auto& allocation : allocations) {
diff --git a/src/video_core/vulkan_common/vulkan_memory_allocator.h b/src/video_core/vulkan_common/vulkan_memory_allocator.h
index db12d02f4..b61e931e0 100644
--- a/src/video_core/vulkan_common/vulkan_memory_allocator.h
+++ b/src/video_core/vulkan_common/vulkan_memory_allocator.h
@@ -69,6 +69,8 @@ private:
69/// Memory allocator container. 69/// Memory allocator container.
70/// Allocates and releases memory allocations on demand. 70/// Allocates and releases memory allocations on demand.
71class MemoryAllocator { 71class MemoryAllocator {
72 friend MemoryAllocation;
73
72public: 74public:
73 /** 75 /**
74 * Construct memory allocator 76 * Construct memory allocator
@@ -104,6 +106,9 @@ private:
104 /// Tries to allocate a chunk of memory. 106 /// Tries to allocate a chunk of memory.
105 bool TryAllocMemory(VkMemoryPropertyFlags flags, u32 type_mask, u64 size); 107 bool TryAllocMemory(VkMemoryPropertyFlags flags, u32 type_mask, u64 size);
106 108
109 /// Releases a chunk of memory.
110 void ReleaseMemory(MemoryAllocation* alloc);
111
107 /// Tries to allocate a memory commit. 112 /// Tries to allocate a memory commit.
108 std::optional<MemoryCommit> TryCommit(const VkMemoryRequirements& requirements, 113 std::optional<MemoryCommit> TryCommit(const VkMemoryRequirements& requirements,
109 VkMemoryPropertyFlags flags); 114 VkMemoryPropertyFlags flags);