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