@drawcall/webgpu 0.1.1 → 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
@@ -1,6 +1,6 @@
1
1
  # @drawcall/webgpu
2
2
 
3
- Use standard Three.js rendering and WebGPU compute in browsers and GPU-less Node.js. Browsers use their own WebGPU implementation; Node uses bundled native CPU rendering. Three.js and original assets stay unchanged.
3
+ Use standard Three.js rendering and WebGPU compute in browsers and Node.js, with or without a GPU. Browsers use their own WebGPU implementation; Node uses the native GPU when available and falls back to bundled CPU rendering. Three.js and original assets stay unchanged.
4
4
 
5
5
  ```js
6
6
  import { createCanvas } from '@drawcall/webgpu';
@@ -15,8 +15,10 @@ 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
- 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. Keep npm optional dependencies enabled. Other platform/CPU combinations are unsupported.
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
+
22
+ When present, Node uses the native GPU through Metal on macOS and installed Vulkan drivers on Linux and Windows. The default adapter is the GPU; `requestAdapter({ forceFallbackAdapter: true })` selects the bundled CPU renderer, whose output is reproducible: pass its device to `WebGPURenderer`. NVIDIA's Linux Vulkan driver needs X11 and EGL client libraries even headless (Debian/Ubuntu: `libx11-6 libxext6 libegl1`). Keep npm optional dependencies enabled. Other platform/CPU combinations are unsupported.
21
23
 
22
24
  Run the upstream Three.js [compute-texture example](examples/compute-texture) with `npm install --prefix examples/compute-texture` and `npm run dev --prefix examples/compute-texture`. Browser and Node entry points share the same scene, compute, and camera logic, extracted from the upstream example. Render a PNG without a browser using `npm run render --prefix examples/compute-texture -- frame.png`.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@drawcall/webgpu",
3
- "version": "0.1.1",
4
- "description": "WebGPU rendering and compute in browsers and GPU-less Node.js",
3
+ "version": "0.2.1",
4
+ "description": "WebGPU rendering and compute in browsers and Node.js, with or without a GPU",
5
5
  "license": "MIT",
6
6
  "type": "module",
7
7
  "exports": {
@@ -37,14 +37,14 @@
37
37
  "sharp": "0.35.5"
38
38
  },
39
39
  "optionalDependencies": {
40
- "@drawcall/webgpu-linux-x64": "0.1.1",
41
- "@drawcall/webgpu-linux-arm64": "0.1.1",
42
- "@drawcall/webgpu-darwin-x64": "0.1.1",
43
- "@drawcall/webgpu-darwin-arm64": "0.1.1",
44
- "@drawcall/webgpu-win32-x64": "0.1.1",
45
- "@drawcall/webgpu-linuxmusl-x64": "0.1.1",
46
- "@drawcall/webgpu-linuxmusl-arm64": "0.1.1",
47
- "@drawcall/webgpu-win32-arm64": "0.1.1"
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"
@@ -1,6 +1,6 @@
1
1
  ---
2
2
  name: webgpu
3
- description: Use @drawcall/webgpu for portable Three.js rendering and WebGPU compute in browsers and GPU-less Node.js, including headless scene export with standard loaders.
3
+ description: Use @drawcall/webgpu for portable Three.js rendering and WebGPU compute in browsers and Node.js with or without a GPU, including headless scene export with standard loaders.
4
4
  ---
5
5
 
6
6
  # Render a scene
@@ -42,10 +42,10 @@ 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
 
49
- Import `requestAdapter` from the package, check its result, then call the adapter’s standard `requestDevice` with the features and limits the workload requires. Use ordinary WGSL, buffers, compute passes, and queue submission. No canvas or loader host is needed. Destroy application-owned devices when finished; do not rely on garbage collection to release GPU resources. Browser and software adapters can expose different capabilities.
49
+ Import `requestAdapter` from the package, check its result, then call the adapter’s standard `requestDevice` with the features and limits the workload requires. Use ordinary WGSL, buffers, compute passes, and queue submission. No canvas or loader host is needed. Destroy application-owned devices when finished; do not rely on garbage collection to release GPU resources. Node prefers a native GPU adapter and falls back to the bundled CPU renderer; request `{ forceFallbackAdapter: true }` to select the CPU renderer explicitly. GPU, browser, and CPU adapters can expose different capabilities. To render Three on a specific adapter, pass a device created from it to `WebGPURenderer`.
50
50
 
51
51
  For combined raw compute and Three rendering, pass the same application-created device to `WebGPURenderer`. For Three TSL compute nodes, use the renderer’s compute API directly. Neither path requires changing Three.js.
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;
@@ -10,10 +11,11 @@ export function getRuntime() {
10
11
  const installed = globals();
11
12
  const loaded = loadNative();
12
13
  try {
13
- const gpu = loaded.native.create(['backend=vulkan']);
14
+ const gpu = loaded.native.create([]);
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);
@@ -26,7 +28,10 @@ export function getRuntime() {
26
28
  }
27
29
  }
28
30
 
29
- /** Select a native software adapter using standard GPURequestAdapterOptions. */
31
+ /**
32
+ * Select an adapter using standard GPURequestAdapterOptions. Native GPUs rank first;
33
+ * the bundled CPU renderer is the fallback adapter (forceFallbackAdapter: true).
34
+ */
30
35
  export async function requestAdapter(options) {
31
36
  return getRuntime().gpu.requestAdapter(options);
32
37
  }
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
+ }