You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
React Native WebGPU exposes Dawn's `SharedTextureMemory` so you can import a native pixel surface (an `IOSurface`-backed `CVPixelBuffer` on iOS, an `AHardwareBuffer` on Android) as a sampleable `GPUTexture` without copying pixels through the CPU. This is the path you want for camera frames, video frames, or anything coming out of a hardware producer.
228
228
229
-
We expose a single umbrella feature name, `"rnwebgpu/shared-texture-memory"`. Request it at device creation.
229
+
Like `importExternalTexture` on the web, this is **enabled by default**, there is nothing to request at device creation. The only thing to check is that the device supports it before importing. It always does on iOS/macOS; it can be missing on some Android drivers and emulators.
// On by default when supported; this is the only check you need.
238
+
if (!device.features.has("rnwebgpu/native-texture"asGPUFeatureName)) {
239
+
return; // rare: some Android drivers/emulators can't import native surfaces
240
+
}
239
241
240
-
// `frame` here is a VideoFrame whose .handle is the native surface
241
-
// (IOSurfaceRef / AHardwareBuffer*). VideoFrames are produced by helpers
242
+
// `frame` here is a NativeVideoFrame whose .handle is the native surface
243
+
// (IOSurfaceRef / AHardwareBuffer*). NativeVideoFrames are produced by helpers
242
244
// like RNWebGPU.createVideoPlayer or RNWebGPU.createTestVideoFrame, or by
243
245
// any third-party module that hands you a compatible native pointer.
244
246
const memory =device.importSharedTextureMemory({
@@ -257,6 +259,53 @@ frame.release();
257
259
258
260
`beginAccess`/`endAccess` bracket the GPU's read window on the shared surface. Pass `initialized: true` when the producer has already written meaningful pixels (the typical video/camera case) and `false` when the next pass will fully overwrite the texture.
259
261
262
+
### Importing External Textures
263
+
264
+
`GPUDevice.importExternalTexture` is the higher-level path for sampling a native surface. You hand it a `NativeVideoFrame` and get back a `GPUExternalTexture` that you bind as a `texture_external` and read with `textureSampleBaseClampToEdge`. It does two things for you on top of `SharedTextureMemory`:
265
+
266
+
-**Color conversion.** Camera and video surfaces are usually biplanar YUV (NV12), not RGB. An external texture carries the YUVβRGB matrix and the source/destination color-space transfer functions, so on the supported paths the sampler returns ready-to-use RGB in hardware. With raw `SharedTextureMemory` you would sample the luma/chroma planes and do that conversion by hand in WGSL. This is the main reason to prefer it for camera and video frames.
267
+
-**Lifecycle.** It owns the `SharedTextureMemory` + `createTexture` + `beginAccess`/`endAccess` sequence internally, so you just import the frame and `destroy()` the result.
268
+
269
+
It builds on the same default-on capability as Shared Texture Memory above, so feature-detect the device the same way before importing.
270
+
271
+
> **Android note:** the hardware YUVβRGB conversion is fully automatic on iOS (NV12 `IOSurface`). On Android, camera frames arrive as an _opaque_ YCbCr `AHardwareBuffer`, and Dawn's Vulkan path forces an identity (`RGB_IDENTITY`) sampler conversion, so the external sample comes back as raw `[Y, Cb, Cr]`. You still get the zero-copy import and the rotation/mirror transform, but you need to apply the YUVβRGB matrix yourself in the shader. See the `CAMERA_PRELUDE` in the [VisionCamera example](/apps/example/src/VisionCamera/shaders.ts) for a ready-made BT.709 decode.
// ... encode a pass that samples `externalTexture`, then:
295
+
device.queue.submit([encoder.finish()]);
296
+
297
+
// Release the surface's access window right after the submit that sampled it.
298
+
externalTexture.destroy();
299
+
context.present();
300
+
};
301
+
```
302
+
303
+
Camera frames arrive in the sensor's native orientation, so `importExternalTexture` also accepts non-spec `rotation` (`0` | `90` | `180` | `270`, in degrees) and `mirrored` (horizontal flip) options. Dawn bakes them into the sampling transform, so the shader sees an upright frame. They map directly onto VisionCamera's `frame.orientation` / `frame.isMirrored`.
304
+
305
+
#### Calling `destroy()`
306
+
307
+
A `GPUExternalTexture` keeps an open access window on the underlying native surface until the wrapper is destroyed. On the Web `importExternalTexture` is core and the lifetime is handled for you; here the window is tied to the JavaScript object's lifetime. Call `externalTexture.destroy()` right after the `queue.submit()` that sampled it (never before) to release the surface back to its producer immediately. `destroy()` is idempotent, and the surface is also released when the object is garbage-collected, but relying on GC can starve a producer's buffer pool (e.g. an `AVPlayer`'s recycled `IOSurface`s) and pile up GPU resources, so prefer the explicit call in a render loop.
308
+
260
309
### Reanimated Integration
261
310
262
311
React Native WebGPU supports running WebGPU rendering on the UI thread using [React Native Reanimated](https://docs.swmansion.com/react-native-reanimated/) and [React Native Worklets](https://github.com/margelo/react-native-worklets).
0 commit comments