-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathgpgpu_gles.h
executable file
·470 lines (418 loc) · 12.8 KB
/
gpgpu_gles.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
//---------------------------------------------------------
// Cat's eye
//
// ©2017-2023 Yuichiro Nakada
//---------------------------------------------------------
#include <assert.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef GL_GLEXT_PROTOTYPES
#define GL_GLEXT_PROTOTYPES
#endif
#define GLFW_INCLUDE_GLU
#ifndef GLES_FLOAT
//#define GLES_FLOAT GL_HALF_FLOAT
#define GLES_FLOAT GL_FLOAT
#endif
#ifdef _MSC_VER
#define strdup _strdup
#endif
#ifdef _WIN32
#include <windows.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#elif __APPLE__
#include <OpenGL/gl3.h>
#include <GLFW/glfw3.h>
#elif __linux
#ifdef GPGPU_USE_GLES
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <GLES3/gl32.h>
#include <GLES3/gl3ext.h>
//#define GL_CLAMP_TO_BORDER GL_CLAMP_TO_BORDER_OES
#else
#include <GL/gl.h>
#define GLFW_INCLUDE_ES3
#include <GLFW/glfw3.h>
#endif
#include <unistd.h>
#else // for emscripten
#ifdef GPGPU_USE_GLES
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <GLES3/gl32.h>
#else
#include <GL/gl.h>
#define GLFW_INCLUDE_ES3
#include <GLFW/glfw3.h>
#endif
#endif
//#define DEBUG
#ifdef DEBUG
void gpu_check_error(int line)
{
GLenum err = glGetError();
if (err != GL_NO_ERROR) {
printf(__FILE__ ":%d glGetError returns %d\n", line, err);
exit(1);
}
}
#define GPU_CHECK() gpu_check_error(__LINE__);
#define debug(fmt, ... ) \
fprintf(stderr, \
"[%s] %s:%u # " fmt "\n", \
__DATE__, __FILE__, \
__LINE__, ##__VA_ARGS__ \
)
#else
#define GPU_CHECK()
#define debug( fmt, ... ) ((void)0)
#endif
#define _STRGF(x) # x
#define STRINGIFY(x) _STRGF(x)
// vertex
char pass_through[] = "#version 300 es\n" STRINGIFY(
precision highp float;
layout(location = 0) in vec4 pos;
layout(location = 1) in vec2 tex;
out vec2 uv;
void main()
{
gl_Position = vec4(pos);
uv = tex;
}\n
);
GLuint __vertexShader;
void gpu_set_vertices(GLuint prog)
{
glUseProgram(prog);
// bind vertices
GLuint vertexBuffer;
glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
float vertices[] = { // define a square that covers the screen
-1.0, -1.0, 0.0, // bottom left
1.0, -1.0, 0.0, // bottom right
1.0, 1.0, 0.0, // top right
-1.0, 1.0, 0.0 }; // top left
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), (void*)vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0/*pos*/, /*item size*/3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(0/*pos*/);
// bind texture cords
GLuint texCoords;
glGenBuffers(1, &texCoords);
glBindBuffer(GL_ARRAY_BUFFER, texCoords);
float textureCoords[] = {
0.0, 0.0,
1.0, 0.0,
1.0, 1.0,
0.0, 1.0 };
glBufferData(GL_ARRAY_BUFFER, sizeof(textureCoords), (void*)textureCoords, GL_STATIC_DRAW);
glVertexAttribPointer(1/*tex*/, /*item size*/2, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(1/*tex*/);
// index to vertices
GLuint indices;
glGenBuffers(1, &indices);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indices);
// tesselate square into triangles
// indeces into vertex array creating triangles, with counter-clockwise winding
unsigned short vIndices[] = {
0, 1, 2, // bottom right triangle
0, 2, 3 }; // top left triangle
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(vIndices), (void*)vIndices, GL_STATIC_DRAW);
GPU_CHECK();
}
GLuint gpu_load_shader(GLenum shaderType, const char* pSource)
{
char *src = strdup(pSource);
char *p = src;
while (*p) {
switch (*p++) {
case '{':
case ';':
if (*p==0x20) *p = '\n';
// if (*p==0x20 && *(p+1)!='i') *p = '\n';
}
}
GLuint shader = glCreateShader(shaderType);
glShaderSource(shader, 1, (const char**)&src, 0);
glCompileShader(shader);
GLint compiled;
glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
if (!compiled) {
int logSize, length;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &logSize);
if (logSize > 1) {
//GLchar infoLog[logSize];
GLchar infoLog[8192];
glGetShaderInfoLog(shader, logSize, &length, infoLog);
debug("Compile Error in %s\n%s\n", src, infoLog);
}
}
GPU_CHECK();
free(src);
return shader;
}
GLuint gpu_compile(const char* pFragmentSource)
{
#ifdef _WIN32
if (!glCreateProgram) printf("glCreateProgram is NULL!! (OpenGL ver:%s)\n", glGetString(GL_VERSION));
#endif
if (!__vertexShader) __vertexShader = gpu_load_shader(GL_VERTEX_SHADER, pass_through);
GLuint pixelShader = gpu_load_shader(GL_FRAGMENT_SHADER, pFragmentSource);
GLuint program = glCreateProgram();
glAttachShader(program, __vertexShader);
glAttachShader(program, pixelShader);
glDeleteShader(pixelShader);
glLinkProgram(program);
GPU_CHECK();
gpu_set_vertices(program);
return program;
}
// https://webgl2fundamentals.org/webgl/lessons/webgl-data-textures.html
#define GPGPU_TEX_PADDING 1
#define GPGPU_TEX_REPEAT 2
GLuint gpu_make_texture(int w, int h, void *texels, GLuint type/*, int flag*/)
{
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
GPU_CHECK();
switch (type) {
case GL_FLOAT:
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, w, h, 0, GL_RGBA, type, texels);
break;
case GL_INT:
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32I, w, h, 0, GL_RGBA_INTEGER, type, texels);
break;
}
/*#ifndef GPGPU_USE_GLES
glTexImage2D(GL_TEXTURE_2D, 0, (type==GLES_FLOAT ? GL_RGBA32F : GL_RGBA), w, h, 0, GL_RGBA, type, texels);
#else
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, type, texels);
#endif*/
// debug("gpu_make_texture: %d, %d\n", w, h);
GPU_CHECK();
#ifndef EMSCRIPTEN
/* GLuint clamp = GL_CLAMP_TO_EDGE;
if (flag & GPGPU_TEX_PADDING) clamp = GL_CLAMP_TO_BORDER; // 0 padding
if (flag & GPGPU_TEX_REPEAT) clamp = GL_REPEAT;
// clamp to edge to support non-power of two textures
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, clamp);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, clamp);*/
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
#endif
// don't interpolate when getting data from texture
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
// we're done with setup, so unbind current texture
glBindTexture(GL_TEXTURE_2D, 0);
GPU_CHECK();
return texture;
}
void gpu_set_input(GLuint program, GLuint texture, GLuint textureUnit, char *name)
{
glActiveTexture(textureUnit); // gl.TEXTURE0, gl.TEXTURE1, etc
glBindTexture(GL_TEXTURE_2D, texture);
GLuint sampler = glGetUniformLocation(program, name);
glUniform1i(sampler, textureUnit - GL_TEXTURE0);
GPU_CHECK();
assert(!glGetError());
}
GLuint gpu_set_output(int N, int M, GLuint texture)
{
glViewport(0, 0, N, M);
GLuint framebuffer;
glGenFramebuffers(1, &framebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, /*level*/0);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
debug("glGetError: %d", glGetError());
debug("glCheckFramebufferStatus: %d", glCheckFramebufferStatus(GL_FRAMEBUFFER));
assert(!"Bound framebuffer is not complete.");
}
GPU_CHECK();
return framebuffer;
}
void gpu_unset_input(GLuint texture)
{
glActiveTexture(texture);
glBindTexture(GL_TEXTURE_2D, 0);
GPU_CHECK();
}
void *gpu_read(int N, int M, void *d)
{
if (!d) d = malloc(4*M*N);
glReadPixels(0, 0, N, M, GL_RGBA, GL_UNSIGNED_BYTE, d);
GPU_CHECK();
return d; // M x N
}
float *gpu_readf(int N, int M, float *d)
{
/*#ifdef GPGPU_USE_GLFW
if (!d) d = malloc(sizeof(float)*4*M*N);
glReadPixels(0, 0, N, M, GL_RGBA32F, GLES_FLOAT, d);
#else*/
if (!d) d = malloc(sizeof(float)*4*M*N);
//glReadPixels(0, 0, N*4, M, GL_RGBA, GLES_FLOAT, d);
glReadPixels(0, 0, N, M, GL_RGBA, GLES_FLOAT, d);
//#endif
GPU_CHECK();
return d;
}
#define gpu_compute() {\
glDrawElements(GL_TRIANGLES, 3*2, GL_UNSIGNED_SHORT, 0);\
}
#define gpu_assert() {\
assert(!glGetError());\
}
#define gpu_param1i(prog, name, v1) {\
GLuint l = glGetUniformLocation(prog, name);\
glUniform1i(l, v1);\
}
#define gpu_param1f(prog, name, v1) {\
GLuint l = glGetUniformLocation(prog, name);\
glUniform1f(l, v1);\
}
#define gpu_param2f(prog, name, v1, v2) {\
GLuint l = glGetUniformLocation(prog, name);\
glUniform2f(l, v1, v2);\
}
#define gpu_param4f(prog, name, v1, v2, v3, v4) {\
GLuint l = glGetUniformLocation(prog, name);\
glUniform4f(l, v1, v2, v3, v4);\
}
#define gpu_param1fv(prog, name, count, v1) {\
GLuint l = glGetUniformLocation(prog, name);\
glUniform1fv(l, count, v1);\
}
#define gpu_param2fv(prog, name, count, v1) {\
GLuint l = glGetUniformLocation(prog, name);\
glUniform2fv(l, count, v1);\
}
#define gpu_param4fv(prog, name, count, v1) {\
GLuint l = glGetUniformLocation(prog, name);\
glUniform4fv(l, count, v1);\
}
#define gpu_paramMatrix3fv(prog, name, v) {\
GLuint l = glGetUniformLocation(prog, name);\
glUniformMatrix3fv(l, 1, GL_FALSE, v);\
}
#ifndef GPGPU_USE_GLES
#define gpu_transfer(texture, x, y, w, h, type, pix) {\
glBindTexture(GL_TEXTURE_2D, texture);\
glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, w, h, GL_RGBA, type, pix);\
assert(!glGetError());\
glBindTexture(GL_TEXTURE_2D, 0);\
}
#else
#define gpu_transfer(texture, x, y, w, h, type, pix) {\
glBindTexture(GL_TEXTURE_2D, texture);\
glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, w, h, GL_RGBA, type, pix);\
assert(!glGetError());\
glBindTexture(GL_TEXTURE_2D, 0);\
}
#endif
#ifndef GPGPU_USE_GLES
void gpu_init()
{
if (!glfwInit()) {
printf("Can't initialize GLFW.\n");
}
#ifdef _WIN32
//#include <GL/glut.h>
/* int argc = 1;
char argv[2][256] = {0};
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(320, 240);
glutCreateWindow(argv[0]);*/
#endif
// glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
glfwWindowHint(GLFW_VISIBLE, 0);
GLFWwindow *window = glfwCreateWindow(320, 240, "Catgl", 0, 0);
if (!window) {
glfwTerminate();
assert(!"glfwCreateWindow error!");
}
glfwMakeContextCurrent(window);
glDisable(GL_DEPTH_TEST);
glDisable(GL_DITHER);
#ifdef _WIN32
//#include <glad/glad.h>
// if (!gladLoadGL()) { exit(-1); }
#pragma comment(lib, "glew32.lib")
int r = glewInit();
if (r != GLEW_OK) {
printf("error at glewInit!! (%s)\n", glewGetErrorString(r));
}
#endif
printf("%s: OpenGL %s\n", glGetString(GL_RENDERER), glGetString(GL_VERSION));
int value[4] = { 0 };
// #define GL_TOTAL_PHYSICAL_MEMORY_ATI 0x87FE
// glGetIntegerv(GL_TOTAL_PHYSICAL_MEMORY_ATI, value);
//glGetIntegerv(GL_VBO_FREE_MEMORY_ATI, value); //GL_TEXTURE_FREE_MEMORY_ATI,GL_RENDERBUFFER_FREE_MEMORY_ATI
//printf(" Total available memory: %d MB\n", value[2]/1024);
glGetIntegerv(GL_GPU_MEMORY_INFO_TOTAL_AVAILABLE_MEMORY_NVX, value); //GL_GPU_MEMORY_INFO_DEDICATED_VIDMEM_NVX,GL_GPU_MEMORY_INFO_CURRENT_AVAILABLE_VIDMEM_NVX
printf(" Total available memory: %d MB\n", value[0]/1024);
int max_texture_width = 0;
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max_texture_width);
max_texture_width /= 8;
int max_texture_size = max_texture_width * max_texture_width;
printf(" Max texture size: %d (%d)\n", max_texture_size, max_texture_width*8);
}
void gpu_term()
{
glfwTerminate();
}
#else
int32_t __fd;
struct gbm_device *__gbm;
EGLDisplay __egl_dpy;
EGLContext __core_ctx;
void gpu_init()
{
/*bool*/int res;
int32_t __fd = open("/dev/dri/renderD128", O_RDWR);
assert(__fd > 0);
__gbm = gbm_create_device(__fd);
assert(__gbm != NULL);
/* setup EGL from the GBM device */
__egl_dpy = eglGetPlatformDisplay(EGL_PLATFORM_GBM_MESA, __gbm, NULL);
assert(__egl_dpy != NULL);
res = eglInitialize(__egl_dpy, NULL, NULL);
assert(res);
const char *egl_extension_st = eglQueryString(__egl_dpy, EGL_EXTENSIONS);
assert(strstr(egl_extension_st, "EGL_KHR_create_context") != NULL);
assert(strstr(egl_extension_st, "EGL_KHR_surfaceless_context") != NULL);
static const EGLint config_attribs[] = {
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES3_BIT,
EGL_NONE
};
EGLConfig cfg;
EGLint count;
res = eglChooseConfig(__egl_dpy, config_attribs, &cfg, 1, &count);
assert(res);
res = eglBindAPI(EGL_OPENGL_ES_API);
assert(res);
static const EGLint attribs[] = {
EGL_CONTEXT_CLIENT_VERSION, 3,
EGL_NONE
};
__core_ctx = eglCreateContext(__egl_dpy, cfg, EGL_NO_CONTEXT, attribs);
assert(__core_ctx != EGL_NO_CONTEXT);
res = eglMakeCurrent(__egl_dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, __core_ctx);
assert(res);
}
void gpu_term()
{
eglDestroyContext(__egl_dpy, __core_ctx);
eglTerminate(__egl_dpy);
gbm_device_destroy(__gbm);
close(__fd);
}
#endif