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.

feat(render): a mesh, a material, a texture and the frame's probes each carry a revision that changes whenever their content does - one counter for all of them (core/Revision.hpp), so what the renderer derives from a resource can be kept until the resource itself changes

yhh4a45001e5766
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 9ef88d8b5..365c570a9 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -17,6 +17,7 @@ published separately; it ships inside the editor.
### Added
- **Terrain layers cost less, and a Planet draws its layers as a Terrain does.** A point of ground is drawn from the four layers with the most weight there, and a layer not painted there costs nothing: on the Terrain & Water example's lakeside view the ground's shading went from 3.0 to 2.6 ms at 1080p with the picture unchanged. The Planet shader now uses the same layer code as Terrain Layers, so a layer's mask (and its `occlusion`) darkens the sky's light on a planet too, and the Planet material gains `u_heightBlend`, `u_blendNoise` and `u_antiTile`, 0 by default, which draws a planet as before.
+- **Resources carry a revision that changes with their content.** A mesh takes a new revision each time it is realized (`Mesh::revision`), a material whenever its state, a param, a texture or its shader's layout changes (`MaterialStore::revisionOf`), a texture whenever the device is given new pixels or sampling for it (`GfxDevice::textureRevision`), and the frame's probe and reflection volumes whenever the set of them, or a probe grid's coefficients, differ from the last frame's (`ProbeStore::revision`, `ReflectionStore::revision`). Every kind draws from one counter (`core/Revision.hpp`, which a probe grid's revision already used), so a revision is never shared, even by a resource that reuses a released one's slot. A texture the GPU itself writes (a render target) does not change revision with what is drawn into it. Nothing reads these yet: with the components' change stamps they are what a renderer that keeps its per-entity work between frames checks before using it.
- **An engine component is stamped whoever writes it.** Every write to an engine component now leaves the change stamp the engine's own readers ask for (`Registry::changedSince`), wherever it is made. A script's write (`set`, `insert`, `update`, a `Mut` query, `markChanged`, a compiled system's rows) is stamped in the engine, batched in the World and handed over in one call before the frame's transforms are composed (`registry_markComponentsChanged`); the physics sync stamps the bodies that moved and no longer rewrites a body that did not; the transform system stamps a transform whose world matrix changed, so a child moved by its parent reads as changed; tweens and the editor's field setters stamp what they set. `Changed()` queries in scripts mean what they meant: physics motion still stays out of them, as it always has, while the engine's stamps include it. With the change audit on, 28 examples (physics, rigs, tweens, UI, particles, tilemaps, streaming worlds) ran 120 frames each with no write left unstamped; with the physics stamps taken out, the physics examples reported every moving body, 5,000 a frame in one of them.
- **Components carry the tick of their last write.** Each component pool keeps, beside every component, the tick at which it was last written, added or replaced, and the latest such tick of the pool. `Registry::patch<T>()` is mutable access that stamps the write, `markChanged<T>()` stamps a write made through a reference held from before, and adding or replacing a component stamps it; `get<T>()` stamps nothing. A reader asks `beginChangeRead()` for the tick it looks at, and next time `changedSince<T>(entity, tick)` and `anyChangedSince<T>(tick)` say what was written after that look, including what was written later in the same frame. Stamps travel with their components through a removal and a re-sort, and are kept within half the counter's range so none reads as new after the tick wraps. `Registry::setChangeAudit` (`World.setChangeAudit` from script) keeps a copy of every component each frame and reports each type written without a stamp, which is what a consumer of changes would never see; `render-submit-cost.mjs --audit` runs it on an example. Nothing reads the stamps yet: this is the ground for a renderer that walks only what changed, and the audit lists what still writes without saying so (an animated rig's bones, the platformer's and the 3D physics example's moving bodies).
- **A playable opens on a start screen.** A single-file playable showed an empty page until its first frame: the engine inflated and compiled, the scene loaded, and with `quality.prewarmShaders` its shaders warmed, twenty-five seconds for one lake scene in a headless browser. It now carries the web export's start screen (the game's title, a bar, what the boot is doing), in markup that is on screen before any script runs, and announces the same `esengine:bootprogress` and `esengine:firstframe` events. `initPlayableRuntime` takes `onBeforeRun`, called once everything is loaded and warmed, and `prewarmShaders.onProgress`. A stretch of a boot nothing measures (shaders compiling, then a ground or a field still being made) runs a light along the bar and says which it is, where the bar used to sit still on "Ready"; a web export's start screen does the same.
diff --git a/src/esengine/core/Revision.hpp b/src/esengine/core/Revision.hpp
new file mode 100644
index 000000000..42e0db8dd
--- /dev/null
+++ b/src/esengine/core/Revision.hpp
@@ -0,0 +1,29 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/**
+ * @file Revision.hpp
+ * @brief What a resource's content is, as a number that changes whenever it does.
+ *
+ * @details A mesh, a material, a texture, a probe grid each carry a revision: taken from here when their content is made and
+ * again whenever it changes. A reader that keeps what it derived from one (the renderer's per-entity record of a draw)
+ * keeps the revision beside it and asks again only when the revision moved. One counter for every kind, so a revision
+ * is unique across them all: a released resource whose memory or slot a new one reuses is never taken for it.
+ *
+ * The engine's components say the same of themselves with change stamps (Registry::changedSince); resources are not in
+ * the registry, so they say it here.
+ */
+#pragma once
+
+#include "Types.hpp"
+
+#include <atomic>
+
+namespace esengine {
+
+/** @brief A revision no content has had before. Never 0, which reads as "none". */
+inline u64 nextRevision() {
+ static std::atomic<u64> next{0};
+ return ++next;
+}
+
+} // namespace esengine
diff --git a/src/esengine/renderer/rhi/GfxDevice.cpp b/src/esengine/renderer/rhi/GfxDevice.cpp
index a81fceeb2..f1e93c3cd 100644
--- a/src/esengine/renderer/rhi/GfxDevice.cpp
+++ b/src/esengine/renderer/rhi/GfxDevice.cpp
@@ -193,6 +193,7 @@ void GfxDevice::settleRecovery() {
void GfxDevice::payTexture(u32 id, GfxTextureRecord& record) {
if (!record.owed) return;
record.owed = false;
+ record.revision = nextRevision();
--owed_count_;
if (record.desc.mipmaps && record.realized) backendGenerateMipmaps(id);
settleRecovery();
@@ -460,6 +461,7 @@ bool GfxDevice::restoreCompressedTexture(TextureHandle texture, GfxCompressedFor
keepBytes(record->bytes, std::vector<u8>(first, first + byteLength));
}
record->realized = backendCreateCompressedTexture(id, record->desc, format, data, byteLength, mipLevels);
+ record->revision = nextRevision();
if (record->realized) payTexture(id, *record);
return record->realized;
}
@@ -486,6 +488,7 @@ bool GfxDevice::restoreTextureFromNative(TextureHandle texture, u32 nativeId) {
if (!record || !isDeviceUsable()) return false;
if (record->realized) backendDeleteTexture(id);
record->realized = backendAdoptTexture(id, nativeId, record->desc);
+ record->revision = nextRevision();
if (record->realized) payTexture(id, *record);
return record->realized;
}
@@ -509,6 +512,7 @@ bool GfxDevice::adoptTextureContent(TextureHandle into, TextureHandle from) {
target->compressedFormat = source->compressedFormat;
target->mipLevels = source->mipLevels;
target->realized = true;
+ target->revision = nextRevision();
std::vector<u8> sourceBytes = takeBytes(source->bytes);
if (target->content.kind() == GfxContentKind::Retained) {
keepBytes(target->bytes, std::move(sourceBytes));
@@ -556,6 +560,7 @@ void GfxDevice::updateTexture(TextureHandle texture, i32 x, i32 y, u32 width, u3
keepBytes(record->bytes, uploadOrderImage(upload, pixels));
}
if (record->realized) backendUpdateTexture(id, x, y, width, height, pixels, flipY);
+ record->revision = nextRevision();
// An owed texture's chain is rebuilt when it is paid.
const bool rebuildChain = record->realized && desc.mipmaps && !record->owed;
if (whole) payTexture(id, *record);
@@ -571,6 +576,7 @@ void GfxDevice::setTextureParams(TextureHandle texture, TextureFilter min, Textu
record->desc.magFilter = mag;
record->desc.wrapS = wrapS;
record->desc.wrapT = wrapT;
+ record->revision = nextRevision();
if (record->realized) backendSetTextureParams(id, record->desc);
}
@@ -579,9 +585,15 @@ void GfxDevice::generateMipmaps(TextureHandle texture) {
auto* record = registry_.textures.find(id);
if (!record) return;
record->desc.mipmaps = true;
+ record->revision = nextRevision();
if (record->realized) backendGenerateMipmaps(id);
}
+u64 GfxDevice::textureRevision(TextureHandle texture) const {
+ const auto* record = registry_.textures.find(static_cast<u32>(texture));
+ return record ? record->revision : 0;
+}
+
const TextureDesc* GfxDevice::textureDesc(TextureHandle texture) const {
const auto* record = registry_.textures.find(static_cast<u32>(texture));
return record ? &record->desc : nullptr;
diff --git a/src/esengine/renderer/rhi/GfxDevice.hpp b/src/esengine/renderer/rhi/GfxDevice.hpp
index 93258c3bf..7a5bc433e 100644
--- a/src/esengine/renderer/rhi/GfxDevice.hpp
+++ b/src/esengine/renderer/rhi/GfxDevice.hpp
@@ -436,6 +436,9 @@ public:
void generateMipmaps(TextureHandle texture);
/** @brief A texture's description as it stands, or null for a handle that names none. */
+ /** @brief What @p texture's content is, as a number that changes whenever the device is given new content or sampling
+ * for it (see GfxTextureRecord::revision); 0 for none. */
+ u64 textureRevision(TextureHandle texture) const;
const TextureDesc* textureDesc(TextureHandle texture) const;
/** @brief Activates a texture slot and binds a 2D texture */
diff --git a/src/esengine/renderer/rhi/GfxResourceRegistry.hpp b/src/esengine/renderer/rhi/GfxResourceRegistry.hpp
index 98d41ef29..e8713afb6 100644
--- a/src/esengine/renderer/rhi/GfxResourceRegistry.hpp
+++ b/src/esengine/renderer/rhi/GfxResourceRegistry.hpp
@@ -13,6 +13,7 @@
#pragma once
#include "./GfxContent.hpp"
+#include "../../core/Revision.hpp"
#include "./GfxEnums.hpp"
#include "./PipelineState.hpp"
@@ -58,6 +59,10 @@ struct GfxTextureRecord {
u32 mipLevels = 1;
bool realized = false;
bool owed = false;
+ /// Changes whenever the device is given its content (made, updated, restored, adopted) or its sampling: see
+ /// core/Revision.hpp and GfxDevice::textureRevision. Not when the GPU writes it (a render target, a storage texture) or
+ /// a host swaps what an imported texture shows: those are not uploads this device sees.
+ u64 revision = nextRevision();
};
struct GfxProgramRecord {
diff --git a/src/esengine/renderer/store/MaterialStore.cpp b/src/esengine/renderer/store/MaterialStore.cpp
index 1eae17bc7..4e7197b65 100644
--- a/src/esengine/renderer/store/MaterialStore.cpp
+++ b/src/esengine/renderer/store/MaterialStore.cpp
@@ -36,6 +36,7 @@ void MaterialStore::setResourceManager(resource::ResourceManager* resources) {
void MaterialStore::forgetShader(resource::ShaderHandle shader) {
forgetLookups();
+ reviseMaterialsOf(shader);
layouts_.erase(shader.id());
sources_.erase(shader.id());
auto found = mesh_programs_.find(shader.id());
@@ -428,6 +429,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();
}
}
}
diff --git a/src/esengine/renderer/store/MaterialStore.hpp b/src/esengine/renderer/store/MaterialStore.hpp
index e93a1d170..e860ca245 100644
--- a/src/esengine/renderer/store/MaterialStore.hpp
+++ b/src/esengine/renderer/store/MaterialStore.hpp
@@ -23,6 +23,7 @@
#pragma once
#include "../../core/Types.hpp"
+#include "../../core/Revision.hpp"
#include "../../resource/Handle.hpp"
#include "../draw/BlendMode.hpp"
#include "../rhi/GfxEnums.hpp"
@@ -186,6 +187,9 @@ struct MaterialRecord {
/// Texture params bound to sampler units, by handle (see MaterialTextureBinding).
std::vector<MaterialTextureBinding> textures;
+
+ /// Changes whenever anything above does, or its shader's layout: see core/Revision.hpp and MaterialStore::revisionOf.
+ u64 revision = 0;
};
/**
@@ -341,6 +345,17 @@ public:
if (!shader.isValid()) return;
layout.resolveNamed();
layouts_[shader.id()] = std::move(layout);
+ // Every material on it now reads its params through another layout.
+ reviseMaterialsOf(shader);
+ }
+
+ /**
+ * @brief What @p materialId is, as a number that changes whenever it does: its state, a param, a texture, its shader's layout.
+ * @return 0 for no material. A reader that derived something from a material keeps this beside it (see core/Revision.hpp).
+ */
+ u64 revisionOf(u32 materialId) const {
+ const MaterialRecord* m = find(materialId);
+ return m ? m->revision : 0;
}
/// Pushes a material's resolved render state. Preserves any already-packed constants
@@ -350,7 +365,9 @@ public:
if (materialId == 0) return;
auto it = materials_.find(materialId);
if (it == materials_.end()) {
- materials_[materialId] = record;
+ MaterialRecord& added = materials_[materialId];
+ added = record;
+ added.revision = nextRevision();
return;
}
MaterialRecord& rec = it->second;
@@ -368,6 +385,7 @@ public:
rec.textures = std::move(texs);
}
rec.uboDirty = true; // re-upload on next bind
+ rec.revision = nextRevision();
}
/// Writes a named param's float components into the material's std140 buffer at the
@@ -384,6 +402,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();
}
/// Binds a texture to a named texture param's sampler unit. No-op if the material,
@@ -395,6 +414,7 @@ public:
if (lit == layouts_.end()) return;
const MaterialTextureSlot* slot = lit->second.findTexture(name);
if (!slot) return;
+ it->second.revision = nextRevision();
for (auto& b : it->second.textures) {
if (b.unit == slot->unit) { b.texture = texture; return; }
}
@@ -541,6 +561,10 @@ private:
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; }
+ /** 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();
+ }
/// A material shader's authored source + features, filed under its handle for
/// the same reason the layout is: a rebuilt program changes the id.
diff --git a/src/esengine/renderer/store/ProbeStore.hpp b/src/esengine/renderer/store/ProbeStore.hpp
index c0ac8511d..5402a7a65 100644
--- a/src/esengine/renderer/store/ProbeStore.hpp
+++ b/src/esengine/renderer/store/ProbeStore.hpp
@@ -18,6 +18,7 @@
#pragma once
#include "../../core/Half.hpp"
+#include "../../core/Revision.hpp"
#include "../../core/Types.hpp"
#include "../../resource/ProbeVolume.hpp"
#include "./ProbeConstants.hpp"
@@ -55,6 +56,30 @@ public:
bool empty() const { return volumes_.empty(); }
u32 count() const { return static_cast<u32>(volumes_.size()); }
+ /**
+ * @brief What the frame's probe volumes say, as a number that changes whenever it does (see core/Revision.hpp): asked after the
+ * collect, it moves only when this frame's set differs from the one it last answered for.
+ */
+ u64 revision() const {
+ u64 h = 0xcbf29ce484222325ull;
+ const auto mix = [&h](const void* data, usize bytes) {
+ const auto* p = static_cast<const u8*>(data);
+ for (usize i = 0; i < bytes; ++i) { h ^= p[i]; h *= 0x100000001b3ull; }
+ };
+ for (const Volume& v : volumes_) {
+ mix(&v.min, sizeof(v.min));
+ mix(&v.max, sizeof(v.max));
+ mix(&v.grid, sizeof(v.grid));
+ mix(&v.grid->revision, sizeof(v.grid->revision));
+ }
+ if (revision_ == 0 || h != revision_hash_) {
+ revision_hash_ = h;
+ revision_ = nextRevision();
+ }
+ return revision_;
+ }
+
+
/**
* @brief The irradiance at @p p, read trilinearly. False = no volume holds it.
* @details Probes sit on the box's CORNERS, so a volume's own bounds are
@@ -255,6 +280,8 @@ private:
}
std::vector<Volume> volumes_;
+ mutable u64 revision_ = 0;
+ mutable u64 revision_hash_ = 0;
std::vector<u64> atlas_key_;
u32 atlas_width_ = 0;
u32 atlas_height_ = 0;
diff --git a/src/esengine/renderer/store/ReflectionStore.hpp b/src/esengine/renderer/store/ReflectionStore.hpp
index 95f92aac1..0680adf1b 100644
--- a/src/esengine/renderer/store/ReflectionStore.hpp
+++ b/src/esengine/renderer/store/ReflectionStore.hpp
@@ -17,6 +17,7 @@
#pragma once
#include "../../core/Types.hpp"
+#include "../../core/Revision.hpp"
#include "./LightConstants.hpp"
#include <glm/glm.hpp>
@@ -60,6 +61,30 @@ public:
bool empty() const { return probes_.empty(); }
u32 count() const { return static_cast<u32>(probes_.size()); }
+ /**
+ * @brief What the frame's reflection probes say, as a number that changes whenever it does (see core/Revision.hpp): asked after the
+ * collect, it moves only when this frame's set differs from the one it last answered for.
+ */
+ u64 revision() const {
+ u64 h = 0xcbf29ce484222325ull;
+ const auto mix = [&h](const void* data, usize bytes) {
+ const auto* p = static_cast<const u8*>(data);
+ for (usize i = 0; i < bytes; ++i) { h ^= p[i]; h *= 0x100000001b3ull; }
+ };
+ mix(&environment_, sizeof(environment_));
+ for (const Probe& p : probes_) {
+ mix(&p.min, sizeof(p.min));
+ mix(&p.max, sizeof(p.max));
+ mix(&p.slot, sizeof(p.slot));
+ }
+ if (revision_ == 0 || h != revision_hash_) {
+ revision_hash_ = h;
+ revision_ = nextRevision();
+ }
+ return revision_;
+ }
+
+
/** @brief The baked environment every probe of this frame reads, or 0. */
u32 environment() const { return environment_; }
@@ -109,6 +134,8 @@ public:
private:
std::vector<Probe> probes_;
u32 environment_ = 0;
+ mutable u64 revision_ = 0;
+ mutable u64 revision_hash_ = 0;
};
} // namespace esengine
diff --git a/src/esengine/resource/Mesh.hpp b/src/esengine/resource/Mesh.hpp
index 1b140e0bd..79549f7df 100644
--- a/src/esengine/resource/Mesh.hpp
+++ b/src/esengine/resource/Mesh.hpp
@@ -130,6 +130,9 @@ public:
return morphTargetCount > 0 && morphTexture != TextureHandle::Invalid;
}
+ /** Changes whenever this mesh's geometry does (it is realized again: a reload, a recovery): see core/Revision.hpp. */
+ u64 revision = 0;
+
/** Whether GPU buffers stand behind this identity. */
bool hasRealization() const {
return vertexBuffer != BufferHandle::Invalid
diff --git a/src/esengine/resource/ProbeVolume.hpp b/src/esengine/resource/ProbeVolume.hpp
index 9bea69b4e..c563bfcfd 100644
--- a/src/esengine/resource/ProbeVolume.hpp
+++ b/src/esengine/resource/ProbeVolume.hpp
@@ -7,6 +7,7 @@
#pragma once
#include "../core/Types.hpp"
+#include "../core/Revision.hpp"
#include "../math/Math.hpp"
#include <atomic>
@@ -43,10 +44,7 @@ public:
* grids, so a released grid whose memory a new one reuses is never mistaken for it. */
u64 revision = 0;
- static u64 nextRevision() {
- static std::atomic<u64> next{0};
- return ++next;
- }
+ static u64 nextRevision() { return esengine::nextRevision(); }
bool hasSkyVisibility() const {
return !skyVisibility.empty() && skyVisibility.size() == static_cast<usize>(probeCount()) * 9;
diff --git a/src/esengine/resource/ResourceManager.cpp b/src/esengine/resource/ResourceManager.cpp
index bdd0d7b09..020a0a072 100644
--- a/src/esengine/resource/ResourceManager.cpp
+++ b/src/esengine/resource/ResourceManager.cpp
@@ -18,6 +18,7 @@
#include "ShaderParser.hpp"
#include "../text/BitmapFont.hpp"
#include "../core/Log.hpp"
+#include "../core/Revision.hpp"
#include "../renderer/rhi/GfxDevice.hpp"
#include "../renderer/rhi/Shader.hpp"
#include "../renderer/rhi/Texture.hpp"
@@ -723,6 +724,7 @@ bool ResourceManager::realizeMesh(Mesh& mesh, ConstSpan<u8> vertexBytes, ConstSp
std::memcpy(mesh.inverseBind.data(), inverseBind.data(), joints * sizeof(glm::mat4));
}
mesh.vertexCount = vertexStride > 0 ? static_cast<u32>(vertexBytes.size()) / vertexStride : 0;
+ mesh.revision = nextRevision();
// The shapes, last: geometry that draws is worth having even when the deltas
// behind it are refused, and the refusal leaves a mesh in its authored shape
diff --git a/tests/renderer/test_probe_store.cpp b/tests/renderer/test_probe_store.cpp
index ce9784b92..d0a8cefdd 100644
--- a/tests/renderer/test_probe_store.cpp
+++ b/tests/renderer/test_probe_store.cpp
@@ -197,7 +197,30 @@ static void testSampleByBox() {
CHECK(!store.sampleBox({-50.0f, 0.0f, -50.0f}, {1.0f, 1.0f, 1.0f}, out), "and one touching nothing reads nothing");
}
+/** The store is rebuilt every frame: its revision says whether this frame's volumes are the last frame's. */
+static void testRevision() {
+ ProbeVolume v = ramp({1, 1, 1}, {1.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f});
+ v.revision = ProbeVolume::nextRevision();
+ ProbeStore store;
+ store.add({-1.0f, -1.0f, -1.0f}, {1.0f, 1.0f, 1.0f}, &v);
+ const u64 first = store.revision();
+ CHECK(first != 0 && store.revision() == first, "a frame's volumes have a revision, asked as often as one likes");
+ store.clear();
+ store.add({-1.0f, -1.0f, -1.0f}, {1.0f, 1.0f, 1.0f}, &v);
+ CHECK(store.revision() == first, "the same volumes the next frame keep it");
+ v.revision = ProbeVolume::nextRevision();
+ CHECK(store.revision() != first, "a grid given new coefficients moves it");
+ const u64 regrid = store.revision();
+ store.clear();
+ store.add({-2.0f, -1.0f, -1.0f}, {1.0f, 1.0f, 1.0f}, &v);
+ CHECK(store.revision() != regrid, "a volume that moved moves it");
+ const u64 moved = store.revision();
+ store.clear();
+ CHECK(store.revision() != moved, "and so does no volume at all");
+}
+
int main() {
+ testRevision();
testInsideAndOutside();
testTrilinearOnEachAxis();
testSmallestVolumeWins();
diff --git a/tests/renderer/test_reflection_store.cpp b/tests/renderer/test_reflection_store.cpp
index 8914b1736..0c0170d08 100644
--- a/tests/renderer/test_reflection_store.cpp
+++ b/tests/renderer/test_reflection_store.cpp
@@ -85,7 +85,26 @@ static void testClearing() {
"clearing leaves the next frame nothing of this one's");
}
+/** Rebuilt every frame: the revision says whether this frame's probes are the last frame's. */
+static void testRevision() {
+ ReflectionStore store;
+ store.add({-1.0f, -1.0f, -1.0f}, {1.0f, 1.0f, 1.0f}, 1, 7);
+ const u64 first = store.revision();
+ CHECK(first != 0 && store.revision() == first, "a frame's probes have a revision, asked as often as one likes");
+ store.clear();
+ store.add({-1.0f, -1.0f, -1.0f}, {1.0f, 1.0f, 1.0f}, 1, 7);
+ CHECK(store.revision() == first, "the same probes the next frame keep it");
+ store.clear();
+ store.add({-1.0f, -1.0f, -1.0f}, {1.0f, 1.0f, 1.0f}, 2, 7);
+ CHECK(store.revision() != first, "a probe in another column moves it");
+ const u64 column = store.revision();
+ store.clear();
+ store.add({-1.0f, -1.0f, -1.0f}, {1.0f, 1.0f, 1.0f}, 2, 8);
+ CHECK(store.revision() != column, "and so does another bake");
+}
+
int main() {
+ testRevision();
testEmpty();
testInsideAndOutside();
testSmallestWins();
diff --git a/tests/resource/test_material_shader_lifetime.cpp b/tests/resource/test_material_shader_lifetime.cpp
index 7bb2bb451..ab3fe5012 100644
--- a/tests/resource/test_material_shader_lifetime.cpp
+++ b/tests/resource/test_material_shader_lifetime.cpp
@@ -21,6 +21,46 @@ void main() { fragColor = v_color; }
#pragma end
)";
int main() {
+ // --- A material's revision moves with every change to it, and only then ---
+ {
+ MockGfxDevice device; resource::ResourceManager rm; rm.init(device);
+ MaterialStore store; store.setDevice(&device); store.setResourceManager(&rm);
+ auto shader = rm.createShader("vertex", "fragment", false);
+ auto other = rm.createShader("vertex", "fragment", false);
+ MaterialUniformLayout layout;
+ layout.blockSize = 16;
+ layout.params.push_back(MaterialParamSlot{"u_tint", 0, 4});
+ MaterialTextureSlot tex;
+ tex.name = "u_tex";
+ tex.unit = 0;
+ layout.textures.push_back(tex);
+ store.registerLayout(shader, layout);
+ store.registerLayout(other, {});
+ CHECK(store.revisionOf(1) == 0, "no material, no revision");
+ MaterialRecord record; record.shaderRef = shader; store.define(1, record);
+ MaterialRecord second; second.shaderRef = other; store.define(2, second);
+ const u64 defined = store.revisionOf(1);
+ CHECK(defined != 0 && defined != store.revisionOf(2), "a defined material has a revision of its own");
+ store.find(1); store.layoutFor(shader);
+ CHECK(store.revisionOf(1) == defined, "reading it changes nothing");
+ const f32 tint[4] = {1, 0, 0, 1};
+ store.setUniform(1, "u_tint", tint, 4);
+ const u64 tinted = store.revisionOf(1);
+ CHECK(tinted != defined, "a param moves it");
+ store.setUniform(1, "u_missing", tint, 4);
+ CHECK(store.revisionOf(1) == tinted, "a param it does not have does not");
+ store.setTexture(1, "u_tex", resource::TextureHandle(3));
+ const u64 textured = store.revisionOf(1);
+ CHECK(textured != tinted, "a texture moves it");
+ record.blend = BlendMode::Additive; store.define(1, record);
+ const u64 redefined = store.revisionOf(1);
+ CHECK(redefined != textured, "its state pushed again moves it");
+ const u64 otherBefore = store.revisionOf(2);
+ 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();
+ }
{
MockGfxDevice device; resource::ResourceManager rm; rm.init(device);
MaterialStore store; store.setDevice(&device); store.setResourceManager(&rm);
diff --git a/tests/resource/test_mesh_recovery.cpp b/tests/resource/test_mesh_recovery.cpp
index 159a5b127..90a8e1026 100644
--- a/tests/resource/test_mesh_recovery.cpp
+++ b/tests/resource/test_mesh_recovery.cpp
@@ -270,6 +270,45 @@ int main() {
CHECK(rm.getMesh(h)->isDrawable(), "and keeps the geometry the device restored");
}
+ // --- A mesh's revision is new each time it is realized, and its own ---
+ {
+ MockGfxDevice d;
+ resource::ResourceManager rm;
+ rm.init(d);
+ const auto a = makeMesh(rm, MeshRecovery::SourceReplayable);
+ const auto b = makeMesh(rm, MeshRecovery::SourceReplayable);
+ const u64 first = rm.getMesh(a)->revision;
+ CHECK(first != 0 && first != rm.getMesh(b)->revision, "a realized mesh has a revision of its own");
+ CHECK(rematerialize(rm, a, 2.0f) && rm.getMesh(a)->revision != first, "realized again, it has another");
+ const u64 standing = rm.getMesh(a)->revision;
+ d.createBufferSucceeds = false;
+ rematerializeWide(rm, a);
+ d.createBufferSucceeds = true;
+ CHECK(rm.getMesh(a)->revision == standing, "a rebuild that failed leaves it as it was");
+ }
+
+ // --- A texture's revision moves when the device is given new content or sampling for it ---
+ {
+ MockGfxDevice d;
+ TextureDesc desc;
+ desc.width = 2;
+ desc.height = 2;
+ const u8 pixels[16] = {};
+ const TextureHandle t = d.createTexture(desc, GfxContent::transient(), pixels);
+ const TextureHandle u = d.createTexture(desc, GfxContent::transient(), pixels);
+ const u64 made = d.textureRevision(t);
+ CHECK(made != 0 && made != d.textureRevision(u), "a texture has a revision of its own");
+ d.updateTexture(t, 0, 0, 1, 1, pixels, false);
+ const u64 updated = d.textureRevision(t);
+ CHECK(updated != made, "an upload moves it");
+ d.setTextureParams(t, TextureFilter::Nearest, TextureFilter::Nearest, TextureWrap::ClampToEdge, TextureWrap::ClampToEdge);
+ CHECK(d.textureRevision(t) != updated, "new sampling moves it");
+ d.textureDesc(t);
+ const u64 asked = d.textureRevision(t);
+ CHECK(d.textureRevision(t) == asked, "asking about it does not");
+ CHECK(d.textureRevision(TextureHandle::Invalid) == 0, "no texture, no revision");
+ }
+
std::printf(g_failures ? "\n%d FAILURE(S)\n" : "\nall passed\n", g_failures);
return g_failures ? 1 : 0;
}
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index 9a519860b..b1076e4dd 100644
--- a/tools/releaseNotes.mjs
+++ b/tools/releaseNotes.mjs
@@ -4836,4 +4836,6 @@ export const NOTED = {
{"note": "Nudging a selection moves each selected root once, so a selected child no longer travels twice."},
"fix(pick): a shape is selected by its drawn size and layer, so a click inside it lands":
{"note": "A click selects a shape by its drawn size, and a bare Canvas no longer takes the click."},
+ "feat(render): a mesh, a material, a texture and the frame's probes each carry a revision that changes whenever their content does - one counter for all of them (core/Revision.hpp), so what the renderer derives from a resource can be kept until the resource itself changes":
+ {"note": "Resources carry a revision that changes with their content."},
};