react-native-wgpu 0.1.11 → 0.1.13

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 (67) hide show
  1. package/{ios/IOSPlatformContext.h → apple/ApplePlatformContext.h} +4 -4
  2. package/{ios/IOSPlatformContext.mm → apple/ApplePlatformContext.mm} +24 -6
  3. package/{ios → apple}/MetalView.h +3 -0
  4. package/apple/MetalView.mm +58 -0
  5. package/{ios → apple}/WebGPUModule.mm +2 -2
  6. package/{ios → apple}/WebGPUView.h +0 -3
  7. package/apple/WebGPUView.mm +68 -0
  8. package/cpp/rnwgpu/api/Convertors.h +0 -9
  9. package/cpp/rnwgpu/api/GPUCanvasContext.cpp +6 -5
  10. package/cpp/rnwgpu/api/descriptors/GPUCanvasConfiguration.h +7 -0
  11. package/cpp/webgpu/webgpu.h +4182 -5
  12. package/cpp/webgpu/webgpu_cpp.h +9009 -5
  13. package/lib/commonjs/hooks.js +61 -1
  14. package/lib/commonjs/hooks.js.map +1 -1
  15. package/lib/commonjs/index.js +0 -8
  16. package/lib/commonjs/index.js.map +1 -1
  17. package/lib/module/hooks.js +57 -1
  18. package/lib/module/hooks.js.map +1 -1
  19. package/lib/module/index.js +0 -5
  20. package/lib/module/index.js.map +1 -1
  21. package/lib/typescript/lib/commonjs/hooks.d.ts +11 -0
  22. package/lib/typescript/lib/commonjs/hooks.d.ts.map +1 -1
  23. package/lib/typescript/lib/commonjs/index.d.ts +0 -1
  24. package/lib/typescript/lib/commonjs/index.d.ts.map +1 -1
  25. package/lib/typescript/lib/module/hooks.d.ts +11 -0
  26. package/lib/typescript/lib/module/hooks.d.ts.map +1 -1
  27. package/lib/typescript/lib/module/index.d.ts +0 -1
  28. package/lib/typescript/lib/module/index.d.ts.map +1 -1
  29. package/lib/typescript/src/__tests__/Alpha.spec.d.ts +2 -0
  30. package/lib/typescript/src/__tests__/Alpha.spec.d.ts.map +1 -0
  31. package/lib/typescript/src/hooks.d.ts +18 -0
  32. package/lib/typescript/src/hooks.d.ts.map +1 -1
  33. package/lib/typescript/src/index.d.ts +0 -1
  34. package/lib/typescript/src/index.d.ts.map +1 -1
  35. package/libs/apple/arm64_iphoneos/libwebgpu_dawn.a +0 -0
  36. package/libs/apple/arm64_iphonesimulator/libwebgpu_dawn.a +0 -0
  37. package/libs/apple/arm64_xros/libwebgpu_dawn.a +0 -0
  38. package/libs/apple/arm64_xrsimulator/libwebgpu_dawn.a +0 -0
  39. package/libs/apple/iphonesimulator/libwebgpu_dawn.a +0 -0
  40. package/libs/apple/libwebgpu_dawn.xcframework/Info.plist +8 -8
  41. package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64/libwebgpu_dawn.a +0 -0
  42. package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64_x86_64-simulator/libwebgpu_dawn.a +0 -0
  43. package/libs/apple/libwebgpu_dawn.xcframework/macos-arm64_x86_64/libwebgpu_dawn.a +0 -0
  44. package/libs/apple/libwebgpu_dawn.xcframework/xros-arm64/libwebgpu_dawn.a +0 -0
  45. package/libs/apple/libwebgpu_dawn.xcframework/xros-arm64-simulator/libwebgpu_dawn.a +0 -0
  46. package/libs/apple/universal_macosx/libwebgpu_dawn.a +0 -0
  47. package/libs/apple/x86_64_iphonesimulator/libwebgpu_dawn.a +0 -0
  48. package/package.json +3 -3
  49. package/react-native-wgpu.podspec +1 -1
  50. package/src/__tests__/Alpha.spec.ts +28 -0
  51. package/src/__tests__/snapshots/semi-opaque-cyan.png +0 -0
  52. package/src/hooks.tsx +125 -0
  53. package/src/index.tsx +0 -8
  54. package/cpp/dawn/dawn_proc_table.h +0 -308
  55. package/cpp/dawn/webgpu.h +0 -4210
  56. package/cpp/dawn/webgpu_cpp.h +0 -9037
  57. package/cpp/dawn/webgpu_cpp_print.h +0 -2466
  58. package/cpp/dawn/wire/client/webgpu.h +0 -339
  59. package/cpp/dawn/wire/client/webgpu_cpp.h +0 -9177
  60. package/ios/MetalView.mm +0 -40
  61. package/ios/SurfaceUtils.h +0 -15
  62. package/ios/SurfaceUtils.mm +0 -34
  63. package/ios/WebGPUView.mm +0 -86
  64. package/ios/WebGPUViewComponentDescriptor.h +0 -61
  65. package/src/hooks.ts +0 -53
  66. /package/{ios → apple}/WebGPUModule.h +0 -0
  67. /package/{ios → apple}/WebGPUViewManager.mm +0 -0
