all repos — mgba @ 7e6f9f20925afc666e964d8a599331ebbf95ced9

mGBA Game Boy Advance Emulator

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