all repos — mgba @ 89c49f15d0880596436a567f300693f01ab2c47f

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