@spacexr/3d-tiles-runtime 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +93 -2
- package/dist/chunk-AI6NWGCA.js +316 -0
- package/dist/chunk-AI6NWGCA.js.map +1 -0
- package/dist/chunk-G7YVZYMR.js +24 -0
- package/dist/chunk-G7YVZYMR.js.map +1 -0
- package/dist/chunk-PJIVVIOF.js +205 -0
- package/dist/chunk-PJIVVIOF.js.map +1 -0
- package/dist/chunk-Q2AL6LWU.js +3 -0
- package/dist/chunk-Q2AL6LWU.js.map +1 -0
- package/dist/chunk-Q35HKP5V.js +249 -0
- package/dist/chunk-Q35HKP5V.js.map +1 -0
- package/dist/chunk-URZDM4BH.js +488 -0
- package/dist/chunk-URZDM4BH.js.map +1 -0
- package/dist/implicit/index.d.ts +64 -0
- package/dist/implicit/index.js +4 -0
- package/dist/implicit/index.js.map +1 -0
- package/dist/index-CNnPRQZc.d.ts +350 -0
- package/dist/index.d.ts +713 -0
- package/dist/index.js +3074 -0
- package/dist/index.js.map +1 -0
- package/dist/metadata/index.d.ts +12 -0
- package/dist/metadata/index.js +3 -0
- package/dist/metadata/index.js.map +1 -0
- package/dist/pipeline/index.d.ts +5 -0
- package/dist/pipeline/index.js +3 -0
- package/dist/pipeline/index.js.map +1 -0
- package/dist/ports/index.d.ts +62 -0
- package/dist/ports/index.js +4 -0
- package/dist/ports/index.js.map +1 -0
- package/dist/resource-DH72MRMN.d.ts +34 -0
- package/dist/spatial/index.d.ts +402 -0
- package/dist/spatial/index.js +3 -0
- package/dist/spatial/index.js.map +1 -0
- package/dist/spatial-3IDPbyv4.d.ts +28 -0
- package/dist/types-Dn5i8Uqg.d.ts +78 -0
- package/dist/types-ri64XfSE.d.ts +237 -0
- package/package.json +102 -5
- package/src/implicit/availability.ts +55 -0
- package/src/implicit/coordinates.ts +193 -0
- package/src/implicit/dense-subtree-loader.ts +23 -0
- package/src/implicit/index.ts +6 -0
- package/src/implicit/standard-subtree-loader.ts +71 -0
- package/src/implicit/tile-data-source.ts +120 -0
- package/src/implicit/types.ts +156 -0
- package/src/index.ts +6 -0
- package/src/metadata/index.ts +2 -0
- package/src/metadata/metadata-resolver.ts +308 -0
- package/src/metadata/types.ts +91 -0
- package/src/pipeline/index.ts +4 -0
- package/src/pipeline/metadata-store.ts +44 -0
- package/src/pipeline/ports.ts +62 -0
- package/src/pipeline/states.ts +56 -0
- package/src/pipeline/tile-3d.ts +165 -0
- package/src/ports/camera.ts +12 -0
- package/src/ports/content.ts +71 -0
- package/src/ports/glyph.ts +13 -0
- package/src/ports/index.ts +32 -0
- package/src/ports/resource.ts +54 -0
- package/src/ports/spatial.ts +28 -0
- package/src/ports/telemetry.ts +44 -0
- package/src/runtime/cache.ts +112 -0
- package/src/runtime/contracts.ts +77 -0
- package/src/runtime/events.ts +57 -0
- package/src/runtime/hooks.ts +99 -0
- package/src/runtime/index.ts +7 -0
- package/src/runtime/instrumentation.ts +163 -0
- package/src/runtime/priority-scheduler.ts +88 -0
- package/src/runtime/quadtree-balance.ts +215 -0
- package/src/runtime/tile-runtime.ts +3508 -0
- package/src/spatial/bounding-volumes.ts +159 -0
- package/src/spatial/ecef-types.ts +110 -0
- package/src/spatial/ecef.ts +196 -0
- package/src/spatial/index.ts +6 -0
- package/src/spatial/utm-bounds.ts +151 -0
- package/src/spatial/utm-types.ts +83 -0
- package/src/spatial/vertical-scale.ts +125 -0
- package/src/spatial/view-metrics.ts +132 -0
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
export type RuntimeMetricName =
|
|
2
|
+
| "frame"
|
|
3
|
+
| "frame.duration"
|
|
4
|
+
| "frame.selected"
|
|
5
|
+
| "tileset.load"
|
|
6
|
+
| "subtree.load"
|
|
7
|
+
| "subtree.load.duration"
|
|
8
|
+
| "subtree.load.bytes"
|
|
9
|
+
| "subtree.cache.evict"
|
|
10
|
+
| "content.load"
|
|
11
|
+
| "content.load.duration"
|
|
12
|
+
| "content.load.network-bytes"
|
|
13
|
+
| "content.error"
|
|
14
|
+
| "content.cache.evict"
|
|
15
|
+
| "tile.attach"
|
|
16
|
+
| "tile.detach"
|
|
17
|
+
| "tile.refine"
|
|
18
|
+
| "tile.coarsen"
|
|
19
|
+
| "tile.refine.attempt"
|
|
20
|
+
| "tile.coarsen.attempt"
|
|
21
|
+
| "cut.entered"
|
|
22
|
+
| "content.cancelled"
|
|
23
|
+
| "cut.left"
|
|
24
|
+
| "tile.node.prune"
|
|
25
|
+
| "cache.sweep.duration"
|
|
26
|
+
| "cache.content.entries"
|
|
27
|
+
| "cache.content.cpu-bytes"
|
|
28
|
+
| "cache.content.gpu-bytes"
|
|
29
|
+
| "cache.subtree.entries"
|
|
30
|
+
| "cache.subtree.bytes"
|
|
31
|
+
| "runtime.error"
|
|
32
|
+
| "metadata.diagnostic";
|
|
33
|
+
|
|
34
|
+
export interface IRuntimeMetric {
|
|
35
|
+
name: RuntimeMetricName;
|
|
36
|
+
value: number;
|
|
37
|
+
unit: "count" | "millisecond" | "pixel" | "byte";
|
|
38
|
+
tags?: Readonly<Record<string, string | number | boolean>>;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Receives operational metrics without imposing a monitoring product. */
|
|
42
|
+
export interface IRuntimeTelemetry {
|
|
43
|
+
record(metric: IRuntimeMetric): void;
|
|
44
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
export interface IRuntimeResourceCost {
|
|
2
|
+
cpuBytes?: number;
|
|
3
|
+
gpuBytes?: number;
|
|
4
|
+
networkBytes?: number;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export interface IRuntimeCacheOptions {
|
|
8
|
+
enabled?: boolean;
|
|
9
|
+
maxMaterializedTiles?: number;
|
|
10
|
+
maxContentEntries?: number;
|
|
11
|
+
/**
|
|
12
|
+
* Makes the budgets of entries and tiles adaptive: they grow to this many times what the current view uses, the
|
|
13
|
+
* selected tiles for `maxContentEntries` and the tiles visited by the traversal for `maxMaterializedTiles`, and come
|
|
14
|
+
* back as the view needs less. Budgets close to the view make a moving camera evict and load the same tiles again and
|
|
15
|
+
* again. Byte budgets stay fixed: they bound the memory. Defaults to 0, fixed budgets.
|
|
16
|
+
*/
|
|
17
|
+
workingSetFactor?: number;
|
|
18
|
+
maxContentCpuBytes?: number;
|
|
19
|
+
maxContentGpuBytes?: number;
|
|
20
|
+
maxSubtreeEntries?: number;
|
|
21
|
+
maxSubtreeBytes?: number;
|
|
22
|
+
/**
|
|
23
|
+
* Time an inactive resource stays cached, in milliseconds: a renderable, a subtree payload or a materialized
|
|
24
|
+
* implicit branch unused for longer is evicted at the next frame. Defaults to 60 000.
|
|
25
|
+
*/
|
|
26
|
+
unusedRetentionMilliseconds?: number;
|
|
27
|
+
/**
|
|
28
|
+
* Number of runtime frames an inactive resource stays cached, in place of `unusedRetentionMilliseconds`.
|
|
29
|
+
*
|
|
30
|
+
* @deprecated Runtime frames only run while the camera moves or loads settle, so a frame count is a short time
|
|
31
|
+
* while the camera turns and an endless one while it rests. Use `unusedRetentionMilliseconds`.
|
|
32
|
+
*/
|
|
33
|
+
unusedFrameRetention?: number;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface IResolvedRuntimeCacheOptions {
|
|
37
|
+
enabled: boolean;
|
|
38
|
+
maxMaterializedTiles: number;
|
|
39
|
+
maxContentEntries: number;
|
|
40
|
+
workingSetFactor: number;
|
|
41
|
+
maxContentCpuBytes: number;
|
|
42
|
+
maxContentGpuBytes: number;
|
|
43
|
+
maxSubtreeEntries: number;
|
|
44
|
+
maxSubtreeBytes: number;
|
|
45
|
+
unusedRetentionMilliseconds: number;
|
|
46
|
+
/** Deprecated frame count, applied instead of the time when an application sets it. */
|
|
47
|
+
unusedFrameRetention?: number;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export type RuntimeCacheResourceKind = "content" | "subtree" | "implicit-branch";
|
|
51
|
+
|
|
52
|
+
export type RuntimeCacheEvictionReason = "stale" | "entry-budget" | "cpu-budget" | "gpu-budget" | "byte-budget" | "node-budget";
|
|
53
|
+
|
|
54
|
+
export interface IRuntimeCacheEviction {
|
|
55
|
+
kind: RuntimeCacheResourceKind;
|
|
56
|
+
reason: RuntimeCacheEvictionReason;
|
|
57
|
+
tileId: string;
|
|
58
|
+
resourceId?: string;
|
|
59
|
+
releasedCpuBytes: number;
|
|
60
|
+
releasedGpuBytes: number;
|
|
61
|
+
releasedEntries: number;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export interface IRuntimeCacheCounters {
|
|
65
|
+
sweeps: number;
|
|
66
|
+
contentEvictions: number;
|
|
67
|
+
subtreeEvictions: number;
|
|
68
|
+
implicitBranchPrunes: number;
|
|
69
|
+
prunedNodes: number;
|
|
70
|
+
releasedCpuBytes: number;
|
|
71
|
+
releasedGpuBytes: number;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export const DEFAULT_RUNTIME_CACHE_OPTIONS: IResolvedRuntimeCacheOptions = {
|
|
75
|
+
enabled: true,
|
|
76
|
+
maxMaterializedTiles: 4096,
|
|
77
|
+
// The byte budgets bound the memory; entries only bound the bookkeeping of many small contents.
|
|
78
|
+
maxContentEntries: 4096,
|
|
79
|
+
workingSetFactor: 0,
|
|
80
|
+
maxContentCpuBytes: 512 * 1024 * 1024,
|
|
81
|
+
maxContentGpuBytes: 1024 * 1024 * 1024,
|
|
82
|
+
maxSubtreeEntries: 128,
|
|
83
|
+
maxSubtreeBytes: 64 * 1024 * 1024,
|
|
84
|
+
unusedRetentionMilliseconds: 60_000,
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
function resolveBudget(value: number | undefined, fallback: number): number {
|
|
88
|
+
if (value === undefined) return fallback;
|
|
89
|
+
if (!Number.isFinite(value)) return value > 0 ? Number.POSITIVE_INFINITY : 0;
|
|
90
|
+
return Math.max(0, Math.floor(value));
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export function resolveRuntimeCacheOptions(options: IRuntimeCacheOptions | false | undefined): IResolvedRuntimeCacheOptions {
|
|
94
|
+
if (options === false) return { ...DEFAULT_RUNTIME_CACHE_OPTIONS, enabled: false };
|
|
95
|
+
return {
|
|
96
|
+
enabled: options?.enabled ?? DEFAULT_RUNTIME_CACHE_OPTIONS.enabled,
|
|
97
|
+
maxMaterializedTiles: resolveBudget(options?.maxMaterializedTiles, DEFAULT_RUNTIME_CACHE_OPTIONS.maxMaterializedTiles),
|
|
98
|
+
maxContentEntries: resolveBudget(options?.maxContentEntries, DEFAULT_RUNTIME_CACHE_OPTIONS.maxContentEntries),
|
|
99
|
+
workingSetFactor: resolveBudget(options?.workingSetFactor, DEFAULT_RUNTIME_CACHE_OPTIONS.workingSetFactor),
|
|
100
|
+
maxContentCpuBytes: resolveBudget(options?.maxContentCpuBytes, DEFAULT_RUNTIME_CACHE_OPTIONS.maxContentCpuBytes),
|
|
101
|
+
maxContentGpuBytes: resolveBudget(options?.maxContentGpuBytes, DEFAULT_RUNTIME_CACHE_OPTIONS.maxContentGpuBytes),
|
|
102
|
+
maxSubtreeEntries: resolveBudget(options?.maxSubtreeEntries, DEFAULT_RUNTIME_CACHE_OPTIONS.maxSubtreeEntries),
|
|
103
|
+
maxSubtreeBytes: resolveBudget(options?.maxSubtreeBytes, DEFAULT_RUNTIME_CACHE_OPTIONS.maxSubtreeBytes),
|
|
104
|
+
unusedRetentionMilliseconds: resolveBudget(
|
|
105
|
+
options?.unusedRetentionMilliseconds,
|
|
106
|
+
DEFAULT_RUNTIME_CACHE_OPTIONS.unusedRetentionMilliseconds,
|
|
107
|
+
),
|
|
108
|
+
...(options?.unusedFrameRetention === undefined
|
|
109
|
+
? {}
|
|
110
|
+
: { unusedFrameRetention: resolveBudget(options.unusedFrameRetention, Number.POSITIVE_INFINITY) }),
|
|
111
|
+
};
|
|
112
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import type { IContent, ITile, ITileset } from "@spacexr/3d-tiles-core";
|
|
2
|
+
|
|
3
|
+
import type { IImplicitCoordinates } from "../implicit/types";
|
|
4
|
+
import type { IMetadataDocumentContext, IRuntimeFeatureMetadata } from "../metadata/types";
|
|
5
|
+
import type { MetadataHandle, TileContentKind, TileReadinessState } from "../pipeline/states";
|
|
6
|
+
import type { Tile3D } from "../pipeline/tile-3d";
|
|
7
|
+
import type { IRuntimeResourceCost } from "./cache";
|
|
8
|
+
|
|
9
|
+
export type RuntimeContentStatus = TileReadinessState;
|
|
10
|
+
export type RuntimeContentKind = TileContentKind;
|
|
11
|
+
|
|
12
|
+
export interface IRuntimeTilesetDocument {
|
|
13
|
+
id: string;
|
|
14
|
+
uri: string;
|
|
15
|
+
baseUri: string;
|
|
16
|
+
tileset: ITileset;
|
|
17
|
+
metadata: IMetadataDocumentContext;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface IRuntimeContent<TContentHandle> {
|
|
21
|
+
id: string;
|
|
22
|
+
descriptor: IContent;
|
|
23
|
+
uri: string;
|
|
24
|
+
status: RuntimeContentStatus;
|
|
25
|
+
kind: RuntimeContentKind;
|
|
26
|
+
attempts: number;
|
|
27
|
+
handle?: TContentHandle;
|
|
28
|
+
featureMetadata?: readonly IRuntimeFeatureMetadata[];
|
|
29
|
+
cacheKey?: string;
|
|
30
|
+
cost?: IRuntimeResourceCost;
|
|
31
|
+
error?: unknown;
|
|
32
|
+
attached: boolean;
|
|
33
|
+
lastTouchedFrame: number;
|
|
34
|
+
readyFrame: number;
|
|
35
|
+
metadataHandle: MetadataHandle;
|
|
36
|
+
featureMetadataHandle: MetadataHandle;
|
|
37
|
+
/** Invalidated: the handle stays presented until its reload replaces it. */
|
|
38
|
+
stale: boolean;
|
|
39
|
+
readonly ready: boolean;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export type IRuntimeTile<TSpatial, TContentHandle, TGlyphHandle> = Tile3D<TSpatial, TContentHandle, TGlyphHandle>;
|
|
43
|
+
|
|
44
|
+
export interface ITileContext<TSpatial> {
|
|
45
|
+
runtimeId: string;
|
|
46
|
+
tileId: string;
|
|
47
|
+
depth: number;
|
|
48
|
+
tile: ITile;
|
|
49
|
+
document: IRuntimeTilesetDocument;
|
|
50
|
+
spatial: TSpatial;
|
|
51
|
+
implicitCoordinates?: IImplicitCoordinates;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Tiles whose loaded contents `TileRuntime.invalidate` marks as stale. */
|
|
55
|
+
export type RuntimeTilePredicate<TSpatial, TContentHandle, TGlyphHandle> = (
|
|
56
|
+
tile: IRuntimeTile<TSpatial, TContentHandle, TGlyphHandle>,
|
|
57
|
+
) => boolean;
|
|
58
|
+
|
|
59
|
+
/** A selected tile displayed next to a tile coarser than `maxLevelDifference` allows, despite the quadtree balance. */
|
|
60
|
+
export interface IRuntimeQuadtreeImbalance<TSpatial, TContentHandle, TGlyphHandle> {
|
|
61
|
+
/** The finer tile. */
|
|
62
|
+
tile: IRuntimeTile<TSpatial, TContentHandle, TGlyphHandle>;
|
|
63
|
+
/** Level of detail of its coarsest edge neighbour, in the quadtree pyramid. */
|
|
64
|
+
coarseLod: number;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export interface IRuntimeFrameResult<TSpatial, TContentHandle, TGlyphHandle> {
|
|
68
|
+
frame: number;
|
|
69
|
+
selected: readonly IRuntimeTile<TSpatial, TContentHandle, TGlyphHandle>[];
|
|
70
|
+
requested: readonly IRuntimeContent<TContentHandle>[];
|
|
71
|
+
pendingLoads: number;
|
|
72
|
+
/**
|
|
73
|
+
* Edges the quadtree balance could not keep within one level this frame, because neither the coarse neighbour's
|
|
74
|
+
* children nor an ancestor of the fine tile were ready. Empty without `quadtreeBalance`.
|
|
75
|
+
*/
|
|
76
|
+
unbalanced: readonly IRuntimeQuadtreeImbalance<TSpatial, TContentHandle, TGlyphHandle>[];
|
|
77
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { IRuntimeImplicitSubtree } from "../implicit/types";
|
|
2
|
+
import type { IMetadataSnapshot } from "../metadata/types";
|
|
3
|
+
import type { IRuntimeCacheEviction } from "./cache";
|
|
4
|
+
import type { IRuntimeContent, IRuntimeFrameResult, IRuntimeQuadtreeImbalance, IRuntimeTile, IRuntimeTilesetDocument } from "./contracts";
|
|
5
|
+
|
|
6
|
+
export type RuntimeEvent<TSpatial, TContentHandle, TGlyphHandle> =
|
|
7
|
+
| { type: "tileset-ready"; document: IRuntimeTilesetDocument }
|
|
8
|
+
| { type: "frame"; result: IRuntimeFrameResult<TSpatial, TContentHandle, TGlyphHandle> }
|
|
9
|
+
| { type: "tile-selected"; tile: IRuntimeTile<TSpatial, TContentHandle, TGlyphHandle> }
|
|
10
|
+
| { type: "tile-deselected"; tile: IRuntimeTile<TSpatial, TContentHandle, TGlyphHandle> }
|
|
11
|
+
| { type: "tile-activated"; tile: IRuntimeTile<TSpatial, TContentHandle, TGlyphHandle> }
|
|
12
|
+
| { type: "tile-deactivated"; tile: IRuntimeTile<TSpatial, TContentHandle, TGlyphHandle> }
|
|
13
|
+
/**
|
|
14
|
+
* The tile spatial state was re-derived: from a volume measured on its own content (`content`), or from the
|
|
15
|
+
* implicit resolver again once the parent content became ready (`parent-content`).
|
|
16
|
+
*/
|
|
17
|
+
| {
|
|
18
|
+
type: "tile-bounds-refined";
|
|
19
|
+
tile: IRuntimeTile<TSpatial, TContentHandle, TGlyphHandle>;
|
|
20
|
+
origin: "content" | "parent-content" | "vertical-scale";
|
|
21
|
+
}
|
|
22
|
+
| {
|
|
23
|
+
type: "content-state";
|
|
24
|
+
tile: IRuntimeTile<TSpatial, TContentHandle, TGlyphHandle>;
|
|
25
|
+
content: IRuntimeContent<TContentHandle>;
|
|
26
|
+
}
|
|
27
|
+
| { type: "subtree-state"; tile: IRuntimeTile<TSpatial, TContentHandle, TGlyphHandle>; subtree: IRuntimeImplicitSubtree }
|
|
28
|
+
| { type: "metadata-resolved"; tile: IRuntimeTile<TSpatial, TContentHandle, TGlyphHandle>; snapshot: IMetadataSnapshot }
|
|
29
|
+
| { type: "cache-eviction"; eviction: IRuntimeCacheEviction }
|
|
30
|
+
/** The selected cut still joins levels two or more apart on some edges; see `IRuntimeFrameResult.unbalanced`. */
|
|
31
|
+
| {
|
|
32
|
+
type: "quadtree-imbalance";
|
|
33
|
+
frame: number;
|
|
34
|
+
imbalances: readonly IRuntimeQuadtreeImbalance<TSpatial, TContentHandle, TGlyphHandle>[];
|
|
35
|
+
}
|
|
36
|
+
| { type: "error"; phase: string; error: unknown; tileId?: string; contentId?: string };
|
|
37
|
+
|
|
38
|
+
export type RuntimeEventListener<TSpatial, TContentHandle, TGlyphHandle> = (
|
|
39
|
+
event: RuntimeEvent<TSpatial, TContentHandle, TGlyphHandle>,
|
|
40
|
+
) => void;
|
|
41
|
+
|
|
42
|
+
export class RuntimeEventHub<TSpatial, TContentHandle, TGlyphHandle> {
|
|
43
|
+
private readonly listeners = new Set<RuntimeEventListener<TSpatial, TContentHandle, TGlyphHandle>>();
|
|
44
|
+
|
|
45
|
+
public subscribe(listener: RuntimeEventListener<TSpatial, TContentHandle, TGlyphHandle>): () => void {
|
|
46
|
+
this.listeners.add(listener);
|
|
47
|
+
return () => this.listeners.delete(listener);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
public emit(event: RuntimeEvent<TSpatial, TContentHandle, TGlyphHandle>): void {
|
|
51
|
+
for (const listener of this.listeners) listener(event);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
public clear(): void {
|
|
55
|
+
this.listeners.clear();
|
|
56
|
+
}
|
|
57
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import type { ILoadedImplicitSubtree, IRuntimeImplicitSubtree } from "../implicit/types";
|
|
2
|
+
import type { IMetadataSnapshot } from "../metadata/types";
|
|
3
|
+
import type { ContentLoadResult, ILoadedTileset } from "../ports";
|
|
4
|
+
import type { IRuntimeCacheEviction } from "./cache";
|
|
5
|
+
import type { IRuntimeContent, IRuntimeFrameResult, IRuntimeTile, IRuntimeTilesetDocument } from "./contracts";
|
|
6
|
+
|
|
7
|
+
export type HookResult = void | Promise<void>;
|
|
8
|
+
|
|
9
|
+
export interface ITilesetLoadHookContext {
|
|
10
|
+
runtimeId: string;
|
|
11
|
+
uri: string;
|
|
12
|
+
parentDocumentUri?: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface ITilesetLoadedHookContext extends ITilesetLoadHookContext {
|
|
16
|
+
loaded: ILoadedTileset;
|
|
17
|
+
document?: IRuntimeTilesetDocument;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface IFrameHookContext<TCamera, TSpatial, TContentHandle, TGlyphHandle> {
|
|
21
|
+
runtimeId: string;
|
|
22
|
+
frame: number;
|
|
23
|
+
camera: TCamera;
|
|
24
|
+
result?: IRuntimeFrameResult<TSpatial, TContentHandle, TGlyphHandle>;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface ITileHookContext<TSpatial, TContentHandle, TGlyphHandle> {
|
|
28
|
+
runtimeId: string;
|
|
29
|
+
tile: IRuntimeTile<TSpatial, TContentHandle, TGlyphHandle>;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface IContentHookContext<TSpatial, TContentHandle, TGlyphHandle> extends ITileHookContext<
|
|
33
|
+
TSpatial,
|
|
34
|
+
TContentHandle,
|
|
35
|
+
TGlyphHandle
|
|
36
|
+
> {
|
|
37
|
+
content: IRuntimeContent<TContentHandle>;
|
|
38
|
+
result?: ContentLoadResult<TContentHandle>;
|
|
39
|
+
metadata?: IMetadataSnapshot;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface ISubtreeHookContext<TSpatial, TContentHandle, TGlyphHandle> extends ITileHookContext<
|
|
43
|
+
TSpatial,
|
|
44
|
+
TContentHandle,
|
|
45
|
+
TGlyphHandle
|
|
46
|
+
> {
|
|
47
|
+
subtree: IRuntimeImplicitSubtree;
|
|
48
|
+
loaded?: ILoadedImplicitSubtree;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface IMetadataHookContext<TSpatial, TContentHandle, TGlyphHandle> extends ITileHookContext<
|
|
52
|
+
TSpatial,
|
|
53
|
+
TContentHandle,
|
|
54
|
+
TGlyphHandle
|
|
55
|
+
> {
|
|
56
|
+
snapshot: IMetadataSnapshot;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export interface IRuntimeErrorHookContext<TSpatial, TContentHandle, TGlyphHandle> {
|
|
60
|
+
runtimeId: string;
|
|
61
|
+
phase: string;
|
|
62
|
+
error: unknown;
|
|
63
|
+
tile?: IRuntimeTile<TSpatial, TContentHandle, TGlyphHandle>;
|
|
64
|
+
content?: IRuntimeContent<TContentHandle>;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export interface ICacheEvictionHookContext<TSpatial, TContentHandle, TGlyphHandle> {
|
|
68
|
+
runtimeId: string;
|
|
69
|
+
eviction: IRuntimeCacheEviction;
|
|
70
|
+
tile: IRuntimeTile<TSpatial, TContentHandle, TGlyphHandle>;
|
|
71
|
+
content?: IRuntimeContent<TContentHandle>;
|
|
72
|
+
subtree?: IRuntimeImplicitSubtree;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export interface IRuntimeHooks<TCamera, TSpatial, TContentHandle, TGlyphHandle> {
|
|
76
|
+
beforeTilesetLoad?(context: ITilesetLoadHookContext): HookResult;
|
|
77
|
+
afterTilesetLoad?(context: ITilesetLoadedHookContext): HookResult;
|
|
78
|
+
beforeFrame?(context: IFrameHookContext<TCamera, TSpatial, TContentHandle, TGlyphHandle>): HookResult;
|
|
79
|
+
afterFrame?(context: IFrameHookContext<TCamera, TSpatial, TContentHandle, TGlyphHandle>): HookResult;
|
|
80
|
+
beforeContentLoad?(context: IContentHookContext<TSpatial, TContentHandle, TGlyphHandle>): HookResult;
|
|
81
|
+
afterContentLoad?(context: IContentHookContext<TSpatial, TContentHandle, TGlyphHandle>): HookResult;
|
|
82
|
+
beforeSubtreeLoad?(context: ISubtreeHookContext<TSpatial, TContentHandle, TGlyphHandle>): HookResult;
|
|
83
|
+
afterSubtreeLoad?(context: ISubtreeHookContext<TSpatial, TContentHandle, TGlyphHandle>): HookResult;
|
|
84
|
+
beforeContentAttach?(context: IContentHookContext<TSpatial, TContentHandle, TGlyphHandle>): HookResult;
|
|
85
|
+
afterContentAttach?(context: IContentHookContext<TSpatial, TContentHandle, TGlyphHandle>): HookResult;
|
|
86
|
+
beforeContentDetach?(context: IContentHookContext<TSpatial, TContentHandle, TGlyphHandle>): HookResult;
|
|
87
|
+
afterContentDetach?(context: IContentHookContext<TSpatial, TContentHandle, TGlyphHandle>): HookResult;
|
|
88
|
+
beforeMetadataResolve?(context: ITileHookContext<TSpatial, TContentHandle, TGlyphHandle>): HookResult;
|
|
89
|
+
afterMetadataResolve?(context: IMetadataHookContext<TSpatial, TContentHandle, TGlyphHandle>): HookResult;
|
|
90
|
+
beforeGlyphPublish?(context: ITileHookContext<TSpatial, TContentHandle, TGlyphHandle>): HookResult;
|
|
91
|
+
afterGlyphPublish?(context: ITileHookContext<TSpatial, TContentHandle, TGlyphHandle>): HookResult;
|
|
92
|
+
beforeGlyphRevoke?(context: ITileHookContext<TSpatial, TContentHandle, TGlyphHandle>): HookResult;
|
|
93
|
+
afterGlyphRevoke?(context: ITileHookContext<TSpatial, TContentHandle, TGlyphHandle>): HookResult;
|
|
94
|
+
onTileSelected?(context: ITileHookContext<TSpatial, TContentHandle, TGlyphHandle>): HookResult;
|
|
95
|
+
onTileDeselected?(context: ITileHookContext<TSpatial, TContentHandle, TGlyphHandle>): HookResult;
|
|
96
|
+
beforeCacheEviction?(context: ICacheEvictionHookContext<TSpatial, TContentHandle, TGlyphHandle>): HookResult;
|
|
97
|
+
afterCacheEviction?(context: ICacheEvictionHookContext<TSpatial, TContentHandle, TGlyphHandle>): HookResult;
|
|
98
|
+
onError?(context: IRuntimeErrorHookContext<TSpatial, TContentHandle, TGlyphHandle>): HookResult;
|
|
99
|
+
}
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import type { IRuntimeMetric, RuntimeMetricName } from "../ports";
|
|
2
|
+
import type { IResolvedRuntimeCacheOptions, IRuntimeCacheCounters } from "./cache";
|
|
3
|
+
|
|
4
|
+
export interface IRuntimeInstrumentationOptions {
|
|
5
|
+
enabled?: boolean;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export interface IRuntimeMetricAggregate {
|
|
9
|
+
count: number;
|
|
10
|
+
sum: number;
|
|
11
|
+
min: number;
|
|
12
|
+
max: number;
|
|
13
|
+
latest: number;
|
|
14
|
+
unit: IRuntimeMetric["unit"];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface IRuntimeNodeStatistics {
|
|
18
|
+
total: number;
|
|
19
|
+
explicit: number;
|
|
20
|
+
implicit: number;
|
|
21
|
+
selected: number;
|
|
22
|
+
visible: number;
|
|
23
|
+
/** Selected tiles next to a tile coarser than `maxLevelDifference` allows in the last frame, with `quadtreeBalance`. */
|
|
24
|
+
unbalanced: number;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface IRuntimeContentStatistics {
|
|
28
|
+
total: number;
|
|
29
|
+
idle: number;
|
|
30
|
+
queued: number;
|
|
31
|
+
loading: number;
|
|
32
|
+
ready: number;
|
|
33
|
+
error: number;
|
|
34
|
+
cancelled: number;
|
|
35
|
+
attached: number;
|
|
36
|
+
cached: number;
|
|
37
|
+
cpuBytes: number;
|
|
38
|
+
gpuBytes: number;
|
|
39
|
+
networkBytes: number;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface IRuntimeSubtreeStatistics {
|
|
43
|
+
total: number;
|
|
44
|
+
idle: number;
|
|
45
|
+
queued: number;
|
|
46
|
+
loading: number;
|
|
47
|
+
ready: number;
|
|
48
|
+
error: number;
|
|
49
|
+
cancelled: number;
|
|
50
|
+
cached: number;
|
|
51
|
+
bytes: number;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export interface IRuntimeCacheStatistics extends IRuntimeCacheCounters {
|
|
55
|
+
enabled: boolean;
|
|
56
|
+
limits: IResolvedRuntimeCacheOptions;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export interface IRuntimeStateStatistics {
|
|
60
|
+
frame: number;
|
|
61
|
+
pendingLoads: number;
|
|
62
|
+
nodes: IRuntimeNodeStatistics;
|
|
63
|
+
contents: IRuntimeContentStatistics;
|
|
64
|
+
subtrees: IRuntimeSubtreeStatistics;
|
|
65
|
+
cache: IRuntimeCacheStatistics;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export interface IRuntimeStatisticsSnapshot extends IRuntimeStateStatistics {
|
|
69
|
+
runtimeId: string;
|
|
70
|
+
timestamp: number;
|
|
71
|
+
collectionStartedAt: number;
|
|
72
|
+
metrics: Readonly<Partial<Record<RuntimeMetricName, IRuntimeMetricAggregate>>>;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export type RuntimeStatisticsListener = (snapshot: IRuntimeStatisticsSnapshot) => void;
|
|
76
|
+
|
|
77
|
+
export class RuntimeInstrumentation {
|
|
78
|
+
private collectionEnabled: boolean;
|
|
79
|
+
private collectionStartedAt = Date.now();
|
|
80
|
+
private readonly aggregates = new Map<RuntimeMetricName, IRuntimeMetricAggregate>();
|
|
81
|
+
private readonly listeners = new Set<RuntimeStatisticsListener>();
|
|
82
|
+
|
|
83
|
+
public constructor(
|
|
84
|
+
options: boolean | IRuntimeInstrumentationOptions | undefined,
|
|
85
|
+
private readonly runtimeId: string,
|
|
86
|
+
private readonly stateProvider: () => IRuntimeStateStatistics,
|
|
87
|
+
) {
|
|
88
|
+
this.collectionEnabled = typeof options === "boolean" ? options : (options?.enabled ?? true);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
public get enabled(): boolean {
|
|
92
|
+
return this.collectionEnabled;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
public get hasSubscribers(): boolean {
|
|
96
|
+
return this.listeners.size > 0;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
public setEnabled(enabled: boolean): void {
|
|
100
|
+
this.collectionEnabled = enabled;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
public reset(): void {
|
|
104
|
+
this.aggregates.clear();
|
|
105
|
+
this.collectionStartedAt = Date.now();
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
public record(metric: IRuntimeMetric): void {
|
|
109
|
+
if (!this.collectionEnabled) return;
|
|
110
|
+
const aggregate = this.aggregates.get(metric.name);
|
|
111
|
+
if (!aggregate) {
|
|
112
|
+
this.aggregates.set(metric.name, {
|
|
113
|
+
count: 1,
|
|
114
|
+
sum: metric.value,
|
|
115
|
+
min: metric.value,
|
|
116
|
+
max: metric.value,
|
|
117
|
+
latest: metric.value,
|
|
118
|
+
unit: metric.unit,
|
|
119
|
+
});
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
aggregate.count++;
|
|
123
|
+
aggregate.sum += metric.value;
|
|
124
|
+
aggregate.min = Math.min(aggregate.min, metric.value);
|
|
125
|
+
aggregate.max = Math.max(aggregate.max, metric.value);
|
|
126
|
+
aggregate.latest = metric.value;
|
|
127
|
+
aggregate.unit = metric.unit;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
public snapshot(): IRuntimeStatisticsSnapshot {
|
|
131
|
+
const metrics: Partial<Record<RuntimeMetricName, IRuntimeMetricAggregate>> = {};
|
|
132
|
+
for (const [name, aggregate] of this.aggregates) metrics[name] = { ...aggregate };
|
|
133
|
+
return {
|
|
134
|
+
runtimeId: this.runtimeId,
|
|
135
|
+
timestamp: Date.now(),
|
|
136
|
+
collectionStartedAt: this.collectionStartedAt,
|
|
137
|
+
...this.stateProvider(),
|
|
138
|
+
metrics,
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
public subscribe(listener: RuntimeStatisticsListener): () => void {
|
|
143
|
+
this.listeners.add(listener);
|
|
144
|
+
return () => this.listeners.delete(listener);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
public publish(): void {
|
|
148
|
+
if (!this.collectionEnabled || this.listeners.size === 0) return;
|
|
149
|
+
const snapshot = this.snapshot();
|
|
150
|
+
for (const listener of this.listeners) {
|
|
151
|
+
try {
|
|
152
|
+
listener(snapshot);
|
|
153
|
+
} catch {
|
|
154
|
+
// Statistics observers must not interrupt traversal or cache enforcement.
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
public clear(): void {
|
|
160
|
+
this.listeners.clear();
|
|
161
|
+
this.aggregates.clear();
|
|
162
|
+
}
|
|
163
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
interface IScheduledTask {
|
|
2
|
+
key: string;
|
|
3
|
+
priority: number;
|
|
4
|
+
order: number;
|
|
5
|
+
run: (signal: AbortSignal) => Promise<void>;
|
|
6
|
+
controller: AbortController;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export class PriorityScheduler {
|
|
10
|
+
private readonly concurrency: number;
|
|
11
|
+
private readonly queued = new Map<string, IScheduledTask>();
|
|
12
|
+
private readonly running = new Map<string, IScheduledTask>();
|
|
13
|
+
private readonly idleWaiters = new Set<() => void>();
|
|
14
|
+
private order = 0;
|
|
15
|
+
private disposed = false;
|
|
16
|
+
|
|
17
|
+
public constructor(concurrency: number) {
|
|
18
|
+
this.concurrency = Math.max(1, Math.floor(concurrency));
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
public get size(): number {
|
|
22
|
+
return this.queued.size + this.running.size;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
public enqueue(key: string, priority: number, run: (signal: AbortSignal) => Promise<void>): boolean {
|
|
26
|
+
if (this.disposed || this.running.has(key)) return false;
|
|
27
|
+
const existing = this.queued.get(key);
|
|
28
|
+
if (existing) {
|
|
29
|
+
existing.priority = priority;
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
this.queued.set(key, { key, priority, order: this.order++, run, controller: new AbortController() });
|
|
33
|
+
this.pump();
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Changes the priority of a task still waiting, up or down. */
|
|
38
|
+
public reprioritize(key: string, priority: number): void {
|
|
39
|
+
const queued = this.queued.get(key);
|
|
40
|
+
if (queued) queued.priority = priority;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
public cancel(key: string): void {
|
|
44
|
+
const queued = this.queued.get(key);
|
|
45
|
+
if (queued) {
|
|
46
|
+
queued.controller.abort();
|
|
47
|
+
this.queued.delete(key);
|
|
48
|
+
this.notifyIdle();
|
|
49
|
+
}
|
|
50
|
+
this.running.get(key)?.controller.abort();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
public whenIdle(): Promise<void> {
|
|
54
|
+
if (this.size === 0) return Promise.resolve();
|
|
55
|
+
return new Promise((resolve) => this.idleWaiters.add(resolve));
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
public dispose(): void {
|
|
59
|
+
this.disposed = true;
|
|
60
|
+
for (const task of this.queued.values()) task.controller.abort();
|
|
61
|
+
for (const task of this.running.values()) task.controller.abort();
|
|
62
|
+
this.queued.clear();
|
|
63
|
+
this.notifyIdle();
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
private pump(): void {
|
|
67
|
+
while (!this.disposed && this.running.size < this.concurrency && this.queued.size > 0) {
|
|
68
|
+
const next = [...this.queued.values()].sort((left, right) => right.priority - left.priority || left.order - right.order)[0];
|
|
69
|
+
if (!next) return;
|
|
70
|
+
this.queued.delete(next.key);
|
|
71
|
+
this.running.set(next.key, next);
|
|
72
|
+
void next
|
|
73
|
+
.run(next.controller.signal)
|
|
74
|
+
.catch(() => undefined)
|
|
75
|
+
.finally(() => {
|
|
76
|
+
this.running.delete(next.key);
|
|
77
|
+
this.pump();
|
|
78
|
+
this.notifyIdle();
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
private notifyIdle(): void {
|
|
84
|
+
if (this.size !== 0) return;
|
|
85
|
+
for (const resolve of this.idleWaiters) resolve();
|
|
86
|
+
this.idleWaiters.clear();
|
|
87
|
+
}
|
|
88
|
+
}
|