@drawcall/webgpu 0.2.0 → 0.2.1

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/README.md CHANGED
@@ -15,7 +15,7 @@ renderer.render(scene, camera);
15
15
 
16
16
  In a browser, append the returned canvas to your container. It is a real canvas with normal CSS, events, and resizing. Use `{ offscreen: true }` as the third argument for an `OffscreenCanvas`. Browser support requires WebGPU in a secure context. Node canvases are always headless: CSS assignments are inert; explicit dimensions control output resolution.
17
17
 
18
- For Node image export, canvas-free compute, loading assets, and cleanup, see the shipped [usage skill](skills/webgpu/SKILL.md). Compute uses `requestAdapter()` followed by standard WebGPU device and command APIs. Node does not provide DOM layout, video, or canvas-2D texture sources.
18
+ For Node image export, canvas-free compute, loading assets, and cleanup, see the shipped [usage skill](skills/webgpu/SKILL.md). Compute uses `requestAdapter()` followed by standard WebGPU device and command APIs. Node does not provide DOM layout, video, or a 2D canvas; a canvas library's 2D canvases work as texture sources.
19
19
 
20
20
  Node requires version 22+. Native packages bundle the software renderer and Vulkan loader for Linux x64/ARM64 (glibc 2.28+ or musl 1.2.5+/Alpine 3.22+), macOS 13.5+ (Intel/Apple Silicon), and Windows x64/ARM64. x64 requires SSE4.2. No GPU, browser, compiler, SDK, or system graphics packages are needed.
21
21
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drawcall/webgpu",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "WebGPU rendering and compute in browsers and Node.js, with or without a GPU",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -37,14 +37,14 @@
37
37
  "sharp": "0.35.5"
38
38
  },
39
39
  "optionalDependencies": {
40
- "@drawcall/webgpu-linux-x64": "0.2.0",
41
- "@drawcall/webgpu-linux-arm64": "0.2.0",
42
- "@drawcall/webgpu-darwin-x64": "0.2.0",
43
- "@drawcall/webgpu-darwin-arm64": "0.2.0",
44
- "@drawcall/webgpu-win32-x64": "0.2.0",
45
- "@drawcall/webgpu-linuxmusl-x64": "0.2.0",
46
- "@drawcall/webgpu-linuxmusl-arm64": "0.2.0",
47
- "@drawcall/webgpu-win32-arm64": "0.2.0"
40
+ "@drawcall/webgpu-linux-x64": "0.2.1",
41
+ "@drawcall/webgpu-linux-arm64": "0.2.1",
42
+ "@drawcall/webgpu-darwin-x64": "0.2.1",
43
+ "@drawcall/webgpu-darwin-arm64": "0.2.1",
44
+ "@drawcall/webgpu-win32-x64": "0.2.1",
45
+ "@drawcall/webgpu-linuxmusl-x64": "0.2.1",
46
+ "@drawcall/webgpu-linuxmusl-arm64": "0.2.1",
47
+ "@drawcall/webgpu-win32-arm64": "0.2.1"
48
48
  },