@@ -4,10 +4,10 @@
4
4
 
5
5
  namespace rnwgpu {
6
6
 
7
- class IOSPlatformContext : public PlatformContext {
7
+ class ApplePlatformContext : public PlatformContext {
8
8
  public:
9
- IOSPlatformContext() = default;
10
- ~IOSPlatformContext() = default;
9
+ ApplePlatformContext();
10
+ ~ApplePlatformContext() = default;
11
11
 
12
12
  wgpu::Surface makeSurface(wgpu::Instance instance, void *surface, int width,
13
13
  int height) override;
@@ -16,4 +16,4 @@ public:
16
16
  double size) override;
17
17
  };
18
18
 
19
- } // namespace rnwgpu
19
+ } // namespace rnwgpu
@@ -1,4 +1,6 @@
1
- #include "IOSPlatformContext.h"
1
+ #include "ApplePlatformContext.h"
2
+
3
+ #include <TargetConditionals.h>
2
4
 
3
5
  #import <React/RCTBlobManager.h>
4
6
  #import <React/RCTBridge+Private.h>
@@ -9,9 +11,25 @@
9
11
 
10
12
  namespace rnwgpu {
11
13
 
12
- wgpu::Surface IOSPlatformContext::makeSurface(wgpu::Instance instance,
13
- void *surface, int width,
14
- int height) {
14
+ void isSimulatorWithAPIValidation() {
15
+ #if TARGET_OS_SIMULATOR
16
+ NSDictionary *environment = [[NSProcessInfo processInfo] environment];
17
+ NSString *metalDeviceWrapperType = environment[@"METAL_DEVICE_WRAPPER_TYPE"];
18
+
19
+ if ([metalDeviceWrapperType isEqualToString:@"1"]) {
20
+ throw std::runtime_error(
21
+ "To use React Native WebGPU project on the iOS simulator, you need to "
22
+ "disable the Metal validation API. In 'Edit Scheme,' uncheck 'Metal "
23
+ "Validation.'");
24
+ }
25
+ #endif
26
+ }
27
+
28
+ ApplePlatformContext::ApplePlatformContext() { isSimulatorWithAPIValidation(); }
29
+
30
+ wgpu::Surface ApplePlatformContext::makeSurface(wgpu::Instance instance,
31
+ void *surface, int width,
32
+ int height) {
15
33
  wgpu::SurfaceDescriptorFromMetalLayer metalSurfaceDesc;
16
34
  metalSurfaceDesc.layer = surface;
17
35
  wgpu::SurfaceDescriptor surfaceDescriptor;
@@ -19,8 +37,8 @@ wgpu::Surface IOSPlatformContext::makeSurface(wgpu::Instance instance,
19
37
  return instance.CreateSurface(&surfaceDescriptor);
20
38
  }
21
39
 
22
- ImageData IOSPlatformContext::createImageBitmap(std::string blobId,
23
- double offset, double size) {
40
+ ImageData ApplePlatformContext::createImageBitmap(std::string blobId,
41
+ double offset, double size) {
24
42
  RCTBlobManager *blobManager =
25
43
  [[RCTBridge currentBridge] moduleForClass:RCTBlobManager.class];
26
44
  NSData *blobData =
@@ -7,4 +7,7 @@
7
7
 
8
8
  @property NSNumber *contextId;
9
9
 
10
+ - (void)configure;
11
+ - (void)update;
12
+
10
13
  @end
@@ -0,0 +1,58 @@
1
+ #import "MetalView.h"
2
+ #import "webgpu_cpp.h"
3
+
4
+ #ifndef RCT_NEW_ARCH_ENABLED
5
+ #import <React/RCTViewManager.h>
6
+ #endif // RCT_NEW_ARCH_ENABLED
7
+
8
+ @implementation MetalView {
9
+ BOOL _isConfigured;
10
+ }
11
+
12
+ + (Class)layerClass {
13
+ return [CAMetalLayer class];
14
+ }
15
+
16
+ - (void)configure {
17
+ auto size = self.frame.size;
18
+ std::shared_ptr<rnwgpu::RNWebGPUManager> manager = [WebGPUModule getManager];
19
+ void *nativeSurface = (__bridge void *)self.layer;
20
+ auto &registry = rnwgpu::SurfaceRegistry::getInstance();
21
+ auto gpu = manager->_gpu;
22
+ auto surface = manager->_platformContext->makeSurface(
23
+ gpu, nativeSurface, size.width, size.height);
24
+ registry
25
+ .getSurfaceInfoOrCreate([_contextId intValue], gpu, size.width,
26
+ size.height)
27
+ ->switchToOnscreen(nativeSurface, surface);
28
+ }
29
+
30
+ - (void)update {
31
+ auto size = self.frame.size;
32
+ auto &registry = rnwgpu::SurfaceRegistry::getInstance();
33
+ registry.getSurfaceInfo([_contextId intValue])
34
+ ->resize(size.width, size.height);
35
+ }
36
+
37
+ - (void)dealloc {
38
+ auto &registry = rnwgpu::SurfaceRegistry::getInstance();
39
+ // Remove the surface info from the registry
40
+ registry.removeSurfaceInfo([_contextId intValue]);
41
+ }
42
+
43
+ #ifndef RCT_NEW_ARCH_ENABLED
44
+ // Paper only method
45
+ // TODO: this method is wrong, it appears to call configureSurface instead of
46
+ // updateSurface sometimes
47
+ - (void)reactSetFrame:(CGRect)frame {
48
+ [super reactSetFrame:frame];
49
+ if (!_isConfigured) {
50
+ [self configure];
51
+ _isConfigured = YES;
52
+ } else {
53
+ [self update];
54
+ }
55
+ }
56
+ #endif // RCT_NEW_ARCH_ENABLED
57
+
58
+ @end
@@ -1,6 +1,6 @@
1
1
  #import "WebGPUModule.h"
2
+ #include "ApplePlatformContext.h"
2
3
  #import "GPUCanvasContext.h"
3
- #include "IOSPlatformContext.h"
4
4
 
5
5
  #import <React/RCTBridge+Private.h>
6
6
  #import <React/RCTLog.h>
@@ -67,7 +67,7 @@ RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(install) {
67
67
  jsInvoker = cxxBridge.jsCallInvoker;
68
68
  }
69
69
  std::shared_ptr<rnwgpu::PlatformContext> platformContext =
70
- std::make_shared<rnwgpu::IOSPlatformContext>();
70
+ std::make_shared<rnwgpu::ApplePlatformContext>();
71
71
  // TODO: remove allocation here
72
72
  webgpuManager = std::make_shared<rnwgpu::RNWebGPUManager>(runtime, jsInvoker,
73
73
  platformContext);
@@ -8,9 +8,6 @@
8
8
  NS_ASSUME_NONNULL_BEGIN
9
9
 
10
10
  @interface WebGPUView : RCTViewComponentView
11
- + (void)registerMetalView:(MetalView *)metalView
12
- withContextId:(NSNumber *)contextId;
13
- + (bool)isContextRegisterd:(NSNumber *)contextId;
14
11
  @end
15
12
 
16
13
  NS_ASSUME_NONNULL_END
@@ -0,0 +1,68 @@
1
+ #ifdef RCT_NEW_ARCH_ENABLED
2
+ #import "WebGPUView.h"
3
+
4
+ #import <react/renderer/components/RNWgpuViewSpec/ComponentDescriptors.h>
5
+ #import <react/renderer/components/RNWgpuViewSpec/EventEmitters.h>
6
+ #import <react/renderer/components/RNWgpuViewSpec/Props.h>
7
+ #import <react/renderer/components/RNWgpuViewSpec/RCTComponentViewHelpers.h>
8
+
9
+ #import "MetalView.h"
10
+ #import "RCTFabricComponentsPlugins.h"
11
+ #import "Utils.h"
12
+
13
+ using namespace facebook::react;
14
+
15
+ @implementation WebGPUView
16
+
17
+ + (ComponentDescriptorProvider)componentDescriptorProvider {
18
+ return concreteComponentDescriptorProvider<WebGPUViewComponentDescriptor>();
19
+ }
20
+
21
+ - (void)prepareForRecycle {
22
+ [super prepareForRecycle];
23
+ /*
24
+ It's important to destroy the Metal Layer before releasing a view
25
+ to the recycled pool to prevent displaying outdated content from
26
+ the last usage in the new context.
27
+ */
28
+ self.contentView = nil;
29
+ }
30
+
31
+ - (MetalView *)getContentView {
32
+ if (!self.contentView) {
33
+ self.contentView = [MetalView new];
34
+ }
35
+ return (MetalView *)self.contentView;
36
+ }
37
+
38
+ - (void)updateProps:(const Props::Shared &)props
39
+ oldProps:(const Props::Shared &)oldProps {
40
+ const auto &oldViewProps =
41
+ *std::static_pointer_cast<const WebGPUViewProps>(_props);
42
+ const auto &newViewProps =
43
+ *std::static_pointer_cast<const WebGPUViewProps>(props);
44
+
45
+ if (newViewProps.contextId != oldViewProps.contextId) {
46
+ /*
47
+ The context is set only once during mounting the component
48
+ and never changes because it isn't available for users to modify.
49
+ */
50
+ MetalView *metalView = [MetalView new];
51
+ self.contentView = metalView;
52
+ [metalView setContextId:@(newViewProps.contextId)];
53
+ [metalView configure];
54
+ }
55
+
56
+ [super updateProps:props oldProps:oldProps];
57
+ }
58
+
59
+ - (void)updateLayoutMetrics:(const LayoutMetrics &)layoutMetrics
60
+ oldLayoutMetrics:(const LayoutMetrics &)oldLayoutMetrics {
61
+ [super updateLayoutMetrics:layoutMetrics oldLayoutMetrics:oldLayoutMetrics];
62
+ [(MetalView *)self.contentView update];
63
+ }
64
+
65
+ Class<RCTComponentViewProtocol> WebGPUViewCls(void) { return WebGPUView.class; }
66
+
67
+ @end
68
+ #endif
@@ -280,15 +280,6 @@ public:
280
280
  Convert(out.label, in.label);
281
281
  }
282
282
 
283
- // [[nodiscard]] bool Convert(wgpu::CanvasConfiguration &out,
284
- // const GPUCanvasConfiguration &in) {
285
- // return Convert(out.device, in.device) && Convert(out.format, in.format)
286
- // &&
287
- // Convert(out.usage, in.usage) && Convert(out.viewFormats,
288
- // in.viewFormats) && Convert(out.colorSpace, in.colorSpace) &&
289
- // Convert(out.alphaMode, in.alphaMode);
290
- // }
291
-
292
283
  [[nodiscard]] bool Convert(wgpu::Color &out, const GPUColor &in) {
293
284
  return Convert(out.r, in.r) && Convert(out.g, in.g) &&
294
285
  Convert(out.b, in.b) && Convert(out.a, in.a);
@@ -16,12 +16,13 @@ void GPUCanvasContext::configure(
16
16
  throw std::runtime_error("Error with SurfaceConfiguration");
17
17
  }
18
18
  }
19
- if (!conv(surfaceConfiguration.format, configuration->format)) {
20
- throw std::runtime_error("Error with SurfaceConfiguration");
21
- }
22
- if (!conv(surfaceConfiguration.usage, configuration->usage)) {
23
- throw std::runtime_error("Error with SurfaceConfiguration");
19
+ if (!conv(surfaceConfiguration.usage, configuration->usage) || !conv(surfaceConfiguration.format, configuration->format)) {
20
+ throw std::runtime_error("Error with SurfaceConfiguration");
24
21
  }
22
+
23
+ #ifdef __APPLE__
24
+ surfaceConfiguration.alphaMode = configuration->alphaMode;
25
+ #endif
25
26
  _surfaceInfo->configure(surfaceConfiguration);
26
27
  }
27
28
 
@@ -21,6 +21,7 @@ struct GPUCanvasConfiguration {
21
21
  std::optional<double> usage; // GPUTextureUsageFlags
22
22
  std::optional<std::vector<wgpu::TextureFormat>>
23
23
  viewFormats; // Iterable<GPUTextureFormat>
24
+ wgpu::CompositeAlphaMode alphaMode = wgpu::CompositeAlphaMode::Opaque;
24
25
  };
25
26
 
26
27
  } // namespace rnwgpu
@@ -58,6 +59,12 @@ struct JSIConverter<std::shared_ptr<rnwgpu::GPUCanvasConfiguration>> {
58
59
  prop,
59
60
  false);
60
61
  }
62
+ if (value.hasProperty(runtime, "alphaMode")) {
63
+ auto prop = value.getProperty(runtime, "alphaMode").asString(runtime).utf8(runtime);
64
+ if (prop == "premultiplied") {
65
+ result->alphaMode = wgpu::CompositeAlphaMode::Premultiplied;
66
+ }
67
+ }
61
68
  }
62
69
 
63
70
  return result;