Skip to content

Commit

Permalink
Deploying to gh-pages from @ 0466b65 🚀
Browse files Browse the repository at this point in the history
  • Loading branch information
greggman committed Jan 6, 2025
1 parent 7cedebe commit 7c1f3cb
Show file tree
Hide file tree
Showing 16 changed files with 180 additions and 19 deletions.
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@
.DS_Store
node_modules

# --cut-here--
dist
# --cut-here--

26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,28 @@ Object.assign(globalThis, globals);
globalThis.navigator = { gpu: create([]) };

...

// do some webgpu
const device = await(await navigator.gpu.requestAdapter()).requestDevice();
...
```

see [example](https://github.com/greggman/node-webgpu/tree/main/example)

You can pass dawn options in `create`

```js
let navigator = {
gpu: create([
"enable-dawn-features=allow_unsafe_apis,dump_shaders,disable_symbol_renaming",
]),
};
```

There is both `enable-dawn-features=comma,separated,toggles` and `disable-dawn-features=comma,separated,toggles`.

The available options are listed [here](https://dawn.googlesource.com/dawn/+/refs/heads/chromium-gpu-experimental/src/dawn_native/Toggles.cpp)

## Notes

This package provides a WebGPU implementation it node. That said, if you are making a webpage
Expand Down Expand Up @@ -76,6 +96,8 @@ I've tested with Visual Studio Community Edition 2022
Further you must have [cmake installed](https://cmake.org/download/)
and either in your path or at it's standard place of `C:\Program Files\CMake`

You must have `go` installed. Get it at https://go.dev/

And you must have `node.js` installed, at least version 18.
I recommend using [nvm-windows](https://github.com/coreybutler/nvm-windows) to install it
as it makes it easy to switch version
Expand All @@ -88,6 +110,8 @@ XCode installed and its command line tools
Further you must have [cmake installed](https://cmake.org/download/)
and either in your path or at it's standard place of `/Applications/CMake.app`

You must have `go` installed. Get it at https://go.dev/

And you must have `node.js` installed, at least version 18.
I recommend using [nvm](https://github.com/nvm-sh/nvm) to install it
as it makes it easy to switch versions.
Expand All @@ -101,6 +125,8 @@ the following dependencies
sudo apt-get install cmake libxrandr-dev libxinerama-dev libxcursor-dev mesa-common-dev libx11-xcb-dev pkg-config nodejs npm
```

You must have `go` installed. Get it at https://go.dev/

And you must have `node.js` installed, at least version 18.
I recommend using [nvm](https://github.com/nvm-sh/nvm) to install it
as it makes it easy to switch versions.
Expand Down
1 change: 1 addition & 0 deletions build/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ async function buildDawnNode() {
...addElemIf(!isWin, '-GNinja'),
'-DDAWN_BUILD_NODE_BINDINGS=1',
'-DDAWN_USE_X11=OFF',
'-DCMAKE_BUILD_TYPE=Release',
...addElemIf(isWin, '-DCMAKE_SYSTEM_VERSION=10.0.26100.0'),
...addElemIf(isMac, '-DCMAKE_OSX_SYSROOT=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk'),
]);
Expand Down
Binary file added dist/darwin-arm64.dawn.node
Binary file not shown.
Binary file added dist/darwin-x64.dawn.node
Binary file not shown.
Binary file added dist/linux-x64.dawn.node
Binary file not shown.
Binary file added dist/win32-x64.dawn.node
Binary file not shown.
4 changes: 4 additions & 0 deletions example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.pyc
.DS_Store
node_modules
output.png
5 changes: 5 additions & 0 deletions example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Example for node-webgpu

* cd to this
* `npm ci`
* `node example.js`
92 changes: 92 additions & 0 deletions example/example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import fs from 'node:fs';
import { PNG } from 'pngjs';
import { create, globals } from 'node-webgpu';

Object.assign(globalThis, globals);
globalThis.navigator = { gpu: create([]) };

const adapter = await navigator.gpu?.requestAdapter();
const device = await adapter?.requestDevice();

