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.

feat(spine): a clip budget charged for what the engine still pays

esengine505d5a24acd0
diff --git a/sdk/etc/spine.api.md b/sdk/etc/spine.api.md
index 4cd4aad65..dd8f67de1 100644
--- a/sdk/etc/spine.api.md
+++ b/sdk/etc/spine.api.md
@@ -65,6 +65,7 @@ events: readonly SpineEvent[]
```
@internal runtimeForDiagnostics: (version: SpineVersion) => SpineRuntime | undefined
boundEntities: () => Iterable<Entity>
+clipBudget: (entity: Entity) => SpineClipBudget | null
collectAllEvents: () => { entity: Entity; raw: RawSpineEvent; }[]
dispose: () => void
enableEvents: (entity: Entity) => void
@@ -104,6 +105,7 @@ static prototype: SpineManager
## SpineModuleController — class @experimental
```
addAnimation: (instanceId: number, animation: string, loop?: boolean, delay?: number, track?: number) => boolean
+clipBudget: (instanceId: number) => SpineClipBudget | null
collectEvents: (instanceId: number) => RawSpineEvent[]
createInstance: (skeletonHandle: number) => number
destroyInstance: (instanceId: number) => void
@@ -159,6 +161,7 @@ static prototype: SpinePlugin
## SpineRuntime — class @experimental
```
+clipBudget: (entity: Entity) => SpineClipBudget | null
collectAllEvents: () => { entity: Entity; raw: RawSpineEvent; }[]
dispose: () => void
enableEvents: (entity: Entity) => void
diff --git a/sdk/src/spine/SpineController.ts b/sdk/src/spine/SpineController.ts
index e19af8753..7edb6a601 100644
--- a/sdk/src/spine/SpineController.ts
+++ b/sdk/src/spine/SpineController.ts
@@ -11,7 +11,7 @@ import { SpineModuleBridge } from './SpineBridge';
import { log } from '../util/logger';
import { withMalloc, withScratch } from '../wasm/wasmScratch';
import { forEachMeshBatch, type MeshBatchVisitor } from '../skeletal/meshBatches';
-import type { SkeletalProbe } from './spineMetrics';
+import type { SpineClipBudget, SkeletalProbe } from './spineMetrics';
export interface RawSpineEvent {
type: number;
@@ -192,6 +192,40 @@ export class SpineModuleController {
return this.api_.getBoneRotation(instanceId, boneName);
}
+ /**
+ * What this instance's clipping costs the engine in the pose it is in, as
+ * work rather than time: a coefficient measured on one machine and compiler
+ * stops being true, and the three quantities below do not.
+ *
+ * `null` where the runtime cannot answer — 2.1 and 4.3 have no counted walk.
+ */
+ clipBudget(instanceId: number): SpineClipBudget | null {
+ return withMalloc(this.raw_, 11 * 4, (ptr) => {
+ if (!this.api_.clipBudget(instanceId, ptr)) return null;
+ const u32 = this.raw_.HEAPU32;
+ const at = ptr >> 2;
+ const charged = u32[at + 6];
+ const inputVertices = u32[at + 8];
+ const outputVertices = u32[at + 9];
+ const outputTriangles = u32[at + 10];
+ return {
+ rawVertices: u32[at],
+ pieces: u32[at + 1],
+ effectiveEdges: u32[at + 2],
+ candidateTriangles: u32[at + 3],
+ rejectedTriangles: u32[at + 4],
+ bypassedTriangles: u32[at + 5],
+ chargedTriangles: charged,
+ edgeWork: u32[at + 7],
+ inputVertices,
+ outputVertices,
+ outputTriangles,
+ vertexAmplification: inputVertices > 0 ? outputVertices / inputVertices : 0,
+ triangleAmplification: charged > 0 ? outputTriangles / charged : 0,
+ };
+ });
+ }
+
getBounds(instanceId: number): { x: number; y: number; width: number; height: number } {
return withMalloc(this.raw_, 16, ptr => {
this.api_.getBounds(instanceId, ptr, ptr + 4, ptr + 8, ptr + 12);
diff --git a/sdk/src/spine/SpineManager.ts b/sdk/src/spine/SpineManager.ts
index bae9bda62..9095b1370 100644
--- a/sdk/src/spine/SpineManager.ts
+++ b/sdk/src/spine/SpineManager.ts
@@ -8,7 +8,7 @@ import type { RawSpineEvent, ConstraintList, TransformMixData, PathMixData } fro
import type { SpineModuleFactory } from './SpineModuleLoader';
import { SpineRuntime } from './SpineRuntime';
import type { SpineEraBinding } from './prepareSpine';
-import type { SpineFrameMetrics } from './spineMetrics';
+import type { SpineClipBudget, SpineFrameMetrics } from './spineMetrics';
import { log } from '../util/logger';
import type { SpineVersion } from '../sideModules/registry';
@@ -200,6 +200,10 @@ export class SpineManager {
return this.runtimeOf_(entity)?.getBounds(entity) ?? null;
}
+ clipBudget(entity: Entity): SpineClipBudget | null {
+ return this.runtimeOf_(entity)?.clipBudget(entity) ?? null;
+ }
+
getAnimations(entity: Entity): string[] {
return this.runtimeOf_(entity)?.getAnimations(entity) ?? [];
}
diff --git a/sdk/src/spine/SpineModuleLoader.ts b/sdk/src/spine/SpineModuleLoader.ts
index dcf5f8f1a..0dc6cc623 100644
--- a/sdk/src/spine/SpineModuleLoader.ts
+++ b/sdk/src/spine/SpineModuleLoader.ts
@@ -61,6 +61,7 @@ export interface SpineWasmModule {
_spine_probe_extract(instanceId: number, stage: number, useCollector: number): number;
_spine_probe_counts(out: number): void;
_spine_probe_clip_storage(out: number): void;
+ _spine_clipBudget(instanceId: number, out: number): number;
_spine_probe_storage(out: number): void;
_spine_probe_pose(instanceId: number, dt: number, stage: number): number;
_spine_probe_pose_counts(out: number): void;
@@ -131,6 +132,12 @@ export interface SpineWrappedAPI {
probeExtract(instanceId: number, stage: number, useCollector: number): number;
probeCounts(out: number): void;
probeClipStorage(out: number): void;
+
+ /**
+ * What one instance's clipping costs this pose, in work rather than time.
+ * Poses and extracts, so it is a diagnostic call and not a frame reading.
+ */
+ clipBudget(instanceId: number, out: number): number;
probeStorage(out: number): void;
probePose(instanceId: number, dt: number, stage: number): number;
probePoseCounts(out: number): void;
@@ -186,6 +193,7 @@ export function wrapSpineModule(raw: SpineWasmModule): SpineWrappedAPI {
probeExtract: cw('spine_probe_extract', 'number', ['number', 'number', 'number']) as SpineWrappedAPI['probeExtract'],
probeCounts: cw('spine_probe_counts', null, ['number']) as SpineWrappedAPI['probeCounts'],
probeClipStorage: cw('spine_probe_clip_storage', null, ['number']) as SpineWrappedAPI['probeClipStorage'],
+ clipBudget: cw('spine_clipBudget', 'number', ['number', 'number']) as SpineWrappedAPI['clipBudget'],
probeStorage: cw('spine_probe_storage', null, ['number']) as SpineWrappedAPI['probeStorage'],
probePose: cw('spine_probe_pose', 'number', ['number', 'number', 'number']) as SpineWrappedAPI['probePose'],
probePoseCounts: cw('spine_probe_pose_counts', null, ['number']) as SpineWrappedAPI['probePoseCounts'],
diff --git a/sdk/src/spine/SpineRuntime.ts b/sdk/src/spine/SpineRuntime.ts
index 34e3bb699..1b915daf4 100644
--- a/sdk/src/spine/SpineRuntime.ts
+++ b/sdk/src/spine/SpineRuntime.ts
@@ -26,6 +26,7 @@ import { beginSpineFrame, newSpineFrameMetrics, SpineTimeWindow,
import type { SpineVersion } from '../sideModules/registry';
import { log } from '../util/logger';
import { submitEntityMeshes, type SkeletalMaterialOf } from '../skeletal/submitMeshes';
+import type { SpineClipBudget } from './spineMetrics';
interface EntityInfo {
skelHandle: number;
@@ -164,6 +165,12 @@ export class SpineRuntime {
return this.controller_.getBounds(info.instanceId);
}
+ clipBudget(entity: Entity): SpineClipBudget | null {
+ const info = this.entities_.get(entity);
+ if (!info) return null;
+ return this.controller_.clipBudget(info.instanceId);
+ }
+
getAnimations(entity: Entity): string[] {
const info = this.entities_.get(entity);
if (!info) return [];
diff --git a/sdk/src/spine/spineMetrics.ts b/sdk/src/spine/spineMetrics.ts
index 9fcf0801c..12f52ec0e 100644
--- a/sdk/src/spine/spineMetrics.ts
+++ b/sdk/src/spine/spineMetrics.ts
@@ -21,6 +21,39 @@
* per entity to say so.
*/
+/**
+ * What one skeleton's clipping costs, in work rather than time. Three facts and
+ * not one score, because they do not grow alike: `rawVertices` prices OPENING a
+ * region and rises faster than itself, `edgeWork` prices the cut and is linear
+ * in triangles x edges, amplification is what clipping hands the stages after.
+ */
+export interface SpineClipBudget {
+ /** The polygon as authored. Fixed by the attachment, not by the pose. */
+ rawVertices: number;
+ /** Convex pieces it decomposed into. More than one means concave. */
+ pieces: number;
+ /** Edges across those pieces — what a cut is actually charged per triangle. */
+ effectiveEdges: number;
+
+ /** Triangles that reached the region at all. */
+ candidateTriangles: number;
+ /** …whose bounds could not meet it. */
+ rejectedTriangles: number;
+ /** …that were wholly inside a convex one and passed through untouched. */
+ bypassedTriangles: number;
+ /** …that the cut actually ran on. The only ones a budget is charged for. */
+ chargedTriangles: number;
+ /** Σ charged triangles x the edges of the region charging them. */
+ edgeWork: number;
+
+ inputVertices: number;
+ outputVertices: number;
+ outputTriangles: number;
+ /** Sharing the cut destroyed: it rebuilds every triangle's own vertices. */
+ vertexAmplification: number;
+ triangleAmplification: number;
+}
+
/** Every crossing a frame made into the module, by what it asked for. */
export interface SpineAbiCounts {
pose: number;
diff --git a/sdk/tests/spine-clip-budget.test.ts b/sdk/tests/spine-clip-budget.test.ts
new file mode 100644
index 000000000..860e7926d
--- /dev/null
+++ b/sdk/tests/spine-clip-budget.test.ts
@@ -0,0 +1,134 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/**
+ * @file spine-clip-budget.test.ts
+ * @brief The budget charges what the engine actually does, not what the
+ * algorithm would have done.
+ *
+ * @details A diagnostic that says "339 triangles against 13 edges" about a
+ * skeleton whose every triangle is rejected on bounds is worse than no
+ * diagnostic: it sends whoever authored the mask after a cost that no
+ * longer exists. So the charge is taken AFTER the fast paths, and the
+ * triangles they answered are reported separately.
+ *
+ * Everything here is dimensionless. The nanoseconds behind the model
+ * were measured on one machine with one compiler; the quantities are
+ * the asset's and they outlive both.
+ */
+import { describe, it, expect, beforeAll } from 'vitest';
+import { existsSync, readFileSync } from 'node:fs';
+import { resolve } from 'node:path';
+import { WASM_DIR } from './helpers/loadWasm';
+import { wrapSpineModule } from '../src/spine/SpineModuleLoader';
+import { SpineModuleController } from '../src/spine/SpineController';
+import type { SpineWasmModule } from '../src/spine/SpineModuleLoader';
+import type { SpineClipBudget } from '../src/spine/spineMetrics';
+import { syntheticSkeleton } from './helpers/syntheticSpine';
+import type { SyntheticOptions } from './helpers/syntheticSpine';
+
+const SPINE38_JS = resolve(WASM_DIR, 'spine38.js');
+const SPINE38_WASM = resolve(WASM_DIR, 'spine38.wasm');
+const FIXTURES = resolve(__dirname, '../benchmarks/fixtures/spine');
+const HAS_WASM = existsSync(SPINE38_WASM);
+const QUADS = 16;
+
+let raw: SpineWasmModule;
+let controller: SpineModuleController;
+
+beforeAll(async () => {
+ if (!HAS_WASM) return;
+ const factory = (await import(SPINE38_JS)).default as (opts: unknown) => Promise<SpineWasmModule>;
+ const bytes = readFileSync(SPINE38_WASM);
+ raw = await factory({
+ instantiateWasm(imports: WebAssembly.Imports, cb: (i: WebAssembly.Instance, m: WebAssembly.Module) => void) {
+ void WebAssembly.instantiate(bytes, imports).then((r) => cb(r.instance, r.module));
+ return {};
+ },
+ });
+ controller = new SpineModuleController(raw, wrapSpineModule(raw));
+});
+
+function budgetOf(options: SyntheticOptions): SpineClipBudget {
+ const { json, atlas } = syntheticSkeleton(options);
+ const handle = controller.loadSkeleton(new TextEncoder().encode(json), atlas, false);
+ if (handle < 0) throw new Error(controller.getLastError());
+ controller.setAtlasPageTexture(handle, 0, 1, 64, 64);
+ const instanceId = controller.createInstance(handle);
+ controller.update(instanceId, 0);
+ const budget = controller.clipBudget(instanceId);
+ if (!budget) throw new Error('the runtime reported no budget');
+ return budget;
+}
+
+function budgetOfAsset(skel: string, atlas: string, animation: string, dt: number): SpineClipBudget {
+ const handle = controller.loadSkeleton(
+ new Uint8Array(readFileSync(resolve(FIXTURES, skel))),
+ readFileSync(resolve(FIXTURES, atlas), 'utf-8'), true);
+ if (handle < 0) throw new Error(controller.getLastError());
+ for (let i = 0, pages = controller.getAtlasPageCount(handle); i < pages; i++) {
+ controller.setAtlasPageTexture(handle, i, 1, 2048, 2048);
+ }
+ const instanceId = controller.createInstance(handle);
+ expect(controller.play(instanceId, animation, true)).toBe(true);
+ controller.update(instanceId, dt);
+ const budget = controller.clipBudget(instanceId);
+ if (!budget) throw new Error('the runtime reported no budget');
+ return budget;
+}
+
+describe.skipIf(!HAS_WASM)('the clip budget charges what is still being paid', () => {
+ it('a skeleton the fast paths answered is charged nothing', () => {
+ // spineboy's portal region has the whole body outside it, so every
+ // candidate is rejected on bounds. A budget quoting 339 triangles here
+ // would send an artist after a cost the engine stopped paying.
+ const budget = budgetOfAsset('spineboy-38/spineboy-pro.skel', 'spineboy-38/spineboy.atlas', 'portal', 0.3);
+ expect(budget.candidateTriangles).toBeGreaterThan(100);
+ expect(budget.rejectedTriangles).toBe(budget.candidateTriangles);
+ expect(budget.chargedTriangles).toBe(0);
+ expect(budget.edgeWork, 'work was charged for triangles nothing ran on').toBe(0);
+ });
+
+ it('a skeleton that really crosses is charged for all of it', () => {
+ const budget = budgetOfAsset('coin-38/coin-pro.skel', 'coin-38/coin.atlas', 'animation', 0.35);
+ expect(budget.rejectedTriangles + budget.bypassedTriangles + budget.chargedTriangles)
+ .toBe(budget.candidateTriangles);
+ expect(budget.chargedTriangles).toBeGreaterThan(0);
+ expect(budget.edgeWork).toBe(budget.chargedTriangles * budget.effectiveEdges);
+ });
+
+ it('the same authored vertex count can cost two different edge counts', () => {
+ // What the diagnostic has to explain: sixteen points drawn, forty-one
+ // edges paid. Concavity is not a tax of its own — it is this expansion.
+ const convex = budgetOf({ quads: QUADS, relation: 'all-crossing', polygonVertices: 16 });
+ const concave = budgetOf({ quads: QUADS, relation: 'all-crossing', polygonVertices: 16, concave: true });
+
+ expect(convex.rawVertices).toBe(concave.rawVertices);
+ expect(convex.pieces).toBe(1);
+ expect(concave.pieces).toBeGreaterThan(1);
+ expect(concave.effectiveEdges / concave.rawVertices)
+ .toBeGreaterThan(convex.effectiveEdges / convex.rawVertices);
+ expect(concave.edgeWork).toBeGreaterThan(convex.edgeWork);
+ });
+
+ it('what the cut hands on is reported as its own fact', () => {
+ const budget = budgetOf({ quads: QUADS, relation: 'all-crossing' });
+ expect(budget.inputVertices).toBe((QUADS + 1) * 2);
+ expect(budget.outputVertices).toBeGreaterThan(budget.inputVertices);
+ expect(budget.vertexAmplification).toBeCloseTo(budget.outputVertices / budget.inputVertices, 6);
+ expect(budget.triangleAmplification).toBeCloseTo(budget.outputTriangles / budget.chargedTriangles, 6);
+ });
+
+ it('simplifying the polygon always reads as cheaper', () => {
+ // The trap a budget built on output would fall into: cutting against a
+ // simpler shape can hand back MORE geometry, and the artist who did the
+ // right thing would be told they made it worse.
+ const work = [4, 8, 16].map((polygonVertices) =>
+ budgetOf({ quads: QUADS, relation: 'all-crossing', polygonVertices }));
+ for (let i = 1; i < work.length; i++) {
+ expect(work[i].rawVertices).toBeGreaterThan(work[i - 1].rawVertices);
+ expect(work[i].effectiveEdges).toBeGreaterThan(work[i - 1].effectiveEdges);
+ expect(work[i].edgeWork, 'a simpler polygon was charged more')
+ .toBeGreaterThan(work[i - 1].edgeWork);
+ }
+ });
+});
diff --git a/sdk/tests/spine-clip-stages.test.ts b/sdk/tests/spine-clip-stages.test.ts
index 1b418173e..6e1f56959 100644
--- a/sdk/tests/spine-clip-stages.test.ts
+++ b/sdk/tests/spine-clip-stages.test.ts
@@ -31,6 +31,8 @@ const COUNTERS = [
'verticesGenerated', 'verticesEmitted', 'indicesEmitted', 'emits',
'clipPolygons', 'clipPolygonVertices', 'clipPolygonEdges', 'clipInputTriangles', 'clipOutputTriangles',
'clipBoundsRejects', 'clipInsideAccepts',
+ 'clipCandidateTriangles', 'clipRejectedTriangles', 'clipBypassedTriangles',
+ 'clipEdgeWork', 'clipInputVertices', 'clipOutputVertices',
] as const;
type Counts = Record<(typeof COUNTERS)[number], number>;
const STAGE_EMIT = 5;
diff --git a/src/esengine/bindings/modules/spine/SpineBindings.hpp b/src/esengine/bindings/modules/spine/SpineBindings.hpp
index 0d4d0abb0..c16fd81d1 100644
--- a/src/esengine/bindings/modules/spine/SpineBindings.hpp
+++ b/src/esengine/bindings/modules/spine/SpineBindings.hpp
@@ -86,9 +86,13 @@ size_t spine_eventBufferBytes();
// or counts and drops them: the one way to price the buffers apart from the work.
int spine_probe_extract(int instanceId, int stage, int useCollector);
-// The sixteen counters of the last probe run, into as many u32 slots.
+// The twenty-two counters of the last probe run, into as many u32 slots.
void spine_probe_counts(uint32_t* out);
+// What one instance's clipping costs this pose, in work rather than time, into
+// eleven u32 slots. Poses and extracts; zero where the runtime cannot count.
+int spine_clipBudget(int instanceId, uint32_t* out);
+
// The clipper's own scratch capacities, into six u32 slots.
void spine_probe_clip_storage(uint32_t* out);
diff --git a/src/esengine/bindings/modules/spine/SpineModuleEntry.cpp b/src/esengine/bindings/modules/spine/SpineModuleEntry.cpp
index fc057ebb4..09c140bec 100644
--- a/src/esengine/bindings/modules/spine/SpineModuleEntry.cpp
+++ b/src/esengine/bindings/modules/spine/SpineModuleEntry.cpp
@@ -289,7 +289,7 @@ int spine_probe_extract(int instanceId, int stage, int useCollector) {
stage, &g_probeCounts) ? 1 : 0;
}
-/** The counts of the last {@link spine_probe_extract}, into sixteen u32 slots. */
+/** The counts of the last {@link spine_probe_extract}, into twenty-two u32 slots. */
EMSCRIPTEN_KEEPALIVE
void spine_probe_counts(std::uint32_t* out) {
if (!out) return;
@@ -309,6 +309,43 @@ void spine_probe_counts(std::uint32_t* out) {
out[13] = g_probeCounts.clipOutputTriangles;
out[14] = g_probeCounts.clipBoundsRejects;
out[15] = g_probeCounts.clipInsideAccepts;
+ out[16] = g_probeCounts.clipCandidateTriangles;
+ out[17] = g_probeCounts.clipRejectedTriangles;
+ out[18] = g_probeCounts.clipBypassedTriangles;
+ out[19] = g_probeCounts.clipEdgeWork;
+ out[20] = g_probeCounts.clipInputVertices;
+ out[21] = g_probeCounts.clipOutputVertices;
+}
+
+/**
+ * What one instance's clipping costs THIS pose, in work not time, into eleven
+ * u32 slots: the polygon as authored, what it decomposed into, the triangles
+ * that reached the region and what became of each, and what the cut handed
+ * back. Poses and extracts, because a diagnostic pays for its own answer.
+ */
+EMSCRIPTEN_KEEPALIVE
+int spine_clipBudget(int instanceId, std::uint32_t* out) {
+ if (!out) return 0;
+ for (int i = 0; i < 11; ++i) out[i] = 0;
+ auto* instance = g_ctx.instanceOf(instanceId);
+ if (!instance) return 0;
+
+ es::spine::ProbeCounts counts{};
+ CountingSink sink;
+ if (!es::spine::renderCounted(instance, sink, g_ctx.clippingEnabled, &counts)) return 0;
+
+ out[0] = counts.clipPolygonVertices;
+ out[1] = counts.clipPolygons;
+ out[2] = counts.clipPolygonEdges;
+ out[3] = counts.clipCandidateTriangles;
+ out[4] = counts.clipRejectedTriangles;
+ out[5] = counts.clipBypassedTriangles;
+ out[6] = counts.clipInputTriangles;
+ out[7] = counts.clipEdgeWork;
+ out[8] = counts.clipInputVertices;
+ out[9] = counts.clipOutputVertices;
+ out[10] = counts.clipOutputTriangles;
+ return 1;
}
/** BENCHMARK ONLY — the clipper's own scratch, into six u32 slots. Zero where a
diff --git a/src/esengine/bindings/modules/spine/SpineRuntime.hpp b/src/esengine/bindings/modules/spine/SpineRuntime.hpp
index d8e05df5b..8cd9316d2 100644
--- a/src/esengine/bindings/modules/spine/SpineRuntime.hpp
+++ b/src/esengine/bindings/modules/spine/SpineRuntime.hpp
@@ -254,6 +254,18 @@ struct ProbeCounts {
/// whose bounds miss the region entirely, one wholly inside a convex one.
std::uint32_t clipBoundsRejects;
std::uint32_t clipInsideAccepts;
+
+ /// The same three answers counted in triangles rather than attachments,
+ /// which is what a budget is charged in.
+ std::uint32_t clipCandidateTriangles;
+ std::uint32_t clipRejectedTriangles;
+ std::uint32_t clipBypassedTriangles;
+ /// Σ over charged attachments of triangles × the edges of the region they
+ /// were charged against — accumulated per region rather than multiplied at
+ /// the end, so a skeleton with two regions is priced against both.
+ std::uint32_t clipEdgeWork;
+ std::uint32_t clipInputVertices;
+ std::uint32_t clipOutputVertices;
};
/** BENCHMARK ONLY — the clipper's own scratch arrays, so a second storage
@@ -270,6 +282,13 @@ struct ClipStorage {
/** False where a backend has no clipper of its own to report on. */
bool clipStorage(ClipStorage* out);
+/**
+ * The whole walk with its counters on — what a diagnostic asks for, as opposed
+ * to {@link renderStage}, which stops short and is for pricing steps. False
+ * where a backend has no counted walk.
+ */
+bool renderCounted(Instance* instance, TriangleSink& sink, bool clipping, ProbeCounts* counts);
+
/**
* BENCHMARK ONLY — the same walk as {@link render}, stopped at `stage`, so its
* cost can be decomposed without a clock inside the loop being decomposed.
diff --git a/src/esengine/bindings/modules/spine/SpineRuntime21.cpp b/src/esengine/bindings/modules/spine/SpineRuntime21.cpp
index 3286e59e0..391bb7f09 100644
--- a/src/esengine/bindings/modules/spine/SpineRuntime21.cpp
+++ b/src/esengine/bindings/modules/spine/SpineRuntime21.cpp
@@ -561,6 +561,10 @@ bool clipStorage(ClipStorage*) {
return false;
}
+bool renderCounted(Instance*, TriangleSink&, bool, ProbeCounts*) {
+ return false;
+}
+
int version() {
return ES_SPINE_VERSION;
}
diff --git a/src/esengine/bindings/modules/spine/SpineRuntimeC.cpp b/src/esengine/bindings/modules/spine/SpineRuntimeC.cpp
index 88e0b707f..d0a915b80 100644
--- a/src/esengine/bindings/modules/spine/SpineRuntimeC.cpp
+++ b/src/esengine/bindings/modules/spine/SpineRuntimeC.cpp
@@ -637,6 +637,8 @@ namespace {
*/
struct ClipRegion {
float minX, minY, maxX, maxY;
+ /// Edges across the pieces, so a charge can be priced where it is made.
+ std::uint32_t edges;
/// The decomposition came back as one piece, so "every vertex is inside it"
/// means the whole triangle is. A concave region decomposes into several and
/// a triangle can span two of them while being inside neither.
@@ -660,6 +662,12 @@ void captureClipRegion() {
}
g_clipRegion.convex = g_clipper->clippingPolygons
&& g_clipper->clippingPolygons->size == 1;
+ if (g_clipper->clippingPolygons) {
+ for (int p = 0; p < g_clipper->clippingPolygons->size; ++p) {
+ g_clipRegion.edges +=
+ static_cast<std::uint32_t>(g_clipper->clippingPolygons->items[p]->size / 2);
+ }
+ }
}
/// Bounds that cannot meet the region's: nothing of this attachment survives.
@@ -723,12 +731,21 @@ void emit(TriangleSink& sink, bool clipping,
// Neither changes what is drawn. The first answers "nothing of this
// survives" without cutting; the second answers "all of it does", and
// keeps the attachment's own topology instead of a per-triangle rebuild.
+ if constexpr (COUNT) {
+ counts->clipCandidateTriangles += static_cast<std::uint32_t>(triangleCount / 3);
+ }
if (outsideClipBounds(positions, vertexCount)) {
- if constexpr (COUNT) counts->clipBoundsRejects++;
+ if constexpr (COUNT) {
+ counts->clipBoundsRejects++;
+ counts->clipRejectedTriangles += static_cast<std::uint32_t>(triangleCount / 3);
+ }
return;
}
if (insideConvexClip(positions, vertexCount)) {
- if constexpr (COUNT) counts->clipInsideAccepts++;
+ if constexpr (COUNT) {
+ counts->clipInsideAccepts++;
+ counts->clipBypassedTriangles += static_cast<std::uint32_t>(triangleCount / 3);
+ }
} else {
spSkeletonClipping_clipTriangles(g_clipper, positions, vertexCount * 2,
triangles, triangleCount, uvs, 2);
@@ -741,6 +758,10 @@ void emit(TriangleSink& sink, bool clipping,
counts->clippedEmits++;
counts->clipInputTriangles += static_cast<std::uint32_t>(triangleCount / 3);
counts->clipOutputTriangles += static_cast<std::uint32_t>(outTriangleCount / 3);
+ counts->clipInputVertices += static_cast<std::uint32_t>(vertexCount);
+ counts->clipOutputVertices += static_cast<std::uint32_t>(outVertices);
+ counts->clipEdgeWork +=
+ static_cast<std::uint32_t>(triangleCount / 3) * g_clipRegion.edges;
}
}
}
@@ -906,6 +927,13 @@ void render(Instance* instance, TriangleSink& sink, bool clipping) {
renderImpl<STAGE_EMIT, false>(instance, sink, clipping, nullptr);
}
+bool renderCounted(Instance* instance, TriangleSink& sink, bool clipping, ProbeCounts* counts) {
+ if (!instance || !counts) return false;
+ *counts = ProbeCounts{};
+ renderImpl<STAGE_EMIT, true>(instance, sink, clipping, counts);
+ return true;
+}
+
bool renderStage(Instance* instance, TriangleSink& sink, bool clipping,
int stage, ProbeCounts* counts) {
switch (stage) {
diff --git a/src/esengine/bindings/modules/spine/SpineRuntimeCpp.cpp b/src/esengine/bindings/modules/spine/SpineRuntimeCpp.cpp
index 2db490887..d10d6655a 100644
--- a/src/esengine/bindings/modules/spine/SpineRuntimeCpp.cpp
+++ b/src/esengine/bindings/modules/spine/SpineRuntimeCpp.cpp
@@ -542,6 +542,10 @@ bool clipStorage(ClipStorage*) {
return false;
}
+bool renderCounted(Instance*, TriangleSink&, bool, ProbeCounts*) {
+ return false;
+}
+
int version() {
return ES_SPINE_VERSION;
}