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
perf: size shadow atlas storage to occupied tiles
yhh410ab0181ac5
diff --git a/src/esengine/renderer/frame/RenderFrame.cpp b/src/esengine/renderer/frame/RenderFrame.cppindex 031b6b89a..8e7a531b4 100644--- a/src/esengine/renderer/frame/RenderFrame.cpp+++ b/src/esengine/renderer/frame/RenderFrame.cpp@@ -2092,6 +2092,8 @@ void RenderFrame::buildShadowPlan(ecs::Registry& registry) {shadow_plan_.clear();shadow_plan_.atlasSize = shadow_atlas_size_;shadow_plan_.cellSize = shadow_cell_size_;+ ES_PROFILE_COUNTER("render.shadow.atlasBudget", shadow_atlas_size_);+ ES_PROFILE_COUNTER("render.shadow.atlasSize", 0);context_.lights().setShadowTiles(nullptr, nullptr, 0, glm::vec4(0.0f));if (!in_frame_ || shadow_casters_.empty() || !device_.isDeviceUsable()) return;@@ -2194,6 +2196,7 @@ void RenderFrame::buildShadowPlan(ecs::Registry& registry) {}}+ const u32 atlasSize = shadow_atlas_.occupiedSize();glm::mat4 matrices[MAX_SHADOW_TILES];glm::vec4 tiles[MAX_SHADOW_TILES];bool anyTile = false;@@ -2308,7 +2311,7 @@ void RenderFrame::buildShadowPlan(ecs::Registry& registry) {// Where the tile sits, and nothing else. What a comparison against it has// to forgive is not a property of the tile but of the fragment asking, and// is derived there out of this matrix and the size of the square.- tiles[tileIndex] = shadow_atlas_.unitRect(tileIndex);+ tiles[tileIndex] = shadow_atlas_.unitRect(tileIndex, atlasSize);// A face whose frustum misses the whole of the geometry gathers nothing, and// asking each mesh in turn is the only way to find that out otherwise. The@@ -2352,12 +2355,10 @@ void RenderFrame::buildShadowPlan(ecs::Registry& registry) {ES_PROFILE_COUNTER("render.shadow.reduced", shadow_plan_.reducedCasters());if (!anyTile) return;- // Borrowed, not held for the run: 2048² with depth is ~32MB. External because- // the id has to exist NOW rather than when the pass runs — the atlas travels- // into the sort key of every draw that receives it, and the scene collects next.+ // The texture must exist before scene collection puts it in receiver sort keys.rg::TargetDesc desc;- desc.width = shadow_atlas_size_;- desc.height = shadow_atlas_size_;+ desc.width = atlasSize;+ desc.height = atlasSize;desc.depthStencil = true;desc.linearFilter = false;shadow_resource_ = graph_.createExternalTarget(desc);@@ -2367,9 +2368,10 @@ void RenderFrame::buildShadowPlan(ecs::Registry& registry) {}shadow_texture_id_ = static_cast<u32>(graph_.textureOf(shadow_resource_));+ ES_PROFILE_COUNTER("render.shadow.atlasSize", atlasSize);context_.lights().setShadowTiles(matrices, tiles, shadow_atlas_.tileCount(),- glm::vec4(1.0f, 1.0f / static_cast<f32>(shadow_atlas_size_), 0.0f, 0.0f));+ glm::vec4(1.0f, 1.0f / static_cast<f32>(atlasSize), 0.0f, 0.0f));// Which of them each light reads. Without it a fragment would test against// every map in the atlas, including ones rendered from where it cannot see.for (usize i = 0; i < shadow_casters_.size(); ++i) {diff --git a/src/esengine/renderer/store/ShadowAtlas.hpp b/src/esengine/renderer/store/ShadowAtlas.hppindex fa617b360..6462c4ea4 100644--- a/src/esengine/renderer/store/ShadowAtlas.hpp+++ b/src/esengine/renderer/store/ShadowAtlas.hpp@@ -18,6 +18,7 @@#include <glm/glm.hpp>+#include <algorithm>#include <vector>namespace esengine {@@ -112,15 +113,32 @@ public:u32 tileCount() const { return static_cast<u32>(tiles_.size()); }const ShadowTile& tile(u32 index) const { return tiles_[index]; }+ // Fit storage to occupied texels without changing tile placement or resolution.+ u32 occupiedSize() const {+ u32 extent = 0;+ for (const ShadowTile& t : tiles_) {+ extent = std::max(extent, std::max(t.x, t.y) + t.size);+ }+ if (extent == 0) {+ return 0;+ }+ u32 size = cell_;+ while (size < extent) {+ size = size > size_ / 2 ? size_ : size * 2;+ }+ return size;+ }+/*** @brief Tile @p index as a fraction of the atlas: (origin.x, origin.y, side, 0).+ * @param textureSize Physical texture side; zero uses the allocation budget.* @details The origin is the low corner in the convention the atlas is RENDERED* in, which is GL's. A backend that samples the other way up turns it* over where it samples — see the WGSL twin of shadowFactor3D.*/- glm::vec4 unitRect(u32 index) const {+ glm::vec4 unitRect(u32 index, u32 textureSize = 0) const {const ShadowTile& t = tiles_[index];- const f32 inv = 1.0f / static_cast<f32>(size_);+ const f32 inv = 1.0f / static_cast<f32>(textureSize > 0 ? textureSize : size_);return {static_cast<f32>(t.x) * inv, static_cast<f32>(t.y) * inv,static_cast<f32>(t.size) * inv, 0.0f};}diff --git a/tests/renderer/test_shadow_atlas.cpp b/tests/renderer/test_shadow_atlas.cppindex 592c5e4c1..2dca2a284 100644--- a/tests/renderer/test_shadow_atlas.cpp+++ b/tests/renderer/test_shadow_atlas.cpp@@ -204,6 +204,53 @@ int main() {CHECK(report.deniedCasters() + report.reducedCasters() > 0,"three two-tile claims do not all fit in four cells, and the report says which");+ ShadowAtlas fitted(4096, 1024);+ CHECK(fitted.occupiedSize() == 0, "an empty atlas needs no texture");+ fitted.allocate(1, 2, why);+ CHECK(fitted.occupiedSize() == 2048, "one high-quality sun keeps its 2048-texel map");+ CHECK(isRect(fitted.unitRect(0, fitted.occupiedSize()), 0, 0, 1),+ "a single sun fills the fitted texture");+ CHECK(fitted.allocate(4, 2, why) < 0 && fitted.occupiedSize() == 2048,+ "a partially allocated refusal does not grow the texture");+ CHECK(fitted.allocate(3, 2, why) == 1 && fitted.occupiedSize() == 4096,+ "fitting storage preserves the full cascade budget");+ fitted.reset();+ CHECK(fitted.occupiedSize() == 0, "reset releases the previous occupied extent");+ fitted.allocate(1, 1, why);+ CHECK(fitted.occupiedSize() == 1024, "a spot uses one cell after a full frame");+ fitted.allocate(1, 1, why);+ CHECK(fitted.occupiedSize() == 2048, "the second spot grows the texture without moving tiles");+ fitted.allocate(1, 1, why);+ CHECK(fitted.occupiedSize() == 4096, "a third column rounds storage up to the next texture size");+ CHECK(tooSmallForCube.occupiedSize() == 0, "a refused cube leaves no storage requirement");++ // UVs and PCF steps must still address the same texels at every quality budget.+ for (u32 budget = 512; budget <= 4096; budget *= 2) {+ for (u32 cell = 128; cell <= budget / 2; cell *= 2) {+ ShadowAtlas sampled(budget, cell);+ for (u32 count = 0; count < MAX_SHADOW_TILES; ++count) {+ if (sampled.allocate(1, count % 3 == 0 ? 2 : 1, why) < 0) {+ continue;+ }+ const u32 side = sampled.occupiedSize();+ bool contained = side > 0 && side <= budget && (side & (side - 1)) == 0;+ bool sameTexels = true;+ for (u32 i = 0; i < sampled.tileCount(); ++i) {+ const ShadowTile& tile = sampled.tile(i);+ const glm::vec4 rect = sampled.unitRect(i, side);+ contained &= tile.x + tile.size <= side && tile.y + tile.size <= side;+ for (const f32 uv : {0.0f, 0.25f, 0.5f, 0.75f, 1.0f}) {+ sameTexels &= near((rect.x + uv * rect.z) * side, tile.x + uv * tile.size);+ sameTexels &= near((rect.y + uv * rect.z) * side, tile.y + uv * tile.size);+ }+ sameTexels &= near((1.0f / side) / rect.z, 1.0f / tile.size);+ }+ CHECK(contained, "fitted storage contains every mixed-size tile within budget");+ CHECK(sameTexels, "fitted UVs and PCF offsets preserve pixel resolution");+ }+ }+ }+std::printf(g_failures ? "\n%d check(s) failed\n" : "\nall checks passed\n", g_failures);return g_failures == 0 ? 0 : 1;}diff --git a/tools/renderScenes.mjs b/tools/renderScenes.mjsindex fd9ca7b93..444b50d2f 100644--- a/tools/renderScenes.mjs+++ b/tools/renderScenes.mjs@@ -199,7 +199,7 @@ export const SCENES = [// A mesh casting on a mesh: a floor 200 units below a blocker, sun leaning 0.5 in// x, so the shadow lands 100 along +x and x 75..175 is floor the light cannot see.// The blocker itself stays lit — shadowing one's own caster is the classic bug.- { id: "mesh-shadow", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_SCENE: "/scenes/mesh-shadow.esscene", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "2", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.708,\"y\":0.5,\"rgb\":[0,0,0],\"tol\":20},{\"x\":0.167,\"y\":0.5,\"rgb\":[228,228,228],\"tol\":20},{\"x\":0.5,\"y\":0.5,\"rgb\":[228,228,228],\"tol\":20}]" } },+ { id: "mesh-shadow", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_SCENE: "/scenes/mesh-shadow.esscene", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "2", ESTELLA_VERIFY_COUNTERS: "{\"render.shadow.atlasBudget\":2048,\"render.shadow.atlasSize\":1024,\"render.targets.bytes\":8388608}", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.708,\"y\":0.5,\"rgb\":[0,0,0],\"tol\":20},{\"x\":0.167,\"y\":0.5,\"rgb\":[228,228,228],\"tol\":20},{\"x\":0.5,\"y\":0.5,\"rgb\":[228,228,228],\"tol\":20}]" } },// The same shadow turned 30 degrees with a hard source, so its edges cross the map's// texel grid; a filter that draws that grid into them strays by a quarter pixel.{ id: "mesh-shadow-tilt", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_SCENE: "/scenes/mesh-shadow-tilt.esscene", ESTELLA_VERIFY_W: "512", ESTELLA_VERIFY_H: "512", ESTELLA_VERIFY_STEPS: "2", ESTELLA_VERIFY_EDGE: "{\"box\":[0.6,0.36,0.9,0.52],\"lit\":228,\"dark\":0,\"maxJag\":0.12}", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.167,\"y\":0.5,\"rgb\":[228,228,228],\"tol\":20}]" } },@@ -241,7 +241,7 @@ export const SCENES = [// A CONE casting, through the tile and the pass a cascade uses. The shadow is offset// from the caster because a point light projects rather than sweeps; the probes either// side of x = 162 are its edge, and the caster is lit because it must not shadow itself.- { id: "spot-shadow", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_SCENE: "/scenes/spot-shadow.esscene", ESTELLA_VERIFY_MANIFEST: "/scenes/spot-shadow.textures.json", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "2", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.7,\"y\":0.5,\"rgb\":[0,0,0],\"tol\":20},{\"x\":0.8,\"y\":0.5,\"rgb\":[155,155,155],\"tol\":25},{\"x\":0.25,\"y\":0.5,\"rgb\":[178,178,178],\"tol\":25},{\"x\":0.5,\"y\":0.5,\"rgb\":[196,196,196],\"tol\":25}]" } },+ { id: "spot-shadow", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_SCENE: "/scenes/spot-shadow.esscene", ESTELLA_VERIFY_MANIFEST: "/scenes/spot-shadow.textures.json", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "2", ESTELLA_VERIFY_COUNTERS: "{\"render.shadow.atlasBudget\":2048,\"render.shadow.atlasSize\":512,\"render.targets.bytes\":2097152}", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.7,\"y\":0.5,\"rgb\":[0,0,0],\"tol\":20},{\"x\":0.8,\"y\":0.5,\"rgb\":[155,155,155],\"tol\":25},{\"x\":0.25,\"y\":0.5,\"rgb\":[178,178,178],\"tol\":25},{\"x\":0.5,\"y\":0.5,\"rgb\":[196,196,196],\"tol\":25}]" } },// A POINT light opens no cone, so its map is a cube and a fragment is shadowed by the face// it lands in: report one tile for every tile and both shadows come back lit. The casters// are probed too, and the sixth face never renders — nothing above the lamp is a mesh.