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(tilemap): a tilemap that has not changed uploads nothing and copies no vertex
yhhdb0810a1bf45
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 6b874f124..ee6e68369 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -47,6 +47,7 @@ published separately; it ships inside the editor.### Changed+- **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`).- **A frame of 2D sprites is not sorted again when its draw keys are the last frame's.** The draw list sorted every command each frame, 0.15 ms for 4,000 sprites and growing as n log n, although a 2D key holds the layer, shader, blend and material and no position: sprites that move, or do not, give the same keys frame after frame. The order is a function of the key sequence alone (ties break on emit index), so a frame with the same keys takes the last frame's order and skips the sort. The draw list's finalize in a 4,000-sprite scene goes from 0.30 ms to 0.10 ms. The sort result is the same bit for bit; a frame whose keys differ is sorted as before.diff --git a/src/esengine/renderer/draw/DrawList.cpp b/src/esengine/renderer/draw/DrawList.cppindex 1319deef2..d26e457c0 100644--- a/src/esengine/renderer/draw/DrawList.cpp+++ b/src/esengine/renderer/draw/DrawList.cpp@@ -356,7 +356,7 @@ void DrawList::stageBlocks(const PerDrawBlockSet& blocks) {// gl_InstanceID restarts at zero every draw while the frame's records are// one continuous run, so a draw has to say where its own run begins in them.- if (blocks.instance && cmd.hasPersistentGeometry()) {+ if (blocks.instance && cmd.hasPersistentGeometry() && cmd.instance_count != 0) {const InstanceConstants at{cmd.vertex_byte_offset / MESH_INSTANCE_STRIDE, {}};staged.instance = blocks.instance->stage(&at, sizeof(at));}@@ -554,15 +554,19 @@ void DrawList::execute(GfxDevice& device, TransientBufferPool& buffers,}if (cmd.hasPersistentGeometry()) {- if (blocks.instance) {+ // Geometry the GPU already holds: instanced when it is a mesh placed by records, plain when it is a run of quads a plugin keeps+ // between frames (a tilemap's chunk strip), which has no records and draws once.+ if (blocks.instance && cmd.instance_count != 0) {blocks.instance->bind(INSTANCE_CONSTANTS_BINDING, staged.instance);}device.setVertexBuffer(0, cmd.vertex_buffer, 0);device.setIndexBuffer(cmd.index_buffer);- device.drawElementsInstanced(- cmd.index_count, GfxDataType::UnsignedInt,- static_cast<u32>(static_cast<uintptr_t>(cmd.index_offset) * sizeof(u32)),- cmd.instance_count);+ const u32 firstIndexByte = static_cast<u32>(static_cast<uintptr_t>(cmd.index_offset) * sizeof(u32));+ if (cmd.instance_count != 0) {+ device.drawElementsInstanced(cmd.index_count, GfxDataType::UnsignedInt, firstIndexByte, cmd.instance_count);+ } else {+ device.drawElements(cmd.index_count, GfxDataType::UnsignedInt, firstIndexByte);+ }} else if (cmd.instance_count > 0) {// Instanced: static geometry (index_count indices from offset 0) drawn// instance_count times, instance attributes rebased at vertex_byte_offset.diff --git a/src/esengine/renderer/plugins/TilemapRenderPlugin.cpp b/src/esengine/renderer/plugins/TilemapRenderPlugin.cppindex 4b622b3ee..d066a365f 100644--- a/src/esengine/renderer/plugins/TilemapRenderPlugin.cpp+++ b/src/esengine/renderer/plugins/TilemapRenderPlugin.cpp@@ -117,6 +117,64 @@ void TilemapRenderPlugin::rebuildChunk(}}+TilemapRenderPlugin::ChunkCache& TilemapRenderPlugin::ensureChunk(+ const tilemap::TilemapSystem::LayerData& layer, const tilemap::ChunkData& chunkData, tilemap::ChunkCoord coord,+ const std::vector<tilemap::TilesetSlot>& slots, const std::vector<ResolvedSlot>& resolved, Entity entity, ChunkMap& chunkCaches+) {+ auto& cache = chunkCaches[coord];+ if (chunkData.revision != cache.built_revision+ || layer.anim_table_revision != cache.built_anim_table_revision+ || (cache.has_animated_tiles && layer.anim_revision != cache.built_anim_revision)+ || cache.slots.size() != slots.size()) {+ rebuildChunk(layer, chunkData, coord, slots, resolved, entity, cache);+ cache.built_revision = chunkData.revision;+ cache.built_anim_revision = layer.anim_revision;+ cache.built_anim_table_revision = layer.anim_table_revision;+ cache.mesh_serial = ++mesh_serial_;+ }+ return cache;+}++void TilemapRenderPlugin::releaseStrip(GfxDevice& device, Strip& strip) {+ // Deleted whatever device generation they were made on: a device that was lost keeps its buffers' records (it rebuilds them with their+ // contents gone), so the handles still name them, and leaving them would grow the device's table by a strip's buffers every recovery.+ for (StripSlot& slot : strip.slots) {+ const u64 bytes = static_cast<u64>(slot.vboBytes) + slot.iboBytes;+ strip_bytes_ = strip_bytes_ >= bytes ? strip_bytes_ - bytes : 0;+ if (slot.vbo != BufferHandle::Invalid) device.deleteBuffer(slot.vbo);+ if (slot.ibo != BufferHandle::Invalid) device.deleteBuffer(slot.ibo);+ slot = StripSlot{};+ }+ strip.built = false;+}++void TilemapRenderPlugin::releaseLayer(LayerCache& layer) {+ if (device_) {+ for (auto& [key, strip] : layer.strips) releaseStrip(*device_, strip);+ }+ layer.strips.clear();+}++void TilemapRenderPlugin::evictStrips(GfxDevice& device) {+ if (strip_bytes_ <= STRIP_BYTES_BUDGET) return;+ // Not drawn by the last two collects: a frame with several cameras collects once for each, and what an earlier one queued has not drawn yet.+ for (auto& [entity, layer] : layer_caches_) {+ for (auto it = layer.strips.begin(); it != layer.strips.end() && strip_bytes_ > STRIP_BYTES_BUDGET;) {+ if (it->second.lastUsed + 2 <= collect_serial_) {+ releaseStrip(device, it->second);+ it = layer.strips.erase(it);+ } else {+ ++it;+ }+ }+ }+}++void TilemapRenderPlugin::shutdown() {+ for (auto& [entity, layer] : layer_caches_) releaseLayer(layer);+ layer_caches_.clear();+}+void TilemapRenderPlugin::collect(RenderCollectContext& collect_ctx) {auto& registry = collect_ctx.registry;auto& clips = collect_ctx.clip_state;@@ -129,10 +187,12 @@ void TilemapRenderPlugin::collect(RenderCollectContext& collect_ctx) {if (layers.empty()) return;const CameraView& cam = collect_ctx.camera;+ ++collect_serial_;for (const auto& [entity, layer] : layers) {auto cachedLayer = layer_caches_.find(entity);if (cachedLayer != layer_caches_.end() && cachedLayer->second.generation != layer.generation) {+ releaseLayer(cachedLayer->second);layer_caches_.erase(cachedLayer);cachedLayer = layer_caches_.end();}@@ -302,6 +362,7 @@ void TilemapRenderPlugin::collect(RenderCollectContext& collect_ctx) {const LayoutInputs layout{layer.tile_width, layer.tile_height, layer.grid_type,layer.hex_side_length, layer.hex_stagger_axis_x, layer.hex_stagger_index_even};if (layerCache.layout != layout || layerCache.tilesets != slots || layerCache.resolved != resolved) {+ releaseLayer(layerCache);layerCache.chunks.clear();layerCache.layout = layout;layerCache.tilesets = slots;@@ -309,6 +370,125 @@ void TilemapRenderPlugin::collect(RenderCollectContext& collect_ctx) {}auto& chunkCaches = layerCache.chunks;+ // A layer that has not moved draws from strips the GPU holds. A parallax layer places its tiles by where the camera is, which changes+ // every frame it moves, so it keeps the path that builds the vertices each frame; so does a layer being faded, tinted or carried, until+ // it has stood still for a while.+ const Placement place{adjOriginX, adjOriginY, originX, originY, originOffset.x, originOffset.y, angle, depth, packedColor};+ constexpr u64 SETTLE_COLLECTS = 30;+ if (layerCache.lastPlacementKnown && !(layerCache.lastPlacement == place)) {+ if (layerCache.movingUntil <= collect_serial_) releaseLayer(layerCache);+ layerCache.movingUntil = collect_serial_ + SETTLE_COLLECTS;+ }+ layerCache.lastPlacement = place;+ layerCache.lastPlacementKnown = true;+ if (parallax.x == 1.0f && parallax.y == 1.0f && layerCache.movingUntil <= collect_serial_ && buffers.device().isDeviceUsable()) {+ GfxDevice& device = buffers.device();+ device_ = &device;+ const u64 generation = device.deviceGeneration();+ const i32 firstStrip = static_cast<i32>(std::floor(static_cast<f32>(minCX) / static_cast<f32>(STRIP_CHUNKS)));+ const i32 lastStrip = static_cast<i32>(std::floor(static_cast<f32>(maxCX - 1) / static_cast<f32>(STRIP_CHUNKS)));+ const VertexLayoutHandle batchLayout = buffers.layoutHandle(LayoutId::Batch);++ 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) {+ 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 bool current = strip.built && strip.stamp == stamp && strip.chunkCount == chunkCount+ && strip.placement == place && strip.deviceGeneration == generation && strip.slots.size() == slots.size();+ if (!current) {+ releaseStrip(device, strip);+ strip.slots.assign(slots.size(), StripSlot{});+ for (usize si = 0; si < slots.size(); ++si) {+ if (!resolved[si].valid) continue;+ 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) {+ 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;+ const SlotMesh& mesh = chunkIt->second.slots[si];+ if (mesh.indices.empty()) continue;+ const u32 baseVertex = static_cast<u32>(strip_vertices_scratch_.size());+ for (const BatchVertex& v : mesh.vertices) {+ const f32 lx = v.position.x + originOffset.x, ly = v.position.y + originOffset.y;+ strip_vertices_scratch_.push_back({+ angle == 0+ ? glm::vec3{v.position.x + adjOriginX, v.position.y + adjOriginY, depth}+ : glm::vec3{originX + parallaxOffsetX + lx * cosAngle - ly * sinAngle,+ originY + parallaxOffsetY + lx * sinAngle + ly * cosAngle, depth},+ packedColor, v.texCoord });+ }+ for (u32 idx : mesh.indices) strip_indices_scratch_.push_back(baseVertex + idx);+ }+ if (strip_indices_scratch_.empty()) continue;+ StripSlot& out = strip.slots[si];+ out.vertexCount = static_cast<u32>(strip_vertices_scratch_.size());+ out.indexCount = static_cast<u32>(strip_indices_scratch_.size());+ out.vboBytes = out.vertexCount * static_cast<u32>(sizeof(BatchVertex));+ out.iboBytes = out.indexCount * static_cast<u32>(sizeof(u32));+ out.vbo = device.createBuffer({GfxBufferUsage::Vertex, out.vboBytes, /*dynamic=*/false}, GfxContent::transient(),+ strip_vertices_scratch_.data());+ out.ibo = device.createBuffer({GfxBufferUsage::Index, out.iboBytes, /*dynamic=*/false}, GfxContent::transient(),+ strip_indices_scratch_.data());+ if (out.vbo == BufferHandle::Invalid || out.ibo == BufferHandle::Invalid) {+ if (out.vbo != BufferHandle::Invalid) device.deleteBuffer(out.vbo);+ if (out.ibo != BufferHandle::Invalid) device.deleteBuffer(out.ibo);+ out = StripSlot{};+ continue;+ }+ strip_bytes_ += out.vboBytes + out.iboBytes;+ }+ strip.placement = place;+ strip.stamp = stamp;+ strip.chunkCount = chunkCount;+ strip.deviceGeneration = generation;+ strip.built = true;+ }+ visible_strips_scratch_.push_back(&strip);+ }+ }++ // Slot by slot, then row by row: the order the merged draws were emitted in, so tiles that overlap overlap the same way.+ for (usize si = 0; si < slots.size(); ++si) {+ if (!resolved[si].valid) continue;+ for (const Strip* strip : visible_strips_scratch_) {+ if (si >= strip->slots.size() || strip->slots[si].indexCount == 0) continue;+ const StripSlot& slot = strip->slots[si];+ BatchDrawKey key{+ .stage = ctx.current_stage,+ .layer = sortLayer,+ .shaderId = batch_shader_id_,+ .blend = BlendMode::Normal,+ .textureId = resolved[si].glTexId,+ .depth = depth,+ .entity = entity,+ .type = RenderType::Sprite,+ };+ key.vertexBuffer = slot.vbo;+ key.indexBuffer = slot.ibo;+ key.vertexLayout = batchLayout;+ if (!batchEntityVisible(draw_list, clips, key.entity, key.layer, key.cullBit)) continue;+ pushBatchDraw(draw_list, clips, 0, slot.vertexCount, 0, slot.indexCount, key);+ }+ }+ continue;+ }+// Per-slot merged geometry for this layer (one draw call per slot/texture).if (slot_vertices_scratch_.size() < slots.size()) {slot_vertices_scratch_.resize(slots.size());@@ -328,18 +508,7 @@ void TilemapRenderPlugin::collect(RenderCollectContext& collect_ctx) {if (chunkIt == layer.chunks.end()) continue;const auto& chunkData = chunkIt->second;- auto& cache = chunkCaches[coord];-- if (chunkData.revision != cache.built_revision- || layer.anim_table_revision != cache.built_anim_table_revision- || (cache.has_animated_tiles && layer.anim_revision != cache.built_anim_revision)- || cache.slots.size() != slots.size()) {- rebuildChunk(layer, chunkData, coord,- slots, resolved, entity, cache);- cache.built_revision = chunkData.revision;- cache.built_anim_revision = layer.anim_revision;- cache.built_anim_table_revision = layer.anim_table_revision;- }+ auto& cache = ensureChunk(layer, chunkData, coord, slots, resolved, entity, chunkCaches);for (usize si = 0; si < slots.size() && si < cache.slots.size(); ++si) {const SlotMesh& mesh = cache.slots[si];@@ -386,11 +555,13 @@ void TilemapRenderPlugin::collect(RenderCollectContext& collect_ctx) {for (auto it = layer_caches_.begin(); it != layer_caches_.end(); ) {if (layers.find(it->first) == layers.end()) {+ releaseLayer(it->second);it = layer_caches_.erase(it);} else {++it;}}+ if (device_) evictStrips(*device_);}} // namespace esenginediff --git a/src/esengine/renderer/plugins/TilemapRenderPlugin.hpp b/src/esengine/renderer/plugins/TilemapRenderPlugin.hppindex aa1cf80f7..ca6676e89 100644--- a/src/esengine/renderer/plugins/TilemapRenderPlugin.hpp+++ b/src/esengine/renderer/plugins/TilemapRenderPlugin.hpp@@ -17,7 +17,7 @@ namespace tilemap { class TilemapSystem; }class TilemapRenderPlugin : public RenderTypePlugin {public:void init(RenderFrameContext& ctx) override;- void shutdown() override {}+ void shutdown() override;void setTilemapSystem(tilemap::TilemapSystem* system) { tilemap_system_ = system; }@@ -44,6 +44,9 @@ private:// Animation-TABLE revision this cache was built at; every chunk rebuilds// once when the table changes so a static chunk can become animated.u32 built_anim_table_revision = 0;+ // Which build of this chunk's mesh a strip was made from: a counter the plugin hands out, so a strip is stale exactly when a chunk in it+ // was rebuilt after it (the newest serial among its chunks is not the one it recorded).+ u64 mesh_serial = 0;};// A layer slot with its texture resolved for the current frame.@@ -84,12 +87,62 @@ private:};using ChunkMap = std::unordered_map<tilemap::ChunkCoord, ChunkCache, tilemap::ChunkCoordHash>;++ // Geometry the GPU keeps between frames. A layer whose grid sits where it did (no parallax, same tint, same place) draws from here, so a frame+ // in which no chunk changed uploads nothing and copies no vertex. The unit is a STRIP: one row of chunks, thirty-two of them wide, in+ // world space. A strip rather than a block of chunks because the merged draw this replaces laid tiles out chunk row by chunk row, and+ // 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;+ /// 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 {+ i32 sx = 0;+ i32 cy = 0;+ bool operator==(const StripKey&) const = default;+ };+ struct StripKeyHash {+ usize operator()(const StripKey& key) const {+ return std::hash<i64>{}((static_cast<i64>(key.sx) << 32) ^ static_cast<i64>(static_cast<u32>(key.cy)));+ }+ };+ /// Where and how a strip's vertices were placed: everything the world-space geometry depends on besides the chunks themselves.+ struct Placement {+ f32 adjX = 0, adjY = 0, originX = 0, originY = 0, offsetX = 0, offsetY = 0, angle = 0, depth = 0;+ u32 color = 0;+ bool operator==(const Placement&) const = default;+ };+ struct StripSlot {+ BufferHandle vbo = BufferHandle::Invalid;+ BufferHandle ibo = BufferHandle::Invalid;+ u32 vertexCount = 0;+ u32 indexCount = 0;+ u32 vboBytes = 0;+ u32 iboBytes = 0;+ };+ struct Strip {+ std::vector<StripSlot> slots; // by the layer's render-slot order+ Placement placement;+ u64 stamp = 0; // newest mesh serial among the chunks it was built from+ u32 chunkCount = 0; // how many chunks that was+ u64 deviceGeneration = 0; // the device its buffers belong to+ u64 lastUsed = 0; // the collect that last drew it+ bool built = false;+ };+struct LayerCache {u32 generation = 0;LayoutInputs layout;std::vector<tilemap::TilesetSlot> tilesets;std::vector<ResolvedSlot> resolved;ChunkMap chunks;+ std::unordered_map<StripKey, Strip, StripKeyHash> strips;+ // Where the layer was placed the last time it was collected, and until which collect it counts as MOVING. A layer faded, tinted or carried+ // along is placed anew every frame, and a strip is made of every chunk in a row of them, many times what is on screen: for such a layer+ // the vertices are built per frame for the visible chunks, as before, until it has stood still for a while.+ Placement lastPlacement;+ bool lastPlacementKnown = false;+ u64 movingUntil = 0;};using LayerChunkMap = std::unordered_map<Entity, LayerCache>;@@ -99,9 +152,26 @@ private:const std::vector<ResolvedSlot>& resolved,Entity entity, ChunkCache& cache);+ /// The chunk's mesh, rebuilt first when what it was built from has moved on.+ ChunkCache& ensureChunk(const tilemap::TilemapSystem::LayerData& layer, const tilemap::ChunkData& chunkData,+ tilemap::ChunkCoord coord, const std::vector<tilemap::TilesetSlot>& slots,+ const std::vector<ResolvedSlot>& resolved, Entity entity, ChunkMap& chunkCaches);+ /// Gives a strip's buffers back (when they belong to the live device) and forgets them.+ void releaseStrip(GfxDevice& device, Strip& strip);+ void releaseLayer(LayerCache& layer);+ /// Gives back strips no recent collect drew, while the strips of every layer hold more than the budget.+ void evictStrips(GfxDevice& device);+tilemap::TilemapSystem* tilemap_system_ = nullptr;u32 batch_shader_id_ = 0;LayerChunkMap layer_caches_;+ GfxDevice* device_ = nullptr;+ u64 mesh_serial_ = 0;+ u64 collect_serial_ = 0;+ u64 strip_bytes_ = 0;+ std::vector<BatchVertex> strip_vertices_scratch_;+ std::vector<u32> strip_indices_scratch_;+ std::vector<Strip*> visible_strips_scratch_;// Per-frame scratch reused across layers/frames so collect allocates nothing// in the steady state.diff --git a/src/esengine/renderer/rhi/TransientBufferPool.hpp b/src/esengine/renderer/rhi/TransientBufferPool.hppindex eb974c94d..9e337eb15 100644--- a/src/esengine/renderer/rhi/TransientBufferPool.hpp+++ b/src/esengine/renderer/rhi/TransientBufferPool.hpp@@ -28,6 +28,9 @@ class TransientBufferPool {public:explicit TransientBufferPool(GfxDevice& device);+ /// The device the pool's buffers live on, for a plugin that keeps geometry of its own on it.+ GfxDevice& device() { return device_; }+/** Initialize layouts; stream storage is allocated on its first non-empty write. */void init(u32 initialVertexBytes = 2 * 1024 * 1024,u32 initialIndexCount = 256 * 1024);diff --git a/tests/renderer/MockGfxDevice.hpp b/tests/renderer/MockGfxDevice.hppindex 3a22c42bc..902e16987 100644--- a/tests/renderer/MockGfxDevice.hpp+++ b/tests/renderer/MockGfxDevice.hpp@@ -119,6 +119,9 @@ struct MockGfxDevice final : GfxDevice {BufferDesc lastBufferDesc{};bool lastCreateBufferHadData = false;std::vector<u8> lastCreateBufferBytes;+ /// Every buffer created with contents, in order: a caller that makes a vertex buffer and then an index buffer leaves only the last in the above.+ struct CreatedBuffer { u32 id; BufferDesc desc; std::vector<u8> bytes; };+ std::vector<CreatedBuffer> createdBuffers;TextureDesc lastTextureDesc{};bool lastCreateTextureHadPixels = false;std::vector<u8> lastCreateTextureBytes;@@ -225,12 +228,13 @@ struct MockGfxDevice final : GfxDevice {i32 getInt(GfxIntParam) override { return 16; }protected:- bool backendCreateBuffer(u32, const BufferDesc& desc, const void* data) override {+ bool backendCreateBuffer(u32 id, const BufferDesc& desc, const void* data) override {++createBufferCalls;lastBufferDesc = desc;lastCreateBufferHadData = data != nullptr;if (data) {lastCreateBufferBytes.assign(static_cast<const u8*>(data), static_cast<const u8*>(data) + desc.size);+ createdBuffers.push_back({id, desc, lastCreateBufferBytes});} else {lastCreateBufferBytes.clear();}diff --git a/tests/renderer/test_batch_builder.cpp b/tests/renderer/test_batch_builder.cppindex dece5bfbf..3080b5737 100644--- a/tests/renderer/test_batch_builder.cpp+++ b/tests/renderer/test_batch_builder.cpp@@ -178,9 +178,11 @@ TEST_CASE("tilemaps: rotation places cached quads and inverts distant chunk cullFrustum 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 revision = system.getLayerData(entity)->edit_revision;for (f32 angle : {0.0f, .37f, glm::half_pi<f32>(), glm::pi<f32>(), -glm::half_pi<f32>()}) {+ // A plugin per angle: a layer placed anew from one collect to the next is treated as moving and built per frame, which is+ // another test's subject; here each angle is the first time the layer is seen at it.+ TilemapRenderPlugin plugin; plugin.setTilemapSystem(&system); plugin.init(frameContext);const auto rotation = glm::angleAxis(angle, glm::vec3{0, 0, 1});transform.worldPosition = origin; transform.worldRotation = rotation;transform.cachedMatrix_ = glm::translate(glm::mat4{1}, origin) * glm::mat4_cast(rotation);@@ -188,9 +190,18 @@ TEST_CASE("tilemaps: rotation places cached quads and inverts distant chunk cullconst auto center = origin + rotation * localCenter;const auto projection = glm::ortho(center.x - 4, center.x + 4, center.y - 4, center.y + 4);frustum.extractFromMatrix(projection); collect.camera = computeCameraView(projection);- h.pool.beginFrame(); h.list.clear(); plugin.collect(collect);- REQUIRE(h.pool.vertexBytesUsed(LayoutId::Batch) == 4 * sizeof(BatchVertex));- const auto* vertices = reinterpret_cast<const BatchVertex*>(h.pool.vertexData(LayoutId::Batch));+ h.pool.beginFrame(); h.list.clear(); h.device.createdBuffers.clear(); plugin.collect(collect);+ // The layer draws from a strip the GPU holds, so the quad is in the buffer made for it, not in the frame's pool: a draw that names+ // that buffer, for the one tile's six indices.+ REQUIRE(h.list.commandCount() == 1);+ REQUIRE(h.list.command(0).hasPersistentGeometry());+ CHECK(h.list.command(0).index_count == 6);+ REQUIRE(h.pool.vertexBytesUsed(LayoutId::Batch) == 0);+ const MockGfxDevice::CreatedBuffer* vertexBuffer = nullptr;+ for (const auto& made : h.device.createdBuffers) if (made.desc.usage == GfxBufferUsage::Vertex) vertexBuffer = &made;+ REQUIRE(vertexBuffer != nullptr);+ REQUIRE(vertexBuffer->bytes.size() == 4 * sizeof(BatchVertex));+ const auto* vertices = reinterpret_cast<const BatchVertex*>(vertexBuffer->bytes.data());const glm::vec3 offsets[] = {{-8, -12, 0}, {8, -12, 0}, {8, 12, 0}, {-8, 12, 0}};for (u32 i = 0; i < 4; ++i) {const auto expected = origin + rotation * (localCenter + offsets[i]);@@ -204,6 +215,153 @@ TEST_CASE("tilemaps: rotation places cached quads and inverts distant chunk cull}}+TEST_CASE("tilemaps: a layer that has not changed draws again without making or filling a buffer") {+ 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;+ system.initLayer(entity, 64, 64, 16, 16);+ system.setTile(entity, 3, 3, 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, 200.0f, -200.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(); plugin.collect(collect); };++ frame();+ REQUIRE(h.list.commandCount() == 1);+ CHECK(h.list.command(0).hasPersistentGeometry());+ const int made = h.device.createBufferCalls;+ CHECK(made >= 2); // a vertex and an index buffer++ // The same layer, drawn again: the same two buffers, nothing created and nothing uploaded through the frame's pool.+ for (int i = 0; i < 3; ++i) {+ frame();+ REQUIRE(h.list.commandCount() == 1);+ CHECK(h.device.createBufferCalls == made);+ CHECK(h.pool.vertexBytesUsed(LayoutId::Batch) == 0);+ }++ // A tile edited: the strip it is in is made again, once.+ system.setTile(entity, 4, 3, 1);+ frame();+ CHECK(h.device.createBufferCalls > made);+ const int afterEdit = h.device.createBufferCalls;+ frame();+ CHECK(h.device.createBufferCalls == afterEdit);++ // The layer moved: its tiles are somewhere else in the world, so the strip is made again.+ transform.worldPosition = {40, 0, 0};+ transform.cachedMatrix_ = glm::translate(glm::mat4{1}, glm::vec3{40, 0, 0});+ frame();+ CHECK(h.device.createBufferCalls > afterEdit);+ resources.shutdown();+}++TEST_CASE("tilemaps: a layer placed anew every frame is built per frame, and goes back to held geometry once it stands still") {+ 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;+ system.initLayer(entity, 64, 64, 16, 16);+ system.setTile(entity, 3, 3, 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, 200.0f, -200.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(); plugin.collect(collect); };++ frame();+ REQUIRE(h.list.commandCount() == 1);+ CHECK(h.list.command(0).hasPersistentGeometry());++ // Faded a little more each frame: its tint is part of where it is placed, so it is placed anew every frame, and a strip is every chunk of a+ // row of them. It is built for the visible chunks into the frame's pool, as before, and makes no buffer of its own.+ frame();+ // Counted by what was made WITH contents: a strip's buffers are, and the frame's own streams (made when the pool is first written) are not.+ const usize made = h.device.createdBuffers.size();+ for (int i = 1; i <= 6; ++i) {+ component.opacity = 1.0f - 0.1f * static_cast<f32>(i);+ frame();+ REQUIRE(h.list.commandCount() == 1);+ CHECK(!h.list.command(0).hasPersistentGeometry());+ CHECK(h.device.createdBuffers.size() == made);+ }++ // Standing still for a while, it is held on the GPU again.+ for (int i = 0; i < 40; ++i) frame();+ REQUIRE(h.list.commandCount() == 1);+ CHECK(h.list.command(0).hasPersistentGeometry());+ resources.shutdown();+}++TEST_CASE("tilemaps: rows of chunks are drawn from the top row down, each a draw of its own") {+ 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;+ system.initLayer(entity, 64, 64, 16, 16);+ // One tile in each of three chunk rows (a chunk is sixteen tiles tall), set last row first.+ system.setTile(entity, 1, 40, 1);+ system.setTile(entity, 1, 20, 1);+ system.setTile(entity, 1, 2, 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, 1100.0f, -1100.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;+ h.pool.beginFrame(); h.list.clear(); h.device.createdBuffers.clear(); plugin.collect(collect);++ REQUIRE(h.list.commandCount() == 3);+ // The tile of chunk row 0 is the one nearest y = 0, and its draw is first: what the merged draw's order was.+ std::vector<f32> firstY;+ for (u32 i = 0; i < 3; ++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);+ firstY.push_back(reinterpret_cast<const BatchVertex*>(found->bytes.data())[0].position.y);+ }+ CHECK(firstY[0] > firstY[1]);+ CHECK(firstY[1] > firstY[2]);+ 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.mjsindex e67201e9b..1930bdf4e 100644--- a/tools/releaseNotes.mjs+++ b/tools/releaseNotes.mjs@@ -3084,4 +3084,6 @@ export const NOTED = {{"note":"UI text costs less: labels are laid out when they change and submitted by the engine"},"perf(ui): the engine watches the world scale an automatic label picks its glyph pipeline from":{"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"},};