all repos — mgba @ 6cf255daf45ac5f9f778d1c4a56cf07a70f48316

mGBA Game Boy Advance Emulator

src/platform/opengl/gles2.c (view raw)

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