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: key WebGPU bind groups by their full layout
yhh03fe9e89229a
diff --git a/src/esengine/renderer/webgpu/WebGPUDevice.cpp b/src/esengine/renderer/webgpu/WebGPUDevice.cppindex 6faac9312..1ece100ce 100644--- a/src/esengine/renderer/webgpu/WebGPUDevice.cpp+++ b/src/esengine/renderer/webgpu/WebGPUDevice.cpp@@ -2127,10 +2127,10 @@ void WebGPUDevice::ensureDummies() {}}-WGPUBindGroup WebGPUDevice::cachedBindGroup(u32 group, u32 mask, const u64* ids, u32 idCount,+WGPUBindGroup WebGPUDevice::cachedBindGroup(const u64* ids, u32 idCount,const WGPUBindGroupDescriptor& desc) {for (auto& e : bind_group_cache_) {- if (e.group != group || e.mask != mask || e.ids.size() != idCount) continue;+ 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; }@@ -2145,7 +2145,7 @@ WGPUBindGroup WebGPUDevice::cachedBindGroup(u32 group, u32 mask, const u64* ids,bind_group_cache_.clear();}WGPUBindGroup bg = wgpuDeviceCreateBindGroup(device_, &desc);- if (bg) bind_group_cache_.push_back({group, mask, std::vector<u64>(ids, ids + idCount), bg});+ if (bg) bind_group_cache_.push_back({desc.layout, std::vector<u64>(ids, ids + idCount), bg});return bg;}@@ -2201,7 +2201,7 @@ void WebGPUDevice::flushBindGroup() {ids[i * 2] = static_cast<u64>(reinterpret_cast<uintptr_t>(entries[i].buffer));ids[i * 2 + 1] = (u64{1} << 63) | (entries[i].offset << 20) | entries[i].size;}- bind_group_ = cachedBindGroup(0, prog->group0Mask, ids, count * 2, bgd);+ bind_group_ = cachedBindGroup(ids, count * 2, bgd);if (bind_group_) wgpuRenderPassEncoderSetBindGroup(pass_, 0, bind_group_, 0, nullptr);}@@ -2268,7 +2268,7 @@ void WebGPUDevice::flushBindGroup() {: reinterpret_cast<uintptr_t>(texEntries[i].sampler);tids[i] = static_cast<u64>(p);}- texture_group_ = cachedBindGroup(1, prog->group1Mask, tids, texCount, tgd);+ texture_group_ = cachedBindGroup(tids, texCount, tgd);if (texture_group_) wgpuRenderPassEncoderSetBindGroup(pass_, 1, texture_group_, 0, nullptr);}bind_group_dirty_ = false;diff --git a/src/esengine/renderer/webgpu/WebGPUDevice.hpp b/src/esengine/renderer/webgpu/WebGPUDevice.hppindex 07292dbfa..39fb37037 100644--- a/src/esengine/renderer/webgpu/WebGPUDevice.hpp+++ b/src/esengine/renderer/webgpu/WebGPUDevice.hpp@@ -627,27 +627,18 @@ private:WGPUBindGroup bind_group_ = nullptr; ///< Currently bound group 0 (points INTO the cache; not owned).WGPUBindGroup texture_group_ = nullptr; ///< Currently bound group 1 (points INTO the cache; not owned).- // Bind-group cache. WebGPU bind groups are immutable, so rebuilding one per- // draw (release + create) is pure churn — a static scene rebuilds the same- // groups every frame. Cache by exact binding contents (group + mask + the- // 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 — 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.+ // Reuse immutable bind groups only when their layout and ordered bindings match.+ // Evict before resource release: emscripten reuses WGPU handle indices immediately.+ // Layouts belong to the device and outlive this cache.struct BindGroupCacheEntry {- u32 group;- u32 mask;+ WGPUBindGroupLayout layout;std::vector<u64> ids; ///< WGPU resource pointers (buffers, or view+sampler), binding order.WGPUBindGroup bg;};static constexpr u32 kBindGroupCacheCap = 256;std::vector<BindGroupCacheEntry> bind_group_cache_;/// Reuse a cached group with these exact contents, else create + insert one.- WGPUBindGroup cachedBindGroup(u32 group, u32 mask, const u64* ids, u32 idCount,+ WGPUBindGroup cachedBindGroup(const u64* ids, u32 idCount,const WGPUBindGroupDescriptor& desc);/// Drop every cached group referencing a dying resource id (see cache note).void evictBindGroups(u64 id);