react-native-wgpu 0.1.0 → 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/README.md +221 -13
- package/android/CMakeLists.txt +15 -1
- package/android/build.gradle +0 -18
- package/android/cpp/AndroidPlatformContext.h +132 -0
- package/android/cpp/cpp-adapter.cpp +70 -4
- package/android/src/main/java/com/webgpu/WebGPUModule.java +69 -21
- package/android/src/main/java/com/webgpu/WebGPUView.java +63 -8
- package/android/src/main/java/com/webgpu/WebGPUViewManager.java +5 -6
- package/android/src/newarch/{WgpuViewManagerSpec.java → com/webgpu/WebGPUViewManagerSpec.java} +5 -5
- package/android/src/oldarch/com/webgpu/NativeWebGPUModuleSpec.java +23 -0
- package/android/src/oldarch/{WebGPUViewManagerSpec.java → com/webgpu/WebGPUViewManagerSpec.java} +1 -1
- package/cpp/{Logger.h → WGPULogger.h} +13 -6
- package/cpp/dawn/webgpu.h +9 -0
- package/cpp/dawn/webgpu_cpp.h +95 -43
- package/cpp/dawn/webgpu_cpp_print.h +6 -0
- package/cpp/dawn/wire/client/webgpu_cpp.h +80 -43
- package/cpp/jsi/RNFHybridObject.cpp +1 -1
- package/cpp/jsi/RNFHybridObject.h +4 -4
- package/cpp/jsi/RNFJSIConverter.h +204 -17
- package/cpp/rnwgpu/ArrayBuffer.h +72 -0
- package/cpp/rnwgpu/PlatformContext.h +29 -0
- package/cpp/rnwgpu/RNWebGPUManager.cpp +13 -11
- package/cpp/rnwgpu/RNWebGPUManager.h +9 -1
- package/cpp/rnwgpu/SurfaceRegistry.h +36 -0
- package/cpp/rnwgpu/api/AsyncRunner.h +30 -0
- package/cpp/rnwgpu/api/Canvas.h +55 -0
- package/cpp/rnwgpu/api/Convertors.h +765 -0
- package/cpp/rnwgpu/api/External.h +12 -0
- package/cpp/rnwgpu/api/GPU.cpp +81 -23
- package/cpp/rnwgpu/api/GPU.h +24 -6
- package/cpp/rnwgpu/api/GPUAdapter.cpp +138 -24
- package/cpp/rnwgpu/api/GPUAdapter.h +24 -5
- package/cpp/rnwgpu/api/GPUAdapterInfo.h +19 -7
- package/cpp/rnwgpu/api/GPUBindGroup.cpp +3 -0
- package/cpp/rnwgpu/api/GPUBindGroup.h +11 -4
- package/cpp/rnwgpu/api/GPUBindGroupLayout.h +11 -4
- package/cpp/rnwgpu/api/GPUBuffer.cpp +82 -7
- package/cpp/rnwgpu/api/GPUBuffer.h +44 -8
- package/cpp/rnwgpu/api/GPUCanvasContext.cpp +63 -0
- package/cpp/rnwgpu/api/GPUCanvasContext.h +51 -8
- package/cpp/rnwgpu/api/GPUCommandBuffer.h +11 -4
- package/cpp/rnwgpu/api/GPUCommandEncoder.cpp +177 -0
- package/cpp/rnwgpu/api/GPUCommandEncoder.h +75 -3
- package/cpp/rnwgpu/api/GPUCompilationInfo.h +65 -8
- package/cpp/rnwgpu/api/GPUCompilationMessage.h +6 -4
- package/cpp/rnwgpu/api/GPUComputePassEncoder.cpp +58 -0
- package/cpp/rnwgpu/api/GPUComputePassEncoder.h +49 -3
- package/cpp/rnwgpu/api/GPUComputePipeline.cpp +11 -0
- package/cpp/rnwgpu/api/GPUComputePipeline.h +18 -3
- package/cpp/rnwgpu/api/GPUDevice.cpp +330 -3
- package/cpp/rnwgpu/api/GPUDevice.h +119 -5
- package/cpp/rnwgpu/api/GPUDeviceLostInfo.cpp +6 -0
- package/cpp/rnwgpu/api/GPUDeviceLostInfo.h +14 -7
- package/cpp/rnwgpu/api/GPUError.h +42 -0
- package/cpp/rnwgpu/api/GPUExtent3D.h +69 -0
- package/cpp/rnwgpu/api/GPUExternalTexture.h +11 -4
- package/cpp/rnwgpu/api/GPUFeatures.h +221 -0
- package/cpp/rnwgpu/api/GPUOrigin2D.h +59 -0
- package/cpp/rnwgpu/api/GPUOrigin3D.h +66 -0
- package/cpp/rnwgpu/api/GPUPipelineLayout.h +11 -4
- package/cpp/rnwgpu/api/GPUQuerySet.cpp +11 -0
- package/cpp/rnwgpu/api/GPUQuerySet.h +19 -5
- package/cpp/rnwgpu/api/GPUQueue.cpp +153 -0
- package/cpp/rnwgpu/api/GPUQueue.h +45 -4
- package/cpp/rnwgpu/api/GPURenderBundle.h +11 -4
- package/cpp/rnwgpu/api/GPURenderBundleEncoder.cpp +128 -0
- package/cpp/rnwgpu/api/GPURenderBundleEncoder.h +72 -3
- package/cpp/rnwgpu/api/GPURenderPassEncoder.cpp +161 -0
- package/cpp/rnwgpu/api/GPURenderPassEncoder.h +93 -3
- package/cpp/rnwgpu/api/GPURenderPipeline.cpp +13 -0
- package/cpp/rnwgpu/api/GPURenderPipeline.h +18 -3
- package/cpp/rnwgpu/api/GPUSampler.h +11 -4
- package/cpp/rnwgpu/api/GPUShaderModule.cpp +39 -0
- package/cpp/rnwgpu/api/GPUShaderModule.h +23 -4
- package/cpp/rnwgpu/api/GPUSupportedLimits.cpp +133 -0
- package/cpp/rnwgpu/api/GPUSupportedLimits.h +128 -4
- package/cpp/rnwgpu/api/GPUTexture.cpp +47 -0
- package/cpp/rnwgpu/api/GPUTexture.h +38 -4
- package/cpp/rnwgpu/api/GPUTextureView.h +11 -4
- package/cpp/rnwgpu/api/ImageBitmap.h +34 -0
- package/cpp/rnwgpu/api/RNWebGPU.h +100 -0
- package/cpp/rnwgpu/api/WebGPUCanvasContextFactory.h +59 -0
- package/cpp/rnwgpu/api/descriptors/GPUBindGroupDescriptor.h +31 -43
- package/cpp/rnwgpu/api/descriptors/GPUBindGroupEntry.h +40 -31
- package/cpp/rnwgpu/api/descriptors/GPUBindGroupLayoutDescriptor.h +24 -32
- package/cpp/rnwgpu/api/descriptors/GPUBindGroupLayoutEntry.h +56 -49
- package/cpp/rnwgpu/api/descriptors/GPUBlendComponent.h +27 -20
- package/cpp/rnwgpu/api/descriptors/GPUBlendState.h +23 -31
- package/cpp/rnwgpu/api/descriptors/GPUBufferBinding.h +25 -36
- package/cpp/rnwgpu/api/descriptors/GPUBufferBindingLayout.h +25 -25
- package/cpp/rnwgpu/api/descriptors/GPUBufferDescriptor.h +26 -46
- package/cpp/rnwgpu/api/descriptors/GPUCanvasConfiguration.h +31 -53
- package/cpp/rnwgpu/api/descriptors/GPUColor.h +72 -0
- package/cpp/rnwgpu/api/descriptors/GPUColorTargetState.h +27 -33
- package/cpp/rnwgpu/api/descriptors/GPUCommandBufferDescriptor.h +51 -0
- package/cpp/rnwgpu/api/descriptors/GPUCommandEncoderDescriptor.h +51 -0
- package/cpp/rnwgpu/api/descriptors/GPUComputePassDescriptor.h +24 -25
- package/cpp/rnwgpu/api/descriptors/GPUComputePassTimestampWrites.h +25 -42
- package/cpp/rnwgpu/api/descriptors/GPUComputePipelineDescriptor.h +32 -42
- package/cpp/rnwgpu/api/descriptors/GPUDepthStencilState.h +58 -76
- package/cpp/rnwgpu/api/descriptors/GPUDeviceDescriptor.h +38 -30
- package/cpp/rnwgpu/api/descriptors/GPUExternalTextureBindingLayout.h +11 -11
- package/cpp/rnwgpu/api/descriptors/GPUExternalTextureDescriptor.h +43 -34
- package/cpp/rnwgpu/api/descriptors/GPUFragmentState.h +39 -46
- package/cpp/rnwgpu/api/descriptors/GPUImageCopyBuffer.h +29 -45
- package/cpp/rnwgpu/api/descriptors/GPUImageCopyExternalImage.h +32 -31
- package/cpp/rnwgpu/api/descriptors/GPUImageCopyTexture.h +32 -36
- package/cpp/rnwgpu/api/descriptors/GPUImageCopyTextureTagged.h +42 -44
- package/cpp/rnwgpu/api/descriptors/GPUImageDataLayout.h +24 -34
- package/cpp/rnwgpu/api/descriptors/GPUMultisampleState.h +24 -32
- package/cpp/rnwgpu/api/descriptors/GPUPipelineLayoutDescriptor.h +25 -35
- package/cpp/rnwgpu/api/descriptors/GPUPrimitiveState.h +35 -26
- package/cpp/rnwgpu/api/descriptors/GPUProgrammableStage.h +29 -35
- package/cpp/rnwgpu/api/descriptors/GPUQuerySetDescriptor.h +23 -43
- package/cpp/rnwgpu/api/descriptors/GPUQueueDescriptor.h +49 -0
- package/cpp/rnwgpu/api/descriptors/GPURenderBundleDescriptor.h +51 -0
- package/cpp/rnwgpu/api/descriptors/GPURenderBundleEncoderDescriptor.h +41 -57
- package/cpp/rnwgpu/api/descriptors/GPURenderPassColorAttachment.h +42 -61
- package/cpp/rnwgpu/api/descriptors/GPURenderPassDepthStencilAttachment.h +54 -66
- package/cpp/rnwgpu/api/descriptors/GPURenderPassDescriptor.h +52 -55
- package/cpp/rnwgpu/api/descriptors/GPURenderPassTimestampWrites.h +25 -42
- package/cpp/rnwgpu/api/descriptors/GPURenderPipelineDescriptor.h +60 -56
- package/cpp/rnwgpu/api/descriptors/GPURequestAdapterOptions.h +21 -20
- package/cpp/rnwgpu/api/descriptors/GPUSamplerBindingLayout.h +17 -14
- package/cpp/rnwgpu/api/descriptors/GPUSamplerDescriptor.h +63 -63
- package/cpp/rnwgpu/api/descriptors/GPUShaderModuleCompilationHint.h +28 -27
- package/cpp/rnwgpu/api/descriptors/GPUShaderModuleDescriptor.h +28 -49
- package/cpp/rnwgpu/api/descriptors/GPUStencilFaceState.h +32 -23
- package/cpp/rnwgpu/api/descriptors/GPUStorageTextureBindingLayout.h +27 -29
- package/cpp/rnwgpu/api/descriptors/GPUTextureBindingLayout.h +27 -20
- package/cpp/rnwgpu/api/descriptors/GPUTextureDescriptor.h +49 -76
- package/cpp/rnwgpu/api/descriptors/GPUTextureViewDescriptor.h +48 -64
- package/cpp/rnwgpu/api/descriptors/GPUUncapturedErrorEventInit.h +29 -31
- package/cpp/rnwgpu/api/descriptors/GPUVertexAttribute.h +23 -44
- package/cpp/rnwgpu/api/descriptors/GPUVertexBufferLayout.h +30 -36
- package/cpp/rnwgpu/api/descriptors/GPUVertexState.h +39 -38
- package/cpp/rnwgpu/api/descriptors/Unions.h +57 -2
- package/ios/IOSPlatformContext.h +19 -0
- package/ios/IOSPlatformContext.mm +68 -0
- package/ios/MetalView.h +10 -0
- package/ios/MetalView.mm +27 -0
- package/ios/SurfaceUtils.h +13 -0
- package/ios/SurfaceUtils.mm +24 -0
- package/ios/WebGPUModule.h +8 -7
- package/ios/WebGPUModule.mm +47 -14
- package/ios/WebGPUView.h +6 -3
- package/ios/WebGPUView.mm +36 -24
- package/ios/WebGPUViewComponentDescriptor.h +61 -0
- package/ios/WebGPUViewManager.mm +6 -4
- package/lib/commonjs/Canvas.js +47 -0
- package/lib/commonjs/Canvas.js.map +1 -0
- package/lib/commonjs/{WebGPUNativeModule.js → NativeWebGPUModule.js} +1 -1
- package/lib/commonjs/{WebGPUNativeModule.js.map → NativeWebGPUModule.js.map} +1 -1
- package/lib/commonjs/WebGPUView.js +2 -0
- package/lib/commonjs/WebGPUView.js.map +1 -0
- package/lib/commonjs/WebGPUViewNativeComponent.js.map +1 -1
- package/lib/commonjs/index.js +183 -19
- 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 +40 -0
- package/lib/module/Canvas.js.map +1 -0
- package/lib/module/{WebGPUNativeModule.js → NativeWebGPUModule.js} +1 -1
- package/lib/module/NativeWebGPUModule.js.map +1 -0
- package/lib/module/WebGPUView.js +2 -0
- package/lib/module/WebGPUView.js.map +1 -0
- package/lib/module/WebGPUViewNativeComponent.js.map +1 -1
- package/lib/module/index.js +158 -6
- 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/example/src/Triangle/triangle.d.ts +3 -0
- package/lib/typescript/example/src/Triangle/triangle.d.ts.map +1 -0
- package/lib/typescript/example/src/Wireframe/Shaders.d.ts +3 -0
- package/lib/typescript/example/src/Wireframe/Shaders.d.ts.map +1 -0
- package/lib/typescript/example/src/Wireframe/models.d.ts +29 -0
- package/lib/typescript/example/src/Wireframe/models.d.ts.map +1 -0
- package/lib/typescript/example/src/components/DrawingContext.d.ts +13 -0
- package/lib/typescript/example/src/components/DrawingContext.d.ts.map +1 -0
- package/lib/typescript/example/src/components/cube.d.ts +7 -0
- package/lib/typescript/example/src/components/cube.d.ts.map +1 -0
- package/lib/typescript/example/src/components/meshes/sphere.d.ts +12 -0
- package/lib/typescript/example/src/components/meshes/sphere.d.ts.map +1 -0
- package/lib/typescript/example/src/components/meshes/teapot.d.ts +6 -0
- package/lib/typescript/example/src/components/meshes/teapot.d.ts.map +1 -0
- package/lib/typescript/example/src/components/meshes/utils.d.ts +10 -0
- package/lib/typescript/example/src/components/meshes/utils.d.ts.map +1 -0
- package/lib/typescript/jest.config.d.ts +4 -0
- package/lib/typescript/lib/commonjs/Canvas.d.ts +4 -0
- package/lib/typescript/lib/commonjs/Canvas.d.ts.map +1 -0
- package/lib/typescript/lib/commonjs/{WebGPUNativeModule.d.ts → NativeWebGPUModule.d.ts} +1 -1
- package/lib/typescript/lib/commonjs/NativeWebGPUModule.d.ts.map +1 -0
- package/lib/typescript/lib/commonjs/WebGPUView.d.ts +1 -0
- package/lib/typescript/lib/commonjs/WebGPUView.d.ts.map +1 -0
- package/lib/typescript/lib/commonjs/index.d.ts +0 -2
- 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 +2 -0
- package/lib/typescript/lib/module/Canvas.d.ts.map +1 -0
- package/lib/typescript/lib/module/{WebGPUNativeModule.d.ts → NativeWebGPUModule.d.ts} +1 -1
- package/lib/typescript/lib/module/NativeWebGPUModule.d.ts.map +1 -0
- package/lib/typescript/lib/module/WebGPUView.d.ts +1 -0
- package/lib/typescript/lib/module/WebGPUView.d.ts.map +1 -0
- package/lib/typescript/lib/module/index.d.ts +3 -3
- 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 +29 -0
- package/lib/typescript/src/Canvas.d.ts.map +1 -0
- package/lib/typescript/src/{WebGPUNativeModule.d.ts → NativeWebGPUModule.d.ts} +2 -1
- package/lib/typescript/src/NativeWebGPUModule.d.ts.map +1 -0
- package/lib/typescript/src/WebGPUView.d.ts +1 -0
- package/lib/typescript/src/WebGPUView.d.ts.map +1 -0
- package/lib/typescript/src/WebGPUViewNativeComponent.d.ts +2 -1
- package/lib/typescript/src/WebGPUViewNativeComponent.d.ts.map +1 -1
- package/lib/typescript/src/index.d.ts +3 -7
- 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/android/arm64-v8a/libwebgpu_dawn.so +0 -0
- package/libs/android/armeabi-v7a/libwebgpu_dawn.so +0 -0
- package/libs/android/x86/libwebgpu_dawn.so +0 -0
- package/libs/android/x86_64/libwebgpu_dawn.so +0 -0
- package/libs/dawn.json +4597 -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 +44 -0
- 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 +25 -8
- package/{react-native-webgpu.podspec → react-native-wgpu.podspec} +6 -9
- package/src/Canvas.tsx +73 -0
- package/src/{WebGPUNativeModule.ts → NativeWebGPUModule.ts} +1 -0
- package/src/WebGPUView.tsx +0 -0
- package/src/WebGPUViewNativeComponent.ts +2 -1
- package/src/index.tsx +191 -8
- package/src/utils.ts +40 -0
- package/android/src/main/AndroidManifestNew.xml +0 -2
- package/cpp/rnwgpu/MutableBuffer.h +0 -48
- package/cpp/rnwgpu/api/GPUUncapturedErrorEvent.h +0 -33
- package/cpp/rnwgpu/api/descriptors/GPUColorDict.h +0 -81
- package/cpp/rnwgpu/api/descriptors/GPUExtent3DDict.h +0 -76
- package/cpp/rnwgpu/api/descriptors/GPUExtent3DDictStrict.h +0 -91
- package/cpp/rnwgpu/api/descriptors/GPUOrigin2DDict.h +0 -61
- package/cpp/rnwgpu/api/descriptors/GPUOrigin2DDictStrict.h +0 -75
- package/cpp/rnwgpu/api/descriptors/GPUOrigin3DDict.h +0 -71
- package/cpp/rnwgpu/api/descriptors/GPUPipelineErrorInit.h +0 -54
- package/cpp/rnwgpu/api/descriptors/GPURenderPassLayout.h +0 -83
- package/ios/RNFAppleLogger.mm +0 -22
- package/ios/Utils.h +0 -5
- package/ios/Utils.m +0 -26
- package/lib/module/WebGPUNativeModule.js.map +0 -1
- package/lib/typescript/lib/commonjs/WebGPUNativeModule.d.ts.map +0 -1
- package/lib/typescript/lib/module/WebGPUNativeModule.d.ts.map +0 -1
- package/lib/typescript/scripts/build/copy-artifacts.d.ts +0 -2
- package/lib/typescript/scripts/build/copy-artifacts.d.ts.map +0 -1
- package/lib/typescript/scripts/build/dawn.d.ts +0 -2
- package/lib/typescript/scripts/build/dawn.d.ts.map +0 -1
- package/lib/typescript/scripts/build/util.d.ts +0 -13
- package/lib/typescript/scripts/build/util.d.ts.map +0 -1
- package/lib/typescript/scripts/codegen/codegen.d.ts +0 -2
- package/lib/typescript/scripts/codegen/codegen.d.ts.map +0 -1
- package/lib/typescript/scripts/codegen/model/dawn.d.ts +0 -2
- package/lib/typescript/scripts/codegen/model/dawn.d.ts.map +0 -1
- package/lib/typescript/scripts/codegen/model/model.d.ts +0 -8
- package/lib/typescript/scripts/codegen/model/model.d.ts.map +0 -1
- package/lib/typescript/scripts/codegen/templates/Descriptor.d.ts +0 -4
- package/lib/typescript/scripts/codegen/templates/Descriptor.d.ts.map +0 -1
- package/lib/typescript/scripts/codegen/templates/Enum.d.ts +0 -3
- package/lib/typescript/scripts/codegen/templates/Enum.d.ts.map +0 -1
- package/lib/typescript/scripts/codegen/templates/HybridObject.d.ts +0 -3
- package/lib/typescript/scripts/codegen/templates/HybridObject.d.ts.map +0 -1
- package/lib/typescript/scripts/codegen/templates/Unions.d.ts +0 -6
- package/lib/typescript/scripts/codegen/templates/Unions.d.ts.map +0 -1
- package/lib/typescript/scripts/codegen/templates/common.d.ts +0 -9
- package/lib/typescript/scripts/codegen/templates/common.d.ts.map +0 -1
- package/lib/typescript/scripts/codegen/util.d.ts +0 -2
- package/lib/typescript/scripts/codegen/util.d.ts.map +0 -1
- package/lib/typescript/src/WebGPUNativeModule.d.ts.map +0 -1
package/README.md
CHANGED
|
@@ -1,31 +1,239 @@
|
|
|
1
|
-
#
|
|
1
|
+
# React Native WebGPU
|
|
2
2
|
|
|
3
|
-
React Native WebGPU
|
|
3
|
+
React Native implementation of WebGPU using [Dawn](https://dawn.googlesource.com/dawn).
|
|
4
|
+
This is currently a technical preview for early adopters.
|
|
4
5
|
|
|
5
6
|
## Installation
|
|
6
7
|
|
|
7
|
-
|
|
8
|
+
Please note that the package name is `react-native-wgpu`.
|
|
9
|
+
|
|
10
|
+
```
|
|
8
11
|
npm install react-native-wgpu
|
|
9
12
|
```
|
|
10
13
|
|
|
11
14
|
## Usage
|
|
12
15
|
|
|
13
|
-
|
|
14
|
-
|
|
16
|
+
You can look at the [example](/package/example) folder for working examples.
|
|
17
|
+
|
|
18
|
+
```tsx
|
|
19
|
+
import React, { useEffect, useRef } from "react";
|
|
20
|
+
import { StyleSheet, View, PixelRatio } from "react-native";
|
|
21
|
+
import type { CanvasRef } from "react-native-webgpu";
|
|
22
|
+
import { Canvas } from "react-native-webgpu";
|
|
23
|
+
|
|
24
|
+
import { redFragWGSL, triangleVertWGSL } from "./triangle";
|
|
25
|
+
|
|
26
|
+
export function HelloTriangle() {
|
|
27
|
+
const ref = useRef<CanvasRef>(null);
|
|
28
|
+
|
|
29
|
+
async function demo() {
|
|
30
|
+
const adapter = await navigator.gpu.requestAdapter();
|
|
31
|
+
if (!adapter) {
|
|
32
|
+
throw new Error("No adapter");
|
|
33
|
+
}
|
|
34
|
+
const device = await adapter.requestDevice();
|
|
35
|
+
const presentationFormat = navigator.gpu.getPreferredCanvasFormat();
|
|
36
|
+
|
|
37
|
+
const context = ref.current!.getContext("webgpu")!;
|
|
38
|
+
canvas.width = canvas.clientWidth * PixelRatio.get();
|
|
39
|
+
canvas.height = canvas.clientHeight * PixelRatio.get();
|
|
40
|
+
|
|
41
|
+
if (!context) {
|
|
42
|
+
throw new Error("No context");
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
context.configure({
|
|
46
|
+
device,
|
|
47
|
+
format: presentationFormat,
|
|
48
|
+
alphaMode: "opaque",
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
const pipeline = device.createRenderPipeline({
|
|
52
|
+
layout: "auto",
|
|
53
|
+
vertex: {
|
|
54
|
+
module: device.createShaderModule({
|
|
55
|
+
code: triangleVertWGSL,
|
|
56
|
+
}),
|
|
57
|
+
entryPoint: "main",
|
|
58
|
+
},
|
|
59
|
+
fragment: {
|
|
60
|
+
module: device.createShaderModule({
|
|
61
|
+
code: redFragWGSL,
|
|
62
|
+
}),
|
|
63
|
+
entryPoint: "main",
|
|
64
|
+
targets: [
|
|
65
|
+
{
|
|
66
|
+
format: presentationFormat,
|
|
67
|
+
},
|
|
68
|
+
],
|
|
69
|
+
},
|
|
70
|
+
primitive: {
|
|
71
|
+
topology: "triangle-list",
|
|
72
|
+
},
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
const commandEncoder = device.createCommandEncoder();
|
|
76
|
+
|
|
77
|
+
const textureView = context.getCurrentTexture().createView();
|
|
78
|
+
|
|
79
|
+
const renderPassDescriptor: GPURenderPassDescriptor = {
|
|
80
|
+
colorAttachments: [
|
|
81
|
+
{
|
|
82
|
+
view: textureView,
|
|
83
|
+
clearValue: [0, 0, 0, 1],
|
|
84
|
+
loadOp: "clear",
|
|
85
|
+
storeOp: "store",
|
|
86
|
+
},
|
|
87
|
+
],
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
const passEncoder = commandEncoder.beginRenderPass(renderPassDescriptor);
|
|
91
|
+
passEncoder.setPipeline(pipeline);
|
|
92
|
+
passEncoder.draw(3);
|
|
93
|
+
passEncoder.end();
|
|
94
|
+
|
|
95
|
+
device.queue.submit([commandEncoder.finish()]);
|
|
96
|
+
|
|
97
|
+
context.present();
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
useEffect(() => {
|
|
101
|
+
demo();
|
|
102
|
+
}, []);
|
|
103
|
+
|
|
104
|
+
return (
|
|
105
|
+
<View style={style.container}>
|
|
106
|
+
<Canvas ref={ref} style={style.webgpu} />
|
|
107
|
+
</View>
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const style = StyleSheet.create({
|
|
112
|
+
container: {
|
|
113
|
+
flex: 1,
|
|
114
|
+
},
|
|
115
|
+
webgpu: {
|
|
116
|
+
flex: 1,
|
|
117
|
+
},
|
|
118
|
+
});
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
## Similarities and Differences with the Web
|
|
122
|
+
|
|
123
|
+
The API has been designed to be completely symmetric with the Web.
|
|
124
|
+
For instance, you can access the WebGPU context synchronously, as well as the canvas size.
|
|
125
|
+
Pixel density and canvas resizing are handled exactly like on the Web as well.
|
|
126
|
+
|
|
127
|
+
```tsx
|
|
128
|
+
// The default canvas size is not scaled to the device pixel ratio
|
|
129
|
+
// When resizing the canvas, the clientWidth and clientHeight are updated automatically
|
|
130
|
+
// This behaviour is symmetric to the Web
|
|
131
|
+
const ctx = canvas.current.getContext("webgpu")!;
|
|
132
|
+
ctx.canvas.width = ctx.canvas.clientWidth * PixelRatio.get();
|
|
133
|
+
ctx.canvas.height = ctx.canvas.clientHeight * PixelRatio.get();
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
However, there are two differences with the Web: frame scheduling and external textures.
|
|
137
|
+
|
|
138
|
+
### Frame Scheduling
|
|
139
|
+
|
|
140
|
+
In React Native, we want to keep frame presentation as a manual operation as we plan to provide more advanced rendering options that are React Native specific.
|
|
141
|
+
This means that when you are ready to present a frame, you need to call `present` on the context.
|
|
142
|
+
|
|
143
|
+
```tsx
|
|
144
|
+
// draw
|
|
145
|
+
// submit to the queue
|
|
146
|
+
device.queue.submit([commandEncoder.finish()]);
|
|
147
|
+
// This method is React Native only
|
|
148
|
+
context.present();
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
### External Textures
|
|
152
|
+
|
|
153
|
+
External textures are not a concept that exists in React Native.
|
|
154
|
+
Consider the following Web example:
|
|
155
|
+
|
|
156
|
+
```tsx
|
|
157
|
+
const response = await fetch('./assets/img/Di-3d.png');
|
|
158
|
+
const imageBitmap = await createImageBitmap(await response.blob());
|
|
159
|
+
|
|
160
|
+
device.queue.copyExternalImageToTexture(
|
|
161
|
+
{ source: imageBitmap },
|
|
162
|
+
{ texture: cubeTexture },
|
|
163
|
+
[imageBitmap.width, imageBitmap.height]
|
|
164
|
+
);
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
In React Native, you would need to load the texture yourself.
|
|
168
|
+
For instance, we use Skia for image decoding [here](/package/example/src/components/useAssets.ts#L6).
|
|
169
|
+
|
|
170
|
+
```tsx
|
|
171
|
+
const imageBitmap = await decodeImage(require("./assets/Di-3d.png"));
|
|
172
|
+
|
|
173
|
+
device.queue.writeTexture(
|
|
174
|
+
{ texture: cubeTexture, mipLevel: 0, origin: { x: 0, y: 0, z: 0 } },
|
|
175
|
+
imageBitmap.data.buffer,
|
|
176
|
+
{
|
|
177
|
+
offset: 0,
|
|
178
|
+
bytesPerRow: 4 * imageBitmap.width,
|
|
179
|
+
rowsPerImage: imageBitmap.height,
|
|
180
|
+
},
|
|
181
|
+
{ width: imageBitmap.width, height: imageBitmap.height },
|
|
182
|
+
);
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
## Troubleshooting
|
|
186
|
+
|
|
187
|
+
### iOS
|
|
188
|
+
|
|
189
|
+
To run the React Native WebGPU project on the iOS simulator, you need to disable the Metal validation API.
|
|
190
|
+
In "Edit Scheme," uncheck "Metal Validation."
|
|
191
|
+
|
|
192
|
+
<img width="1052" alt="Uncheck 'Metal Validation'" src="https://github.com/user-attachments/assets/2676e5cc-e351-4a97-bdc8-22cbd7df2ef2">
|
|
15
193
|
|
|
16
|
-
|
|
194
|
+
### Android
|
|
17
195
|
|
|
18
|
-
|
|
196
|
+
On a physical device, you need Android API level 26 or higher.
|
|
197
|
+
On a simulator, you need Android API level 34 or higher.
|
|
198
|
+
We are currently working on relaxing that rule for Android simulators.
|
|
199
|
+
|
|
200
|
+
## Library Development
|
|
201
|
+
|
|
202
|
+
Make sure to check out the submodules:
|
|
203
|
+
|
|
204
|
+
```
|
|
205
|
+
git submodule update --init
|
|
19
206
|
```
|
|
20
207
|
|
|
21
|
-
|
|
208
|
+
Make sure you have all the tools required for building the Skia libraries (Android Studio, XCode, Ninja, CMake, Android NDK/build tools).
|
|
209
|
+
|
|
210
|
+
### Building
|
|
211
|
+
|
|
212
|
+
* `cd package && yarn`
|
|
213
|
+
* `yarn build-dawn`
|
|
214
|
+
|
|
215
|
+
### Upgrading
|
|
22
216
|
|
|
23
|
-
|
|
217
|
+
1. `git submodule update --remote`
|
|
218
|
+
2. `yarn clean-dawn`
|
|
219
|
+
3. `yarn build-dawn`
|
|
24
220
|
|
|
25
|
-
|
|
221
|
+
### Codegen
|
|
26
222
|
|
|
27
|
-
|
|
223
|
+
* `cd package && yarn codegen`
|
|
28
224
|
|
|
29
|
-
|
|
225
|
+
### Testing
|
|
30
226
|
|
|
31
|
-
|
|
227
|
+
In the `package` folder, to run the test against Chrome for reference:
|
|
228
|
+
|
|
229
|
+
```
|
|
230
|
+
yarn test:ref
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
To run the e2e test, open the example app on the e2e screen.
|
|
234
|
+
By default, it will try to connect to a localhost test server.
|
|
235
|
+
If you want to run the test suite on a physical device, you can modify the address [here](/package/example/src/useClient.ts#L4).
|
|
236
|
+
|
|
237
|
+
```
|
|
238
|
+
yarn test
|
|
239
|
+
```
|
package/android/CMakeLists.txt
CHANGED
|
@@ -4,7 +4,7 @@ project(RNWGPU)
|
|
|
4
4
|
set (CMAKE_VERBOSE_MAKEFILE ON)
|
|
5
5
|
set (CMAKE_CXX_STANDARD 17)
|
|
6
6
|
|
|
7
|
-
set (PACKAGE_NAME "react-native-
|
|
7
|
+
set (PACKAGE_NAME "react-native-wgpu")
|
|
8
8
|
|
|
9
9
|
#link_directories(../libs/android/${ANDROID_ABI}/)
|
|
10
10
|
|
|
@@ -27,8 +27,22 @@ add_library(${PACKAGE_NAME} SHARED
|
|
|
27
27
|
./cpp/cpp-adapter.cpp
|
|
28
28
|
../cpp/rnwgpu/api/GPU.cpp
|
|
29
29
|
../cpp/rnwgpu/api/GPUAdapter.cpp
|
|
30
|
+
../cpp/rnwgpu/api/GPUSupportedLimits.cpp
|
|
31
|
+
../cpp/rnwgpu/api/GPUShaderModule.cpp
|
|
32
|
+
../cpp/rnwgpu/api/GPUDeviceLostInfo.cpp
|
|
30
33
|
../cpp/rnwgpu/api/GPUDevice.cpp
|
|
31
34
|
../cpp/rnwgpu/api/GPUBuffer.cpp
|
|
35
|
+
../cpp/rnwgpu/api/GPUQueue.cpp
|
|
36
|
+
../cpp/rnwgpu/api/GPUCommandEncoder.cpp
|
|
37
|
+
../cpp/rnwgpu/api/GPUQuerySet.cpp
|
|
38
|
+
../cpp/rnwgpu/api/GPUTexture.cpp
|
|
39
|
+
../cpp/rnwgpu/api/GPURenderBundleEncoder.cpp
|
|
40
|
+
../cpp/rnwgpu/api/GPURenderPassEncoder.cpp
|
|
41
|
+
../cpp/rnwgpu/api/GPURenderPipeline.cpp
|
|
42
|
+
../cpp/rnwgpu/api/GPUBindGroup.cpp
|
|
43
|
+
../cpp/rnwgpu/api/GPUComputePassEncoder.cpp
|
|
44
|
+
../cpp/rnwgpu/api/GPUComputePipeline.cpp
|
|
45
|
+
../cpp/rnwgpu/api/GPUCanvasContext.cpp
|
|
32
46
|
../cpp/rnwgpu/RNWebGPUManager.cpp
|
|
33
47
|
../cpp/jsi/RNFPromise.cpp
|
|
34
48
|
../cpp/jsi/RNFHybridObject.cpp
|
package/android/build.gradle
CHANGED
|
@@ -70,16 +70,6 @@ def supportsNamespace() {
|
|
|
70
70
|
}
|
|
71
71
|
|
|
72
72
|
android {
|
|
73
|
-
if (supportsNamespace()) {
|
|
74
|
-
namespace "com.wgpu"
|
|
75
|
-
|
|
76
|
-
sourceSets {
|
|
77
|
-
main {
|
|
78
|
-
manifest.srcFile "src/main/AndroidManifestNew.xml"
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
73
|
compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")
|
|
84
74
|
|
|
85
75
|
defaultConfig {
|
|
@@ -156,11 +146,3 @@ dependencies {
|
|
|
156
146
|
//noinspection GradleDynamicVersion
|
|
157
147
|
implementation "com.facebook.react:react-native:+"
|
|
158
148
|
}
|
|
159
|
-
|
|
160
|
-
if (isNewArchitectureEnabled()) {
|
|
161
|
-
react {
|
|
162
|
-
jsRootDir = file("../src/")
|
|
163
|
-
libraryName = "WgpuView"
|
|
164
|
-
codegenJavaPackageName = "com.wgpu"
|
|
165
|
-
}
|
|
166
|
-
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include <android/bitmap.h>
|
|
4
|
+
#include <jni.h>
|
|
5
|
+
|
|
6
|
+
#include <memory>
|
|
7
|
+
#include <string>
|
|
8
|
+
#include <vector>
|
|
9
|
+
|
|
10
|
+
#include "webgpu/webgpu_cpp.h"
|
|
11
|
+
|
|
12
|
+
#include "PlatformContext.h"
|
|
13
|
+
#include "RNWebGPUManager.h"
|
|
14
|
+
|
|
15
|
+
namespace rnwgpu {
|
|
16
|
+
|
|
17
|
+
namespace jsi = facebook::jsi;
|
|
18
|
+
namespace jni = facebook::jni;
|
|
19
|
+
|
|
20
|
+
class AndroidPlatformContext : public PlatformContext {
|
|
21
|
+
private:
|
|
22
|
+
jobject _blobModule;
|
|
23
|
+
|
|
24
|
+
public:
|
|
25
|
+
explicit AndroidPlatformContext(jobject blobModule) : _blobModule(blobModule) {}
|
|
26
|
+
~AndroidPlatformContext() {
|
|
27
|
+
if (_blobModule) {
|
|
28
|
+
JNIEnv *env = facebook::jni::Environment::current();
|
|
29
|
+
env->DeleteGlobalRef(_blobModule);
|
|
30
|
+
_blobModule = nullptr;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
wgpu::Surface makeSurface(wgpu::Instance instance, void *window, int width,
|
|
35
|
+
int height) override {
|
|
36
|
+
wgpu::SurfaceDescriptorFromAndroidNativeWindow androidSurfaceDesc;
|
|
37
|
+
androidSurfaceDesc.window = reinterpret_cast<ANativeWindow *>(window);
|
|
38
|
+
wgpu::SurfaceDescriptor surfaceDescriptor;
|
|
39
|
+
surfaceDescriptor.nextInChain = &androidSurfaceDesc;
|
|
40
|
+
return instance.CreateSurface(&surfaceDescriptor);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
ImageData createImageBitmap(std::string blobId, double offset,
|
|
44
|
+
double size) override {
|
|
45
|
+
jni::Environment::ensureCurrentThreadIsAttached();
|
|
46
|
+
|
|
47
|
+
JNIEnv *env = facebook::jni::Environment::current();
|
|
48
|
+
if (!env) {
|
|
49
|
+
throw std::runtime_error("Couldn't get JNI environment");
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Use the BlobModule instance from _blobModule
|
|
53
|
+
if (!_blobModule) {
|
|
54
|
+
throw std::runtime_error("BlobModule instance is null");
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Get the resolve method ID
|
|
58
|
+
jclass blobModuleClass = env->GetObjectClass(_blobModule);
|
|
59
|
+
if (!blobModuleClass) {
|
|
60
|
+
throw std::runtime_error("Couldn't find BlobModule class");
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
jmethodID resolveMethod = env->GetMethodID(blobModuleClass, "resolve",
|
|
64
|
+
"(Ljava/lang/String;II)[B");
|
|
65
|
+
if (!resolveMethod) {
|
|
66
|
+
throw std::runtime_error("Couldn't find resolve method in BlobModule");
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Resolve the blob data
|
|
70
|
+
jstring jBlobId = env->NewStringUTF(blobId.c_str());
|
|
71
|
+
jbyteArray blobData = (jbyteArray)env->CallObjectMethod(
|
|
72
|
+
_blobModule, resolveMethod, jBlobId, static_cast<jint>(offset),
|
|
73
|
+
static_cast<jint>(size));
|
|
74
|
+
env->DeleteLocalRef(jBlobId);
|
|
75
|
+
|
|
76
|
+
if (!blobData) {
|
|
77
|
+
throw std::runtime_error("Couldn't retrieve blob data");
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Create a Bitmap from the blob data
|
|
81
|
+
jclass bitmapFactoryClass =
|
|
82
|
+
env->FindClass("android/graphics/BitmapFactory");
|
|
83
|
+
jmethodID decodeByteArrayMethod =
|
|
84
|
+
env->GetStaticMethodID(bitmapFactoryClass, "decodeByteArray",
|
|
85
|
+
"([BII)Landroid/graphics/Bitmap;");
|
|
86
|
+
jint blobLength = env->GetArrayLength(blobData);
|
|
87
|
+
jobject bitmap = env->CallStaticObjectMethod(
|
|
88
|
+
bitmapFactoryClass, decodeByteArrayMethod, blobData, 0, blobLength);
|
|
89
|
+
|
|
90
|
+
if (!bitmap) {
|
|
91
|
+
env->DeleteLocalRef(blobData);
|
|
92
|
+
throw std::runtime_error("Couldn't decode image");
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Get bitmap info
|
|
96
|
+
AndroidBitmapInfo bitmapInfo;
|
|
97
|
+
if (AndroidBitmap_getInfo(env, bitmap, &bitmapInfo) !=
|
|
98
|
+
ANDROID_BITMAP_RESULT_SUCCESS) {
|
|
99
|
+
env->DeleteLocalRef(blobData);
|
|
100
|
+
env->DeleteLocalRef(bitmap);
|
|
101
|
+
throw std::runtime_error("Couldn't get bitmap info");
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// Lock the bitmap pixels
|
|
105
|
+
void *bitmapPixels;
|
|
106
|
+
if (AndroidBitmap_lockPixels(env, bitmap, &bitmapPixels) !=
|
|
107
|
+
ANDROID_BITMAP_RESULT_SUCCESS) {
|
|
108
|
+
env->DeleteLocalRef(blobData);
|
|
109
|
+
env->DeleteLocalRef(bitmap);
|
|
110
|
+
throw std::runtime_error("Couldn't lock bitmap pixels");
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// Copy the bitmap data
|
|
114
|
+
std::vector<uint8_t> imageData(bitmapInfo.height * bitmapInfo.stride);
|
|
115
|
+
memcpy(imageData.data(), bitmapPixels, imageData.size());
|
|
116
|
+
|
|
117
|
+
// Unlock the bitmap pixels
|
|
118
|
+
AndroidBitmap_unlockPixels(env, bitmap);
|
|
119
|
+
|
|
120
|
+
// Clean up JNI references
|
|
121
|
+
env->DeleteLocalRef(blobData);
|
|
122
|
+
env->DeleteLocalRef(bitmap);
|
|
123
|
+
|
|
124
|
+
ImageData result;
|
|
125
|
+
result.width = static_cast<int>(bitmapInfo.width);
|
|
126
|
+
result.height = static_cast<int>(bitmapInfo.height);
|
|
127
|
+
result.data = imageData;
|
|
128
|
+
return result;
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
} // namespace rnwgpu
|
|
@@ -1,12 +1,78 @@
|
|
|
1
|
-
#include
|
|
2
|
-
#include
|
|
1
|
+
#include <memory>
|
|
2
|
+
#include <unordered_map>
|
|
3
|
+
|
|
3
4
|
#include <fbjni/fbjni.h>
|
|
4
5
|
#include <jni.h>
|
|
6
|
+
#include <jsi/jsi.h>
|
|
7
|
+
|
|
8
|
+
#include <android/native_window_jni.h>
|
|
9
|
+
#include <webgpu/webgpu_cpp.h>
|
|
10
|
+
|
|
11
|
+
#include "AndroidPlatformContext.h"
|
|
12
|
+
#include "GPUCanvasContext.h"
|
|
13
|
+
#include "RNWebGPUManager.h"
|
|
5
14
|
|
|
6
15
|
#define LOG_TAG "WebGPUModule"
|
|
7
16
|
|
|
17
|
+
std::shared_ptr<rnwgpu::RNWebGPUManager> manager;
|
|
18
|
+
|
|
8
19
|
extern "C" JNIEXPORT void JNICALL Java_com_webgpu_WebGPUModule_initializeNative(
|
|
9
|
-
JNIEnv *env, jobject /* this */, jlong jsRuntime, jobject jsInvokerHolder
|
|
20
|
+
JNIEnv *env, jobject /* this */, jlong jsRuntime, jobject jsInvokerHolder,
|
|
21
|
+
jobject blobModule) {
|
|
22
|
+
auto runtime = reinterpret_cast<facebook::jsi::Runtime *>(jsRuntime);
|
|
23
|
+
jobject globalBlobModule = env->NewGlobalRef(blobModule);
|
|
24
|
+
auto platformContext =
|
|
25
|
+
std::make_shared<rnwgpu::AndroidPlatformContext>(globalBlobModule);
|
|
26
|
+
manager = std::make_shared<rnwgpu::RNWebGPUManager>(runtime, nullptr,
|
|
27
|
+
platformContext);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
extern "C" JNIEXPORT void JNICALL
|
|
31
|
+
Java_com_webgpu_WebGPUModule_createSurfaceContext(JNIEnv *env, jobject thiz,
|
|
32
|
+
jlong jsRuntime,
|
|
33
|
+
jint contextId) {
|
|
34
|
+
auto canvas = manager->surfacesRegistry.getSurface(contextId);
|
|
35
|
+
if (canvas == nullptr) {
|
|
36
|
+
throw std::runtime_error("Surface haven't configured yet");
|
|
37
|
+
}
|
|
38
|
+
|
|
10
39
|
auto runtime = reinterpret_cast<facebook::jsi::Runtime *>(jsRuntime);
|
|
11
|
-
auto
|
|
40
|
+
auto webGPUContextRegistry = runtime->global().getPropertyAsObject(
|
|
41
|
+
*runtime, "__WebGPUContextRegistry");
|
|
42
|
+
if (webGPUContextRegistry.hasProperty(*runtime,
|
|
43
|
+
std::to_string(contextId).c_str())) {
|
|
44
|
+
// Context already exists, just update width/height
|
|
45
|
+
auto prop =
|
|
46
|
+
webGPUContextRegistry
|
|
47
|
+
.getPropertyAsObject(*runtime, std::to_string(contextId).c_str())
|
|
48
|
+
.asHostObject<rnwgpu::Canvas>(*runtime);
|
|
49
|
+
prop->setWidth(canvas->getWidth());
|
|
50
|
+
prop->setHeight(canvas->getHeight());
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
webGPUContextRegistry.setProperty(
|
|
54
|
+
*runtime, std::to_string(contextId).c_str(),
|
|
55
|
+
facebook::jsi::Object::createFromHostObject(*runtime, canvas));
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
extern "C" JNIEXPORT void JNICALL Java_com_webgpu_WebGPUView_onSurfaceChanged(
|
|
59
|
+
JNIEnv *env, jobject thiz, jobject surface, jint contextId, jfloat width,
|
|
60
|
+
jfloat height) {
|
|
61
|
+
manager->surfacesRegistry.updateSurface(contextId, width, height);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
extern "C" JNIEXPORT void JNICALL Java_com_webgpu_WebGPUView_onSurfaceCreate(
|
|
65
|
+
JNIEnv *env, jobject thiz, jobject surface, jint contextId, jfloat width,
|
|
66
|
+
jfloat height) {
|
|
67
|
+
auto window = ANativeWindow_fromSurface(env, surface);
|
|
68
|
+
// ANativeWindow_acquire(window);
|
|
69
|
+
manager->surfacesRegistry.addSurface(contextId, window, width, height);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
extern "C" JNIEXPORT void JNICALL Java_com_webgpu_WebGPUView_onSurfaceDestroy(
|
|
73
|
+
JNIEnv *env, jobject thiz, jint contextId) {
|
|
74
|
+
auto canvas = manager->surfacesRegistry.getSurface(contextId);
|
|
75
|
+
ANativeWindow_release(
|
|
76
|
+
reinterpret_cast<ANativeWindow *>(canvas->getSurface()));
|
|
77
|
+
manager->surfacesRegistry.removeSurface(contextId);
|
|
12
78
|
}
|
|
@@ -1,39 +1,87 @@
|
|
|
1
1
|
package com.webgpu;
|
|
2
2
|
|
|
3
|
-
import
|
|
3
|
+
import android.util.Log;
|
|
4
|
+
|
|
5
|
+
import androidx.annotation.OptIn;
|
|
6
|
+
|
|
7
|
+
import java.util.HashSet;
|
|
8
|
+
import java.util.Set;
|
|
9
|
+
|
|
4
10
|
import com.facebook.proguard.annotations.DoNotStrip;
|
|
5
11
|
import com.facebook.react.bridge.ReactApplicationContext;
|
|
6
|
-
import com.facebook.react.bridge.ReactContextBaseJavaModule;
|
|
7
12
|
import com.facebook.react.bridge.JavaScriptContextHolder;
|
|
8
13
|
import com.facebook.react.bridge.ReactMethod;
|
|
9
14
|
import com.facebook.react.common.annotations.FrameworkAPI;
|
|
15
|
+
import com.facebook.react.module.annotations.ReactModule;
|
|
16
|
+
import com.facebook.react.modules.blob.BlobModule;
|
|
17
|
+
import com.facebook.react.modules.blob.BlobProvider;
|
|
10
18
|
import com.facebook.react.turbomodule.core.CallInvokerHolderImpl;
|
|
11
19
|
import com.facebook.react.turbomodule.core.interfaces.CallInvokerHolder;
|
|
12
20
|
|
|
13
|
-
@
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
21
|
+
@ReactModule(name = WebGPUModule.NAME)
|
|
22
|
+
public class WebGPUModule extends NativeWebGPUModuleSpec {
|
|
23
|
+
static {
|
|
24
|
+
System.loadLibrary("react-native-wgpu"); // Load the C++ library
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
private final Object mContextLock = new Object();
|
|
28
|
+
private final Set<Integer> mSurfaceContextsIds = new HashSet<>();
|
|
17
29
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
30
|
+
public WebGPUModule(ReactApplicationContext reactContext) {
|
|
31
|
+
super(reactContext);
|
|
32
|
+
// Initialize the C++ module
|
|
33
|
+
initialize();
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
@OptIn(markerClass = FrameworkAPI.class)
|
|
37
|
+
@ReactMethod(isBlockingSynchronousMethod = true)
|
|
38
|
+
public boolean install() {
|
|
39
|
+
ReactApplicationContext context = getReactApplicationContext();
|
|
40
|
+
JavaScriptContextHolder jsContext = context.getJavaScriptContextHolder();
|
|
41
|
+
CallInvokerHolder callInvokerHolder = context.getCatalystInstance().getJSCallInvokerHolder();
|
|
42
|
+
BlobModule blobModule = getReactApplicationContext().getNativeModule(BlobModule.class);
|
|
43
|
+
if (blobModule == null) {
|
|
44
|
+
throw new RuntimeException("React Native's BlobModule was not found!");
|
|
22
45
|
}
|
|
46
|
+
initializeNative(jsContext.get(), (CallInvokerHolderImpl) callInvokerHolder, blobModule);
|
|
47
|
+
return true;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
@OptIn(markerClass = FrameworkAPI.class)
|
|
51
|
+
@DoNotStrip
|
|
52
|
+
private native void initializeNative(long jsRuntime, CallInvokerHolderImpl jsInvoker, BlobModule blobModule);
|
|
53
|
+
|
|
54
|
+
@ReactMethod(isBlockingSynchronousMethod = true)
|
|
55
|
+
public boolean createSurfaceContext(double contextId) {
|
|
56
|
+
waitForNativeSurface((int)contextId);
|
|
57
|
+
|
|
58
|
+
ReactApplicationContext context = getReactApplicationContext();
|
|
59
|
+
JavaScriptContextHolder jsContext = context.getJavaScriptContextHolder();
|
|
60
|
+
createSurfaceContext(jsContext.get(), (int)contextId);
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
@DoNotStrip
|
|
65
|
+
private native void createSurfaceContext(long jsRuntime, int contextId);
|
|
23
66
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
67
|
+
private void waitForNativeSurface(Integer contextId) {
|
|
68
|
+
synchronized (mContextLock) {
|
|
69
|
+
while (!mSurfaceContextsIds.contains(contextId)) {
|
|
70
|
+
try {
|
|
71
|
+
mContextLock.wait();
|
|
72
|
+
} catch (InterruptedException e) {
|
|
73
|
+
Log.e("RNWebGPU", "Unable to create a context");
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
27
77
|
}
|
|
78
|
+
}
|
|
28
79
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
CallInvokerHolder callInvokerHolder = context.getCatalystInstance().getJSCallInvokerHolder();
|
|
34
|
-
initializeNative(jsContext.get(), (CallInvokerHolderImpl) callInvokerHolder);
|
|
80
|
+
protected void onSurfaceCreated(Integer contextId) {
|
|
81
|
+
synchronized (mContextLock) {
|
|
82
|
+
mSurfaceContextsIds.add(contextId);
|
|
83
|
+
mContextLock.notifyAll();
|
|
35
84
|
}
|
|
85
|
+
}
|
|
36
86
|
|
|
37
|
-
@DoNotStrip
|
|
38
|
-
private native void initializeNative(long jsRuntime, CallInvokerHolderImpl jsInvoker);
|
|
39
87
|
}
|