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