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