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(webgpu): explicit bind-group layouts + material texture units land (WGSL phase 3 slice 1)

yhh634629c76764
diff --git a/src/esengine/renderer/webgpu/WGSLTwins.hpp b/src/esengine/renderer/webgpu/WGSLTwins.hpp
index 029312c8b..16f10a54a 100644
--- a/src/esengine/renderer/webgpu/WGSLTwins.hpp
+++ b/src/esengine/renderer/webgpu/WGSLTwins.hpp
@@ -13,8 +13,10 @@
* and by the WebGPU bring-up harnesses; Phase 3's dual-language
* emitter replaces the hand-written pairs.
*
- * Twin discipline: declare exactly the bindings you use — the WebGPU
- * device reflects bind-group contents by scanning these declarations.
+ * The device reflects bind-group contents by scanning these
+ * declarations and builds explicit layouts from them, so a declared-
+ * but-unused binding is legal (dummy-backfilled) — but twins still
+ * declare only what they use, keeping the scan output minimal.
*
* Plain string constants — safe in every build; no WebGPU headers.
*
diff --git a/src/esengine/renderer/webgpu/WebGPUDevice.cpp b/src/esengine/renderer/webgpu/WebGPUDevice.cpp
index 05f8a13eb..8d5d19163 100644
--- a/src/esengine/renderer/webgpu/WebGPUDevice.cpp
+++ b/src/esengine/renderer/webgpu/WebGPUDevice.cpp
@@ -89,6 +89,16 @@ void WebGPUDevice::shutdown() {
clear_pipelines_.clear();
if (clear_layout_) { wgpuPipelineLayoutRelease(clear_layout_); clear_layout_ = nullptr; }
if (clear_bgl_) { wgpuBindGroupLayoutRelease(clear_bgl_); clear_bgl_ = nullptr; }
+ for (auto& [key, layout] : pipeline_layouts_) {
+ if (layout) wgpuPipelineLayoutRelease(layout);
+ }
+ pipeline_layouts_.clear();
+ for (auto& [key, layout] : group_layouts_) {
+ if (layout) wgpuBindGroupLayoutRelease(layout);
+ }
+ group_layouts_.clear();
+ dummy_ubo_ = BufferHandle{}; // the WGPUBuffer/WGPUTexture went with the maps above
+ dummy_texture_ = 0;
framebuffers_.clear();
for (auto& [key, sampler] : samplers_) {
if (sampler) wgpuSamplerRelease(sampler);
@@ -443,7 +453,6 @@ void WebGPUDevice::bindTexture(u32 slot, TextureHandle texture) {
texture_slots_[slot] = id;
bind_group_dirty_ = true;
}
- if (id != 0) any_texture_bound_ = true;
}
bool WebGPUDevice::supportsCompressedFormat(GfxCompressedFormat format) {
@@ -606,6 +615,11 @@ WGPURenderPipeline WebGPUDevice::ensurePipeline(u32 id) {
fragment.targets = ⌖
WGPURenderPipelineDescriptor pd{};
+ // Explicit layout from the program's binding masks — the same cached
+ // objects flushBindGroup builds bind groups against, so declared-but-
+ // unbound bindings are legal (dummy backfill) and group compatibility
+ // holds by identity.
+ pd.layout = pipelineLayoutFor(progIt->second.group0Mask, progIt->second.group1Mask);
pd.vertex.module = progIt->second.vertex;
pd.vertex.entryPoint = sv("vs_main");
pd.vertex.bufferCount = slotCount;
@@ -822,6 +836,94 @@ void WebGPUDevice::drawInternalClear(bool color, bool depth, bool stencil,
bind_group_dirty_ = true;
}
+WGPUBindGroupLayout WebGPUDevice::groupLayoutFor(u32 group, u32 mask) {
+ const u64 key = (static_cast<u64>(group) << 32) | mask;
+ auto cached = group_layouts_.find(key);
+ if (cached != group_layouts_.end()) return cached->second;
+ if (!device_) return nullptr;
+
+ WGPUBindGroupLayoutEntry entries[kTextureSlots * 2];
+ u32 count = 0;
+ if (group == 0) {
+ // Engine UBO slots. minBindingSize stays 0: the shader's own struct
+ // size is validated per draw, and dummy backfill covers unbound slots.
+ for (u32 slot = 0; slot < kUniformSlots; ++slot) {
+ if ((mask & (1u << slot)) == 0) continue;
+ WGPUBindGroupLayoutEntry e{};
+ e.binding = slot;
+ e.visibility = WGPUShaderStage_Vertex | WGPUShaderStage_Fragment;
+ e.buffer.type = WGPUBufferBindingType_Uniform;
+ entries[count++] = e;
+ }
+ } else {
+ // Texture units through the unit→binding convention. Fragment-only
+ // visibility: the engine samples in fragment stages exclusively.
+ static_assert(kTextureSlots == kGroup1TextureUnits,
+ "device texture slots mirror the group-1 convention");
+ for (u32 unit = 0; unit < kTextureSlots; ++unit) {
+ const u32 tb = textureBindingForUnit(unit);
+ const u32 sb = samplerBindingForUnit(unit);
+ if (mask & (1u << tb)) {
+ WGPUBindGroupLayoutEntry e{};
+ e.binding = tb;
+ e.visibility = WGPUShaderStage_Fragment;
+ e.texture.sampleType = WGPUTextureSampleType_Float;
+ e.texture.viewDimension = WGPUTextureViewDimension_2D;
+ entries[count++] = e;
+ }
+ if (mask & (1u << sb)) {
+ WGPUBindGroupLayoutEntry e{};
+ e.binding = sb;
+ e.visibility = WGPUShaderStage_Fragment;
+ e.sampler.type = WGPUSamplerBindingType_Filtering;
+ entries[count++] = e;
+ }
+ }
+ }
+
+ WGPUBindGroupLayoutDescriptor bld{};
+ bld.entryCount = count;
+ bld.entries = entries;
+ WGPUBindGroupLayout layout = wgpuDeviceCreateBindGroupLayout(device_, &bld);
+ group_layouts_[key] = layout;
+ return layout;
+}
+
+WGPUPipelineLayout WebGPUDevice::pipelineLayoutFor(u32 group0Mask, u32 group1Mask) {
+ const u64 key = (static_cast<u64>(group1Mask) << 32) | group0Mask;
+ auto cached = pipeline_layouts_.find(key);
+ if (cached != pipeline_layouts_.end()) return cached->second;
+ if (!device_) return nullptr;
+
+ // Group indices are positional: a program with group-1 bindings always
+ // carries a group-0 layout too (zero entries when the mask is empty).
+ WGPUBindGroupLayout bgls[2];
+ u32 count = 0;
+ if (group0Mask != 0 || group1Mask != 0) bgls[count++] = groupLayoutFor(0, group0Mask);
+ if (group1Mask != 0) bgls[count++] = groupLayoutFor(1, group1Mask);
+
+ WGPUPipelineLayoutDescriptor pld{};
+ pld.bindGroupLayoutCount = count;
+ pld.bindGroupLayouts = bgls;
+ WGPUPipelineLayout layout = wgpuDeviceCreatePipelineLayout(device_, &pld);
+ pipeline_layouts_[key] = layout;
+ return layout;
+}
+
+void WebGPUDevice::ensureDummies() {
+ if (dummy_ubo_ == BufferHandle::Invalid || buffers_.find(static_cast<u32>(dummy_ubo_)) == buffers_.end()) {
+ // Sized past the largest engine block (LightConstants, 1184 bytes) so a
+ // declared-but-unbound block always satisfies draw-time size validation.
+ const std::vector<u8> zeros(2048, 0);
+ dummy_ubo_ = createBuffer({GfxBufferUsage::Uniform, 2048, false}, zeros.data());
+ }
+ if (dummy_texture_ == 0 || textures_.find(dummy_texture_) == textures_.end()) {
+ const u8 white[4] = {255, 255, 255, 255};
+ TextureDesc td{};
+ dummy_texture_ = static_cast<u32>(createTexture(td, white));
+ }
+}
+
void WebGPUDevice::flushBindGroup() {
if (!bind_group_dirty_ || !pass_ || !device_) return;
@@ -834,81 +936,82 @@ void WebGPUDevice::flushBindGroup() {
if (progIt != programs_.end()) prog = &progIt->second;
}
if (!prog) return;
-
- // Group 0 = the engine's UBO bindings (0..4). Entries must match the
- // pipeline's auto layout exactly — the layout contains only the bindings
- // the shader declares — so the bound slots filter through the program's
- // scanned mask. (The engine keeps Frame/Time/DrawParams armed on 0/3/4;
- // a shader that only declares FrameConstants gets a one-entry group.)
- WGPUBindGroupEntry entries[kUniformSlots];
- u32 count = 0;
- for (u32 slot = 0; slot < kUniformSlots; ++slot) {
- if (uniform_slots_[slot] == 0 || (prog->group0Mask & (1u << slot)) == 0) continue;
- auto it = buffers_.find(uniform_slots_[slot]);
- if (it == buffers_.end()) continue;
- WGPUBindGroupEntry e{};
- e.binding = slot;
- e.buffer = it->second.buffer;
- e.offset = 0;
- e.size = it->second.size;
- entries[count++] = e;
+ if (prog->group0Mask == 0 && prog->group1Mask == 0) {
+ bind_group_dirty_ = false;
+ return;
}
- if (bind_group_) { wgpuBindGroupRelease(bind_group_); bind_group_ = nullptr; }
- if (count > 0) {
+ ensureDummies();
+
+ // Group 0 = the engine's UBO bindings (0..4). Every mask bit gets an entry
+ // against the explicit layout: the armed slot's buffer (the engine keeps
+ // Frame/Time/DrawParams on 0/3/4, Material/Light arrive per draw/frame), or
+ // the zeroed dummy for a declared-but-unbound slot. A group-1-only program
+ // still sets a (zero-entry) group 0, keeping indices positional.
+ {
+ WGPUBindGroupEntry entries[kUniformSlots];
+ u32 count = 0;
+ for (u32 slot = 0; slot < kUniformSlots; ++slot) {
+ if ((prog->group0Mask & (1u << slot)) == 0) continue;
+ auto it = buffers_.find(uniform_slots_[slot]);
+ if (it == buffers_.end()) it = buffers_.find(static_cast<u32>(dummy_ubo_));
+ if (it == buffers_.end()) continue;
+ WGPUBindGroupEntry e{};
+ e.binding = slot;
+ e.buffer = it->second.buffer;
+ e.offset = 0;
+ e.size = it->second.size;
+ entries[count++] = e;
+ }
+
+ if (bind_group_) { wgpuBindGroupRelease(bind_group_); bind_group_ = nullptr; }
WGPUBindGroupDescriptor bgd{};
- bgd.layout = wgpuRenderPipelineGetBindGroupLayout(p, 0);
+ bgd.layout = groupLayoutFor(0, prog->group0Mask);
bgd.entryCount = count;
bgd.entries = entries;
bind_group_ = wgpuDeviceCreateBindGroup(device_, &bgd);
- if (bgd.layout) wgpuBindGroupLayoutRelease(bgd.layout);
if (bind_group_) wgpuRenderPassEncoderSetBindGroup(pass_, 0, bind_group_, 0, nullptr);
}
- // Group 1: the texture units. The WGSL twin convention is texture_2d at
- // bindings 0..7 paired with samplers at bindings 8..15 (u_textures[8]
- // de-combined: sampler i carries texture i's filter/wrap params via the
- // sampler cache); a single-texture shader declares just t0/s0. Entries
- // filter through the program's group-1 mask; missing units repeat slot 0's
- // view/sampler — the same trick DrawList::execute uses for WebGL2's
- // complete-texture rule. Only set when the pass bound textures AND the
- // program declares the group — a sampler-less shader (shape) drawn after
- // textured ones must keep its group-0-only layout.
- if (any_texture_bound_ && prog->group1Mask != 0) {
- const TextureRec* slot0 = nullptr;
- for (u32 slot = 0; slot < kTextureSlots && !slot0; ++slot) {
- auto it = textures_.find(texture_slots_[slot]);
- if (it != textures_.end()) slot0 = &it->second;
- }
- if (slot0) {
- WGPUBindGroupEntry texEntries[kTextureSlots * 2];
- u32 texCount = 0;
- for (u32 slot = 0; slot < kTextureSlots; ++slot) {
- auto it = textures_.find(texture_slots_[slot]);
- const TextureRec& rec = (it != textures_.end()) ? it->second : *slot0;
- if (prog->group1Mask & (1u << slot)) {
- WGPUBindGroupEntry e{};
- e.binding = slot;
- e.textureView = rec.view;
- texEntries[texCount++] = e;
- }
- if (prog->group1Mask & (1u << (kTextureSlots + slot))) {
- WGPUBindGroupEntry e{};
- e.binding = kTextureSlots + slot;
- e.sampler = samplerFor(rec.samplerKey);
- texEntries[texCount++] = e;
- }
+ // Group 1: the texture units through the unit→binding convention (engine
+ // units 0..7 at texture bindings 0..7 / samplers 8..15, material units
+ // 8..15 at 16..23 / 24..31; sampler i carries texture i's filter/wrap
+ // params via the sampler cache). Every declared binding gets an entry —
+ // the bound unit's view/sampler or the 1x1 white dummy — so the group
+ // always matches the explicit layout, textures bound or not.
+ if (prog->group1Mask != 0) {
+ auto dummyIt = textures_.find(dummy_texture_);
+ const TextureRec* dummy = (dummyIt != textures_.end()) ? &dummyIt->second : nullptr;
+
+ WGPUBindGroupEntry texEntries[kTextureSlots * 2];
+ u32 texCount = 0;
+ for (u32 unit = 0; unit < kTextureSlots; ++unit) {
+ auto it = textures_.find(texture_slots_[unit]);
+ const TextureRec* rec = (it != textures_.end()) ? &it->second : dummy;
+ if (!rec) continue;
+ const u32 tb = textureBindingForUnit(unit);
+ const u32 sb = samplerBindingForUnit(unit);
+ if (prog->group1Mask & (1u << tb)) {
+ WGPUBindGroupEntry e{};
+ e.binding = tb;
+ e.textureView = rec->view;
+ texEntries[texCount++] = e;
+ }
+ if (prog->group1Mask & (1u << sb)) {
+ WGPUBindGroupEntry e{};
+ e.binding = sb;
+ e.sampler = samplerFor(rec->samplerKey);
+ texEntries[texCount++] = e;
}
-
- if (texture_group_) { wgpuBindGroupRelease(texture_group_); texture_group_ = nullptr; }
- WGPUBindGroupDescriptor tgd{};
- tgd.layout = wgpuRenderPipelineGetBindGroupLayout(p, 1);
- tgd.entryCount = texCount;
- tgd.entries = texEntries;
- texture_group_ = wgpuDeviceCreateBindGroup(device_, &tgd);
- if (tgd.layout) wgpuBindGroupLayoutRelease(tgd.layout);
- if (texture_group_) wgpuRenderPassEncoderSetBindGroup(pass_, 1, texture_group_, 0, nullptr);
}
+
+ if (texture_group_) { wgpuBindGroupRelease(texture_group_); texture_group_ = nullptr; }
+ WGPUBindGroupDescriptor tgd{};
+ tgd.layout = groupLayoutFor(1, prog->group1Mask);
+ tgd.entryCount = texCount;
+ tgd.entries = texEntries;
+ texture_group_ = wgpuDeviceCreateBindGroup(device_, &tgd);
+ if (texture_group_) wgpuRenderPassEncoderSetBindGroup(pass_, 1, texture_group_, 0, nullptr);
}
bind_group_dirty_ = false;
}
@@ -1054,10 +1157,6 @@ void WebGPUDevice::beginRenderPass(const RenderPassDesc& desc) {
desc.clearStencil && hasStencilPlanes(pass_ds_format_),
desc.clearColorValue, desc.clearStencilValue, &desc);
}
- // Each pass starts textureless: group 1 only attaches to pipelines whose
- // draws bind textures (the engine rebinds per draw), never to sampler-less
- // pipelines from a previous pass's leftovers.
- any_texture_bound_ = false;
}
void WebGPUDevice::endRenderPass() {
diff --git a/src/esengine/renderer/webgpu/WebGPUDevice.hpp b/src/esengine/renderer/webgpu/WebGPUDevice.hpp
index 7d5dd3575..e62de395b 100644
--- a/src/esengine/renderer/webgpu/WebGPUDevice.hpp
+++ b/src/esengine/renderer/webgpu/WebGPUDevice.hpp
@@ -9,9 +9,13 @@
* depth-stencil; endRenderPass submits), lazy WGPURenderPipeline builds
* from the retained PipelineDesc + layout + shader modules — one variant
* per pass depth-stencil shape, since WebGPU validates that coupling —
- * bind groups (group 0 = UBO slots, group 1 = texture/sampler pairs with
- * a per-params sampler cache), indexed/instanced draw recording, and an
- * internal clear-triangle family that emulates region-scoped clears and
+ * EXPLICIT bind-group layouts built from each program's binding masks
+ * (group 0 = UBO slots, group 1 = texture/sampler pairs per the
+ * WebGPUMappings unit→binding convention, with dummy backfill for
+ * declared-but-unbound bindings — GL's tolerance for unused
+ * declarations, which the dual-language emitter's uniform injection
+ * relies on), indexed/instanced draw recording, and an internal
+ * clear-triangle family that emulates region-scoped clears and
* mid-pass stencil resets (the two clears WebGPU load-ops cannot spell).
* The class stays null-device safe: constructed without a WGPUDevice it
* degrades every entry point to a logged no-op, so handle bookkeeping
@@ -168,11 +172,11 @@ private:
WGPUShaderModule vertex = nullptr;
WGPUShaderModule fragment = nullptr;
/** @brief `@group(0/1) @binding(i)` masks scanned from the WGSL (both
- * stages). Bind groups must match a pipeline's auto layout
- * exactly, so the device filters its bound state through these:
- * group 0 = which UBO slots the program declares, group 1 =
- * which texture/sampler bindings. Zero group-1 mask means the
- * program must never have the texture group set at all. */
+ * stages). They drive the program's EXPLICIT bind-group and
+ * pipeline layouts: group 0 = which UBO slots the program
+ * declares, group 1 = which texture/sampler bindings. Declared
+ * bindings with no bound resource are backfilled with dummies,
+ * so an unused declaration is as legal as in GLSL. */
u32 group0Mask = 0;
u32 group1Mask = 0;
};
@@ -191,13 +195,27 @@ private:
/** @brief Builds (once) and returns the WGPURenderPipeline for a handle,
* in the variant matching the current pass's depth-stencil shape. */
WGPURenderPipeline ensurePipeline(u32 id);
- /** @brief (Re)creates the bind groups: group 0 = UBO slots, group 1 = the
- * texture units (8 texture_2d at bindings 0..7 + their 8 samplers at
- * bindings 8..15 — the WGSL twin convention for u_textures[8]; GL's
- * combined texture+sampler state arrives de-combined, sampler i
- * carrying texture i's filter/wrap params). Group 1 is set only when
- * the pass bound any texture, so shaders without samplers (shape)
- * never see a mismatched group. */
+ /** @brief Returns the cached explicit bind-group layout for a binding mask.
+ * Group 0 entries are uniform buffers at their slot; group 1 entries
+ * are texture_2d/sampler pairs per the WebGPUMappings unit→binding
+ * convention (engine units 0..7 at 0..7/8..15, material units 8..15
+ * at 16..23/24..31). */
+ WGPUBindGroupLayout groupLayoutFor(u32 group, u32 mask);
+ /** @brief Returns the cached explicit pipeline layout for a program's masks.
+ * A program with group-1 bindings but an empty group 0 still gets a
+ * (zero-entry) group-0 layout, so group indices stay positional. */
+ WGPUPipelineLayout pipelineLayoutFor(u32 group0Mask, u32 group1Mask);
+ /** @brief Lazily creates the dummy backfill resources: a zeroed uniform
+ * buffer and a 1x1 white texture, standing in for declared-but-
+ * unbound bindings (GL reads an unbound block/unit without
+ * validation errors; here it reads zeros/white). */
+ void ensureDummies();
+ /** @brief (Re)creates the bind groups against the program's explicit
+ * layouts: group 0 = UBO slots, group 1 = texture units mapped
+ * through the unit→binding convention, sampler i carrying texture
+ * i's filter/wrap params (GL's combined texture+sampler state
+ * de-combined). Every declared binding gets an entry — bound
+ * resource or dummy — so the groups always match the layouts. */
void flushBindGroup();
/** @brief Returns the cached sampler for packed filter/wrap params. */
WGPUSampler samplerFor(u8 key);
@@ -252,14 +270,24 @@ private:
i32 stencil_ref_ = 0; ///< Last user-set reference (re-applied after internal quads).
static constexpr u32 kUniformSlots = 8;
u32 uniform_slots_[kUniformSlots] = {}; ///< BufferHandle id per UBO binding slot.
- static constexpr u32 kTextureSlots = 8;
+ /// Engine units 0..7 (batch multi-texture) + material-param units 8..15
+ /// (== webgpu::kGroup1TextureUnits, static_asserted in the .cpp).
+ static constexpr u32 kTextureSlots = 16;
u32 texture_slots_[kTextureSlots] = {}; ///< TextureHandle id per sampler unit.
- bool any_texture_bound_ = false;
bool bind_group_dirty_ = true;
WGPUBindGroup bind_group_ = nullptr;
WGPUBindGroup texture_group_ = nullptr;
std::unordered_map<u8, WGPUSampler> samplers_; ///< Keyed by packed filter/wrap params.
+ // Explicit layouts, cached by binding mask (key = group << 32 | mask for
+ // bind-group layouts, group1Mask << 32 | group0Mask for pipeline layouts).
+ // Pipelines and bind groups share the cached objects, so group
+ // compatibility holds by identity.
+ std::unordered_map<u64, WGPUBindGroupLayout> group_layouts_;
+ std::unordered_map<u64, WGPUPipelineLayout> pipeline_layouts_;
+ BufferHandle dummy_ubo_{}; ///< Zeroed backfill for declared-but-unbound UBO slots.
+ u32 dummy_texture_ = 0; ///< 1x1 white backfill for declared-but-unbound units.
+
u32 next_id_ = 1;
std::unordered_map<u32, BufferRec> buffers_;
std::unordered_map<u32, TextureRec> textures_;
diff --git a/src/esengine/renderer/webgpu/WebGPUMappings.hpp b/src/esengine/renderer/webgpu/WebGPUMappings.hpp
index 0f0cfb508..fc225ee9d 100644
--- a/src/esengine/renderer/webgpu/WebGPUMappings.hpp
+++ b/src/esengine/renderer/webgpu/WebGPUMappings.hpp
@@ -314,20 +314,42 @@ inline WGPUCullMode toWGPUCullMode(bool enabled, bool front) {
return front ? WGPUCullMode_Front : WGPUCullMode_Back;
}
+// =============================================================================
+// Group-1 binding convention (texture units → bindings)
+// =============================================================================
+
+/**
+ * @brief Texture units the group-1 convention spans: engine units 0..7 (the
+ * batch's u_textures[8]) plus material-param units 8..15 (the GL
+ * MATERIAL_TEXTURE_UNIT_BASE range).
+ */
+inline constexpr u32 kGroup1TextureUnits = 16;
+
+/**
+ * @brief Group-1 binding of a texture unit's texture_2d.
+ * @details Engine units 0..7 sit at bindings 0..7 with their samplers at 8..15
+ * (GL's combined texture+sampler state de-combined); material units
+ * 8..15 extend the group at bindings 16..23 with samplers at 24..31.
+ * The two maps are disjoint and together cover bits 0..31 exactly, so
+ * one u32 mask describes a whole group.
+ */
+inline constexpr u32 textureBindingForUnit(u32 unit) { return unit < 8 ? unit : unit + 8; }
+
+/** @brief Group-1 binding of a texture unit's sampler (see textureBindingForUnit). */
+inline constexpr u32 samplerBindingForUnit(u32 unit) { return unit < 8 ? unit + 8 : unit + 16; }
+
// =============================================================================
// WGSL binding reflection (source scan)
// =============================================================================
/**
* @brief Bit mask of the `@group(N) @binding(i)` indices a WGSL source declares.
- * @details Bind-group entries must match a pipeline's auto layout EXACTLY, and
- * auto layouts contain only the bindings the shader uses — so the
- * device filters its bound state (UBO slots, texture units) through
- * this mask per program. A source scan stands in for real reflection
- * until the Phase 3 emitter carries binding metadata; the twins'
- * discipline is declare-exactly-what-you-use, which makes the two
- * equivalent. Bindings ≥ 32 are ignored (the engine's conventions top
- * out at 16).
+ * @details Declarations drive the device's EXPLICIT bind-group layouts: every
+ * declared binding gets a layout entry and a bind-group entry (bound
+ * resource or dummy backfill), so declared-but-unused bindings are as
+ * legal as they are in GLSL. A source scan stands in for real
+ * reflection until the Phase 3 emitter carries binding metadata.
+ * Bindings ≥ 32 are ignored (the group-1 convention tops out at 31).
*/
inline u32 scanWGSLBindingMask(const char* source, u32 group) {
if (!source) return 0;
diff --git a/tests/renderer/test_webgpu_device.cpp b/tests/renderer/test_webgpu_device.cpp
index efbca7ce5..e0ac3e326 100644
--- a/tests/renderer/test_webgpu_device.cpp
+++ b/tests/renderer/test_webgpu_device.cpp
@@ -102,7 +102,29 @@ TEST_CASE("pipeline depth-stencil state carries GL semantics exactly") {
CHECK(!hasStencilPlanes(WGPUTextureFormat_Undefined));
}
-TEST_CASE("binding-mask scan mirrors what a WGSL auto layout will contain") {
+TEST_CASE("group-1 unit→binding convention: disjoint maps covering one u32 mask") {
+ // Engine units 0..7: textures at 0..7, samplers at 8..15.
+ CHECK(textureBindingForUnit(0) == 0u);
+ CHECK(textureBindingForUnit(7) == 7u);
+ CHECK(samplerBindingForUnit(0) == 8u);
+ CHECK(samplerBindingForUnit(7) == 15u);
+ // Material units 8..15 (MATERIAL_TEXTURE_UNIT_BASE range): textures at
+ // 16..23, samplers at 24..31.
+ CHECK(textureBindingForUnit(8) == 16u);
+ CHECK(textureBindingForUnit(15) == 23u);
+ CHECK(samplerBindingForUnit(8) == 24u);
+ CHECK(samplerBindingForUnit(15) == 31u);
+
+ // Bijection: the 16 units' texture+sampler bindings tile bits 0..31 exactly.
+ u32 mask = 0;
+ for (u32 unit = 0; unit < kGroup1TextureUnits; ++unit) {
+ mask |= (1u << textureBindingForUnit(unit));
+ mask |= (1u << samplerBindingForUnit(unit));
+ }
+ CHECK(mask == 0xFFFFFFFFu);
+}
+
+TEST_CASE("binding-mask scan mirrors the declarations an explicit layout carries") {
// A batch-like fragment: the full group-1 texture/sampler set.
const char* batchish = R"(
@group(1) @binding(0) var t0 : texture_2d<f32>;
@@ -122,6 +144,14 @@ TEST_CASE("binding-mask scan mirrors what a WGSL auto layout will contain") {
CHECK(scanWGSLBindingMask(ubos, 0) == ((1u << 0) | (1u << 3) | (1u << 4)));
CHECK(scanWGSLBindingMask(ubos, 1) == 0u);
+ // Material texture params ride the extended group-1 range (units 8..15 →
+ // texture bindings 16..23, samplers 24..31).
+ const char* material = R"(
+@group(1) @binding(16) var u_mask : texture_2d<f32>;
+@group(1) @binding(24) var u_mask_s : sampler;
+)";
+ CHECK(scanWGSLBindingMask(material, 1) == ((1u << 16) | (1u << 24)));
+
CHECK(scanWGSLBindingMask(nullptr, 0) == 0u);
CHECK(scanWGSLBindingMask("no bindings here", 0) == 0u);
}