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.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
fix(renderer): WebGPU bind-group cache evicts dying resource ids
yhhe9eca86befd4
diff --git a/desktop/src/engine/EngineHost.ts b/desktop/src/engine/EngineHost.tsindex 479b05958..c1c3a9923 100644--- a/desktop/src/engine/EngineHost.ts+++ b/desktop/src/engine/EngineHost.ts@@ -524,6 +524,15 @@ class EngineHostImpl {console.info(`[engine] webgpu timestamp-query: ${hasTimestamp ? 'enabled (GPU timing on)' : 'unavailable (no GPU timing)'}`);moduleArg.preinitializedWebGPUDevice = await adapter.requestDevice(requiredFeatures.length ? { requiredFeatures } : undefined);+ // Surface Dawn validation failures: without a listener an invalid draw is+ // silently dropped — a black pass with no trace (exactly how the WGSL+ // bloom-chain regression hid). console.error passes the shot/verify+ // console filters, so headless runs carry the evidence.+ (moduleArg.preinitializedWebGPUDevice as {+ addEventListener?: (t: string, cb: (e: { error?: { message?: string } }) => void) => void;+ }).addEventListener?.('uncapturederror', (e) => {+ console.error('[webgpu] uncaptured error:', e.error?.message ?? e);+ });// The swapchain glue resolves the canvas by document.querySelector, so// it must be connected (the headless host never attaches it to a view).// Pin it at the page origin at its backing size: a hidden window neverdiff --git a/src/esengine/renderer/webgpu/WebGPUDevice.cpp b/src/esengine/renderer/webgpu/WebGPUDevice.cppindex 2def788ba..153edae97 100644--- a/src/esengine/renderer/webgpu/WebGPUDevice.cpp+++ b/src/esengine/renderer/webgpu/WebGPUDevice.cpp@@ -284,10 +284,29 @@ BufferHandle WebGPUDevice::createBuffer(const BufferDesc& desc, const void* initvoid WebGPUDevice::deleteBuffer(BufferHandle buffer) {auto it = buffers_.find(static_cast<u32>(buffer));if (it == buffers_.end()) return;- if (it->second.buffer) wgpuBufferRelease(it->second.buffer);+ if (it->second.buffer) {+ evictBindGroups(static_cast<u64>(reinterpret_cast<uintptr_t>(it->second.buffer)));+ wgpuBufferRelease(it->second.buffer);+ }buffers_.erase(it);}+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; }+ }+ 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;+ }+}+void WebGPUDevice::updateBuffer(BufferHandle buffer, u32 offsetBytes, const void* data, u32 sizeBytes) {auto it = buffers_.find(static_cast<u32>(buffer));if (it == buffers_.end() || !data || sizeBytes == 0) return;@@ -432,7 +451,10 @@ TextureHandle WebGPUDevice::importExternalTexture(u32, const TextureDesc&) {void WebGPUDevice::deleteTexture(TextureHandle texture) {auto it = textures_.find(static_cast<u32>(texture));if (it == textures_.end()) return;- if (it->second.view) wgpuTextureViewRelease(it->second.view);+ if (it->second.view) {+ evictBindGroups(static_cast<u64>(reinterpret_cast<uintptr_t>(it->second.view)));+ wgpuTextureViewRelease(it->second.view);+ }if (it->second.texture) wgpuTextureRelease(it->second.texture);textures_.erase(it);}diff --git a/src/esengine/renderer/webgpu/WebGPUDevice.hpp b/src/esengine/renderer/webgpu/WebGPUDevice.hppindex 237105eeb..41c8949ea 100644--- a/src/esengine/renderer/webgpu/WebGPUDevice.hpp+++ b/src/esengine/renderer/webgpu/WebGPUDevice.hpp@@ -319,9 +319,11 @@ private:// WGPU resource pointers, in binding order): a hit means the identical// bindings were used before (bind_group_dirty_ is set on every binding// change, so the key always reflects the live state). Entries own live WGPU- // objects, which internally ref their resources — a deleted resource lingers- // harmlessly until its entry is evicted; a resized buffer gets a new pointer,- // so its key changes and a fresh group is built.+ // objects, which internally ref their resources — but a DELETED resource's+ // entries must be evicted immediately (deleteBuffer/deleteTexture call+ // evictBindGroups): emscripten WGPU "pointers" are JS-table handle indices+ // that are reused right after release, so a new resource can inherit a dead+ // one's id and silently hit the dead one's cached group.struct BindGroupCacheEntry {u32 group;u32 mask;@@ -333,6 +335,8 @@ private:/// Reuse a cached group with these exact contents, else create + insert one.WGPUBindGroup cachedBindGroup(u32 group, u32 mask, const u64* ids, u32 idCount,const WGPUBindGroupDescriptor& desc);+ /// Drop every cached group referencing a dying resource id (see cache note).+ void evictBindGroups(u64 id);// GPU timing — the WebGPU analog of GLDevice's GL_TIME_ELAPSED timer, so the// profiler's gpuMs/gpuScopes populate on both backends. Active only when the