react-native-wgpu 0.1.2 → 0.1.3

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 (52) hide show
  1. package/android/cpp/AndroidPlatformContext.h +108 -2
  2. package/android/cpp/cpp-adapter.cpp +5 -2
  3. package/android/src/main/java/com/webgpu/WebGPUModule.java +8 -2
  4. package/cpp/rnwgpu/PlatformContext.h +14 -1
  5. package/cpp/rnwgpu/RNWebGPUManager.cpp +6 -9
  6. package/cpp/rnwgpu/RNWebGPUManager.h +0 -3
  7. package/cpp/rnwgpu/api/GPU.cpp +33 -52
  8. package/cpp/rnwgpu/api/GPUAdapter.cpp +92 -92
  9. package/cpp/rnwgpu/api/GPUBuffer.h +1 -1
  10. package/cpp/rnwgpu/api/GPUQueue.cpp +45 -2
  11. package/cpp/rnwgpu/api/ImageBitmap.h +34 -0
  12. package/cpp/rnwgpu/api/RNWebGPU.h +100 -0
  13. package/cpp/rnwgpu/api/descriptors/GPUImageCopyExternalImage.h +14 -4
  14. package/ios/IOSPlatformContext.h +3 -0
  15. package/ios/IOSPlatformContext.mm +50 -0
  16. package/lib/commonjs/Canvas.js +1 -3
  17. package/lib/commonjs/Canvas.js.map +1 -1
  18. package/lib/commonjs/index.js +25 -0
  19. package/lib/commonjs/index.js.map +1 -1
  20. package/lib/commonjs/utils.js +39 -0
  21. package/lib/commonjs/utils.js.map +1 -0
  22. package/lib/module/Canvas.js +1 -3
  23. package/lib/module/Canvas.js.map +1 -1
  24. package/lib/module/index.js +14 -0
  25. package/lib/module/index.js.map +1 -1
  26. package/lib/module/utils.js +31 -0
  27. package/lib/module/utils.js.map +1 -0
  28. package/lib/typescript/lib/commonjs/utils.d.ts +5 -0
  29. package/lib/typescript/lib/commonjs/utils.d.ts.map +1 -0
  30. package/lib/typescript/lib/module/Canvas.d.ts.map +1 -1
  31. package/lib/typescript/lib/module/index.d.ts +1 -0
  32. package/lib/typescript/lib/module/utils.d.ts +3 -0
  33. package/lib/typescript/lib/module/utils.d.ts.map +1 -0
  34. package/lib/typescript/src/Canvas.d.ts +7 -0
  35. package/lib/typescript/src/Canvas.d.ts.map +1 -1
  36. package/lib/typescript/src/index.d.ts +1 -0
  37. package/lib/typescript/src/index.d.ts.map +1 -1
  38. package/lib/typescript/src/utils.d.ts +7 -0
  39. package/lib/typescript/src/utils.d.ts.map +1 -0
  40. package/libs/ios/arm64_iphoneos/libwebgpu_dawn.a +0 -0
  41. package/libs/ios/arm64_iphonesimulator/libwebgpu_dawn.a +0 -0
  42. package/libs/ios/libwebgpu_dawn.a +0 -0
  43. package/libs/ios/libwebgpu_dawn.xcframework/Info.plist +5 -5
  44. package/libs/ios/libwebgpu_dawn.xcframework/ios-arm64/libwebgpu_dawn.a +0 -0
  45. package/libs/ios/libwebgpu_dawn.xcframework/ios-arm64_x86_64-simulator/libwebgpu_dawn.a +0 -0
  46. package/libs/ios/x86_64_iphonesimulator/libwebgpu_dawn.a +0 -0
  47. package/package.json +1 -1
  48. package/src/Canvas.tsx +8 -3
  49. package/src/index.tsx +19 -0
  50. package/src/utils.ts +40 -0
  51. package/cpp/rnwgpu/api/ImageData.h +0 -50
  52. package/cpp/rnwgpu/api/Navigator.h +0 -46
