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(transform): reuse authored matrix decomposition across frames
yhh8dc6d1d4e81d
diff --git a/src/esengine/ecs/TransformSystem.hpp b/src/esengine/ecs/TransformSystem.hppindex b600b11af..463a35ea7 100644--- a/src/esengine/ecs/TransformSystem.hpp+++ b/src/esengine/ecs/TransformSystem.hpp@@ -80,9 +80,10 @@ public:/*** @brief Collect which entities' composed OUTPUT changed, from now on- * @details Off by default: comparing costs about a third of a compose- * (bench/transform-composition) and only a consumer maintaining an- * incremental structure needs it. Turning it on does not compose.+ * @details Off by default: only a consumer maintaining an incremental+ * structure needs the change mailbox. Matrix equality also protects+ * lazy world-TRS caches independently of tracking. Turning tracking+ * on does not compose.* @note One consumer. The pending set is a mailbox, not a per-reader journal:* whoever acknowledges empties it for everyone.*/@@ -128,8 +129,7 @@ private:std::vector<Entity> dirty_to_clear_;- // Off on the shipped path: composition answers "are they stale", and only a- // caller building an incremental structure pays for "which ones moved".+ // Only a caller building an incremental structure pays to collect changes.bool tracking_ = false;std::vector<Entity>* changedOut_ = nullptr;std::vector<Entity> changed_;@@ -298,12 +298,18 @@ private:flatCollapsed = transform->scale.x == 0 || transform->scale.y == 0|| (task.parentFlatCollapsed && transform->rotation.x == 0 && transform->rotation.y == 0);const auto matrix = *task.parentMatrix * math::compose(transform->position, transform->rotation, transform->scale);+ const bool changed = matrix != transform->cachedMatrix_;if (changedOut_) {++visited_;- if (matrix != transform->cachedMatrix_) changedOut_->push_back(task.entity);+ if (changed) changedOut_->push_back(task.entity);+ }+ // A moving sibling can require a hierarchy pass without+ // changing this entity. Keep its derived TRS warm: a few+ // physics bodies must not force every static mesh to decompose.+ if (changed) {+ transform->cachedMatrix_ = matrix;+ transform->decomposed_ = false;}- transform->cachedMatrix_ = matrix;- transform->decomposed_ = false;} else if (keepStatic) {flatCollapsed = registry.has<FlatGeometryCollapsed>(task.entity);}diff --git a/tests/ecs/test_transform_traversal.cpp b/tests/ecs/test_transform_traversal.cppindex 0794e5008..e31fe1662 100644--- a/tests/ecs/test_transform_traversal.cpp+++ b/tests/ecs/test_transform_traversal.cpp@@ -189,3 +189,23 @@ TEST_CASE("siblings retain their own parent matrices during iterative traversal"const std::vector<Entity> expected{root, siblings[0], left, siblings[1], right};CHECK(system.pendingChanges() == expected);}++TEST_CASE("moving siblings preserve unchanged descendant decomposition caches") {+ Registry r; TransformSystem system;+ const auto root=r.create(), fixed=r.create(), moving=r.create(), leaf=r.create();+ r.emplace<Transform>(root).scale={100,100,100};+ r.emplace<Transform>(fixed).position={2,3,4};+ r.emplace<Transform>(moving).position={5,6,7};+ r.emplace<Transform>(leaf).position={1,0,0};+ setParent(r,fixed,root);setParent(r,moving,root);setParent(r,leaf,moving);+ system.ensureComposed(r);+ for(auto e:{fixed,moving,leaf})r.get<Transform>(e).ensureDecomposed();+ r.get<Transform>(moving).position.x+=2;+ invalidateTransformComposition(); system.ensureComposed(r);+ CHECK(r.get<Transform>(fixed).decomposed_);+ CHECK_FALSE(r.get<Transform>(moving).decomposed_);+ CHECK_FALSE(r.get<Transform>(leaf).decomposed_);+ CHECK(r.get<Transform>(fixed).worldPosition==glm::vec3(200,300,400));+ r.get<Transform>(leaf).ensureDecomposed();+ CHECK(r.get<Transform>(leaf).worldPosition.x==doctest::Approx(800));+}