all repos — mgba @ e456798712bb3c89462a2080033982675a52ade2

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