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 shadow map is drawn again only when what it holds changed - every caster of every light was drawn into the atlas every frame, still or not; the frame keeps its atlas and a hash of what each square would draw, and skips the pass when nothing in it moved

yhhb93ee26d6610
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 0835c11b6..d646fad7d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -38,6 +38,7 @@ published separately; it ships inside the editor.
- **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.
+- **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.
- **A shader warm-up waits for the scene still being made.** `warmShaders` (what `quality.prewarmShaders` runs behind the loading screen) stopped at the first three frames in which nothing was compiling. A ground still painting its pages or a field still growing its cells draws seconds later, with shaders nothing had asked for yet, so whether a scene opened complete depended on which arrived first: of the runs of one lake scene, about half opened at 13 s and went on compiling for eight more, the rest at 25 s and complete. Terrain and Foliage now hold the warm-up while they are preparing, and every run opens complete; `holdShaderWarmup(app, busy)` is how a game's own generated content does the same. The wait is still bounded by `prewarmTimeoutMs`.
diff --git a/src/esengine/renderer/draw/DrawList.cpp b/src/esengine/renderer/draw/DrawList.cpp
index 6fa91fd54..83ebc75cc 100644
--- a/src/esengine/renderer/draw/DrawList.cpp
+++ b/src/esengine/renderer/draw/DrawList.cpp
@@ -619,4 +619,39 @@ void DrawList::execute(GfxDevice& device, TransientBufferPool& buffers,
}
}
+u64 DrawList::contentHash(u64 h) const {
+ const auto mix = [&h](u64 v) {
+ h ^= v + 0x9e3779b97f4a7c15ull + (h << 6) + (h >> 2);
+ };
+ mix(commands_.size());
+ for (const DrawCommand& c : commands_) {
+ mix(c.sort_key);
+ mix((static_cast<u64>(c.index_offset) << 32) | c.index_count);
+ mix((static_cast<u64>(c.vertex_byte_offset) << 32) | c.vertex_count);
+ mix((static_cast<u64>(c.shader_id) << 32) | c.material_id);
+ mix((static_cast<u64>(c.instance_count) << 32) | c.instance_stride);
+ mix((static_cast<u64>(static_cast<u32>(c.vertex_buffer)) << 32) | static_cast<u32>(c.index_buffer));
+ mix((static_cast<u64>(static_cast<u32>(c.vertex_layout)) << 32) | static_cast<u32>(c.instance_buffer));
+ mix((static_cast<u64>(c.skin_offset) << 32) | c.skin_count);
+ mix((static_cast<u64>(c.morph_index) << 32) | static_cast<u32>(c.layout_id));
+ mix((static_cast<u64>(static_cast<u16>(c.depth_bias)) << 32) | (static_cast<u64>(c.cull) << 24)
+ | (static_cast<u64>(c.depth_test) << 16) | (static_cast<u64>(c.depth_write) << 8)
+ | static_cast<u64>(c.blend_mode) | (static_cast<u64>(c.topology) << 40));
+ mix(c.texture_count);
+ for (u32 t = 0; t < c.texture_count && t < MAX_CMD_TEXTURE_SLOTS; ++t) mix(c.texture_ids[t]);
+ }
+ const auto words = [&mix](const void* data, usize bytes) {
+ const auto* p = static_cast<const u8*>(data);
+ usize at = 0;
+ for (; at + 8 <= bytes; at += 8) { u64 w; std::memcpy(&w, p + at, 8); mix(w); }
+ u64 tail = 0;
+ if (at < bytes) { std::memcpy(&tail, p + at, bytes - at); mix(tail); }
+ };
+ mix(skin_matrices_.size());
+ if (!skin_matrices_.empty()) words(skin_matrices_.data(), skin_matrices_.size() * sizeof(glm::mat4));
+ mix(morph_shapes_.size());
+ if (!morph_shapes_.empty()) words(morph_shapes_.data(), morph_shapes_.size() * sizeof(MorphConstants));
+ return h;
+}
+
} // namespace esengine
diff --git a/src/esengine/renderer/draw/DrawList.hpp b/src/esengine/renderer/draw/DrawList.hpp
index 99095041a..fda352df8 100644
--- a/src/esengine/renderer/draw/DrawList.hpp
+++ b/src/esengine/renderer/draw/DrawList.hpp
@@ -119,6 +119,15 @@ public:
const DrawCommand* commands() const { return commands_.data(); }
+
+ /**
+ * @brief A hash of what the list draws: each command's program, geometry, state and ranges, and the bone matrices and
+ * shapes it carries. Folded into @p seed.
+ *
+ * @details Not the bytes of the instance records, which live in the pool (hash those beside it), and not what a material
+ * or a texture holds. Two lists with the same hash over the same pool draw the same pixels; read after finalize().
+ */
+ u64 contentHash(u64 seed) const;
const DrawCommand& command(u32 index) const { return commands_[index]; }
// Bit i set ⇒ layer i (0..31) sorts by world Y within the layer (top-down
diff --git a/src/esengine/renderer/frame/RenderFrame.cpp b/src/esengine/renderer/frame/RenderFrame.cpp
index ab65572f2..850684742 100644
--- a/src/esengine/renderer/frame/RenderFrame.cpp
+++ b/src/esengine/renderer/frame/RenderFrame.cpp
@@ -683,8 +683,11 @@ void RenderFrame::publishFrameCounters() {
if (frame_breaks_[r] == 0) continue;
FrameProfiler::get().counter(batchBreakCounter(static_cast<BatchBreak>(r)), frame_breaks_[r]);
}
- ES_PROFILE_COUNTER("render.targets", target_pool_.count());
- ES_PROFILE_COUNTER("render.targets.bytes", target_pool_.bytes());
+ // The shadow atlas is the frame's own and kept, not pooled, and counted with the pool it used to come from: a colour and a
+ // depth texel per atlas texel, four bytes each, as the pool counts its own.
+ const u64 atlasSide = shadow_target_ ? shadow_target_->getWidth() : 0u;
+ ES_PROFILE_COUNTER("render.targets", target_pool_.count() + (shadow_target_ ? 1u : 0u));
+ ES_PROFILE_COUNTER("render.targets.bytes", target_pool_.bytes() + atlasSide * atlasSide * 8u);
// Memory on the other side: pixels and buffers the device copies so they
// survive a loss, which no GPU figure counts.
ES_PROFILE_COUNTER("render.retained.bytes", device_.retainedBytes());
@@ -2511,12 +2514,23 @@ void RenderFrame::buildShadowPlan(ecs::Registry& registry) {
if (!anyTile) return;
// The texture must exist before scene collection puts it in receiver sort keys.
- rg::TargetDesc desc;
- desc.width = atlasSize;
- desc.height = atlasSize;
- desc.depthStencil = true;
- desc.linearFilter = false;
- shadow_resource_ = graph_.createExternalTarget(desc);
+ // Owned and kept, not pooled: what was drawn into it last frame is what lets this frame skip drawing it.
+ if (!shadow_target_ || shadow_target_->getWidth() != atlasSize) {
+ FramebufferSpec spec;
+ spec.width = atlasSize;
+ spec.height = atlasSize;
+ spec.depthStencil = true;
+ spec.linearFilter = false;
+ spec.colorFormat = GfxPixelFormat::RGBA8;
+ shadow_target_ = Framebuffer::create(device_, spec);
+ shadow_drawn_valid_ = false;
+ }
+ if (!shadow_target_) {
+ shadow_views_.clear();
+ return;
+ }
+ shadow_resource_ = graph_.importTarget(shadow_target_->handle(), atlasSize, atlasSize, /*output=*/false,
+ shadow_target_->getColorAttachment());
if (shadow_resource_ == rg::kNoResource) {
shadow_views_.clear();
return;
@@ -2537,7 +2551,33 @@ void RenderFrame::buildShadowPlan(ecs::Registry& registry) {
}
void RenderFrame::declareShadowPass(ecs::Registry& registry) {
- if (shadow_resource_ == rg::kNoResource) return;
+ if (shadow_resource_ == rg::kNoResource) {
+ // A frame that plans no map gives the atlas back, as the pool it came from gave back what no frame asked for.
+ shadow_target_.reset();
+ shadow_drawn_valid_ = false;
+ return;
+ }
+
+ // Collected now rather than when the pass runs: whether it runs at all is decided by what it would draw. (Before the camera's
+ // own collect, as the casters' level of detail always assumed: they cast what the camera settled on last frame.)
+ u64 key = 0;
+ const bool trusted = collectShadowViews(registry, key);
+ shadow_reused_ = trusted && shadow_drawn_valid_ && key == shadow_drawn_key_;
+ ES_PROFILE_COUNTER("render.shadow.reused", shadow_reused_ ? 1u : 0u);
+ if (shadow_reused_) {
+ // What this frame drew (nothing) and, apart from it, what the atlas it reads holds: the maps and draws of the frame that drew it.
+ ES_PROFILE_COUNTER("render.shadow.tiles", 0u);
+ ES_PROFILE_COUNTER("render.shadow.draws", 0u);
+ ES_PROFILE_COUNTER("render.shadow.heldTiles", shadow_held_tiles_);
+ ES_PROFILE_COUNTER("render.shadow.heldDraws", shadow_held_draws_);
+ ES_PROFILE_COUNTER("render.shadow.castersCulled", shadow_casters_culled_);
+ } else {
+ // Recorded by the pass when it runs, not here: a pass the graph culls drew nothing, and its key would vouch for an atlas
+ // that never held what it says.
+ shadow_drawn_valid_ = false;
+ shadow_pending_key_ = key;
+ shadow_pending_trusted_ = trusted;
+ }
rg::PassDesc shadow;
shadow.name = "shadow-atlas";
@@ -2553,8 +2593,9 @@ void RenderFrame::declareShadowPass(ecs::Registry& registry) {
// collect comes back empty reads as no occluder rather than as a black wall.
shadow.clearColor[0] = shadow.clearColor[1] = shadow.clearColor[2] = 1.0f;
shadow.clearColor[3] = 1.0f;
- shadow.execute = [this, &registry](const rg::PassContext&) { executeShadowPass(registry); };
- graph_.addPass(std::move(shadow));
+ shadow.execute = [this](const rg::PassContext&) { executeShadowPass(); };
+ // The atlas already holds what this would draw: no pass, and every reader samples last frame's squares, which are this frame's.
+ if (!shadow_reused_) graph_.addPass(std::move(shadow));
#ifdef ES_ENABLE_POSTPROCESS
if (cloud_tile_ < 0 || !post_process_) return;
// The sky draws what of the sun its clouds leave into the sun's tile, before the scene reads it: white above, that in the alpha.
@@ -2587,14 +2628,8 @@ void RenderFrame::releaseShadowFrameUbos() {
shadow_frame_ubos_.clear();
}
-void RenderFrame::executeShadowPass(ecs::Registry& registry) {
- // What the pass cost, for a profiler and the gates that pin it: the maps it
- // actually rendered and the draws they took. Neither shows in a pixel, and a
- // face that stopped being skipped would look exactly like one that never was.
- u32 shadowTiles = 0;
- u32 shadowDraws = 0;
+bool RenderFrame::collectShadowViews(ecs::Registry& registry, u64& key) {
u32 shadowCastersCulled = 0;
-
const usize viewCount = shadow_views_.size();
// Reset once for the pass, NOT once per view: a draw carries an offset rather
// than the bytes behind it, so six faces reset to zero are six draws reading
@@ -2641,6 +2676,67 @@ void RenderFrame::executeShadowPass(ecs::Registry& registry) {
shadowCastersCulled += collectCtx.shadowCulled;
list.finalize(shadow_pool_);
}
+ shadow_casters_culled_ = shadowCastersCulled;
+
+ // What the squares would draw, as one number. Everything a depth draw reads: where each square is and what it looks through,
+ // the draws, their records in the pool, and the materials of the few that carry one (a cut-out's alpha, a bent caster). Beside
+ // them, what would make the same handles draw something else: a lost device, geometry rewritten in place, another atlas.
+ u64 h = 0xcbf29ce484222325ull;
+ const auto mix = [&h](u64 v) { h ^= v + 0x9e3779b97f4a7c15ull + (h << 6) + (h >> 2); };
+ const auto words = [&mix](const void* data, usize bytes) {
+ const auto* p = static_cast<const u8*>(data);
+ usize at = 0;
+ for (; at + 8 <= bytes; at += 8) { u64 w; std::memcpy(&w, p + at, 8); mix(w); }
+ u64 tail = 0;
+ if (at < bytes) { std::memcpy(&tail, p + at, bytes - at); mix(tail); }
+ };
+ bool trusted = true;
+ mix(device_.deviceGeneration());
+ mix(device_.geometryWrites());
+ mix(static_cast<u64>(shadow_target_ ? static_cast<u32>(shadow_target_->handle()) : 0u));
+ mix(viewCount);
+ const MaterialStore& materials = context_.materials();
+ for (usize vi = 0; vi < viewCount; ++vi) {
+ const ShadowView& view = shadow_views_[vi];
+ words(&view.view_projection, sizeof(glm::mat4));
+ mix((static_cast<u64>(static_cast<u32>(view.x)) << 32) | static_cast<u32>(view.y));
+ mix(view.size);
+ const DrawList& list = shadow_view_lists_[vi];
+ h = list.contentHash(h);
+ for (u32 c = 0; c < list.commandCount(); ++c) {
+ const DrawCommand& cmd = list.command(c);
+ // Records a compute pass wrote are not in the pool to be read.
+ if (cmd.instance_buffer != BufferHandle::Invalid) trusted = false;
+ if (cmd.material_id == 0) continue;
+ // A material that moves its vertices may move them by the clock (wind): the same draw, another shape.
+ if (materials.hasVertexHook(cmd.material_id)) { trusted = false; continue; }
+ if (const MaterialRecord* m = materials.find(cmd.material_id)) {
+ words(m->uboBytes.data(), m->uboBytes.size());
+ for (const auto& binding : m->textures) mix(binding.texture.id());
+ }
+ }
+ }
+ for (u32 layout = 0; layout < LAYOUT_COUNT; ++layout) {
+ const auto id = static_cast<LayoutId>(layout);
+ const u32 used = shadow_pool_.vertexBytesUsed(id);
+ mix(used);
+ if (used > 0) words(shadow_pool_.vertexData(id), used);
+ // Indices built this frame are not read back here; a caster drawn through them is drawn every frame.
+ if (shadow_pool_.indicesUsed(id) > 0) trusted = false;
+ }
+ key = h;
+ return trusted;
+}
+
+void RenderFrame::executeShadowPass() {
+ // What the pass cost, for a profiler and the gates that pin it: the maps it
+ // actually rendered and the draws they took. Neither shows in a pixel, and a
+ // face that stopped being skipped would look exactly like one that never was.
+ u32 shadowTiles = 0;
+ u32 shadowDraws = 0;
+ const usize viewCount = shadow_views_.size();
+ shadow_drawn_key_ = shadow_pending_key_;
+ shadow_drawn_valid_ = shadow_pending_trusted_;
shadow_pool_.upload();
for (usize vi = 0; vi < viewCount; ++vi) {
@@ -2672,7 +2768,11 @@ void RenderFrame::executeShadowPass(ecs::Registry& registry) {
ES_PROFILE_COUNTER("render.shadow.tiles", shadowTiles);
ES_PROFILE_COUNTER("render.shadow.draws", shadowDraws);
- ES_PROFILE_COUNTER("render.shadow.castersCulled", shadowCastersCulled);
+ shadow_held_tiles_ = shadowTiles;
+ shadow_held_draws_ = shadowDraws;
+ ES_PROFILE_COUNTER("render.shadow.heldTiles", shadow_held_tiles_);
+ ES_PROFILE_COUNTER("render.shadow.heldDraws", shadow_held_draws_);
+ ES_PROFILE_COUNTER("render.shadow.castersCulled", shadow_casters_culled_);
ES_PROFILE_COUNTER("render.shadow.cullViews", [&] { u32 n = 0; for (const ShadowView& v : shadow_views_) n += v.sun ? 1u : 0u; return n; }());
// The scene draws next, through pipelines nothing here matches.
device_.invalidatePipelineCache();
diff --git a/src/esengine/renderer/frame/RenderFrame.hpp b/src/esengine/renderer/frame/RenderFrame.hpp
index e5fef107a..2f597df89 100644
--- a/src/esengine/renderer/frame/RenderFrame.hpp
+++ b/src/esengine/renderer/frame/RenderFrame.hpp
@@ -884,9 +884,13 @@ private:
* for the collect the pass does, and outlives execute().
*/
void declareShadowPass(ecs::Registry& registry);
- /// The shadow pass's body: for each planned square, collect the occluders it can
- /// see and draw them. The graph has bound and cleared the atlas.
- void executeShadowPass(ecs::Registry& registry);
+ /// Collects every planned square's occluders into its list, and answers what they would draw as one number
+ /// (shadow_drawn_key_): the squares, their views, the draws, their records and their materials. False when
+ /// something in them can change without the number changing (a material that moves its vertices, records a
+ /// compute pass wrote), and the atlas must be drawn whatever the number says.
+ bool collectShadowViews(ecs::Registry& registry, u64& key);
+ /// The shadow pass's body: the lists collectShadowViews made, drawn. The graph has bound and cleared the atlas.
+ void executeShadowPass();
/**
* @brief Gathers the frame's 2D occluders and hands the casting lights a mask channel.
@@ -1187,6 +1191,22 @@ private:
/// One per view, kept across frames for the capacity. The whole pass is collected
/// before any of it is drawn, so a view's commands have to outlive its collect.
std::vector<DrawList> shadow_view_lists_;
+ /// The atlas, kept from frame to frame so that a frame whose squares would draw what they already hold need not draw them:
+ /// a still sun over a still scene, a lamp in a room. Pooled, it was any target of its size the graph had free that frame.
+ Unique<Framebuffer> shadow_target_;
+ /// What the atlas holds: the key collectShadowViews made the frame it was last drawn, and whether that drawing can be trusted
+ /// to stand. Drawing it again or not is the only thing the key decides.
+ u64 shadow_drawn_key_ = 0;
+ bool shadow_drawn_valid_ = false;
+ /// What this frame's pass will vouch for once it has drawn.
+ u64 shadow_pending_key_ = 0;
+ bool shadow_pending_trusted_ = false;
+ /// The frame's own answer, from collectShadowViews to the pass that draws (or does not).
+ bool shadow_reused_ = false;
+ u32 shadow_casters_culled_ = 0;
+ /// The maps and draws the atlas holds, from the frame that last drew it (render.shadow.heldTiles / heldDraws).
+ u32 shadow_held_tiles_ = 0;
+ u32 shadow_held_draws_ = 0;
/// One FrameConstants buffer per view, grown to the busiest frame and never shrunk.
/// A buffer write lands on the queue, not between two draws already recorded into
/// an open pass — so what tells one view from another is which buffer is BOUND.
diff --git a/src/esengine/renderer/rhi/GfxDevice.cpp b/src/esengine/renderer/rhi/GfxDevice.cpp
index d97815bcf..a81fceeb2 100644
--- a/src/esengine/renderer/rhi/GfxDevice.cpp
+++ b/src/esengine/renderer/rhi/GfxDevice.cpp
@@ -306,6 +306,7 @@ void GfxDevice::updateBuffer(BufferHandle buffer, u32 offsetBytes, const void* d
const u32 id = static_cast<u32>(buffer);
auto* record = registry_.buffers.find(id);
if (!record || !data || sizeBytes == 0) return;
+ countGeometryWrite(record->desc);
const bool whole = offsetBytes == 0 && sizeBytes >= record->desc.size;
if (!record->bytes.empty() && static_cast<usize>(offsetBytes) + sizeBytes <= record->bytes.size()) {
std::memcpy(record->bytes.data() + offsetBytes, data, sizeBytes);
@@ -325,6 +326,7 @@ void GfxDevice::resizeBuffer(BufferHandle buffer, u32 sizeBytes, const void* dat
const u32 id = static_cast<u32>(buffer);
auto* record = registry_.buffers.find(id);
if (!record) return;
+ countGeometryWrite(record->desc);
record->desc.size = sizeBytes;
if (record->content.kind() == GfxContentKind::Retained || (!record->realized && data)) {
std::vector<u8> bytes(sizeBytes, 0);
diff --git a/src/esengine/renderer/rhi/GfxDevice.hpp b/src/esengine/renderer/rhi/GfxDevice.hpp
index 02655e54f..57254ddde 100644
--- a/src/esengine/renderer/rhi/GfxDevice.hpp
+++ b/src/esengine/renderer/rhi/GfxDevice.hpp
@@ -120,6 +120,15 @@ public:
*/
u64 deviceGeneration() const { return device_generation_; }
+ /**
+ * @brief How many times a static vertex or index buffer has been written or resized since the device was made.
+ *
+ * @details What a picture kept from an earlier frame (a shadow map drawn again only when what it holds changed) asks to
+ * know that geometry it names by handle is still the geometry it drew: a handle is never reused, but a static
+ * buffer can still be rewritten in place. Dynamic buffers (a frame's own vertex pool) are not counted.
+ */
+ u64 geometryWrites() const { return geometry_writes_; }
+
/** @brief The loss report, or null while the device is Live. */
const GfxDeviceLostInfo* deviceLostInfo() const {
return device_status_ == GfxDeviceStatus::Live ? nullptr : &device_info_;
@@ -1016,6 +1025,10 @@ private:
GfxDeviceLostInfo device_info_;
u64 device_frame_ = 0;
u64 device_generation_ = 0;
+ u64 geometry_writes_ = 0;
+ void countGeometryWrite(const BufferDesc& desc) {
+ if (!desc.dynamic && (desc.usage == GfxBufferUsage::Vertex || desc.usage == GfxBufferUsage::Index)) ++geometry_writes_;
+ }
std::function<void(const GfxDeviceLostInfo&)> device_lost_handler_;
GfxResourceRegistry registry_;
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index 94138bfee..3b9835f2d 100644
--- a/tools/releaseNotes.mjs
+++ b/tools/releaseNotes.mjs
@@ -4794,4 +4794,6 @@ export const NOTED = {
{"note": "A playable opens on a start screen."},
"fix(playable): a playable's start-screen bar moves through the whole boot - it was weighed as a web page's, whose start is mostly fetching the engine, so a page with everything inlined stood at 82% within a second and crawled for the rest; a playable weighs its own stages, and the scene's is followed through loading, shaders compiled and the world still being made, which a warm-up hold can now count (holdShaderWarmup answers how much is left)":
{"note": "A playable's start-screen bar moves through the whole boot."},
+ "perf(render): a shadow map is drawn again only when what it holds changed - every caster of every light was drawn into the atlas every frame, still or not; the frame keeps its atlas and a hash of what each square would draw, and skips the pass when nothing in it moved":
+ {"note": "A shadow map is drawn again only when what it holds changed."},
};
diff --git a/tools/renderScenes.mjs b/tools/renderScenes.mjs
index 6cbbaea54..3b33d1613 100644
--- a/tools/renderScenes.mjs
+++ b/tools/renderScenes.mjs
@@ -334,6 +334,8 @@ export const SCENES = [
// x, so the shadow lands 100 along +x and x 75..175 is floor the light cannot see.
// The blocker itself stays lit — shadowing one's own caster is the classic bug.
{ id: "mesh-shadow", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_SCENE: "/scenes/mesh-shadow.esscene", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "2", ESTELLA_VERIFY_COUNTERS: "{\"render.shadow.atlasBudget\":2048,\"render.shadow.atlasSize\":1024,\"render.targets.bytes\":8388608}", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.708,\"y\":0.5,\"rgb\":[0,0,0],\"tol\":20},{\"x\":0.167,\"y\":0.5,\"rgb\":[228,228,228],\"tol\":20},{\"x\":0.5,\"y\":0.5,\"rgb\":[228,228,228],\"tol\":20}]" } },
+ // A shadow map kept from frame to frame follows its caster: drawn, kept for frames (render.shadow.reused), then the blocker moves up, across the light, so nothing but its record says it moved. A stale map would leave the old shadow at (0.708, 0.5).
+ { id: "mesh-shadow-moved", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_SCENE: "/scenes/mesh-shadow.esscene", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "2", ESTELLA_VERIFY_SET_FIELD: "[{\"entity\":2,\"component\":\"Transform\",\"key\":\"position\",\"value\":{\"x\":0,\"y\":0,\"z\":0},\"steps\":10},{\"entity\":2,\"component\":\"Transform\",\"key\":\"position\",\"value\":{\"x\":0,\"y\":150,\"z\":0},\"steps\":3}]", ESTELLA_VERIFY_COUNTERS: "{\"render.shadow.reused\":1}", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.708,\"y\":0.5,\"rgb\":[228,228,228],\"tol\":20},{\"x\":0.708,\"y\":0.25,\"rgb\":[0,0,0],\"tol\":20},{\"x\":0.5,\"y\":0.25,\"rgb\":[228,228,228],\"tol\":20}]" } },
// The same shadow turned 30 degrees with a hard source, so its edges cross the map's
// texel grid; a filter that draws that grid into them strays by a quarter pixel.
{ id: "mesh-shadow-tilt", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_SCENE: "/scenes/mesh-shadow-tilt.esscene", ESTELLA_VERIFY_W: "512", ESTELLA_VERIFY_H: "512", ESTELLA_VERIFY_STEPS: "2", ESTELLA_VERIFY_EDGE: "{\"box\":[0.6,0.36,0.9,0.52],\"lit\":228,\"dark\":0,\"maxJag\":0.12}", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.167,\"y\":0.5,\"rgb\":[228,228,228],\"tol\":20}]" } },
@@ -379,7 +381,7 @@ export const SCENES = [
// A POINT light opens no cone, so its map is a cube and a fragment is shadowed by the face
// it lands in: report one tile for every tile and both shadows come back lit. The casters
// are probed too, and the sixth face never renders — nothing above the lamp is a mesh.
- { id: "point-shadow", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_SCENE: "/scenes/point-shadow.esscene", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "2", ESTELLA_VERIFY_COUNTERS: "{\"render.shadow.tiles\":5,\"render.targets\":1,\"render.targets.bytes\":33554432}", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.167,\"y\":0.5,\"rgb\":[0,0,0],\"tol\":20},{\"x\":0.5,\"y\":0.833,\"rgb\":[0,0,0],\"tol\":20},{\"x\":0.333,\"y\":0.5,\"rgb\":[108,108,108],\"tol\":25},{\"x\":0.833,\"y\":0.5,\"rgb\":[102,102,102],\"tol\":25},{\"x\":0.5,\"y\":0.5,\"rgb\":[242,242,242],\"tol\":20}]" } },
+ { id: "point-shadow", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_SCENE: "/scenes/point-shadow.esscene", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "2", ESTELLA_VERIFY_COUNTERS: "{\"render.shadow.heldTiles\":5,\"render.targets\":1,\"render.targets.bytes\":33554432}", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.167,\"y\":0.5,\"rgb\":[0,0,0],\"tol\":20},{\"x\":0.5,\"y\":0.833,\"rgb\":[0,0,0],\"tol\":20},{\"x\":0.333,\"y\":0.5,\"rgb\":[108,108,108],\"tol\":25},{\"x\":0.833,\"y\":0.5,\"rgb\":[102,102,102],\"tol\":25},{\"x\":0.5,\"y\":0.5,\"rgb\":[242,242,242],\"tol\":20}]" } },
// A shadow map holds depth, so only geometry drawn depth-only belongs in one. A BLACK
// sprite stands opposite a mesh blocker, both between lamp and floor: black unpacks as
// depth 0, so a collected sprite would shadow its half — which has to come back lit.
@@ -387,11 +389,11 @@ export const SCENES = [
// A lamp with every mesh off to one side. Four of its six faces miss the box the whole
// of the geometry sits in and never gather at all; a fifth gathers and comes back empty.
// Both skips are invisible in a pixel, which is what the two counters are here for.
- { id: "point-shadow-offset", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_SCENE: "/scenes/point-shadow-offset.esscene", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "2", ESTELLA_VERIFY_COUNTERS: "{\"render.shadow.tiles\":2,\"render.shadow.collects\":3}", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.20,\"y\":0.5,\"rgb\":[0,0,0],\"tol\":20},{\"x\":0.35,\"y\":0.5,\"rgb\":[135,135,135],\"tol\":25},{\"x\":0.5,\"y\":0.5,\"rgb\":[191,191,191],\"tol\":25},{\"x\":0.6,\"y\":0.5,\"rgb\":[173,173,173],\"tol\":25}]" } },
+ { id: "point-shadow-offset", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_SCENE: "/scenes/point-shadow-offset.esscene", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "2", ESTELLA_VERIFY_COUNTERS: "{\"render.shadow.heldTiles\":2,\"render.shadow.collects\":3}", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.20,\"y\":0.5,\"rgb\":[0,0,0],\"tol\":20},{\"x\":0.35,\"y\":0.5,\"rgb\":[135,135,135],\"tol\":25},{\"x\":0.5,\"y\":0.5,\"rgb\":[191,191,191],\"tol\":25},{\"x\":0.6,\"y\":0.5,\"rgb\":[173,173,173],\"tol\":25}]" } },
// TWO point lights, which nothing else in the corpus has: twelve faces claimed from a
// sixteen-cell atlas, ten of them drawn. The dark probes are dim and not black because the
// far lamp reaches that floor past the near blocker — the second map read, not a leak.
- { id: "point-shadow-pair", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_SCENE: "/scenes/point-shadow-pair.esscene", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "2", ESTELLA_VERIFY_COUNTERS: "{\"render.shadow.tiles\":10,\"render.shadow.draws\":10}", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.15,\"y\":0.5,\"rgb\":[66,66,66],\"tol\":20},{\"x\":0.85,\"y\":0.5,\"rgb\":[66,66,66],\"tol\":20},{\"x\":0.25,\"y\":0.5,\"rgb\":[220,220,220],\"tol\":25},{\"x\":0.5,\"y\":0.5,\"rgb\":[255,255,255],\"tol\":20}]" } },
+ { id: "point-shadow-pair", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_SCENE: "/scenes/point-shadow-pair.esscene", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "2", ESTELLA_VERIFY_COUNTERS: "{\"render.shadow.heldTiles\":10,\"render.shadow.heldDraws\":10}", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.15,\"y\":0.5,\"rgb\":[66,66,66],\"tol\":20},{\"x\":0.85,\"y\":0.5,\"rgb\":[66,66,66],\"tol\":20},{\"x\":0.25,\"y\":0.5,\"rgb\":[220,220,220],\"tol\":25},{\"x\":0.5,\"y\":0.5,\"rgb\":[255,255,255],\"tol\":20}]" } },
// A SUN WITH A SIZE: its source is infinitely far away, so what it carries is an angle
// and not a length — the penumbra is tan(sourceAngle/2) times how far the caster stands
// above what it falls on.
@@ -467,7 +469,7 @@ export const SCENES = [
// Does an atlas draw scale with the CASTER count: 3249 casters are two draws (a ground quad,
// then every cube instanced), and a fallback to one draw each changes no pixel. The probes take
// the only ground the grid leaves bare — lit past its left edge, shadowed past its right.
- { id: "shadow-scale-cost", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_SCENE: "/scenes/scale-shadow.esscene", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "2", ESTELLA_VERIFY_SCALE: "{\"copies\":3249,\"cols\":57,\"spacing\":[10,10],\"keep\":[\"Camera\",\"Light\",\"Ground\"]}", ESTELLA_VERIFY_COUNTERS_MAX: "{\"render.shadow.draws\":8,\"render.targets.bytes\":33554432}", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.012,\"y\":0.5,\"rgb\":[228,228,228],\"tol\":20},{\"x\":0.984,\"y\":0.5,\"rgb\":[0,0,0],\"tol\":20}]" } },
+ { id: "shadow-scale-cost", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_SCENE: "/scenes/scale-shadow.esscene", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "2", ESTELLA_VERIFY_SCALE: "{\"copies\":3249,\"cols\":57,\"spacing\":[10,10],\"keep\":[\"Camera\",\"Light\",\"Ground\"]}", ESTELLA_VERIFY_COUNTERS_MAX: "{\"render.shadow.heldDraws\":8,\"render.targets.bytes\":33554432}", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.012,\"y\":0.5,\"rgb\":[228,228,228],\"tol\":20},{\"x\":0.984,\"y\":0.5,\"rgb\":[0,0,0],\"tol\":20}]" } },
// Two opaque cubes overlapping on screen, the red one 120 units nearer. Declares NO
// depth layer and boots LINEAR — the cheap way to engage the post-process capture.
// Opaque sorts front-to-back, so with no attachment the far cube paints over.