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(materials): a mesh shader's eighth texture finds a unit - reading its base texture as u_textures[0] was counted as reading all eight batch units, so Ground Blend lost its wet map and did not compile on WebGPU, and voxel ground had given its own up; a literal index is that unit alone
yhh82c0c5066c28
diff --git a/sdk/src/voxel/voxelShader.ts b/sdk/src/voxel/voxelShader.tsindex 69c020a3f..4e6c796bd 100644--- a/sdk/src/voxel/voxelShader.ts+++ b/sdk/src/voxel/voxelShader.ts@@ -85,12 +85,6 @@ export function deriveVoxelGround(model: string): string {s = swap(s, '#pragma param u_iridescence float', `${PARAMS}#pragma param u_iridescence float`);s = swap(s, '#pragma param u_emissiveMap texture default(white)\n', '');s = swap(s, 'texture(u_emissiveMap, uv).rgb', 'vec3(1.0)');- // The wet-ground map: Voxel Ground already reads seven textures, and a shader has only so many units for them. A texture past the last- // unit gets none, its declaration is dropped, and the whole shader stops compiling (WebGPU said `unresolved value u_wetMask`). Ground- // made of voxels takes no wet map, so what it would have read is dry.- s = swap(s, '#pragma param u_wetMask texture default(black) texel(data)\n', '');- s = swap(s, 'textureLod(u_wetMask, wuv, 0.0).rg', 'vec2(0.0)');- s = swap(s, 'textureSampleLevel(u_wetMask, u_wetMask_s, wuv, 0.0).rg', 'vec2f(0.0)');s = swap(s, 'textureSampleGrad(u_emissiveMap, u_emissiveMap_s, uv, duvx, duvy).rgb', 'vec3f(1.0)');s = swap(s, 'float specularAA(highp vec3', `${GLSL_HELPERS}float specularAA(highp vec3`);s = swap(s, 'fn specularAA(dndx : vec3f', `${WGSL_HELPERS}fn specularAA(dndx : vec3f`);diff --git a/src/esengine/resource/ShaderParser.cpp b/src/esengine/resource/ShaderParser.cppindex 2516d7008..aca795197 100644--- a/src/esengine/resource/ShaderParser.cpp+++ b/src/esengine/resource/ShaderParser.cpp@@ -4301,6 +4301,37 @@ bool namesIdentifier(const std::string& text, std::string_view word) {return false;}+/**+ * The batch units @p text reads through `u_textures`, as a mask: the one a literal index names (`u_textures[0]`), every one of+ * 0..7 where the index is worked out (a merged run of sprites) or the array is handed on whole.+ */+u32 batchUnitsRead(const std::string& text) {+ constexpr std::string_view word = "u_textures";+ const auto isIdent = [](char c) { return std::isalnum(static_cast<unsigned char>(c)) || c == '_'; };+ u32 mask = 0;+ for (usize at = text.find(word); at != std::string::npos; at = text.find(word, at + 1)) {+ usize i = at + word.size();+ if ((at > 0 && isIdent(text[at - 1])) || (i < text.size() && isIdent(text[i]))) continue;+ // A type before the name is its declaration, whose size is no read.+ usize before = at;+ while (before > 0 && std::isspace(static_cast<unsigned char>(text[before - 1]))) --before;+ if (before > 0 && before < at && isIdent(text[before - 1])) continue;+ while (i < text.size() && std::isspace(static_cast<unsigned char>(text[i]))) ++i;+ if (i >= text.size() || text[i] != '[') return 0xFFu;+ const usize close = text.find(']', i);+ if (close == std::string::npos) return 0xFFu;+ u32 index = 0;+ bool literal = close > i + 1;+ for (usize c = i + 1; c < close && literal; ++c) {+ if (std::isdigit(static_cast<unsigned char>(text[c]))) index = std::min(index * 10u + static_cast<u32>(text[c] - '0'), 255u);+ else if (!std::isspace(static_cast<unsigned char>(text[c]))) literal = false;+ }+ if (!literal) return 0xFFu;+ if (index < MATERIAL_TEXTURE_UNIT_BASE) mask |= 1u << index;+ }+ return mask;+}+} // namespace/**@@ -4308,8 +4339,8 @@ bool namesIdentifier(const std::string& text, std::string_view word) {* @details The engine binds those units per draw, so one is free only where this shader never reads what the engine puts* there. Two questions decide it. What the domain's injected code reads: a Lit stage takes the shadow map, the* reflection atlas, the bake and both 2D masks, and every mesh variant takes the morph deltas. And what the- * shader's own text names, in either language, all variants at once: the batch textures (u_textures, every one- * of 0..7, since a merged run puts its sprites there), an engine sampler by name, or a `tN`/`sN` unit. A+ * shader's own text names, in either language, all variants at once: the batch textures (u_textures: the one a+ * literal index names, every one where the index is worked out), an engine sampler by name, or a `tN`/`sN` unit. A* fullscreen pass and the UI reach their low units through their own conventions, so they keep none free.*/u32 ShaderParser::materialOverflowUnits(const ParsedShader& shader) {@@ -4330,7 +4361,7 @@ u32 ShaderParser::materialOverflowUnits(const ParsedShader& shader) {for (const auto& [stage, source] : shader.wgslStages) wgsl += source;const std::string both = glsl + wgsl;- if (namesIdentifier(both, "u_textures")) reads |= 0xFFu;+ reads |= batchUnitsRead(both);static constexpr struct { const char* name; u32 unit; } kEngine[] = {{SHADOW_MAP_SAMPLER, SHADOW_MAP_TEXTURE_UNIT}, {ENV_MAP_SAMPLER, ENV_MAP_TEXTURE_UNIT},{MORPH_DELTA_SAMPLER, MORPH_DELTA_TEXTURE_UNIT}, {LIGHTMAP_SAMPLER, LIGHTMAP_TEXTURE_UNIT},diff --git a/tests/renderer/test_shader_variant.cpp b/tests/renderer/test_shader_variant.cppindex e998cc283..f0a8a981b 100644--- a/tests/renderer/test_shader_variant.cpp+++ b/tests/renderer/test_shader_variant.cpp@@ -304,8 +304,14 @@ static void testMaterialOverflowUnits() {CHECK(unitOf(litFull, "u_m9") == -1, "a Lit shader's tenth texture finds no unit");// What the shader's own text reads is not free, in either language.- ParsedShader readsBatch = ShaderParser::parse(manyTextures("Unlit", 8, " c += texture(u_textures[0], v_texCoord);\n"));- CHECK(unitOf(readsBatch, "u_m7") == -1, "a shader that reads the batch textures keeps all of 0..7");+ // The batch textures: one read by a literal index is that unit alone (a mesh's base texture), and an index worked out is+ // every one of 0..7 (a merged run of sprites).+ ParsedShader readsBase = ShaderParser::parse(manyTextures("Unlit", 9, " c += texture(u_textures[0], v_texCoord);\n"));+ CHECK(unitOf(readsBase, "u_m7") == 7 && unitOf(readsBase, "u_m8") == 6, "a shader that reads u_textures[0] keeps unit 0 only");+ ParsedShader litBase = ShaderParser::parse(manyTextures("Lit", 9, " c += texture(u_textures[0], v_texCoord);\n"));+ CHECK(unitOf(litBase, "u_m7") == 1 && unitOf(litBase, "u_m8") == -1, "a Lit shader that reads its base texture has unit 1 left");+ ParsedShader readsRun = ShaderParser::parse(manyTextures("Unlit", 8, " c += texture(u_textures[int(v_texCoord.x)], v_texCoord);\n"));+ CHECK(unitOf(readsRun, "u_m7") == -1, "a shader that indexes the batch textures keeps all of 0..7");ParsedShader readsT7 = ShaderParser::parse(manyTextures("Unlit", 8, "", " c += textureSample(t7, s7, v.v_texCoord);\n"));CHECK(unitOf(readsT7, "u_m7") == 6, "a WGSL twin that reads t7 leaves unit 7 alone");ParsedShader readsShape = ShaderParser::parse(manyTextures("Unlit", 8, " c += texture(u_lightShape2D, v_texCoord);\n"));