all repos — mgba @ a9c94e9d1867d0f1f00547dac4880decfb15405e

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/vfs.h"
 11
 12#define MAX_PASSES 8
 13
 14static const char* const _vertexShader =
 15	"attribute vec4 position;\n"
 16	"varying vec2 texCoord;\n"
 17
 18	"void main() {\n"
 19	"	gl_Position = position;\n"
 20	"	texCoord = (position.st + vec2(1.0, -1.0)) * vec2(0.5, -0.5);\n"
 21	"}";
 22
 23static const char* const _nullVertexShader =
 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 _fragmentShader =
 33	"varying vec2 texCoord;\n"
 34	"uniform sampler2D tex;\n"
 35	"uniform float gamma;\n"
 36	"uniform vec3 scale;\n"
 37	"uniform vec3 bias;\n"
 38
 39	"void main() {\n"
 40	"	vec4 color = texture2D(tex, texCoord);\n"
 41	"	color.a = 1.;\n"
 42	"	color.rgb = scale * pow(color.rgb, vec3(gamma, gamma, gamma)) + bias;\n"
 43	"	gl_FragColor = color;\n"
 44	"}";
 45
 46static const char* const _nullFragmentShader =
 47	"varying vec2 texCoord;\n"
 48	"uniform sampler2D tex;\n"
 49
 50	"void main() {\n"
 51	"	vec4 color = texture2D(tex, texCoord);\n"
 52	"	color.a = 1.;\n"
 53	"	gl_FragColor = color;\n"
 54	"}";
 55
 56static const GLfloat _vertices[] = {
 57	-1.f, -1.f,
 58	-1.f, 1.f,
 59	1.f, 1.f,
 60	1.f, -1.f,
 61};
 62
 63
 64static void GBAGLES2ContextInit(struct VideoBackend* v, WHandle handle) {
 65	UNUSED(handle);
 66	struct GBAGLES2Context* context = (struct GBAGLES2Context*) v;
 67	glGenTextures(1, &context->tex);
 68	glBindTexture(GL_TEXTURE_2D, context->tex);
 69	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
 70	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
 71
 72#ifdef COLOR_16_BIT
 73#ifdef COLOR_5_6_5
 74	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, 0);
 75#else
 76	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);
 77#endif
 78#else
 79	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
 80#endif
 81
 82	glClearColor(0.f, 0.f, 0.f, 1.f);
 83
 84	struct GBAGLES2Uniform* uniforms = malloc(sizeof(struct GBAGLES2Uniform) * 3);
 85	uniforms[0].name = "gamma";
 86	uniforms[0].type = GL_FLOAT;
 87	uniforms[0].value.f = 1.0f;
 88	uniforms[1].name = "scale";
 89	uniforms[1].type = GL_FLOAT_VEC3;
 90	uniforms[1].value.fvec3[0] = 1.0f;
 91	uniforms[1].value.fvec3[1] = 1.0f;
 92	uniforms[1].value.fvec3[2] = 1.0f;
 93	uniforms[2].name = "bias";
 94	uniforms[2].type = GL_FLOAT_VEC3;
 95	uniforms[2].value.fvec3[0] = 0.0f;
 96	uniforms[2].value.fvec3[1] = 0.0f;
 97	uniforms[2].value.fvec3[2] = 0.0f;
 98	GBAGLES2ShaderInit(&context->initialShader, _vertexShader, _fragmentShader, -1, -1, uniforms, 3);
 99	GBAGLES2ShaderInit(&context->finalShader, 0, 0, 0, 0, 0, 0);
