all repos — mgba @ 7436a5afef25fcb9160aa1884cc31503bc739f2b

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