@margoengine/quality 0.0.0-stage → 1.0.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/NOTICE +4 -0
- package/README.md +306 -2
- package/dist/demand.d.ts +45 -0
- package/dist/demand.js +74 -0
- package/dist/errors-XvcwUnU4.js +7 -0
- package/dist/events.d.ts +42 -0
- package/dist/events.js +88 -0
- package/dist/features.d.ts +164 -0
- package/dist/features.js +189 -0
- package/dist/index.d.ts +148 -0
- package/dist/index.js +3 -0
- package/dist/knobs.d.ts +80 -0
- package/dist/knobs.js +100 -0
- package/dist/pacing.d.ts +9 -0
- package/dist/power.d.ts +121 -0
- package/dist/power.js +74 -0
- package/dist/reading-DeIpfOlV.js +94 -0
- package/dist/reading.d.ts +20 -0
- package/dist/resolution-QY5Gc9gc.js +81 -0
- package/dist/resolution.d.ts +87 -0
- package/dist/src-CDJk96xr.js +166 -0
- package/dist/ssaa.d.ts +41 -0
- package/dist/ssaa.js +140 -0
- package/dist/warm.d.ts +51 -0
- package/dist/warm.js +126 -0
- package/package.json +73 -4
package/dist/knobs.d.ts
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import type { FeatureKnob } from './features.js';
|
|
2
|
+
import { type QualityOptions } from './index.js';
|
|
3
|
+
/** A named, weighted ladder. Values go from full quality to least quality. */
|
|
4
|
+
export interface KnobOptions {
|
|
5
|
+
/** Unique scaler name. Default depends on the factory. */
|
|
6
|
+
name?: string;
|
|
7
|
+
/** Perceptual importance, finite and above zero. Default 2. */
|
|
8
|
+
weight?: number;
|
|
9
|
+
/** Linear scale per level, strictly decreasing from 1 to above 0. Default [1, 0.5, 0.25]. */
|
|
10
|
+
scales?: readonly number[];
|
|
11
|
+
}
|
|
12
|
+
/** A resource knob restores the caller-owned original and frees only its own allocations. */
|
|
13
|
+
export interface TextureKnob extends FeatureKnob {
|
|
14
|
+
/** Currently bound texture; full resolution until a lower level is applied. */
|
|
15
|
+
readonly texture: GPUTexture;
|
|
16
|
+
/** Restore original binding, release reduced textures and reject subsequent apply calls. Idempotent. */
|
|
17
|
+
dispose(): void;
|
|
18
|
+
[Symbol.dispose](): void;
|
|
19
|
+
}
|
|
20
|
+
/** Device, caller-owned original and a synchronous binding update, for a live texture or colour target. */
|
|
21
|
+
export interface TextureKnobOptions extends KnobOptions {
|
|
22
|
+
/** Device owning every supplied resource. Rebuild the knob after device restoration. */
|
|
23
|
+
device: GPUDevice;
|
|
24
|
+
/** Original 2D, single-layer, single-sampled texture. Never destroyed by the knob. */
|
|
25
|
+
texture: GPUTexture;
|
|
26
|
+
/** Rebind the material or view to this texture. Must complete synchronously; no default. */
|
|
27
|
+
bind(texture: GPUTexture): void;
|
|
28
|
+
}
|
|
29
|
+
/** Raster source retained by the caller for reversible texture downsampling. */
|
|
30
|
+
export interface TextureResolutionOptions extends TextureKnobOptions {
|
|
31
|
+
/** Full-size raster matching texture dimensions, retained until disposal. No default. */
|
|
32
|
+
source: ImageBitmap | HTMLCanvasElement | OffscreenCanvas;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Resample a raster into a genuinely smaller bound texture on each live level change. Original storage is retained
|
|
36
|
+
* for exact restoration. Reduced textures have one mip; use an ordinary non-comparison filtering sampler.
|
|
37
|
+
* @throws RangeError for an invalid ladder, source size or texture; use matching RGBA8 2D resources with COPY_DST and RENDER_ATTACHMENT usage.
|
|
38
|
+
*/
|
|
39
|
+
export declare function textureResolutionKnob(options: TextureResolutionOptions): TextureKnob;
|
|
40
|
+
/**
|
|
41
|
+
* Resize a live colour render target; bind updates the view's target and any dependent resources together. Omitting
|
|
42
|
+
* view depth lets the engine manage matching depth. Contents are redrawn, never copied. Original is caller-owned.
|
|
43
|
+
* @throws RangeError for an invalid ladder/target; use a single-layer 2D colour texture with RENDER_ATTACHMENT usage.
|
|
44
|
+
*/
|
|
45
|
+
export declare function renderTargetKnob(options: TextureKnobOptions): TextureKnob;
|
|
46
|
+
/** Live particle birth-rate target; accepts VFX without importing its package. Capacity remains unchanged. */
|
|
47
|
+
export interface ParticleTarget {
|
|
48
|
+
/** Fixed allocation capacity; determines the default birth rate. */
|
|
49
|
+
readonly capacity: number;
|
|
50
|
+
/** VFX reads rate each simulation frame. */
|
|
51
|
+
readonly options: {
|
|
52
|
+
rate?: number;
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Lower particle births, including a final zero-emission level; existing particles finish their lives. Does not
|
|
57
|
+
* reduce capacity, suppress bursts or disable simulation. Level 0 restores the exact original rate option.
|
|
58
|
+
* @throws RangeError for an invalid rate, capacity or ladder; use finite nonnegative rate and positive integer capacity.
|
|
59
|
+
*/
|
|
60
|
+
export declare function particleKnob(target: ParticleTarget, options?: KnobOptions): FeatureKnob;
|
|
61
|
+
/** Deferred hardware MSAA choice. Deliberately separate from live FeatureKnob to avoid measuring imaginary savings. */
|
|
62
|
+
export interface MSAAKnob {
|
|
63
|
+
/** Requested sample count, default 4. Applying a level only updates this value. */
|
|
64
|
+
readonly samples: 1 | 4;
|
|
65
|
+
/** Two levels: 0 requests 4, 1 requests 1. */
|
|
66
|
+
readonly levels: 2;
|
|
67
|
+
/** Record a choice without changing any engine, global tier, attachment or pipeline. */
|
|
68
|
+
apply(level: number): void;
|
|
69
|
+
/**
|
|
70
|
+
* Apply the public quality tier before the next Engine.create, with no live engines. Returns effective samples:
|
|
71
|
+
* phones stay at 1; exactSize (without antialias) or antialias: false also force 1 at engine creation. This page-wide tier persists for device restore.
|
|
72
|
+
* Rebuild the scene and plugins after disposing/recreating an existing engine. No live hardware change.
|
|
73
|
+
*/
|
|
74
|
+
prepare(options?: Omit<QualityOptions, 'search'>): 1 | 4;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Record MSAA for the next engine creation through the existing public quality-tier API. No core mutation.
|
|
78
|
+
* @throws RangeError for any level other than 0 or 1; choose one of the two supported sample counts.
|
|
79
|
+
*/
|
|
80
|
+
export declare function msaaKnob(): MSAAKnob;
|
package/dist/knobs.js
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { a as e, h as t } from "./src-CDJk96xr.js";
|
|
2
|
+
import { r as n, t as r } from "./errors-XvcwUnU4.js";
|
|
3
|
+
//#region src/knobs.ts
|
|
4
|
+
function i(e, t) {
|
|
5
|
+
let { name: r = t, weight: i = 2, scales: a = [
|
|
6
|
+
1,
|
|
7
|
+
.5,
|
|
8
|
+
.25
|
|
9
|
+
] } = e, o = [...a];
|
|
10
|
+
if (!r || !Number.isFinite(i) || i <= 0 || o.length < 2 || o[0] !== 1 || o.some((e, t) => !Number.isFinite(e) || e <= 0 || t > 0 && e >= o[t - 1])) throw n("Invalid knob ladder; use a name, positive weight and decreasing scales starting at 1", "arguments");
|
|
11
|
+
return {
|
|
12
|
+
name: r,
|
|
13
|
+
weight: i,
|
|
14
|
+
values: o,
|
|
15
|
+
check: (e) => {
|
|
16
|
+
if (!Number.isInteger(e) || e < 0 || e >= o.length) throw n("Invalid knob level; use an integer within the ladder", "arguments");
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
function a(e, t, a) {
|
|
21
|
+
let { device: o, texture: s, bind: c } = e, { name: l, weight: u, values: d, check: f } = i(e, t);
|
|
22
|
+
if (s.dimension !== "2d" || s.depthOrArrayLayers !== 1 || s.sampleCount !== 1) throw n("Invalid knob texture; use a single-layer, single-sampled 2D texture", "arguments");
|
|
23
|
+
let p = s, m = !1, h = (e) => {
|
|
24
|
+
if (m) throw r("disposed", "Texture knob disposed; create a new knob", "disposed");
|
|
25
|
+
f(e);
|
|
26
|
+
let t = Math.max(1, Math.floor(s.width * d[e])), n = Math.max(1, Math.floor(s.height * d[e]));
|
|
27
|
+
if (p.width === t && p.height === n) return;
|
|
28
|
+
let i = e === 0 ? s : o.createTexture({
|
|
29
|
+
label: l,
|
|
30
|
+
size: [t, n],
|
|
31
|
+
format: s.format,
|
|
32
|
+
usage: s.usage
|
|
33
|
+
});
|
|
34
|
+
try {
|
|
35
|
+
i !== s && a?.(i), c(i);
|
|
36
|
+
} catch (e) {
|
|
37
|
+
throw i !== s && i.destroy(), e;
|
|
38
|
+
}
|
|
39
|
+
let u = p;
|
|
40
|
+
p = i, u !== s && u.destroy();
|
|
41
|
+
}, g = () => {
|
|
42
|
+
m ||= (h(0), !0);
|
|
43
|
+
};
|
|
44
|
+
return {
|
|
45
|
+
name: l,
|
|
46
|
+
weight: u,
|
|
47
|
+
levels: d.length,
|
|
48
|
+
apply: h,
|
|
49
|
+
get texture() {
|
|
50
|
+
return p;
|
|
51
|
+
},
|
|
52
|
+
dispose: g,
|
|
53
|
+
[Symbol.dispose]: g
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
function o(e) {
|
|
57
|
+
let { source: t, texture: r, device: i } = e;
|
|
58
|
+
if (t.width !== r.width || t.height !== r.height || !["rgba8unorm", "rgba8unorm-srgb"].includes(r.format) || (r.usage & 18) != 18) throw n("Invalid raster texture; use matching RGBA8 dimensions and COPY_DST and RENDER_ATTACHMENT usage", "arguments");
|
|
59
|
+
return a(e, "texture resolution", (e) => {
|
|
60
|
+
let n = new OffscreenCanvas(e.width, e.height), r = n.getContext("2d");
|
|
61
|
+
r.imageSmoothingEnabled = !0, r.imageSmoothingQuality = "high", r.drawImage(t, 0, 0, e.width, e.height), i.queue.copyExternalImageToTexture({ source: n }, { texture: e }, [e.width, e.height]);
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
function s(e) {
|
|
65
|
+
if (!(e.texture.usage & 16) || e.texture.format.startsWith("depth") || e.texture.format === "stencil8") throw n("Invalid colour target; use a colour texture with RENDER_ATTACHMENT usage", "arguments");
|
|
66
|
+
return a(e, "render target size");
|
|
67
|
+
}
|
|
68
|
+
function c(e, t = {}) {
|
|
69
|
+
let { name: r, weight: a, values: o, check: s } = i(t, "particles"), c = e.options.rate, l = c ?? e.capacity / 2;
|
|
70
|
+
if (!Number.isInteger(e.capacity) || e.capacity < 1 || !Number.isFinite(l) || l < 0) throw n("Invalid particle target; use positive integer capacity and finite nonnegative rate", "arguments");
|
|
71
|
+
return {
|
|
72
|
+
name: r,
|
|
73
|
+
weight: a,
|
|
74
|
+
levels: o.length + 1,
|
|
75
|
+
apply(t) {
|
|
76
|
+
t === o.length ? e.options.rate = 0 : (s(t), t === 0 && c === void 0 ? delete e.options.rate : e.options.rate = t === 0 ? c : l * o[t]);
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
function l() {
|
|
81
|
+
let r = 4;
|
|
82
|
+
return {
|
|
83
|
+
levels: 2,
|
|
84
|
+
get samples() {
|
|
85
|
+
return r;
|
|
86
|
+
},
|
|
87
|
+
apply(e) {
|
|
88
|
+
if (e !== 0 && e !== 1) throw n("Invalid MSAA level; choose 0 (4 samples) or 1 (1 sample)", "arguments");
|
|
89
|
+
r = e ? 1 : 4;
|
|
90
|
+
},
|
|
91
|
+
prepare(n = {}) {
|
|
92
|
+
return e({
|
|
93
|
+
...n,
|
|
94
|
+
search: `?quality=${r === 4 ? "high" : "low"}`
|
|
95
|
+
}), t();
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
//#endregion
|
|
100
|
+
export { l as msaaKnob, c as particleKnob, s as renderTargetKnob, o as textureResolutionKnob };
|
package/dist/pacing.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Gate an animation loop to `targetRate` without dropping near-native refresh ticks.
|
|
3
|
+
* @param clock Retained [next deadline, previous timestamp, mean callback interval]; start with `new Float64Array(3)`.
|
|
4
|
+
* @param now rAF timestamp in milliseconds.
|
|
5
|
+
* @param targetRate Requested frames per second.
|
|
6
|
+
*/
|
|
7
|
+
export declare function frameDue(clock: Float64Array, now: number, targetRate: number): boolean;
|
|
8
|
+
/** Report the measured browser callback cadence separately from the requested render cap. */
|
|
9
|
+
export declare function frameStatus(targetRate: number, callbackRate: number, fps: number): string;
|
package/dist/power.d.ts
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { type Paced } from './index.js';
|
|
2
|
+
import { type FrameTimers } from './resolution.js';
|
|
3
|
+
/** Options of {@link FrameDrift}. */
|
|
4
|
+
export interface FrameDriftOptions {
|
|
5
|
+
/** Frames per reading; the reading is their median. Default 300, about five seconds at 60 FPS. */
|
|
6
|
+
window?: number;
|
|
7
|
+
/** Cost over the coolest reading, as a ratio, that counts as hot. Above 1. Default 1.25. */
|
|
8
|
+
rise?: number;
|
|
9
|
+
/** Readings in a row over `rise` before it is called heat. Default 6, about half a minute. */
|
|
10
|
+
readings?: number;
|
|
11
|
+
/** A change between two readings above this share is a new scene, not heat, and starts over. Default 0.15. */
|
|
12
|
+
jump?: number;
|
|
13
|
+
/** Readings `hot` holds at least once set, however the cost moves: easing off lowers the cost, which says nothing
|
|
14
|
+
* about the temperature. Default 60, about five minutes. */
|
|
15
|
+
hold?: number;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Tells heat from a heavier scene by the shape of the frame cost. A GPU that throttles gets slower gradually, so the
|
|
19
|
+
* cost creeps up with no large step between readings and stays up; a new view, level or setting moves it in one step,
|
|
20
|
+
* which starts the measure over. Once hot it stays hot for `hold` readings, or until the cost creeps back down. The
|
|
21
|
+
* first reading is skipped (startup). Free of timers; feed it one frame cost per
|
|
22
|
+
* frame with {@link FrameDrift.push}.
|
|
23
|
+
*/
|
|
24
|
+
export declare class FrameDrift {
|
|
25
|
+
/** True while the frame cost has drifted up and stayed there. */
|
|
26
|
+
hot: boolean;
|
|
27
|
+
private readonly samples;
|
|
28
|
+
private readonly rise;
|
|
29
|
+
private readonly readings;
|
|
30
|
+
private readonly jump;
|
|
31
|
+
private count;
|
|
32
|
+
private seen;
|
|
33
|
+
private coolest;
|
|
34
|
+
private last;
|
|
35
|
+
private over;
|
|
36
|
+
private held;
|
|
37
|
+
private eased;
|
|
38
|
+
private readonly hold;
|
|
39
|
+
/** @throws RangeError when `window` is below 8, `rise` is not above 1, `readings` or `hold` is below 1 or `jump` is not above 0. */
|
|
40
|
+
constructor({ window, rise, readings, jump, hold }?: FrameDriftOptions);
|
|
41
|
+
/** Forget the measure, after a change you know of (a new level, a different quality). */
|
|
42
|
+
restart(): void;
|
|
43
|
+
/** Record one frame's cost in milliseconds; returns `hot` when this sample changed it. Other samples return undefined. */
|
|
44
|
+
push(ms: number): boolean | undefined;
|
|
45
|
+
private settle;
|
|
46
|
+
}
|
|
47
|
+
/** The part of the Battery Status API {@link watchPower} reads. */
|
|
48
|
+
export interface BatteryLike {
|
|
49
|
+
charging: boolean;
|
|
50
|
+
level: number;
|
|
51
|
+
addEventListener(type: string, listener: () => void): void;
|
|
52
|
+
removeEventListener(type: string, listener: () => void): void;
|
|
53
|
+
}
|
|
54
|
+
/** The part of the Compute Pressure API {@link watchPower} reads. */
|
|
55
|
+
export interface PressureLike {
|
|
56
|
+
observe(source: 'cpu'): Promise<void>;
|
|
57
|
+
disconnect(): void;
|
|
58
|
+
}
|
|
59
|
+
/** What {@link watchPower} knows now. */
|
|
60
|
+
export interface PowerReading {
|
|
61
|
+
/** 0 mains or unknown, 1 on battery, 2 strained: hot, under pressure or low on battery. */
|
|
62
|
+
level: 0 | 1 | 2;
|
|
63
|
+
/** False on battery. Undefined where the browser does not say. */
|
|
64
|
+
charging?: boolean;
|
|
65
|
+
/** Battery charge, 0 to 1. Undefined where the browser does not say. */
|
|
66
|
+
battery?: number;
|
|
67
|
+
/** Compute Pressure state: 'nominal', 'fair', 'serious' or 'critical'. Undefined where the browser does not say. */
|
|
68
|
+
pressure?: string;
|
|
69
|
+
/** Frame times have drifted up and stayed there. */
|
|
70
|
+
hot: boolean;
|
|
71
|
+
}
|
|
72
|
+
/** Options of {@link watchPower}; browser sources default to the browser's own, so tests can inject them. */
|
|
73
|
+
export interface PowerOptions extends FrameDriftOptions, FrameTimers {
|
|
74
|
+
/** Capped to `rates` while on battery or strained, and given its own pace back after. */
|
|
75
|
+
engine?: Paced;
|
|
76
|
+
/** Frames per second at levels 1 and 2. Default [60, 30]. */
|
|
77
|
+
rates?: readonly [number, number];
|
|
78
|
+
/** A {@link FeatureScaler}, or anything with a `budget`: it is multiplied by `budgets` at levels 1 and 2. */
|
|
79
|
+
features?: {
|
|
80
|
+
budget: number;
|
|
81
|
+
};
|
|
82
|
+
/** Share of the budget at levels 1 and 2. Default [0.75, 0.5]. */
|
|
83
|
+
budgets?: readonly [number, number];
|
|
84
|
+
/** Charge at or under which a discharging battery counts as strained. Default 0.2. */
|
|
85
|
+
lowBattery?: number;
|
|
86
|
+
/** Told every change of the reading. */
|
|
87
|
+
onChange?: (reading: PowerReading) => void;
|
|
88
|
+
/** Runs once on the full tier when the level leaves 0, after this tab's next load has been set to the light tier.
|
|
89
|
+
* Default none, so the page is never reloaded unasked; pass `() => location.reload()` to change tier at once. */
|
|
90
|
+
reload?: () => void;
|
|
91
|
+
/** Where this tab's lowered tier is kept. Default sessionStorage. */
|
|
92
|
+
session?: {
|
|
93
|
+
getItem(key: string): string | null;
|
|
94
|
+
setItem(key: string, value: string): void;
|
|
95
|
+
removeItem(key: string): void;
|
|
96
|
+
};
|
|
97
|
+
/** Default `navigator.getBattery`. Null for none. */
|
|
98
|
+
battery?: (() => Promise<BatteryLike>) | null;
|
|
99
|
+
/** Makes the pressure observer; the callback gets its records. Default the browser's `PressureObserver`. Null for none. */
|
|
100
|
+
pressure?: ((callback: (records: readonly {
|
|
101
|
+
state: string;
|
|
102
|
+
}[]) => void) => PressureLike) | null;
|
|
103
|
+
}
|
|
104
|
+
/** Stops {@link watchPower}. */
|
|
105
|
+
export interface PowerWatch {
|
|
106
|
+
/** The reading now. */
|
|
107
|
+
readonly reading: Readonly<PowerReading>;
|
|
108
|
+
/** Forget the frame time measure, after a change you know of. */
|
|
109
|
+
restart(): void;
|
|
110
|
+
/** Stop, and give the engine its pace and the features their budget back. Also runs on `using` disposal. */
|
|
111
|
+
dispose(): void;
|
|
112
|
+
[Symbol.dispose](): void;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Eases off on battery and when the machine runs hot. Reads the battery where the browser gives it, CPU pressure where
|
|
116
|
+
* the browser gives it, and frame times everywhere (see {@link FrameDrift}). Level 1 (on battery) and level 2 (strained)
|
|
117
|
+
* cap the frame rate and cut the feature budget; on the full tier, whose 4x multisampling cannot change while running,
|
|
118
|
+
* this tab's next load starts on the light tier until the machine is back on mains.
|
|
119
|
+
* @throws RangeError for invalid options.
|
|
120
|
+
*/
|
|
121
|
+
export declare function watchPower({ engine, rates, features, budgets, lowBattery, onChange, reload, session: store, battery, pressure, request, queue, now, ...drift }?: PowerOptions): PowerWatch;
|
package/dist/power.js
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { n as e, o as t, s as n } from "./src-CDJk96xr.js";
|
|
2
|
+
import { n as r } from "./resolution-QY5Gc9gc.js";
|
|
3
|
+
//#region src/power.ts
|
|
4
|
+
var i = class {
|
|
5
|
+
constructor({ window: e = 300, rise: t = 1.25, readings: n = 6, jump: r = .15, hold: i = 60 } = {}) {
|
|
6
|
+
if (this.hot = !1, this.count = 0, this.seen = 0, this.coolest = Infinity, this.last = 0, this.over = 0, this.held = 0, this.eased = !1, !Number.isInteger(e) || e < 8 || !(t > 1 && t < Infinity) || !Number.isInteger(n) || n < 1 || !Number.isInteger(i) || i < 1 || !(r > 0 && r < Infinity)) throw RangeError("Invalid drift options; use an integer window >= 8, rise above 1, integer readings and hold >= 1 and jump above 0");
|
|
7
|
+
this.samples = new Float64Array(e), this.rise = t, this.readings = n, this.jump = r, this.hold = i;
|
|
8
|
+
}
|
|
9
|
+
restart() {
|
|
10
|
+
this.count = this.seen = this.over = 0, this.eased = !1, this.coolest = Infinity, this.hot = !1;
|
|
11
|
+
}
|
|
12
|
+
push(e) {
|
|
13
|
+
if (!Number.isFinite(e) || e < 0 || (this.samples[this.count++] = e, this.count < this.samples.length)) return;
|
|
14
|
+
this.count = 0;
|
|
15
|
+
let t = this.hot, n = this.samples.sort()[this.samples.length >> 1], r = this.seen++;
|
|
16
|
+
if (r) {
|
|
17
|
+
if (this.hot && ++this.held >= this.hold) return this.hot = !1, this.settle(n), !1;
|
|
18
|
+
if (r > 1 && Math.abs(n - this.last) > this.last * this.jump) this.settle(n);
|
|
19
|
+
else return this.last = n, n < this.coolest && (this.coolest = n), n > this.coolest * this.rise ? ++this.over >= this.readings && !this.hot && (this.hot = !0, this.held = 0) : n < this.coolest * (1 + (this.rise - 1) / 2) && (this.over = 0, this.eased || (this.hot = !1)), this.hot === t ? void 0 : this.hot;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
settle(e) {
|
|
23
|
+
this.over = 0, this.coolest = this.last = e, this.eased = this.hot;
|
|
24
|
+
}
|
|
25
|
+
}, a = () => {
|
|
26
|
+
try {
|
|
27
|
+
return globalThis.sessionStorage;
|
|
28
|
+
} catch {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
function o({ engine: o, rates: s = [60, 30], features: c, budgets: l = [.75, .5], lowBattery: u = .2, onChange: d, reload: f, session: p = a(), battery: m = globalThis.navigator?.getBattery?.bind(globalThis.navigator), pressure: h = typeof PressureObserver > "u" ? void 0 : (e) => new PressureObserver(e), request: g, queue: _, now: v, ...y } = {}) {
|
|
33
|
+
if (!s.every((e) => e > 0) || !l.every((e) => e > 0 && e <= 1)) throw RangeError("Invalid power options; use rates above 0 and budgets in (0, 1]");
|
|
34
|
+
if (!(u >= 0 && u <= 1)) throw RangeError("Invalid power options; use lowBattery in 0..1");
|
|
35
|
+
let b = new i(y), x = c?.budget, S = o?.pace, C = {
|
|
36
|
+
level: 0,
|
|
37
|
+
hot: !1
|
|
38
|
+
}, w, T, E = !1, D = !1, O = () => {
|
|
39
|
+
let { charging: e, battery: t, pressure: n } = C, r = e === !1 && t !== void 0 && t <= u, i = C.hot || r || n === "serious" || n === "critical" ? 2 : +(e === !1);
|
|
40
|
+
i !== C.level && k(i);
|
|
41
|
+
}, k = (r) => {
|
|
42
|
+
C.level = r, o && (r ? t(o, s[r - 1]) : o.pace = S), c && (c.budget = r ? x * l[r - 1] : x), r ? n() === "high" && (p?.setItem(e, "1"), D || (D = !0, f?.())) : p?.removeItem(e);
|
|
43
|
+
}, A = () => {
|
|
44
|
+
E || (O(), d?.({ ...C }));
|
|
45
|
+
}, j = () => {
|
|
46
|
+
(C.charging !== w.charging || C.battery !== w.level) && (C.charging = w.charging, C.battery = w.level, A());
|
|
47
|
+
}, M = r({
|
|
48
|
+
request: g,
|
|
49
|
+
queue: _,
|
|
50
|
+
now: v
|
|
51
|
+
}, (e) => {
|
|
52
|
+
let t = b.push(e);
|
|
53
|
+
t === void 0 || E || (C.hot = t, A());
|
|
54
|
+
});
|
|
55
|
+
m?.().then((e) => {
|
|
56
|
+
E || (w = e, e.addEventListener("chargingchange", j), e.addEventListener("levelchange", j), j());
|
|
57
|
+
}, () => {}), h && (T = h((e) => {
|
|
58
|
+
let t = e.at(-1)?.state;
|
|
59
|
+
t !== C.pressure && (C.pressure = t, A());
|
|
60
|
+
}), T.observe("cpu").catch(() => {}));
|
|
61
|
+
let N = () => {
|
|
62
|
+
E || (E = !0, M(), w?.removeEventListener("chargingchange", j), w?.removeEventListener("levelchange", j), T?.disconnect(), C.level && (o && (o.pace = S), c && (c.budget = x)));
|
|
63
|
+
};
|
|
64
|
+
return {
|
|
65
|
+
reading: C,
|
|
66
|
+
restart: () => {
|
|
67
|
+
b.restart(), C.hot = !1, A();
|
|
68
|
+
},
|
|
69
|
+
dispose: N,
|
|
70
|
+
[Symbol.dispose]: N
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
//#endregion
|
|
74
|
+
export { i as FrameDrift, o as watchPower };
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
function e(e) {
|
|
2
|
+
let t = 0, n = -Infinity;
|
|
3
|
+
for (let [r, i] of e.sort((e, t) => e[0] - t[0])) i <= n || (t += i - Math.max(r, n), n = i);
|
|
4
|
+
return t / 1e6;
|
|
5
|
+
}
|
|
6
|
+
function t(t, n, r, i) {
|
|
7
|
+
let a = t.submit, o = n.createCommandEncoder, s = [], c = [], l = /* @__PURE__ */ new WeakMap(), u = [], d = () => ({
|
|
8
|
+
spans: [],
|
|
9
|
+
open: 0,
|
|
10
|
+
counted: !1,
|
|
11
|
+
sealed: !1,
|
|
12
|
+
bad: !1
|
|
13
|
+
}), f = d(), p = -1, m = 0, h = !1, g = () => {
|
|
14
|
+
h = !0, t.submit === b && (t.submit = a), n.createCommandEncoder === y && (n.createCommandEncoder = o), u.sort((e, t) => e - t);
|
|
15
|
+
let e = u[Math.floor(u.length * .9)];
|
|
16
|
+
i(u.length === 120 && u[119] > 0 ? e : void 0), s.length && r(() => {
|
|
17
|
+
for (let e of s) e.set.destroy(), e.resolve.destroy(), e.read.destroy();
|
|
18
|
+
});
|
|
19
|
+
}, _ = (t) => {
|
|
20
|
+
h || !t.sealed || t.open || t.bad || !t.counted || u.push(e(t.spans)) === 120 && g();
|
|
21
|
+
}, v = (e) => {
|
|
22
|
+
let t = c.pop();
|
|
23
|
+
return !t && s.length < 8 && (t = {
|
|
24
|
+
set: e.createQuerySet({
|
|
25
|
+
type: "timestamp",
|
|
26
|
+
count: 256
|
|
27
|
+
}),
|
|
28
|
+
resolve: e.createBuffer({
|
|
29
|
+
size: 2048,
|
|
30
|
+
usage: 516
|
|
31
|
+
}),
|
|
32
|
+
read: e.createBuffer({
|
|
33
|
+
size: 2048,
|
|
34
|
+
usage: 9
|
|
35
|
+
}),
|
|
36
|
+
used: 0
|
|
37
|
+
}, s.push(t)), t;
|
|
38
|
+
};
|
|
39
|
+
function y(e) {
|
|
40
|
+
let t = o.call(this, e);
|
|
41
|
+
if (m <= 2e3) return t;
|
|
42
|
+
if (!this.features.has("timestamp-query")) return u.length = 0, g(), t;
|
|
43
|
+
let n;
|
|
44
|
+
for (let e of ["beginRenderPass", "beginComputePass"]) {
|
|
45
|
+
let r = t[e];
|
|
46
|
+
t[e] = (e) => {
|
|
47
|
+
if (h) return r.call(t, e);
|
|
48
|
+
if (n ??= v(this), e?.timestampWrites || !n || n.used + 2 > 256) return f.bad = !0, r.call(t, e);
|
|
49
|
+
let i = n.used;
|
|
50
|
+
return n.used += 2, r.call(t, {
|
|
51
|
+
...e,
|
|
52
|
+
timestampWrites: {
|
|
53
|
+
querySet: n.set,
|
|
54
|
+
beginningOfPassWriteIndex: i,
|
|
55
|
+
endOfPassWriteIndex: i + 1
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
let r = t.finish;
|
|
61
|
+
return t.finish = (e) => {
|
|
62
|
+
n?.used && (t.resolveQuerySet(n.set, 0, n.used, n.resolve, 0), t.copyBufferToBuffer(n.resolve, 0, n.read, 0, n.used * 8));
|
|
63
|
+
let i = r.call(t, e);
|
|
64
|
+
return n?.used ? l.set(i, n) : n && c.push(n), i;
|
|
65
|
+
}, t;
|
|
66
|
+
}
|
|
67
|
+
function b(e) {
|
|
68
|
+
let t = a.call(this, e);
|
|
69
|
+
if (h) return t;
|
|
70
|
+
for (let t of e) {
|
|
71
|
+
let e = l.get(t);
|
|
72
|
+
if (!e) continue;
|
|
73
|
+
let n = f, r = e.used * 8;
|
|
74
|
+
n.open++, e.read.mapAsync(1, 0, r).then(() => {
|
|
75
|
+
let t = new BigInt64Array(e.read.getMappedRange(0, r));
|
|
76
|
+
for (let r = 0; r < e.used; r += 2) t[r + 1] > t[r] && n.spans.push([Number(t[r]), Number(t[r + 1])]);
|
|
77
|
+
e.read.unmap(), e.used = 0, c.push(e), n.counted = !0, n.open--, _(n);
|
|
78
|
+
}, () => {
|
|
79
|
+
n.bad = !0;
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
return t;
|
|
83
|
+
}
|
|
84
|
+
t.submit = b, n.createCommandEncoder = y;
|
|
85
|
+
let x = (e) => {
|
|
86
|
+
if (h) return;
|
|
87
|
+
if (p < 0 && (p = e), (m = e - p) > 1e4) return g();
|
|
88
|
+
let t = f;
|
|
89
|
+
f = d(), t.sealed = !0, _(t), h || r(x);
|
|
90
|
+
};
|
|
91
|
+
r(x);
|
|
92
|
+
}
|
|
93
|
+
//#endregion
|
|
94
|
+
export { t as readGpuTime };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/** The part of `GPUQueue` the reading uses. */
|
|
2
|
+
export interface QueueLike {
|
|
3
|
+
submit(buffers: Iterable<object>): unknown;
|
|
4
|
+
}
|
|
5
|
+
/** The part of `GPUDevice` the reading uses. */
|
|
6
|
+
export interface DeviceLike {
|
|
7
|
+
features: {
|
|
8
|
+
has(name: string): boolean;
|
|
9
|
+
};
|
|
10
|
+
createCommandEncoder(descriptor?: unknown): any;
|
|
11
|
+
createQuerySet(descriptor: unknown): {
|
|
12
|
+
destroy(): void;
|
|
13
|
+
};
|
|
14
|
+
createBuffer(descriptor: unknown): any;
|
|
15
|
+
}
|
|
16
|
+
/** Skip two seconds of startup (shader compiles, uploads), then read 120 frames, giving up ten seconds in. Reports the
|
|
17
|
+
* 90th percentile in milliseconds, or undefined without timestamps, with too few frames or when every frame reads zero (a
|
|
18
|
+
* browser returning empty timestamps is no evidence of headroom). Passes the page timed itself spoil their frame. Copies
|
|
19
|
+
* between passes, presenting and other work on the machine are not counted. */
|
|
20
|
+
export declare function readGpuTime(queue: QueueLike, device: Pick<DeviceLike, 'createCommandEncoder'>, request: (callback: (time: number) => void) => unknown, report: (milliseconds: number | undefined) => void): void;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
//#region src/resolution.ts
|
|
2
|
+
var e = class {
|
|
3
|
+
constructor({ budget: e = 12, min: t = .5, max: n = 1, window: r = 30, granularity: i = .05 } = {}) {
|
|
4
|
+
if (this.sum = 0, this.count = 0, this.skip = 0, this.calm = 0, this.holdOff = 2, this.sinceRise = Infinity, !(Number.isFinite(e) && e > 0) || !(t > 0 && t <= n && n <= 1) || !Number.isInteger(r) || r < 1 || !(i > 0 && i <= .5)) throw RangeError("Invalid resolution options; use budget > 0, 0 < min <= max <= 1, an integer window >= 1 and granularity in (0, 0.5]");
|
|
5
|
+
this.budget = e, this.min = t, this.max = n, this.window = r, this.granularity = i, this.scale = n;
|
|
6
|
+
}
|
|
7
|
+
push(e) {
|
|
8
|
+
if (!Number.isFinite(e) || e < 0) return;
|
|
9
|
+
if (this.skip > 0) {
|
|
10
|
+
this.skip--;
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
if (this.sum += e, ++this.count < this.window) return;
|
|
14
|
+
let t = this.sum / this.count;
|
|
15
|
+
this.sum = this.count = 0;
|
|
16
|
+
let n = (e) => Math.min(this.max, Math.max(this.min, Math.round(e / this.granularity) * this.granularity));
|
|
17
|
+
if (this.sinceRise++, t > this.budget) {
|
|
18
|
+
this.calm = 0;
|
|
19
|
+
let e = n(this.scale * Math.max(.7, Math.sqrt(this.budget * .95 / t)));
|
|
20
|
+
return e >= this.scale && (e = Math.max(this.min, this.scale - this.granularity)), this.sinceRise <= 3 && (this.holdOff = Math.min(32, this.holdOff * 2)), this.change(e);
|
|
21
|
+
}
|
|
22
|
+
if (t < this.budget * .7 && this.scale < this.max) {
|
|
23
|
+
if (++this.calm >= this.holdOff) return this.calm = 0, this.sinceRise = 0, this.change(n(this.scale * 1.05 + this.granularity / 2));
|
|
24
|
+
} else t < this.budget && (this.calm = Math.max(0, this.calm - 1));
|
|
25
|
+
}
|
|
26
|
+
change(e) {
|
|
27
|
+
if (!(Math.abs(e - this.scale) < 1e-9)) return this.scale = e, this.skip = 8, e;
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
function t({ request: e = globalThis.requestAnimationFrame?.bind(globalThis), queue: t = typeof GPUQueue > "u" ? void 0 : GPUQueue.prototype, now: n = () => performance.now() }, r) {
|
|
31
|
+
if (t?.submit) {
|
|
32
|
+
let e = t.submit, i = function(t) {
|
|
33
|
+
let i = n(), a = e.call(this, t);
|
|
34
|
+
return this.onSubmittedWorkDone().then(() => r(n() - i)), a;
|
|
35
|
+
};
|
|
36
|
+
return t.submit = i, () => {
|
|
37
|
+
t.submit === i && (t.submit = e);
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
let i = !1;
|
|
41
|
+
if (e) {
|
|
42
|
+
let t, n = (a) => {
|
|
43
|
+
i || (t !== void 0 && a - t < 250 && r(a - t), t = a, e(n));
|
|
44
|
+
};
|
|
45
|
+
e(n);
|
|
46
|
+
}
|
|
47
|
+
return () => {
|
|
48
|
+
i = !0;
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
var n = {
|
|
52
|
+
scale: 1,
|
|
53
|
+
dispose() {},
|
|
54
|
+
[Symbol.dispose]() {}
|
|
55
|
+
};
|
|
56
|
+
function r({ request: r, queue: i, now: a, apply: o, ...s } = {}) {
|
|
57
|
+
let c = new e(s), l = globalThis.margoLow;
|
|
58
|
+
if (!o && !l) return n;
|
|
59
|
+
let u = o ?? ((e) => {
|
|
60
|
+
globalThis.margoLow = Math.max(.25, Math.min(globalThis.devicePixelRatio ?? 1, l) * e);
|
|
61
|
+
}), d = !1, f = t({
|
|
62
|
+
request: r,
|
|
63
|
+
queue: i,
|
|
64
|
+
now: a
|
|
65
|
+
}, (e) => {
|
|
66
|
+
if (d) return;
|
|
67
|
+
let t = c.push(e);
|
|
68
|
+
t !== void 0 && u(t);
|
|
69
|
+
}), p = () => {
|
|
70
|
+
d || (d = !0, f(), o ? o(1) : globalThis.margoLow = l);
|
|
71
|
+
};
|
|
72
|
+
return {
|
|
73
|
+
get scale() {
|
|
74
|
+
return c.scale;
|
|
75
|
+
},
|
|
76
|
+
dispose: p,
|
|
77
|
+
[Symbol.dispose]: p
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
//#endregion
|
|
81
|
+
export { t as n, r, e as t };
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/** Options for {@link ResolutionScaler}. */
|
|
2
|
+
export interface ResolutionOptions {
|
|
3
|
+
/** Frame cost in milliseconds above which the resolution drops; below 70% of it the resolution creeps back up. Default 12 (leaves room in a 16.7 ms frame). */
|
|
4
|
+
budget?: number;
|
|
5
|
+
/** Lowest render scale (fraction of the full pixel ratio), in (0, 1]. Default 0.5. */
|
|
6
|
+
min?: number;
|
|
7
|
+
/** Highest render scale, in [min, 1]. Default 1. */
|
|
8
|
+
max?: number;
|
|
9
|
+
/** Samples averaged before each decision. Default 30. */
|
|
10
|
+
window?: number;
|
|
11
|
+
/** Scale changes are rounded to a multiple of this, so the canvas is not reallocated for tiny moves. Default 0.05. */
|
|
12
|
+
granularity?: number;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* The decision logic of dynamic resolution, free of timers and the DOM so it is easy to test and to reuse in your own
|
|
16
|
+
* loop. Feed it one frame cost per frame with {@link ResolutionScaler.push}; it returns the new render scale when it
|
|
17
|
+
* changes. Cost grows with pixel count (scale squared), so a drop is sized to land just under the budget. It rises
|
|
18
|
+
* slowly (5% per decision) and backs off after a rise that had to be undone, so it settles instead of oscillating.
|
|
19
|
+
*/
|
|
20
|
+
export declare class ResolutionScaler {
|
|
21
|
+
/** Current render scale, between `min` and `max`. Starts at `max`. */
|
|
22
|
+
scale: number;
|
|
23
|
+
private readonly budget;
|
|
24
|
+
private readonly min;
|
|
25
|
+
private readonly max;
|
|
26
|
+
private readonly window;
|
|
27
|
+
private readonly granularity;
|
|
28
|
+
private sum;
|
|
29
|
+
private count;
|
|
30
|
+
private skip;
|
|
31
|
+
private calm;
|
|
32
|
+
private holdOff;
|
|
33
|
+
private sinceRise;
|
|
34
|
+
/**
|
|
35
|
+
* @throws RangeError when `budget` is not above 0, `min` and `max` are not 0 < min <= max <= 1, or `window` is below 1.
|
|
36
|
+
*/
|
|
37
|
+
constructor({ budget, min, max, window, granularity }?: ResolutionOptions);
|
|
38
|
+
/**
|
|
39
|
+
* Record one frame's cost in milliseconds (GPU time or frame interval). Returns the new scale when this sample made
|
|
40
|
+
* the scaler change it, otherwise `undefined`. Non-finite or negative samples are ignored.
|
|
41
|
+
*/
|
|
42
|
+
push(ms: number): number | undefined;
|
|
43
|
+
private change;
|
|
44
|
+
}
|
|
45
|
+
/** Where a watcher gets frame costs from; each defaults to the browser's own, so tests can inject them. */
|
|
46
|
+
export interface FrameTimers {
|
|
47
|
+
/** Frame scheduler, used only when no queue is available. Default `requestAnimationFrame`. */
|
|
48
|
+
request?: ((callback: (time: number) => void) => unknown) | null;
|
|
49
|
+
/** Queue whose `submit` is wrapped to time GPU work (submit to finished). Default `GPUQueue.prototype` when WebGPU exists. */
|
|
50
|
+
queue?: {
|
|
51
|
+
submit(buffers: unknown): unknown;
|
|
52
|
+
onSubmittedWorkDone(): Promise<unknown>;
|
|
53
|
+
} | null;
|
|
54
|
+
/** Clock in milliseconds. Default `performance.now`. */
|
|
55
|
+
now?: () => number;
|
|
56
|
+
}
|
|
57
|
+
/** Calls `record` with each frame's cost until the returned function runs: GPU work from submit to finished, or without
|
|
58
|
+
* a queue the gap between animation frames. */
|
|
59
|
+
export declare function timeFrames({ request, queue, now, }: FrameTimers, record: (ms: number) => void): () => void;
|
|
60
|
+
/** Hooks of {@link watchResolution}; each defaults to the browser's own, so tests can inject them. */
|
|
61
|
+
export interface WatchResolutionOptions extends ResolutionOptions, FrameTimers {
|
|
62
|
+
/** Receives each new scale. Default sets `globalThis.margoLow` (core's pixel-ratio cap) so the next frame resizes the canvas. */
|
|
63
|
+
apply?: (scale: number) => void;
|
|
64
|
+
}
|
|
65
|
+
/** Stops {@link watchResolution} and restores the starting pixel ratio. */
|
|
66
|
+
export interface ResolutionWatch {
|
|
67
|
+
/** Current render scale. */
|
|
68
|
+
readonly scale: number;
|
|
69
|
+
/** Stop measuring and restore full resolution. Also runs on `using` disposal. */
|
|
70
|
+
dispose(): void;
|
|
71
|
+
[Symbol.dispose](): void;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Dynamic resolution: while the scene runs, time each frame's GPU work and lower the canvas pixel ratio when it goes
|
|
75
|
+
* over `budget`, raise it again when there is room. It works by moving `globalThis.margoLow`, core's pixel-ratio cap,
|
|
76
|
+
* which core reads every frame, so it needs the light or phone tier (`applyQualityTier()` first). On the full 4x MSAA
|
|
77
|
+
* tier it does nothing and returns a handle with `scale` 1, because changing the cap there would also switch off
|
|
78
|
+
* multisampling; pass your own `apply` to scale some other way.
|
|
79
|
+
*
|
|
80
|
+
* ```ts
|
|
81
|
+
* applyQualityTier();
|
|
82
|
+
* const engine = await Engine.create(canvas, {backend: 'webgpu'});
|
|
83
|
+
* using resolution = watchResolution({budget: 12, min: 0.6});
|
|
84
|
+
* ```
|
|
85
|
+
* @throws RangeError for invalid options (see {@link ResolutionOptions}).
|
|
86
|
+
*/
|
|
87
|
+
export declare function watchResolution({ request, queue, now, apply, ...options }?: WatchResolutionOptions): ResolutionWatch;
|