A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
perf(render): meshes of one vertex shape share a vertex buffer, an index buffer and a layout - each mesh had its own, so every draw of a scene switched vertex array and pipeline; replayable meshes take ranges of shared pages (MeshArena) with their indices rebased, and a run of them switches nothing
yhh680cb3d5e491
diff --git a/.github/workflows/build.yml b/.github/workflows/build.ymlindex 89b9d3a28..fd4452aa9 100644--- a/.github/workflows/build.yml+++ b/.github/workflows/build.yml@@ -39,6 +39,7 @@ env:test_touch_streamtest_frame_lifecycletest_particle_stretch test_particle_speed_limit test_particle_bursts+ test_record_tablejobs:playable-runtime:diff --git a/CHANGELOG.md b/CHANGELOG.mdindex c161aa0c2..4deebc24b 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -41,6 +41,7 @@ published separately; it ships inside the editor.- **A model's report is its steps as written, and names a step that undoes a decimate.** `report.steps` of a baked model listed every plain step its repeats came to, by an index among those: a spruce of thirteen rows of branches reported a hundred and thirty-eight steps, most the same line, and none under the index `model_edit` takes. It is now one entry for each step as written, by that index, a repeat counted once with how many steps it `made`, and each entry says the `triangles` the mesh had after it. And a step that splits faces to carry its own detail (a stain by a pattern, a displace, a sculpt) after a `decimate` took the decimate's triangles back in silence: a rock thinned to 1100 came out at 214,534, under the 300,000 the report calls heavy, and a field of them would have been gigabytes. The report now warns of each such step, with what it was given, what it left and which decimate it undid, and says how to keep the detail (before the decimate, carried by `normalBake`) or bound it (`detail`). The test also notes what the advice costs: a pattern stained before a decimate leaves colour seams the decimate will not cross, so it is `normalBake`'s `color: true` that carries it.- **A playable's start-screen bar moves through the whole boot.** The bar's stages were weighed for a web page, most of whose start is fetching the engine. A playable has the engine inlined, so its bar stood at 82% within a second and crawled to 95% over the twenty-five that followed. A playable now weighs its own stages (the scene's is four fifths of the bar), and that stage is followed in three stretches: assets being read and made (eased by the clock, since nothing counts them), shaders compiled against the most seen compiling, and the world prepared against the most its makers said was left. For that, a hold given to `holdShaderWarmup` may answer a number (pages, cells) where it answered yes, and `warmShaders` passes the sum to `onProgress`; Terrain and Foliage count theirs. One lake scene's bar went from 18% to 95% over its 22 seconds, with one stretch of four seconds standing still. `attachSplash` takes the weights.- **An effect chain keeps its uniform buffers from frame to frame.** A camera's post-process chain is said again every frame, and each of its passes made a uniform buffer for its parameters and deleted it when the chain was cleared: seventeen buffers created and seventeen deleted every frame for a lake scene with a volume's usual effects, counted in the GL calls of an exported page. The buffers of a cleared chain are now taken over by the passes of the next chain that need one the same size, and one that nothing has taken in eight clears is deleted. The same scene creates none in a steady frame.+- **Meshes of one vertex shape share their buffers.** Every mesh was given a vertex buffer, an index buffer and a vertex layout of its own, and a layout is part of a pipeline's key: a scene of five hundred meshes switched vertex arrays five hundred times a frame, and pipelines with them, though most of its meshes had the same few shapes of vertex. Meshes loaded from an asset now take ranges of shared pages, one page set per vertex shape (pages start at 1 MB and double, so a small game pays for little), and a run of them is drawn without switching vertex array, buffers or pipeline. Indices are rebased onto the page when a mesh is placed, since WebGL2 has no base vertex; a mesh with morph targets (whose deltas are found by vertex number) and a mesh built in the running game keep buffers of their own. After a lost device a page is rebuilt from its meshes' sources, each into a page of the new device. On Bistro at 640x360 the vertex-array binds a frame went from 540 to 55 and the WebGL calls from 3,627 to 3,102, and the renderer's CPU time from 1.6-1.8 ms to 1.2 ms in three runs each (the frame, which something else bounds at this size, stayed at 6.0 ms). `ResourceManager::meshPageCount` and `meshPageBytes` say what the pages hold.- **A shadow map is drawn again only when what it holds changed.** Every shadow-casting mesh was drawn into the shadow atlas every frame for every light, though in most frames nothing in a map had moved: Bistro drew 891 casters a frame for a sun and a scene that were both still. The atlas is now kept from frame to frame, and the frame compares what its squares would draw (where each looks from, the casters, their transforms, bones and shapes, the materials of cut-out casters) with what was last drawn into them, and skips the pass when they are the same. A caster that moves, a light that turns, a camera that walks the sun's cascades on to the next texel, a lost device or geometry rewritten in place draw it again. A material that moves its vertices (wind, a ground's height) is assumed to move them every frame, so a map it casts into is always drawn. On Bistro at 640x360 the WebGL calls a frame went from 7,139 to 3,627 and the renderer's CPU time from 3.3 to 2.3 ms. `render.shadow.reused` says whether a frame drew the atlas.- **Walking a frame's meshes costs less, and the profiler says what it costs.** Every mesh, in every view, asked its material's layout several questions by name (does it read the picture behind it, is its alpha coverage, does it cut out a shadow), each a string search; they are answered once when the layout is registered, and the last material and layout asked for are kept. A child of a model (most of an imported scene's meshes) was decomposed afresh by every view of every frame though its matrix had not changed; it is decomposed again only when it moves. And the mesh walk read the clock at every phase of every mesh while profiled, which was four in ten samples of a profiled frame and most of the time it reported; it is timed whole (the split by phase is the build option `ES_PROFILE_COLLECT_PHASES`). On the Bistro scene at 640x360, the best of seven interleaved runs went from 9.97 to 6.83 ms a frame, and a grid of 4096 meshes on one material from 4.08 to 3.09 ms. `render-submit-cost.mjs --wall` times frames with the profiler off.- **Sprite and particle shaders compile in the background too.** With `quality.asyncShaderCompile`, mesh and material shaders were compiled without holding a frame, and the batch shader's variants (sprites, text, lit sprites) and the particle shader were not: each was linked in place when first needed, measured at 350 to 490 ms of one frame mid-play under ANGLE. They are deferred the same way now, their texture units set when the link lands. Without the setting nothing changes.diff --git a/src/esengine/renderer/draw/BatchBuilder.cpp b/src/esengine/renderer/draw/BatchBuilder.cppindex c6b5ab42b..5149f4d0b 100644--- a/src/esengine/renderer/draw/BatchBuilder.cpp+++ b/src/esengine/renderer/draw/BatchBuilder.cpp@@ -102,7 +102,7 @@ void pushBatchDraw(DrawList& drawList, const ClipState& clips,: DrawCommand::buildSortKey(stage, layer, key.shaderId,key.blend, 0, key.depth, key.materialId, order,key.instanceCount != 0 && key.vertexBuffer != BufferHandle::Invalid- ? (static_cast<u32>(key.vertexBuffer) * 2654435761u) >> 24 | 1u+ ? ((static_cast<u32>(key.vertexBuffer) * 2654435761u) ^ (indexOffset * 2246822519u)) >> 24 | 1u: 0u);// Inside a group the member's own order is all that separates siblings. Under a NESTED// group it is the inner group's instead — a block does not come apart around anotherdiff --git a/src/esengine/renderer/plugins/MeshPlugin.cpp b/src/esengine/renderer/plugins/MeshPlugin.cppindex 0467f6238..f307ff2ca 100644--- a/src/esengine/renderer/plugins/MeshPlugin.cpp+++ b/src/esengine/renderer/plugins/MeshPlugin.cpp@@ -1201,7 +1201,7 @@ void MeshPlugin::collect(RenderCollectContext& collect_ctx) {drawn.vertexBuffer = geo.vertexBuffer;drawn.indexBuffer = geo.indexBuffer;drawn.vertexLayout = geo.layout;- pushBatchDraw(draw_list, clips, offset, 0, 0, geo.indexCount, drawn);+ pushBatchDraw(draw_list, clips, offset, 0, geo.firstIndex, geo.indexCount, drawn);if (!depthPass && !wire && key.materialId != 0 && ctx.materials && ctx.materials->hasTipPass(key.materialId)) {const u32 tipProgram = ctx.materials->meshProgram(key.materialId, ctx.resources, geo.hasNormals, skinned,key.envTextureId != 0, geo.hasLightmapUV, false,@@ -1212,7 +1212,7 @@ void MeshPlugin::collect(RenderCollectContext& collect_ctx) {tips.stage = RenderStage::Transparent;tips.blend = BlendMode::Normal;tips.depthWrite = false;- pushBatchDraw(draw_list, clips, offset, 0, 0, geo.indexCount, tips);+ pushBatchDraw(draw_list, clips, offset, 0, geo.firstIndex, geo.indexCount, tips);}}};diff --git a/src/esengine/renderer/rhi/GLDevice.cpp b/src/esengine/renderer/rhi/GLDevice.cppindex d7adc61b8..151274efa 100644--- a/src/esengine/renderer/rhi/GLDevice.cpp+++ b/src/esengine/renderer/rhi/GLDevice.cpp@@ -428,6 +428,7 @@ bool GLDevice::recreateDevice() {query_names_.clear();framebuffer_resolve_.clear();vaos_.clear();+ last_vao_ = nullptr;readbacks_.clear();max_samples_ = 0;// A timer query in flight was begun on the old context, so the timings are@@ -1153,6 +1154,7 @@ void GLDevice::backendDeleteVertexLayout(u32 id) {}glDeleteVertexArrays(1, &it->second.vao);}+ last_vao_ = nullptr;it = vaos_.erase(it);}}@@ -1173,6 +1175,7 @@ void GLDevice::releaseVaosUsing(u32 bufferName) {}glDeleteVertexArrays(1, &it->second.vao);}+ last_vao_ = nullptr;it = vaos_.erase(it);}}@@ -1197,7 +1200,11 @@ void GLDevice::prepareVertexState() {const u32 slot = desc->attributes[a].bufferSlot;if (slot < MAX_VERTEX_BUFFER_SLOTS) key.vbo[slot] = pending_vbo_[slot];}- VaoCache& rec = vaos_[key];+ if (!last_vao_ || !(key == last_vao_key_)) {+ last_vao_ = &vaos_[key];+ last_vao_key_ = key;+ }+ VaoCache& rec = *last_vao_;if (rec.vao == 0) {glGenVertexArrays(1, &rec.vao);diff --git a/src/esengine/renderer/rhi/GLDevice.hpp b/src/esengine/renderer/rhi/GLDevice.hppindex b74785066..51292410f 100644--- a/src/esengine/renderer/rhi/GLDevice.hpp+++ b/src/esengine/renderer/rhi/GLDevice.hpp@@ -320,6 +320,10 @@ private:}};std::unordered_map<VaoKey, VaoCache, VaoKeyHash> vaos_;+ /// The last VAO looked up, and by what: draws one after another through the same buffers (meshes sharing a page) ask for+ /// the same one, and are answered without a hash. Forgotten whenever vaos_ erases.+ VaoKey last_vao_key_{};+ VaoCache* last_vao_ = nullptr;/// Deletes every VAO that reads from a GL buffer name, before the name is deleted and reused.void releaseVaosUsing(u32 bufferName);u32 current_layout_ = 0;diff --git a/src/esengine/resource/Mesh.hpp b/src/esengine/resource/Mesh.hppindex 517f3cf98..1b140e0bd 100644--- a/src/esengine/resource/Mesh.hpp+++ b/src/esengine/resource/Mesh.hpp@@ -61,6 +61,16 @@ public:u32 indexCount = 0;+ /**+ * Where this mesh's first index sits in indexBuffer: past zero when the mesh shares a page with others of its vertex shape+ * (MeshArena), whose buffers and layout it then draws through. Its indices are rebased onto the page's vertices, so a draw+ * reads indexCount of them from here and needs nothing else to find its own.+ */+ u32 firstIndex = 0;+ /** The page and first vertex of that place, to give it back: page 0 is buffers of the mesh's own. */+ u32 arenaPage = 0;+ u32 arenaFirstVertex = 0;+/*** The mesh's own edges as line-list indices into the same vertices, one pair per* distinct edge. Derived from `indices` when the mesh is realized, and only whilediff --git a/src/esengine/resource/MeshArena.hpp b/src/esengine/resource/MeshArena.hppnew file mode 100644index 000000000..0d5b0adda--- /dev/null+++ b/src/esengine/resource/MeshArena.hpp@@ -0,0 +1,258 @@+// SPDX-License-Identifier: Apache-2.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+/**+ * @file MeshArena.hpp+ * @brief Meshes of one vertex shape sharing a vertex buffer, an index buffer and a layout.+ *+ * @details A mesh realized with buffers of its own is drawn with a vertex array of its own, and a scene of five hundred meshes+ * switched vertex arrays five hundred times a frame (and pipelines with them, since a layout is part of a pipeline's+ * key) though most of them have the same few shapes of vertex. Here the meshes of one shape take ranges of the same+ * page: one layout, one vertex buffer, one index buffer, so a run of them is drawn without switching any of the three.+ *+ * Indices are rebased when a mesh is placed (an index names a vertex of the page, not of the mesh): WebGL2 has no+ * base vertex. Which is why a mesh that reads its own vertex number (a morph target's deltas are looked up by+ * gl_VertexID) is not placed here, and keeps buffers of its own.+ *+ * Pages belong to the device generation they were made on. After a loss, every page is owed, so every mesh in it is+ * owed; its source rebuilds it, and the rebuild places it in a page of the new generation. A page whose last mesh has+ * left is deleted.+ */+#pragma once++#include "../core/Types.hpp"+#include "../renderer/rhi/GfxDevice.hpp"+#include "../renderer/rhi/GfxEnums.hpp"+#include "../renderer/rhi/GfxContent.hpp"++#include <algorithm>+#include <cstring>+#include <vector>++namespace esengine::resource {++class MeshArena {+public:+ /** Where a placed mesh is. page is 1 + the page's index; 0 = not placed. */+ struct Slot {+ u32 page = 0;+ u32 firstVertex = 0;+ u32 vertexCount = 0;+ u32 firstIndex = 0;+ u32 indexCount = 0;+ };++ /** What a placed mesh draws through. */+ struct Binding {+ BufferHandle vertexBuffer = BufferHandle::Invalid;+ BufferHandle indexBuffer = BufferHandle::Invalid;+ VertexLayoutHandle layout = VertexLayoutHandle::Invalid;+ };++ /// The first page of a shape holds this much, and each one after it twice the last, up to kMaxPageBytes: a game with one+ /// small model does not pay for a page sized for a city.+ static constexpr u32 kFirstPageBytes = 1u << 20;+ static constexpr u32 kMaxPageBytes = 32u << 20;++ void init(GfxDevice& device) { device_ = &device; }++ /** Deletes every page. The meshes in them are gone already, or are about to be. */+ void shutdown() {+ for (Page& page : pages_) destroy(page);+ pages_.clear();+ device_ = nullptr;+ }++ /**+ * @brief Places a mesh: its vertices copied into a page of its shape, its indices rebased onto the page's.+ * @return false when it is not placed (bigger than a page may be, or the device refused a page); the caller then gives+ * the mesh buffers of its own.+ */+ bool place(const VertexLayoutDesc& layout, GfxContent content, ConstSpan<u8> vertexBytes, u32 stride,+ ConstSpan<u32> indices, Slot& slot, Binding& binding) {+ if (!device_ || stride == 0 || vertexBytes.empty() || indices.empty()) return false;+ // A page is filled by writes into its middle, which a device that is not standing does not keep: what is made while it+ // is lost gets buffers of its own, made whole with their bytes.+ if (!device_->isDeviceUsable()) return false;+ const u64 vertexCount = vertexBytes.size() / stride;+ const u64 indexBytes = static_cast<u64>(indices.size()) * sizeof(u32);+ if (vertexBytes.size() > kMaxPageBytes / 2 || indexBytes > kMaxPageBytes / 2) return false;+ const u64 generation = device_->deviceGeneration();++ u32 pageIndex = 0;+ u32 firstVertex = 0, firstIndex = 0;+ bool found = false;+ u32 largest = 0;+ for (u32 i = 0; i < pages_.size() && !found; ++i) {+ Page& page = pages_[i];+ if (!page.alive || page.generation != generation || page.stride != stride || !sameLayout(page.layout, layout)) continue;+ largest = std::max(largest, page.vertexBytes);+ u32 v = 0, x = 0;+ if (!page.vertices.take(static_cast<u32>(vertexCount), v)) continue;+ if (!page.indices.take(static_cast<u32>(indices.size()), x)) { page.vertices.give(v, static_cast<u32>(vertexCount)); continue; }+ pageIndex = i;+ firstVertex = v;+ firstIndex = x;+ found = true;+ }+ if (!found) {+ u32 bytes = largest == 0 ? kFirstPageBytes : std::min(largest * 2, kMaxPageBytes);+ while (bytes < vertexBytes.size() * 2 || bytes < indexBytes * 2) bytes *= 2;+ if (!makePage(layout, content, stride, bytes, generation, pageIndex)) return false;+ Page& page = pages_[pageIndex];+ if (!page.vertices.take(static_cast<u32>(vertexCount), firstVertex)+ || !page.indices.take(static_cast<u32>(indices.size()), firstIndex)) {+ return false;+ }+ }++ Page& page = pages_[pageIndex];+ device_->updateBuffer(page.vb, firstVertex * stride, vertexBytes.data(), static_cast<u32>(vertexBytes.size()));+ rebased_.resize(indices.size());+ for (usize i = 0; i < indices.size(); ++i) rebased_[i] = indices[i] + firstVertex;+ device_->updateBuffer(page.ib, firstIndex * static_cast<u32>(sizeof(u32)), rebased_.data(),+ static_cast<u32>(indexBytes));+ ++page.live;++ slot.page = pageIndex + 1;+ slot.firstVertex = firstVertex;+ slot.vertexCount = static_cast<u32>(vertexCount);+ slot.firstIndex = firstIndex;+ slot.indexCount = static_cast<u32>(indices.size());+ binding.vertexBuffer = page.vb;+ binding.indexBuffer = page.ib;+ binding.layout = page.layoutHandle;+ return true;+ }++ /** Gives a placed mesh's ranges back; a page left empty is deleted. */+ void free(const Slot& slot) {+ if (slot.page == 0 || slot.page > pages_.size()) return;+ Page& page = pages_[slot.page - 1];+ if (!page.alive) return;+ page.vertices.give(slot.firstVertex, slot.vertexCount);+ page.indices.give(slot.firstIndex, slot.indexCount);+ if (page.live > 0) --page.live;+ if (page.live == 0) destroy(page);+ }++ /** Pages standing, for a profiler: how many and the bytes they hold on the GPU. */+ u32 pageCount() const {+ u32 n = 0;+ for (const Page& page : pages_) n += page.alive ? 1u : 0u;+ return n;+ }+ u64 pageBytes() const {+ u64 n = 0;+ for (const Page& page : pages_) if (page.alive) n += static_cast<u64>(page.vertexBytes) + page.indexBytes;+ return n;+ }++ static bool sameLayout(const VertexLayoutDesc& a, const VertexLayoutDesc& b) {+ if (a.attributeCount != b.attributeCount) return false;+ for (u32 i = 0; i < a.attributeCount; ++i) {+ const GfxVertexAttribute& x = a.attributes[i];+ const GfxVertexAttribute& y = b.attributes[i];+ if (x.location != y.location || x.components != y.components || x.type != y.type+ || x.normalized != y.normalized || x.offset != y.offset || x.bufferSlot != y.bufferSlot) return false;+ }+ for (u32 s = 0; s < MAX_VERTEX_BUFFER_SLOTS; ++s) {+ if (a.strides[s] != b.strides[s] || a.instanceStep[s] != b.instanceStep[s]) return false;+ }+ return true;+ }++private:+ /** First-fit free ranges, in elements, kept sorted and merged. */+ struct FreeList {+ std::vector<std::pair<u32, u32>> ranges; // at, size+ bool take(u32 n, u32& at) {+ for (usize i = 0; i < ranges.size(); ++i) {+ if (ranges[i].second < n) continue;+ at = ranges[i].first;+ ranges[i].first += n;+ ranges[i].second -= n;+ if (ranges[i].second == 0) ranges.erase(ranges.begin() + static_cast<std::ptrdiff_t>(i));+ return true;+ }+ return false;+ }+ void give(u32 at, u32 n) {+ if (n == 0) return;+ auto it = std::lower_bound(ranges.begin(), ranges.end(), std::make_pair(at, 0u));+ it = ranges.insert(it, {at, n});+ // Merge with the next, then with the previous.+ auto next = it + 1;+ if (next != ranges.end() && it->first + it->second == next->first) {+ it->second += next->second;+ ranges.erase(next);+ }+ if (it != ranges.begin()) {+ auto prev = it - 1;+ if (prev->first + prev->second == it->first) {+ prev->second += it->second;+ ranges.erase(it);+ }+ }+ }+ };++ struct Page {+ VertexLayoutDesc layout{};+ VertexLayoutHandle layoutHandle = VertexLayoutHandle::Invalid;+ BufferHandle vb = BufferHandle::Invalid;+ BufferHandle ib = BufferHandle::Invalid;+ u32 stride = 0;+ u32 vertexBytes = 0;+ u32 indexBytes = 0;+ FreeList vertices;+ FreeList indices;+ u64 generation = 0;+ u32 live = 0;+ bool alive = false;+ };++ bool makePage(const VertexLayoutDesc& layout, GfxContent content, u32 stride, u32 bytes, u64 generation, u32& index) {+ Page page;+ page.layout = layout;+ page.stride = stride;+ page.vertexBytes = (bytes / stride) * stride;+ page.indexBytes = bytes;+ page.generation = generation;+ page.vb = device_->createBuffer({GfxBufferUsage::Vertex, page.vertexBytes, false}, content, nullptr);+ page.ib = device_->createBuffer({GfxBufferUsage::Index, page.indexBytes, false}, content, nullptr);+ page.layoutHandle = device_->createVertexLayout(layout);+ if (page.vb == BufferHandle::Invalid || page.ib == BufferHandle::Invalid || page.layoutHandle == VertexLayoutHandle::Invalid) {+ destroy(page);+ return false;+ }+ page.vertices.ranges.push_back({0u, page.vertexBytes / stride});+ page.indices.ranges.push_back({0u, page.indexBytes / static_cast<u32>(sizeof(u32))});+ page.alive = true;+ // A dead page's place is reused rather than grown past: a slot names its page by index.+ for (u32 i = 0; i < pages_.size(); ++i) {+ if (!pages_[i].alive && pages_[i].vb == BufferHandle::Invalid) {+ pages_[i] = std::move(page);+ index = i;+ return true;+ }+ }+ pages_.push_back(std::move(page));+ index = static_cast<u32>(pages_.size() - 1);+ return true;+ }++ void destroy(Page& page) {+ if (device_) {+ if (page.vb != BufferHandle::Invalid) device_->deleteBuffer(page.vb);+ if (page.ib != BufferHandle::Invalid) device_->deleteBuffer(page.ib);+ if (page.layoutHandle != VertexLayoutHandle::Invalid) device_->deleteVertexLayout(page.layoutHandle);+ }+ page = Page{};+ }++ GfxDevice* device_ = nullptr;+ std::vector<Page> pages_;+ std::vector<u32> rebased_;+};++} // namespace esengine::resourcediff --git a/src/esengine/resource/ResourceManager.cpp b/src/esengine/resource/ResourceManager.cppindex 10089796f..bdd0d7b09 100644--- a/src/esengine/resource/ResourceManager.cpp+++ b/src/esengine/resource/ResourceManager.cpp@@ -54,6 +54,7 @@ void ResourceManager::init(GfxDevice& device) {}device_ = &device;+ meshArena_.init(device);stats_ = {};initialized_ = true;@@ -78,6 +79,7 @@ void ResourceManager::shutdown() {std::vector<MeshHandle> retiredMeshes;meshes_.forEachAlive([&](MeshHandle handle, const Mesh&) { retiredMeshes.push_back(handle); });for (MeshHandle handle : retiredMeshes) releaseMesh(handle);+ meshArena_.shutdown();vertexBuffers_.clear();indexBuffers_.clear();@@ -618,26 +620,49 @@ bool ResourceManager::realizeMesh(Mesh& mesh, ConstSpan<u8> vertexBytes, ConstSpconst VertexBufferHandle previousVertices = mesh.vertices;const IndexBufferHandle previousIndices = mesh.indices;const IndexBufferHandle previousEdges = mesh.edges;+ const VertexLayoutHandle previousLayout = mesh.layout;+ const u32 previousPage = mesh.arenaPage, previousFirstVertex = mesh.arenaFirstVertex, previousVertexCount = mesh.vertexCount;+ const u32 previousFirstIndex = mesh.firstIndex, previousIndexCount = mesh.indexCount;// A replayable mesh is refilled by its source; one built in this process has// no other copy, so the device keeps it.const GfxContent content = mesh.recovery == MeshRecovery::SourceReplayable? toGfxContent(ResourceContent::Mesh): GfxContent::retained();- const VertexBufferHandle vertices = createVertexBuffer(content, vertexBytes);- const IndexBufferHandle indices_handle = createIndexBuffer(content, indices);- const VertexBuffer* vb = getVertexBuffer(vertices);- const IndexBuffer* ib = getIndexBuffer(indices_handle);- const VertexLayoutHandle layoutHandle = device_->createVertexLayout(layout);- // A pool record is not a GPU object. An allocation the device refused still- // lands in the pool, so its handle reads as valid while naming nothing — the- // shape in which a failure becomes a mesh that silently draws no triangles.- if (!vb || !ib || vb->handle() == BufferHandle::Invalid- || ib->handle() == BufferHandle::Invalid || layoutHandle == VertexLayoutHandle::Invalid) {- releaseVertexBuffer(vertices);- releaseIndexBuffer(indices_handle);- device_->deleteVertexLayout(layoutHandle);- return false;++ // A page shared with the meshes of its vertex shape, where it can have one: drawn one after another, they switch no vertex+ // array, buffer or pipeline. Not a mesh that reads its own vertex number (morph deltas are found by gl_VertexID, which a+ // page's numbering would shift), and not one the device has to keep the bytes of (a page is written in parts, and kept+ // whole would keep every mesh in it twice).+ const bool morphs = morph.targetCount > 0 && !morph.deltas.empty();+ MeshArena::Slot slot;+ MeshArena::Binding shared;+ const bool placed = mesh.recovery == MeshRecovery::SourceReplayable && !morphs+ && meshArena_.place(layout, content, vertexBytes, vertexStride, indices, slot, shared);++ VertexBufferHandle vertices;+ IndexBufferHandle indices_handle;+ BufferHandle vertexBufferId = shared.vertexBuffer;+ BufferHandle indexBufferId = shared.indexBuffer;+ VertexLayoutHandle layoutHandle = shared.layout;+ if (!placed) {+ vertices = createVertexBuffer(content, vertexBytes);+ indices_handle = createIndexBuffer(content, indices);+ const VertexBuffer* vb = getVertexBuffer(vertices);+ const IndexBuffer* ib = getIndexBuffer(indices_handle);+ layoutHandle = device_->createVertexLayout(layout);+ // A pool record is not a GPU object. An allocation the device refused still+ // lands in the pool, so its handle reads as valid while naming nothing — the+ // shape in which a failure becomes a mesh that silently draws no triangles.+ if (!vb || !ib || vb->handle() == BufferHandle::Invalid+ || ib->handle() == BufferHandle::Invalid || layoutHandle == VertexLayoutHandle::Invalid) {+ releaseVertexBuffer(vertices);+ releaseIndexBuffer(indices_handle);+ device_->deleteVertexLayout(layoutHandle);+ return false;+ }+ vertexBufferId = vb->handle();+ indexBufferId = ib->handle();}// The edges follow the triangles' own content policy, and a failure to make them@@ -645,7 +670,9 @@ bool ResourceManager::realizeMesh(Mesh& mesh, ConstSpan<u8> vertexBytes, ConstSpIndexBufferHandle edgeHandle;u32 edgeCount = 0;if (meshEdges_) {- const std::vector<u32> lines = distinctMeshEdges(indices);+ std::vector<u32> lines = distinctMeshEdges(indices);+ // The edges index the same vertices as the triangles, so they are rebased onto a page's as the triangles were.+ if (placed) for (u32& index : lines) index += slot.firstVertex;edgeHandle = createIndexBuffer(content, ConstSpan<u32>(lines));const IndexBuffer* eb = getIndexBuffer(edgeHandle);if (eb && eb->handle() != BufferHandle::Invalid) {@@ -656,20 +683,22 @@ bool ResourceManager::realizeMesh(Mesh& mesh, ConstSpan<u8> vertexBytes, ConstSp}}- releaseVertexBuffer(previousVertices);- releaseIndexBuffer(previousIndices);+ releaseMeshGeometry(previousVertices, previousIndices, previousLayout, previousPage, previousFirstVertex,+ previousVertexCount, previousFirstIndex, previousIndexCount);releaseIndexBuffer(previousEdges);- device_->deleteVertexLayout(mesh.layout);mesh.edges = edgeHandle;mesh.edgeIndexBuffer = edgeCount > 0 ? getIndexBuffer(edgeHandle)->handle() : BufferHandle::Invalid;mesh.edgeIndexCount = edgeCount;mesh.vertices = vertices;mesh.indices = indices_handle;- mesh.vertexBuffer = vb->handle();- mesh.indexBuffer = ib->handle();+ mesh.vertexBuffer = vertexBufferId;+ mesh.indexBuffer = indexBufferId;mesh.layout = layoutHandle;mesh.indexCount = static_cast<u32>(indices.size());+ mesh.firstIndex = placed ? slot.firstIndex : 0;+ mesh.arenaPage = placed ? slot.page : 0;+ mesh.arenaFirstVertex = placed ? slot.firstVertex : 0;mesh.hasNormals = hasNormals;mesh.hasLightmapUV = lightmapped;// A tangent frame is a frame OF the normal: one without a normal to square it to is not read.@@ -834,13 +863,25 @@ std::vector<MeshHandle> ResourceManager::meshesAwaitingRematerialization() constreturn out;}+void ResourceManager::releaseMeshGeometry(VertexBufferHandle vertices, IndexBufferHandle indices, VertexLayoutHandle layout,+ u32 arenaPage, u32 arenaFirstVertex, u32 vertexCount, u32 firstIndex,+ u32 indexCount) {+ if (arenaPage != 0) {+ // The page's buffers and layout are the page's: the mesh gives back its ranges, and the last one out deletes the page.+ meshArena_.free({arenaPage, arenaFirstVertex, vertexCount, firstIndex, indexCount});+ return;+ }+ // The RHI creates a distinct layout for each realization, owned by this mesh.+ if (device_) device_->deleteVertexLayout(layout);+ releaseVertexBuffer(vertices);+ releaseIndexBuffer(indices);+}+void ResourceManager::releaseMesh(MeshHandle handle) {Mesh* mesh = meshes_.get(handle);if (!mesh) return;- // The RHI creates a distinct layout for each realization, owned by this mesh.- if (device_) device_->deleteVertexLayout(mesh->layout);- releaseVertexBuffer(mesh->vertices);- releaseIndexBuffer(mesh->indices);+ releaseMeshGeometry(mesh->vertices, mesh->indices, mesh->layout, mesh->arenaPage, mesh->arenaFirstVertex,+ mesh->vertexCount, mesh->firstIndex, mesh->indexCount);releaseIndexBuffer(mesh->edges);// The delta texture IS the mesh's own — nothing shares it, unlike the layout.if (mesh->morphTexture != esengine::TextureHandle::Invalid && device_) {diff --git a/src/esengine/resource/ResourceManager.hpp b/src/esengine/resource/ResourceManager.hppindex 46a7e89f8..c2b0c4510 100644--- a/src/esengine/resource/ResourceManager.hpp+++ b/src/esengine/resource/ResourceManager.hpp@@ -28,6 +28,7 @@#include "../renderer/rhi/Texture.hpp"#include "../renderer/rhi/Buffer.hpp"#include "Mesh.hpp"+#include "MeshArena.hpp"#include "Environment.hpp"#include "ProbeVolume.hpp"#include "../text/BitmapFont.hpp"@@ -736,6 +737,18 @@ private:ResourcePool<IndexBuffer> indexBuffers_;bool meshEdges_ = false;ResourcePool<Mesh> meshes_;+ /// The pages meshes of one vertex shape share (see MeshArena).+ MeshArena meshArena_;+ /** Gives back what a mesh's geometry holds, its own buffers and layout or its place in a page; the edges are apart. */+ void releaseMeshGeometry(VertexBufferHandle vertices, IndexBufferHandle indices, VertexLayoutHandle layout,+ u32 arenaPage, u32 arenaFirstVertex, u32 vertexCount, u32 firstIndex, u32 indexCount);++public:+ /** The shared mesh pages standing and the bytes they hold, for a profiler. */+ u32 meshPageCount() const { return meshArena_.pageCount(); }+ u64 meshPageBytes() const { return meshArena_.pageBytes(); }++private:ResourcePool<Environment> environments_;ResourcePool<ProbeVolume> probeVolumes_;ResourcePool<text::BitmapFont> fonts_;diff --git a/tests/resource/test_mesh_recovery.cpp b/tests/resource/test_mesh_recovery.cppindex cc8a5b842..159a5b127 100644--- a/tests/resource/test_mesh_recovery.cpp+++ b/tests/resource/test_mesh_recovery.cpp@@ -55,40 +55,69 @@ bool rematerialize(resource::ResourceManager& rm, resource::MeshHandle h, f32 exsizeof(Vertex), glm::vec3(0.0f), glm::vec3(extent));}+// The same triangle with a third channel: a vertex of another shape, which no page of the first shape can hold.+bool rematerializeWide(resource::ResourceManager& rm, resource::MeshHandle h) {+ const f32 verts[3 * 8] = {0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1};+ const u32 indices[3] = {0, 1, 2};+ const GfxVertexAttribute wide[3] = {+ {0, 3, GfxDataType::Float, false, 0, 0},+ {1, 2, GfxDataType::Float, false, 12, 0},+ {2, 3, GfxDataType::Float, false, 20, 0},+ };+ return rm.rematerializeMesh(+ h, ConstSpan<u8>(reinterpret_cast<const u8*>(verts), sizeof(verts)),+ ConstSpan<u32>(indices, 3), ConstSpan<GfxVertexAttribute>(wide, 3),+ 8 * sizeof(f32), glm::vec3(0.0f), glm::vec3(1.0f));+}+} // namespaceint main() {- // Layout ownership follows the mesh through replacement, failure and shutdown.+ // Replayable meshes of one vertex shape share a page: its layout and both its buffers, each mesh at its own range. A mesh+ // of buffers of its own owns its layout. A page goes with its last mesh.{MockGfxDevice d;resource::ResourceManager rm;rm.init(d);const auto first = makeMesh(rm, MeshRecovery::SourceReplayable);- const auto second = makeMesh(rm, MeshRecovery::HostOnly);- const auto oldLayout = rm.getMesh(first)->layout;- const auto secondLayout = rm.getMesh(second)->layout;- CHECK(oldLayout != secondLayout, "identical mesh descriptions own distinct layouts");- CHECK(rematerialize(rm, first, 2.0f), "a mesh can replace its realization");- CHECK(d.vertexLayoutDesc(oldLayout) == nullptr,- "replacement releases the previous vertex layout");- const auto liveLayout = rm.getMesh(first)->layout;- const int deletedBeforeFailure = d.deleteVertexLayoutCalls;- d.createBufferSucceeds = false;- CHECK(!rematerialize(rm, first, 3.0f), "allocation failure refuses a replacement");- CHECK(d.deleteVertexLayoutCalls == deletedBeforeFailure + 1 &&- d.vertexLayoutDesc(liveLayout) != nullptr,- "failed allocation releases its new layout and preserves the live one");- d.createBufferSucceeds = true;+ const auto second = makeMesh(rm, MeshRecovery::SourceReplayable);+ const auto own = makeMesh(rm, MeshRecovery::HostOnly);+ const Mesh* a = rm.getMesh(first);+ const Mesh* b = rm.getMesh(second);+ const Mesh* o = rm.getMesh(own);+ CHECK(a->arenaPage != 0 && a->arenaPage == b->arenaPage, "two replayable meshes of one vertex shape share a page");+ CHECK(a->layout == b->layout && a->vertexBuffer == b->vertexBuffer && a->indexBuffer == b->indexBuffer,+ "its layout and both its buffers");+ CHECK(a->firstIndex != b->firstIndex && a->arenaFirstVertex != b->arenaFirstVertex,+ "each at its own range of the indices and of the vertices");+ CHECK(rm.meshPageCount() == 1, "one page holds them");+ CHECK(o->arenaPage == 0 && o->layout != a->layout, "a host-only mesh owns its buffers and its layout");+ const auto pageLayout = a->layout;+ const auto ownLayout = o->layout;+ CHECK(rematerialize(rm, first, 2.0f), "a shared mesh can replace its realization");+ CHECK(rm.getMesh(first)->layout == pageLayout && d.vertexLayoutDesc(pageLayout) != nullptr,+ "in the page it shares, whose layout stands");rm.releaseMesh(first);- CHECK(d.vertexLayoutDesc(liveLayout) == nullptr && d.vertexLayoutDesc(secondLayout) != nullptr,- "releasing one mesh releases only its layout");+ CHECK(d.vertexLayoutDesc(pageLayout) != nullptr, "releasing one mesh of a page keeps the page");+ rm.releaseMesh(second);+ CHECK(d.vertexLayoutDesc(pageLayout) == nullptr && rm.meshPageCount() == 0, "and the page goes with its last mesh");rm.shutdown();- CHECK(d.vertexLayoutDesc(secondLayout) == nullptr && rm.getMesh(second) == nullptr,+ CHECK(d.vertexLayoutDesc(ownLayout) == nullptr && rm.getMesh(own) == nullptr,"shutdown releases remaining meshes and their layouts");const int deleted = d.deleteVertexLayoutCalls;rm.shutdown();CHECK(d.deleteVertexLayoutCalls == deleted, "resource shutdown is idempotent");}+ // A mint the device refuses releases the layout it made.+ {+ MockGfxDevice d;+ resource::ResourceManager rm;+ rm.init(d);+ const int before = d.deleteVertexLayoutCalls;+ d.createBufferSucceeds = false;+ CHECK(!makeMesh(rm, MeshRecovery::HostOnly).isValid(), "a host-only mesh the device refuses is not minted");+ CHECK(d.deleteVertexLayoutCalls == before + 1, "and the layout made for it is released");+ }// --- The identity and its buffers survive; replayable geometry is owed ---{MockGfxDevice d;@@ -173,6 +202,7 @@ int main() {CHECK(rematerialize(rm, h, 2.0f), "a replayed source rematerializes");CHECK(rm.getMesh(h) == record, "into the SAME record — no second handle was minted");CHECK(record->isDrawable(), "which is drawable");+ CHECK(record->arenaPage != 0, "in a page of the new device: the page it left was owed whole");CHECK(record->localMax == glm::vec3(2.0f), "carrying the geometry that was replayed");CHECK(rm.meshesAwaitingRematerialization().empty(), "and the debt is paid");CHECK(d.deviceStatus() == GfxDeviceStatus::Live, "so the device is whole again");@@ -216,13 +246,16 @@ int main() {CHECK(record->hasRealization(), "and the mesh keeps what it had");// The other way it fails, and the one the promise is actually about: the- // geometry is fine and the GPU cannot allocate for it.+ // geometry is fine and the GPU cannot allocate for it. A vertex of another shape, so+ // the page the mesh is in cannot take it and a new one has to be made.+ const u32 firstIndex = record->firstIndex;d.createBufferSucceeds = false;- CHECK(!rematerialize(rm, h, 1.0f), "a rebuild the GPU cannot allocate for fails");+ CHECK(!rematerializeWide(rm, h), "a rebuild the GPU cannot allocate for fails");CHECK(record->hasRealization(),"and the mesh KEEPS the realization it had — a rebuild that cannot finish must"" not take away the geometry that was already drawing");- CHECK(record->vertexBuffer == vbo && record->indexBuffer == ebo, "the same buffers");+ CHECK(record->vertexBuffer == vbo && record->indexBuffer == ebo && record->firstIndex == firstIndex,+ "the same buffers, at the same range");}// --- Host-only geometry cannot be rematerialized into ---diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjsindex 222dd97bb..c3c4c72d8 100644--- a/tools/releaseNotes.mjs+++ b/tools/releaseNotes.mjs@@ -4802,4 +4802,6 @@ export const NOTED = {{"note": "A model's picture can be a recipe."},"fix(mcp): clearing an asset field from automation clears it - set_field turned 0 and null into the paths \"0\" and \"null\", which the scene kept and an export then could not find; nothing (null, 0, false, the empty string, or the words for it from a text transport) is what the inspector's clear button stores, by one function both ask, and a number that names nothing is refused":{"note": "Clearing an asset field from automation clears it."},+ "perf(render): meshes of one vertex shape share a vertex buffer, an index buffer and a layout - each mesh had its own, so every draw of a scene switched vertex array and pipeline; replayable meshes take ranges of shared pages (MeshArena) with their indices rebased, and a run of them switches nothing":+ {"note": "Meshes of one vertex shape share their buffers."},};