Skip to content

Commit c2942d3

Browse files
authored
fix(🏎️): switch to native state (#298)
1 parent abd2b11 commit c2942d3

144 files changed

Lines changed: 3766 additions & 3421 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

β€ŽREADME.mdβ€Ž

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,37 @@ device.queue.copyExternalImageToTexture(
222222
);
223223
```
224224

225+
### Reanimated Integration
226+
227+
React Native WebGPU supports running WebGPU rendering on the UI thread using [React Native Reanimated](https://docs.swmansion.com/react-native-reanimated/) and [React Native Worklets](https://github.com/margelo/react-native-worklets).
228+
229+
First, install the optional peer dependencies:
230+
231+
```sh
232+
npm install react-native-reanimated react-native-worklets
233+
```
234+
235+
WebGPU objects are automatically registered for Worklets serialization when the module loads. You can pass WebGPU objects like `GPUDevice` and `GPUCanvasContext` directly to worklets:
236+
237+
```tsx
238+
import { Canvas } from "react-native-wgpu";
239+
import { runOnUI } from "react-native-reanimated";
240+
241+
const renderFrame = (device: GPUDevice, context: GPUCanvasContext) => {
242+
"worklet";
243+
// WebGPU rendering code runs on the UI thread
244+
const commandEncoder = device.createCommandEncoder();
245+
// ... render ...
246+
device.queue.submit([commandEncoder.finish()]);
247+
context.present();
248+
};
249+
250+
// Initialize WebGPU on main thread, then run on UI thread
251+
const device = await adapter.requestDevice();
252+
const context = canvasRef.current.getContext("webgpu");
253+
runOnUI(renderFrame)(device, context);
254+
```
255+
225256
## Troubleshooting
226257

227258
### iOS

β€Žapps/example/ios/Podfile.lockβ€Ž

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1748,7 +1748,7 @@ PODS:
17481748
- React-RCTFBReactNativeSpec
17491749
- ReactCommon/turbomodule/core
17501750
- SocketRocket
1751-
- react-native-safe-area-context (5.6.1):
1751+
- react-native-safe-area-context (5.6.2):
17521752
- boost
17531753
- DoubleConversion
17541754
- fast_float
@@ -1766,8 +1766,8 @@ PODS:
17661766
- React-graphics
17671767
- React-ImageManager
17681768
- React-jsi
1769-
- react-native-safe-area-context/common (= 5.6.1)
1770-
- react-native-safe-area-context/fabric (= 5.6.1)
1769+
- react-native-safe-area-context/common (= 5.6.2)
1770+
- react-native-safe-area-context/fabric (= 5.6.2)
17711771
- React-NativeModulesApple
17721772
- React-RCTFabric
17731773
- React-renderercss
@@ -1778,7 +1778,7 @@ PODS:
17781778
- ReactCommon/turbomodule/core
17791779
- SocketRocket
17801780
- Yoga
1781-
- react-native-safe-area-context/common (5.6.1):
1781+
- react-native-safe-area-context/common (5.6.2):
17821782
- boost
17831783
- DoubleConversion
17841784
- fast_float
@@ -1806,7 +1806,7 @@ PODS:
18061806
- ReactCommon/turbomodule/core
18071807
- SocketRocket
18081808
- Yoga
1809-
- react-native-safe-area-context/fabric (5.6.1):
1809+
- react-native-safe-area-context/fabric (5.6.2):
18101810
- boost
18111811
- DoubleConversion
18121812
- fast_float
@@ -1865,7 +1865,7 @@ PODS:
18651865
- ReactCommon/turbomodule/core
18661866
- SocketRocket
18671867
- Yoga
1868-
- react-native-wgpu (0.4.2):
1868+
- react-native-wgpu (0.5.0):
18691869
- boost
18701870
- DoubleConversion
18711871
- fast_float
@@ -2398,7 +2398,7 @@ PODS:
23982398
- React-perflogger (= 0.81.4)
23992399
- React-utils (= 0.81.4)
24002400
- SocketRocket
2401-
- ReactNativeHost (0.5.13):
2401+
- ReactNativeHost (0.5.15):
24022402
- boost
24032403
- DoubleConversion
24042404
- fast_float
@@ -2432,7 +2432,7 @@ PODS:
24322432
- React-Core
24332433
- React-jsi
24342434
- ReactTestApp-Resources (1.0.0-dev)
2435-
- RNGestureHandler (2.28.0):
2435+
- RNGestureHandler (2.30.0):
24362436
- boost
24372437
- DoubleConversion
24382438
- fast_float
@@ -2552,7 +2552,7 @@ PODS:
25522552
- RNWorklets
25532553
- SocketRocket
25542554
- Yoga
2555-
- RNWorklets (0.7.1):
2555+
- RNWorklets (0.7.2):
25562556
- boost
25572557
- DoubleConversion
25582558
- fast_float
@@ -2579,10 +2579,10 @@ PODS:
25792579
- ReactCodegen
25802580
- ReactCommon/turbomodule/bridging
25812581
- ReactCommon/turbomodule/core
2582-
- RNWorklets/worklets (= 0.7.1)
2582+
- RNWorklets/worklets (= 0.7.2)
25832583
- SocketRocket
25842584
- Yoga
2585-
- RNWorklets/worklets (0.7.1):
2585+
- RNWorklets/worklets (0.7.2):
25862586
- boost
25872587
- DoubleConversion
25882588
- fast_float
@@ -2609,10 +2609,10 @@ PODS:
26092609
- ReactCodegen
26102610
- ReactCommon/turbomodule/bridging
26112611
- ReactCommon/turbomodule/core
2612-
- RNWorklets/worklets/apple (= 0.7.1)
2612+
- RNWorklets/worklets/apple (= 0.7.2)
26132613
- SocketRocket
26142614
- Yoga
2615-
- RNWorklets/worklets/apple (0.7.1):
2615+
- RNWorklets/worklets/apple (0.7.2):
26162616
- boost
26172617
- DoubleConversion
26182618
- fast_float
@@ -2936,9 +2936,9 @@ SPEC CHECKSUMS:
29362936
React-logger: a3cb5b29c32b8e447b5a96919340e89334062b48
29372937
React-Mapbuffer: 9d2434a42701d6144ca18f0ca1c4507808ca7696
29382938
React-microtasksnativemodule: 75b6604b667d297292345302cc5bfb6b6aeccc1b
2939-
react-native-safe-area-context: c6e2edd1c1da07bdce287fa9d9e60c5f7b514616
2939+
react-native-safe-area-context: c00143b4823773bba23f2f19f85663ae89ceb460
29402940
react-native-skia: 5bf2b2107cd7f2d806fd364f5e16b1c7554ed3cd
2941-
react-native-wgpu: 8b91bdc8de384f0fce7dd622698e46645895b085
2941+
react-native-wgpu: e54fcee5946cc2cee4814f63f425be358f097b14
29422942
React-NativeModulesApple: 879fbdc5dcff7136abceb7880fe8a2022a1bd7c3
29432943
React-oscompat: 93b5535ea7f7dff46aaee4f78309a70979bdde9d
29442944
React-perflogger: 5536d2df3d18fe0920263466f7b46a56351c0510
@@ -2967,14 +2967,14 @@ SPEC CHECKSUMS:
29672967
React-timing: 1e6a8acb66e2b7ac9d418956617fd1fdb19322fd
29682968
React-utils: 52bbb03f130319ef82e4c3bc7a85eaacdb1fec87
29692969
ReactAppDependencyProvider: 433ddfb4536948630aadd5bd925aff8a632d2fe3
2970-
ReactCodegen: 64dbbed4e9e0264d799578ea78492479a66fba4a
2970+
ReactCodegen: 7042ec4a7316b59e8f247b8fa312891179d24f5a
29712971
ReactCommon: 394c6b92765cf6d211c2c3f7f6bc601dffb316a6
2972-
ReactNativeHost: 40e374201201cc54f9ef41458f2e412fbdde0d62
2972+
ReactNativeHost: f5e054387e917216a2a021a3f7fdc4f9f158e7e4
29732973
ReactTestApp-DevSupport: 9b7bbba5e8fed998e763809171d9906a1375f9d3
29742974
ReactTestApp-Resources: 1bd9ff10e4c24f2ad87101a32023721ae923bccf
2975-
RNGestureHandler: 3a73f098d74712952870e948b3d9cf7b6cae9961
2975+
RNGestureHandler: e37bdb684df1ac17c7e1d8f71a3311b2793c186b
29762976
RNReanimated: 464375ff2caa801358547c44eca894ff0bf68e74
2977-
RNWorklets: 8068c8af4b241eb2c19221310729e4c440bee023
2977+
RNWorklets: ee58e869ea579800ec5f2f1cb6ae195fd3537546
29782978
SocketRocket: d4aabe649be1e368d1318fdf28a022d714d65748
29792979
Yoga: a3ed390a19db0459bd6839823a6ac6d9c6db198d
29802980

β€Žapps/example/package.jsonβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"react-native-safe-area-context": "^5.4.0",
3838
"react-native-web": "^0.21.2",
3939
"react-native-wgpu": "*",
40-
"react-native-worklets": "0.7.1",
40+
"react-native-worklets": "0.7.2",
4141
"teapot": "^1.0.0",
4242
"three": "0.172.0",
4343
"typegpu": "^0.3.2",

β€Žapps/example/src/Reanimated/Reanimated.tsxβ€Ž

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@ const webGPUDemo = (
1111
runAnimation: SharedValue<boolean>,
1212
device: GPUDevice,
1313
context: RNCanvasContext,
14+
presentationFormat: GPUTextureFormat,
1415
) => {
1516
"worklet";
16-
const presentationFormat = navigator.gpu.getPreferredCanvasFormat();
17-
1817
if (!context) {
1918
throw new Error("No context");
2019
}
@@ -108,8 +107,9 @@ export function Reanimated() {
108107
console.error("Failed to get GPU canvas context");
109108
return;
110109
}
110+
const presentationFormat = navigator.gpu.getPreferredCanvasFormat();
111111
// TODO: stop the animation on unmount
112-
runOnUI(webGPUDemo)(runAnimation, device, ctx);
112+
runOnUI(webGPUDemo)(runAnimation, device, ctx, presentationFormat);
113113
};
114114
initWebGPU();
115115
return () => {

β€Žapps/example/src/Triangle/HelloTriangle.tsxβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export function HelloTriangle() {
1313
if (!adapter) {
1414
throw new Error("No adapter");
1515
}
16+
1617
const device = await adapter.requestDevice();
1718
const presentationFormat = navigator.gpu.getPreferredCanvasFormat();
1819

β€Žpackages/webgpu/README.mdβ€Ž

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,37 @@ device.queue.copyExternalImageToTexture(
222222
);
223223
```
224224

225+
### Reanimated Integration
226+
227+
React Native WebGPU supports running WebGPU rendering on the UI thread using [React Native Reanimated](https://docs.swmansion.com/react-native-reanimated/) and [React Native Worklets](https://github.com/margelo/react-native-worklets).
228+
229+
First, install the optional peer dependencies:
230+
231+
```sh
232+
npm install react-native-reanimated react-native-worklets
233+
```
234+
235+
WebGPU objects are automatically registered for Worklets serialization when the module loads. You can pass WebGPU objects like `GPUDevice` and `GPUCanvasContext` directly to worklets:
236+
237+
```tsx
238+
import { Canvas } from "react-native-wgpu";
239+
import { runOnUI } from "react-native-reanimated";
240+
241+
const renderFrame = (device: GPUDevice, context: GPUCanvasContext) => {
242+
"worklet";
243+
// WebGPU rendering code runs on the UI thread
244+
const commandEncoder = device.createCommandEncoder();
245+
// ... render ...
246+
device.queue.submit([commandEncoder.finish()]);
247+
context.present();
248+
};
249+
250+
// Initialize WebGPU on main thread, then run on UI thread
251+
const device = await adapter.requestDevice();
252+
const context = canvasRef.current.getContext("webgpu");
253+
runOnUI(renderFrame)(device, context);
254+
```
255+
225256
## Troubleshooting
226257

227258
### iOS

β€Žpackages/webgpu/android/CMakeLists.txtβ€Ž

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ add_library(${PACKAGE_NAME} SHARED
4444
../cpp/rnwgpu/api/GPUComputePipeline.cpp
4545
../cpp/rnwgpu/api/GPUCanvasContext.cpp
4646
../cpp/rnwgpu/RNWebGPUManager.cpp
47-
../cpp/jsi/RNFPromise.cpp
48-
../cpp/jsi/RNFHybridObject.cpp
49-
../cpp/jsi/RNFRuntimeState.cpp
47+
../cpp/jsi/Promise.cpp
48+
../cpp/jsi/RuntimeLifecycleMonitor.cpp
49+
../cpp/jsi/RuntimeAwareCache.cpp
5050
../cpp/rnwgpu/async/AsyncRunner.cpp
5151
../cpp/rnwgpu/async/AsyncTaskHandle.cpp
5252
../cpp/rnwgpu/async/JSIMicrotaskDispatcher.cpp
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
module.exports = {
22
presets: ['module:@react-native/babel-preset'],
3+
plugins: ['react-native-worklets/plugin'],
34
};
Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
//
2-
// Created by Marc Rousavy on 22.02.24.
3-
//
4-
51
#pragma once
62

73
#include <stdexcept>
84
#include <string>
95

10-
namespace margelo {
6+
namespace rnwgpu {
117

128
namespace EnumMapper {
139
// Add these two methods in namespace "EnumMapper" to allow parsing a custom enum:
@@ -46,4 +42,4 @@ namespace EnumMapper {
4642
}
4743
} // namespace EnumMapper
4844

49-
} // namespace margelo
45+
} // namespace rnwgpu

0 commit comments

Comments
Β (0)