Skip to content

Commit e0eed38

Browse files
authored
Merge branch 'main' into gc-heuristics
2 parents 4de88c3 + 926f5d3 commit e0eed38

38 files changed

Lines changed: 1190 additions & 385 deletions

.github/workflows/ci.yml

Lines changed: 82 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,35 @@ on:
99
merge_group:
1010
types:
1111
- checks_requested
12+
workflow_dispatch:
13+
inputs:
14+
build_android:
15+
description: 'Build for Android'
16+
type: boolean
17+
default: false
18+
build_ios:
19+
description: 'Build for iOS'
20+
type: boolean
21+
default: false
22+
build_macos:
23+
description: 'Build for macOS'
24+
type: boolean
25+
default: false
26+
run_reference_tests:
27+
description: 'Run reference tests'
28+
type: boolean
29+
default: false
1230

1331
concurrency:
1432
group: ${{ github.ref }}
1533
cancel-in-progress: true
1634

1735
jobs:
18-
static-analysis:
36+
build:
1937
runs-on: ubuntu-latest
2038
steps:
2139
- name: Checkout
22-
uses: actions/checkout@v3
40+
uses: actions/checkout@v4
2341

2442
- name: Setup
2543
uses: ./.github/actions/setup
@@ -36,11 +54,27 @@ jobs:
3654
- name: Typecheck files
3755
run: yarn tsc
3856

39-
test:
57+
- name: Build
58+
working-directory: packages/webgpu
59+
run: yarn build
60+
61+
- name: Package
62+
working-directory: packages/webgpu
63+
run: yarn pack
64+
65+
- name: Upload package artifact
66+
uses: actions/upload-artifact@v4
67+
with:
68+
name: package
69+
path: packages/webgpu/package.tgz
70+
compression-level: 0
71+
72+
reference-tests:
73+
if: github.event_name == 'workflow_dispatch' && github.event.inputs.run_reference_tests == 'true'
4074
runs-on: macos-latest
4175
steps:
4276
- name: Checkout
43-
uses: actions/checkout@v3
77+
uses: actions/checkout@v4
4478

4579
- name: Setup
4680
uses: ./.github/actions/setup
@@ -54,24 +88,22 @@ jobs:
5488
working-directory: packages/webgpu
5589
run: yarn test:ref
5690

57-
- name: Build package
58-
run: yarn build
59-
6091
build-android:
92+
if: github.event_name == 'workflow_dispatch' && github.event.inputs.build_android == 'true'
6193
runs-on: macos-latest
6294
env:
6395
TURBO_CACHE_DIR: .turbo/android
6496
steps:
6597
- name: Checkout
66-
uses: actions/checkout@v3
98+
uses: actions/checkout@v4
6799

68100
- name: Setup
69101
uses: ./.github/actions/setup
70102
with:
71103
github_token: ${{ secrets.GITHUB_TOKEN }}
72104

73105
- name: Cache turborepo for Android
74-
uses: actions/cache@v3
106+
uses: actions/cache@v4
75107
with:
76108
path: ${{ env.TURBO_CACHE_DIR }}
77109
key: ${{ runner.os }}-turborepo-android-${{ hashFiles('yarn.lock') }}
@@ -98,7 +130,7 @@ jobs:
98130
id: setup-ndk
99131
with:
100132
ndk-version: r27d
101-
133+
102134
- name: Set ANDROID_NDK
103135
run: echo "ANDROID_NDK=$ANDROID_HOME/ndk-bundle" >> $GITHUB_ENV
104136

@@ -112,7 +144,7 @@ jobs:
112144

113145
- name: Cache Gradle
114146
if: env.turbo_cache_hit != 1
115-
uses: actions/cache@v3
147+
uses: actions/cache@v4
116148
with:
117149
path: |
118150
~/.gradle/wrapper
@@ -128,20 +160,21 @@ jobs:
128160
yarn turbo run build:android --cache-dir="${{ env.TURBO_CACHE_DIR }}" --force
129161
130162
build-ios:
163+
if: github.event_name == 'workflow_dispatch' && github.event.inputs.build_ios == 'true'
131164
runs-on: macos-latest
132165
env:
133166
TURBO_CACHE_DIR: .turbo/ios
134167
steps:
135168
- name: Checkout
136-
uses: actions/checkout@v3
169+
uses: actions/checkout@v4
137170

138171
- name: Setup
139172
uses: ./.github/actions/setup
140173
with:
141174
github_token: ${{ secrets.GITHUB_TOKEN }}
142175

