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: zero-allocation spine mesh submit for legacy 3.8/4.1 runtimes (C7)
esenginee6be33e272b4
diff --git a/sdk/src/spine/ModuleBackend.ts b/sdk/src/spine/ModuleBackend.tsindex 146617c2c..adb3d4a6e 100644--- a/sdk/src/spine/ModuleBackend.ts+++ b/sdk/src/spine/ModuleBackend.ts@@ -14,18 +14,10 @@ interface EntityInfo {layer: number;}-type BatchEntry = {- vertices: Float32Array; indices: Uint16Array;- textureId: number; blendMode: number;- entity: number; info: EntityInfo;-};-export class ModuleBackend {private controller_: SpineModuleController;private entities_: Map<Entity, EntityInfo> = new Map();private disabledEntities_: Set<Entity> = new Set();- private cachedFrame_: number = -1;- private cachedEntries_: BatchEntry[] = [];constructor(controller: SpineModuleController) {this.controller_ = controller;@@ -201,60 +193,29 @@ export class ModuleBackend {}}- extractAndSubmitMeshes(coreModule: ESEngineModule, registry: CppRegistry, frameCount: number = -1): void {+ extractAndSubmitMeshes(coreModule: ESEngineModule, registry: CppRegistry): void {const submitFn = coreModule.renderer_submitSpineBatchByEntity;if (!submitFn) return;- if (this.cachedFrame_ !== frameCount) {- this.cachedEntries_.length = 0;- for (const [entity, info] of this.entities_) {- if (this.disabledEntities_.has(entity)) continue;- const batches = this.controller_.extractMeshBatches(info.instanceId);- for (const batch of batches) {- if (batch.vertices.length === 0 || batch.indices.length === 0) continue;- this.cachedEntries_.push({- vertices: batch.vertices, indices: batch.indices,- textureId: batch.textureId, blendMode: batch.blendMode,- entity: entity as number, info,+ for (const [entity, info] of this.entities_) {+ if (this.disabledEntities_.has(entity)) continue;+ // One engine-side scratch arena per entity; each batch's spine-heap+ // bytes are copied straight into it and submitted while the spine view+ // is still live. No intermediate JS arrays, no per-frame cache (C7).+ withScratch(coreModule, alloc => {+ this.controller_.forEachMeshBatch(info.instanceId,+ (vertBytes, idxBytes, vertexCount, indexCount, textureId, blendMode) => {+ const dstVert = alloc(vertBytes.byteLength);+ const dstIdx = alloc(idxBytes.byteLength);+ coreModule.HEAPU8.set(vertBytes, dstVert);+ coreModule.HEAPU8.set(idxBytes, dstIdx);+ submitFn.call(coreModule, registry,+ dstVert, vertexCount, dstIdx, indexCount,+ textureId, blendMode, entity as number,+ info.skeletonScale, info.flipX, info.flipY, info.layer, 0);});- }- }- this.cachedFrame_ = frameCount;+ });}-- if (this.cachedEntries_.length === 0) return;-- let totalVertBytes = 0;- let totalIdxBytes = 0;- for (const e of this.cachedEntries_) {- totalVertBytes += e.vertices.byteLength;- totalIdxBytes += e.indices.byteLength;- }-- withScratch(coreModule, alloc => {- const vertBase = alloc(totalVertBytes);- const idxBase = alloc(totalIdxBytes);-- let vOff = 0;- let iOff = 0;- for (const e of this.cachedEntries_) {- const vertPtr = vertBase + vOff;- const idxPtr = idxBase + iOff;-- coreModule.HEAPF32.set(e.vertices, vertPtr >> 2);- new Uint16Array(coreModule.HEAPU8.buffer, idxPtr, e.indices.length).set(e.indices);-- submitFn.call(coreModule, registry,- vertPtr, e.vertices.length / 8,- idxPtr, e.indices.length,- e.textureId, e.blendMode,- e.entity, e.info.skeletonScale, e.info.flipX, e.info.flipY,- e.info.layer, 0);-- vOff += e.vertices.byteLength;- iOff += e.indices.byteLength;- }- });}removeEntity(entity: Entity): void {diff --git a/sdk/src/spine/SpineController.ts b/sdk/src/spine/SpineController.tsindex d49f39d62..9ef28085a 100644--- a/sdk/src/spine/SpineController.ts+++ b/sdk/src/spine/SpineController.ts@@ -221,19 +221,24 @@ export class SpineModuleController {// Mesh Extraction// =========================================================================- extractMeshBatches(instanceId: number): {- vertices: Float32Array;- indices: Uint16Array;- textureId: number;- blendMode: number;- }[] {+ /**+ * Iterate this instance's mesh batches, invoking `cb` for each with Uint8Array+ * views directly into spine wasm scratch — valid only for the duration of the+ * call. No per-batch Float32Array/Uint16Array allocation and no copy out; the+ * consumer copies the bytes straight into its own wasm heap while the view is+ * live. Replaces extractMeshBatches, which allocated and cached two typed+ * arrays per batch per frame (C7).+ */+ forEachMeshBatch(+ instanceId: number,+ cb: (+ vertBytes: Uint8Array, indexBytes: Uint8Array,+ vertexCount: number, indexCount: number,+ textureId: number, blendMode: number,+ ) => void,+ ): void {const batchCount = this.api_.getMeshBatchCount(instanceId);- const batches: {- vertices: Float32Array;- indices: Uint16Array;- textureId: number;- blendMode: number;- }[] = [];+ if (batchCount === 0) return;withMalloc(this.raw_, 8, metaPtr => {const texIdPtr = metaPtr;@@ -244,30 +249,25 @@ export class SpineModuleController {const indexCount = this.api_.getMeshBatchIndexCount(instanceId, i);if (vertexCount <= 0 || indexCount <= 0) continue;- const vertBytes = vertexCount * 8 * 4;- const idxBytes = indexCount * 2;+ const vertByteLen = vertexCount * 8 * 4;+ const idxByteLen = indexCount * 2;withScratch(this.raw_, alloc => {- const vertPtr = alloc(vertBytes);- const idxPtr = alloc(idxBytes);+ const vertPtr = alloc(vertByteLen);+ const idxPtr = alloc(idxByteLen);this.api_.getMeshBatchData(instanceId, i, vertPtr, idxPtr, texIdPtr, blendPtr);- const vertices = new Float32Array(- this.raw_.HEAPF32.buffer, vertPtr, vertexCount * 8);- const indices = new Uint16Array(- this.raw_.HEAPU8.buffer, idxPtr, indexCount);-- batches.push({- vertices: new Float32Array(vertices),- indices: new Uint16Array(indices),- textureId: this.raw_.HEAPU32[texIdPtr >> 2],- blendMode: this.raw_.HEAPU32[blendPtr >> 2],- });+ cb(+ new Uint8Array(this.raw_.HEAPU8.buffer, vertPtr, vertByteLen),+ new Uint8Array(this.raw_.HEAPU8.buffer, idxPtr, idxByteLen),+ vertexCount, indexCount,+ this.raw_.HEAPU32[texIdPtr >> 2],+ this.raw_.HEAPU32[blendPtr >> 2],+ );});}});- return batches;}// =========================================================================diff --git a/sdk/src/spine/SpineManager.ts b/sdk/src/spine/SpineManager.tsindex 532e79c0f..4c294f1e2 100644--- a/sdk/src/spine/SpineManager.ts+++ b/sdk/src/spine/SpineManager.ts@@ -91,9 +91,9 @@ export class SpineManager {}}- submitMeshes(registry: CppRegistry, frameCount: number = -1): void {+ submitMeshes(registry: CppRegistry): void {for (const backend of this.backends_.values()) {- backend.extractAndSubmitMeshes(this.coreModule_, registry, frameCount);+ backend.extractAndSubmitMeshes(this.coreModule_, registry);}}diff --git a/sdk/src/spine/SpinePlugin.ts b/sdk/src/spine/SpinePlugin.tsindex 67682db5a..7bdedd63c 100644--- a/sdk/src/spine/SpinePlugin.ts+++ b/sdk/src/spine/SpinePlugin.ts@@ -63,8 +63,7 @@ export class SpinePlugin implements Plugin {const app = this.app_;const pipeline = app.pipeline;pipeline?.addPreFlushCallback((registry) => {- const fc = app.getResource(Time).frameCount;- manager.submitMeshes(registry._cpp, fc);+ manager.submitMeshes(registry._cpp);});}}@@ -125,8 +124,7 @@ export class SpinePlugin implements Plugin {const manager = this.spineManager_;const pipeline = app.pipeline;pipeline?.addPreFlushCallback((registry) => {- const fc = app.getResource(Time).frameCount;- manager.submitMeshes(registry._cpp, fc);+ manager.submitMeshes(registry._cpp);});}}