|
1 | | -import React, { Component, Fragment } from 'react'; |
2 | | -import { Button, StyleSheet, Text, View } from 'react-native'; |
| 1 | +import React, { useCallback, useEffect, useState } from 'react'; |
| 2 | +import { Button, ScrollView, StyleSheet, Text, View } from 'react-native'; |
| 3 | +import { StatusBar } from 'expo-status-bar'; |
3 | 4 | import { GLView } from 'expo-gl'; |
4 | | -import { GPU, IKernelRunShortcut } from '@gpujs/expo-gl'; |
5 | | - |
6 | | -async function createVanillaKernel() { |
7 | | - const context = await GLView.createContextAsync(); |
8 | | - const gpu = new GPU({ context }); |
9 | | - return gpu.createKernel(function () { |
10 | | - return Math.random(); |
11 | | - }, { |
12 | | - output: [2048, 2048], |
13 | | - }); |
14 | | -} |
| 5 | +import { GPU } from '@gpujs/expo-gl'; |
| 6 | + |
| 7 | +// Deliberately a diagnostic rather than a minimal demo: each stage of bringing |
| 8 | +// GPU.js up on Expo is reported separately, so a failure on a device says which |
| 9 | +// stage broke rather than just "it didn't work". |
| 10 | +type StageState = 'pending' | 'running' | 'done' | 'failed'; |
15 | 11 |
|
16 | | -interface IAppState { |
17 | | - error: any, |
18 | | - kernel: IKernelRunShortcut, |
19 | | - milliseconds: number, |
20 | | - kernelResult: number[][], |
21 | | - kernelRunning: boolean, |
| 12 | +interface Stage { |
| 13 | + name: string; |
| 14 | + state: StageState; |
| 15 | + detail?: string; |
22 | 16 | } |
23 | 17 |
|
24 | | -export default class App extends Component<any, IAppState> { |
25 | | - setError = error => { |
26 | | - this.setState({ error }); |
27 | | - }; |
28 | | - |
29 | | - setKernel = async kernel => { |
30 | | - this.setState({ kernel }); |
31 | | - }; |
32 | | - |
33 | | - runKernel = () => { |
34 | | - this.setState({ |
35 | | - kernelRunning: true, |
36 | | - }); |
37 | | - const start = Date.now(); |
38 | | - const result = this.state.kernel(); |
39 | | - const milliseconds = Date.now() - start; |
40 | | - this.setState({ |
41 | | - kernelResult: result as number[][], |
42 | | - milliseconds, |
43 | | - kernelRunning: false, |
44 | | - }); |
45 | | - }; |
46 | | - |
47 | | - constructor(props) { |
48 | | - super(props); |
49 | | - this.state = { |
50 | | - error: null, |
51 | | - kernel: null, |
52 | | - kernelResult: null, |
53 | | - milliseconds: null, |
54 | | - kernelRunning: false, |
55 | | - }; |
56 | | - } |
| 18 | +const STAGES = [ |
| 19 | + 'Create GL context', |
| 20 | + 'Create GPU', |
| 21 | + 'Detect features', |
| 22 | + 'Compile kernel', |
| 23 | + 'Run kernel', |
| 24 | +] as const; |
57 | 25 |
|
58 | | - componentDidMount(): void { |
59 | | - createVanillaKernel() |
60 | | - .catch(this.setError) |
61 | | - .then(this.setKernel); |
62 | | - } |
| 26 | +const SIZE = 512; |
| 27 | + |
| 28 | +export default function App() { |
| 29 | + const [stages, setStages] = useState<Stage[]>( |
| 30 | + STAGES.map(name => ({ name, state: 'pending' })) |
| 31 | + ); |
| 32 | + const [kernel, setKernel] = useState<((...args: any[]) => any) | null>(null); |
| 33 | + const [result, setResult] = useState<string | null>(null); |
| 34 | + const [milliseconds, setMilliseconds] = useState<number | null>(null); |
| 35 | + |
| 36 | + const update = useCallback((index: number, state: StageState, detail?: string) => { |
| 37 | + setStages(current => current.map((stage, i) => ( |
| 38 | + i === index ? { ...stage, state, detail } : stage |
| 39 | + ))); |
| 40 | + }, []); |
| 41 | + |
| 42 | + useEffect(() => { |
| 43 | + let cancelled = false; |
| 44 | + |
| 45 | + (async () => { |
| 46 | + let stage = 0; |
| 47 | + try { |
| 48 | + update(stage, 'running'); |
| 49 | + const context = await GLView.createContextAsync(); |
| 50 | + if (cancelled) return; |
| 51 | + update(stage, 'done', `contextId ${(context as any).contextId}`); |
| 52 | + |
| 53 | + stage = 1; |
| 54 | + update(stage, 'running'); |
| 55 | + const gpu = new GPU({ context }); |
| 56 | + update(stage, 'done', gpu.Kernel?.name ?? 'kernel selected'); |
| 57 | + |
| 58 | + stage = 2; |
| 59 | + update(stage, 'running'); |
| 60 | + // features are detected lazily, and detection itself compiles and runs |
| 61 | + // a probe kernel — so this stage exercises real GL work |
| 62 | + const features = (gpu.Kernel as any).features; |
| 63 | + update(stage, 'done', `float read ${features?.isFloatRead}, max texture ${features?.maxTextureSize}`); |
63 | 64 |
|
64 | | - render() { |
65 | | - const { |
66 | | - error, |
67 | | - kernel, |
68 | | - kernelResult, |
69 | | - milliseconds, |
70 | | - kernelRunning, |
71 | | - } = this.state; |
72 | | - |
73 | | - let children; |
74 | | - |
75 | | - if (error) { |
76 | | - children = <Text>There was an error { error.toString() }</Text>; |
77 | | - } else if (!kernel) { |
78 | | - children = <Text>Loading context</Text>; |
79 | | - } else { |
80 | | - children = <Fragment> |
81 | | - <Button |
82 | | - disabled={kernelRunning} |
83 | | - onPress={this.runKernel} |
84 | | - title="Tap to run Kernel" |
85 | | - /> |
86 | | - { |
87 | | - kernelResult |
88 | | - ? <Text>Kernel calculated with length of { kernelResult.length * kernelResult[0].length } and took { milliseconds } milliseconds</Text> |
89 | | - : null |
90 | | - } |
91 | | - </Fragment> |
| 65 | + stage = 3; |
| 66 | + update(stage, 'running'); |
| 67 | + const built = gpu.createKernel(function (a: number[][], b: number[][]) { |
| 68 | + let sum = 0; |
| 69 | + for (let i = 0; i < 512; i++) { |
| 70 | + sum += a[this.thread.y][i] * b[i][this.thread.x]; |
| 71 | + } |
| 72 | + return sum; |
| 73 | + }).setOutput([SIZE, SIZE]); |
| 74 | + update(stage, 'done', `${SIZE}x${SIZE} matrix multiply`); |
| 75 | + |
| 76 | + if (!cancelled) setKernel(() => built); |
| 77 | + } catch (error: any) { |
| 78 | + if (!cancelled) update(stage, 'failed', String(error?.message ?? error)); |
| 79 | + } |
| 80 | + })(); |
| 81 | + |
| 82 | + return () => { cancelled = true; }; |
| 83 | + }, [update]); |
| 84 | + |
| 85 | + const run = useCallback(() => { |
| 86 | + if (!kernel) return; |
| 87 | + update(4, 'running'); |
| 88 | + try { |
| 89 | + const a = Array.from({ length: SIZE }, () => Array.from({ length: SIZE }, () => Math.random())); |
| 90 | + const b = Array.from({ length: SIZE }, () => Array.from({ length: SIZE }, () => Math.random())); |
| 91 | + const start = Date.now(); |
| 92 | + const output = kernel(a, b) as number[][]; |
| 93 | + const elapsed = Date.now() - start; |
| 94 | + setMilliseconds(elapsed); |
| 95 | + setResult(`${output.length}x${output[0].length}, [0][0] = ${output[0][0].toFixed(4)}`); |
| 96 | + update(4, 'done', `${elapsed} ms`); |
| 97 | + } catch (error: any) { |
| 98 | + update(4, 'failed', String(error?.message ?? error)); |
92 | 99 | } |
93 | | - return (<View style={styles.container}>{children}</View>); |
| 100 | + }, [kernel, update]); |
| 101 | + |
| 102 | + const failed = stages.some(stage => stage.state === 'failed'); |
| 103 | + |
| 104 | + return ( |
| 105 | + <View style={styles.container}> |
| 106 | + <StatusBar style="auto" /> |
| 107 | + <Text style={styles.title}>GPU.js on Expo</Text> |
| 108 | + <ScrollView style={styles.stages} contentContainerStyle={styles.stagesContent}> |
| 109 | + {stages.map(stage => ( |
| 110 | + <View key={stage.name} style={styles.stage}> |
| 111 | + <Text style={styles.marker}>{marker(stage.state)}</Text> |
| 112 | + <View style={styles.stageText}> |
| 113 | + <Text style={stage.state === 'failed' ? styles.failed : styles.name}>{stage.name}</Text> |
| 114 | + {stage.detail ? <Text style={styles.detail}>{stage.detail}</Text> : null} |
| 115 | + </View> |
| 116 | + </View> |
| 117 | + ))} |
| 118 | + </ScrollView> |
| 119 | + |
| 120 | + <Button title={kernel ? 'Run kernel' : 'Preparing…'} onPress={run} disabled={!kernel} /> |
| 121 | + |
| 122 | + {result ? ( |
| 123 | + <Text style={styles.result}>{result}{milliseconds !== null ? ` in ${milliseconds} ms` : ''}</Text> |
| 124 | + ) : null} |
| 125 | + {failed ? ( |
| 126 | + <Text style={styles.hint}> |
| 127 | + Report this at github.com/gpujs/expo-gl/issues, including the failing stage above. |
| 128 | + </Text> |
| 129 | + ) : null} |
| 130 | + </View> |
| 131 | + ); |
| 132 | +} |
| 133 | + |
| 134 | +function marker(state: StageState) { |
| 135 | + switch (state) { |
| 136 | + case 'done': return '✓'; |
| 137 | + case 'failed': return '✗'; |
| 138 | + case 'running': return '…'; |
| 139 | + default: return '·'; |
94 | 140 | } |
95 | 141 | } |
96 | 142 |
|
97 | 143 | const styles = StyleSheet.create({ |
98 | | - container: { |
99 | | - flex: 1, |
100 | | - backgroundColor: '#fff', |
101 | | - alignItems: 'center', |
102 | | - justifyContent: 'center', |
103 | | - }, |
| 144 | + container: { flex: 1, backgroundColor: '#fff', padding: 24, paddingTop: 72 }, |
| 145 | + title: { fontSize: 22, fontWeight: '600', marginBottom: 20 }, |
| 146 | + stages: { flexGrow: 0, marginBottom: 20 }, |
| 147 | + stagesContent: { gap: 12 }, |
| 148 | + stage: { flexDirection: 'row', gap: 10 }, |
| 149 | + marker: { width: 16, fontSize: 15 }, |
| 150 | + stageText: { flex: 1 }, |
| 151 | + name: { fontSize: 15 }, |
| 152 | + failed: { fontSize: 15, color: '#b00020', fontWeight: '600' }, |
| 153 | + detail: { fontSize: 12, color: '#666', marginTop: 2 }, |
| 154 | + result: { marginTop: 16, fontSize: 14 }, |
| 155 | + hint: { marginTop: 16, fontSize: 12, color: '#666' }, |
104 | 156 | }); |
0 commit comments