-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.tsx
104 lines (93 loc) · 2.3 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import React, { Component, Fragment } from 'react';
import { Button, StyleSheet, Text, View } from 'react-native';
import { GLView } from 'expo-gl';
import { GPU, IKernelRunShortcut } from '@gpujs/expo-gl';
async function createVanillaKernel() {
const context = await GLView.createContextAsync();
const gpu = new GPU({ context });
return gpu.createKernel(function () {
return Math.random();
}, {
output: [2048, 2048],
});
}
interface IAppState {
error: any,
kernel: IKernelRunShortcut,
milliseconds: number,
kernelResult: number[][],
kernelRunning: boolean,
}
export default class App extends Component<any, IAppState> {
setError = error => {
this.setState({ error });
};
setKernel = async kernel => {
this.setState({ kernel });
};
runKernel = () => {
this.setState({
kernelRunning: true,
});
const start = Date.now();
const result = this.state.kernel();
const milliseconds = Date.now() - start;
this.setState({
kernelResult: result as number[][],
milliseconds,
kernelRunning: false,
});
};
constructor(props) {
super(props);
this.state = {
error: null,
kernel: null,
kernelResult: null,
milliseconds: null,
kernelRunning: false,
};
}
componentDidMount(): void {
createVanillaKernel()
.catch(this.setError)
.then(this.setKernel);
}
render() {
const {
error,
kernel,
kernelResult,
milliseconds,
kernelRunning,
} = this.state;
let children;
if (error) {
children = <Text>There was an error { error.toString() }</Text>;
} else if (!kernel) {
children = <Text>Loading context</Text>;
} else {
children = <Fragment>
<Button
disabled={kernelRunning}
onPress={this.runKernel}
title="Tap to run Kernel"
/>
{
kernelResult
? <Text>Kernel calculated with length of { kernelResult.length * kernelResult[0].length } and took { milliseconds } milliseconds</Text>
: null
}
</Fragment>
}
return (<View style={styles.container}>{children}</View>);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});