143176
- name: Cache turborepo for iOS
144-
uses: actions/cache@v3
177+
uses: actions/cache@v4
145178
with:
146179
path: ${{ env.TURBO_CACHE_DIR }}
147180
key: ${{ runner.os }}-turborepo-ios-${{ hashFiles('yarn.lock') }}
@@ -159,36 +192,39 @@ jobs:
159192
- name: Build example for iOS
160193
run: |
161194
yarn turbo run build:ios --cache-dir="${{ env.TURBO_CACHE_DIR }}"
162-
# We disable it until RNTA support 0.81
163-
# build-macos:
164-
# runs-on: macos-latest
165-
# env:
166-
# TURBO_CACHE_DIR: .turbo/macos
167-
# steps:
168-
# - name: Checkout
169-
# uses: actions/checkout@v3
170-
171-
# - name: Setup
172-
# uses: ./.github/actions/setup
173-
# with:
174-
# github_token: ${{ secrets.GITHUB_TOKEN }}
175-
176-
# - name: Cache turborepo for macOS
177-
# uses: actions/cache@v3
178-
# with:
179-
# path: ${{ env.TURBO_CACHE_DIR }}
180-
# key: ${{ runner.os }}-turborepo-macos-${{ hashFiles('yarn.lock') }}
181-
# restore-keys: |
182-
# ${{ runner.os }}-turborepo-macos-
183-
184-
# - name: Check turborepo cache for macOS
185-
# run: |
186-
# TURBO_CACHE_STATUS=$(node -p "($(yarn turbo run build:macos --cache-dir="${{ env.TURBO_CACHE_DIR }}" --dry=json)).tasks.find(t => t.task === 'build:macos').cache.status")
187-
188-
# if [[ $TURBO_CACHE_STATUS == "HIT" ]]; then
189-
# echo "turbo_cache_hit=1" >> $GITHUB_ENV
190-
# fi
191-
192-
# - name: Build example for macOS
193-
# run: |
194-
# yarn turbo run build:macos --cache-dir="${{ env.TURBO_CACHE_DIR }}"
195+
196+
# Note: This job is disabled by default until RNTA supports 0.81
197+
# Enable it manually via workflow_dispatch when needed
198+
build-macos:
199+
if: github.event_name == 'workflow_dispatch' && github.event.inputs.build_macos == 'true'
200+
runs-on: macos-latest
201+
env:
202+
TURBO_CACHE_DIR: .turbo/macos
203+
steps:
204+
- name: Checkout
205+
uses: actions/checkout@v4
206+
207+
- name: Setup
208+
uses: ./.github/actions/setup
209+
with:
210+
github_token: ${{ secrets.GITHUB_TOKEN }}
211+
212+
- name: Cache turborepo for macOS
213+
uses: actions/cache@v4
214+
with:
215+
path: ${{ env.TURBO_CACHE_DIR }}
216+
key: ${{ runner.os }}-turborepo-macos-${{ hashFiles('yarn.lock') }}
217+
restore-keys: |
218+
${{ runner.os }}-turborepo-macos-
219+
220+
- name: Check turborepo cache for macOS
221+
run: |
222+
TURBO_CACHE_STATUS=$(node -p "($(yarn turbo run build:macos --cache-dir="${{ env.TURBO_CACHE_DIR }}" --dry=json)).tasks.find(t => t.task === 'build:macos').cache.status")
223+
224+
if [[ $TURBO_CACHE_STATUS == "HIT" ]]; then
225+
echo "turbo_cache_hit=1" >> $GITHUB_ENV
226+
fi
227+
228+
- name: Build example for macOS
229+
run: |
230+
yarn turbo run build:macos --cache-dir="${{ env.TURBO_CACHE_DIR }}"

README.md

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
# React Native WebGPU
22

