60 for (Page& page : pages_) destroy(page);
72 if (!device_ || stride == 0 || vertexBytes.empty() || indices.empty())
return false;
75 if (!device_->isDeviceUsable())
return false;
76 const u64 vertexCount = vertexBytes.size() / stride;
77 const u64 indexBytes =
static_cast<u64>(indices.size()) *
sizeof(
u32);
79 const u64 generation = device_->deviceGeneration();
82 u32 firstVertex = 0, firstIndex = 0;
85 for (
u32 i = 0; i < pages_.size() && !found; ++i) {
86 Page& page = pages_[i];
87 if (!page.alive || page.generation != generation || page.stride != stride || !
sameLayout(page.layout, layout))
continue;
88 largest = std::max(largest, page.vertexBytes);
90 if (!page.vertices.take(
static_cast<u32>(vertexCount), v))
continue;
91 if (!page.indices.take(
static_cast<u32>(indices.size()), x)) { page.vertices.give(v,
static_cast<u32>(vertexCount));
continue; }
99 while (bytes < vertexBytes.size() * 2 || bytes < indexBytes * 2) bytes *= 2;
100 if (!makePage(layout, content, stride, bytes, generation, pageIndex))
return false;
101 Page& page = pages_[pageIndex];
102 if (!page.vertices.take(
static_cast<u32>(vertexCount), firstVertex)
103 || !page.indices.take(
static_cast<u32>(indices.size()), firstIndex)) {
108 Page& page = pages_[pageIndex];
109 device_->updateBuffer(page.vb, firstVertex * stride, vertexBytes.data(),
static_cast<u32>(vertexBytes.size()));
110 rebased_.resize(indices.size());
111 for (
usize i = 0; i < indices.size(); ++i) rebased_[i] = indices[i] + firstVertex;
112 device_->updateBuffer(page.ib, firstIndex *
static_cast<u32>(
sizeof(
u32)), rebased_.data(),
113 static_cast<u32>(indexBytes));
116 slot.
page = pageIndex + 1;
123 binding.
layout = page.layoutHandle;
129 if (slot.
page == 0 || slot.
page > pages_.size())
return;
130 Page& page = pages_[slot.
page - 1];
131 if (!page.alive)
return;
134 if (page.live > 0) --page.live;
135 if (page.live == 0) destroy(page);
141 for (
const Page& page : pages_) n += page.alive ? 1u : 0u;
146 for (
const Page& page : pages_)
if (page.alive) n +=
static_cast<u64>(page.vertexBytes) + page.indexBytes;
167 std::vector<std::pair<u32, u32>> ranges;
168 bool take(
u32 n,
u32& at) {
169 for (
usize i = 0; i < ranges.size(); ++i) {
170 if (ranges[i].second < n)
continue;
171 at = ranges[i].first;
172 ranges[i].first += n;
173 ranges[i].second -= n;
174 if (ranges[i].second == 0) ranges.erase(ranges.begin() +
static_cast<std::ptrdiff_t
>(i));
179 void give(
u32 at,
u32 n) {
181 auto it = std::lower_bound(ranges.begin(), ranges.end(), std::make_pair(at, 0u));
182 it = ranges.insert(it, {at, n});
185 if (next != ranges.end() && it->first + it->second == next->first) {
186 it->second += next->second;
189 if (it != ranges.begin()) {
191 if (prev->first + prev->second == it->first) {
192 prev->second += it->second;
200 VertexLayoutDesc layout{};
214 bool makePage(
const VertexLayoutDesc& layout, GfxContent content,
u32 stride,
u32 bytes,
u64 generation,
u32& index) {
216 page.layout = layout;
217 page.stride = stride;
218 page.vertexBytes = (bytes / stride) * stride;
219 page.indexBytes = bytes;
220 page.generation = generation;
223 page.layoutHandle = device_->createVertexLayout(layout);
228 page.vertices.ranges.push_back({0u, page.vertexBytes / stride});
229 page.indices.ranges.push_back({0u, page.indexBytes /
static_cast<u32>(
sizeof(
u32))});
232 for (
u32 i = 0; i < pages_.size(); ++i) {
234 pages_[i] = std::move(page);
239 pages_.push_back(std::move(page));
240 index =
static_cast<u32>(pages_.size() - 1);
244 void destroy(Page& page) {
253 GfxDevice* device_ =
nullptr;
254 std::vector<Page> pages_;
255 std::vector<u32> rebased_;
Who restores a GPU object's contents after the device is lost.
A content policy. provider names who refills a Sourced object and key what it refills; both are the p...
bool place(const VertexLayoutDesc &layout, GfxContent content, ConstSpan< u8 > vertexBytes, u32 stride, ConstSpan< u32 > indices, Slot &slot, Binding &binding)
Places a mesh: its vertices copied into a page of its shape, its indices rebased onto the page's.