all repos — mgba @ 130cb63bbc45819d844dd7cf30015a533d4168d8

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
  16#define FLAG_CONST "const vec4 flagCoeff = vec4(64., 32., 16., 1.);\n"
  17
  18static void GBAVideoGLRendererInit(struct GBAVideoRenderer* renderer);
  19static void GBAVideoGLRendererDeinit(struct GBAVideoRenderer* renderer);
  20static void GBAVideoGLRendererReset(struct GBAVideoRenderer* renderer);
  21static void GBAVideoGLRendererWriteVRAM(struct GBAVideoRenderer* renderer, uint32_t address);
  22static void GBAVideoGLRendererWriteOAM(struct GBAVideoRenderer* renderer, uint32_t oam);
  23static void GBAVideoGLRendererWritePalette(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value);
  24static uint16_t GBAVideoGLRendererWriteVideoRegister(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value);
  25static void GBAVideoGLRendererDrawScanline(struct GBAVideoRenderer* renderer, int y);
  26static void GBAVideoGLRendererFinishFrame(struct GBAVideoRenderer* renderer);
  27static void GBAVideoGLRendererGetPixels(struct GBAVideoRenderer* renderer, size_t* stride, const void** pixels);
  28static void GBAVideoGLRendererPutPixels(struct GBAVideoRenderer* renderer, size_t stride, const void* pixels);
  29
  30static void GBAVideoGLRendererUpdateDISPCNT(struct GBAVideoGLRenderer* renderer);
  31static void GBAVideoGLRendererWriteBGCNT(struct GBAVideoGLBackground* bg, uint16_t value);
  32static void GBAVideoGLRendererWriteBGX_LO(struct GBAVideoGLBackground* bg, uint16_t value);
  33static void GBAVideoGLRendererWriteBGX_HI(struct GBAVideoGLBackground* bg, uint16_t value);
  34static void GBAVideoGLRendererWriteBGY_LO(struct GBAVideoGLBackground* bg, uint16_t value);
  35static void GBAVideoGLRendererWriteBGY_HI(struct GBAVideoGLBackground* bg, uint16_t value);
  36static void GBAVideoGLRendererWriteBLDCNT(struct GBAVideoGLRenderer* renderer, uint16_t value);
  37
  38static void GBAVideoGLRendererDrawSprite(struct GBAVideoGLRenderer* renderer, struct GBAObj* sprite, int y, int spriteY);
  39static void GBAVideoGLRendererDrawBackgroundMode0(struct GBAVideoGLRenderer* renderer, struct GBAVideoGLBackground* background, int y);
  40static void GBAVideoGLRendererDrawBackgroundMode2(struct GBAVideoGLRenderer* renderer, struct GBAVideoGLBackground* background, int y);
  41static void GBAVideoGLRendererDrawBackgroundMode3(struct GBAVideoGLRenderer* renderer, struct GBAVideoGLBackground* background, int y);
  42static void GBAVideoGLRendererDrawBackgroundMode4(struct GBAVideoGLRenderer* renderer, struct GBAVideoGLBackground* background, int y);
  43static void GBAVideoGLRendererDrawBackgroundMode5(struct GBAVideoGLRenderer* renderer, struct GBAVideoGLBackground* background, int y);
  44static void GBAVideoGLRendererDrawWindow(struct GBAVideoGLRenderer* renderer, int y);
  45
  46static void _cleanRegister(struct GBAVideoGLRenderer* renderer, int address, uint16_t value);
  47static void _drawScanlines(struct GBAVideoGLRenderer* renderer, int lastY);
  48static void _finalizeLayers(struct GBAVideoGLRenderer* renderer);
  49
  50#define TEST_LAYER_ENABLED(X) !glRenderer->d.disableBG[X] && glRenderer->bg[X].enabled == 4
  51
  52struct GBAVideoGLUniform {
  53	const char* name;
  54	int type;
  55};
  56
  57static const GLchar* const _gles3Header =
  58	"#version 300 es\n"
  59	"#define OUT(n) layout(location = n)\n"
  60	"precision highp float;\n"
  61	"precision highp int;\n"
  62	"precision highp isampler2D;\n";
  63
  64static const GLchar* const _gl3Header =
  65	"#version 130\n"
  66	"#define OUT(n)\n"
  67	"precision highp float;\n";
  68
  69static const char* const _vertexShader =
  70	"in vec2 position;\n"
  71	"uniform ivec2 loc;\n"
  72	"uniform ivec2 maxPos;\n"
  73	"out vec2 texCoord;\n"
  74
  75	"void main() {\n"
  76	"	vec2 local = vec2(position.x, float(position.y * float(loc.x) + float(loc.y)) / float(maxPos.y));\n"
  77	"	gl_Position = vec4((local * 2. - 1.) * vec2(sign(maxPos)), 0., 1.);\n"
  78	"	texCoord = local * vec2(abs(maxPos));\n"
  79	"}";
  80
  81static const char* const _renderTile16 =
  82	"vec4 renderTile(int tile, int paletteId, ivec2 localCoord) {\n"
  83	"	int address = charBase + tile * 16 + (localCoord.x >> 2) + (localCoord.y << 1);\n"
  84	"	vec4 halfrow = texelFetch(vram, ivec2(address & 255, address >> 8), 0);\n"
  85	"	int entry = int(halfrow[3 - (localCoord.x & 3)] * 15.9);\n"
  86	"	vec4 color = texelFetch(palette, ivec2(entry, paletteId), 0);\n"
  87	"	if (entry == 0) {\n"
  88	"		discard;\n"
  89	"	}\n"
  90	"	color.a = 1.;\n"
  91	"	return color;\n"
  92	"}";
  93
  94static const char* const _renderTile256 =
  95	"vec4 renderTile(int tile, int paletteId, ivec2 localCoord) {\n"
  96	"	int address = charBase + tile * 32 + (localCoord.x >> 1) + (localCoord.y << 2);\n"
  97	"	vec4 halfrow = texelFetch(vram, ivec2(address & 255, address >> 8), 0);\n"
  98	"	int entry = int(halfrow[3 - 2 * (localCoord.x & 1)] * 15.9);\n"
  99	"	int pal2 = int(halfrow[2 - 2 * (localCoord.x & 1)] * 15.9);\n"
 100	"	vec4 color = texelFetch(palette, ivec2(entry, pal2 + (paletteId & 16)), 0);\n"
 101	"	if ((pal2 | entry) == 0) {\n"
 102	"		discard;\n"
 103	"	}\n"
 104	"	color.a = 1.;\n"
 105	"	return color;\n"
 106	"}";
 107
 108static const struct GBAVideoGLUniform _uniformsMode0[] = {
 109	{ "loc", GBA_GL_VS_LOC, },
 110	{ "maxPos", GBA_GL_VS_MAXPOS, },
 111	{ "vram", GBA_GL_BG_VRAM, },
 112	{ "palette", GBA_GL_BG_PALETTE, },
 113	{ "screenBase", GBA_GL_BG_SCREENBASE, },
 114	{ "charBase", GBA_GL_BG_CHARBASE, },
 115	{ "size", GBA_GL_BG_SIZE, },
 116	{ "offset", GBA_GL_BG_OFFSET, },
 117	{ "inflags", GBA_GL_BG_INFLAGS, },
 118	{ "mosaic", GBA_GL_BG_MOSAIC, },
 119	{ 0 }
 120};
 121
 122static const char* const _renderMode0 =
 123	"in vec2 texCoord;\n"
 124	"uniform sampler2D vram;\n"
 125	"uniform sampler2D palette;\n"
 126	"uniform int screenBase;\n"
 127	"uniform int charBase;\n"
 128	"uniform int size;\n"
 129	"uniform ivec2 offset;\n"
 130	"uniform ivec4 inflags;\n"
 131	"uniform ivec2 mosaic;\n"
 132	"OUT(0) out vec4 color;\n"
 133	"OUT(1) out vec4 flags;\n"
 134	FLAG_CONST
 135
 136	"vec4 renderTile(int tile, int paletteId, ivec2 localCoord);\n"
 137
 138	"void main() {\n"
 139	"	ivec2 coord = ivec2(texCoord);\n"
 140	"	if (mosaic.x > 1) {\n"
 141	"		coord.x -= coord.x % mosaic.x;\n"
 142	"	}\n"
 143	"	if (mosaic.y > 1) {\n"
 144	"		coord.y -= coord.y % mosaic.y;\n"
 145	"	}\n"
 146	"	coord += offset;\n"
 147	"	if ((size & 1) == 1) {\n"
 148	"		coord.y += coord.x & 256;\n"
 149	"	}\n"
 150	"	coord.x &= 255;\n"
 151	"	int mapAddress = screenBase + (coord.x >> 3) + (coord.y >> 3) * 32;\n"
 152	"	vec4 map = texelFetch(vram, ivec2(mapAddress & 255, mapAddress >> 8), 0);\n"
 153	"	int tileFlags = int(map.g * 15.9);\n"
 154	"	if ((tileFlags & 4) == 4) {\n"
 155	"		coord.x ^= 7;\n"
 156	"	}\n"
 157	"	if ((tileFlags & 8) == 8) {\n"
 158	"		coord.y ^= 7;\n"
 159	"	}\n"
 160	"	int tile = int(map.a * 15.9) + int(map.b * 15.9) * 16 + (tileFlags & 0x3) * 256;\n"
 161	"	color = renderTile(tile, int(map.r * 15.9), coord & 7);\n"
 162	"	flags = vec4(inflags) / flagCoeff;\n"
 163	"}";
 164
 165static const char* const _fetchTileOverflow =
 166	"vec4 fetchTile(ivec2 coord) {\n"
 167	"	int sizeAdjusted = (0x8000 << size) - 1;\n"
 168	"	coord &= sizeAdjusted;\n"
 169	"	return renderTile(coord);\n"
 170	"}";
 171
 172static const char* const _fetchTileNoOverflow =
 173	"vec4 fetchTile(ivec2 coord) {\n"
 174	"	int sizeAdjusted = (0x8000 << size) - 1;\n"
 175	"	ivec2 outerCoord = coord & ~sizeAdjusted;\n"
 176	"	if ((outerCoord.x | outerCoord.y) != 0) {\n"
 177	"		discard;\n"
 178	"	}\n"
 179	"	return renderTile(coord);\n"
 180	"}";
 181
 182static const struct GBAVideoGLUniform _uniformsMode2[] = {
 183	{ "loc", GBA_GL_VS_LOC, },
 184	{ "maxPos", GBA_GL_VS_MAXPOS, },
 185	{ "vram", GBA_GL_BG_VRAM, },
 186	{ "palette", GBA_GL_BG_PALETTE, },
 187	{ "screenBase", GBA_GL_BG_SCREENBASE, },
 188	{ "charBase", GBA_GL_BG_CHARBASE, },
 189	{ "size", GBA_GL_BG_SIZE, },
 190	{ "inflags", GBA_GL_BG_INFLAGS, },
 191	{ "offset", GBA_GL_BG_OFFSET, },
 192	{ "transform", GBA_GL_BG_TRANSFORM, },
 193	{ "range", GBA_GL_BG_RANGE, },
 194	{ "mosaic", GBA_GL_BG_MOSAIC, },
 195	{ 0 }
 196};
 197
 198static const char* const _interpolate =
 199	"vec2 interpolate(ivec2 arr[4], float x) {\n"
 200	"	float x1m = 1. - x;\n"
 201	"	return x1m * x1m * x1m * vec2(arr[0]) +"
 202		" 3. * x1m * x1m * x   * vec2(arr[1]) +"
 203		" 3. * x1m * x   * x   * vec2(arr[2]) +"
 204		"      x   * x   * x   * vec2(arr[3]);\n"
 205	"}\n"
 206
 207	"void loadAffine(int y, out ivec2 mat[4], out ivec2 aff[4]) {\n"
 208	"	int start = max(range.x, y - 3);\n"
 209	"	ivec4 splitOffset[4];\n"
 210
 211	"	mat[0] = texelFetch(transform, ivec2(0, start), 0).xz;\n"
 212	"	mat[1] = texelFetch(transform, ivec2(0, start + 1), 0).xz;\n"
 213	"	mat[2] = texelFetch(transform, ivec2(0, start + 2), 0).xz;\n"
 214	"	mat[3] = texelFetch(transform, ivec2(0, start + 3), 0).xz;\n"
 215
 216	"	splitOffset[0] = texelFetch(transform, ivec2(1, start + 0), 0);\n"
 217	"	splitOffset[1] = texelFetch(transform, ivec2(1, start + 1), 0);\n"
 218	"	splitOffset[2] = texelFetch(transform, ivec2(1, start + 2), 0);\n"
 219	"	splitOffset[3] = texelFetch(transform, ivec2(1, start + 3), 0);\n"
 220
 221	"	aff[0] = (splitOffset[0].xz & 0xFFFF) + (splitOffset[0].yw << 16);\n"
 222	"	aff[1] = (splitOffset[1].xz & 0xFFFF) + (splitOffset[1].yw << 16);\n"
 223	"	aff[2] = (splitOffset[2].xz & 0xFFFF) + (splitOffset[2].yw << 16);\n"
 224	"	aff[3] = (splitOffset[3].xz & 0xFFFF) + (splitOffset[3].yw << 16);\n"
 225
 226	"	if (y - 3 < range.x) {\n"
 227	"		ivec2 tempMat[3];\n"
 228	"		ivec2 tempAff[3];\n"
 229	"		tempMat[0] = ivec2(interpolate(mat, -0.75));\n"
 230	"		tempMat[1] = ivec2(interpolate(mat, -0.5));\n"
 231	"		tempMat[2] = ivec2(interpolate(mat, -0.25));\n"
 232	"		tempAff[0] = ivec2(interpolate(aff, -0.75));\n"
 233	"		tempAff[1] = ivec2(interpolate(aff, -0.5));\n"
 234	"		tempAff[2] = ivec2(interpolate(aff, -0.25));\n"
 235	"		if (range.x == y) {\n"
 236	"			mat[3] = mat[0];\n"
 237	"			mat[2] = tempMat[2];\n"
 238	"			mat[1] = tempMat[1];\n"
 239	"			mat[0] = tempMat[0];\n"
 240	"			aff[3] = aff[0];\n"
 241	"			aff[2] = tempAff[2];\n"
 242	"			aff[1] = tempAff[1];\n"
 243	"			aff[0] = tempAff[0];\n"
 244	"		} else if (range.x == y - 1) {\n"
 245	"			mat[3] = mat[1];\n"
 246	"			mat[2] = mat[0];\n"
 247	"			mat[1] = tempMat[2];\n"
 248	"			mat[0] = tempMat[1];\n"
 249	"			aff[3] = aff[1];\n"
 250	"			aff[2] = aff[0];\n"
 251	"			aff[1] = tempAff[2];\n"
 252	"			aff[0] = tempAff[1];\n"
 253	"		} else if (range.x == y - 2) {\n"
 254	"			mat[3] = mat[2];\n"
 255	"			mat[2] = mat[1];\n"
 256	"			mat[1] = mat[0];\n"
 257	"			mat[0] = tempMat[0];\n"
 258	"			aff[3] = aff[2];\n"
 259	"			aff[2] = aff[1];\n"
 260	"			aff[1] = aff[0];\n"
 261	"			aff[0] = tempAff[0];\n"
 262	"		}\n"
 263	"	}\n"
 264	"}\n";
 265
 266static const char* const _renderMode2 =
 267	"in vec2 texCoord;\n"
 268	"uniform sampler2D vram;\n"
 269	"uniform sampler2D palette;\n"
 270	"uniform int screenBase;\n"
 271	"uniform int charBase;\n"
 272	"uniform int size;\n"
 273	"uniform ivec4 inflags;\n"
 274	"uniform isampler2D transform;\n"
 275	"uniform ivec2 range;\n"
 276	"uniform ivec2 mosaic;\n"
 277	"OUT(0) out vec4 color;\n"
 278	"OUT(1) out vec4 flags;\n"
 279	FLAG_CONST
 280
 281	"vec4 fetchTile(ivec2 coord);\n"
 282	"vec2 interpolate(ivec2 arr[4], float x);\n"
 283	"void loadAffine(int y, out ivec2 mat[4], out ivec2 aff[4]);\n"
 284
 285	"vec4 renderTile(ivec2 coord) {\n"
 286	"	int map = (coord.x >> 11) + (((coord.y >> 7) & 0x7F0) << size);\n"
 287	"	int mapAddress = screenBase + (map >> 1);\n"
 288	"	vec4 twomaps = texelFetch(vram, ivec2(mapAddress & 255, mapAddress >> 8), 0);\n"
 289	"	int tile = int(twomaps[3 - 2 * (map & 1)] * 15.9) + int(twomaps[2 - 2 * (map & 1)] * 15.9) * 16;\n"
 290	"	int address = charBase + tile * 32 + ((coord.x >> 9) & 3) + ((coord.y >> 6) & 0x1C);\n"
 291	"	vec4 halfrow = texelFetch(vram, ivec2(address & 255, address >> 8), 0);\n"
 292	"	int entry = int(halfrow[3 - ((coord.x >> 7) & 2)] * 15.9);\n"
 293	"	int pal2 = int(halfrow[2 - ((coord.x >> 7) & 2)] * 15.9);\n"
 294	"	vec4 color = texelFetch(palette, ivec2(entry, pal2), 0);\n"
 295	"	if ((pal2 | entry) == 0) {\n"
 296	"		discard;\n"
 297	"	}\n"
 298	"	color.a = 1.;\n"
 299	"	return color;\n"
 300	"}\n"
 301
 302	"void main() {\n"
 303	"	ivec2 mat[4];\n"
 304	"	ivec2 offset[4];\n"
 305	"	vec2 incoord = texCoord;\n"
 306	"	if (mosaic.x > 1) {\n"
 307	"		incoord.x = float(int(incoord.x) % mosaic.x);\n"
 308	"	}\n"
 309	"	if (mosaic.y > 1) {\n"
 310	"		incoord.y = float(int(incoord.y) % mosaic.y);\n"
 311	"	}\n"
 312	"	loadAffine(int(incoord.y), mat, offset);\n"
 313	"	float y = fract(incoord.y);\n"
 314	"	float lin = 0.75 + y * 0.25;\n"
 315	"	vec2 mixedTransform = interpolate(mat, lin);\n"
 316	"	vec2 mixedOffset = interpolate(offset, lin);\n"
 317	"	color = fetchTile(ivec2(mixedTransform * incoord.x + mixedOffset));\n"
 318	"	flags = vec4(inflags) / flagCoeff;\n"
 319	"}";
 320
 321static const struct GBAVideoGLUniform _uniformsMode35[] = {
 322	{ "loc", GBA_GL_VS_LOC, },
 323	{ "maxPos", GBA_GL_VS_MAXPOS, },
 324	{ "vram", GBA_GL_BG_VRAM, },
 325	{ "charBase", GBA_GL_BG_CHARBASE, },
 326	{ "size", GBA_GL_BG_SIZE, },
 327	{ "inflags", GBA_GL_BG_INFLAGS, },
 328	{ "offset", GBA_GL_BG_OFFSET, },
 329	{ "transform", GBA_GL_BG_TRANSFORM, },
 330	{ "range", GBA_GL_BG_RANGE, },
 331	{ "mosaic", GBA_GL_BG_MOSAIC, },
 332	{ 0 }
 333};
 334
 335static const char* const _renderMode35 =
 336	"in vec2 texCoord;\n"
 337	"uniform sampler2D vram;\n"
 338	"uniform int charBase;\n"
 339	"uniform ivec2 size;\n"
 340	"uniform ivec4 inflags;\n"
 341	"uniform isampler2D transform;\n"
 342	"uniform ivec2 range;\n"
 343	"uniform ivec2 mosaic;\n"
 344	"OUT(0) out vec4 color;\n"
 345	"OUT(1) out vec4 flags;\n"
 346	FLAG_CONST
 347
 348	"vec2 interpolate(ivec2 arr[4], float x);\n"
 349	"void loadAffine(int y, out ivec2 mat[4], out ivec2 aff[4]);\n"
 350
 351	"void main() {\n"
 352	"	ivec2 mat[4];\n"
 353	"	ivec2 offset[4];\n"
 354	"	vec2 incoord = texCoord;\n"
 355	"	if (mosaic.x > 1) {\n"
 356	"		incoord.x = float(int(incoord.x) % mosaic.x);\n"
 357	"	}\n"
 358	"	if (mosaic.y > 1) {\n"
 359	"		incoord.y = float(int(incoord.y) % mosaic.y);\n"
 360	"	}\n"
 361	"	loadAffine(int(incoord.y), mat, offset);\n"
 362	"	float y = fract(incoord.y);\n"
 363	"	float lin = 0.75 + y * 0.25;\n"
 364	"	vec2 mixedTransform = interpolate(mat, lin);\n"
 365	"	vec2 mixedOffset = interpolate(offset, lin);\n"
 366	"	ivec2 coord = ivec2(mixedTransform * incoord.x + mixedOffset);\n"
 367	"	if (coord.x < 0 || coord.x >= (size.x << 8)) {\n"
 368	"		discard;\n"
 369	"	}\n"
 370	"	if (coord.y < 0 || coord.y >= (size.y << 8)) {\n"
 371	"		discard;\n"
 372	"	}\n"
 373	"	int address = charBase + (coord.x >> 8) + (coord.y >> 8) * size.x;\n"
 374	"	ivec4 entry = ivec4(texelFetch(vram, ivec2(address & 255, address >> 8), 0) * 15.9);\n"
 375	"	int sixteen = (entry.x << 12) | (entry.y << 8) | (entry.z << 4) | entry.w;\n"
 376	"	color = vec4(float(sixteen & 0x1F) / 31., float((sixteen >> 5) & 0x1F) / 31., float((sixteen >> 10) & 0x1F) / 31., 1.);\n"
 377	"	flags = vec4(inflags) / flagCoeff;\n"
 378	"}";
 379
 380static const struct GBAVideoGLUniform _uniformsMode4[] = {
 381	{ "loc", GBA_GL_VS_LOC, },
 382	{ "maxPos", GBA_GL_VS_MAXPOS, },
 383	{ "vram", GBA_GL_BG_VRAM, },
 384	{ "palette", GBA_GL_BG_PALETTE, },
 385	{ "charBase", GBA_GL_BG_CHARBASE, },
 386	{ "size", GBA_GL_BG_SIZE, },
 387	{ "inflags", GBA_GL_BG_INFLAGS, },
 388	{ "offset", GBA_GL_BG_OFFSET, },
 389	{ "transform", GBA_GL_BG_TRANSFORM, },
 390	{ "range", GBA_GL_BG_RANGE, },
 391	{ "mosaic", GBA_GL_BG_MOSAIC, },
 392	{ 0 }
 393};
 394
 395static const char* const _renderMode4 =
 396	"in vec2 texCoord;\n"
 397	"uniform sampler2D vram;\n"
 398	"uniform sampler2D palette;\n"
 399	"uniform int charBase;\n"
 400	"uniform ivec2 size;\n"
 401	"uniform ivec4 inflags;\n"
 402	"uniform isampler2D transform;\n"
 403	"uniform ivec2 range;\n"
 404	"uniform ivec2 mosaic;\n"
 405	"OUT(0) out vec4 color;\n"
 406	"OUT(1) out vec4 flags;\n"
 407	FLAG_CONST
 408
 409	"vec2 interpolate(ivec2 arr[4], float x);\n"
 410	"void loadAffine(int y, out ivec2 mat[4], out ivec2 aff[4]);\n"
 411
 412	"void main() {\n"
 413	"	ivec2 mat[4];\n"
 414	"	ivec2 offset[4];\n"
 415	"	vec2 incoord = texCoord;\n"
 416	"	if (mosaic.x > 1) {\n"
 417	"		incoord.x = float(int(incoord.x) % mosaic.x);\n"
 418	"	}\n"
 419	"	if (mosaic.y > 1) {\n"
 420	"		incoord.y = float(int(incoord.y) % mosaic.y);\n"
 421	"	}\n"
 422	"	loadAffine(int(incoord.y), mat, offset);\n"
 423	"	float y = fract(incoord.y);\n"
 424	"	float lin = 0.75 + y * 0.25;\n"
 425	"	vec2 mixedTransform = interpolate(mat, lin);\n"
 426	"	vec2 mixedOffset = interpolate(offset, lin);\n"
 427	"	ivec2 coord = ivec2(mixedTransform * incoord.x + mixedOffset);\n"
 428	"	if (coord.x < 0 || coord.x >= (size.x << 8)) {\n"
 429	"		discard;\n"
 430	"	}\n"
 431	"	if (coord.y < 0 || coord.y >= (size.y << 8)) {\n"
 432	"		discard;\n"
 433	"	}\n"
 434	"	int address = charBase + (coord.x >> 8) + (coord.y >> 8) * size.x;\n"
 435	"	vec4 twoEntries = texelFetch(vram, ivec2((address >> 1) & 255, address >> 9), 0);\n"
 436	"	ivec2 entry = ivec2(twoEntries[3 - 2 * (address & 1)] * 15.9, twoEntries[2 - 2 * (address & 1)] * 15.9);\n"
 437	"	color = texelFetch(palette, entry, 0);\n"
 438	"	color.a = 1.;\n"
 439	"	flags = vec4(inflags) / flagCoeff;\n"
 440	"}";
 441
 442static const struct GBAVideoGLUniform _uniformsObj[] = {
 443	{ "loc", GBA_GL_VS_LOC, },
 444	{ "maxPos", GBA_GL_VS_MAXPOS, },
 445	{ "vram", GBA_GL_OBJ_VRAM, },
 446	{ "palette", GBA_GL_OBJ_PALETTE, },
 447	{ "charBase", GBA_GL_OBJ_CHARBASE, },
 448	{ "stride", GBA_GL_OBJ_STRIDE, },
 449	{ "localPalette", GBA_GL_OBJ_LOCALPALETTE, },
 450	{ "inflags", GBA_GL_OBJ_INFLAGS, },
 451	{ "transform", GBA_GL_OBJ_TRANSFORM, },
 452	{ "dims", GBA_GL_OBJ_DIMS, },
 453	{ "objwin", GBA_GL_OBJ_OBJWIN, },
 454	{ "mosaic", GBA_GL_OBJ_MOSAIC, },
 455	{ 0 }
 456};
 457
 458static const char* const _renderObj =
 459	"in vec2 texCoord;\n"
 460	"uniform sampler2D vram;\n"
 461	"uniform sampler2D palette;\n"
 462	"uniform int charBase;\n"
 463	"uniform int stride;\n"
 464	"uniform int localPalette;\n"
 465	"uniform ivec4 inflags;\n"
 466	"uniform mat2x2 transform;\n"
 467	"uniform ivec4 dims;\n"
 468	"uniform vec4 objwin;\n"
 469	"uniform ivec4 mosaic;\n"
 470	"OUT(0) out vec4 color;\n"
 471	"OUT(1) out vec4 flags;\n"
 472	"OUT(2) out vec3 window;\n"
 473	FLAG_CONST
 474
 475	"vec4 renderTile(int tile, int paletteId, ivec2 localCoord);\n"
 476
 477	"void main() {\n"
 478	"	vec2 incoord = texCoord;\n"
 479	"	if (mosaic.x > 1) {\n"
 480	"		int x = int(incoord.x);\n"
 481	"		incoord.x = float(clamp(x - (mosaic.z + x) % mosaic.x, 0, dims.z - 1));\n"
 482	"	} else if (mosaic.x < -1) {\n"
 483	"		int x = dims.z - int(incoord.x) - 1;\n"
 484	"		incoord.x = float(clamp(dims.z - x + (mosaic.z + x) % -mosaic.x - 1, 0, dims.z - 1));\n"
 485	"	}\n"
 486	"	if (mosaic.y > 1) {\n"
 487	"		int y = int(incoord.y);\n"
 488	"		incoord.y = float(clamp(y - (mosaic.w + y) % mosaic.y, 0, dims.w - 1));\n"
 489	"	}\n"
 490	"	ivec2 coord = ivec2(transform * (incoord - vec2(dims.zw) / 2.) + vec2(dims.xy) / 2.);\n"
 491	"	if ((coord & ~(dims.xy - 1)) != ivec2(0, 0)) {\n"
 492	"		discard;\n"
 493	"	}\n"
 494	"	vec4 pix = renderTile((coord.x >> 3) + (coord.y >> 3) * stride, 16 + localPalette, coord & 7);\n"
 495	"	if (objwin.x > 0.) {\n"
 496	"		pix.a = 0.;\n"
 497	"	}\n"
 498	"	color = pix;\n"
 499	"	flags = vec4(inflags) / flagCoeff;\n"
 500	"	gl_FragDepth = flags.x;\n"
 501	"	window = objwin.yzw;\n"
 502	"}";
 503
 504static const struct GBAVideoGLUniform _uniformsFinalize[] = {
 505	{ "loc", GBA_GL_VS_LOC, },
 506	{ "maxPos", GBA_GL_VS_MAXPOS, },
 507	{ "scale", GBA_GL_FINALIZE_SCALE, },
 508	{ "layers", GBA_GL_FINALIZE_LAYERS, },
 509	{ "flags", GBA_GL_FINALIZE_FLAGS, },
 510	{ "window", GBA_GL_FINALIZE_WINDOW, },
 511	{ "backdrop", GBA_GL_FINALIZE_BACKDROP, },
 512	{ "backdropFlags", GBA_GL_FINALIZE_BACKDROPFLAGS, },
 513	{ 0 }
 514};
 515
 516static const char* const _finalize =
 517	"in vec2 texCoord;\n"
 518	"uniform int scale;\n"
 519	"uniform sampler2D layers[5];\n"
 520	"uniform sampler2D flags[5];\n"
 521	"uniform sampler2D window;\n"
 522	"uniform sampler2D backdrop;\n"
 523	"uniform sampler2D backdropFlags;\n"
 524	FLAG_CONST
 525	"out vec4 color;\n"
 526
 527	"void composite(vec4 pixel, ivec4 flags, inout vec4 topPixel, inout ivec4 topFlags, inout vec4 bottomPixel, inout ivec4 bottomFlags) {\n"
 528	"	if (pixel.a == 0.) {\n"
 529	"		return;\n"
 530	"	}\n"
 531	"	if (flags.x >= topFlags.x) {\n"
 532	"		if (flags.x >= bottomFlags.x) {\n"
 533	"			return;\n"
 534	"		}\n"
 535	"		bottomFlags = flags;\n"
 536	"		bottomPixel = pixel;\n"
 537	"	} else {\n"
 538	"		bottomFlags = topFlags;\n"
 539	"		topFlags = flags;\n"
 540	"		bottomPixel = topPixel;\n"
 541	"		topPixel = pixel;\n"
 542	"	}\n"
 543	"}\n"
 544
 545	"void main() {\n"
 546	"	vec4 topPixel = texelFetch(backdrop, ivec2(0, texCoord.y), 0);\n"
 547	"	vec4 bottomPixel = topPixel;\n"
 548	"	ivec4 topFlags = ivec4(texelFetch(backdropFlags, ivec2(0, texCoord.y), 0) * flagCoeff);\n"
 549	"	ivec4 bottomFlags = topFlags;\n"
 550	"	vec4 windowFlags = texelFetch(window, ivec2(texCoord * float(scale)), 0);\n"
 551	"	int layerWindow = int(windowFlags.x * 128.);\n"
 552	"	if ((layerWindow & 1) == 0) {\n"
 553	"		vec4 pix = texelFetch(layers[0], ivec2(texCoord * float(scale)), 0);\n"
 554	"		ivec4 inflags = ivec4(texelFetch(flags[0], ivec2(texCoord * float(scale)), 0).xyz * flagCoeff.xyz, 0);\n"
 555	"		composite(pix, inflags, topPixel, topFlags, bottomPixel, bottomFlags);\n"
 556	"	}\n"
 557	"	if ((layerWindow & 2) == 0) {\n"
 558	"		vec4 pix = texelFetch(layers[1], ivec2(texCoord * float(scale)), 0);\n"
 559	"		ivec4 inflags = ivec4(texelFetch(flags[1], ivec2(texCoord * float(scale)), 0).xyz * flagCoeff.xyz, 0);\n"
 560	"		composite(pix, inflags, topPixel, topFlags, bottomPixel, bottomFlags);\n"
 561	"	}\n"
 562	"	if ((layerWindow & 4) == 0) {\n"
 563	"		vec4 pix = texelFetch(layers[2], ivec2(texCoord * float(scale)), 0);\n"
 564	"		ivec4 inflags = ivec4(texelFetch(flags[2], ivec2(texCoord * float(scale)), 0).xyz * flagCoeff.xyz, 0);\n"
 565	"		composite(pix, inflags, topPixel, topFlags, bottomPixel, bottomFlags);\n"
 566	"	}\n"
 567	"	if ((layerWindow & 8) == 0) {\n"
 568	"		vec4 pix = texelFetch(layers[3], ivec2(texCoord * float(scale)), 0);\n"
 569	"		ivec4 inflags = ivec4(texelFetch(flags[3], ivec2(texCoord * float(scale)), 0).xyz * flagCoeff.xyz, 0);\n"
 570	"		composite(pix, inflags, topPixel, topFlags, bottomPixel, bottomFlags);\n"
 571	"	}\n"
 572	"	if ((layerWindow & 16) == 0) {\n"
 573	"		vec4 pix = texelFetch(layers[4], ivec2(texCoord * float(scale)), 0);\n"
 574	"		ivec4 inflags = ivec4(texelFetch(flags[4], ivec2(texCoord * float(scale)), 0) * flagCoeff);\n"
 575	"		composite(pix, inflags, topPixel, topFlags, bottomPixel, bottomFlags);\n"
 576	"	}\n"
 577	"	if ((layerWindow & 32) != 0) {\n"
 578	"		topFlags.y &= ~1;\n"
 579	"	}\n"
 580	"	if (((topFlags.y & 13) == 5 || topFlags.w > 0) && (bottomFlags.y & 2) == 2) {\n"
 581	"		topPixel *= float(topFlags.z) / 16.;\n"
 582	"		topPixel += bottomPixel * windowFlags.y;\n"
 583	"	} else if ((topFlags.y & 13) == 9) {\n"
 584	"		topPixel += (1. - topPixel) * windowFlags.z;\n"
 585	"	} else if ((topFlags.y & 13) == 13) {\n"
 586	"		topPixel -= topPixel * windowFlags.z;\n"
 587	"	}\n"
 588	"	color = topPixel;\n"
 589	"}";
 590
 591static const GLint _vertices[] = {
 592	0, 0,
 593	0, 1,
 594	1, 1,
 595	1, 0,
 596};
 597
 598void GBAVideoGLRendererCreate(struct GBAVideoGLRenderer* renderer) {
 599	renderer->d.init = GBAVideoGLRendererInit;
 600	renderer->d.reset = GBAVideoGLRendererReset;
 601	renderer->d.deinit = GBAVideoGLRendererDeinit;
 602	renderer->d.writeVideoRegister = GBAVideoGLRendererWriteVideoRegister;
 603	renderer->d.writeVRAM = GBAVideoGLRendererWriteVRAM;
 604	renderer->d.writeOAM = GBAVideoGLRendererWriteOAM;
 605	renderer->d.writePalette = GBAVideoGLRendererWritePalette;
 606	renderer->d.drawScanline = GBAVideoGLRendererDrawScanline;
 607	renderer->d.finishFrame = GBAVideoGLRendererFinishFrame;
 608	renderer->d.getPixels = GBAVideoGLRendererGetPixels;
 609	renderer->d.putPixels = GBAVideoGLRendererPutPixels;
 610
 611	renderer->d.disableBG[0] = false;
 612	renderer->d.disableBG[1] = false;
 613	renderer->d.disableBG[2] = false;
 614	renderer->d.disableBG[3] = false;
 615	renderer->d.disableOBJ = false;
 616
 617	renderer->scale = 1;
 618}
 619
 620static void _compileShader(struct GBAVideoGLRenderer* glRenderer, struct GBAVideoGLShader* shader, const char** shaderBuffer, int shaderBufferLines, GLuint vs, const struct GBAVideoGLUniform* uniforms, char* log) {
 621	GLuint program = glCreateProgram();
 622	shader->program = program;
 623
 624	GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
 625	glAttachShader(program, vs);
 626	glAttachShader(program, fs);
 627	glShaderSource(fs, shaderBufferLines, shaderBuffer, 0);
 628	glCompileShader(fs);
 629	glGetShaderInfoLog(fs, 2048, 0, log);
 630	if (log[0]) {
 631		mLOG(GBA_VIDEO, ERROR, "Fragment shader compilation failure: %s", log);
 632	}
 633	glLinkProgram(program);
 634	glGetProgramInfoLog(program, 2048, 0, log);
 635	if (log[0]) {
 636		mLOG(GBA_VIDEO, ERROR, "Program link failure: %s", log);
 637	}
 638	glDeleteShader(fs);
 639#ifndef BUILD_GLES3
 640	glBindFragDataLocation(program, 0, "color");
 641	glBindFragDataLocation(program, 1, "flags");
 642#endif
 643
 644	glGenVertexArrays(1, &shader->vao);
 645	glBindVertexArray(shader->vao);
 646	glBindBuffer(GL_ARRAY_BUFFER, glRenderer->vbo);
 647	GLuint positionLocation = glGetAttribLocation(program, "position");
 648	glEnableVertexAttribArray(positionLocation);
 649	glVertexAttribPointer(positionLocation, 2, GL_INT, GL_FALSE, 0, NULL);
 650
 651	size_t i;
 652	for (i = 0; uniforms[i].name; ++i) {
 653		shader->uniforms[uniforms[i].type] = glGetUniformLocation(program, uniforms[i].name);
 654	}
 655}
 656
 657static void _deleteShader(struct GBAVideoGLShader* shader) {
 658	glDeleteProgram(shader->program);
 659	glDeleteVertexArrays(1, &shader->vao);
 660}
 661
 662static void _initFramebufferTextureEx(GLuint tex, GLenum internalFormat, GLenum format, GLenum type, GLenum attachment, int scale) {
 663	glBindTexture(GL_TEXTURE_2D, tex);
 664	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
 665	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
 666	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
 667	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
 668	glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, scale > 0 ? GBA_VIDEO_HORIZONTAL_PIXELS * scale : 1, GBA_VIDEO_VERTICAL_PIXELS * (scale > 0 ? scale : 1), 0, format, type, 0);
 669	glFramebufferTexture2D(GL_FRAMEBUFFER, attachment, GL_TEXTURE_2D, tex, 0);	
 670}
 671
 672static void _initFramebufferTexture(GLuint tex, GLenum format, GLenum attachment, int scale) {
 673	_initFramebufferTextureEx(tex, format, format, GL_UNSIGNED_BYTE, attachment, scale);
 674}
 675
 676void GBAVideoGLRendererInit(struct GBAVideoRenderer* renderer) {
 677	struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;
 678	glRenderer->temporaryBuffer = NULL;
 679
 680	glGenFramebuffers(GBA_GL_FBO_MAX, glRenderer->fbo);
 681	glGenTextures(GBA_GL_TEX_MAX, glRenderer->layers);
 682
 683	glGenTextures(1, &glRenderer->paletteTex);
 684	glBindTexture(GL_TEXTURE_2D, glRenderer->paletteTex);
 685	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
 686	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
 687
 688	glGenTextures(1, &glRenderer->vramTex);
 689	glBindTexture(GL_TEXTURE_2D, glRenderer->vramTex);
 690	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
 691	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
 692	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA4, 256, 192, 0, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, 0);
 693
 694	glBindTexture(GL_TEXTURE_2D, glRenderer->layers[GBA_GL_TEX_AFFINE_2]);
 695	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
 696	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
 697	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
 698	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
 699	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16I, 2, GBA_VIDEO_VERTICAL_PIXELS, 0, GL_RGBA_INTEGER, GL_SHORT, NULL);
 700	glBindTexture(GL_TEXTURE_2D, glRenderer->layers[GBA_GL_TEX_AFFINE_3]);
 701	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
 702	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
 703	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
 704	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
 705	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16I, 2, GBA_VIDEO_VERTICAL_PIXELS, 0, GL_RGBA_INTEGER, GL_SHORT, NULL);
 706
 707	glBindFramebuffer(GL_FRAMEBUFFER, glRenderer->fbo[GBA_GL_FBO_OBJ]);
 708	_initFramebufferTexture(glRenderer->layers[GBA_GL_TEX_OBJ_COLOR], GL_RGBA, GL_COLOR_ATTACHMENT0, glRenderer->scale);
 709	_initFramebufferTexture(glRenderer->layers[GBA_GL_TEX_OBJ_FLAGS], GL_RGBA, GL_COLOR_ATTACHMENT1, glRenderer->scale);
 710	_initFramebufferTexture(glRenderer->layers[GBA_GL_TEX_WINDOW], GL_RGBA, GL_COLOR_ATTACHMENT2, glRenderer->scale);
 711	_initFramebufferTextureEx(glRenderer->layers[GBA_GL_TEX_OBJ_DEPTH], GL_DEPTH_COMPONENT16, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, GL_DEPTH_ATTACHMENT, glRenderer->scale);
 712
 713	glBindFramebuffer(GL_FRAMEBUFFER, glRenderer->fbo[GBA_GL_FBO_BACKDROP]);
 714	_initFramebufferTexture(glRenderer->layers[GBA_GL_TEX_BACKDROP_COLOR], GL_RGB, GL_COLOR_ATTACHMENT0, 0);
 715	_initFramebufferTexture(glRenderer->layers[GBA_GL_TEX_BACKDROP_FLAGS], GL_RGBA, GL_COLOR_ATTACHMENT1, 0);
 716
 717	glBindFramebuffer(GL_FRAMEBUFFER, glRenderer->fbo[GBA_GL_FBO_WINDOW]);
 718	_initFramebufferTexture(glRenderer->layers[GBA_GL_TEX_WINDOW], GL_RGB, GL_COLOR_ATTACHMENT0, glRenderer->scale);
 719
 720	glBindFramebuffer(GL_FRAMEBUFFER, glRenderer->fbo[GBA_GL_FBO_OUTPUT]);
 721	_initFramebufferTexture(glRenderer->outputTex, GL_RGB, GL_COLOR_ATTACHMENT0, glRenderer->scale);
 722
 723	glBindFramebuffer(GL_FRAMEBUFFER, 0);
 724
 725	glGenBuffers(1, &glRenderer->vbo);
 726	glBindBuffer(GL_ARRAY_BUFFER, glRenderer->vbo);
 727	glBufferData(GL_ARRAY_BUFFER, sizeof(_vertices), _vertices, GL_STATIC_DRAW);
 728
 729	int i;
 730	for (i = 0; i < 4; ++i) {
 731		struct GBAVideoGLBackground* bg = &glRenderer->bg[i];
 732		bg->index = i;
 733		bg->enabled = 0;
 734		bg->priority = 0;
 735		bg->charBase = 0;
 736		bg->mosaic = 0;
 737		bg->multipalette = 0;
 738		bg->screenBase = 0;
 739		bg->overflow = 0;
 740		bg->size = 0;
 741		bg->target1 = 0;
 742		bg->target2 = 0;
 743		bg->x = 0;
 744		bg->y = 0;
 745		bg->refx = 0;
 746		bg->refy = 0;
 747		bg->affine.dx = 256;
 748		bg->affine.dmx = 0;
 749		bg->affine.dy = 0;
 750		bg->affine.dmy = 256;
 751		bg->affine.sx = 0;
 752		bg->affine.sy = 0;
 753		glGenFramebuffers(1, &bg->fbo);
 754		glGenTextures(1, &bg->tex);
 755		glGenTextures(1, &bg->flags);
 756		glBindFramebuffer(GL_FRAMEBUFFER, bg->fbo);
 757		_initFramebufferTexture(bg->tex, GL_RGBA, GL_COLOR_ATTACHMENT0, glRenderer->scale);
 758		_initFramebufferTexture(bg->flags, GL_RGB, GL_COLOR_ATTACHMENT1, glRenderer->scale);
 759		glBindFramebuffer(GL_FRAMEBUFFER, 0);
 760	}
 761
 762	char log[2048];
 763	const GLchar* shaderBuffer[4];
 764	const GLubyte* version = glGetString(GL_VERSION);
 765	if (strncmp((const char*) version, "OpenGL ES ", strlen("OpenGL ES "))) {
 766		shaderBuffer[0] = _gl3Header;
 767	} else {
 768		shaderBuffer[0] = _gles3Header;
 769	}
 770
 771	GLuint vs = glCreateShader(GL_VERTEX_SHADER);
 772	shaderBuffer[1] = _vertexShader;
 773	glShaderSource(vs, 2, shaderBuffer, 0);
 774	glCompileShader(vs);
 775	glGetShaderInfoLog(vs, 2048, 0, log);
 776	if (log[0]) {
 777		mLOG(GBA_VIDEO, ERROR, "Vertex shader compilation failure: %s", log);
 778	}
 779
 780	shaderBuffer[1] = _renderMode0;
 781
 782	shaderBuffer[2] = _renderTile16;
 783	_compileShader(glRenderer, &glRenderer->bgShader[0], shaderBuffer, 3, vs, _uniformsMode0, log);
 784
 785	shaderBuffer[2] = _renderTile256;
 786	_compileShader(glRenderer, &glRenderer->bgShader[1], shaderBuffer, 3, vs, _uniformsMode0, log);
 787
 788	shaderBuffer[1] = _renderMode2;
 789	shaderBuffer[2] = _interpolate;
 790
 791	shaderBuffer[3] = _fetchTileOverflow;
 792	_compileShader(glRenderer, &glRenderer->bgShader[2], shaderBuffer, 4, vs, _uniformsMode2, log);
 793
 794	shaderBuffer[3] = _fetchTileNoOverflow;
 795	_compileShader(glRenderer, &glRenderer->bgShader[3], shaderBuffer, 4, vs, _uniformsMode2, log);
 796
 797	shaderBuffer[1] = _renderMode4;
 798	shaderBuffer[2] = _interpolate;
 799	_compileShader(glRenderer, &glRenderer->bgShader[4], shaderBuffer, 3, vs, _uniformsMode4, log);
 800
 801	shaderBuffer[1] = _renderMode35;
 802	shaderBuffer[2] = _interpolate;
 803	_compileShader(glRenderer, &glRenderer->bgShader[5], shaderBuffer, 3, vs, _uniformsMode35, log);
 804
 805	shaderBuffer[1] = _renderObj;
 806
 807	shaderBuffer[2] = _renderTile16;
 808	_compileShader(glRenderer, &glRenderer->objShader[0], shaderBuffer, 3, vs, _uniformsObj, log);
 809#ifndef BUILD_GLES3
 810	glBindFragDataLocation(glRenderer->objShader[0].program, 2, "window");
 811#endif
 812
 813	shaderBuffer[2] = _renderTile256;
 814	_compileShader(glRenderer, &glRenderer->objShader[1], shaderBuffer, 3, vs, _uniformsObj, log);
 815#ifndef BUILD_GLES3
 816	glBindFragDataLocation(glRenderer->objShader[1].program, 2, "window");
 817#endif
 818
 819	shaderBuffer[1] = _finalize;
 820	_compileShader(glRenderer, &glRenderer->finalizeShader, shaderBuffer, 2, vs, _uniformsFinalize, log);
 821
 822	glBindVertexArray(0);
 823	glDeleteShader(vs);
 824
 825	GBAVideoGLRendererReset(renderer);
 826}
 827
 828void GBAVideoGLRendererDeinit(struct GBAVideoRenderer* renderer) {
 829	struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;
 830	if (glRenderer->temporaryBuffer) {
 831		mappedMemoryFree(glRenderer->temporaryBuffer, GBA_VIDEO_HORIZONTAL_PIXELS * GBA_VIDEO_VERTICAL_PIXELS * glRenderer->scale * glRenderer->scale);
 832	}
 833	glDeleteFramebuffers(GBA_GL_FBO_MAX, glRenderer->fbo);
 834	glDeleteTextures(GBA_GL_TEX_MAX, glRenderer->layers);
 835	glDeleteTextures(1, &glRenderer->paletteTex);
 836	glDeleteTextures(1, &glRenderer->vramTex);
 837
 838	_deleteShader(&glRenderer->bgShader[0]);
 839	_deleteShader(&glRenderer->bgShader[1]);
 840	_deleteShader(&glRenderer->bgShader[2]);
 841	_deleteShader(&glRenderer->bgShader[3]);
 842	_deleteShader(&glRenderer->objShader[0]);
 843	_deleteShader(&glRenderer->objShader[1]);
 844	_deleteShader(&glRenderer->finalizeShader);
 845
 846	int i;
 847	for (i = 0; i < 4; ++i) {
 848		struct GBAVideoGLBackground* bg = &glRenderer->bg[i];
 849		glDeleteFramebuffers(1, &bg->fbo);
 850		glDeleteTextures(1, &bg->tex);
 851		glDeleteTextures(1, &bg->flags);
 852	}
 853}
 854
 855void GBAVideoGLRendererReset(struct GBAVideoRenderer* renderer) {
 856	struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;
 857
 858#ifdef BUILD_GLES3
 859	int i;
 860	for (i = 0; i < 512; ++i) {
 861		renderer->writePalette(renderer, i << 1, renderer->palette[i]);
 862	}
 863#else
 864	glRenderer->paletteDirty = true;
 865#endif
 866	glRenderer->vramDirty = 0xFFFFFF;
 867	glRenderer->firstAffine = -1;
 868	glRenderer->firstY = -1;
 869	glRenderer->dispcnt = 0x0080;
 870	glRenderer->mosaic = 0;
 871	memset(glRenderer->shadowRegs, 0, sizeof(glRenderer->shadowRegs));
 872	glRenderer->regsDirty = 0xFFFFFFFFFFFEULL;
 873}
 874
 875void GBAVideoGLRendererWriteVRAM(struct GBAVideoRenderer* renderer, uint32_t address) {
 876	struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;
 877	glRenderer->vramDirty |= 1 << (address >> 12);
 878}
 879
 880void GBAVideoGLRendererWriteOAM(struct GBAVideoRenderer* renderer, uint32_t oam) {
 881	UNUSED(oam);
 882	struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;
 883	glRenderer->oamDirty = true;
 884}
 885
 886void GBAVideoGLRendererWritePalette(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
 887	struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;
 888#ifdef BUILD_GLES3
 889	glRenderer->shadowPalette[address >> 1] = ((value & 0x1F) << 11) | ((value & 0x3E0) << 1) | ((value & 0x7C00) >> 10);
 890#else
 891	UNUSED(address);
 892	UNUSED(value);
 893#endif
 894	glRenderer->paletteDirty = true;
 895}
 896
 897uint16_t GBAVideoGLRendererWriteVideoRegister(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
 898	struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;
 899	if (renderer->cache) {
 900		GBAVideoCacheWriteVideoRegister(renderer->cache, address, value);
 901	}
 902
 903	bool dirty = true;
 904	switch (address) {
 905	case REG_DISPCNT:
 906		value &= 0xFFF7;
 907		break;
 908	case REG_BG0CNT:
 909	case REG_BG1CNT:
 910		value &= 0xDFFF;
 911		break;
 912	case REG_BG0HOFS:
 913	case REG_BG0VOFS:
 914	case REG_BG1HOFS:
 915	case REG_BG1VOFS:
 916	case REG_BG2HOFS:
 917	case REG_BG2VOFS:
 918	case REG_BG3HOFS:
 919	case REG_BG3VOFS:
 920		value &= 0x01FF;
 921		break;
 922	case REG_BG2PA:
 923		glRenderer->bg[2].affine.dx = value;
 924		dirty = false;
 925		break;
 926	case REG_BG2PB:
 927		glRenderer->bg[2].affine.dmx = value;
 928		dirty = false;
 929		break;
 930	case REG_BG2PC:
 931		glRenderer->bg[2].affine.dy = value;
 932		dirty = false;
 933		break;
 934	case REG_BG2PD:
 935		glRenderer->bg[2].affine.dmy = value;
 936		dirty = false;
 937		break;
 938	case REG_BG2X_LO:
 939		GBAVideoGLRendererWriteBGX_LO(&glRenderer->bg[2], value);
 940		dirty = false;
 941		break;
 942	case REG_BG2X_HI:
 943		GBAVideoGLRendererWriteBGX_HI(&glRenderer->bg[2], value);
 944		dirty = false;
 945		break;
 946	case REG_BG2Y_LO:
 947		GBAVideoGLRendererWriteBGY_LO(&glRenderer->bg[2], value);
 948		dirty = false;
 949		break;
 950	case REG_BG2Y_HI:
 951		GBAVideoGLRendererWriteBGY_HI(&glRenderer->bg[2], value);
 952		dirty = false;
 953		break;
 954	case REG_BG3PA:
 955		glRenderer->bg[3].affine.dx = value;
 956		dirty = false;
 957		break;
 958	case REG_BG3PB:
 959		glRenderer->bg[3].affine.dmx = value;
 960		dirty = false;
 961		break;
 962	case REG_BG3PC:
 963		glRenderer->bg[3].affine.dy = value;
 964		dirty = false;
 965		break;
 966	case REG_BG3PD:
 967		glRenderer->bg[3].affine.dmy = value;
 968		dirty = false;
 969		break;
 970	case REG_BG3X_LO:
 971		GBAVideoGLRendererWriteBGX_LO(&glRenderer->bg[3], value);
 972		dirty = false;
 973		break;
 974	case REG_BG3X_HI:
 975		GBAVideoGLRendererWriteBGX_HI(&glRenderer->bg[3], value);
 976		dirty = false;
 977		break;
 978	case REG_BG3Y_LO:
 979		GBAVideoGLRendererWriteBGY_LO(&glRenderer->bg[3], value);
 980		dirty = false;
 981		break;
 982	case REG_BG3Y_HI:
 983		GBAVideoGLRendererWriteBGY_HI(&glRenderer->bg[3], value);
 984		dirty = false;
 985		break;
 986	case REG_BLDALPHA:
 987		value &= 0x1F1F;
 988		break;
 989	case REG_BLDY:
 990		value &= 0x1F;
 991		if (value > 0x10) {
 992			value = 0x10;
 993		}
 994		break;
 995	case REG_WININ:
 996		value &= 0x3F3F;
 997		break;
 998	case REG_WINOUT:
 999		value &= 0x3F3F;
1000		break;
1001	default:
1002		break;
1003	}
1004	if (glRenderer->shadowRegs[address >> 1] == value) {
1005		dirty = false;
1006	} else {
1007		glRenderer->shadowRegs[address >> 1] = value;
1008	}
1009	if (dirty) {
1010		glRenderer->regsDirty |= 1ULL << (address >> 1);
1011	}
1012	return value;
1013}
1014
1015void _cleanRegister(struct GBAVideoGLRenderer* glRenderer, int address, uint16_t value) {
1016	switch (address) {
1017	case REG_DISPCNT:
1018		glRenderer->dispcnt = value;
1019		GBAVideoGLRendererUpdateDISPCNT(glRenderer);
1020		break;
1021	case REG_BG0CNT:
1022		GBAVideoGLRendererWriteBGCNT(&glRenderer->bg[0], value);
1023		break;
1024	case REG_BG1CNT:
1025		GBAVideoGLRendererWriteBGCNT(&glRenderer->bg[1], value);
1026		break;
1027	case REG_BG2CNT:
1028		GBAVideoGLRendererWriteBGCNT(&glRenderer->bg[2], value);
1029		break;
1030	case REG_BG3CNT:
1031		GBAVideoGLRendererWriteBGCNT(&glRenderer->bg[3], value);
1032		break;
1033	case REG_BG0HOFS:
1034		glRenderer->bg[0].x = value;
1035		break;
1036	case REG_BG0VOFS:
1037		glRenderer->bg[0].y = value;
1038		break;
1039	case REG_BG1HOFS:
1040		glRenderer->bg[1].x = value;
1041		break;
1042	case REG_BG1VOFS:
1043		glRenderer->bg[1].y = value;
1044		break;
1045	case REG_BG2HOFS:
1046		glRenderer->bg[2].x = value;
1047		break;
1048	case REG_BG2VOFS:
1049		glRenderer->bg[2].y = value;
1050		break;
1051	case REG_BG3HOFS:
1052		glRenderer->bg[3].x = value;
1053		break;
1054	case REG_BG3VOFS:
1055		glRenderer->bg[3].y = value;
1056		break;
1057	case REG_BLDCNT:
1058		GBAVideoGLRendererWriteBLDCNT(glRenderer, value);
1059		value &= 0x3FFF;
1060		break;
1061	case REG_BLDALPHA:
1062		glRenderer->blda = value & 0x1F;
1063		if (glRenderer->blda > 0x10) {
1064			glRenderer->blda = 0x10;
1065		}
1066		glRenderer->bldb = (value >> 8) & 0x1F;
1067		if (glRenderer->bldb > 0x10) {
1068			glRenderer->bldb = 0x10;
1069		}
1070		value &= 0x1F1F;
1071		break;
1072	case REG_BLDY:
1073		glRenderer->bldy = value;
1074		break;
1075	case REG_WIN0H:
1076		glRenderer->winN[0].h[0].end = value;
1077		glRenderer->winN[0].h[0].start = value >> 8;
1078		if (glRenderer->winN[0].h[0].start > GBA_VIDEO_HORIZONTAL_PIXELS && glRenderer->winN[0].h[0].start > glRenderer->winN[0].h[0].end) {
1079			glRenderer->winN[0].h[0].start = 0;
1080		}
1081		if (glRenderer->winN[0].h[0].end > GBA_VIDEO_HORIZONTAL_PIXELS) {
1082			glRenderer->winN[0].h[0].end = GBA_VIDEO_HORIZONTAL_PIXELS;
1083			if (glRenderer->winN[0].h[0].start > GBA_VIDEO_HORIZONTAL_PIXELS) {
1084				glRenderer->winN[0].h[0].start = GBA_VIDEO_HORIZONTAL_PIXELS;
1085			}
1086		}
1087		break;
1088	case REG_WIN1H:
1089		glRenderer->winN[1].h[0].end = value;
1090		glRenderer->winN[1].h[0].start = value >> 8;
1091		if (glRenderer->winN[1].h[0].start > GBA_VIDEO_HORIZONTAL_PIXELS && glRenderer->winN[1].h[0].start > glRenderer->winN[1].h[0].end) {
1092			glRenderer->winN[1].h[0].start = 0;
1093		}
1094		if (glRenderer->winN[1].h[0].end > GBA_VIDEO_HORIZONTAL_PIXELS) {
1095			glRenderer->winN[1].h[0].end = GBA_VIDEO_HORIZONTAL_PIXELS;
1096			if (glRenderer->winN[1].h[0].start > GBA_VIDEO_HORIZONTAL_PIXELS) {
1097				glRenderer->winN[1].h[0].start = GBA_VIDEO_HORIZONTAL_PIXELS;
1098			}
1099		}
1100		break;
1101	case REG_WIN0V:
1102		glRenderer->winN[0].v.end = value;
1103		glRenderer->winN[0].v.start = value >> 8;
1104		if (glRenderer->winN[0].v.start > GBA_VIDEO_VERTICAL_PIXELS && glRenderer->winN[0].v.start > glRenderer->winN[0].v.end) {
1105			glRenderer->winN[0].v.start = 0;
1106		}
1107		if (glRenderer->winN[0].v.end > GBA_VIDEO_VERTICAL_PIXELS) {
1108			glRenderer->winN[0].v.end = GBA_VIDEO_VERTICAL_PIXELS;
1109			if (glRenderer->winN[0].v.start > GBA_VIDEO_VERTICAL_PIXELS) {
1110				glRenderer->winN[0].v.start = GBA_VIDEO_VERTICAL_PIXELS;
1111			}
1112		}
1113		break;
1114	case REG_WIN1V:
1115		glRenderer->winN[1].v.end = value;
1116		glRenderer->winN[1].v.start = value >> 8;
1117		if (glRenderer->winN[1].v.start > GBA_VIDEO_VERTICAL_PIXELS && glRenderer->winN[1].v.start > glRenderer->winN[1].v.end) {
1118			glRenderer->winN[1].v.start = 0;
1119		}
1120		if (glRenderer->winN[1].v.end > GBA_VIDEO_VERTICAL_PIXELS) {
1121			glRenderer->winN[1].v.end = GBA_VIDEO_VERTICAL_PIXELS;
1122			if (glRenderer->winN[1].v.start > GBA_VIDEO_VERTICAL_PIXELS) {
1123				glRenderer->winN[1].v.start = GBA_VIDEO_VERTICAL_PIXELS;
1124			}
1125		}
1126		break;
1127	case REG_WININ:
1128		glRenderer->winN[0].control = value;
1129		glRenderer->winN[1].control = value >> 8;
1130		break;
1131	case REG_WINOUT:
1132		glRenderer->winout = value;
1133		glRenderer->objwin = value >> 8;
1134		break;
1135	case REG_MOSAIC:
1136		glRenderer->mosaic = value;
1137		break;
1138	default:
1139		break;
1140	}
1141}
1142
1143static bool _dirtyMode0(struct GBAVideoGLRenderer* renderer, struct GBAVideoGLBackground* background, int y) {
1144	UNUSED(y);
1145	if (!background->enabled) {
1146		return false;
1147	}
1148	unsigned screenBase = background->screenBase >> 11; // Lops off one extra bit
1149	unsigned screenMask = (7 << screenBase) & 0xFFFF; // Technically overzealous
1150	if (renderer->vramDirty & screenMask) {
1151		return true;
1152	}
1153	unsigned charBase = background->charBase >> 11;
1154	unsigned charMask = (0xFFFF << charBase) & 0xFFFF;
1155	if (renderer->vramDirty & charMask) {
1156		return true;
1157	}
1158	return false;
1159}
1160
1161static bool _dirtyMode2(struct GBAVideoGLRenderer* renderer, struct GBAVideoGLBackground* background, int y) {
1162	UNUSED(y);
1163	if (!background->enabled) {
1164		return false;
1165	}
1166	unsigned screenBase = background->screenBase >> 11; // Lops off one extra bit
1167	unsigned screenMask = (0xF << screenBase) & 0xFFFF;
1168	if (renderer->vramDirty & screenMask) {
1169		return true;
1170	}
1171	unsigned charBase = background->charBase >> 11;
1172	unsigned charMask = (0x3FFF << charBase) & 0xFFFF;
1173	if (renderer->vramDirty & charMask) {
1174		return true;
1175	}
1176	return false;
1177}
1178
1179static bool _dirtyMode3(struct GBAVideoGLRenderer* renderer, struct GBAVideoGLBackground* background, int y) {
1180	UNUSED(y);
1181	if (!background->enabled) {
1182		return false;
1183	}
1184	if (renderer->vramDirty & 0xFFFFF) {
1185		return true;
1186	}
1187	return false;
1188}
1189
1190static bool _dirtyMode45(struct GBAVideoGLRenderer* renderer, struct GBAVideoGLBackground* background, int y) {
1191	UNUSED(y);
1192	if (!background->enabled) {
1193		return false;
1194	}
1195	int start = GBARegisterDISPCNTIsFrameSelect(renderer->dispcnt) ? 5 : 0;
1196	int mask = 0x3FF << start;
1197	if (renderer->vramDirty & mask) {
1198		return true;
1199	}
1200	return false;
1201}
1202
1203static bool _needsVramUpload(struct GBAVideoGLRenderer* renderer, int y) {
1204	if (!renderer->vramDirty) {
1205		return false;
1206	}
1207	if (y == 0) {
1208		return true;
1209	}
1210
1211	if (GBARegisterDISPCNTIsObjEnable(renderer->dispcnt) && renderer->vramDirty & 0xFF0000) {
1212		return true;
1213	}
1214
1215	bool dirty = false;
1216	switch (GBARegisterDISPCNTGetMode(renderer->dispcnt)) {
1217	case 0:
1218		dirty = dirty || _dirtyMode0(renderer, &renderer->bg[0], y);
1219		dirty = dirty || _dirtyMode0(renderer, &renderer->bg[1], y);
1220		dirty = dirty || _dirtyMode0(renderer, &renderer->bg[2], y);
1221		dirty = dirty || _dirtyMode0(renderer, &renderer->bg[3], y);
1222		break;
1223	case 1:
1224		dirty = dirty || _dirtyMode0(renderer, &renderer->bg[0], y);
1225		dirty = dirty || _dirtyMode0(renderer, &renderer->bg[1], y);
1226		dirty = dirty || _dirtyMode2(renderer, &renderer->bg[2], y);
1227		break;
1228	case 2:
1229		dirty = dirty || _dirtyMode2(renderer, &renderer->bg[2], y);
1230		dirty = dirty || _dirtyMode2(renderer, &renderer->bg[3], y);
1231		break;
1232	case 3:
1233		dirty = _dirtyMode3(renderer, &renderer->bg[2], y);
1234		break;
1235	case 4:
1236		dirty = _dirtyMode45(renderer, &renderer->bg[2], y);
1237		break;
1238	case 5:
1239		dirty = _dirtyMode45(renderer, &renderer->bg[2], y);
1240		break;
1241	}
1242	return dirty;
1243}
1244
1245void GBAVideoGLRendererDrawScanline(struct GBAVideoRenderer* renderer, int y) {
1246	struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;
1247
1248	memcpy(&glRenderer->affine[0][y], &glRenderer->bg[2].affine, sizeof(struct GBAVideoGLAffine));
1249	memcpy(&glRenderer->affine[1][y], &glRenderer->bg[3].affine, sizeof(struct GBAVideoGLAffine));
1250	if (GBARegisterDISPCNTGetMode(glRenderer->dispcnt) != 0) {
1251		if (glRenderer->firstAffine < 0) {
1252			glRenderer->firstAffine = y;
1253		}
1254	} else {
1255		glRenderer->firstAffine = -1;
1256	}
1257
1258	if (glRenderer->paletteDirty || _needsVramUpload(glRenderer, y) || glRenderer->oamDirty || glRenderer->regsDirty) {
1259		if (glRenderer->firstY >= 0) {
1260			_drawScanlines(glRenderer, y - 1);
1261			glBindVertexArray(0);
1262		}
1263	}
1264	if (glRenderer->firstY < 0) {
1265		glRenderer->firstY = y;
1266	}
1267
1268	memcpy(&glRenderer->winN[0].h[1], &glRenderer->winN[0].h[0], sizeof(struct GBAVideoWindowRegion));
1269	memcpy(&glRenderer->winN[1].h[1], &glRenderer->winN[1].h[0], sizeof(struct GBAVideoWindowRegion));
1270
1271	int i;
1272	for (i = 0; i < 0x30; ++i) {
1273		if (!(glRenderer->regsDirty & (1ULL << i))) {
1274			continue;
1275		}
1276		_cleanRegister(glRenderer, i << 1, glRenderer->shadowRegs[i]);
1277	}
1278	glRenderer->regsDirty = 0;
1279
1280	if (glRenderer->paletteDirty) {
1281		glBindTexture(GL_TEXTURE_2D, glRenderer->paletteTex);
1282#ifdef BUILD_GLES3
1283		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB565, 16, 32, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, glRenderer->shadowPalette);
1284#else
1285		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB5_A1, 16, 32, 0, GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV, glRenderer->d.palette);
1286#endif
1287		glRenderer->paletteDirty = false;
1288	}
1289
1290	if (_needsVramUpload(glRenderer, y)) {
1291		int first = -1;
1292		glBindTexture(GL_TEXTURE_2D, glRenderer->vramTex);
1293		for (i = 0; i < 25; ++i) {
1294			if (!(glRenderer->vramDirty & (1 << i))) {
1295				if (first >= 0) {
1296					glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 8 * first, 256, 8 * (i - first), GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, &glRenderer->d.vram[2048 * first]);
1297					first = -1;
1298				}
1299			} else if (first < 0) {
1300				first = i;
1301			}
1302		}
1303		glRenderer->vramDirty = 0;
1304	}
1305
1306	if (glRenderer->oamDirty) {
1307		glRenderer->oamMax = GBAVideoRendererCleanOAM(glRenderer->d.oam->obj, glRenderer->sprites, 0);
1308		glRenderer->oamDirty = false;
1309	}
1310
1311	if (y == 0) {
1312		memcpy(&glRenderer->winN[0].h[1], &glRenderer->winN[0].h[0], sizeof(struct GBAVideoWindowRegion));
1313		memcpy(&glRenderer->winN[1].h[1], &glRenderer->winN[1].h[0], sizeof(struct GBAVideoWindowRegion));
1314
1315		glDisable(GL_SCISSOR_TEST);
1316		glClearColor(0, 0, 0, 0);
1317#ifdef BUILD_GLES3
1318		glClearDepthf(1.f);
1319#else
1320		glClearDepth(1);
1321#endif
1322		glBindFramebuffer(GL_FRAMEBUFFER, glRenderer->fbo[GBA_GL_FBO_OBJ]);
1323		glDrawBuffers(2, (GLenum[]) { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 });
1324		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1325
1326		for (i = 0; i < 4; ++i) {
1327			glBindFramebuffer(GL_FRAMEBUFFER, glRenderer->bg[i].fbo);
1328			glDrawBuffers(2, (GLenum[]) { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 });
1329			glClear(GL_COLOR_BUFFER_BIT);
1330		}
1331	}
1332
1333	if (GBARegisterDISPCNTGetMode(glRenderer->dispcnt) != 0) {
1334		glRenderer->bg[2].affine.sx += glRenderer->bg[2].affine.dmx;
1335		glRenderer->bg[2].affine.sy += glRenderer->bg[2].affine.dmy;
1336		glRenderer->bg[3].affine.sx += glRenderer->bg[3].affine.dmx;
1337		glRenderer->bg[3].affine.sy += glRenderer->bg[3].affine.dmy;
1338	}
1339}
1340
1341void _drawScanlines(struct GBAVideoGLRenderer* glRenderer, int y) {
1342	if (glRenderer->firstAffine >= 0) {
1343		glBindTexture(GL_TEXTURE_2D, glRenderer->layers[GBA_GL_TEX_AFFINE_2]);
1344		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16I, 2, GBA_VIDEO_VERTICAL_PIXELS, 0, GL_RGBA_INTEGER, GL_SHORT, glRenderer->affine[0]);
1345		glBindTexture(GL_TEXTURE_2D, glRenderer->layers[GBA_GL_TEX_AFFINE_3]);
1346		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16I, 2, GBA_VIDEO_VERTICAL_PIXELS, 0, GL_RGBA_INTEGER, GL_SHORT, glRenderer->affine[1]);
1347	}
1348	glEnable(GL_SCISSOR_TEST);
1349
1350	uint32_t backdrop = M_RGB5_TO_RGB8(glRenderer->d.palette[0]);
1351	glViewport(0, 0, 1, GBA_VIDEO_VERTICAL_PIXELS);
1352	glScissor(0, glRenderer->firstY, 1, y - glRenderer->firstY + 1);
1353	glBindFramebuffer(GL_FRAMEBUFFER, glRenderer->fbo[GBA_GL_FBO_BACKDROP]);
1354	glDrawBuffers(2, (GLenum[]) { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 });
1355	glClearBufferfv(GL_COLOR, 0, (GLfloat[]) { ((backdrop >> 16) & 0xFF) / 256., ((backdrop >> 8) & 0xFF) / 256., (backdrop & 0xFF) / 256., 0.f });
1356	glClearBufferfv(GL_COLOR, 1, (GLfloat[]) { 1.f, (glRenderer->target1Bd | (glRenderer->target2Bd * 2) | (glRenderer->blendEffect * 4)) / 32.f, glRenderer->blda / 16.f, 0.f });
1357	glDrawBuffers(1, (GLenum[]) { GL_COLOR_ATTACHMENT0 });
1358
1359	GBAVideoGLRendererDrawWindow(glRenderer, y);
1360	if (GBARegisterDISPCNTIsObjEnable(glRenderer->dispcnt) && !glRenderer->d.disableOBJ) {
1361		int i;
1362		glEnable(GL_DEPTH_TEST);
1363		glDepthFunc(GL_LESS);
1364		for (i = 0; i < glRenderer->oamMax; ++i) {
1365			struct GBAVideoRendererSprite* sprite = &glRenderer->sprites[i];
1366			if ((y < sprite->y && (sprite->endY - 256 < 0 || glRenderer->firstY >= sprite->endY - 256)) || glRenderer->firstY >= sprite->endY) {
1367				continue;
1368			}
1369
1370			GBAVideoGLRendererDrawSprite(glRenderer, &sprite->obj, y, sprite->y);
1371		}
1372		glDisable(GL_DEPTH_TEST);
1373	}
1374
1375	if (TEST_LAYER_ENABLED(0) && GBARegisterDISPCNTGetMode(glRenderer->dispcnt) < 2) {
1376		GBAVideoGLRendererDrawBackgroundMode0(glRenderer, &glRenderer->bg[0], y);
1377	}
1378	if (TEST_LAYER_ENABLED(1) && GBARegisterDISPCNTGetMode(glRenderer->dispcnt) < 2) {
1379		GBAVideoGLRendererDrawBackgroundMode0(glRenderer, &glRenderer->bg[1], y);
1380	}
1381	if (TEST_LAYER_ENABLED(2)) {
1382		switch (GBARegisterDISPCNTGetMode(glRenderer->dispcnt)) {
1383		case 0:
1384			GBAVideoGLRendererDrawBackgroundMode0(glRenderer, &glRenderer->bg[2], y);
1385			break;
1386		case 1:
1387		case 2:
1388			GBAVideoGLRendererDrawBackgroundMode2(glRenderer, &glRenderer->bg[2], y);
1389			break;
1390		case 3:
1391			GBAVideoGLRendererDrawBackgroundMode3(glRenderer, &glRenderer->bg[2], y);
1392			break;
1393		case 4:
1394			GBAVideoGLRendererDrawBackgroundMode4(glRenderer, &glRenderer->bg[2], y);
1395			break;
1396		case 5:
1397			GBAVideoGLRendererDrawBackgroundMode5(glRenderer, &glRenderer->bg[2], y);
1398			break;
1399		}
1400	}
1401	if (TEST_LAYER_ENABLED(3)) {
1402		switch (GBARegisterDISPCNTGetMode(glRenderer->dispcnt)) {
1403		case 0:
1404			GBAVideoGLRendererDrawBackgroundMode0(glRenderer, &glRenderer->bg[3], y);
1405			break;
1406		case 2:
1407			GBAVideoGLRendererDrawBackgroundMode2(glRenderer, &glRenderer->bg[3], y);
1408			break;
1409		}
1410	}
1411	glRenderer->firstY = -1;
1412}
1413
1414void GBAVideoGLRendererFinishFrame(struct GBAVideoRenderer* renderer) {
1415	struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;
1416	_drawScanlines(glRenderer, GBA_VIDEO_VERTICAL_PIXELS - 1);
1417	_finalizeLayers(glRenderer);
1418	glDisable(GL_SCISSOR_TEST);
1419	glBindVertexArray(0);
1420	glRenderer->firstAffine = -1;
1421	glRenderer->firstY = -1;
1422	glRenderer->bg[2].affine.sx = glRenderer->bg[2].refx;
1423	glRenderer->bg[2].affine.sy = glRenderer->bg[2].refy;
1424	glRenderer->bg[3].affine.sx = glRenderer->bg[3].refx;
1425	glRenderer->bg[3].affine.sy = glRenderer->bg[3].refy;
1426}
1427
1428void GBAVideoGLRendererGetPixels(struct GBAVideoRenderer* renderer, size_t* stride, const void** pixels) {
1429	struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;
1430	*stride = GBA_VIDEO_HORIZONTAL_PIXELS * glRenderer->scale;
1431	if (!glRenderer->temporaryBuffer) {
1432		glRenderer->temporaryBuffer = anonymousMemoryMap(GBA_VIDEO_HORIZONTAL_PIXELS * GBA_VIDEO_VERTICAL_PIXELS * glRenderer->scale * glRenderer->scale * BYTES_PER_PIXEL);
1433	}
1434	glFinish();
1435	glBindFramebuffer(GL_FRAMEBUFFER, glRenderer->fbo[GBA_GL_FBO_OUTPUT]);
1436	glPixelStorei(GL_PACK_ROW_LENGTH, GBA_VIDEO_HORIZONTAL_PIXELS * glRenderer->scale);
1437	glPixelStorei(GL_PACK_ALIGNMENT, 1);
1438	glReadPixels(0, 0, GBA_VIDEO_HORIZONTAL_PIXELS * glRenderer->scale, GBA_VIDEO_VERTICAL_PIXELS * glRenderer->scale, GL_RGBA, GL_UNSIGNED_BYTE, (void*) glRenderer->temporaryBuffer);
1439	*pixels = glRenderer->temporaryBuffer;
1440}
1441
1442void GBAVideoGLRendererPutPixels(struct GBAVideoRenderer* renderer, size_t stride, const void* pixels) {
1443
1444}
1445
1446static void _enableBg(struct GBAVideoGLRenderer* renderer, int bg, bool active) {
1447	int wasActive = renderer->bg[bg].enabled;
1448	if (!active) {
1449		renderer->bg[bg].enabled = 0;
1450	} else if (!wasActive && active) {
1451		/*if (renderer->nextY == 0 || GBARegisterDISPCNTGetMode(renderer->dispcnt) > 2) {
1452			// TODO: Investigate in more depth how switching background works in different modes
1453			renderer->bg[bg].enabled = 4;
1454		} else {
1455			renderer->bg[bg].enabled = 1;
1456		}*/
1457		renderer->bg[bg].enabled = 4;
1458	}
1459}
1460
1461static void GBAVideoGLRendererUpdateDISPCNT(struct GBAVideoGLRenderer* renderer) {
1462	_enableBg(renderer, 0, GBARegisterDISPCNTGetBg0Enable(renderer->dispcnt));
1463	_enableBg(renderer, 1, GBARegisterDISPCNTGetBg1Enable(renderer->dispcnt));
1464	_enableBg(renderer, 2, GBARegisterDISPCNTGetBg2Enable(renderer->dispcnt));
1465	_enableBg(renderer, 3, GBARegisterDISPCNTGetBg3Enable(renderer->dispcnt));
1466}
1467
1468static void GBAVideoGLRendererWriteBGCNT(struct GBAVideoGLBackground* bg, uint16_t value) {
1469	bg->priority = GBARegisterBGCNTGetPriority(value);
1470	bg->charBase = GBARegisterBGCNTGetCharBase(value) << 13;
1471	bg->mosaic = GBARegisterBGCNTGetMosaic(value);
1472	bg->multipalette = GBARegisterBGCNTGet256Color(value);
1473	bg->screenBase = GBARegisterBGCNTGetScreenBase(value) << 10;
1474	bg->overflow = GBARegisterBGCNTGetOverflow(value);
1475	bg->size = GBARegisterBGCNTGetSize(value);
1476}
1477
1478static void GBAVideoGLRendererWriteBGX_LO(struct GBAVideoGLBackground* bg, uint16_t value) {
1479	bg->refx = (bg->refx & 0xFFFF0000) | value;
1480	bg->affine.sx = bg->refx;
1481}
1482
1483static void GBAVideoGLRendererWriteBGX_HI(struct GBAVideoGLBackground* bg, uint16_t value) {
1484	bg->refx = (bg->refx & 0x0000FFFF) | (value << 16);
1485	bg->refx <<= 4;
1486	bg->refx >>= 4;
1487	bg->affine.sx = bg->refx;
1488}
1489
1490static void GBAVideoGLRendererWriteBGY_LO(struct GBAVideoGLBackground* bg, uint16_t value) {
1491	bg->refy = (bg->refy & 0xFFFF0000) | value;
1492	bg->affine.sy = bg->refy;
1493}
1494
1495static void GBAVideoGLRendererWriteBGY_HI(struct GBAVideoGLBackground* bg, uint16_t value) {
1496	bg->refy = (bg->refy & 0x0000FFFF) | (value << 16);
1497	bg->refy <<= 4;
1498	bg->refy >>= 4;
1499	bg->affine.sy = bg->refy;
1500}
1501
1502static void GBAVideoGLRendererWriteBLDCNT(struct GBAVideoGLRenderer* renderer, uint16_t value) {
1503	renderer->bg[0].target1 = GBARegisterBLDCNTGetTarget1Bg0(value);
1504	renderer->bg[1].target1 = GBARegisterBLDCNTGetTarget1Bg1(value);
1505	renderer->bg[2].target1 = GBARegisterBLDCNTGetTarget1Bg2(value);
1506	renderer->bg[3].target1 = GBARegisterBLDCNTGetTarget1Bg3(value);
1507	renderer->bg[0].target2 = GBARegisterBLDCNTGetTarget2Bg0(value);
1508	renderer->bg[1].target2 = GBARegisterBLDCNTGetTarget2Bg1(value);
1509	renderer->bg[2].target2 = GBARegisterBLDCNTGetTarget2Bg2(value);
1510	renderer->bg[3].target2 = GBARegisterBLDCNTGetTarget2Bg3(value);
1511
1512	renderer->blendEffect = GBARegisterBLDCNTGetEffect(value);
1513	renderer->target1Obj = GBARegisterBLDCNTGetTarget1Obj(value);
1514	renderer->target1Bd = GBARegisterBLDCNTGetTarget1Bd(value);
1515	renderer->target2Obj = GBARegisterBLDCNTGetTarget2Obj(value);
1516	renderer->target2Bd = GBARegisterBLDCNTGetTarget2Bd(value);
1517}
1518
1519void _finalizeLayers(struct GBAVideoGLRenderer* renderer) {
1520	const GLuint* uniforms = renderer->finalizeShader.uniforms;
1521	glBindFramebuffer(GL_FRAMEBUFFER, renderer->fbo[GBA_GL_FBO_OUTPUT]);
1522	glViewport(0, 0, GBA_VIDEO_HORIZONTAL_PIXELS * renderer->scale, GBA_VIDEO_VERTICAL_PIXELS * renderer->scale);
1523	glScissor(0, 0, GBA_VIDEO_HORIZONTAL_PIXELS * renderer->scale, GBA_VIDEO_VERTICAL_PIXELS * renderer->scale);
1524	if (GBARegisterDISPCNTIsForcedBlank(renderer->dispcnt)) {
1525		glClearColor(1.f, 1.f, 1.f, 1.f);
1526		glClear(GL_COLOR_BUFFER_BIT);
1527	} else {
1528		glUseProgram(renderer->finalizeShader.program);
1529		glBindVertexArray(renderer->finalizeShader.vao);
1530		glActiveTexture(GL_TEXTURE0);
1531		glBindTexture(GL_TEXTURE_2D, renderer->layers[GBA_GL_TEX_WINDOW]);
1532		glActiveTexture(GL_TEXTURE0 + 1);
1533		glBindTexture(GL_TEXTURE_2D, renderer->layers[GBA_GL_TEX_OBJ_COLOR]);
1534		glActiveTexture(GL_TEXTURE0 + 2);
1535		glBindTexture(GL_TEXTURE_2D, renderer->layers[GBA_GL_TEX_OBJ_FLAGS]);
1536		glActiveTexture(GL_TEXTURE0 + 3);
1537		glBindTexture(GL_TEXTURE_2D, renderer->bg[0].tex);
1538		glActiveTexture(GL_TEXTURE0 + 4);
1539		glBindTexture(GL_TEXTURE_2D, renderer->bg[0].flags);
1540		glActiveTexture(GL_TEXTURE0 + 5);
1541		glBindTexture(GL_TEXTURE_2D, renderer->bg[1].tex);
1542		glActiveTexture(GL_TEXTURE0 + 6);
1543		glBindTexture(GL_TEXTURE_2D, renderer->bg[1].flags);
1544		glActiveTexture(GL_TEXTURE0 + 7);
1545		glBindTexture(GL_TEXTURE_2D, renderer->bg[2].tex);
1546		glActiveTexture(GL_TEXTURE0 + 8);
1547		glBindTexture(GL_TEXTURE_2D, renderer->bg[2].flags);
1548		glActiveTexture(GL_TEXTURE0 + 9);
1549		glBindTexture(GL_TEXTURE_2D, renderer->bg[3].tex);
1550		glActiveTexture(GL_TEXTURE0 + 10);
1551		glBindTexture(GL_TEXTURE_2D, renderer->bg[3].flags);
1552		glActiveTexture(GL_TEXTURE0 + 11);
1553		glBindTexture(GL_TEXTURE_2D, renderer->layers[GBA_GL_TEX_BACKDROP_COLOR]);
1554		glActiveTexture(GL_TEXTURE0 + 12);
1555		glBindTexture(GL_TEXTURE_2D, renderer->layers[GBA_GL_TEX_BACKDROP_FLAGS]);
1556
1557		glUniform2i(uniforms[GBA_GL_VS_LOC], GBA_VIDEO_VERTICAL_PIXELS, 0);
1558		glUniform2i(uniforms[GBA_GL_VS_MAXPOS], GBA_VIDEO_HORIZONTAL_PIXELS, GBA_VIDEO_VERTICAL_PIXELS);
1559		glUniform1i(uniforms[GBA_GL_FINALIZE_SCALE], renderer->scale);
1560		glUniform1iv(uniforms[GBA_GL_FINALIZE_LAYERS], 5, (GLint[]) { 3, 5, 7, 9, 1 });
1561		glUniform1iv(uniforms[GBA_GL_FINALIZE_FLAGS], 5, (GLint[]) { 4, 6, 8, 10, 2 });
1562		glUniform1i(uniforms[GBA_GL_FINALIZE_WINDOW], 0);
1563		glUniform1i(uniforms[GBA_GL_FINALIZE_WINDOW], 0);
1564		glUniform1i(uniforms[GBA_GL_FINALIZE_BACKDROP], 11);
1565		glUniform1i(uniforms[GBA_GL_FINALIZE_BACKDROPFLAGS], 12);
1566		glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1567	}
1568	glBindFramebuffer(GL_FRAMEBUFFER, 0);
1569}
1570
1571void GBAVideoGLRendererDrawSprite(struct GBAVideoGLRenderer* renderer, struct GBAObj* sprite, int y, int spriteY) {
1572	int width = GBAVideoObjSizes[GBAObjAttributesAGetShape(sprite->a) * 4 + GBAObjAttributesBGetSize(sprite->b)][0];
1573	int height = GBAVideoObjSizes[GBAObjAttributesAGetShape(sprite->a) * 4 + GBAObjAttributesBGetSize(sprite->b)][1];
1574	int32_t x = (uint32_t) GBAObjAttributesBGetX(sprite->b) << 23;
1575	x >>= 23;
1576
1577	int align = GBAObjAttributesAIs256Color(sprite->a) && !GBARegisterDISPCNTIsObjCharacterMapping(renderer->dispcnt);
1578	unsigned charBase = (BASE_TILE >> 1) + (GBAObjAttributesCGetTile(sprite->c) & ~align) * 0x10;
1579	int stride = GBARegisterDISPCNTIsObjCharacterMapping(renderer->dispcnt) ? (width >> 3) : (0x20 >> GBAObjAttributesAGet256Color(sprite->a));
1580
1581	if (spriteY + height >= 256) {
1582		spriteY -= 256;
1583	}
1584
1585	int totalWidth = width;
1586	int totalHeight = height;
1587	if (GBAObjAttributesAIsTransformed(sprite->a) && GBAObjAttributesAIsDoubleSize(sprite->a)) {
1588		totalWidth <<= 1;
1589		totalHeight <<= 1;
1590	}
1591
1592	const struct GBAVideoGLShader* shader = &renderer->objShader[GBAObjAttributesAGet256Color(sprite->a)];
1593	const GLuint* uniforms = shader->uniforms;
1594	glBindFramebuffer(GL_FRAMEBUFFER, renderer->fbo[GBA_GL_FBO_OBJ]);
1595	glViewport(x * renderer->scale, spriteY * renderer->scale, totalWidth * renderer->scale, totalHeight * renderer->scale);
1596	glScissor(x * renderer->scale, renderer->firstY * renderer->scale, totalWidth * renderer->scale, (y - renderer->firstY + 1) * renderer->scale);
1597	glUseProgram(shader->program);
1598	glBindVertexArray(shader->vao);
1599	glActiveTexture(GL_TEXTURE0);
1600	glBindTexture(GL_TEXTURE_2D, renderer->vramTex);
1601	glActiveTexture(GL_TEXTURE0 + 1);
1602	glBindTexture(GL_TEXTURE_2D, renderer->paletteTex);
1603	glUniform2i(uniforms[GBA_GL_VS_LOC], totalHeight, 0);
1604	glUniform2i(uniforms[GBA_GL_VS_MAXPOS], totalWidth, totalHeight);
1605	glUniform1i(uniforms[GBA_GL_OBJ_VRAM], 0);
1606	glUniform1i(uniforms[GBA_GL_OBJ_PALETTE], 1);
1607	glUniform1i(uniforms[GBA_GL_OBJ_CHARBASE], charBase);
1608	glUniform1i(uniforms[GBA_GL_OBJ_STRIDE], stride);
1609	glUniform1i(uniforms[GBA_GL_OBJ_LOCALPALETTE], GBAObjAttributesCGetPalette(sprite->c));
1610	glUniform4i(uniforms[GBA_GL_OBJ_INFLAGS], GBAObjAttributesCGetPriority(sprite->c) << 3,
1611	                                          (renderer->target1Obj || GBAObjAttributesAGetMode(sprite->a) == OBJ_MODE_SEMITRANSPARENT) | (renderer->target2Obj * 2) | (renderer->blendEffect * 4),
1612	                                          renderer->blda, GBAObjAttributesAGetMode(sprite->a) == OBJ_MODE_SEMITRANSPARENT);
1613	if (GBAObjAttributesAIsTransformed(sprite->a)) {
1614		struct GBAOAMMatrix mat;
1615		LOAD_16(mat.a, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].a);
1616		LOAD_16(mat.b, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].b);
1617		LOAD_16(mat.c, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].c);
1618		LOAD_16(mat.d, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].d);
1619
1620		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 });
1621	} else {
1622		int flipX = 1;
1623		int flipY = 1;
1624		if (GBAObjAttributesBIsHFlip(sprite->b)) {
1625			flipX = -1;
1626		}
1627		if (GBAObjAttributesBIsVFlip(sprite->b)) {
1628			flipY = -1;
1629		}
1630		glUniformMatrix2fv(uniforms[GBA_GL_OBJ_TRANSFORM], 1, GL_FALSE, (GLfloat[]) { flipX, 0, 0, flipY });
1631	}
1632	glUniform4i(uniforms[GBA_GL_OBJ_DIMS], width, height, totalWidth, totalHeight);
1633	if (GBAObjAttributesAGetMode(sprite->a) == OBJ_MODE_OBJWIN) {
1634		int window = ~renderer->objwin & 0x3F;
1635		glUniform4f(uniforms[GBA_GL_OBJ_OBJWIN], 1, window / 128.f, renderer->bldb / 16.f, renderer->bldy / 16.f);
1636		glDrawBuffers(3, (GLenum[]) { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2 });
1637	} else {
1638		glUniform4f(uniforms[GBA_GL_OBJ_OBJWIN], 0, 0, 0, 0);
1639		glDrawBuffers(2, (GLenum[]) { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 });
1640	}
1641	if (GBAObjAttributesAIsMosaic(sprite->a) && GBAObjAttributesAGetMode(sprite->a) != OBJ_MODE_OBJWIN) {
1642		int mosaicH = GBAMosaicControlGetObjH(renderer->mosaic) + 1;
1643		if (GBAObjAttributesBIsHFlip(sprite->b)) {
1644			mosaicH = -mosaicH;
1645		}
1646		glUniform4i(uniforms[GBA_GL_OBJ_MOSAIC], mosaicH, GBAMosaicControlGetObjV(renderer->mosaic) + 1, x, spriteY);
1647	} else {
1648		glUniform4i(uniforms[GBA_GL_OBJ_MOSAIC], 0, 0, 0, 0);
1649	}
1650	glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1651	glDrawBuffers(1, (GLenum[]) { GL_COLOR_ATTACHMENT0 });
1652}
1653
1654void _prepareBackground(struct GBAVideoGLRenderer* renderer, struct GBAVideoGLBackground* background, const GLuint* uniforms) {
1655	glBindFramebuffer(GL_FRAMEBUFFER, background->fbo);
1656	glViewport(0, 0, GBA_VIDEO_HORIZONTAL_PIXELS * renderer->scale, GBA_VIDEO_VERTICAL_PIXELS * renderer->scale);
1657	glActiveTexture(GL_TEXTURE0);
1658	glBindTexture(GL_TEXTURE_2D, renderer->vramTex);
1659	glActiveTexture(GL_TEXTURE0 + 1);
1660	glBindTexture(GL_TEXTURE_2D, renderer->paletteTex);
1661	glUniform2i(uniforms[GBA_GL_VS_MAXPOS], GBA_VIDEO_HORIZONTAL_PIXELS, GBA_VIDEO_VERTICAL_PIXELS);
1662	glUniform1i(uniforms[GBA_GL_BG_VRAM], 0);
1663	glUniform1i(uniforms[GBA_GL_BG_PALETTE], 1);
1664	if (background->mosaic) {
1665		glUniform2i(uniforms[GBA_GL_BG_MOSAIC], GBAMosaicControlGetBgV(renderer->mosaic) + 1, GBAMosaicControlGetBgH(renderer->mosaic) + 1);
1666	} else {
1667		glUniform2i(uniforms[GBA_GL_BG_MOSAIC], 0, 0);
1668	}
1669	glUniform4i(uniforms[GBA_GL_BG_INFLAGS], (background->priority << 3) + (background->index << 1) + 1,
1670		                                     background->target1 | (background->target2 * 2) | (renderer->blendEffect * 4),
1671		                                     renderer->blda, 0);
1672	glDrawBuffers(2, (GLenum[]) { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 });
1673}
1674
1675void GBAVideoGLRendererDrawBackgroundMode0(struct GBAVideoGLRenderer* renderer, struct GBAVideoGLBackground* background, int y) {
1676	int inY0 = renderer->firstY + background->y;
1677	int yDiv = (((y + background->y) & ~0xFF) - background->y) & 0xFF;
1678	int inY1 = yDiv + background->y;
1679	int yBase0 = inY0 & 0xFF;
1680	int yBase1 = inY1 & 0xFF;
1681	if (background->size == 2) {
1682		yBase0 += inY0 & 0x100;
1683		yBase1 += inY1 & 0x100;
1684	} else if (background->size == 3) {
1685		yBase0 += (inY0 & 0x100) << 1;
1686		yBase1 += (inY1 & 0x100) << 1;
1687	}
1688
1689	const struct GBAVideoGLShader* shader = &renderer->bgShader[background->multipalette ? 1 : 0];
1690	const GLuint* uniforms = shader->uniforms;
1691	glUseProgram(shader->program);
1692	glBindVertexArray(shader->vao);
1693	_prepareBackground(renderer, background, uniforms);
1694	glUniform1i(uniforms[GBA_GL_BG_SCREENBASE], background->screenBase);
1695	glUniform1i(uniforms[GBA_GL_BG_CHARBASE], background->charBase);
1696	glUniform1i(uniforms[GBA_GL_BG_SIZE], background->size);
1697
1698	if (yDiv > renderer->firstY) {
1699		int end = yDiv - 1;
1700		if (end > y) {
1701			end = y;
1702		}
1703		glScissor(0, renderer->firstY * renderer->scale, GBA_VIDEO_HORIZONTAL_PIXELS * renderer->scale, (end - renderer->firstY + 1) * renderer->scale);
1704		glUniform2i(uniforms[GBA_GL_VS_LOC], end - renderer->firstY + 1, renderer->firstY);
1705		glUniform2i(uniforms[GBA_GL_BG_OFFSET], background->x, yBase0 - renderer->firstY);
1706		glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1707	}
1708
1709	if (y >= yDiv) {
1710		int start = yDiv;
1711		if (yDiv < renderer->firstY) {
1712			start = renderer->firstY;
1713		}
1714		glScissor(0, start * renderer->scale, GBA_VIDEO_HORIZONTAL_PIXELS * renderer->scale, (y - start + 1) * renderer->scale);
1715		glUniform2i(uniforms[GBA_GL_VS_LOC], y - start + 1, start);
1716		glUniform2i(uniforms[GBA_GL_BG_OFFSET], background->x, yBase1 - yDiv);
1717		glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1718	}
1719
1720	glDrawBuffers(1, (GLenum[]) { GL_COLOR_ATTACHMENT0 });
1721}
1722
1723void _prepareTransform(struct GBAVideoGLRenderer* renderer, struct GBAVideoGLBackground* background, const GLuint* uniforms, int y) {
1724	glScissor(0, renderer->firstY * renderer->scale, GBA_VIDEO_HORIZONTAL_PIXELS * renderer->scale, renderer->scale * (y - renderer->firstY + 1));
1725	glUniform2i(uniforms[GBA_GL_VS_LOC], y - renderer->firstY + 1, renderer->firstY);
1726	glUniform2i(uniforms[GBA_GL_BG_RANGE], renderer->firstAffine, y);
1727
1728	glActiveTexture(GL_TEXTURE0 + 2);
1729	glBindTexture(GL_TEXTURE_2D, renderer->layers[GBA_GL_TEX_AFFINE_2 + background->index - 2]);
1730	glUniform1i(uniforms[GBA_GL_BG_TRANSFORM], 2);
1731	_prepareBackground(renderer, background, uniforms);
1732}
1733
1734void GBAVideoGLRendererDrawBackgroundMode2(struct GBAVideoGLRenderer* renderer, struct GBAVideoGLBackground* background, int y) {
1735	const struct GBAVideoGLShader* shader = &renderer->bgShader[background->overflow ? 2 : 3];
1736	const GLuint* uniforms = shader->uniforms;
1737	glUseProgram(shader->program);
1738	glBindVertexArray(shader->vao);
1739	_prepareTransform(renderer, background, uniforms, y);
1740	glUniform1i(uniforms[GBA_GL_BG_SCREENBASE], background->screenBase);
1741	glUniform1i(uniforms[GBA_GL_BG_CHARBASE], background->charBase);
1742	glUniform1i(uniforms[GBA_GL_BG_SIZE], background->size);
1743	glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1744	glDrawBuffers(1, (GLenum[]) { GL_COLOR_ATTACHMENT0 });
1745}
1746
1747void GBAVideoGLRendererDrawBackgroundMode3(struct GBAVideoGLRenderer* renderer, struct GBAVideoGLBackground* background, int y) {
1748	const struct GBAVideoGLShader* shader = &renderer->bgShader[5];
1749	const GLuint* uniforms = shader->uniforms;
1750	glBindFramebuffer(GL_FRAMEBUFFER, background->fbo);
1751	glViewport(0, 0, GBA_VIDEO_HORIZONTAL_PIXELS * renderer->scale, GBA_VIDEO_VERTICAL_PIXELS * renderer->scale);
1752	glUseProgram(shader->program);
1753	glBindVertexArray(shader->vao);
1754	_prepareTransform(renderer, background, uniforms, y);
1755	glUniform1i(uniforms[GBA_GL_BG_CHARBASE], 0);
1756	glUniform2i(uniforms[GBA_GL_BG_SIZE], GBA_VIDEO_HORIZONTAL_PIXELS, GBA_VIDEO_VERTICAL_PIXELS);
1757	glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1758	glDrawBuffers(1, (GLenum[]) { GL_COLOR_ATTACHMENT0 });
1759}
1760
1761void GBAVideoGLRendererDrawBackgroundMode4(struct GBAVideoGLRenderer* renderer, struct GBAVideoGLBackground* background, int y) {
1762	const struct GBAVideoGLShader* shader = &renderer->bgShader[4];
1763	const GLuint* uniforms = shader->uniforms;
1764	glBindFramebuffer(GL_FRAMEBUFFER, background->fbo);
1765	glViewport(0, 0, GBA_VIDEO_HORIZONTAL_PIXELS * renderer->scale, GBA_VIDEO_VERTICAL_PIXELS * renderer->scale);
1766	glUseProgram(shader->program);
1767	glBindVertexArray(shader->vao);
1768	_prepareTransform(renderer, background, uniforms, y);
1769	glUniform1i(uniforms[GBA_GL_BG_CHARBASE], GBARegisterDISPCNTIsFrameSelect(renderer->dispcnt) ? 0xA000 : 0);
1770	glUniform2i(uniforms[GBA_GL_BG_SIZE], GBA_VIDEO_HORIZONTAL_PIXELS, GBA_VIDEO_VERTICAL_PIXELS);
1771	glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1772	glDrawBuffers(1, (GLenum[]) { GL_COLOR_ATTACHMENT0 });
1773}
1774
1775void GBAVideoGLRendererDrawBackgroundMode5(struct GBAVideoGLRenderer* renderer, struct GBAVideoGLBackground* background, int y) {
1776	const struct GBAVideoGLShader* shader = &renderer->bgShader[5];
1777	const GLuint* uniforms = shader->uniforms;
1778	glBindFramebuffer(GL_FRAMEBUFFER, background->fbo);
1779	glViewport(0, 0, GBA_VIDEO_HORIZONTAL_PIXELS * renderer->scale, GBA_VIDEO_VERTICAL_PIXELS * renderer->scale);
1780	glUseProgram(shader->program);
1781	glBindVertexArray(shader->vao);
1782	_prepareTransform(renderer, background, uniforms, y);
1783	glUniform1i(uniforms[GBA_GL_BG_CHARBASE], GBARegisterDISPCNTIsFrameSelect(renderer->dispcnt) ? 0x5000 : 0);
1784	glUniform2i(uniforms[GBA_GL_BG_SIZE], 160, 128);
1785	glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1786	glDrawBuffers(1, (GLenum[]) { GL_COLOR_ATTACHMENT0 });
1787}
1788
1789static void _scissorWindow(int start, int end, int y, int lines, int scale) {
1790	if (start > end) {
1791		_scissorWindow(start, GBA_VIDEO_HORIZONTAL_PIXELS * scale, y, lines, scale);
1792		_scissorWindow(0, end, y, lines, scale);
1793		return;
1794	}
1795	glScissor(start, y, end - start, lines);
1796	glClear(GL_COLOR_BUFFER_BIT);	
1797}
1798
1799static void _scissorWindowN(const struct GBAVideoWindowRegion* region, const struct GBAVideoWindowRegion* v, const struct GBAVideoWindowRegion* y, int scale) {
1800	int sdelta = region[0].start - region[1].start;
1801	int edelta = region[0].end - region[1].end;
1802	int maxDelta = 0;
1803	if (sdelta > maxDelta) {
1804		maxDelta = sdelta;
1805	} else if (-sdelta > maxDelta) {
1806		maxDelta = -sdelta;
1807	}
1808	if (edelta > maxDelta) {
1809		maxDelta = edelta;
1810	} else if (-edelta > maxDelta) {
1811		maxDelta = -edelta;
1812	}
1813	int startY = y->start;
1814	int endY = y->end;
1815	if (startY < v->start) {
1816		startY = v->start;
1817	}
1818	if (endY >= v->end) {
1819		endY = v->end - 1;
1820	}
1821	if (!(sdelta | edelta) || maxDelta >= GBA_VIDEO_VERTICAL_PIXELS / 2) {
1822		_scissorWindow(region[0].start * scale, region[0].end * scale, startY * scale, (endY - startY + 1) * scale, scale);
1823	} else {
1824		int i;
1825		for (i = 0; i < scale * (endY - startY + 1); ++i) {
1826			int start = region[1].start * scale + sdelta * i;
1827			int end = region[1].end * scale + edelta * i;
1828			_scissorWindow(start, end, startY * scale + i, 1, scale);
1829		}
1830	}
1831}
1832
1833static void _clearWindow(GBAWindowControl window, int bldb, int bldy) {
1834	window = ~window & 0x3F;
1835	glClearColor(window / 128.f, bldb / 16.f, bldy / 16.f, 0);
1836}
1837
1838void GBAVideoGLRendererDrawWindow(struct GBAVideoGLRenderer* renderer, int y) {
1839	glBindFramebuffer(GL_FRAMEBUFFER, renderer->fbo[GBA_GL_FBO_WINDOW]);
1840	int dispcnt = ((renderer->dispcnt >> 8) & 0x1F) | 0x20;
1841	if (!(renderer->dispcnt & 0xE000)) {
1842		_clearWindow(dispcnt, renderer->bldb, renderer->bldy);
1843		_scissorWindow(0, GBA_VIDEO_HORIZONTAL_PIXELS * renderer->scale, renderer->firstY * renderer->scale, (y - renderer->firstY + 1) * renderer->scale, renderer->scale);
1844	} else {
1845		_clearWindow(renderer->winout & dispcnt, renderer->bldb, renderer->bldy);
1846		_scissorWindow(0, GBA_VIDEO_HORIZONTAL_PIXELS * renderer->scale, renderer->firstY * renderer->scale, (y - renderer->firstY + 1) * renderer->scale, renderer->scale);
1847		struct GBAVideoWindowRegion yRegion = {
1848			y,
1849			renderer->firstY
1850		};
1851		if (GBARegisterDISPCNTIsWin1Enable(renderer->dispcnt) && y >= renderer->winN[1].v.start && renderer->firstY < renderer->winN[1].v.end) {
1852			_clearWindow(renderer->winN[1].control & dispcnt, renderer->bldb, renderer->bldy);
1853			_scissorWindowN(renderer->winN[1].h, &renderer->winN[1].v, &yRegion, renderer->scale);
1854		}
1855		if (GBARegisterDISPCNTIsWin0Enable(renderer->dispcnt) && y >= renderer->winN[0].v.start && renderer->firstY < renderer->winN[0].v.end) {
1856			_clearWindow(renderer->winN[0].control & dispcnt, renderer->bldb, renderer->bldy);
1857			_scissorWindowN(renderer->winN[0].h, &renderer->winN[0].v, &yRegion, renderer->scale);
1858		}
1859	}
1860}
1861
1862#endif