49
49
  "devDependencies": {
50
50
  "three": "0.186.1"
@@ -42,7 +42,7 @@ The example above exports a frame in Node. In browsers, use the canvas normally;
42
42
 
43
43
  Render the final frame to the canvas. If custom passes use render targets, call `renderer.setRenderTarget(null)` before the final canvas render and readback. Dispose the renderer and loaders before the canvas. A canvas does not own an application-supplied device: destroy that device after all rendering and compute users finish.
44
44
 
45
- For cold server-side rendering, launch a fresh process per task, render, encode, and await disposal. Node does not supply a browser DOM, video decoding, 2D canvas, or the complete browser Worker API. Blob image decoding and classic workers support the tested glTF and Draco paths; other loaders may need browser APIs outside this scope. Treat unsupported API errors as compatibility gaps rather than replacing assets or patching Three.js.
45
+ For cold server-side rendering, launch a fresh process per task, render, encode, and await disposal. Node does not supply a browser DOM, video decoding, 2D canvas, or the complete browser Worker API. Code that needs a 2D canvas can use a canvas library: `copyExternalImageToTexture`, and so Three's `CanvasTexture`, accepts that library's canvases and `ImageData`, and `readImageBitmap` turns a decoded `ImageBitmap` into pixels it can draw. Blob image decoding and classic workers support the tested glTF and Draco paths; other loaders may need browser APIs outside this scope. Treat unsupported API errors as compatibility gaps rather than replacing assets or patching Three.js.
46
46
 
47
47
  # Compute without a canvas
48
48
 
package/src/browser.js CHANGED
@@ -24,3 +24,10 @@ export async function requestAdapter(options) {
24
24
  export function getPreferredCanvasFormat() {
25
25
  return gpu().getPreferredCanvasFormat();
26
26
  }
27
+
28
+ /** The straight-alpha sRGB RGBA pixels of an ImageBitmap, as ImageData. */
29
+ export function readImageBitmap(bitmap) {
30
+ const context = new OffscreenCanvas(bitmap.width, bitmap.height).getContext('2d');
31
+ context.drawImage(bitmap, 0, 0);
32
+ return context.getImageData(0, 0, bitmap.width, bitmap.height);
33
+ }
package/src/gpu.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { globals } from './globals.js';
2
+ import { installCopySources } from './image.js';
2
3
  import { loadNative } from './native.js';
3
4
 
4
5
  let runtime;
@@ -14,6 +15,7 @@ export function getRuntime() {
14
15
  for (const [name, value] of Object.entries(loaded.native.globals)) {
15
16
  if (name !== 'ImageBitmap') installed.set(name, value);
16
17
  }
18
+ installCopySources(loaded.native);
17
19
  const navigator = globalThis.navigator ?? {};
18
20
  Object.defineProperty(navigator, 'gpu', { value: gpu, configurable: true });
19
21
  if (!globalThis.navigator) installed.set('navigator', navigator);
package/src/image.js CHANGED
@@ -2,6 +2,49 @@
2
2
  import { globals } from './globals.js';
3
3
 
4
4
  let sharpPromise;
5
+
6
+ /**
7
+ * Let copyExternalImageToTexture take ImageData and 2D canvases, which Node has only through canvas libraries: their
8
+ * pixels upload through a temporary native ImageBitmap, so copies behave as the ImageBitmap copies browsers match.
9
+ */
10
+ export function installCopySources(native) {
11
+ const { prototype } = native.globals.GPUQueue;
12
+ const copy = prototype.copyExternalImageToTexture;
13
+ prototype.copyExternalImageToTexture = function (source, destination, size) {
14
+ const pixels = imageData(source?.source);
15
+ // Anything else reaches the native method, which reports what it does not support.
16
+ if (!pixels) return copy.call(this, source, destination, size);
17
+ const bytes = new Uint8Array(pixels.data.buffer, pixels.data.byteOffset, pixels.data.byteLength);
18
+ const bitmap = native.createImageBitmapFromRGBA(pixels.width, pixels.height, bytes);
19
+ try {
20
+ return copy.call(this, { ...source, source: bitmap }, destination, size);
21
+ } finally {
22
+ bitmap.close();
23
+ }
24
+ };
25
+ }
26
+
27
+ /** The straight-alpha sRGB pixels of ImageData, or of a canvas with a 2D context. */
28
+ function imageData(image) {
29
+ if (typeof image !== 'object' || image === null) return;
30
+ const pixels = typeof image.getContext === 'function'
31
+ ? image.getContext('2d')?.getImageData(0, 0, image.width, image.height)
32
+ : image;
33
+ if (!(pixels?.data instanceof Uint8ClampedArray)) return;
34
+ if ((pixels.colorSpace ?? 'srgb') !== 'srgb') throw new TypeError(`Only sRGB image data uploads, not ${pixels.colorSpace}`);
35
+ return pixels;
36
+ }
37
+
38
+ /** The straight-alpha RGBA pixels of an ImageBitmap from createImageBitmap, as ImageData's fields. */
39
+ export function readImageBitmap(native, bitmap) {
40
+ const { data, premultipliedAlpha } = native.readImageBitmapRGBA(bitmap);
41
+ if (premultipliedAlpha) for (let i = 0; i < data.length; i += 4) {
42
+ const alpha = data[i + 3];
43
+ if (alpha) for (let channel = i; channel < i + 3; channel++) data[channel] = Math.round(data[channel] * 255 / alpha);
44
+ }
45
+ return { width: bitmap.width, height: bitmap.height, data: new Uint8ClampedArray(data.buffer), colorSpace: 'srgb' };
46
+ }
47
+
5
48
  export function installImages(native) {
6
49
  if (typeof native.createImageBitmapFromRGBA !== 'function') throw new Error('Native ImageBitmap bridge is unavailable');
7
50
  const installed = globals();
package/src/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { canvas } from './canvas.js';
2
2
  import { getRuntime } from './gpu.js';
3
+ import { readImageBitmap as readNative } from './image.js';
3
4
 
4
5
  export { requestAdapter, getPreferredCanvasFormat } from './gpu.js';
5
6
 
@@ -28,3 +29,11 @@ export async function createCanvas(width, height, { offscreen = false } = {}) {
28
29
  })();
29
30
  return target.output;
30
31
  }
32
+
33
+ /**
34
+ * The straight-alpha sRGB RGBA pixels of an ImageBitmap from createImageBitmap, as ImageData's fields: what a 2D canvas
35
+ * library needs to draw it, since Node has no 2D canvas of its own.
36
+ */
37
+ export function readImageBitmap(bitmap) {
38
+ return readNative(getRuntime().native, bitmap);
39
+ }