@motionscript/renderer 0.0.0-stage → 0.1.0-alpha.8

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.
@@ -0,0 +1,20 @@
1
+ {
2
+ "name": "@motionscript/renderer",
3
+ "version": "0.1.0-alpha.8",
4
+ "exports": {
5
+ ".": "dist/browser/index.js"
6
+ },
7
+ "peers": [
8
+ "@motionscript/core"
9
+ ],
10
+ "assets": {
11
+ "ms_renderer_bg.wasm": "dist/ms_renderer_bg.wasm"
12
+ },
13
+ "thirdParty": [
14
+ {
15
+ "name": "mediabunny",
16
+ "version": "1.49.0",
17
+ "license": "MPL-2.0"
18
+ }
19
+ ]
20
+ }
@@ -0,0 +1,153 @@
1
+ // The package's published types (dist/index.d.ts): the engine entry's surface.
2
+ import type { EngineRenderContext, MeasureContext2D, PlatformDeps, StorageAdapter } from "@motionscript/core";
3
+
4
+ export interface WgpuRendererOptions {
5
+ /** Where `ms_renderer_bg.wasm` is served; defaults to beside this module. */
6
+ wasmUrl?: string | URL;
7
+ /** Fetches an asset's bytes (images, video, audio, fonts); defaults to `fetch`, rejecting on a failed response. */
8
+ loadBytes?: (src: string) => Promise<Blob>;
9
+ }
10
+
11
+ /** The renderer's asset storage: decoded images and video frames, and font bytes. */
12
+ export interface WgpuStorageAdapter extends StorageAdapter {}
13
+
14
+ /** A session's WebGPU surface. */
15
+ export type WgpuSurfaceContext = EngineRenderContext & {
16
+ /** Settles once the surface `mount` began can draw (its device is up). */
17
+ whenMounted(): Promise<void>;
18
+ /** The frame last drawn, copied into a fresh 2D canvas, or `undefined` before one was. */
19
+ snapshotCanvas(): HTMLCanvasElement | undefined;
20
+ };
21
+
22
+ /** What a platform draws a session's frames with, and the storage and measurement they read from. */
23
+ export interface WgpuRenderer {
24
+ createStorageAdapter(deps: PlatformDeps): WgpuStorageAdapter;
25
+ createRenderContext(storage: WgpuStorageAdapter): WgpuSurfaceContext;
26
+ createMeasureContext(storage: WgpuStorageAdapter): MeasureContext2D;
27
+ dispose?(): void;
28
+ }
29
+
30
+ /** Whether this browser can run the renderer (WebGPU with an adapter). */
31
+ export declare function isWgpuSupported(): Promise<boolean>;
32
+
33
+ /** A GPU fault on the page. Every surface shares one device, so each is news for all of them. */
34
+ export type WgpuEvent =
35
+ | { type: "device-lost"; reason: string; message: string }
36
+ | { type: "device-restored" }
37
+ | { type: "gpu-error"; error: "validation" | "out-of-memory" | "internal"; message: string }
38
+ | { type: "crashed"; message: string };
39
+
40
+ /**
41
+ * Calls `listener` on every GPU fault on the page; returns the unsubscribe.
42
+ * A lost device is replaced and every live surface remounted on the new one
43
+ * (`device-restored` once it is up); each fault also reaches every surface's
44
+ * render issues, and fails an export under way with a `RenderBackendError`.
45
+ */
46
+ export declare function onWgpuEvent(listener: (event: WgpuEvent) => void): () => void;
47
+
48
+ /**
49
+ * Loads the renderer's WASM, for @motionscript/browser's createBrowserEngine()
50
+ * to draw with. Rejects where WebGPU is unavailable only once a surface mounts;
51
+ * check {@link isWgpuSupported} first.
52
+ */
53
+ export declare function createWgpuRenderer(options?: WgpuRendererOptions): Promise<WgpuRenderer>;
54
+
55
+ // ── User shaders (WGSL) ─────────────────────────────────────────────────────
56
+
57
+ export type ShaderScope = "fill" | "effect";
58
+
59
+ /** One problem in an author's source. `line`/`column` are 1-based; 0 = not in the source. */
60
+ export interface ShaderDiagnostic {
61
+ line: number;
62
+ column: number;
63
+ length: number;
64
+ message: string;
65
+ /** `naga`: the renderer's validation; `browser`: the GPU's own compile (`getCompilationInfo`). */
66
+ source: "naga" | "browser";
67
+ }
68
+
69
+ /** One member of the source's `struct Params`, as WGSL's uniform layout places it. */
70
+ export interface ShaderField {
71
+ name: string;
72
+ /** Byte offset in `Params`. */
73
+ offset: number;
74
+ /** 0 f32, 1 i32, 2 u32. */
75
+ scalar: number;
76
+ /** Components per element (1–4). */
77
+ components: number;
78
+ /** Elements (array length or matrix columns), `stride` bytes apart. */
79
+ elements: number;
80
+ stride: number;
81
+ /** The WGSL type (`vec3<f32>`, `array<vec4<f32>, 4>`). */
82
+ type: string;
83
+ }
84
+
85
+ export interface ShaderReport {
86
+ ok: boolean;
87
+ diagnostics: ShaderDiagnostic[];
88
+ fields: ShaderField[];
89
+ /** Size of `Params` in bytes. */
90
+ size: number;
91
+ /** The assembled module. */
92
+ wgsl?: string;
93
+ }
94
+
95
+ /**
96
+ * Validates a WGSL fill or effect with naga (loading the renderer's WASM on
97
+ * first use): diagnostics located in `source`, and the reflected `Params`.
98
+ */
99
+ export declare function validateShader(scope: ShaderScope, source: string, wasmUrl?: string | URL): Promise<ShaderReport>;
100
+
101
+ /** What the renderer puts in front of an author's code, for an editor to show. */
102
+ export declare const WGSL_PRELUDE: Record<ShaderScope, string>;
103
+
104
+ export interface ShaderPreviewInput {
105
+ scope: ShaderScope;
106
+ source: string;
107
+ /** `Params` values by member name. */
108
+ uniforms: Record<string, number | readonly number[]>;
109
+ /** Seconds (`in.time`). */
110
+ time: number;
111
+ /** Effects: run over what is beneath the shape (`mode: "backdrop"`) rather than the shape's own content. */
112
+ backdrop?: boolean;
113
+ /** Effects: how the output composites onto the input (core blend name); absent = replaces it. */
114
+ blendMode?: string;
115
+ /** Effects: px the effect may draw or read past the shape. */
116
+ reach?: number;
117
+ /** Effects: the whole frame (default), or an inset rounded rect so the untouched content shows around it. */
118
+ frame?: "full" | "inset";
119
+ /** The content underneath, cover-fitted, in place of the test pattern (or of nothing, under a fill). */
120
+ image?: ImageBitmap | null;
121
+ /** Before/after: left of `split × width` shows the content without the shader. */
122
+ split?: number | null;
123
+ /** A fill's texture (`sample_texture`). */
124
+ texture?: ImageBitmap | null;
125
+ }
126
+
127
+ export interface ShaderStatus {
128
+ state: "compiling" | "ready" | "failed" | "empty";
129
+ /** naga's, then the browser's. */
130
+ diagnostics: ShaderDiagnostic[];
131
+ }
132
+
133
+ /**
134
+ * One preview surface on its own WebGPU device. `draw` never waits: a program
135
+ * still compiling draws what lies beneath, and the last input is drawn again
136
+ * when it is ready (`onDraw` says so).
137
+ */
138
+ export declare class ShaderPreview {
139
+ /** Called after a redraw the preview made by itself (a program became ready). */
140
+ onDraw: (() => void) | null;
141
+ static create(canvas: HTMLCanvasElement | OffscreenCanvas, wasmUrl?: string | URL): Promise<ShaderPreview>;
142
+ /** Draws `input` at the canvas's current size. */
143
+ draw(input: ShaderPreviewInput): ShaderStatus;
144
+ /** Draws `input` once its program has compiled, and resolves to the frame (straight alpha). */
145
+ pixels(input: ShaderPreviewInput): Promise<ImageData>;
146
+ /** The same, encoded. */
147
+ snapshot(input: ShaderPreviewInput, type?: string, quality?: number): Promise<Blob>;
148
+ /** Settles once every program drawn so far is ready or failed. */
149
+ whenReady(): Promise<void>;
150
+ /** A program's state and diagnostics (compiling it if it is new). */
151
+ status(scope: ShaderScope, source: string): ShaderStatus;
152
+ destroy(): void;
153
+ }