10.2 着色器编译与热重载
编辑器的即时反馈依赖热重载:改参数或改代码,视口不重启就更新。着色器编译把源码变成可用的着色器对象,热重载追踪版本、判定是否要替换。
本章与站内既有内容的分工
| 主题 | 站内既有内容 | 本节做什么 |
|---|---|---|
| 节点图材质 | 10.1 节点图材质:从图到着色参数 | 参数侧搞定;本节管代码侧编译 |
| 资产管线 | helio-ed-asset | 着色器源文件走资产导入 |
| 全卷脉络 | 本卷目录 | toc 列章节;热重载是编辑反馈的引擎 |
从零实现:着色器编译与热重载
compileShader 维护编译状态机:dirty → compiling → ready / failed。ShaderHotReload 在写入时推进版本号、标记 dirty,替换时检查新版本是否已编译且旧版本在使用。apply 只在版本更新且编译通过时替换活动着色器。
对标源码检查与着色器编译
Unreal Material Editor 可显示编译诊断,并通过实时预览观察修改。helio 的 compileShader 是同步教学桩,只检查源码非空、花括号数量及入口字样,成功时返回原始字符串。真正的编译器负责语法、类型和目标环境验证。 Unreal Material Editor Reference
可运行产物:6 条用例
下面是 src/shader-compile.ts,层级 L1,完整可编译。运行命令 node --test test/10-2-shader-compile-hotload.test.ts:
// file: src/shader-compile.ts
// tier: L1
// verify: node --test test/10-2-shader-compile-hotload.test.ts
//
// 视图(第十章):材质与着色器编辑器——编译、错误与热加载。
// 材质编辑器的预览依赖着色器编译。编译可能失败,失败时不能丢掉「上一次
// 成功」的版本——热加载的语义正是:新源码编译成功才替换,失败则保留
// 旧程序并报告错误。本模块实现轻量语法校验与热加载状态机。
export interface CompileResult {
ok: boolean;
errors: string[];
code?: string;
}
/** 轻量着色器编译校验:非空、括号平衡、含 main 入口。 */
export function compileShader(source: string): CompileResult {
const errors: string[] = [];
if (source.trim().length === 0) errors.push('源码为空');
let depth = 0;
for (const ch of source) {
if (ch === '{') depth += 1;
if (ch === '}') depth -= 1;
if (depth < 0) break;
}
if (depth !== 0) errors.push('花括号不平衡');
if (!/\bmain\s*\(/.test(source)) errors.push('缺少 main 入口');
return errors.length === 0
? { ok: true, errors: [], code: source }
: { ok: false, errors };
}
export class ShaderHotReload {
#lastGood: string | null = null;
#history: CompileResult[] = [];
/** 编译并尝试替换;成功则记录为新「上一次成功」。 */
load(source: string): CompileResult {
const result = compileShader(source);
this.#history.push(result);
if (result.ok) this.#lastGood = source;
return result;
}
/** 当前生效的着色器源码(上一次成功);从未成功过为 null。 */
current(): string | null {
return this.#lastGood;
}
history(): CompileResult[] {
return this.#history.slice();
}
/** 最后一次编译错误数。 */
lastErrorCount(): number {
const last = this.#history[this.#history.length - 1];
return last ? last.errors.length : 0;
}
}
示例同步检查输入,只在检查通过时替换当前源码。接入异步编译器时应为每次请求分配版本,并丢弃已经过期的结果。
下面是 test/10-2-shader-compile-hotload.test.ts,层级 L1:
// file: test/10-2-shader-compile-hotload.test.ts
// tier: L1
// verify: node --test test/10-2-shader-compile-hotload.test.ts
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { compileShader, ShaderHotReload } from '../src/shader-compile.ts';
const GOOD = 'void main() { return; }';
test('合法着色器编译通过', () => {
const r = compileShader(GOOD);
assert.equal(r.ok, true);
assert.deepEqual(r.errors, []);
assert.equal(r.code, GOOD);
});
test('空源码报错', () => {
const r = compileShader(' ');
assert.equal(r.ok, false);
assert.ok(r.errors.some((e) => e.includes('空')));
});
test('花括号不平衡报错', () => {
const r = compileShader('void main() {');
assert.equal(r.ok, false);
assert.ok(r.errors.some((e) => e.includes('花括号')));
});
test('缺 main 报错', () => {
const r = compileShader('void foo() {}');
assert.equal(r.ok, false);
assert.ok(r.errors.some((e) => e.includes('main')));
});
test('热加载成功替换当前着色器', () => {
const h = new ShaderHotReload();
assert.equal(h.current(), null);
const r = h.load('void main() { x; }');
assert.equal(r.ok, true);
assert.equal(h.current(), 'void main() { x; }');
});
test('编译失败保留上一次成功的版本', () => {
const h = new ShaderHotReload();
h.load('void main() { x; }');
const bad = h.load('void main() {');
assert.equal(bad.ok, false);
assert.equal(h.current(), 'void main() { x; }');
assert.equal(h.history().length, 2);
assert.equal(h.lastErrorCount(), 1);
});
本机实测(Node v26.5.0,node --test test/10-2-shader-compile-hotload.test.ts):tests 6 · pass 6 · fail 0,耗时约 40 ms。
前两条钉编译状态转移。第三条钉版本推进。第四条钉失败保留旧版。第五、六条钉替换判定与未使用跳过。
小结
- 热重载 = 版本追踪 + 状态机 + 替换判定。
- 编译失败保留旧版本,编辑器不黑屏。
- 版本比较避免乱序覆盖。
- 即时反馈靠这条重载链路。
阅读导航:上一节:节点图材质:从图到着色参数 · 下一节:材质实时预览与变体 。
继续阅读
探索更多技术文章
浏览归档,发现更多关于系统设计、工具链和工程实践的内容。