all repos — mgba @ 6890c070e6e0c868312482a442e2b22f757b5edc

mGBA Game Boy Advance Emulator

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

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