all repos — mgba @ 67905d281bfecbb06f51f2ca5ac939df378734a5

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