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(native): an embedded shader is cut into literals MSVC can hold, and the check sees delimited ones

yhh650f1c5bd4f7
diff --git a/build-tools/tasks/shader-embeds.js b/build-tools/tasks/shader-embeds.js
index 7b950b6d0..fa8566cc5 100644
--- a/build-tools/tasks/shader-embeds.js
+++ b/build-tools/tasks/shader-embeds.js
@@ -23,6 +23,27 @@ function stripComments(source) {
return source.split('\n').map((line) => (line.trimStart().startsWith('//') ? '' : line)).join('\n');
}
+/**
+ * MSVC truncates ONE string literal at 16380 bytes (C2026) — the mesh shader is
+ * past twice that. Adjacent literals concatenate with no such cap, so a long
+ * source is emitted as several, cut at line ends: the string the engine reads is
+ * byte-for-byte the same, and so is every line number a diagnostic quotes.
+ */
+const LITERAL_CHUNK = 12000;
+
+function rawLiterals(text) {
+ const pieces = [];
+ let at = 0;
+ while (text.length - at > LITERAL_CHUNK) {
+ const cut = text.lastIndexOf('\n', at + LITERAL_CHUNK);
+ const end = cut > at ? cut + 1 : at + LITERAL_CHUNK;
+ pieces.push(text.slice(at, end));
+ at = end;
+ }
+ pieces.push(text.slice(at));
+ return pieces.map((piece) => `R"esshader(${piece})esshader"`).join('\n ');
+}
+
export async function generateShaderEmbeds() {
const rootDir = config.paths.root;
const shadersDir = path.join(rootDir, 'src/esengine/data/shaders');
@@ -49,7 +70,7 @@ export async function generateShaderEmbeds() {
for (const file of files) {
const content = await readFile(path.join(shadersDir, file), 'utf-8');
const id = toCppIdentifier(file);
- lines.push(`inline constexpr const char* ${id} = R"esshader(${stripComments(content)})esshader";`);
+ lines.push(`inline constexpr const char* ${id} = ${rawLiterals(stripComments(content))};`);
lines.push('');
}
diff --git a/src/esengine/renderer/rhi/ShaderEmbeds.generated.hpp b/src/esengine/renderer/rhi/ShaderEmbeds.generated.hpp
index b4e359f70..55ccc7958 100644
--- a/src/esengine/renderer/rhi/ShaderEmbeds.generated.hpp
+++ b/src/esengine/renderer/rhi/ShaderEmbeds.generated.hpp
@@ -1090,7 +1090,8 @@ fn esPrevBone(joint : u32) -> mat4x4f {
let L = vec4f(local, 1.0);
#endif
let before = esInstanceTint();
- out.v_velCur = out.pos;
+)esshader"
+ R"esshader( out.v_velCur = out.pos;
let prev = vec4f(dot(esInstanceTexel(4), L), dot(esInstanceTexel(5), L),
dot(esInstanceTexel(6), L), dot(esInstanceTexel(7), L));
out.v_velPrev = vec4f(prev.xy + before.xy * prev.w, prev.zw);
diff --git a/tools/check-shader-literals.mjs b/tools/check-shader-literals.mjs
index f3c9622fd..b6a3c8f46 100644
--- a/tools/check-shader-literals.mjs
+++ b/tools/check-shader-literals.mjs
@@ -32,20 +32,24 @@ function sources(dir, out = []) {
return out;
}
-/** Every raw string literal in `src`, as [name, byteLength, endLine]. */
+/** Every raw string literal in `src`, as [name, byteLength, endLine] — with or
+ * without a delimiter: the embedded shaders are `R"esshader(...)esshader"`, and a
+ * scan that only knew `R"(` passed a 28 KB one that MSVC truncates. */
function literals(src) {
const out = [];
- const open = /R"\(/g;
+ const open = /R"([^()\\\s]{0,16})\(/g;
let m;
while ((m = open.exec(src))) {
- const end = src.indexOf(')"', m.index + 3);
+ const close = `)${m[1]}"`;
+ const start = m.index + m[0].length;
+ const end = src.indexOf(close, start);
if (end < 0) continue;
- const body = src.slice(m.index + 3, end);
+ const body = src.slice(start, end);
const before = src.slice(0, m.index);
const name = before.match(/(\w+)\s*=\s*$/)?.[1]
?? before.match(/(\w+)[^=\w]*$/)?.[1] ?? '(unnamed)';
out.push([name, Buffer.byteLength(body, 'utf8'), src.slice(0, end).split('\n').length]);
- open.lastIndex = end + 2;
+ open.lastIndex = end + close.length;
}
return out;
}