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: extend pointer-based reads to physics, camera, velocity, UIRect
esengine132130b15a9a
diff --git a/sdk/benchmarks/wasm-vs-world.bench.ts b/sdk/benchmarks/wasm-vs-world.bench.tsindex 09db479f3..26f070e06 100644--- a/sdk/benchmarks/wasm-vs-world.bench.ts+++ b/sdk/benchmarks/wasm-vs-world.bench.ts@@ -40,6 +40,134 @@ const Sprite = defineBuiltin('Sprite', {enabled: true,});+const Velocity = defineBuiltin('Velocity', {+ linear: { x: 0, y: 0, z: 0 },+ angular: { x: 0, y: 0, z: 0 },+});++const Camera = defineBuiltin('Camera', {+ projectionType: 0,+ fov: 60,+ orthoSize: 5,+ nearPlane: 0.1,+ farPlane: 1000,+ aspectRatio: 0,+ isActive: false,+ priority: 0,+ viewportX: 0,+ viewportY: 0,+ viewportW: 1,+ viewportH: 1,+ clearFlags: 3,+});++const UIRect = defineBuiltin('UIRect', {+ anchorMin: { x: 0.5, y: 0.5 },+ anchorMax: { x: 0.5, y: 0.5 },+ offsetMin: { x: 0, y: 0 },+ offsetMax: { x: 0, y: 0 },+ size: { x: 100, y: 100 },+ pivot: { x: 0.5, y: 0.5 },+});++const RigidBody = defineBuiltin('RigidBody', {+ bodyType: 2,+ gravityScale: 1,+ linearDamping: 0,+ angularDamping: 0,+ fixedRotation: false,+ bullet: false,+ enabled: true,+});++const BoxCollider = defineBuiltin('BoxCollider', {+ halfExtents: { x: 0.5, y: 0.5 },+ offset: { x: 0, y: 0 },+ density: 1,+ friction: 0.3,+ restitution: 0,+ isSensor: false,+ enabled: true,+ categoryBits: 0x0001,+ maskBits: 0xFFFF,+});++const CircleCollider = defineBuiltin('CircleCollider', {+ radius: 0.5,+ offset: { x: 0, y: 0 },+ density: 1,+ friction: 0.3,+ restitution: 0,+ isSensor: false,+ enabled: true,+ categoryBits: 0x0001,+ maskBits: 0xFFFF,+});++const VELOCITY_DATA = {+ linear: { x: 5, y: 0, z: 0 },+ angular: { x: 0, y: 0, z: 1 },+};++const CAMERA_DATA = {+ projectionType: 1,+ fov: 60,+ orthoSize: 5,+ nearPlane: 0.1,+ farPlane: 1000,+ aspectRatio: 1.777,+ isActive: true,+ priority: 0,+ viewportX: 0,+ viewportY: 0,+ viewportW: 1,+ viewportH: 1,+ clearFlags: 3,+};++const UIRECT_DATA = {+ anchorMin: { x: 0, y: 0 },+ anchorMax: { x: 1, y: 1 },+ offsetMin: { x: 10, y: 10 },+ offsetMax: { x: -10, y: -10 },+ size: { x: 200, y: 100 },+ pivot: { x: 0.5, y: 0.5 },+};++const RIGIDBODY_DATA = {+ bodyType: 2,+ gravityScale: 1,+ linearDamping: 0.1,+ angularDamping: 0.05,+ fixedRotation: false,+ bullet: false,+ enabled: true,+};++const BOXCOLLIDER_DATA = {+ halfExtents: { x: 0.5, y: 0.5 },+ offset: { x: 0, y: 0 },+ density: 1,+ friction: 0.3,+ restitution: 0.2,+ isSensor: false,+ enabled: true,+ categoryBits: 0x0001,+ maskBits: 0xFFFF,+};++const CIRCLECOLLIDER_DATA = {+ radius: 0.5,+ offset: { x: 0, y: 0 },+ density: 1,+ friction: 0.3,+ restitution: 0.2,+ isSensor: false,+ enabled: true,+ categoryBits: 0x0001,+ maskBits: 0xFFFF,+};+const TRANSFORM_WASM = {position: { x: 1, y: 2, z: 0 },rotation: { x: 0, y: 0, z: 0, w: 1 },@@ -292,6 +420,132 @@ describe('Ptr-based multi-entity query - Transform + Sprite', () => {});});+describe('Ptr-based getter vs embind - Velocity', () => {+ bench('embind: getVelocity x100', () => {+ const reg = new wasmModule.Registry();+ const e = reg.create();+ (reg as any).addVelocity(e, VELOCITY_DATA);+ for (let i = 0; i < 100; i++) (reg as any).getVelocity(e);+ reg.delete();+ });++ bench('ptr-based: resolveGetter(Velocity) x100', () => {+ const world = new World();+ const reg = new wasmModule.Registry();+ world.connectCpp(reg as unknown as CppRegistry, wasmModule);+ const e = world.spawn();+ world.insert(e, Velocity, VELOCITY_DATA as any);+ const getter = world.resolveGetter(Velocity)!;+ for (let i = 0; i < 100; i++) getter(e);+ reg.delete();+ });+});++describe('Ptr-based getter vs embind - Camera', () => {+ bench('embind: getCamera x100', () => {+ const reg = new wasmModule.Registry();+ const e = reg.create();+ (reg as any).addCamera(e, CAMERA_DATA);+ for (let i = 0; i < 100; i++) (reg as any).getCamera(e);+ reg.delete();+ });++ bench('ptr-based: resolveGetter(Camera) x100', () => {+ const world = new World();+ const reg = new wasmModule.Registry();+ world.connectCpp(reg as unknown as CppRegistry, wasmModule);+ const e = world.spawn();+ world.insert(e, Camera, CAMERA_DATA as any);+ const getter = world.resolveGetter(Camera)!;+ for (let i = 0; i < 100; i++) getter(e);+ reg.delete();+ });+});++describe('Ptr-based getter vs embind - UIRect', () => {+ bench('embind: getUIRect x100', () => {+ const reg = new wasmModule.Registry();+ const e = reg.create();+ (reg as any).addUIRect(e, UIRECT_DATA);+ for (let i = 0; i < 100; i++) (reg as any).getUIRect(e);+ reg.delete();+ });++ bench('ptr-based: resolveGetter(UIRect) x100', () => {+ const world = new World();+ const reg = new wasmModule.Registry();+ world.connectCpp(reg as unknown as CppRegistry, wasmModule);+ const e = world.spawn();+ world.insert(e, UIRect, UIRECT_DATA as any);+ const getter = world.resolveGetter(UIRect)!;+ for (let i = 0; i < 100; i++) getter(e);+ reg.delete();+ });+});++describe('Ptr-based getter vs embind - RigidBody', () => {+ bench('embind: getRigidBody x100', () => {+ const reg = new wasmModule.Registry();+ const e = reg.create();+ (reg as any).addRigidBody(e, RIGIDBODY_DATA);+ for (let i = 0; i < 100; i++) (reg as any).getRigidBody(e);+ reg.delete();+ });++ bench('ptr-based: resolveGetter(RigidBody) x100', () => {+ const world = new World();+ const reg = new wasmModule.Registry();+ world.connectCpp(reg as unknown as CppRegistry, wasmModule);+ const e = world.spawn();+ world.insert(e, RigidBody, RIGIDBODY_DATA as any);+ const getter = world.resolveGetter(RigidBody)!;+ for (let i = 0; i < 100; i++) getter(e);+ reg.delete();+ });+});++describe('Ptr-based getter vs embind - BoxCollider', () => {+ bench('embind: getBoxCollider x100', () => {+ const reg = new wasmModule.Registry();+ const e = reg.create();+ (reg as any).addBoxCollider(e, BOXCOLLIDER_DATA);+ for (let i = 0; i < 100; i++) (reg as any).getBoxCollider(e);+ reg.delete();+ });++ bench('ptr-based: resolveGetter(BoxCollider) x100', () => {+ const world = new World();+ const reg = new wasmModule.Registry();+ world.connectCpp(reg as unknown as CppRegistry, wasmModule);+ const e = world.spawn();+ world.insert(e, BoxCollider, BOXCOLLIDER_DATA as any);+ const getter = world.resolveGetter(BoxCollider)!;+ for (let i = 0; i < 100; i++) getter(e);+ reg.delete();+ });+});++describe('Ptr-based getter vs embind - CircleCollider', () => {+ bench('embind: getCircleCollider x100', () => {+ const reg = new wasmModule.Registry();+ const e = reg.create();+ (reg as any).addCircleCollider(e, CIRCLECOLLIDER_DATA);+ for (let i = 0; i < 100; i++) (reg as any).getCircleCollider(e);+ reg.delete();+ });++ bench('ptr-based: resolveGetter(CircleCollider) x100', () => {+ const world = new World();+ const reg = new wasmModule.Registry();+ world.connectCpp(reg as unknown as CppRegistry, wasmModule);+ const e = world.spawn();+ world.insert(e, CircleCollider, CIRCLECOLLIDER_DATA as any);+ const getter = world.resolveGetter(CircleCollider)!;+ for (let i = 0; i < 100; i++) getter(e);+ reg.delete();+ });+});+describe('World wrapper overhead - full entity lifecycle', () => {bench('direct WASM: create + addTransform + addSprite + getTransform + destroy', () => {const reg = new wasmModule.Registry();diff --git a/sdk/src/wasm.ts b/sdk/src/wasm.tsindex b321008d2..b4cc4a898 100644--- a/sdk/src/wasm.ts+++ b/sdk/src/wasm.ts@@ -266,6 +266,12 @@ export interface ESEngineModule {// Pointer-based component accessgetTransformPtr(registry: CppRegistry, entity: number): number;getSpritePtr(registry: CppRegistry, entity: number): number;+ getVelocityPtr(registry: CppRegistry, entity: number): number;+ getCameraPtr(registry: CppRegistry, entity: number): number;+ getUIRectPtr(registry: CppRegistry, entity: number): number;+ getRigidBodyPtr(registry: CppRegistry, entity: number): number;+ getBoxColliderPtr(registry: CppRegistry, entity: number): number;+ getCircleColliderPtr(registry: CppRegistry, entity: number): number;_malloc(size: number): number;_free(ptr: number): void;diff --git a/sdk/src/world.ts b/sdk/src/world.tsindex 33b83a902..740eef907 100644--- a/sdk/src/world.ts+++ b/sdk/src/world.ts@@ -641,6 +641,125 @@ export class World {};}+ if (cppName === 'Velocity') {+ return (e: Entity) => {+ const ptr = mod.getVelocityPtr(reg, e);+ if (!ptr) return null;+ const f = mod.HEAPF32;+ const b = ptr >> 2;+ return {+ linear: { x: f[b], y: f[b+1], z: f[b+2] },+ angular: { x: f[b+3], y: f[b+4], z: f[b+5] },+ };+ };+ }++ if (cppName === 'Camera') {+ return (e: Entity) => {+ const ptr = mod.getCameraPtr(reg, e);+ if (!ptr) return null;+ const f = mod.HEAPF32;+ const u8 = mod.HEAPU8;+ const i32 = new Int32Array(f.buffer);+ const b = ptr >> 2;+ return {+ projectionType: u8[ptr],+ fov: f[b+1],+ orthoSize: f[b+2],+ nearPlane: f[b+3],+ farPlane: f[b+4],+ aspectRatio: f[b+5],+ isActive: u8[ptr + 24] !== 0,+ priority: i32[b+7],+ viewportX: f[b+8],+ viewportY: f[b+9],+ viewportW: f[b+10],+ viewportH: f[b+11],+ clearFlags: i32[b+12],+ };+ };+ }++ if (cppName === 'UIRect') {+ return (e: Entity) => {+ const ptr = mod.getUIRectPtr(reg, e);+ if (!ptr) return null;+ const f = mod.HEAPF32;+ const b = ptr >> 2;+ return {+ anchorMin: { x: f[b], y: f[b+1] },+ anchorMax: { x: f[b+2], y: f[b+3] },+ offsetMin: { x: f[b+4], y: f[b+5] },+ offsetMax: { x: f[b+6], y: f[b+7] },+ size: { x: f[b+8], y: f[b+9] },+ pivot: { x: f[b+10], y: f[b+11] },+ };+ };+ }++ if (cppName === 'RigidBody') {+ return (e: Entity) => {+ const ptr = mod.getRigidBodyPtr(reg, e);+ if (!ptr) return null;+ const f = mod.HEAPF32;+ const u8 = mod.HEAPU8;+ const b = ptr >> 2;+ return {+ bodyType: u8[ptr],+ gravityScale: f[b+1],+ linearDamping: f[b+2],+ angularDamping: f[b+3],+ fixedRotation: u8[ptr + 16] !== 0,+ bullet: u8[ptr + 17] !== 0,+ enabled: u8[ptr + 18] !== 0,+ };+ };+ }++ if (cppName === 'BoxCollider') {+ return (e: Entity) => {+ const ptr = mod.getBoxColliderPtr(reg, e);+ if (!ptr) return null;+ const f = mod.HEAPF32;+ const u8 = mod.HEAPU8;+ const u32 = mod.HEAPU32;+ const b = ptr >> 2;+ return {+ halfExtents: { x: f[b], y: f[b+1] },+ offset: { x: f[b+2], y: f[b+3] },+ density: f[b+4],+ friction: f[b+5],+ restitution: f[b+6],+ isSensor: u8[ptr + 28] !== 0,+ enabled: u8[ptr + 29] !== 0,+ categoryBits: u32[b+8],+ maskBits: u32[b+9],+ };+ };+ }++ if (cppName === 'CircleCollider') {+ return (e: Entity) => {+ const ptr = mod.getCircleColliderPtr(reg, e);+ if (!ptr) return null;+ const f = mod.HEAPF32;+ const u8 = mod.HEAPU8;+ const u32 = mod.HEAPU32;+ const b = ptr >> 2;+ return {+ radius: f[b],+ offset: { x: f[b+1], y: f[b+2] },+ density: f[b+3],+ friction: f[b+4],+ restitution: f[b+5],+ isSensor: u8[ptr + 24] !== 0,+ enabled: u8[ptr + 25] !== 0,+ categoryBits: u32[b+7],+ maskBits: u32[b+8],+ };+ };+ }+return null;}diff --git a/src/esengine/bindings/WebSDKEntry.cpp b/src/esengine/bindings/WebSDKEntry.cppindex 8af519707..8362ab0ac 100644--- a/src/esengine/bindings/WebSDKEntry.cpp+++ b/src/esengine/bindings/WebSDKEntry.cpp@@ -34,6 +34,12 @@#include "../renderer/CustomGeometry.hpp"#include "../resource/ResourceManager.hpp"#include "../ecs/TransformSystem.hpp"+#include "../ecs/components/Velocity.hpp"+#include "../ecs/components/Camera.hpp"+#include "../ecs/components/UIRect.hpp"+#include "../ecs/components/RigidBody.hpp"+#include "../ecs/components/Collider.hpp"+#include "../ecs/components/ShapeRenderer.hpp"#include "../animation/TweenSystem.hpp"#include "../core/Log.hpp"#ifdef ES_ENABLE_SPINE@@ -315,11 +321,113 @@ int getSpritePtr(ecs::Registry& r, u32 e) {return static_cast<int>(reinterpret_cast<uintptr_t>(s));}+// Velocity: 2x vec3 = 24 bytes pure float+static_assert(offsetof(ecs::Velocity, linear) == 0);+static_assert(offsetof(ecs::Velocity, angular) == 12);++int getVelocityPtr(ecs::Registry& r, u32 e) {+ auto* v = r.tryGet<ecs::Velocity>(static_cast<Entity>(e));+ if (!v) return 0;+ return static_cast<int>(reinterpret_cast<uintptr_t>(v));+}++// Camera: enum u8 + padding + floats + bool + i32+static_assert(offsetof(ecs::Camera, projectionType) == 0);+static_assert(offsetof(ecs::Camera, fov) == 4);+static_assert(offsetof(ecs::Camera, orthoSize) == 8);+static_assert(offsetof(ecs::Camera, nearPlane) == 12);+static_assert(offsetof(ecs::Camera, farPlane) == 16);+static_assert(offsetof(ecs::Camera, aspectRatio) == 20);+static_assert(offsetof(ecs::Camera, isActive) == 24);+static_assert(offsetof(ecs::Camera, priority) == 28);+static_assert(offsetof(ecs::Camera, viewportX) == 32);+static_assert(offsetof(ecs::Camera, viewportY) == 36);+static_assert(offsetof(ecs::Camera, viewportW) == 40);+static_assert(offsetof(ecs::Camera, viewportH) == 44);+static_assert(offsetof(ecs::Camera, clearFlags) == 48);++int getCameraPtr(ecs::Registry& r, u32 e) {+ auto* c = r.tryGet<ecs::Camera>(static_cast<Entity>(e));+ if (!c) return 0;+ return static_cast<int>(reinterpret_cast<uintptr_t>(c));+}++// UIRect: 6x vec2 (48 bytes) + computed fields+static_assert(offsetof(ecs::UIRect, anchorMin) == 0);+static_assert(offsetof(ecs::UIRect, anchorMax) == 8);+static_assert(offsetof(ecs::UIRect, offsetMin) == 16);+static_assert(offsetof(ecs::UIRect, offsetMax) == 24);+static_assert(offsetof(ecs::UIRect, size) == 32);+static_assert(offsetof(ecs::UIRect, pivot) == 40);+static_assert(offsetof(ecs::UIRect, computed_width_) == 48);+static_assert(offsetof(ecs::UIRect, computed_height_) == 52);++int getUIRectPtr(ecs::Registry& r, u32 e) {+ auto* rect = r.tryGet<ecs::UIRect>(static_cast<Entity>(e));+ if (!rect) return 0;+ return static_cast<int>(reinterpret_cast<uintptr_t>(rect));+}++// RigidBody: enum u8 + padding + floats + bools+static_assert(offsetof(ecs::RigidBody, bodyType) == 0);+static_assert(offsetof(ecs::RigidBody, gravityScale) == 4);+static_assert(offsetof(ecs::RigidBody, linearDamping) == 8);+static_assert(offsetof(ecs::RigidBody, angularDamping) == 12);+static_assert(offsetof(ecs::RigidBody, fixedRotation) == 16);+static_assert(offsetof(ecs::RigidBody, bullet) == 17);+static_assert(offsetof(ecs::RigidBody, enabled) == 18);++int getRigidBodyPtr(ecs::Registry& r, u32 e) {+ auto* rb = r.tryGet<ecs::RigidBody>(static_cast<Entity>(e));+ if (!rb) return 0;+ return static_cast<int>(reinterpret_cast<uintptr_t>(rb));+}++// BoxCollider: vec2 + vec2 + floats + bools + u32s+static_assert(offsetof(ecs::BoxCollider, halfExtents) == 0);+static_assert(offsetof(ecs::BoxCollider, offset) == 8);+static_assert(offsetof(ecs::BoxCollider, density) == 16);+static_assert(offsetof(ecs::BoxCollider, friction) == 20);+static_assert(offsetof(ecs::BoxCollider, restitution) == 24);+static_assert(offsetof(ecs::BoxCollider, isSensor) == 28);+static_assert(offsetof(ecs::BoxCollider, enabled) == 29);+static_assert(offsetof(ecs::BoxCollider, categoryBits) == 32);+static_assert(offsetof(ecs::BoxCollider, maskBits) == 36);++int getBoxColliderPtr(ecs::Registry& r, u32 e) {+ auto* bc = r.tryGet<ecs::BoxCollider>(static_cast<Entity>(e));+ if (!bc) return 0;+ return static_cast<int>(reinterpret_cast<uintptr_t>(bc));+}++// CircleCollider: f32 + vec2 + floats + bools + u32s+static_assert(offsetof(ecs::CircleCollider, radius) == 0);+static_assert(offsetof(ecs::CircleCollider, offset) == 4);+static_assert(offsetof(ecs::CircleCollider, density) == 12);+static_assert(offsetof(ecs::CircleCollider, friction) == 16);+static_assert(offsetof(ecs::CircleCollider, restitution) == 20);+static_assert(offsetof(ecs::CircleCollider, isSensor) == 24);+static_assert(offsetof(ecs::CircleCollider, enabled) == 25);+static_assert(offsetof(ecs::CircleCollider, categoryBits) == 28);+static_assert(offsetof(ecs::CircleCollider, maskBits) == 32);++int getCircleColliderPtr(ecs::Registry& r, u32 e) {+ auto* cc = r.tryGet<ecs::CircleCollider>(static_cast<Entity>(e));+ if (!cc) return 0;+ return static_cast<int>(reinterpret_cast<uintptr_t>(cc));+}+} // namespace esengineEMSCRIPTEN_BINDINGS(esengine_ptr_access) {emscripten::function("getTransformPtr", &esengine::getTransformPtr);emscripten::function("getSpritePtr", &esengine::getSpritePtr);+ emscripten::function("getVelocityPtr", &esengine::getVelocityPtr);+ emscripten::function("getCameraPtr", &esengine::getCameraPtr);+ emscripten::function("getUIRectPtr", &esengine::getUIRectPtr);+ emscripten::function("getRigidBodyPtr", &esengine::getRigidBodyPtr);+ emscripten::function("getBoxColliderPtr", &esengine::getBoxColliderPtr);+ emscripten::function("getCircleColliderPtr", &esengine::getCircleColliderPtr);}EMSCRIPTEN_BINDINGS(esengine_renderer) {