GPUDeviceProvider
Context provider that requests a device and gates children until ready.
GPUDeviceProvider requests a GPUDevice once and shares it through React context, rendering children only when device is non-null. Wrap your scene root so every child can read the shared device with useMainDevice instead of requesting its own.
Props
interface DeviceProviderProps {
?: ;
?: GPURequestAdapterOptions;
?: GPUDeviceDescriptor;
}Usage
import { } from "react";
import { } from "react-native";
import {
,
,
,
,
} from "react-native-webgpu";
function () {
const = ();
// Never null here: the provider renders its children once the device is ready
const { } = ();
(() => {
const = .!.("webgpu")!;
const = . as HTMLCanvasElement;
const = ..();
. = . * .();
. = . * .();
.({ : !, });
// ... render and present
}, [, ]);
return < ={} ={{ : 1 }} />;
}
export function () {
return (
< ={{ : ["shader-f16"] }}>
< />
</>
);
}Request features through deviceDescriptor: you cannot add features to an existing device later. Native buffer import needs none; it is enabled by default.
While the device is loading, the provider renders nothing. To show a loading state, render it next to the provider, and hide it once your scene has mounted.
useMainDevice
useMainDevice is the hook that consumes this provider's context. It returns the same { adapter, device } the provider created, so any descendant can share that one device instead of requesting its own. If you are not using a provider, useDevice requests a device directly instead.
function () {
const { , } = ();
// Read adapter info / feature list, or pass `device` to Three.js, overlays, etc.
return null;
}Must be inside the provider
useMainDevice throws No DeviceContext found. when called outside a GPUDeviceProvider. Because the provider renders null until the device is ready, any child that calls it only mounts once device is non-null.
See also
- React APIs: hooks for requesting and sharing a device
- Platform Guide: threading and
device.lost - Native Extensions: camera and video import