react-native-webgpu 0.22.0 → 0.24.0

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 (165) hide show
  1. package/Package.swift +18 -129
  2. package/README.md +15 -2
  3. package/android/CMakeLists.txt +2 -1
  4. package/android/cpp/AndroidAnimationFrameSource.h +136 -0
  5. package/android/cpp/AndroidPlatformContext.h +65 -9
  6. package/android/cpp/cpp-adapter.cpp +148 -2
  7. package/android/src/main/java/com/webgpu/WebGPUModule.java +24 -7
  8. package/android/src/main/java/com/webgpu/WebGPUNativeBufferView.java +370 -0
  9. package/android/src/main/java/com/webgpu/WebGPUNativeBufferViewManager.java +56 -0
  10. package/android/src/main/java/com/webgpu/WebGPUTextureView.java +21 -0
  11. package/android/src/main/java/com/webgpu/WebGPUVideoPlayer.java +2 -2
  12. package/android/src/main/java/com/webgpu/WebGPUView.java +19 -3
  13. package/android/src/main/java/com/webgpu/WebGPUViewPackage.java +2 -2
  14. package/android/src/newarch/com/webgpu/WebGPUNativeBufferViewManagerSpec.java +24 -0
  15. package/apple/AppleAnimationFrameSource.mm +208 -0
  16. package/apple/ApplePlatformContext.h +8 -1
  17. package/apple/ApplePlatformContext.mm +75 -2
  18. package/apple/NativeBufferFenceWait.mm +88 -0
  19. package/apple/WebGPUNativeBufferView.h +15 -0
  20. package/apple/WebGPUNativeBufferView.mm +200 -0
  21. package/binaries.json +53 -105
  22. package/cpp/dawn/common/ExternalTextureParams.h +3 -3
  23. package/cpp/dawn/include/dawn/dawn_proc.h +53 -0
  24. package/cpp/dawn/include/dawn/dawn_proc_table.h +329 -0
  25. package/cpp/dawn/include/dawn/dawn_thread_dispatch_proc.h +47 -0
  26. package/cpp/dawn/include/dawn/dawn_version.h +51 -0
  27. package/cpp/dawn/include/dawn/native/D3D11Backend.h +65 -0
  28. package/cpp/dawn/include/dawn/native/D3D12Backend.h +104 -0
  29. package/cpp/dawn/include/dawn/native/D3DBackend.h +56 -0
  30. package/cpp/dawn/include/dawn/native/DawnNative.h +375 -0
  31. package/cpp/dawn/include/dawn/native/MetalBackend.h +67 -0
  32. package/cpp/dawn/include/dawn/native/NullBackend.h +39 -0
  33. package/cpp/dawn/include/dawn/native/OpenGLBackend.h +89 -0
  34. package/cpp/dawn/include/dawn/native/VulkanBackend.h +199 -0
  35. package/cpp/dawn/include/dawn/native/WebGPUBackend.h +49 -0
  36. package/cpp/dawn/include/dawn/native/dawn_native_export.h +49 -0
  37. package/cpp/dawn/include/dawn/platform/DawnPlatform.h +210 -0
  38. package/cpp/dawn/include/dawn/platform/dawn_platform_export.h +49 -0
  39. package/cpp/dawn/include/dawn/replay/Replay.h +99 -0
  40. package/cpp/dawn/include/dawn/replay/dawn_replay_export.h +49 -0
  41. package/cpp/dawn/include/dawn/webgpu.h +5119 -0
  42. package/cpp/dawn/include/dawn/webgpu_cpp.h +10506 -0
  43. package/cpp/dawn/include/dawn/webgpu_cpp_print.h +2897 -0
  44. package/cpp/dawn/include/webgpu/webgpu.h +33 -0
  45. package/cpp/dawn/include/webgpu/webgpu_cpp.h +33 -0
  46. package/cpp/dawn/include/webgpu/webgpu_cpp_chained_struct.h +57 -0
  47. package/cpp/dawn/include/webgpu/webgpu_cpp_print.h +33 -0
  48. package/cpp/dawn/include/webgpu/webgpu_enum_class_bitmasks.h +163 -0
  49. package/cpp/dawn/include/webgpu/webgpu_glfw.h +88 -0
  50. package/cpp/rnwgpu/AnimationFrames.cpp +576 -0
  51. package/cpp/rnwgpu/AnimationFrames.h +233 -0
  52. package/cpp/rnwgpu/NativeBufferViewRegistry.cpp +396 -0
  53. package/cpp/rnwgpu/NativeBufferViewRegistry.h +163 -0
  54. package/cpp/rnwgpu/PlatformContext.h +21 -1
  55. package/cpp/rnwgpu/RNWebGPUManager.cpp +17 -4
  56. package/cpp/rnwgpu/SurfaceRegistry.h +2 -2
  57. package/cpp/rnwgpu/api/GPUAdapter.cpp +2 -2
  58. package/cpp/rnwgpu/api/GPUDevice.cpp +16 -131
  59. package/cpp/rnwgpu/api/GPUDevice.h +0 -18
  60. package/cpp/rnwgpu/api/GPUExternalTexture.cpp +35 -3
  61. package/cpp/rnwgpu/api/GPUExternalTexture.h +4 -4
  62. package/cpp/rnwgpu/api/GPUQueue.cpp +1 -1
  63. package/cpp/rnwgpu/api/GPUQueue.h +1 -1
  64. package/cpp/rnwgpu/api/GPUSharedFence.h +1 -0
  65. package/cpp/rnwgpu/api/GPUSharedTextureMemory.cpp +11 -5
  66. package/cpp/rnwgpu/api/GPUSharedTextureMemory.h +21 -2
  67. package/cpp/rnwgpu/api/GPUTexture.h +18 -0
  68. package/cpp/rnwgpu/api/NativeFrameFeatures.h +1 -1
  69. package/cpp/rnwgpu/api/NativeFrameImport.cpp +20 -17
  70. package/cpp/rnwgpu/api/NativeFrameImport.h +31 -20
  71. package/cpp/rnwgpu/api/RNWebGPU.h +206 -7
  72. package/cpp/rnwgpu/api/VideoFrame.h +5 -4
  73. package/cpp/rnwgpu/api/VideoFrameBlit.h +2 -2
  74. package/cpp/rnwgpu/api/descriptors/GPUExternalTextureDescriptor.h +21 -14
  75. package/cpp/rnwgpu/api/descriptors/GPUImageCopyExternalImage.h +1 -1
  76. package/cpp/rnwgpu/api/descriptors/GPUSharedTextureMemoryDescriptor.h +17 -9
  77. package/cpp/rnwgpu/async/RuntimeContext.cpp +21 -4
  78. package/cpp/rnwgpu/async/RuntimeContext.h +8 -0
  79. package/lib/commonjs/NativeBufferView.js +131 -0
  80. package/lib/commonjs/NativeBufferView.js.map +1 -0
  81. package/lib/commonjs/NativeBufferViewNativeComponent.js +10 -0
  82. package/lib/commonjs/NativeBufferViewNativeComponent.js.map +1 -0
  83. package/lib/commonjs/animationFrame.js +91 -0
  84. package/lib/commonjs/animationFrame.js.map +1 -0
  85. package/lib/commonjs/index.js +16 -1
  86. package/lib/commonjs/index.js.map +1 -1
  87. package/lib/commonjs/main/index.js +12 -0
  88. package/lib/commonjs/main/index.js.map +1 -1
  89. package/lib/commonjs/mock.js +30 -1
  90. package/lib/commonjs/mock.js.map +1 -1
  91. package/lib/commonjs/video.js +21 -7
  92. package/lib/commonjs/video.js.map +1 -1
  93. package/lib/module/NativeBufferView.js +122 -0
  94. package/lib/module/NativeBufferView.js.map +1 -0
  95. package/lib/module/NativeBufferViewNativeComponent.js +4 -0
  96. package/lib/module/NativeBufferViewNativeComponent.js.map +1 -0
  97. package/lib/module/animationFrame.js +83 -0
  98. package/lib/module/animationFrame.js.map +1 -0
  99. package/lib/module/index.js +1 -0
  100. package/lib/module/index.js.map +1 -1
  101. package/lib/module/main/index.js +1 -0
  102. package/lib/module/main/index.js.map +1 -1
  103. package/lib/module/mock.js +21 -0
  104. package/lib/module/mock.js.map +1 -1
  105. package/lib/module/video.js +17 -4
  106. package/lib/module/video.js.map +1 -1
  107. package/lib/typescript/lib/commonjs/NativeBufferView.d.ts +48 -0
  108. package/lib/typescript/lib/commonjs/NativeBufferView.d.ts.map +1 -0
  109. package/lib/typescript/lib/commonjs/NativeBufferViewNativeComponent.d.ts +5 -0
  110. package/lib/typescript/lib/commonjs/NativeBufferViewNativeComponent.d.ts.map +1 -0
  111. package/lib/typescript/lib/commonjs/animationFrame.d.ts +54 -0
  112. package/lib/typescript/lib/commonjs/animationFrame.d.ts.map +1 -0
  113. package/lib/typescript/lib/commonjs/index.d.ts +2 -0
  114. package/lib/typescript/lib/commonjs/mock.d.ts +12 -0
  115. package/lib/typescript/lib/commonjs/mock.d.ts.map +1 -1
  116. package/lib/typescript/lib/commonjs/video.d.ts +9 -4
  117. package/lib/typescript/lib/commonjs/video.d.ts.map +1 -1
  118. package/lib/typescript/lib/module/NativeBufferView.d.ts +14 -0
  119. package/lib/typescript/lib/module/NativeBufferView.d.ts.map +1 -0
  120. package/lib/typescript/lib/module/NativeBufferViewNativeComponent.d.ts +3 -0
  121. package/lib/typescript/lib/module/NativeBufferViewNativeComponent.d.ts.map +1 -0
  122. package/lib/typescript/lib/module/animationFrame.d.ts +3 -0
  123. package/lib/typescript/lib/module/animationFrame.d.ts.map +1 -0
  124. package/lib/typescript/lib/module/index.d.ts +1 -0
  125. package/lib/typescript/lib/module/main/index.d.ts +1 -0
  126. package/lib/typescript/lib/module/mock.d.ts +14 -0
  127. package/lib/typescript/lib/module/mock.d.ts.map +1 -1
  128. package/lib/typescript/lib/module/video.d.ts +4 -3
  129. package/lib/typescript/lib/module/video.d.ts.map +1 -1
  130. package/lib/typescript/react-native.config.d.ts +1 -0
  131. package/lib/typescript/src/NativeBufferView.d.ts +99 -0
  132. package/lib/typescript/src/NativeBufferView.d.ts.map +1 -0
  133. package/lib/typescript/src/NativeBufferViewNativeComponent.d.ts +13 -0
  134. package/lib/typescript/src/NativeBufferViewNativeComponent.d.ts.map +1 -0
  135. package/lib/typescript/src/animationFrame.d.ts +59 -0
  136. package/lib/typescript/src/animationFrame.d.ts.map +1 -0
  137. package/lib/typescript/src/index.d.ts +18 -8
  138. package/lib/typescript/src/index.d.ts.map +1 -1
  139. package/lib/typescript/src/main/index.d.ts +1 -0
  140. package/lib/typescript/src/main/index.d.ts.map +1 -1
  141. package/lib/typescript/src/mock.d.ts +11 -0
  142. package/lib/typescript/src/mock.d.ts.map +1 -1
  143. package/lib/typescript/src/types.d.ts +33 -32
  144. package/lib/typescript/src/types.d.ts.map +1 -1
  145. package/lib/typescript/src/video.d.ts +18 -6
  146. package/lib/typescript/src/video.d.ts.map +1 -1
  147. package/package.json +16 -6
  148. package/react-native-webgpu.podspec +47 -79
  149. package/react-native.config.js +9 -6
  150. package/scripts/apple-binaries-utils.sh +280 -0
  151. package/scripts/prepare-apple-binaries.sh +309 -0
  152. package/scripts/spm-plugin.js +130 -0
  153. package/src/NativeBufferView.tsx +199 -0
  154. package/src/NativeBufferViewNativeComponent.ts +20 -0
  155. package/src/animationFrame.ts +82 -0
  156. package/src/index.tsx +48 -42
  157. package/src/main/index.tsx +1 -0
  158. package/src/mock.ts +35 -0
  159. package/src/types.ts +62 -88
  160. package/src/video.ts +32 -12
  161. package/cpp/rnwgpu/api/GPUNativeBufferExternalTexture.h +0 -60
  162. package/cpp/rnwgpu/api/GPUNativeBufferTexture.h +0 -52
  163. package/cpp/rnwgpu/api/GPUNativeBufferWrapper.cpp +0 -144
  164. package/cpp/rnwgpu/api/GPUNativeBufferWrapper.h +0 -143
  165. package/cpp/rnwgpu/api/descriptors/GPUNativeBufferTextureDescriptor.h +0 -55
