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.
- package/android/cpp/AndroidPlatformContext.h +108 -2
- package/android/cpp/cpp-adapter.cpp +5 -2
- package/android/src/main/java/com/webgpu/WebGPUModule.java +8 -2
- package/cpp/rnwgpu/PlatformContext.h +14 -1
- package/cpp/rnwgpu/RNWebGPUManager.cpp +6 -9
- package/cpp/rnwgpu/RNWebGPUManager.h +0 -3
- package/cpp/rnwgpu/api/GPU.cpp +33 -52
- package/cpp/rnwgpu/api/GPUAdapter.cpp +92 -92
- package/cpp/rnwgpu/api/GPUBuffer.h +1 -1
- package/cpp/rnwgpu/api/GPUQueue.cpp +45 -2
- package/cpp/rnwgpu/api/ImageBitmap.h +34 -0
- package/cpp/rnwgpu/api/RNWebGPU.h +100 -0
- package/cpp/rnwgpu/api/descriptors/GPUImageCopyExternalImage.h +14 -4
- package/ios/IOSPlatformContext.h +3 -0
- package/ios/IOSPlatformContext.mm +50 -0
- package/lib/commonjs/Canvas.js +1 -3
- package/lib/commonjs/Canvas.js.map +1 -1
- package/lib/commonjs/index.js +25 -0
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/utils.js +39 -0
- package/lib/commonjs/utils.js.map +1 -0
- package/lib/module/Canvas.js +1 -3
- package/lib/module/Canvas.js.map +1 -1
- package/lib/module/index.js +14 -0
- package/lib/module/index.js.map +1 -1
- package/lib/module/utils.js +31 -0
- package/lib/module/utils.js.map +1 -0
- package/lib/typescript/lib/commonjs/utils.d.ts +5 -0
- package/lib/typescript/lib/commonjs/utils.d.ts.map +1 -0
- package/lib/typescript/lib/module/Canvas.d.ts.map +1 -1
- package/lib/typescript/lib/module/index.d.ts +1 -0
- package/lib/typescript/lib/module/utils.d.ts +3 -0
- package/lib/typescript/lib/module/utils.d.ts.map +1 -0
- package/lib/typescript/src/Canvas.d.ts +7 -0
- package/lib/typescript/src/Canvas.d.ts.map +1 -1
- package/lib/typescript/src/index.d.ts +1 -0
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/lib/typescript/src/utils.d.ts +7 -0
- package/lib/typescript/src/utils.d.ts.map +1 -0
- package/libs/ios/arm64_iphoneos/libwebgpu_dawn.a +0 -0
- package/libs/ios/arm64_iphonesimulator/libwebgpu_dawn.a +0 -0
- package/libs/ios/libwebgpu_dawn.a +0 -0
- package/libs/ios/libwebgpu_dawn.xcframework/Info.plist +5 -5
- package/libs/ios/libwebgpu_dawn.xcframework/ios-arm64/libwebgpu_dawn.a +0 -0
- package/libs/ios/libwebgpu_dawn.xcframework/ios-arm64_x86_64-simulator/libwebgpu_dawn.a +0 -0
- package/libs/ios/x86_64_iphonesimulator/libwebgpu_dawn.a +0 -0
- package/package.json +1 -1
- package/src/Canvas.tsx +8 -3
- package/src/index.tsx +19 -0
- package/src/utils.ts +40 -0
- package/cpp/rnwgpu/api/ImageData.h +0 -50
- 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
|
-
|
|
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
|