|
| 1 | +import React, { Component, Fragment } from 'react'; |
| 2 | +import { Button, StyleSheet, Text, View } from 'react-native'; |
| 3 | +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 | +} |
| 15 | + |
| 16 | +interface IAppState { |
| 17 | + error: any, |
| 18 | + kernel: IKernelRunShortcut, |
| 19 | + milliseconds: number, |
| 20 | + kernelResult: number[][], |
| 21 | + kernelRunning: boolean, |
| 22 | +} |
| 23 | + |
| 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 | + } |
| 57 | + |
| 58 | + componentDidMount(): void { |
| 59 | + createVanillaKernel() |
| 60 | + .catch(this.setError) |
| 61 | + .then(this.setKernel); |
| 62 | + } |
| 63 | + |
| 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> |
| 92 | + } |
| 93 | + return (<View style={styles.container}>{children}</View>); |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +const styles = StyleSheet.create({ |
| 98 | + container: { |
| 99 | + flex: 1, |
| 100 | + backgroundColor: '#fff', |
| 101 | + alignItems: 'center', |
| 102 | + justifyContent: 'center', |
| 103 | + }, |
| 104 | +}); |
0 commit comments