100	glDeleteFramebuffers(1, &context->finalShader.fbo);
101	context->finalShader.fbo = 0;
102}
103
104static void GBAGLES2ContextDeinit(struct VideoBackend* v) {
105	struct GBAGLES2Context* context = (struct GBAGLES2Context*) v;
106	glDeleteTextures(1, &context->tex);
107	GBAGLES2ShaderDeinit(&context->initialShader);
108	GBAGLES2ShaderDeinit(&context->finalShader);
109	free(context->initialShader.uniforms);
110}
111
112static void GBAGLES2ContextResized(struct VideoBackend* v, int w, int h) {
113	int drawW = w;
114	int drawH = h;
115	if (v->lockAspectRatio) {
116		if (w * 2 > h * 3) {
117			drawW = h * 3 / 2;
118		} else if (w * 2 < h * 3) {
119			drawH = w * 2 / 3;
120		}
121	}
122	glViewport(0, 0, 240, 160);
123	glClearColor(0.f, 0.f, 0.f, 1.f);
124	glClear(GL_COLOR_BUFFER_BIT);
125	glViewport((w - drawW) / 2, (h - drawH) / 2, drawW, drawH);
126}
127
128static void GBAGLES2ContextClear(struct VideoBackend* v) {
129	UNUSED(v);
130	glClearColor(0.f, 0.f, 0.f, 1.f);
131	glClear(GL_COLOR_BUFFER_BIT);
132}
133
134void _drawShader(struct GBAGLES2Shader* shader) {
135	GLint viewport[4];
136	glBindFramebuffer(GL_FRAMEBUFFER, shader->fbo);
137	if (shader->blend) {
138		glEnable(GL_BLEND);
139		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
140	} else {
141		glDisable(GL_BLEND);
142	}
143	glGetIntegerv(GL_VIEWPORT, viewport);
144	glViewport(0, 0, shader->width ? shader->width : viewport[2], shader->height ? shader->height : viewport[3]);
145	if (!shader->width || !shader->height) {
146		GLint oldTex;
147		glGetIntegerv(GL_TEXTURE_BINDING_2D, &oldTex);
148		glBindTexture(GL_TEXTURE_2D, shader->tex);
149		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, shader->width ? shader->width : viewport[2], shader->height ? shader->height : viewport[3], 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
150		glBindTexture(GL_TEXTURE_2D, oldTex);
151	}
152
153	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, shader->filter ? GL_LINEAR : GL_NEAREST);
154	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, shader->filter ? GL_LINEAR : GL_NEAREST);
155	glUseProgram(shader->program);
156	glUniform1i(shader->texLocation, 0);
157	glVertexAttribPointer(shader->positionLocation, 2, GL_FLOAT, GL_FALSE, 0, _vertices);
158	glEnableVertexAttribArray(shader->positionLocation);
159	size_t u;
160	for (u = 0; u < shader->nUniforms; ++u) {
161		struct GBAGLES2Uniform* uniform = &shader->uniforms[u];
162		switch (uniform->type) {
163		case GL_FLOAT:
164			glUniform1f(uniform->location, uniform->value.f);
165			break;
166		case GL_INT:
167			glUniform1f(uniform->location, uniform->value.i);
168			break;
169		case GL_UNSIGNED_INT:
170			glUniform1f(uniform->location, uniform->value.ui);
171			break;
172		case GL_FLOAT_VEC2:
173			glUniform2fv(uniform->location, 1, uniform->value.fvec2);
174			break;
175		case GL_FLOAT_VEC3:
176			glUniform3fv(uniform->location, 1, uniform->value.fvec3);
177			break;
178		case GL_FLOAT_VEC4:
179			glUniform4fv(uniform->location, 1, uniform->value.fvec4);
180			break;
181		case GL_INT_VEC2:
182			glUniform2iv(uniform->location, 1, uniform->value.ivec2);
183			break;
184		case GL_INT_VEC3:
185			glUniform3iv(uniform->location, 1, uniform->value.ivec3);
186			break;
187		case GL_INT_VEC4:
188			glUniform4iv(uniform->location, 1, uniform->value.ivec4);
189			break;
190		case GL_UNSIGNED_INT_VEC2:
191			glUniform2uiv(uniform->location, 1, uniform->value.uivec2);
192			break;
193		case GL_UNSIGNED_INT_VEC3:
194			glUniform3uiv(uniform->location, 1, uniform->value.uivec3);
195			break;
196		case GL_UNSIGNED_INT_VEC4:
197			glUniform4uiv(uniform->location, 1, uniform->value.uivec4);
198			break;
199		case GL_FLOAT_MAT2:
200			glUniformMatrix2fv(uniform->location, 1, GL_FALSE, uniform->value.fmat2x2);
201			break;
202		case GL_FLOAT_MAT2x3:
203			glUniformMatrix2x3fv(uniform->location, 1, GL_FALSE, uniform->value.fmat2x3);
204			break;
205		case GL_FLOAT_MAT2x4:
206			glUniformMatrix2x4fv(uniform->location, 1, GL_FALSE, uniform->value.fmat2x4);
207			break;
208		case GL_FLOAT_MAT3x2:
209			glUniformMatrix3x2fv(uniform->location, 1, GL_FALSE, uniform->value.fmat3x2);
210			break;
211		case GL_FLOAT_MAT3:
212			glUniformMatrix3fv(uniform->location, 1, GL_FALSE, uniform->value.fmat3x3);
213			break;
214		case GL_FLOAT_MAT3x4:
215			glUniformMatrix3x4fv(uniform->location, 1, GL_FALSE, uniform->value.fmat3x4);
216			break;
217		case GL_FLOAT_MAT4x2:
218			glUniformMatrix2fv(uniform->location, 1, GL_FALSE, uniform->value.fmat4x2);
219			break;
220		case GL_FLOAT_MAT4x3:
221			glUniformMatrix2x3fv(uniform->location, 1, GL_FALSE, uniform->value.fmat4x3);
222			break;
223		case GL_FLOAT_MAT4:
224			glUniformMatrix2x4fv(uniform->location, 1, GL_FALSE, uniform->value.fmat4x4);
225			break;
226		}
227	}
228	glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
229	glBindFramebuffer(GL_FRAMEBUFFER, 0);
230	glBindTexture(GL_TEXTURE_2D, shader->tex);
231	glViewport(viewport[0], viewport[1], viewport[2], viewport[3]);
232}
233
234void GBAGLES2ContextDrawFrame(struct VideoBackend* v) {
235	struct GBAGLES2Context* context = (struct GBAGLES2Context*) v;
236	glActiveTexture(GL_TEXTURE0);
237	glBindTexture(GL_TEXTURE_2D, context->tex);
238
239	context->finalShader.filter = v->filter;
240	_drawShader(&context->initialShader);
241	size_t n;
242	for (n = 0; n < context->nShaders; ++n) {
243		_drawShader(&context->shaders[n]);
244	}
245	_drawShader(&context->finalShader);
246	glUseProgram(0);
247}
248
249void GBAGLES2ContextPostFrame(struct VideoBackend* v, const void* frame) {
250	struct GBAGLES2Context* context = (struct GBAGLES2Context*) v;
251	glBindTexture(GL_TEXTURE_2D, context->tex);
252	glPixelStorei(GL_UNPACK_ROW_LENGTH, 256);
253#ifdef COLOR_16_BIT
254#ifdef COLOR_5_6_5
255	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, frame);
256#else
257	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);
258#endif
259#else
260	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, 0, GL_RGBA, GL_UNSIGNED_BYTE, frame);
261#endif
262	glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
263}
264
265void GBAGLES2ContextCreate(struct GBAGLES2Context* context) {
266	context->d.init = GBAGLES2ContextInit;
267	context->d.deinit = GBAGLES2ContextDeinit;
268	context->d.resized = GBAGLES2ContextResized;
269	context->d.swap = 0;
270	context->d.clear = GBAGLES2ContextClear;
271	context->d.postFrame = GBAGLES2ContextPostFrame;
272	context->d.drawFrame = GBAGLES2ContextDrawFrame;
273	context->d.setMessage = 0;
274	context->d.clearMessage = 0;
275	context->shaders = 0;
276	context->nShaders = 0;
277}
278
279void GBAGLES2ShaderInit(struct GBAGLES2Shader* shader, const char* vs, const char* fs, int width, int height, struct GBAGLES2Uniform* uniforms, size_t nUniforms) {
280	shader->width = width >= 0 ? width : VIDEO_HORIZONTAL_PIXELS;
281	shader->height = height >= 0 ? height : VIDEO_VERTICAL_PIXELS;
282	shader->filter = false;
283	shader->blend = false;
284	shader->uniforms = uniforms;
285	shader->nUniforms = nUniforms;
286	glGenFramebuffers(1, &shader->fbo);
287	glBindFramebuffer(GL_FRAMEBUFFER, shader->fbo);
288
289	glGenTextures(1, &shader->tex);
290	glBindTexture(GL_TEXTURE_2D, shader->tex);
291	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
292	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
293	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
294	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
295	if (shader->width && shader->height) {
296		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, shader->width, shader->height, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
297	}
298
299	glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, shader->tex, 0);
300	shader->program = glCreateProgram();
301	shader->vertexShader = glCreateShader(GL_VERTEX_SHADER);
302	shader->fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
303	if (vs) {
304		glShaderSource(shader->vertexShader, 1, (const GLchar**) &vs, 0);
305	} else {
306		glShaderSource(shader->vertexShader, 1, (const GLchar**) &_nullVertexShader, 0);
307	}
308	if (fs) {
309		glShaderSource(shader->fragmentShader, 1, (const GLchar**) &fs, 0);
310	} else {
311		glShaderSource(shader->fragmentShader, 1, (const GLchar**) &_nullFragmentShader, 0);
312	}
313	glAttachShader(shader->program, shader->vertexShader);
314	glAttachShader(shader->program, shader->fragmentShader);
315	char log[1024];
316	glCompileShader(shader->fragmentShader);
317	glGetShaderInfoLog(shader->fragmentShader, 1024, 0, log);
318	printf("%s\n", log);
319	glCompileShader(shader->vertexShader);
320	glGetShaderInfoLog(shader->vertexShader, 1024, 0, log);
321	printf("%s\n", log);
322	glLinkProgram(shader->program);
323	glGetProgramInfoLog(shader->program, 1024, 0, log);
324	printf("%s\n", log);
325
326	shader->texLocation = glGetUniformLocation(shader->program, "tex");
327	shader->positionLocation = glGetAttribLocation(shader->program, "position");
328	size_t i;
329	for (i = 0; i < shader->nUniforms; ++i) {
330		shader->uniforms[i].location = glGetUniformLocation(shader->program, shader->uniforms[i].name);
331	}
332	glBindFramebuffer(GL_FRAMEBUFFER, 0);
333}
334
335void GBAGLES2ShaderDeinit(struct GBAGLES2Shader* shader) {
336	glDeleteTextures(1, &shader->tex);
337	glDeleteShader(shader->fragmentShader);
338	glDeleteProgram(shader->program);
339	glDeleteFramebuffers(1, &shader->fbo);
340}
341
342void GBAGLES2ShaderAttach(struct GBAGLES2Context* context, struct GBAGLES2Shader* shaders, size_t nShaders) {
343	if (context->shaders) {
344		if (context->shaders == shaders && context->nShaders == nShaders) {
345			return;
346		}
347		GBAGLES2ShaderDetach(context);
348	}
349	context->shaders = shaders;
350	context->nShaders = nShaders;
351}
352
353void GBAGLES2ShaderDetach(struct GBAGLES2Context* context) {
354	if (!context->shaders) {
355		return;
356	}
357	context->shaders = 0;
358}
359
360static bool _lookupIntValue(const struct Configuration* config, const char* section, const char* key, int* out) {
361	const char* charValue = ConfigurationGetValue(config, section, key);
362	if (!charValue) {
363		return false;
364	}
365	char* end;
366	unsigned long value = strtol(charValue, &end, 10);
367	if (*end) {
368		return false;
369	}
370	*out = value;
371	return true;
372}
373
374bool GBAGLES2ShaderLoad(struct GBAGLES2Shader** shaders, size_t* nShaders, struct GBAGLES2ShaderMetadata* metadata, struct VDir* dir) {
375	struct VFile* manifest = dir->openFile(dir, "manifest.ini", O_RDONLY);
376	if (!manifest) {
377		return false;
378	}
379	bool success = false;
380	struct Configuration description;
381	ConfigurationInit(&description);
382	if (ConfigurationReadVFile(&description, manifest)) {
383		int inShaders;
384		success = _lookupIntValue(&description, "shader", "passes", &inShaders);
385		if (inShaders > MAX_PASSES || inShaders < 1) {
386			success = false;
387		}
388		if (success) {
389			if (metadata) {
390				metadata->name = ConfigurationGetValue(&description, "shader", "name");
391				if (metadata->name) {
392					metadata->name = strdup(metadata->name);
393				}
394				metadata->author = ConfigurationGetValue(&description, "shader", "author");
395				if (metadata->author) {
396					metadata->author = strdup(metadata->author);
397				}
398				metadata->description = ConfigurationGetValue(&description, "shader", "description");
399				if (metadata->description) {
400					metadata->description = strdup(metadata->description);
401				}
402			}
403			struct GBAGLES2Shader* shaderBlock = malloc(sizeof(struct GBAGLES2Shader) * inShaders);
404			int n;
405			for (n = 0; n < inShaders; ++n) {
406				char passName[12];
407				snprintf(passName, sizeof(passName), "pass.%u", n);
408				const char* fs = ConfigurationGetValue(&description, passName, "fragmentShader");
409				const char* vs = ConfigurationGetValue(&description, passName, "vertexShader");
410				if (fs && (fs[0] == '.' || strstr(fs, PATH_SEP))) {
411					success = false;
412					break;
413				}
414				if (vs && (vs[0] == '.' || strstr(vs, PATH_SEP))) {
415					success = false;
416					break;
417				}
418				char* fssrc = 0;
419				char* vssrc = 0;
420				if (fs) {
421					struct VFile* fsf = dir->openFile(dir, fs, O_RDONLY);
422					if (!fsf) {
423						success = false;
424						break;
425					}
426					fssrc = malloc(fsf->size(fsf));
427					fsf->read(fsf, fssrc, fsf->size(fsf));
428					fsf->close(fsf);
429				}
430				if (vs) {
431					struct VFile* vsf = dir->openFile(dir, vs, O_RDONLY);
432					if (!vsf) {
433						success = false;
434						free(fssrc);
435						break;
436					}
437					vssrc = malloc(vsf->size(vsf));
438					vsf->read(vsf, vssrc, vsf->size(vsf));
439					vsf->close(vsf);
440				}
441				int width = 0;
442				int height = 0;
443				_lookupIntValue(&description, passName, "width", &width);
444				_lookupIntValue(&description, passName, "height", &height);
445				GBAGLES2ShaderInit(&shaderBlock[n], vssrc, fssrc, width, height, 0, 0);
446				int b = 0;
447				_lookupIntValue(&description, passName, "blend", &b);
448				if (b) {
449					shaderBlock[n].blend = b;
450				}
451				b = 0;
452				_lookupIntValue(&description, passName, "filter", &b);
453				if (b) {
454					shaderBlock[n].filter = b;
455				}
456				free(fssrc);
457				free(vssrc);
458			}
459			if (success) {
460				*nShaders = inShaders;
461				*shaders = shaderBlock;
462			} else {
463				inShaders = n;
464				for (n = 0; n < inShaders; ++n) {
465					GBAGLES2ShaderDeinit(&shaderBlock[n]);
466				}
467			}
468		}
469	}
470	ConfigurationDeinit(&description);
471	return success;
472}
473
474void GBAGLES2ShaderFree(struct GBAGLES2Shader* shaders, size_t nShaders) {
475	size_t n;
476	for (n = 0; n < nShaders; ++n) {
477		GBAGLES2ShaderDeinit(&shaders[n]);
478		size_t u;
479		for (u = 0; u < shaders[n].nUniforms; ++u) {
480			free((void*) shaders[n].uniforms[u].name);
481		}
482	}
483	free(shaders);
484}