all repos — mgba @ a64c38d3140d8a4015650096c0f61b7d4a9ee38f

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