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 context->nShaders = 0;
368}
369
370static bool _lookupIntValue(const struct Configuration* config, const char* section, const char* key, int* out) {
371 const char* charValue = ConfigurationGetValue(config, section, key);
372 if (!charValue) {
373 return false;
374 }
375 char* end;
376 unsigned long value = strtol(charValue, &end, 10);
377 if (*end) {
378 return false;
379 }
380 *out = value;
381 return true;
382}
383
384static bool _lookupFloatValue(const struct Configuration* config, const char* section, const char* key, float* out) {
385 const char* charValue = ConfigurationGetValue(config, section, key);
386 if (!charValue) {
387 return false;
388 }
389 char* end;
390 float value = strtof_u(charValue, &end);
391 if (*end) {
392 return false;
393 }
394 *out = value;
395 return true;
396}
397
398static bool _lookupBoolValue(const struct Configuration* config, const char* section, const char* key, GLboolean* out) {
399 const char* charValue = ConfigurationGetValue(config, section, key);
400 if (!charValue) {
401 return false;
402 }
403 if (!strcmp(charValue, "true")) {
404 *out = GL_TRUE;
405 return true;
406 }
407 if (!strcmp(charValue, "false")) {
408 *out = GL_FALSE;
409 return true;
410 }
411 char* end;
412 unsigned long value = strtol(charValue, &end, 10);
413 if (*end) {
414 return false;
415 }
416 *out = value;
417 return true;
418}
419
420DECLARE_VECTOR(GBAGLES2UniformList, struct GBAGLES2Uniform);
421DEFINE_VECTOR(GBAGLES2UniformList, struct GBAGLES2Uniform);
422
423static void _uniformHandler(const char* sectionName, void* user) {
424 struct GBAGLES2UniformList* uniforms = user;
425 if (strstr(sectionName, "uniform.") != sectionName) {
426 return;
427 }
428 struct GBAGLES2Uniform* u = GBAGLES2UniformListAppend(uniforms);
429 u->name = sectionName;
430}
431
432
433static void _loadValue(struct Configuration* description, const char* name, GLenum type, const char* field, union GBAGLES2UniformValue* value) {
434 char fieldName[16];
435 switch (type) {
436 case GL_FLOAT:
437 value->f = 0;
438 _lookupFloatValue(description, name, field, &value->f);
439 break;
440 case GL_FLOAT_VEC2:
441 value->fvec2[0] = 0;
442 value->fvec2[1] = 0;
443 snprintf(fieldName, sizeof(fieldName), "%s[0]", field);
444 _lookupFloatValue(description, name, fieldName, &value->fvec2[0]);
445 snprintf(fieldName, sizeof(fieldName), "%s[1]", field);
446 _lookupFloatValue(description, name, fieldName, &value->fvec2[1]);
447 break;
448 case GL_FLOAT_VEC3:
449 value->fvec3[0] = 0;
450 value->fvec3[1] = 0;
451 value->fvec3[2] = 0;
452 snprintf(fieldName, sizeof(fieldName), "%s[0]", field);
453 _lookupFloatValue(description, name, fieldName, &value->fvec3[0]);
454 snprintf(fieldName, sizeof(fieldName), "%s[1]", field);
455 _lookupFloatValue(description, name, fieldName, &value->fvec3[1]);
456 snprintf(fieldName, sizeof(fieldName), "%s[2]", field);
457 _lookupFloatValue(description, name, fieldName, &value->fvec3[2]);
458 break;
459 case GL_FLOAT_VEC4:
460 value->fvec4[0] = 0;
461 value->fvec4[1] = 0;
462 value->fvec4[2] = 0;
463 value->fvec4[3] = 0;
464 snprintf(fieldName, sizeof(fieldName), "%s[0]", field);
465 _lookupFloatValue(description, name, fieldName, &value->fvec4[0]);
466 snprintf(fieldName, sizeof(fieldName), "%s[1]", field);
467 _lookupFloatValue(description, name, fieldName, &value->fvec4[1]);
468 snprintf(fieldName, sizeof(fieldName), "%s[2]", field);
469 _lookupFloatValue(description, name, fieldName, &value->fvec4[2]);
470 snprintf(fieldName, sizeof(fieldName), "%s[3]", field);
471 _lookupFloatValue(description, name, fieldName, &value->fvec4[3]);
472 break;
473 case GL_FLOAT_MAT2:
474 value->fmat2x2[0] = 0;
475 value->fmat2x2[1] = 0;
476 value->fmat2x2[2] = 0;
477 value->fmat2x2[3] = 0;
478 snprintf(fieldName, sizeof(fieldName), "%s[0,0]", field);
479 _lookupFloatValue(description, name, fieldName, &value->fmat2x2[0]);
480 snprintf(fieldName, sizeof(fieldName), "%s[0,1]", field);
481 _lookupFloatValue(description, name, fieldName, &value->fmat2x2[1]);
482 snprintf(fieldName, sizeof(fieldName), "%s[1,0]", field);
483 _lookupFloatValue(description, name, fieldName, &value->fmat2x2[2]);
484 snprintf(fieldName, sizeof(fieldName), "%s[1,1]", field);
485 _lookupFloatValue(description, name, fieldName, &value->fmat2x2[3]);
486 break;
487 case GL_FLOAT_MAT3:
488 value->fmat3x3[0] = 0;
489 value->fmat3x3[1] = 0;
490 value->fmat3x3[2] = 0;
491 value->fmat3x3[3] = 0;
492 value->fmat3x3[4] = 0;
493 value->fmat3x3[5] = 0;
494 value->fmat3x3[6] = 0;
495 value->fmat3x3[7] = 0;
496 value->fmat3x3[8] = 0;
497 snprintf(fieldName, sizeof(fieldName), "%s[0,0]", field);
498 _lookupFloatValue(description, name, fieldName, &value->fmat3x3[0]);
499 snprintf(fieldName, sizeof(fieldName), "%s[0,1]", field);
500 _lookupFloatValue(description, name, fieldName, &value->fmat3x3[1]);
501 snprintf(fieldName, sizeof(fieldName), "%s[0,2]", field);
502 _lookupFloatValue(description, name, fieldName, &value->fmat3x3[2]);
503 snprintf(fieldName, sizeof(fieldName), "%s[1,0]", field);
504 _lookupFloatValue(description, name, fieldName, &value->fmat3x3[3]);
505 snprintf(fieldName, sizeof(fieldName), "%s[1,1]", field);
506 _lookupFloatValue(description, name, fieldName, &value->fmat3x3[4]);
507 snprintf(fieldName, sizeof(fieldName), "%s[1,2]", field);
508 _lookupFloatValue(description, name, fieldName, &value->fmat3x3[5]);
509 snprintf(fieldName, sizeof(fieldName), "%s[2,0]", field);
510 _lookupFloatValue(description, name, fieldName, &value->fmat3x3[6]);
511 snprintf(fieldName, sizeof(fieldName), "%s[2,1]", field);
512 _lookupFloatValue(description, name, fieldName, &value->fmat3x3[7]);
513 snprintf(fieldName, sizeof(fieldName), "%s[2,2]", field);
514 _lookupFloatValue(description, name, fieldName, &value->fmat3x3[8]);
515 break;
516 case GL_FLOAT_MAT4:
517 value->fmat4x4[0] = 0;
518 value->fmat4x4[1] = 0;
519 value->fmat4x4[2] = 0;
520 value->fmat4x4[3] = 0;
521 value->fmat4x4[4] = 0;
522 value->fmat4x4[5] = 0;
523 value->fmat4x4[6] = 0;
524 value->fmat4x4[7] = 0;
525 value->fmat4x4[8] = 0;
526 value->fmat4x4[9] = 0;
527 value->fmat4x4[10] = 0;
528 value->fmat4x4[11] = 0;
529 value->fmat4x4[12] = 0;
530 value->fmat4x4[13] = 0;
531 value->fmat4x4[14] = 0;
532 value->fmat4x4[15] = 0;
533 snprintf(fieldName, sizeof(fieldName), "%s[0,0]", field);
534 _lookupFloatValue(description, name, fieldName, &value->fmat4x4[0]);
535 snprintf(fieldName, sizeof(fieldName), "%s[0,1]", field);
536 _lookupFloatValue(description, name, fieldName, &value->fmat4x4[1]);
537 snprintf(fieldName, sizeof(fieldName), "%s[0,2]", field);
538 _lookupFloatValue(description, name, fieldName, &value->fmat4x4[2]);
539 snprintf(fieldName, sizeof(fieldName), "%s[0,3]", field);
540 _lookupFloatValue(description, name, fieldName, &value->fmat4x4[3]);
541 snprintf(fieldName, sizeof(fieldName), "%s[1,0]", field);
542 _lookupFloatValue(description, name, fieldName, &value->fmat4x4[4]);
543 snprintf(fieldName, sizeof(fieldName), "%s[1,1]", field);
544 _lookupFloatValue(description, name, fieldName, &value->fmat4x4[5]);
545 snprintf(fieldName, sizeof(fieldName), "%s[1,2]", field);
546 _lookupFloatValue(description, name, fieldName, &value->fmat4x4[6]);
547 snprintf(fieldName, sizeof(fieldName), "%s[1,3]", field);
548 _lookupFloatValue(description, name, fieldName, &value->fmat4x4[7]);
549 snprintf(fieldName, sizeof(fieldName), "%s[2,0]", field);
550 _lookupFloatValue(description, name, fieldName, &value->fmat4x4[8]);
551 snprintf(fieldName, sizeof(fieldName), "%s[2,1]", field);
552 _lookupFloatValue(description, name, fieldName, &value->fmat4x4[9]);
553 snprintf(fieldName, sizeof(fieldName), "%s[2,2]", field);
554 _lookupFloatValue(description, name, fieldName, &value->fmat4x4[10]);
555 snprintf(fieldName, sizeof(fieldName), "%s[2,3]", field);
556 _lookupFloatValue(description, name, fieldName, &value->fmat4x4[11]);
557 snprintf(fieldName, sizeof(fieldName), "%s[3,0]", field);
558 _lookupFloatValue(description, name, fieldName, &value->fmat4x4[12]);
559 snprintf(fieldName, sizeof(fieldName), "%s[3,1]", field);
560 _lookupFloatValue(description, name, fieldName, &value->fmat4x4[13]);
561 snprintf(fieldName, sizeof(fieldName), "%s[3,2]", field);
562 _lookupFloatValue(description, name, fieldName, &value->fmat4x4[14]);
563 snprintf(fieldName, sizeof(fieldName), "%s[3,3]", field);
564 _lookupFloatValue(description, name, fieldName, &value->fmat4x4[15]);
565 break;
566 case GL_INT:
567 value->i = 0;
568 _lookupIntValue(description, name, field, &value->i);
569 break;
570 case GL_INT_VEC2:
571 value->ivec2[0] = 0;
572 value->ivec2[1] = 0;
573 snprintf(fieldName, sizeof(fieldName), "%s[0]", field);
574 _lookupIntValue(description, name, fieldName, &value->ivec2[0]);
575 snprintf(fieldName, sizeof(fieldName), "%s[1]", field);
576 _lookupIntValue(description, name, fieldName, &value->ivec2[1]);
577 break;
578 case GL_INT_VEC3:
579 value->ivec3[0] = 0;
580 value->ivec3[1] = 0;
581 value->ivec3[2] = 0;
582 snprintf(fieldName, sizeof(fieldName), "%s[0]", field);
583 _lookupIntValue(description, name, fieldName, &value->ivec3[0]);
584 snprintf(fieldName, sizeof(fieldName), "%s[1]", field);
585 _lookupIntValue(description, name, fieldName, &value->ivec3[1]);
586 snprintf(fieldName, sizeof(fieldName), "%s[2]", field);
587 _lookupIntValue(description, name, fieldName, &value->ivec3[2]);
588 break;
589 case GL_INT_VEC4:
590 value->ivec4[0] = 0;
591 value->ivec4[1] = 0;
592 value->ivec4[2] = 0;
593 value->ivec4[3] = 0;
594 snprintf(fieldName, sizeof(fieldName), "%s[0]", field);
595 _lookupIntValue(description, name, fieldName, &value->ivec4[0]);
596 snprintf(fieldName, sizeof(fieldName), "%s[1]", field);
597 _lookupIntValue(description, name, fieldName, &value->ivec4[1]);
598 snprintf(fieldName, sizeof(fieldName), "%s[2]", field);
599 _lookupIntValue(description, name, fieldName, &value->ivec4[2]);
600 snprintf(fieldName, sizeof(fieldName), "%s[3]", field);
601 _lookupIntValue(description, name, fieldName, &value->ivec4[3]);
602 break;
603 case GL_BOOL:
604 value->b = 0;
605 _lookupBoolValue(description, name, field, &value->b);
606 break;
607 case GL_BOOL_VEC2:
608 value->bvec2[0] = 0;
609 value->bvec2[1] = 0;
610 snprintf(fieldName, sizeof(fieldName), "%s[0]", field);
611 _lookupBoolValue(description, name, fieldName, &value->bvec2[0]);
612 snprintf(fieldName, sizeof(fieldName), "%s[1]", field);
613 _lookupBoolValue(description, name, fieldName, &value->bvec2[1]);
614 break;
615 case GL_BOOL_VEC3:
616 value->bvec3[0] = 0;
617 value->bvec3[1] = 0;
618 value->bvec3[2] = 0;
619 snprintf(fieldName, sizeof(fieldName), "%s[0]", field);
620 _lookupBoolValue(description, name, fieldName, &value->bvec3[0]);
621 snprintf(fieldName, sizeof(fieldName), "%s[1]", field);
622 _lookupBoolValue(description, name, fieldName, &value->bvec3[1]);
623 snprintf(fieldName, sizeof(fieldName), "%s[2]", field);
624 _lookupBoolValue(description, name, fieldName, &value->bvec3[2]);
625 break;
626 case GL_BOOL_VEC4:
627 value->bvec4[0] = 0;
628 value->bvec4[1] = 0;
629 value->bvec4[2] = 0;
630 value->bvec4[3] = 0;
631 snprintf(fieldName, sizeof(fieldName), "%s[0]", field);
632 _lookupBoolValue(description, name, fieldName, &value->bvec4[0]);
633 snprintf(fieldName, sizeof(fieldName), "%s[1]", field);
634 _lookupBoolValue(description, name, fieldName, &value->bvec4[1]);
635 snprintf(fieldName, sizeof(fieldName), "%s[2]", field);
636 _lookupBoolValue(description, name, fieldName, &value->bvec4[2]);
637 snprintf(fieldName, sizeof(fieldName), "%s[3]", field);
638 _lookupBoolValue(description, name, fieldName, &value->bvec4[3]);
639 break;
640 }
641}
642
643static bool _loadUniform(struct Configuration* description, size_t pass, struct GBAGLES2Uniform* uniform) {
644 char passId[12];
645 snprintf(passId, sizeof(passId), "pass[%zu]", pass);
646 GLboolean inPass;
647 if (_lookupBoolValue(description, uniform->name, passId, &inPass) && !inPass) {
648 return false;
649 }
650 const char* type = ConfigurationGetValue(description, uniform->name, "type");
651 if (!strcmp(type, "float")) {
652 uniform->type = GL_FLOAT;
653 } else if (!strcmp(type, "float2")) {
654 uniform->type = GL_FLOAT_VEC2;
655 } else if (!strcmp(type, "float3")) {
656 uniform->type = GL_FLOAT_VEC3;
657 } else if (!strcmp(type, "float4")) {
658 uniform->type = GL_FLOAT_VEC4;
659 } else if (!strcmp(type, "float2x2")) {
660 uniform->type = GL_FLOAT_MAT2;
661 } else if (!strcmp(type, "float3x3")) {
662 uniform->type = GL_FLOAT_MAT3;
663 } else if (!strcmp(type, "float4x4")) {
664 uniform->type = GL_FLOAT_MAT4;
665 } else if (!strcmp(type, "int")) {
666 uniform->type = GL_INT;
667 } else if (!strcmp(type, "int2")) {
668 uniform->type = GL_INT_VEC2;
669 } else if (!strcmp(type, "int3")) {
670 uniform->type = GL_INT_VEC3;
671 } else if (!strcmp(type, "int4")) {
672 uniform->type = GL_INT_VEC4;
673 } else if (!strcmp(type, "bool")) {
674 uniform->type = GL_BOOL;
675 } else if (!strcmp(type, "int2")) {
676 uniform->type = GL_BOOL_VEC2;
677 } else if (!strcmp(type, "int3")) {
678 uniform->type = GL_BOOL_VEC3;
679 } else if (!strcmp(type, "int4")) {
680 uniform->type = GL_BOOL_VEC4;
681 } else {
682 return false;
683 }
684 _loadValue(description, uniform->name, uniform->type, "default", &uniform->value);
685 _loadValue(description, uniform->name, uniform->type, "min", &uniform->min);
686 _loadValue(description, uniform->name, uniform->type, "max", &uniform->max);
687 const char* readable = ConfigurationGetValue(description, uniform->name, "readableName");
688 if (readable) {
689 uniform->readableName = strdup(readable);
690 } else {
691 uniform->readableName = 0;
692 }
693 uniform->name = strdup(uniform->name + strlen("uniform."));
694 return true;
695}
696
697bool GBAGLES2ShaderLoad(struct VideoShader* shader, struct VDir* dir) {
698 struct VFile* manifest = dir->openFile(dir, "manifest.ini", O_RDONLY);
699 if (!manifest) {
700 return false;
701 }
702 bool success = false;
703 struct Configuration description;
704 ConfigurationInit(&description);
705 if (ConfigurationReadVFile(&description, manifest)) {
706 int inShaders;
707 success = _lookupIntValue(&description, "shader", "passes", &inShaders);
708 if (inShaders > MAX_PASSES || inShaders < 1) {
709 success = false;
710 }
711 if (success) {
712 struct GBAGLES2Shader* shaderBlock = malloc(sizeof(struct GBAGLES2Shader) * inShaders);
713 int n;
714 for (n = 0; n < inShaders; ++n) {
715 char passName[12];
716 snprintf(passName, sizeof(passName), "pass.%u", n);
717 const char* fs = ConfigurationGetValue(&description, passName, "fragmentShader");
718 const char* vs = ConfigurationGetValue(&description, passName, "vertexShader");
719 if (fs && (fs[0] == '.' || strstr(fs, PATH_SEP))) {
720 success = false;
721 break;
722 }
723 if (vs && (vs[0] == '.' || strstr(vs, PATH_SEP))) {
724 success = false;
725 break;
726 }
727 char* fssrc = 0;
728 char* vssrc = 0;
729 if (fs) {
730 struct VFile* fsf = dir->openFile(dir, fs, O_RDONLY);
731 if (!fsf) {
732 success = false;
733 break;
734 }
735 fssrc = malloc(fsf->size(fsf) + 1);
736 fssrc[fsf->size(fsf)] = '\0';
737 fsf->read(fsf, fssrc, fsf->size(fsf));
738 fsf->close(fsf);
739 }
740 if (vs) {
741 struct VFile* vsf = dir->openFile(dir, vs, O_RDONLY);
742 if (!vsf) {
743 success = false;
744 free(fssrc);
745 break;
746 }
747 vssrc = malloc(vsf->size(vsf) + 1);
748 vssrc[vsf->size(vsf)] = '\0';
749 vsf->read(vsf, vssrc, vsf->size(vsf));
750 vsf->close(vsf);
751 }
752 int width = 0;
753 int height = 0;
754 _lookupIntValue(&description, passName, "width", &width);
755 _lookupIntValue(&description, passName, "height", &height);
756
757 struct GBAGLES2UniformList uniformVector;
758 GBAGLES2UniformListInit(&uniformVector, 0);
759 ConfigurationEnumerateSections(&description, _uniformHandler, &uniformVector);
760 size_t u;
761 for (u = 0; u < GBAGLES2UniformListSize(&uniformVector); ++u) {
762 struct GBAGLES2Uniform* uniform = GBAGLES2UniformListGetPointer(&uniformVector, u);
763 if (!_loadUniform(&description, n, uniform)) {
764 GBAGLES2UniformListShift(&uniformVector, u, 1);
765 --u;
766 }
767 }
768 u = GBAGLES2UniformListSize(&uniformVector);
769 struct GBAGLES2Uniform* uniformBlock = malloc(sizeof(*uniformBlock) * u);
770 memcpy(uniformBlock, GBAGLES2UniformListGetPointer(&uniformVector, 0), sizeof(*uniformBlock) * u);
771 GBAGLES2UniformListDeinit(&uniformVector);
772
773 GBAGLES2ShaderInit(&shaderBlock[n], vssrc, fssrc, width, height, uniformBlock, u);
774 int b = 0;
775 _lookupIntValue(&description, passName, "blend", &b);
776 if (b) {
777 shaderBlock[n].blend = b;
778 }
779 b = 0;
780 _lookupIntValue(&description, passName, "filter", &b);
781 if (b) {
782 shaderBlock[n].filter = b;
783 }
784 free(fssrc);
785 free(vssrc);
786 }
787 if (success) {
788 shader->nPasses = inShaders;
789 shader->passes = shaderBlock;
790 shader->name = ConfigurationGetValue(&description, "shader", "name");
791 if (shader->name) {
792 shader->name = strdup(shader->name);
793 }
794 shader->author = ConfigurationGetValue(&description, "shader", "author");
795 if (shader->author) {
796 shader->author = strdup(shader->author);
797 }
798 shader->description = ConfigurationGetValue(&description, "shader", "description");
799 if (shader->description) {
800 shader->description = strdup(shader->description);
801 }
802 } else {
803 inShaders = n;
804 for (n = 0; n < inShaders; ++n) {
805 GBAGLES2ShaderDeinit(&shaderBlock[n]);
806 }
807 }
808 }
809 }
810 ConfigurationDeinit(&description);
811 return success;
812}
813
814void GBAGLES2ShaderFree(struct VideoShader* shader) {
815 free((void*) shader->name);
816 free((void*) shader->author);
817 free((void*) shader->description);
818 shader->name = 0;
819 shader->author = 0;
820 shader->description = 0;
821 struct GBAGLES2Shader* shaders = shader->passes;
822 size_t n;
823 for (n = 0; n < shader->nPasses; ++n) {
824 GBAGLES2ShaderDeinit(&shaders[n]);
825 size_t u;
826 for (u = 0; u < shaders[n].nUniforms; ++u) {
827 free((void*) shaders[n].uniforms[u].name);
828 free((void*) shaders[n].uniforms[u].readableName);
829 }
830 }
831 free(shaders);
832 shader->passes = 0;
833 shader->nPasses = 0;
834}