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(render): a mesh draw finds its cached program without searching shader source
yhh49b15858b50a
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 233be3d20..ef981e451 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -39,6 +39,7 @@ published separately; it ships inside the editor.### Changed+- **A mesh draw no longer searches its shader's source or rebuilds a feature list to find a program it already has.** `MaterialStore::meshProgram`, asked once per mesh draw, looked for "ES_GBUFFER" through the whole text of the material's shader every time and built nine strings and a vector before it checked its cache. The mention is found once when the source is stored and the cache is asked first. With the engine's own profiler off, the terrain and water example's frame at a CPU-bound size went from 8.4 to 6.7-6.9 ms (about 19% less), the same programs are chosen, and every render scene draws what it did (262 of 262 on WebGL2, 248 of 248 on WebGPU). Note for anyone reading the frame costs: switching the engine's profiler on (`render()`, `costs()`, `gpu()` on the cooked hook) made that frame 76% dearer, so a per-phase number from it says where time goes, not what it costs; `render.collect.mesh.<scene|depth>.walks/.visits/.ms` now say which pass walked the meshes and how many times (two per frame in that example: the scene and the sun's shadow).- **The engine's wasm is 239KB smaller, with no change to what it does.** Reordering a component's storage (`Sprite.order` and the authored entity order reach the draw list through it) sorted inside `SparseSet<T>::reorderBy`, so each of the hundred-odd component types carried its own copy of `std::stable_sort` and its merge helpers: 275KB, 15% of the engine's code. The sort never looked at the component, so it is one function now (`SparseSetBase::stablePermutation`) and the template only moves the components along the permutation it returns. The web engine is 2,437,989 bytes, now 2,199,086 (238,903 less); the same sort and the same order: 300 random cases with ties and unranked entities came out identical to the old one, and the terrain and water example draws the same frame at the same cost. `ESTELLA_WASM_NAMES=1` keeps the name section through the optimizer, for a build made to find where the bytes are.- **A blank mini-game package no longer carries terrain, water and timelines.** The mini-game build was shipping 47KB of terrain and 37KB of water code (and its component definitions) in a project that uses neither: the SDK's shared chunks hold every entry's code together, and a bundler cannot drop a top-level `defineComponent(...)` or `new SomePlugin()` it cannot tell is pure. The chunks now keep their `/* @__PURE__ */` annotations (the minifier that removed them is set to mangle names only; consumers minify again), and terrain, water and timelines mark what only defines something. A blank WeChat package was 3,345,193 bytes and is now 3,234,518 (111KB less, 79.8% of the 4MB main-package cap down to 77.1%); a project that uses terrain, water or a timeline installs them as before. A gate (`lean-bundle`) fails if a blank package carries them again.- **A multisampled frame is not written out on a tile-based GPU.** The samples of a multisampled target, once resolved and with nothing left to draw on it, the depth of a shadow pass (the map keeps its depth in colour), and the default framebuffer's depth and stencil at the end of a frame are now said not to be needed, which a phone's GPU takes as not writing them to memory. The picture is the same on both backends; it changes nothing measurable on a desktop GPU and is most of a multisampled frame's memory traffic on a phone, which is where it was not measured.diff --git a/src/esengine/renderer/plugins/MeshPlugin.cpp b/src/esengine/renderer/plugins/MeshPlugin.cppindex a5ddaab87..d1996617c 100644--- a/src/esengine/renderer/plugins/MeshPlugin.cpp+++ b/src/esengine/renderer/plugins/MeshPlugin.cpp@@ -1190,6 +1190,32 @@ void MeshPlugin::collect(RenderCollectContext& collect_ctx) {profiler.add("render.collect.mesh.bounds", phase[2]);profiler.add("render.collect.mesh.program", phase[3]);profiler.add("render.collect.mesh.emit", phase[4]);+ // Which pass walked, how many times, and what the walk cost it: the same meshes are visited once per view of the frame, and the+ // total above cannot say which view the time belongs to.+ // The profiler keeps the name pointer, so these are literals, one set per purpose.+ const f64 spent = phase[0] + phase[1] + phase[2] + phase[3] + phase[4];+ switch (ctx.purpose) {+ case RenderPurpose::Scene:+ profiler.add("render.collect.mesh.scene.walks", 1.0);+ profiler.add("render.collect.mesh.scene.visits", static_cast<f64>(walked));+ profiler.add("render.collect.mesh.scene.ms", spent);+ break;+ case RenderPurpose::ShadowDepth:+ profiler.add("render.collect.mesh.depth.walks", 1.0);+ profiler.add("render.collect.mesh.depth.visits", static_cast<f64>(walked));+ profiler.add("render.collect.mesh.depth.ms", spent);+ break;+ case RenderPurpose::Motion:+ profiler.add("render.collect.mesh.motion.walks", 1.0);+ profiler.add("render.collect.mesh.motion.visits", static_cast<f64>(walked));+ profiler.add("render.collect.mesh.motion.ms", spent);+ break;+ default:+ profiler.add("render.collect.mesh.other.walks", 1.0);+ profiler.add("render.collect.mesh.other.visits", static_cast<f64>(walked));+ profiler.add("render.collect.mesh.other.ms", spent);+ break;+ }}// The counts beside the time, because a phase that grew and one whose UNIT// cost grew are different problems and the milliseconds cannot tell themdiff --git a/src/esengine/renderer/store/MaterialStore.cpp b/src/esengine/renderer/store/MaterialStore.cppindex 3176169f3..36d869675 100644--- a/src/esengine/renderer/store/MaterialStore.cpp+++ b/src/esengine/renderer/store/MaterialStore.cpp@@ -76,7 +76,7 @@ u32 MaterialStore::meshProgram(u32 materialId, resource::ResourceManager& resourif (gbuffer) {const MaterialRecord* rec = find(materialId);const auto src = rec && rec->shaderRef.isValid() ? sources_.find(rec->shaderRef.id()) : sources_.end();- gbuffer = src != sources_.end() && src->second.source.find("ES_GBUFFER") != std::string::npos;+ gbuffer = src != sources_.end() && src->second.mentionsGbuffer;}// Keyed by shader AND vertex shape: a layout may not declare an attribute its// shader ignores, so geometry with normals — or posed by bones — needs its own.@@ -87,6 +87,18 @@ u32 MaterialStore::meshProgram(u32 materialId, resource::ResourceManager& resour| (gbuffer ? (1ull << 38) : 0ull)| (tangents && withNormals ? (1ull << 39) : 0ull)| (tipPass ? (1ull << 40) : 0ull);+ // A program already made for this shader and vertex shape is the answer, and almost every draw of a frame asks for one: it is looked up before+ // the feature list is built, since building it (nine strings and the vector that holds them) was most of what a cached answer cost.+ if (const MaterialRecord* rec = find(materialId); rec && rec->shaderRef.isValid()) {+ if (const auto bucket = mesh_programs_.find(rec->shaderRef.id()); bucket != mesh_programs_.end()) {+ if (const auto hit = bucket->second.find(shape); hit != bucket->second.end()) {+ const Shader* shader = resources.getShader(hit->second);+ const u32 cached = shader ? shader->getProgramId() : 0;+ if (gbuffer && cached != 0) gbuffer_programs_.insert(cached);+ return cached;+ }+ }+ }std::vector<std::string> features{ "MESH" };if (withNormals) features.push_back("MESH_NORMALS");if (skinned) features.push_back("SKINNED");diff --git a/src/esengine/renderer/store/MaterialStore.hpp b/src/esengine/renderer/store/MaterialStore.hppindex f118c7262..e28dfe88f 100644--- a/src/esengine/renderer/store/MaterialStore.hpp+++ b/src/esengine/renderer/store/MaterialStore.hpp@@ -198,7 +198,10 @@ public:/// be compiled for another vertex source later. Without it the store holds only/// the assembled program and a variant would need the author's file again.void rememberSource(resource::ShaderHandle shader, std::string source, std::string features) {- if (shader.isValid()) sources_[shader.id()] = { std::move(source), std::move(features) };+ if (shader.isValid()) {+ const bool mentionsGbuffer = source.find("ES_GBUFFER") != std::string::npos;+ sources_[shader.id()] = { std::move(source), std::move(features), mentionsGbuffer };+ }}/// The program that draws @p materialId on resident geometry of this shape, compiled@@ -446,6 +449,9 @@ private:struct ShaderSource {std::string source;std::string features;+ /// Whether the source mentions ES_GBUFFER, found once when it is stored: the mesh path asks it for every draw, and the answer was a+ /// substring search through the whole text each time.+ bool mentionsGbuffer = false;};std::unordered_map<u32, ShaderSource> sources_;/// (Handle, vertex source) → the program compiled for it (0 = tried anddiff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjsindex b71b285b7..d69b0c8bc 100644--- a/tools/releaseNotes.mjs+++ b/tools/releaseNotes.mjs@@ -3020,4 +3020,6 @@ export const NOTED = {{"note":"A blank mini-game package no longer carries terrain and water."},"perf(ecs): one stable sort for every component type, 239KB off the engine wasm":{"note":"The engine's wasm is 239KB smaller, with no change to what it does."},+ "perf(render): a mesh draw finds its cached program without searching shader source":+ {"note":"A mesh draw no longer searches its shader's source or rebuilds a feature list"},};