all repos — mgba @ e83a371e50638abfe513ef971e16c270f00f012b

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