package/src/Canvas.tsx CHANGED
@@ -12,6 +12,13 @@ function generateContextId() {
12
12
  declare global {
13
13
  // eslint-disable-next-line no-var
14
14
  var __WebGPUContextRegistry: Record<number, NativeCanvas>;
15
+ // eslint-disable-next-line no-var
16
+ var RNWebGPU: {
17
+ gpu: GPU;
18
+ MakeWebGPUCanvasContext: (nativeCanvas: NativeCanvas) => CanvasContext;
19
+ DecodeToUTF8: (buffer: NodeJS.ArrayBufferView | ArrayBuffer) => string;
20
+ createImageBitmap: typeof createImageBitmap;
21
+ };
15
22
  }
16
23
 
17
24
  export interface NativeCanvas {
@@ -51,9 +58,7 @@ export const Canvas = forwardRef<CanvasRef, ViewProps>((props, ref) => {
51
58
  if (!nativeSurface) {
52
59
  return null;
53
60
  }
54
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
55
- // @ts-expect-error
56
- const ctx = navigator.MakeWebGPUCanvasContext(nativeSurface);
61
+ const ctx = RNWebGPU.MakeWebGPUCanvasContext(nativeSurface);
57
62
  return ctx;
58
63
  },
59
64
  }));
package/src/index.tsx CHANGED
@@ -3,6 +3,7 @@ import WebGPUNativeModule from "./NativeWebGPUModule";
3
3
 
4
4
  export * from "./Canvas";
5
5
  export * from "./WebGPUViewNativeComponent";
6
+ export * from "./utils";
6
7
  export { default as WebGPUModule } from "./NativeWebGPUModule";
7
8
 
8
9
  const GPU: any = {};
@@ -175,3 +176,21 @@ global.GPUTexture = GPUTexture;
175
176
  global.GPUTextureView = GPUTextureView;
176
177
 
177
178
  WebGPUNativeModule.install();
