all repos — mgba @ be2641c77b4a438e0db487bc82b43bc27a26e0c2

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