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(lighting): a lit quad says which lights can reach it and its pixels work out only those - the reach is settled where the sprite is submitted, quad by quad, and rides beside the layer in the lane the layer already had; 448 lit tiles four deep under sixteen local lamps go from 2.03 ms to 0.58 on WebGL2 and from 2.72 to 0.76 on WebGPU with the same pixels, and a render gate pins the light slots spared

yhhd6e075ce4203
diff --git a/docs/GPU_PERFORMANCE_OPTIONS.md b/docs/GPU_PERFORMANCE_OPTIONS.md
index 78405ee14..556f46276 100644
--- a/docs/GPU_PERFORMANCE_OPTIONS.md
+++ b/docs/GPU_PERFORMANCE_OPTIONS.md
@@ -137,7 +137,22 @@ ESTELLA_VERIFY_GPU_PASSES=print node tools/run-electron.mjs tools/render-host/ru
**试了反而慢的(D3D):**把点光和聚光合成一个分支、聚光锥再判一次(5.7 ms);照不到的灯提前返回(同上)。保留了和通用函数一样的三个分支。
-**剩下的 3.9 ms 是每盏灯的算术**(2.6 亿次,每次约 15 皮秒),再改着色器文本只有零头。真正的杠杆是**少算灯**:现在每个受光像素对场景里的每一盏灯都算一遍,不管照不照得到。按精灵(或按屏幕分块,分块灯已经有这套掩码)在 CPU 上剔掉照不到的灯,像素只算照得到它的那几盏——真实的 2D 场景里灯是局部的,这才是数量级的差别。没有做。
+**剩下的 3.9 ms 是每盏灯的算术**(2.6 亿次,每次约 15 皮秒),再改着色器文本只有零头。真正的杠杆是**少算灯**:现在每个受光像素对场景里的每一盏灯都算一遍,不管照不照得到。按精灵(或按屏幕分块,分块灯已经有这套掩码)在 CPU 上剔掉照不到的灯,像素只算照得到它的那几盏——真实的 2D 场景里灯是局部的,这才是数量级的差别。**已做,见下。**
+
+**按四边形剔灯(2026-11-12 落地):**灯在任何插件提交之前就收集好了,所以精灵提交时知道循环里每盏灯的位置和半径。每个受光四边形(平铺精灵是每块一个,不是整张)拿自己顶点的包围盒去比:灯的衰减从半径起恒为零(线性、幂次、平方反比都是),整个盒子在半径外的灯对它恰好贡献零。照得到的灯记成 15 位掩码,和图层一起放进顶点里原来只放图层的那一格(`-(图层 + 129 + 512 × 掩码)`,负号表示带了掩码;正数是旧写法,所有灯照算),片元循环里掩码没置位的灯直接跳过。第 16 盏灯没有位,总是算;分块灯(16 盏之外)本来就有自己的掩码,不变。`lightReachMask2D` / `packSpriteLightLane`(`LightConstants.hpp`),`SpritePlugin.cpp` 里算,`applyLighting2DLayer` 里解。
+
+量法:`fixtures/scenes/sprite-lit-local-cost.esscene`(四张叠在一起的 40 单位受光砖,16 盏半径 90 的点光排成 4×4),渲染宿主铺成 28×16 = 448 格(`ESTELLA_VERIFY_SCALE={"copies":448,"cols":28,"spacing":[40,40]}`),1080p:
+
+| 448 格 × 4 层,16 盏局部灯 | 之前 | 之后 |
+|---|---|---|
+| WebGL2 | 2.03 ms | 0.58 ms |
+| WebGPU | 2.72 ms | 0.76 ms |
+
+- 画面:前后两帧逐像素相同(960×540,最大通道差 0)。
+- 每个四边形平均只剩不到一盏灯要算(26880 个灯槽里省掉 25320 个)。
+- **代价:**没有灯可剔时(上面那个四盏灯都盖满全屏的压力场景)多一次位判断,4.70 → 5.09 ms(+8%)。把位判断并进灯函数已有的提前返回里试过:压力场景 4.95,但局部灯场景变成 0.88,所以留在循环里。
+- 防回归:`sprite-lit-reach` 渲染场景钉住 `render.lights2d.spared`(1024 个四边形省掉 14476 个灯槽)和三个点(两处灯下必须亮,一处灯外必须黑),两个后端都过。像素看不出剔没剔,所以钉的是计数。
+- **没有做的:**只有精灵插件写掩码;自带顶点流的受光东西(瓦片地图条带、粒子、Spine)仍然所有灯照算。聚光只按半径剔,不按锥角。手机没有量。
## 这一轮已经量过、**不是**杠杆的(别再花时间)
diff --git a/fixtures/scenes/sprite-lit-local-cost.esscene b/fixtures/scenes/sprite-lit-local-cost.esscene
new file mode 100644
index 000000000..d7e92659c
--- /dev/null
+++ b/fixtures/scenes/sprite-lit-local-cost.esscene
@@ -0,0 +1,27 @@
+{
+ "version": 4,
+ "name": "SpriteLitLocalCost",
+ "entities": [
+ {"id":0,"name":"Camera","parent":null,"children":[],"visible":true,"components":[{"type":"Transform","data":{"position":{"x":0,"y":0,"z":10}}},{"type":"Camera","data":{"projectionType":1,"orthoSize":300,"isActive":true,"priority":0}}]},
+ {"id":1,"name":"Tile0","parent":null,"children":[],"visible":true,"components":[{"type":"Transform","data":{"position":{"x":0,"y":0,"z":0}}},{"type":"Sprite","data":{"size":{"x":40,"y":40},"color":{"r":0.9,"g":0.8,"b":0.7,"a":0.5},"lit":true}}]},
+ {"id":2,"name":"Tile1","parent":null,"children":[],"visible":true,"components":[{"type":"Transform","data":{"position":{"x":0,"y":0,"z":0}}},{"type":"Sprite","data":{"size":{"x":40,"y":40},"color":{"r":0.9,"g":0.8,"b":0.7,"a":0.5},"lit":true}}]},
+ {"id":3,"name":"Tile2","parent":null,"children":[],"visible":true,"components":[{"type":"Transform","data":{"position":{"x":0,"y":0,"z":0}}},{"type":"Sprite","data":{"size":{"x":40,"y":40},"color":{"r":0.9,"g":0.8,"b":0.7,"a":0.5},"lit":true}}]},
+ {"id":4,"name":"Tile3","parent":null,"children":[],"visible":true,"components":[{"type":"Transform","data":{"position":{"x":0,"y":0,"z":0}}},{"type":"Sprite","data":{"size":{"x":40,"y":40},"color":{"r":0.9,"g":0.8,"b":0.7,"a":0.5},"lit":true}}]},
+ {"id":5,"name":"Lamp00","parent":null,"children":[],"visible":true,"components":[{"type":"Transform","data":{"position":{"x":-399,"y":-225,"z":0}}},{"type":"Light","data":{"type":0,"color":{"r":1,"g":0.8,"b":0.6,"a":1},"intensity":1,"radius":90}}]},
+ {"id":6,"name":"Lamp01","parent":null,"children":[],"visible":true,"components":[{"type":"Transform","data":{"position":{"x":-133,"y":-225,"z":0}}},{"type":"Light","data":{"type":0,"color":{"r":1,"g":0.8,"b":0.6,"a":1},"intensity":1,"radius":90}}]},
+ {"id":7,"name":"Lamp02","parent":null,"children":[],"visible":true,"components":[{"type":"Transform","data":{"position":{"x":133,"y":-225,"z":0}}},{"type":"Light","data":{"type":0,"color":{"r":1,"g":0.8,"b":0.6,"a":1},"intensity":1,"radius":90}}]},
+ {"id":8,"name":"Lamp03","parent":null,"children":[],"visible":true,"components":[{"type":"Transform","data":{"position":{"x":399,"y":-225,"z":0}}},{"type":"Light","data":{"type":0,"color":{"r":1,"g":0.8,"b":0.6,"a":1},"intensity":1,"radius":90}}]},
+ {"id":9,"name":"Lamp10","parent":null,"children":[],"visible":true,"components":[{"type":"Transform","data":{"position":{"x":-399,"y":-75,"z":0}}},{"type":"Light","data":{"type":0,"color":{"r":1,"g":0.8,"b":0.6,"a":1},"intensity":1,"radius":90}}]},
+ {"id":10,"name":"Lamp11","parent":null,"children":[],"visible":true,"components":[{"type":"Transform","data":{"position":{"x":-133,"y":-75,"z":0}}},{"type":"Light","data":{"type":0,"color":{"r":1,"g":0.8,"b":0.6,"a":1},"intensity":1,"radius":90}}]},
+ {"id":11,"name":"Lamp12","parent":null,"children":[],"visible":true,"components":[{"type":"Transform","data":{"position":{"x":133,"y":-75,"z":0}}},{"type":"Light","data":{"type":0,"color":{"r":1,"g":0.8,"b":0.6,"a":1},"intensity":1,"radius":90}}]},
+ {"id":12,"name":"Lamp13","parent":null,"children":[],"visible":true,"components":[{"type":"Transform","data":{"position":{"x":399,"y":-75,"z":0}}},{"type":"Light","data":{"type":0,"color":{"r":1,"g":0.8,"b":0.6,"a":1},"intensity":1,"radius":90}}]},
+ {"id":13,"name":"Lamp20","parent":null,"children":[],"visible":true,"components":[{"type":"Transform","data":{"position":{"x":-399,"y":75,"z":0}}},{"type":"Light","data":{"type":0,"color":{"r":1,"g":0.8,"b":0.6,"a":1},"intensity":1,"radius":90}}]},
+ {"id":14,"name":"Lamp21","parent":null,"children":[],"visible":true,"components":[{"type":"Transform","data":{"position":{"x":-133,"y":75,"z":0}}},{"type":"Light","data":{"type":0,"color":{"r":1,"g":0.8,"b":0.6,"a":1},"intensity":1,"radius":90}}]},
+ {"id":15,"name":"Lamp22","parent":null,"children":[],"visible":true,"components":[{"type":"Transform","data":{"position":{"x":133,"y":75,"z":0}}},{"type":"Light","data":{"type":0,"color":{"r":1,"g":0.8,"b":0.6,"a":1},"intensity":1,"radius":90}}]},
+ {"id":16,"name":"Lamp23","parent":null,"children":[],"visible":true,"components":[{"type":"Transform","data":{"position":{"x":399,"y":75,"z":0}}},{"type":"Light","data":{"type":0,"color":{"r":1,"g":0.8,"b":0.6,"a":1},"intensity":1,"radius":90}}]},
+ {"id":17,"name":"Lamp30","parent":null,"children":[],"visible":true,"components":[{"type":"Transform","data":{"position":{"x":-399,"y":225,"z":0}}},{"type":"Light","data":{"type":0,"color":{"r":1,"g":0.8,"b":0.6,"a":1},"intensity":1,"radius":90}}]},
+ {"id":18,"name":"Lamp31","parent":null,"children":[],"visible":true,"components":[{"type":"Transform","data":{"position":{"x":-133,"y":225,"z":0}}},{"type":"Light","data":{"type":0,"color":{"r":1,"g":0.8,"b":0.6,"a":1},"intensity":1,"radius":90}}]},
+ {"id":19,"name":"Lamp32","parent":null,"children":[],"visible":true,"components":[{"type":"Transform","data":{"position":{"x":133,"y":225,"z":0}}},{"type":"Light","data":{"type":0,"color":{"r":1,"g":0.8,"b":0.6,"a":1},"intensity":1,"radius":90}}]},
+ {"id":20,"name":"Lamp33","parent":null,"children":[],"visible":true,"components":[{"type":"Transform","data":{"position":{"x":399,"y":225,"z":0}}},{"type":"Light","data":{"type":0,"color":{"r":1,"g":0.8,"b":0.6,"a":1},"intensity":1,"radius":90}}]}
+ ]
+}
diff --git a/src/esengine/renderer/RenderTypePlugin.hpp b/src/esengine/renderer/RenderTypePlugin.hpp
index 4b588d8f1..878c857a5 100644
--- a/src/esengine/renderer/RenderTypePlugin.hpp
+++ b/src/esengine/renderer/RenderTypePlugin.hpp
@@ -376,6 +376,13 @@ struct RenderCollectContext {
*/
u32 occluded = 0;
+ /**
+ * @brief Light slots the lit quads of this collect were spared: per quad, the loop lights that cannot reach it.
+ * @details What `lightReachMask2D` took off the fragments. A pixel shows nothing of it (a light out of reach added
+ * zero before as well), so this is the number that says the reach still rides in the lane.
+ */
+ u32 lightsSpared = 0;
+
/**
* @brief Which view is looking, where its level choices are kept, and where
* the selections it makes are counted.
diff --git a/src/esengine/renderer/frame/RenderFrame.cpp b/src/esengine/renderer/frame/RenderFrame.cpp
index 8b5642596..0b7bdbc1a 100644
--- a/src/esengine/renderer/frame/RenderFrame.cpp
+++ b/src/esengine/renderer/frame/RenderFrame.cpp
@@ -697,6 +697,8 @@ void RenderFrame::publishFrameCounters() {
ES_PROFILE_COUNTER("render.cull.occluders", occlusion_.boxes());
ES_PROFILE_COUNTER("render.reflection.probes", context_.reflections().count());
ES_PROFILE_COUNTER("render.sprites", stats_.sprites);
+ // Light slots the lit quads were spared: a light out of reach added nothing before either, so no pixel says whether it is skipped.
+ ES_PROFILE_COUNTER("render.lights2d.spared", stats_.lightsSpared);
// The 3D path had no counter at all, and its cost is the one a pixel hides
// best: identical geometry drawn N times merges into ONE instanced call, so
// batch.draws does not move when a cull stops working. These two do.
@@ -2768,6 +2770,7 @@ void RenderFrame::collectAll(ecs::Registry& registry, bool sceneGBuffer) {
// cameras reports every cull each of them made.
stats_.culled += collectCtx.culled;
stats_.occluded += collectCtx.occluded;
+ stats_.lightsSpared += collectCtx.lightsSpared;
velocity_list_.clear();
cover_list_.clear();
diff --git a/src/esengine/renderer/frame/RenderFrame.hpp b/src/esengine/renderer/frame/RenderFrame.hpp
index e71c34671..0fc9b40bd 100644
--- a/src/esengine/renderer/frame/RenderFrame.hpp
+++ b/src/esengine/renderer/frame/RenderFrame.hpp
@@ -104,6 +104,8 @@ public:
u32 culled = 0;
/// Of `culled`, how many an occluder refused rather than the frustum.
u32 occluded = 0;
+ /// Loop lights the lit quads did not have to work out (RenderCollectContext::lightsSpared).
+ u32 lightsSpared = 0;
f32 gpu_time_ms = -1.0f; // -1 when the timer is unavailable
};
diff --git a/src/esengine/renderer/plugins/SpritePlugin.cpp b/src/esengine/renderer/plugins/SpritePlugin.cpp
index 2e46964c8..b6b8cc8af 100644
--- a/src/esengine/renderer/plugins/SpritePlugin.cpp
+++ b/src/esengine/renderer/plugins/SpritePlugin.cpp
@@ -15,7 +15,10 @@
#include "../draw/SpriteDrawResolve.hpp"
#include "../store/LightConstants.hpp"
+#include "../store/LightStore.hpp"
+
#include <algorithm>
+#include <bit>
#include <cmath>
namespace esengine {
@@ -197,9 +200,27 @@ void SpritePlugin::collect(RenderCollectContext& collect_ctx) {
if (sprite.lit || sprite.material != 0) {
const u32 vertexEnd = buffers.vertexBytesUsed(LayoutId::Batch);
auto* vertices = reinterpret_cast<BatchVertex*>(buffers.vertexData(LayoutId::Batch));
- const f32 lane = static_cast<f32>(std::clamp(sprite.layer, -128, 127)) + SPRITE_LAYER_BIAS;
- for (u32 i = vertexStart / sizeof(BatchVertex); i < vertexEnd / sizeof(BatchVertex); ++i) {
- vertices[i].sdfBias = lane;
+ // ...and with it, which of the frame's lights can reach each quad, so a pixel works out the lamps near it and not every
+ // lamp in the level (packSpriteLightLane). Quad by quad: a tiled floor is one sprite and hundreds of quads, and it is
+ // the floor that is lit. The lights were collected before any plugin ran, so the slots are the ones the loop reads.
+ const LightStore& lights = ctx.render_context.lights();
+ const GpuLight* slots = lights.data().lights;
+ const u32 lightCount = lights.count();
+ const u32 first = vertexStart / sizeof(BatchVertex);
+ const u32 end = vertexEnd / sizeof(BatchVertex);
+ const u32 step = (end - first) % 4 == 0 ? 4 : std::max(end - first, 1u);
+ for (u32 q = first; q < end; q += step) {
+ glm::vec2 lo{vertices[q].position};
+ glm::vec2 hi = lo;
+ for (u32 i = q + 1; i < q + step; ++i) {
+ lo = glm::min(lo, glm::vec2{vertices[i].position});
+ hi = glm::max(hi, glm::vec2{vertices[i].position});
+ }
+ const u32 reach = lightReachMask2D(slots, lightCount, lo, hi);
+ collect_ctx.lightsSpared += std::min(lightCount, SPRITE_LIGHT_REACH_BITS)
+ - static_cast<u32>(std::popcount(reach));
+ const f32 lane = packSpriteLightLane(sprite.layer, reach);
+ for (u32 i = q; i < q + step; ++i) vertices[i].sdfBias = lane;
}
}
}
diff --git a/src/esengine/renderer/store/LightConstants.hpp b/src/esengine/renderer/store/LightConstants.hpp
index 698f5c016..9d5ee05f0 100644
--- a/src/esengine/renderer/store/LightConstants.hpp
+++ b/src/esengine/renderer/store/LightConstants.hpp
@@ -185,6 +185,48 @@ struct LightConstants {
glm::vec4 ambientGround{0.0f};
};
+/**
+ * @brief Which of the loop's lights can reach a lit quad rides in the same vertex lane as its layer: `-(layer + 129 + 512 * reach)`.
+ * @details A lit pixel used to work every light of the frame out, the ones across the level included, and that arithmetic is the whole
+ * cost of 2D lighting (docs/GPU_PERFORMANCE_OPTIONS.md, section 9). The quad's box is known where it is submitted, so which
+ * lights reach it is settled there and the fragment skips the rest. NEGATIVE says the lane carries a reach at all: a lane
+ * written by anything older, or by a draw that does not know, is the bare layer and every light is worked out as before.
+ * Fifteen bits, because 9 bits of layer and 15 of reach is what a float carries whole; the sixteenth light is always worked out.
+ */
+inline constexpr f32 SPRITE_LIGHT_REACH_STEP = 512.0f;
+inline constexpr u32 SPRITE_LIGHT_REACH_BITS = 15;
+
+/**
+ * @brief Bit i set when loop light i can put any light on the box @p lo .. @p hi (the plane's XY, the space the vertices are in).
+ * @details A light's ramp is zero from its radius outward whichever way it falls off (distanceFalloff), so a box wholly past the
+ * radius gets exactly nothing from it. A directional light reaches everything; a light of no intensity reaches nothing.
+ * The radius is taken a hair wide: the fragment measures from an interpolated position, not from the corners.
+ */
+inline u32 lightReachMask2D(const GpuLight* lights, u32 count, const glm::vec2& lo, const glm::vec2& hi) {
+ u32 mask = 0;
+ const u32 n = std::min(count, SPRITE_LIGHT_REACH_BITS);
+ for (u32 i = 0; i < n; ++i) {
+ const GpuLight& light = lights[i];
+ if (light.color.a == 0.0f) continue;
+ const bool directional = light.posDir.z >= 0.5f && light.posDir.z < 1.5f;
+ if (!directional) {
+ const f32 dx = std::max(std::max(lo.x - light.posDir.x, light.posDir.x - hi.x), 0.0f);
+ const f32 dy = std::max(std::max(lo.y - light.posDir.y, light.posDir.y - hi.y), 0.0f);
+ const f32 reach = std::max(light.posDir.w, 0.0001f) * 1.001f + 0.001f;
+ if (dx * dx + dy * dy >= reach * reach) continue;
+ }
+ mask |= 1u << i;
+ }
+ return mask;
+}
+
+/** @brief The lane a lit quad carries: its layer and the lights that reach it (lightReachMask2D). */
+inline f32 packSpriteLightLane(i32 layer, u32 reachMask) {
+ const f32 layerLane = static_cast<f32>(std::clamp(layer, -128, 127)) + SPRITE_LAYER_BIAS;
+ const u32 reach = reachMask & ((1u << SPRITE_LIGHT_REACH_BITS) - 1u);
+ return -(layerLane + SPRITE_LIGHT_REACH_STEP * static_cast<f32>(reach));
+}
+
static_assert(sizeof(GpuLight) == 96, "GpuLight must be std140-tight (six vec4s)");
static_assert(sizeof(LightConstants) <= 16384, "the block has to fit the 16 KB every device guarantees a uniform block");
static_assert(sizeof(LightConstants) == 16 + 96 * MAX_LIGHTS + 16
diff --git a/src/esengine/resource/ShaderParser.cpp b/src/esengine/resource/ShaderParser.cpp
index ac1c171a8..9383f419d 100644
--- a/src/esengine/resource/ShaderParser.cpp
+++ b/src/esengine/resource/ShaderParser.cpp
@@ -2494,7 +2494,8 @@ fn shadeLight2D(pd : vec4f, col : vec4f, sp : vec4f, sh : vec4f, sm : vec4f, fo
}
return col.rgb * (col.a * max(dot(N, L), 0.0) * atten);
}
-fn applyLighting2DAO(albedo : vec3f, N : vec3f, worldPos : vec2f, ao : f32) -> vec3f {
+// `reach`: bit i set when loop light i can reach this quad at all (see the GLSL twin); the rest are not worked out.
+fn applyLighting2DReach(albedo : vec3f, N : vec3f, worldPos : vec2f, ao : f32, reach : u32) -> vec3f {
let P = vec3f(worldPos, 0.0);
g_probePos = P;
var lit = indirectIrradiance(N) * ao;
@@ -2513,6 +2514,7 @@ fn applyLighting2DAO(albedo : vec3f, N : vec3f, worldPos : vec2f, ao : f32) -> v
#endif
let lightCount = i32(lc.u_ambient.a);
for (var i = 0; i < lightCount; i++) {
+ if (((reach >> u32(i)) & 1u) == 0u) { continue; }
lit += shadeLight2D(lc.u_lights[i].posDir, lc.u_lights[i].color, lc.u_lights[i].spot, lc.u_lights[i].shadow,
lc.u_lights[i].shadowMap, lc.u_lights[i].falloff, N, P, masked, shadowMask, shapeMask, true);
}
@@ -2541,13 +2543,21 @@ fn applyLighting2DAO(albedo : vec3f, N : vec3f, worldPos : vec2f, ao : f32) -> v
#endif
return albedo * lit;
}
+fn applyLighting2DAO(albedo : vec3f, N : vec3f, worldPos : vec2f, ao : f32) -> vec3f {
+ return applyLighting2DReach(albedo, N, worldPos, ao, 65535u);
+}
fn applyLighting2D(albedo : vec3f, N : vec3f, worldPos : vec2f) -> vec3f {
return applyLighting2DAO(albedo, N, worldPos, 1.0);
}
-// The same, for a sprite that knows its layer: layerLane is the varying the vertex stage wrote (v_lightLayer).
+// The same, for a sprite that knows its layer: layerLane is the varying the vertex stage wrote (v_lightLayer). A negative lane carries,
+// above the layer, which lights reach the quad (packSpriteLightLane); the sixteenth light has no bit and is always worked out.
fn applyLighting2DLayer(albedo : vec3f, N : vec3f, worldPos : vec2f, layerLane : f32) -> vec3f {
- g_surfaceLayer = layerLane;
- return applyLighting2DAO(albedo, N, worldPos, 1.0);
+ let lane = abs(layerLane);
+ let reachLane = floor(lane * 0.001953125);
+ g_surfaceLayer = lane - reachLane * 512.0;
+ var reach = 65535u;
+ if (layerLane < 0.0) { reach = u32(reachLane) + 32768u; }
+ return applyLighting2DReach(albedo, N, worldPos, 1.0, reach);
}
)";
@@ -4132,8 +4142,10 @@ ShaderParser::AssembledStage ShaderParser::assembleStageEx(const ParsedShader& p
" }\n"
" lit += col.rgb * (col.a * max(dot(N, L), 0.0) * atten);\n"
"}\n"
- "highp vec3 applyLighting2DAO(highp vec3 albedo, highp vec3 N, highp vec2 worldPos,\n"
- " highp float ao) {\n"
+ // `reach`: bit i set when loop light i can reach this quad at all (lightReachMask2D, settled where the quad was submitted);
+ // the rest are not worked out. All sixteen bits for a surface nobody settled it for.
+ "highp vec3 applyLighting2DReach(highp vec3 albedo, highp vec3 N, highp vec2 worldPos,\n"
+ " highp float ao, int reach) {\n"
" highp vec3 P = vec3(worldPos, 0.0);\n"
" g_probePos = P;\n"
" highp vec3 lit = indirectIrradiance(N) * ao;\n"
@@ -4152,6 +4164,7 @@ ShaderParser::AssembledStage ShaderParser::assembleStageEx(const ParsedShader& p
"#endif\n"
" int lightCount = int(u_ambient.a);\n"
" for (int i = 0; i < lightCount; ++i) {\n"
+ " if (((reach >> i) & 1) == 0) continue;\n"
" shadeLight2D(u_lights[i].posDir, u_lights[i].color, u_lights[i].spot, u_lights[i].shadow,\n"
" u_lights[i].shadowMap, u_lights[i].falloff, N, P, masked, shadowMask, shapeMask, lit, true);\n"
" }\n"
@@ -4179,13 +4192,21 @@ ShaderParser::AssembledStage ShaderParser::assembleStageEx(const ParsedShader& p
"#endif\n"
" return albedo * lit;\n"
"}\n"
+ "highp vec3 applyLighting2DAO(highp vec3 albedo, highp vec3 N, highp vec2 worldPos,\n"
+ " highp float ao) {\n"
+ " return applyLighting2DReach(albedo, N, worldPos, ao, 65535);\n"
+ "}\n"
"highp vec3 applyLighting2D(highp vec3 albedo, highp vec3 N, highp vec2 worldPos) {\n"
" return applyLighting2DAO(albedo, N, worldPos, 1.0);\n"
"}\n"
// The same, for a sprite that knows its layer: layerLane is the varying the vertex stage wrote (v_lightLayer).
+ // A negative lane carries, above the layer, which lights reach the quad (packSpriteLightLane): 512 apart, so the two come
+ // apart by a power of two and whole. The sixteenth light has no bit and is always worked out.
"highp vec3 applyLighting2DLayer(highp vec3 albedo, highp vec3 N, highp vec2 worldPos, highp float layerLane) {\n"
- " g_surfaceLayer = layerLane;\n"
- " return applyLighting2DAO(albedo, N, worldPos, 1.0);\n"
+ " highp float lane = abs(layerLane);\n"
+ " highp float reach = floor(lane * 0.001953125);\n"
+ " g_surfaceLayer = lane - reach * 512.0;\n"
+ " return applyLighting2DReach(albedo, N, worldPos, 1.0, layerLane < 0.0 ? int(reach) + 32768 : 65535);\n"
"}\n";
// Emitted only for a variant that has the feature, not behind an #ifdef:
// a varying declared in dead text still reads as one to a tool that
diff --git a/tools/renderScenes.mjs b/tools/renderScenes.mjs
index dc755e552..db2fcc4a0 100644
--- a/tools/renderScenes.mjs
+++ b/tools/renderScenes.mjs
@@ -419,6 +419,10 @@ export const SCENES = [
{ id: "static-batch-off", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_PLAY: "1", ESTELLA_VERIFY_SCENE: "/scenes/static-batch-off.esscene", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "12", ESTELLA_VERIFY_DRAW_CALLS: "5", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.1,\"y\":0.2,\"rgb\":[255,0,0],\"tol\":30},{\"x\":0.1,\"y\":0.4,\"rgb\":[255,0,0],\"tol\":30},{\"x\":0.1,\"y\":0.6,\"rgb\":[255,0,0],\"tol\":30},{\"x\":0.1,\"y\":0.8,\"rgb\":[255,0,0],\"tol\":30},{\"x\":0.3,\"y\":0.2,\"rgb\":[255,0,0],\"tol\":30},{\"x\":0.3,\"y\":0.4,\"rgb\":[255,0,0],\"tol\":30},{\"x\":0.3,\"y\":0.6,\"rgb\":[255,0,0],\"tol\":30},{\"x\":0.3,\"y\":0.8,\"rgb\":[255,0,0],\"tol\":30},{\"x\":0.5,\"y\":0.2,\"rgb\":[255,0,0],\"tol\":30},{\"x\":0.5,\"y\":0.4,\"rgb\":[255,0,0],\"tol\":30},{\"x\":0.5,\"y\":0.6,\"rgb\":[255,0,0],\"tol\":30},{\"x\":0.5,\"y\":0.8,\"rgb\":[255,0,0],\"tol\":30},{\"x\":0.7,\"y\":0.2,\"rgb\":[255,0,0],\"tol\":30},{\"x\":0.7,\"y\":0.4,\"rgb\":[255,0,0],\"tol\":30},{\"x\":0.7,\"y\":0.6,\"rgb\":[255,0,0],\"tol\":30},{\"x\":0.7,\"y\":0.8,\"rgb\":[255,0,0],\"tol\":30},{\"x\":0.9,\"y\":0.2,\"rgb\":[255,0,0],\"tol\":30},{\"x\":0.9,\"y\":0.4,\"rgb\":[255,0,0],\"tol\":30},{\"x\":0.9,\"y\":0.6,\"rgb\":[255,0,0],\"tol\":30},{\"x\":0.9,\"y\":0.8,\"rgb\":[255,0,0],\"tol\":30},{\"x\":0.5,\"y\":0.5,\"rgb\":[0,0,0],\"tol\":20}]" } },
{ id: "static-batch-on", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_PLAY: "1", ESTELLA_VERIFY_SCENE: "/scenes/static-batch-on.esscene", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "12", ESTELLA_VERIFY_DRAW_CALLS: "1", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.1,\"y\":0.2,\"rgb\":[255,0,0],\"tol\":30},{\"x\":0.1,\"y\":0.4,\"rgb\":[255,0,0],\"tol\":30},{\"x\":0.1,\"y\":0.6,\"rgb\":[255,0,0],\"tol\":30},{\"x\":0.1,\"y\":0.8,\"rgb\":[255,0,0],\"tol\":30},{\"x\":0.3,\"y\":0.2,\"rgb\":[255,0,0],\"tol\":30},{\"x\":0.3,\"y\":0.4,\"rgb\":[255,0,0],\"tol\":30},{\"x\":0.3,\"y\":0.6,\"rgb\":[255,0,0],\"tol\":30},{\"x\":0.3,\"y\":0.8,\"rgb\":[255,0,0],\"tol\":30},{\"x\":0.5,\"y\":0.2,\"rgb\":[255,0,0],\"tol\":30},{\"x\":0.5,\"y\":0.4,\"rgb\":[255,0,0],\"tol\":30},{\"x\":0.5,\"y\":0.6,\"rgb\":[255,0,0],\"tol\":30},{\"x\":0.5,\"y\":0.8,\"rgb\":[255,0,0],\"tol\":30},{\"x\":0.7,\"y\":0.2,\"rgb\":[255,0,0],\"tol\":30},{\"x\":0.7,\"y\":0.4,\"rgb\":[255,0,0],\"tol\":30},{\"x\":0.7,\"y\":0.6,\"rgb\":[255,0,0],\"tol\":30},{\"x\":0.7,\"y\":0.8,\"rgb\":[255,0,0],\"tol\":30},{\"x\":0.9,\"y\":0.2,\"rgb\":[255,0,0],\"tol\":30},{\"x\":0.9,\"y\":0.4,\"rgb\":[255,0,0],\"tol\":30},{\"x\":0.9,\"y\":0.6,\"rgb\":[255,0,0],\"tol\":30},{\"x\":0.9,\"y\":0.8,\"rgb\":[255,0,0],\"tol\":30},{\"x\":0.5,\"y\":0.5,\"rgb\":[0,0,0],\"tol\":20}]" } },
{ id: "mesh-cost", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_SCENE: "/scenes/mesh-cost.esscene", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "2", ESTELLA_VERIFY_COUNTERS: "{\"render.meshes\":9,\"render.culled\":6,\"render.triangles\":108,\"batch.draws\":1}", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.1667,\"y\":0.1667,\"rgb\":[255,0,0],\"tol\":20},{\"x\":0.5,\"y\":0.5,\"rgb\":[255,0,255],\"tol\":20},{\"x\":0.8333,\"y\":0.8333,\"rgb\":[0,255,0],\"tol\":20},{\"x\":0.3,\"y\":0.5,\"rgb\":[0,0,0],\"tol\":10}]" } },
+ // A lit quad says which lights can reach it, and its pixels work out only those. No pixel shows that: a light out of
+ // reach added nothing before either. So the count of light slots spared is pinned (1024 quads under sixteen lamps,
+ // fifteen of which a quad can decline), beside two points a lamp must still light and one none may.
+ { id: "sprite-lit-reach", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_SCENE: "/scenes/sprite-lit-local-cost.esscene", ESTELLA_VERIFY_MANIFEST: "/scenes/sprite-lit.textures.json", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "2", ESTELLA_VERIFY_SCALE: "{\"copies\":256,\"cols\":16,\"spacing\":[40,40]}", ESTELLA_VERIFY_COUNTERS: "{\"render.sprites\":1024,\"render.lights2d.spared\":14476}", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.7217,\"y\":0.375,\"rgb\":[213,151,100],\"tol\":20},{\"x\":0.2783,\"y\":0.625,\"rgb\":[213,151,100],\"tol\":20},{\"x\":0.5,\"y\":0.5,\"rgb\":[0,0,0],\"tol\":10}]" } },
// A CEILING, not an exact value: at scale the question is whether submission stays O(1)
// in the count, and render.sprites stays exact so a frame that dropped content cannot pass
// under it. The grid is odd (99x99) so the centre probe lands ON a 2-pixel sprite.