all repos — mgba @ 99d07c98c5d03510899c50a67573498900cddfef

mGBA Game Boy Advance Emulator

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

  1/* Copyright (c) 2013-2015 Jeffrey Pfau
  2 *
  3 * This Source Code Form is subject to the terms of the Mozilla Public
  4 * License, v. 2.0. If a copy of the MPL was not distributed with this
  5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6#include "gles2.h"
  7
  8#include <mgba/core/log.h>
  9#include <mgba-util/configuration.h>
 10#include <mgba-util/formatting.h>
 11#include <mgba-util/math.h>
 12#include <mgba-util/vector.h>
 13#include <mgba-util/vfs.h>
 14
 15mLOG_DECLARE_CATEGORY(OPENGL);
 16mLOG_DEFINE_CATEGORY(OPENGL, "OpenGL", "video.ogl");
 17
 18#define MAX_PASSES 8
 19
 20static const GLchar* const _gles2Header =
 21	"#version 100\n"
 22	"precision mediump float;\n";
 23
 24static const GLchar* const _gl3Header =
 25	"#version 120\n";
 26
 27static const char* const _vertexShader =
 28	"attribute vec4 position;\n"
 29	"varying vec2 texCoord;\n"
 30
 31	"void main() {\n"
 32	"	gl_Position = position;\n"
 33	"	texCoord = (position.st + vec2(1.0, -1.0)) * vec2(0.5, -0.5);\n"
 34	"}";
 35
 36static const char* const _nullVertexShader =
 37	"attribute vec4 position;\n"
 38	"varying vec2 texCoord;\n"
 39
 40	"void main() {\n"
 41	"	gl_Position = position;\n"
 42	"	texCoord = (position.st + vec2(1.0, 1.0)) * vec2(0.5, 0.5);\n"
 43	"}";
 44
 45static const char* const _fragmentShader =
 46	"varying vec2 texCoord;\n"
 47	"uniform sampler2D tex;\n"
 48	"uniform float gamma;\n"
 49	"uniform vec3 desaturation;\n"
 50	"uniform vec3 scale;\n"
 51	"uniform vec3 bias;\n"
 52
 53	"void main() {\n"
 54	"	vec4 color = texture2D(tex, texCoord);\n"
 55	"	color.a = 1.;\n"
 56	"	float average = dot(color.rgb, vec3(1.)) / 3.;\n"
 57	"	color.rgb = mix(color.rgb, vec3(average), desaturation);\n"
 58	"	color.rgb = scale * pow(color.rgb, vec3(gamma, gamma, gamma)) + bias;\n"
 59	"	gl_FragColor = color;\n"
 60	"}";
 61
 62static const char* const _nullFragmentShader =
 63	"varying vec2 texCoord;\n"
 64	"uniform sampler2D tex;\n"
 65
 66	"void main() {\n"
 67	"	vec4 color = texture2D(tex, texCoord);\n"
 68	"	color.a = 1.;\n"
 69	"	gl_FragColor = color;\n"
 70	"}";
 71
 72static const GLfloat _vertices[] = {
 73	-1.f, -1.f,
 74	-1.f, 1.f,
 75	1.f, 1.f,
 76	1.f, -1.f,
 77};
 78
 79static void mGLES2ContextInit(struct VideoBackend* v, WHandle handle) {
 80	UNUSED(handle);
 81	struct mGLES2Context* context = (struct mGLES2Context*) v;
 82	glGenTextures(1, &context->tex);
 83	glBindTexture(GL_TEXTURE_2D, context->tex);
 84	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
 85	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
 86
 87	glGenBuffers(1, &context->vbo);
 88	glBindBuffer(GL_ARRAY_BUFFER, context->vbo);
 89	glBufferData(GL_ARRAY_BUFFER, sizeof(_vertices), _vertices, GL_STATIC_DRAW);
 90
 91	glClearColor(0.f, 0.f, 0.f, 1.f);
 92
 93	struct mGLES2Uniform* uniforms = malloc(sizeof(struct mGLES2Uniform) * 4);
 94	uniforms[0].name = "gamma";
 95	uniforms[0].readableName = "Gamma";
 96	uniforms[0].type = GL_FLOAT;
 97	uniforms[0].value.f = 1.0f;
 98	uniforms[0].min.f = 0.1f;
 99	uniforms[0].max.f = 3.0f;
100	uniforms[1].name = "scale";
101	uniforms[1].readableName = "Scale";
102	uniforms[1].type = GL_FLOAT_VEC3;
103	uniforms[1].value.fvec3[0] = 1.0f;
104	uniforms[1].value.fvec3[1] = 1.0f;
105	uniforms[1].value.fvec3[2] = 1.0f;
106	uniforms[1].min.fvec3[0] = -1.0f;
107	uniforms[1].min.fvec3[1] = -1.0f;
108	uniforms[1].min.fvec3[2] = -1.0f;
109	uniforms[1].max.fvec3[0] = 2.0f;
110	uniforms[1].max.fvec3[1] = 2.0f;
111	uniforms[1].max.fvec3[2] = 2.0f;
112	uniforms[2].name = "bias";
113	uniforms[2].readableName = "Bias";
114	uniforms[2].type = GL_FLOAT_VEC3;
115	uniforms[2].value.fvec3[0] = 0.0f;
116	uniforms[2].value.fvec3[1] = 0.0f;
117	uniforms[2].value.fvec3[2] = 0.0f;
118	uniforms[2].min.fvec3[0] = -1.0f;
119	uniforms[2].min.fvec3[1] = -1.0f;
120	uniforms[2].min.fvec3[2] = -1.0f;
121	uniforms[2].max.fvec3[0] = 1.0f;
122	uniforms[2].max.fvec3[1] = 1.0f;
123	uniforms[2].max.fvec3[2] = 1.0f;
124	uniforms[3].name = "desaturation";
125	uniforms[3].readableName = "Desaturation";
126	uniforms[3].type = GL_FLOAT_VEC3;
127	uniforms[3].value.fvec3[0] = 0.0f;
128	uniforms[3].value.fvec3[1] = 0.0f;
129	uniforms[3].value.fvec3[2] = 0.0f;
130	uniforms[3].min.fvec3[0] = 0.0f;
131	uniforms[3].min.fvec3[1] = 0.0f;
132	uniforms[3].min.fvec3[2] = 0.0f;
133	uniforms[3].max.fvec3[0] = 1.0f;
134	uniforms[3].max.fvec3[1] = 1.0f;
135	uniforms[3].max.fvec3[2] = 1.0f;
136	mGLES2ShaderInit(&context->initialShader, _vertexShader, _fragmentShader, -1, -1, false, uniforms, 4);
137	mGLES2ShaderInit(&context->finalShader, 0, 0, 0, 0, false, 0, 0);
138
139	glBindVertexArray(context->initialShader.vao);
140	glBindBuffer(GL_ARRAY_BUFFER, context->vbo);
141	glVertexAttribPointer(context->initialShader.positionLocation, 2, GL_FLOAT, GL_FALSE, 0, NULL);
142	glBindVertexArray(context->finalShader.vao);
143	glBindBuffer(GL_ARRAY_BUFFER, context->vbo);
144	glVertexAttribPointer(context->finalShader.positionLocation, 2, GL_FLOAT, GL_FALSE, 0, NULL);
145	glBindVertexArray(0);
146
147	glDeleteFramebuffers(1, &context->finalShader.fbo);
148	glDeleteTextures(1, &context->finalShader.tex);
149	context->finalShader.fbo = 0;
150	context->finalShader.tex = 0;
151}
152
153static void mGLES2ContextSetDimensions(struct VideoBackend* v, unsigned width, unsigned height) {
154	struct mGLES2Context* context = (struct mGLES2Context*) v;
155	v->width = width;
156	v->height = height;
157
158	glBindTexture(GL_TEXTURE_2D, context->tex);
159#ifdef COLOR_16_BIT
160#ifdef COLOR_5_6_5
161	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, 0);
162#else
163	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV, 0);
164#endif
165#elif defined(__BIG_ENDIAN__)
166	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, 0);
167#else
168	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
169#endif
170}
171
172static void mGLES2ContextDeinit(struct VideoBackend* v) {
173	struct mGLES2Context* context = (struct mGLES2Context*) v;
174	glDeleteTextures(1, &context->tex);
175	glDeleteBuffers(1, &context->vbo);
176	mGLES2ShaderDeinit(&context->initialShader);
177	mGLES2ShaderDeinit(&context->finalShader);
178	free(context->initialShader.uniforms);
179}
180
181static void mGLES2ContextResized(struct VideoBackend* v, unsigned w, unsigned h) {
182	unsigned drawW = w;
183	unsigned drawH = h;
184	if (v->lockAspectRatio) {
185		if (w * v->height > h * v->width) {
186			drawW = h * v->width / v->height;
187		} else if (w * v->height < h * v->width) {
188			drawH = w * v->height / v->width;
189		}
190	}
191	if (v->lockIntegerScaling) {
192		drawW -= drawW % v->width;
193		drawH -= drawH % v->height;
194	}
195	glBindFramebuffer(GL_FRAMEBUFFER, 0);
196	glViewport((w - drawW) / 2, (h - drawH) / 2, drawW, drawH);
197}
198
199static void mGLES2ContextClear(struct VideoBackend* v) {
200	UNUSED(v);
201	glBindFramebuffer(GL_FRAMEBUFFER, 0);
202	glClearColor(0.f, 0.f, 0.f, 1.f);
203	glClear(GL_COLOR_BUFFER_BIT);
204}
205
206void _drawShader(struct mGLES2Context* context, struct mGLES2Shader* shader) {
207	GLint viewport[4];
208	glBindFramebuffer(GL_FRAMEBUFFER, shader->fbo);
209
210	glGetIntegerv(GL_VIEWPORT, viewport);
211	int drawW = shader->width;
212	int drawH = shader->height;
213	int padW = 0;
214	int padH = 0;
215	if (!drawW) {
216		drawW = viewport[2];
217		padW = viewport[0];
218	} else if (shader->width < 0) {
219		drawW = context->d.width * -shader->width;
220	}
221	if (!drawH) {
222		drawH = viewport[3];
223		padH = viewport[1];
224	} else if (shader->height < 0) {
225		drawH = context->d.height * -shader->height;
226	}
227	if (shader->integerScaling) {
228		padW = 0;
229		padH = 0;
230		drawW -= drawW % context->d.width;
231		drawH -= drawH % context->d.height;
232	}
233	glViewport(padW, padH, drawW, drawH);
234	if (shader->blend) {
235		glEnable(GL_BLEND);
236		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
237	} else {
238		glDisable(GL_BLEND);
239		glClear(GL_COLOR_BUFFER_BIT);
240	}
241
242	if (shader->tex && (shader->width <= 0 || shader->height <= 0)) {
243		GLint oldTex;
244		glGetIntegerv(GL_TEXTURE_BINDING_2D, &oldTex);
245		glBindTexture(GL_TEXTURE_2D, shader->tex);
246		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, drawW, drawH, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
247		glBindTexture(GL_TEXTURE_2D, oldTex);
248	}
249
250	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
251	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, shader->filter ? GL_LINEAR : GL_NEAREST);
252	glUseProgram(shader->program);
253	glUniform1i(shader->texLocation, 0);
254	glUniform2f(shader->texSizeLocation, context->d.width - padW, context->d.height - padH);
255	glBindVertexArray(shader->vao);
256	size_t u;
257	for (u = 0; u < shader->nUniforms; ++u) {
258		struct mGLES2Uniform* uniform = &shader->uniforms[u];
259		switch (uniform->type) {
260		case GL_FLOAT:
261			glUniform1f(uniform->location, uniform->value.f);
262			break;
263		case GL_INT:
264			glUniform1i(uniform->location, uniform->value.i);
265			break;
266		case GL_BOOL:
267			glUniform1i(uniform->location, uniform->value.b);
268			break;
269		case GL_FLOAT_VEC2:
270			glUniform2fv(uniform->location, 1, uniform->value.fvec2);
271			break;
272		case GL_FLOAT_VEC3:
273			glUniform3fv(uniform->location, 1, uniform->value.fvec3);
274			break;
275		case GL_FLOAT_VEC4:
276			glUniform4fv(uniform->location, 1, uniform->value.fvec4);
277			break;
278		case GL_INT_VEC2:
279			glUniform2iv(uniform->location, 1, uniform->value.ivec2);
280			break;
281		case GL_INT_VEC3:
282			glUniform3iv(uniform->location, 1, uniform->value.ivec3);
283			break;
284		case GL_INT_VEC4:
285			glUniform4iv(uniform->location, 1, uniform->value.ivec4);
286			break;
287		case GL_BOOL_VEC2:
288			glUniform2i(uniform->location, uniform->value.bvec2[0], uniform->value.bvec2[1]);
289			break;
290		case GL_BOOL_VEC3:
291			glUniform3i(uniform->location, uniform->value.bvec3[0], uniform->value.bvec3[1], uniform->value.bvec3[2]);
292			break;
293		case GL_BOOL_VEC4:
294			glUniform4i(uniform->location, uniform->value.bvec4[0], uniform->value.bvec4[1], uniform->value.bvec4[2], uniform->value.bvec4[3]);
295			break;
296		case GL_FLOAT_MAT2:
297			glUniformMatrix2fv(uniform->location, 1, GL_FALSE, uniform->value.fmat2x2);
298			break;
299		case GL_FLOAT_MAT3:
300			glUniformMatrix3fv(uniform->location, 1, GL_FALSE, uniform->value.fmat3x3);
301			break;
302		case GL_FLOAT_MAT4:
303			glUniformMatrix4fv(uniform->location, 1, GL_FALSE, uniform->value.fmat4x4);
304			break;
305		}
306	}
307	glEnableVertexAttribArray(shader->positionLocation);
308	glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
309	glBindTexture(GL_TEXTURE_2D, shader->tex);
310}
311
312void mGLES2ContextDrawFrame(struct VideoBackend* v) {
313	struct mGLES2Context* context = (struct mGLES2Context*) v;
314	glActiveTexture(GL_TEXTURE0);
315	glBindTexture(GL_TEXTURE_2D, context->tex);
316
317	GLint viewport[4];
318	glGetIntegerv(GL_VIEWPORT, viewport);
319
320	context->finalShader.filter = v->filter;
321	_drawShader(context, &context->initialShader);
322	size_t n;
323	for (n = 0; n < context->nShaders; ++n) {
324		glViewport(0, 0, viewport[2], viewport[3]);
325		_drawShader(context, &context->shaders[n]);
326	}
327	glViewport(viewport[0], viewport[1], viewport[2], viewport[3]);
328	_drawShader(context, &context->finalShader);
329	glBindFramebuffer(GL_FRAMEBUFFER, 0);
330	glUseProgram(0);
331}
332
333void mGLES2ContextPostFrame(struct VideoBackend* v, const void* frame) {
334	struct mGLES2Context* context = (struct mGLES2Context*) v;
335	glBindTexture(GL_TEXTURE_2D, context->tex);
336#ifdef COLOR_16_BIT
337#ifdef COLOR_5_6_5
338	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, v->width, v->height, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, frame);
339#else
340	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, v->width, v->height, 0, GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV, frame);
341#endif
342#elif defined(__BIG_ENDIAN__)
343	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, v->width, v->height, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, frame);
344#else
345	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, v->width, v->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, frame);
346#endif
347}
348
349void mGLES2ContextCreate(struct mGLES2Context* context) {
350	context->d.init = mGLES2ContextInit;
351	context->d.deinit = mGLES2ContextDeinit;
352	context->d.setDimensions = mGLES2ContextSetDimensions;
353	context->d.resized = mGLES2ContextResized;
354	context->d.swap = 0;
355	context->d.clear = mGLES2ContextClear;
356	context->d.postFrame = mGLES2ContextPostFrame;
357	context->d.drawFrame = mGLES2ContextDrawFrame;
358	context->d.setMessage = 0;
359	context->d.clearMessage = 0;
360	context->shaders = 0;
361	context->nShaders = 0;
362}
363
364void mGLES2ShaderInit(struct mGLES2Shader* shader, const char* vs, const char* fs, int width, int height, bool integerScaling, struct mGLES2Uniform* uniforms, size_t nUniforms) {
365	shader->width = width;
366	shader->height = height;
367	shader->integerScaling = integerScaling;
368	shader->filter = false;
369	shader->blend = false;
370	shader->uniforms = uniforms;
371	shader->nUniforms = nUniforms;
372	glGenFramebuffers(1, &shader->fbo);
373	glBindFramebuffer(GL_FRAMEBUFFER, shader->fbo);
374
375	glGenTextures(1, &shader->tex);
376	glBindTexture(GL_TEXTURE_2D, shader->tex);
377	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
378	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
379	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
380	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
381	if (shader->width > 0 && shader->height > 0) {
382		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, shader->width, shader->height, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
383	}
384
385	glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, shader->tex, 0);
386	shader->program = glCreateProgram();
387	shader->vertexShader = glCreateShader(GL_VERTEX_SHADER);
388	shader->fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
389	const GLchar* shaderBuffer[2];
390	const GLubyte* version = glGetString(GL_VERSION);
391	if (strncmp((const char*) version, "OpenGL ES ", strlen("OpenGL ES "))) {
392		shaderBuffer[0] = _gl3Header;
393	} else {
394		shaderBuffer[0] = _gles2Header;
395	}
396	if (vs) {
397		shaderBuffer[1] = vs;
398	} else {
399		shaderBuffer[1] = _nullVertexShader;
400	}
401	glShaderSource(shader->vertexShader, 2, shaderBuffer, 0);
402
403	if (fs) {
404		shaderBuffer[1] = fs;
405	} else {
406		shaderBuffer[1] = _nullFragmentShader;
407	}
408	glShaderSource(shader->fragmentShader, 2, shaderBuffer, 0);
409
410	glAttachShader(shader->program, shader->vertexShader);
411	glAttachShader(shader->program, shader->fragmentShader);
412	char log[1024];
413	glCompileShader(shader->fragmentShader);
414	glGetShaderInfoLog(shader->fragmentShader, 1024, 0, log);
415	if (log[0]) {
416		mLOG(OPENGL, ERROR, "%s\n", log);
417	}
418	glCompileShader(shader->vertexShader);
419	glGetShaderInfoLog(shader->vertexShader, 1024, 0, log);
420	if (log[0]) {
421		mLOG(OPENGL, ERROR, "%s\n", log);
422	}
423	glLinkProgram(shader->program);
424	glGetProgramInfoLog(shader->program, 1024, 0, log);
425	if (log[0]) {
426		mLOG(OPENGL, ERROR, "%s\n", log);
427	}
428
429	shader->texLocation = glGetUniformLocation(shader->program, "tex");
430	shader->texSizeLocation = glGetUniformLocation(shader->program, "texSize");
431	shader->positionLocation = glGetAttribLocation(shader->program, "position");
432	size_t i;
433	for (i = 0; i < shader->nUniforms; ++i) {
434		shader->uniforms[i].location = glGetUniformLocation(shader->program, shader->uniforms[i].name);
435	}
436
437	glGenVertexArrays(1, &shader->vao);
438
439	glBindFramebuffer(GL_FRAMEBUFFER, 0);
440}
441
442void mGLES2ShaderDeinit(struct mGLES2Shader* shader) {
443	glDeleteTextures(1, &shader->tex);
444	glDeleteShader(shader->fragmentShader);
445	glDeleteProgram(shader->program);
446	glDeleteFramebuffers(1, &shader->fbo);
447	glDeleteVertexArrays(1, &shader->vao);
448}
449
450void mGLES2ShaderAttach(struct mGLES2Context* context, struct mGLES2Shader* shaders, size_t nShaders) {
451	if (context->shaders) {
452		if (context->shaders == shaders && context->nShaders == nShaders) {
453			return;
454		}
455		mGLES2ShaderDetach(context);
456	}
457	context->shaders = shaders;
458	context->nShaders = nShaders;
459	size_t i;
460	for (i = 0; i < nShaders; ++i) {
461		glBindFramebuffer(GL_FRAMEBUFFER, context->shaders[i].fbo);
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}