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.

perf(render): a lit surface skips the light slots nothing shines from

yhh6a4c17c173bf
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 2499fe575..2a3c150fa 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -30,6 +30,7 @@ published separately; it ships inside the editor.
### Changed
+- **A lit surface no longer works out the light slots nothing shines from.** The light block holds sixteen lights and a scene has one or two, but the lighting loop shaded all sixteen: every empty slot paid for its distance, its direction and the whole specular lobe, to add zero. A slot with no intensity returns at once now; the picture is the same to the pixel gates on WebGL2 and WebGPU. A frame of the Terrain & Water example is 4 to 6 ms shorter on an RTX 3060 (the trees' shading alone is more than a third faster).
- **The sky's clouds and the fog's light shafts cost a third of what they did.** The cloud layer, the shadow it casts on the ground and the sun's light through the haze are smooth and were worked out for every pixel of the frame; they are worked out at half size now, and each pixel reads them back from the four texels around it, weighted by how near their depth is to its own, so nothing is carried across the edge of a hill or a leaf. The dome, the sun, the moon and the stars, which have an edge, stay full size. The picture is the same to the eye (a mean difference of a third of a level of 255 on the Terrain & Water example); a frame of it is 5 ms shorter on an RTX 3060 (25.6 to 20.1). The sky and the height fog are two passes each in a chain now, `sky_clouds` and `sky_composite`, `fog_volume` and `fog_composite`.
- **The water surface leaves early where it is not water.** A shader that may discard late is not let test depth early, so the water behind a hill ran all of it; the test that decides whether a pixel is water now comes before the waves and the light are worked out. The picture is the same, a frame of the Terrain & Water example about a millisecond shorter or more on an RTX 3060. The WebGL2 shader only.
- **A lit surface takes its shadow in fewer operations.** The shadow lookup of a light with a size (the sun's penumbra) worked a `sin`, a `cos` and a `sqrt` for each of its forty-eight taps and a whole matrix for each of the sixteen that look for a blocker; the taps are a table now, turned once a pixel, and the matrix is applied to the light's direction once. Inside a shadow, where every blocker tap hits, it returns at once, and a penumbra a texel or two wide is averaged over 8 or 16 taps, not 32. The picture is the same to the pixel gates on WebGL2 and WebGPU; a frame of the Terrain & Water example is 3 to 10 ms shorter on an RTX 3060.
diff --git a/docs/TERRAIN_WATER_GPU_COST.md b/docs/TERRAIN_WATER_GPU_COST.md
index 3e71b6078..1ea69ef71 100644
--- a/docs/TERRAIN_WATER_GPU_COST.md
+++ b/docs/TERRAIN_WATER_GPU_COST.md
@@ -415,3 +415,28 @@
- 这些读数与消融一致:树的叶片卡片(镂空、阴影接收、4× MSAA 边缘像素)是 scene pass 的最大一块;没有哪一个“着色器阶段”是主要成本,是三者相乘(像素数 × 阴影查找 × 多重采样)。
没有量的:植物着色器里每个阶段(光照、阴影、风、透射)各占多少——要做就像之前量阴影接收那样,剪一个着色器重编一次;顶点阶段(三角形数 0.75M 到 2M)的成本也没有单独量。
+
+### 植物着色器(Model)里各阶段占多少(2026-10-26)
+
+方法:把 Model 着色器(SDK 里的 `builtinShaders.ts`)或光照函数(`ShaderParser.cpp`)里的一个阶段剪掉,各导出一个包,在同一机位用 `--detail` 比较 `scene/Model` 一段。机位 1(Model 段 12.27 ms,`output/model-variants.mjs`):
+
+| 剪掉 | Model 段 | 这一阶段的成本 |
+|---|---|---|
+| 一切(只剩底色贴图、镂空测试、光栅与多重采样) | 3.01 | **下限 3.0**(25%) |
+| 整个灯循环 | 4.48 | **灯循环 7.8**(64%) |
+| 其中阴影接收 | 7.82 | 阴影接收 4.45,其余灯循环(空槽的光照数学)3.3 |
+| 环境反射 | 11.81 | 0.46 |
+| 法线贴图与切线、镜面抗锯齿 | 11.88 | 0.39 |
+| 遮挡、金属粗糙度、自发光三张贴图 | 11.98 | 0.29 |
+| 环境光 | 12.44 | 0 |
+
+默认机位(Model 5.64):下限 1.08、灯循环 3.95(阴影接收 2.25)、环境 0.58。**风、透射、半透明都不在 Model 里**:风是顶点里的另一个着色器,透射与半透明在这里是 uniform 分支,植物的材质不开,成本为 0。
+
+**意外的一块:3.3 ms 是空灯槽。**灯块有 16 个槽,场景只有一个太阳,但灯循环对 16 个槽都做一遍:距离、方向、整个高光叶(GGX、菲涅尔)都算,只是乘以 0 强度加零。一个强度为 0 的槽现在直接返回(GLSL 与 WGSL 一份),结果逐位相同:
+
+| 机位 | 之前 Model 段 | 之后 | 整图 |
+|---|---|---|---|
+| 1 | 12.27 | 7.07 | 28.2 → 22.3 |
+| 0 | 5.64 | 3.21 | 18.9 → 14.8 |
+
+比“少算 3.3 ms 的数学”省得更多(机位 1 少了 5.2 ms),因为着色器变短后占用率也上来了。画面与之前逐像素比,平均差 0.09/255。这条对每个有光照的网格都成立,不只这个示例。
diff --git a/src/esengine/resource/ShaderParser.cpp b/src/esengine/resource/ShaderParser.cpp
index eb04de0e1..c815c606e 100644
--- a/src/esengine/resource/ShaderParser.cpp
+++ b/src/esengine/resource/ShaderParser.cpp
@@ -1989,6 +1989,8 @@ fn shadeLight(pd : vec4f, col : vec4f, sp : vec4f, sh : vec4f, sm : vec4f, fo :
var lit = vec3f(0.0);
var gloss = vec3f(0.0);
var extra = vec3f(0.0);
+ // A slot nothing lights from adds exactly nothing (see the GLSL twin).
+ if (col.a == 0.0) { return LightTerm(lit, gloss, extra); }
{
var L : vec3f;
var atten : f32;
@@ -3287,6 +3289,9 @@ ShaderParser::AssembledStage ShaderParser::assembleStageEx(const ParsedShader& p
" in highp float a, in highp vec3 F0, in highp float NdotV, in highp float specular,\n"
" in highp float roughness, in SurfaceExt ext, inout highp vec3 lit, inout highp vec3 gloss,\n"
" inout highp vec3 extra) {\n"
+ // A slot nothing lights from (the block holds sixteen and a scene has one or two): every term below is its colour times
+ // its intensity, so it adds exactly nothing, but the lobe is worked out for it all the same without this.
+ " if (col.a == 0.0) return;\n"
" highp vec3 L;\n"
" highp float atten;\n"
" highp vec2 target = pd.xy;\n" // shadow-ray aim point (light position by default)
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index e12489895..dc48726cd 100644
--- a/tools/releaseNotes.mjs
+++ b/tools/releaseNotes.mjs
@@ -2988,4 +2988,6 @@ export const NOTED = {
{"note":"The water surface leaves early where it is not water."},
"perf(postprocess): the sky's clouds and the fog's shafts are worked out at half size and read back by depth":
{"note":"The sky's clouds and the fog's light shafts cost a third of what they did."},
+ "perf(render): a lit surface skips the light slots nothing shines from":
+ {"note":"A lit surface no longer works out the light slots nothing shines from."},
};