Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
MeshArena.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
20#pragma once
21
22#include "../core/Types.hpp"
26
27#include <algorithm>
28#include <cstring>
29#include <vector>
30
31namespace esengine::resource {
32
33class MeshArena {
34public:
43
50
53 static constexpr u32 kFirstPageBytes = 1u << 20;
54 static constexpr u32 kMaxPageBytes = 32u << 20;
55
56 void init(GfxDevice& device) { device_ = &device; }
57
59 void shutdown() {
60 for (Page& page : pages_) destroy(page);
61 pages_.clear();
62 device_ = nullptr;
63 }
64
70 bool place(const VertexLayoutDesc& layout, GfxContent content, ConstSpan<u8> vertexBytes, u32 stride,
71 ConstSpan<u32> indices, Slot& slot, Binding& binding) {
72 if (!device_ || stride == 0 || vertexBytes.empty() || indices.empty()) return false;
73 // A page is filled by writes into its middle, which a device that is not standing does not keep: what is made while it
74 // is lost gets buffers of its own, made whole with their bytes.
75 if (!device_->isDeviceUsable()) return false;
76 const u64 vertexCount = vertexBytes.size() / stride;
77 const u64 indexBytes = static_cast<u64>(indices.size()) * sizeof(u32);
78 if (vertexBytes.size() > kMaxPageBytes / 2 || indexBytes > kMaxPageBytes / 2) return false;
79 const u64 generation = device_->deviceGeneration();
80
81 u32 pageIndex = 0;
82 u32 firstVertex = 0, firstIndex = 0;
83 bool found = false;
84 u32 largest = 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);
89 u32 v = 0, x = 0;
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; }
92 pageIndex = i;
93 firstVertex = v;
94 firstIndex = x;
95 found = true;
96 }
97 if (!found) {
98 u32 bytes = largest == 0 ? kFirstPageBytes : std::min(largest * 2, kMaxPageBytes);
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)) {
104 return false;
105 }
106 }
107
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));
114 ++page.live;
115
116 slot.page = pageIndex + 1;
117 slot.firstVertex = firstVertex;
118 slot.vertexCount = static_cast<u32>(vertexCount);
119 slot.firstIndex = firstIndex;
120 slot.indexCount = static_cast<u32>(indices.size());
121 binding.vertexBuffer = page.vb;
122 binding.indexBuffer = page.ib;
123 binding.layout = page.layoutHandle;
124 return true;
125 }
126
128 void free(const Slot& slot) {
129 if (slot.page == 0 || slot.page > pages_.size()) return;
130 Page& page = pages_[slot.page - 1];
131 if (!page.alive) return;
132 page.vertices.give(slot.firstVertex, slot.vertexCount);
133 page.indices.give(slot.firstIndex, slot.indexCount);
134 if (page.live > 0) --page.live;
135 if (page.live == 0) destroy(page);
136 }
137
139 u32 pageCount() const {
140 u32 n = 0;
141 for (const Page& page : pages_) n += page.alive ? 1u : 0u;
142 return n;
143 }
144 u64 pageBytes() const {
145 u64 n = 0;
146 for (const Page& page : pages_) if (page.alive) n += static_cast<u64>(page.vertexBytes) + page.indexBytes;
147 return n;
148 }
149
150 static bool sameLayout(const VertexLayoutDesc& a, const VertexLayoutDesc& b) {
151 if (a.attributeCount != b.attributeCount) return false;
152 for (u32 i = 0; i < a.attributeCount; ++i) {
153 const GfxVertexAttribute& x = a.attributes[i];
154 const GfxVertexAttribute& y = b.attributes[i];
155 if (x.location != y.location || x.components != y.components || x.type != y.type
156 || x.normalized != y.normalized || x.offset != y.offset || x.bufferSlot != y.bufferSlot) return false;
157 }
158 for (u32 s = 0; s < MAX_VERTEX_BUFFER_SLOTS; ++s) {
159 if (a.strides[s] != b.strides[s] || a.instanceStep[s] != b.instanceStep[s]) return false;
160 }
161 return true;
162 }
163
164private:
166 struct FreeList {
167 std::vector<std::pair<u32, u32>> ranges; // at, size
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));
175 return true;
176 }
177 return false;
178 }
179 void give(u32 at, u32 n) {
180 if (n == 0) return;
181 auto it = std::lower_bound(ranges.begin(), ranges.end(), std::make_pair(at, 0u));
182 it = ranges.insert(it, {at, n});
183 // Merge with the next, then with the previous.
184 auto next = it + 1;
185 if (next != ranges.end() && it->first + it->second == next->first) {
186 it->second += next->second;
187 ranges.erase(next);
188 }
189 if (it != ranges.begin()) {
190 auto prev = it - 1;
191 if (prev->first + prev->second == it->first) {
192 prev->second += it->second;
193 ranges.erase(it);
194 }
195 }
196 }
197 };
198
199 struct Page {
200 VertexLayoutDesc layout{};
204 u32 stride = 0;
205 u32 vertexBytes = 0;
206 u32 indexBytes = 0;
207 FreeList vertices;
208 FreeList indices;
209 u64 generation = 0;
210 u32 live = 0;
211 bool alive = false;
212 };
213
214 bool makePage(const VertexLayoutDesc& layout, GfxContent content, u32 stride, u32 bytes, u64 generation, u32& index) {
215 Page page;
216 page.layout = layout;
217 page.stride = stride;
218 page.vertexBytes = (bytes / stride) * stride;
219 page.indexBytes = bytes;
220 page.generation = generation;
221 page.vb = device_->createBuffer({GfxBufferUsage::Vertex, page.vertexBytes, false}, content, nullptr);
222 page.ib = device_->createBuffer({GfxBufferUsage::Index, page.indexBytes, false}, content, nullptr);
223 page.layoutHandle = device_->createVertexLayout(layout);
224 if (page.vb == BufferHandle::Invalid || page.ib == BufferHandle::Invalid || page.layoutHandle == VertexLayoutHandle::Invalid) {
225 destroy(page);
226 return false;
227 }
228 page.vertices.ranges.push_back({0u, page.vertexBytes / stride});
229 page.indices.ranges.push_back({0u, page.indexBytes / static_cast<u32>(sizeof(u32))});
230 page.alive = true;
231 // A dead page's place is reused rather than grown past: a slot names its page by index.
232 for (u32 i = 0; i < pages_.size(); ++i) {
233 if (!pages_[i].alive && pages_[i].vb == BufferHandle::Invalid) {
234 pages_[i] = std::move(page);
235 index = i;
236 return true;
237 }
238 }
239 pages_.push_back(std::move(page));
240 index = static_cast<u32>(pages_.size() - 1);
241 return true;
242 }
243
244 void destroy(Page& page) {
245 if (device_) {
246 if (page.vb != BufferHandle::Invalid) device_->deleteBuffer(page.vb);
247 if (page.ib != BufferHandle::Invalid) device_->deleteBuffer(page.ib);
248 if (page.layoutHandle != VertexLayoutHandle::Invalid) device_->deleteVertexLayout(page.layoutHandle);
249 }
250 page = Page{};
251 }
252
253 GfxDevice* device_ = nullptr;
254 std::vector<Page> pages_;
255 std::vector<u32> rebased_;
256};
257
258} // namespace esengine::resource
Who restores a GPU object's contents after the device is lost.
Abstract graphics device interface.
Backend-agnostic graphics enumerations.
Core type definitions and utilities for ESEngine.
A content policy. provider names who refills a Sourced object and key what it refills; both are the p...
Abstract graphics device interface.
Definition GfxDevice.hpp:75
static constexpr u32 kFirstPageBytes
Definition MeshArena.hpp:53
void free(const Slot &slot)
void init(GfxDevice &device)
Definition MeshArena.hpp:56
static constexpr u32 kMaxPageBytes
Definition MeshArena.hpp:54
static bool sameLayout(const VertexLayoutDesc &a, const VertexLayoutDesc &b)
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.
Definition MeshArena.hpp:70
std::uint64_t u64
Unsigned 64-bit integer.
Definition Types.hpp:47
std::size_t usize
Size type for indexing and sizes.
Definition Types.hpp:64
std::span< const T > ConstSpan
Definition Types.hpp:242
std::uint32_t u32
Unsigned 32-bit integer.
Definition Types.hpp:45
static constexpr u32 MAX_VERTEX_BUFFER_SLOTS
Definition GfxEnums.hpp:218
u32 offset
Byte offset within one element of its buffer slot.
Definition GfxEnums.hpp:225
Describes how vertex-buffer bytes map to shader attributes.
Definition GfxEnums.hpp:235
GfxVertexAttribute attributes[MAX_VERTEX_ATTRIBUTES]
Definition GfxEnums.hpp:236
u32 strides[MAX_VERTEX_BUFFER_SLOTS]
Definition GfxEnums.hpp:238
bool instanceStep[MAX_VERTEX_BUFFER_SLOTS]
Definition GfxEnums.hpp:239