-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
GLHeadlessRenderingScreen.tsx
229 lines (184 loc) · 6.05 KB
/
GLHeadlessRenderingScreen.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import Slider from '@react-native-community/slider';
import { Asset } from 'expo-asset';
import * as FileSystem from 'expo-file-system';
import * as GL from 'expo-gl';
import { GLView } from 'expo-gl';
import React from 'react';
import { Image, StyleSheet, Text, View } from 'react-native';
import exampleImage from '../../../assets/images/example3.jpg';
const vertShaderSource = `#version 300 es
precision mediump float;
in vec2 position;
out vec2 textureCoord;
void main() {
vec2 clipSpace = (1.0 - 2.0 * position) * vec2(-1.0, -1.0);
textureCoord = position;
gl_Position = vec4(clipSpace, 0.0, 1.0);
}`;
const fragShaderSource = `#version 300 es
precision mediump float;
uniform sampler2D inputImageTexture;
uniform float contrast;
in vec2 textureCoord;
out vec4 fragColor;
void main() {
vec4 textureColor = texture(inputImageTexture, textureCoord);
vec3 rgb = (textureColor.rgb - vec3(0.5)) * contrast + vec3(0.5);
fragColor = vec4(rgb, textureColor.a);
}`;
// location to contrast uniform
let contrastLocation: WebGLUniformLocation | null;
// create and prepare GL context
const glPromise = GLView.createContextAsync().then(async gl => {
// Compile vertex and fragment shaders
const vertShader = gl.createShader(gl.VERTEX_SHADER)!;
gl.shaderSource(vertShader, vertShaderSource);
gl.compileShader(vertShader);
const fragShader = gl.createShader(gl.FRAGMENT_SHADER)!;
gl.shaderSource(fragShader, fragShaderSource);
gl.compileShader(fragShader);
// Link, use program, save and enable attributes
const program = gl.createProgram()!;
gl.attachShader(program, vertShader);
gl.attachShader(program, fragShader);
gl.linkProgram(program);
gl.validateProgram(program);
gl.useProgram(program);
contrastLocation = gl.getUniformLocation(program, 'contrast');
// create framebuffer
const framebuffer = gl.createFramebuffer();
gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
// Create, bind, fill buffer
const buffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
const verts = new Float32Array([0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0]);
gl.bufferData(gl.ARRAY_BUFFER, verts, gl.STATIC_DRAW);
// Bind 'position' attribute
const positionAttrib = gl.getAttribLocation(program, 'position');
gl.enableVertexAttribArray(positionAttrib);
gl.vertexAttribPointer(positionAttrib, 2, gl.FLOAT, false, 0, 0);
// download an asset
const asset = Asset.fromModule(exampleImage);
await asset.downloadAsync();
// set viewport
gl.viewport(0, 0, asset.width!, asset.height!);
// create output texture
const outputTexture = gl.createTexture();
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, outputTexture);
gl.texImage2D(
gl.TEXTURE_2D,
0,
gl.RGBA,
asset.width!,
asset.height!,
0,
gl.RGBA,
gl.UNSIGNED_BYTE,
null
);
// create input texture
const inputTexture = gl.createTexture();
gl.activeTexture(gl.TEXTURE1);
gl.bindTexture(gl.TEXTURE_2D, inputTexture);
// attach texture to framebuffer
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, outputTexture, 0);
// set texture parameters
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
// fill up input texture
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, asset as any);
gl.uniform1i(gl.getUniformLocation(program, 'inputImageTexture'), 1);
gl.clearColor(0, 0, 0, 1);
// tslint:disable-next-line: no-bitwise
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
return gl;
});
interface State {
contrast: number;
snapshot?: GL.GLSnapshot;
}
export default class GLHeadlessRenderingScreen extends React.PureComponent<{}, State> {
static title = 'Headless rendering';
isDrawing = false;
readonly state: State = {
contrast: 1.0,
};
componentDidMount() {
this.draw();
}
async draw() {
if (this.isDrawing) {
// if another draw call is already running, we need to skip this one
return;
}
this.isDrawing = true;
const gl = await glPromise;
gl.uniform1f(contrastLocation, this.state.contrast);
gl.drawArrays(gl.TRIANGLES, 0, 6);
gl.endFrameEXP();
// we need to use flip option because framebuffer contents are flipped vertically
const snapshot = await GLView.takeSnapshotAsync(gl, {
flip: true,
});
// delete previous snapshot
if (this.state.snapshot) {
FileSystem.deleteAsync(this.state.snapshot.uri as string, { idempotent: true });
}
this.setState({ snapshot });
this.isDrawing = false;
}
onContrastChange = (contrast: number) => {
this.setState({ contrast }, () => {
this.draw();
});
};
render() {
const { contrast, snapshot } = this.state;
return (
<View style={styles.container}>
<Text>
This example shows how to use headless rendering context with Expo.GLView. Instead of the
GLView, it renders snapshots taken from headless context that sits on top of the app and
can be reused in multiple components without redundant shader recompilations.
</Text>
<View style={styles.flex}>
{snapshot && (
<Image
style={styles.flex}
fadeDuration={0}
source={{ uri: snapshot.uri as string }}
resizeMode="contain"
/>
)}
</View>
<Text style={styles.sliderHeader}>{`Contrast: ${parseInt(
String(contrast * 100),
10
)}%`}</Text>
<Slider
value={contrast}
step={0.01}
minimumValue={0}
maximumValue={2}
onValueChange={this.onContrastChange}
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
padding: 10,
},
flex: {
flex: 1,
width: '100%',
},
sliderHeader: {
paddingHorizontal: 10,
},
});