-
Notifications
You must be signed in to change notification settings - Fork 2
/
oil-backend-opengl.c
380 lines (319 loc) · 12 KB
/
oil-backend-opengl.c
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
#ifdef ENABLE_OPENGL_BACKEND
#include "oil.h"
#include "oil-image-private.h"
#include "oil-backend-private.h"
#include <stdio.h>
#include <stddef.h>
#include <string.h>
#include <X11/X.h>
#include <X11/Xlib.h>
#include <GL/glew.h>
#include <GL/gl.h>
#include <GL/glx.h>
static const char *vshadersrc = " \
void main() { \
gl_Position = gl_ModelViewMatrix * gl_Vertex; \
gl_FrontColor = gl_Color; \
gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; \
} \
";
static const char *fshadersrc = " \
uniform sampler2D tex; \
void main() { \
gl_FragColor = gl_Color * texture2D(tex, gl_TexCoord[0]); \
} \
";
typedef struct {
GLuint framebuffer;
GLuint depthbuffer;
GLuint colorbuffer;
int dirty_mipmaps;
} OpenGLPriv;
static OILMatrix modelview = {{{1.0, 0.0, 0.0, 0.0}, {0.0, 1.0, 0.0, 0.0}, {0.0, 0.0, 1.0, 0.0}, {0.0, 0.0, 0.0, 1.0}}};
static GLuint framebuffer = 0;
static GLuint colorbuffer = 0;
static int oil_backend_opengl_initialize(void) {
static int initialized = 0;
static int initialization_result = 0;
Display *dpy;
Window root;
GLint attr[] = { GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None };
XVisualInfo *vi;
GLXContext glc;
int tmp;
GLuint vshader, fshader;
GLuint shader;
char infolog[1024];
/* only run this once, ever */
if (initialized)
return initialization_result;
initialized = 1;
/* open display */
if (!(dpy = XOpenDisplay(NULL))) {
return 0;
}
/* get root window */
root = DefaultRootWindow(dpy);
/* get visual matching attr */
if (!(vi = glXChooseVisual(dpy, 0, attr))) {
return 0;
}
/* create a context */
if (!(glc = glXCreateContext(dpy, vi, NULL, GL_TRUE))) {
return 0;
}
glXMakeCurrent(dpy, root, glc);
if (glewInit() != GLEW_OK) {
return 0;
}
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glFrontFace(GL_CW);
glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GREATER, 0.0f);
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
/* set up the texture matrix to handle our flipped images */
glMatrixMode(GL_TEXTURE);
glScalef(1.0, -1.0, 1.0);
glTranslatef(0.0, 1.0, 0.0);
glMatrixMode(GL_MODELVIEW);
vshader = glCreateShader(GL_VERTEX_SHADER);
tmp = strlen(vshadersrc);
glShaderSource(vshader, 1, &vshadersrc, &tmp);
glCompileShader(vshader);
glGetShaderiv(vshader, GL_COMPILE_STATUS, &tmp);
if (tmp == GL_FALSE) {
glGetShaderInfoLog(vshader, 1024, &tmp, infolog);
infolog[tmp] = 0;
printf("%s", infolog);
return 0;
}
fshader = glCreateShader(GL_FRAGMENT_SHADER);
tmp = strlen(fshadersrc);
glShaderSource(fshader, 1, &fshadersrc, &tmp);
glCompileShader(fshader);
glGetShaderiv(fshader, GL_COMPILE_STATUS, &tmp);
if (tmp == GL_FALSE) {
glGetShaderInfoLog(fshader, 1024, &tmp, infolog);
infolog[tmp] = 0;
printf("%s", infolog);
return 0;
}
shader = glCreateProgram();
glAttachShader(shader, vshader);
glAttachShader(shader, fshader);
glLinkProgram(shader);
glGetProgramiv(shader, GL_LINK_STATUS, &tmp);
if (tmp == GL_FALSE) {
glGetProgramInfoLog(shader, 1024, &tmp, infolog);
infolog[tmp] = 0;
printf("%s", infolog);
return 0;
}
glUseProgram(shader);
/* printf("vendor: %s\n", (const char*)glGetString(GL_VENDOR)); */
initialization_result = 1;
return 1;
}
static void oil_backend_opengl_new(OILImage *im) {
/* add our data struct. FIXME fail if any of these are NULL */
OpenGLPriv *priv;
priv = im->backend_data = malloc(sizeof(OpenGLPriv));
glGenFramebuffers(1, &(priv->framebuffer));
glGenRenderbuffers(1, &(priv->depthbuffer));
glGenTextures(1, &(priv->colorbuffer));
priv->dirty_mipmaps = 1;
/* set up the color buffer */
glBindTexture(GL_TEXTURE_2D, priv->colorbuffer);
colorbuffer = priv->colorbuffer;
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, im->width, im->height, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, NULL);
/* set up our depth buffer */
glBindRenderbuffer(GL_RENDERBUFFER, priv->depthbuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, im->width, im->height);
glBindRenderbuffer(GL_RENDERBUFFER, 0);
/* bind everything to our framebuffer */
glBindFramebuffer(GL_FRAMEBUFFER, priv->framebuffer);
framebuffer = priv->framebuffer;
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, priv->colorbuffer, 0);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, priv->depthbuffer);
/* make sure we're good */
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
/* FIXME fail! */
printf("ack! failed to make framebuffer! %i %i %i (%i, %i) 0x%x\n", priv->framebuffer, priv->colorbuffer, priv->depthbuffer, im->width, im->height, glCheckFramebufferStatus(GL_FRAMEBUFFER));
}
/* clear the default data */
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
framebuffer = 0;
}
static void oil_backend_opengl_free(OILImage *im) {
/* free our data struct */
OpenGLPriv *priv = im->backend_data;
if (colorbuffer == priv->colorbuffer) {
glBindTexture(GL_TEXTURE_2D, 0);
colorbuffer = 0;
}
if (framebuffer == priv->framebuffer) {
glBindFramebuffer(GL_FRAMEBUFFER, 0);
framebuffer = 0;
}
glDeleteFramebuffers(1, &(priv->framebuffer));
glDeleteRenderbuffers(1, &(priv->depthbuffer));
glDeleteTextures(1, &(priv->colorbuffer));
free(priv);
}
static inline void load_matrix(OILMatrix *matrix) {
if (memcmp(matrix, &modelview, sizeof(OILMatrix)) != 0) {
OILMatrix fullmat;
oil_matrix_set_identity(&fullmat);
oil_matrix_scale(&fullmat, 1.0f, -1.0f, -1.0f);
oil_matrix_multiply(&fullmat, &fullmat, matrix);
glLoadTransposeMatrixf((GLfloat *)fullmat.data);
oil_matrix_copy(&modelview, matrix);
}
}
static inline void bind_framebuffer(OILImage *im) {
if (im) {
OpenGLPriv *priv = im->backend_data;
if (framebuffer != priv->framebuffer) {
glBindFramebuffer(GL_FRAMEBUFFER, priv->framebuffer);
glViewport(0, 0, im->width, im->height);
framebuffer = priv->framebuffer;
}
} else {
if (framebuffer != 0) {
glBindFramebuffer(GL_FRAMEBUFFER, 0);
framebuffer = 0;
}
}
}
static inline void bind_colorbuffer(OILImage *im) {
if (im) {
OpenGLPriv *priv = im->backend_data;
if (colorbuffer != priv->colorbuffer) {
glBindTexture(GL_TEXTURE_2D, priv->colorbuffer);
colorbuffer = priv->colorbuffer;
}
} else {
if (colorbuffer != 0) {
glBindTexture(GL_TEXTURE_2D, 0);
colorbuffer = 0;
}
}
}
static inline void mark_dirty(OILImage *im) {
if (im) {
OpenGLPriv *priv = im->backend_data;
priv->dirty_mipmaps = 1;
}
}
static void oil_backend_opengl_load(OILImage *im) {
bind_framebuffer(im);
glReadPixels(0, 0, im->width, im->height, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, im->data);
}
static void oil_backend_opengl_save(OILImage *im) {
bind_colorbuffer(im);
mark_dirty(im);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, im->width, im->height, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, im->data);
}
static void oil_backend_opengl_draw_triangles(OILImage *im, OILMatrix *matrix, OILImage *tex, OILVertex *vertices, unsigned int vertices_length, unsigned int *indices, unsigned int indices_length, OILTriangleFlags flags);
static int oil_backend_opengl_composite(OILImage *im, OILImage *src, unsigned char alpha, int dx, int dy, unsigned int sx, unsigned int sy, unsigned int xsize, unsigned int ysize) {
OILVertex vertices[] = {
{0.0, 0.0, 0.0, ((float)sx) / src->width, ((float)src->height - sy) / src->height, {255, 255, 255, 255}},
{1.0, 0.0, 0.0, ((float)sx + xsize) / src->width, ((float)src->height - sy) / src->height, {255, 255, 255, 255}},
{1.0, 1.0, 0.0, ((float)sx + xsize) / src->width, ((float)src->height - sy - ysize) / src->height, {255, 255, 255, 255}},
{0.0, 1.0, 0.0, ((float)sx) / src->width, ((float)src->height - sy - ysize) / src->height, {255, 255, 255, 255}},
};
unsigned int indices[] = {
0, 2, 1,
0, 3, 2,
};
OILMatrix mat;
oil_matrix_set_identity(&mat);
oil_matrix_orthographic(&mat, 0, im->width, im->height, 0, -1.0, 1.0);
oil_matrix_translate(&mat, dx, dy, 0.0);
oil_matrix_scale(&mat, xsize, ysize, 1.0);
oil_backend_opengl_draw_triangles(im, &mat, src, vertices, 4, indices, 6, 0);
return 1;
}
static void oil_backend_opengl_draw_triangles(OILImage *im, OILMatrix *matrix, OILImage *tex, OILVertex *vertices, unsigned int vertices_length, unsigned int *indices, unsigned int indices_length, OILTriangleFlags flags) {
unsigned int i;
load_matrix(matrix);
bind_framebuffer(im);
bind_colorbuffer(tex);
mark_dirty(im);
/* if we need to, re-generate the tex mipmaps */
if (tex) {
OpenGLPriv *tex_priv = tex->backend_data;
if (tex_priv->dirty_mipmaps) {
/* tex colorbuffer is already bound */
glGenerateMipmap(GL_TEXTURE_2D);
tex_priv->dirty_mipmaps = 0;
}
}
/* set up any drawing flags */
if (!(flags & OIL_DEPTH_TEST)) {
glDisable(GL_DEPTH_TEST);
}
glBegin(GL_TRIANGLES);
for (i = 0; i < indices_length; i += 3) {
OILVertex v0 = vertices[indices[i]];
OILVertex v1 = vertices[indices[i + 1]];
OILVertex v2 = vertices[indices[i + 2]];
glTexCoord2f(v0.s, v0.t);
glColor4f(v0.color.r / 255.0, v0.color.g / 255.0, v0.color.b / 255.0, v0.color.a / 255.0);
glVertex3f(v0.x, v0.y, v0.z);
glTexCoord2f(v1.s, v1.t);
glColor4f(v1.color.r / 255.0, v1.color.g / 255.0, v1.color.b / 255.0, v1.color.a / 255.0);
glVertex3f(v1.x, v1.y, v1.z);
glTexCoord2f(v2.s, v2.t);
glColor4f(v2.color.r / 255.0, v2.color.g / 255.0, v2.color.b / 255.0, v2.color.a / 255.0);
glVertex3f(v2.x, v2.y, v2.z);
}
glEnd();
/* undo our drawing flags */
if (!(flags & OIL_DEPTH_TEST)) {
glEnable(GL_DEPTH_TEST);
}
}
static int oil_backend_opengl_resize_half(OILImage *im, OILImage *src) {
OILVertex vertices[] = {
{0.0, 0.0, 0.0, 0.0, 1.0, {255, 255, 255, 255}},
{1.0, 0.0, 0.0, 1.0, 1.0, {255, 255, 255, 255}},
{1.0, 1.0, 0.0, 1.0, 0.0, {255, 255, 255, 255}},
{0.0, 1.0, 0.0, 0.0, 0.0, {255, 255, 255, 255}},
};
unsigned int indices[] = {
0, 2, 1,
0, 3, 2,
};
OILMatrix mat;
oil_matrix_set_identity(&mat);
oil_matrix_orthographic(&mat, 0.0, 1.0, 1.0, 0.0, -1.0, 1.0);
oil_backend_opengl_draw_triangles(im, &mat, src, vertices, 4, indices, 6, 0);
return 1;
}
static void oil_backend_opengl_clear(OILImage *im) {
bind_framebuffer(im);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
OILBackend oil_backend_opengl = {
oil_backend_opengl_initialize,
oil_backend_opengl_new,
oil_backend_opengl_free,
oil_backend_opengl_load,
oil_backend_opengl_save,
oil_backend_opengl_composite,
oil_backend_opengl_draw_triangles,
oil_backend_opengl_resize_half,
oil_backend_opengl_clear,
};
#endif /* ENABLE_OPENGL_BACKEND */