all repos — mgba @ 3797e1e5f6b001fa1cb2855304c752d81ef25ff6

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