react-native-webgpu 0.19.2 → 0.21.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 (72) hide show
  1. package/Package.swift +19 -7
  2. package/android/CMakeLists.txt +1 -0
  3. package/android/build.gradle +7 -0
  4. package/android/cpp/cpp-adapter.cpp +23 -1
  5. package/android/src/main/java/com/webgpu/WebGPUModule.java +34 -3
  6. package/apple/ApplePlatformContext.h +6 -1
  7. package/apple/ApplePlatformContext.mm +7 -8
  8. package/apple/AppleVideoPlayer.mm +21 -11
  9. package/apple/MetalLayerColorSpace.mm +24 -1
  10. package/apple/WebGPUModule.mm +30 -1
  11. package/cpp/jsi/Promise.cpp +4 -0
  12. package/cpp/jsi/Promise.h +3 -0
  13. package/cpp/rnwgpu/CanvasColorSpace.h +5 -0
  14. package/cpp/rnwgpu/DawnBlobCache.cpp +477 -0
  15. package/cpp/rnwgpu/DawnBlobCache.h +76 -0
  16. package/cpp/rnwgpu/RNWebGPUManager.cpp +2 -4
  17. package/cpp/rnwgpu/SurfaceRegistry.h +16 -7
  18. package/cpp/rnwgpu/WebGPURuntime.cpp +2 -1
  19. package/cpp/rnwgpu/api/GPU.cpp +6 -6
  20. package/cpp/rnwgpu/api/GPU.h +1 -1
  21. package/cpp/rnwgpu/api/GPUAdapter.cpp +43 -7
  22. package/cpp/rnwgpu/api/GPUBuffer.cpp +5 -6
  23. package/cpp/rnwgpu/api/GPUCanvasContext.cpp +2 -1
  24. package/cpp/rnwgpu/api/GPUDevice.cpp +165 -48
  25. package/cpp/rnwgpu/api/GPUDevice.h +67 -14
  26. package/cpp/rnwgpu/api/GPUQueue.cpp +11 -2
  27. package/cpp/rnwgpu/api/GPUQueue.h +7 -2
  28. package/cpp/rnwgpu/api/GPUShaderModule.cpp +2 -2
  29. package/cpp/rnwgpu/api/RNWebGPU.h +11 -10
  30. package/cpp/rnwgpu/api/descriptors/GPUCanvasConfiguration.h +24 -0
  31. package/cpp/rnwgpu/async/AsyncTaskHandle.cpp +33 -45
  32. package/cpp/rnwgpu/async/AsyncTaskHandle.h +5 -6
  33. package/cpp/rnwgpu/async/RuntimeContext.cpp +321 -112
  34. package/cpp/rnwgpu/async/RuntimeContext.h +90 -82
  35. package/lib/commonjs/Canvas.js +1 -0
  36. package/lib/commonjs/Canvas.js.map +1 -1
  37. package/lib/commonjs/WebGPUViewNativeComponent.js.map +1 -1
  38. package/lib/commonjs/WebGPUViewNativeComponent.web.js.map +1 -1
  39. package/lib/commonjs/external/reanimated/registerWebGPUForReanimated.js +19 -1
  40. package/lib/commonjs/external/reanimated/registerWebGPUForReanimated.js.map +1 -1
  41. package/lib/commonjs/install.js +6 -5
  42. package/lib/commonjs/install.js.map +1 -1
  43. package/lib/module/Canvas.js +1 -0
  44. package/lib/module/Canvas.js.map +1 -1
  45. package/lib/module/WebGPUViewNativeComponent.js.map +1 -1
  46. package/lib/module/WebGPUViewNativeComponent.web.js.map +1 -1
  47. package/lib/module/external/reanimated/registerWebGPUForReanimated.js +19 -1
  48. package/lib/module/external/reanimated/registerWebGPUForReanimated.js.map +1 -1
  49. package/lib/module/install.js +6 -5
  50. package/lib/module/install.js.map +1 -1
  51. package/lib/typescript/lib/commonjs/Canvas.d.ts.map +1 -1
  52. package/lib/typescript/lib/commonjs/external/reanimated/registerWebGPUForReanimated.d.ts.map +1 -1
  53. package/lib/typescript/lib/commonjs/install.d.ts +6 -5
  54. package/lib/typescript/lib/commonjs/install.d.ts.map +1 -1
  55. package/lib/typescript/lib/module/Canvas.d.ts.map +1 -1
  56. package/lib/typescript/lib/module/external/reanimated/registerWebGPUForReanimated.d.ts.map +1 -1
  57. package/lib/typescript/lib/module/install.d.ts.map +1 -1
  58. package/lib/typescript/src/Canvas.d.ts.map +1 -1
  59. package/lib/typescript/src/WebGPUViewNativeComponent.d.ts +6 -7
  60. package/lib/typescript/src/WebGPUViewNativeComponent.d.ts.map +1 -1
  61. package/lib/typescript/src/WebGPUViewNativeComponent.web.d.ts +2 -3
  62. package/lib/typescript/src/WebGPUViewNativeComponent.web.d.ts.map +1 -1
  63. package/lib/typescript/src/external/reanimated/registerWebGPUForReanimated.d.ts +1 -0
  64. package/lib/typescript/src/external/reanimated/registerWebGPUForReanimated.d.ts.map +1 -1
  65. package/lib/typescript/src/install.d.ts +6 -5
  66. package/lib/typescript/src/install.d.ts.map +1 -1
  67. package/package.json +3 -2
  68. package/src/Canvas.tsx +4 -2
  69. package/src/WebGPUViewNativeComponent.ts +6 -10
  70. package/src/WebGPUViewNativeComponent.web.ts +2 -3
  71. package/src/external/reanimated/registerWebGPUForReanimated.ts +21 -1
  72. package/src/install.ts +6 -5
