-
Notifications
You must be signed in to change notification settings - Fork 11
/
gpgpu_gles3.h
276 lines (241 loc) · 7.01 KB
/
gpgpu_gles3.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
//---------------------------------------------------------
// Cat's eye
//
// ©2023 Yuichiro Nakada
//---------------------------------------------------------
#include <stdio.h>
#include <string.h>
#include <assert.h>
#ifndef GL_GLEXT_PROTOTYPES
#define GL_GLEXT_PROTOTYPES
#endif
#define GLFW_INCLUDE_GLU
#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>
#include <GLFW/glfw3.h>
#endif
#include <unistd.h>
#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)
const char* fragment_shader_source = "#version 320 es\nvoid main(){}";
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);
GPU_CHECK();
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);
}
}
free(src);
return shader;
}
GLuint gpu_compile(const char* src, const char *varyings[])
{
GLuint fragmentShader = gpu_load_shader(GL_FRAGMENT_SHADER, fragment_shader_source);
if (!fragmentShader) return 0;
GLuint vertexShader = gpu_load_shader(GL_VERTEX_SHADER, src);
if (!vertexShader) return 0;
GLuint program = glCreateProgram();
if (!program) {
printf("gpgpu_compile error!\n");
return 0;
}
glAttachShader(program, fragmentShader);
glAttachShader(program, vertexShader);
// for transform feedback output
glTransformFeedbackVaryings(program, 1, varyings, GL_INTERLEAVED_ATTRIBS);
glLinkProgram(program);
GLint linkStatus = GL_FALSE;
glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);
if (linkStatus != GL_TRUE) {
GLint bufLength = 0;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &bufLength);
if (bufLength) {
char* buf = (char*)malloc(bufLength);
if (buf) {
glGetProgramInfoLog(program, bufLength, NULL, buf);
printf("Could not link program:\n%s\n", buf);
free(buf);
exit(1);
}
}
glDeleteProgram(program);
program = 0;
}
// setup variables
glUseProgram(program);
return program;
}
GLuint gpu_make_texture(int w, int h, float *texels)
{
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, w, h, 0, GL_RGBA, GL_FLOAT, texels);
// clamp to edge to support non-power of two textures
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
// 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);
glBindTexture(GL_TEXTURE_2D, 0);
return texture;
}
#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_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);*/
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
GLFWwindow *window = glfwCreateWindow(320, 240, "Catgl", 0, 0);
if (!window) {
glfwTerminate();
assert(!"glfwCreateWindow error!");
}
glfwMakeContextCurrent(window);
glDisable(GL_DEPTH_TEST);
#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);
glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, value);
printf(" Max vertex texture image unit (VTF): %d\n", value[0]);
}
void gpu_term()
{
glfwTerminate();
}
#else
int32_t gpu_fd;
struct gbm_device *gpu_gbm;
EGLDisplay gpu_egl_dpy;
EGLContext gpu_core_ctx;
void gpu_init()
{
/*bool*/int res;
int32_t gpu_fd = open("/dev/dri/renderD128", O_RDWR);
assert(gpu_fd > 0);
gpu_gbm = gbm_create_device(gpu_fd);
assert(gpu_gbm != NULL);
/* setup EGL from the GBM device */
gpu_egl_dpy = eglGetPlatformDisplay(EGL_PLATFORM_GBM_MESA, gpu_gbm, NULL);
assert(gpu_egl_dpy != NULL);
res = eglInitialize(gpu_egl_dpy, NULL, NULL);
assert(res);
const char *egl_extension_st = eglQueryString(gpu_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(gpu_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
};
gpu_core_ctx = eglCreateContext(gpu_egl_dpy, cfg, EGL_NO_CONTEXT, attribs);
assert(gpu_core_ctx != EGL_NO_CONTEXT);
res = eglMakeCurrent(gpu_egl_dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, gpu_core_ctx);
assert(res);
}
void gpu_term()
{
eglDestroyContext(gpu_egl_dpy, gpu_core_ctx);
eglTerminate(gpu_egl_dpy);
gbm_device_destroy(gpu_gbm);
close(gpu_fd);
}
#endif