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.

fix(render): the target pool no longer destroys a frame's own targets every frame once they add up past its idle budget, which cost a phone 40 ms at quality high

yhh166c0df22adb
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 35171275d..b39913ff4 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -23,7 +23,8 @@ published separately; it ships inside the editor.
### Fixed
-- **A linear project on a native build is no longer far too dark.** A native host read only half of the packaged config. The colour space, output transform, MSAA, quality, y-sort and depth layers and camera fit never reached it, with no error. A linear game ran as gamma and showed its linear values with no encode: a grey of 0.2 read 51 instead of 124, on Android, Windows and every other native build. Web and native now apply these through one function, and the colour space is read before the first shader compiles. A native build matches the web to about one level in the test room. Linear costs a phone 2.7 ms a frame at 2670x1200 with nothing else on. A project that declares a quality level now gets it on a device, including what that level costs: `high` measured 50 ms a frame on a Snapdragon 8 Elite, unexplained so far, against 7 ms for `medium`.
+- **Quality high no longer costs a phone 40 ms a frame.** The render target pool keeps at most 256 MB of targets nobody has borrowed, and it counted what the frame had just given back as nobody's. A frame's own targets past that size were destroyed and allocated again every frame: a 4096 shadow atlas and a 4x MSAA scene target at 2670x1200 come to 320 MB. On a Snapdragon 8 Elite `high` went from 50 to 10.5 ms with GI off, level with no quality setting at all. Any platform with targets that large paid the same.
+- **A linear project on a native build is no longer far too dark.** A native host read only half of the packaged config. The colour space, output transform, MSAA, quality, y-sort and depth layers and camera fit never reached it, with no error. A linear game ran as gamma and showed its linear values with no encode: a grey of 0.2 read 51 instead of 124, on Android, Windows and every other native build. Web and native now apply these through one function, and the colour space is read before the first shader compiles. A native build matches the web to about one level in the test room. Linear costs a phone 2.7 ms a frame at 2670x1200 with nothing else on. A project that declares a quality level now gets it on a device.
- **Voxel GI no longer changes with the size of the picture.** It took the same one pixel in four for its cache every frame, so a face a few rows tall (the top of a block seen nearly edge on) landed in it at one row or two according to the window's size, and the light around it moved: a wall read 18% apart across sizes, up to 11% for a window one pixel taller. The pixel now changes each frame. Across eight of nine sizes the same wall is within 7%, one size with an odd width and height still reads higher, and the light against Cycles and the frame-to-frame stability are unchanged.
- **Voxel GI costs a third of what it did.** Every cell a ray crossed took two integer divisions, a hash, and a read in the 32 MB grid, which is a cache miss, for a cell that is nearly always empty. Wrapping is now a mask, the hash check is gone (the cache re-tags every slot each frame, so it could not fail), and the tags hold a mark saying whether a slot has anything, read before the grid is. On an RTX 3060 at 720p the default settings went from 17.3 to about 5.4 ms a frame, and the picture is the same: held against the old kernels, the difference is no larger than between two runs of one build.
- **Writing an entity list on the web replaces it.** A `SpringBone.colliders` or `MeshSkin.joints` written to a component that already had one was added to what was there, so a `[a, b]` written twice read `[a, b, a, b]`, a `Mut` row grew it on every write-back (72 entries in a few frames) and writing `[]` cleared nothing. Number lists and native builds were right.
diff --git a/src/esengine/renderer/graph/TargetPool.cpp b/src/esengine/renderer/graph/TargetPool.cpp
index 2a0f39453..c1c40bba4 100644
--- a/src/esengine/renderer/graph/TargetPool.cpp
+++ b/src/esengine/renderer/graph/TargetPool.cpp
@@ -152,15 +152,17 @@ void TargetPool::age() {
}
}
// A viewport that is being resized borrows a new shape every tick and leaves the last one idle: two seconds of that is a hundred
- // sets of targets. What sits idle past the budget goes at once, the longest idle first, whatever its age.
+ // sets of targets. What sits idle past the budget goes at once, the longest idle first, whatever its age. Only what has really been
+ // left: everything a frame borrows has just been given back when this runs, so counting that too destroyed and rebuilt a frame's own
+ // targets every frame once they added up past the budget (a 4096 shadow atlas and a 4x scene target at 2670x1200: 40 ms on a phone).
u64 idleBytes = 0;
for (const auto& entry : entries_) {
- if (entry.fbo && !entry.inUse) idleBytes += static_cast<u64>(entry.shape.width) * entry.shape.height * texelBytes(entry.shape);
+ if (entry.fbo && !entry.inUse && entry.idle >= kBudgetGraceTicks) idleBytes += static_cast<u64>(entry.shape.width) * entry.shape.height * texelBytes(entry.shape);
}
while (idleBytes > kIdleByteBudget) {
Entry* oldest = nullptr;
for (auto& entry : entries_) {
- if (entry.fbo && !entry.inUse && (!oldest || entry.idle > oldest->idle)) oldest = &entry;
+ if (entry.fbo && !entry.inUse && entry.idle >= kBudgetGraceTicks && (!oldest || entry.idle > oldest->idle)) oldest = &entry;
}
if (!oldest) break;
idleBytes -= static_cast<u64>(oldest->shape.width) * oldest->shape.height * texelBytes(oldest->shape);
diff --git a/src/esengine/renderer/graph/TargetPool.hpp b/src/esengine/renderer/graph/TargetPool.hpp
index 5dfa55c4a..e93756bd6 100644
--- a/src/esengine/renderer/graph/TargetPool.hpp
+++ b/src/esengine/renderer/graph/TargetPool.hpp
@@ -125,6 +125,10 @@ public:
/** @brief Bytes of unborrowed targets the pool keeps, whatever their age: past it the longest idle are destroyed. */
static constexpr u64 kIdleByteBudget = 256ull * 1024 * 1024;
+ /** @brief Ticks unborrowed before a target counts against that budget. More than one, since age runs once per graph and a frame can
+ * have several: a target one graph borrows is otherwise idle on the ticks the others take. */
+ static constexpr u32 kBudgetGraceTicks = 4;
+
private:
struct Entry {
Unique<Framebuffer> fbo;
diff --git a/tests/renderer/test_render_graph.cpp b/tests/renderer/test_render_graph.cpp
index 217b8166f..4fbb771c2 100644
--- a/tests/renderer/test_render_graph.cpp
+++ b/tests/renderer/test_render_graph.cpp
@@ -428,6 +428,41 @@ int main() {
CHECK(pool.count() < 200, "and the sizes the window has left are gone");
}
+ // --- what a frame uses every frame is kept, however much of it there is ---
+ {
+ MockGfxDevice d;
+ rg::TargetPool pool(d);
+ // Past the idle budget between them, as a 4096 shadow atlas and a 4x scene target at 2670x1200 are. All of it has just been given
+ // back when the pool ages, and that is not the same as nobody wanting it.
+ rg::TargetShape atlas;
+ atlas.width = atlas.height = 4096;
+ atlas.depthStencil = true;
+ rg::TargetShape scene;
+ scene.width = 2670;
+ scene.height = 1200;
+ scene.format = GfxPixelFormat::RGBA16F;
+ scene.depthStencil = true;
+ scene.samples = 4;
+ const rg::TargetHandle firstAtlas = pool.acquire(atlas);
+ const rg::TargetHandle firstScene = pool.acquire(scene);
+ CHECK(pool.bytes() > rg::TargetPool::kIdleByteBudget, "the two together are past the budget");
+ pool.release(firstAtlas);
+ pool.release(firstScene);
+ for (u32 frame = 0; frame < 300; ++frame) {
+ pool.age();
+ const rg::TargetHandle a = pool.acquire(atlas);
+ const rg::TargetHandle s = pool.acquire(scene);
+ pool.release(a);
+ pool.release(s);
+ }
+ CHECK(pool.holds(firstAtlas) && pool.holds(firstScene), "a frame's own targets are not destroyed and rebuilt every frame");
+ CHECK(pool.count() == 2, "and no second set has grown");
+
+ // And once nothing asks for them, they do go.
+ for (u32 tick = 0; tick < rg::TargetPool::kBudgetGraceTicks + 1; ++tick) pool.age();
+ CHECK(pool.bytes() <= rg::TargetPool::kIdleByteBudget, "left alone, they answer to the budget again");
+ }
+
// --- a handle outliving its target names nothing, not the next thing ---
{
MockGfxDevice d;
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index a1ed7d6de..55c033bec 100644
--- a/tools/releaseNotes.mjs
+++ b/tools/releaseNotes.mjs
@@ -3858,4 +3858,6 @@ export const NOTED = {
{"note":"A linear project on a native build is no longer far too dark."},
"fix(water): the albedo lines of the water shader go back inside its source, where a misplaced edit left them as top-level script that stopped every export from loading":
{"internal":"broke after 0.98.0 and before any release, so no creator ever had it"},
+ "fix(render): the target pool no longer destroys a frame's own targets every frame once they add up past its idle budget, which cost a phone 40 ms at quality high":
+ {"note":"Quality high no longer costs a phone 40 ms a frame."},
};