installWebGPU
Install navigator.gpu and the WebGPU flag constants on a worklet runtime.
On a worklet runtime, neither navigator.gpu nor the WebGPU flag constants are available by default.
Call installWebGPU() once at the top of a worklet to install the needed globals for that runtime: navigator.gpu plus GPUBufferUsage, GPUTextureUsage, GPUShaderStage, GPUColorWrite, and GPUMapMode.
On the main JS thread these are already installed when the native module loads; calling installWebGPU() there is a safe no-op.
Usage
UI thread worklet
import { } from "react-native-webgpu";
import { } from "react-native-worklets";
const = (
: GPUDevice,
: ,
: GPURenderPipeline,
) => {
"worklet";
();
const = .();
const = .({
: [{
: .().(),
: [0, 0, 0, 1],
: "clear",
: "store",
}],
});
.();
.(3);
.();
..([.()]);
.();
};
// Create device on the main thread, pass handles to the worklet
()(, , );Dedicated worklet runtime
import { , } from "react-native-worklets";
import { } from "react-native-webgpu";
const = ({ : "gpu" });
const = (: GPUDevice) => {
"worklet";
();
const = .({
: 256,
: . | .,
});
// …
};
(, )();navigator.gpu on a worklet runtime
After installWebGPU(), read navigator through globalThis.navigator inside a worklet. The Worklets babel plugin does not currently treat a bare navigator as a known global, so without the prefix it captures the main runtime's navigator object by closure instead of reading the one installed on the worklet runtime. This is fixed upstream (software-mansion/react-native-reanimated#10364); once you are on a version of react-native-worklets that includes it, a bare navigator works and the prefix is no longer needed.
import { } from "react-native-webgpu";
import { } from "react-native-worklets";
(() => {
"worklet";
();
const = ...();
...().(() => {
// …
});
})();If you create a device on the worklet runtime, beware that spontaneous device events are only delivered for devices created on the main JS runtime:
device.lostread on a worklet runtime returns a promise that never settles, unless the device is already lost at that point, in which case it resolves normally.uncapturederrorlisteners registered on a device created on a worklet runtime never fire.
If you need to observe device loss or uncaptured errors, create the device on the main JS thread, attach the handlers there, and pass the device into the worklet.