all repos — mgba @ 3e8bb42e9f745f69f2137f071cfdc8fda0f3d435

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