33
React Native implementation of WebGPU using [Dawn](https://dawn.googlesource.com/dawn).
4-
This is currently a technical preview for early adopters.
54

6-
React Native WebGPU requires React Native 0.81 or newer and doesn't run on legacy architecture.
5+
React Native WebGPU requires React Native 0.81 or newer. It doesn't support the legacy architecture.
76

87
## Installation
98

@@ -13,6 +12,17 @@ Please note that the package name is `react-native-wgpu`.
1312
npm install react-native-wgpu
1413
```
1514

15+
## With Expo
16+
17+
Expo provides a React Native WebGPU template that works with React Three Fiber.
18+
The works on iOS, Android, and Web.
19+
20+
```
21+
npx create-expo-app@latest -e with-webgpu
22+
```
23+
24+
https://github.com/user-attachments/assets/efbd05f8-4ce0-46c2-919c-03e1095bc8ac
25+
1626
Below are some examples from the [example app](/apps/example/).
1727

1828
https://github.com/user-attachments/assets/116a41b2-2cf8-49f1-9f16-a5c83637c198
@@ -175,6 +185,19 @@ device.queue.submit([commandEncoder.finish()]);
175185
context.present();
176186
```
177187

188+
### Canvas Transparency
189+
190+
On Android, the `alphaMode` property is ignored when configuring the canvas.
191+
To have a transparent canvas by default, use the `transparent` property.
192+
193+
```tsx
194+
return (
195+
<View style={style.container}>
196+
<Canvas ref={ref} style={style.webgpu} transparent />
197+
</View>
198+
);
199+
```
200+
178201
### External Textures
179202

180203
This module provides a `createImageBitmap` function that you can use in `copyExternalImageToTexture`.

apps/example/getWebMetroConfig.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const path = require("path");
2+
const root = path.resolve(__dirname, "../..");
3+
const rnwPath = path.resolve(root, "node_modules/react-native-web");
4+
const assetRegistryPath = path.resolve(
5+
root,
6+
"node_modules/react-native-web/dist/modules/AssetRegistry/index",
7+
);
8+
9+
module.exports = function (metroConfig) {
10+
metroConfig.resolver.platforms = ["ios", "android", "web"];
11+
const origResolveRequest = metroConfig.resolver.resolveRequest;
12+
metroConfig.resolver.resolveRequest = (contextRaw, moduleName, platform) => {
13+
const context = {
14+
...contextRaw,
15+
preferNativePlatform: false,
16+
};
17+
18+
if (moduleName === "react-native") {
19+
return {
20+
filePath: path.resolve(rnwPath, "dist/index.js"),
21+
type: "sourceFile",
22+
};
23+
}
24+
25+
// Let default config handle other modules
26+
return origResolveRequest(context, moduleName, platform);
27+
};
28+
29+
metroConfig.transformer.assetRegistryPath = assetRegistryPath;
30+
31+
return metroConfig;
32+
};

apps/example/index.html

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta httpEquiv="X-UA-Compatible" content="IE=edge" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
7+
<link rel="icon" href="/src/assets/react.png" type="image/png">
8+
<script src="https://unpkg.com/canvaskit-wasm/bin/full/canvaskit.js"></script>
9+
10+
<title>Example</title>
11+
<!-- The `react-native-web` recommended style reset: https://necolas.github.io/react-native-web/docs/setup/#root-element -->
12+
<style id="react-native-web-reset">
13+
/* These styles make the body full-height */
14+
html,
15+
body {
16+
height: 100%;
17+
}
18+
/* These styles disable body scrolling if you are using <ScrollView> */
19+
body {
20+
overflow: hidden;
21+
}
22+
/* These styles make the root element full-height */
23+
#root {
24+
display: flex;
25+
height: 100%;
26+
flex: 1;
27+
}
28+
</style>
29+
</head>
30+
<body>
31+
<noscript>
32+
You need to enable JavaScript to run this app.
33+
</noscript>
34+
<div id="root"></div>
35+
<script src="index.bundle?platform=web&dev=true&hot=false&lazy=true&transform.engine=hermes&transform.routerRoot=app&unstable_transformProfile=hermes-stable" defer></script>
36+
</body>
37+
</html>

apps/example/index.web.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { AppRegistry } from "react-native";
2+
3+
import App from "./src/App";
4+
import { name as appName } from "./app.json";
5+
6+
AppRegistry.registerComponent(appName, () => App);
7+
8+
const rootTag = document.getElementById("root");
9+
if (process.env.NODE_ENV !== "production") {
10+
if (!rootTag) {
11+
throw new Error(
12+
'Required HTML element with id "root" was not found in the document HTML.',
13+
);
14+
}
15+
}
16+
17+
CanvasKitInit({
18+
locateFile: (file) => `https://unpkg.com/canvaskit-wasm/bin/full/${file}`,
19+
}).then((CanvasKit) => {
20+
window.CanvasKit = global.CanvasKit = CanvasKit;
21+
AppRegistry.runApplication(appName, { rootTag });
22+
});

apps/example/ios/Podfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1865,7 +1865,7 @@ PODS:
18651865
- ReactCommon/turbomodule/core
18661866
- SocketRocket
18671867
- Yoga
1868-
- react-native-wgpu (0.3.0):
1868+
- react-native-wgpu (0.4.1):
18691869
- boost
18701870
- DoubleConversion
18711871
- fast_float
@@ -2903,7 +2903,7 @@ SPEC CHECKSUMS:
29032903
React-microtasksnativemodule: 75b6604b667d297292345302cc5bfb6b6aeccc1b
29042904
react-native-safe-area-context: c6e2edd1c1da07bdce287fa9d9e60c5f7b514616
29052905
react-native-skia: 5bf2b2107cd7f2d806fd364f5e16b1c7554ed3cd
2906-
react-native-wgpu: 6704c6ad8ef91fdbbad58543089ac50c0e6f3fa7
2906+
react-native-wgpu: 9b91079473265b91dc2c60c692845b70a4f0182b
29072907
React-NativeModulesApple: 879fbdc5dcff7136abceb7880fe8a2022a1bd7c3
29082908
React-oscompat: 93b5535ea7f7dff46aaee4f78309a70979bdde9d
29092909
React-perflogger: 5536d2df3d18fe0920263466f7b46a56351c0510

0 commit comments

Comments
 (0)