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