package/Package.swift CHANGED
@@ -56,21 +56,33 @@ let dawnVersion =
56
56
  // changing either package's Dawn reset the package caches (File > Packages >
57
57
  // Reset Package Caches) if this does not re-run.
58
58
  //
59
- // A react-native-skia that depends on react-native-webgpu-dawn itself must pin
60
- // the same version as this package. Older Graphite builds (react-native-skia
61
- // 3.0.x) vendor their own Dawn and record its release tag at
62
- // libs/.dawn-version in their binary packages; this package's `dawn` field
59
+ // A react-native-skia that depends on react-native-webgpu-dawn itself must
60
+ // resolve the same version as this package: the copy nested under it when the
61
+ // package manager could not hoist it, else the one next to it (Node's
62
+ // resolution, which react-native-skia's podspec compares too). A workspace
63
+ // that resolves the package to one version for both (yarn `resolutions`, npm
64
+ // `overrides`) passes while react-native-skia's own pin lags behind a Dawn
65
+ // bump: the app then contains that one Dawn. Older Graphite builds
66
+ // (react-native-skia 3.0.x) vendor their own Dawn and record its release tag
67
+ // at libs/.dawn-version in their binary packages; this package's `dawn` field
63
68
  // names the release that Dawn is interchangeable with.
64
69
  if let skiaPath = nodePackage("react-native-skia"),
65
70
  let skiaPackage = readJSON("\(skiaPath)/package.json"),
