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(terrain): a ground with no layer files is painted again - its paint is the eighth texture of its shader and found no unit, because three colours the shader keeps in s0..s3 were read as four engine units in use; a name a WGSL source declares for itself is no longer a unit, and a criterion draws a painted ground
yhh265b7b179a24
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 59f673fd9..432df72c3 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -16,6 +16,7 @@ published separately; it ships inside the editor.### Fixed+- **A ground with no layer files is painted again.** In 0.104.0 a `Terrain` drawn by the stock shader (four layer colours or textures and a splatmap, no `.esterrainlayer` files) was one colour from edge to edge on every platform, and logged `shader Terrain: texture param "u_paintPages" finds no free texture unit`. The baked occlusion map made the paint that shader's eighth texture, one past the seven units a material has; the eighth takes an engine unit the shader does not read, and the shader was counted as reading four of them because it keeps three colours in variables named `s0` to `s3`, which is how WGSL names a sampler unit. A name a source declares for itself (`let s2`, a `var` in a function, a parameter) is not a unit now. A criterion, `terrain-paint`, draws a ground painted in two colours on both backends: nothing in the pull-request tier drew a terrain before.- **A loop is seamless in a packaged game.** A `.wav` is re-encoded to MP3 when a game is exported, and a decoder gives an MP3 back with 1105 samples of silence before its sound and several hundred after it: measured in a browser, a one-second loop that was seamless as a `.wav` had 21 ms of silence every time round once packaged, and every sound, looping or not, began a fortieth of a second late. No tag in the file tells a player to take the silence off, and none does. The exporter now records, for each clip it re-encodes, how long it really is and what leads it, and the runtime plays the clip from past the lead to its real end and loops it between the two: exact to the sample where the clip is decoded (the web's decoded clips and the native host), and started past the lead where it is played from its file (a streamed clip on the web, a mini-game). A decoder that takes the silence off itself is seen by the length of what it returns and is not asked to twice. A project played from the editor reads its own `.wav` files and was never affected.- **A large ground no longer throws every frame once its pages run out.** With every page slot in use (a `detail` that wants more than the 900 there are, or a long flight), `TerrainSystem` threw `Cannot read properties of undefined (reading 'slot')` each frame: the ground stopped following the eye, and the log filled. The ground chose what to draw, made new pages, then placed its nodes, and a page was marked as in use only when placed; a page made in between took the slot of the least recently drawn one, which was a page just chosen. What is drawn is now marked before anything is made, and when every slot is drawn that frame the new page waits.diff --git a/fixtures/scenes/terrain-paint.esscene b/fixtures/scenes/terrain-paint.esscenenew file mode 100644index 000000000..8fffa1190--- /dev/null+++ b/fixtures/scenes/terrain-paint.esscene@@ -0,0 +1,118 @@+{+ "version": 4,+ "name": "A ground painted by its splatmap: two layers, two colours",+ "entities": [+ {+ "id": 0,+ "name": "Camera",+ "parent": null,+ "children": [],+ "visible": true,+ "components": [+ {+ "type": "Transform",+ "data": {+ "position": {+ "x": 0,+ "y": 40,+ "z": 0+ },+ "rotation": {+ "x": -0.7071068,+ "y": 0,+ "z": 0,+ "w": 0.7071068+ }+ }+ },+ {+ "type": "Camera",+ "data": {+ "projectionType": 1,+ "orthoSize": 12,+ "nearPlane": 0.1,+ "farPlane": 200,+ "isActive": true,+ "clearFlags": 3+ }+ }+ ]+ },+ {+ "id": 1,+ "name": "Sky",+ "parent": null,+ "children": [],+ "visible": true,+ "components": [+ {+ "type": "Transform",+ "data": {}+ },+ {+ "type": "Light",+ "data": {+ "type": 2,+ "color": {+ "r": 1,+ "g": 1,+ "b": 1,+ "a": 1+ },+ "intensity": 1,+ "enabled": true+ }+ }+ ]+ },+ {+ "id": 2,+ "name": "Ground",+ "parent": null,+ "children": [],+ "visible": true,+ "components": [+ {+ "type": "Transform",+ "data": {}+ },+ {+ "type": "Terrain",+ "data": {+ "sizeX": 32,+ "sizeZ": 32,+ "height": 1,+ "cellSize": 1,+ "detail": 1,+ "heightmap": "/scenes/terrain-paint/height.png",+ "splatmap": "/scenes/terrain-paint/splat.png",+ "layerColor0": {+ "r": 1,+ "g": 0,+ "b": 0,+ "a": 1+ },+ "layerColor1": {+ "r": 0,+ "g": 0,+ "b": 1,+ "a": 1+ },+ "layerColor2": {+ "r": 0,+ "g": 1,+ "b": 0,+ "a": 1+ },+ "layerColor3": {+ "r": 1,+ "g": 1,+ "b": 1,+ "a": 1+ }+ }+ }+ ]+ }+ ]+}\ No newline at end of filediff --git a/fixtures/scenes/terrain-paint/height.png b/fixtures/scenes/terrain-paint/height.pngnew file mode 100644index 000000000..4d9523520Binary files /dev/null and b/fixtures/scenes/terrain-paint/height.png differdiff --git a/fixtures/scenes/terrain-paint/splat.png b/fixtures/scenes/terrain-paint/splat.pngnew file mode 100644index 000000000..5f0641b91Binary files /dev/null and b/fixtures/scenes/terrain-paint/splat.png differdiff --git a/src/esengine/renderer/rhi/WgslBindings.hpp b/src/esengine/renderer/rhi/WgslBindings.hppindex 8f3b265b4..6dafefc5c 100644--- a/src/esengine/renderer/rhi/WgslBindings.hpp+++ b/src/esengine/renderer/rhi/WgslBindings.hpp@@ -68,6 +68,12 @@ inline u32 scanWGSLBindingMask(const char* source, u32 group) {* stage names, as against what it declares. A name reached and not declared is* an unresolved identifier, which WebGPU reports as an invalid PIPELINE with no* shader named anywhere in it — so the assembler completes the set instead.+ *+ * A name the source makes its own is not a unit: `let s2`, `var t0` in a+ * function, a parameter or a field `s1 : f32`. Three colours kept in s0..s3+ * once counted as four units read, and a material's eighth texture was told+ * there was no unit left for it. A `var` after an attribute's `)` is a+ * binding's own declaration, which does name the unit.*/inline void scanWGSLReachedUnits(const char* source, u32& textures, u32& samplers) {textures = 0;@@ -76,6 +82,25 @@ inline void scanWGSLReachedUnits(const char* source, u32& textures, u32& samplerconst auto isIdent = [](char c) {return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_';};+ const auto isSpace = [](char c) { return c == ' ' || c == '\t' || c == '\n' || c == '\r'; };+ // Whether the name starting at @p p (ending before @p end) is one the source declares for itself.+ const auto ownName = [&](const char* p, const char* end) -> bool {+ const char* b = p;+ while (b != source && isSpace(b[-1])) --b;+ const char* after = end;+ while (isSpace(*after)) ++after;+ if (b != source && (b[-1] == '(' || b[-1] == ',' || b[-1] == '{') && *after == ':') return true;+ const char* w = b;+ while (w != source && isIdent(w[-1])) --w;+ const usize len = static_cast<usize>(b - w);+ if (len == 3 && std::strncmp(w, "let", 3) == 0) return true;+ if (len == 5 && std::strncmp(w, "const", 5) == 0) return true;+ if (len != 3 || std::strncmp(w, "var", 3) != 0) return false;+ while (w != source && isSpace(w[-1])) --w;+ return w == source || w[-1] != ')';+ };+ u32 ownTextures = 0;+ u32 ownSamplers = 0;for (const char* p = source; *p; ++p) {if (*p != 't' && *p != 's') continue;if (p != source && isIdent(p[-1])) continue;@@ -84,9 +109,12 @@ inline void scanWGSLReachedUnits(const char* source, u32& textures, u32& samplerif (end == p + 1 || isIdent(*end)) continue;if (unit < WGSL_MAX_TEXTURE_UNITS) {(*p == 't' ? textures : samplers) |= (1u << unit);+ if (ownName(p, end)) (*p == 't' ? ownTextures : ownSamplers) |= (1u << unit);}p = end - 1;}+ textures &= ~ownTextures;+ samplers &= ~ownSamplers;}} // namespace esenginediff --git a/tests/renderer/test_shader_variant.cpp b/tests/renderer/test_shader_variant.cppindex 73e58d261..1b8d8738c 100644--- a/tests/renderer/test_shader_variant.cpp+++ b/tests/renderer/test_shader_variant.cpp@@ -300,6 +300,11 @@ static void testMaterialOverflowUnits() {// Lit reads the shadow map, reflections, morph, bake and both 2D masks (2..7): only 1 and 0 are left.ParsedShader lit = ShaderParser::parse(manyTextures("Lit", 9));CHECK(lit.valid && unitOf(lit, "u_m7") == 1 && unitOf(lit, "u_m8") == 0, "a Lit shader overflows into units 1 and 0");+ // Names the shader makes its own are not units: the stock Terrain kept three colours in s0..s3, and its eighth texture found none.+ ParsedShader litLocals = ShaderParser::parse(manyTextures("Lit", 9, "", " var s0 = vec3f(0.0);\n let s1 = s0;\n let t0 : f32 = s1.x;\n c += vec4f(t0);\n"));+ CHECK(litLocals.valid && unitOf(litLocals, "u_m7") == 1 && unitOf(litLocals, "u_m8") == 0, "locals named s0, s1 and t0 take no unit from a material");+ ParsedShader litReads = ShaderParser::parse(manyTextures("Lit", 9, "", " c += textureSample(t1, s1, v.v_texCoord);\n"));+ CHECK(unitOf(litReads, "u_m7") == 0 && unitOf(litReads, "u_m8") == -1, "a shader that samples t1 keeps unit 1 and has one left");ParsedShader litFull = ShaderParser::parse(manyTextures("Lit", 10));CHECK(unitOf(litFull, "u_m9") == -1, "a Lit shader's tenth texture finds no unit");diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjsindex b1f4c0863..32004c717 100644--- a/tools/releaseNotes.mjs+++ b/tools/releaseNotes.mjs@@ -4710,6 +4710,8 @@ export const NOTED = {{"internal": "the manual's own build and a gate over its sources; no page a reader sees changed but the five links that now resolve"},"fix(terrain): a ground with more pages wanted than it holds no longer throws every frame - the pages it had chosen to draw were marked in use only after new pages were made, so a new page took the slot of one about to be drawn and its node was placed on nothing":{"note": "A large ground no longer throws every frame once its pages run out."},+ "fix(terrain): a ground with no layer files is painted again - its paint is the eighth texture of its shader and found no unit, because three colours the shader keeps in s0..s3 were read as four engine units in use; a name a WGSL source declares for itself is no longer a unit, and a criterion draws a painted ground":+ {"note": "A ground with no layer files is painted again."},"chore(gates): what the checks asked of this week's work - a disposition for esengine/image in a native script and its namespace there, Waterfall's width and its place in the corpus, the recipe worker's bundle as not a contract, shadeLight2D's inout against its return, and a test that named App as a type":{"internal": "tables the gates read, brought level; a natively packaged script can now import esengine/image, which no native game has run yet"},"feat(image): a picture's source goes where a texture goes, and its maps are recipes too - a texture field takes an .esimage, the image tools write a picture's normal and surface maps as recipes beside it and answer their refs, text is set by the drawer a game sets it with, and the viewport draws a recipe as a game does":diff --git a/tools/renderScenes.mjs b/tools/renderScenes.mjsindex e0a60b7a8..df56fb452 100644--- a/tools/renderScenes.mjs+++ b/tools/renderScenes.mjs@@ -80,6 +80,19 @@ export const SCENES = []),},})),+ // A ground with no layer files reads its paint from the last of its eight textures, the one past the material's seven units:+ // 0.104.0 shipped with that texture given no unit, and every such ground one colour from edge to edge.+ {+ id: 'terrain-paint', tier: 'pr', webgpu: true,+ env: {+ ESTELLA_VERIFY_SCENE: '/scenes/terrain-paint.esscene',+ ESTELLA_VERIFY_W: '256', ESTELLA_VERIFY_H: '256', ESTELLA_VERIFY_STEPS: '30', ESTELLA_VERIFY_SETTLE_MS: '2500',+ ESTELLA_VERIFY_EXPECT: JSON.stringify([+ { x: 0.3, y: 0.5, rgb: [255, 0, 0], tol: 90 },+ { x: 0.7, y: 0.5, rgb: [0, 0, 255], tol: 90 },+ ]),+ },+ },{id: 'shadow-vertex-displacement', tier: 'pr', webgpu: true,env: {