react-native-webgpu 0.16.0 → 0.16.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/android/build.gradle +11 -1
  2. package/android/src/main/java/com/webgpu/WebGPUView.java +84 -34
  3. package/cpp/jsi/NativeObject.h +45 -5
  4. package/cpp/rnwgpu/RNWebGPUManager.cpp +49 -43
  5. package/cpp/rnwgpu/RNWebGPUManager.h +10 -0
  6. package/cpp/rnwgpu/api/GPUNativeBufferWrapper.h +1 -0
  7. package/lib/commonjs/Canvas.js +51 -0
  8. package/lib/commonjs/Canvas.js.map +1 -1
  9. package/lib/commonjs/external/reanimated/registerWebGPUForReanimated.js.map +1 -1
  10. package/lib/commonjs/install.js +26 -7
  11. package/lib/commonjs/install.js.map +1 -1
  12. package/lib/module/Canvas.js +51 -0
  13. package/lib/module/Canvas.js.map +1 -1
  14. package/lib/module/external/reanimated/registerWebGPUForReanimated.js.map +1 -1
  15. package/lib/module/install.js +26 -7
  16. package/lib/module/install.js.map +1 -1
  17. package/lib/typescript/lib/commonjs/Canvas.d.ts.map +1 -1
  18. package/lib/typescript/lib/commonjs/install.d.ts +10 -7
  19. package/lib/typescript/lib/commonjs/install.d.ts.map +1 -1
  20. package/lib/typescript/lib/module/Canvas.d.ts.map +1 -1
  21. package/lib/typescript/lib/module/install.d.ts.map +1 -1
  22. package/lib/typescript/src/Canvas.d.ts +18 -4
  23. package/lib/typescript/src/Canvas.d.ts.map +1 -1
  24. package/lib/typescript/src/external/reanimated/registerWebGPUForReanimated.d.ts +1 -0
  25. package/lib/typescript/src/external/reanimated/registerWebGPUForReanimated.d.ts.map +1 -1
  26. package/lib/typescript/src/install.d.ts +10 -7
  27. package/lib/typescript/src/install.d.ts.map +1 -1
  28. package/lib/typescript/src/types.d.ts +1 -1
  29. package/package.json +6 -5
  30. package/plugin/build/index.js +2 -1
  31. package/plugin/build/setMinSdkVersion.d.ts +9 -0
  32. package/plugin/build/setMinSdkVersion.js +25 -0
  33. package/plugin/build/withMinSdkVersion.d.ts +11 -0
  34. package/plugin/build/withMinSdkVersion.js +20 -0
  35. package/src/Canvas.tsx +71 -4
  36. package/src/external/reanimated/registerWebGPUForReanimated.ts +4 -0
  37. package/src/install.ts +26 -7
  38. package/src/types.ts +1 -1
package/src/install.ts CHANGED
@@ -29,14 +29,25 @@ const constants = {
29
29
  const gpu: GPU | undefined =
30
30
  typeof RNWebGPU === "undefined" ? undefined : RNWebGPU.gpu;
31
31
 
32
+ // The native half of the worklet serializer (see registerWebGPUForReanimated),
33
+ // read from the main runtime like `gpu` above and captured into the worklet
34
+ // closure: worklets hands a HostObject to every runtime by reference, so it is
35
+ // callable on the target runtime without installing anything there. Its
36
+ // installConstructors() installs the WebGPU class constructors on the runtime
37
+ // that calls it. Undefined on web and node, where the native module is not
38
+ // installed.
39
+ const helpers = globalThis.__webgpuWorkletHelpers;
40
+
32
41
  /**
33
42
  * Install WebGPU on the runtime that calls it.
34
43
  *
35
44
  * The native module sets up WebGPU on the main JS runtime, but worklet
36
45
  * runtimes (Reanimated UI, dedicated worklet runtimes, Vision Camera frame
37
- * processors) start without it: `navigator.gpu` and the flag constants
46
+ * processors) start without it: `navigator.gpu`, the flag constants
38
47
  * (`GPUBufferUsage`, `GPUTextureUsage`, `GPUShaderStage`, `GPUColorWrite`,
39
- * `GPUMapMode`) are all `undefined` there.
48
+ * `GPUMapMode`) and the class constructors (`GPUBuffer`, `GPUTexture`,
49
+ * `GPURenderBundleEncoder`, ...) are all `undefined` there. Without the
50
+ * constructors, `instanceof` against a WebGPU class throws a ReferenceError.
40
51
  *
41
52
  * Call `installWebGPU()` once at the top of a worklet to make them available:
42
53
  *
@@ -65,12 +76,13 @@ const gpu: GPU | undefined =
65
76
  * Observe those on a device created on the main JS thread.
66
77
  *
67
78
  * Everything is captured into the worklet by closure: the constants like a
68
- * shader string would be, and the GPU object through the Worklets custom
79
+ * shader string would be, the GPU object through the Worklets custom
69
80
  * serializer, which installs the native prototypes on the target runtime when
70
- * it crosses. Promises returned by `requestAdapter`/`requestDevice` settle on
71
- * the calling runtime (each runtime gets its own async pump). Calling it on a
72
- * runtime that already has the globals (e.g. the main JS runtime) is a safe
73
- * no-op.
81
+ * it crosses, and the native helper that installs the class constructors,
82
+ * which worklets hands to every runtime by reference. Promises returned by
83
+ * `requestAdapter`/`requestDevice` settle on the calling runtime (each
84
+ * runtime gets its own async pump). Calling it on a runtime that already has
85
+ * the globals (e.g. the main JS runtime) is a safe no-op.
74
86
  */
75
87
  export const installWebGPU = () => {
76
88
  "worklet";
@@ -88,4 +100,11 @@ export const installWebGPU = () => {
88
100
  nav.gpu = gpu;
89
101
  }
90
102
  }
103
+ // The class constructors, so `instanceof` works against any WebGPU class on
104
+ // this runtime (three.js checks `pass instanceof GPURenderBundleEncoder` on
105
+ // every render, for instance). Unboxing an object installs only its own
106
+ // class; this installs the rest and skips the ones already there.
107
+ if (helpers !== undefined) {
108
+ helpers.installConstructors();
109
+ }
91
110
  };
package/src/types.ts CHANGED
@@ -21,7 +21,7 @@ export type RNCanvasContext = GPUCanvasContext & {
21
21
  *
22
22
  * Call this after `queue.submit()` on every runtime: the main JS runtime, the
23
23
  * Reanimated UI runtime, and dedicated worklet runtimes (e.g.
24
- * `createWorkletRuntime` / `runOnRuntime`, or a Vision Camera frame
24
+ * `createWorkletRuntime` / `scheduleOnRuntime`, or a Vision Camera frame
25
25
  * processor). It runs synchronously on the calling thread, so the frame is
26
26
  * presented from whichever thread did the rendering.
27
27
  */