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(webgpu): a view of many materials no longer loses the device - the bind group cache held 256 and was emptied when full, so a view that binds more made every group again each frame and the device ran out of descriptors after some hundreds of frames; it holds 4096 by hash and lets go only of what the last two frames did not bind

yhh292055c735f2
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 5087b728e..e3faefd42 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -39,6 +39,7 @@ published separately; it ships inside the editor.
### Fixed
+- **A view of many materials no longer loses the WebGPU device.** The WebGPU backend kept 256 bind groups and emptied the whole cache when it filled, on the assumption that a steady scene never fills it. A view that binds more (three imported towns at once: hundreds of materials, each in several passes) made every group again every frame, and on Windows the device was lost with `CreateDescriptorHeap failed with E_OUTOFMEMORY` after 700 frames with three towns in view and 1190 with two; one town was under the limit and ran. The cache holds 4096, is found by hash instead of a walk of every entry, and when full lets go only of the groups neither this frame nor the last bound.
- **A waterfall's foot is torn to veils, and light goes through it.** A `Waterfall`'s sheets are blended one over another, so three of them added up to a white board in the lower half of the fall. The stock shader now tears more of the foot away (`u_veil` 0.75, by the foot itself, `u_tearBy` 1): beside the fall it was made from, the foot is 10 levels of 255 over and the middle 14, where the foot was 55 over. And a sheet was lit as an opaque wall facing one way; `u_translucency` (1 by default) lets the light through, as the Model shader's does. `u_veil` 0.47, `u_tearBy` 0.85 and `u_translucency` 0 are the sheet as it was.
- **A waterfall falls in strands, not in speckle.** The stock Waterfall shader's pattern was sixteen times too fine each way: sixty cells across half a sheet and fifteen to a second of the fall, so a sheet read as a solid white board peppered with dark flecks. About four strands now lie across half a sheet (`u_strands` 3.75) and one lasts about a second of the fall (`u_run` 0.97, now the count itself and no longer a share of 28), and a little more of the foot is torn away (`u_veil` 0.47). `u_strands` 60, `u_run` 15.4 and `u_veil` 0.42 are the old sheet.
- **A far plant grows across the ground, not taller.** A mesh plant kept in one of `Foliage`'s far versions was grown the same every way to cover the ground of those left out. Cover is a matter of width: a flower that stood for ten stood ten times as high as well, and a meadow seen from a hill was a field of dark poles. A kept plant now grows across the ground only and is as tall as it was. `farGrowUp` (0) is the share of the growth its height takes: 1 is the growth every way that it was before.
diff --git a/src/esengine/renderer/webgpu/WebGPUDevice.cpp b/src/esengine/renderer/webgpu/WebGPUDevice.cpp
index c43b9294a..e648158a7 100644
--- a/src/esengine/renderer/webgpu/WebGPUDevice.cpp
+++ b/src/esengine/renderer/webgpu/WebGPUDevice.cpp
@@ -187,8 +187,9 @@ void WebGPUDevice::releaseDeviceObjects() {
span_passes_ = 0;
// bind_group_/texture_group_ point INTO the cache — release the cache, not them.
- for (auto& e : bind_group_cache_) if (e.bg) wgpuBindGroupRelease(e.bg);
+ for (auto& bucket : bind_group_cache_) for (auto& e : bucket.second) if (e.bg) wgpuBindGroupRelease(e.bg);
bind_group_cache_.clear();
+ bind_group_count_ = 0;
bind_group_ = nullptr;
texture_group_ = nullptr;
bind_group_dirty_ = true;
@@ -787,18 +788,23 @@ void WebGPUDevice::backendDeleteBuffer(u32 id) {
}
void WebGPUDevice::evictBindGroups(u64 id) {
- for (usize i = bind_group_cache_.size(); i-- > 0;) {
- auto& e = bind_group_cache_[i];
- bool hit = false;
- for (u64 v : e.ids) {
- if (v == id) { hit = true; break; }
+ for (auto it = bind_group_cache_.begin(); it != bind_group_cache_.end();) {
+ auto& bucket = it->second;
+ for (usize i = bucket.size(); i-- > 0;) {
+ auto& e = bucket[i];
+ bool hit = false;
+ for (u64 v : e.ids) {
+ if (v == id) { hit = true; break; }
+ }
+ if (!hit) continue;
+ if (e.bg == bind_group_) bind_group_ = nullptr;
+ if (e.bg == texture_group_) texture_group_ = nullptr;
+ if (e.bg) wgpuBindGroupRelease(e.bg);
+ bucket.erase(bucket.begin() + static_cast<std::ptrdiff_t>(i));
+ --bind_group_count_;
+ bind_group_dirty_ = true;
}
- if (!hit) continue;
- if (e.bg == bind_group_) bind_group_ = nullptr;
- if (e.bg == texture_group_) texture_group_ = nullptr;
- if (e.bg) wgpuBindGroupRelease(e.bg);
- bind_group_cache_.erase(bind_group_cache_.begin() + static_cast<std::ptrdiff_t>(i));
- bind_group_dirty_ = true;
+ it = bucket.empty() ? bind_group_cache_.erase(it) : std::next(it);
}
}
@@ -2133,23 +2139,38 @@ void WebGPUDevice::ensureDummies() {
WGPUBindGroup WebGPUDevice::cachedBindGroup(const u64* ids, u32 idCount,
const WGPUBindGroupDescriptor& desc) {
- for (auto& e : bind_group_cache_) {
- if (e.layout != desc.layout || e.ids.size() != idCount) continue;
- bool same = true;
- for (u32 i = 0; i < idCount; ++i) {
- if (e.ids[i] != ids[i]) { same = false; break; }
+ u64 hash = 1469598103934665603ull ^ reinterpret_cast<uintptr_t>(desc.layout);
+ for (u32 i = 0; i < idCount; ++i) hash = (hash ^ ids[i]) * 1099511628211ull;
+ auto found = bind_group_cache_.find(hash);
+ if (found != bind_group_cache_.end()) {
+ for (auto& e : found->second) {
+ if (e.layout != desc.layout || e.ids.size() != idCount) continue;
+ bool same = true;
+ for (u32 i = 0; i < idCount; ++i) {
+ if (e.ids[i] != ids[i]) { same = false; break; }
+ }
+ if (same) { e.used = bind_group_frame_; return e.bg; } // hit — reuse, no wgpuDeviceCreateBindGroup
}
- if (same) return e.bg; // hit — reuse, no wgpuDeviceCreateBindGroup
}
- // Miss. A stable scene never fills the cache; clearing when full is a simple,
- // safe eviction — a group bound this frame stays alive via the pass encoder's
- // own reference until submit, so releasing our handle now is fine.
- if (bind_group_cache_.size() >= kBindGroupCacheCap) {
- for (auto& e : bind_group_cache_) if (e.bg) wgpuBindGroupRelease(e.bg);
- bind_group_cache_.clear();
+ // Miss. When full, only what neither this frame nor the last bound goes: emptying the cache made a view of more groups than
+ // it holds create every one of them again each frame, and the device ran out of descriptors within some hundreds of frames.
+ if (bind_group_count_ >= kBindGroupCacheCap) {
+ for (auto it = bind_group_cache_.begin(); it != bind_group_cache_.end();) {
+ auto& bucket = it->second;
+ for (usize i = bucket.size(); i-- > 0;) {
+ if (bucket[i].used + 1 >= bind_group_frame_) continue;
+ if (bucket[i].bg) wgpuBindGroupRelease(bucket[i].bg);
+ bucket.erase(bucket.begin() + static_cast<std::ptrdiff_t>(i));
+ --bind_group_count_;
+ }
+ it = bucket.empty() ? bind_group_cache_.erase(it) : std::next(it);
+ }
}
WGPUBindGroup bg = wgpuDeviceCreateBindGroup(device_, &desc);
- if (bg) bind_group_cache_.push_back({desc.layout, std::vector<u64>(ids, ids + idCount), bg});
+ if (bg) {
+ bind_group_cache_[hash].push_back({desc.layout, std::vector<u64>(ids, ids + idCount), bg, bind_group_frame_});
+ ++bind_group_count_;
+ }
return bg;
}
@@ -2602,6 +2623,7 @@ void WebGPUDevice::endRenderPass() {
/** Give the swapchain texture back: the frame is over (see GfxDevice::endFrame).
* A frame may have opened several passes on it, so no single pass may do this. */
void WebGPUDevice::endFrame() {
+ ++bind_group_frame_;
// A capture is served HERE and nowhere else: this is the last instant at which
// the frame is both finished and still ours. Asking from outside cannot work —
// whoever could ask runs after the renderer has already given the image back.
diff --git a/src/esengine/renderer/webgpu/WebGPUDevice.hpp b/src/esengine/renderer/webgpu/WebGPUDevice.hpp
index 2c5e27a0e..44117ae7e 100644
--- a/src/esengine/renderer/webgpu/WebGPUDevice.hpp
+++ b/src/esengine/renderer/webgpu/WebGPUDevice.hpp
@@ -634,9 +634,15 @@ private:
WGPUBindGroupLayout layout;
std::vector<u64> ids; ///< WGPU resource pointers (buffers, or view+sampler), binding order.
WGPUBindGroup bg;
+ u64 used; ///< The frame that last bound it.
};
- static constexpr u32 kBindGroupCacheCap = 256;
- std::vector<BindGroupCacheEntry> bind_group_cache_;
+ /// How many groups are kept before those no frame has bound lately are let go. A group bound this frame or the last is never
+ /// let go for room: a view with more than this many is kept whole, since making them again every frame exhausts the device.
+ static constexpr u32 kBindGroupCacheCap = 4096;
+ /// By a hash of the layout and the ids; a bucket holds the few that share one.
+ std::unordered_map<u64, std::vector<BindGroupCacheEntry>> bind_group_cache_;
+ u32 bind_group_count_ = 0;
+ u64 bind_group_frame_ = 0;
/// Reuse a cached group with these exact contents, else create + insert one.
WGPUBindGroup cachedBindGroup(const u64* ids, u32 idCount,
const WGPUBindGroupDescriptor& desc);
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index 3f95a7b49..cfc70eed6 100644
--- a/tools/releaseNotes.mjs
+++ b/tools/releaseNotes.mjs
@@ -4586,6 +4586,8 @@ export const NOTED = {
{"note": "A waterfall falls in strands, not in speckle."},
"fix(water): a waterfall's foot is torn to veils, and light goes through it - three blended sheets added up to a board at the foot, where the fall the shader was made from shows the most of its three: the stock Waterfall tears more of the foot away (u_veil 0.75, u_tearBy 1), and u_translucency lights the sheet from whichever side the sun is on":
{"note": "A waterfall's foot is torn to veils, and light goes through it."},
+ "fix(webgpu): a view of many materials no longer loses the device - the bind group cache held 256 and was emptied when full, so a view that binds more made every group again each frame and the device ran out of descriptors after some hundreds of frames; it holds 4096 by hash and lets go only of what the last two frames did not bind":
+ {"note": "A view of many materials no longer loses the WebGPU device."},
"fix(terrain): a detail the pages cannot hold no longer cracks the ground - a tree that wanted more nodes than the atlas has pages was drawn in part by coarser pages standing in, which do not meet their finer neighbours' edges, and the pages were fought over every frame; the ground is now drawn at the share of its detail's range that fits, and says so once":
{"note": "A detail the pages cannot hold no longer cracks the ground."},
"fix(foliage): a far plant grows across the ground, not taller - a mesh plant kept in a far version was grown the same every way to cover the ground of those left out, so a flower that stood for ten stood ten times as high and a far meadow was a field of poles; it now grows across the ground only, and Foliage.farGrowUp gives its height a share of the growth back":