all repos — mgba @ 89983901f8289119e4ed2b03a800f3e665af16a0

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