const module = device.createShaderModule({
code: `
@vertex fn vs(
@builtin(vertex_index) vertexIndex : u32
) -> @builtin(position) vec4f {
let pos = array(
vec2f( 0.0, 0.5),
vec2f(-0.5, -0.5),
vec2f( 0.5, -0.5),
);
return vec4f(pos[vertexIndex], 0.0, 1.0);
}
@fragment fn fs() -> @location(0) vec4f {
return vec4f(1, 0, 0, 1);
}
`,
});

const pipeline = device.createRenderPipeline({
layout: 'auto',
vertex: { module },
fragment: { module, targets: [{ format: 'rgba8unorm' }] },
});

const texture = device.createTexture({
format: 'rgba8unorm',
usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.COPY_SRC,
size: [256, 256],
});

const align = (v, alignment) => Math.ceil(v / alignment) * alignment;

const bytesPerRow = align(texture.width * 4, 256);
const buffer = device.createBuffer({
size: bytesPerRow * texture.height,
usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ,
});

const encoder = device.createCommandEncoder();
const pass = encoder.beginRenderPass({
colorAttachments: [
{
view: texture.createView(),
clearValue: [0.3, 0.3, 0.3, 1],
loadOp: 'clear',
storeOp: 'store',
},
],
});
pass.setPipeline(pipeline);
pass.draw(3);
pass.end();
encoder.copyTextureToBuffer(
{ texture },
{ buffer, bytesPerRow },
[ texture.width, texture.height ],
);
const commandBuffer = encoder.finish();
device.queue.submit([commandBuffer]);

await buffer.mapAsync(GPUMapMode.READ);
const rawPixelData = buffer.getMappedRange();

const png = new PNG({
width: texture.width,
height: texture.height,
filterType: -1,
});

const dstBytesPerRow = texture.width * 4;
for (let y = 0; y < texture.height; y++) {
const dst = (texture.width * y) * 4;
const src = (y * bytesPerRow);
png.data.set(new Uint8Array(rawPixelData, src, dstBytesPerRow), dst);
}

// Write the PNG to a file
fs.writeFileSync('output.png', PNG.sync.write(png, {colorType: 6}));
console.log('PNG file has been saved as output.png');
process.exit(0);
30 changes: 30 additions & 0 deletions example/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions example/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "example",
"version": "1.0.0",
"description": "example using node-webgpu",
"main": "example.js",
"type": "module",
"scripts": {
"start": "node example.js"
},
"author": "",
"license": "MIT",
"dependencies": {
"node-webgpu": "^0.0.6",
"pngjs": "^7.0.0"
}
}
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import { createRequire } from 'module';
const require = createRequire(import.meta.url);

const __dirname = dirname(fileURLToPath(import.meta.url));
const dawnNodePath = join(__dirname, `${process.platform}-${process.arch}.node`);
const dawnNodePath = join(__dirname, 'dist', `${process.platform}-${process.arch}.dawn.node`);
const { create, globals } = require(dawnNodePath);
export { create, globals }
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-webgpu",
"version": "0.0.4",
"version": "0.0.8",
"description": "WebGPU for node",
"main": "index.js",
"type": "module",
Expand Down
13 changes: 1 addition & 12 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
import Mocha from 'mocha';

import { createRequire } from 'module';
const require = createRequire(import.meta.url);

const isDev = process.argv[2] !== 'dev';
const isWin = process.platform === 'win32';
const dawnNodePath = isDev
? isWin
? `${process.cwd()}/third_party/dawn/out/cmake-release/Debug/dawn.node`
: `${process.cwd()}/third_party/dawn/out/cmake-release/dawn.node`
: `${process.cwd()}/dist/${process.platform}-${process.arch}.node`;

const { create, globals } = require(dawnNodePath);
import { create, globals } from '../index.js';

Object.assign(globalThis, globals);
globalThis.navigator = { gpu: create([]) };
Expand Down

0 comments on commit 7c1f3cb

Please sign in to comment.