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.

refactor(sdk): one submission path, because the core never knew which runtime

esengine0f50e04428d1
diff --git a/sdk/src/skeletal/submitMeshes.ts b/sdk/src/skeletal/submitMeshes.ts
new file mode 100644
index 000000000..7e7741075
--- /dev/null
+++ b/sdk/src/skeletal/submitMeshes.ts
@@ -0,0 +1,84 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/**
+ * @file skeletal/submitMeshes.ts
+ * @brief Handing one entity's posed geometry to the engine core.
+ *
+ * @details The core's entry point is `renderer_submitSpineBatchByEntity`, and the
+ * name is the only thing about it that is Spine's: it takes vertices,
+ * indices, a texture, a blend mode, an entity, a scale, two flips and a
+ * layer, and nothing in that list knows what posed them. DragonBones
+ * submits through exactly the same call.
+ *
+ * (The name is worth fixing, but it spans the wasm ABI, the native host
+ * and the WeChat build, so it is its own change rather than a rename
+ * smuggled into this one.)
+ *
+ * The copy is the delicate part. A module's batch bytes live in ITS heap,
+ * and the core reads from the CORE's — so each batch is copied into an
+ * engine-side scratch arena and submitted while the source view is still
+ * live. Nothing intermediate is allocated, and nothing is cached between
+ * frames.
+ */
+import { withScratch } from '../wasmScratch';
+import type { Entity } from '../types';
+import type { MeshBatchVisitor } from './meshBatches';
+
+/** How an entity's skeleton sits in the world, independent of what posed it. */
+export interface SkeletalSubmitProps {
+ skeletonScale: number;
+ flipX: boolean;
+ flipY: boolean;
+ layer: number;
+}
+
+/** The slice of the engine core this needs. */
+export interface SkeletalSubmitCore {
+ renderer_submitSpineBatchByEntity?(
+ registry: unknown,
+ verticesPtr: number, vertexCount: number,
+ indicesPtr: number, indexCount: number,
+ textureId: number, blendMode: number,
+ entity: number, skeletonScale: number, flipX: boolean, flipY: boolean,
+ layer: number, depth: number,
+ ): void;
+ _malloc?(size: number): number;
+ _free?(ptr: number): void;
+ HEAPU8?: Uint8Array;
+}
+
+/** Iterates a module's batches for one instance. */
+export type MeshBatchWalker = (cb: MeshBatchVisitor) => void;
+
+/**
+ * Copy each posed batch into the core's heap and submit it for `entity`.
+ *
+ * Returns false when the core cannot accept geometry at all (no submit export, no
+ * heap) — the caller stops rather than repeating the check per entity.
+ */
+export function submitEntityMeshes(
+ core: SkeletalSubmitCore,
+ registry: unknown,
+ entity: Entity,
+ props: SkeletalSubmitProps,
+ walk: MeshBatchWalker,
+): boolean {
+ const submit = core.renderer_submitSpineBatchByEntity;
+ const heap = core.HEAPU8;
+ if (!submit || !heap || !core._malloc || !core._free) return false;
+
+ const allocator = { _malloc: core._malloc, _free: core._free };
+ withScratch(allocator, alloc => {
+ walk((vertBytes, idxBytes, vertexCount, indexCount, textureId, blendMode) => {
+ const dstVert = alloc(vertBytes.byteLength);
+ const dstIdx = alloc(idxBytes.byteLength);
+ heap.set(vertBytes, dstVert);
+ heap.set(idxBytes, dstIdx);
+ submit.call(core, registry,
+ dstVert, vertexCount, dstIdx, indexCount,
+ textureId, blendMode, entity as number,
+ props.skeletonScale, props.flipX, props.flipY, props.layer, 0);
+ });
+ });
+ return true;
+}
diff --git a/sdk/src/spine/ModuleBackend.ts b/sdk/src/spine/ModuleBackend.ts
index a2cfde8a2..2adca86f3 100644
--- a/sdk/src/spine/ModuleBackend.ts
+++ b/sdk/src/spine/ModuleBackend.ts
@@ -6,7 +6,7 @@ import type { EngineApi } from '../ecs/engineApi';
import type { SpineModuleController } from './SpineController';
import type { RawSpineEvent, ConstraintList, TransformMixData, PathMixData } from './SpineController';
import { log } from '../logger';
-import { withScratch } from '../wasmScratch';
+import { submitEntityMeshes } from '../skeletal/submitMeshes';
interface EntityInfo {
skelHandle: number;
@@ -235,29 +235,15 @@ export class ModuleBackend {
* batches cross through its heap, which is wasm linear memory on the web and the
* host arena on a device. */
extractAndSubmitMeshes(core: NonNullable<EngineApi>, registry: CppRegistry): void {
- const submitFn = core.renderer_submitSpineBatchByEntity;
- const heap = core.HEAPU8;
- if (!submitFn || !heap || !core._malloc || !core._free) return;
- const allocator = { _malloc: core._malloc, _free: core._free };
-
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(allocator, alloc => {
- this.controller_.forEachMeshBatch(info.instanceId,
- (vertBytes, idxBytes, vertexCount, indexCount, textureId, blendMode) => {
- const dstVert = alloc(vertBytes.byteLength);
- const dstIdx = alloc(idxBytes.byteLength);
- heap.set(vertBytes, dstVert);
- heap.set(idxBytes, dstIdx);
- submitFn.call(core, registry,
- dstVert, vertexCount, dstIdx, indexCount,
- textureId, blendMode, entity as number,
- info.skeletonScale, info.flipX, info.flipY, info.layer, 0);
- });
- });
+ // Shared with every skeletal runtime (skeletal/submitMeshes): the core's
+ // entry point takes geometry and a transform, and knows nothing about
+ // what posed them. A false means the core cannot take geometry at all,
+ // so there is no point asking again for the next entity.
+ const accepted = submitEntityMeshes(core, registry, entity, info, cb =>
+ this.controller_.forEachMeshBatch(info.instanceId, cb));
+ if (!accepted) return;
}
}