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/formatting.h"
11#include "util/vector.h"
12#include "util/vfs.h"
13
14#define MAX_PASSES 8
15
16static const GLchar* const _gles2Header =
17 "#version 100\n"
18 "precision mediump float;\n";
19
20static const GLchar* const _gl3Header =
21 "#version 120\n";
22
23static const char* const _vertexShader =
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 _nullVertexShader =
33 "attribute vec4 position;\n"
34 "varying vec2 texCoord;\n"
35
36 "void main() {\n"
37 " gl_Position = position;\n"
38 " texCoord = (position.st + vec2(1.0, 1.0)) * vec2(0.5, 0.5);\n"
39 "}";
40
41static const char* const _fragmentShader =
42 "varying vec2 texCoord;\n"
43 "uniform sampler2D tex;\n"
44 "uniform float gamma;\n"
45 "uniform vec3 desaturation;\n"
46 "uniform vec3 scale;\n"
47 "uniform vec3 bias;\n"
48
49 "void main() {\n"
50 " vec4 color = texture2D(tex, texCoord);\n"
51 " color.a = 1.;\n"
52 " float average = dot(color.rgb, vec3(1.)) / 3.;\n"
53 " color.rgb = vec3(average) * desaturation + color.rgb * (vec3(1.) - desaturation);\n"
54 " color.rgb = scale * pow(color.rgb, vec3(gamma, gamma, gamma)) + bias;\n"
55 " gl_FragColor = color;\n"
56 "}";
57
58static const char* const _nullFragmentShader =
59 "varying vec2 texCoord;\n"
60 "uniform sampler2D tex;\n"
61
62 "void main() {\n"
63 " vec4 color = texture2D(tex, texCoord);\n"
64 " color.a = 1.;\n"
65 " gl_FragColor = color;\n"
66 "}";
67
68static const GLfloat _vertices[] = {
69 -1.f, -1.f,
70 -1.f, 1.f,
71 1.f, 1.f,
72 1.f, -1.f,
73};
74
75static void GBAGLES2ContextInit(struct VideoBackend* v, WHandle handle) {
76 UNUSED(handle);
77 struct GBAGLES2Context* context = (struct GBAGLES2Context*) v;
78 glGenTextures(1, &context->tex);
79 glBindTexture(GL_TEXTURE_2D, context->tex);
80 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
81 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
82
83#ifdef COLOR_16_BIT
84#ifdef COLOR_5_6_5
85 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, 0);
86#else
87 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);
88#endif
89#else
90 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
91#endif
92
93 glClearColor(0.f, 0.f, 0.f, 1.f);
94
95 struct GBAGLES2Uniform* uniforms = malloc(sizeof(struct GBAGLES2Uniform) * 4);
96 uniforms[0].name = "gamma";
97 uniforms[0].readableName = "Gamma";
98 uniforms[0].type = GL_FLOAT;
99 uniforms[0].value.f = 1.0f;
100 uniforms[0].min.f = 0.1f;
101 uniforms[0].max.f = 3.0f;
102 uniforms[1].name = "scale";
103 uniforms[1].readableName = "Scale";
104 uniforms[1].type = GL_FLOAT_VEC3;
105 uniforms[1].value.fvec3[0] = 1.0f;
106 uniforms[1].value.fvec3[1] = 1.0f;
107 uniforms[1].value.fvec3[2] = 1.0f;
108 uniforms[1].min.fvec3[0] = -1.0f;
109 uniforms[1].min.fvec3[1] = -1.0f;
110 uniforms[1].min.fvec3[2] = -1.0f;
111 uniforms[1].max.fvec3[0] = 2.0f;
112 uniforms[1].max.fvec3[1] = 2.0f;
113 uniforms[1].max.fvec3[2] = 2.0f;
114 uniforms[2].name = "bias";
115 uniforms[2].readableName = "Bias";
116 uniforms[2].type = GL_FLOAT_VEC3;
117 uniforms[2].value.fvec3[0] = 0.0f;
118 uniforms[2].value.fvec3[1] = 0.0f;
119 uniforms[2].value.fvec3[2] = 0.0f;
120 uniforms[2].min.fvec3[0] = -1.0f;
121 uniforms[2].min.fvec3[1] = -1.0f;
122 uniforms[2].min.fvec3[2] = -1.0f;
123 uniforms[2].max.fvec3[0] = 1.0f;
124 uniforms[2].max.fvec3[1] = 1.0f;
125 uniforms[2].max.fvec3[2] = 1.0f;
126 uniforms[3].name = "desaturation";
127 uniforms[3].readableName = "Desaturation";
128 uniforms[3].type = GL_FLOAT_VEC3;
129 uniforms[3].value.fvec3[0] = 0.0f;
130 uniforms[3].value.fvec3[1] = 0.0f;
131 uniforms[3].value.fvec3[2] = 0.0f;
132 uniforms[3].min.fvec3[0] = 0.0f;
133 uniforms[3].min.fvec3[1] = 0.0f;
134 uniforms[3].min.fvec3[2] = 0.0f;
135 uniforms[3].max.fvec3[0] = 1.0f;
136 uniforms[3].max.fvec3[1] = 1.0f;
137 uniforms[3].max.fvec3[2] = 1.0f;
138 GBAGLES2ShaderInit(&context->initialShader, _vertexShader, _fragmentShader, -1, -1, uniforms, 4);
139 GBAGLES2ShaderInit(&context->finalShader, 0, 0, 0, 0, 0, 0);
140 glDeleteFramebuffers(1, &context->finalShader.fbo);
141 context->finalShader.fbo = 0;
142}
143
144static void GBAGLES2ContextDeinit(struct VideoBackend* v) {
145 struct GBAGLES2Context* context = (struct GBAGLES2Context*) v;
146 glDeleteTextures(1, &context->tex);
147 GBAGLES2ShaderDeinit(&context->initialShader);
148 GBAGLES2ShaderDeinit(&context->finalShader);
149 free(context->initialShader.uniforms);
150}
151
152static void GBAGLES2ContextResized(struct VideoBackend* v, int w, int h) {
153 int drawW = w;
154 int drawH = h;
155 if (v->lockAspectRatio) {
156 if (w * 2 > h * 3) {
157 drawW = h * 3 / 2;
158 } else if (w * 2 < h * 3) {
159 drawH = w * 2 / 3;
160 }
161 }
162 glViewport(0, 0, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS);
163 glClearColor(0.f, 0.f, 0.f, 1.f);
164 glClear(GL_COLOR_BUFFER_BIT);
165 glViewport((w - drawW) / 2, (h - drawH) / 2, drawW, drawH);
166}
167
168static void GBAGLES2ContextClear(struct VideoBackend* v) {
169 UNUSED(v);
170 glClearColor(0.f, 0.f, 0.f, 1.f);
171 glClear(GL_COLOR_BUFFER_BIT);
172}
173
174void _drawShader(struct GBAGLES2Shader* shader) {
175 GLint viewport[4];
176 glBindFramebuffer(GL_FRAMEBUFFER, shader->fbo);
177 if (shader->blend) {
178 glEnable(GL_BLEND);
179 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
180 } else {
181 glDisable(GL_BLEND);
182 glClear(GL_COLOR_BUFFER_BIT);
183 }
184
185 glGetIntegerv(GL_VIEWPORT, viewport);
186 int drawW = shader->width;
187 int drawH = shader->height;
188 int padW = 0;
189 int padH = 0;
190 if (!shader->width) {
191 drawW = viewport[2];
192 padW = viewport[0];
193 }
194 if (!shader->height) {
195 drawH = viewport[3];
196 padH = viewport[1];
197 }
198 glViewport(padW, padH, drawW, drawH);
199 if (!shader->width || !shader->height) {
200 GLint oldTex;
201 glGetIntegerv(GL_TEXTURE_BINDING_2D, &oldTex);
202 glBindTexture(GL_TEXTURE_2D, shader->tex);
203 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, drawW, drawH, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
204 glBindTexture(GL_TEXTURE_2D, oldTex);
205 }
206
207 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, shader->filter ? GL_LINEAR : GL_NEAREST);
208 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, shader->filter ? GL_LINEAR : GL_NEAREST);
209 glUseProgram(shader->program);
210 glUniform1i(shader->texLocation, 0);
211 glVertexAttribPointer(shader->positionLocation, 2, GL_FLOAT, GL_FALSE, 0, _vertices);
212 glEnableVertexAttribArray(shader->positionLocation);
213 size_t u;
214 for (u = 0; u < shader->nUniforms; ++u) {
215 struct GBAGLES2Uniform* uniform = &shader->uniforms[u];
216 switch (uniform->type) {
217 case GL_FLOAT:
218 glUniform1f(uniform->location, uniform->value.f);
219 break;
220 case GL_INT:
221 glUniform1i(uniform->location, uniform->value.i);
222 break;
223 case GL_BOOL:
224 glUniform1i(uniform->location, uniform->value.b);
225 break;
226 case GL_FLOAT_VEC2:
227 glUniform2fv(uniform->location, 1, uniform->value.fvec2);
228 break;
229 case GL_FLOAT_VEC3:
230 glUniform3fv(uniform->location, 1, uniform->value.fvec3);
231 break;
232 case GL_FLOAT_VEC4:
233 glUniform4fv(uniform->location, 1, uniform->value.fvec4);
234 break;
235 case GL_INT_VEC2:
236 glUniform2iv(uniform->location, 1, uniform->value.ivec2);
237 break;
238 case GL_INT_VEC3:
239 glUniform3iv(uniform->location, 1, uniform->value.ivec3);
240 break;
241 case GL_INT_VEC4:
242 glUniform4iv(uniform->location, 1, uniform->value.ivec4);
243 break;
244 case GL_BOOL_VEC2:
245 glUniform2i(uniform->location, uniform->value.bvec2[0], uniform->value.bvec2[1]);
246 break;
247 case GL_BOOL_VEC3:
248 glUniform3i(uniform->location, uniform->value.bvec3[0], uniform->value.bvec3[1], uniform->value.bvec3[2]);
249 break;
250 case GL_BOOL_VEC4:
251 glUniform4i(uniform->location, uniform->value.bvec4[0], uniform->value.bvec4[1], uniform->value.bvec4[2], uniform->value.bvec4[3]);
252 break;
253 case GL_FLOAT_MAT2:
254 glUniformMatrix2fv(uniform->location, 1, GL_FALSE, uniform->value.fmat2x2);
255 break;
256 case GL_FLOAT_MAT3:
257 glUniformMatrix3fv(uniform->location, 1, GL_FALSE, uniform->value.fmat3x3);
258 break;
259 case GL_FLOAT_MAT4:
260 glUniformMatrix4fv(uniform->location, 1, GL_FALSE, uniform->value.fmat4x4);
261 break;
262 }
263 }
264 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
265 glBindTexture(GL_TEXTURE_2D, shader->tex);
266 glViewport(viewport[0], viewport[1], viewport[2], viewport[3]);
267}
268
269void GBAGLES2ContextDrawFrame(struct VideoBackend* v) {
270 struct GBAGLES2Context* context = (struct GBAGLES2Context*) v;
271 glActiveTexture(GL_TEXTURE0);
272 glBindTexture(GL_TEXTURE_2D, context->tex);
273
274 context->finalShader.filter = v->filter;
275 _drawShader(&context->initialShader);
276 size_t n;
277 for (n = 0; n < context->nShaders; ++n) {
278 _drawShader(&context->shaders[n]);
279 }
280 _drawShader(&context->finalShader);
281 glBindFramebuffer(GL_FRAMEBUFFER, 0);
282 glUseProgram(0);
283}
284
285void GBAGLES2ContextPostFrame(struct VideoBackend* v, const void* frame) {
286 struct GBAGLES2Context* context = (struct GBAGLES2Context*) v;
287 glBindTexture(GL_TEXTURE_2D, context->tex);
288#ifdef COLOR_16_BIT
289#ifdef COLOR_5_6_5
290 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, frame);
291#else
292 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);
293#endif
294#else
295 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, 0, GL_RGBA, GL_UNSIGNED_BYTE, frame);
296#endif
297}
298
299void GBAGLES2ContextCreate(struct GBAGLES2Context* context) {
300 context->d.init = GBAGLES2ContextInit;
301 context->d.deinit = GBAGLES2ContextDeinit;
302 context->d.resized = GBAGLES2ContextResized;
303 context->d.swap = 0;
304 context->d.clear = GBAGLES2ContextClear;
305 context->d.postFrame = GBAGLES2ContextPostFrame;
306 context->d.drawFrame = GBAGLES2ContextDrawFrame;
307 context->d.setMessage = 0;
308 context->d.clearMessage = 0;
309 context->shaders = 0;
310 context->nShaders = 0;
311}
312
313void GBAGLES2ShaderInit(struct GBAGLES2Shader* shader, const char* vs, const char* fs, int width, int height, struct GBAGLES2Uniform* uniforms, size_t nUniforms) {
314 shader->width = width >= 0 ? width : VIDEO_HORIZONTAL_PIXELS;
315 shader->height = height >= 0 ? height : VIDEO_VERTICAL_PIXELS;
316 shader->filter = false;
317 shader->blend = false;
318 shader->uniforms = uniforms;
319 shader->nUniforms = nUniforms;
320 glGenFramebuffers(1, &shader->fbo);
321 glBindFramebuffer(GL_FRAMEBUFFER, shader->fbo);
322
323 glGenTextures(1, &shader->tex);
324 glBindTexture(GL_TEXTURE_2D, shader->tex);
325 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
326 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
327 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
328 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
329 if (shader->width && shader->height) {
330 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, shader->width, shader->height, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
331 }
332
333 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, shader->tex, 0);
334 shader->program = glCreateProgram();
335 shader->vertexShader = glCreateShader(GL_VERTEX_SHADER);
336 shader->fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
337 const GLchar* shaderBuffer[2];
338 const GLubyte* version = glGetString(GL_VERSION);
339 if (strncmp((const char*) version, "OpenGL ES ", strlen("OpenGL ES "))) {
340 shaderBuffer[0] = _gl3Header;
341 } else {
342 shaderBuffer[0] = _gles2Header;
343 }
344 if (vs) {
345 shaderBuffer[1] = vs;
346 } else {
347 shaderBuffer[1] = _nullVertexShader;
348 }
349 glShaderSource(shader->vertexShader, 2, shaderBuffer, 0);
350
351 if (fs) {
352 shaderBuffer[1] = fs;
353 } else {
354 shaderBuffer[1] = _nullFragmentShader;
355 }
356 glShaderSource(shader->fragmentShader, 2, shaderBuffer, 0);
357
358 glAttachShader(shader->program, shader->vertexShader);
359 glAttachShader(shader->program, shader->fragmentShader);
360 char log[1024];
361 glCompileShader(shader->fragmentShader);
362 glGetShaderInfoLog(shader->fragmentShader, 1024, 0, log);
363 if (log[0]) {
364 printf("%s\n", log);
365 }
366 glCompileShader(shader->vertexShader);
367 glGetShaderInfoLog(shader->vertexShader, 1024, 0, log);
368 if (log[0]) {
369 printf("%s\n", log);
370 }
371 glLinkProgram(shader->program);
372 glGetProgramInfoLog(shader->program, 1024, 0, log);
373 if (log[0]) {
374 printf("%s\n", log);
375 }
376
377 shader->texLocation = glGetUniformLocation(shader->program, "tex");
378 shader->positionLocation = glGetAttribLocation(shader->program, "position");
379 size_t i;
380 for (i = 0; i < shader->nUniforms; ++i) {
381 shader->uniforms[i].location = glGetUniformLocation(shader->program, shader->uniforms[i].name);
382 }
383 glBindFramebuffer(GL_FRAMEBUFFER, 0);
384}
385
386void GBAGLES2ShaderDeinit(struct GBAGLES2Shader* shader) {
387 glDeleteTextures(1, &shader->tex);
388 glDeleteShader(shader->fragmentShader);
389 glDeleteProgram(shader->program);
390 glDeleteFramebuffers(1, &shader->fbo);
391}
392
393void GBAGLES2ShaderAttach(struct GBAGLES2Context* context, struct GBAGLES2Shader* shaders, size_t nShaders) {
394 if (context->shaders) {
395 if (context->shaders == shaders && context->nShaders == nShaders) {
396 return;
397 }
398 GBAGLES2ShaderDetach(context);
399 }
400 context->shaders = shaders;
401 context->nShaders = nShaders;
402 size_t i;
403 for (i = 0; i < nShaders; ++i) {
404 glBindFramebuffer(GL_FRAMEBUFFER, context->shaders[i].fbo);
405 glClear(GL_COLOR_BUFFER_BIT);
406 }
407 glBindFramebuffer(GL_FRAMEBUFFER, 0);
408}
409
410void GBAGLES2ShaderDetach(struct GBAGLES2Context* context) {
411 if (!context->shaders) {
412 return;
413 }
414 context->shaders = 0;
415 context->nShaders = 0;
416}
417
418static bool _lookupIntValue(const struct Configuration* config, const char* section, const char* key, int* out) {
419 const char* charValue = ConfigurationGetValue(config, section, key);
420 if (!charValue) {
421 return false;
422 }
423 char* end;
424 unsigned long value = strtol(charValue, &end, 10);
425 if (*end) {
426 return false;
427 }
428 *out = value;
429 return true;
430}
431
432static bool _lookupFloatValue(const struct Configuration* config, const char* section, const char* key, float* out) {
433 const char* charValue = ConfigurationGetValue(config, section, key);
434 if (!charValue) {
435 return false;
436 }
437 char* end;
438 float value = strtof_u(charValue, &end);
439 if (*end) {
440 return false;
441 }
442 *out = value;
443 return true;
444}
445
446static bool _lookupBoolValue(const struct Configuration* config, const char* section, const char* key, GLboolean* out) {
447 const char* charValue = ConfigurationGetValue(config, section, key);
448 if (!charValue) {
449 return false;
450 }
451 if (!strcmp(charValue, "true")) {
452 *out = GL_TRUE;
453 return true;
454 }
455 if (!strcmp(charValue, "false")) {
456 *out = GL_FALSE;
457 return true;
458 }
459 char* end;
460 unsigned long value = strtol(charValue, &end, 10);
461 if (*end) {
462 return false;
463 }
464 *out = value;
465 return true;
466}
467
468DECLARE_VECTOR(GBAGLES2UniformList, struct GBAGLES2Uniform);
469DEFINE_VECTOR(GBAGLES2UniformList, struct GBAGLES2Uniform);
470
471static void _uniformHandler(const char* sectionName, void* user) {
472 struct GBAGLES2UniformList* uniforms = user;
473 unsigned passId;
474 int sentinel;
475 if (sscanf(sectionName, "pass.%u.uniform.%n", &passId, &sentinel) < 1) {
476 return;
477 }
478 struct GBAGLES2Uniform* u = GBAGLES2UniformListAppend(uniforms);
479 u->name = sectionName;
480}
481
482
483static void _loadValue(struct Configuration* description, const char* name, GLenum type, const char* field, union GBAGLES2UniformValue* value) {
484 char fieldName[16];
485 switch (type) {
486 case GL_FLOAT:
487 value->f = 0;
488 _lookupFloatValue(description, name, field, &value->f);
489 break;
490 case GL_FLOAT_VEC2:
491 value->fvec2[0] = 0;
492 value->fvec2[1] = 0;
493 snprintf(fieldName, sizeof(fieldName), "%s[0]", field);
494 _lookupFloatValue(description, name, fieldName, &value->fvec2[0]);
495 snprintf(fieldName, sizeof(fieldName), "%s[1]", field);
496 _lookupFloatValue(description, name, fieldName, &value->fvec2[1]);
497 break;
498 case GL_FLOAT_VEC3:
499 value->fvec3[0] = 0;
500 value->fvec3[1] = 0;
501 value->fvec3[2] = 0;
502 snprintf(fieldName, sizeof(fieldName), "%s[0]", field);
503 _lookupFloatValue(description, name, fieldName, &value->fvec3[0]);
504 snprintf(fieldName, sizeof(fieldName), "%s[1]", field);
505 _lookupFloatValue(description, name, fieldName, &value->fvec3[1]);
506 snprintf(fieldName, sizeof(fieldName), "%s[2]", field);
507 _lookupFloatValue(description, name, fieldName, &value->fvec3[2]);
508 break;
509 case GL_FLOAT_VEC4:
510 value->fvec4[0] = 0;
511 value->fvec4[1] = 0;
512 value->fvec4[2] = 0;
513 value->fvec4[3] = 0;
514 snprintf(fieldName, sizeof(fieldName), "%s[0]", field);
515 _lookupFloatValue(description, name, fieldName, &value->fvec4[0]);
516 snprintf(fieldName, sizeof(fieldName), "%s[1]", field);
517 _lookupFloatValue(description, name, fieldName, &value->fvec4[1]);
518 snprintf(fieldName, sizeof(fieldName), "%s[2]", field);
519 _lookupFloatValue(description, name, fieldName, &value->fvec4[2]);
520 snprintf(fieldName, sizeof(fieldName), "%s[3]", field);
521 _lookupFloatValue(description, name, fieldName, &value->fvec4[3]);
522 break;
523 case GL_FLOAT_MAT2:
524 value->fmat2x2[0] = 0;
525 value->fmat2x2[1] = 0;
526 value->fmat2x2[2] = 0;
527 value->fmat2x2[3] = 0;
528 snprintf(fieldName, sizeof(fieldName), "%s[0,0]", field);
529 _lookupFloatValue(description, name, fieldName, &value->fmat2x2[0]);
530 snprintf(fieldName, sizeof(fieldName), "%s[0,1]", field);
531 _lookupFloatValue(description, name, fieldName, &value->fmat2x2[1]);
532 snprintf(fieldName, sizeof(fieldName), "%s[1,0]", field);
533 _lookupFloatValue(description, name, fieldName, &value->fmat2x2[2]);
534 snprintf(fieldName, sizeof(fieldName), "%s[1,1]", field);
535 _lookupFloatValue(description, name, fieldName, &value->fmat2x2[3]);
536 break;
537 case GL_FLOAT_MAT3:
538 value->fmat3x3[0] = 0;
539 value->fmat3x3[1] = 0;
540 value->fmat3x3[2] = 0;
541 value->fmat3x3[3] = 0;
542 value->fmat3x3[4] = 0;
543 value->fmat3x3[5] = 0;
544 value->fmat3x3[6] = 0;
545 value->fmat3x3[7] = 0;
546 value->fmat3x3[8] = 0;
547 snprintf(fieldName, sizeof(fieldName), "%s[0,0]", field);
548 _lookupFloatValue(description, name, fieldName, &value->fmat3x3[0]);
549 snprintf(fieldName, sizeof(fieldName), "%s[0,1]", field);
550 _lookupFloatValue(description, name, fieldName, &value->fmat3x3[1]);
551 snprintf(fieldName, sizeof(fieldName), "%s[0,2]", field);
552 _lookupFloatValue(description, name, fieldName, &value->fmat3x3[2]);
553 snprintf(fieldName, sizeof(fieldName), "%s[1,0]", field);
554 _lookupFloatValue(description, name, fieldName, &value->fmat3x3[3]);
555 snprintf(fieldName, sizeof(fieldName), "%s[1,1]", field);
556 _lookupFloatValue(description, name, fieldName, &value->fmat3x3[4]);
557 snprintf(fieldName, sizeof(fieldName), "%s[1,2]", field);
558 _lookupFloatValue(description, name, fieldName, &value->fmat3x3[5]);
559 snprintf(fieldName, sizeof(fieldName), "%s[2,0]", field);
560 _lookupFloatValue(description, name, fieldName, &value->fmat3x3[6]);
561 snprintf(fieldName, sizeof(fieldName), "%s[2,1]", field);
562 _lookupFloatValue(description, name, fieldName, &value->fmat3x3[7]);
563 snprintf(fieldName, sizeof(fieldName), "%s[2,2]", field);
564 _lookupFloatValue(description, name, fieldName, &value->fmat3x3[8]);
565 break;
566 case GL_FLOAT_MAT4:
567 value->fmat4x4[0] = 0;
568 value->fmat4x4[1] = 0;
569 value->fmat4x4[2] = 0;
570 value->fmat4x4[3] = 0;
571 value->fmat4x4[4] = 0;
572 value->fmat4x4[5] = 0;
573 value->fmat4x4[6] = 0;
574 value->fmat4x4[7] = 0;
575 value->fmat4x4[8] = 0;
576 value->fmat4x4[9] = 0;
577 value->fmat4x4[10] = 0;
578 value->fmat4x4[11] = 0;
579 value->fmat4x4[12] = 0;
580 value->fmat4x4[13] = 0;
581 value->fmat4x4[14] = 0;
582 value->fmat4x4[15] = 0;
583 snprintf(fieldName, sizeof(fieldName), "%s[0,0]", field);
584 _lookupFloatValue(description, name, fieldName, &value->fmat4x4[0]);
585 snprintf(fieldName, sizeof(fieldName), "%s[0,1]", field);
586 _lookupFloatValue(description, name, fieldName, &value->fmat4x4[1]);
587 snprintf(fieldName, sizeof(fieldName), "%s[0,2]", field);
588 _lookupFloatValue(description, name, fieldName, &value->fmat4x4[2]);
589 snprintf(fieldName, sizeof(fieldName), "%s[0,3]", field);
590 _lookupFloatValue(description, name, fieldName, &value->fmat4x4[3]);
591 snprintf(fieldName, sizeof(fieldName), "%s[1,0]", field);
592 _lookupFloatValue(description, name, fieldName, &value->fmat4x4[4]);
593 snprintf(fieldName, sizeof(fieldName), "%s[1,1]", field);
594 _lookupFloatValue(description, name, fieldName, &value->fmat4x4[5]);
595 snprintf(fieldName, sizeof(fieldName), "%s[1,2]", field);
596 _lookupFloatValue(description, name, fieldName, &value->fmat4x4[6]);
597 snprintf(fieldName, sizeof(fieldName), "%s[1,3]", field);
598 _lookupFloatValue(description, name, fieldName, &value->fmat4x4[7]);
599 snprintf(fieldName, sizeof(fieldName), "%s[2,0]", field);
600 _lookupFloatValue(description, name, fieldName, &value->fmat4x4[8]);
601 snprintf(fieldName, sizeof(fieldName), "%s[2,1]", field);
602 _lookupFloatValue(description, name, fieldName, &value->fmat4x4[9]);
603 snprintf(fieldName, sizeof(fieldName), "%s[2,2]", field);
604 _lookupFloatValue(description, name, fieldName, &value->fmat4x4[10]);
605 snprintf(fieldName, sizeof(fieldName), "%s[2,3]", field);
606 _lookupFloatValue(description, name, fieldName, &value->fmat4x4[11]);
607 snprintf(fieldName, sizeof(fieldName), "%s[3,0]", field);
608 _lookupFloatValue(description, name, fieldName, &value->fmat4x4[12]);
609 snprintf(fieldName, sizeof(fieldName), "%s[3,1]", field);
610 _lookupFloatValue(description, name, fieldName, &value->fmat4x4[13]);
611 snprintf(fieldName, sizeof(fieldName), "%s[3,2]", field);
612 _lookupFloatValue(description, name, fieldName, &value->fmat4x4[14]);
613 snprintf(fieldName, sizeof(fieldName), "%s[3,3]", field);
614 _lookupFloatValue(description, name, fieldName, &value->fmat4x4[15]);
615 break;
616 case GL_INT:
617 value->i = 0;
618 _lookupIntValue(description, name, field, &value->i);
619 break;
620 case GL_INT_VEC2:
621 value->ivec2[0] = 0;
622 value->ivec2[1] = 0;
623 snprintf(fieldName, sizeof(fieldName), "%s[0]", field);
624 _lookupIntValue(description, name, fieldName, &value->ivec2[0]);
625 snprintf(fieldName, sizeof(fieldName), "%s[1]", field);
626 _lookupIntValue(description, name, fieldName, &value->ivec2[1]);
627 break;
628 case GL_INT_VEC3:
629 value->ivec3[0] = 0;
630 value->ivec3[1] = 0;
631 value->ivec3[2] = 0;
632 snprintf(fieldName, sizeof(fieldName), "%s[0]", field);
633 _lookupIntValue(description, name, fieldName, &value->ivec3[0]);
634 snprintf(fieldName, sizeof(fieldName), "%s[1]", field);
635 _lookupIntValue(description, name, fieldName, &value->ivec3[1]);
636 snprintf(fieldName, sizeof(fieldName), "%s[2]", field);
637 _lookupIntValue(description, name, fieldName, &value->ivec3[2]);
638 break;
639 case GL_INT_VEC4:
640 value->ivec4[0] = 0;
641 value->ivec4[1] = 0;
642 value->ivec4[2] = 0;
643 value->ivec4[3] = 0;
644 snprintf(fieldName, sizeof(fieldName), "%s[0]", field);
645 _lookupIntValue(description, name, fieldName, &value->ivec4[0]);
646 snprintf(fieldName, sizeof(fieldName), "%s[1]", field);
647 _lookupIntValue(description, name, fieldName, &value->ivec4[1]);
648 snprintf(fieldName, sizeof(fieldName), "%s[2]", field);
649 _lookupIntValue(description, name, fieldName, &value->ivec4[2]);
650 snprintf(fieldName, sizeof(fieldName), "%s[3]", field);
651 _lookupIntValue(description, name, fieldName, &value->ivec4[3]);
652 break;
653 case GL_BOOL:
654 value->b = 0;
655 _lookupBoolValue(description, name, field, &value->b);
656 break;
657 case GL_BOOL_VEC2:
658 value->bvec2[0] = 0;
659 value->bvec2[1] = 0;
660 snprintf(fieldName, sizeof(fieldName), "%s[0]", field);
661 _lookupBoolValue(description, name, fieldName, &value->bvec2[0]);
662 snprintf(fieldName, sizeof(fieldName), "%s[1]", field);
663 _lookupBoolValue(description, name, fieldName, &value->bvec2[1]);
664 break;
665 case GL_BOOL_VEC3:
666 value->bvec3[0] = 0;
667 value->bvec3[1] = 0;
668 value->bvec3[2] = 0;
669 snprintf(fieldName, sizeof(fieldName), "%s[0]", field);
670 _lookupBoolValue(description, name, fieldName, &value->bvec3[0]);
671 snprintf(fieldName, sizeof(fieldName), "%s[1]", field);
672 _lookupBoolValue(description, name, fieldName, &value->bvec3[1]);
673 snprintf(fieldName, sizeof(fieldName), "%s[2]", field);
674 _lookupBoolValue(description, name, fieldName, &value->bvec3[2]);
675 break;
676 case GL_BOOL_VEC4:
677 value->bvec4[0] = 0;
678 value->bvec4[1] = 0;
679 value->bvec4[2] = 0;
680 value->bvec4[3] = 0;
681 snprintf(fieldName, sizeof(fieldName), "%s[0]", field);
682 _lookupBoolValue(description, name, fieldName, &value->bvec4[0]);
683 snprintf(fieldName, sizeof(fieldName), "%s[1]", field);
684 _lookupBoolValue(description, name, fieldName, &value->bvec4[1]);
685 snprintf(fieldName, sizeof(fieldName), "%s[2]", field);
686 _lookupBoolValue(description, name, fieldName, &value->bvec4[2]);
687 snprintf(fieldName, sizeof(fieldName), "%s[3]", field);
688 _lookupBoolValue(description, name, fieldName, &value->bvec4[3]);
689 break;
690 }
691}
692
693static bool _loadUniform(struct Configuration* description, size_t pass, struct GBAGLES2Uniform* uniform) {
694 unsigned passId;
695 if (sscanf(uniform->name, "pass.%u.uniform.", &passId) < 1 || passId != pass) {
696 return false;
697 }
698 const char* type = ConfigurationGetValue(description, uniform->name, "type");
699 if (!type) {
700 return false;
701 }
702 if (!strcmp(type, "float")) {
703 uniform->type = GL_FLOAT;
704 } else if (!strcmp(type, "float2")) {
705 uniform->type = GL_FLOAT_VEC2;
706 } else if (!strcmp(type, "float3")) {
707 uniform->type = GL_FLOAT_VEC3;
708 } else if (!strcmp(type, "float4")) {
709 uniform->type = GL_FLOAT_VEC4;
710 } else if (!strcmp(type, "float2x2")) {
711 uniform->type = GL_FLOAT_MAT2;
712 } else if (!strcmp(type, "float3x3")) {
713 uniform->type = GL_FLOAT_MAT3;
714 } else if (!strcmp(type, "float4x4")) {
715 uniform->type = GL_FLOAT_MAT4;
716 } else if (!strcmp(type, "int")) {
717 uniform->type = GL_INT;
718 } else if (!strcmp(type, "int2")) {
719 uniform->type = GL_INT_VEC2;
720 } else if (!strcmp(type, "int3")) {
721 uniform->type = GL_INT_VEC3;
722 } else if (!strcmp(type, "int4")) {
723 uniform->type = GL_INT_VEC4;
724 } else if (!strcmp(type, "bool")) {
725 uniform->type = GL_BOOL;
726 } else if (!strcmp(type, "int2")) {
727 uniform->type = GL_BOOL_VEC2;
728 } else if (!strcmp(type, "int3")) {
729 uniform->type = GL_BOOL_VEC3;
730 } else if (!strcmp(type, "int4")) {
731 uniform->type = GL_BOOL_VEC4;
732 } else {
733 return false;
734 }
735 _loadValue(description, uniform->name, uniform->type, "default", &uniform->value);
736 _loadValue(description, uniform->name, uniform->type, "min", &uniform->min);
737 _loadValue(description, uniform->name, uniform->type, "max", &uniform->max);
738 const char* readable = ConfigurationGetValue(description, uniform->name, "readableName");
739 if (readable) {
740 uniform->readableName = strdup(readable);
741 } else {
742 uniform->readableName = 0;
743 }
744 uniform->name = strdup(strstr(uniform->name, "uniform.") + strlen("uniform."));
745 return true;
746}
747
748bool GBAGLES2ShaderLoad(struct VideoShader* shader, struct VDir* dir) {
749 struct VFile* manifest = dir->openFile(dir, "manifest.ini", O_RDONLY);
750 if (!manifest) {
751 return false;
752 }
753 bool success = false;
754 struct Configuration description;
755 ConfigurationInit(&description);
756 if (ConfigurationReadVFile(&description, manifest)) {
757 int inShaders;
758 success = _lookupIntValue(&description, "shader", "passes", &inShaders);
759 if (inShaders > MAX_PASSES || inShaders < 1) {
760 success = false;
761 }
762 if (success) {
763 struct GBAGLES2Shader* shaderBlock = malloc(sizeof(struct GBAGLES2Shader) * inShaders);
764 int n;
765 for (n = 0; n < inShaders; ++n) {
766 char passName[12];
767 snprintf(passName, sizeof(passName), "pass.%u", n);
768 const char* fs = ConfigurationGetValue(&description, passName, "fragmentShader");
769 const char* vs = ConfigurationGetValue(&description, passName, "vertexShader");
770 if (fs && (fs[0] == '.' || strstr(fs, PATH_SEP))) {
771 success = false;
772 break;
773 }
774 if (vs && (vs[0] == '.' || strstr(vs, PATH_SEP))) {
775 success = false;
776 break;
777 }
778 char* fssrc = 0;
779 char* vssrc = 0;
780 if (fs) {
781 struct VFile* fsf = dir->openFile(dir, fs, O_RDONLY);
782 if (!fsf) {
783 success = false;
784 break;
785 }
786 fssrc = malloc(fsf->size(fsf) + 1);
787 fssrc[fsf->size(fsf)] = '\0';
788 fsf->read(fsf, fssrc, fsf->size(fsf));
789 fsf->close(fsf);
790 }
791 if (vs) {
792 struct VFile* vsf = dir->openFile(dir, vs, O_RDONLY);
793 if (!vsf) {
794 success = false;
795 free(fssrc);
796 break;
797 }
798 vssrc = malloc(vsf->size(vsf) + 1);
799 vssrc[vsf->size(vsf)] = '\0';
800 vsf->read(vsf, vssrc, vsf->size(vsf));
801 vsf->close(vsf);
802 }
803 int width = 0;
804 int height = 0;
805 _lookupIntValue(&description, passName, "width", &width);
806 _lookupIntValue(&description, passName, "height", &height);
807
808 struct GBAGLES2UniformList uniformVector;
809 GBAGLES2UniformListInit(&uniformVector, 0);
810 ConfigurationEnumerateSections(&description, _uniformHandler, &uniformVector);
811 size_t u;
812 for (u = 0; u < GBAGLES2UniformListSize(&uniformVector); ++u) {
813 struct GBAGLES2Uniform* uniform = GBAGLES2UniformListGetPointer(&uniformVector, u);
814 if (!_loadUniform(&description, n, uniform)) {
815 GBAGLES2UniformListShift(&uniformVector, u, 1);
816 --u;
817 }
818 }
819 u = GBAGLES2UniformListSize(&uniformVector);
820 struct GBAGLES2Uniform* uniformBlock = malloc(sizeof(*uniformBlock) * u);
821 memcpy(uniformBlock, GBAGLES2UniformListGetPointer(&uniformVector, 0), sizeof(*uniformBlock) * u);
822 GBAGLES2UniformListDeinit(&uniformVector);
823
824 GBAGLES2ShaderInit(&shaderBlock[n], vssrc, fssrc, width, height, uniformBlock, u);
825 int b = 0;
826 _lookupIntValue(&description, passName, "blend", &b);
827 if (b) {
828 shaderBlock[n].blend = b;
829 }
830 b = 0;
831 _lookupIntValue(&description, passName, "filter", &b);
832 if (b) {
833 shaderBlock[n].filter = b;
834 }
835 free(fssrc);
836 free(vssrc);
837 }
838 if (success) {
839 shader->nPasses = inShaders;
840 shader->passes = shaderBlock;
841 shader->name = ConfigurationGetValue(&description, "shader", "name");
842 if (shader->name) {
843 shader->name = strdup(shader->name);
844 }
845 shader->author = ConfigurationGetValue(&description, "shader", "author");
846 if (shader->author) {
847 shader->author = strdup(shader->author);
848 }
849 shader->description = ConfigurationGetValue(&description, "shader", "description");
850 if (shader->description) {
851 shader->description = strdup(shader->description);
852 }
853 } else {
854 inShaders = n;
855 for (n = 0; n < inShaders; ++n) {
856 GBAGLES2ShaderDeinit(&shaderBlock[n]);
857 }
858 }
859 }
860 }
861 ConfigurationDeinit(&description);
862 return success;
863}
864
865void GBAGLES2ShaderFree(struct VideoShader* shader) {
866 free((void*) shader->name);
867 free((void*) shader->author);
868 free((void*) shader->description);
869 shader->name = 0;
870 shader->author = 0;
871 shader->description = 0;
872 struct GBAGLES2Shader* shaders = shader->passes;
873 size_t n;
874 for (n = 0; n < shader->nPasses; ++n) {
875 GBAGLES2ShaderDeinit(&shaders[n]);
876 size_t u;
877 for (u = 0; u < shaders[n].nUniforms; ++u) {
878 free((void*) shaders[n].uniforms[u].name);
879 free((void*) shaders[n].uniforms[u].readableName);
880 }
881 }
882 free(shaders);
883 shader->passes = 0;
884 shader->nPasses = 0;
885}