all repos — mgba @ d25ba2ec59b940940950aa1330fe563bced278bd

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