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: optimize renderer, transform system, and SDK hot paths

esengine87bcc17a123f
diff --git a/sdk/src/app.ts b/sdk/src/app.ts
index 3fa939f0b..50b261db7 100644
--- a/sdk/src/app.ts
+++ b/sdk/src/app.ts
@@ -230,12 +230,9 @@ export class App {
private updateTime(delta: number): void {
const time = this.resources_.get(Time);
- const newTime: TimeData = {
- delta,
- elapsed: time.elapsed + delta,
- frameCount: time.frameCount + 1
- };
- this.resources_.set(Time, newTime);
+ time.delta = delta;
+ time.elapsed += delta;
+ time.frameCount++;
}
}
diff --git a/sdk/src/material.ts b/sdk/src/material.ts
index 63973d603..635c2f985 100644
--- a/sdk/src/material.ts
+++ b/sdk/src/material.ts
@@ -337,6 +337,8 @@ export const Material = {
let materialCallbackRegistered = false;
let uniformBuffer: number = 0;
const UNIFORM_BUFFER_SIZE = 4096;
+const encodedNameCache = new Map<string, Uint8Array>();
+const encoder = new TextEncoder();
function ensureUniformBuffer(): number {
if (uniformBuffer === 0 && module) {
@@ -358,7 +360,11 @@ function serializeUniforms(uniforms: Map<string, UniformValue>): { ptr: number;
for (const [name, value] of uniforms) {
if (offset + 128 > UNIFORM_BUFFER_SIZE) break;
- const nameBytes = new TextEncoder().encode(name);
+ let nameBytes = encodedNameCache.get(name);
+ if (!nameBytes) {
+ nameBytes = encoder.encode(name);
+ encodedNameCache.set(name, nameBytes);
+ }
const nameLen = nameBytes.length;
const namePadded = Math.ceil(nameLen / 4) * 4;
diff --git a/sdk/src/system.ts b/sdk/src/system.ts
index 5e5acaa3c..b52d963d4 100644
--- a/sdk/src/system.ts
+++ b/sdk/src/system.ts
@@ -108,6 +108,7 @@ export function addSystemToSchedule(schedule: Schedule, system: SystemDef): void
export class SystemRunner {
private readonly world_: World;
private readonly resources_: ResourceStorage;
+ private readonly argsCache_ = new Map<symbol, unknown[]>();
constructor(world: World, resources: ResourceStorage) {
this.world_ = world;
@@ -115,13 +116,21 @@ export class SystemRunner {
}
run(system: SystemDef): void {
- const args = system._params.map(param => this.resolveParam(param));
+ let args = this.argsCache_.get(system._id);
+ if (!args) {
+ args = new Array(system._params.length);
+ this.argsCache_.set(system._id, args);
+ }
+
+ for (let i = 0; i < system._params.length; i++) {
+ args[i] = this.resolveParam(system._params[i]);
+ }
system._fn(...args);
- for (const arg of args) {
- if (arg instanceof CommandsInstance) {
- arg.flush();
+ for (let i = 0; i < args.length; i++) {
+ if (args[i] instanceof CommandsInstance) {
+ (args[i] as CommandsInstance).flush();
}
}
}
diff --git a/src/esengine/ecs/TransformSystem.hpp b/src/esengine/ecs/TransformSystem.hpp
index 73a09651e..68ff9c631 100644
--- a/src/esengine/ecs/TransformSystem.hpp
+++ b/src/esengine/ecs/TransformSystem.hpp
@@ -70,13 +70,9 @@ public:
private:
void updateDirtyTransforms(Registry& registry) {
- registry.each<LocalTransform>([&registry](Entity entity, LocalTransform&) {
- if (!registry.has<WorldTransform>(entity)) {
- registry.emplace<WorldTransform>(entity);
- }
- });
-
registry.each<LocalTransform>([&registry, this](Entity entity, LocalTransform& local) {
+ registry.getOrEmplace<WorldTransform>(entity);
+
if (!registry.has<Parent>(entity)) {
bool isStatic = registry.has<TransformStatic>(entity);
bool isDirty = registry.has<TransformDirty>(entity);
@@ -95,16 +91,17 @@ private:
const glm::mat4& parentWorldMatrix,
bool parentDirty) {
bool isDirty = parentDirty || registry.has<TransformDirty>(entity);
- bool isStatic = registry.has<TransformStatic>(entity);
- if (isStatic && !isDirty) {
- if (registry.has<Children>(entity)) {
+ if (registry.has<TransformStatic>(entity) && !isDirty) {
+ auto* children = registry.tryGet<Children>(entity);
+ if (children) {
const auto& world = registry.get<WorldTransform>(entity);
- const auto& children = registry.get<Children>(entity);
- for (Entity child : children.entities) {
- if (registry.valid(child) && registry.has<LocalTransform>(child)) {
- const auto& childLocal = registry.get<LocalTransform>(child);
- updateEntityTransform(registry, child, childLocal, world.matrix, false);
+ for (Entity child : children->entities) {
+ if (registry.valid(child)) {
+ auto* childLocal = registry.tryGet<LocalTransform>(child);
+ if (childLocal) {
+ updateEntityTransform(registry, child, *childLocal, world.matrix, false);
+ }
}
}
}
@@ -118,16 +115,18 @@ private:
world.matrix = worldMatrix;
math::decompose(worldMatrix, world.position, world.rotation, world.scale);
- if (registry.has<TransformDirty>(entity)) {
+ if (isDirty && !parentDirty) {
registry.remove<TransformDirty>(entity);
}
- if (registry.has<Children>(entity)) {
- const auto& children = registry.get<Children>(entity);
- for (Entity child : children.entities) {
- if (registry.valid(child) && registry.has<LocalTransform>(child)) {
- const auto& childLocal = registry.get<LocalTransform>(child);
- updateEntityTransform(registry, child, childLocal, worldMatrix, isDirty);
+ auto* children = registry.tryGet<Children>(entity);
+ if (children) {
+ for (Entity child : children->entities) {
+ if (registry.valid(child)) {
+ auto* childLocal = registry.tryGet<LocalTransform>(child);
+ if (childLocal) {
+ updateEntityTransform(registry, child, *childLocal, worldMatrix, isDirty);
+ }
}
}
}
diff --git a/src/esengine/renderer/Renderer.cpp b/src/esengine/renderer/Renderer.cpp
index 0359e7358..23f56c72f 100644
--- a/src/esengine/renderer/Renderer.cpp
+++ b/src/esengine/renderer/Renderer.cpp
@@ -457,10 +457,9 @@ void BatchRenderer2D::flush() {
data_->vbo->setDataRaw(data_->triVertices.data(), triBytes);
}
- u32 whiteTexture = data_->textureSlots[0];
- for (u32 i = 0; i < MAX_TEXTURE_SLOTS; ++i) {
+ for (u32 i = 0; i < data_->textureSlotIndex; ++i) {
glActiveTexture(GL_TEXTURE0 + i);
- glBindTexture(GL_TEXTURE_2D, i < data_->textureSlotIndex ? data_->textureSlots[i] : whiteTexture);
+ glBindTexture(GL_TEXTURE_2D, data_->textureSlots[i]);
}
shader->bind();
@@ -674,8 +673,23 @@ void BatchRenderer2D::drawNineSlice(const glm::vec2& position, const glm::vec2&
);
};
- // Helper to draw a single slice quad with rotation
- auto drawSlice = [this, textureId, &color, &rotatePoint, rotation](
+ auto resolveTextureSlot = [this, textureId]() -> f32 {
+ if (textureId == 0) return 0.0f;
+ for (u32 i = 0; i < data_->textureSlotIndex; ++i) {
+ if (data_->textureSlots[i] == textureId) {
+ return static_cast<f32>(i);
+ }
+ }
+ if (data_->textureSlotIndex >= MAX_TEXTURE_SLOTS) {
+ flush();
+ }
+ data_->textureSlots[data_->textureSlotIndex] = textureId;
+ return static_cast<f32>(data_->textureSlotIndex++);
+ };
+
+ f32 cachedTexIndex = resolveTextureSlot();
+
+ auto drawSlice = [this, &color, &rotatePoint, &cachedTexIndex, &resolveTextureSlot](
f32 px, f32 py, f32 pw, f32 ph,
f32 uvX, f32 uvY, f32 uvW, f32 uvH) {
@@ -683,28 +697,10 @@ void BatchRenderer2D::drawNineSlice(const glm::vec2& position, const glm::vec2&
if (data_->vertices.size() + data_->triVertices.size() + 4 > MAX_VERTICES) {
flush();
+ cachedTexIndex = resolveTextureSlot();
}
- // Find or add texture slot
- f32 texIndex = 0.0f;
- if (textureId != 0) {
- bool found = false;
- for (u32 i = 0; i < data_->textureSlotIndex; ++i) {
- if (data_->textureSlots[i] == textureId) {
- texIndex = static_cast<f32>(i);
- found = true;
- break;
- }
- }
- if (!found) {
- if (data_->textureSlotIndex >= MAX_TEXTURE_SLOTS) {
- flush();
- }
- data_->textureSlots[data_->textureSlotIndex] = textureId;
- texIndex = static_cast<f32>(data_->textureSlotIndex);
- data_->textureSlotIndex++;
- }
- }
+ f32 texIndex = cachedTexIndex;
// Vertices for this slice (bottom-left origin)
// 0: bottom-left, 1: bottom-right, 2: top-right, 3: top-left
diff --git a/src/esengine/spine/SpineRenderer.cpp b/src/esengine/spine/SpineRenderer.cpp
index 5e8e86c88..625c06b3d 100644
--- a/src/esengine/spine/SpineRenderer.cpp
+++ b/src/esengine/spine/SpineRenderer.cpp
@@ -54,6 +54,26 @@ void SpineRenderer::init() {
indices_.reserve(2048);
world_vertices_.reserve(1024);
+ glGenVertexArrays(1, &vao_);
+ glGenBuffers(1, &vbo_);
+ glGenBuffers(1, &ebo_);
+
+ glBindVertexArray(vao_);
+ glBindBuffer(GL_ARRAY_BUFFER, vbo_);
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo_);
+
+ glEnableVertexAttribArray(0);
+ glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(SpineRenderVertex),
+ reinterpret_cast<void*>(offsetof(SpineRenderVertex, position)));
+ glEnableVertexAttribArray(1);
+ glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(SpineRenderVertex),
+ reinterpret_cast<void*>(offsetof(SpineRenderVertex, uv)));
+ glEnableVertexAttribArray(2);
+ glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, sizeof(SpineRenderVertex),
+ reinterpret_cast<void*>(offsetof(SpineRenderVertex, color)));
+
+ glBindVertexArray(0);
+
initialized_ = true;
ES_LOG_INFO("SpineRenderer initialized");
}
@@ -61,6 +81,12 @@ void SpineRenderer::init() {
void SpineRenderer::shutdown() {
if (!initialized_) return;
+ if (ebo_) { glDeleteBuffers(1, &ebo_); ebo_ = 0; }
+ if (vbo_) { glDeleteBuffers(1, &vbo_); vbo_ = 0; }
+ if (vao_) { glDeleteVertexArrays(1, &vao_); vao_ = 0; }
+ vbo_capacity_ = 0;
+ ebo_capacity_ = 0;
+
vertices_.clear();
indices_.clear();
world_vertices_.clear();
@@ -336,34 +362,23 @@ void SpineRenderer::flushBatch() {
glBindTexture(GL_TEXTURE_2D, current_texture_id_);
shader->setUniform("u_texture", 0);
- u32 vao, vbo, ebo;
- glGenVertexArrays(1, &vao);
- glGenBuffers(1, &vbo);
- glGenBuffers(1, &ebo);
-
- glBindVertexArray(vao);
+ glBindVertexArray(vao_);
- glBindBuffer(GL_ARRAY_BUFFER, vbo);
- glBufferData(GL_ARRAY_BUFFER,
- static_cast<GLsizeiptr>(vertices_.size() * sizeof(SpineRenderVertex)),
- vertices_.data(), GL_STREAM_DRAW);
-
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER,
- static_cast<GLsizeiptr>(indices_.size() * sizeof(u32)),
- indices_.data(), GL_STREAM_DRAW);
-
- glEnableVertexAttribArray(0);
- glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(SpineRenderVertex),
- reinterpret_cast<void*>(offsetof(SpineRenderVertex, position)));
-
- glEnableVertexAttribArray(1);
- glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(SpineRenderVertex),
- reinterpret_cast<void*>(offsetof(SpineRenderVertex, uv)));
+ auto vertexBytes = static_cast<GLsizeiptr>(vertices_.size() * sizeof(SpineRenderVertex));
+ glBindBuffer(GL_ARRAY_BUFFER, vbo_);
+ if (static_cast<u32>(vertexBytes) > vbo_capacity_) {
+ vbo_capacity_ = static_cast<u32>(vertexBytes) * 2;
+ glBufferData(GL_ARRAY_BUFFER, static_cast<GLsizeiptr>(vbo_capacity_), nullptr, GL_STREAM_DRAW);
+ }
+ glBufferSubData(GL_ARRAY_BUFFER, 0, vertexBytes, vertices_.data());
- glEnableVertexAttribArray(2);
- glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, sizeof(SpineRenderVertex),
- reinterpret_cast<void*>(offsetof(SpineRenderVertex, color)));
+ auto indexBytes = static_cast<GLsizeiptr>(indices_.size() * sizeof(u32));
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo_);
+ if (static_cast<u32>(indexBytes) > ebo_capacity_) {
+ ebo_capacity_ = static_cast<u32>(indexBytes) * 2;
+ glBufferData(GL_ELEMENT_ARRAY_BUFFER, static_cast<GLsizeiptr>(ebo_capacity_), nullptr, GL_STREAM_DRAW);
+ }
+ glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, indexBytes, indices_.data());
glDrawElements(GL_TRIANGLES, static_cast<GLsizei>(indices_.size()),
GL_UNSIGNED_INT, nullptr);
@@ -371,9 +386,7 @@ void SpineRenderer::flushBatch() {
triangle_count_ += static_cast<u32>(indices_.size() / 3);
draw_call_count_++;
- glDeleteBuffers(1, &ebo);
- glDeleteBuffers(1, &vbo);
- glDeleteVertexArrays(1, &vao);
+ glBindVertexArray(0);
vertices_.clear();
indices_.clear();
diff --git a/src/esengine/spine/SpineRenderer.hpp b/src/esengine/spine/SpineRenderer.hpp
index b8823341a..fd6d56d29 100644
--- a/src/esengine/spine/SpineRenderer.hpp
+++ b/src/esengine/spine/SpineRenderer.hpp
@@ -154,6 +154,12 @@ private:
u32 triangle_count_ = 0;
u32 draw_call_count_ = 0;
bool initialized_ = false;
+
+ u32 vao_ = 0;
+ u32 vbo_ = 0;
+ u32 ebo_ = 0;
+ u32 vbo_capacity_ = 0;
+ u32 ebo_capacity_ = 0;
};
} // namespace esengine::spine