Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions apps/example/src/VisionCamera/VisionCamera.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,13 @@ const CameraView = () => {
}, []);
const device = gpu?.device ?? null;
const adapter = gpu?.adapter ?? null;
// Capture the RNWebGPU singleton into a local so the frame-processor worklet
// closes over it. RNWebGPU is a registered, boxable WebGPU NativeObject, so
// the Worklets custom serializer ships it across the worklet boundary the same
// way it does `device` (it is NOT installed as a global on worklet runtimes).
// This is what lets us call the interop factory off RNWebGPU, where the native
// platform context already lives, instead of off `device`.
const rnwgpu = RNWebGPU;
const devices = useCameraDevices();
// Pick back camera if available, otherwise front, otherwise anything. The
// iOS simulator returns an empty list since there are no cameras, in which
Expand Down Expand Up @@ -443,11 +450,12 @@ const CameraView = () => {
try {
let videoFrame;
try {
// Call createVideoFrameFromNativeBuffer on the device, not on the
// RNWebGPU global — `device` is already box-able across worklet
// runtimes via the WebGPU custom serializer (proven by the
// Reanimated demo); RNWebGPU is a main-runtime-only global.
videoFrame = device.createVideoFrameFromNativeBuffer(
// Call createVideoFrameFromNativeBuffer on the captured RNWebGPU
// singleton (see `rnwgpu` above). It rides into this worklet via the
// WebGPU custom serializer, same as `device`, so the factory can live
// on RNWebGPU where the native platform context already is — no
// GPUDevice-level workaround and no PlatformContext global singleton.
videoFrame = rnwgpu.createVideoFrameFromNativeBuffer(
nativeBuffer.pointer,
);
} catch (e) {
Expand Down
8 changes: 0 additions & 8 deletions packages/webgpu/cpp/rnwgpu/PlatformContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,6 @@ class PlatformContext {
PlatformContext() = default;
virtual ~PlatformContext() = default;

// Singleton-style accessor so leaf classes (e.g. GPUDevice) can reach the
// platform context without threading it through every constructor. Set by
// RNWebGPUManager at startup.
static std::shared_ptr<PlatformContext> &global() {
static std::shared_ptr<PlatformContext> instance;
return instance;
}

virtual wgpu::Surface makeSurface(wgpu::Instance instance, void *surface,
int width, int height) = 0;
virtual ImageData createImageBitmap(std::string blobId, double offset,
Expand Down
5 changes: 0 additions & 5 deletions packages/webgpu/cpp/rnwgpu/RNWebGPUManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,6 @@ RNWebGPUManager::RNWebGPUManager(
// Register main runtime for RuntimeAwareCache
BaseRuntimeAwareCache::setMainJsRuntime(_jsRuntime);

// Expose the platform context for leaf classes that need it (e.g.
// GPUDevice::createVideoFrameFromNativeBuffer) without threading it through
// every constructor.
PlatformContext::global() = _platformContext;

auto gpu = std::make_shared<GPU>(*_jsRuntime);
auto rnWebGPU =
std::make_shared<RNWebGPU>(gpu, _platformContext, _jsCallInvoker);
Expand Down
14 changes: 0 additions & 14 deletions packages/webgpu/cpp/rnwgpu/api/GPUDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

#include "Convertors.h"
#include "JSIConverter.h"
#include "PlatformContext.h"

#include "GPUFeatures.h"
#include "GPUInternalError.h"
Expand Down Expand Up @@ -242,19 +241,6 @@ std::shared_ptr<GPUExternalTexture> GPUDevice::importExternalTexture(
return GPUExternalTexture::Create(_instance, std::move(descriptor));
}

std::shared_ptr<VideoFrame>
GPUDevice::createVideoFrameFromNativeBuffer(uint64_t pointer) {
auto platformContext = PlatformContext::global();
if (!platformContext) {
throw std::runtime_error(
"GPUDevice::createVideoFrameFromNativeBuffer(): PlatformContext is "
"not initialized");
}
auto handle =
platformContext->wrapNativeBuffer(reinterpret_cast<void *>(pointer));
return std::make_shared<VideoFrame>(std::move(handle));
}

std::shared_ptr<GPUSharedTextureMemory> GPUDevice::importSharedTextureMemory(
std::shared_ptr<GPUSharedTextureMemoryDescriptor> descriptor) {
if (!descriptor || descriptor->handle == nullptr) {
Expand Down
9 changes: 0 additions & 9 deletions packages/webgpu/cpp/rnwgpu/api/GPUDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
#include "GPUTexture.h"
#include "GPUTextureDescriptor.h"
#include "GPUUncapturedErrorEvent.h"
#include "VideoFrame.h"

namespace rnwgpu {

Expand Down Expand Up @@ -121,12 +120,6 @@ class GPUDevice : public NativeObject<GPUDevice> {
std::shared_ptr<GPUExternalTextureDescriptor> descriptor);
std::shared_ptr<GPUSharedTextureMemory> importSharedTextureMemory(
std::shared_ptr<GPUSharedTextureMemoryDescriptor> descriptor);
// Wrap a CVPixelBufferRef / AHardwareBuffer* pointer (as a BigInt) into a
// VideoFrame. Mirrors RNWebGPU.createVideoFrameFromNativeBuffer but is
// reachable from worklet runtimes since GPUDevice is already serialized
// across the worklet boundary via the WebGPU custom serializer.
std::shared_ptr<VideoFrame>
createVideoFrameFromNativeBuffer(uint64_t pointer);
std::shared_ptr<GPUBindGroupLayout> createBindGroupLayout(
std::shared_ptr<GPUBindGroupLayoutDescriptor> descriptor);
std::shared_ptr<GPUPipelineLayout>
Expand Down Expand Up @@ -182,8 +175,6 @@ class GPUDevice : public NativeObject<GPUDevice> {
&GPUDevice::importExternalTexture);
installMethod(runtime, prototype, "importSharedTextureMemory",
&GPUDevice::importSharedTextureMemory);
installMethod(runtime, prototype, "createVideoFrameFromNativeBuffer",
&GPUDevice::createVideoFrameFromNativeBuffer);
installMethod(runtime, prototype, "createBindGroupLayout",
&GPUDevice::createBindGroupLayout);
installMethod(runtime, prototype, "createPipelineLayout",
Expand Down
4 changes: 0 additions & 4 deletions packages/webgpu/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,6 @@ declare global {
importSharedTextureMemory(
descriptor: GPUSharedTextureMemoryDescriptor,
): GPUSharedTextureMemory;
// Wrap a NativeBuffer.pointer into a NativeVideoFrame. Reachable from
// worklet runtimes (e.g. Vision Camera frame processors) because GPUDevice
// is serialized across worklet boundaries via the WebGPU custom serializer.
createVideoFrameFromNativeBuffer(pointer: bigint): NativeVideoFrame;
}

// Non-spec extension: camera frames arrive in the sensor's native
Expand Down
Loading