all repos — mgba @ ffe7142d1f847919d3bbd025fb849d1bc674c539

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