@margoengine/headless 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/NOTICE +4 -0
- package/README.md +768 -3
- package/dist/accumulate.d.ts +123 -0
- package/dist/accumulate.js +198 -0
- package/dist/avc-range.d.ts +15 -0
- package/dist/compare.d.ts +97 -0
- package/dist/compare.js +320 -0
- package/dist/crc32-Cc3b8qnn.js +23 -0
- package/dist/errors-CDgOgzCd.js +7 -0
- package/dist/ffmpeg-colour-Cg00HQ4H.js +15 -0
- package/dist/ffmpeg-colour.d.ts +4 -0
- package/dist/ffmpeg-files.d.ts +129 -0
- package/dist/ffmpeg-files.js +406 -0
- package/dist/ffmpeg.d.ts +159 -0
- package/dist/ffmpeg.js +372 -0
- package/dist/formats.d.ts +46 -0
- package/dist/formats.js +685 -0
- package/dist/frames.d.ts +33 -0
- package/dist/frames.js +70 -0
- package/dist/gif.d.ts +20 -0
- package/dist/gif.js +7 -0
- package/dist/index.d.ts +66 -0
- package/dist/index.js +1 -0
- package/dist/look.d.ts +553 -0
- package/dist/look.js +1277 -0
- package/dist/null.d.ts +39 -0
- package/dist/null.js +61 -0
- package/dist/readback-HJLttuJt.js +122 -0
- package/dist/readback.d.ts +55 -0
- package/dist/src-VufTH2fo.js +154 -0
- package/dist/stage.d.ts +52 -0
- package/dist/stage.js +106 -0
- package/dist/tiles.d.ts +38 -0
- package/dist/tiles.js +79 -0
- package/dist/video-mux.d.ts +77 -0
- package/dist/video.d.ts +176 -0
- package/dist/video.js +1114 -0
- package/package.json +122 -6
package/dist/null.d.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { type CameraOptions } from '@margoengine/core';
|
|
2
|
+
import { HeadlessStage } from './stage.js';
|
|
3
|
+
/** Options for a GPU-free scene simulation. */
|
|
4
|
+
export interface NullStageOptions {
|
|
5
|
+
/** Logical viewport width. Default 1. */
|
|
6
|
+
width?: number;
|
|
7
|
+
/** Logical viewport height. Default 1. */
|
|
8
|
+
height?: number;
|
|
9
|
+
/** Initial transform capacity. Default 1024; grows automatically. */
|
|
10
|
+
capacity?: number;
|
|
11
|
+
/** Initial camera configuration. Default core camera defaults. */
|
|
12
|
+
camera?: CameraOptions;
|
|
13
|
+
/** Cancels creation. Default none. */
|
|
14
|
+
signal?: AbortSignal;
|
|
15
|
+
/** Engine error callback. Default core error reporting. */
|
|
16
|
+
onError?: (error: unknown) => void;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Real scenes and transforms with no graphics context, binding or render passes.
|
|
20
|
+
* step runs ticks, frame callbacks and every live scene's plugin updates once.
|
|
21
|
+
* CPU animation samplers and property animation work unchanged; GPU deformation
|
|
22
|
+
* and readback are unsupported. Nothing runs until step is called.
|
|
23
|
+
*/
|
|
24
|
+
export declare class NullStage extends HeadlessStage {
|
|
25
|
+
#private;
|
|
26
|
+
/** Identifies this stage independently of the engine fallback dispatch label. */
|
|
27
|
+
readonly isNull = true;
|
|
28
|
+
/** Accumulated successful steps in seconds. */
|
|
29
|
+
get time(): number;
|
|
30
|
+
/** Advance all live scenes by delta seconds, 0..1. Default 0. Invalid and reentrant steps reject before callbacks. */
|
|
31
|
+
step(delta?: number): void;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Creates a GPU-free stage using core's existing renderer injection. It never
|
|
35
|
+
* requests an adapter or graphics context, modifies no globals and needs no DOM.
|
|
36
|
+
* engine.backend is the existing 'webgl2' fallback dispatch label; gpu and
|
|
37
|
+
* renderTarget are null. Render passes are registered but never executed.
|
|
38
|
+
*/
|
|
39
|
+
export declare function createNullStage({ width, height, onError, ...options }?: NullStageOptions): Promise<NullStage>;
|
package/dist/null.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// @margo/core-built 0.1.0
|
|
2
|
+
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
+
const i=(engine,version="0.1.0",source=import.meta.url)=>__margoRuntime(engine,version,source);
|
|
4
|
+
import { t as e } from "./src-VufTH2fo.js";
|
|
5
|
+
import { r as t, t as n } from "./errors-CDgOgzCd.js";
|
|
6
|
+
import { HeadlessStage as r } from "./stage.js";
|
|
7
|
+
import { engineRuntime as __margoRuntime } from "@margoengine/core/extension";
|
|
8
|
+
import { Engine as a } from "@margoengine/core";
|
|
9
|
+
//#region src/null.ts
|
|
10
|
+
var o = class extends r {
|
|
11
|
+
constructor(...e) {
|
|
12
|
+
super(...e), this.isNull = !0, this.#e = 0;
|
|
13
|
+
}
|
|
14
|
+
#e;
|
|
15
|
+
get time() {
|
|
16
|
+
return this.#e;
|
|
17
|
+
}
|
|
18
|
+
step(e = 0) {
|
|
19
|
+
if (i(this.engine).disposed) throw n("disposed", "Null stage disposed; create another stage", "headless");
|
|
20
|
+
super.step(e), this.#e += e;
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
async function s({ width: n = 1, height: r = 1, onError: s, ...c } = {}) {
|
|
24
|
+
if (c.signal?.throwIfAborted(), s !== void 0 && typeof s != "function") throw t("Null stage onError must be a function or undefined; pass a callback or omit it", "arguments");
|
|
25
|
+
let l = {
|
|
26
|
+
backend: "webgl2",
|
|
27
|
+
add() {},
|
|
28
|
+
remove() {},
|
|
29
|
+
render() {},
|
|
30
|
+
dispose() {}
|
|
31
|
+
}, u = await a.create(e({
|
|
32
|
+
width: n,
|
|
33
|
+
height: r
|
|
34
|
+
}), {
|
|
35
|
+
...c,
|
|
36
|
+
backend: "webgl2",
|
|
37
|
+
exactSize: !0,
|
|
38
|
+
fallback: () => l
|
|
39
|
+
});
|
|
40
|
+
s && (u.onError = s);
|
|
41
|
+
let d = i(u), f = u.render.bind(u);
|
|
42
|
+
return u.render = (e = {}) => {
|
|
43
|
+
let t = [...d.scenes].map((t) => ({
|
|
44
|
+
scene: t,
|
|
45
|
+
camera: e.camera ?? d.camera,
|
|
46
|
+
viewport: [
|
|
47
|
+
0,
|
|
48
|
+
0,
|
|
49
|
+
1,
|
|
50
|
+
1
|
|
51
|
+
]
|
|
52
|
+
}));
|
|
53
|
+
f({
|
|
54
|
+
...e,
|
|
55
|
+
culling: !1,
|
|
56
|
+
views: e.views ?? t
|
|
57
|
+
});
|
|
58
|
+
}, new o(u);
|
|
59
|
+
}
|
|
60
|
+
//#endregion
|
|
61
|
+
export { o as NullStage, s as createNullStage };
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
// @margo/core-built 0.1.0
|
|
2
|
+
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
+
const t=(engine,version="0.1.0",source=import.meta.url)=>__margoRuntime(engine,version,source);
|
|
4
|
+
import { t as e } from "./errors-CDgOgzCd.js";
|
|
5
|
+
import { engineRuntime as __margoRuntime } from "@margoengine/core/extension";
|
|
6
|
+
//#region src/readback.ts
|
|
7
|
+
var n = 16, r = 256;
|
|
8
|
+
function i(e, t) {
|
|
9
|
+
return e.createBuffer({
|
|
10
|
+
label: "headless readback",
|
|
11
|
+
size: t,
|
|
12
|
+
usage: 9
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
var a = class {
|
|
16
|
+
#e = null;
|
|
17
|
+
#t = null;
|
|
18
|
+
#n = null;
|
|
19
|
+
#r = 0;
|
|
20
|
+
constructor(e) {
|
|
21
|
+
this.canvas = e;
|
|
22
|
+
}
|
|
23
|
+
configure(e) {
|
|
24
|
+
this.#i(), this.#e = { ...e };
|
|
25
|
+
}
|
|
26
|
+
unconfigure() {
|
|
27
|
+
this.#i(), this.#e = null;
|
|
28
|
+
}
|
|
29
|
+
getConfiguration() {
|
|
30
|
+
return this.#e && { ...this.#e };
|
|
31
|
+
}
|
|
32
|
+
getCurrentTexture() {
|
|
33
|
+
let t = this.#e;
|
|
34
|
+
if (!t) throw e("state", "Headless canvas is not configured", "headless");
|
|
35
|
+
let { width: r, height: i } = this.canvas;
|
|
36
|
+
return (this.#t?.width !== r || this.#t.height !== i) && (this.#i(), this.#t = t.device.createTexture({
|
|
37
|
+
label: "headless canvas",
|
|
38
|
+
size: [r, i],
|
|
39
|
+
format: t.format,
|
|
40
|
+
usage: (t.usage ?? n) | 17,
|
|
41
|
+
viewFormats: t.viewFormats
|
|
42
|
+
})), this.#t;
|
|
43
|
+
}
|
|
44
|
+
get frame() {
|
|
45
|
+
return this.#t;
|
|
46
|
+
}
|
|
47
|
+
take(e, t) {
|
|
48
|
+
let n = this.#n;
|
|
49
|
+
return this.#n = null, n?.size === t ? n : (n?.buffer.destroy(), {
|
|
50
|
+
buffer: i(e, t),
|
|
51
|
+
size: t,
|
|
52
|
+
generation: this.#r
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
give(e) {
|
|
56
|
+
this.#n || e.generation !== this.#r ? e.buffer.destroy() : this.#n = e;
|
|
57
|
+
}
|
|
58
|
+
#i() {
|
|
59
|
+
this.#r++, this.#n?.buffer.destroy(), this.#n = null, this.#t?.destroy(), this.#t = null;
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
function o(n, i) {
|
|
63
|
+
let o = t(n), s = o.canvas.getContext("webgpu"), c = o.gpu;
|
|
64
|
+
if (!(s instanceof a)) throw e("unsupported", `${i} needs an engine on createHeadlessCanvas`, "headless");
|
|
65
|
+
if (!c) throw e("unsupported", `${i} needs the WebGPU backend`, "headless");
|
|
66
|
+
let l = s.frame, u = s.getConfiguration();
|
|
67
|
+
if (!l || !u) throw e("state", "No frame to read: call engine.render() first", "headless");
|
|
68
|
+
let d = l.format;
|
|
69
|
+
if (d !== "rgba8unorm" && d !== "bgra8unorm") throw e("unsupported", `${i} reads rgba8unorm or bgra8unorm only`, "headless");
|
|
70
|
+
let { width: f, height: p } = l, m = Math.ceil(f * 4 / r) * r;
|
|
71
|
+
return {
|
|
72
|
+
context: s,
|
|
73
|
+
device: c,
|
|
74
|
+
texture: l,
|
|
75
|
+
width: f,
|
|
76
|
+
height: p,
|
|
77
|
+
bytesPerRow: m,
|
|
78
|
+
size: m * p,
|
|
79
|
+
swap: d === "bgra8unorm",
|
|
80
|
+
premultiplied: u.alphaMode === "premultiplied"
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
function s(e, t) {
|
|
84
|
+
let { device: n, texture: r, width: i, height: a, bytesPerRow: o, swap: s, premultiplied: c } = e, l = n.createCommandEncoder();
|
|
85
|
+
return l.copyTextureToBuffer({ texture: r }, {
|
|
86
|
+
buffer: t,
|
|
87
|
+
bytesPerRow: o,
|
|
88
|
+
rowsPerImage: a
|
|
89
|
+
}, [i, a]), n.queue.submit([l.finish()]), {
|
|
90
|
+
width: i,
|
|
91
|
+
height: a,
|
|
92
|
+
bytesPerRow: o,
|
|
93
|
+
swap: s,
|
|
94
|
+
premultiplied: c
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
function c(e, { width: t, height: n, bytesPerRow: r, swap: i, premultiplied: a }) {
|
|
98
|
+
let o = t * 4, s = new Uint8Array(e), c = new Uint8Array(o * n);
|
|
99
|
+
if (r === o) c.set(s.subarray(0, c.length));
|
|
100
|
+
else for (let e = 0; e < n; e++) c.set(s.subarray(e * r, e * r + o), e * o);
|
|
101
|
+
return l(new Uint32Array(c.buffer), i, a), c;
|
|
102
|
+
}
|
|
103
|
+
function l(e, t, n) {
|
|
104
|
+
let r = e.length;
|
|
105
|
+
if (n) for (let n = 0; n < r; n++) {
|
|
106
|
+
let r = e[n];
|
|
107
|
+
t && (r = r & 4278255360 | r >>> 16 & 255 | (r & 255) << 16);
|
|
108
|
+
let i = r >>> 24;
|
|
109
|
+
if (i > 0 && i < 255) {
|
|
110
|
+
let e = 255 / i, t = Math.min(255, Math.round((r & 255) * e)), n = Math.min(255, Math.round((r >>> 8 & 255) * e)), a = Math.min(255, Math.round((r >>> 16 & 255) * e));
|
|
111
|
+
r = i << 24 | a << 16 | n << 8 | t;
|
|
112
|
+
}
|
|
113
|
+
e[n] = r;
|
|
114
|
+
}
|
|
115
|
+
else if (t) for (let t = 0; t < r; t++) {
|
|
116
|
+
let n = e[t];
|
|
117
|
+
e[t] = 4278190080 | n & 65280 | n >>> 16 & 255 | (n & 255) << 16;
|
|
118
|
+
}
|
|
119
|
+
else for (let t = 0; t < r; t++) e[t] |= 4278190080;
|
|
120
|
+
}
|
|
121
|
+
//#endregion
|
|
122
|
+
export { c as a, o as i, s as n, i as r, a as t };
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { Engine } from '@margoengine/core';
|
|
2
|
+
export declare const MAP_READ = 1;
|
|
3
|
+
/** A readback buffer lent by a context, with the canvas configuration it belongs to. */
|
|
4
|
+
export interface TakenBuffer {
|
|
5
|
+
buffer: GPUBuffer;
|
|
6
|
+
size: number;
|
|
7
|
+
generation: number;
|
|
8
|
+
}
|
|
9
|
+
/** A new buffer a texture can be copied into and the CPU can map. */
|
|
10
|
+
export declare function createReadback(device: GPUDevice, size: number): GPUBuffer;
|
|
11
|
+
/** Stands in for a canvas's WebGPU context: the frame goes to one texture this package owns, which stays readable after
|
|
12
|
+
* the frame because nothing presents it. It also keeps one readback buffer between reads. */
|
|
13
|
+
export declare class HeadlessContext {
|
|
14
|
+
#private;
|
|
15
|
+
readonly canvas: {
|
|
16
|
+
width: number;
|
|
17
|
+
height: number;
|
|
18
|
+
};
|
|
19
|
+
constructor(canvas: {
|
|
20
|
+
width: number;
|
|
21
|
+
height: number;
|
|
22
|
+
});
|
|
23
|
+
configure(configuration: GPUCanvasConfiguration): void;
|
|
24
|
+
unconfigure(): void;
|
|
25
|
+
getConfiguration(): GPUCanvasConfiguration | null;
|
|
26
|
+
getCurrentTexture(): GPUTexture;
|
|
27
|
+
/** The last frame's texture, if a frame has asked for one. */
|
|
28
|
+
get frame(): GPUTexture | null;
|
|
29
|
+
/** Lends a readback buffer of `size` bytes: the kept one when it fits, else a new one. */
|
|
30
|
+
take(device: GPUDevice, size: number): TakenBuffer;
|
|
31
|
+
/** Takes an unmapped buffer back. One is kept; a second, or one from before a resize or reconfigure, is destroyed. */
|
|
32
|
+
give(taken: TakenBuffer): void;
|
|
33
|
+
}
|
|
34
|
+
/** What it takes to turn a mapped copy into pixels. */
|
|
35
|
+
export interface FrameCopy {
|
|
36
|
+
width: number;
|
|
37
|
+
height: number;
|
|
38
|
+
bytesPerRow: number;
|
|
39
|
+
swap: boolean;
|
|
40
|
+
premultiplied: boolean;
|
|
41
|
+
}
|
|
42
|
+
/** The last frame of a headless engine, ready to copy. */
|
|
43
|
+
export interface HeadlessFrame extends FrameCopy {
|
|
44
|
+
context: HeadlessContext;
|
|
45
|
+
device: GPUDevice;
|
|
46
|
+
texture: GPUTexture;
|
|
47
|
+
/** Bytes a readback buffer of this frame holds. */
|
|
48
|
+
size: number;
|
|
49
|
+
}
|
|
50
|
+
/** The engine's last frame, or a coded error that names `caller`. */
|
|
51
|
+
export declare function headlessFrame(engine: Engine, caller: string): HeadlessFrame;
|
|
52
|
+
/** Submits the copy of `frame` into `buffer`. It is queued before any later frame the caller draws. */
|
|
53
|
+
export declare function copyFrame(frame: HeadlessFrame, buffer: GPUBuffer): FrameCopy;
|
|
54
|
+
/** RGBA, straight alpha, no row padding, from a mapped copy. */
|
|
55
|
+
export declare function unpack(range: ArrayBuffer, { width, height, bytesPerRow, swap, premultiplied }: FrameCopy): Uint8Array<ArrayBuffer>;
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { t as e } from "./crc32-Cc3b8qnn.js";
|
|
2
|
+
import { n as t, r as n } from "./errors-CDgOgzCd.js";
|
|
3
|
+
import { a as r, i, n as a, t as o } from "./readback-HJLttuJt.js";
|
|
4
|
+
//#region ../shared/png.js
|
|
5
|
+
var s = /* @__PURE__ */ Object.freeze([
|
|
6
|
+
137,
|
|
7
|
+
80,
|
|
8
|
+
78,
|
|
9
|
+
71,
|
|
10
|
+
13,
|
|
11
|
+
10,
|
|
12
|
+
26,
|
|
13
|
+
10
|
|
14
|
+
]), c = (t, n) => {
|
|
15
|
+
let r = new Uint8Array(n.length + 12), i = new DataView(r.buffer);
|
|
16
|
+
i.setUint32(0, n.length);
|
|
17
|
+
for (let e = 0; e < 4; e++) r[4 + e] = t.charCodeAt(e);
|
|
18
|
+
return r.set(n, 8), i.setUint32(n.length + 8, e(r.subarray(4, n.length + 8))), r;
|
|
19
|
+
};
|
|
20
|
+
function l(e, t, n, r) {
|
|
21
|
+
let i = /* @__PURE__ */ new Uint8Array(13), a = new DataView(i.buffer);
|
|
22
|
+
return a.setUint32(0, e), a.setUint32(4, t), i[8] = 8, i[9] = n, [
|
|
23
|
+
Uint8Array.from(s),
|
|
24
|
+
c("IHDR", i),
|
|
25
|
+
c("IDAT", r),
|
|
26
|
+
c("IEND", /* @__PURE__ */ new Uint8Array())
|
|
27
|
+
];
|
|
28
|
+
}
|
|
29
|
+
//#endregion
|
|
30
|
+
//#region src/index.ts
|
|
31
|
+
var u = {
|
|
32
|
+
GPUBufferUsage: {
|
|
33
|
+
MAP_READ: 1,
|
|
34
|
+
MAP_WRITE: 2,
|
|
35
|
+
COPY_SRC: 4,
|
|
36
|
+
COPY_DST: 8,
|
|
37
|
+
INDEX: 16,
|
|
38
|
+
VERTEX: 32,
|
|
39
|
+
UNIFORM: 64,
|
|
40
|
+
STORAGE: 128,
|
|
41
|
+
INDIRECT: 256,
|
|
42
|
+
QUERY_RESOLVE: 512
|
|
43
|
+
},
|
|
44
|
+
GPUTextureUsage: {
|
|
45
|
+
COPY_SRC: 1,
|
|
46
|
+
COPY_DST: 2,
|
|
47
|
+
TEXTURE_BINDING: 4,
|
|
48
|
+
STORAGE_BINDING: 8,
|
|
49
|
+
RENDER_ATTACHMENT: 16
|
|
50
|
+
},
|
|
51
|
+
GPUMapMode: {
|
|
52
|
+
READ: 1,
|
|
53
|
+
WRITE: 2
|
|
54
|
+
},
|
|
55
|
+
GPUShaderStage: {
|
|
56
|
+
VERTEX: 1,
|
|
57
|
+
FRAGMENT: 2,
|
|
58
|
+
COMPUTE: 4
|
|
59
|
+
},
|
|
60
|
+
GPUColorWrite: {
|
|
61
|
+
RED: 1,
|
|
62
|
+
GREEN: 2,
|
|
63
|
+
BLUE: 4,
|
|
64
|
+
ALPHA: 8,
|
|
65
|
+
ALL: 15
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
function d({ gpu: e, globals: t = {} }) {
|
|
69
|
+
if (typeof e?.requestAdapter != "function") throw n("installGPU needs a gpu with requestAdapter", "headless");
|
|
70
|
+
let r = globalThis, i = [], a = (e, t) => {
|
|
71
|
+
e in r || (Object.defineProperty(r, e, {
|
|
72
|
+
value: t,
|
|
73
|
+
configurable: !0,
|
|
74
|
+
writable: !0
|
|
75
|
+
}), i.push(e));
|
|
76
|
+
};
|
|
77
|
+
a("navigator", {});
|
|
78
|
+
let o = r.navigator, s = Object.getOwnPropertyDescriptor(o, "gpu"), c = typeof e.getPreferredCanvasFormat == "function" ? e : {
|
|
79
|
+
requestAdapter: (t) => e.requestAdapter(t),
|
|
80
|
+
getPreferredCanvasFormat: () => "rgba8unorm",
|
|
81
|
+
get wgslLanguageFeatures() {
|
|
82
|
+
return e.wgslLanguageFeatures;
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
Object.defineProperty(o, "gpu", {
|
|
86
|
+
value: c,
|
|
87
|
+
configurable: !0,
|
|
88
|
+
writable: !0
|
|
89
|
+
});
|
|
90
|
+
for (let e of Object.keys(t)) a(e, t[e]);
|
|
91
|
+
for (let e of Object.keys(u)) a(e, Object.freeze({ ...u[e] }));
|
|
92
|
+
let l = !1, d = () => {
|
|
93
|
+
if (!l) {
|
|
94
|
+
l = !0, s ? Object.defineProperty(o, "gpu", s) : delete o.gpu;
|
|
95
|
+
for (let e of i) delete r[e];
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
return {
|
|
99
|
+
gpu: c,
|
|
100
|
+
dispose: d,
|
|
101
|
+
[Symbol.dispose]: d
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
function f({ width: e, height: n }) {
|
|
105
|
+
for (let r of [e, n]) if (!Number.isInteger(r) || r < 1) throw t("Invalid headless canvas size", "headless");
|
|
106
|
+
let r, i = {
|
|
107
|
+
width: e,
|
|
108
|
+
height: n,
|
|
109
|
+
clientWidth: 0,
|
|
110
|
+
clientHeight: 0,
|
|
111
|
+
getContext(e) {
|
|
112
|
+
return e === "webgpu" ? r ??= new o(i) : null;
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
return i;
|
|
116
|
+
}
|
|
117
|
+
async function p(e, { signal: t } = {}) {
|
|
118
|
+
t?.throwIfAborted();
|
|
119
|
+
let n = i(e, "readPixels"), o = n.context.take(n.device, n.size), { buffer: s } = o, c = () => s.destroy();
|
|
120
|
+
t?.addEventListener("abort", c, { once: !0 });
|
|
121
|
+
let l = !1;
|
|
122
|
+
try {
|
|
123
|
+
let e = a(n, s);
|
|
124
|
+
await s.mapAsync(1).catch((e) => {
|
|
125
|
+
throw t?.throwIfAborted(), e;
|
|
126
|
+
}), t?.throwIfAborted();
|
|
127
|
+
let i = r(s.getMappedRange(), e);
|
|
128
|
+
return s.unmap(), l = !0, {
|
|
129
|
+
width: e.width,
|
|
130
|
+
height: e.height,
|
|
131
|
+
data: i
|
|
132
|
+
};
|
|
133
|
+
} finally {
|
|
134
|
+
t?.removeEventListener("abort", c), l ? n.context.give(o) : s.destroy();
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
async function m({ width: e, height: n, data: r }) {
|
|
138
|
+
if (!Number.isInteger(e) || !Number.isInteger(n) || e < 1 || n < 1) throw t("Invalid image size", "headless");
|
|
139
|
+
if (r?.length !== e * n * 4) throw t("Frame data does not match its size", "headless");
|
|
140
|
+
let i = e * 4, a = new Uint8Array((i + 1) * n);
|
|
141
|
+
for (let e = 0; e < n; e++) {
|
|
142
|
+
let t = e * (i + 1);
|
|
143
|
+
a[t] = 2;
|
|
144
|
+
for (let n = 0; n < i; n++) a[t + 1 + n] = r[e * i + n] - (e > 0 ? r[(e - 1) * i + n] : 0);
|
|
145
|
+
}
|
|
146
|
+
let o = new Response(new Blob([a]).stream().pipeThrough(new CompressionStream("deflate"))), s = l(e, n, 6, new Uint8Array(await o.arrayBuffer())), c = new Uint8Array(s.reduce((e, t) => e + t.length, 0)), u = 0;
|
|
147
|
+
for (let e of s) c.set(e, u), u += e.length;
|
|
148
|
+
return c;
|
|
149
|
+
}
|
|
150
|
+
async function h(e, t) {
|
|
151
|
+
return m(await p(e, t));
|
|
152
|
+
}
|
|
153
|
+
//#endregion
|
|
154
|
+
export { h as a, p as i, m as n, d as r, f as t };
|
package/dist/stage.d.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// @ts-ignore optional peer: its types apply when it is installed
|
|
2
|
+
import { Stage, type EngineOptions, type Mesh } from '@margoengine/stage';
|
|
3
|
+
import { type GPUBinding, type HeadlessPixels, type HeadlessReadOptions } from './index.js';
|
|
4
|
+
/** Options of {@link createHeadlessStage}: engine options, less `backend`, which is always WebGPU, plus these. */
|
|
5
|
+
export interface HeadlessStageOptions extends Omit<EngineOptions, 'backend'> {
|
|
6
|
+
/** Width of the image in pixels, a whole number from 1. Required. */
|
|
7
|
+
width: number;
|
|
8
|
+
/** Height of the image in pixels, a whole number from 1. Required. */
|
|
9
|
+
height: number;
|
|
10
|
+
/** The WebGPU implementation: `create([])` from a Node binding. Default `navigator.gpu`. */
|
|
11
|
+
gpu?: GPUBinding;
|
|
12
|
+
/** Globals the binding exports, as in `installGPU`. Default none. */
|
|
13
|
+
globals?: Readonly<Record<string, unknown>>;
|
|
14
|
+
/** Receives engine errors (set as `engine.onError`). */
|
|
15
|
+
onError?: (error: unknown) => void;
|
|
16
|
+
/**
|
|
17
|
+
* No background: pixels nothing draws read back with alpha 0, for graphics laid over other pictures. Attaches
|
|
18
|
+
* `TransparentCanvas` from `@margoengine/environment`, which must be installed. Default false.
|
|
19
|
+
*/
|
|
20
|
+
transparent?: boolean;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* A {@link Stage} with no page. Nothing runs by itself: `engine.start()` needs `requestAnimationFrame`, so call
|
|
24
|
+
* {@link HeadlessStage.step} to advance time, or just read an image. Create one with {@link createHeadlessStage}.
|
|
25
|
+
*/
|
|
26
|
+
export declare class HeadlessStage extends Stage {
|
|
27
|
+
#private;
|
|
28
|
+
constructor(engine: Stage['engine']);
|
|
29
|
+
/** Runs on each {@link HeadlessStage.step}. Returns a function that removes it. */
|
|
30
|
+
onTick(callback: (delta: number) => void): () => void;
|
|
31
|
+
/**
|
|
32
|
+
* Runs `onTick` and `engine.onFrame` callbacks, then draws a frame that advances plugins and animation by `delta`
|
|
33
|
+
* seconds, 0 to 1 as `engine.render` takes it. Default 0. A delta outside that range, or a step from inside a step,
|
|
34
|
+
* throws before any callback runs.
|
|
35
|
+
*/
|
|
36
|
+
step(delta?: number): void;
|
|
37
|
+
/** Sets the image size, whole numbers from 1 as in {@link createHeadlessStage}. The next frame uses it. */
|
|
38
|
+
resize(width: number, height: number): void;
|
|
39
|
+
fit(meshes?: Iterable<Mesh>): void;
|
|
40
|
+
/** Draws a frame and reads it as RGBA pixels. See `readPixels`. */
|
|
41
|
+
readPixels(options?: HeadlessReadOptions): Promise<HeadlessPixels>;
|
|
42
|
+
/** Draws a frame and gives it as PNG file bytes. */
|
|
43
|
+
toPNG(options?: HeadlessReadOptions): Promise<Uint8Array>;
|
|
44
|
+
/** `dispose()`, for `using`. */
|
|
45
|
+
[Symbol.dispose](): void;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* The server version of `createStage`: installs the GPU, creates an engine on a stand-in canvas and wraps it. There
|
|
49
|
+
* are no camera controls and no frame loop. Disposing the stage (or its engine) restores what `installGPU` changed once
|
|
50
|
+
* no other headless stage is alive.
|
|
51
|
+
*/
|
|
52
|
+
export declare function createHeadlessStage(_arg0: HeadlessStageOptions): Promise<HeadlessStage>;
|
package/dist/stage.js
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
// @margo/core-built 0.1.0
|
|
2
|
+
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
+
|
|
4
|
+
import { a as e, i as t, r as n, t as r } from "./src-VufTH2fo.js";
|
|
5
|
+
import { n as i, r as a, t as o } from "./errors-CDgOgzCd.js";
|
|
6
|
+
import { Engine as s, Stage as c } from "@margoengine/stage";
|
|
7
|
+
//#region src/stage.ts
|
|
8
|
+
var l;
|
|
9
|
+
function u(e, t) {
|
|
10
|
+
if (l && l.gpu !== e) throw o("state", "Headless stages alive at once share one gpu", "headless");
|
|
11
|
+
l ??= {
|
|
12
|
+
gpu: e,
|
|
13
|
+
installed: n({
|
|
14
|
+
gpu: e,
|
|
15
|
+
globals: t
|
|
16
|
+
}),
|
|
17
|
+
users: 0
|
|
18
|
+
}, l.users++;
|
|
19
|
+
let r = !1;
|
|
20
|
+
return () => {
|
|
21
|
+
r || (r = !0, l && --l.users === 0 && (l.installed.dispose(), l = void 0));
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
var d = class extends c {
|
|
25
|
+
#e = /* @__PURE__ */ new Set();
|
|
26
|
+
#t = /* @__PURE__ */ new Set();
|
|
27
|
+
#n = !1;
|
|
28
|
+
constructor(e) {
|
|
29
|
+
super(e);
|
|
30
|
+
let t = e.onFrame.bind(e);
|
|
31
|
+
e.onFrame = (e) => {
|
|
32
|
+
let n = t(e);
|
|
33
|
+
return this.#t.add(e), () => {
|
|
34
|
+
n(), this.#t.delete(e);
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
onTick(e) {
|
|
39
|
+
return this.#e.add(e), () => void this.#e.delete(e);
|
|
40
|
+
}
|
|
41
|
+
step(e = 0) {
|
|
42
|
+
if (typeof e != "number" || !(e >= 0 && e <= 1)) throw i("Stage step delta must be 0 to 1 seconds", "headless");
|
|
43
|
+
if (this.#n) throw o("state", "Stage step is already running", "headless");
|
|
44
|
+
this.#n = !0;
|
|
45
|
+
try {
|
|
46
|
+
for (let t of this.#e) t(e);
|
|
47
|
+
for (let t of this.#t) t(this.engine, e);
|
|
48
|
+
this.engine.render({ delta: e });
|
|
49
|
+
} finally {
|
|
50
|
+
this.#n = !1;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
resize(e, t) {
|
|
54
|
+
for (let n of [e, t]) if (!Number.isInteger(n) || n < 1) throw i("Stage size must be whole numbers from 1", "headless");
|
|
55
|
+
let { canvas: n } = this;
|
|
56
|
+
n.width = e, n.height = t;
|
|
57
|
+
}
|
|
58
|
+
fit(e) {
|
|
59
|
+
let t = this.canvas;
|
|
60
|
+
t.clientWidth = t.width, t.clientHeight = t.height;
|
|
61
|
+
try {
|
|
62
|
+
super.fit(e);
|
|
63
|
+
} finally {
|
|
64
|
+
t.clientWidth = t.clientHeight = 0;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
readPixels(e) {
|
|
68
|
+
return this.engine.render(), t(this.engine, e);
|
|
69
|
+
}
|
|
70
|
+
toPNG(t) {
|
|
71
|
+
return this.engine.render(), e(this.engine, t);
|
|
72
|
+
}
|
|
73
|
+
[Symbol.dispose]() {
|
|
74
|
+
this.dispose();
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
async function f() {
|
|
78
|
+
try {
|
|
79
|
+
return await import("@margoengine/environment");
|
|
80
|
+
} catch (e) {
|
|
81
|
+
throw e.code === "ERR_MODULE_NOT_FOUND" ? Object.assign(o("state", "Install @margoengine/environment", "install"), { cause: e }) : e;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
async function p({ width: e, height: t, gpu: n = globalThis.navigator?.gpu, globals: i, onError: o, transparent: c = !1, ...l }) {
|
|
85
|
+
if (o !== void 0 && typeof o != "function") throw a("Invalid stage onError: expected a function or undefined; pass an error callback or omit onError", "arguments");
|
|
86
|
+
if (typeof c != "boolean") throw a("Invalid stage transparent: expected a boolean; pass true for a transparent background or omit transparent", "arguments");
|
|
87
|
+
let p = c ? await f() : void 0, m = r({
|
|
88
|
+
width: e,
|
|
89
|
+
height: t
|
|
90
|
+
}), h = u(n, i);
|
|
91
|
+
try {
|
|
92
|
+
let e = await s.create(m, {
|
|
93
|
+
...l,
|
|
94
|
+
backend: "webgpu"
|
|
95
|
+
});
|
|
96
|
+
return o && (e.onError = o), e.use({coreRequirement:/*@__PURE__*/__margoCoreRequirement("0.1.0",import.meta.url),
|
|
97
|
+
name: "margoengine-headless",
|
|
98
|
+
setup() {},
|
|
99
|
+
dispose: h
|
|
100
|
+
}), p && e.use(new p.TransparentCanvas()), new d(e);
|
|
101
|
+
} catch (e) {
|
|
102
|
+
throw h(), e;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
//#endregion
|
|
106
|
+
export { d as HeadlessStage, p as createHeadlessStage };
|
package/dist/tiles.d.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { Camera, type Engine } from '@margoengine/core';
|
|
2
|
+
import { type HeadlessPixels } from './index.js';
|
|
3
|
+
/** Options for a still larger than the device texture limit. */
|
|
4
|
+
export interface TiledRenderOptions {
|
|
5
|
+
/** Output width in pixels, a positive safe integer. Required. */
|
|
6
|
+
width: number;
|
|
7
|
+
/** Output height in pixels, a positive safe integer. Required. */
|
|
8
|
+
height: number;
|
|
9
|
+
/** Maximum tile edge, including padding. Default min(1024, device limit). */
|
|
10
|
+
tileSize?: number;
|
|
11
|
+
/** Subpixel renders per output pixel, 1..1024. Default 1. */
|
|
12
|
+
samples?: number;
|
|
13
|
+
/** Guard pixels on each tile edge for local screen-space filters. Default 0. */
|
|
14
|
+
padding?: number;
|
|
15
|
+
/** Camera, including its custom projection. Default engine.camera. */
|
|
16
|
+
camera?: Camera;
|
|
17
|
+
/** Cancel between samples or during readback. Default none. */
|
|
18
|
+
signal?: AbortSignal;
|
|
19
|
+
}
|
|
20
|
+
/** One finished tile, top row first, with guard pixels removed. */
|
|
21
|
+
export interface RenderedTile extends HeadlessPixels {
|
|
22
|
+
/** Left edge in the full image. */
|
|
23
|
+
x: number;
|
|
24
|
+
/** Top edge in the full image. */
|
|
25
|
+
y: number;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Streams a WebGPU headless still in row-major tiles. Time stays fixed (delta 0).
|
|
29
|
+
* Each sample has the same full-image subpixel offset, so boundaries agree.
|
|
30
|
+
* Accumulation uses premultiplied linear light, returning straight-alpha sRGB.
|
|
31
|
+
* CPU memory is O(tileSize squared), independent of image size and sample count.
|
|
32
|
+
* Do not render or resize this engine concurrently. Closing the iterator restores
|
|
33
|
+
* the canvas; the caller's camera is never modified. Nonlocal/temporal effects
|
|
34
|
+
* need to be disabled; finite-radius filters need sufficient padding.
|
|
35
|
+
*/
|
|
36
|
+
export declare function renderTiles(instance: Engine, options: TiledRenderOptions): AsyncGenerator<RenderedTile>;
|
|
37
|
+
/** Collects renderTiles into one RGBA image. Full-image CPU memory must fit the runtime. */
|
|
38
|
+
export declare function renderTiled(engine: Engine, options: TiledRenderOptions): Promise<HeadlessPixels>;
|