all repos — mgba @ 43180dca1df957a9e82f08edbf02cd19b00248e8

mGBA Game Boy Advance Emulator

src/gba/renderers/gl.c (view raw)

   1/* Copyright (c) 2013-2019 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 <mgba/internal/gba/renderers/gl.h>
   7
   8#if defined(BUILD_GLES2) || defined(BUILD_GLES3)
   9
  10#include <mgba/core/cache-set.h>
  11#include <mgba/internal/arm/macros.h>
  12#include <mgba/internal/gba/io.h>
  13#include <mgba/internal/gba/renderers/cache-set.h>
  14#include <mgba-util/memory.h>
  15
  16static void GBAVideoGLRendererInit(struct GBAVideoRenderer* renderer);
  17static void GBAVideoGLRendererDeinit(struct GBAVideoRenderer* renderer);
  18static void GBAVideoGLRendererReset(struct GBAVideoRenderer* renderer);
  19static void GBAVideoGLRendererWriteVRAM(struct GBAVideoRenderer* renderer, uint32_t address);
  20static void GBAVideoGLRendererWriteOAM(struct GBAVideoRenderer* renderer, uint32_t oam);
  21static void GBAVideoGLRendererWritePalette(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value);
  22static uint16_t GBAVideoGLRendererWriteVideoRegister(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value);
  23static void GBAVideoGLRendererDrawScanline(struct GBAVideoRenderer* renderer, int y);
  24static void GBAVideoGLRendererFinishFrame(struct GBAVideoRenderer* renderer);
  25static void GBAVideoGLRendererGetPixels(struct GBAVideoRenderer* renderer, size_t* stride, const void** pixels);
  26static void GBAVideoGLRendererPutPixels(struct GBAVideoRenderer* renderer, size_t stride, const void* pixels);
  27
  28static void GBAVideoGLRendererUpdateDISPCNT(struct GBAVideoGLRenderer* renderer);
  29static void GBAVideoGLRendererWriteBGCNT(struct GBAVideoGLBackground* bg, uint16_t value);
  30static void GBAVideoGLRendererWriteBGX_LO(struct GBAVideoGLBackground* bg, uint16_t value);
  31static void GBAVideoGLRendererWriteBGX_HI(struct GBAVideoGLBackground* bg, uint16_t value);
  32static void GBAVideoGLRendererWriteBGY_LO(struct GBAVideoGLBackground* bg, uint16_t value);
  33static void GBAVideoGLRendererWriteBGY_HI(struct GBAVideoGLBackground* bg, uint16_t value);
  34static void GBAVideoGLRendererWriteBLDCNT(struct GBAVideoGLRenderer* renderer, uint16_t value);
  35
  36static void GBAVideoGLRendererDrawSprite(struct GBAVideoGLRenderer* renderer, struct GBAObj* sprite, int y, int spriteY);
  37static void GBAVideoGLRendererDrawBackgroundMode0(struct GBAVideoGLRenderer* renderer, struct GBAVideoGLBackground* background, int y);
  38static void GBAVideoGLRendererDrawBackgroundMode2(struct GBAVideoGLRenderer* renderer, struct GBAVideoGLBackground* background, int y);
  39static void GBAVideoGLRendererDrawBackgroundMode3(struct GBAVideoGLRenderer* renderer, struct GBAVideoGLBackground* background, int y);
  40static void GBAVideoGLRendererDrawBackgroundMode4(struct GBAVideoGLRenderer* renderer, struct GBAVideoGLBackground* background, int y);
  41static void GBAVideoGLRendererDrawBackgroundMode5(struct GBAVideoGLRenderer* renderer, struct GBAVideoGLBackground* background, int y);
  42static void GBAVideoGLRendererDrawWindow(struct GBAVideoGLRenderer* renderer, int y);
  43
  44static void _finalizeLayers(struct GBAVideoGLRenderer* renderer);
  45
  46#define TEST_LAYER_ENABLED(X) !renderer->disableBG[X] && glRenderer->bg[X].enabled == 4
  47
  48struct GBAVideoGLUniform {
  49	const char* name;
  50	int type;
  51};
  52
  53static const GLchar* const _gl3Header =
  54	"#version 130\n";
  55
  56static const char* const _vertexShader =
  57	"in vec2 position;\n"
  58	"uniform ivec2 loc;\n"
  59	"uniform ivec2 maxPos;\n"
  60	"out vec2 texCoord;\n"
  61
  62	"void main() {\n"
  63	"	vec2 local = vec2(position.x, float(position.y * loc.x + loc.y) / abs(maxPos.y));\n"
  64	"	gl_Position = vec4((local * 2. - 1.) * sign(maxPos), 0., 1.);\n"
  65	"	texCoord = local * abs(maxPos);\n"
  66	"}";
  67
  68static const char* const _renderTile16 =
  69	"vec4 renderTile(int tile, int paletteId, ivec2 localCoord) {\n"
  70	"	int address = charBase + tile * 16 + (localCoord.x >> 2) + (localCoord.y << 1);\n"
  71	"	vec4 halfrow = texelFetch(vram, ivec2(address & 255, address >> 8), 0);\n"
  72	"	int entry = int(halfrow[3 - (localCoord.x & 3)] * 15.9);\n"
  73	"	vec4 color = texelFetch(palette, ivec2(entry, paletteId), 0);\n"
  74	"	if (entry == 0) {\n"
  75	"		discard;\n"
  76	"	}\n"
  77	"	color.a = 1;\n"
  78	"	return color;\n"
  79	"}";
  80
  81static const char* const _renderTile256 =
  82	"vec4 renderTile(int tile, int paletteId, ivec2 localCoord) {\n"
  83	"	int address = charBase + tile * 32 + (localCoord.x >> 1) + (localCoord.y << 2);\n"
  84	"	vec4 halfrow = texelFetch(vram, ivec2(address & 255, address >> 8), 0);\n"
  85	"	int entry = int(halfrow[3 - 2 * (localCoord.x & 1)] * 15.9);\n"
  86	"	int pal2 = int(halfrow[2 - 2 * (localCoord.x & 1)] * 15.9);\n"
  87	"	vec4 color = texelFetch(palette, ivec2(entry, pal2 + (paletteId & 16)), 0);\n"
  88	"	if ((pal2 | entry) == 0) {\n"
  89	"		discard;\n"
  90	"	}\n"
  91	"	color.a = 1.;\n"
  92	"	return color;\n"
  93	"}";
  94
  95static const struct GBAVideoGLUniform _uniformsMode0[] = {
  96	{ "loc", GBA_GL_VS_LOC, },
  97	{ "maxPos", GBA_GL_VS_MAXPOS, },
  98	{ "vram", GBA_GL_BG_VRAM, },
  99	{ "palette", GBA_GL_BG_PALETTE, },
 100	{ "screenBase", GBA_GL_BG_SCREENBASE, },
 101	{ "charBase", GBA_GL_BG_CHARBASE, },
 102	{ "size", GBA_GL_BG_SIZE, },
 103	{ "offset", GBA_GL_BG_OFFSET, },
 104	{ "inflags", GBA_GL_BG_INFLAGS, },
 105	{ 0 }
 106};
 107
 108static const char* const _renderMode0 =
 109	"in vec2 texCoord;\n"
 110	"uniform sampler2D vram;\n"
 111	"uniform sampler2D palette;\n"
 112	"uniform int screenBase;\n"
 113	"uniform int charBase;\n"
 114	"uniform int size;\n"
 115	"uniform ivec2 offset;\n"
 116	"uniform ivec4 inflags;\n"
 117	"out vec4 color;\n"
 118	"out vec4 flags;\n"
 119	"const vec4 flagCoeff = vec4(32., 32., 16., 16.);\n"
 120
 121	"vec4 renderTile(int tile, int paletteId, ivec2 localCoord);\n"
 122
 123	"void main() {\n"
 124	"	ivec2 coord = ivec2(texCoord) + offset;\n"
 125	"	if ((size & 1) == 1) {\n"
 126	"		coord.y += coord.x & 256;\n"
 127	"	}\n"
 128	"	coord.x &= 255;\n"
 129	"	int mapAddress = screenBase + (coord.x >> 3) + (coord.y >> 3) * 32;\n"
 130	"	vec4 map = texelFetch(vram, ivec2(mapAddress & 255, mapAddress >> 8), 0);\n"
 131	"	int tileFlags = int(map.g * 15.9);\n"
 132	"	if ((tileFlags & 4) == 4) {\n"
 133	"		coord.x ^= 7;\n"
 134	"	}\n"
 135	"	if ((tileFlags & 8) == 8) {\n"
 136	"		coord.y ^= 7;\n"
 137	"	}\n"
 138	"	int tile = int(map.a * 15.9) + int(map.b * 15.9) * 16 + (tileFlags & 0x3) * 256;\n"
 139	"	color = renderTile(tile, int(map.r * 15.9), coord & 7);\n"
 140	"	flags = inflags / flagCoeff;\n"
 141	"}";
 142
 143static const char* const _fetchTileOverflow =
 144	"vec4 fetchTile(ivec2 coord) {\n"
 145	"	int sizeAdjusted = (0x8000 << size) - 1;\n"
 146	"	coord &= sizeAdjusted;\n"
 147	"	return renderTile(coord);\n"
 148	"}";
 149
 150static const char* const _fetchTileNoOverflow =
 151	"vec4 fetchTile(ivec2 coord) {\n"
 152	"	int sizeAdjusted = (0x8000 << size) - 1;\n"
 153	"	ivec2 outerCoord = coord & ~sizeAdjusted;\n"
 154	"	if ((outerCoord.x | outerCoord.y) != 0) {\n"
 155	"		discard;\n"
 156	"	}\n"
 157	"	return renderTile(coord);\n"
 158	"}";
 159
 160static const struct GBAVideoGLUniform _uniformsMode2[] = {
 161	{ "loc", GBA_GL_VS_LOC, },
 162	{ "maxPos", GBA_GL_VS_MAXPOS, },
 163	{ "vram", GBA_GL_BG_VRAM, },
 164	{ "palette", GBA_GL_BG_PALETTE, },
 165	{ "screenBase", GBA_GL_BG_SCREENBASE, },
 166	{ "charBase", GBA_GL_BG_CHARBASE, },
 167	{ "size", GBA_GL_BG_SIZE, },
 168	{ "inflags", GBA_GL_BG_INFLAGS, },
 169	{ "offset", GBA_GL_BG_OFFSET, },
 170	{ "transform", GBA_GL_BG_TRANSFORM, },
 171	{ "range", GBA_GL_BG_RANGE, },
 172	{ 0 }
 173};
 174
 175static const char* const _renderMode2 =
 176	"in vec2 texCoord;\n"
 177	"uniform sampler2D vram;\n"
 178	"uniform sampler2D palette;\n"
 179	"uniform int screenBase;\n"
 180	"uniform int charBase;\n"
 181	"uniform int size;\n"
 182	"uniform ivec4 inflags;\n"
 183	"uniform ivec2[4] offset;\n"
 184	"uniform ivec2[4] transform;\n"
 185	"uniform vec2 range;\n"
 186	"out vec4 color;\n"
 187	"out vec4 flags;\n"
 188	"const vec4 flagCoeff = vec4(32., 32., 16., 16.);\n"
 189	"precision highp float;\n"
 190	"precision highp int;\n"
 191
 192	"vec4 fetchTile(ivec2 coord);\n"
 193
 194	"vec4 renderTile(ivec2 coord) {\n"
 195	"	int map = (coord.x >> 11) + (((coord.y >> 7) & 0x7F0) << size);\n"
 196	"	int mapAddress = screenBase + (map >> 1);\n"
 197	"	vec4 twomaps = texelFetch(vram, ivec2(mapAddress & 255, mapAddress >> 8), 0);\n"
 198	"	int tile = int(twomaps[3 - 2 * (map & 1)] * 15.9) + int(twomaps[2 - 2 * (map & 1)] * 15.9) * 16;\n"
 199	"	int address = charBase + tile * 32 + ((coord.x >> 9) & 3) + ((coord.y >> 6) & 0x1C);\n"
 200	"	vec4 halfrow = texelFetch(vram, ivec2(address & 255, address >> 8), 0);\n"
 201	"	int entry = int(halfrow[3 - ((coord.x >> 7) & 2)] * 15.9);\n"
 202	"	int pal2 = int(halfrow[2 - ((coord.x >> 7) & 2)] * 15.9);\n"
 203	"	vec4 color = texelFetch(palette, ivec2(entry, pal2), 0);\n"
 204	"	if ((pal2 | entry) == 0) {\n"
 205	"		discard;\n"
 206	"	}\n"
 207	"	color.a = 1.;\n"
 208	"	return color;\n"
 209	"}\n"
 210
 211	"vec2 interpolate(ivec2 arr[4], float x) {\n"
 212	"	float x1m = 1. - x;\n"
 213	"	return x1m * x1m * x1m * arr[0] +"
 214		"  3 * x1m * x1m * x   * arr[1] +"
 215		"  3 * x1m * x   * x   * arr[2] +"
 216		"      x   * x   * x   * arr[3];\n"
 217	"}\n"
 218
 219	"void main() {\n"
 220	"	float y = texCoord.y - range.x;\n"
 221	"	float lin = 0.5 - y / range.y * 0.25;\n"
 222	"	vec2 mixedTransform = interpolate(transform, lin);\n"
 223	"	vec2 mixedOffset = interpolate(offset, lin);\n"
 224	"	color = fetchTile(ivec2(mixedTransform * texCoord.x + mixedOffset));\n"
 225	"	flags = inflags / flagCoeff;\n"
 226	"}";
 227
 228static const struct GBAVideoGLUniform _uniformsObj[] = {
 229	{ "loc", GBA_GL_VS_LOC, },
 230	{ "maxPos", GBA_GL_VS_MAXPOS, },
 231	{ "vram", GBA_GL_OBJ_VRAM, },
 232	{ "palette", GBA_GL_OBJ_PALETTE, },
 233	{ "charBase", GBA_GL_OBJ_CHARBASE, },
 234	{ "stride", GBA_GL_OBJ_STRIDE, },
 235	{ "localPalette", GBA_GL_OBJ_LOCALPALETTE, },
 236	{ "inflags", GBA_GL_OBJ_INFLAGS, },
 237	{ "transform", GBA_GL_OBJ_TRANSFORM, },
 238	{ "dims", GBA_GL_OBJ_DIMS, },
 239	{ "objwin", GBA_GL_OBJ_OBJWIN, },
 240	{ 0 }
 241};
 242
 243static const char* const _renderObj =
 244	"in vec2 texCoord;\n"
 245	"uniform sampler2D vram;\n"
 246	"uniform sampler2D palette;\n"
 247	"uniform int charBase;\n"
 248	"uniform int stride;\n"
 249	"uniform int localPalette;\n"
 250	"uniform ivec4 inflags;\n"
 251	"uniform mat2x2 transform;\n"
 252	"uniform ivec4 dims;\n"
 253	"uniform vec3 objwin;\n"
 254	"out vec4 color;\n"
 255	"out vec4 flags;\n"
 256	"out vec2 window;\n"
 257	"const vec4 flagCoeff = vec4(32., 32., 16., 16.);\n"
 258
 259	"vec4 renderTile(int tile, int paletteId, ivec2 localCoord);\n"
 260
 261	"void main() {\n"
 262	"	ivec2 coord = ivec2(transform * (texCoord - dims.zw / 2) + dims.xy / 2);\n"
 263	"	if ((coord & ~(dims.xy - 1)) != ivec2(0, 0)) {\n"
 264	"		discard;\n"
 265	"	}\n"
 266	"	vec4 pix = renderTile((coord.x >> 3) + (coord.y >> 3) * stride, 16 + localPalette, coord & 7);\n"
 267	"	if (objwin.x > 0) {\n"
 268	"		pix.a = 0;\n"
 269	"	}\n"
 270	"	color = pix;\n"
 271	"	flags = inflags / flagCoeff;\n"
 272	"	window = objwin.yz;\n"
 273	"}";
 274
 275static const struct GBAVideoGLUniform _uniformsFinalize[] = {
 276	{ "loc", GBA_GL_VS_LOC, },
 277	{ "maxPos", GBA_GL_VS_MAXPOS, },
 278	{ "scale", GBA_GL_FINALIZE_SCALE, },
 279	{ "layers", GBA_GL_FINALIZE_LAYERS, },
 280	{ "flags", GBA_GL_FINALIZE_FLAGS, },
 281	{ "window", GBA_GL_FINALIZE_WINDOW, },
 282	{ "backdrop", GBA_GL_FINALIZE_BACKDROP, },
 283	{ "backdropFlags", GBA_GL_FINALIZE_BACKDROPFLAGS, },
 284	{ 0 }
 285};
 286
 287static const char* const _finalize =
 288	"in vec2 texCoord;\n"
 289	"uniform int scale;\n"
 290	"uniform sampler2D layers[5];\n"
 291	"uniform sampler2D flags[5];\n"
 292	"uniform sampler2D window;\n"
 293	"uniform vec4 backdrop;\n"
 294	"uniform vec4 backdropFlags;\n"
 295	"const vec4 flagCoeff = vec4(32., 32., 16., 16.);\n"
 296	"out vec4 color;\n"
 297
 298	"void composite(vec4 pixel, ivec4 flags, inout vec4 topPixel, inout ivec4 topFlags, inout vec4 bottomPixel, inout ivec4 bottomFlags) {\n"
 299	"	if (pixel.a == 0) {\n"
 300	"		return;\n"
 301	"	}\n"
 302	"	if (flags.x >= topFlags.x) {\n"
 303	"		if (flags.x >= bottomFlags.x) {\n"
 304	"			return;\n"
 305	"		}\n"
 306	"		bottomFlags = flags;\n"
 307	"		bottomPixel = pixel;\n"
 308	"	} else {\n"
 309	"		bottomFlags = topFlags;\n"
 310	"		topFlags = flags;\n"
 311	"		bottomPixel = topPixel;\n"
 312	"		topPixel = pixel;\n"
 313	"	}\n"
 314	"}\n"
 315
 316	"void main() {\n"
 317	"	vec4 windowFlags = texelFetch(window, ivec2(texCoord * scale), 0);\n"
 318	"	int layerWindow = int(windowFlags.x * 32) | int(windowFlags.y * 512);\n"
 319	"	vec4 topPixel = backdrop;\n"
 320	"	vec4 bottomPixel = backdrop;\n"
 321	"	ivec4 topFlags = ivec4(backdropFlags * flagCoeff);\n"
 322	"	ivec4 bottomFlags = ivec4(backdropFlags * flagCoeff);\n"
 323	"	if ((layerWindow & 1) == 0) {\n"
 324	"		vec4 pix = texelFetch(layers[0], ivec2(texCoord * scale), 0);\n"
 325	"		ivec4 inflags = ivec4(texelFetch(flags[0], ivec2(texCoord * scale), 0) * flagCoeff);\n"
 326	"		composite(pix, inflags, topPixel, topFlags, bottomPixel, bottomFlags);\n"
 327	"	}\n"
 328	"	if ((layerWindow & 2) == 0) {\n"
 329	"		vec4 pix = texelFetch(layers[1], ivec2(texCoord * scale), 0);\n"
 330	"		ivec4 inflags = ivec4(texelFetch(flags[1], ivec2(texCoord * scale), 0) * flagCoeff);\n"
 331	"		composite(pix, inflags, topPixel, topFlags, bottomPixel, bottomFlags);\n"
 332	"	}\n"
 333	"	if ((layerWindow & 4) == 0) {\n"
 334	"		vec4 pix = texelFetch(layers[2], ivec2(texCoord * scale), 0);\n"
 335	"		ivec4 inflags = ivec4(texelFetch(flags[2], ivec2(texCoord * scale), 0) * flagCoeff);\n"
 336	"		composite(pix, inflags, topPixel, topFlags, bottomPixel, bottomFlags);\n"
 337	"	}\n"
 338	"	if ((layerWindow & 8) == 0) {\n"
 339	"		vec4 pix = texelFetch(layers[3], ivec2(texCoord * scale), 0);\n"
 340	"		ivec4 inflags = ivec4(texelFetch(flags[3], ivec2(texCoord * scale), 0) * flagCoeff);\n"
 341	"		composite(pix, inflags, topPixel, topFlags, bottomPixel, bottomFlags);\n"
 342	"	}\n"
 343	"	if ((layerWindow & 32) != 0) {\n"
 344	"		topFlags.y &= ~1;\n"
 345	"	}\n"
 346	"	if ((layerWindow & 16) == 0) {\n"
 347	"		vec4 pix = texelFetch(layers[4], ivec2(texCoord * scale), 0);\n"
 348	"		ivec4 inflags = ivec4(texelFetch(flags[4], ivec2(texCoord * scale), 0) * flagCoeff);\n"
 349	"		composite(pix, inflags, topPixel, topFlags, bottomPixel, bottomFlags);\n"
 350	"	}\n"
 351	"	if ((topFlags.y & 13) == 5) {\n"
 352	"		if ((bottomFlags.y & 2) == 2) {\n"
 353	"			topPixel *= topFlags.z / 16.;\n"
 354	"			topPixel += bottomPixel * bottomFlags.w / 16.;\n"
 355	"		}\n"
 356	"	} else if ((topFlags.y & 13) == 9) {\n"
 357	"		topPixel += (1. - topPixel) * windowFlags.z;\n"
 358	"	} else if ((topFlags.y & 13) == 13) {\n"
 359	"		topPixel -= topPixel * windowFlags.z;\n"
 360	"	}\n"
 361	"	color = topPixel;\n"
 362	"}";
 363
 364static const GLint _vertices[] = {
 365	0, 0,
 366	0, 1,
 367	1, 1,
 368	1, 0,
 369};
 370
 371void GBAVideoGLRendererCreate(struct GBAVideoGLRenderer* renderer) {
 372	renderer->d.init = GBAVideoGLRendererInit;
 373	renderer->d.reset = GBAVideoGLRendererReset;
 374	renderer->d.deinit = GBAVideoGLRendererDeinit;
 375	renderer->d.writeVideoRegister = GBAVideoGLRendererWriteVideoRegister;
 376	renderer->d.writeVRAM = GBAVideoGLRendererWriteVRAM;
 377	renderer->d.writeOAM = GBAVideoGLRendererWriteOAM;
 378	renderer->d.writePalette = GBAVideoGLRendererWritePalette;
 379	renderer->d.drawScanline = GBAVideoGLRendererDrawScanline;
 380	renderer->d.finishFrame = GBAVideoGLRendererFinishFrame;
 381	renderer->d.getPixels = GBAVideoGLRendererGetPixels;
 382	renderer->d.putPixels = GBAVideoGLRendererPutPixels;
 383
 384	renderer->d.disableBG[0] = false;
 385	renderer->d.disableBG[1] = false;
 386	renderer->d.disableBG[2] = false;
 387	renderer->d.disableBG[3] = false;
 388	renderer->d.disableOBJ = false;
 389
 390	renderer->scale = 1;
 391}
 392
 393void _compileShader(struct GBAVideoGLRenderer* glRenderer, struct GBAVideoGLShader* shader, const char** shaderBuffer, int shaderBufferLines, GLuint vs, const struct GBAVideoGLUniform* uniforms, char* log) {
 394	GLuint program = glCreateProgram();
 395	shader->program = program;
 396
 397	GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
 398	glAttachShader(program, vs);
 399	glAttachShader(program, fs);
 400	glShaderSource(fs, shaderBufferLines, shaderBuffer, 0);
 401	glCompileShader(fs);
 402	glGetShaderInfoLog(fs, 1024, 0, log);
 403	if (log[0]) {
 404		mLOG(GBA_VIDEO, ERROR, "Fragment shader compilation failure: %s", log);
 405	}
 406	glLinkProgram(program);
 407	glGetProgramInfoLog(program, 1024, 0, log);
 408	if (log[0]) {
 409		mLOG(GBA_VIDEO, ERROR, "Program link failure: %s", log);
 410	}
 411	glDeleteShader(fs);
 412#ifndef BUILD_GLES3
 413	glBindFragDataLocation(program, 0, "color");
 414	glBindFragDataLocation(program, 1, "flags");
 415#endif
 416
 417	glGenVertexArrays(1, &shader->vao);
 418	glBindVertexArray(shader->vao);
 419	glBindBuffer(GL_ARRAY_BUFFER, glRenderer->vbo);
 420	GLuint positionLocation = glGetAttribLocation(program, "position");
 421	glVertexAttribPointer(positionLocation, 2, GL_INT, GL_FALSE, 0, NULL);
 422
 423	size_t i;
 424	for (i = 0; uniforms[i].name; ++i) {
 425		shader->uniforms[uniforms[i].type] = glGetUniformLocation(program, uniforms[i].name);
 426	}
 427}
 428
 429static void _initFramebufferTexture(GLuint tex, GLenum format, GLenum attachment, int scale) {
 430	glBindTexture(GL_TEXTURE_2D, tex);
 431	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
 432	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
 433	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
 434	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
 435	glTexImage2D(GL_TEXTURE_2D, 0, format, GBA_VIDEO_HORIZONTAL_PIXELS * scale, GBA_VIDEO_VERTICAL_PIXELS * scale, 0, format, GL_UNSIGNED_BYTE, 0);
 436	glFramebufferTexture2D(GL_FRAMEBUFFER, attachment, GL_TEXTURE_2D, tex, 0);	
 437}
 438
 439void GBAVideoGLRendererInit(struct GBAVideoRenderer* renderer) {
 440	struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;
 441	glRenderer->temporaryBuffer = NULL;
 442
 443	glGenFramebuffers(GBA_GL_FBO_MAX, glRenderer->fbo);
 444	glGenTextures(GBA_GL_TEX_MAX, glRenderer->layers);
 445
 446	glGenTextures(1, &glRenderer->paletteTex);
 447	glBindTexture(GL_TEXTURE_2D, glRenderer->paletteTex);
 448	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
 449	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
 450
 451	glGenTextures(1, &glRenderer->vramTex);
 452	glBindTexture(GL_TEXTURE_2D, glRenderer->vramTex);
 453	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
 454	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
 455	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA4, 256, 192, 0, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, 0);
 456
 457	glBindFramebuffer(GL_FRAMEBUFFER, glRenderer->fbo[GBA_GL_FBO_OBJ]);
 458	_initFramebufferTexture(glRenderer->layers[GBA_GL_TEX_OBJ_COLOR], GL_RGBA, GL_COLOR_ATTACHMENT0, glRenderer->scale);
 459	_initFramebufferTexture(glRenderer->layers[GBA_GL_TEX_OBJ_FLAGS], GL_RGBA, GL_COLOR_ATTACHMENT1, glRenderer->scale);
 460
 461	glBindFramebuffer(GL_FRAMEBUFFER, glRenderer->fbo[GBA_GL_FBO_WINDOW]);
 462	_initFramebufferTexture(glRenderer->layers[GBA_GL_TEX_WINDOW], GL_RGB, GL_COLOR_ATTACHMENT0, glRenderer->scale);
 463
 464	glBindFramebuffer(GL_FRAMEBUFFER, glRenderer->fbo[GBA_GL_FBO_OUTPUT]);
 465	_initFramebufferTexture(glRenderer->outputTex, GL_RGB, GL_COLOR_ATTACHMENT0, glRenderer->scale);
 466
 467	glBindFramebuffer(GL_FRAMEBUFFER, 0);
 468
 469	glGenBuffers(1, &glRenderer->vbo);
 470	glBindBuffer(GL_ARRAY_BUFFER, glRenderer->vbo);
 471	glBufferData(GL_ARRAY_BUFFER, sizeof(_vertices), _vertices, GL_STATIC_DRAW);
 472
 473	int i;
 474	for (i = 0; i < 4; ++i) {
 475		struct GBAVideoGLBackground* bg = &glRenderer->bg[i];
 476		bg->index = i;
 477		bg->enabled = 0;
 478		bg->priority = 0;
 479		bg->charBase = 0;
 480		bg->mosaic = 0;
 481		bg->multipalette = 0;
 482		bg->screenBase = 0;
 483		bg->overflow = 0;
 484		bg->size = 0;
 485		bg->target1 = 0;
 486		bg->target2 = 0;
 487		bg->x = 0;
 488		bg->y = 0;
 489		bg->refx = 0;
 490		bg->refy = 0;
 491		bg->affine[0].dx = 256;
 492		bg->affine[0].dmx = 0;
 493		bg->affine[0].dy = 0;
 494		bg->affine[0].dmy = 256;
 495		bg->affine[0].sx = 0;
 496		bg->affine[0].sy = 0;
 497		glGenFramebuffers(1, &bg->fbo);
 498		glGenTextures(1, &bg->tex);
 499		glGenTextures(1, &bg->flags);
 500		glBindFramebuffer(GL_FRAMEBUFFER, bg->fbo);
 501		_initFramebufferTexture(bg->tex, GL_RGBA, GL_COLOR_ATTACHMENT0, glRenderer->scale);
 502		_initFramebufferTexture(bg->flags, GL_RGBA, GL_COLOR_ATTACHMENT1, glRenderer->scale);
 503		glBindFramebuffer(GL_FRAMEBUFFER, 0);
 504	}
 505
 506	char log[1024];
 507	const GLchar* shaderBuffer[8];
 508	shaderBuffer[0] = _gl3Header;
 509
 510	GLuint vs = glCreateShader(GL_VERTEX_SHADER);
 511	shaderBuffer[1] = _vertexShader;
 512	glShaderSource(vs, 2, shaderBuffer, 0);
 513	glCompileShader(vs);
 514	glGetShaderInfoLog(vs, 1024, 0, log);
 515	if (log[0]) {
 516		mLOG(GBA_VIDEO, ERROR, "Vertex shader compilation failure: %s", log);
 517	}
 518
 519	shaderBuffer[1] = _renderMode0;
 520
 521	shaderBuffer[2] = _renderTile16;
 522	_compileShader(glRenderer, &glRenderer->bgShader[0], shaderBuffer, 3, vs, _uniformsMode0, log);
 523
 524	shaderBuffer[2] = _renderTile256;
 525	_compileShader(glRenderer, &glRenderer->bgShader[1], shaderBuffer, 3, vs, _uniformsMode0, log);
 526
 527	shaderBuffer[1] = _renderMode2;
 528
 529	shaderBuffer[2] = _fetchTileOverflow;
 530	_compileShader(glRenderer, &glRenderer->bgShader[2], shaderBuffer, 3, vs, _uniformsMode2, log);
 531
 532	shaderBuffer[2] = _fetchTileNoOverflow;
 533	_compileShader(glRenderer, &glRenderer->bgShader[3], shaderBuffer, 3, vs, _uniformsMode2, log);
 534
 535	shaderBuffer[1] = _renderObj;
 536
 537	shaderBuffer[2] = _renderTile16;
 538	_compileShader(glRenderer, &glRenderer->objShader[0], shaderBuffer, 3, vs, _uniformsObj, log);
 539#ifndef BUILD_GLES3
 540	glBindFragDataLocation(glRenderer->objShader[0].program, 2, "window");
 541#endif
 542
 543	shaderBuffer[2] = _renderTile256;
 544	_compileShader(glRenderer, &glRenderer->objShader[1], shaderBuffer, 3, vs, _uniformsObj, log);
 545#ifndef BUILD_GLES3
 546	glBindFragDataLocation(glRenderer->objShader[1].program, 2, "window");
 547#endif
 548
 549	shaderBuffer[1] = _finalize;
 550	_compileShader(glRenderer, &glRenderer->finalizeShader, shaderBuffer, 2, vs, _uniformsFinalize, log);
 551
 552	glBindVertexArray(0);
 553	glDeleteShader(vs);
 554
 555	GBAVideoGLRendererReset(renderer);
 556}
 557
 558void GBAVideoGLRendererDeinit(struct GBAVideoRenderer* renderer) {
 559	struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;
 560	if (glRenderer->temporaryBuffer) {
 561		mappedMemoryFree(glRenderer->temporaryBuffer, GBA_VIDEO_HORIZONTAL_PIXELS * GBA_VIDEO_VERTICAL_PIXELS * glRenderer->scale * glRenderer->scale);
 562	}
 563	glDeleteFramebuffers(GBA_GL_FBO_MAX, glRenderer->fbo);
 564	glDeleteTextures(GBA_GL_TEX_MAX, glRenderer->layers);
 565	glDeleteTextures(1, &glRenderer->paletteTex);
 566	glDeleteTextures(1, &glRenderer->vramTex);
 567}
 568
 569void GBAVideoGLRendererReset(struct GBAVideoRenderer* renderer) {
 570	struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;
 571
 572	glRenderer->paletteDirty = true;
 573	glRenderer->vramDirty = 0xFFFFFF;
 574	glRenderer->firstAffine = -1;
 575}
 576
 577void GBAVideoGLRendererWriteVRAM(struct GBAVideoRenderer* renderer, uint32_t address) {
 578	struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;
 579	glRenderer->vramDirty |= 1 << (address >> 12);
 580}
 581
 582void GBAVideoGLRendererWriteOAM(struct GBAVideoRenderer* renderer, uint32_t oam) {
 583	UNUSED(oam);
 584	struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;
 585	glRenderer->oamDirty = true;
 586}
 587
 588void GBAVideoGLRendererWritePalette(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
 589	struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;
 590#ifdef BUILD_GLES3
 591	glRenderer->shadowPalette[address >> 1] = (value & 0x3F) | ((value & 0x7FE0) << 1);
 592#else
 593	UNUSED(address);
 594	UNUSED(value);
 595#endif
 596	glRenderer->paletteDirty = true;
 597}
 598
 599uint16_t GBAVideoGLRendererWriteVideoRegister(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
 600	struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;
 601	if (renderer->cache) {
 602		GBAVideoCacheWriteVideoRegister(renderer->cache, address, value);
 603	}
 604
 605	switch (address) {
 606	case REG_DISPCNT:
 607		value &= 0xFFF7;
 608		glRenderer->dispcnt = value;
 609		GBAVideoGLRendererUpdateDISPCNT(glRenderer);
 610		break;
 611	case REG_BG0CNT:
 612		value &= 0xDFFF;
 613		GBAVideoGLRendererWriteBGCNT(&glRenderer->bg[0], value);
 614		break;
 615	case REG_BG1CNT:
 616		value &= 0xDFFF;
 617		GBAVideoGLRendererWriteBGCNT(&glRenderer->bg[1], value);
 618		break;
 619	case REG_BG2CNT:
 620		value &= 0xFFFF;
 621		GBAVideoGLRendererWriteBGCNT(&glRenderer->bg[2], value);
 622		break;
 623	case REG_BG3CNT:
 624		value &= 0xFFFF;
 625		GBAVideoGLRendererWriteBGCNT(&glRenderer->bg[3], value);
 626		break;
 627	case REG_BG0HOFS:
 628		value &= 0x01FF;
 629		glRenderer->bg[0].x = value;
 630		break;
 631	case REG_BG0VOFS:
 632		value &= 0x01FF;
 633		glRenderer->bg[0].y = value;
 634		break;
 635	case REG_BG1HOFS:
 636		value &= 0x01FF;
 637		glRenderer->bg[1].x = value;
 638		break;
 639	case REG_BG1VOFS:
 640		value &= 0x01FF;
 641		glRenderer->bg[1].y = value;
 642		break;
 643	case REG_BG2HOFS:
 644		value &= 0x01FF;
 645		glRenderer->bg[2].x = value;
 646		break;
 647	case REG_BG2VOFS:
 648		value &= 0x01FF;
 649		glRenderer->bg[2].y = value;
 650		break;
 651	case REG_BG3HOFS:
 652		value &= 0x01FF;
 653		glRenderer->bg[3].x = value;
 654		break;
 655	case REG_BG3VOFS:
 656		value &= 0x01FF;
 657		glRenderer->bg[3].y = value;
 658		break;
 659	case REG_BG2PA:
 660		glRenderer->bg[2].affine[0].dx = value;
 661		break;
 662	case REG_BG2PB:
 663		glRenderer->bg[2].affine[0].dmx = value;
 664		break;
 665	case REG_BG2PC:
 666		glRenderer->bg[2].affine[0].dy = value;
 667		break;
 668	case REG_BG2PD:
 669		glRenderer->bg[2].affine[0].dmy = value;
 670		break;
 671	case REG_BG2X_LO:
 672		GBAVideoGLRendererWriteBGX_LO(&glRenderer->bg[2], value);
 673		break;
 674	case REG_BG2X_HI:
 675		GBAVideoGLRendererWriteBGX_HI(&glRenderer->bg[2], value);
 676		break;
 677	case REG_BG2Y_LO:
 678		GBAVideoGLRendererWriteBGY_LO(&glRenderer->bg[2], value);
 679		break;
 680	case REG_BG2Y_HI:
 681		GBAVideoGLRendererWriteBGY_HI(&glRenderer->bg[2], value);
 682		break;
 683	case REG_BG3PA:
 684		glRenderer->bg[3].affine[0].dx = value;
 685		break;
 686	case REG_BG3PB:
 687		glRenderer->bg[3].affine[0].dmx = value;
 688		break;
 689	case REG_BG3PC:
 690		glRenderer->bg[3].affine[0].dy = value;
 691		break;
 692	case REG_BG3PD:
 693		glRenderer->bg[3].affine[0].dmy = value;
 694		break;
 695	case REG_BG3X_LO:
 696		GBAVideoGLRendererWriteBGX_LO(&glRenderer->bg[3], value);
 697		break;
 698	case REG_BG3X_HI:
 699		GBAVideoGLRendererWriteBGX_HI(&glRenderer->bg[3], value);
 700		break;
 701	case REG_BG3Y_LO:
 702		GBAVideoGLRendererWriteBGY_LO(&glRenderer->bg[3], value);
 703		break;
 704	case REG_BG3Y_HI:
 705		GBAVideoGLRendererWriteBGY_HI(&glRenderer->bg[3], value);
 706		break;
 707	case REG_BLDCNT:
 708		GBAVideoGLRendererWriteBLDCNT(glRenderer, value);
 709		value &= 0x3FFF;
 710		break;
 711	case REG_BLDALPHA:
 712		glRenderer->blda = value & 0x1F;
 713		if (glRenderer->blda > 0x10) {
 714			glRenderer->blda = 0x10;
 715		}
 716		glRenderer->bldb = (value >> 8) & 0x1F;
 717		if (glRenderer->bldb > 0x10) {
 718			glRenderer->bldb = 0x10;
 719		}
 720		value &= 0x1F1F;
 721		break;
 722	case REG_BLDY:
 723		value &= 0x1F;
 724		if (value > 0x10) {
 725			value = 0x10;
 726		}
 727		glRenderer->bldy = value;
 728		break;
 729	case REG_WIN0H:
 730		glRenderer->winN[0].h[0].end = value;
 731		glRenderer->winN[0].h[0].start = value >> 8;
 732		if (glRenderer->winN[0].h[0].start > GBA_VIDEO_HORIZONTAL_PIXELS && glRenderer->winN[0].h[0].start > glRenderer->winN[0].h[0].end) {
 733			glRenderer->winN[0].h[0].start = 0;
 734		}
 735		if (glRenderer->winN[0].h[0].end > GBA_VIDEO_HORIZONTAL_PIXELS) {
 736			glRenderer->winN[0].h[0].end = GBA_VIDEO_HORIZONTAL_PIXELS;
 737			if (glRenderer->winN[0].h[0].start > GBA_VIDEO_HORIZONTAL_PIXELS) {
 738				glRenderer->winN[0].h[0].start = GBA_VIDEO_HORIZONTAL_PIXELS;
 739			}
 740		}
 741		break;
 742	case REG_WIN1H:
 743		glRenderer->winN[1].h[0].end = value;
 744		glRenderer->winN[1].h[0].start = value >> 8;
 745		if (glRenderer->winN[1].h[0].start > GBA_VIDEO_HORIZONTAL_PIXELS && glRenderer->winN[1].h[0].start > glRenderer->winN[1].h[0].end) {
 746			glRenderer->winN[1].h[0].start = 0;
 747		}
 748		if (glRenderer->winN[1].h[0].end > GBA_VIDEO_HORIZONTAL_PIXELS) {
 749			glRenderer->winN[1].h[0].end = GBA_VIDEO_HORIZONTAL_PIXELS;
 750			if (glRenderer->winN[1].h[0].start > GBA_VIDEO_HORIZONTAL_PIXELS) {
 751				glRenderer->winN[1].h[0].start = GBA_VIDEO_HORIZONTAL_PIXELS;
 752			}
 753		}
 754		break;
 755	case REG_WIN0V:
 756		glRenderer->winN[0].v.end = value;
 757		glRenderer->winN[0].v.start = value >> 8;
 758		if (glRenderer->winN[0].v.start > GBA_VIDEO_VERTICAL_PIXELS && glRenderer->winN[0].v.start > glRenderer->winN[0].v.end) {
 759			glRenderer->winN[0].v.start = 0;
 760		}
 761		if (glRenderer->winN[0].v.end > GBA_VIDEO_VERTICAL_PIXELS) {
 762			glRenderer->winN[0].v.end = GBA_VIDEO_VERTICAL_PIXELS;
 763			if (glRenderer->winN[0].v.start > GBA_VIDEO_VERTICAL_PIXELS) {
 764				glRenderer->winN[0].v.start = GBA_VIDEO_VERTICAL_PIXELS;
 765			}
 766		}
 767		break;
 768	case REG_WIN1V:
 769		glRenderer->winN[1].v.end = value;
 770		glRenderer->winN[1].v.start = value >> 8;
 771		if (glRenderer->winN[1].v.start > GBA_VIDEO_VERTICAL_PIXELS && glRenderer->winN[1].v.start > glRenderer->winN[1].v.end) {
 772			glRenderer->winN[1].v.start = 0;
 773		}
 774		if (glRenderer->winN[1].v.end > GBA_VIDEO_VERTICAL_PIXELS) {
 775			glRenderer->winN[1].v.end = GBA_VIDEO_VERTICAL_PIXELS;
 776			if (glRenderer->winN[1].v.start > GBA_VIDEO_VERTICAL_PIXELS) {
 777				glRenderer->winN[1].v.start = GBA_VIDEO_VERTICAL_PIXELS;
 778			}
 779		}
 780		break;
 781	case REG_WININ:
 782		value &= 0x3F3F;
 783		glRenderer->winN[0].control = value;
 784		glRenderer->winN[1].control = value >> 8;
 785		break;
 786	case REG_WINOUT:
 787		value &= 0x3F3F;
 788		glRenderer->winout = value;
 789		glRenderer->objwin = value >> 8;
 790		break;
 791	case REG_MOSAIC:
 792		glRenderer->mosaic = value;
 793		break;
 794	default:
 795		break;
 796	}
 797	return value;
 798}
 799
 800void GBAVideoGLRendererDrawScanline(struct GBAVideoRenderer* renderer, int y) {
 801	struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;
 802	if (glRenderer->paletteDirty) {
 803		glBindTexture(GL_TEXTURE_2D, glRenderer->paletteTex);
 804#ifdef BUILD_GLES3
 805		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB565, 16, 32, 0, GL_RGBA, GL_UNSIGNED_SHORT_5_6_5, glRenderer->shadowPalette);
 806#else
 807		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB5_A1, 16, 32, 0, GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV, glRenderer->d.palette);
 808#endif
 809		glRenderer->paletteDirty = false;
 810	}
 811	int i;
 812	for (i = 0; i < 24; ++i) {
 813		if (!(glRenderer->vramDirty & (1 << i))) {
 814			continue;
 815		}
 816		// TODO: PBOs
 817		glBindTexture(GL_TEXTURE_2D, glRenderer->vramTex);
 818		glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 8 * i, 256, 8, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, &glRenderer->d.vram[2048 * i]);
 819	}
 820	glRenderer->vramDirty = 0;
 821
 822	if (y == 0) {
 823		memcpy(&glRenderer->winN[0].h[1], &glRenderer->winN[0].h[0], sizeof(struct GBAVideoWindowRegion));
 824		memcpy(&glRenderer->winN[1].h[1], &glRenderer->winN[1].h[0], sizeof(struct GBAVideoWindowRegion));
 825
 826		glDisable(GL_SCISSOR_TEST);		
 827		glClearColor(0, 0, 0, 0);
 828		glBindFramebuffer(GL_FRAMEBUFFER, glRenderer->fbo[GBA_GL_FBO_OBJ]);
 829		glDrawBuffers(2, (GLenum[]) { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 });
 830		glClear(GL_COLOR_BUFFER_BIT);
 831
 832		for (i = 0; i < 4; ++i) {
 833			glBindFramebuffer(GL_FRAMEBUFFER, glRenderer->bg[i].fbo);
 834			glDrawBuffers(2, (GLenum[]) { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 });
 835			glClear(GL_COLOR_BUFFER_BIT);
 836		}
 837		glDrawBuffers(1, (GLenum[]) { GL_COLOR_ATTACHMENT0 });
 838	}
 839	glEnable(GL_SCISSOR_TEST);
 840
 841	if (GBARegisterDISPCNTGetMode(glRenderer->dispcnt) != 0) {
 842		if (glRenderer->firstAffine < 0) {
 843			memcpy(&glRenderer->bg[2].affine[3], &glRenderer->bg[2].affine[0], sizeof(struct GBAVideoGLAffine));
 844			memcpy(&glRenderer->bg[3].affine[3], &glRenderer->bg[3].affine[0], sizeof(struct GBAVideoGLAffine));
 845			memcpy(&glRenderer->bg[2].affine[2], &glRenderer->bg[2].affine[0], sizeof(struct GBAVideoGLAffine));
 846			memcpy(&glRenderer->bg[3].affine[2], &glRenderer->bg[3].affine[0], sizeof(struct GBAVideoGLAffine));
 847			memcpy(&glRenderer->bg[2].affine[1], &glRenderer->bg[2].affine[0], sizeof(struct GBAVideoGLAffine));
 848			memcpy(&glRenderer->bg[3].affine[1], &glRenderer->bg[3].affine[0], sizeof(struct GBAVideoGLAffine));
 849			glRenderer->firstAffine = y;
 850		}
 851	} else {
 852		glRenderer->firstAffine = -1;
 853	}
 854
 855	GBAVideoGLRendererDrawWindow(glRenderer, y);
 856	if (GBARegisterDISPCNTIsObjEnable(glRenderer->dispcnt) && !glRenderer->d.disableOBJ) {
 857		if (glRenderer->oamDirty) {
 858			glRenderer->oamMax = GBAVideoRendererCleanOAM(glRenderer->d.oam->obj, glRenderer->sprites, 0);
 859			glRenderer->oamDirty = false;
 860		}
 861		int i;
 862		for (i = glRenderer->oamMax; i--;) {
 863			struct GBAVideoRendererSprite* sprite = &glRenderer->sprites[i];
 864			if ((y < sprite->y && (sprite->endY - 256 < 0 || y >= sprite->endY - 256)) || y >= sprite->endY) {
 865				continue;
 866			}
 867
 868			GBAVideoGLRendererDrawSprite(glRenderer, &sprite->obj, y, sprite->y);
 869		}
 870	}
 871
 872	if (TEST_LAYER_ENABLED(0) && GBARegisterDISPCNTGetMode(glRenderer->dispcnt) < 2) {
 873		GBAVideoGLRendererDrawBackgroundMode0(glRenderer, &glRenderer->bg[0], y);
 874	}
 875	if (TEST_LAYER_ENABLED(1) && GBARegisterDISPCNTGetMode(glRenderer->dispcnt) < 2) {
 876		GBAVideoGLRendererDrawBackgroundMode0(glRenderer, &glRenderer->bg[1], y);
 877	}
 878	if (TEST_LAYER_ENABLED(2)) {
 879		switch (GBARegisterDISPCNTGetMode(glRenderer->dispcnt)) {
 880		case 0:
 881			GBAVideoGLRendererDrawBackgroundMode0(glRenderer, &glRenderer->bg[2], y);
 882			break;
 883		case 1:
 884		case 2:
 885			GBAVideoGLRendererDrawBackgroundMode2(glRenderer, &glRenderer->bg[2], y);
 886			break;
 887		case 3:
 888			//GBAVideoGLRendererDrawBackgroundMode3(glRenderer, &glRenderer->bg[2], y);
 889			break;
 890		case 4:
 891			//GBAVideoGLRendererDrawBackgroundMode4(glRenderer, &glRenderer->bg[2], y);
 892			break;
 893		case 5:
 894			//GBAVideoGLRendererDrawBackgroundMode5(glRenderer, &glRenderer->bg[2], y);
 895			break;
 896		}
 897	}
 898	if (TEST_LAYER_ENABLED(3)) {
 899		switch (GBARegisterDISPCNTGetMode(glRenderer->dispcnt)) {
 900		case 0:
 901			GBAVideoGLRendererDrawBackgroundMode0(glRenderer, &glRenderer->bg[3], y);
 902			break;
 903		case 2:
 904			GBAVideoGLRendererDrawBackgroundMode2(glRenderer, &glRenderer->bg[3], y);
 905			break;
 906		}
 907	}
 908
 909	if (GBARegisterDISPCNTGetMode(glRenderer->dispcnt) != 0) {
 910		memcpy(&glRenderer->bg[2].affine[3], &glRenderer->bg[2].affine[2], sizeof(struct GBAVideoGLAffine));
 911		memcpy(&glRenderer->bg[3].affine[3], &glRenderer->bg[3].affine[2], sizeof(struct GBAVideoGLAffine));
 912		memcpy(&glRenderer->bg[2].affine[2], &glRenderer->bg[2].affine[1], sizeof(struct GBAVideoGLAffine));
 913		memcpy(&glRenderer->bg[3].affine[2], &glRenderer->bg[3].affine[1], sizeof(struct GBAVideoGLAffine));
 914		memcpy(&glRenderer->bg[2].affine[1], &glRenderer->bg[2].affine[0], sizeof(struct GBAVideoGLAffine));
 915		memcpy(&glRenderer->bg[3].affine[1], &glRenderer->bg[3].affine[0], sizeof(struct GBAVideoGLAffine));
 916
 917		glRenderer->bg[2].affine[0].sx += glRenderer->bg[2].affine[0].dmx;
 918		glRenderer->bg[2].affine[0].sy += glRenderer->bg[2].affine[0].dmy;
 919		glRenderer->bg[3].affine[0].sx += glRenderer->bg[3].affine[0].dmx;
 920		glRenderer->bg[3].affine[0].sy += glRenderer->bg[3].affine[0].dmy;
 921	}
 922	memcpy(&glRenderer->winN[0].h[1], &glRenderer->winN[0].h[0], sizeof(struct GBAVideoWindowRegion));
 923	memcpy(&glRenderer->winN[1].h[1], &glRenderer->winN[1].h[0], sizeof(struct GBAVideoWindowRegion));
 924}
 925
 926void GBAVideoGLRendererFinishFrame(struct GBAVideoRenderer* renderer) {
 927	struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;
 928	_finalizeLayers(glRenderer);
 929	glRenderer->firstAffine = -1;
 930	glRenderer->bg[2].affine[0].sx = glRenderer->bg[2].refx;
 931	glRenderer->bg[2].affine[0].sy = glRenderer->bg[2].refy;
 932	glRenderer->bg[3].affine[0].sx = glRenderer->bg[3].refx;
 933	glRenderer->bg[3].affine[0].sy = glRenderer->bg[3].refy;
 934	glFlush();
 935}
 936
 937void GBAVideoGLRendererGetPixels(struct GBAVideoRenderer* renderer, size_t* stride, const void** pixels) {
 938	struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;
 939	*stride = GBA_VIDEO_HORIZONTAL_PIXELS * glRenderer->scale;
 940	if (!glRenderer->temporaryBuffer) {
 941		glRenderer->temporaryBuffer = anonymousMemoryMap(GBA_VIDEO_HORIZONTAL_PIXELS * GBA_VIDEO_VERTICAL_PIXELS * glRenderer->scale * glRenderer->scale * BYTES_PER_PIXEL);
 942	}
 943	glFinish();
 944	glBindFramebuffer(GL_FRAMEBUFFER, glRenderer->fbo[GBA_GL_FBO_OUTPUT]);
 945	glPixelStorei(GL_PACK_ROW_LENGTH, GBA_VIDEO_HORIZONTAL_PIXELS * glRenderer->scale);
 946	glPixelStorei(GL_PACK_ALIGNMENT, 1);
 947	glReadPixels(0, 0, GBA_VIDEO_HORIZONTAL_PIXELS * glRenderer->scale, GBA_VIDEO_VERTICAL_PIXELS * glRenderer->scale, GL_RGBA, GL_UNSIGNED_BYTE, (void*) glRenderer->temporaryBuffer);
 948	*pixels = glRenderer->temporaryBuffer;
 949}
 950
 951void GBAVideoGLRendererPutPixels(struct GBAVideoRenderer* renderer, size_t stride, const void* pixels) {
 952
 953}
 954
 955static void _enableBg(struct GBAVideoGLRenderer* renderer, int bg, bool active) {
 956	int wasActive = renderer->bg[bg].enabled;
 957	if (!active) {
 958		renderer->bg[bg].enabled = 0;
 959	} else if (!wasActive && active) {
 960		/*if (renderer->nextY == 0 || GBARegisterDISPCNTGetMode(renderer->dispcnt) > 2) {
 961			// TODO: Investigate in more depth how switching background works in different modes
 962			renderer->bg[bg].enabled = 4;
 963		} else {
 964			renderer->bg[bg].enabled = 1;
 965		}*/
 966		renderer->bg[bg].enabled = 4;
 967	}
 968}
 969
 970static void GBAVideoGLRendererUpdateDISPCNT(struct GBAVideoGLRenderer* renderer) {
 971	_enableBg(renderer, 0, GBARegisterDISPCNTGetBg0Enable(renderer->dispcnt));
 972	_enableBg(renderer, 1, GBARegisterDISPCNTGetBg1Enable(renderer->dispcnt));
 973	_enableBg(renderer, 2, GBARegisterDISPCNTGetBg2Enable(renderer->dispcnt));
 974	_enableBg(renderer, 3, GBARegisterDISPCNTGetBg3Enable(renderer->dispcnt));
 975}
 976
 977static void GBAVideoGLRendererWriteBGCNT(struct GBAVideoGLBackground* bg, uint16_t value) {
 978	bg->priority = GBARegisterBGCNTGetPriority(value);
 979	bg->charBase = GBARegisterBGCNTGetCharBase(value) << 13;
 980	bg->mosaic = GBARegisterBGCNTGetMosaic(value);
 981	bg->multipalette = GBARegisterBGCNTGet256Color(value);
 982	bg->screenBase = GBARegisterBGCNTGetScreenBase(value) << 10;
 983	bg->overflow = GBARegisterBGCNTGetOverflow(value);
 984	bg->size = GBARegisterBGCNTGetSize(value);
 985}
 986
 987static void GBAVideoGLRendererWriteBGX_LO(struct GBAVideoGLBackground* bg, uint16_t value) {
 988	bg->refx = (bg->refx & 0xFFFF0000) | value;
 989	bg->affine[0].sx = bg->refx;
 990}
 991
 992static void GBAVideoGLRendererWriteBGX_HI(struct GBAVideoGLBackground* bg, uint16_t value) {
 993	bg->refx = (bg->refx & 0x0000FFFF) | (value << 16);
 994	bg->refx <<= 4;
 995	bg->refx >>= 4;
 996	bg->affine[0].sx = bg->refx;
 997}
 998
 999static void GBAVideoGLRendererWriteBGY_LO(struct GBAVideoGLBackground* bg, uint16_t value) {
1000	bg->refy = (bg->refy & 0xFFFF0000) | value;
1001	bg->affine[0].sy = bg->refy;
1002}
1003
1004static void GBAVideoGLRendererWriteBGY_HI(struct GBAVideoGLBackground* bg, uint16_t value) {
1005	bg->refy = (bg->refy & 0x0000FFFF) | (value << 16);
1006	bg->refy <<= 4;
1007	bg->refy >>= 4;
1008	bg->affine[0].sy = bg->refy;
1009}
1010
1011static void GBAVideoGLRendererWriteBLDCNT(struct GBAVideoGLRenderer* renderer, uint16_t value) {
1012	renderer->bg[0].target1 = GBARegisterBLDCNTGetTarget1Bg0(value);
1013	renderer->bg[1].target1 = GBARegisterBLDCNTGetTarget1Bg1(value);
1014	renderer->bg[2].target1 = GBARegisterBLDCNTGetTarget1Bg2(value);
1015	renderer->bg[3].target1 = GBARegisterBLDCNTGetTarget1Bg3(value);
1016	renderer->bg[0].target2 = GBARegisterBLDCNTGetTarget2Bg0(value);
1017	renderer->bg[1].target2 = GBARegisterBLDCNTGetTarget2Bg1(value);
1018	renderer->bg[2].target2 = GBARegisterBLDCNTGetTarget2Bg2(value);
1019	renderer->bg[3].target2 = GBARegisterBLDCNTGetTarget2Bg3(value);
1020
1021	renderer->blendEffect = GBARegisterBLDCNTGetEffect(value);
1022	renderer->target1Obj = GBARegisterBLDCNTGetTarget1Obj(value);
1023	renderer->target1Bd = GBARegisterBLDCNTGetTarget1Bd(value);
1024	renderer->target2Obj = GBARegisterBLDCNTGetTarget2Obj(value);
1025	renderer->target2Bd = GBARegisterBLDCNTGetTarget2Bd(value);
1026}
1027
1028void _finalizeLayers(struct GBAVideoGLRenderer* renderer) {
1029	const GLuint* uniforms = renderer->finalizeShader.uniforms;
1030	glBindFramebuffer(GL_FRAMEBUFFER, renderer->fbo[GBA_GL_FBO_OUTPUT]);
1031	glViewport(0, 0, GBA_VIDEO_HORIZONTAL_PIXELS * renderer->scale, GBA_VIDEO_VERTICAL_PIXELS * renderer->scale);
1032	glScissor(0, 0, GBA_VIDEO_HORIZONTAL_PIXELS * renderer->scale, GBA_VIDEO_VERTICAL_PIXELS * renderer->scale);
1033	glUseProgram(renderer->finalizeShader.program);
1034	glBindVertexArray(renderer->finalizeShader.vao);
1035	glActiveTexture(GL_TEXTURE0);
1036	glBindTexture(GL_TEXTURE_2D, renderer->layers[GBA_GL_TEX_WINDOW]);
1037	glActiveTexture(GL_TEXTURE0 + 1);
1038	glBindTexture(GL_TEXTURE_2D, renderer->layers[GBA_GL_TEX_OBJ_COLOR]);
1039	glActiveTexture(GL_TEXTURE0 + 2);
1040	glBindTexture(GL_TEXTURE_2D, renderer->layers[GBA_GL_TEX_OBJ_FLAGS]);
1041	glActiveTexture(GL_TEXTURE0 + 3);
1042	glBindTexture(GL_TEXTURE_2D, renderer->bg[0].tex);
1043	glActiveTexture(GL_TEXTURE0 + 4);
1044	glBindTexture(GL_TEXTURE_2D, renderer->bg[0].flags);
1045	glActiveTexture(GL_TEXTURE0 + 5);
1046	glBindTexture(GL_TEXTURE_2D, renderer->bg[1].tex);
1047	glActiveTexture(GL_TEXTURE0 + 6);
1048	glBindTexture(GL_TEXTURE_2D, renderer->bg[1].flags);
1049	glActiveTexture(GL_TEXTURE0 + 7);
1050	glBindTexture(GL_TEXTURE_2D, renderer->bg[2].tex);
1051	glActiveTexture(GL_TEXTURE0 + 8);
1052	glBindTexture(GL_TEXTURE_2D, renderer->bg[2].flags);
1053	glActiveTexture(GL_TEXTURE0 + 9);
1054	glBindTexture(GL_TEXTURE_2D, renderer->bg[3].tex);
1055	glActiveTexture(GL_TEXTURE0 + 10);
1056	glBindTexture(GL_TEXTURE_2D, renderer->bg[3].flags);
1057
1058	uint32_t backdrop = M_RGB5_TO_RGB8(renderer->d.palette[0]);
1059	glUniform2i(uniforms[GBA_GL_VS_LOC], GBA_VIDEO_VERTICAL_PIXELS, 0);
1060	glUniform2i(uniforms[GBA_GL_VS_MAXPOS], GBA_VIDEO_HORIZONTAL_PIXELS, GBA_VIDEO_VERTICAL_PIXELS);
1061	glUniform1i(uniforms[GBA_GL_FINALIZE_SCALE], renderer->scale);
1062	glUniform1iv(uniforms[GBA_GL_FINALIZE_LAYERS], 5, (GLint[]) { 3, 5, 7, 9, 1 });
1063	glUniform1iv(uniforms[GBA_GL_FINALIZE_FLAGS], 5, (GLint[]) { 4, 6, 8, 10, 2 });
1064	glUniform1i(uniforms[GBA_GL_FINALIZE_WINDOW], 0);
1065	glUniform4f(uniforms[GBA_GL_FINALIZE_BACKDROP], ((backdrop >> 16) & 0xFF) / 256., ((backdrop >> 8) & 0xFF) / 256., (backdrop & 0xFF) / 256., 0.f);
1066	glUniform4f(uniforms[GBA_GL_FINALIZE_BACKDROPFLAGS], 1, (renderer->target1Bd | (renderer->target2Bd * 2) | (renderer->blendEffect * 4)) / 32.f, renderer->blda / 16.f, renderer->bldb / 16.f);
1067	glEnableVertexAttribArray(0);
1068	glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1069	glBindFramebuffer(GL_FRAMEBUFFER, 0);
1070}
1071
1072void GBAVideoGLRendererDrawSprite(struct GBAVideoGLRenderer* renderer, struct GBAObj* sprite, int y, int spriteY) {
1073	int width = GBAVideoObjSizes[GBAObjAttributesAGetShape(sprite->a) * 4 + GBAObjAttributesBGetSize(sprite->b)][0];
1074	int height = GBAVideoObjSizes[GBAObjAttributesAGetShape(sprite->a) * 4 + GBAObjAttributesBGetSize(sprite->b)][1];
1075	int32_t x = (uint32_t) GBAObjAttributesBGetX(sprite->b) << 23;
1076	x >>= 23;
1077
1078	int align = GBAObjAttributesAIs256Color(sprite->a) && !GBARegisterDISPCNTIsObjCharacterMapping(renderer->dispcnt);
1079	unsigned charBase = (BASE_TILE >> 1) + (GBAObjAttributesCGetTile(sprite->c) & ~align) * 0x10;
1080	int stride = GBARegisterDISPCNTIsObjCharacterMapping(renderer->dispcnt) ? (width >> 3) : (0x20 >> GBAObjAttributesAGet256Color(sprite->a));
1081
1082	if (spriteY + height >= 256) {
1083		spriteY -= 256;
1084	}
1085
1086	if (!GBAObjAttributesAIsTransformed(sprite->a) && GBAObjAttributesBIsVFlip(sprite->b)) {
1087		spriteY = (y - height) + (y - spriteY) + 1;
1088	}
1089
1090	int totalWidth = width;
1091	int totalHeight = height;
1092	if (GBAObjAttributesAIsTransformed(sprite->a) && GBAObjAttributesAIsDoubleSize(sprite->a)) {
1093		totalWidth <<= 1;
1094		totalHeight <<= 1;
1095	}
1096
1097	enum GBAVideoBlendEffect blendEffect = GBAObjAttributesAGetMode(sprite->a) == OBJ_MODE_SEMITRANSPARENT ? BLEND_ALPHA : renderer->blendEffect;
1098
1099	const struct GBAVideoGLShader* shader = &renderer->objShader[GBAObjAttributesAGet256Color(sprite->a)];
1100	const GLuint* uniforms = shader->uniforms;
1101	glBindFramebuffer(GL_FRAMEBUFFER, renderer->fbo[GBA_GL_FBO_OBJ]);
1102	glViewport(x * renderer->scale, spriteY * renderer->scale, totalWidth * renderer->scale, totalHeight * renderer->scale);
1103	glScissor(x * renderer->scale, y * renderer->scale, totalWidth * renderer->scale, renderer->scale);
1104	glUseProgram(shader->program);
1105	glBindVertexArray(shader->vao);
1106	glActiveTexture(GL_TEXTURE0);
1107	glBindTexture(GL_TEXTURE_2D, renderer->vramTex);
1108	glActiveTexture(GL_TEXTURE0 + 1);
1109	glBindTexture(GL_TEXTURE_2D, renderer->paletteTex);
1110	glUniform2i(uniforms[GBA_GL_VS_LOC], 1, y - spriteY);
1111	glUniform2i(uniforms[GBA_GL_VS_MAXPOS], (GBAObjAttributesBIsHFlip(sprite->b) && !GBAObjAttributesAIsTransformed(sprite->a)) ? -totalWidth : totalWidth, totalHeight);
1112	glUniform1i(uniforms[GBA_GL_OBJ_VRAM], 0);
1113	glUniform1i(uniforms[GBA_GL_OBJ_PALETTE], 1);
1114	glUniform1i(uniforms[GBA_GL_OBJ_CHARBASE], charBase);
1115	glUniform1i(uniforms[GBA_GL_OBJ_STRIDE], stride);
1116	glUniform1i(uniforms[GBA_GL_OBJ_LOCALPALETTE], GBAObjAttributesCGetPalette(sprite->c));
1117	glUniform4i(uniforms[GBA_GL_OBJ_INFLAGS], GBAObjAttributesCGetPriority(sprite->c) << 3,
1118	                                          (renderer->target1Obj || GBAObjAttributesAGetMode(sprite->a) == OBJ_MODE_SEMITRANSPARENT) | (renderer->target2Obj * 2) | (blendEffect * 4),
1119	                                          renderer->blda, renderer->bldb);
1120	if (GBAObjAttributesAIsTransformed(sprite->a)) {
1121		struct GBAOAMMatrix mat;
1122		LOAD_16(mat.a, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].a);
1123		LOAD_16(mat.b, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].b);
1124		LOAD_16(mat.c, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].c);
1125		LOAD_16(mat.d, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].d);
1126
1127		glUniformMatrix2fv(uniforms[GBA_GL_OBJ_TRANSFORM], 1, GL_FALSE, (GLfloat[]) { mat.a / 256.f, mat.c / 256.f, mat.b / 256.f, mat.d / 256.f });
1128	} else {
1129		glUniformMatrix2fv(uniforms[GBA_GL_OBJ_TRANSFORM], 1, GL_FALSE, (GLfloat[]) { 1.f, 0, 0, 1.f });
1130	}
1131	glUniform4i(uniforms[GBA_GL_OBJ_DIMS], width, height, totalWidth, totalHeight);
1132	if (GBAObjAttributesAGetMode(sprite->a) == OBJ_MODE_OBJWIN) {
1133		int window = ~renderer->objwin & 0xFF;
1134		glUniform3f(uniforms[GBA_GL_OBJ_OBJWIN], 1, (window & 0xF) / 32.f, (window >> 4) / 32.f);
1135		glDrawBuffers(3, (GLenum[]) { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2 });
1136	} else {
1137		glUniform3f(uniforms[GBA_GL_OBJ_OBJWIN], 0, 0, 0);
1138		glDrawBuffers(2, (GLenum[]) { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 });
1139	}
1140	glEnableVertexAttribArray(0);
1141	glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1142	glDrawBuffers(1, (GLenum[]) { GL_COLOR_ATTACHMENT0 });
1143}
1144
1145void GBAVideoGLRendererDrawBackgroundMode0(struct GBAVideoGLRenderer* renderer, struct GBAVideoGLBackground* background, int y) {
1146	int inY = y + background->y;
1147	int yBase = inY & 0xFF;
1148	if (background->size == 2) {
1149		yBase += inY & 0x100;
1150	} else if (background->size == 3) {
1151		yBase += (inY & 0x100) << 1;
1152	}
1153
1154	const struct GBAVideoGLShader* shader = &renderer->bgShader[background->multipalette ? 1 : 0];
1155	const GLuint* uniforms = shader->uniforms;
1156	glBindFramebuffer(GL_FRAMEBUFFER, background->fbo);
1157	glViewport(0, 0, GBA_VIDEO_HORIZONTAL_PIXELS * renderer->scale, GBA_VIDEO_VERTICAL_PIXELS * renderer->scale);
1158	glScissor(0, y * renderer->scale, GBA_VIDEO_HORIZONTAL_PIXELS * renderer->scale, renderer->scale);
1159	glUseProgram(shader->program);
1160	glBindVertexArray(shader->vao);
1161	glActiveTexture(GL_TEXTURE0);
1162	glBindTexture(GL_TEXTURE_2D, renderer->vramTex);
1163	glActiveTexture(GL_TEXTURE0 + 1);
1164	glBindTexture(GL_TEXTURE_2D, renderer->paletteTex);
1165	glUniform2i(uniforms[GBA_GL_VS_LOC], 1, y);
1166	glUniform2i(uniforms[GBA_GL_VS_MAXPOS], GBA_VIDEO_HORIZONTAL_PIXELS, GBA_VIDEO_VERTICAL_PIXELS);
1167	glUniform1i(uniforms[GBA_GL_BG_VRAM], 0);
1168	glUniform1i(uniforms[GBA_GL_BG_PALETTE], 1);
1169	glUniform1i(uniforms[GBA_GL_BG_SCREENBASE], background->screenBase);
1170	glUniform1i(uniforms[GBA_GL_BG_CHARBASE], background->charBase);
1171	glUniform1i(uniforms[GBA_GL_BG_SIZE], background->size);
1172	glUniform2i(uniforms[GBA_GL_BG_OFFSET], background->x, yBase - y);
1173	glUniform4i(uniforms[GBA_GL_BG_INFLAGS], (background->priority << 3) + (background->index << 1) + 1,
1174		                                     background->target1 | (background->target2 * 2) | (renderer->blendEffect * 4),
1175		                                     renderer->blda, renderer->bldb);
1176	glDrawBuffers(2, (GLenum[]) { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 });
1177	glEnableVertexAttribArray(0);
1178	glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1179	glDrawBuffers(1, (GLenum[]) { GL_COLOR_ATTACHMENT0 });
1180}
1181
1182void GBAVideoGLRendererDrawBackgroundMode2(struct GBAVideoGLRenderer* renderer, struct GBAVideoGLBackground* background, int y) {
1183	int reverse;
1184	int forward;
1185	switch (y - renderer->firstAffine) {
1186	case 0:
1187	case 1:
1188	case 2:
1189	case 3:
1190		return;
1191	case 4:
1192		forward = 2;
1193		reverse = 4;
1194		break;
1195	case 5:
1196		forward = 2;
1197		reverse = 3;
1198		break;
1199	case 6:
1200		forward = 2;
1201		reverse = 2;
1202		break;
1203	default:
1204		forward = 1;
1205		reverse = 1;
1206	}
1207
1208	const struct GBAVideoGLShader* shader = &renderer->bgShader[background->overflow ? 2 : 3];
1209	const GLuint* uniforms = shader->uniforms;
1210	glBindFramebuffer(GL_FRAMEBUFFER, background->fbo);
1211	glViewport(0, 0, GBA_VIDEO_HORIZONTAL_PIXELS * renderer->scale, GBA_VIDEO_VERTICAL_PIXELS * renderer->scale);
1212	glScissor(0, (y - reverse) * renderer->scale, GBA_VIDEO_HORIZONTAL_PIXELS * renderer->scale, renderer->scale * forward);
1213	glUseProgram(shader->program);
1214	glBindVertexArray(shader->vao);
1215	glActiveTexture(GL_TEXTURE0);
1216	glBindTexture(GL_TEXTURE_2D, renderer->vramTex);
1217	glActiveTexture(GL_TEXTURE0 + 1);
1218	glBindTexture(GL_TEXTURE_2D, renderer->paletteTex);
1219	glUniform2i(uniforms[GBA_GL_VS_LOC], forward, y - reverse);
1220	glUniform2i(uniforms[GBA_GL_VS_MAXPOS], GBA_VIDEO_HORIZONTAL_PIXELS, GBA_VIDEO_VERTICAL_PIXELS);
1221	glUniform1i(uniforms[GBA_GL_BG_VRAM], 0);
1222	glUniform1i(uniforms[GBA_GL_BG_PALETTE], 1);
1223	glUniform1i(uniforms[GBA_GL_BG_SCREENBASE], background->screenBase);
1224	glUniform1i(uniforms[GBA_GL_BG_CHARBASE], background->charBase);
1225	glUniform1i(uniforms[GBA_GL_BG_SIZE], background->size);
1226	glUniform2f(uniforms[GBA_GL_BG_RANGE], y, 1);
1227	glUniform4i(uniforms[GBA_GL_BG_INFLAGS], (background->priority << 3) + (background->index << 1) + 1,
1228		                                     background->target1 | (background->target2 * 2) | (renderer->blendEffect * 4),
1229		                                     renderer->blda, renderer->bldb);
1230	if (renderer->scale > 1) {
1231		glUniform2iv(uniforms[GBA_GL_BG_OFFSET], 4, (GLint[]) {
1232			background->affine[0].sx, background->affine[0].sy,
1233			background->affine[1].sx, background->affine[1].sy,
1234			background->affine[2].sx, background->affine[2].sy,
1235			background->affine[3].sx, background->affine[3].sy,
1236		});
1237		glUniform2iv(uniforms[GBA_GL_BG_TRANSFORM], 4, (GLint[]) {
1238			background->affine[0].dx, background->affine[0].dy,
1239			background->affine[1].dx, background->affine[1].dy,
1240			background->affine[2].dx, background->affine[2].dy,
1241			background->affine[3].dx, background->affine[3].dy,
1242		});
1243	} else {
1244		glUniform2iv(uniforms[GBA_GL_BG_OFFSET], 4, (GLint[]) {
1245			background->affine[0].sx, background->affine[0].sy,
1246			background->affine[0].sx, background->affine[0].sy,
1247			background->affine[0].sx, background->affine[0].sy,
1248			background->affine[0].sx, background->affine[0].sy,
1249		});
1250		glUniform2iv(uniforms[GBA_GL_BG_TRANSFORM], 4, (GLint[]) {
1251			background->affine[0].dx, background->affine[0].dy,
1252			background->affine[0].dx, background->affine[0].dy,
1253			background->affine[0].dx, background->affine[0].dy,
1254			background->affine[0].dx, background->affine[0].dy,
1255		});
1256	}
1257	glDrawBuffers(2, (GLenum[]) { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 });
1258	glEnableVertexAttribArray(0);
1259	glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1260	glDrawBuffers(1, (GLenum[]) { GL_COLOR_ATTACHMENT0 });
1261}
1262
1263static void _scissorWindow(int start, int end, int y, int lines, int scale) {
1264	if (start > end) {
1265		_scissorWindow(start, GBA_VIDEO_HORIZONTAL_PIXELS * scale, y, lines, scale);
1266		_scissorWindow(0, end, y, lines, scale);
1267		return;
1268	}
1269	glScissor(start, y, end - start, lines);
1270	glClear(GL_COLOR_BUFFER_BIT);	
1271}
1272
1273static void _scissorWindowN(struct GBAVideoWindowRegion* region, int y, int scale) {
1274	int sdelta = region[0].start - region[1].start;
1275	int edelta = region[0].end - region[1].end;
1276	int maxDelta = 0;
1277	if (sdelta > maxDelta) {
1278		maxDelta = sdelta;
1279	} else if (-sdelta > maxDelta) {
1280		maxDelta = -sdelta;
1281	}
1282	if (edelta > maxDelta) {
1283		maxDelta = edelta;
1284	} else if (-edelta > maxDelta) {
1285		maxDelta = -edelta;
1286	}
1287	if (!(sdelta | edelta) || maxDelta >= GBA_VIDEO_VERTICAL_PIXELS / 2) {
1288		_scissorWindow(region[0].start * scale, region[0].end * scale, y, scale, scale);
1289	} else {
1290		int i;
1291		for (i = 0; i < scale; ++i) {
1292			int start = region[1].start * scale + sdelta * i;
1293			int end = region[1].end * scale + edelta * i;
1294			_scissorWindow(start, end, y + i, 1, scale);
1295		}
1296	}
1297}
1298
1299static void _clearWindow(GBAWindowControl window, int bldy) {
1300	window = ~window & 0xFF;
1301	glClearColor((window & 0xF) / 32.f, (window >> 4) / 32.f, bldy / 16.f, 0);
1302}
1303
1304void GBAVideoGLRendererDrawWindow(struct GBAVideoGLRenderer* renderer, int y) {
1305	glBindFramebuffer(GL_FRAMEBUFFER, renderer->fbo[GBA_GL_FBO_WINDOW]);
1306	int dispcnt = ((renderer->dispcnt >> 8) & 0x1F) | 0x20;
1307	if (!(renderer->dispcnt & 0xE000)) {
1308		_clearWindow(dispcnt, renderer->bldy);
1309		_scissorWindow(0, GBA_VIDEO_HORIZONTAL_PIXELS * renderer->scale, y * renderer->scale, renderer->scale, renderer->scale);
1310	} else {
1311		_clearWindow(renderer->winout & dispcnt, renderer->bldy);
1312		_scissorWindow(0, GBA_VIDEO_HORIZONTAL_PIXELS * renderer->scale, y * renderer->scale, renderer->scale, renderer->scale);
1313		if (GBARegisterDISPCNTIsWin1Enable(renderer->dispcnt) && y >= renderer->winN[1].v.start && y < renderer->winN[1].v.end) {
1314			_clearWindow(renderer->winN[1].control & dispcnt, renderer->bldy);
1315			_scissorWindowN(renderer->winN[1].h, y * renderer->scale, renderer->scale);
1316		}
1317		if (GBARegisterDISPCNTIsWin0Enable(renderer->dispcnt) && y >= renderer->winN[0].v.start && y < renderer->winN[0].v.end) {
1318			_clearWindow(renderer->winN[0].control & dispcnt, renderer->bldy);
1319			_scissorWindowN(renderer->winN[0].h, y * renderer->scale, renderer->scale);
1320		}
1321	}
1322}
1323
1324#endif