all repos — mgba @ 2d930cbc610b28d82746bbcd121efcd47be47592

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