alpha2 0.4.3 → 0.4.5

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/dist/worker.js CHANGED
@@ -36,6 +36,7 @@ export function installRenderWorker(scope, createHost) {
36
36
  switch (request.type) {
37
37
  case "initialize":
38
38
  host = await createHost(request.canvas);
39
+ result = host.rendererChoice;
39
40
  break;
40
41
  case "upload-video-frame":
41
42
  // The frame was transferred here; with no host to take it over, it is closed here.
@@ -150,9 +151,13 @@ export function installRenderWorker(scope, createHost) {
150
151
  * binding. The callback makes a canvas renderer, registers `options`' effects, resources and
151
152
  * fonts, then loads `projectJson`.
152
153
  *
153
- * The callback rejects when the binding has no `createForCanvas`, or when making the renderer,
154
- * compiling an effect or loading the project fails. After a device loss, `recover()` repeats the
155
- * same registration on a new renderer.
154
+ * The renderer is the one `options.renderer` picks (see {@link WasmRenderWorkerHostOptions}); the
155
+ * host's `rendererChoice` says which, and `initialize` answers it.
156
+ *
157
+ * The callback rejects when the binding lacks the constructor the renderer needs
158
+ * (`createForCanvas`, or `createCpu` for `"cpu"`), or when making the renderer, compiling an
159
+ * effect or loading the project fails. After a device loss, `recover()` repeats the same
160
+ * registration on a new renderer of the same kind.
156
161
  */
157
162
  export function createWasmRenderWorkerHost(binding, projectJson, options = {}) {
158
163
  const effects = options.effects ?? [];
@@ -160,11 +165,11 @@ export function createWasmRenderWorkerHost(binding, projectJson, options = {}) {
160
165
  const videoConversion = options.videoConversion ?? "engine";
161
166
  if (videoConversion !== "engine" && videoConversion !== "browser")
162
167
  throw new RangeError('videoConversion must be "engine" or "browser"');
168
+ const renderer = options.renderer ?? "auto";
169
+ if (renderer !== "auto" && renderer !== "gpu" && renderer !== "cpu")
170
+ throw new RangeError('renderer must be "auto", "gpu" or "cpu"');
163
171
  return async (canvas) => {
164
- if (binding.createForCanvas === undefined) {
165
- throw new Error("wasm binding does not expose createForCanvas");
166
- }
167
- return WasmRenderWorkerHost.create(binding, canvas, projectJson, effects, resources, options.fonts ?? [], options.uploadRetention, videoConversion);
172
+ return WasmRenderWorkerHost.create(binding, canvas, projectJson, effects, resources, options.fonts ?? [], options.uploadRetention, videoConversion, renderer);
168
173
  };
169
174
  }
170
175
  class WasmRenderWorkerHost {
@@ -178,6 +183,7 @@ class WasmRenderWorkerHost {
178
183
  #uploadedFrames;
179
184
  #physicalFrames = new PhysicalFrameAliases();
180
185
  #videoConversion;
186
+ rendererChoice;
181
187
  #pendingUploads = [];
182
188
  /** Buffers the planar copies reuse; each is given back once its upload has taken it. */
183
189
  #planarCopies = new PlanarCopyPool();
@@ -189,8 +195,9 @@ class WasmRenderWorkerHost {
189
195
  throw lostRenderer();
190
196
  return this.#registry;
191
197
  }
192
- constructor(binding, canvas, projectJson, effects, resources, fonts, wasm, uploadRetention, videoConversion) {
198
+ constructor(binding, canvas, projectJson, effects, resources, fonts, wasm, uploadRetention, videoConversion, rendererChoice) {
193
199
  this.#binding = binding;
200
+ this.rendererChoice = rendererChoice;
194
201
  this.#canvas = canvas;
195
202
  this.#projectJson = projectJson;
196
203
  this.#effects = effects;
@@ -204,15 +211,13 @@ class WasmRenderWorkerHost {
204
211
  this.#registry?.releaseFrameGeneration(generation);
205
212
  }, uploadRetention);
206
213
  }
207
- static async create(binding, canvas, projectJson, effects, resources, fonts, uploadRetention, videoConversion) {
208
- const wasm = await binding.createForCanvas?.(canvas);
209
- if (wasm === undefined)
210
- throw new Error("createForCanvas is unavailable");
214
+ static async create(binding, canvas, projectJson, effects, resources, fonts, uploadRetention, videoConversion, mode) {
215
+ const { wasm, choice } = await makeRenderer(binding, canvas, mode);
211
216
  await registerEffects(wasm, effects);
212
217
  registerResources(wasm, resources);
213
218
  registerFonts(wasm, fonts);
214
219
  wasm.loadProject(projectJson);
215
- return new WasmRenderWorkerHost(binding, canvas, projectJson, effects, resources, fonts, wasm, uploadRetention, videoConversion);
220
+ return new WasmRenderWorkerHost(binding, canvas, projectJson, effects, resources, fonts, wasm, uploadRetention, videoConversion, choice);
216
221
  }
217
222
  async uploadVideoFrame(frame) {
218
223
  const rotation = frame.rotationDegrees ?? 0;
@@ -338,6 +343,7 @@ class WasmRenderWorkerHost {
338
343
  const lost = this.#registry;
339
344
  this.#registry = undefined;
340
345
  lost?.free?.();
346
+ // Only a GPU renderer is ever lost; the CPU one has no device.
341
347
  if (this.#binding.createForCanvas === undefined) {
342
348
  throw new Error("createForCanvas is unavailable");
343
349
  }
@@ -369,6 +375,50 @@ class WasmRenderWorkerHost {
369
375
  this.#physicalFrames.clear();
370
376
  }
371
377
  }
378
+ /**
379
+ * Makes the renderer `mode` asks for on `canvas` and says which it is. `"auto"` falls back to the
380
+ * CPU only when the GPU renderer could not be made for want of a GPU (`GPU_INIT_FAILED`, or a
381
+ * canvas that takes no GPU surface: `INVALID_INPUT`); a canvas whose failed WebGL2 request left it
382
+ * without a context still takes the CPU renderer's 2D one.
383
+ */
384
+ async function makeRenderer(binding, canvas, mode) {
385
+ const cpu = (rendererReason) => {
386
+ if (binding.createCpu === undefined)
387
+ throw new Error("wasm binding does not expose createCpu");
388
+ return {
389
+ wasm: binding.createCpu(canvas),
390
+ choice: { renderer: "cpu", rendererReason },
391
+ };
392
+ };
393
+ if (mode === "cpu")
394
+ return cpu('the CPU renderer was asked for (renderer "cpu")');
395
+ if (binding.createForCanvas === undefined) {
396
+ if (mode === "auto" && binding.createCpu !== undefined)
397
+ return cpu("the wasm binding has no GPU renderer");
398
+ throw new Error("wasm binding does not expose createForCanvas");
399
+ }
400
+ try {
401
+ return {
402
+ wasm: await binding.createForCanvas(canvas),
403
+ choice: { renderer: "gpu", rendererReason: null },
404
+ };
405
+ }
406
+ catch (error) {
407
+ const code = error?.code;
408
+ if (mode !== "auto" ||
409
+ binding.createCpu === undefined ||
410
+ (code !== "GPU_INIT_FAILED" && code !== "INVALID_INPUT"))
411
+ throw error;
412
+ const message = error instanceof Error ? error.message : String(error);
413
+ try {
414
+ return cpu(`the browser offers no WebGPU adapter and no WebGL2 context (${message})`);
415
+ }
416
+ catch {
417
+ // The GPU's failure is the one to report: it says why there is no renderer at all.
418
+ throw error;
419
+ }
420
+ }
421
+ }
372
422
  /** The engine's name for a missing frame, with its source time as a tick count. */
373
423
  function missingFrame(frame) {
374
424
  return {
@@ -434,6 +484,7 @@ export class WorkerRenderBackend {
434
484
  #pending = new Map();
435
485
  #pendingExportReadbacks = new Map();
436
486
  #nextId = 1;
487
+ #rendererChoice;
437
488
  #deviceLost = false;
438
489
  #disposed = false;
439
490
  /** Set once the worker raised an uncaught error; see `#onWorkerError`. */
@@ -443,9 +494,19 @@ export class WorkerRenderBackend {
443
494
  worker.addEventListener("message", this.#onMessage);
444
495
  worker.addEventListener("error", this.#onWorkerError);
445
496
  }
497
+ /**
498
+ * The renderer the worker's host draws with, once {@link WorkerRenderBackend.initialize} has
499
+ * resolved; `undefined` before, or for a host that does not say.
500
+ */
501
+ get rendererChoice() {
502
+ return this.#rendererChoice;
503
+ }
446
504
  /** Transfers `canvas` to the worker and creates its host there. */
447
505
  async initialize(canvas) {
448
- await this.#request({ type: "initialize", canvas }, [canvas]);
506
+ const choice = await this.#request({ type: "initialize", canvas }, [
507
+ canvas,
508
+ ]);
509
+ this.#rendererChoice = choice;
449
510
  }
450
511
  /**
451
512
  * Transfers `frame.frame` to the worker; it is closed here if it cannot be posted, including
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "alpha2",
3
- "version": "0.4.3",
3
+ "version": "0.4.5",
4
4
  "license": "MIT",
5
5
  "description": "JavaScript bindings for the standalone alpha2 media engine.",
6
6
  "repository": {
Binary file
Binary file