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: add root transform fast path and remove redundant GL calls in flush

esengine2d93fd0722a8
diff --git a/src/esengine/ecs/TransformSystem.hpp b/src/esengine/ecs/TransformSystem.hpp
index 5e9783ac4..bc1faf6f7 100644
--- a/src/esengine/ecs/TransformSystem.hpp
+++ b/src/esengine/ecs/TransformSystem.hpp
@@ -81,11 +81,35 @@ private:
return;
}
- updateEntityTransform(registry, entity, local, glm::mat4(1.0f), true);
+ updateRootTransform(registry, entity, local);
}
});
}
+ void updateRootTransform(Registry& registry, Entity entity, const LocalTransform& local) {
+ auto& world = registry.get<WorldTransform>(entity);
+ world.position = local.position;
+ world.rotation = local.rotation;
+ world.scale = local.scale;
+
+ if (registry.has<TransformDirty>(entity)) {
+ registry.remove<TransformDirty>(entity);
+ }
+
+ auto* children = registry.tryGet<Children>(entity);
+ if (children) {
+ glm::mat4 worldMatrix = math::compose(world.position, world.rotation, world.scale);
+ for (Entity child : children->entities) {
+ if (registry.valid(child)) {
+ auto* childLocal = registry.tryGet<LocalTransform>(child);
+ if (childLocal) {
+ updateEntityTransform(registry, child, *childLocal, worldMatrix, true);
+ }
+ }
+ }
+ }
+ }
+
void updateEntityTransform(Registry& registry, Entity entity,
const LocalTransform& local,
const glm::mat4& parentWorldMatrix,
@@ -96,11 +120,12 @@ private:
auto* children = registry.tryGet<Children>(entity);
if (children) {
const auto& world = registry.get<WorldTransform>(entity);
+ glm::mat4 worldMatrix = math::compose(world.position, world.rotation, world.scale);
for (Entity child : children->entities) {
if (registry.valid(child)) {
auto* childLocal = registry.tryGet<LocalTransform>(child);
if (childLocal) {
- updateEntityTransform(registry, child, *childLocal, world.matrix, false);
+ updateEntityTransform(registry, child, *childLocal, worldMatrix, false);
}
}
}
@@ -112,7 +137,6 @@ private:
glm::mat4 worldMatrix = parentWorldMatrix * localMatrix;
auto& world = registry.get<WorldTransform>(entity);
- world.matrix = worldMatrix;
math::decompose(worldMatrix, world.position, world.rotation, world.scale);
if (isDirty && !parentDirty) {
diff --git a/src/esengine/ecs/components/Transform.hpp b/src/esengine/ecs/components/Transform.hpp
index 46027f5fd..bfdf5352d 100644
--- a/src/esengine/ecs/components/Transform.hpp
+++ b/src/esengine/ecs/components/Transform.hpp
@@ -102,19 +102,15 @@ struct LocalTransform {
*/
ES_COMPONENT()
struct WorldTransform {
- /** @brief Combined world-space transformation matrix */
- ES_PROPERTY()
- glm::mat4 matrix{1.0f};
-
- /** @brief World-space position (extracted from matrix for convenience) */
+ /** @brief World-space position */
ES_PROPERTY()
glm::vec3 position{0.0f, 0.0f, 0.0f};
- /** @brief World-space rotation (extracted from matrix) */
+ /** @brief World-space rotation */
ES_PROPERTY()
glm::quat rotation{1.0f, 0.0f, 0.0f, 0.0f};
- /** @brief World-space scale (extracted from matrix) */
+ /** @brief World-space scale */
ES_PROPERTY()
glm::vec3 scale{1.0f, 1.0f, 1.0f};
diff --git a/src/esengine/renderer/Renderer.cpp b/src/esengine/renderer/Renderer.cpp
index 38fd8eece..e24d1cab7 100644
--- a/src/esengine/renderer/Renderer.cpp
+++ b/src/esengine/renderer/Renderer.cpp
@@ -453,12 +453,6 @@ void BatchRenderer2D::flush() {
shader->setUniform("u_projection", data_->projection);
data_->vao->bind();
- data_->vbo->bind();
- constexpr u32 STRIDE = sizeof(BatchVertex);
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, STRIDE, reinterpret_cast<void*>(0));
- glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, STRIDE, reinterpret_cast<void*>(12));
- glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, STRIDE, reinterpret_cast<void*>(28));
- glVertexAttribPointer(3, 1, GL_FLOAT, GL_FALSE, STRIDE, reinterpret_cast<void*>(36));
if (hasQuads) {
auto ib = data_->vao->getIndexBuffer();