66
- let skiaDawn = (skiaPackage["dependencies"] as? [String: String])?["react-native-webgpu-dawn"]
71
+ let skiaDawnPin = (skiaPackage["dependencies"] as? [String: String])?["react-native-webgpu-dawn"]
67
72
  {
73
+ let skiaDawnPath = [
74
+ "\(skiaPath)/node_modules/react-native-webgpu-dawn",
75
+ "\(skiaPath)/../react-native-webgpu-dawn",
76
+ ].first { FileManager.default.fileExists(atPath: "\($0)/package.json") }
77
+ let skiaDawn =
78
+ skiaDawnPath.flatMap { readJSON("\($0)/package.json")?["version"] as? String } ?? skiaDawnPin
68
79
  if skiaDawn != dawnVersion {
69
80
  fatalError(
70
81
  """
71
82
  react-native-webgpu: Dawn version mismatch. This package links \
72
- react-native-webgpu-dawn \(dawnVersion) but react-native-skia depends on \
73
- \(skiaDawn). Align the two packages so the app contains exactly one Dawn.
83
+ react-native-webgpu-dawn \(dawnVersion) but react-native-skia resolves \
84
+ \(skiaDawn) (it depends on \(skiaDawnPin)). Align the two packages so the \
85
+ app contains exactly one Dawn.
74
86
  """)
75
87
  }
76
88
  } else {
@@ -55,6 +55,7 @@ add_library(${PACKAGE_NAME} SHARED
55
55
  ../cpp/rnwgpu/api/GPUComputePassEncoder.cpp
56
56
  ../cpp/rnwgpu/api/GPUComputePipeline.cpp
57
57
  ../cpp/rnwgpu/api/GPUCanvasContext.cpp
58
+ ../cpp/rnwgpu/DawnBlobCache.cpp
58
59
  ../cpp/rnwgpu/RNWebGPUManager.cpp
59
60
  ../cpp/rnwgpu/WebGPURuntime.cpp
60
61
  ../cpp/jsi/Promise.cpp
@@ -97,6 +97,13 @@ def supportsNamespace() {
97
97
  android {
98
98
  compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")
99
99
 
100
+ if (rootProject.hasProperty("ndkPath")) {
101
+ ndkPath = rootProject.ext.ndkPath
102
+ }
103
+ if (rootProject.hasProperty("ndkVersion")) {
104
+ ndkVersion = rootProject.ext.ndkVersion
105
+ }
106
+
100
107
  defaultConfig {
101
108
  minSdkVersion resolveMinSdkVersion()
102
109
  targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")
@@ -21,7 +21,9 @@
21
21
  #include <webgpu/webgpu_cpp.h>
22
22
 
23
23
  #include "AndroidPlatformContext.h"
24
+ #include "DawnBlobCache.h"
24
25
  #include "GPUCanvasContext.h"
26
+ #include "GPUDevice.h"
25
27
  #include "RNWebGPUManager.h"
26
28
  #include "WebGPURuntime.h"
27
29
 
@@ -31,8 +33,18 @@ std::shared_ptr<rnwgpu::RNWebGPUManager> manager;
31
33
 
32
34
  extern "C" JNIEXPORT void JNICALL Java_com_webgpu_WebGPUModule_initializeNative(
33
35
  JNIEnv *env, jobject thiz, jlong jsRuntime, jobject jsCallInvokerHolder,
34
- jobject blobModule) {
36
+ jobject blobModule, jstring cacheDirectory) {
35
37
  auto runtime = reinterpret_cast<facebook::jsi::Runtime *>(jsRuntime);
38
+ if (cacheDirectory != nullptr) {
39
+ const char *path = env->GetStringUTFChars(cacheDirectory, nullptr);
40
+ if (path == nullptr) {
41
+ // The JVM could not copy the string, and an OutOfMemoryError is pending:
42
+ // no further JNI call is allowed, so let it reach the caller.
43
+ return;
44
+ }
45
+ rnwgpu::DawnBlobCache::setDirectory(path);
46
+ env->ReleaseStringUTFChars(cacheDirectory, path);
47
+ }
36
48
  jobject globalBlobModule = env->NewGlobalRef(blobModule);
37
49
  jobject globalModule = env->NewGlobalRef(thiz);
38
50
  auto jsCallInvoker{
@@ -45,6 +57,16 @@ extern "C" JNIEXPORT void JNICALL Java_com_webgpu_WebGPUModule_initializeNative(
45
57
  platformContext);
46
58
  }
47
59
 
60
+ // WebGPUModule.onHostPause (UI thread): the app is going to the background,
61
+ // the last moment to write what Dawn's pipeline caches hold before the process
62
+ // may be killed. Each device flushes here, or on the thread of its own runtime
63
+ // when it does not synchronize itself.
64
+ extern "C" JNIEXPORT void JNICALL
65
+ Java_com_webgpu_WebGPUModule_flushPipelineCaches(JNIEnv * /* env */,
66
+ jobject /* this */) {
67
+ rnwgpu::GPUDevice::flushPipelineCaches();
68
+ }
69
+
48
70
  // Completion of WebGPUModule.snapshotView (UI thread). Reclaims the callbacks
49
71
  // handed to Java and delivers either the bitmap's pixels or the error.
50
72
  extern "C" JNIEXPORT void JNICALL Java_com_webgpu_WebGPUModule_onViewSnapshot(
@@ -8,12 +8,14 @@ import android.view.View;
8
8
 
9
9
  import androidx.annotation.OptIn;
10
10
 
11
+ import java.io.File;
11
12
  import java.util.HashSet;
12
13
  import java.util.Set;
13
14
 
14
15
  import com.facebook.proguard.annotations.DoNotStrip;
15
16
  import com.facebook.react.bridge.ReactApplicationContext;
16
17
  import com.facebook.react.bridge.JavaScriptContextHolder;
18
+ import com.facebook.react.bridge.LifecycleEventListener;
17
19
  import com.facebook.react.bridge.ReactMethod;
18
20
  import com.facebook.react.bridge.UIManager;
19
21
  import com.facebook.react.bridge.UiThreadUtil;
@@ -26,7 +28,7 @@ import com.facebook.react.turbomodule.core.CallInvokerHolderImpl;
26
28
  import com.facebook.react.turbomodule.core.interfaces.CallInvokerHolder;
27
29
 
28
30
  @ReactModule(name = WebGPUModule.NAME)
29
- public class WebGPUModule extends NativeWebGPUModuleSpec {
31
+ public class WebGPUModule extends NativeWebGPUModuleSpec implements LifecycleEventListener {
30
32
  static {
31
33
  System.loadLibrary("react-native-webgpu"); // Load the C++ library
32
34
  }
@@ -35,8 +37,34 @@ public class WebGPUModule extends NativeWebGPUModuleSpec {
35
37
  super(reactContext);
36
38
  // Initialize the C++ module
37
39
  initialize();
40
+ reactContext.addLifecycleEventListener(this);
38
41
  }
39
42
 
43
+ @Override
44
+ public void invalidate() {
45
+ getReactApplicationContext().removeLifecycleEventListener(this);
46
+ super.invalidate();
47
+ }
48
+
49
+ @Override
50
+ public void onHostResume() {}
51
+
52
+ /**
53
+ * The app is going to the background: the last moment to write what Dawn's
54
+ * pipeline caches hold before the process may be killed (see
55
+ * GPUDevice::flushPipelineCaches).
56
+ */
57
+ @Override
58
+ public void onHostPause() {
59
+ flushPipelineCaches();
60
+ }
61
+
62
+ @Override
63
+ public void onHostDestroy() {}
64
+
65
+ @DoNotStrip
66
+ private native void flushPipelineCaches();
67
+
40
68
  @OptIn(markerClass = FrameworkAPI.class)
41
69
  @ReactMethod(isBlockingSynchronousMethod = true)
42
70
  public boolean install() {
@@ -47,13 +75,16 @@ public class WebGPUModule extends NativeWebGPUModuleSpec {
47
75
  if (blobModule == null) {
48
76
  throw new RuntimeException("React Native's BlobModule was not found!");
49
77
  }
50
- initializeNative(jsContext.get(), (CallInvokerHolderImpl) callInvokerHolder, blobModule);
78
+ // Dawn keeps the pipelines it compiles here, so a relaunch does not
79
+ // compile them again (see DawnBlobCache).
80
+ String cacheDirectory = new File(context.getCacheDir(), "webgpu-dawn").getAbsolutePath();
81
+ initializeNative(jsContext.get(), (CallInvokerHolderImpl) callInvokerHolder, blobModule, cacheDirectory);
51
82
  return true;
52
83
  }
53
84
 
54
85
  @OptIn(markerClass = FrameworkAPI.class)
55
86
  @DoNotStrip
56
- private native void initializeNative(long jsRuntime, CallInvokerHolderImpl jsInvoker, BlobModule blobModule);
87
+ private native void initializeNative(long jsRuntime, CallInvokerHolderImpl jsInvoker, BlobModule blobModule, String cacheDirectory);
57
88
 
58
89
  /**
59
90
  * Called from C++ (AndroidPlatformContext::snapshotView) on an arbitrary
@@ -13,8 +13,12 @@ public:
13
13
  // UIView* / NSView* bridged to void*, or nullptr). Called on the main thread
14
14
  // only. Provided by WebGPUModule from React Native's view registry.
15
15
  using ViewLookup = std::function<void *(int)>;
16
+ // Returns React Native's RCTBlobManager (unretained, bridged to void*), or
17
+ // nullptr. Provided by WebGPUModule from React Native's module registry.
18
+ using BlobManagerLookup = std::function<void *()>;
16
19
 
17
- explicit ApplePlatformContext(ViewLookup viewLookup = nullptr);
20
+ explicit ApplePlatformContext(ViewLookup viewLookup,
21
+ BlobManagerLookup blobManagerLookup);
18
22
  ~ApplePlatformContext() = default;
19
23
 
20
24
  void snapshotView(const ViewSnapshotRequest &request,
@@ -52,6 +56,7 @@ public:
52
56
 
53
57
  private:
54
58
  ViewLookup _viewLookup;
59
+ BlobManagerLookup _blobManagerLookup;
55
60
  };
56
61
 
57
62
  } // namespace rnwgpu
@@ -6,7 +6,6 @@
6
6
  #import <CoreVideo/CoreVideo.h>
7
7
  #import <ImageIO/ImageIO.h>
8
8
  #import <React/RCTBlobManager.h>
9
- #import <React/RCTBridge+Private.h>
10
9
  #import <ReactCommon/RCTTurboModule.h>
11
10
 
12
11
  #include "AppleVideoPlayer.h"
@@ -35,8 +34,10 @@ void checkIfUsingSimulatorWithAPIValidation() {
35
34
  #endif
36
35
  }
37
36
 
38
- ApplePlatformContext::ApplePlatformContext(ViewLookup viewLookup)
39
- : _viewLookup(std::move(viewLookup)) {
37
+ ApplePlatformContext::ApplePlatformContext(ViewLookup viewLookup,
38
+ BlobManagerLookup blobManagerLookup)
39
+ : _viewLookup(std::move(viewLookup)),
40
+ _blobManagerLookup(std::move(blobManagerLookup)) {
40
41
  checkIfUsingSimulatorWithAPIValidation();
41
42
  }
42
43
 
@@ -360,8 +361,7 @@ static std::span<const uint8_t> nsDataToSpan(NSData *data) {
360
361
 
361
362
  ImageData ApplePlatformContext::createImageBitmap(std::string blobId,
362
363
  double offset, double size) {
363
- RCTBlobManager *blobManager =
364
- [[RCTBridge currentBridge] moduleForClass:RCTBlobManager.class];
364
+ RCTBlobManager *blobManager = (__bridge RCTBlobManager *)_blobManagerLookup();
365
365
  NSData *blobData =
366
366
  [blobManager resolve:[NSString stringWithUTF8String:blobId.c_str()]
367
367
  offset:(long)offset
@@ -378,9 +378,8 @@ void ApplePlatformContext::createImageBitmapAsync(
378
378
  std::string blobId, double offset, double size,
379
379
  std::function<void(ImageData)> onSuccess,
380
380
  std::function<void(std::string)> onError) {
381
- // Resolve blob on current thread (requires RCTBridge access)
382
- RCTBlobManager *blobManager =
383
- [[RCTBridge currentBridge] moduleForClass:RCTBlobManager.class];
381
+ // Resolve blob on current thread
382
+ RCTBlobManager *blobManager = (__bridge RCTBlobManager *)_blobManagerLookup();
384
383
  NSData *blobData =
385
384
  [blobManager resolve:[NSString stringWithUTF8String:blobId.c_str()]
386
385
  offset:(long)offset
@@ -141,6 +141,15 @@ public:
141
141
  // The metadata loads asynchronously, so that a remote asset never blocks
142
142
  // the calling thread; the getters report 0 until it has.
143
143
  AVAsset *asset = item.asset;
144
+ void (^readVideoTrack)(AVAssetTrack *) = ^(AVAssetTrack *track) {
145
+ if (track) {
146
+ CGSize size = track.naturalSize;
147
+ state->width = static_cast<uint32_t>(size.width);
148
+ state->height = static_cast<uint32_t>(size.height);
149
+ state->rotation = rotationFromTransform(track.preferredTransform);
150
+ state->frameRate = track.nominalFrameRate;
151
+ }
152
+ };
144
153
  #pragma clang diagnostic push
145
154
  #pragma clang diagnostic ignored "-Wdeprecated-declarations"
146
155
  [asset
@@ -155,17 +164,18 @@ public:
155
164
  }
156
165
  if ([asset statusOfValueForKey:@"tracks" error:nil] ==
157
166
  AVKeyValueStatusLoaded) {
158
- AVAssetTrack *track =
159
- [[asset tracksWithMediaType:AVMediaTypeVideo]
160
- firstObject];
161
- if (track) {
162
- CGSize size = track.naturalSize;
163
- state->width = static_cast<uint32_t>(size.width);
164
- state->height = static_cast<uint32_t>(size.height);
165
- state->rotation =
166
- rotationFromTransform(track.preferredTransform);
167
- state->frameRate = track.nominalFrameRate;
168
- }
167
+ #if TARGET_OS_VISION
168
+ // tracksWithMediaType: is unavailable on visionOS.
169
+ [asset loadTracksWithMediaType:AVMediaTypeVideo
170
+ completionHandler:^(
171
+ NSArray<AVAssetTrack *> *tracks,
172
+ NSError * /*error*/) {
173
+ readVideoTrack(tracks.firstObject);
174
+ }];
175
+ #else
176
+ readVideoTrack(
177
+ [[asset tracksWithMediaType:AVMediaTypeVideo] firstObject]);
178
+ #endif
169
179
  }
170
180
  }];
171
181
  #pragma clang diagnostic pop
@@ -19,9 +19,18 @@ namespace rnwgpu {
19
19
  // "display-p3" the layer is tagged Display P3 instead, or its extended range
20
20
  // variant on a float format, so the values reach a wide-gamut display without
21
21
  // being converted from sRGB first.
22
+ //
23
+ // GPUCanvasConfiguration.toneMapping.mode says what happens to values above
24
+ // 1.0 on a float canvas. Core Animation keeps an extended colorspace inside
25
+ // the display's SDR range unless the layer asks for extended dynamic range
26
+ // content, so without it the values clip at white: that is "standard". For
27
+ // "extended" the layer opts in, and the values use the display's headroom
28
+ // (EDR) as they do in a browser. An 8-bit canvas cannot hold values above 1.0,
29
+ // so the flag only applies to a float format.
22
30
  void applyCAMetalLayerColorSpace(void *nativeSurface,
23
31
  wgpu::TextureFormat format,
24
- CanvasColorSpace canvasColorSpace) {
32
+ CanvasColorSpace canvasColorSpace,
33
+ CanvasToneMappingMode toneMappingMode) {
25
34
  CALayer *layer = (__bridge CALayer *)nativeSurface;
26
35
  if (![layer isKindOfClass:[CAMetalLayer class]]) {
27
36
  return;
@@ -43,6 +52,20 @@ void applyCAMetalLayerColorSpace(void *nativeSurface,
43
52
  // 8-bit sRGB canvas.
44
53
  metalLayer.colorspace = nil;
45
54
  }
55
+ const bool extendedRange =
56
+ isFloat && toneMappingMode == CanvasToneMappingMode::Extended;
57
+ // The property does not exist on tvOS (the SDK marks it unavailable, which
58
+ // @available cannot work around), so it is compiled only where it exists.
59
+ // On tvOS "extended" behaves like "standard".
60
+ #if TARGET_OS_IOS || TARGET_OS_OSX || TARGET_OS_VISION
61
+ if (@available(iOS 16.0, macOS 10.11, visionOS 1.0, *)) {
62
+ if (metalLayer.wantsExtendedDynamicRangeContent != extendedRange) {
63
+ metalLayer.wantsExtendedDynamicRangeContent = extendedRange;
64
+ }
65
+ }
66
+ #else
67
+ (void)extendedRange;
68
+ #endif
46
69
  // The change must be set synchronously so the first present already sees
47
70
  // it, and it must reach the render server. On a non-main thread (RN JS or
48
71
  // worklet runtime) the property lands in that thread's implicit
@@ -1,5 +1,6 @@
1
1
  #import "WebGPUModule.h"
2
2
  #include "ApplePlatformContext.h"
3
+ #include "DawnBlobCache.h"
3
4
  #import "GPUCanvasContext.h"
4
5
  #import "RNWGUIKit.h"
5
6
 
@@ -37,6 +38,10 @@ static std::shared_ptr<rnwgpu::RNWebGPUManager> webgpuManager;
37
38
  // (Fabric component views included), which drawElementImageToTexture needs.
38
39
  @synthesize viewRegistry_DEPRECATED = _viewRegistry_DEPRECATED;
39
40
 
41
+ // Synthesize the module registry so the TurboModule infra injects it too. It
42
+ // reaches React Native's BlobModule in both Bridge and Bridgeless mode.
43
+ @synthesize moduleRegistry = _moduleRegistry;
44
+
40
45
  + (std::shared_ptr<rnwgpu::RNWebGPUManager>)getManager {
41
46
  return webgpuManager;
42
47
  }
@@ -93,8 +98,32 @@ RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(install) {
93
98
  [strongSelf.viewRegistry_DEPRECATED viewForReactTag:@(tag)];
94
99
  return (__bridge void *)view;
95
100
  };
101
+ rnwgpu::ApplePlatformContext::BlobManagerLookup blobManagerLookup =
102
+ [weakSelf]() -> void * {
103
+ WebGPUModule *strongSelf = weakSelf;
104
+ if (strongSelf == nil) {
105
+ return nullptr;
106
+ }
107
+ return (__bridge void *)[strongSelf.moduleRegistry
108
+ moduleForName:"BlobModule"];
109
+ };
96
110
  std::shared_ptr<rnwgpu::PlatformContext> platformContext =
97
- std::make_shared<rnwgpu::ApplePlatformContext>(std::move(viewLookup));
111
+ std::make_shared<rnwgpu::ApplePlatformContext>(
112
+ std::move(viewLookup), std::move(blobManagerLookup));
113
+ // Dawn keeps the pipelines it compiles here, so a relaunch does not compile
114
+ // them again (see DawnBlobCache). The caches directory is per app on iOS but
115
+ // shared on macOS, hence the bundle identifier.
116
+ NSString *caches = NSSearchPathForDirectoriesInDomains(NSCachesDirectory,
117
+ NSUserDomainMask, YES)
118
+ .firstObject;
119
+ NSString *bundle = NSBundle.mainBundle.bundleIdentifier;
120
+ if (caches != nil) {
121
+ if (bundle != nil) {
122
+ caches = [caches stringByAppendingPathComponent:bundle];
123
+ }
124
+ rnwgpu::DawnBlobCache::setDirectory(
125
+ [caches stringByAppendingPathComponent:@"webgpu-dawn"].UTF8String);
126
+ }
98
127
  webgpuManager = std::make_shared<rnwgpu::RNWebGPUManager>(runtime, jsInvoker,
99
128
  platformContext);
100
129
  return @true;
@@ -41,4 +41,8 @@ void Promise::reject(std::string message) {
41
41
  _rejecter.call(runtime, error.value());
42
42
  }
43
43
 
44
+ void Promise::reject(const jsi::JSError& error) {
45
+ _rejecter.call(runtime, error.value());
46
+ }
47
+
44
48
  } // namespace rnwgpu
package/cpp/jsi/Promise.h CHANGED
@@ -15,7 +15,10 @@ public:
15
15
  Promise(jsi::Runtime& runtime, jsi::Function&& resolver, jsi::Function&& rejecter);
16
16
 
17
17
  void resolve(jsi::Value&& result);
18
+ /** Rejects with a new Error carrying `error` as its message. */
18
19
  void reject(std::string error);
20
+ /** Rejects with the JS error `error` carries, as thrown. */
21
+ void reject(const jsi::JSError& error);
19
22
 
20
23
  public:
21
24
  jsi::Runtime& runtime;
@@ -6,4 +6,9 @@ namespace rnwgpu {
6
6
  // values are interpreted when it is composited.
7
7
  enum class CanvasColorSpace { SRGB, DisplayP3 };
8
8
 
9
+ // GPUCanvasConfiguration.toneMapping.mode (GPUCanvasToneMappingMode): whether
10
+ // values above 1.0 on a float canvas are clamped to the display's SDR range
11
+ // ("standard", the default) or reach its extended range ("extended").
12
+ enum class CanvasToneMappingMode { Standard, Extended };
13
+
9
14
  } // namespace rnwgpu