A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
fix(shaders): an #if or #elif condition resolves in WGSL assembly and in the twin generator, so a lit material's WebGPU program no longer carries a directive into the source
yhh33360533faa9
diff --git a/src/esengine/resource/ShaderParser.cpp b/src/esengine/resource/ShaderParser.cppindex e5cbff357..7566cbb1a 100644--- a/src/esengine/resource/ShaderParser.cpp+++ b/src/esengine/resource/ShaderParser.cpp@@ -22,6 +22,7 @@#include <algorithm>#include <cctype>#include <cstdlib>+#include <cstring>#include <unordered_set>namespace esengine::resource {@@ -2250,10 +2251,66 @@ std::string trimWs(const std::string& s) {return s.substr(a, b - a + 1);}-// WGSL has no preprocessor, so feature permutations resolve at assembly time:-// #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.+// An #if / #elif condition over the feature set: defined(NAME), defined NAME, a bare+// NAME (a feature is #defined to 1 on the GLSL side, anything else reads 0), integer+// literals, !, &&, || and parentheses.+class ConditionParser {+public:+ ConditionParser(const std::string& text, const std::unordered_set<std::string>& defined)+ : s_(text), defined_(defined) {}++ bool evaluate() { return orExpr(); }++private:+ void skipWs() { while (i_ < s_.size() && (s_[i_] == ' ' || s_[i_] == '\t' || s_[i_] == '\r')) ++i_; }+ bool eat(const char* tok) {+ skipWs();+ const usize n = std::strlen(tok);+ if (s_.compare(i_, n, tok) != 0) return false;+ i_ += n;+ return true;+ }+ std::string ident() {+ skipWs();+ const usize start = i_;+ while (i_ < s_.size() && (std::isalnum(static_cast<unsigned char>(s_[i_])) || s_[i_] == '_')) ++i_;+ return s_.substr(start, i_ - start);+ }+ bool orExpr() {+ bool v = andExpr();+ while (eat("||")) v = andExpr() || v;+ return v;+ }+ bool andExpr() {+ bool v = unary();+ while (eat("&&")) v = unary() && v;+ return v;+ }+ bool unary() {+ if (eat("!")) return !unary();+ if (eat("(")) {+ const bool v = orExpr();+ eat(")");+ return v;+ }+ const std::string word = ident();+ if (word == "defined") {+ const bool paren = eat("(");+ const bool v = defined_.count(ident()) != 0;+ if (paren) eat(")");+ return v;+ }+ if (!word.empty() && std::isdigit(static_cast<unsigned char>(word[0]))) return std::strtol(word.c_str(), nullptr, 0) != 0;+ return defined_.count(word) != 0;+ }++ const std::string& s_;+ const std::unordered_set<std::string>& defined_;+ usize i_ = 0;+};++// WGSL has no preprocessor, so feature permutations resolve at assembly time over the assembled text, with the+// feature set as the defined names. GLSL keeps real #defines for the driver, so both targets see the same variants.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 {@@ -2278,15 +2335,14 @@ std::string preprocessConditionals(const std::string& source, const std::vector<stack.push_back(Frame{parent, cond, cond});continue;}+ if (lead.rfind("#if", 0) == 0 && (lead.size() == 3 || lead[3] == ' ' || lead[3] == '\t' || lead[3] == '(' || lead[3] == '!')) {+ const bool parent = activeNow();+ const bool cond = parent && ConditionParser(lead.substr(3), defined).evaluate();+ stack.push_back(Frame{parent, cond, cond});+ continue;+ }if (lead.rfind("#elif", 0) == 0 && !stack.empty()) {- bool cond = false;- const usize p = lead.find("defined(");- if (p != std::string::npos) {- const usize close = lead.find(')', p + 8);- if (close != std::string::npos) {- cond = defined.count(trimWs(lead.substr(p + 8, close - p - 8))) != 0;- }- }+ const bool cond = ConditionParser(lead.substr(5), defined).evaluate();Frame& f = stack.back();f.active = f.parentActive && !f.taken && cond;f.taken = f.taken || f.active;diff --git a/tests/renderer/test_shader_variant.cpp b/tests/renderer/test_shader_variant.cppindex b478f2db8..8f0735ea9 100644--- a/tests/renderer/test_shader_variant.cpp+++ b/tests/renderer/test_shader_variant.cpp@@ -12,6 +12,7 @@#include <cstdio>#include <sstream>#include <string>+#include <vector>static int g_failures = 0;#define CHECK(cond, msg) \@@ -183,6 +184,13 @@ void main() { fragColor = vec4(1.0); }#pragma fragment wgsl@fragment fn fs_main() -> @location(0) vec4f {+#if defined(A) && (defined(B) || !defined(C))+ let k = 0.25;+#elif defined(C) || D+ let k = 0.75;+#else+ let k = 0.125;+#endif#ifndef LITreturn vec4f(1.0);#else@@ -287,6 +295,13 @@ static void testWGSLEmission() {// been applied. .xy because 2D lighting works in that plane.CHECK(litVs.find("out.v_worldPos = world.xy;") != std::string::npos,"Lit canonical WGSL vertex forwards world position");+ for (const auto& features : std::vector<std::vector<std::string>>{{}, {"MESH"}, {"SKINNED"}, {"MESH", "MESH_TANGENTS"}}) {+ const std::string vs = wgsl(lit, ShaderStage::Vertex, features);+ CHECK(vs.find('#') == std::string::npos, "Lit canonical WGSL vertex leaves no directive behind");+ }+ CHECK(wgsl(lit, ShaderStage::Vertex).find("a_lightLayer") != std::string::npos &&+ wgsl(lit, ShaderStage::Vertex, {"MESH"}).find("a_lightLayer") == std::string::npos,+ "#if !defined(MESH) && !defined(SKINNED) keeps the sprite-only attribute off a mesh");const std::string litFs = wgsl(lit, ShaderStage::Fragment);CHECK(litFs.find("@location(2) v_worldPos : vec2f,") != std::string::npos,"Lit VSOut carries the world-pos varying");@@ -305,6 +320,15 @@ static void testWGSLEmission() {"TimeConstants still injected for authored twins");CHECK(pairFs.find("vec4f(1.0)") != std::string::npos && pairFs.find("vec4f(0.5)") == std::string::npos,"#ifndef keeps its branch when the feature is off");+ const auto kOf = [&](const std::vector<std::string>& features) {+ const std::string fs = wgsl(pair, ShaderStage::Fragment, features);+ const size_t at = fs.find("let k = ");+ return fs.find('#') == std::string::npos && at != std::string::npos && fs.find("let k = ", at + 1) == std::string::npos+ ? fs.substr(at + 8, fs.find(';', at) - at - 8) : std::string("?");+ };+ CHECK(kOf({}) == "0.125" && kOf({"A"}) == "0.25" && kOf({"A", "B", "C"}) == "0.25"+ && kOf({"A", "C"}) == "0.75" && kOf({"D"}) == "0.75",+ "#if / #elif expressions resolve at assembly");const std::string pairVsLit = wgsl(pair, ShaderStage::Vertex, {"LIT"});CHECK(pairVsLit.find("vec4f(pos, 1.0, 1.0)") != std::string::npos &&pairVsLit.find("vec4f(pos, 0.0, 1.0)") == std::string::npos,diff --git a/tools/gen-shader-twins.mjs b/tools/gen-shader-twins.mjsindex e79fbb006..e5b233d0c 100644--- a/tools/gen-shader-twins.mjs+++ b/tools/gen-shader-twins.mjs@@ -276,25 +276,38 @@ function rewriteCalls(src, fname, mapArgs) {const VARYING_DECL = /^([ \t]*)(flat\s+)?(out|in)\s+((?:highp\s+)?(?:float|int|uint|[iu]?vec[234]|mat[234]))\s+(\w+)\s*;/gm;/**- * Whether the text at an offset is live under the permutation being translated, given the toggles it switches on. Only `#ifdef` / `#ifndef` / `#else` / `#endif` are- * read, and a name that is not one of the permutation's own is undefined, as it is to glslang later: the engine's fragment prelude declares a varying inside an- * `#ifdef` for a vertex stage that may not write it, and a declaration that conditional is not a varying of this program. A conditional it cannot read counts as live.+ * Whether the text at an offset is live under the permutation being translated, given the toggles it switches on. `#if` / `#ifdef` / `#ifndef` / `#elif` / `#else`+ * / `#endif` are read, and a name that is not one of the permutation's own is undefined, as it is to glslang later: the engine's fragment prelude declares a varying+ * inside a conditional for a vertex stage that may not write it, and a declaration that conditional is not a varying of this program. A condition it cannot read+ * counts as live, and so does every other branch of its chain.*/export function liveAt(src, defined) {const edges = []; // [offset, live] from each directive on, in order- const stack = [];+ const stack = []; // { outer, taken, unknown }let live = true;let offset = 0;+ const open = (cond) => {+ const top = { outer: live, taken: cond === true, unknown: cond === null };+ stack.push(top);+ live = top.outer && cond !== false;+ };for (const line of src.split('\n')) {- const m = /^\s*#\s*(ifdef|ifndef|else|endif)\b\s*(\w+)?/.exec(line);+ const m = /^\s*#\s*(ifdef|ifndef|if|elif|else|endif)\b(.*)$/.exec(line);if (m) {+ const top = stack[stack.length - 1];if (m[1] === 'ifdef' || m[1] === 'ifndef') {- stack.push({ outer: live, cond: m[1] === 'ifdef' ? defined.has(m[2]) : !defined.has(m[2]) });- live = stack[stack.length - 1].outer && stack[stack.length - 1].cond;- } else if (m[1] === 'else' && stack.length > 0) {- const top = stack[stack.length - 1];- live = top.outer && !top.cond;- } else if (m[1] === 'endif' && stack.length > 0) {+ const has = defined.has(m[2].trim().split(/\s/)[0]);+ open(m[1] === 'ifdef' ? has : !has);+ } else if (m[1] === 'if') {+ open(evalCondition(m[2], defined));+ } else if (m[1] === 'elif' && top) {+ const cond = top.unknown ? null : evalCondition(m[2], defined);+ if (cond === null) top.unknown = true;+ live = top.outer && (top.unknown || (!top.taken && cond));+ top.taken ||= cond === true;+ } else if (m[1] === 'else' && top) {+ live = top.outer && (top.unknown || !top.taken);+ } else if (m[1] === 'endif' && top) {live = stack.pop().outer;}edges.push([offset + line.length + 1, live]);@@ -308,6 +321,46 @@ export function liveAt(src, defined) {};}+/** An `#if` / `#elif` condition over `defined`: defined(NAME), defined NAME, a bare NAME, integer literals, !, &&, || and parentheses; null when it says+ * anything else. Mirrors the engine's WGSL assembly (ShaderParser.cpp preprocessConditionals). */+export function evalCondition(text, defined) {+ const tokens = text.replace(/\/\/.*$/, '').match(/defined|\w+|&&|\|\||[!()]|\S/g) ?? [];+ let i = 0;+ const peek = () => tokens[i];+ const fail = () => { throw new Error('unreadable'); };+ const orExpr = () => {+ let v = andExpr();+ while (peek() === '||') { i++; v = andExpr() || v; }+ return v;+ };+ const andExpr = () => {+ let v = unary();+ while (peek() === '&&') { i++; v = unary() && v; }+ return v;+ };+ const unary = () => {+ const t = tokens[i++];+ if (t === '!') return !unary();+ if (t === '(') { const v = orExpr(); if (tokens[i++] !== ')') fail(); return v; }+ if (t === 'defined') {+ const paren = peek() === '(';+ if (paren) i++;+ const name = tokens[i++];+ if (!/^\w+$/.test(name ?? '') || (paren && tokens[i++] !== ')')) fail();+ return defined.has(name);+ }+ if (/^\d+$/.test(t ?? '')) return Number(t) !== 0;+ if (/^[A-Za-z_]\w*$/.test(t ?? '')) return defined.has(t);+ return fail();+ };+ try {+ const v = orExpr();+ return i === tokens.length ? v : null;+ } catch {+ return null;+ }+}+/*** Assign explicit, name-stable varying locations across both stages: glslang's* --auto-map-locations numbers each stage independently by declaration order,diff --git a/tools/tests/shader-twin-permutations.test.ts b/tools/tests/shader-twin-permutations.test.tsindex ffcca7b8c..eb0511d84 100644--- a/tools/tests/shader-twin-permutations.test.ts+++ b/tools/tests/shader-twin-permutations.test.ts@@ -92,4 +92,27 @@ describe('liveAt', () => {expect(liveAt(nested, new Set(['A', 'B']))(x)).toBe(true);expect(liveAt(nested, new Set(['B']))(y)).toBe(false);});++ it('reads an #if expression, and the #endif that closes it does not close the one around it', () => {+ const src = ['#ifdef OUTER', '#if defined(MESH_TANGENTS) && defined(LIT)', 'in vec4 t;', '#endif', 'in vec4 after;', '#endif', 'in vec4 tail;', ''].join('\n');+ const t = src.indexOf(' t;'), after = src.indexOf(' after;'), tail = src.indexOf(' tail;');+ expect(liveAt(src, new Set(['MESH_TANGENTS']))(t)).toBe(false);+ expect(liveAt(src, new Set(['OUTER', 'MESH_TANGENTS', 'LIT']))(t)).toBe(true);+ expect(liveAt(src, new Set())(after)).toBe(false);+ expect(liveAt(src, new Set())(tail)).toBe(true);+ });++ it('takes the first #elif that holds, and the #else only when none did', () => {+ const src = ['#if !defined(A)', 'in vec4 x;', '#elif defined(B) || C', 'in vec4 y;', '#else', 'in vec4 z;', '#endif', ''].join('\n');+ const live = (on: string[], name: string) => liveAt(src, new Set(on))(src.indexOf(` ${name};`));+ expect([live([], 'x'), live([], 'y'), live([], 'z')]).toEqual([true, false, false]);+ expect([live(['A', 'C'], 'x'), live(['A', 'C'], 'y'), live(['A', 'C'], 'z')]).toEqual([false, true, false]);+ expect([live(['A'], 'x'), live(['A'], 'y'), live(['A'], 'z')]).toEqual([false, false, true]);+ });++ it('counts a condition it cannot read as live, with every branch of its chain', () => {+ const src = ['#if VERSION >= 300', 'in vec4 x;', '#else', 'in vec4 y;', '#endif', ''].join('\n');+ expect(liveAt(src, new Set())(src.indexOf(' x;'))).toBe(true);+ expect(liveAt(src, new Set())(src.indexOf(' y;'))).toBe(true);+ });});