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(tilemap): a row of tiles being edited uploads a piece of itself

yhh5c172b205d46
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 1c2a6fb48..5c9291ff3 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -14,6 +14,10 @@ published separately; it ships inside the editor.
## [Unreleased]
+### Changed
+
+- **A row of tiles being edited uploads a piece of itself.** One changed tile re-uploaded its whole row of chunks, over a megabyte when the row is full. A row that changes is now held in pieces of four chunks, so an edit uploads about an eighth of that, for up to seven more draws in that row until it has rested. A static map is drawn as before.
+
## [0.100.0] - 2026-11-08
### Added
diff --git a/src/esengine/renderer/plugins/TilemapRenderPlugin.cpp b/src/esengine/renderer/plugins/TilemapRenderPlugin.cpp
index 04599355b..4ecc8eb7b 100644
--- a/src/esengine/renderer/plugins/TilemapRenderPlugin.cpp
+++ b/src/esengine/renderer/plugins/TilemapRenderPlugin.cpp
@@ -146,6 +146,8 @@ void TilemapRenderPlugin::releaseStrip(GfxDevice& device, Strip& strip) {
if (slot.ibo != BufferHandle::Invalid) device.deleteBuffer(slot.ibo);
slot = StripSlot{};
}
+ for (Strip& piece : strip.pieces) releaseStrip(device, piece);
+ strip.pieces.clear();
strip.built = false;
}
@@ -401,22 +403,20 @@ void TilemapRenderPlugin::collect(RenderCollectContext& collect_ctx) {
visible_strips_scratch_.clear();
for (i32 cy = minCY; cy < maxCY; ++cy) {
- for (i32 sx = firstStrip; sx <= lastStrip; ++sx) {
- Strip& strip = layerCache.strips[StripKey{sx, cy}];
- strip.lastUsed = collect_serial_;
- // The chunks the strip is made of, brought up to date: the newest build among them and how many there are say whether the
- // strip still describes them.
- u64 stamp = 0;
- u32 chunkCount = 0;
- const i32 cxBegin = sx * STRIP_CHUNKS;
- for (i32 cx = cxBegin; cx < cxBegin + STRIP_CHUNKS; ++cx) {
+ // The chunks a strip is made of, brought up to date: the newest build among them and how many there are say whether the
+ // strip still describes them.
+ const auto measure = [&](i32 cxBegin, i32 width, u64& stamp, u32& chunkCount) {
+ stamp = 0;
+ chunkCount = 0;
+ for (i32 cx = cxBegin; cx < cxBegin + width; ++cx) {
const tilemap::ChunkCoord coord{cx, cy};
const auto chunkIt = layer.chunks.find(coord);
if (chunkIt == layer.chunks.end()) continue;
stamp = std::max(stamp, ensureChunk(layer, chunkIt->second, coord, slots, resolved, entity, chunkCaches).mesh_serial);
++chunkCount;
}
- if (chunkCount == 0 && !strip.built) continue;
+ };
+ const auto refresh = [&](Strip& strip, i32 cxBegin, i32 width, u64 stamp, u32 chunkCount) {
const bool current = strip.built && strip.stamp == stamp && strip.chunkCount == chunkCount
&& strip.placement == place && strip.deviceGeneration == generation && strip.slots.size() == slots.size();
if (!current) {
@@ -427,7 +427,7 @@ void TilemapRenderPlugin::collect(RenderCollectContext& collect_ctx) {
strip_vertices_scratch_.clear();
strip_indices_scratch_.clear();
// Chunks in ascending x: the order the merged draw laid a row of them out in.
- for (i32 cx = cxBegin; cx < cxBegin + STRIP_CHUNKS; ++cx) {
+ for (i32 cx = cxBegin; cx < cxBegin + width; ++cx) {
const auto chunkIt = chunkCaches.find(tilemap::ChunkCoord{cx, cy});
if (chunkIt == chunkCaches.end() || si >= chunkIt->second.slots.size()) continue;
if (layer.chunks.find(tilemap::ChunkCoord{cx, cy}) == layer.chunks.end()) continue;
@@ -470,6 +470,35 @@ void TilemapRenderPlugin::collect(RenderCollectContext& collect_ctx) {
strip.built = true;
}
visible_strips_scratch_.push_back(&strip);
+ };
+ for (i32 sx = firstStrip; sx <= lastStrip; ++sx) {
+ Strip& strip = layerCache.strips[StripKey{sx, cy}];
+ strip.lastUsed = collect_serial_;
+ const i32 cxBegin = sx * STRIP_CHUNKS;
+ u64 stamp = 0;
+ u32 chunkCount = 0;
+ measure(cxBegin, STRIP_CHUNKS, stamp, chunkCount);
+ const bool held = strip.built || !strip.pieces.empty();
+ if (chunkCount == 0 && !held) continue;
+ if (held && (strip.stamp != stamp || strip.chunkCount != chunkCount)) strip.piecesUntil = collect_serial_ + STRIP_PIECES_COLLECTS;
+ if (strip.piecesUntil > collect_serial_) {
+ if (strip.built) releaseStrip(device, strip);
+ if (strip.pieces.empty()) strip.pieces.resize(STRIP_CHUNKS / STRIP_PIECE_CHUNKS);
+ strip.stamp = stamp;
+ strip.chunkCount = chunkCount;
+ for (usize i = 0; i < strip.pieces.size(); ++i) {
+ Strip& piece = strip.pieces[i];
+ const i32 pieceBegin = cxBegin + static_cast<i32>(i) * STRIP_PIECE_CHUNKS;
+ u64 pieceStamp = 0;
+ u32 pieceChunks = 0;
+ measure(pieceBegin, STRIP_PIECE_CHUNKS, pieceStamp, pieceChunks);
+ if (pieceChunks == 0 && !piece.built) continue;
+ refresh(piece, pieceBegin, STRIP_PIECE_CHUNKS, pieceStamp, pieceChunks);
+ }
+ continue;
+ }
+ if (!strip.pieces.empty()) releaseStrip(device, strip);
+ refresh(strip, cxBegin, STRIP_CHUNKS, stamp, chunkCount);
}
}
diff --git a/src/esengine/renderer/plugins/TilemapRenderPlugin.hpp b/src/esengine/renderer/plugins/TilemapRenderPlugin.hpp
index 09d7fd5f9..e7edb4d7f 100644
--- a/src/esengine/renderer/plugins/TilemapRenderPlugin.hpp
+++ b/src/esengine/renderer/plugins/TilemapRenderPlugin.hpp
@@ -94,6 +94,11 @@ private:
// tiles that overlap (a tall tile over its neighbour, an isometric map) are drawn in that order: strips drawn row by row, slot by slot,
// reproduce it, and a square block of chunks would not.
static constexpr i32 STRIP_CHUNKS = 32;
+ // A strip makes a frame with no change free and a frame with one dear: one tile dug is the whole row uploaded again, over a megabyte
+ // when the row is full. So a strip whose chunks change is cut into pieces this many chunks wide, of which a change uploads one, until
+ // no change has reached it for this many collects. The pieces cost a few more draws, and are drawn in the order the strip's tiles were.
+ static constexpr i32 STRIP_PIECE_CHUNKS = 4;
+ static constexpr u64 STRIP_PIECES_COLLECTS = 1800;
/// What the strips of every layer may hold before the ones not drawn lately are given back.
static constexpr u64 STRIP_BYTES_BUDGET = 96ull * 1024 * 1024;
struct StripKey {
@@ -128,6 +133,8 @@ private:
u64 deviceGeneration = 0; // the device its buffers belong to
u64 lastUsed = 0; // the collect that last drew it
bool built = false;
+ std::vector<Strip> pieces; // while its chunks keep changing: these are drawn, left to right, and it holds no buffers of its own
+ u64 piecesUntil = 0; // the collect it goes back to being one strip
};
struct LayerCache {
diff --git a/tests/renderer/test_batch_builder.cpp b/tests/renderer/test_batch_builder.cpp
index 066dc03ae..fd9b824d5 100644
--- a/tests/renderer/test_batch_builder.cpp
+++ b/tests/renderer/test_batch_builder.cpp
@@ -363,6 +363,85 @@ TEST_CASE("tilemaps: rows of chunks are drawn from the top row down, each a draw
resources.shutdown();
}
+TEST_CASE("tilemaps: a row being dug uploads the piece that changed, keeps its draw order, and is one strip again once it rests") {
+ Harness h;
+ RenderContext context(h.device);
+ resource::ResourceManager resources;
+ resources.init(h.device);
+ const u8 pixels[16 * 16 * 4] = {};
+ const auto texture = resources.createTexture(resource::ResourceContent::Retained, 16, 16, pixels, TextureFormat::RGBA8);
+ ecs::Registry registry;
+ const auto entity = registry.create();
+ auto& transform = registry.emplace<ecs::Transform>(entity);
+ auto& component = registry.emplace<ecs::TilemapLayer>(entity);
+ component.tileset = texture; component.tilesetColumns = 1; component.cellSize = {16, 16};
+ tilemap::TilemapSystem system;
+ constexpr u32 ROW_TILES = 32 * 16 * 16;
+ system.initLayer(entity, 32 * 16, 16, 16, 16);
+ system.fillRect(entity, 0, 0, 32 * 16, 16, 1);
+ Frustum frustum;
+ RenderFrameContext frameContext{context, resources};
+ RenderCollectContext collect{registry, frustum, h.clips, h.pool, h.list, frameContext, {}};
+ TilemapRenderPlugin plugin; plugin.setTilemapSystem(&system); plugin.init(frameContext);
+ const auto projection = glm::ortho(0.0f, 8300.0f, -300.0f, 0.0f);
+ frustum.extractFromMatrix(projection); collect.camera = computeCameraView(projection);
+ transform.worldPosition = {0, 0, 0}; transform.worldRotation = glm::quat{1, 0, 0, 0};
+ transform.cachedMatrix_ = glm::mat4{1}; transform.decomposed_ = true;
+ const auto frame = [&] { h.pool.beginFrame(); h.list.clear(); h.device.createdBuffers.clear(); plugin.collect(collect); };
+ const auto indices = [&] {
+ u32 total = 0;
+ for (u32 i = 0; i < h.list.commandCount(); ++i) total += h.list.command(i).index_count;
+ return total;
+ };
+ const auto vertexBytes = [&] {
+ usize total = 0;
+ for (const auto& made : h.device.createdBuffers) if (made.desc.usage == GfxBufferUsage::Vertex) total += made.bytes.size();
+ return total;
+ };
+
+ frame();
+ REQUIRE(h.list.commandCount() == 1);
+ const usize wholeRow = vertexBytes();
+ CHECK(indices() == ROW_TILES * 6);
+
+ // The first tile dug cuts the row, so that one uploads all of it, and every piece is drawn left to right.
+ system.setTile(entity, 5 * 16 + 3, 3, 0);
+ frame();
+ REQUIRE(h.list.commandCount() == 8);
+ CHECK(indices() == (ROW_TILES - 1) * 6);
+ std::vector<f32> firstX;
+ for (u32 i = 0; i < 8; ++i) {
+ const u32 id = static_cast<u32>(h.list.command(i).vertex_buffer);
+ const MockGfxDevice::CreatedBuffer* found = nullptr;
+ for (const auto& made : h.device.createdBuffers) if (made.id == id) found = &made;
+ REQUIRE(found != nullptr);
+ firstX.push_back(reinterpret_cast<const BatchVertex*>(found->bytes.data())[0].position.x);
+ }
+ CHECK(std::is_sorted(firstX.begin(), firstX.end()));
+
+ // Every tile after it uploads one piece: an eighth of the row, a vertex and an index buffer.
+ system.setTile(entity, 5 * 16 + 4, 3, 0);
+ frame();
+ CHECK(h.device.createdBuffers.size() == 2);
+ CHECK(vertexBytes() < wholeRow / 8);
+ CHECK(vertexBytes() > wholeRow / 16);
+ CHECK(indices() == (ROW_TILES - 2) * 6);
+
+ // A frame with no change uploads nothing, pieces or not.
+ frame();
+ CHECK(h.device.createdBuffers.empty());
+ CHECK(h.list.commandCount() == 8);
+
+ // At rest the row goes back to one draw, once, and holds still.
+ bool rejoined = false;
+ for (int i = 0; i < 4000 && !rejoined; ++i) { frame(); rejoined = h.list.commandCount() == 1; }
+ REQUIRE(rejoined);
+ CHECK(indices() == (ROW_TILES - 2) * 6);
+ frame();
+ CHECK(h.device.createdBuffers.empty());
+ resources.shutdown();
+}
+
TEST_CASE("appendQuad: Batch command with baseVertex-offset indices and one texture slot") {
Harness h;
BatchVertex quad[4] = {
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index 53f91c85b..f68283a19 100644
--- a/tools/releaseNotes.mjs
+++ b/tools/releaseNotes.mjs
@@ -4208,4 +4208,6 @@ export const NOTED = {
{"note": "A generated building stands on its ground, and generated roads keep foliage off them."},
"feat: expose ground fitting and vegetation budgets to editor agents":
{"note": "A generated building stands on its ground, and generated roads keep foliage off them."},
+ "perf(tilemap): a row of tiles being edited uploads a piece of itself":
+ {"note": "A row of tiles being edited uploads a piece of itself."},
};