@@ -0,0 +1,163 @@
1
+ #pragma once
2
+
3
+ #include <cstdint>
4
+ #include <functional>
5
+ #include <memory>
6
+ #include <mutex>
7
+ #include <unordered_map>
8
+
9
+ #include "webgpu/webgpu_cpp.h"
10
+
11
+ #include "PlatformContext.h"
12
+
13
+ namespace rnwgpu {
14
+
15
+ // The color space a NativeBufferView displays a buffer in. The buffer holds
16
+ // premultiplied, gamma-encoded values in these primaries. rgba16float gives
17
+ // more precision, not extended range: the view shows values in [0, 1] and
18
+ // clamps the rest.
19
+ enum class NativeBufferColorSpace { SRGB, DisplayP3 };
20
+
21
+ // A native buffer handed to a NativeBufferView: an AHardwareBuffer* (Android)
22
+ // or an IOSurfaceRef (Apple platforms), with a reference of its own, released
23
+ // when the last holder (the registry, a pending fence wait, the view) drops
24
+ // it. The compositor takes references of its own on what it displays
25
+ // (Bitmap.wrapHardwareBuffer, CALayer.contents), so dropping this one while
26
+ // the buffer is on screen is safe.
27
+ class NativeBufferContent {
28
+ public:
29
+ // Validates the frame for display (a single-plane RGBA buffer in a format
30
+ // the view can show) and takes a reference on its buffer. Throws a
31
+ // std::runtime_error naming the problem.
32
+ static std::shared_ptr<NativeBufferContent>
33
+ fromFrame(const VideoFrameHandle &frame);
34
+
35
+ ~NativeBufferContent();
36
+
37
+ NativeBufferContent(const NativeBufferContent &) = delete;
38
+ NativeBufferContent &operator=(const NativeBufferContent &) = delete;
39
+
40
+ void *buffer() const { return _buffer; }
41
+ uint32_t width() const { return _width; }
42
+ uint32_t height() const { return _height; }
43
+ NativeBufferColorSpace colorSpace() const { return _colorSpace; }
44
+
45
+ private:
46
+ NativeBufferContent(void *buffer, uint32_t width, uint32_t height,
47
+ NativeBufferColorSpace colorSpace);
48
+
49
+ void *_buffer;
50
+ uint32_t _width;
51
+ uint32_t _height;
52
+ NativeBufferColorSpace _colorSpace;
53
+ };
54
+
55
+ // Waits for `fence` to reach `signaledValue` without blocking the caller, then
56
+ // calls `done` from another thread: true once the fence signaled, false when
57
+ // the wait failed or gave up after 10 s (the reason is logged). Implemented
58
+ // per platform: a sync-fd poll on a waiter thread on Android, an
59
+ // MTLSharedEvent listener with a timer on Apple platforms.
60
+ void waitForSharedFence(wgpu::SharedFence fence, uint64_t signaledValue,
61
+ std::function<void(bool)> done);
62
+
63
+ // The buffers presented to each NativeBufferView, keyed by a view id that JS
64
+ // allocates (allocateViewId) and passes to the native view as a prop. JS
65
+ // presents a buffer from any runtime (presentNativeBuffer); once its fence
66
+ // has signaled the buffer becomes the view's content and the view's listener
67
+ // is called, from any thread. The view then reads the content on the main
68
+ // thread and hands it to the compositor.
69
+ //
70
+ // Presents are ordered: a buffer whose fence signals after a later present
71
+ // already landed is dropped, so the view always ends on the last buffer
72
+ // presented.
73
+ //
74
+ // An entry has two owners: the JS component (retainFromJS on mount,
75
+ // releaseFromJS on unmount, releaseAllFromJS on reload) and the native view
76
+ // (setListener / detachView). It is erased once both let go, in either order.
77
+ // A native view that attaches after the JS component let go still gets an
78
+ // entry, which detachView erases. React StrictMode's simulated unmount (JS
79
+ // effects re-run, native view kept) releases and retains again.
80
+ class NativeBufferViewRegistry {
81
+ public:
82
+ using Listener = std::function<void()>;
83
+
84
+ static NativeBufferViewRegistry &getInstance() {
85
+ static NativeBufferViewRegistry instance;
86
+ return instance;
87
+ }
88
+
89
+ NativeBufferViewRegistry(const NativeBufferViewRegistry &) = delete;
90
+ NativeBufferViewRegistry &
91
+ operator=(const NativeBufferViewRegistry &) = delete;
92
+
93
+ // Unique for the life of the process: the registry outlives a JS reload.
94
+ // Creates no entry (React StrictMode may discard the id): retainFromJS does.
95
+ int allocateViewId() {
96
+ std::lock_guard<std::mutex> lock(_mutex);
97
+ return ++_nextViewId;
98
+ }
99
+
100
+ // Makes `content` the view's content once `fence` (if any) has reached
101
+ // `signaledValue` and, on Android, once `acquireFenceFd` (a sync fd the
102
+ // producer signals when it is done writing the buffer, or -1) has
103
+ // signaled. Takes ownership of `acquireFenceFd`. Returns immediately.
104
+ //
105
+ // A present to a view the JS component does not hold (released, or never
106
+ // retained) is dropped: nothing would ever free its buffer.
107
+ void present(int viewId, std::shared_ptr<NativeBufferContent> content,
108
+ wgpu::SharedFence fence, uint64_t signaledValue,
109
+ int acquireFenceFd = -1);
110
+
111
+ // The latest ready content and its sequence number (0 when there is none
112
+ // yet). A view keeps the sequence number of what it shows and skips a
113
+ // content it already has.
114
+ std::shared_ptr<NativeBufferContent> content(int viewId, uint64_t *sequence);
115
+
116
+ // The native view attaches (a listener) or detaches (nullptr). The listener
117
+ // is called from any thread whenever new content is ready, and once right
118
+ // away if the view already has content.
119
+ void setListener(int viewId, Listener listener);
120
+
121
+ // The native view is gone for good: drops its listener, and the entry when
122
+ // the JS component has released it too.
123
+ void detachView(int viewId);
124
+
125
+ // The JS component mounted (or mounted again with the same id, when React
126
+ // StrictMode re-runs its effects): creates the entry if needed and takes
127
+ // the JS ownership.
128
+ void retainFromJS(int viewId);
129
+
130
+ // The JS component unmounted: drops the entry when no native view is
131
+ // attached to it (else detachView does).
132
+ void releaseFromJS(int viewId);
133
+
134
+ // The JS runtime is going away (a reload): its components never run their
135
+ // cleanup, so this releases every entry from JS.
136
+ void releaseAllFromJS();
137
+
138
+ private:
139
+ NativeBufferViewRegistry() = default;
140
+
141
+ struct Entry {
142
+ std::shared_ptr<NativeBufferContent> content;
143
+ // The last present, and the one `content` comes from.
144
+ uint64_t presentedSequence = 0;
145
+ uint64_t readySequence = 0;
146
+ Listener listener;
147
+ bool ownedByJS = false;
148
+ };
149
+
150
+ // Erases `it` when neither the JS component nor a native view holds it,
151
+ // moving its content to `released` so the buffer is freed outside the lock.
152
+ void eraseIfUnowned(std::unordered_map<int, Entry>::iterator it,
153
+ std::shared_ptr<NativeBufferContent> *released);
154
+
155
+ void deliver(int viewId, uint64_t sequence,
156
+ std::shared_ptr<NativeBufferContent> content);
157
+
158
+ std::mutex _mutex;
159
+ std::unordered_map<int, Entry> _entries;
160
+ int _nextViewId = 0;
161
+ };
162
+
163
+ } // namespace rnwgpu
@@ -13,6 +13,8 @@
13
13
 