179
+
180
+ if (!navigator) {
181
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
182
+ // @ts-expect-error
183
+ navigator = {};
184
+ }
185
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
186
+ // @ts-expect-error
187
+ navigator.gpu = RNWebGPU.gpu;
188
+
189
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
190
+ // @ts-expect-error
191
+ navigator.userAgent = "react-native";
192
+
193
+ global.createImageBitmap =
194
+ global.createImageBitmap ??
195
+ ((...params: Parameters<typeof createImageBitmap>) =>
196
+ new Promise((resolve) => resolve(RNWebGPU.createImageBitmap(...params))));
package/src/utils.ts ADDED
@@ -0,0 +1,40 @@
1
+ import { useEffect, useRef } from "react";
2
+
3
+ import type { CanvasRef } from "./Canvas";
4
+
5
+ type Unsubscribe = () => void;
6
+
7
+ export const warnIfNotHardwareAccelerated = (adapter: GPUAdapter) => {
8
+ if (adapter.info.architecture === "swiftshader") {
9
+ console.warn(
10
+ "GPUAdapter is not hardware accelerated. This is common on Android emulators. Rendering will be slow.",
11
+ );
12
+ }
13
+ };
14
+
15
+ export const useCanvasEffect = (
16
+ effect: () => void | Unsubscribe | Promise<void | Unsubscribe>,
17
+ ) => {
18
+ const ref = useRef<CanvasRef>(null);
19
+ const unsubscribe = useRef<Unsubscribe>();
20
+ useEffect(() => {
21
+ requestAnimationFrame(async () => {
22
+ // const adapter = await navigator.gpu.requestAdapter();
23
+ // if (!adapter) {
24
+ // return;
25
+ // }
26
+ // const device = await adapter.requestDevice();
27
+ const unsub = await effect();
28
+ if (unsub) {
29
+ unsubscribe.current = unsub;
30
+ }
31
+ });
32
+ return () => {
33
+ if (unsubscribe.current) {
34
+ unsubscribe.current();
35
+ }
36
+ };
37
+ // eslint-disable-next-line react-hooks/exhaustive-deps
38
+ }, []);
39
+ return ref;
40
+ };
@@ -1,50 +0,0 @@
1
- #pragma once
2
-
3
- #include <memory>
4
-
5
- #include "ArrayBuffer.h"
6
-
7
- namespace rnwgpu {
8
-
9
- struct ImageData {
10
- std::shared_ptr<ArrayBuffer> data;
11
- size_t width;
12
- size_t height;
13
- };
14
-
15
- } // namespace rnwgpu
16
-
17
- namespace margelo {
18
-
19
- using namespace rnwgpu; // NOLINT(build/namespaces)
20
-
21
- template <> struct JSIConverter<std::shared_ptr<rnwgpu::ImageData>> {
22
- static std::shared_ptr<rnwgpu::ImageData>
23
- fromJSI(jsi::Runtime &runtime, const jsi::Value &arg, bool outOfBounds) {
24
- auto result = std::make_unique<rnwgpu::ImageData>();
25
- if (!outOfBounds && arg.isObject()) {
26
- auto obj = arg.getObject(runtime);
27
- if (obj.hasProperty(runtime, "data")) {
28
- auto prop = obj.getProperty(runtime, "data");
29
- result->data = JSIConverter<std::shared_ptr<ArrayBuffer>>::fromJSI(
30
- runtime, prop, false);
31
- }
32
- if (obj.hasProperty(runtime, "width")) {
33
- auto prop = obj.getProperty(runtime, "width");
34
- result->width = prop.getNumber();
35
- }
36
- if (obj.hasProperty(runtime, "height")) {
37
- auto prop = obj.getProperty(runtime, "height");
38
- result->height = prop.getNumber();
39
- }
40
- }
41
-
42
- return result;
43
- }
44
- static jsi::Value toJSI(jsi::Runtime &runtime,
45
- std::shared_ptr<rnwgpu::ImageData> arg) {
46
- throw std::runtime_error("Invalid ImageData::toJSI()");
47
- }
48
- };
49
-
50
- } // namespace margelo
@@ -1,46 +0,0 @@
1
- #pragma once
2
-
3
- #include <memory>
4
-
5
- #include "GPU.h"
6
- #include "GPUCanvasContext.h"
7
-
8
- namespace rnwgpu {
9
-
10
- namespace m = margelo;
11
-
12
- class Navigator : public m::HybridObject {
13
- public:
14
- explicit Navigator(std::shared_ptr<GPU> gpu,
15
- std::shared_ptr<PlatformContext> platformContext)
16
- : HybridObject("Navigator"), _gpu(gpu),
17
- _platformContext(platformContext) {}
18
-
19
- std::shared_ptr<GPU> getGPU() { return _gpu; }
20
-
21
- std::shared_ptr<GPUCanvasContext>
22
- MakeWebGPUCanvasContext(std::shared_ptr<Canvas> canvas) {
23
- auto nativeSurface = canvas->getSurface();
24
- auto width = canvas->getWidth();
25
- auto height = canvas->getHeight();
26
- auto surface = _platformContext->makeSurface(
27
- _gpu->get(), reinterpret_cast<void *>(nativeSurface), width, height);
28
- if (surface == nullptr) {
29
- throw std::runtime_error("null surface");
30
- }
31
- auto ctx = std::make_shared<GPUCanvasContext>(surface, canvas);
32
- return ctx;
33
- }
34
-
35
- void loadHybridMethods() override {
36
- registerHybridGetter("gpu", &Navigator::getGPU, this);
37
- registerHybridMethod("MakeWebGPUCanvasContext",
38
- &Navigator::MakeWebGPUCanvasContext, this);
39
- }
40
-
41
- private:
42
- std::shared_ptr<GPU> _gpu;
43
- std::shared_ptr<PlatformContext> _platformContext;
44
- };
45
-
46
- } // namespace rnwgpu