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
feat(render): the per-object record leaves the vertex attributes
esengine93a3fec4f039
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 91d761966..500cf75ab 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -94,10 +94,31 @@ published separately; it ships inside the editor.longer than the cap carries no irradiance by construction, so its tail readsthe zeroes and falls back to the environment, which is its answer anyway.- Per-instance, not per-object-record, because the record is out of room: the 16- vertex attribute slots are a fixed vocabulary — 0-7 the mesh's own channels- (serialised in `.esmesh`), 8-15 the per-object transform — with nothing spare.- Moving that record to a texture is what would free them, and is its own change.+ Per-instance, not per-object-record, because the record was out of room at the+ time. The entry below is the change that moved it.++- **A mesh has somewhere to put its own vertex attributes.** All 16 vertex+ attribute slots had a semantic owner — 0-7 the mesh's own channels (serialised+ in `.esmesh`), 8-15 the engine's per-object record — so a mesh that wanted to+ carry anything else had nowhere to carry it, and neither did the record when it+ wanted to grow.++ The record is a texture now, read by index rather than by whichever instance+ the hardware happens to be on, and **slots 8-15 are free**. A draw says where+ its own run of records begins (`gl_InstanceID` restarts every draw while the+ frame's records are one run), and the whole layout collapses to one fixed+ eight-texel shape: the stride the layout, the allocation and the packer each+ used to derive for themselves is gone, and so is every variant of it.++ Full floats, because what a record carries is a world-space translation and a+ half loses whole units of one a thousand away — so `RGBA32F` is a format the+ engine has now, on both backends. On WebGPU it is unfilterable, which the+ bind-group layout has to say: a unit that declares no sampler cannot be+ filtered, so that is the answer read out of the shader itself rather than a+ second table to keep.++ Every one of the 174 pixel scenes draws what it drew before, on both backends,+ and 200 copies of a crate still cost 1-2 draw calls.- **Automation can read what a frame cost.** A probe could ask what a game wasdoing and not what it was paying: `Stats` is installed by StatsPlugin, which adiff --git a/examples/effects-gallery/assets/materials/dissolve.esshader b/examples/effects-gallery/assets/materials/dissolve.esshaderindex f1011a579..92d87fcfb 100644--- a/examples/effects-gallery/assets/materials/dissolve.esshader+++ b/examples/effects-gallery/assets/materials/dissolve.esshader@@ -40,7 +40,7 @@ void main() {// GENERATED by tools/gen-shader-twins.mjs (glslang + naga) - do not edit.// Regenerate after changing the GLSL stages above.// source-hash: 0a687499da49fe50-// assembled-hash: 60636c76d066e1af+// assembled-hash: 3b7c3069a5e2ac75struct gl_PerVertex {@builtin(position) gl_Position: vec4<f32>,gl_PointSize: f32,@@ -64,6 +64,10 @@ struct MaterialConstants {u_noiseScale: f32,}+struct InstanceConstants {+ u_instanceBase: vec4<u32>,+}+struct VertexOutput {@location(0) member: vec4<f32>,@builtin(position) gl_Position: vec4<f32>,@@ -82,19 +86,25 @@ var<private> a_texCoord_1: vec2<f32>;var<uniform> unnamed_2: TimeConstants;@group(0) @binding(1)var<uniform> unnamed_3: MaterialConstants;+@group(0) @binding(4)+var<uniform> unnamed_4: InstanceConstants;+@group(1) @binding(23)+var u_instanceData_est: texture_2d<f32>;+@group(1) @binding(31)+var u_instanceData_ess: sampler;fn main_1() {var world: vec4<f32>;- let _e12 = a_position_1;- world = vec4<f32>(_e12.x, _e12.y, _e12.z, 1f);- let _e17 = a_color_1;- v_color = _e17;- let _e19 = unnamed_1.u_projection;- let _e20 = world;- unnamed.gl_Position = (_e19 * _e20);- let _e23 = a_texCoord_1;- v_texCoord = _e23;+ let _e15 = a_position_1;+ world = vec4<f32>(_e15.x, _e15.y, _e15.z, 1f);+ let _e20 = a_color_1;+ v_color = _e20;+ let _e22 = unnamed_1.u_projection;+ let _e23 = world;+ unnamed.gl_Position = (_e22 * _e23);+ let _e26 = a_texCoord_1;+ v_texCoord = _e26;return;}@@ -115,7 +125,7 @@ fn vs_main(@location(0) a_position: vec3<f32>, @location(1) a_color: vec4<f32>,// GENERATED by tools/gen-shader-twins.mjs (glslang + naga) - do not edit.// Regenerate after changing the GLSL stages above.// source-hash: 0a687499da49fe50-// assembled-hash: 60636c76d066e1af+// assembled-hash: 3b7c3069a5e2ac75struct MaterialConstants {u_progress: f32,u_edgeColor: vec4<f32>,diff --git a/examples/effects-gallery/assets/materials/hit-flash.esshader b/examples/effects-gallery/assets/materials/hit-flash.esshaderindex 4d434aa56..439ff4d28 100644--- a/examples/effects-gallery/assets/materials/hit-flash.esshader+++ b/examples/effects-gallery/assets/materials/hit-flash.esshader@@ -25,7 +25,7 @@ void main() {// GENERATED by tools/gen-shader-twins.mjs (glslang + naga) - do not edit.// Regenerate after changing the GLSL stages above.// source-hash: 674537b45fb865fb-// assembled-hash: 72ca189990dafa80+// assembled-hash: 51a5fa5bc7072ae0struct gl_PerVertex {@builtin(position) gl_Position: vec4<f32>,gl_PointSize: f32,@@ -47,6 +47,10 @@ struct MaterialConstants {u_flashColor: vec4<f32>,}+struct InstanceConstants {+ u_instanceBase: vec4<u32>,+}+struct VertexOutput {@location(0) member: vec4<f32>,@builtin(position) gl_Position: vec4<f32>,@@ -65,19 +69,25 @@ var<private> a_texCoord_1: vec2<f32>;var<uniform> unnamed_2: TimeConstants;@group(0) @binding(1)var<uniform> unnamed_3: MaterialConstants;+@group(0) @binding(4)+var<uniform> unnamed_4: InstanceConstants;+@group(1) @binding(23)+var u_instanceData_est: texture_2d<f32>;+@group(1) @binding(31)+var u_instanceData_ess: sampler;fn main_1() {var world: vec4<f32>;- let _e12 = a_position_1;- world = vec4<f32>(_e12.x, _e12.y, _e12.z, 1f);- let _e17 = a_color_1;- v_color = _e17;- let _e19 = unnamed_1.u_projection;- let _e20 = world;- unnamed.gl_Position = (_e19 * _e20);- let _e23 = a_texCoord_1;- v_texCoord = _e23;+ let _e15 = a_position_1;+ world = vec4<f32>(_e15.x, _e15.y, _e15.z, 1f);+ let _e20 = a_color_1;+ v_color = _e20;+ let _e22 = unnamed_1.u_projection;+ let _e23 = world;+ unnamed.gl_Position = (_e22 * _e23);+ let _e26 = a_texCoord_1;+ v_texCoord = _e26;return;}@@ -98,7 +108,7 @@ fn vs_main(@location(0) a_position: vec3<f32>, @location(1) a_color: vec4<f32>,// GENERATED by tools/gen-shader-twins.mjs (glslang + naga) - do not edit.// Regenerate after changing the GLSL stages above.// source-hash: 674537b45fb865fb-// assembled-hash: 72ca189990dafa80+// assembled-hash: 51a5fa5bc7072ae0struct MaterialConstants {u_flash: f32,u_flashColor: vec4<f32>,diff --git a/examples/effects-gallery/assets/materials/outline.esshader b/examples/effects-gallery/assets/materials/outline.esshaderindex 8f5e71d1a..f3dd29fcd 100644--- a/examples/effects-gallery/assets/materials/outline.esshader+++ b/examples/effects-gallery/assets/materials/outline.esshader@@ -34,7 +34,7 @@ void main() {// GENERATED by tools/gen-shader-twins.mjs (glslang + naga) - do not edit.// Regenerate after changing the GLSL stages above.// source-hash: 7ed4f4686c6bfd8d-// assembled-hash: e924eb284d528261+// assembled-hash: 659232dd4e620239struct gl_PerVertex {@builtin(position) gl_Position: vec4<f32>,gl_PointSize: f32,@@ -56,6 +56,10 @@ struct MaterialConstants {u_outlineWidth: f32,}+struct InstanceConstants {+ u_instanceBase: vec4<u32>,+}+struct VertexOutput {@location(0) member: vec4<f32>,@builtin(position) gl_Position: vec4<f32>,@@ -74,19 +78,25 @@ var<private> a_texCoord_1: vec2<f32>;var<uniform> unnamed_2: TimeConstants;@group(0) @binding(1)var<uniform> unnamed_3: MaterialConstants;+@group(0) @binding(4)+var<uniform> unnamed_4: InstanceConstants;+@group(1) @binding(23)+var u_instanceData_est: texture_2d<f32>;+@group(1) @binding(31)+var u_instanceData_ess: sampler;fn main_1() {var world: vec4<f32>;- let _e12 = a_position_1;- world = vec4<f32>(_e12.x, _e12.y, _e12.z, 1f);- let _e17 = a_color_1;- v_color = _e17;- let _e19 = unnamed_1.u_projection;- let _e20 = world;- unnamed.gl_Position = (_e19 * _e20);- let _e23 = a_texCoord_1;- v_texCoord = _e23;+ let _e15 = a_position_1;+ world = vec4<f32>(_e15.x, _e15.y, _e15.z, 1f);+ let _e20 = a_color_1;+ v_color = _e20;+ let _e22 = unnamed_1.u_projection;+ let _e23 = world;+ unnamed.gl_Position = (_e22 * _e23);+ let _e26 = a_texCoord_1;+ v_texCoord = _e26;return;}@@ -107,7 +117,7 @@ fn vs_main(@location(0) a_position: vec3<f32>, @location(1) a_color: vec4<f32>,// GENERATED by tools/gen-shader-twins.mjs (glslang + naga) - do not edit.// Regenerate after changing the GLSL stages above.// source-hash: 7ed4f4686c6bfd8d-// assembled-hash: e924eb284d528261+// assembled-hash: 659232dd4e620239struct MaterialConstants {u_outlineColor: vec4<f32>,u_outlineWidth: f32,diff --git a/examples/effects-gallery/assets/materials/pixelate.esshader b/examples/effects-gallery/assets/materials/pixelate.esshaderindex 8a5021c14..1bee84001 100644--- a/examples/effects-gallery/assets/materials/pixelate.esshader+++ b/examples/effects-gallery/assets/materials/pixelate.esshader@@ -23,7 +23,7 @@ void main() {// GENERATED by tools/gen-shader-twins.mjs (glslang + naga) - do not edit.// Regenerate after changing the GLSL stages above.// source-hash: 0c2fb4b31599ec7a-// assembled-hash: 8d6d37a7023e822f+// assembled-hash: aa5efc32d552a8dbstruct gl_PerVertex {@builtin(position) gl_Position: vec4<f32>,gl_PointSize: f32,@@ -44,6 +44,10 @@ struct MaterialConstants {u_pixels: f32,}+struct InstanceConstants {+ u_instanceBase: vec4<u32>,+}+struct VertexOutput {@location(0) member: vec4<f32>,@builtin(position) gl_Position: vec4<f32>,@@ -62,19 +66,25 @@ var<private> a_texCoord_1: vec2<f32>;var<uniform> unnamed_2: TimeConstants;@group(0) @binding(1)var<uniform> unnamed_3: MaterialConstants;+@group(0) @binding(4)+var<uniform> unnamed_4: InstanceConstants;+@group(1) @binding(23)+var u_instanceData_est: texture_2d<f32>;+@group(1) @binding(31)+var u_instanceData_ess: sampler;fn main_1() {var world: vec4<f32>;- let _e12 = a_position_1;- world = vec4<f32>(_e12.x, _e12.y, _e12.z, 1f);- let _e17 = a_color_1;- v_color = _e17;- let _e19 = unnamed_1.u_projection;- let _e20 = world;- unnamed.gl_Position = (_e19 * _e20);- let _e23 = a_texCoord_1;- v_texCoord = _e23;+ let _e15 = a_position_1;+ world = vec4<f32>(_e15.x, _e15.y, _e15.z, 1f);+ let _e20 = a_color_1;+ v_color = _e20;+ let _e22 = unnamed_1.u_projection;+ let _e23 = world;+ unnamed.gl_Position = (_e22 * _e23);+ let _e26 = a_texCoord_1;+ v_texCoord = _e26;return;}@@ -95,7 +105,7 @@ fn vs_main(@location(0) a_position: vec3<f32>, @location(1) a_color: vec4<f32>,// GENERATED by tools/gen-shader-twins.mjs (glslang + naga) - do not edit.// Regenerate after changing the GLSL stages above.// source-hash: 0c2fb4b31599ec7a-// assembled-hash: 8d6d37a7023e822f+// assembled-hash: aa5efc32d552a8dbstruct MaterialConstants {u_pixels: f32,}diff --git a/examples/effects-gallery/assets/materials/uv-scroll.esshader b/examples/effects-gallery/assets/materials/uv-scroll.esshaderindex 1789680f9..aabefe441 100644--- a/examples/effects-gallery/assets/materials/uv-scroll.esshader+++ b/examples/effects-gallery/assets/materials/uv-scroll.esshader@@ -24,7 +24,7 @@ void main() {// GENERATED by tools/gen-shader-twins.mjs (glslang + naga) - do not edit.// Regenerate after changing the GLSL stages above.// source-hash: 301c5b2b73d966f3-// assembled-hash: d2039f3013a5d165+// assembled-hash: 31e405ee96a6823estruct gl_PerVertex {@builtin(position) gl_Position: vec4<f32>,gl_PointSize: f32,@@ -45,6 +45,10 @@ struct MaterialConstants {u_scrollSpeed: vec2<f32>,}+struct InstanceConstants {+ u_instanceBase: vec4<u32>,+}+struct VertexOutput {@location(0) member: vec4<f32>,@builtin(position) gl_Position: vec4<f32>,@@ -63,19 +67,25 @@ var<private> a_texCoord_1: vec2<f32>;var<uniform> unnamed_2: TimeConstants;@group(0) @binding(1)var<uniform> unnamed_3: MaterialConstants;+@group(0) @binding(4)+var<uniform> unnamed_4: InstanceConstants;+@group(1) @binding(23)+var u_instanceData_est: texture_2d<f32>;+@group(1) @binding(31)+var u_instanceData_ess: sampler;fn main_1() {var world: vec4<f32>;- let _e12 = a_position_1;- world = vec4<f32>(_e12.x, _e12.y, _e12.z, 1f);- let _e17 = a_color_1;- v_color = _e17;- let _e19 = unnamed_1.u_projection;- let _e20 = world;- unnamed.gl_Position = (_e19 * _e20);- let _e23 = a_texCoord_1;- v_texCoord = _e23;+ let _e15 = a_position_1;+ world = vec4<f32>(_e15.x, _e15.y, _e15.z, 1f);+ let _e20 = a_color_1;+ v_color = _e20;+ let _e22 = unnamed_1.u_projection;+ let _e23 = world;+ unnamed.gl_Position = (_e22 * _e23);+ let _e26 = a_texCoord_1;+ v_texCoord = _e26;return;}@@ -96,7 +106,7 @@ fn vs_main(@location(0) a_position: vec3<f32>, @location(1) a_color: vec4<f32>,// GENERATED by tools/gen-shader-twins.mjs (glslang + naga) - do not edit.// Regenerate after changing the GLSL stages above.// source-hash: 301c5b2b73d966f3-// assembled-hash: d2039f3013a5d165+// assembled-hash: 31e405ee96a6823estruct TimeConstants {u_time: vec4<f32>,u_viewport: vec4<f32>,diff --git a/fixtures/scenes/depth-layers/opaque.esshader b/fixtures/scenes/depth-layers/opaque.esshaderindex df8e73d64..08673f0ad 100644--- a/fixtures/scenes/depth-layers/opaque.esshader+++ b/fixtures/scenes/depth-layers/opaque.esshader@@ -24,7 +24,7 @@ void main() {// GENERATED by tools/gen-shader-twins.mjs (glslang + naga) - do not edit.// Regenerate after changing the GLSL stages above.// source-hash: 930787555114ce9c-// assembled-hash: 4fbd4cec4b7a3eab+// assembled-hash: cab87529431e525astruct gl_PerVertex {@builtin(position) gl_Position: vec4<f32>,gl_PointSize: f32,@@ -41,6 +41,10 @@ struct TimeConstants {u_viewport: vec4<f32>,}+struct InstanceConstants {+ u_instanceBase: vec4<u32>,+}+struct VertexOutput {@location(0) member: vec4<f32>,@builtin(position) gl_Position: vec4<f32>,@@ -57,19 +61,25 @@ var<private> v_texCoord: vec2<f32>;var<private> a_texCoord_1: vec2<f32>;@group(0) @binding(3)var<uniform> unnamed_2: TimeConstants;+@group(0) @binding(4)+var<uniform> unnamed_3: InstanceConstants;+@group(1) @binding(23)+var u_instanceData_est: texture_2d<f32>;+@group(1) @binding(31)+var u_instanceData_ess: sampler;fn main_1() {var world: vec4<f32>;- let _e11 = a_position_1;- world = vec4<f32>(_e11.x, _e11.y, _e11.z, 1f);- let _e16 = a_color_1;- v_color = _e16;- let _e18 = unnamed_1.u_projection;- let _e19 = world;- unnamed.gl_Position = (_e18 * _e19);- let _e22 = a_texCoord_1;- v_texCoord = _e22;+ let _e14 = a_position_1;+ world = vec4<f32>(_e14.x, _e14.y, _e14.z, 1f);+ let _e19 = a_color_1;+ v_color = _e19;+ let _e21 = unnamed_1.u_projection;+ let _e22 = world;+ unnamed.gl_Position = (_e21 * _e22);+ let _e25 = a_texCoord_1;+ v_texCoord = _e25;return;}@@ -90,7 +100,7 @@ fn vs_main(@location(0) a_position: vec3<f32>, @location(1) a_color: vec4<f32>,// GENERATED by tools/gen-shader-twins.mjs (glslang + naga) - do not edit.// Regenerate after changing the GLSL stages above.// source-hash: 930787555114ce9c-// assembled-hash: 4fbd4cec4b7a3eab+// assembled-hash: cab87529431e525astruct FrameConstants {u_projection: mat4x4<f32>,u_camera: vec4<f32>,diff --git a/fixtures/scenes/mat-builtin-lit/lit.esshader b/fixtures/scenes/mat-builtin-lit/lit.esshaderindex 4d467d302..91f7ef0cd 100644--- a/fixtures/scenes/mat-builtin-lit/lit.esshader+++ b/fixtures/scenes/mat-builtin-lit/lit.esshader@@ -27,7 +27,7 @@ void main() {// GENERATED by tools/gen-shader-twins.mjs (glslang + naga) - do not edit.// Regenerate after changing the GLSL stages above.// source-hash: 5330079245724fa2-// assembled-hash: 5d2188654785ea29+// assembled-hash: cc964016cf869c0bstruct gl_PerVertex {@builtin(position) gl_Position: vec4<f32>,gl_PointSize: f32,@@ -48,6 +48,10 @@ struct MaterialConstants {u_tint: vec4<f32>,}+struct InstanceConstants {+ u_instanceBase: vec4<u32>,+}+struct VertexOutput {@location(1) member: vec4<f32>,@builtin(position) gl_Position: vec4<f32>,@@ -75,23 +79,29 @@ var<uniform> unnamed_3: MaterialConstants;var u_normalMap_est: texture_2d<f32>;@group(1) @binding(24)var u_normalMap_ess: sampler;+@group(0) @binding(4)+var<uniform> unnamed_4: InstanceConstants;+@group(1) @binding(23)+var u_instanceData_est: texture_2d<f32>;+@group(1) @binding(31)+var u_instanceData_ess: sampler;fn main_1() {var world: vec4<f32>;- let _e17 = a_position_1;- world = vec4<f32>(_e17.x, _e17.y, _e17.z, 1f);- let _e22 = a_color_1;- v_color = _e22;- let _e24 = unnamed_1.u_projection;- let _e25 = world;- unnamed.gl_Position = (_e24 * _e25);- let _e28 = a_texCoord_1;- v_texCoord = _e28;- let _e29 = world;- v_worldPos = _e29.xy;- let _e31 = gl_InstanceIndex_1;- v_probeSlot = f32(_e31);+ let _e20 = a_position_1;+ world = vec4<f32>(_e20.x, _e20.y, _e20.z, 1f);+ let _e25 = a_color_1;+ v_color = _e25;+ let _e27 = unnamed_1.u_projection;+ let _e28 = world;+ unnamed.gl_Position = (_e27 * _e28);+ let _e31 = a_texCoord_1;+ v_texCoord = _e31;+ let _e32 = world;+ v_worldPos = _e32.xy;+ let _e34 = gl_InstanceIndex_1;+ v_probeSlot = f32(_e34);return;}@@ -115,7 +125,7 @@ fn vs_main(@location(0) a_position: vec3<f32>, @location(1) a_color: vec4<f32>,// GENERATED by tools/gen-shader-twins.mjs (glslang + naga) - do not edit.// Regenerate after changing the GLSL stages above.// source-hash: 5330079245724fa2-// assembled-hash: 5d2188654785ea29+// assembled-hash: cc964016cf869c0bstruct FrameConstants {u_projection: mat4x4<f32>,u_camera: vec4<f32>,diff --git a/fixtures/scenes/mat-dissolve/dissolve.esshader b/fixtures/scenes/mat-dissolve/dissolve.esshaderindex f1011a579..92d87fcfb 100644--- a/fixtures/scenes/mat-dissolve/dissolve.esshader+++ b/fixtures/scenes/mat-dissolve/dissolve.esshader@@ -40,7 +40,7 @@ void main() {// GENERATED by tools/gen-shader-twins.mjs (glslang + naga) - do not edit.// Regenerate after changing the GLSL stages above.// source-hash: 0a687499da49fe50-// assembled-hash: 60636c76d066e1af+// assembled-hash: 3b7c3069a5e2ac75struct gl_PerVertex {@builtin(position) gl_Position: vec4<f32>,gl_PointSize: f32,@@ -64,6 +64,10 @@ struct MaterialConstants {u_noiseScale: f32,}+struct InstanceConstants {+ u_instanceBase: vec4<u32>,+}+struct VertexOutput {@location(0) member: vec4<f32>,@builtin(position) gl_Position: vec4<f32>,@@ -82,19 +86,25 @@ var<private> a_texCoord_1: vec2<f32>;var<uniform> unnamed_2: TimeConstants;@group(0) @binding(1)var<uniform> unnamed_3: MaterialConstants;+@group(0) @binding(4)+var<uniform> unnamed_4: InstanceConstants;+@group(1) @binding(23)+var u_instanceData_est: texture_2d<f32>;+@group(1) @binding(31)+var u_instanceData_ess: sampler;fn main_1() {var world: vec4<f32>;- let _e12 = a_position_1;- world = vec4<f32>(_e12.x, _e12.y, _e12.z, 1f);- let _e17 = a_color_1;- v_color = _e17;- let _e19 = unnamed_1.u_projection;- let _e20 = world;- unnamed.gl_Position = (_e19 * _e20);- let _e23 = a_texCoord_1;- v_texCoord = _e23;+ let _e15 = a_position_1;+ world = vec4<f32>(_e15.x, _e15.y, _e15.z, 1f);+ let _e20 = a_color_1;+ v_color = _e20;+ let _e22 = unnamed_1.u_projection;+ let _e23 = world;+ unnamed.gl_Position = (_e22 * _e23);+ let _e26 = a_texCoord_1;+ v_texCoord = _e26;return;}@@ -115,7 +125,7 @@ fn vs_main(@location(0) a_position: vec3<f32>, @location(1) a_color: vec4<f32>,// GENERATED by tools/gen-shader-twins.mjs (glslang + naga) - do not edit.// Regenerate after changing the GLSL stages above.// source-hash: 0a687499da49fe50-// assembled-hash: 60636c76d066e1af+// assembled-hash: 3b7c3069a5e2ac75struct MaterialConstants {u_progress: f32,u_edgeColor: vec4<f32>,diff --git a/fixtures/scenes/mat-graph/graph-tint.esshader b/fixtures/scenes/mat-graph/graph-tint.esshaderindex 8ab803576..8e2868dae 100644--- a/fixtures/scenes/mat-graph/graph-tint.esshader+++ b/fixtures/scenes/mat-graph/graph-tint.esshader@@ -39,7 +39,7 @@ void main() {// GENERATED by tools/gen-shader-twins.mjs (glslang + naga) - do not edit.// Regenerate after changing the GLSL stages above.// source-hash: 5382212ba4021113-// assembled-hash: 948218796d1bc56c+// assembled-hash: f490343500205763struct gl_PerVertex {@builtin(position) gl_Position: vec4<f32>,gl_PointSize: f32,@@ -113,7 +113,7 @@ fn vs_main(@location(0) a_position: vec3<f32>, @location(1) a_color: vec4<f32>,// GENERATED by tools/gen-shader-twins.mjs (glslang + naga) - do not edit.// Regenerate after changing the GLSL stages above.// source-hash: 5382212ba4021113-// assembled-hash: 948218796d1bc56c+// assembled-hash: f490343500205763struct MaterialConstants {u_tint: vec4<f32>,}diff --git a/fixtures/scenes/mat-sw/switched.esshader b/fixtures/scenes/mat-sw/switched.esshaderindex 315603270..009d82cd5 100644--- a/fixtures/scenes/mat-sw/switched.esshader+++ b/fixtures/scenes/mat-sw/switched.esshader@@ -38,7 +38,7 @@ void main() {// GENERATED by tools/gen-shader-twins.mjs (glslang + naga) - do not edit.// Regenerate after changing the GLSL stages above.// source-hash: 0ba3d3644597ce83-// assembled-hash: eb67150bbfaf17cb+// assembled-hash: da96256e5deec553#ifdef USE_GREENstruct gl_PerVertex {@builtin(position) gl_Position: vec4<f32>,@@ -152,7 +152,7 @@ fn vs_main(@location(0) a_position: vec3<f32>, @location(1) a_color: vec4<f32>,// GENERATED by tools/gen-shader-twins.mjs (glslang + naga) - do not edit.// Regenerate after changing the GLSL stages above.// source-hash: 0ba3d3644597ce83-// assembled-hash: eb67150bbfaf17cb+// assembled-hash: da96256e5deec553#ifdef USE_GREENstruct FrameConstants {u_projection: mat4x4<f32>,diff --git a/fixtures/scenes/mat-tex/masked.esshader b/fixtures/scenes/mat-tex/masked.esshaderindex 1adff28c1..282219971 100644--- a/fixtures/scenes/mat-tex/masked.esshader+++ b/fixtures/scenes/mat-tex/masked.esshader@@ -38,7 +38,7 @@ void main() {// GENERATED by tools/gen-shader-twins.mjs (glslang + naga) - do not edit.// Regenerate after changing the GLSL stages above.// source-hash: 11b26db499fb2fc6-// assembled-hash: 995c87fac03d97a3+// assembled-hash: d081d31e97177956struct gl_PerVertex {@builtin(position) gl_Position: vec4<f32>,gl_PointSize: f32,@@ -106,7 +106,7 @@ fn vs_main(@location(0) a_position: vec3<f32>, @location(1) a_color: vec4<f32>,// GENERATED by tools/gen-shader-twins.mjs (glslang + naga) - do not edit.// Regenerate after changing the GLSL stages above.// source-hash: 11b26db499fb2fc6-// assembled-hash: 995c87fac03d97a3+// assembled-hash: d081d31e97177956struct FrameConstants {u_projection: mat4x4<f32>,u_camera: vec4<f32>,diff --git a/fixtures/scenes/mat-tint/tint.esshader b/fixtures/scenes/mat-tint/tint.esshaderindex 692585474..5edbda82f 100644--- a/fixtures/scenes/mat-tint/tint.esshader+++ b/fixtures/scenes/mat-tint/tint.esshader@@ -78,7 +78,7 @@ struct VSOut {// GENERATED by tools/gen-shader-twins.mjs (glslang + naga) - do not edit.// Regenerate after changing the GLSL stages above.// source-hash: 7be4a77f9921c078-// assembled-hash: 153358b0bdd2ce6a+// assembled-hash: 2907a720efa553dcstruct gl_PerVertex {@builtin(position) gl_Position: vec4<f32>,gl_PointSize: f32,@@ -148,7 +148,7 @@ fn vs_main(@location(0) a_position: vec3<f32>, @location(1) a_color: vec4<f32>,// GENERATED by tools/gen-shader-twins.mjs (glslang + naga) - do not edit.// Regenerate after changing the GLSL stages above.// source-hash: 7be4a77f9921c078-// assembled-hash: 153358b0bdd2ce6a+// assembled-hash: 2907a720efa553dcstruct MaterialConstants {u_tint: vec4<f32>,}diff --git a/src/esengine/bindings/WebSDKEntry.cpp b/src/esengine/bindings/WebSDKEntry.cppindex 1289541cb..2617e9f8f 100644--- a/src/esengine/bindings/WebSDKEntry.cpp+++ b/src/esengine/bindings/WebSDKEntry.cpp@@ -125,7 +125,7 @@ emscripten::val esshader_cookInfo(const std::string& source, const std::string&out.set("hasWgslFragment", parsed.wgslStages.count(resource::ShaderStage::Fragment) != 0);out.set("hasSwitches", !parsed.switches.empty());// The switch/feature NAMES, so a cook can enumerate the permutations it has- // to emit a twin for — WGSL resolves them at assembly time (preprocessWGSL),+ // to emit a twin for — WGSL resolves them at assembly time (preprocessConditionals),// which needs every branch present in the body.emscripten::val switches = emscripten::val::array();u32 switchCount = 0;diff --git a/src/esengine/data/shaders/mesh.esshader b/src/esengine/data/shaders/mesh.esshaderindex 91cdcdd01..ab0c38fc4 100644--- a/src/esengine/data/shaders/mesh.esshader+++ b/src/esengine/data/shaders/mesh.esshader@@ -59,31 +59,10 @@ layout(location = 6) in vec4 a_weights;layout(location = 7) in vec2 a_texCoord1;#endif-// 8.. and not 3..: a channel's semantic IS its location, so adding normals to a-// mesh must not move the per-object record (see MESH_INSTANCE_FIRST_LOCATION).-#ifndef SKINNED-// Three ROWS of an affine 4x4. The fourth is (0,0,0,1) on every transform a-// Transform can state, so carrying it spends an attribute slot and 16 bytes an-// instance on a constant.-layout(location = 8) in vec4 a_model0;-layout(location = 9) in vec4 a_model1;-layout(location = 10) in vec4 a_model2;-#ifdef MESH_LIGHTMAP-// Appended to the record, so the tint and the normal matrix keep the offsets-// they had. Never on a skinned draw: bones move a character, and a character is-// what a bake cannot hold still long enough to light.-layout(location = 11) in vec4 a_lightmapRect;-#endif-#endif-layout(location = 12) in vec4 a_instTint;-#if defined(MESH_NORMALS) && !defined(SKINNED)-// The normal matrix (inverse transpose of the model's 3x3), per object: under a-// non-uniform scale the model matrix skews a normal, and inverting one per-// vertex is the alternative.-layout(location = 13) in vec3 a_nrm0;-layout(location = 14) in vec3 a_nrm1;-layout(location = 15) in vec3 a_nrm2;-#endif+// The per-object record comes from the frame's texture, not from attributes:+// esInstanceModel / esInstanceTint / esInstanceNormalMatrix /+// esInstanceLightmapRect are injected by the assembler for any vertex stage that+// names one, which is what leaves every slot above the mesh channels free.#ifdef SKINNED// This draw's pose, rewritten immediately before it (SkinConstants, binding 5).@@ -169,18 +148,16 @@ void main() {+ a_weights.w * u_bones[a_joints.w];vec4 world = skin * vec4(local, 1.0);#else- mat4 model = mat4(vec4(a_model0.x, a_model1.x, a_model2.x, 0.0),- vec4(a_model0.y, a_model1.y, a_model2.y, 0.0),- vec4(a_model0.z, a_model1.z, a_model2.z, 0.0),- vec4(a_model0.w, a_model1.w, a_model2.w, 1.0));+ mat4 model = esInstanceModel();vec4 world = model * vec4(local, 1.0);#endifgl_Position = u_projection * world;v_texCoord = a_texCoord;- v_color = a_color * a_instTint;+ v_color = a_color * esInstanceTint();#ifdef MESH_LIGHTMAP- v_lightmap = vec3(a_texCoord1 * a_lightmapRect.xy + a_lightmapRect.zw,- a_lightmapRect.x > 0.0 ? 1.0 : 0.0);+ highp vec4 lmRect = esInstanceLightmapRect();+ v_lightmap = vec3(a_texCoord1 * lmRect.xy + lmRect.zw,+ lmRect.x > 0.0 ? 1.0 : 0.0);#endif#ifdef SHADOW_DEPTHv_shadowClip = gl_Position;@@ -192,7 +169,7 @@ void main() {#if defined(MESH_NORMALS) && defined(SKINNED)v_worldNormal = mat3(skin) * localNormal;#elif defined(MESH_NORMALS)- v_worldNormal = mat3(a_nrm0, a_nrm1, a_nrm2) * localNormal;+ v_worldNormal = esInstanceNormalMatrix() * localNormal;#else// A surface with no normal channel faces the viewer, exactly as a sprite does.v_worldNormal = vec3(0.0, 0.0, 1.0);@@ -301,27 +278,12 @@ struct VSIn {#ifdef SKINNED@location(5) a_joints : vec4u,@location(6) a_weights : vec4f,-#else- @location(8) a_model0 : vec4f,- @location(9) a_model1 : vec4f,- @location(10) a_model2 : vec4f,-#ifdef MESH_LIGHTMAP- @location(11) a_lightmapRect : vec4f,-#endif#endif#ifdef MESH_LIGHTMAP@location(7) a_texCoord1 : vec2f,#endif- @location(12) a_instTint : vec4f,// What the GLSL twin reads as gl_VertexID: a morph delta is addressed by it.@builtin(vertex_index) vertexIndex : u32,-#ifdef MESH_NORMALS-#ifndef SKINNED- @location(13) a_nrm0 : vec3f,- @location(14) a_nrm1 : vec3f,- @location(15) a_nrm2 : vec3f,-#endif-#endif};struct VSOut {@builtin(position) pos : vec4f,@@ -344,6 +306,7 @@ struct VSOut {};@vertex fn vs_main(v : VSIn, @builtin(instance_index) inst : u32) -> VSOut {+ g_instanceId = i32(inst);var local = v.a_position;#ifdef MESH_NORMALSvar localNormal = v.a_normal;@@ -370,20 +333,17 @@ struct VSOut {+ v.a_weights.w * skin.bones[v.a_joints.w];let world = pose * vec4f(local, 1.0);#else- let model = mat4x4f(vec4f(v.a_model0.x, v.a_model1.x, v.a_model2.x, 0.0),- vec4f(v.a_model0.y, v.a_model1.y, v.a_model2.y, 0.0),- vec4f(v.a_model0.z, v.a_model1.z, v.a_model2.z, 0.0),- vec4f(v.a_model0.w, v.a_model1.w, v.a_model2.w, 1.0));- let world = model * vec4f(local, 1.0);+ let world = esInstanceModel() * vec4f(local, 1.0);#endifvar out : VSOut;out.pos = frame.projection * world;out.v_texCoord = v.a_texCoord;- out.v_color = v.a_color * v.a_instTint;+ out.v_color = v.a_color * esInstanceTint();#ifdef MESH_LIGHTMAP- out.v_lightmap = vec3f(v.a_texCoord1 * v.a_lightmapRect.xy + v.a_lightmapRect.zw,- select(0.0, 1.0, v.a_lightmapRect.x > 0.0));+ let lmRect = esInstanceLightmapRect();+ out.v_lightmap = vec3f(v.a_texCoord1 * lmRect.xy + lmRect.zw,+ select(0.0, 1.0, lmRect.x > 0.0));#endif#ifdef SHADOW_DEPTHout.v_shadowClip = out.pos;@@ -395,7 +355,7 @@ struct VSOut {#ifdef SKINNEDout.v_worldNormal = mat3x3f(pose[0].xyz, pose[1].xyz, pose[2].xyz) * localNormal;#else- out.v_worldNormal = mat3x3f(v.a_nrm0, v.a_nrm1, v.a_nrm2) * localNormal;+ out.v_worldNormal = esInstanceNormalMatrix() * localNormal;#endif#else// A surface with no normal channel faces the viewer, exactly as a sprite does.diff --git a/src/esengine/renderer/draw/DrawList.cpp b/src/esengine/renderer/draw/DrawList.cppindex 3281160cc..9f9ba96b4 100644--- a/src/esengine/renderer/draw/DrawList.cpp+++ b/src/esengine/renderer/draw/DrawList.cpp@@ -3,6 +3,7 @@#include "./DrawList.hpp"#include "./BatchVertex.hpp"#include "../store/MaterialStore.hpp"+#include "../store/InstanceConstants.hpp"#include "../store/SkinConstants.hpp"#include "../../core/FrameProfiler.hpp"@@ -195,11 +196,15 @@ void DrawList::finalize(TransientBufferPool& pool) {void DrawList::execute(GfxDevice& device, TransientBufferPool& buffers,MaterialStore& materials, u32 white_texture_id,- FrameCapture* capture, PerDrawBlocks* skin_blocks,- PerDrawBlocks* morph_blocks, PerDrawBlocks* probe_blocks) {+ FrameCapture* capture, const PerDrawBlockSet& blocks) {PipelineDesc lastDesc{};PipelineHandle lastHandle = PipelineHandle::Invalid;+ // Once for the pass, not once per draw: every resident mesh reads the same+ // texture. Rebound each pass because growing it mints a new handle.+ if (buffers.instanceTexture() != TextureHandle::Invalid) {+ device.bindTexture(MESH_INSTANCE_TEXTURE_UNIT, buffers.instanceTexture());+ }for (u32 i = 0; i < merged_draw_calls_; ++i) {const auto& cmd = commands_[i];@@ -243,28 +248,28 @@ void DrawList::execute(GfxDevice& device, TransientBufferPool& buffers,// The pose, for a draw that has one — in a buffer of this draw's own. A// block shared between two draws of one pass is read by both with what// the last wrote, which is a room of characters in one pose.- if (cmd.skin_count > 0 && skin_blocks) {+ if (cmd.skin_count > 0 && blocks.skin) {device.setUniformBuffer(SKIN_CONSTANTS_BINDING,- skin_blocks->write(skin_matrices_.data() + cmd.skin_offset,+ blocks.skin->write(skin_matrices_.data() + cmd.skin_offset,cmd.skin_count * static_cast<u32>(sizeof(glm::mat4))));}// The shapes, the same way — and a draw with none binds the zeroed block// rather than erasing the last one's, so "unshaped" is an object instead// of an erasure someone has to remember to perform.- if (morph_blocks) {+ if (blocks.morph) {device.setUniformBuffer(MORPH_CONSTANTS_BINDING,cmd.morph_index > 0- ? morph_blocks->write(&morph_shapes_[cmd.morph_index - 1],+ ? blocks.morph->write(&morph_shapes_[cmd.morph_index - 1],sizeof(MorphConstants))- : morph_blocks->zero());+ : blocks.morph->zero());}// WHERE this draw's instances read their irradiance, not what it is: the// coefficients are in the frame's texture, one run per instance, so a// merged draw holding objects in many places still says one thing here.- if (probe_blocks) {+ if (blocks.probe) {// Every instance of this run, in the order it draws them, capped at// what the block holds — a longer run carries no irradiance at all,// so its tail reads zeroes and takes the environment.@@ -280,7 +285,7 @@ void DrawList::execute(GfxDevice& device, TransientBufferPool& buffers,}device.setUniformBuffer(PROBE_CONSTANTS_BINDING,- probe_blocks->write(probe_run_.data(),+ blocks.probe->write(probe_run_.data(),static_cast<u32>(probe_run_.size() * sizeof(glm::vec4))));}@@ -312,12 +317,15 @@ void DrawList::execute(GfxDevice& device, TransientBufferPool& buffers,}if (cmd.hasPersistentGeometry()) {- // The mesh's buffers for geometry, the frame's pool for transforms.- // Only the second was written this frame: an unchanged mesh costs no- // upload, and drawing it twice costs one more transform.+ // gl_InstanceID restarts at zero every draw while the frame's+ // records are one continuous run, so a draw has to say where its own+ // run begins in them.+ if (blocks.instance) {+ const InstanceConstants at{cmd.vertex_byte_offset / MESH_INSTANCE_STRIDE, {}};+ device.setUniformBuffer(INSTANCE_CONSTANTS_BINDING,+ blocks.instance->write(&at, sizeof(at)));+ }device.setVertexBuffer(0, cmd.vertex_buffer, 0);- device.setVertexBuffer(1, buffers.vertexBuffer(LayoutId::MeshInstance),- cmd.vertex_byte_offset);device.setIndexBuffer(cmd.index_buffer);device.drawElementsInstanced(cmd.index_count, GfxDataType::UnsignedInt,diff --git a/src/esengine/renderer/draw/DrawList.hpp b/src/esengine/renderer/draw/DrawList.hppindex d17964ed6..5b7861c38 100644--- a/src/esengine/renderer/draw/DrawList.hpp+++ b/src/esengine/renderer/draw/DrawList.hpp@@ -52,9 +52,7 @@ public:void execute(GfxDevice& device, TransientBufferPool& buffers,MaterialStore& materials, u32 white_texture_id = 0,FrameCapture* capture = nullptr,- PerDrawBlocks* skin_blocks = nullptr,- PerDrawBlocks* morph_blocks = nullptr,- PerDrawBlocks* probe_blocks = nullptr);+ const PerDrawBlockSet& blocks = {});/** Append one draw's bone matrices; returns where they start. Frame-scoped,* like the vertex pool: a command references an offset, not a copy. */diff --git a/src/esengine/renderer/frame/RenderContext.cpp b/src/esengine/renderer/frame/RenderContext.cppindex 1796ae50e..95c4924d2 100644--- a/src/esengine/renderer/frame/RenderContext.cpp+++ b/src/esengine/renderer/frame/RenderContext.cpp@@ -17,6 +17,7 @@#include "../draw/DrawParams.hpp"#include "../store/SkinConstants.hpp"#include "../store/MorphConstants.hpp"+#include "../store/InstanceConstants.hpp"#include "../store/ProbeConstants.hpp"#include "../../core/Log.hpp"@@ -155,10 +156,12 @@ void RenderContext::initFrameUbo() {// says about every object in it, and GL wants the whole declared block bound.probeBlocks_.init(device_,static_cast<u32>(sizeof(ProbeConstants)) * PROBE_MAX_INSTANCES);+ instanceBlocks_.init(device_, static_cast<u32>(sizeof(InstanceConstants)));// What a draw with no shapes and no volume reads. A pose has no zero: geometry// without one is drawn by a shader that does not declare the block.device_.setUniformBuffer(MORPH_CONSTANTS_BINDING, morphBlocks_.zero());device_.setUniformBuffer(PROBE_CONSTANTS_BINDING, probeBlocks_.zero());+ device_.setUniformBuffer(INSTANCE_CONSTANTS_BINDING, instanceBlocks_.zero());ES_LOG_DEBUG("FrameConstants UBO created (handle: {})", static_cast<u32>(frameUbo_));}diff --git a/src/esengine/renderer/frame/RenderContext.hpp b/src/esengine/renderer/frame/RenderContext.hppindex e5ed1f60f..f2f689ed6 100644--- a/src/esengine/renderer/frame/RenderContext.hpp+++ b/src/esengine/renderer/frame/RenderContext.hpp@@ -216,20 +216,26 @@ private:BufferHandle frameUbo_ = BufferHandle::Invalid;BufferHandle timeUbo_ = BufferHandle::Invalid;BufferHandle drawParamsFallback_ = BufferHandle::Invalid;- /// A bone-matrix block per skinned draw, a shape-weight block per morphed one,- /// and an indirect-light block per draw standing in a volume. One buffer EACH- /// and not one shared: see PerDrawBlocks.+ /// A block each per skinned draw, per morphed one, per draw standing in a+ /// volume, and per draw reading the record texture. One buffer EACH and not+ /// one shared: see PerDrawBlocks.PerDrawBlocks skinBlocks_;PerDrawBlocks morphBlocks_;PerDrawBlocks probeBlocks_;+ PerDrawBlocks instanceBlocks_;public:- /** @brief The pool a skinned draw takes its pose buffer from. */- PerDrawBlocks& skinBlocks() { return skinBlocks_; }- /** @brief The pool a morphed draw takes its shape buffer from. */- PerDrawBlocks& morphBlocks() { return morphBlocks_; }- /** @brief The pool a draw inside a probe volume takes its light buffer from. */- PerDrawBlocks& probeBlocks() { return probeBlocks_; }+ /** @brief The blocks the draw loop rewrites before each draw. */+ PerDrawBlockSet perDrawBlocks() {+ return {&skinBlocks_, &morphBlocks_, &probeBlocks_, &instanceBlocks_};+ }+ /** @brief Hand every one of them back at the top of a frame. */+ void beginPerDrawBlocks() {+ skinBlocks_.beginFrame();+ morphBlocks_.beginFrame();+ probeBlocks_.beginFrame();+ instanceBlocks_.beginFrame();+ }private:f32 lastElapsed_ = 0.0f;diff --git a/src/esengine/renderer/frame/RenderFrame.cpp b/src/esengine/renderer/frame/RenderFrame.cppindex 5abc90994..f11a8a8ac 100644--- a/src/esengine/renderer/frame/RenderFrame.cpp+++ b/src/esengine/renderer/frame/RenderFrame.cpp@@ -288,8 +288,7 @@ void RenderFrame::drawScene() {{ES_PROFILE_SCOPE("render.submit");draw_list_.execute(device_, pool_, context_.materials(), context_.getWhiteTextureId(),- &frame_capture_, &context_.skinBlocks(), &context_.morphBlocks(),- &context_.probeBlocks());+ &frame_capture_, context_.perDrawBlocks());}// Handed over to whatever the graph runs next. Blend/depth/colour-mask come@@ -333,9 +332,7 @@ void RenderFrame::beginFrame() {// The per-draw blocks go back at the FRAME boundary and not at each pass's:// a frame reaches the device as several passes with one submission behind// them, so a buffer reused between two of them is read by both.- context_.skinBlocks().beginFrame();- context_.morphBlocks().beginFrame();- context_.probeBlocks().beginFrame();+ context_.beginPerDrawBlocks();}void RenderFrame::applySceneDepthNeed() {@@ -685,8 +682,7 @@ void RenderFrame::replayToDrawCall(i32 stopAtDrawCall) {context_.updateCameraConstants(view_projection_);context_.lights().uploadAndBind();draw_list_.execute(device_, pool_, context_.materials(), context_.getWhiteTextureId(),- &frame_capture_, &context_.skinBlocks(), &context_.morphBlocks(),- &context_.probeBlocks());+ &frame_capture_, context_.perDrawBlocks());// Leave scissor disabled for whatever renders next; invalidate so the next// setPipeline re-applies its full state (stencil included).@@ -759,8 +755,7 @@ void RenderFrame::renderSurface(ecs::Registry& registry, const glm::mat4& viewPrcontext_.updateCameraConstants(viewProjection);context_.lights().uploadAndBind();draw_list_.execute(device_, pool_, context_.materials(), context_.getWhiteTextureId(),- &frame_capture_, &context_.skinBlocks(), &context_.morphBlocks(),- &context_.probeBlocks());+ &frame_capture_, context_.perDrawBlocks());frame_capture_.endCapture();rt->unbind();@@ -1748,8 +1743,7 @@ void RenderFrame::executeShadowPass(ecs::Registry& registry) {// nothing — the whole pass came back empty for exactly this.context_.lights().uploadAndBind();list.execute(device_, shadow_pool_, context_.materials(),- context_.getWhiteTextureId(), nullptr, &context_.skinBlocks(),- &context_.morphBlocks(), &context_.probeBlocks());+ context_.getWhiteTextureId(), nullptr, context_.perDrawBlocks());++shadowTiles;shadowDraws += list.mergedDrawCallCount();}diff --git a/src/esengine/renderer/frame/RenderFrameOverlay.cpp b/src/esengine/renderer/frame/RenderFrameOverlay.cppindex 2ec913946..3b1e8dd7d 100644--- a/src/esengine/renderer/frame/RenderFrameOverlay.cpp+++ b/src/esengine/renderer/frame/RenderFrameOverlay.cpp@@ -80,8 +80,7 @@ void RenderFrame::drawScreenOverlay() {{ES_PROFILE_SCOPE("render.overlay.submit");draw_list_.execute(device_, pool_, context_.materials(), context_.getWhiteTextureId(),- &frame_capture_, &context_.skinBlocks(), &context_.morphBlocks(),- &context_.probeBlocks());+ &frame_capture_, context_.perDrawBlocks());}device_.invalidatePipelineCache();device_.setScissorTest(false);diff --git a/src/esengine/renderer/plugins/MeshPlugin.cpp b/src/esengine/renderer/plugins/MeshPlugin.cppindex 646097e1a..aa1443a0e 100644--- a/src/esengine/renderer/plugins/MeshPlugin.cpp+++ b/src/esengine/renderer/plugins/MeshPlugin.cpp@@ -663,10 +663,13 @@ void MeshPlugin::collect(RenderCollectContext& collect_ctx) {// the record is read through that layout, so the fact that built// it must pack it. (It already excludes a skinned mesh.)const bool lightmapped = resident->hasLightmapUV;- const u32 stride = meshInstanceStride(skinned, resident->hasNormals, lightmapped);- u32 instOffset = buffers.allocVertices(LayoutId::MeshInstance, stride);+ u32 instOffset = buffers.allocVertices(LayoutId::MeshInstance,+ MESH_INSTANCE_STRIDE);auto* dst = buffers.vertexData(LayoutId::MeshInstance) + instOffset;- u32 tintRGBA = packColor(mesh.color);+ // The record is one fixed shape, so every texel this variant+ // does not write still has to say something. Zero is the answer+ // each of them reads as absent.+ std::memset(dst, 0, MESH_INSTANCE_STRIDE);// The shapes, whether or not bones move it: the two deform the// same vertices, and a morph target is what the mesh is BEFORE a// pose is applied to it.@@ -675,8 +678,8 @@ void MeshPlugin::collect(RenderCollectContext& collect_ctx) {key.morphIndex = draw_list.addMorphShapes(shapes);key.morphTextureId = static_cast<u32>(resident->morphTexture);}+ std::memcpy(dst + MESH_INSTANCE_TEXEL_TINT * 16, &mesh.color, 16);if (skinned) {- std::memcpy(dst, &tintRGBA, 4);key.skinOffset = draw_list.addSkinMatrices(pose_scratch_.data(), poseSize);key.skinCount = poseSize;} else {@@ -687,15 +690,14 @@ void MeshPlugin::collect(RenderCollectContext& collect_ctx) {// the one an affine transform never varies.for (u32 row = 0; row < 3; ++row) {const glm::vec4 r{model[0][row], model[1][row], model[2][row], model[3][row]};- std::memcpy(dst + row * 16, &r, 16);+ std::memcpy(dst + (MESH_INSTANCE_TEXEL_MODEL + row) * 16, &r, 16);}- std::memcpy(dst + 48, &tintRGBA, 4);if (resident->hasNormals) {// Written per object rather than derived per vertex: this is// the transform a normal takes under a non-uniform scale.const glm::mat3 nrm = glm::transpose(glm::inverse(glm::mat3(model)));for (u32 row = 0; row < 3; ++row) {- std::memcpy(dst + 52 + row * 12, &nrm[row][0], 12);+ std::memcpy(dst + (MESH_INSTANCE_TEXEL_NORMAL + row) * 16, &nrm[row][0], 12);}}if (lightmapped) {@@ -709,7 +711,7 @@ void MeshPlugin::collect(RenderCollectContext& collect_ctx) {key.lightmapTextureId = atlas->getId();}}- std::memcpy(dst + meshInstanceLightmapOffset(resident->hasNormals), &rect, 16);+ std::memcpy(dst + MESH_INSTANCE_TEXEL_LIGHTMAP * 16, &rect, 16);}}@@ -743,7 +745,7 @@ void MeshPlugin::collect(RenderCollectContext& collect_ctx) {key.shaderId = materialProgram != 0 ? materialProgram : residentShader;key.layoutId = LayoutId::MeshInstance;key.instanceCount = 1;- key.instanceStride = stride;+ key.instanceStride = MESH_INSTANCE_STRIDE;key.vertexBuffer = resident->vertexBuffer;key.indexBuffer = resident->indexBuffer;key.vertexLayout = resident->layout;diff --git a/src/esengine/renderer/rhi/GLDevice.cpp b/src/esengine/renderer/rhi/GLDevice.cppindex f033a4096..7ddae4ea8 100644--- a/src/esengine/renderer/rhi/GLDevice.cpp+++ b/src/esengine/renderer/rhi/GLDevice.cpp@@ -63,6 +63,9 @@#ifndef GL_RGBA16F#define GL_RGBA16F 0x881A#endif+#ifndef GL_RGBA32F+ #define GL_RGBA32F 0x8814+#endif#ifndef GL_HALF_FLOAT#define GL_HALF_FLOAT 0x140B#endif@@ -190,6 +193,7 @@ GLPixelFormatInfo toGLPixelFormat(GfxPixelFormat fmt) {case GfxPixelFormat::RGBA8: return { GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE };case GfxPixelFormat::SRGB8_ALPHA8: return { GL_SRGB8_ALPHA8, GL_RGBA, GL_UNSIGNED_BYTE };case GfxPixelFormat::RGBA16F: return { GL_RGBA16F, GL_RGBA, GL_HALF_FLOAT };+ case GfxPixelFormat::RGBA32F: return { GL_RGBA32F, GL_RGBA, GL_FLOAT };case GfxPixelFormat::DepthComponent24: return { GL_DEPTH_COMPONENT24, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT };case GfxPixelFormat::Depth24Stencil8: return { GL_DEPTH24_STENCIL8, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8 };default: return { GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE };diff --git a/src/esengine/renderer/rhi/GfxEnums.hpp b/src/esengine/renderer/rhi/GfxEnums.hppindex 97fd893e5..223ad1bb0 100644--- a/src/esengine/renderer/rhi/GfxEnums.hpp+++ b/src/esengine/renderer/rhi/GfxEnums.hpp@@ -93,22 +93,35 @@ enum class LayoutId : u8 {static constexpr u32 LAYOUT_COUNT = 5;-/// One per-object record for a resident mesh: three ROWS of an affine model-/// matrix, then a tint. The matrix's fourth row is (0,0,0,1) on every transform-/// a Transform can state, so it is neither sent nor given an attribute slot.-static constexpr u32 MESH_INSTANCE_STRIDE = 52;-/// The same plus a normal matrix (three vec3 rows), for geometry with normals.-/// Carried per object because a non-uniform scale makes the model matrix the-/// wrong transform for a normal, and inverting one per vertex is the alternative.-static constexpr u32 MESH_INSTANCE_STRIDE_LIT = 88;-/// A SKINNED object's record: the tint, and nothing else. No model matrix — a-/// skinned mesh's own transform is ignored (glTF says so) because its joints are-/// already placed in the world — and the pose itself is a uniform block.-static constexpr u32 MESH_INSTANCE_STRIDE_SKINNED = 4;-/// What a lightmapped object APPENDS: the atlas rectangle its second UV set is-/// read through (xy scale, zw offset). Per object, because the same mesh placed-/// twice is lit twice and occupies two patches of one atlas.-static constexpr u32 MESH_INSTANCE_LIGHTMAP_BYTES = 16;+/// One per-object record for a resident mesh, in texels of the frame's RGBA32F+/// instance texture. FIXED at one shape for every variant, so no stride has to be+/// derived by the layout, the allocation and the packer separately.+/// @see MESH_INSTANCE_TEXEL_MODEL and the offsets below.+static constexpr u32 MESH_INSTANCE_TEXELS = 8;+/// The model matrix's three ROWS, translation in .w. Its fourth row is (0,0,0,1)+/// on every transform a Transform can state, so it is not sent.+static constexpr u32 MESH_INSTANCE_TEXEL_MODEL = 0;+static constexpr u32 MESH_INSTANCE_TEXEL_TINT = 3;+/// The normal matrix's three rows in .xyz. Per object because a non-uniform scale+/// makes the model matrix the wrong transform for a normal.+static constexpr u32 MESH_INSTANCE_TEXEL_NORMAL = 4;+/// The bake's atlas rectangle (xy scale, zw offset). Per object, because the same+/// mesh placed twice is lit twice and occupies two patches of one atlas.+static constexpr u32 MESH_INSTANCE_TEXEL_LIGHTMAP = 7;+/// Bytes one record occupies. RGBA32F: a world-space translation is what this+/// carries, and half precision loses whole units of it a thousand away.+static constexpr u32 MESH_INSTANCE_STRIDE = MESH_INSTANCE_TEXELS * 16;+/// Texels per row of the instance texture. A multiple of the record, so no+/// record straddles a row; 2048 is the smallest MAX_TEXTURE_SIZE WebGL2 grants.+static constexpr u32 MESH_INSTANCE_TEXTURE_WIDTH = 2048;+/// Records the texture holds. Beyond it the frame's tail draws unplaced, which+/// is visible; a texture the device refuses would draw nothing at all.+static constexpr u32 MESH_INSTANCE_MAX_RECORDS =+ MESH_INSTANCE_TEXTURE_WIDTH * MESH_INSTANCE_TEXTURE_WIDTH / MESH_INSTANCE_TEXELS;+/// The unit the frame's record texture binds on. Not one of the eight a draw's+/// own stream can claim, because it is FRAME state; and the TOP of what remains,+/// since a material's textures are handed units upward from unit 8.+static constexpr u32 MESH_INSTANCE_TEXTURE_UNIT = 15;/// Bone matrices one skinned draw may carry. 64 mat4 is 4KB, inside the 16KB a/// WebGL2 uniform block is guaranteed; a mesh wanting more is drawn static.static constexpr u32 MESH_MAX_BONES = 64;@@ -123,34 +136,10 @@ static constexpr u32 MORPH_TEXTURE_WIDTH = 2048;/// Beyond it the geometry draws unmorphed — a texture the device refuses is a/// mesh that does not draw at all.static constexpr u32 MORPH_MAX_TEXELS = MORPH_TEXTURE_WIDTH * MORPH_TEXTURE_WIDTH;-/// The attributes that record occupies (4 matrix rows + the tint + the atlas-/// rectangle) — the budget check, so the widest record is the one measured.-static constexpr u32 MESH_INSTANCE_ATTRIBUTES = 6;/// Programs the resident-mesh shader has: one per combination of {normals, lit,/// normal-mapped, skinned, depth-only, env-mapped, lightmapped}. Seven bits, not/// one: the geometry, the draw, the frame and the pass each answer a different.static constexpr u32 MESH_VARIANTS = 128;-/// Where those attributes start. FIXED, not "after the mesh's channels": a mesh-/// that gains normals would otherwise move them, and every mesh shader with it.-static constexpr u32 MESH_INSTANCE_FIRST_LOCATION = 8;--/**- * @brief How many bytes one object's record is, for the shape it is drawn in.- * @details One author for a number three places need — the layout, the- * allocation and the packer — so a part added to the record cannot- * reach two of them and miss the third.- */-constexpr u32 meshInstanceStride(bool skinned, bool hasNormals, bool lightmapped) {- if (skinned) return MESH_INSTANCE_STRIDE_SKINNED;- return (hasNormals ? MESH_INSTANCE_STRIDE_LIT : MESH_INSTANCE_STRIDE)- + (lightmapped ? MESH_INSTANCE_LIGHTMAP_BYTES : 0);-}--/** @brief Where the atlas rectangle sits: appended, so nothing before it moves. */-constexpr u32 meshInstanceLightmapOffset(bool hasNormals) {- return hasNormals ? MESH_INSTANCE_STRIDE_LIT : MESH_INSTANCE_STRIDE;-}-/*** @brief What a mesh vertex channel MEANS, and the attribute location it binds to.* @details One vocabulary for the file format, the layout and the shaders: a@@ -167,8 +156,7 @@ enum class MeshChannel : u8 {Joints = 5,Weights = 6,/// A second UV set — what a bake is read through, laid out so no two of a- /// model's surfaces overlap. Location 7 is the last before the per-object- /// record at MESH_INSTANCE_FIRST_LOCATION, which is why that is fixed.+ /// model's surfaces overlap.TexCoord1 = 7,};@@ -284,6 +272,10 @@ enum class GfxPixelFormat : u8 {/// Half-float HDR target; gate render-target use on/// GfxDevice::supportsFloatTargets (EXT_color_buffer_float on WebGL2).RGBA16F,+ /// Full-float storage, for values a half cannot hold — a world-space+ /// translation is one. Fetched by texel index only: filtering full floats is+ /// an optional device feature.+ RGBA32F,DepthComponent24,Depth24Stencil8,};@@ -296,6 +288,7 @@ inline u32 gfxBytesPerPixel(GfxPixelFormat fmt) {switch (fmt) {case GfxPixelFormat::RGB8: return 3;case GfxPixelFormat::RGBA16F: return 8;+ case GfxPixelFormat::RGBA32F: return 16;default: return 4;}}diff --git a/src/esengine/renderer/rhi/PerDrawBlocks.hpp b/src/esengine/renderer/rhi/PerDrawBlocks.hppindex 1c165f7a7..11a02ffe2 100644--- a/src/esengine/renderer/rhi/PerDrawBlocks.hpp+++ b/src/esengine/renderer/rhi/PerDrawBlocks.hpp@@ -65,4 +65,18 @@ private:BufferHandle zero_ = BufferHandle::Invalid;};+/**+ * @brief The uniform blocks a draw rewrites immediately before it.+ *+ * @details One parameter and not four: every caller passes the same set from the+ * same owner, so a block added to the vocabulary reaches the draw loop+ * without every call site having to learn its name.+ */+struct PerDrawBlockSet {+ PerDrawBlocks* skin = nullptr;+ PerDrawBlocks* morph = nullptr;+ PerDrawBlocks* probe = nullptr;+ PerDrawBlocks* instance = nullptr;+};+} // namespace esenginediff --git a/src/esengine/renderer/rhi/Shader.cpp b/src/esengine/renderer/rhi/Shader.cppindex a44c40eb5..ae511dfbd 100644--- a/src/esengine/renderer/rhi/Shader.cpp+++ b/src/esengine/renderer/rhi/Shader.cpp@@ -20,6 +20,7 @@#include "../store/MaterialConstants.hpp"#include "../store/SkinConstants.hpp"#include "../store/LightConstants.hpp"+#include "../store/InstanceConstants.hpp"#include "../store/MorphConstants.hpp"#include "../store/ProbeConstants.hpp"#include "../../core/Log.hpp"@@ -269,6 +270,13 @@ bool Shader::compile(const std::string& vertexSrc, const std::string& fragmentSrdevice_->uniformBlockBinding(program_, probeBlock, PROBE_CONSTANTS_BINDING);}+ // And where this draw's objects start in the frame's record texture, which+ // DrawList rewrites per draw for the same reason.+ u32 instanceBlock = device_->getUniformBlockIndex(program_, INSTANCE_CONSTANTS_BLOCK);+ if (instanceBlock != GFX_INVALID_UNIFORM_BLOCK) {+ device_->uniformBlockBinding(program_, instanceBlock, INSTANCE_CONSTANTS_BINDING);+ }+// Same for the per-draw params block (rewriteLooseUniforms generates it for// shaders whose loose uniforms were lifted); commitParams binds the UBO.u32 drawParamsBlock = device_->getUniformBlockIndex(program_, DRAW_PARAMS_BLOCK);@@ -276,34 +284,26 @@ bool Shader::compile(const std::string& vertexSrc, const std::string& fragmentSrdevice_->uniformBlockBinding(program_, drawParamsBlock, DRAW_PARAMS_BINDING);}- // Engine-injected samplers sit on fixed units, pinned here for the same reason the- // blocks above are: the header reaches every Lit shader, so a compile site that- // forgot the unit would sample slot 0 silently. GLSL ES 300 has no layout(binding).- if (hasUniform(SHADOW_MAP_SAMPLER) || hasUniform(ENV_MAP_SAMPLER)- || hasUniform(SHADOW_2D_SAMPLER) || hasUniform(SHAPE_2D_SAMPLER)- || hasUniform(MORPH_DELTA_SAMPLER) || hasUniform(LIGHTMAP_SAMPLER)) {- bind();- if (hasUniform(SHADOW_MAP_SAMPLER)) {- setUniform(SHADOW_MAP_SAMPLER, static_cast<i32>(SHADOW_MAP_TEXTURE_UNIT));- }- if (hasUniform(ENV_MAP_SAMPLER)) {- setUniform(ENV_MAP_SAMPLER, static_cast<i32>(ENV_MAP_TEXTURE_UNIT));- }- if (hasUniform(SHADOW_2D_SAMPLER)) {- setUniform(SHADOW_2D_SAMPLER, static_cast<i32>(SHADOW_2D_TEXTURE_UNIT));- }- if (hasUniform(SHAPE_2D_SAMPLER)) {- setUniform(SHAPE_2D_SAMPLER, static_cast<i32>(SHAPE_2D_TEXTURE_UNIT));- }- // The one sampler here read by the VERTEX stage.- if (hasUniform(MORPH_DELTA_SAMPLER)) {- setUniform(MORPH_DELTA_SAMPLER, static_cast<i32>(MORPH_DELTA_TEXTURE_UNIT));- }- if (hasUniform(LIGHTMAP_SAMPLER)) {- setUniform(LIGHTMAP_SAMPLER, static_cast<i32>(LIGHTMAP_TEXTURE_UNIT));- }- unbind();+ // Engine-injected samplers sit on fixed units, pinned here because GLSL ES 300+ // has no layout(binding) and a compile site that forgot one samples slot 0+ // silently. The last two are read by the VERTEX stage.+ struct PinnedSampler { const char* name; u32 unit; };+ static const PinnedSampler kPinned[] = {+ {SHADOW_MAP_SAMPLER, SHADOW_MAP_TEXTURE_UNIT},+ {ENV_MAP_SAMPLER, ENV_MAP_TEXTURE_UNIT},+ {SHADOW_2D_SAMPLER, SHADOW_2D_TEXTURE_UNIT},+ {SHAPE_2D_SAMPLER, SHAPE_2D_TEXTURE_UNIT},+ {LIGHTMAP_SAMPLER, LIGHTMAP_TEXTURE_UNIT},+ {MORPH_DELTA_SAMPLER, MORPH_DELTA_TEXTURE_UNIT},+ {INSTANCE_DATA_SAMPLER, MESH_INSTANCE_TEXTURE_UNIT},+ };+ bool pinned = false;+ for (const PinnedSampler& p : kPinned) {+ if (!hasUniform(p.name)) continue;+ if (!pinned) { bind(); pinned = true; }+ setUniform(p.name, static_cast<i32>(p.unit));}+ if (pinned) unbind();ES_LOG_DEBUG("Shader compiled successfully (program handle: {}, active uniforms: {})",static_cast<u32>(program_), activeUniforms_.size());diff --git a/src/esengine/renderer/rhi/ShaderEmbeds.generated.hpp b/src/esengine/renderer/rhi/ShaderEmbeds.generated.hppindex ba1893284..2e253631e 100644--- a/src/esengine/renderer/rhi/ShaderEmbeds.generated.hpp+++ b/src/esengine/renderer/rhi/ShaderEmbeds.generated.hpp@@ -412,30 +412,9 @@ layout(location = 7) in vec2 a_texCoord1;-#ifndef SKINNED-layout(location = 8) in vec4 a_model0;-layout(location = 9) in vec4 a_model1;-layout(location = 10) in vec4 a_model2;-#ifdef MESH_LIGHTMAP----layout(location = 11) in vec4 a_lightmapRect;-#endif-#endif-layout(location = 12) in vec4 a_instTint;-#if defined(MESH_NORMALS) && !defined(SKINNED)----layout(location = 13) in vec3 a_nrm0;-layout(location = 14) in vec3 a_nrm1;-layout(location = 15) in vec3 a_nrm2;-#endif-#ifdef SKINNEDlayout(std140) uniform SkinConstants {@@ -520,18 +499,16 @@ void main() {+ a_weights.w * u_bones[a_joints.w];vec4 world = skin * vec4(local, 1.0);#else- mat4 model = mat4(vec4(a_model0.x, a_model1.x, a_model2.x, 0.0),- vec4(a_model0.y, a_model1.y, a_model2.y, 0.0),- vec4(a_model0.z, a_model1.z, a_model2.z, 0.0),- vec4(a_model0.w, a_model1.w, a_model2.w, 1.0));+ mat4 model = esInstanceModel();vec4 world = model * vec4(local, 1.0);#endifgl_Position = u_projection * world;v_texCoord = a_texCoord;- v_color = a_color * a_instTint;+ v_color = a_color * esInstanceTint();#ifdef MESH_LIGHTMAP- v_lightmap = vec3(a_texCoord1 * a_lightmapRect.xy + a_lightmapRect.zw,- a_lightmapRect.x > 0.0 ? 1.0 : 0.0);+ highp vec4 lmRect = esInstanceLightmapRect();+ v_lightmap = vec3(a_texCoord1 * lmRect.xy + lmRect.zw,+ lmRect.x > 0.0 ? 1.0 : 0.0);#endif#ifdef SHADOW_DEPTHv_shadowClip = gl_Position;@@ -543,7 +520,7 @@ void main() {#if defined(MESH_NORMALS) && defined(SKINNED)v_worldNormal = mat3(skin) * localNormal;#elif defined(MESH_NORMALS)- v_worldNormal = mat3(a_nrm0, a_nrm1, a_nrm2) * localNormal;+ v_worldNormal = esInstanceNormalMatrix() * localNormal;#elsev_worldNormal = vec3(0.0, 0.0, 1.0);@@ -652,27 +629,12 @@ struct VSIn {#ifdef SKINNED@location(5) a_joints : vec4u,@location(6) a_weights : vec4f,-#else- @location(8) a_model0 : vec4f,- @location(9) a_model1 : vec4f,- @location(10) a_model2 : vec4f,-#ifdef MESH_LIGHTMAP- @location(11) a_lightmapRect : vec4f,-#endif#endif#ifdef MESH_LIGHTMAP@location(7) a_texCoord1 : vec2f,#endif- @location(12) a_instTint : vec4f,@builtin(vertex_index) vertexIndex : u32,-#ifdef MESH_NORMALS-#ifndef SKINNED- @location(13) a_nrm0 : vec3f,- @location(14) a_nrm1 : vec3f,- @location(15) a_nrm2 : vec3f,-#endif-#endif};struct VSOut {@builtin(position) pos : vec4f,@@ -695,6 +657,7 @@ struct VSOut {};@vertex fn vs_main(v : VSIn, @builtin(instance_index) inst : u32) -> VSOut {+ g_instanceId = i32(inst);var local = v.a_position;#ifdef MESH_NORMALSvar localNormal = v.a_normal;@@ -721,20 +684,17 @@ struct VSOut {+ v.a_weights.w * skin.bones[v.a_joints.w];let world = pose * vec4f(local, 1.0);#else- let model = mat4x4f(vec4f(v.a_model0.x, v.a_model1.x, v.a_model2.x, 0.0),- vec4f(v.a_model0.y, v.a_model1.y, v.a_model2.y, 0.0),- vec4f(v.a_model0.z, v.a_model1.z, v.a_model2.z, 0.0),- vec4f(v.a_model0.w, v.a_model1.w, v.a_model2.w, 1.0));- let world = model * vec4f(local, 1.0);+ let world = esInstanceModel() * vec4f(local, 1.0);#endifvar out : VSOut;out.pos = frame.projection * world;out.v_texCoord = v.a_texCoord;- out.v_color = v.a_color * v.a_instTint;+ out.v_color = v.a_color * esInstanceTint();#ifdef MESH_LIGHTMAP- out.v_lightmap = vec3f(v.a_texCoord1 * v.a_lightmapRect.xy + v.a_lightmapRect.zw,- select(0.0, 1.0, v.a_lightmapRect.x > 0.0));+ let lmRect = esInstanceLightmapRect();+ out.v_lightmap = vec3f(v.a_texCoord1 * lmRect.xy + lmRect.zw,+ select(0.0, 1.0, lmRect.x > 0.0));#endif#ifdef SHADOW_DEPTHout.v_shadowClip = out.pos;@@ -746,7 +706,7 @@ struct VSOut {#ifdef SKINNEDout.v_worldNormal = mat3x3f(pose[0].xyz, pose[1].xyz, pose[2].xyz) * localNormal;#else- out.v_worldNormal = mat3x3f(v.a_nrm0, v.a_nrm1, v.a_nrm2) * localNormal;+ out.v_worldNormal = esInstanceNormalMatrix() * localNormal;#endif#elsediff --git a/src/esengine/renderer/rhi/Texture.cpp b/src/esengine/renderer/rhi/Texture.cppindex 3c0038cb3..fc017a7ed 100644--- a/src/esengine/renderer/rhi/Texture.cpp+++ b/src/esengine/renderer/rhi/Texture.cpp@@ -30,6 +30,7 @@ GfxPixelFormat toGfxPixelFormat(TextureFormat format) {case TextureFormat::RGBA8: return GfxPixelFormat::RGBA8;case TextureFormat::SRGB8A8: return GfxPixelFormat::SRGB8_ALPHA8;case TextureFormat::RGBA16F: return GfxPixelFormat::RGBA16F;+ case TextureFormat::RGBA32F: return GfxPixelFormat::RGBA32F;case TextureFormat::Depth24: return GfxPixelFormat::DepthComponent24;default: return GfxPixelFormat::RGBA8;}diff --git a/src/esengine/renderer/rhi/Texture.hpp b/src/esengine/renderer/rhi/Texture.hppindex 5fde4ea0d..cda6927c8 100644--- a/src/esengine/renderer/rhi/Texture.hpp+++ b/src/esengine/renderer/rhi/Texture.hpp@@ -47,6 +47,7 @@ enum class TextureFormat {RGBA8, ///< 4 channels, 8 bits each (32 bpp)SRGB8A8, ///< sRGB-encoded RGBA8: hardware-linearized on sample (32 bpp)RGBA16F, ///< Half-float HDR (64 bpp); render use gated on supportsFloatTargets+ RGBA32F, ///< Full-float storage (128 bpp); fetched by texel, never filteredDepth24 ///< Depth buffer format (24 bits)};diff --git a/src/esengine/renderer/rhi/TransientBufferPool.cpp b/src/esengine/renderer/rhi/TransientBufferPool.cppindex 1389e7cc8..cacaee9fb 100644--- a/src/esengine/renderer/rhi/TransientBufferPool.cpp+++ b/src/esengine/renderer/rhi/TransientBufferPool.cpp@@ -40,6 +40,8 @@ void TransientBufferPool::recreateGpuResources() {s.ebo_capacity = 0;s.vertex_write_pos = 0;s.index_write_pos = 0;+ s.texture = TextureHandle::Invalid;+ s.texture_rows = 0;}initialized_ = false;init(initial_vertex_bytes_, initial_index_count_);@@ -56,6 +58,11 @@ void TransientBufferPool::shutdown() {if (s.vbo != BufferHandle::Invalid) { device_.deleteBuffer(s.vbo); s.vbo = BufferHandle::Invalid; }if (s.ebo != BufferHandle::Invalid) { device_.deleteBuffer(s.ebo); s.ebo = BufferHandle::Invalid; }if (s.quad_vbo != BufferHandle::Invalid) { device_.deleteBuffer(s.quad_vbo); s.quad_vbo = BufferHandle::Invalid; }+ if (s.texture != TextureHandle::Invalid) {+ device_.deleteTexture(s.texture);+ s.texture = TextureHandle::Invalid;+ }+ s.texture_rows = 0;s.vertex_staging.clear();s.index_staging.clear();s.vertex_write_pos = 0;@@ -81,6 +88,47 @@ const TransientBufferPool::Stream& TransientBufferPool::stream(LayoutId layout)return streams_[static_cast<u32>(layout)];}+/**+ * @brief The frame's records onto the GPU, as whole rows.+ *+ * @details Whole rows because a partial one asks the upload to describe a+ * rectangle the staging is not laid out as. The record divides the row+ * evenly, so a row boundary never falls inside one.+ */+void TransientBufferPool::uploadInstanceRows(Stream& s, u32& grows, u32& writes) {+ const u32 rowBytes = instanceRowBytes();+ const u32 rows = (s.vertex_write_pos + rowBytes - 1) / rowBytes;+ if (static_cast<u64>(s.vertex_staging.size()) < static_cast<u64>(rows) * rowBytes) {+ s.vertex_staging.resize(static_cast<usize>(rows) * rowBytes);+ }+ if (rows > s.texture_rows) {+ // Doubling, the way the staging grows: a workload creeping upward+ // otherwise reallocates a texture every frame.+ const u32 want = std::max(rows, s.texture_rows * 2);+ if (s.texture != TextureHandle::Invalid) device_.deleteTexture(s.texture);+ TextureDesc desc;+ desc.width = MESH_INSTANCE_TEXTURE_WIDTH;+ desc.height = want;+ desc.format = GfxPixelFormat::RGBA32F;+ // Fetched by index, never filtered: a value between two records belongs+ // to no object, and full floats are not filterable everywhere anyway.+ desc.minFilter = TextureFilter::Nearest;+ desc.magFilter = TextureFilter::Nearest;+ desc.wrapS = TextureWrap::ClampToEdge;+ desc.wrapT = TextureWrap::ClampToEdge;+ desc.mipmaps = false;+ s.texture = device_.createTexture(desc, s.vertex_staging.data());+ s.texture_rows = s.texture == TextureHandle::Invalid ? 0 : want;+ ++grows;+ return;+ }+ // No flip: this is a record store addressed by index, not an image, so row 0+ // has to stay the row the writer wrote first.+ device_.updateTexture(s.texture, 0, 0, MESH_INSTANCE_TEXTURE_WIDTH, rows,+ s.vertex_staging.data(), /*flipY=*/false);+ ++writes;+}+u32 TransientBufferPool::allocVertices(LayoutId layout, u32 byteSize) {Stream& s = stream(layout);u32 offset = s.vertex_write_pos;@@ -131,6 +179,13 @@ void TransientBufferPool::upload() {// Growth goes through resizeBuffer, which keeps the handle stable, so per-draw// buffer bindings and the backend's cached vertex state stay valid.for (auto& s : streams_) {+ if (&s == &stream(LayoutId::MeshInstance)) {+ if (s.vertex_write_pos == 0) continue;+ ++streams;+ vertexBytes += s.vertex_write_pos;+ uploadInstanceRows(s, grows, writes);+ continue;+ }if (s.vbo == BufferHandle::Invalid) continue;if (s.vertex_write_pos == 0 && s.index_write_pos == 0) continue;++streams;@@ -217,13 +272,12 @@ void TransientBufferPool::setupStream(LayoutId layout) {Stream& s = stream(layout);if (layout == LayoutId::MeshInstance) {- // Only a stream. The geometry is the mesh's and so is the layout — this- // holds the per-object transforms written for the frame, which is the one- // part of drawing a resident mesh that is not resident.- s.vertex_staging.resize(initial_vertex_bytes_);- s.vbo_capacity = initial_vertex_bytes_;- s.vbo = device_.createBuffer({GfxBufferUsage::Vertex, s.vbo_capacity, /*dynamic=*/true}, nullptr);+ // Only a stream, and not a vertex one: the frame's per-object records go+ // in a texture, so a shader can reach any record by index rather than only+ // the one the hardware happens to be on.s.vertex_stride = MESH_INSTANCE_STRIDE;+ s.vertex_staging.resize(instanceRowBytes());+ s.texture_rows = 0;return;}diff --git a/src/esengine/renderer/rhi/TransientBufferPool.hpp b/src/esengine/renderer/rhi/TransientBufferPool.hppindex 852f35199..ffdd8ea6f 100644--- a/src/esengine/renderer/rhi/TransientBufferPool.hpp+++ b/src/esengine/renderer/rhi/TransientBufferPool.hpp@@ -75,12 +75,20 @@ public:BufferHandle vertexBuffer(LayoutId layout) const;BufferHandle indexBuffer(LayoutId layout) const;+ /** @brief The frame's per-object records. Invalid until a frame writes some. */+ TextureHandle instanceTexture() const { return stream(LayoutId::MeshInstance).texture; }+private:struct Stream {BufferHandle vbo = BufferHandle::Invalid; // for ParticleInstance: the per-instance (streamed) bufferBufferHandle ebo = BufferHandle::Invalid;VertexLayoutHandle layout = VertexLayoutHandle::Invalid;BufferHandle quad_vbo = BufferHandle::Invalid; // ParticleInstance only: static unit-quad geometry (slot 0)+ /// MeshInstance only: the frame's records live in a texture, because a+ /// vertex attribute can only be read at the instance the hardware is on+ /// and a record has to be reachable by index.+ TextureHandle texture = TextureHandle::Invalid;+ u32 texture_rows = 0; ///< Rows the texture currently holds.std::vector<u8> vertex_staging;std::vector<u32> index_staging; // 32-bit indices: a single Batch stream can exceed 65535 verticesu32 vertex_stride = 0;@@ -91,6 +99,8 @@ private:};void setupStream(LayoutId layout);+ void uploadInstanceRows(Stream& s, u32& grows, u32& writes);+ static constexpr u32 instanceRowBytes() { return MESH_INSTANCE_TEXTURE_WIDTH * 16; }void growVertexStaging(Stream& s, u32 requiredBytes);void growIndexStaging(Stream& s, u32 requiredCount);diff --git a/src/esengine/renderer/store/InstanceConstants.hpp b/src/esengine/renderer/store/InstanceConstants.hppnew file mode 100644index 000000000..75291b29d--- /dev/null+++ b/src/esengine/renderer/store/InstanceConstants.hpp@@ -0,0 +1,44 @@+// SPDX-License-Identifier: Apache-2.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+/**+ * @file InstanceConstants.hpp+ * @brief WHERE one draw's objects start in the frame's per-object record texture.+ *+ * @details Per DRAW and rewritten immediately before it, the way @ref ProbeConstants+ * and @ref MorphConstants are. `gl_InstanceID` restarts at zero every draw+ * while the frame's records are one continuous run, so a draw has to say+ * where its own run begins; a merge makes that run longer, never split.+ *+ * A uniform and not a vertex attribute: an attribute would cost the record+ * the very slot this whole carrier exists to give back.+ */+#pragma once++#include "../../core/Types.hpp"++namespace esengine {++/** @brief Indexed UBO binding point for the per-draw InstanceConstants block.+ * Nine and not four: slot 4 is DRAW_PARAMS_BINDING, which every shader whose+ * loose uniforms were lifted declares — two blocks on one binding is a pipeline+ * the device refuses without naming either. */+inline constexpr u32 INSTANCE_CONSTANTS_BINDING = 8;++/** @brief GLSL block name; must match the injected mesh header + Shader::compile lookup. */+inline constexpr const char* INSTANCE_CONSTANTS_BLOCK = "InstanceConstants";++/** @brief GLSL name of the record texture; pinned to its unit by Shader::compile. */+inline constexpr const char* INSTANCE_DATA_SAMPLER = "u_instanceData";++/** @brief The first record of one draw's run. */+struct InstanceConstants {+ u32 base = 0;+ /// std140 rounds a block up to a vec4 anyway; naming the rest keeps the C+++ /// size equal to what the shader's block occupies rather than nearly equal.+ u32 pad[3]{};+};++static_assert(sizeof(InstanceConstants) == 16,+ "InstanceConstants must be one vec4 to match std140");++} // namespace esenginediff --git a/src/esengine/renderer/webgpu/WebGPUDevice.cpp b/src/esengine/renderer/webgpu/WebGPUDevice.cppindex 41d104b47..f88ddae1f 100644--- a/src/esengine/renderer/webgpu/WebGPUDevice.cpp+++ b/src/esengine/renderer/webgpu/WebGPUDevice.cpp@@ -1475,9 +1475,13 @@ WGPUBindGroupLayout WebGPUDevice::groupLayoutFor(u32 group, u32 mask, u32 depthMWGPUBindGroupLayoutEntry e{};e.binding = tb;e.visibility = stages(tb);- e.texture.sampleType = (depthMask & (1u << tb))- ? WGPUTextureSampleType_Depth- : WGPUTextureSampleType_Float;+ // A unit with no sampler cannot be textureSample'd, so it is+ // unfilterable by construction — which is what lets rgba32float+ // bind here. Filterable formats satisfy it too.+ e.texture.sampleType =+ (depthMask & (1u << tb)) ? WGPUTextureSampleType_Depth+ : (mask & (1u << sb)) ? WGPUTextureSampleType_Float+ : WGPUTextureSampleType_UnfilterableFloat;e.texture.viewDimension = WGPUTextureViewDimension_2D;entries[count++] = e;}diff --git a/src/esengine/renderer/webgpu/WebGPUDevice.hpp b/src/esengine/renderer/webgpu/WebGPUDevice.hppindex 0da515ca5..523485677 100644--- a/src/esengine/renderer/webgpu/WebGPUDevice.hpp+++ b/src/esengine/renderer/webgpu/WebGPUDevice.hpp@@ -521,7 +521,10 @@ private:u32 current_pipeline_ = 0;u32 bound_index_buffer_ = 0;i32 stencil_ref_ = 0; ///< Last user-set reference (re-applied after internal quads).- static constexpr u32 kUniformSlots = 8;+ /// Nine: the eight the engine had, plus where a draw's objects start in the+ /// frame's record texture. WebGPU grants 12 uniform buffers a stage and+ /// WebGL2 grants 12 blocks a stage, so nine is inside both.+ static constexpr u32 kUniformSlots = 9;u32 uniform_slots_[kUniformSlots] = {}; ///< BufferHandle id per UBO binding slot./// Engine units 0..7 (batch multi-texture) + material-param units 8..15/// (== webgpu::kGroup1TextureUnits, static_asserted in the .cpp).diff --git a/src/esengine/renderer/webgpu/WebGPUMappings.hpp b/src/esengine/renderer/webgpu/WebGPUMappings.hppindex 542ea5426..c185b26c4 100644--- a/src/esengine/renderer/webgpu/WebGPUMappings.hpp+++ b/src/esengine/renderer/webgpu/WebGPUMappings.hpp@@ -160,6 +160,7 @@ inline WGPUTextureFormat toWGPUTextureFormat(GfxPixelFormat fmt) {case GfxPixelFormat::RGBA8: return WGPUTextureFormat_RGBA8Unorm;case GfxPixelFormat::SRGB8_ALPHA8: return WGPUTextureFormat_RGBA8UnormSrgb;case GfxPixelFormat::RGBA16F: return WGPUTextureFormat_RGBA16Float;+ case GfxPixelFormat::RGBA32F: return WGPUTextureFormat_RGBA32Float;case GfxPixelFormat::DepthComponent24: return WGPUTextureFormat_Depth24Plus;case GfxPixelFormat::Depth24Stencil8: return WGPUTextureFormat_Depth24PlusStencil8;default: return WGPUTextureFormat_RGBA8Unorm;@@ -168,7 +169,9 @@ inline WGPUTextureFormat toWGPUTextureFormat(GfxPixelFormat fmt) {/** Bytes per pixel of an uncompressed upload in @p fmt (RGB8 expands to RGBA8). */inline u32 wgpuBytesPerPixel(GfxPixelFormat fmt) {- return fmt == GfxPixelFormat::RGBA16F ? 8u : 4u;+ if (fmt == GfxPixelFormat::RGBA16F) return 8u;+ if (fmt == GfxPixelFormat::RGBA32F) return 16u;+ return 4u;}inline WGPUTextureFormat toWGPUCompressedFormat(GfxCompressedFormat fmt) {diff --git a/src/esengine/resource/ResourceManager.cpp b/src/esengine/resource/ResourceManager.cppindex 718f505d5..45edc004a 100644--- a/src/esengine/resource/ResourceManager.cpp+++ b/src/esengine/resource/ResourceManager.cpp@@ -502,9 +502,9 @@ bool ResourceManager::realizeMesh(Mesh& mesh, ConstSpan<u8> vertexBytes, ConstSpConstSpan<f32> inverseBind, const MeshMorphSource& morph) {if (!device_ || vertexBytes.empty() || indices.empty() || channels.empty()) return false;- // The mesh describes its own vertices; the per-object transform is the- // engine's and is appended here, so no caller has to know how a transform- // reaches the shader — the reason a mesh is drawn without touching its bytes.+ // The mesh describes its own vertices and nothing else: the per-object+ // transform reaches the shader through the frame's record texture, which is+ // what leaves every slot above the mesh channels free.VertexLayoutDesc layout;// A channel no shader reads stays out of the layout (meshShaderReads): it// would spend an attribute slot and a fetch per vertex. The stride is@@ -512,7 +512,7 @@ bool ResourceManager::realizeMesh(Mesh& mesh, ConstSpan<u8> vertexBytes, ConstSpu32 bound = 0;for (const GfxVertexAttribute& c : channels) {if (!meshShaderReads(static_cast<MeshChannel>(c.location))) continue;- if (bound + MESH_INSTANCE_ATTRIBUTES >= MAX_VERTEX_ATTRIBUTES) {+ if (bound >= MAX_VERTEX_ATTRIBUTES) {ES_LOG_ERROR("realizeMesh: {} bound channels exceeds the layout budget", bound + 1);return false;}@@ -535,31 +535,7 @@ bool ResourceManager::realizeMesh(Mesh& mesh, ConstSpan<u8> vertexBytes, ConstSpconst bool lightmapped = lightmapUV && !skinned;layout.strides[0] = vertexStride;- layout.strides[1] = meshInstanceStride(skinned, hasNormals, lightmapped);- layout.instanceStep[1] = true;- u32 next = bound;- // Only where the shader will read them: a layout may not declare an attribute- // its shader does not consume, which WebGPU rejects. A skinned record thus- // carries neither model nor normal matrix — its bones are world-space.- if (!skinned) {- for (u32 row = 0; row < 3; ++row) {- layout.attributes[next++] = {MESH_INSTANCE_FIRST_LOCATION + row, 4, GfxDataType::Float,- false, row * 16u, 1};- }- }- layout.attributes[next++] = {MESH_INSTANCE_FIRST_LOCATION + 4, 4, GfxDataType::UnsignedByte,- true, skinned ? 0u : 48u, 1};- if (hasNormals && !skinned) {- for (u32 row = 0; row < 3; ++row) {- layout.attributes[next++] = {MESH_INSTANCE_FIRST_LOCATION + 5 + row, 3,- GfxDataType::Float, false, 52 + row * 12u, 1};- }- }- if (lightmapped) {- layout.attributes[next++] = {MESH_INSTANCE_FIRST_LOCATION + 3, 4, GfxDataType::Float,- false, meshInstanceLightmapOffset(hasNormals), 1};- }- layout.attributeCount = next;+ layout.attributeCount = bound;// The old records go only once the new ones stand: a rematerialization that// fails half-way must leave the mesh as it found it, not stripped of thediff --git a/src/esengine/resource/ShaderParser.cpp b/src/esengine/resource/ShaderParser.cppindex cb646bf2e..01b6dd042 100644--- a/src/esengine/resource/ShaderParser.cpp+++ b/src/esengine/resource/ShaderParser.cpp@@ -13,7 +13,9 @@#include "ShaderParser.hpp"#include "../core/Log.hpp"+#include "../renderer/store/InstanceConstants.hpp"#include "../renderer/store/MaterialConstants.hpp"+#include "../renderer/rhi/GfxEnums.hpp"#include "../renderer/rhi/WgslBindings.hpp"#include <sstream>@@ -30,6 +32,79 @@ namespace esengine::resource {namespace {+/**+ * @brief How a vertex stage reaches its own object's record.+ *+ * @details Built from the layout constants rather than written out, so the texel+ * a shader reads and the texel the packer writes cannot drift apart.+ * `esInstance*` is the name a stage reaches for; a stage that names one+ * gets this header, the way a stage that names `tN` gets that unit.+ */+/** @brief The same record, for a WGSL vertex stage. @see instanceRecordGLSL */+std::string instanceRecordWGSL() {+ const std::string texels = std::to_string(MESH_INSTANCE_TEXELS);+ const std::string width = std::to_string(MESH_INSTANCE_TEXTURE_WIDTH);+ const std::string unit = std::to_string(MESH_INSTANCE_TEXTURE_UNIT);+ return+ "struct InstanceConstants { base : vec4u };\n"+ "@group(0) @binding(" + std::to_string(INSTANCE_CONSTANTS_BINDING)+ + ") var<uniform> inst : InstanceConstants;\n"+ // vs_main's instance_index, put where the helpers can reach it — WGSL has+ // no global for it the way GLSL has gl_InstanceID.+ "var<private> g_instanceId : i32 = 0;\n"+ "fn esInstanceTexel(texel : i32) -> vec4f {\n"+ " let at = (i32(inst.base.x) + g_instanceId) * " + texels + " + texel;\n"+ " return textureLoad(t" + unit + ", vec2i(at % " + width + ", at / " + width + "), 0);\n"+ "}\n"+ "fn esInstanceModel() -> mat4x4f {\n"+ " let r0 = esInstanceTexel(" + std::to_string(MESH_INSTANCE_TEXEL_MODEL) + ");\n"+ " let r1 = esInstanceTexel(" + std::to_string(MESH_INSTANCE_TEXEL_MODEL + 1) + ");\n"+ " let r2 = esInstanceTexel(" + std::to_string(MESH_INSTANCE_TEXEL_MODEL + 2) + ");\n"+ " return mat4x4f(vec4f(r0.x, r1.x, r2.x, 0.0), vec4f(r0.y, r1.y, r2.y, 0.0),\n"+ " vec4f(r0.z, r1.z, r2.z, 0.0), vec4f(r0.w, r1.w, r2.w, 1.0));\n"+ "}\n"+ "fn esInstanceNormalMatrix() -> mat3x3f {\n"+ " return mat3x3f(esInstanceTexel(" + std::to_string(MESH_INSTANCE_TEXEL_NORMAL) + ").xyz,\n"+ " esInstanceTexel(" + std::to_string(MESH_INSTANCE_TEXEL_NORMAL + 1) + ").xyz,\n"+ " esInstanceTexel(" + std::to_string(MESH_INSTANCE_TEXEL_NORMAL + 2) + ").xyz);\n"+ "}\n"+ "fn esInstanceTint() -> vec4f { return esInstanceTexel("+ + std::to_string(MESH_INSTANCE_TEXEL_TINT) + "); }\n"+ "fn esInstanceLightmapRect() -> vec4f { return esInstanceTexel("+ + std::to_string(MESH_INSTANCE_TEXEL_LIGHTMAP) + "); }\n";+}++std::string instanceRecordGLSL() {+ const std::string texels = std::to_string(MESH_INSTANCE_TEXELS);+ const std::string width = std::to_string(MESH_INSTANCE_TEXTURE_WIDTH);+ return+ "layout(std140) uniform InstanceConstants {\n"+ " highp uvec4 u_instanceBase;\n"+ "};\n"+ "uniform highp sampler2D u_instanceData;\n"+ "highp vec4 esInstanceTexel(int texel) {\n"+ " int at = (int(u_instanceBase.x) + ES_INSTANCE_ID) * " + texels + " + texel;\n"+ " return texelFetch(u_instanceData, ivec2(at % " + width + ", at / " + width + "), 0);\n"+ "}\n"+ "highp mat4 esInstanceModel() {\n"+ " highp vec4 r0 = esInstanceTexel(" + std::to_string(MESH_INSTANCE_TEXEL_MODEL) + ");\n"+ " highp vec4 r1 = esInstanceTexel(" + std::to_string(MESH_INSTANCE_TEXEL_MODEL + 1) + ");\n"+ " highp vec4 r2 = esInstanceTexel(" + std::to_string(MESH_INSTANCE_TEXEL_MODEL + 2) + ");\n"+ " return mat4(vec4(r0.x, r1.x, r2.x, 0.0), vec4(r0.y, r1.y, r2.y, 0.0),\n"+ " vec4(r0.z, r1.z, r2.z, 0.0), vec4(r0.w, r1.w, r2.w, 1.0));\n"+ "}\n"+ "highp mat3 esInstanceNormalMatrix() {\n"+ " return mat3(esInstanceTexel(" + std::to_string(MESH_INSTANCE_TEXEL_NORMAL) + ").xyz,\n"+ " esInstanceTexel(" + std::to_string(MESH_INSTANCE_TEXEL_NORMAL + 1) + ").xyz,\n"+ " esInstanceTexel(" + std::to_string(MESH_INSTANCE_TEXEL_NORMAL + 2) + ").xyz);\n"+ "}\n"+ "highp vec4 esInstanceTint() { return esInstanceTexel("+ + std::to_string(MESH_INSTANCE_TEXEL_TINT) + "); }\n"+ "highp vec4 esInstanceLightmapRect() { return esInstanceTexel("+ + std::to_string(MESH_INSTANCE_TEXEL_LIGHTMAP) + "); }\n";+}++constexpr u32 kMaxIncludeDepth = 16;std::string ltrim(const std::string& s) {@@ -178,32 +253,17 @@ std::string canonicalVertexStageWGSL(bool lit) {"#ifdef SKINNED\n"" @location(5) a_joints : vec4u,\n"" @location(6) a_weights : vec4f,\n"- " @location(12) a_instTint : vec4f,\n"- "#else\n"- "#ifdef MESH\n"- " @location(8) a_model0 : vec4f,\n"- " @location(9) a_model1 : vec4f,\n"- " @location(10) a_model2 : vec4f,\n"- "#ifdef MESH_LIGHTMAP\n"- " @location(11) a_lightmapRect : vec4f,\n"- "#endif\n"- " @location(12) a_instTint : vec4f,\n"- "#endif\n"- "#ifdef MESH_NORMALS\n"- " @location(13) a_nrm0 : vec3f,\n"- " @location(14) a_nrm1 : vec3f,\n"- " @location(15) a_nrm2 : vec3f,\n"- "#endif\n"- "#endif\n""#endif\n""#ifdef MESH\n"// What the GLSL twin reads as gl_VertexID." @builtin(vertex_index) vertexIndex : u32,\n""#endif\n"+ "#endif\n""};\n""\n"- "@vertex fn vs_main(v : VSIn) -> VSOut {\n"+ "@vertex fn vs_main(v : VSIn, @builtin(instance_index) instanceId : u32) -> VSOut {\n"" var out : VSOut;\n"+ " g_instanceId = i32(instanceId);\n""#ifdef MESH\n"" var local = v.a_position;\n""#ifdef MESH_NORMALS\n"@@ -247,12 +307,10 @@ std::string canonicalVertexStageWGSL(bool lit) {" + v.a_weights.z * skin.bones[v.a_joints.z]\n"" + v.a_weights.w * skin.bones[v.a_joints.w];\n"" let world = pose * vec4f(local, 1.0);\n"- " out.v_color = v.a_color * v.a_instTint;\n"+ " out.v_color = v.a_color * esInstanceTint();\n""#elif defined(MESH)\n"- " let world = vec4f(dot(v.a_model0, vec4f(local, 1.0)),\n"- " dot(v.a_model1, vec4f(local, 1.0)),\n"- " dot(v.a_model2, vec4f(local, 1.0)), 1.0);\n"- " out.v_color = v.a_color * v.a_instTint;\n"+ " let world = esInstanceModel() * vec4f(local, 1.0);\n"+ " out.v_color = v.a_color * esInstanceTint();\n""#else\n"" let world = vec4f(v.a_position, 1.0);\n"" out.v_color = v.a_color;\n"@@ -268,16 +326,20 @@ std::string canonicalVertexStageWGSL(bool lit) {"#ifdef SKINNED\n"" out.v_worldNormal = mat3x3f(pose[0].xyz, pose[1].xyz, pose[2].xyz) * localNormal;\n""#else\n"- " out.v_worldNormal = mat3x3f(v.a_nrm0, v.a_nrm1, v.a_nrm2) * localNormal;\n"+ " out.v_worldNormal = esInstanceNormalMatrix() * localNormal;\n""#endif\n"" out.v_worldXYZ = world.xyz;\n""#endif\n""#ifdef MESH_LIGHTMAP\n"- " out.v_lightmap = vec3f(v.a_texCoord1 * v.a_lightmapRect.xy + v.a_lightmapRect.zw,\n"- " select(0.0, 1.0, v.a_lightmapRect.x > 0.0));\n"+ " let lmRect = esInstanceLightmapRect();\n"+ " out.v_lightmap = vec3f(v.a_texCoord1 * lmRect.xy + lmRect.zw,\n"+ " select(0.0, 1.0, lmRect.x > 0.0));\n""#endif\n";if (lit) {src += " out.v_worldPos = world.xy;\n";+ // Declared by wgslVSOut under the same condition, so filled here —+ // a slot left at zero makes every instance read the first probe.+ src += " out.v_probeSlot = f32(instanceId);\n";}src +=" return out;\n"@@ -360,26 +422,10 @@ std::string canonicalVertexStage(bool lit) {"#ifdef SKINNED\n""layout(location = 5) in uvec4 a_joints;\n""layout(location = 6) in vec4 a_weights;\n"- "layout(location = 12) in vec4 a_instTint;\n"// This draw's pose, rewritten immediately before it (SkinConstants, binding 5)."layout(std140) uniform SkinConstants {\n"" mat4 u_bones[64];\n""};\n"- "#else\n"- "#ifdef MESH\n"- "layout(location = 8) in vec4 a_model0;\n"- "layout(location = 9) in vec4 a_model1;\n"- "layout(location = 10) in vec4 a_model2;\n"- "#ifdef MESH_LIGHTMAP\n"- "layout(location = 11) in vec4 a_lightmapRect;\n"- "#endif\n"- "layout(location = 12) in vec4 a_instTint;\n"- "#endif\n"- "#ifdef MESH_NORMALS\n"- "layout(location = 13) in vec3 a_nrm0;\n"- "layout(location = 14) in vec3 a_nrm1;\n"- "layout(location = 15) in vec3 a_nrm2;\n"- "#endif\n""#endif\n""#endif\n""#ifdef MESH\n"@@ -460,12 +506,10 @@ std::string canonicalVertexStage(bool lit) {" + a_weights.z * u_bones[a_joints.z]\n"" + a_weights.w * u_bones[a_joints.w];\n"" vec4 world = skin * vec4(local, 1.0);\n"- " v_color = a_color * a_instTint;\n"+ " v_color = a_color * esInstanceTint();\n""#elif defined(MESH)\n"- " vec4 world = vec4(dot(a_model0, vec4(local, 1.0)),\n"- " dot(a_model1, vec4(local, 1.0)),\n"- " dot(a_model2, vec4(local, 1.0)), 1.0);\n"- " v_color = a_color * a_instTint;\n"+ " vec4 world = esInstanceModel() * vec4(local, 1.0);\n"+ " v_color = a_color * esInstanceTint();\n""#else\n"" vec4 world = vec4(a_position, 1.0);\n"" v_color = a_color;\n"@@ -481,13 +525,14 @@ std::string canonicalVertexStage(bool lit) {"#ifdef SKINNED\n"" v_worldNormal = mat3(skin) * localNormal;\n""#else\n"- " v_worldNormal = mat3(a_nrm0, a_nrm1, a_nrm2) * localNormal;\n"+ " v_worldNormal = esInstanceNormalMatrix() * localNormal;\n""#endif\n"" v_worldXYZ = world.xyz;\n""#endif\n""#ifdef MESH_LIGHTMAP\n"- " v_lightmap = vec3(a_texCoord1 * a_lightmapRect.xy + a_lightmapRect.zw,\n"- " a_lightmapRect.x > 0.0 ? 1.0 : 0.0);\n"+ " highp vec4 lmRect = esInstanceLightmapRect();\n"+ " v_lightmap = vec3(a_texCoord1 * lmRect.xy + lmRect.zw,\n"+ " lmRect.x > 0.0 ? 1.0 : 0.0);\n""#endif\n";if (lit) {src += " v_worldPos = world.xy;\n";@@ -1426,7 +1471,7 @@ std::string trimWs(const std::string& s) {// #ifdef/#ifndef/#else/#elif defined(NAME)/#endif over the assembled text,// with the feature set as the defined names. GLSL keeps real #defines for the// driver, so both targets see identical variant logic in the authored bodies.-std::string preprocessWGSL(const std::string& source, const std::vector<std::string>& features) {+std::string preprocessConditionals(const std::string& source, const std::vector<std::string>& features) {const std::unordered_set<std::string> defined(features.begin(), features.end());struct Frame {bool parentActive; ///< Whether the enclosing region emits lines.@@ -1509,7 +1554,7 @@ ShaderParser::AssembledStage assembleWGSLStage(const ParsedShader& parsed,// injecting the shared headers would double-declare them.auto fullIt = parsed.wgslStageFull.find(stage);if (fullIt != parsed.wgslStageFull.end() && fullIt->second) {- result.source = preprocessWGSL(bodyIt->second, features);+ result.source = preprocessConditionals(bodyIt->second, features);result.headerLineCount = 0;return result;}@@ -1546,13 +1591,19 @@ ShaderParser::AssembledStage assembleWGSLStage(const ParsedShader& parsed,if (lit) inject(kLitHeaderWGSL);inject(kColorHelpersWGSL);}+ // The records, for a stage that names them — the same question, and the same+ // answer, as the GLSL side. Before the texture completion below, so the unit+ // this reaches for is one of the ones it sees.+ if (stage == ShaderStage::Vertex && bodyIt->second.find("esInstance") != std::string::npos) {+ inject(instanceRecordWGSL());+ }// Last, so it sees every declaration made above as well as the body's own.inject(wgslReachedTextureDecls(assembled.str() + bodyIt->second,parsed.domain == "PostProcess"));assembled << bodyIt->second;- result.source = preprocessWGSL(assembled.str(), features);+ result.source = preprocessConditionals(assembled.str(), features);result.headerLineCount = headerLines;return result;}@@ -1688,18 +1739,30 @@ ShaderParser::AssembledStage ShaderParser::assembleStageEx(const ParsedShader& p// carry explicit highp for the same reason MaterialConstants does: a fragment shader has no// default float precision until its `precision` line, which follows this injected header.- // Declared by every Lit vertex because every Lit fragment takes it in. One- // that never fills it reads slot 0 (the fragment clamps), not garbage.- if (stage == ShaderStage::Vertex && parsed.domain == "Lit") {- // The twin generator compiles this stage under Vulkan semantics, where the- // instance number is spelled differently and gl_InstanceID does not exist.+ if (stage == ShaderStage::Vertex) {+ // Every vertex stage, not only the Lit ones: an unlit mesh reads the same+ // record. Spelled twice because the twin generator compiles this under+ // Vulkan semantics, where gl_InstanceID does not exist.assembled << "#ifdef VULKAN\n""#define ES_INSTANCE_ID gl_InstanceIndex\n""#else\n""#define ES_INSTANCE_ID gl_InstanceID\n"- "#endif\n"- "flat out highp float v_probeSlot;\n";- headerLines += 6;+ "#endif\n";+ headerLines += 5;+ // The records themselves, for a stage that names them. Reached-not-declared+ // is how wgslReachedTextureDecls decides the same question: one author for+ // the declaration, rather than a copy in every shader that reads a record.+ if (stageIt->second.find("esInstance") != std::string::npos) {+ const std::string decl = instanceRecordGLSL();+ assembled << decl;+ headerLines += static_cast<u32>(std::count(decl.begin(), decl.end(), '\n'));+ }+ }+ // Declared by every Lit vertex because every Lit fragment takes it in. One+ // that never fills it reads slot 0 (the fragment clamps), not garbage.+ if (stage == ShaderStage::Vertex && parsed.domain == "Lit") {+ assembled << "flat out highp float v_probeSlot;\n";+ headerLines += 1;}// The light-array size and packing here MUST match renderer/LightConstants.hpp (MAX_LIGHTS,@@ -2394,6 +2457,18 @@ void ShaderParser::computeMaterialLayout(ParsedShader& shader) {for (auto& p : shader.properties) {if (!p.fromParam) continue; // legacy properties-block entries are reflection-onlyif (p.type == ShaderPropertyType::Texture) {+ // The top unit is the frame's per-object record, on every mesh shader+ // there is. Handing it out here would bind a material's texture over+ // the records and draw every object at the origin.+ if (textureUnit >= MESH_INSTANCE_TEXTURE_UNIT) {+ ES_LOG_ERROR("shader {}: texture param \"{}\" would take unit {}, which is the"+ " per-object record; a material reads at most {} textures",+ shader.name, p.name, textureUnit,+ MESH_INSTANCE_TEXTURE_UNIT - MATERIAL_TEXTURE_UNIT_BASE);+ p.textureUnit = -1;+ p.std140Offset = -1;+ continue;+ }p.textureUnit = static_cast<i32>(textureUnit++);p.std140Offset = -1;continue;diff --git a/tests/renderer/test_shader_device.cpp b/tests/renderer/test_shader_device.cppindex 5d89d44c8..ffb63f9c2 100644--- a/tests/renderer/test_shader_device.cpp+++ b/tests/renderer/test_shader_device.cpp@@ -7,6 +7,7 @@#include "MockGfxDevice.hpp"#include "esengine/renderer/rhi/Shader.hpp"#include "esengine/renderer/store/LightConstants.hpp"+#include "esengine/renderer/rhi/GfxEnums.hpp"#include "esengine/renderer/store/MorphConstants.hpp"#include <cstdio>@@ -31,16 +32,16 @@ int main() {CHECK(shader->getProgramId() == 1, "programId is the device-returned id");CHECK(d.getActiveUniformsCalls == 1, "reflection routes through device.getActiveUniforms");- // compile() pins the engine-injected samplers to their units, so the counts- // below start from what that seeding left behind. Six: the shadow map, the- // reflection, the two 2D masks, the morph deltas and the baked atlas.- CHECK(d.setUniform1iCalls == 6- && d.lastUniform1iVal == static_cast<i32>(LIGHTMAP_TEXTURE_UNIT),+ // compile() pins every engine-injected sampler to its unit, so the counts+ // below start from what that seeding left behind — seven of them, ending+ // on the per-object records.+ CHECK(d.setUniform1iCalls == 7+ && d.lastUniform1iVal == static_cast<i32>(MESH_INSTANCE_TEXTURE_UNIT),"compile pins the injected samplers to their texture units");CHECK(d.useProgramCalls == 2, "seeding them binds and unbinds the program once");shader->setUniform("u_tex", 3);- CHECK(d.setUniform1iCalls == 7, "setUniform(name,int) routes through device.setUniform1i");+ CHECK(d.setUniform1iCalls == 8, "setUniform(name,int) routes through device.setUniform1i");CHECK(d.lastUniform1iVal == 3, "uniform value forwarded");shader->setUniform("u_color", glm::vec4(1, 0, 0, 1));diff --git a/tools/check-mesh-instance-record.mjs b/tools/check-mesh-instance-record.mjsindex 2eef97eae..dc34e2ca8 100644--- a/tools/check-mesh-instance-record.mjs+++ b/tools/check-mesh-instance-record.mjs@@ -1,22 +1,24 @@// SPDX-License-Identifier: Apache-2.0// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team/**- * @file check-mesh-instance-record.mjs — the per-object record says one thing.+ * @file check-mesh-instance-record.mjs — the per-object record stays out of the slots.*- * The engine appends a per-object record to every resident mesh's layout, and- * what it contains is written down FOUR times: mesh.esshader declares it in GLSL- * and again in WGSL, and ShaderParser injects the same pair into a material's- * mesh variant. ResourceManager builds the layout those four must match. Nothing- * held them together, and dropping a row from the model matrix meant editing all- * five by hand — one missed edit reads whatever the driver left in the slot.+ * The record a resident mesh is drawn with lives in the frame's texture, read+ * through the `esInstance*` helpers the assembler injects. That is what leaves+ * every attribute slot above the mesh channels free, and it is a property with+ * no compiler behind it: a shader that declares an instance attribute again+ * compiles, draws, and quietly takes the room back.*- * Two claims, because they fail differently:+ * Three claims, because they fail differently:*- * the four declarations disagree- * a material draws its mesh through a layout built for the other one; the- * frame is wrong and no error names the cause- * the declarations and MESH_INSTANCE_STRIDE disagree- * the record is read past its end, or a part of it is never sent+ * an instance attribute is declared above the mesh channels+ * the slots are spent again, and the next mesh channel or custom attribute+ * has nowhere to go — nothing reports it until one is added+ * a shader names an `esInstance*` helper the header does not define+ * GLSL says so at compile; WGSL reports an invalid pipeline that names no+ * shader, which is how a whole variant goes missing with no message+ * the record's texels do not fit the record+ * a part is written past the end of one object and read as the next one's** Run: node tools/check-mesh-instance-record.mjs*/@@ -30,109 +32,92 @@ const read = (rel) => readFileSync(path.join(ROOT, rel), 'utf8');const SHADER = 'src/esengine/data/shaders/mesh.esshader';const PARSER = 'src/esengine/resource/ShaderParser.cpp';const ENUMS = 'src/esengine/renderer/rhi/GfxEnums.hpp';+/** The first slot a mesh's own channels may claim — the room this check keeps free. */+const FIRST_FREE_SLOT = 8;const enums = read(ENUMS);const constant = (name) => {const found = new RegExp(`${name}\\s*=\\s*(\\d+)`).exec(enums);- if (!found) throw new Error(`${ENUMS} no longer declares ${name}`);- return Number(found[1]);+ if (found) return Number(found[1]);+ throw new Error(`${ENUMS} no longer declares ${name}`);};-const FIRST = constant('MESH_INSTANCE_FIRST_LOCATION');-const STRIDE = constant('MESH_INSTANCE_STRIDE');-const STRIDE_LIT = constant('MESH_INSTANCE_STRIDE_LIT');-const LIGHTMAP_BYTES = constant('MESH_INSTANCE_LIGHTMAP_BYTES');+const TEXELS = constant('MESH_INSTANCE_TEXELS');+const WIDTH = constant('MESH_INSTANCE_TEXTURE_WIDTH');+const PARTS = {+ MESH_INSTANCE_TEXEL_MODEL: 3,+ MESH_INSTANCE_TEXEL_TINT: 1,+ MESH_INSTANCE_TEXEL_NORMAL: 3,+ MESH_INSTANCE_TEXEL_LIGHTMAP: 1,+};++const problems = [];-/** Every instance-range attribute a source declares: location → name, per language. */-function declarations(text) {- const glsl = new Map();- const wgsl = new Map();+// Claim 1: nothing takes the slots back. Both languages, both sources — a+// declaration in either one is a layout the other cannot serve anyway.+for (const file of [SHADER, PARSER]) {+ const text = read(file);for (const [, loc, name] of text.matchAll(/layout\(location\s*=\s*(\d+)\)\s*in\s+\w+\s+(\w+)/g)) {- if (Number(loc) >= FIRST) glsl.set(Number(loc), name);+ if (Number(loc) >= FIRST_FREE_SLOT) {+ problems.push(`${file} declares GLSL attribute "${name}" at location ${loc};`+ + ` ${FIRST_FREE_SLOT} and up are free for a mesh's own channels`);+ }}- for (const [, loc, name] of text.matchAll(/@location\((\d+)\)\s*(\w+)\s*:/g)) {- if (Number(loc) >= FIRST) wgsl.set(Number(loc), name);+ for (const [, loc, name] of text.matchAll(/@location\((\d+)\)\s+(a_\w+)\s*:/g)) {+ if (Number(loc) >= FIRST_FREE_SLOT) {+ problems.push(`${file} declares WGSL attribute "${name}" at location ${loc};`+ + ` ${FIRST_FREE_SLOT} and up are free for a mesh's own channels`);+ }}- return { glsl, wgsl };}-const problems = [];-const sources = { [SHADER]: declarations(read(SHADER)), [PARSER]: declarations(read(PARSER)) };--// All four must name the same thing at the same location. Compared as text so a-// renamed attribute is a finding too: a name is how the twin's reader matches up.-const spellings = new Map();-for (const [file, langs] of Object.entries(sources)) {- for (const [lang, found] of Object.entries(langs)) {- for (const [loc, name] of found) {- const seen = spellings.get(loc);- if (!seen) { spellings.set(loc, { name, where: `${file} (${lang})` }); continue; }- if (seen.name !== name) {- problems.push(`location ${loc} is "${seen.name}" in ${seen.where}`- + ` and "${name}" in ${file} (${lang}) — one of them reads the other's bytes`);- }+// Claim 2: every helper a shader reaches for is one the header defines. The+// header is built in PARSER, so its own text answers both halves.+const parser = read(PARSER);+const defined = new Set([+ ...[...parser.matchAll(/highp \w+ (esInstance\w+)\(/g)].map((m) => m[1]),+ ...[...parser.matchAll(/fn (esInstance\w+)\(/g)].map((m) => m[1]),+]);+for (const file of [SHADER, PARSER]) {+ for (const [, name] of read(file).matchAll(/\b(esInstance\w+)\s*\(/g)) {+ if (!defined.has(name)) {+ problems.push(`${file} names ${name}(), which the injected header does not define`);}}}--// Each declaration set must cover the same locations: a part declared in one-// language and not its twin is a backend that draws the record differently.-const every = [...spellings.keys()].sort((a, b) => a - b);-for (const [file, langs] of Object.entries(sources)) {- for (const [lang, found] of Object.entries(langs)) {- const missing = every.filter((loc) => !found.has(loc));- if (missing.length) {- problems.push(`${file} (${lang}) declares no attribute at location(s) `- + `${missing.join(', ')}, which the others do`);- }+for (const half of ['highp \\w+ (esInstance\\w+)\\(', 'fn (esInstance\\w+)\\(']) {+ const found = new Set([...parser.matchAll(new RegExp(half, 'g'))].map((m) => m[1]));+ const missing = [...defined].filter((n) => !found.has(n));+ if (missing.length) {+ problems.push(`${PARSER} defines ${missing.join(', ')} in one language and not the other`);}}-/**- * What the declarations add up to, against what the packer sends. Measured by- * WHERE each part is rather than by how many there are: normals and a bake are- * each optional, so a count alone cannot say which record a total is of.- */-const LIGHTMAP = FIRST + 3;-const TINT = FIRST + 4;-const NORMAL_FIRST = FIRST + 5;-const modelRows = every.filter((loc) => loc < LIGHTMAP).length;-const normalRows = every.filter((loc) => loc >= NORMAL_FIRST).length;-const base = modelRows * 16 + 4;-const lit = base + normalRows * 12;-if (!spellings.has(TINT)) {- problems.push(`no attribute at location ${TINT}, where the record's tint is`);-}-if (base !== STRIDE) {- problems.push(`${modelRows} model row(s) plus the tint is ${base} bytes, but`- + ` MESH_INSTANCE_STRIDE is ${STRIDE} — the record is packed to a different shape`- + ' than it is declared');-}-if (lit !== STRIDE_LIT) {- problems.push(`with ${normalRows} normal row(s) that is ${lit} bytes, but`- + ` MESH_INSTANCE_STRIDE_LIT is ${STRIDE_LIT}`);-}-// The bake's rectangle is APPENDED, so its size is all the packer needs to agree-// on: everything before it keeps the offset it had, which is what lets one-// record serve an object with a bake and one without.-if (spellings.has(LIGHTMAP) && LIGHTMAP_BYTES !== 16) {- problems.push(`location ${LIGHTMAP} declares a vec4 atlas rectangle, which is 16 bytes,`- + ` but MESH_INSTANCE_LIGHTMAP_BYTES is ${LIGHTMAP_BYTES}`);+// Claim 3: the parts fit, do not overlap, and a record never straddles a row.+const occupied = new Map();+for (const [name, span] of Object.entries(PARTS)) {+ const at = constant(name);+ for (let i = at; i < at + span; ++i) {+ if (i >= TEXELS) {+ problems.push(`${name} reaches texel ${i}, past the ${TEXELS} a record holds`);+ }+ const seen = occupied.get(i);+ if (seen) problems.push(`texel ${i} is both ${seen} and ${name}`);+ occupied.set(i, name);+ }}-if (!spellings.has(LIGHTMAP) && LIGHTMAP_BYTES !== 0) {- problems.push(`MESH_INSTANCE_LIGHTMAP_BYTES is ${LIGHTMAP_BYTES}, but no attribute at`- + ` location ${LIGHTMAP} carries an atlas rectangle — the bytes are sent and never read`);+if (WIDTH % TEXELS !== 0) {+ problems.push(`MESH_INSTANCE_TEXTURE_WIDTH is ${WIDTH}, not a multiple of the`+ + ` ${TEXELS}-texel record — a record would straddle a row`);}if (problems.length) {- console.error('check-mesh-instance-record: the per-object record is described'- + ' more than one way.\n');+ console.error('check-mesh-instance-record: the per-object record is not where it belongs.\n');for (const p of problems) console.error(` ${p}`);- console.error(`\nIt is declared in ${SHADER} (both languages) and injected by ${PARSER}`- + ` (both languages); ${ENUMS} says how many bytes it is.`);+ console.error(`\nThe record is read through the helpers ${PARSER} injects and written by`+ + ` MeshPlugin; ${ENUMS} says what texel each part is at.`);process.exit(1);}-const bake = spellings.has(LIGHTMAP) ? ` (+${LIGHTMAP_BYTES} where a bake is read)` : '';-console.log(`check-mesh-instance-record: ${every.length} part(s) of the per-object record,`- + ` spelled the same in all four declarations and adding to ${STRIDE}/${STRIDE_LIT}`- + ` bytes${bake}.`);+console.log(`check-mesh-instance-record: ${occupied.size} of ${TEXELS} texel(s) spoken for,`+ + ` read through ${defined.size} helper(s) defined in both languages,`+ + ` and no attribute slot from ${FIRST_FREE_SLOT} up is spent on them.`);diff --git a/tools/check-shader-conditionals.mjs b/tools/check-shader-conditionals.mjsindex 170a211a0..e238e1598 100644--- a/tools/check-shader-conditionals.mjs+++ b/tools/check-shader-conditionals.mjs@@ -5,7 +5,7 @@* engine can actually resolve.** A GLSL stage is preprocessed by the DRIVER, which has a full C preprocessor.- * A WGSL twin is expanded by the engine (ShaderParser::preprocessWGSL), which+ * A WGSL twin is expanded by the engine (ShaderParser::preprocessConditionals), which* understands `#ifdef`, `#ifndef`, `#elif defined(X)`, `#else` and `#endif` —* and nothing else. A `#if defined(A) && !defined(B)` there is not an error: the* line is emitted verbatim into the WGSL, which then fails to compile, and what@@ -13,6 +13,10 @@** The two halves of a file therefore do NOT have the same powers, which is the* kind of asymmetry nothing else would state.+ *+ * And a second claim, on the same expander: every `#ifdef` closes. An unclosed+ * one swallows the whole rest of the module for the variants that take the dead+ * branch — silently, because this expander does not report one.*/import { readFileSync, existsSync } from 'node:fs';import { corpusRoots, censusFindings, sourceFiles } from './lib/sourceCensus.mjs';@@ -45,13 +49,78 @@ for (const file of files) {}}+/**+ * The conditional directives of a text, in order, as `{ line, kind }`.+ */+function directives(text, firstLine) {+ const out = [];+ text.split('\n').forEach((raw, i) => {+ const lead = raw.trim();+ if (!lead.startsWith('#if') && !lead.startsWith('#else')+ && !lead.startsWith('#elif') && !lead.startsWith('#endif')) return;+ out.push({ line: firstLine + i, kind: lead.split(/[\s(]/)[0] });+ });+ return out;+}++/** What @p text leaves open, as a list of the lines that opened it. */+function unclosed(text, firstLine) {+ const open = [];+ for (const d of directives(text, firstLine)) {+ if (d.kind === '#ifdef' || d.kind === '#ifndef' || d.kind === '#if') open.push(d.line);+ else if (d.kind === '#endif') open.pop();+ }+ return open;+}++for (const file of files) {+ const text = readFileSync(path.join(ROOT, file), 'utf8');+ for (const block of text.matchAll(/^#pragma\s+\w+\s+wgsl\s*$([\s\S]*?)^#pragma\s+end\s*$/gm)) {+ const before = text.slice(0, block.index).split('\n').length;+ for (const line of unclosed(block[1], before + 1)) {+ problems.push(`${file}:${line} opens a conditional this block never closes`);+ }+ }+}++// The stages the engine BUILDS are shader sources too, and they run through the+// same expander. Their conditionals live in C++ string literals, so the text a+// variant sees is not one anybody reads whole.+const BUILDER = 'src/esengine/resource/ShaderParser.cpp';+let builders = 0;+{+ const cpp = readFileSync(path.join(ROOT, BUILDER), 'utf8');+ // Literal contents, by the function that holds them. A hand-rolled scan+ // because a comment on the way may carry a quote of its own.+ for (const fn of cpp.matchAll(/^std::string (\w+)\([^)]*\)\s*\{$([\s\S]*?)^\}$/gm)) {+ let text = '';+ const body = fn[2];+ for (let i = 0; i < body.length; ++i) {+ if (body[i] === '/' && body[i + 1] === '/') { while (i < body.length && body[i] !== '\n') ++i; continue; }+ if (body[i] !== '"') continue;+ for (++i; i < body.length && body[i] !== '"'; ++i) {+ if (body[i] !== '\\') { text += body[i]; continue; }+ text += body[++i] === 'n' ? '\n' : body[i];+ }+ }+ if (!/#if(def|ndef)?\b/.test(text)) continue;+ ++builders;+ if (unclosed(text, 0).length) {+ problems.push(`${BUILDER} ${fn[1]}() leaves `+ + `${unclosed(text, 0).length} conditional(s) open — every variant that takes`+ + ' the dead branch loses the rest of the stage');+ }+ }+}+if (problems.length) {console.error('check-shader-conditionals: a WGSL block uses a conditional the engine'+ " does not resolve — it reaches the compiler verbatim.\n");for (const p of problems) console.error(` ${p}`);- console.error('\nUse nested #ifdef / #ifndef, or #elif defined(X).');+ console.error('\nUse nested #ifdef / #ifndef, or #elif defined(X), and close every one.');process.exit(1);}console.log(`check-shader-conditionals: ${wgslBlocks} WGSL block(s) across ${files.length}`- + ' shader(s) — every conditional is one the engine expands.');+ + ` shader(s) and ${builders} built stage(s) — every conditional is one the engine`+ + ' expands, and every one of them closes.');diff --git a/tools/gen-shader-twins.mjs b/tools/gen-shader-twins.mjsindex 33e220a7b..ea5057901 100644--- a/tools/gen-shader-twins.mjs+++ b/tools/gen-shader-twins.mjs@@ -56,6 +56,10 @@ const BLOCK_BINDINGS = {MaterialConstants: 1,LightConstants: 2,TimeConstants: 3,+ // Where a draw's objects start in the frame's record texture, which the+ // injected header declares for every vertex stage that reads a record — see+ // InstanceConstants.hpp.+ InstanceConstants: 8,// The indirect light where a draw stands, which the injected Lit header// declares for every Lit fragment stage — see ProbeConstants.hpp.ProbeConstants: 7,@@ -77,8 +81,11 @@ const ENGINE_SAMPLERS = [{ name: 'u_envMap', unit: 3 },{ name: 'u_shadow2D', unit: 7 },{ name: 'u_lightShape2D', unit: 6 },- // The one the VERTEX stage reads, pinned in MorphConstants.hpp.+ // The two the VERTEX stage reads, pinned in MorphConstants.hpp and+ // InstanceConstants.hpp. The record sits on the TOP unit, above every unit a+ // material's own textures are handed (see GfxEnums MESH_INSTANCE_TEXTURE_UNIT).{ name: 'u_morphDeltas', unit: 4 },+ { name: 'u_instanceData', unit: 15 },];// Group-1 unit map — mirrors WebGPUMappings textureBindingForUnit/samplerBindingForUnit.@@ -375,7 +382,7 @@ const permutationKey = (on) => [...on].sort().join(',');/*** One body carrying every permutation, selected by the same `#ifdef` the GLSL- * side gets from real defines — `preprocessWGSL` resolves it at assembly, so+ * side gets from real defines — `preprocessConditionals` resolves it at assembly, so* both targets see identical variant logic.*/export function composePermutations(toggles, bodies, remaining = toggles, on = []) {diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjsindex 8ec96ff1e..9e9fb27d2 100644--- a/tools/releaseNotes.mjs+++ b/tools/releaseNotes.mjs@@ -28,6 +28,9 @@ export const NOTES_FLOOR = 'v0.65.0';* the reader recognises anyway.*/export const NOTED = {+ 'feat(render): the per-object record leaves the vertex attributes':+ { note: 'A mesh has somewhere to put its own vertex attributes.' },+'feat(render): indirect light per instance, so a probe stops refusing the merge':{ note: 'A scene can hold a thousand of something again.' },