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