Skip to content

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.

perf(render): a draw that changes buffer no longer re-points its vertex attributes

yhh581c36891648
diff --git a/CHANGELOG.md b/CHANGELOG.md
index ee6e68369..e437da439 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -47,6 +47,7 @@ published separately; it ships inside the editor.
### Changed
+- **A draw that changes buffer no longer re-points its vertex attributes.** The WebGL2 device kept one vertex array object per vertex layout, so every draw from a different buffer (each strip of a tilemap, each mesh sharing a layout) rebound the buffer and set its five attributes again: 15 GL calls a draw that a vertex array object exists to save. It now keeps one per layout and the buffers it reads, and deletes them with the buffer. The 47,000-tile view goes from 651 GL calls a frame to 190 and the 3,300-tile view from 188 to 73, with the same picture; the 3D example is unchanged (about 2,000 calls, a few more vertex array switches).
- **A tilemap that has not changed uploads nothing and copies no vertex.** Every frame the layer copied the vertices of each visible chunk into a list, placed them, copied them again into the frame's buffer and uploaded all of it: 47,000 visible tiles were 6 MB a frame and 5.8 ms of CPU. The layer now keeps its geometry on the GPU in strips (a row of up to 32 chunks, in world space) and a frame in which no chunk changed submits them as they are. 47,000 tiles go from 5.8 ms to 0.5 ms; a 3,300-tile view from 0.59 ms to 0.40 ms. The picture is the same: the strips are submitted slot by slot and row by row, the order the merged draw laid tiles out in, which matters where tiles overlap, and the near and far views are pixel-identical to before (the far one within the run-to-run noise of its animated tiles). A strip is made again only when a chunk in it changes, the layer is placed somewhere else, or the device was lost; a layer faded, tinted or carried along is placed anew every frame, and a strip is every chunk of a row, so such a layer (and one with parallax) keeps the old per-frame path until it has stood still for thirty frames. What the strips hold is bounded (96 MB, the ones not drawn lately go first). The cost is more, smaller draws: 8 for the near view and 32 for the far one, and about four times the triangles in the near view, which the GPU clips.
- **A label's glyph pipeline no longer costs a transform read per label per frame.** An automatic label draws with hinted bitmaps near 1:1 and with SDF past 2% of it, so script read every label's world scale every frame to see whether the choice had changed, a `Transform` converted through embind each time: about two thirds of what the script still spent on 600 static labels. The engine now holds the choice it was laid out with and watches the scale itself in `renderer_retainedTextStale`, by the same rule (pinned by one table on each side). 600 static labels go from 1.6 ms to 1.1 ms of CPU a frame, and the script's own per-frame loop to about 0.05 ms. Evaluated and not taken: driving the check from the change tracker (a script component written in place through `world.get` is not reported, and that is documented and tested behaviour), and making `Text` an engine component (an in-place write to one is dropped, which would end that behaviour for user code).
- **UI text is laid out when it changes and submitted by the engine every frame, instead of handed over by script every frame.** Every label cost script an embind round trip for each of its domain, hidden state, opacity, matrix, size, order and cull bit, a JSON signature to find out nothing had changed, and a copy of its quads into the engine: 4.3 microseconds a label, which is 2.7 ms a frame for 600 labels that never change. The glyph quads are now handed to the engine once, in `renderer_retainedTextBegin/Batch`, and the engine reads everything else off the entity itself, clips against the view as the script did, and submits them (`renderer_retainedTextSubmit`). Script compares the fields a label was laid out from and lays it out again only when one differs; the engine reports the labels whose box was resized or that moved between the world and the screen (`renderer_retainedTextStale`). 600 static labels go from 2.7 ms to 1.6 ms of CPU a frame, with the same draws and triangles, and a pixel-identical picture once the page has settled. A host without the new entry points keeps the old path. The engine's culling and the script's are checked quad for quad on 120 random cases (`test_retained_text`).
diff --git a/src/esengine/renderer/rhi/GLDevice.cpp b/src/esengine/renderer/rhi/GLDevice.cpp
index 74b923e08..ec9237b53 100644
--- a/src/esengine/renderer/rhi/GLDevice.cpp
+++ b/src/esengine/renderer/rhi/GLDevice.cpp
@@ -972,6 +972,8 @@ bool GLDevice::backendCreateBuffer(u32 id, const BufferDesc& desc, const void* d
void GLDevice::backendDeleteBuffer(u32 id) {
GLuint name = nameOf(buffer_names_, id);
if (!name) return;
+ // The VAOs that read this buffer go with it: GL gives a deleted name to the next buffer made, and a VAO still keyed by it would be taken for that one.
+ releaseVaosUsing(name);
glDeleteBuffers(1, &name);
setName(buffer_names_, id, 0);
// The context unbinds a deleted buffer from the slots that held it; the record of those slots must not go on saying it is there.
@@ -1016,16 +1018,40 @@ void GLDevice::bindUniformSlot(u32 slot, u32 name) {
// =============================================================================
void GLDevice::backendDeleteVertexLayout(u32 id) {
- auto it = vaos_.find(id);
- if (it == vaos_.end()) return;
- if (it->second.vao != 0) {
- if (bound_vao_ == it->second.vao) {
- glBindVertexArray(0);
- bound_vao_ = 0;
+ for (auto it = vaos_.begin(); it != vaos_.end();) {
+ if (it->first.layout != id) {
+ ++it;
+ continue;
+ }
+ if (it->second.vao != 0) {
+ if (bound_vao_ == it->second.vao) {
+ glBindVertexArray(0);
+ bound_vao_ = 0;
+ }
+ glDeleteVertexArrays(1, &it->second.vao);
+ }
+ it = vaos_.erase(it);
+ }
+}
+
+void GLDevice::releaseVaosUsing(u32 bufferName) {
+ if (!bufferName) return;
+ for (auto it = vaos_.begin(); it != vaos_.end();) {
+ bool reads = it->first.ibo == bufferName;
+ for (u32 slot = 0; slot < MAX_VERTEX_BUFFER_SLOTS; ++slot) reads = reads || it->first.vbo[slot] == bufferName;
+ if (!reads) {
+ ++it;
+ continue;
+ }
+ if (it->second.vao != 0) {
+ if (bound_vao_ == it->second.vao) {
+ glBindVertexArray(0);
+ bound_vao_ = 0;
+ }
+ glDeleteVertexArrays(1, &it->second.vao);
}
- glDeleteVertexArrays(1, &it->second.vao);
+ it = vaos_.erase(it);
}
- vaos_.erase(it);
}
void GLDevice::setVertexBuffer(u32 slot, BufferHandle buffer, u32 offsetBytes) {
@@ -1041,7 +1067,14 @@ void GLDevice::setIndexBuffer(BufferHandle buffer) {
void GLDevice::prepareVertexState() {
const VertexLayoutDesc* desc = vertexLayoutDesc(VertexLayoutHandle{current_layout_});
if (!desc) return;
- VaoCache& rec = vaos_[current_layout_];
+ VaoKey key;
+ key.layout = current_layout_;
+ key.ibo = pending_ibo_;
+ for (u32 a = 0; a < desc->attributeCount; ++a) {
+ const u32 slot = desc->attributes[a].bufferSlot;
+ if (slot < MAX_VERTEX_BUFFER_SLOTS) key.vbo[slot] = pending_vbo_[slot];
+ }
+ VaoCache& rec = vaos_[key];
if (rec.vao == 0) {
glGenVertexArrays(1, &rec.vao);
diff --git a/src/esengine/renderer/rhi/GLDevice.hpp b/src/esengine/renderer/rhi/GLDevice.hpp
index ea144f442..e08bbd475 100644
--- a/src/esengine/renderer/rhi/GLDevice.hpp
+++ b/src/esengine/renderer/rhi/GLDevice.hpp
@@ -294,7 +294,26 @@ private:
u32 bakedOffset[MAX_VERTEX_BUFFER_SLOTS] = {};
u32 bakedIbo = 0;
};
- std::unordered_map<u32, VaoCache> vaos_;
+ // A vertex array object per (layout, the buffers it reads from): it holds the attribute pointers, which name their buffer, so one VAO shared by
+ // every buffer of a layout re-pointed all of them at each draw that changed buffer (a tilemap's strips, a mesh's own vertices). Slots the
+ // layout does not read are 0 in the key, so a stale handle left in one does not make a second VAO. Offsets are not in the key: a draw that
+ // moves along a buffer (instance records) re-points just that slot, as before.
+ struct VaoKey {
+ u32 layout = 0;
+ u32 vbo[MAX_VERTEX_BUFFER_SLOTS] = {};
+ u32 ibo = 0;
+ bool operator==(const VaoKey&) const = default;
+ };
+ struct VaoKeyHash {
+ usize operator()(const VaoKey& key) const {
+ usize h = key.layout * 2654435761u;
+ for (u32 slot = 0; slot < MAX_VERTEX_BUFFER_SLOTS; ++slot) h = (h ^ key.vbo[slot]) * 1099511628211ull;
+ return (h ^ key.ibo) * 1099511628211ull;
+ }
+ };
+ std::unordered_map<VaoKey, VaoCache, VaoKeyHash> vaos_;
+ /// 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;
u32 pending_vbo_[MAX_VERTEX_BUFFER_SLOTS] = {};
u32 pending_vbo_offset_[MAX_VERTEX_BUFFER_SLOTS] = {};
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index 1930bdf4e..d8f2ebaa7 100644
--- a/tools/releaseNotes.mjs
+++ b/tools/releaseNotes.mjs
@@ -3086,4 +3086,6 @@ export const NOTED = {
{"note":"UI text costs a third less again"},
"perf(tilemap): a tilemap that has not changed uploads nothing and copies no vertex":
{"note":"Tilemaps that have not changed cost almost nothing per frame"},
+ "perf(render): a draw that changes buffer no longer re-points its vertex attributes":
+ {"note":"Fewer GL calls when a draw changes buffer"},
};