all repos — mgba @ 4bd788f7153ced2403def6478af29da7d0e3cbd5

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