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