src/platform/opengl/gles2.c (view raw)
1/* Copyright (c) 2013-2015 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include "gles2.h"
7
8#include "gba/video.h"
9#include "util/configuration.h"
10#include "util/vfs.h"
11
12#define MAX_PASSES 8
13
14static const char* const _vertexShader =
15 "attribute vec4 position;\n"
16 "varying vec2 texCoord;\n"
17
18 "void main() {\n"
19 " gl_Position = position;\n"
20 " texCoord = (position.st + vec2(1.0, -1.0)) * vec2(0.5, -0.5);\n"
21 "}";
22
23static const char* const _nullVertexShader =
24 "attribute vec4 position;\n"
25 "varying vec2 texCoord;\n"
26
27 "void main() {\n"
28 " gl_Position = position;\n"
29 " texCoord = (position.st + vec2(1.0, 1.0)) * vec2(0.5, 0.5);\n"
30 "}";
31
32static const char* const _fragmentShader =
33 "varying vec2 texCoord;\n"
34 "uniform sampler2D tex;\n"
35 "uniform float gamma;\n"
36 "uniform vec3 scale;\n"
37 "uniform vec3 bias;\n"
38
39 "void main() {\n"
40 " vec4 color = texture2D(tex, texCoord);\n"
41 " color.a = 1.;\n"
42 " color.rgb = scale * pow(color.rgb, vec3(gamma, gamma, gamma)) + bias;\n"
43 " gl_FragColor = color;\n"
44 "}";
45
46static const char* const _nullFragmentShader =
47 "varying vec2 texCoord;\n"
48 "uniform sampler2D tex;\n"
49
50 "void main() {\n"
51 " vec4 color = texture2D(tex, texCoord);\n"
52 " color.a = 1.;\n"
53 " gl_FragColor = color;\n"
54 "}";
55
56static const GLfloat _vertices[] = {
57 -1.f, -1.f,
58 -1.f, 1.f,
59 1.f, 1.f,
60 1.f, -1.f,
61};
62
63
64static void GBAGLES2ContextInit(struct VideoBackend* v, WHandle handle) {
65 UNUSED(handle);
66 struct GBAGLES2Context* context = (struct GBAGLES2Context*) v;
67 glGenTextures(1, &context->tex);
68 glBindTexture(GL_TEXTURE_2D, context->tex);
69 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
70 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
71
72#ifdef COLOR_16_BIT
73#ifdef COLOR_5_6_5
74 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, 0);
75#else
76 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, 0, GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV, 0);
77#endif
78#else
79 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
80#endif
81
82 glClearColor(0.f, 0.f, 0.f, 1.f);
83
84 struct GBAGLES2Uniform* uniforms = malloc(sizeof(struct GBAGLES2Uniform) * 3);
85 uniforms[0].name = "gamma";
86 uniforms[0].type = GL_FLOAT;
87 uniforms[0].value.f = 1.0f;
88 uniforms[1].name = "scale";
89 uniforms[1].type = GL_FLOAT_VEC3;
90 uniforms[1].value.fvec3[0] = 1.0f;
91 uniforms[1].value.fvec3[1] = 1.0f;
92 uniforms[1].value.fvec3[2] = 1.0f;
93 uniforms[2].name = "bias";
94 uniforms[2].type = GL_FLOAT_VEC3;
95 uniforms[2].value.fvec3[0] = 0.0f;
96 uniforms[2].value.fvec3[1] = 0.0f;
97 uniforms[2].value.fvec3[2] = 0.0f;
98 GBAGLES2ShaderInit(&context->initialShader, _vertexShader, _fragmentShader, -1, -1, uniforms, 3);
99 GBAGLES2ShaderInit(&context->finalShader, 0, 0, 0, 0, 0, 0);
100 glDeleteFramebuffers(1, &context->finalShader.fbo);
101 context->finalShader.fbo = 0;
102}
103
104static void GBAGLES2ContextDeinit(struct VideoBackend* v) {
105 struct GBAGLES2Context* context = (struct GBAGLES2Context*) v;
106 glDeleteTextures(1, &context->tex);
107 GBAGLES2ShaderDeinit(&context->initialShader);
108 GBAGLES2ShaderDeinit(&context->finalShader);
109 free(context->initialShader.uniforms);
110}
111
112static void GBAGLES2ContextResized(struct VideoBackend* v, int w, int h) {
113 int drawW = w;
114 int drawH = h;
115 if (v->lockAspectRatio) {
116 if (w * 2 > h * 3) {
117 drawW = h * 3 / 2;
118 } else if (w * 2 < h * 3) {
119 drawH = w * 2 / 3;
120 }
121 }
122 glViewport(0, 0, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS);
123 glClearColor(0.f, 0.f, 0.f, 1.f);
124 glClear(GL_COLOR_BUFFER_BIT);
125 glViewport((w - drawW) / 2, (h - drawH) / 2, drawW, drawH);
126}
127
128static void GBAGLES2ContextClear(struct VideoBackend* v) {
129 UNUSED(v);
130 glClearColor(0.f, 0.f, 0.f, 1.f);
131 glClear(GL_COLOR_BUFFER_BIT);
132}
133
134void _drawShader(struct GBAGLES2Shader* shader) {
135 GLint viewport[4];
136 glBindFramebuffer(GL_FRAMEBUFFER, shader->fbo);
137 if (shader->blend) {
138 glEnable(GL_BLEND);
139 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
140 } else {
141 glDisable(GL_BLEND);
142 }
143
144 glGetIntegerv(GL_VIEWPORT, viewport);
145 int drawW = shader->width;
146 int drawH = shader->height;
147 int padW = 0;
148 int padH = 0;
149 if (!shader->width) {
150 drawW = viewport[2];
151 padW = viewport[0];
152 }
153 if (!shader->height) {
154 drawH = viewport[3];
155 padH = viewport[1];
156 }
157 glViewport(padW, padH, drawW, drawH);
158 if (!shader->width || !shader->height) {
159 GLint oldTex;
160 glGetIntegerv(GL_TEXTURE_BINDING_2D, &oldTex);
161 glBindTexture(GL_TEXTURE_2D, shader->tex);
162 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, drawW, drawH, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
163 glBindTexture(GL_TEXTURE_2D, oldTex);
164 }
165
166 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, shader->filter ? GL_LINEAR : GL_NEAREST);
167 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, shader->filter ? GL_LINEAR : GL_NEAREST);
168 glUseProgram(shader->program);
169 glUniform1i(shader->texLocation, 0);
170 glVertexAttribPointer(shader->positionLocation, 2, GL_FLOAT, GL_FALSE, 0, _vertices);
171 glEnableVertexAttribArray(shader->positionLocation);
172 size_t u;
173 for (u = 0; u < shader->nUniforms; ++u) {
174 struct GBAGLES2Uniform* uniform = &shader->uniforms[u];
175 switch (uniform->type) {
176 case GL_FLOAT:
177 glUniform1f(uniform->location, uniform->value.f);
178 break;
179 case GL_INT:
180 glUniform1i(uniform->location, uniform->value.i);
181 break;
182 case GL_UNSIGNED_INT:
183 glUniform1ui(uniform->location, uniform->value.ui);
184 break;
185 case GL_BOOL:
186 glUniform1i(uniform->location, uniform->value.b);
187 break;
188 case GL_FLOAT_VEC2:
189 glUniform2fv(uniform->location, 1, uniform->value.fvec2);
190 break;
191 case GL_FLOAT_VEC3:
192 glUniform3fv(uniform->location, 1, uniform->value.fvec3);
193 break;
194 case GL_FLOAT_VEC4:
195 glUniform4fv(uniform->location, 1, uniform->value.fvec4);
196 break;
197 case GL_INT_VEC2:
198 glUniform2iv(uniform->location, 1, uniform->value.ivec2);
199 break;
200 case GL_INT_VEC3:
201 glUniform3iv(uniform->location, 1, uniform->value.ivec3);
202 break;
203 case GL_INT_VEC4:
204 glUniform4iv(uniform->location, 1, uniform->value.ivec4);
205 break;
206 case GL_BOOL_VEC2:
207 glUniform2i(uniform->location, uniform->value.bvec2[0], uniform->value.bvec2[1]);
208 break;
209 case GL_BOOL_VEC3:
210 glUniform3i(uniform->location, uniform->value.bvec3[0], uniform->value.bvec3[1], uniform->value.bvec3[2]);
211 break;
212 case GL_BOOL_VEC4:
213 glUniform4i(uniform->location, uniform->value.bvec4[0], uniform->value.bvec4[1], uniform->value.bvec4[2], uniform->value.bvec4[3]);
214 break;
215 case GL_FLOAT_MAT2:
216 glUniformMatrix2fv(uniform->location, 1, GL_FALSE, uniform->value.fmat2x2);
217 break;
218 case GL_FLOAT_MAT3:
219 glUniformMatrix3fv(uniform->location, 1, GL_FALSE, uniform->value.fmat3x3);
220 break;
221 case GL_FLOAT_MAT4:
222 glUniformMatrix4fv(uniform->location, 1, GL_FALSE, uniform->value.fmat4x4);
223 break;
224 }
225 }
226 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
227 glBindFramebuffer(GL_FRAMEBUFFER, 0);
228 glBindTexture(GL_TEXTURE_2D, shader->tex);
229 glViewport(viewport[0], viewport[1], viewport[2], viewport[3]);
230}
231
232void GBAGLES2ContextDrawFrame(struct VideoBackend* v) {
233 struct GBAGLES2Context* context = (struct GBAGLES2Context*) v;
234 glActiveTexture(GL_TEXTURE0);
235 glBindTexture(GL_TEXTURE_2D, context->tex);
236
237 context->finalShader.filter = v->filter;
238 _drawShader(&context->initialShader);
239 size_t n;
240 for (n = 0; n < context->nShaders; ++n) {
241 _drawShader(&context->shaders[n]);
242 }
243 _drawShader(&context->finalShader);
244 glUseProgram(0);
245}
246
247void GBAGLES2ContextPostFrame(struct VideoBackend* v, const void* frame) {
248 struct GBAGLES2Context* context = (struct GBAGLES2Context*) v;
249 glBindTexture(GL_TEXTURE_2D, context->tex);
250 glPixelStorei(GL_UNPACK_ROW_LENGTH, 256);
251#ifdef COLOR_16_BIT
252#ifdef COLOR_5_6_5
253 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, frame);
254#else
255 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, 0, GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV, frame);
256#endif
257#else
258 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, 0, GL_RGBA, GL_UNSIGNED_BYTE, frame);
259#endif
260 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
261}
262
263void GBAGLES2ContextCreate(struct GBAGLES2Context* context) {
264 context->d.init = GBAGLES2ContextInit;
265 context->d.deinit = GBAGLES2ContextDeinit;
266 context->d.resized = GBAGLES2ContextResized;
267 context->d.swap = 0;
268 context->d.clear = GBAGLES2ContextClear;
269 context->d.postFrame = GBAGLES2ContextPostFrame;
270 context->d.drawFrame = GBAGLES2ContextDrawFrame;
271 context->d.setMessage = 0;
272 context->d.clearMessage = 0;
273 context->shaders = 0;
274 context->nShaders = 0;
275}
276
277void GBAGLES2ShaderInit(struct GBAGLES2Shader* shader, const char* vs, const char* fs, int width, int height, struct GBAGLES2Uniform* uniforms, size_t nUniforms) {
278 shader->width = width >= 0 ? width : VIDEO_HORIZONTAL_PIXELS;
279 shader->height = height >= 0 ? height : VIDEO_VERTICAL_PIXELS;
280 shader->filter = false;
281 shader->blend = false;
282 shader->uniforms = uniforms;
283 shader->nUniforms = nUniforms;
284 glGenFramebuffers(1, &shader->fbo);
285 glBindFramebuffer(GL_FRAMEBUFFER, shader->fbo);
286
287 glGenTextures(1, &shader->tex);
288 glBindTexture(GL_TEXTURE_2D, shader->tex);
289 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
290 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
291 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
292 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
293 if (shader->width && shader->height) {
294 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, shader->width, shader->height, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
295 }
296
297 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, shader->tex, 0);
298 shader->program = glCreateProgram();
299 shader->vertexShader = glCreateShader(GL_VERTEX_SHADER);
300 shader->fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
301 if (vs) {
302 glShaderSource(shader->vertexShader, 1, (const GLchar**) &vs, 0);
303 } else {
304 glShaderSource(shader->vertexShader, 1, (const GLchar**) &_nullVertexShader, 0);
305 }
306 if (fs) {
307 glShaderSource(shader->fragmentShader, 1, (const GLchar**) &fs, 0);
308 } else {
309 glShaderSource(shader->fragmentShader, 1, (const GLchar**) &_nullFragmentShader, 0);
310 }
311 glAttachShader(shader->program, shader->vertexShader);
312 glAttachShader(shader->program, shader->fragmentShader);
313 char log[1024];
314 glCompileShader(shader->fragmentShader);
315 glGetShaderInfoLog(shader->fragmentShader, 1024, 0, log);
316 printf("%s\n", log);
317 glCompileShader(shader->vertexShader);
318 glGetShaderInfoLog(shader->vertexShader, 1024, 0, log);
319 printf("%s\n", log);
320 glLinkProgram(shader->program);
321 glGetProgramInfoLog(shader->program, 1024, 0, log);
322 printf("%s\n", log);
323
324 shader->texLocation = glGetUniformLocation(shader->program, "tex");
325 shader->positionLocation = glGetAttribLocation(shader->program, "position");
326 size_t i;
327 for (i = 0; i < shader->nUniforms; ++i) {
328 shader->uniforms[i].location = glGetUniformLocation(shader->program, shader->uniforms[i].name);
329 }
330 glBindFramebuffer(GL_FRAMEBUFFER, 0);
331}
332
333void GBAGLES2ShaderDeinit(struct GBAGLES2Shader* shader) {
334 glDeleteTextures(1, &shader->tex);
335 glDeleteShader(shader->fragmentShader);
336 glDeleteProgram(shader->program);
337 glDeleteFramebuffers(1, &shader->fbo);
338}
339
340void GBAGLES2ShaderAttach(struct GBAGLES2Context* context, struct GBAGLES2Shader* shaders, size_t nShaders) {
341 if (context->shaders) {
342 if (context->shaders == shaders && context->nShaders == nShaders) {
343 return;
344 }
345 GBAGLES2ShaderDetach(context);
346 }
347 context->shaders = shaders;
348 context->nShaders = nShaders;
349}
350
351void GBAGLES2ShaderDetach(struct GBAGLES2Context* context) {
352 if (!context->shaders) {
353 return;
354 }
355 context->shaders = 0;
356}
357
358static bool _lookupIntValue(const struct Configuration* config, const char* section, const char* key, int* out) {
359 const char* charValue = ConfigurationGetValue(config, section, key);
360 if (!charValue) {
361 return false;
362 }
363 char* end;
364 unsigned long value = strtol(charValue, &end, 10);
365 if (*end) {
366 return false;
367 }
368 *out = value;
369 return true;
370}
371
372bool GBAGLES2ShaderLoad(struct GBAGLES2Shader** shaders, size_t* nShaders, struct GBAGLES2ShaderMetadata* metadata, struct VDir* dir) {
373 struct VFile* manifest = dir->openFile(dir, "manifest.ini", O_RDONLY);
374 if (!manifest) {
375 return false;
376 }
377 bool success = false;
378 struct Configuration description;
379 ConfigurationInit(&description);
380 if (ConfigurationReadVFile(&description, manifest)) {
381 int inShaders;
382 success = _lookupIntValue(&description, "shader", "passes", &inShaders);
383 if (inShaders > MAX_PASSES || inShaders < 1) {
384 success = false;
385 }
386 if (success) {
387 if (metadata) {
388 metadata->name = ConfigurationGetValue(&description, "shader", "name");
389 if (metadata->name) {
390 metadata->name = strdup(metadata->name);
391 }
392 metadata->author = ConfigurationGetValue(&description, "shader", "author");
393 if (metadata->author) {
394 metadata->author = strdup(metadata->author);
395 }
396 metadata->description = ConfigurationGetValue(&description, "shader", "description");
397 if (metadata->description) {
398 metadata->description = strdup(metadata->description);
399 }
400 }
401 struct GBAGLES2Shader* shaderBlock = malloc(sizeof(struct GBAGLES2Shader) * inShaders);
402 int n;
403 for (n = 0; n < inShaders; ++n) {
404 char passName[12];
405 snprintf(passName, sizeof(passName), "pass.%u", n);
406 const char* fs = ConfigurationGetValue(&description, passName, "fragmentShader");
407 const char* vs = ConfigurationGetValue(&description, passName, "vertexShader");
408 if (fs && (fs[0] == '.' || strstr(fs, PATH_SEP))) {
409 success = false;
410 break;
411 }
412 if (vs && (vs[0] == '.' || strstr(vs, PATH_SEP))) {
413 success = false;
414 break;
415 }
416 char* fssrc = 0;
417 char* vssrc = 0;
418 if (fs) {
419 struct VFile* fsf = dir->openFile(dir, fs, O_RDONLY);
420 if (!fsf) {
421 success = false;
422 break;
423 }
424 fssrc = malloc(fsf->size(fsf));
425 fsf->read(fsf, fssrc, fsf->size(fsf));
426 fsf->close(fsf);
427 }
428 if (vs) {
429 struct VFile* vsf = dir->openFile(dir, vs, O_RDONLY);
430 if (!vsf) {
431 success = false;
432 free(fssrc);
433 break;
434 }
435 vssrc = malloc(vsf->size(vsf));
436 vsf->read(vsf, vssrc, vsf->size(vsf));
437 vsf->close(vsf);
438 }
439 int width = 0;
440 int height = 0;
441 _lookupIntValue(&description, passName, "width", &width);
442 _lookupIntValue(&description, passName, "height", &height);
443 GBAGLES2ShaderInit(&shaderBlock[n], vssrc, fssrc, width, height, 0, 0);
444 int b = 0;
445 _lookupIntValue(&description, passName, "blend", &b);
446 if (b) {
447 shaderBlock[n].blend = b;
448 }
449 b = 0;
450 _lookupIntValue(&description, passName, "filter", &b);
451 if (b) {
452 shaderBlock[n].filter = b;
453 }
454 free(fssrc);
455 free(vssrc);
456 }
457 if (success) {
458 *nShaders = inShaders;
459 *shaders = shaderBlock;
460 } else {
461 inShaders = n;
462 for (n = 0; n < inShaders; ++n) {
463 GBAGLES2ShaderDeinit(&shaderBlock[n]);
464 }
465 }
466 }
467 }
468 ConfigurationDeinit(&description);
469 return success;
470}
471
472void GBAGLES2ShaderFree(struct GBAGLES2Shader* shaders, size_t nShaders) {
473 size_t n;
474 for (n = 0; n < nShaders; ++n) {
475 GBAGLES2ShaderDeinit(&shaders[n]);
476 size_t u;
477 for (u = 0; u < shaders[n].nUniforms; ++u) {
478 free((void*) shaders[n].uniforms[u].name);
479 }
480 }
481 free(shaders);
482}