Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

fix(gates): meshShaderReads is a claim about the shader, so hold it against one

esengine55a22c9f24e0
diff --git a/src/esengine/renderer/rhi/GfxEnums.hpp b/src/esengine/renderer/rhi/GfxEnums.hpp
index 317aa47c8..fd2c49c85 100644
--- a/src/esengine/renderer/rhi/GfxEnums.hpp
+++ b/src/esengine/renderer/rhi/GfxEnums.hpp
@@ -134,13 +134,13 @@ enum class MeshChannel : u8 {
/**
* @brief Whether the mesh shaders READ a channel, as opposed to the file carrying it.
- * @details A channel kept for its information stays in the vertex buffer and out
- * of the LAYOUT: an attribute nothing reads spends a slot and a fetch
- * per vertex. Binding it anyway was tried on both backends and neither
- * refused, so this is a cost, not a correctness fix.
+ * @details An attribute nothing reads spends a slot and a fetch per vertex, so a
+ * channel kept for its information stays out of the LAYOUT. Binding it
+ * anyway was tried on both backends and neither refused: a cost, not a
+ * correctness fix. check-mesh-shader-inputs holds this both ways.
*/
constexpr bool meshShaderReads(MeshChannel c) {
- return c != MeshChannel::TexCoord1;
+ return c != MeshChannel::Tangent && c != MeshChannel::TexCoord1;
}
/**
diff --git a/tools/check-mesh-shader-inputs.mjs b/tools/check-mesh-shader-inputs.mjs
new file mode 100644
index 000000000..8e844c6aa
--- /dev/null
+++ b/tools/check-mesh-shader-inputs.mjs
@@ -0,0 +1,83 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/**
+ * @file check-mesh-shader-inputs.mjs — `meshShaderReads` says what the Mesh shader does.
+ *
+ * A mesh vertex layout binds the channels the file carries; `meshShaderReads`
+ * decides which of them reach it. That predicate is a claim ABOUT the shader,
+ * kept in a different file and a different language, and nothing else notices
+ * when the two stop agreeing. Both directions are wrong in their own way:
+ *
+ * the shader declares a channel the predicate excludes
+ * the attribute is never bound and the shader reads whatever the driver
+ * leaves there — a wrong frame with nothing naming the cause
+ * the predicate admits a channel no shader declares
+ * an attribute slot from sixteen and a fetch per vertex, spent on nothing,
+ * which is the entire reason the predicate exists
+ *
+ * The instance record (location >= MESH_INSTANCE_FIRST_LOCATION) is not a mesh
+ * channel and is skipped.
+ */
+import { readFileSync } from 'node:fs';
+import path from 'node:path';
+import { fileURLToPath } from 'node:url';
+
+const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
+const ENUMS = path.join('src', 'esengine', 'renderer', 'rhi', 'GfxEnums.hpp');
+const SHADERS = path.join('src', 'esengine', 'renderer', 'rhi', 'ShaderEmbeds.generated.hpp');
+
+const enums = readFileSync(path.join(ROOT, ENUMS), 'utf8');
+const shaders = readFileSync(path.join(ROOT, SHADERS), 'utf8');
+
+const problems = [];
+
+const channels = new Map();
+const body = /enum class MeshChannel\s*:[^{]*\{([^}]*)\}/m.exec(enums);
+for (const [, name, value] of (body?.[1] ?? '').matchAll(/^\s*(\w+)\s*=\s*(\d+)\s*,?\s*$/gm)) {
+ channels.set(Number(value), name);
+}
+const firstInstance = Number(/MESH_INSTANCE_FIRST_LOCATION\s*=\s*(\d+)/.exec(enums)?.[1] ?? NaN);
+
+/** The predicate reads as a chain of `c != MeshChannel::X`; those are the exclusions. */
+const predicate = /constexpr bool meshShaderReads\(MeshChannel c\)\s*\{([^}]*)\}/m.exec(enums)?.[1] ?? '';
+const excluded = new Set([...predicate.matchAll(/MeshChannel::(\w+)/g)].map((m) => m[1]));
+if (!/return\b/.test(predicate) || (!excluded.size && !/return true/.test(predicate))) {
+ problems.push('meshShaderReads is no longer a chain of `c != MeshChannel::X` — this check can no longer read it');
+}
+const claimsRead = new Set([...channels.values()].filter((n) => !excluded.has(n)));
+
+/** The Mesh shader's own vertex inputs, below the per-object record. */
+const mesh = /const char\* MESH = R"esshader\(([\s\S]*?)\)esshader"/.exec(shaders)?.[1];
+if (!mesh) problems.push(`no Mesh shader found in ${SHADERS}`);
+const declared = new Set();
+for (const [, loc] of (mesh ?? '').matchAll(/layout\(location\s*=\s*(\d+)\)\s*in\b/g)) {
+ const n = Number(loc);
+ if (n >= firstInstance) continue;
+ const name = channels.get(n);
+ if (!name) {
+ problems.push(`the Mesh shader declares location ${n}, which MeshChannel does not name`);
+ continue;
+ }
+ declared.add(name);
+}
+
+for (const name of declared) {
+ if (!claimsRead.has(name)) {
+ problems.push(`the Mesh shader reads ${name} and meshShaderReads excludes it`
+ + ' — the layout would never bind it, and the shader would read whatever is there');
+ }
+}
+for (const name of claimsRead) {
+ if (!declared.has(name)) {
+ problems.push(`meshShaderReads admits ${name} and no Mesh shader declares it`
+ + ' — an attribute slot and a fetch per vertex, spent on nothing');
+ }
+}
+
+if (problems.length) {
+ console.error('check-mesh-shader-inputs: the predicate and the shader disagree:');
+ for (const p of problems) console.error(` ${p}`);
+ process.exit(1);
+}
+console.log(`check-mesh-shader-inputs: ${declared.size} mesh channel(s) both declared and admitted`
+ + ` (${[...declared].join(', ')}); ${[...excluded].join(', ') || 'none'} carried but not bound.`);
diff --git a/tools/gates.mjs b/tools/gates.mjs
index c97fa0b48..af7e83304 100644
--- a/tools/gates.mjs
+++ b/tools/gates.mjs
@@ -188,6 +188,7 @@ export const GATES = [
{ id: 'native-components', run: 'node tools/check-native-components.mjs' },
{ id: 'shader-conditionals', run: 'node tools/check-shader-conditionals.mjs' },
{ id: 'mesh-vocabulary', run: 'node tools/check-mesh-vocabulary.mjs' },
+ { id: 'mesh-shader-inputs', run: 'node tools/check-mesh-shader-inputs.mjs' },
// …and that whoever mints one can say how it survives a device generation:
// recovery only ever holds a number, so the answer has to exist before it runs.
{ id: 'mesh-provenance', run: 'node tools/check-mesh-provenance.mjs' },