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