14
14
  namespace rnwgpu {
15
15
 
16
+ class AnimationFrameSource;
17
+
16
18
  struct ImageData {
17
19
  std::vector<uint8_t> data;
18
20
  size_t width;
@@ -226,6 +228,17 @@ public:
226
228
  virtual VideoFrameHandle createTestVideoFrame(uint32_t width, uint32_t height,
227
229
  VideoPixelFormat format) = 0;
228
230
 
231
+ // Allocate an empty, GPU-renderable buffer that a NativeBufferView can
232
+ // display: an AHardwareBuffer (Android) or an IOSurface (Apple platforms),
233
+ // with premultiplied pixels in `format`, tagged with `displayP3` or sRGB
234
+ // primaries (VideoFrameHandle.colorSpace.primaries; on Apple platforms the
235
+ // IOSurface is tagged too). Throws a message containing "unsupported" for a
236
+ // format the platform can't display (bgra8unorm on Android, rgba8unorm on
237
+ // Apple platforms).
238
+ virtual VideoFrameHandle createNativeBuffer(uint32_t width, uint32_t height,
239
+ wgpu::TextureFormat format,
240
+ bool displayP3) = 0;
241
+
229
242
  // Open a video file at `path` for playback. The returned player yields
230
243
  // IOSurface / AHardwareBuffer-backed frames via copyLatestFrame().
231
244
  //
@@ -233,7 +246,7 @@ public:
233
246
  // for a regular sampled GPUTexture; NV12 is the right shape for the
234
247
  // importExternalTexture path (zero-copy biplanar YUV).
235
248
  virtual std::unique_ptr<IVideoPlayer>
236
- createVideoPlayer(const std::string &path, VideoPixelFormat format) = 0;
249
+ createTestVideoPlayer(const std::string &path, VideoPixelFormat format) = 0;
237
250
 
238
251
  // Wrap a CVPixelBufferRef (Apple) or AHardwareBuffer* (Android) pointer
239
252
  // obtained from another library (typically VisionCamera's
@@ -249,6 +262,13 @@ public:
249
262
  // with it. The releaser can run on any thread.
250
263
  virtual std::function<void(void *)> retainNativeWindow(void *window) = 0;
251
264
 
265
+ // The display's frames, for requestAnimationFrame (see AnimationFrames.h);
266
+ // null where the platform has none, and every runtime then uses its own
267
+ // global requestAnimationFrame.
268
+ virtual std::shared_ptr<AnimationFrameSource> makeAnimationFrameSource() {
269
+ return nullptr;
270
+ }
271
+
252
272
  // Write a small procedurally-generated test video to a temporary location
253
273
  // and return its absolute path. Lets the SharedTextureMemory example play
254
274
  // a real decoded video without bundling an asset.
@@ -1,6 +1,9 @@
1
1
  #include "RNWebGPUManager.h"
2
2
 
3
+ #include "AnimationFrames.h"
4
+
3
5
  #include "GPU.h"
6
+ #include "NativeBufferViewRegistry.h"
4
7
  #include "NativeObject.h"
5
8
  #include "RNWebGPU.h"
6
9
 
@@ -22,8 +25,6 @@
22
25
  #include "GPUError.h"
23
26
  #include "GPUExternalTexture.h"
24
27
  #include "GPUInternalError.h"
25
- #include "GPUNativeBufferExternalTexture.h"
26
- #include "GPUNativeBufferTexture.h"
27
28
  #include "GPUOutOfMemoryError.h"
28
29
  #include "GPUPipelineLayout.h"
29
30
  #include "GPUQuerySet.h"
@@ -95,6 +96,17 @@ RNWebGPUManager::RNWebGPUManager(
95
96
  _jsRuntime->global().setProperty(*_jsRuntime, "GPUTextureUsage",
96
97
  GPUTextureUsage::create(*_jsRuntime));
97
98
 
99
+ // requestAnimationFrame paced by the display, on every runtime (see
100
+ // src/animationFrame.ts). The source is the platform's, kept across reloads;
101
+ // the HostObject is handed to other runtimes by reference, like the
102
+ // helpers below.
103
+ AnimationFrameDispatcher::shared().setSource(
104
+ _platformContext->makeAnimationFrameSource());
105
+ _jsRuntime->global().setProperty(
106
+ *_jsRuntime, "__webgpuAnimationFrames",
107
+ jsi::Object::createFromHostObject(
108
+ *_jsRuntime, std::make_shared<AnimationFramesHostObject>()));
109
+
98
110
  // Install global helper functions for Worklets serialization
99
111
  // These are standalone functions that don't require RNWebGPU instance
100
112
  installWebGPUWorkletHelpers(*_jsRuntime);
@@ -118,8 +130,6 @@ void RNWebGPUManager::installConstructors(jsi::Runtime &runtime) {
118
130
  GPUDeviceLostInfo::installConstructor(runtime);
119
131
  GPUError::installConstructor(runtime);
120
132
  GPUExternalTexture::installConstructor(runtime);
121
- GPUNativeBufferExternalTexture::installConstructor(runtime);
122
- GPUNativeBufferTexture::installConstructor(runtime);
123
133
  GPUInternalError::installConstructor(runtime);
124
134
  GPUOutOfMemoryError::installConstructor(runtime);
125
135
  GPUValidationError::installConstructor(runtime);
@@ -169,6 +179,9 @@ RNWebGPUManager::~RNWebGPUManager() {
169
179
  // never run their JS cleanup, and their ids are not handed out again (see
170
180
  // SurfaceRegistry::allocateContextId).
171
181
  SurfaceRegistry::getInstance().clear();
182
+ // Same for the NativeBufferView entries: their buffers are freed once
183
+ // their native views detach (or right away when there is none).
184
+ NativeBufferViewRegistry::getInstance().releaseAllFromJS();
172
185
  if (PlatformContext::current() == _platformContext) {
173
186
  PlatformContext::setCurrent(nullptr);
174
187
  }
@@ -442,8 +442,8 @@ public:
442
442
  // not touch the device, and this must not change behaviour there.
443
443
  //
444
444
  // Remove once the pinned Dawn contains the upstream fix; the check in
445
- // scripts/check-dawn.ts fails when the Dawn the react-native-webgpu-dawn
446
- // package was built from changes, so that decision is not forgotten.
445
+ // scripts/check-dawn.ts fails when binaries.json selects another Dawn
446
+ // commit, so that decision is not forgotten.
447
447
  void releaseSurfaceForDevice(const wgpu::Device &device) {
448
448
  #if defined(__ANDROID__)
449
449
  std::unique_lock<std::shared_mutex> lock(_mutex);
@@ -30,7 +30,7 @@ async::AsyncTaskHandle GPUAdapter::requestDevice(
30
30
  jsi::Runtime &runtime,
31
31
  std::optional<std::shared_ptr<GPUDeviceDescriptor>> descriptor) {
32
32
  // Native frame import (importSharedTextureMemory, importExternalTexture,
33
- // copyExternalImageToTexture with a NativeVideoFrame) needs no feature
33
+ // copyExternalImageToTexture with a NativeBuffer) needs no feature
34
34
  // request, like importExternalTexture on the web: the Dawn features behind
35
35
  // it are added to requiredFeatures here, each one when the adapter supports
36
36
  // it. Requesting a feature the adapter lacks would make RequestDevice fail,
@@ -295,7 +295,7 @@ async::AsyncTaskHandle GPUAdapter::requestDevice(
295
295
  // held work settles once the app is back (the same toggle Skia
296
296
  // requests on the device it shares, see react-native-skia's
297
297
  // DawnUtils::requestDevice). Dawn applies it on iOS and tvOS only, and
298
- // a Dawn without it (react-native-webgpu-dawn before 157.1.0) drops
298
+ // a Dawn without it (Dawn binaries releases before 157.1.0) drops
299
299
  // the name. The caller opts out through disabledToggles.
300
300
  {
301
301
  static const char *const kName =
@@ -446,54 +446,25 @@ std::shared_ptr<GPUExternalTexture> GPUDevice::importExternalTexture(
446
446
 
447
447
  std::shared_ptr<GPUSharedTextureMemory> GPUDevice::importSharedTextureMemory(
448
448
  std::shared_ptr<GPUSharedTextureMemoryDescriptor> descriptor) {
449
- if (!descriptor || descriptor->handle == nullptr) {
450
- throw std::runtime_error("GPUDevice::importSharedTextureMemory(): handle "
451
- "must be a non-null native pointer");
449
+ static constexpr const char *kCaller = "GPUDevice::importSharedTextureMemory";
450
+ if (!descriptor || (!descriptor->source && descriptor->handle == nullptr)) {
451
+ throw std::runtime_error(
452
+ "GPUDevice::importSharedTextureMemory(): source (a NativeBuffer) or "
453
+ "handle (a non-null native pointer) is required");
452
454
  }
453
-
454
- wgpu::SharedTextureMemoryDescriptor desc{};
455
455
  std::string label = descriptor->label.value_or("");
456
- if (!label.empty()) {
457
- desc.label = wgpu::StringView(label.c_str(), label.size());
458
- }
459
-
460
- #if defined(__APPLE__)
461
- wgpu::SharedTextureMemoryIOSurfaceDescriptor platformDesc{};
462
- platformDesc.ioSurface = descriptor->handle;
463
- // Default off: enabling it propagates StorageBinding into properties.usage,
464
- // which then forces memory.createTexture() (no-descriptor form) to validate
465
- // the format against storage capabilities. bgra8unorm (the standard
466
- // CVPixelBuffer format) only supports storage when the device opts into the
467
- // bgra8unorm-storage feature, so unconditionally setting this here breaks
468
- // the common sample-only case.
469
- platformDesc.allowStorageBinding = false;
470
- desc.nextInChain = &platformDesc;
471
- #elif defined(__ANDROID__)
472
- wgpu::SharedTextureMemoryAHardwareBufferDescriptor platformDesc{};
473
- platformDesc.handle = descriptor->handle;
474
- desc.nextInChain = &platformDesc;
475
- #else
476
- throw std::runtime_error(
477
- "GPUDevice::importSharedTextureMemory(): unsupported platform");
478
- #endif
479
-
480
- auto memory = _instance.ImportSharedTextureMemory(&desc);
481
- // Dawn reports a failed import (feature missing, unusable handle) as a
482
- // validation error and returns an error object rather than null, with no
483
- // format in its properties: the format getter of any texture created from
484
- // it would throw. Fail the import instead.
485
- wgpu::SharedTextureMemoryProperties properties{};
486
- if (memory == nullptr ||
487
- memory.GetProperties(&properties) != wgpu::Status::Success ||
488
- properties.format == wgpu::TextureFormat::Undefined) {
489
- throw std::runtime_error(
490
- "GPUDevice::importSharedTextureMemory(): the import failed - is the "
491
- "'shared-texture-memory-iosurface' (Apple) or "
492
- "'shared-texture-memory-ahardware-buffer' (Android) feature enabled "
493
- "on the device, and the handle a usable IOSurface / AHardwareBuffer?");
456
+ wgpu::SharedTextureMemory memory;
457
+ if (descriptor->source) {
458
+ memory = native_frame::importMemory(_instance, descriptor->source->handle(),
459
+ label, kCaller);
460
+ } else {
461
+ VideoFrameHandle frame;
462
+ frame.handle = descriptor->handle;
463
+ memory = native_frame::importMemory(_instance, frame, label, kCaller);
494
464
  }
495
- return std::make_shared<GPUSharedTextureMemory>(_instance, std::move(memory),
496
- std::move(label));
465
+ return std::make_shared<GPUSharedTextureMemory>(
466
+ _instance, std::move(memory), std::move(label),
467
+ std::move(descriptor->source));
497
468
  }
498
469
 
499
470
  std::shared_ptr<GPUSharedFence> GPUDevice::importSharedFence(
@@ -546,92 +517,6 @@ std::shared_ptr<GPUSharedFence> GPUDevice::importSharedFence(
546
517
  std::move(label));
547
518
  }
548
519
 
549
- std::shared_ptr<GPUNativeBufferTexture> GPUDevice::createNativeBufferTexture(
550
- std::shared_ptr<VideoFrame> source,
551
- std::optional<std::shared_ptr<GPUNativeBufferTextureDescriptor>>
552
- descriptor) {
553
- static constexpr const char *kCaller = "GPUDevice::createNativeBufferTexture";
554
- if (!source) {
555
- throw std::runtime_error("GPUDevice::createNativeBufferTexture(): "
556
- "source (NativeVideoFrame) is required");
557
- }
558
- std::string label;
559
- std::optional<double> usage;
560
- if (descriptor.has_value() && *descriptor) {
561
- label = (*descriptor)->label.value_or("");
562
- usage = (*descriptor)->usage;
563
- }
564
- auto memory =
565
- native_frame::importMemory(_instance, source->handle(), label, kCaller);
566
- wgpu::Texture texture;
567
- if (usage.has_value()) {
568
- // Dawn's layer describes the texture from the memory's properties, with
569
- // the caller's usage. An unsupported usage is a validation error and an
570
- // error texture, as with createTexture().
571
- wgpu::SharedTextureMemoryProperties properties{};
572
- (void)memory.GetProperties(&properties);
573
- wgpu::TextureDescriptor desc{};
574
- if (!label.empty()) {
575
- desc.label = wgpu::StringView(label.c_str(), label.size());
576
- }
577
- desc.usage = static_cast<wgpu::TextureUsage>(static_cast<uint64_t>(*usage));
578
- desc.dimension = wgpu::TextureDimension::e2D;
579
- desc.size = properties.size;
580
- desc.format = properties.format;
581
- desc.mipLevelCount = 1;
582
- desc.sampleCount = 1;
583
- texture = memory.CreateTexture(&desc);
584
- } else {
585
- texture = memory.CreateTexture();
586
- }
587
- if (texture == nullptr) {
588
- throw std::runtime_error("GPUDevice::createNativeBufferTexture(): "
589
- "CreateTexture returned null");
590
- }
591
- auto wrapped = std::make_shared<GPUTexture>(std::move(texture), label);
592
- return std::make_shared<GPUNativeBufferTexture>(
593
- _instance, std::move(memory), std::move(wrapped), std::move(source),
594
- std::move(label));
595
- }
596
-
597
- std::shared_ptr<GPUNativeBufferExternalTexture>
598
- GPUDevice::createNativeBufferExternalTexture(
599
- std::shared_ptr<VideoFrame> source,
600
- std::optional<std::shared_ptr<GPUExternalTextureDescriptor>> descriptor) {
601
- static constexpr const char *kCaller =
602
- "GPUDevice::createNativeBufferExternalTexture";
603
- if (!source) {
604
- throw std::runtime_error(
605
- "GPUDevice::createNativeBufferExternalTexture(): source "
606
- "(NativeVideoFrame) is required");
607
- }
608
- std::shared_ptr<GPUExternalTextureDescriptor> options =
609
- descriptor.has_value() && *descriptor
610
- ? *descriptor
611
- : std::make_shared<GPUExternalTextureDescriptor>();
612
- const auto &frame = source->handle();
613
- std::string label = options->label.value_or("external-texture");
614
- // The import, the texture and the external texture, once per buffer. No
615
- // access begins here: the wrapper's beginAccess() / endAccess() bracket
616
- // each use.
617
- auto memory = native_frame::importMemory(_instance, frame, label, kCaller);
618
- auto texture = memory.CreateTexture();
619
- if (texture == nullptr) {
620
- throw std::runtime_error(
621
- "GPUDevice::createNativeBufferExternalTexture(): CreateTexture "
622
- "returned null");
623
- }
624
- auto external = native_frame::createExternalTexture(
625
- _instance, memory, texture, frame, *options, label, kCaller);
626
- // Non-owning: the wrapper owns the import (see GPUExternalTexture's
627
- // constructor).
628
- auto externalTexture = std::make_shared<GPUExternalTexture>(
629
- std::move(external), nullptr, nullptr, nullptr, label);
630
- return std::make_shared<GPUNativeBufferExternalTexture>(
631
- _instance, std::move(memory), std::move(texture),
632
- std::move(externalTexture), std::move(source), std::move(label));
633
- }
634
-
635
520
  async::AsyncTaskHandle GPUDevice::createComputePipelineAsync(
636
521
  jsi::Runtime &runtime,
637
522
  std::shared_ptr<GPUComputePipelineDescriptor> descriptor) {
@@ -37,9 +37,6 @@
37
37
  #include "GPUError.h"
38
38
  #include "GPUExternalTexture.h"
39
39
  #include "GPUExternalTextureDescriptor.h"
40
- #include "GPUNativeBufferExternalTexture.h"
41
- #include "GPUNativeBufferTexture.h"
42
- #include "GPUNativeBufferTextureDescriptor.h"
43
40
  #include "GPUPipelineLayout.h"
44
41
  #include "GPUPipelineLayoutDescriptor.h"
45
42
  #include "GPUQuerySet.h"
@@ -137,17 +134,6 @@ public:
137
134
  std::shared_ptr<GPUSharedTextureMemoryDescriptor> descriptor);
138
135
  std::shared_ptr<GPUSharedFence>
139
136
  importSharedFence(std::shared_ptr<GPUSharedFenceDescriptor> descriptor);
140
- // Import a native buffer once and bracket each use with beginAccess /
141
- // endAccess: the counterparts of createTexture / createExternalTexture in
142
- // GPUAndroidHardwareBufferUtil of Dawn's Android layer.
143
- std::shared_ptr<GPUNativeBufferTexture> createNativeBufferTexture(
144
- std::shared_ptr<VideoFrame> source,
145
- std::optional<std::shared_ptr<GPUNativeBufferTextureDescriptor>>
146
- descriptor);
147
- std::shared_ptr<GPUNativeBufferExternalTexture>
148
- createNativeBufferExternalTexture(
149
- std::shared_ptr<VideoFrame> source,
150
- std::optional<std::shared_ptr<GPUExternalTextureDescriptor>> descriptor);
151
137
  std::shared_ptr<GPUBindGroupLayout> createBindGroupLayout(
152
138
  std::shared_ptr<GPUBindGroupLayoutDescriptor> descriptor);
153
139
  std::shared_ptr<GPUPipelineLayout>
@@ -208,10 +194,6 @@ public:
208
194
  &GPUDevice::importSharedTextureMemory);
209
195
  installMethod(runtime, prototype, "importSharedFence",
210
196
  &GPUDevice::importSharedFence);
211
- installMethod(runtime, prototype, "createNativeBufferTexture",
212
- &GPUDevice::createNativeBufferTexture);
213
- installMethod(runtime, prototype, "createNativeBufferExternalTexture",
214
- &GPUDevice::createNativeBufferExternalTexture);
215
197
  installMethod(runtime, prototype, "createBindGroupLayout",
216
198
  &GPUDevice::createBindGroupLayout);
217
199
  installMethod(runtime, prototype, "createPipelineLayout",
@@ -7,6 +7,7 @@
7
7
  #include <utility>
8
8
  #include <vector>
9
9
 
10
+ #include "DestroyedError.h"
10
11
  #include "GPUExternalTextureDescriptor.h"
11
12
  #include "NativeFrameImport.h"
12
13
 
@@ -16,10 +17,41 @@ std::shared_ptr<GPUExternalTexture> GPUExternalTexture::Create(
16
17
  wgpu::Device device,
17
18
  std::shared_ptr<GPUExternalTextureDescriptor> descriptor) {
18
19
  static constexpr const char *kCaller = "GPUDevice::importExternalTexture";
20
+ if (descriptor && descriptor->sourceTexture) {
21
+ // A texture of a GPUSharedTextureMemory: the caller imported the buffer
22
+ // once and brackets each use with the memory's beginAccess() / endAccess(),
23
+ // so the external texture only samples it, and can be kept for every
24
+ // access of the buffer.
25
+ const auto &source = descriptor->sourceTexture;
26
+ const auto &origin = source->sharedTextureOrigin();
27
+ if (!origin) {
28
+ throw std::runtime_error(
29
+ "GPUDevice::importExternalTexture(): descriptor.source is a "
30
+ "GPUTexture that was not created by "
31
+ "GPUSharedTextureMemory.createTexture()");
32
+ }
33
+ // Copied: destroy() on the memory clears the origin's reference.
34
+ wgpu::SharedTextureMemory memory = origin->memory;
35
+ if (!memory) {
36
+ // Like an access to a destroyed memory: a validation error, then the
37
+ // throw.
38
+ reportDestroyedUse(device, "GPUSharedTextureMemory", origin->label,
39
+ "importExternalTexture");
40
+ throw std::runtime_error(
41
+ "GPUDevice::importExternalTexture(): descriptor.source is a "
42
+ "texture of a GPUSharedTextureMemory that was destroyed");
43
+ }
44
+ std::string label = descriptor->label.value_or("external-texture");
45
+ auto external = native_frame::createExternalTexture(
46
+ device, memory, source->get(), origin->colorSpace, *descriptor, label,
47
+ kCaller);
48
+ return std::make_shared<GPUExternalTexture>(
49
+ std::move(external), nullptr, nullptr, nullptr, std::move(label));
50
+ }
19
51
  if (!descriptor || !descriptor->source) {
20
52
  throw std::runtime_error(
21
- "GPUDevice::importExternalTexture(): descriptor.source "
22
- "(NativeVideoFrame) is required");
53
+ "GPUDevice::importExternalTexture(): descriptor.source (a "
54
+ "NativeBuffer, or a texture of a GPUSharedTextureMemory) is required");
23
55
  }
24
56
  const auto &frame = descriptor->source->handle();
25
57
  std::string label = descriptor->label.value_or("external-texture");
@@ -51,7 +83,7 @@ std::shared_ptr<GPUExternalTexture> GPUExternalTexture::Create(
51
83
  wgpu::ExternalTexture external;
52
84
  try {
53
85
  external = native_frame::createExternalTexture(
54
- device, memory, texture, frame, *descriptor, label, kCaller);
86
+ device, memory, texture, frame.colorSpace, *descriptor, label, kCaller);
55
87
  } catch (...) {
56
88
  native_frame::endAccess(memory, texture);
57
89
  throw;
@@ -30,10 +30,10 @@ public:
30
30
 
31
31
  // `memory` and `texture` are the import this external texture owns:
32
32
  // destroy() ends their access and releases them, so the producer (e.g. an
33
- // AVPlayer) can reclaim the surface. A GPUNativeBufferExternalTexture
34
- // passes null for both: it owns the import itself and brackets each use with
35
- // its own beginAccess() / endAccess(), so destroy() here only releases the
36
- // external texture.
33
+ // AVPlayer) can reclaim the surface. An external texture over a texture of
34
+ // a GPUSharedTextureMemory passes null for both: the caller owns the import
35
+ // and brackets each use with the memory's beginAccess() / endAccess(), so
36
+ // destroy() here only releases the external texture.
37
37
  GPUExternalTexture(wgpu::ExternalTexture instance,
38
38
  wgpu::SharedTextureMemory memory, wgpu::Texture texture,
39
39
  std::shared_ptr<VideoFrame> source, std::string label)
@@ -190,7 +190,7 @@ void GPUQueue::copyExternalImageToTexture(
190
190
  }
191
191
  if (!source->source) {
192
192
  throw std::runtime_error("copyExternalImageToTexture: the source must be "
193
- "an ImageBitmap or a NativeVideoFrame");
193
+ "an ImageBitmap or a NativeBuffer");
194
194
  }
195
195
  uint32_t bytesPerPixel =
196
196
  source->source->getSize() /
@@ -55,7 +55,7 @@ public:
55
55
  std::shared_ptr<GPUImageDataLayout> dataLayout,
56
56
  std::shared_ptr<GPUExtent3D> size);
57
57
  // The source is an ImageBitmap (uploaded from the CPU) or, as a React
58
- // Native extension, a NativeVideoFrame rendered into the destination through
58
+ // Native extension, a NativeBuffer rendered into the destination through
59
59
  // an external texture (see VideoFrameBlit).
60
60
  void copyExternalImageToTexture(
61
61
  std::shared_ptr<GPUImageCopyExternalImage> source,
@@ -56,6 +56,7 @@ public:
56
56
  }
57
57
 
58
58
  inline wgpu::SharedFence get() { return _instance; }
59
+ inline const wgpu::Device &device() const { return _device; }
59
60
 
60
61
  // Reports a use of the destroyed fence the way Dawn reports an invalid
61
62
  // object: a validation error, caught by error scopes or else delivered to
@@ -27,7 +27,11 @@ void GPUSharedTextureMemory::destroy() {
27
27
  _device.InjectError(wgpu::ErrorType::Validation, message.c_str());
28
28
  _accessed.clear();
29
29
  }
30
+ // The textures created from the memory share the origin: without this, each
31
+ // one still referenced from JS would keep the import alive.
32
+ _origin->memory = nullptr;
30
33
  _instance = nullptr;
34
+ _source = nullptr;
31
35
  }
32
36
 
33
37
  void GPUSharedTextureMemory::injectDestroyedError(const char *method) {
@@ -61,8 +65,9 @@ std::shared_ptr<GPUTexture> GPUSharedTextureMemory::createTexture(
61
65
  std::move(label), false);
62
66
  }
63
67
  if (!descriptor.has_value() || descriptor.value() == nullptr) {
64
- auto texture = _instance.CreateTexture();
65
- return std::make_shared<GPUTexture>(texture, "");
68
+ auto texture = std::make_shared<GPUTexture>(_instance.CreateTexture(), "");
69
+ texture->setSharedTextureOrigin(_origin);
70
+ return texture;
66
71
  }
67
72
 
68
73
  wgpu::TextureDescriptor desc{};
@@ -72,9 +77,10 @@ std::shared_ptr<GPUTexture> GPUSharedTextureMemory::createTexture(
72
77
  "GPUSharedTextureMemory::createTexture(): Error with "
73
78
  "GPUTextureDescriptor");
74
79
  }
75
- auto texture = _instance.CreateTexture(&desc);
76
- return std::make_shared<GPUTexture>(texture,
77
- descriptor.value()->label.value_or(""));
80
+ auto texture = std::make_shared<GPUTexture>(
81
+ _instance.CreateTexture(&desc), descriptor.value()->label.value_or(""));
82
+ texture->setSharedTextureOrigin(_origin);
83
+ return texture;
78
84
  }
79
85
 
80
86
  void GPUSharedTextureMemory::beginAccess(