Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/Common/GPU/Vulkan/VulkanMemory.cpp
3187 views
1
// Copyright (c) 2016- PPSSPP Project.
2
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
6
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
11
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
14
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17
18
// Additionally, Common/Vulkan/* , including this file, are also licensed
19
// under the public domain.
20
21
#include <algorithm>
22
#include "Common/Math/math_util.h"
23
24
#include "Common/Log.h"
25
#include "Common/TimeUtil.h"
26
#include "Common/GPU/Vulkan/VulkanMemory.h"
27
#include "Common/Data/Text/Parsers.h"
28
29
using namespace PPSSPP_VK;
30
31
// Always keep around push buffers at least this long (seconds).
32
static const double PUSH_GARBAGE_COLLECTION_DELAY = 10.0;
33
34
VulkanPushPool::VulkanPushPool(VulkanContext *vulkan, const char *name, size_t originalBlockSize, VkBufferUsageFlags usage)
35
: vulkan_(vulkan), name_(name), originalBlockSize_(originalBlockSize), usage_(usage) {
36
RegisterGPUMemoryManager(this);
37
38
for (int i = 0; i < VulkanContext::MAX_INFLIGHT_FRAMES; i++) {
39
blocks_.push_back(CreateBlock(originalBlockSize));
40
blocks_.back().original = true;
41
blocks_.back().frameIndex = i;
42
}
43
}
44
45
VulkanPushPool::~VulkanPushPool() {
46
UnregisterGPUMemoryManager(this);
47
_dbg_assert_(blocks_.empty());
48
}
49
50
void VulkanPushPool::Destroy() {
51
for (auto &block : blocks_) {
52
block.Destroy(vulkan_);
53
}
54
blocks_.clear();
55
}
56
57
VulkanPushPool::Block VulkanPushPool::CreateBlock(size_t size) {
58
Block block{};
59
block.size = size;
60
block.frameIndex = -1;
61
62
VkBufferCreateInfo b{ VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO };
63
b.size = size;
64
b.usage = usage_;
65
b.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
66
VmaAllocationCreateInfo allocCreateInfo{};
67
68
allocCreateInfo.usage = VMA_MEMORY_USAGE_CPU_TO_GPU;
69
allocCreateInfo.requiredFlags = VK_MEMORY_PROPERTY_HOST_COHERENT_BIT; // required to not get memory where we have to manually flush.
70
VmaAllocationInfo allocInfo{};
71
72
VkResult result = vmaCreateBuffer(vulkan_->Allocator(), &b, &allocCreateInfo, &block.buffer, &block.allocation, &allocInfo);
73
74
_assert_msg_(result == VK_SUCCESS, "VulkanPushPool: Failed to create buffer (result = %s, size = %d)", VulkanResultToString(result), (int)size);
75
76
result = vmaMapMemory(vulkan_->Allocator(), block.allocation, (void **)(&block.writePtr));
77
78
_assert_msg_(result == VK_SUCCESS, "VulkanPushPool: Failed to map memory (result = %s, size = %d)", VulkanResultToString(result), (int)size);
79
_assert_msg_(block.writePtr != nullptr, "VulkanPushPool: Failed to map memory on block of size %d", (int)block.size);
80
return block;
81
}
82
83
VulkanPushPool::Block::~Block() {}
84
85
void VulkanPushPool::Block::Destroy(VulkanContext *vulkan) {
86
vmaUnmapMemory(vulkan->Allocator(), allocation);
87
vulkan->Delete().QueueDeleteBufferAllocation(buffer, allocation);
88
}
89
90
void VulkanPushPool::BeginFrame() {
91
double now = time_now_d();
92
curBlockIndex_ = -1;
93
for (auto &block : blocks_) {
94
if (block.frameIndex == vulkan_->GetCurFrame()) {
95
if (curBlockIndex_ == -1) {
96
// Pick a block associated with the current frame to start at.
97
// We always start with one block per frame index.
98
curBlockIndex_ = block.frameIndex;
99
block.lastUsed = now;
100
}
101
block.used = 0;
102
if (!block.original) {
103
// Return block to the common pool
104
block.frameIndex = -1;
105
}
106
}
107
}
108
109
// Do a single pass of bubblesort to move the bigger buffers earlier in the sequence.
110
// Over multiple frames this will quickly converge to the right order.
111
for (size_t i = 3; i < blocks_.size() - 1; i++) {
112
if (blocks_[i].frameIndex == -1 && blocks_[i + 1].frameIndex == -1 && blocks_[i].size < blocks_[i + 1].size) {
113
std::swap(blocks_[i], blocks_[i + 1]);
114
}
115
}
116
117
// If we have lots of little buffers and the last one hasn't been used in a while, drop it.
118
// Still, let's keep around a few big ones (6 - 3).
119
if (blocks_.size() > 6 && blocks_.back().lastUsed < now - PUSH_GARBAGE_COLLECTION_DELAY) {
120
double start = time_now_d();
121
size_t size = blocks_.back().size;
122
blocks_.back().Destroy(vulkan_);
123
blocks_.pop_back();
124
DEBUG_LOG(Log::G3D, "%s: Garbage collected block of size %s in %0.2f ms", name_, NiceSizeFormat(size).c_str(), time_now_d() - start);
125
}
126
}
127
128
void VulkanPushPool::NextBlock(VkDeviceSize allocationSize) {
129
_dbg_assert_(allocationSize != 0); // If so, the logic in the caller is wrong, should never need a new block for this case.
130
131
int curFrameIndex = vulkan_->GetCurFrame();
132
curBlockIndex_++;
133
while (curBlockIndex_ < blocks_.size()) {
134
Block &block = blocks_[curBlockIndex_];
135
// Grab the first matching block, or unused block (frameIndex == -1).
136
if ((block.frameIndex == curFrameIndex || block.frameIndex == -1) && block.size >= allocationSize) {
137
_assert_(block.used == 0);
138
block.used = allocationSize;
139
block.lastUsed = time_now_d();
140
block.frameIndex = curFrameIndex;
141
_assert_(block.writePtr != nullptr);
142
return;
143
}
144
curBlockIndex_++;
145
}
146
147
double start = time_now_d();
148
VkDeviceSize newBlockSize = std::max(originalBlockSize_ * 2, (VkDeviceSize)RoundUpToPowerOf2((uint32_t)allocationSize));
149
150
// We're still here and ran off the end of blocks. Create a new one.
151
blocks_.push_back(CreateBlock(newBlockSize));
152
blocks_.back().frameIndex = curFrameIndex;
153
blocks_.back().used = allocationSize;
154
blocks_.back().lastUsed = time_now_d();
155
// curBlockIndex_ is already set correctly here.
156
DEBUG_LOG(Log::G3D, "%s: Created new block of size %s in %0.2f ms", name_, NiceSizeFormat(newBlockSize).c_str(), 1000.0 * (time_now_d() - start));
157
}
158
159
size_t VulkanPushPool::GetUsedThisFrame() const {
160
size_t used = 0;
161
for (auto &block : blocks_) {
162
if (block.frameIndex == vulkan_->GetCurFrame()) {
163
used += block.used;
164
}
165
}
166
return used;
167
}
168
169
void VulkanPushPool::GetDebugString(char *buffer, size_t bufSize) const {
170
size_t used = 0;
171
size_t capacity = 0;
172
for (auto &block : blocks_) {
173
used += block.used;
174
capacity += block.size;
175
}
176
177
snprintf(buffer, bufSize, "Pool %s: %s / %s (%d extra blocks)", name_, NiceSizeFormat(used).c_str(), NiceSizeFormat(capacity).c_str(), (int)blocks_.size() - 3);
178
}
179
180