-
Notifications
You must be signed in to change notification settings - Fork 17
/
GPGPU.h
333 lines (252 loc) · 9.87 KB
/
GPGPU.h
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#include <string>
#include <unordered_map>
#include <stdexcept>
// Copied over only the necessary code from the html5.h emscripten library
// https://github.com/kripken/emscripten/blob/master/system/include/emscripten/html5.h
// http://kripken.github.io/emscripten-site/docs/api_reference/html5.h
extern "C" {
#define EM_BOOL int
#define EMSCRIPTEN_RESULT int
typedef int EMSCRIPTEN_WEBGL_CONTEXT_HANDLE;
typedef struct EmscriptenWebGLContextAttributes {
EM_BOOL alpha;
EM_BOOL depth;
EM_BOOL stencil;
EM_BOOL antialias;
EM_BOOL premultipliedAlpha;
EM_BOOL preserveDrawingBuffer;
EM_BOOL preferLowPowerToHighPerformance;
EM_BOOL failIfMajorPerformanceCaveat;
int majorVersion;
int minorVersion;
EM_BOOL enableExtensionsByDefault;
EM_BOOL explicitSwapControl;
} EmscriptenWebGLContextAttributes;
extern EMSCRIPTEN_WEBGL_CONTEXT_HANDLE emscripten_webgl_create_context(const char *target, const EmscriptenWebGLContextAttributes *attributes);
extern EMSCRIPTEN_RESULT emscripten_webgl_destroy_context(EMSCRIPTEN_WEBGL_CONTEXT_HANDLE context);
extern EMSCRIPTEN_RESULT emscripten_webgl_make_context_current(EMSCRIPTEN_WEBGL_CONTEXT_HANDLE context);
}
class Tex {
public:
GLuint texId;
int width;
int height;
Tex (GLuint id, int w, int h) {
texId = id;
width = w;
height = h;
}
};
class GPGPU {
public:
bool ready = false;
int height = 0;
int width = 0;
std::unordered_map<std::string, int> attribs;
std::unordered_map<std::string, int> uniforms;
GLuint program;
std::vector<Tex> textures;
EMSCRIPTEN_WEBGL_CONTEXT_HANDLE gl;
float standardGeometryVals[20] = {
-1.0, 1.0, 0.0, 0.0, 1.0,
-1.0, -1.0, 0.0, 0.0, 0.0,
1.0, 1.0, 0.0, 1.0, 1.0,
1.0, -1.0, 0.0, 1.0, 0.0
};
std::string standardVertex = R"V0G0N(
attribute vec3 position;
attribute vec2 textureCoord;
varying highp vec2 vTextureCoord;
void main() {
gl_Position = vec4(position, 1.0);
vTextureCoord = textureCoord;
}
)V0G0N";
// TODO, accept existing context as parameter
GPGPU (int h, int w) {
height = h;
width = w;
// Context configurations
EmscriptenWebGLContextAttributes attrs;
attrs.explicitSwapControl = 0;
attrs.depth = 1;
attrs.stencil = 1;
attrs.antialias = 1;
attrs.majorVersion = 2;
attrs.minorVersion = 0;
char id[10] = "theCanvas";
gl = emscripten_webgl_create_context(id, &attrs);
emscripten_webgl_make_context_current(gl);
// Check for the OES_texture_float extension support
std::string availableExtensions = std::string((char*)glGetString(GL_EXTENSIONS));
if (availableExtensions.find("OES_texture_float") == std::string::npos) {
printf("Floating point textures not supported\n");
}
};
~GPGPU() {
emscripten_webgl_destroy_context(gl);
};
void makeTexture (void) {
makeTexture({});
}
void makeTexture (float* buffer) {
makeTexture(buffer, width, height);
}
void makeTexture (float* buffer, int w, int h) {
GLuint texId;
glGenTextures(1, &texId);
glBindTexture(GL_TEXTURE_2D, texId);
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA32F, w, h, 0, GL_RGBA, GL_FLOAT, buffer);
Tex tex = Tex(texId, w, h);
textures.push_back(tex);
}
void updateTexture (float* buffer, int textureIndex=0) {
Tex tex = textures[textureIndex];
glBindTexture(GL_TEXTURE_2D, tex.texId);
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA32F, tex.width, tex.height, 0, GL_RGBA, GL_FLOAT, buffer);
}
void makeFrameBuffer (void) {
makeFrameBuffer(width, height);
}
void makeFrameBuffer (int w, int h) {
GLuint texId;
glGenTextures(1, &texId);
glBindTexture(GL_TEXTURE_2D, texId);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, w, h, 0, GL_RGBA, GL_FLOAT, 0);
glBindTexture(GL_TEXTURE_2D, 0);
GLuint fbId;
glGenFramebuffers(1, &fbId);
glBindFramebuffer(GL_FRAMEBUFFER, fbId);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texId, 0);
int status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (status != GL_FRAMEBUFFER_COMPLETE) {
switch (status) {
case GL_FRAMEBUFFER_UNSUPPORTED:
printf("FRAMEBUFFER_UNSUPPORTED\n");
break;
case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
printf("GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT\n");
break;
case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS:
printf("GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS\n");
break;
case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
printf("GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT\n");
break;
default:
printf("Framebuffer error: %d\n", status);
}
}
}
void buildProgram (std::string &fragmentSource) {
buildProgram(fragmentSource, standardVertex);
}
void buildProgram (std::string &fragmentSource, std::string &vertexSource) {
program = glCreateProgram();
GLuint vertex = compileShader(&vertexSource, GL_VERTEX_SHADER);
GLuint fragment = compileShader(&fragmentSource, GL_FRAGMENT_SHADER);
glAttachShader(program, vertex);
glAttachShader(program, fragment);
glLinkProgram(program);
glDeleteShader(vertex);
glDeleteShader(fragment);
glUseProgram(program);
GLuint standardGeometry;
glGenBuffers(1, &standardGeometry);
glBindBuffer(GL_ARRAY_BUFFER, standardGeometry);
glBufferData(GL_ARRAY_BUFFER, 20*4, standardGeometryVals, GL_STATIC_DRAW);
}
GLuint compileShader (std::string *source, GLenum type) {
const GLchar* sourceString[1];
GLint sourceStringLengths[1];
sourceString[0] = source->c_str();
sourceStringLengths[0] = source->length();
GLuint shader = glCreateShader(type);
glShaderSource(shader, 1, sourceString, sourceStringLengths);
glCompileShader(shader);
// Check if there were errors
int infoLen = 0;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
if (infoLen > 1) {
char infoLog[infoLen];
// And print them out
glGetShaderInfoLog(shader, infoLen, NULL, infoLog);
printf("%s\n", infoLog);
}
return shader;
}
void addAttrib (std::string name, int numElements=3, int stride=20, int offset=0) {
glUseProgram(program);
int loc = glGetAttribLocation(program, name.c_str());
attribs[name] = loc;
glEnableVertexAttribArray(loc);
glVertexAttribPointer(loc, numElements, GL_FLOAT, GL_FALSE, stride, (GLvoid*)offset);
}
template<class T>
void addUniform (std::string name, T value=0, std::string type="uniform1f") {
int loc = glGetUniformLocation(program, name.c_str());
uniforms[name] = loc;
if (type=="uniform1f") {
glUniform1f(loc, value);
} else if (type=="uniform1i") {
glUniform1i(loc, value);
}
}
void draw (int textureIndex=-1) {
Tex texture = textures[textures.size()-1];
if (textureIndex!=-1) {
texture = textures[textureIndex];
}
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture.texId);
if (!ready) {
ready = true;
addUniform("texture0", 0, "uniform1i");
int texToAdd = textureIndex==textures.size()-1 ? textures.size()-1 : textures.size();
for (int t=0; t<texToAdd; t++) {
glActiveTexture(GL_TEXTURE0+t+1);
glBindTexture(GL_TEXTURE_2D, textures[t].texId);
addUniform("texture"+std::to_string(t+1), (int)t+1, "uniform1i");
}
glActiveTexture(GL_TEXTURE0);
GLenum error = glGetError();
while (error != GL_NO_ERROR) {
switch (error) {
case GL_INVALID_ENUM:
printf("GL_INVALID_ENUM\n");
break;
case GL_INVALID_VALUE:
printf("GL_INVALID_VALUE\n");
break;
case GL_INVALID_OPERATION:
printf("GL_INVALID_OPERATION\n");
break;
case GL_INVALID_FRAMEBUFFER_OPERATION:
printf("GL_INVALID_FRAMEBUFFER_OPERATION\n");
break;
case GL_OUT_OF_MEMORY:
printf("GL_OUT_OF_MEMORY\n");
break;
}
error = glGetError();
}
}
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
float* getPixels (int startX=0, int startY=0, int spanX=-1, int spanY=-1) {
if (spanX==-1) spanX = width-startX;
if (spanY==-1) spanY = width-startY;
float* buffer = (float*) malloc(spanX * spanY * 4 * sizeof(float));
glReadPixels(startX, startY, spanX, spanY, GL_RGBA, GL_FLOAT, buffer);
return buffer;
}
void deleteGL () {
emscripten_webgl_make_context_current(gl);
glDeleteProgram(program);
textures.clear();
}
};