1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
|
#pragma once
// Memory for textures, handed out as ranges of larger blocks instead of one driver allocation each. Making and freeing
// an allocation is a kernel call that costs tens to hundreds of microseconds, where a range costs a map lookup; a title
// that streams textures makes and frees them all the time. Vulkan also guarantees only 4096 live allocations
// (maxMemoryAllocationCount).
//
// No Vulkan or Windows dependency: the caller allocates and frees the blocks (ImageMemoryPool's two callbacks), so the
// policy is checked on the CPU (test/image_memory_test.cpp). All the images of a block are optimal-tiled, so
// bufferImageGranularity, which separates linear from optimal resources, does not apply.
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <functional>
#include <iterator>
#include <map>
#include <memory>
#include <mutex>
#include <set>
#include <utility>
#include <vector>
namespace leonida {
// The free space of one block: the smallest free piece that holds the request is used (best fit), and a released range
// is merged with the free pieces beside it.
class BlockRanges {
public:
explicit BlockRanges(std::uint64_t size) : size_(size) { insert(0, size); }
// False when no free piece holds `size` bytes at `alignment`. `reused`: the range starts in bytes some earlier range
// of this block held, so it may hold that range's data.
bool allocate(std::uint64_t size, std::uint64_t alignment, std::uint64_t& offset, bool& reused) {
if (!size || !alignment || size > size_) return false;
for (auto it = by_size_.lower_bound({size, 0}); it != by_size_.end(); ++it) {
const auto [piece_size, piece] = *it;
const std::uint64_t start = (piece + alignment - 1) / alignment * alignment;
const std::uint64_t lead = start - piece;
if (lead > piece_size || size > piece_size - lead) continue;
erase(piece, piece_size);
insert(piece, lead);
insert(start + size, piece_size - lead - size);
offset = start;
reused = start < touched_;
touched_ = std::max(touched_, start + size);
in_use_ += size;
return true;
}
return false;
}
// Gives back a range allocate() returned.
void release(std::uint64_t offset, std::uint64_t size) {
in_use_ -= size;
if (const auto next = by_offset_.lower_bound(offset); next != by_offset_.end() && offset + size == next->first) {
const std::uint64_t next_size = next->second;
erase(next->first, next_size);
size += next_size;
}
if (auto previous = by_offset_.lower_bound(offset); previous != by_offset_.begin()) {
previous = std::prev(previous);
const auto [start, previous_size] = *previous;
if (start + previous_size == offset) {
erase(start, previous_size);
offset = start;
size += previous_size;
}
}
insert(offset, size);
}
bool empty() const { return in_use_ == 0; }
std::uint64_t size() const { return size_; }
std::uint64_t in_use() const { return in_use_; }
// The largest free piece, so a full block is passed over without a search.
std::uint64_t largest() const { return by_size_.empty() ? 0 : by_size_.rbegin()->first; }
private:
void insert(std::uint64_t offset, std::uint64_t size) {
if (!size) return;
by_offset_[offset] = size;
by_size_.insert({size, offset});
}
void erase(std::uint64_t offset, std::uint64_t size) {
by_offset_.erase(offset);
by_size_.erase({size, offset});
}
std::uint64_t size_;
std::uint64_t in_use_ = 0;
std::uint64_t touched_ = 0; // bytes below this were handed out before
std::map<std::uint64_t, std::uint64_t> by_offset_; // offset -> size
std::set<std::pair<std::uint64_t, std::uint64_t>> by_size_; // (size, offset)
};
// Blocks of device memory, by memory type, split into ranges. Thread-safe. Every Range must be gone before the pool is.
template<typename Memory>
class ImageMemoryPool {
public:
// A null Memory{}: the driver has no room in the type.
using AllocateFn = std::function<Memory(std::uint32_t type, std::uint64_t bytes)>;
using FreeFn = std::function<void(Memory)>;
// One image's range. Destroying it gives the bytes back to the block, so it is destroyed when no submission that
// used the image is still running (DeferredDeletionQueue).
class Range {
public:
Range(const Range&) = delete;
Range& operator=(const Range&) = delete;
~Range() { pool_.release(*this); }
Memory memory() const { return memory_; }
std::uint64_t offset() const { return offset_; }
// The bytes may hold what an earlier range left; the memory of a new block was never given to one.
bool reused() const { return reused_; }
private:
friend class ImageMemoryPool;
Range(ImageMemoryPool& pool, void* block, Memory memory, std::uint64_t offset, std::uint64_t size, bool reused)
: pool_(pool), block_(block), memory_(memory), offset_(offset), size_(size), reused_(reused) {}
ImageMemoryPool& pool_;
void* block_;
Memory memory_;
std::uint64_t offset_;
std::uint64_t size_;
bool reused_;
};
struct Stats {
std::uint64_t blocks = 0; // live driver allocations
std::uint64_t block_bytes = 0; // their size
std::uint64_t ranges = 0; // live ranges in them
};
ImageMemoryPool(AllocateFn allocate, FreeFn free) : allocate_(std::move(allocate)), free_(std::move(free)) {}
ImageMemoryPool(const ImageMemoryPool&) = delete;
ImageMemoryPool& operator=(const ImageMemoryPool&) = delete;
~ImageMemoryPool() {
for (auto& [type, pool] : types_)
for (const auto& block : pool.blocks) free_(block->memory);
}
// A range of a block of `type`, in a new block of `block_bytes` when no block has room. Null when the driver has no
// room for that block; the caller then makes its own allocation.
std::unique_ptr<Range> allocate(std::uint32_t type, std::uint64_t size, std::uint64_t alignment,
std::uint64_t block_bytes) {
std::lock_guard lock(mutex_);
Type& pool = types_[type];
std::uint64_t offset = 0;
bool reused = false;
for (const auto& block : pool.blocks) {
if (block->ranges.largest() < size || !block->ranges.allocate(size, alignment, offset, reused)) continue;
++ranges_;
return std::unique_ptr<Range>(new Range(*this, block.get(), block->memory, offset, size, reused));
}
// A failed driver allocation is slow, and a full heap stays full until something is given back: do not ask for
// another block at every texture until a range is released, or that many textures later.
if (size > block_bytes || (pool.skip && pool.skip--)) return nullptr;
const Memory memory = allocate_(type, block_bytes);
if (!memory) {
pool.skip = kRetryAfter;
return nullptr;
}
auto block = std::make_unique<Block>(type, memory, block_bytes);
if (!block->ranges.allocate(size, alignment, offset, reused)) {
free_(memory);
return nullptr;
}
++ranges_;
pool.blocks.push_back(std::move(block));
return std::unique_ptr<Range>(new Range(*this, pool.blocks.back().get(), memory, offset, size, reused));
}
Stats stats() const {
std::lock_guard lock(mutex_);
Stats result;
result.ranges = ranges_;
for (const auto& [type, pool] : types_)
for (const auto& block : pool.blocks) {
++result.blocks;
result.block_bytes += block->ranges.size();
}
return result;
}
private:
static constexpr std::uint32_t kRetryAfter = 64;
struct Block {
Block(std::uint32_t t, Memory m, std::uint64_t bytes) : type(t), memory(m), ranges(bytes) {}
std::uint32_t type;
Memory memory;
BlockRanges ranges;
};
struct Type {
std::vector<std::unique_ptr<Block>> blocks;
std::uint32_t skip = 0;
};
void release(const Range& range) {
Memory dead{};
bool free_block = false;
{
std::lock_guard lock(mutex_);
Block& block = *static_cast<Block*>(range.block_);
Type& pool = types_[block.type];
block.ranges.release(range.offset_, range.size_);
pool.skip = 0;
--ranges_;
// One empty block is kept, so a title that frees and makes the same textures does not ask the driver each
// time.
if (block.ranges.empty() && std::count_if(pool.blocks.begin(), pool.blocks.end(),
[](const auto& b) { return b->ranges.empty(); }) > 1) {
dead = block.memory;
free_block = true;
std::erase_if(pool.blocks, [&](const auto& b) { return b.get() == █ });
}
}
if (free_block) free_(dead);
}
mutable std::mutex mutex_;
AllocateFn allocate_;
FreeFn free_;
std::map<std::uint32_t, Type> types_;
std::uint64_t ranges_ = 0;
};
} // namespace leonida
|