diff --git a/README.md b/README.md index a5d5259..fc8bf64 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ Then in your code import { create, globals } from 'node-webgpu'; Object.assign(globalThis, globals); -globalThis.navigator = { gpu: create([]) }; +const navigator = { gpu: create([]) }; ... @@ -32,7 +32,7 @@ see [example](https://github.com/greggman/node-webgpu/tree/main/example) You can pass dawn options in `create` ```js -globalThis.navigator = { +const navigator = { gpu: create([ "enable-dawn-features=allow_unsafe_apis,dump_shaders,disable_symbol_renaming", ]), @@ -45,8 +45,29 @@ The available options are listed [here](https://dawn.googlesource.com/dawn/+/ref ## Notes +### Lifetime + +The `dawn.node` implementation exists as long as the `navigator` variable +in the examples is in scope, or rather, as long as there is a reference to +the object returned by `create`. As such, if you assign it to a global +variable like this + +```js +globalThis.navigator = { gpu: create([]) }; +``` + +node will not exit because it's still running GPU code in the background. +You can fix that by removing the reference. + +```js +delete globalThis.navigator +``` + +See: https://issues.chromium.org/issues/387965810 + +### What to use this for This package provides a WebGPU implementation it node. That said, if you are making a webpage -and are considering using this for testing, you'd probably be better off using puppeteer. You can +and are considering using this for testing, you'd probably be better off using [puppeteer](https://pptr.dev/). You can find an example of using puppeteer for testing WebGPU in [this repo](https://github.com/greggman/webgpu-debug-helper). This package is for WebGPU in node. It provides WebGPU in node. But, it does not not provide integration @@ -58,9 +79,13 @@ I suspect you could provide many of those with polyfills without changing this r looked into it. What you can do is render to textures and then read them back. You can also run compute shaders -and read their results. +and read their results. See the example linked above. + +## Bugs / Issue -## Bugs +This repo just publishes `dawn.node` from the dawn project [here](https://dawn.googlesource.com/dawn/+/refs/heads/main/src/dawn/node/). +Bugs related to dawn, WebGPU should be filed in the in the +[chromium issue tracker](https://crbug.com/dawn) ## Updating diff --git a/example/example.js b/example/example.js index 5236712..08ecdf1 100644 --- a/example/example.js +++ b/example/example.js @@ -3,7 +3,7 @@ import { PNG } from 'pngjs'; import { create, globals } from 'node-webgpu'; Object.assign(globalThis, globals); -globalThis.navigator = { gpu: create([]) }; +const navigator = { gpu: create([]) }; const adapter = await navigator.gpu?.requestAdapter(); const device = await adapter?.requestDevice(); @@ -89,4 +89,4 @@ for (let y = 0; y < texture.height; y++) { // 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); + diff --git a/test/test.js b/test/test.js index bffcaff..c6ebae1 100644 --- a/test/test.js +++ b/test/test.js @@ -11,5 +11,6 @@ mocha.addFile('./test/tests/basic-tests.js'); await mocha.loadFilesAsync(); mocha.run(failures => { + delete globalThis.navigator; process.exit(failures); });