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 rendering pipeline, build size and texture loading

esenginee00969b5c714
diff --git a/cmake/Emscripten.cmake b/cmake/Emscripten.cmake
index d62a8394c..159bb4bc4 100644
--- a/cmake/Emscripten.cmake
+++ b/cmake/Emscripten.cmake
@@ -168,6 +168,7 @@ set(ES_EMSCRIPTEN_SDK_LINK_FLAGS
-sALLOW_MEMORY_GROWTH=1
-sALLOW_TABLE_GROWTH=1
-sNO_EXIT_RUNTIME=1
+ -sASSERTIONS=0
-sEXPORT_ES6=1
-sMODULARIZE=1
-sFORCE_FILESYSTEM=1
@@ -175,6 +176,7 @@ set(ES_EMSCRIPTEN_SDK_LINK_FLAGS
"-sEXPORTED_FUNCTIONS=['_malloc','_free']"
"-sEXPORTED_RUNTIME_METHODS=['ccall','cwrap','HEAPF32','HEAPU8','HEAPU32','FS','addFunction']"
-O3
+ -flto
--closure=1
)
@@ -200,7 +202,7 @@ set(ES_EMSCRIPTEN_SINGLE_FILE_FLAGS
function(es_apply_sdk_settings TARGET_NAME)
if(ES_BUILD_WEB OR ES_BUILD_WXGAME)
- target_compile_options(${TARGET_NAME} PRIVATE ${ES_EMSCRIPTEN_COMPILE_FLAGS})
+ target_compile_options(${TARGET_NAME} PRIVATE ${ES_EMSCRIPTEN_COMPILE_FLAGS} -flto)
string(REPLACE ";" " " LINK_FLAGS_STR "${ES_EMSCRIPTEN_SDK_LINK_FLAGS}")
set_target_properties(${TARGET_NAME} PROPERTIES
diff --git a/sdk/package-lock.json b/sdk/package-lock.json
index df5645c0d..eebb5a9b4 100644
--- a/sdk/package-lock.json
+++ b/sdk/package-lock.json
@@ -1,14 +1,15 @@
{
"name": "esengine",
- "version": "0.3.0",
+ "version": "0.4.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "esengine",
- "version": "0.3.0",
+ "version": "0.4.0",
"license": "MIT",
"devDependencies": {
+ "@rollup/plugin-terser": "^0.4.4",
"@rollup/plugin-typescript": "^11.1.5",
"minigame-api-typings": "^3.8.16",
"rimraf": "^5.0.5",
@@ -63,6 +64,38 @@
"node": ">=12"
}
},
+ "node_modules/@jridgewell/gen-mapping": {
+ "version": "0.3.13",
+ "resolved": "https://registry.npmjs.org/@jridgewell/gen-mapping/-/gen-mapping-0.3.13.tgz",
+ "integrity": "sha512-2kkt/7niJ6MgEPxF0bYdQ6etZaA+fQvDcLKckhy1yIQOzaoKjBBjSj63/aLVjYE3qhRt5dvM+uUyfCg6UKCBbA==",
+ "dev": true,
+ "license": "MIT",
+ "dependencies": {
+ "@jridgewell/sourcemap-codec": "^1.5.0",
+ "@jridgewell/trace-mapping": "^0.3.24"
+ }
+ },
+ "node_modules/@jridgewell/resolve-uri": {
+ "version": "3.1.2",
+ "resolved": "https://registry.npmjs.org/@jridgewell/resolve-uri/-/resolve-uri-3.1.2.tgz",
+ "integrity": "sha512-bRISgCIjP20/tbWSPWMEi54QVPRZExkuD9lJL+UIxUKtwVJA8wW1Trb1jMs1RFXo1CBTNZ/5hpC9QvmKWdopKw==",
+ "dev": true,
+ "license": "MIT",
+ "engines": {
+ "node": ">=6.0.0"
+ }
+ },
+ "node_modules/@jridgewell/source-map": {
+ "version": "0.3.11",
+ "resolved": "https://registry.npmjs.org/@jridgewell/source-map/-/source-map-0.3.11.tgz",
+ "integrity": "sha512-ZMp1V8ZFcPG5dIWnQLr3NSI1MiCU7UETdS/A0G8V/XWHvJv3ZsFqutJn1Y5RPmAPX6F3BiE397OqveU/9NCuIA==",
+ "dev": true,
+ "license": "MIT",
+ "dependencies": {
+ "@jridgewell/gen-mapping": "^0.3.5",
+ "@jridgewell/trace-mapping": "^0.3.25"
+ }
+ },
"node_modules/@jridgewell/sourcemap-codec": {
"version": "1.5.5",
"resolved": "https://registry.npmjs.org/@jridgewell/sourcemap-codec/-/sourcemap-codec-1.5.5.tgz",
@@ -70,6 +103,17 @@
"dev": true,
"license": "MIT"
},
+ "node_modules/@jridgewell/trace-mapping": {
+ "version": "0.3.31",
+ "resolved": "https://registry.npmjs.org/@jridgewell/trace-mapping/-/trace-mapping-0.3.31.tgz",
+ "integrity": "sha512-zzNR+SdQSDJzc8joaeP8QQoCQr8NuYx2dIIytl1QeBEZHJ9uW6hebsrYgbz8hJwUQao3TWCMtmfV8Nu1twOLAw==",
+ "dev": true,
+ "license": "MIT",
+ "dependencies": {
+ "@jridgewell/resolve-uri": "^3.1.0",
+ "@jridgewell/sourcemap-codec": "^1.4.14"
+ }
+ },
"node_modules/@pkgjs/parseargs": {
"version": "0.11.0",
"resolved": "https://registry.npmjs.org/@pkgjs/parseargs/-/parseargs-0.11.0.tgz",
@@ -81,6 +125,29 @@
"node": ">=14"
}
},
+ "node_modules/@rollup/plugin-terser": {
+ "version": "0.4.4",
+ "resolved": "https://registry.npmjs.org/@rollup/plugin-terser/-/plugin-terser-0.4.4.tgz",
+ "integrity": "sha512-XHeJC5Bgvs8LfukDwWZp7yeqin6ns8RTl2B9avbejt6tZqsqvVoWI7ZTQrcNsfKEDWBTnTxM8nMDkO2IFFbd0A==",
+ "dev": true,
+ "license": "MIT",
+ "dependencies": {
+ "serialize-javascript": "^6.0.1",
+ "smob": "^1.0.0",
+ "terser": "^5.17.4"
+ },
+ "engines": {
+ "node": ">=14.0.0"
+ },
+ "peerDependencies": {
+ "rollup": "^2.0.0||^3.0.0||^4.0.0"
+ },
+ "peerDependenciesMeta": {
+ "rollup": {
+ "optional": true
+ }
+ }
+ },
"node_modules/@rollup/plugin-typescript": {
"version": "11.1.6",
"resolved": "https://registry.npmjs.org/@rollup/plugin-typescript/-/plugin-typescript-11.1.6.tgz",
@@ -488,6 +555,19 @@
"dev": true,
"license": "MIT"
},
+ "node_modules/acorn": {
+ "version": "8.15.0",
+ "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.15.0.tgz",
+ "integrity": "sha512-NZyJarBfL7nWwIq+FDL6Zp/yHEhePMNnnJ0y3qfieCrmNvYct8uvtiV41UvlSe6apAfk0fY1FbWx+NwfmpvtTg==",
+ "dev": true,
+ "license": "MIT",
+ "bin": {
+ "acorn": "bin/acorn"
+ },
+ "engines": {
+ "node": ">=0.4.0"
+ }
+ },
"node_modules/ansi-regex": {
"version": "6.2.2",
"resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-6.2.2.tgz",
@@ -531,6 +611,13 @@
"balanced-match": "^1.0.0"
}
},
+ "node_modules/buffer-from": {
+ "version": "1.1.2",
+ "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.2.tgz",
+ "integrity": "sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ==",
+ "dev": true,
+ "license": "MIT"
+ },
"node_modules/color-convert": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz",
@@ -551,6 +638,13 @@
"dev": true,
"license": "MIT"
},
+ "node_modules/commander": {
+ "version": "2.20.3",
+ "resolved": "https://registry.npmjs.org/commander/-/commander-2.20.3.tgz",
+ "integrity": "sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==",
+ "dev": true,
+ "license": "MIT"
+ },
"node_modules/cross-spawn": {
"version": "7.0.6",
"resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.6.tgz",
@@ -832,6 +926,16 @@
"url": "https://github.com/sponsors/jonschlinkert"
}
},
+ "node_modules/randombytes": {
+ "version": "2.1.0",
+ "resolved": "https://registry.npmjs.org/randombytes/-/randombytes-2.1.0.tgz",
+ "integrity": "sha512-vYl3iOX+4CKUWuxGi9Ukhie6fsqXqS9FE2Zaic4tNFD2N2QQaXOMFbuKK4QmDHC0JO6B1Zp41J0LpT0oR68amQ==",
+ "dev": true,
+ "license": "MIT",
+ "dependencies": {
+ "safe-buffer": "^5.1.0"
+ }
+ },
"node_modules/resolve": {
"version": "1.22.11",
"resolved": "https://registry.npmjs.org/resolve/-/resolve-1.22.11.tgz",
@@ -875,6 +979,7 @@
"integrity": "sha512-9FwVqlgUHzbXtDg9RCMgodF3Ua4Na6Gau+Sdt9vyCN4RhHfVKX2DCHy3BjMLTDd47ITDhYAnTwGulWTblJSDLg==",
"dev": true,
"license": "MIT",
+ "peer": true,
"dependencies": {
"@types/estree": "1.0.8"
},
@@ -937,6 +1042,37 @@
"typescript": "^4.5 || ^5.0"
}
},
+ "node_modules/safe-buffer": {
+ "version": "5.2.1",
+ "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz",
+ "integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==",
+ "dev": true,
+ "funding": [
+ {
+ "type": "github",
+ "url": "https://github.com/sponsors/feross"
+ },
+ {
+ "type": "patreon",
+ "url": "https://www.patreon.com/feross"
+ },
+ {
+ "type": "consulting",
+ "url": "https://feross.org/support"
+ }
+ ],
+ "license": "MIT"
+ },
+ "node_modules/serialize-javascript": {
+ "version": "6.0.2",
+ "resolved": "https://registry.npmjs.org/serialize-javascript/-/serialize-javascript-6.0.2.tgz",
+ "integrity": "sha512-Saa1xPByTTq2gdeFZYLLo+RFE35NHZkAbqZeWNd3BpzppeVisAqpDjcp8dyf6uIvEqJRd46jemmyA4iFIeVk8g==",
+ "dev": true,
+ "license": "BSD-3-Clause",
+ "dependencies": {
+ "randombytes": "^2.1.0"
+ }
+ },
"node_modules/shebang-command": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/shebang-command/-/shebang-command-2.0.0.tgz",
@@ -973,6 +1109,34 @@
"url": "https://github.com/sponsors/isaacs"
}
},
+ "node_modules/smob": {
+ "version": "1.5.0",
+ "resolved": "https://registry.npmjs.org/smob/-/smob-1.5.0.tgz",
+ "integrity": "sha512-g6T+p7QO8npa+/hNx9ohv1E5pVCmWrVCUzUXJyLdMmftX6ER0oiWY/w9knEonLpnOp6b6FenKnMfR8gqwWdwig==",
+ "dev": true,
+ "license": "MIT"
+ },
+ "node_modules/source-map": {
+ "version": "0.6.1",
+ "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz",
+ "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==",
+ "dev": true,
+ "license": "BSD-3-Clause",
+ "engines": {
+ "node": ">=0.10.0"
+ }
+ },
+ "node_modules/source-map-support": {
+ "version": "0.5.21",
+ "resolved": "https://registry.npmjs.org/source-map-support/-/source-map-support-0.5.21.tgz",
+ "integrity": "sha512-uBHU3L3czsIyYXKX88fdrGovxdSCoTGDRZ6SYXtSRxLZUzHg5P/66Ht6uoUlHu9EZod+inXhKo3qQgwXUT/y1w==",
+ "dev": true,
+ "license": "MIT",
+ "dependencies": {
+ "buffer-from": "^1.0.0",
+ "source-map": "^0.6.0"
+ }
+ },
"node_modules/string-width": {
"version": "5.1.2",
"resolved": "https://registry.npmjs.org/string-width/-/string-width-5.1.2.tgz",
@@ -1090,12 +1254,32 @@
"url": "https://github.com/sponsors/ljharb"
}
},
+ "node_modules/terser": {
+ "version": "5.46.0",
+ "resolved": "https://registry.npmjs.org/terser/-/terser-5.46.0.tgz",
+ "integrity": "sha512-jTwoImyr/QbOWFFso3YoU3ik0jBBDJ6JTOQiy/J2YxVJdZCc+5u7skhNwiOR3FQIygFqVUPHl7qbbxtjW2K3Qg==",
+ "dev": true,
+ "license": "BSD-2-Clause",
+ "dependencies": {
+ "@jridgewell/source-map": "^0.3.3",
+ "acorn": "^8.15.0",
+ "commander": "^2.20.0",
+ "source-map-support": "~0.5.20"
+ },
+ "bin": {
+ "terser": "bin/terser"
+ },
+ "engines": {
+ "node": ">=10"
+ }
+ },
"node_modules/tslib": {
"version": "2.8.1",
"resolved": "https://registry.npmjs.org/tslib/-/tslib-2.8.1.tgz",
"integrity": "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w==",
"dev": true,
- "license": "0BSD"
+ "license": "0BSD",
+ "peer": true
},
"node_modules/typescript": {
"version": "5.9.3",
@@ -1103,6 +1287,7 @@
"integrity": "sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw==",
"dev": true,
"license": "Apache-2.0",
+ "peer": true,
"bin": {
"tsc": "bin/tsc",
"tsserver": "bin/tsserver"
diff --git a/sdk/package.json b/sdk/package.json
index 9cd43008a..4cfa99ae2 100644
--- a/sdk/package.json
+++ b/sdk/package.json
@@ -44,6 +44,7 @@
"author": "ESEngine Team",
"license": "MIT",
"devDependencies": {
+ "@rollup/plugin-terser": "^0.4.4",
"@rollup/plugin-typescript": "^11.1.5",
"minigame-api-typings": "^3.8.16",
"rimraf": "^5.0.5",
diff --git a/sdk/rollup.config.js b/sdk/rollup.config.js
index 4798c8c89..bbb11d086 100644
--- a/sdk/rollup.config.js
+++ b/sdk/rollup.config.js
@@ -1,4 +1,5 @@
import typescript from '@rollup/plugin-typescript';
+import terser from '@rollup/plugin-terser';
import dts from 'rollup-plugin-dts';
const tsPlugin = typescript({
@@ -14,25 +15,25 @@ const esmBuilds = [
{
input: 'src/index.ts',
output: { file: 'dist/index.js', format: 'esm', sourcemap: true },
- plugins: [tsPlugin],
+ plugins: [tsPlugin, terser()],
treeshake,
},
{
input: 'src/index.wechat.ts',
output: { file: 'dist/index.wechat.js', format: 'esm', sourcemap: true },
- plugins: [typescript({ tsconfig: './tsconfig.json', declaration: false })],
+ plugins: [typescript({ tsconfig: './tsconfig.json', declaration: false }), terser()],
treeshake,
},
{
input: 'src/index.wechat.ts',
output: { file: 'dist/index.wechat.cjs.js', format: 'cjs', sourcemap: true },
- plugins: [typescript({ tsconfig: './tsconfig.json', declaration: false })],
+ plugins: [typescript({ tsconfig: './tsconfig.json', declaration: false }), terser()],
treeshake,
},
{
input: 'src/wasm.ts',
output: { file: 'dist/wasm.js', format: 'esm', sourcemap: true },
- plugins: [typescript({ tsconfig: './tsconfig.json', declaration: false })],
+ plugins: [typescript({ tsconfig: './tsconfig.json', declaration: false }), terser()],
treeshake,
},
];
diff --git a/sdk/src/asset/AssetServer.ts b/sdk/src/asset/AssetServer.ts
index 8702be14e..c2dc04467 100644
--- a/sdk/src/asset/AssetServer.ts
+++ b/sdk/src/asset/AssetServer.ts
@@ -395,7 +395,69 @@ export class AssetServer {
private createTextureFromImage(img: HTMLImageElement | ImageBitmap, flip: boolean): TextureInfo {
const { width, height } = img;
+ const gl = this.getWebGL2Context();
+ if (gl) {
+ return this.createTextureWebGL2(gl, img, width, height, flip);
+ }
+
+ return this.createTextureFallback(img, width, height, flip);
+ }
+
+ private getWebGL2Context(): WebGL2RenderingContext | null {
+ try {
+ const glObj = (this.module_ as any).GL;
+ if (glObj && glObj.currentContext && glObj.currentContext.GLctx) {
+ const ctx = glObj.currentContext.GLctx;
+ if (ctx instanceof WebGL2RenderingContext) {
+ return ctx;
+ }
+ }
+ } catch {
+ // Fall through to fallback
+ }
+ return null;
+ }
+
+ private createTextureWebGL2(
+ gl: WebGL2RenderingContext,
+ img: HTMLImageElement | ImageBitmap,
+ width: number,
+ height: number,
+ flip: boolean,
+ ): TextureInfo {
+ const texture = gl.createTexture()!;
+ gl.bindTexture(gl.TEXTURE_2D, texture);
+
+ gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, flip ? 1 : 0);
+ gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, 0);
+
+ gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, img as any);
+
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT);
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT);
+ gl.generateMipmap(gl.TEXTURE_2D);
+
+ gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 0);
+
+ const glObj = (this.module_ as any).GL;
+ const glTextureId = glObj.getNewId(glObj.textures);
+ glObj.textures[glTextureId] = texture;
+
+ const rm = this.module_.getResourceManager();
+ const handle = rm.registerExternalTexture(glTextureId, width, height);
+
+ return { handle, width, height };
+ }
+
+ private createTextureFallback(
+ img: HTMLImageElement | ImageBitmap,
+ width: number,
+ height: number,
+ flip: boolean,
+ ): TextureInfo {
if (this.canvas_.width < width || this.canvas_.height < height) {
this.canvas_.width = Math.max(this.canvas_.width, this.nextPowerOf2(width));
this.canvas_.height = Math.max(this.canvas_.height, this.nextPowerOf2(height));
diff --git a/sdk/src/wasm.ts b/sdk/src/wasm.ts
index 4401097a6..02c60fdb8 100644
--- a/sdk/src/wasm.ts
+++ b/sdk/src/wasm.ts
@@ -72,6 +72,7 @@ export interface CppRegistry {
export interface CppResourceManager {
createTexture(width: number, height: number, pixels: number, pixelsLen: number, format: number): number;
createShader(vertSrc: string, fragSrc: string): number;
+ registerExternalTexture(glTextureId: number, width: number, height: number): number;
releaseTexture(handle: number): void;
releaseShader(handle: number): void;
setTextureMetadata(handle: number, left: number, right: number, top: number, bottom: number): void;
diff --git a/src/esengine/bindings/WebSDKEntry.cpp b/src/esengine/bindings/WebSDKEntry.cpp
index da47f6aea..80439a114 100644
--- a/src/esengine/bindings/WebSDKEntry.cpp
+++ b/src/esengine/bindings/WebSDKEntry.cpp
@@ -297,6 +297,12 @@ u32 rm_createShader(resource::ResourceManager& rm,
return handle.id();
}
+u32 rm_registerExternalTexture(resource::ResourceManager& rm, u32 glTextureId,
+ u32 width, u32 height) {
+ auto handle = rm.registerExternalTexture(glTextureId, width, height);
+ return handle.id();
+}
+
void rm_releaseTexture(resource::ResourceManager& rm, u32 handleId) {
rm.releaseTexture(resource::TextureHandle(handleId));
}
@@ -982,6 +988,7 @@ EMSCRIPTEN_BINDINGS(esengine_renderer) {
emscripten::class_<esengine::resource::ResourceManager>("ResourceManager")
.function("createTexture", &esengine::rm_createTexture)
.function("createShader", &esengine::rm_createShader)
+ .function("registerExternalTexture", &esengine::rm_registerExternalTexture)
.function("releaseTexture", &esengine::rm_releaseTexture)
.function("releaseShader", &esengine::rm_releaseShader)
.function("setTextureMetadata", &esengine::rm_setTextureMetadata)
diff --git a/src/esengine/ecs/Component.hpp b/src/esengine/ecs/Component.hpp
index 281aa7b9d..5bf801e40 100644
--- a/src/esengine/ecs/Component.hpp
+++ b/src/esengine/ecs/Component.hpp
@@ -6,7 +6,7 @@
* reduced compile times in larger projects.
*
* Component files:
- * - components/Transform.hpp - LocalTransform, WorldTransform, TransformDirty
+ * - components/Transform.hpp - LocalTransform, WorldTransform, TransformDirty, TransformStatic
* - components/Hierarchy.hpp - Parent, Children, HierarchyDepth
* - components/Sprite.hpp - Sprite
* - components/Camera.hpp - Camera, ProjectionType
diff --git a/src/esengine/ecs/TransformSystem.hpp b/src/esengine/ecs/TransformSystem.hpp
index 93fb28ae8..73a09651e 100644
--- a/src/esengine/ecs/TransformSystem.hpp
+++ b/src/esengine/ecs/TransformSystem.hpp
@@ -65,70 +65,69 @@ public:
void update(Registry& registry, f32 deltaTime) override {
(void)deltaTime;
-
- // Collect all entities that need updating
updateDirtyTransforms(registry);
}
private:
- /**
- * @brief Updates all transforms respecting hierarchy
- */
void updateDirtyTransforms(Registry& registry) {
- // Strategy: Process all root entities (no Parent), then recurse to children
- // This ensures parents are always processed before children
-
- // First, ensure all entities with LocalTransform have WorldTransform
registry.each<LocalTransform>([&registry](Entity entity, LocalTransform&) {
if (!registry.has<WorldTransform>(entity)) {
registry.emplace<WorldTransform>(entity);
}
});
- // Process root entities (those without Parent component)
registry.each<LocalTransform>([&registry, this](Entity entity, LocalTransform& local) {
if (!registry.has<Parent>(entity)) {
- // Root entity - world transform equals local transform
- updateEntityTransform(registry, entity, local, glm::mat4(1.0f));
+ bool isStatic = registry.has<TransformStatic>(entity);
+ bool isDirty = registry.has<TransformDirty>(entity);
+
+ if (isStatic && !isDirty) {
+ return;
+ }
+
+ updateEntityTransform(registry, entity, local, glm::mat4(1.0f), true);
}
});
}
- /**
- * @brief Recursively updates an entity and its children
- * @param registry The ECS registry
- * @param entity The entity to update
- * @param local The entity's local transform
- * @param parentWorldMatrix The parent's world matrix (identity for roots)
- */
void updateEntityTransform(Registry& registry, Entity entity,
const LocalTransform& local,
- const glm::mat4& parentWorldMatrix) {
- // Compute local matrix from LocalTransform
- glm::mat4 localMatrix = math::compose(local.position, local.rotation, local.scale);
+ const glm::mat4& parentWorldMatrix,
+ bool parentDirty) {
+ bool isDirty = parentDirty || registry.has<TransformDirty>(entity);
+ bool isStatic = registry.has<TransformStatic>(entity);
- // Compute world matrix
+ if (isStatic && !isDirty) {
+ if (registry.has<Children>(entity)) {
+ 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);
+ }
+ }
+ }
+ return;
+ }
+
+ glm::mat4 localMatrix = math::compose(local.position, local.rotation, local.scale);
glm::mat4 worldMatrix = parentWorldMatrix * localMatrix;
- // Update WorldTransform component
auto& world = registry.get<WorldTransform>(entity);
world.matrix = worldMatrix;
-
- // Extract world-space components for convenience
math::decompose(worldMatrix, world.position, world.rotation, world.scale);
- // Remove dirty flag if present
if (registry.has<TransformDirty>(entity)) {
registry.remove<TransformDirty>(entity);
}
- // Recursively update children
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);
+ updateEntityTransform(registry, child, childLocal, worldMatrix, isDirty);
}
}
}
diff --git a/src/esengine/ecs/components/Transform.hpp b/src/esengine/ecs/components/Transform.hpp
index e6e8f56b5..46027f5fd 100644
--- a/src/esengine/ecs/components/Transform.hpp
+++ b/src/esengine/ecs/components/Transform.hpp
@@ -134,4 +134,13 @@ struct WorldTransform {
*/
struct TransformDirty {};
+/**
+ * @brief Tag component for static transforms that rarely change
+ *
+ * @details Entities with this tag are skipped by TransformSystem unless
+ * they also have TransformDirty. Use for background scenery,
+ * static UI elements, etc. that don't move every frame.
+ */
+struct TransformStatic {};
+
} // namespace esengine::ecs
diff --git a/src/esengine/renderer/RenderFrame.cpp b/src/esengine/renderer/RenderFrame.cpp
index 9db2aefc4..9b273e81e 100644
--- a/src/esengine/renderer/RenderFrame.cpp
+++ b/src/esengine/renderer/RenderFrame.cpp
@@ -72,6 +72,33 @@ void RenderFrame::init(u32 width, u32 height) {
spine_indices_.reserve(2048);
spine_world_vertices_.reserve(1024);
+ glGenVertexArrays(1, &spine_vao_);
+ glGenBuffers(1, &spine_vbo_);
+ glGenBuffers(1, &spine_ebo_);
+
+ glBindVertexArray(spine_vao_);
+ glBindBuffer(GL_ARRAY_BUFFER, spine_vbo_);
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, spine_ebo_);
+
+ glEnableVertexAttribArray(0);
+ glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(SpineVertex),
+ reinterpret_cast<void*>(offsetof(SpineVertex, position)));
+ glEnableVertexAttribArray(1);
+ glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(SpineVertex),
+ reinterpret_cast<void*>(offsetof(SpineVertex, uv)));
+ glEnableVertexAttribArray(2);
+ glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, sizeof(SpineVertex),
+ reinterpret_cast<void*>(offsetof(SpineVertex, color)));
+
+ glBindVertexArray(0);
+ spine_vbo_capacity_ = 0;
+ spine_ebo_capacity_ = 0;
+
+ glGenVertexArrays(1, &mat_sprite_vao_);
+ glGenBuffers(1, &mat_sprite_vbo_);
+ glGenBuffers(1, &mat_sprite_ebo_);
+ mat_sprite_ebo_initialized_ = false;
+
ES_LOG_INFO("RenderFrame initialized ({}x{})", width, height);
}
@@ -86,6 +113,17 @@ void RenderFrame::shutdown() {
post_process_.reset();
}
+ if (spine_ebo_) { glDeleteBuffers(1, &spine_ebo_); spine_ebo_ = 0; }
+ if (spine_vbo_) { glDeleteBuffers(1, &spine_vbo_); spine_vbo_ = 0; }
+ if (spine_vao_) { glDeleteVertexArrays(1, &spine_vao_); spine_vao_ = 0; }
+ spine_vbo_capacity_ = 0;
+ spine_ebo_capacity_ = 0;
+
+ if (mat_sprite_ebo_) { glDeleteBuffers(1, &mat_sprite_ebo_); mat_sprite_ebo_ = 0; }
+ if (mat_sprite_vbo_) { glDeleteBuffers(1, &mat_sprite_vbo_); mat_sprite_vbo_ = 0; }
+ if (mat_sprite_vao_) { glDeleteVertexArrays(1, &mat_sprite_vao_); mat_sprite_vao_ = 0; }
+ mat_sprite_ebo_initialized_ = false;
+
items_.clear();
ES_LOG_INFO("RenderFrame shutdown");
}
@@ -124,7 +162,7 @@ void RenderFrame::begin(const glm::mat4& view_projection, RenderTargetManager::H
void RenderFrame::end() {
if (!in_frame_) return;
- sortItems();
+ sortAndBucket();
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
@@ -177,10 +215,18 @@ void RenderFrame::submitSprites(ecs::Registry& registry) {
item.type = RenderType::Sprite;
item.stage = current_stage_;
- item.transform = glm::mat4(1.0f);
- item.transform = glm::translate(item.transform, position);
- item.transform *= glm::mat4_cast(rotation);
- item.transform = glm::scale(item.transform, scale);
+ item.world_position = position;
+ item.world_scale = glm::vec2(scale);
+ f32 sinHalfAngle = rotation.z;
+ f32 cosHalfAngle = rotation.w;
+ item.world_angle = 2.0f * std::atan2(sinHalfAngle, cosHalfAngle);
+
+ if (sprite.material != 0) {
+ item.transform = glm::mat4(1.0f);
+ item.transform = glm::translate(item.transform, position);
+ item.transform *= glm::mat4_cast(rotation);
+ item.transform = glm::scale(item.transform, scale);
+ }
item.layer = sprite.layer;
item.depth = position.z;
@@ -256,6 +302,9 @@ void RenderFrame::submitSpine(ecs::Registry& registry, spine::SpineSystem& spine
item.transform *= glm::mat4_cast(rotation);
item.transform = glm::scale(item.transform, scale);
+ item.world_position = position;
+ item.world_scale = glm::vec2(scale);
+
item.layer = comp.layer;
item.depth = position.z;
item.skeleton = instance->skeleton.get();
@@ -281,84 +330,87 @@ void RenderFrame::submit(const RenderItem& item) {
}
}
-void RenderFrame::sortItems() {
+void RenderFrame::sortAndBucket() {
std::sort(items_.begin(), items_.end(),
[](const RenderItem& a, const RenderItem& b) {
return a.sortKey() < b.sortKey();
});
-}
-void RenderFrame::executeStage(RenderStage stage) {
- std::vector<RenderItem*> stageItems;
+ for (auto& sb : stage_boundaries_) {
+ sb.begin = 0;
+ sb.end = 0;
+ }
- for (auto& item : items_) {
- if (item.stage == stage) {
- stageItems.push_back(&item);
+ if (items_.empty()) return;
+
+ u32 n = static_cast<u32>(items_.size());
+ u32 i = 0;
+ while (i < n) {
+ auto stage = items_[i].stage;
+ u32 stageIdx = static_cast<u32>(stage);
+ if (stageIdx < STAGE_COUNT) {
+ stage_boundaries_[stageIdx].begin = i;
+ while (i < n && items_[i].stage == stage) {
+ ++i;
+ }
+ stage_boundaries_[stageIdx].end = i;
+ } else {
+ ++i;
}
}
+}
- if (stageItems.empty()) return;
+void RenderFrame::executeStage(RenderStage stage) {
+ u32 stageIdx = static_cast<u32>(stage);
+ if (stageIdx >= STAGE_COUNT) return;
- std::vector<RenderItem*> currentBatch;
- RenderType currentType = stageItems[0]->type;
+ auto& sb = stage_boundaries_[stageIdx];
+ if (sb.begin >= sb.end) return;
- auto flushCurrentBatch = [&]() {
- if (currentBatch.empty()) return;
+ u32 batchStart = sb.begin;
+ RenderType currentType = items_[batchStart].type;
+ auto flushBatch = [&](u32 begin, u32 end) {
switch (currentType) {
case RenderType::Sprite:
- renderSprites(currentBatch);
+ renderSprites(begin, end);
break;
case RenderType::Spine:
- renderSpine(currentBatch);
+ renderSpine(begin, end);
break;
case RenderType::Mesh:
- renderMeshes(currentBatch);
+ renderMeshes(begin, end);
break;
}
- currentBatch.clear();
};
- for (auto* item : stageItems) {
- if (item->type != currentType) {
- flushCurrentBatch();
- currentType = item->type;
+ for (u32 i = sb.begin; i < sb.end; ++i) {
+ if (items_[i].type != currentType) {
+ flushBatch(batchStart, i);
+ batchStart = i;
+ currentType = items_[i].type;
}
- currentBatch.push_back(item);
}
- flushCurrentBatch();
+ flushBatch(batchStart, sb.end);
}
-void RenderFrame::renderSprites(const std::vector<RenderItem*>& items) {
+void RenderFrame::renderSprites(u32 begin, u32 end) {
batcher_->setProjection(view_projection_);
batcher_->beginBatch();
- for (auto* item : items) {
+ for (u32 i = begin; i < end; ++i) {
+ auto* item = &items_[i];
+
if (item->material_id != 0) {
batcher_->flush();
renderSpriteWithMaterial(item);
continue;
}
- glm::vec3 position = glm::vec3(item->transform[3]);
- glm::vec3 scale = glm::vec3(
- glm::length(glm::vec3(item->transform[0])),
- glm::length(glm::vec3(item->transform[1])),
- glm::length(glm::vec3(item->transform[2]))
- );
-
- glm::vec2 finalSize = item->size * glm::vec2(scale);
-
- glm::mat3 rotMat = glm::mat3(item->transform);
- rotMat[0] /= scale.x;
- rotMat[1] /= scale.y;
- rotMat[2] /= scale.z;
- glm::quat rotation = glm::quat_cast(rotMat);
-
- f32 angle = glm::angle(rotation);
- glm::vec3 axis = glm::axis(rotation);
- if (axis.z < 0) angle = -angle;
+ glm::vec2 position(item->world_position);
+ glm::vec2 finalSize = item->size * item->world_scale;
+ f32 angle = item->world_angle;
if (item->use_nine_slice) {
resource::SliceBorder border;
@@ -368,7 +420,7 @@ void RenderFrame::renderSprites(const std::vector<RenderItem*>& items) {
border.bottom = item->slice_border.w;
batcher_->drawNineSlice(
- glm::vec2(position),
+ position,
finalSize,
item->texture_id,
item->texture_size,
@@ -380,7 +432,7 @@ void RenderFrame::renderSprites(const std::vector<RenderItem*>& items) {
);
} else if (std::abs(angle) > 0.001f) {
batcher_->drawRotatedQuad(
- glm::vec2(position),
+ position,
finalSize,
angle,
item->texture_id,
@@ -405,14 +457,15 @@ void RenderFrame::renderSprites(const std::vector<RenderItem*>& items) {
stats_.triangles += batcher_->getQuadCount() * 2;
}
-void RenderFrame::renderSpine(const std::vector<RenderItem*>& items) {
+void RenderFrame::renderSpine(u32 begin, u32 end) {
spine_vertices_.clear();
spine_indices_.clear();
spine_current_texture_ = 0;
static ::spine::SkeletonClipping clipper;
- for (auto* item : items) {
+ for (u32 idx = begin; idx < end; ++idx) {
+ auto* item = &items_[idx];
auto* skeleton = static_cast<::spine::Skeleton*>(item->skeleton);
if (!skeleton) continue;
@@ -574,51 +627,39 @@ void RenderFrame::flushSpineBatch() {
glBindTexture(GL_TEXTURE_2D, spine_current_texture_);
shader->setUniform("u_texture", 0);
- u32 vao, vbo, ebo;
- glGenVertexArrays(1, &vao);
- glGenBuffers(1, &vbo);
- glGenBuffers(1, &ebo);
-
- glBindVertexArray(vao);
-
- glBindBuffer(GL_ARRAY_BUFFER, vbo);
- glBufferData(GL_ARRAY_BUFFER,
- static_cast<GLsizeiptr>(spine_vertices_.size() * sizeof(SpineVertex)),
- spine_vertices_.data(), GL_STREAM_DRAW);
+ glBindVertexArray(spine_vao_);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER,
- static_cast<GLsizeiptr>(spine_indices_.size() * sizeof(u32)),
- spine_indices_.data(), GL_STREAM_DRAW);
-
- glEnableVertexAttribArray(0);
- glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(SpineVertex),
- reinterpret_cast<void*>(offsetof(SpineVertex, position)));
-
- glEnableVertexAttribArray(1);
- glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(SpineVertex),
- reinterpret_cast<void*>(offsetof(SpineVertex, uv)));
+ auto vboBytes = static_cast<GLsizeiptr>(spine_vertices_.size() * sizeof(SpineVertex));
+ glBindBuffer(GL_ARRAY_BUFFER, spine_vbo_);
+ if (static_cast<u32>(vboBytes) > spine_vbo_capacity_) {
+ spine_vbo_capacity_ = static_cast<u32>(vboBytes) * 2;
+ glBufferData(GL_ARRAY_BUFFER, spine_vbo_capacity_, nullptr, GL_STREAM_DRAW);
+ }
+ glBufferSubData(GL_ARRAY_BUFFER, 0, vboBytes, spine_vertices_.data());
- glEnableVertexAttribArray(2);
- glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, sizeof(SpineVertex),
- reinterpret_cast<void*>(offsetof(SpineVertex, color)));
+ auto eboBytes = static_cast<GLsizeiptr>(spine_indices_.size() * sizeof(u32));
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, spine_ebo_);
+ if (static_cast<u32>(eboBytes) > spine_ebo_capacity_) {
+ spine_ebo_capacity_ = static_cast<u32>(eboBytes) * 2;
+ glBufferData(GL_ELEMENT_ARRAY_BUFFER, spine_ebo_capacity_, nullptr, GL_STREAM_DRAW);
+ }
+ glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, eboBytes, spine_indices_.data());
glDrawElements(GL_TRIANGLES, static_cast<GLsizei>(spine_indices_.size()),
GL_UNSIGNED_INT, nullptr);
+ glBindVertexArray(0);
+
stats_.triangles += static_cast<u32>(spine_indices_.size() / 3);
stats_.draw_calls++;
- glDeleteBuffers(1, &ebo);
- glDeleteBuffers(1, &vbo);
- glDeleteVertexArrays(1, &vao);
-
spine_vertices_.clear();
spine_indices_.clear();
}
-void RenderFrame::renderMeshes(const std::vector<RenderItem*>& items) {
- for (auto* item : items) {
+void RenderFrame::renderMeshes(u32 begin, u32 end) {
+ for (u32 i = begin; i < end; ++i) {
+ auto* item = &items_[i];
if (!item->geometry || !item->shader) continue;
stats_.draw_calls++;
}
@@ -666,63 +707,52 @@ void RenderFrame::renderSpriteWithMaterial(RenderItem* item) {
glm::vec2 halfSize = item->size * 0.5f;
- f32 positions[] = {
- -halfSize.x, -halfSize.y,
- halfSize.x, -halfSize.y,
- halfSize.x, halfSize.y,
- -halfSize.x, halfSize.y,
- };
-
- f32 texCoords[] = {
- 0.0f, 1.0f,
- 1.0f, 1.0f,
- 1.0f, 0.0f,
- 0.0f, 0.0f,
+ struct MatSpriteVertex {
+ f32 px, py;
+ f32 tx, ty;
+ f32 cr, cg, cb, ca;
};
- f32 colors[] = {
- item->color.r, item->color.g, item->color.b, item->color.a,
- item->color.r, item->color.g, item->color.b, item->color.a,
- item->color.r, item->color.g, item->color.b, item->color.a,
- item->color.r, item->color.g, item->color.b, item->color.a,
+ MatSpriteVertex vertices[4] = {
+ { -halfSize.x, -halfSize.y, 0.0f, 1.0f, item->color.r, item->color.g, item->color.b, item->color.a },
+ { halfSize.x, -halfSize.y, 1.0f, 1.0f, item->color.r, item->color.g, item->color.b, item->color.a },
+ { halfSize.x, halfSize.y, 1.0f, 0.0f, item->color.r, item->color.g, item->color.b, item->color.a },
+ { -halfSize.x, halfSize.y, 0.0f, 0.0f, item->color.r, item->color.g, item->color.b, item->color.a },
};
- u32 indices[] = { 0, 1, 2, 2, 3, 0 };
-
- u32 vao, vbo_pos, vbo_tex, vbo_color, ebo;
- glGenVertexArrays(1, &vao);
- glGenBuffers(1, &vbo_pos);
- glGenBuffers(1, &vbo_tex);
- glGenBuffers(1, &vbo_color);
- glGenBuffers(1, &ebo);
+ glBindVertexArray(mat_sprite_vao_);
- glBindVertexArray(vao);
-
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STREAM_DRAW);
+ glBindBuffer(GL_ARRAY_BUFFER, mat_sprite_vbo_);
+ glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STREAM_DRAW);
GLint locPos = shader->getAttribLocation("a_position");
if (locPos >= 0) {
- glBindBuffer(GL_ARRAY_BUFFER, vbo_pos);
- glBufferData(GL_ARRAY_BUFFER, sizeof(positions), positions, GL_STREAM_DRAW);
glEnableVertexAttribArray(locPos);
- glVertexAttribPointer(locPos, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
+ glVertexAttribPointer(locPos, 2, GL_FLOAT, GL_FALSE, sizeof(MatSpriteVertex),
+ reinterpret_cast<void*>(0));
}
GLint locTex = shader->getAttribLocation("a_texCoord");
if (locTex >= 0) {
- glBindBuffer(GL_ARRAY_BUFFER, vbo_tex);
- glBufferData(GL_ARRAY_BUFFER, sizeof(texCoords), texCoords, GL_STREAM_DRAW);
glEnableVertexAttribArray(locTex);
- glVertexAttribPointer(locTex, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
+ glVertexAttribPointer(locTex, 2, GL_FLOAT, GL_FALSE, sizeof(MatSpriteVertex),
+ reinterpret_cast<void*>(2 * sizeof(f32)));
}
GLint locColor = shader->getAttribLocation("a_color");
if (locColor >= 0) {
- glBindBuffer(GL_ARRAY_BUFFER, vbo_color);
- glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STREAM_DRAW);
glEnableVertexAttribArray(locColor);
- glVertexAttribPointer(locColor, 4, GL_FLOAT, GL_FALSE, 0, nullptr);
+ glVertexAttribPointer(locColor, 4, GL_FLOAT, GL_FALSE, sizeof(MatSpriteVertex),
+ reinterpret_cast<void*>(4 * sizeof(f32)));
+ }
+
+ if (!mat_sprite_ebo_initialized_) {
+ u32 indices[] = { 0, 1, 2, 2, 3, 0 };
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mat_sprite_ebo_);
+ glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
+ mat_sprite_ebo_initialized_ = true;
+ } else {
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mat_sprite_ebo_);
}
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr);
@@ -731,11 +761,7 @@ void RenderFrame::renderSpriteWithMaterial(RenderItem* item) {
if (locTex >= 0) glDisableVertexAttribArray(locTex);
if (locColor >= 0) glDisableVertexAttribArray(locColor);
- glDeleteBuffers(1, &ebo);
- glDeleteBuffers(1, &vbo_color);
- glDeleteBuffers(1, &vbo_tex);
- glDeleteBuffers(1, &vbo_pos);
- glDeleteVertexArrays(1, &vao);
+ glBindVertexArray(0);
stats_.draw_calls++;
stats_.triangles += 2;
diff --git a/src/esengine/renderer/RenderFrame.hpp b/src/esengine/renderer/RenderFrame.hpp
index 7d7302929..d6024f7d1 100644
--- a/src/esengine/renderer/RenderFrame.hpp
+++ b/src/esengine/renderer/RenderFrame.hpp
@@ -9,6 +9,7 @@
#include "../resource/ResourceManager.hpp"
#include <glm/glm.hpp>
+#include <array>
#include <vector>
namespace esengine {
@@ -55,13 +56,15 @@ public:
const Stats& stats() const { return stats_; }
+ static constexpr u32 STAGE_COUNT = 4;
+
private:
- void sortItems();
+ void sortAndBucket();
void executeStage(RenderStage stage);
- void renderSprites(const std::vector<RenderItem*>& items);
+ void renderSprites(u32 begin, u32 end);
void renderSpriteWithMaterial(RenderItem* item);
- void renderSpine(const std::vector<RenderItem*>& items);
- void renderMeshes(const std::vector<RenderItem*>& items);
+ void renderSpine(u32 begin, u32 end);
+ void renderMeshes(u32 begin, u32 end);
void flushSpineBatch();
RenderContext& context_;
@@ -81,6 +84,12 @@ private:
u32 width_ = 0;
u32 height_ = 0;
+ struct StageBoundary {
+ u32 begin = 0;
+ u32 end = 0;
+ };
+ std::array<StageBoundary, STAGE_COUNT> stage_boundaries_{};
+
std::vector<f32> spine_world_vertices_;
struct SpineVertex {
glm::vec2 position;
@@ -91,6 +100,17 @@ private:
std::vector<u32> spine_indices_;
u32 spine_current_texture_ = 0;
BlendMode spine_current_blend_ = BlendMode::Normal;
+
+ u32 spine_vao_ = 0;
+ u32 spine_vbo_ = 0;
+ u32 spine_ebo_ = 0;
+ u32 spine_vbo_capacity_ = 0;
+ u32 spine_ebo_capacity_ = 0;
+
+ u32 mat_sprite_vao_ = 0;
+ u32 mat_sprite_vbo_ = 0;
+ u32 mat_sprite_ebo_ = 0;
+ bool mat_sprite_ebo_initialized_ = false;
};
} // namespace esengine
diff --git a/src/esengine/renderer/RenderItem.hpp b/src/esengine/renderer/RenderItem.hpp
index 78ab1a846..5c29c35a2 100644
--- a/src/esengine/renderer/RenderItem.hpp
+++ b/src/esengine/renderer/RenderItem.hpp
@@ -20,6 +20,10 @@ struct RenderItem {
RenderStage stage = RenderStage::Transparent;
glm::mat4 transform{1.0f};
+ glm::vec3 world_position{0.0f};
+ f32 world_angle = 0.0f;
+ glm::vec2 world_scale{1.0f};
+
i32 layer = 0;
f32 depth = 0.0f;
u32 texture_id = 0;
diff --git a/src/esengine/renderer/Texture.cpp b/src/esengine/renderer/Texture.cpp
index dbfbe9f80..44ea15e2c 100644
--- a/src/esengine/renderer/Texture.cpp
+++ b/src/esengine/renderer/Texture.cpp
@@ -171,6 +171,15 @@ Unique<Texture> Texture::createFromFile(const std::string& path) {
}
#endif
+Unique<Texture> Texture::createFromExternalId(u32 glTextureId, u32 width, u32 height, TextureFormat format) {
+ auto texture = makeUnique<Texture>();
+ texture->textureId_ = glTextureId;
+ texture->width_ = width;
+ texture->height_ = height;
+ texture->format_ = format;
+ return texture;
+}
+
bool Texture::initialize(const TextureSpecification& spec) {
width_ = spec.width;
height_ = spec.height;
diff --git a/src/esengine/renderer/Texture.hpp b/src/esengine/renderer/Texture.hpp
index 4a5cd2a52..4ba01ce15 100644
--- a/src/esengine/renderer/Texture.hpp
+++ b/src/esengine/renderer/Texture.hpp
@@ -245,6 +245,17 @@ public:
static Unique<Texture> createRaw(u32 width, u32 height, const void* data,
TextureFormat format = TextureFormat::RGBA8);
+ /**
+ * @brief Wraps an existing GL texture ID
+ * @param glTextureId The OpenGL texture ID created externally
+ * @param width Texture width in pixels
+ * @param height Texture height in pixels
+ * @param format Pixel format
+ * @return Unique pointer to the texture wrapper
+ */
+ static Unique<Texture> createFromExternalId(u32 glTextureId, u32 width, u32 height,
+ TextureFormat format = TextureFormat::RGBA8);
+
/**
* @brief Updates texture pixel data from raw pointer (internal use)
* @param data Pointer to pixel data
diff --git a/src/esengine/resource/ResourceManager.cpp b/src/esengine/resource/ResourceManager.cpp
index 28e4e5724..ac3a08835 100644
--- a/src/esengine/resource/ResourceManager.cpp
+++ b/src/esengine/resource/ResourceManager.cpp
@@ -237,6 +237,15 @@ void ResourceManager::releaseTexture(TextureHandle handle) {
}
}
+TextureHandle ResourceManager::registerExternalTexture(u32 glTextureId, u32 width, u32 height) {
+ auto texture = Texture::createFromExternalId(glTextureId, width, height, TextureFormat::RGBA8);
+ if (!texture) {
+ ES_LOG_ERROR("Failed to register external texture (GL ID: {})", glTextureId);
+ return TextureHandle();
+ }
+ return textures_.add(std::move(texture));
+}
+
void ResourceManager::registerTextureWithPath(TextureHandle handle, const std::string& path) {
if (handle.isValid() && !path.empty()) {
textures_.setPath(handle, path);
diff --git a/src/esengine/resource/ResourceManager.hpp b/src/esengine/resource/ResourceManager.hpp
index 1afe9796a..0bb79a00c 100644
--- a/src/esengine/resource/ResourceManager.hpp
+++ b/src/esengine/resource/ResourceManager.hpp
@@ -213,6 +213,15 @@ public:
*/
void releaseTexture(TextureHandle handle);
+ /**
+ * @brief Registers an externally-created GL texture
+ * @param glTextureId The OpenGL texture ID
+ * @param width Texture width in pixels
+ * @param height Texture height in pixels
+ * @return Handle to the registered texture
+ */
+ TextureHandle registerExternalTexture(u32 glTextureId, u32 width, u32 height);
+
/**
* @brief Registers a texture with a path for cache lookup
* @param handle The texture handle