all repos — mgba @ ac7ae74822d2740f82b62a644e88fbed789ad2be

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