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 mesh's draw keeps what no view changes between frames - where it stands, its world box, its model and normal matrices, its material's say over culling and its program facts are derived once and kept until its Transform or MeshRenderer is written, its mesh realized again, its material changed or the render origin moves; a static mesh that did not move is passed over by the motion pass before its cull, and the change audit checks every kept draw against a fresh one

yhh81a012d09cb1
diff --git a/CHANGELOG.md b/CHANGELOG.md
index a952b8be8..c57d455d0 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -54,6 +54,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.
+- **A mesh's draw keeps what no view changes.** Every view of every frame derived each mesh's world box, model matrix, normal matrix, material lookups and program facts again, and looked up its motion pose three times in a hash map. What does not depend on the view is now kept per entity and used until something it came from changes: the entity's Transform or MeshRenderer is written (its change stamp), its mesh is realized again (the mesh's revision), its material changes or any material is added or removed (`MaterialStore::revision`, new), a Billboard, MeshInstances, LODGroup, Impostor, MeshSkin or MeshMorph is added to or taken from it, or the render origin moves. A mesh with one of those components, a parallax, or a view that overrides the look (wireframe, clay, weights) is derived every walk as before. The motion pass now passes over a static mesh that did not move before culling it. With the change audit on, every kept draw is compared with a fresh derivation and a difference is logged (`render.mesh.proxy.stale`); `render.mesh.proxy.kept` and `.built` count the draws kept and derived again. On Bistro (1,143 meshes) the mesh collect's share of the sampled frame went from 23% to 16%, and the motion pass's walk to nothing measurable. With the audit on, 26 examples (Bistro, physics, streaming worlds, terrain, a rig) found no kept draw that differed; with the check of the Transform's stamp taken out, physics-3d reported 365. A renderer that walks another registry (a scene loaded again) keeps nothing from the last one (`Registry::identity`, new).
- **A run of mesh draws is one call on WebGL2.** Consecutive mesh draws that differ only in which range of a shared buffer they draw and which objects they place (the same program, material, textures and state) were each a call of their own, with two blocks bound for each: where its objects' records start, and the light probes its objects stand in. Where the browser offers `WEBGL_multi_draw`, such a run is now one call: a block staged once for the run says, for each draw, where its records and its instances' probes begin, and the vertex stage finds its own by `gl_DrawID`. A run of draws that carry probe light shares one probe block, so it holds at most as many objects as that block does. WebGPU, and a browser without the extension, draw as before. On Bistro at 640x360 the instanced draw calls a frame went from 538 to 71 plus 73 multi-draw calls, uniform-range binds from 1,022 to 276, and the WebGL calls from about 3,100 to about 1,920; the renderer's CPU time went from 1.2 to 1.1 ms. `render.multiDraw.calls` counts the runs drawn.
- **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.
diff --git a/src/esengine/ecs/Registry.hpp b/src/esengine/ecs/Registry.hpp
index b8bcc0351..6990cce3b 100644
--- a/src/esengine/ecs/Registry.hpp
+++ b/src/esengine/ecs/Registry.hpp
@@ -21,6 +21,7 @@
// Project includes
#include "../core/Log.hpp"
+#include "../core/Revision.hpp"
#include "../core/Types.hpp"
#include "ComponentMask.hpp"
#include "Entity.hpp"
@@ -155,6 +156,13 @@ public:
/** @brief The tick a write made now is stamped with. */
u32 changeTick() const { return change_tick_; }
+ /**
+ * @brief Which registry this is, never shared with another made in this process (a registry assigned over this one brings
+ * its own). Ticks and entities are each registry's own, so a reader that keeps what it derived from one asks this
+ * before trusting a tick or an entity it kept.
+ */
+ u64 identity() const { return identity_; }
+
/**
* @brief For a reader about to look: the tick it is looking at, after which the clock moves on.
* @details Keep the answer; next time, changedSince(it) is what was written since this look. Stamps older than half the
@@ -212,6 +220,13 @@ public:
return pool && tickAfter(pool->lastChangedTick(), tick);
}
+ /** @brief The tick of the latest write, addition or removal of any T; 0 when there has been none. */
+ template<typename T>
+ u32 lastChangedTick() const {
+ const auto* pool = getPool<T>();
+ return pool ? pool->lastChangedTick() : 0;
+ }
+
/**
* @brief Turns the change audit on or off. On, auditChanges() compares every auditable component with the copy it kept and
* reports each one whose bytes changed with no stamp to say so — a write some consumer will never see.
@@ -1004,6 +1019,8 @@ private:
/// The tick a write is stamped with (see beginChangeRead). Starts at 1: 0 is "never written".
u32 change_tick_ = 1;
+ /// See identity(): drawn from the counter every revision comes from, so no two registries share one.
+ u64 identity_ = nextRevision();
bool change_audit_ = false;
std::vector<SparseSetBase::AuditCopy> audit_copies_;
std::vector<u8> audit_reported_;
diff --git a/src/esengine/renderer/plugins/MeshPlugin.cpp b/src/esengine/renderer/plugins/MeshPlugin.cpp
index f307ff2ca..3b1da07e6 100644
--- a/src/esengine/renderer/plugins/MeshPlugin.cpp
+++ b/src/esengine/renderer/plugins/MeshPlugin.cpp
@@ -323,16 +323,52 @@ MeshProgramFacts meshFactsFromEntity(RenderFrameContext& ctx, ecs::Registry& reg
return facts;
}
-MeshDrawKeys meshDrawFor(RenderFrameContext& ctx, ecs::Registry& registry, Entity entity,
- const ecs::MeshRenderer& mesh, const Mesh* resident, bool shadowDepth,
- bool& warnedBones, bool weightHeat = false) {
- const MeshProgramFacts facts =
- meshFactsFromEntity(ctx, registry, entity, mesh, resident, warnedBones);
+/** @brief The program a draw with these facts is asked for in this frame and pass: the frame's half of meshDrawFor. */
+MeshDrawKeys meshDrawFrom(RenderFrameContext& ctx, Entity entity, const MeshProgramFacts& facts, bool shadowDepth,
+ bool weightHeat = false) {
return { meshVariantFrom(facts, ctx.environment_texture_id != 0
|| (!shadowDepth && ctx.frame && ctx.frame->planarTextureFor(entity) != 0), shadowDepth, ctx.lighting_direct, weightHeat,
ctx.gbuffer && !shadowDepth),
facts.normalTextureId };
}
+
+MeshDrawKeys meshDrawFor(RenderFrameContext& ctx, ecs::Registry& registry, Entity entity,
+ const ecs::MeshRenderer& mesh, const Mesh* resident, bool shadowDepth,
+ bool& warnedBones, bool weightHeat = false) {
+ return meshDrawFrom(ctx, entity, meshFactsFromEntity(ctx, registry, entity, mesh, resident, warnedBones), shadowDepth,
+ weightHeat);
+}
+
+/** @brief The normal map's id as this frame has it, for facts kept without one: a texture's id is not kept. */
+u32 normalTextureOf(RenderFrameContext& ctx, const ecs::MeshRenderer& mesh, const MeshProgramFacts& facts) {
+ if (!mesh.normalMap.isValid() || !facts.lit || !facts.hasNormals) return 0;
+ Texture* tex = ctx.resources.getTexture(mesh.normalMap);
+ return tex ? tex->getId() : 0;
+}
+
+/**
+ * @brief The components whose presence keeps a draw from being kept (MeshPlugin::MeshProxy), one bit each: each turns, picks
+ * or poses the mesh per view or per frame. Read where the walk reads them.
+ */
+u8 ruledOut(ecs::Registry& registry, Entity entity) {
+ return (registry.has<ecs::Billboard>(entity) ? 1u : 0u)
+ | (registry.has<ecs::MeshInstances>(entity) ? 2u : 0u)
+ | (registry.has<ecs::LODGroup>(entity) ? 4u : 0u)
+ | (registry.has<ecs::Impostor>(entity) ? 8u : 0u)
+ | (registry.has<ecs::MeshSkin>(entity) ? 16u : 0u)
+ | (registry.has<ecs::MeshMorph>(entity) ? 32u : 0u);
+}
+
+/** @brief The latest tick any of ruledOut's components was written, added or removed at. */
+u32 ruledOutChangedAt(const ecs::Registry& registry) {
+ u32 latest = 0;
+ for (const u32 t : {registry.lastChangedTick<ecs::Billboard>(), registry.lastChangedTick<ecs::MeshInstances>(),
+ registry.lastChangedTick<ecs::LODGroup>(), registry.lastChangedTick<ecs::Impostor>(),
+ registry.lastChangedTick<ecs::MeshSkin>(), registry.lastChangedTick<ecs::MeshMorph>()}) {
+ if (latest == 0 || (t != 0 && ecs::tickAfter(t, latest))) latest = t;
+ }
+ return latest;
+}
} // namespace
void MeshPlugin::init(RenderFrameContext& ctx) {
@@ -351,6 +387,10 @@ void MeshPlugin::init(RenderFrameContext& ctx) {
}
mesh_compiled_.fill(false);
mesh_programs_.fill(0);
+ // A renderer rebuilt has resources rebuilt under it: nothing derived before is kept.
+ proxies_.clear();
+ proxy_slot_.clear();
+ ++proxy_epoch_;
// The base variant now, so a broken shader is a boot-time failure rather than
// one that waits for the first mesh; the rest compile when a draw asks.
meshProgram(ctx, 0);
@@ -512,9 +552,26 @@ RenderPrewarmResult MeshPlugin::prewarm(RenderFrameContext& ctx, ecs::Registry&
anyResident);
}
-void MeshPlugin::trackPose(const RenderFrameContext& ctx, Entity entity, const glm::mat4& model,
+MeshPlugin::Pose* MeshPlugin::poseOf(const RenderFrameContext& ctx, Entity entity, MeshProxy* kept, bool make) {
+ const u64 key = poseKey(ctx, entity);
+ if (kept && kept->pose && kept->poseKey == key && kept->poseGeneration == pose_generation_) return kept->pose;
+ Pose* pose = nullptr;
+ if (make) {
+ pose = &poses_[key];
+ } else {
+ const auto found = poses_.find(key);
+ if (found != poses_.end()) pose = &found->second;
+ }
+ if (kept && pose) {
+ kept->pose = pose;
+ kept->poseKey = key;
+ kept->poseGeneration = pose_generation_;
+ }
+ return pose;
+}
+
+void MeshPlugin::trackPose(Pose& pose, const RenderFrameContext& ctx, const glm::mat4& model,
const std::vector<glm::mat4>* bones) {
- Pose& pose = poses_[poseKey(ctx, entity)];
// Last frame's, when the view drew this mesh last frame; otherwise it has no past worth following, and
// standing where it stands says "did not move".
const bool continued = pose.frame != 0 && pose.frame + 1 == ctx.motion_frame;
@@ -535,6 +592,109 @@ void MeshPlugin::trackPose(const RenderFrameContext& ctx, Entity entity, const g
pose.origin = renderOrigin();
}
+void MeshPlugin::deriveProxy(RenderCollectContext& collect, MeshProxy& proxy, Entity entity, const ecs::MeshRenderer& mesh,
+ ecs::Transform& transform, u8 present, u32 now) {
+ auto& ctx = collect.frame_context;
+ proxy = MeshProxy{};
+ proxy.entity = entity;
+ proxy.builtAt = now;
+ proxy.presentAt = now;
+ proxy.epoch = proxy_epoch_;
+ proxy.present = present;
+ proxy.resident = mesh.mesh.isValid() ? ctx.resources.getMesh(mesh.mesh) : nullptr;
+ proxy.meshRevision = proxy.resident ? proxy.resident->revision : 0;
+ proxy.materialStore = ctx.materials ? ctx.materials->revision() : 0;
+ proxy.material = mesh.material != 0 && ctx.materials ? ctx.materials->find(mesh.material) : nullptr;
+ proxy.materialRevision = proxy.material ? proxy.material->revision : 0;
+ // A parallax shifts the mesh toward the camera's centre, which is the view's to say.
+ proxy.kept = present == 0 && proxy.resident != nullptr && mesh.parallax == glm::vec2(1.0f);
+ if (!proxy.kept) return;
+ // What collect() derives for such a draw, the same way: anything else and a kept draw stands somewhere a fresh one would not.
+ transform.ensureDecomposed();
+ proxy.position = rebased(transform.worldPosition);
+ proxy.rotation = transform.worldRotation;
+ proxy.scale = transform.worldScale;
+ proxy.bend = proxy.material ? ctx.materials->vertexBounds(mesh.material) : 0.0f;
+ proxy.depthTested = mesh.opaque || (proxy.material && proxy.material->depthTest);
+ const glm::vec3 padding(proxy.bend);
+ orientedWorldAabb(proxy.position, proxy.rotation, proxy.scale, proxy.resident->localMin - padding,
+ proxy.resident->localMax + padding, proxy.boxCentre, proxy.boxHalf);
+ proxy.model = glm::translate(glm::mat4(1.0f), proxy.position) * glm::mat4_cast(proxy.rotation)
+ * glm::scale(glm::mat4(1.0f), proxy.scale);
+ proxy.normalRows = glm::transpose(glm::inverse(glm::mat3(proxy.model)));
+ proxy.facts = meshFactsFromEntity(ctx, collect.registry, entity, mesh, proxy.resident, warned_bones_);
+ proxy.facts.normalTextureId = 0;
+}
+
+MeshPlugin::MeshProxy* MeshPlugin::retain(RenderCollectContext& collect, Entity entity, const ecs::MeshRenderer& mesh,
+ ecs::Transform& transform, u32 now, u32 rulesChangedAt) {
+ auto& registry = collect.registry;
+ auto& ctx = collect.frame_context;
+ const u32 index = entity.index();
+ if (index >= proxy_slot_.size()) proxy_slot_.resize(static_cast<usize>(index) + 1, ~0u);
+ u32& slot = proxy_slot_[index];
+ if (slot == ~0u) {
+ slot = static_cast<u32>(proxies_.size());
+ proxies_.emplace_back();
+ }
+ MeshProxy& proxy = proxies_[slot];
+
+ const Mesh* resident = mesh.mesh.isValid() ? ctx.resources.getMesh(mesh.mesh) : nullptr;
+ bool stale = proxy.entity != entity || proxy.epoch != proxy_epoch_
+ || resident != proxy.resident || (resident && resident->revision != proxy.meshRevision)
+ || registry.changedSince<ecs::Transform>(entity, proxy.builtAt)
+ || registry.changedSince<ecs::MeshRenderer>(entity, proxy.builtAt);
+ // The store moved: whether THIS draw's material is the record it was, at the revision it was, is one lookup.
+ const u64 store = ctx.materials ? ctx.materials->revision() : 0;
+ if (!stale && store != proxy.materialStore) {
+ const MaterialRecord* material = mesh.material != 0 && ctx.materials ? ctx.materials->find(mesh.material) : nullptr;
+ stale = material != proxy.material || (material && material->revision != proxy.materialRevision);
+ if (!stale) proxy.materialStore = store;
+ }
+ // A component that rules keeping out was added or removed somewhere since this one looked: is it this entity's?
+ u8 present = proxy.present;
+ bool looked = false;
+ if (!stale && rulesChangedAt != 0 && ecs::tickAfter(rulesChangedAt, proxy.presentAt)) {
+ present = ruledOut(registry, entity);
+ looked = true;
+ stale = present != proxy.present;
+ proxy.presentAt = now;
+ }
+ if (stale) {
+ deriveProxy(collect, proxy, entity, mesh, transform, looked ? present : ruledOut(registry, entity), now);
+ ++proxies_built_;
+ } else if (proxy.kept) {
+ ++proxies_kept_;
+ // With the change audit on, what is kept is checked against what the components derive now: a difference is a write
+ // that nothing stamped, and a draw that stands where the entity no longer does.
+ if (registry.changeAuditEnabled()) {
+ MeshProxy fresh;
+ deriveProxy(collect, fresh, entity, mesh, transform, ruledOut(registry, entity), now);
+ // By bits: a mesh scaled to nothing has a normal matrix of NaN, which equals nothing, itself included, though
+ // the same inputs give the same bits.
+ const auto bits = [](const auto& a, const auto& b) { return std::memcmp(&a, &b, sizeof(a)) == 0; };
+ const bool same = fresh.kept && fresh.resident == proxy.resident && fresh.material == proxy.material
+ && bits(fresh.position, proxy.position) && bits(fresh.rotation, proxy.rotation) && bits(fresh.scale, proxy.scale)
+ && bits(fresh.boxCentre, proxy.boxCentre) && bits(fresh.boxHalf, proxy.boxHalf) && bits(fresh.model, proxy.model)
+ && bits(fresh.normalRows, proxy.normalRows) && bits(fresh.bend, proxy.bend) && fresh.depthTested == proxy.depthTested
+ && fresh.facts.hasNormals == proxy.facts.hasNormals && fresh.facts.lit == proxy.facts.lit
+ && fresh.facts.skinned == proxy.facts.skinned && fresh.facts.lightmapped == proxy.facts.lightmapped
+ && fresh.facts.tangents == proxy.facts.tangents && fresh.facts.opaque == proxy.facts.opaque
+ && fresh.facts.materialId == proxy.facts.materialId;
+ if (!same) {
+ ++proxies_stale_total_;
+ if (!warned_stale_) {
+ warned_stale_ = true;
+ ES_LOG_WARN("change audit: a kept mesh draw (entity {}) is not what its components derive now - something it"
+ " is derived from changed without a stamp or a revision", entity.index());
+ }
+ proxy = fresh;
+ }
+ }
+ }
+ return proxy.kept ? &proxy : nullptr;
+}
+
namespace {
/**
* @brief The card an impostor draws: the picture taken nearest this view's direction to the object, on a square facing that way.
@@ -678,7 +838,12 @@ void MeshPlugin::collect(RenderCollectContext& collect_ctx) {
const bool depthPass = shadowDepth || motion;
if (!motion && ctx.track_motion && ctx.motion_frame % 256 == 0) {
for (auto it = poses_.begin(); it != poses_.end();) {
- it = it->second.frame + 8 < ctx.motion_frame ? poses_.erase(it) : std::next(it);
+ if (it->second.frame + 8 < ctx.motion_frame) {
+ it = poses_.erase(it);
+ ++pose_generation_;
+ } else {
+ ++it;
+ }
}
}
@@ -687,6 +852,22 @@ void MeshPlugin::collect(RenderCollectContext& collect_ctx) {
const ShadingMode shading = depthPass ? ShadingMode::Shaded : collect_ctx.shading;
ecs::MeshRenderer shaded;
+ // What is kept between walks is the look as authored, which every view of it agrees on; a view that overrides it derives
+ // afresh. The tick is this walk's look at the change stamps: a proxy derived now has seen every write stamped up to it.
+ const bool keeping = shading == ShadingMode::Shaded;
+ u32 now = 0, rulesChangedAt = 0;
+ proxies_kept_ = proxies_built_ = 0;
+ if (keeping) {
+ if (renderOrigin() != proxy_origin_ || ctx.materials != proxy_materials_ || registry.identity() != proxy_registry_) {
+ ++proxy_epoch_;
+ proxy_origin_ = renderOrigin();
+ proxy_materials_ = ctx.materials;
+ proxy_registry_ = registry.identity();
+ }
+ now = registry.beginChangeRead();
+ rulesChangedAt = ruledOutChangedAt(registry);
+ }
+
// The walk is timed whole: two clock reads a walk. A clock read is a call out to the page (performance.now), and read at
// every phase of every mesh it was four of every ten samples of a profiled frame and most of the time it reported - the
// collect looked several times dearer than it was. The split by phase is a build option (ES_PROFILE_COLLECT_PHASES) for
@@ -732,33 +913,49 @@ void MeshPlugin::collect(RenderCollectContext& collect_ctx) {
if (!mesh.enabled || (mesh.indices.empty() && !mesh.mesh.isValid())) continue;
auto& transform = meshView.get<ecs::Transform>(entity);
- if (!transform.decomposed_) ++coldDecompose;
- // Parallax is a CAMERA trick — a renderable shifted toward the view centre to
- // scroll slower. A shadow pass looks from a light, whose centre means nothing to
- // it, and its map is fitted to unshifted bounds: a caster stands where it is.
- transform.ensureDecomposed();
- const glm::vec3 position = shadowDepth
- ? rebased(transform.worldPosition)
- : parallaxedWorldPosition(transform, mesh.parallax, collect_ctx.camera);
- // A card turns to THIS view, whichever it is: the Transform keeps what was authored. Placements are each turned by their own
- // record, so a set of them is left as it stands.
- const ecs::Billboard* card = registry.tryGet<ecs::Billboard>(entity);
- const glm::quat rotation = card && card->enabled && !registry.tryGet<ecs::MeshInstances>(entity)
- ? billboard::facing(card->mode, ctx.view_projection, collect_ctx.camera.eye, position)
- : transform.worldRotation;
- const auto& scale = transform.worldScale;
+ // Kept from an earlier walk when nothing it was derived from has changed (MeshProxy); null derives it here.
+ MeshProxy* kept = keeping && !heated ? retain(collect_ctx, entity, mesh, transform, now, rulesChangedAt) : nullptr;
+ glm::vec3 position, scale;
+ glm::quat rotation;
+ if (kept) {
+ // The motion pass draws what moved since the view's last frame, and only what is opaque. A kept draw that did not
+ // is passed over here rather than after its cull, which is all the work the walk would have done to reach the same answer.
+ if (motion) {
+ const Pose* pose = poseOf(ctx, entity, kept, false);
+ if (!mesh.opaque || !pose || pose->frame != ctx.motion_frame || pose->previous == pose->current) continue;
+ }
+ position = kept->position;
+ rotation = kept->rotation;
+ scale = kept->scale;
+ } else {
+ if (!transform.decomposed_) ++coldDecompose;
+ // Parallax is a CAMERA trick — a renderable shifted toward the view centre to
+ // scroll slower. A shadow pass looks from a light, whose centre means nothing to
+ // it, and its map is fitted to unshifted bounds: a caster stands where it is.
+ transform.ensureDecomposed();
+ position = shadowDepth
+ ? rebased(transform.worldPosition)
+ : parallaxedWorldPosition(transform, mesh.parallax, collect_ctx.camera);
+ // A card turns to THIS view, whichever it is: the Transform keeps what was authored. Placements are each turned by their own
+ // record, so a set of them is left as it stands.
+ const ecs::Billboard* card = registry.tryGet<ecs::Billboard>(entity);
+ rotation = card && card->enabled && !registry.tryGet<ecs::MeshInstances>(entity)
+ ? billboard::facing(card->mode, ctx.view_projection, collect_ctx.camera.eye, position)
+ : transform.worldRotation;
+ scale = transform.worldScale;
+ }
tick(0);
// Bounds come from whichever geometry this is: a resident mesh keeps its
// own, and the inline payload's are recomputed on upload. Reading the
// component's for a resident mesh would cull it against an empty box.
- const Mesh* resident = mesh.mesh.isValid() ? ctx.resources.getMesh(mesh.mesh) : nullptr;
+ const Mesh* resident = kept ? kept->resident : mesh.mesh.isValid() ? ctx.resources.getMesh(mesh.mesh) : nullptr;
// Only GPU-resident geometry casts: the inline payload takes the CPU path
// below, which bakes world space into vertices for the BATCH shader — a
// shader that writes colour, not the depth this pass is here to collect.
if (depthPass && !resident) continue;
// Many placements of one mesh, drawn as instances of it. What is placed does not move on its own, so the motion pass has nothing to say.
- const ecs::MeshInstances* placements = resident ? registry.tryGet<ecs::MeshInstances>(entity) : nullptr;
+ const ecs::MeshInstances* placements = resident && !kept ? registry.tryGet<ecs::MeshInstances>(entity) : nullptr;
if (placements && (placements->transforms.empty() || motion)) continue;
// What light passes through (water) stands in no light's way. Rain's way is another matter: a see-through surface
// set to cast no shadow (glass) still keeps rain off what is under it, so the rain's cover map takes it. What is
@@ -773,7 +970,7 @@ void MeshPlugin::collect(RenderCollectContext& collect_ctx) {
// make the answer the entity's rather than each view's.
LodLevels levels;
const ecs::LODGroup* group = nullptr;
- if (collect_ctx.lod.state && resident && !placements) {
+ if (collect_ctx.lod.state && resident && !placements && !kept) {
const auto* declared = registry.tryGet<ecs::LODGroup>(entity);
if (declared && declared->enabled) {
++lodGathers;
@@ -786,7 +983,7 @@ void MeshPlugin::collect(RenderCollectContext& collect_ctx) {
tick(1);
// Small enough, and an impostor with its pictures taken: a card facing this view stands in for the mesh. Only the scene's own
// views draw one: a card casts no shadow, and a picture has no motion to write.
- if (ctx.purpose == RenderPurpose::Scene && resident && !placements && ctx.frame) {
+ if (ctx.purpose == RenderPurpose::Scene && resident && !placements && !kept && ctx.frame) {
const auto* stand = registry.tryGet<ecs::Impostor>(entity);
const RenderFrame::ImpostorAtlas* atlas = stand && stand->enabled ? ctx.frame->impostorAtlas(entity) : nullptr;
if (atlas) {
@@ -804,12 +1001,12 @@ void MeshPlugin::collect(RenderCollectContext& collect_ctx) {
// BEFORE the cull, because whether this draw depth-tests decides if an
// occluder may refuse it, and the material is where that is finally said.
// Reused by the draw key below: two lookups is how the two come to differ.
- const MaterialRecord* material = (mesh.material != 0 && ctx.materials)
- ? ctx.materials->find(mesh.material) : nullptr;
+ const MaterialRecord* material = kept ? kept->material
+ : (mesh.material != 0 && ctx.materials) ? ctx.materials->find(mesh.material) : nullptr;
// A material that bends its vertices can put them outside the mesh's own box; culling the
// box would drop a bent mesh that is still on screen, so it grows by what the material says.
- const f32 bend = material ? ctx.materials->vertexBounds(mesh.material) : 0.0f;
- const bool depthTested = mesh.opaque || (material && material->depthTest);
+ const f32 bend = kept ? kept->bend : material ? ctx.materials->vertexBounds(mesh.material) : 0.0f;
+ const bool depthTested = kept ? kept->depthTested : mesh.opaque || (material && material->depthTest);
const Mesh* placementCoarse = placements ? placementCoarseMesh(ctx.resources, *resident, *placements) : nullptr;
f32 placementRadius = 0.0f;
@@ -832,9 +1029,14 @@ void MeshPlugin::collect(RenderCollectContext& collect_ctx) {
const glm::vec3 padding(placements ? 0.0f : bend);
glm::vec3 aabbCenter(0.0f), halfExtents(0.0f);
- orientedWorldAabb(position, rotation, scale, localMin - padding, localMax + padding,
- aabbCenter, halfExtents);
- if (resident && !placements && !group) {
+ if (kept) {
+ aabbCenter = kept->boxCentre;
+ halfExtents = kept->boxHalf;
+ } else {
+ orientedWorldAabb(position, rotation, scale, localMin - padding, localMax + padding,
+ aabbCenter, halfExtents);
+ }
+ if (resident && !placements && !group && !kept) {
skinnedWorldBounds(registry, entity, *resident, warned_bones_, aabbCenter, halfExtents);
}
// The collect's own predicate, not a frustum test of this plugin's: it
@@ -915,17 +1117,20 @@ void MeshPlugin::collect(RenderCollectContext& collect_ctx) {
if (motion) {
// What was drawn last frame and what is drawn now, for THIS view. Nothing known, or nothing changed,
// means the camera's own motion says it all and the temporal pass already has that.
- const auto pose = poses_.find(poseKey(ctx, entity));
- if (pose == poses_.end() || pose->second.frame != ctx.motion_frame
- || (pose->second.previous == pose->second.current
- && pose->second.bonesPrevious == pose->second.bonesCurrent)) {
+ const Pose* pose = poseOf(ctx, entity, kept, false);
+ if (!pose || pose->frame != ctx.motion_frame
+ || (pose->previous == pose->current && pose->bonesPrevious == pose->bonesCurrent)) {
continue;
}
const MeshProgramFacts facts =
- meshFactsFromEntity(ctx, registry, entity, mesh, resident, warned_bones_);
+ kept ? kept->facts : meshFactsFromEntity(ctx, registry, entity, mesh, resident, warned_bones_);
// A palette of a different size than last frame's has no joint-for-joint past to follow.
- if (facts.skinned && pose->second.bonesPrevious.size() != pose->second.bonesCurrent.size()) continue;
+ if (facts.skinned && pose->bonesPrevious.size() != pose->bonesCurrent.size()) continue;
keys = { meshVariant(facts.hasNormals, false, false, facts.skinned, false, false, false, false, false, true, facts.tangents), 0 };
+ } else if (kept) {
+ MeshProgramFacts facts = kept->facts;
+ facts.normalTextureId = normalTextureOf(ctx, mesh, facts);
+ keys = meshDrawFrom(ctx, entity, facts, shadowDepth);
} else {
keys = meshDrawFor(ctx, registry, entity, mesh, resident, shadowDepth, warned_bones_, heated);
}
@@ -1008,7 +1213,7 @@ void MeshPlugin::collect(RenderCollectContext& collect_ctx) {
// The pose, when this entity says what moves it. The bones are
// world-space, so the entity's own transform is not read — which is
// what glTF requires of a skinned mesh.
- const u32 poseSize = skinPose(registry, entity, *resident, pose_scratch_, warned_bones_);
+ const u32 poseSize = kept ? 0u : skinPose(registry, entity, *resident, pose_scratch_, warned_bones_);
const bool skinned = poseSize > 0;
// `lit` is the draw's own word and is honoured either way: geometry
@@ -1037,7 +1242,7 @@ void MeshPlugin::collect(RenderCollectContext& collect_ctx) {
// same vertices, and a morph target is what the mesh is BEFORE a
// pose is applied to it.
MorphConstants shapes{};
- if (morphShapes(registry, entity, *resident, shapes)) {
+ if (!kept && morphShapes(registry, entity, *resident, shapes)) {
key.morphIndex = draw_list.addMorphShapes(shapes);
key.morphTextureId = static_cast<u32>(resident->morphTexture);
}
@@ -1046,9 +1251,10 @@ void MeshPlugin::collect(RenderCollectContext& collect_ctx) {
key.skinOffset = draw_list.addSkinMatrices(pose_scratch_.data(), poseSize);
key.skinCount = poseSize;
} else {
- glm::mat4 model = glm::translate(glm::mat4(1.0f), position)
- * glm::mat4_cast(rotation)
- * glm::scale(glm::mat4(1.0f), scale);
+ const glm::mat4 model = kept ? kept->model
+ : glm::translate(glm::mat4(1.0f), position)
+ * glm::mat4_cast(rotation)
+ * glm::scale(glm::mat4(1.0f), scale);
// Three rows, not four columns: the row the shader rebuilds is
// the one an affine transform never varies.
for (u32 row = 0; row < 3; ++row) {
@@ -1058,7 +1264,7 @@ void MeshPlugin::collect(RenderCollectContext& collect_ctx) {
if (resident->hasNormals) {
// Written per object rather than derived per vertex: this is
// the transform a normal takes under a non-uniform scale.
- const glm::mat3 nrm = glm::transpose(glm::inverse(glm::mat3(model)));
+ const glm::mat3 nrm = kept ? kept->normalRows : glm::transpose(glm::inverse(glm::mat3(model)));
for (u32 row = 0; row < 3; ++row) {
std::memcpy(dst + (MESH_INSTANCE_TEXEL_NORMAL + row) * 16, &nrm[row][0], 12);
}
@@ -1081,7 +1287,7 @@ void MeshPlugin::collect(RenderCollectContext& collect_ctx) {
// This record is the motion pass's own, so the texels a lit draw keeps its tint, normal
// matrix and bake in are free: last frame's shift in the tint's, and last frame's
// view-projection times last frame's model — as rows, so the shader dots them with the local position.
- const Pose& pose = poses_.find(poseKey(ctx, entity))->second;
+ const Pose& pose = *poseOf(ctx, entity, kept, false);
const glm::vec4 jitter{ctx.motion_prev_jitter, 0.0f, 0.0f};
std::memcpy(dst + MESH_INSTANCE_TEXEL_TINT * 16, &jitter, 16);
// A rigid mesh's last place is its last model, so the matrix is the two together; a skinned
@@ -1102,8 +1308,9 @@ void MeshPlugin::collect(RenderCollectContext& collect_ctx) {
}
}
} else if (ctx.track_motion && !shadowDepth) {
- trackPose(ctx, entity,
+ trackPose(*poseOf(ctx, entity, kept, true), ctx,
skinned ? glm::mat4(1.0f)
+ : kept ? kept->model
: glm::translate(glm::mat4(1.0f), position)
* glm::mat4_cast(rotation)
* glm::scale(glm::mat4(1.0f), scale),
@@ -1181,7 +1388,8 @@ void MeshPlugin::collect(RenderCollectContext& collect_ctx) {
resident->hasTangents);
if (key.depthShaderId != 0) ++prepassTaken; else ++prepassBent;
} else {
- const u32 depthVariant = meshDrawFor(ctx, registry, entity, mesh, resident, true, warned_bones_).variant;
+ const u32 depthVariant = kept ? meshDrawFrom(ctx, entity, kept->facts, true).variant
+ : meshDrawFor(ctx, registry, entity, mesh, resident, true, warned_bones_).variant;
key.depthShaderId = meshProgram(ctx, depthVariant);
if (key.depthShaderId != 0) ++prepassTaken; else ++prepassNoProgram;
}
@@ -1326,6 +1534,11 @@ void MeshPlugin::collect(RenderCollectContext& collect_ctx) {
ES_PROFILE_COUNTER("render.mesh.programAsks", programAsks);
// The scene's own collect says it; a shadow map's, which comes after, would overwrite it with zeroes.
if (ctx.purpose == RenderPurpose::Scene) {
+ // Draws whose view-independent half was kept from an earlier walk, and those derived again because something changed.
+ ES_PROFILE_COUNTER("render.mesh.proxy.kept", proxies_kept_);
+ ES_PROFILE_COUNTER("render.mesh.proxy.built", proxies_built_);
+ // Kept draws the change audit found differing from a fresh derivation, since the plugin started: anything but 0 is a bug.
+ ES_PROFILE_COUNTER("render.mesh.proxy.stale", proxies_stale_total_);
ES_PROFILE_COUNTER("render.prepass.taken", prepassTaken);
ES_PROFILE_COUNTER("render.prepass.cutout", prepassCutout);
ES_PROFILE_COUNTER("render.prepass.bent", prepassBent);
diff --git a/src/esengine/renderer/plugins/MeshPlugin.hpp b/src/esengine/renderer/plugins/MeshPlugin.hpp
index 7b3e39497..8d43ead8b 100644
--- a/src/esengine/renderer/plugins/MeshPlugin.hpp
+++ b/src/esengine/renderer/plugins/MeshPlugin.hpp
@@ -7,11 +7,22 @@
#include "../draw/BatchVertex.hpp"
#include "../../resource/Handle.hpp"
+#include <glm/glm.hpp>
+#include <glm/gtc/quaternion.hpp>
+
#include <unordered_map>
#include <vector>
namespace esengine {
+class Mesh;
+struct MaterialRecord;
+class MaterialStore;
+namespace ecs {
+struct MeshRenderer;
+struct Transform;
+} // namespace ecs
+
/**
* @brief Scene-level 2D mesh renderer (MeshRenderer component).
* @details Streams component-local geometry through the unified Batch face each
@@ -70,11 +81,82 @@ private:
glm::dvec3 origin{0.0};
};
std::unordered_map<u64, Pose> poses_;
- void trackPose(const RenderFrameContext& ctx, Entity entity, const glm::mat4& model,
+ void trackPose(Pose& pose, const RenderFrameContext& ctx, const glm::mat4& model,
const std::vector<glm::mat4>* bones = nullptr);
static u64 poseKey(const RenderFrameContext& ctx, Entity entity) {
return ctx.motion_key * 0x9E3779B97F4A7C15ull ^ static_cast<u64>(entity.id());
}
+ /**
+ * @brief What one entity's draw is derived from that no view changes: where it stands, its world box and matrices, its
+ * material's say over culling, its program facts. Kept between walks, and rebuilt when what it was derived from
+ * changes: the entity's Transform or MeshRenderer written (their change stamps), its mesh realized again or its
+ * material store changed (their revisions), a component that rules keeping out added or removed, or the render
+ * origin moved.
+ *
+ * @details Only what draws through resident geometry with nothing that turns, picks or poses it per view or per frame
+ * (a billboard, placements, a level-of-detail group, an impostor, bones, shapes, parallax) is kept; the rest is
+ * derived afresh every walk, as before. What the frame decides (textures by id, the probe a box reads, the
+ * reflection, the program asked for) is still asked every walk.
+ */
+ struct MeshProxy {
+ Entity entity = INVALID_ENTITY;
+ /// The registry tick the proxy was derived at (Registry::beginChangeRead): a stamp after it is a write it has not seen.
+ u32 builtAt = 0;
+ /// When `present` was last confirmed against the components that rule keeping out.
+ u32 presentAt = 0;
+ u32 epoch = 0;
+ /// Which of those components the entity has, one bit each (see ruledOut in MeshPlugin.cpp).
+ u8 present = 0;
+ /// Whether the draw is derived from what is kept; false, it is derived every walk.
+ bool kept = false;
+ bool depthTested = false;
+ f32 bend = 0.0f;
+ u64 meshRevision = 0;
+ u64 materialStore = 0;
+ u64 materialRevision = 0;
+ const Mesh* resident = nullptr;
+ const MaterialRecord* material = nullptr;
+ glm::vec3 position{0.0f};
+ glm::quat rotation{1.0f, 0.0f, 0.0f, 0.0f};
+ glm::vec3 scale{1.0f};
+ glm::vec3 boxCentre{0.0f};
+ glm::vec3 boxHalf{0.0f};
+ glm::mat4 model{1.0f};
+ /// The transform a normal takes, as the three rows the instance record holds.
+ glm::mat3 normalRows{1.0f};
+ /// normalTextureId is left 0: a texture's id is asked every walk.
+ MeshProgramFacts facts;
+ /// The entity's pose for the view that last asked (poseKey), while poses_ has erased nothing since (pose_generation_):
+ /// a pointer into an unordered_map stays good until its node is erased.
+ Pose* pose = nullptr;
+ u64 poseKey = 0;
+ u32 poseGeneration = 0;
+ };
+ /// Moves whenever poses_ erases a node, so a pose a proxy points at is known to be still there.
+ u32 pose_generation_ = 0;
+ /** @brief The pose @p entity has in this view, made if @p make; through @p kept's pointer when that is still good. */
+ Pose* poseOf(const RenderFrameContext& ctx, Entity entity, MeshProxy* kept, bool make);
+ /// Indexed by entity index: where in proxies_ that entity's proxy is, or ~0u.
+ std::vector<u32> proxy_slot_;
+ std::vector<MeshProxy> proxies_;
+ /// Moves when something every proxy was derived from changes: the render origin, the material store object, or the registry
+ /// walked (Registry::identity: its ticks and entities are its own, and a scene loaded again is another registry).
+ u32 proxy_epoch_ = 1;
+ u64 proxy_registry_ = 0;
+ glm::dvec3 proxy_origin_{0.0};
+ const MaterialStore* proxy_materials_ = nullptr;
+ /// Proxies kept and rebuilt this walk, and (with the change audit on) kept ones found to differ from a fresh derivation.
+ u32 proxies_kept_ = 0, proxies_built_ = 0;
+ u32 proxies_stale_total_ = 0;
+ bool warned_stale_ = false;
+ /**
+ * @brief The proxy for @p entity, rebuilt if anything it was derived from changed since.
+ * @return Null when the entity's draw is not one that is kept; it is then derived as it always was.
+ */
+ MeshProxy* retain(RenderCollectContext& collect, Entity entity, const ecs::MeshRenderer& mesh,
+ ecs::Transform& transform, u32 now, u32 rulesChangedAt);
+ void deriveProxy(RenderCollectContext& collect, MeshProxy& proxy, Entity entity, const ecs::MeshRenderer& mesh,
+ ecs::Transform& transform, u8 present, u32 now);
bool warned_material_ = false; ///< A material on resident geometry is said once.
bool warned_bones_ = false; ///< A skin over the pose budget is said once.
bool warned_lod_ = false; ///< A group that cannot be honoured is said once.
diff --git a/src/esengine/renderer/store/MaterialStore.cpp b/src/esengine/renderer/store/MaterialStore.cpp
index efc4cb486..d9b2f30f3 100644
--- a/src/esengine/renderer/store/MaterialStore.cpp
+++ b/src/esengine/renderer/store/MaterialStore.cpp
@@ -431,7 +431,7 @@ void MaterialStore::setWorldUnitsPerMeter(f32 unitsPerMeter) {
defaultInUnits(p, scale, now);
std::memcpy(rec.uboBytes.data() + p.offset, now, p.arity * sizeof(f32));
rec.uboDirty = true;
- rec.revision = nextRevision();
+ rec.revision = revise();
}
}
}
diff --git a/src/esengine/renderer/store/MaterialStore.hpp b/src/esengine/renderer/store/MaterialStore.hpp
index 7f2937954..f45a52b05 100644
--- a/src/esengine/renderer/store/MaterialStore.hpp
+++ b/src/esengine/renderer/store/MaterialStore.hpp
@@ -360,6 +360,13 @@ public:
return m ? m->revision : 0;
}
+ /**
+ * @brief Changes whenever any material does (revisionOf), or one is added or removed.
+ * @details A reader that kept what it derived from many materials, or a pointer to a record, asks this once rather than
+ * revisionOf each: while it holds, every record kept is the one it was.
+ */
+ u64 revision() const { return store_revision_; }
+
/// Pushes a material's resolved render state. Preserves any already-packed constants
/// (an edit such as setBlendMode re-pushes state without clobbering uniforms); a shader
/// change invalidates the old constants since the layout differs.
@@ -369,7 +376,7 @@ public:
if (it == materials_.end()) {
MaterialRecord& added = materials_[materialId];
added = record;
- added.revision = nextRevision();
+ added.revision = revise();
return;
}
MaterialRecord& rec = it->second;
@@ -387,7 +394,7 @@ public:
rec.textures = std::move(texs);
}
rec.uboDirty = true; // re-upload on next bind
- rec.revision = nextRevision();
+ rec.revision = revise();
}
/// Writes a named param's float components into the material's std140 buffer at the
@@ -404,7 +411,7 @@ public:
const u32 n = std::min(arity, slot->arity);
std::memcpy(rec.uboBytes.data() + slot->offset, values, n * sizeof(f32));
rec.uboDirty = true;
- rec.revision = nextRevision();
+ rec.revision = revise();
}
/// Binds a texture to a named texture param's sampler unit. No-op if the material,
@@ -416,7 +423,7 @@ public:
if (lit == layouts_.end()) return;
const MaterialTextureSlot* slot = lit->second.findTexture(name);
if (!slot) return;
- it->second.revision = nextRevision();
+ it->second.revision = revise();
for (auto& b : it->second.textures) {
if (b.unit == slot->unit) { b.texture = texture; return; }
}
@@ -576,10 +583,17 @@ private:
mutable const MaterialRecord* last_material_ = nullptr;
mutable u32 last_layout_id_ = 0;
mutable const MaterialUniformLayout* last_layout_ = nullptr;
- void forgetLookups() { last_material_ = nullptr; last_material_id_ = 0; last_layout_ = nullptr; last_layout_id_ = 0; }
+ void forgetLookups() {
+ last_material_ = nullptr; last_material_id_ = 0; last_layout_ = nullptr; last_layout_id_ = 0;
+ // A record went: a pointer anyone kept to one may be to it.
+ store_revision_ = nextRevision();
+ }
+ /// The revision a changed material takes, which the store as a whole takes too.
+ u64 revise() { return store_revision_ = nextRevision(); }
+ u64 store_revision_ = nextRevision();
/** Every material on @p shader takes a new revision: its layout changed or went. */
void reviseMaterialsOf(resource::ShaderHandle shader) {
- for (auto& [id, rec] : materials_) if (rec.shaderRef == shader) rec.revision = nextRevision();
+ for (auto& [id, rec] : materials_) if (rec.shaderRef == shader) rec.revision = revise();
}
/// A material shader's authored source + features, filed under its handle for
diff --git a/tests/ecs/test_change_stamps.cpp b/tests/ecs/test_change_stamps.cpp
index 5dfaf471b..abc489ea6 100644
--- a/tests/ecs/test_change_stamps.cpp
+++ b/tests/ecs/test_change_stamps.cpp
@@ -63,6 +63,14 @@ int main() {
r.remove<Pos>(a);
CHECK(r.anyChangedSince<Pos>(before), "a remove reads as a change of the pool");
CHECK(!r.changedSince<Pos>(a, before), "while the entity has no stamp left to ask");
+ CHECK(tickAfter(r.lastChangedTick<Pos>(), before), "the pool's last change is the remove's, after the look before it");
+ CHECK(r.lastChangedTick<Rank>() == 0, "a type never stored has none");
+
+ Registry other;
+ CHECK(other.identity() != 0 && other.identity() != r.identity(), "each registry is its own");
+ const auto kept = other.identity();
+ other = Registry{};
+ CHECK(other.identity() != kept && other.identity() != r.identity(), "and one assigned over another brings its own");
const u32 later = r.beginChangeRead();
r.emplaceOrReplace<Pos>(a);
diff --git a/tests/resource/test_material_shader_lifetime.cpp b/tests/resource/test_material_shader_lifetime.cpp
index ab3fe5012..f7f5a3743 100644
--- a/tests/resource/test_material_shader_lifetime.cpp
+++ b/tests/resource/test_material_shader_lifetime.cpp
@@ -59,7 +59,28 @@ int main() {
store.registerLayout(shader, layout);
CHECK(store.revisionOf(1) != redefined, "its shader's layout replaced moves it");
CHECK(store.revisionOf(2) == otherBefore, "and leaves a material on another shader alone");
- store.clear(); rm.releaseShader(shader); rm.releaseShader(other); rm.shutdown();
+
+ // The store's own revision: whatever any material does, or a material added or removed, and not a read.
+ const u64 whole = store.revision();
+ CHECK(whole != 0, "the store has a revision");
+ store.find(2); store.revisionOf(1);
+ CHECK(store.revision() == whole, "reading it changes nothing");
+ store.setUniform(1, "u_tint", tint, 4);
+ CHECK(store.revision() != whole && store.revision() == store.revisionOf(1), "a material's change moves it, to that revision");
+ const u64 changed = store.revision();
+ store.setUniform(1, "u_missing", tint, 4);
+ CHECK(store.revision() == changed, "a write that changes nothing does not");
+ MaterialRecord third; third.shaderRef = other; store.define(3, third);
+ CHECK(store.revision() != changed, "a material added moves it");
+ const u64 added = store.revision();
+ store.undefine(3);
+ CHECK(store.revision() != added, "and one removed");
+ const u64 removed = store.revision();
+ store.undefine(3);
+ CHECK(store.revision() == removed, "removing one it does not have does not");
+ store.clear();
+ CHECK(store.revision() != removed, "and so does clearing it");
+ rm.releaseShader(shader); rm.releaseShader(other); rm.shutdown();
}
{
MockGfxDevice device; resource::ResourceManager rm; rm.init(device);
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index 93269c839..312b21513 100644
--- a/tools/releaseNotes.mjs
+++ b/tools/releaseNotes.mjs
@@ -4854,4 +4854,6 @@ export const NOTED = {
{"note": "A post-process shader can read more than seven textures."},
"feat(terrain): a terrain's surface can be baked into a runtime virtual texture - with virtualTexture on, each node of a layered ground from virtualTextureFrom (level 1) out is drawn from a page of what Terrain Layers makes of it before lighting (colour and roughness, normal and occlusion), baked once on the GPU through the ground's own material and read in two texture fetches; steep ground and a node whose page is not ready are drawn live. The engine draws a material through another (PostProcess) shader into a rect of a render target before the scene (Renderer.bakeMaterial, MaterialStore::bindThrough, matched by name), and a render target can keep its texture as drawn. On terrain-water (RTX 3060, 1080p, WebGL2) the ground's shading 2.1 -> 0.8 ms and the frame 15.1 -> 13.5 ms at the default camera; 2.6 -> 1.8 ms at the lake (1.2 from level 0). Off by default. Also: the planet-layers scene's sky at half strength, where its layered box read a saturated 255 that the earlier tolerance let pass":
{"note": "A terrain's surface can be baked into a runtime virtual texture."},
+ "perf(render): a mesh's draw keeps what no view changes between frames - where it stands, its world box, its model and normal matrices, its material's say over culling and its program facts are derived once and kept until its Transform or MeshRenderer is written, its mesh realized again, its material changed or the render origin moves; a static mesh that did not move is passed over by the motion pass before its cull, and the change audit checks every kept draw against a fresh one":
+ {"note": "A mesh's draw keeps what no view changes."},
};