all repos — mgba @ 5c90abd294cb969722808b9513e3a7ce58db1da7

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