all repos — mgba @ 3797e1e5f6b001fa1cb2855304c752d81ef25ff6

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, struct GBAVideoGLShader* shader, const char** shaderBuffer, int shaderBufferLines, GLuint vs, const struct GBAVideoGLUniform* uniforms, char* log) {
 403	GLuint program = glCreateProgram();
 404	shader->program = program;
 405
 406	GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
 407	glAttachShader(program, vs);
 408	glAttachShader(program, fs);
 409	glShaderSource(fs, shaderBufferLines, shaderBuffer, 0);
 410	glCompileShader(fs);
 411	glGetShaderInfoLog(fs, 1024, 0, log);
 412	if (log[0]) {
 413		mLOG(GBA_VIDEO, ERROR, "Fragment shader compilation failure: %s", log);
 414	}
 415	glLinkProgram(program);
 416	glGetProgramInfoLog(program, 1024, 0, log);
 417	if (log[0]) {
 418		mLOG(GBA_VIDEO, ERROR, "Program link failure: %s", log);
 419	}
 420	glDeleteShader(fs);
 421#ifndef BUILD_GLES3
 422	glBindFragDataLocation(program, 0, "color");
 423	glBindFragDataLocation(program, 1, "flags");
 424#endif
 425
 426	glGenVertexArrays(1, &shader->vao);
 427	glBindVertexArray(shader->vao);
 428	glBindBuffer(GL_ARRAY_BUFFER, glRenderer->vbo);
 429	GLuint positionLocation = glGetAttribLocation(program, "position");
 430	glVertexAttribPointer(positionLocation, 2, GL_INT, GL_FALSE, 0, NULL);
 431	glEnableVertexAttribArray(positionLocation);
 432
 433	size_t i;
 434	for (i = 0; uniforms[i].name; ++i) {
 435		shader->uniforms[uniforms[i].type] = glGetUniformLocation(program, uniforms[i].name);
 436	}
 437}
 438
 439static void _initFramebufferTexture(GLuint tex, GLenum format, GLenum attachment, int scale) {
 440	glBindTexture(GL_TEXTURE_2D, tex);
 441	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
 442	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
 443	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
 444	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
 445	glTexImage2D(GL_TEXTURE_2D, 0, format, GBA_VIDEO_HORIZONTAL_PIXELS * scale, GBA_VIDEO_VERTICAL_PIXELS * scale, 0, format, GL_UNSIGNED_BYTE, 0);
 446	glFramebufferTexture2D(GL_FRAMEBUFFER, attachment, GL_TEXTURE_2D, tex, 0);	
 447}
 448
 449void GBAVideoGLRendererInit(struct GBAVideoRenderer* renderer) {
 450	struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;
 451	glGenFramebuffers(GBA_GL_FBO_MAX, glRenderer->fbo);
 452	glGenTextures(GBA_GL_TEX_MAX, glRenderer->layers);
 453
 454	glGenTextures(1, &glRenderer->paletteTex);
 455	glBindTexture(GL_TEXTURE_2D, glRenderer->paletteTex);
 456	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
 457	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
 458
 459	glGenTextures(1, &glRenderer->vramTex);
 460	glBindTexture(GL_TEXTURE_2D, glRenderer->vramTex);
 461	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
 462	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
 463	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA4, 256, 192, 0, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, 0);
 464
 465	glBindFramebuffer(GL_FRAMEBUFFER, glRenderer->fbo[GBA_GL_FBO_OBJ]);
 466	_initFramebufferTexture(glRenderer->layers[GBA_GL_TEX_OBJ_COLOR], GL_RGBA, GL_COLOR_ATTACHMENT0, glRenderer->scale);
 467	_initFramebufferTexture(glRenderer->layers[GBA_GL_TEX_OBJ_FLAGS], GL_RGBA, GL_COLOR_ATTACHMENT1, glRenderer->scale);
 468
 469	glBindFramebuffer(GL_FRAMEBUFFER, glRenderer->fbo[GBA_GL_FBO_COMPOSITE]);
 470	_initFramebufferTexture(glRenderer->layers[GBA_GL_TEX_COMPOSITE_COLOR], GL_RGBA, GL_COLOR_ATTACHMENT0, glRenderer->scale);
 471	_initFramebufferTexture(glRenderer->layers[GBA_GL_TEX_COMPOSITE_FLAGS], GL_RGBA, GL_COLOR_ATTACHMENT1, glRenderer->scale);
 472	_initFramebufferTexture(glRenderer->layers[GBA_GL_TEX_COMPOSITE_OLD_COLOR], GL_RGBA, GL_COLOR_ATTACHMENT2, glRenderer->scale);
 473	_initFramebufferTexture(glRenderer->layers[GBA_GL_TEX_COMPOSITE_OLD_FLAGS], GL_RGBA, GL_COLOR_ATTACHMENT3, glRenderer->scale);
 474
 475	glBindFramebuffer(GL_FRAMEBUFFER, glRenderer->fbo[GBA_GL_FBO_WINDOW]);
 476	_initFramebufferTexture(glRenderer->layers[GBA_GL_TEX_WINDOW], GL_RG, GL_COLOR_ATTACHMENT0, glRenderer->scale);
 477
 478	glBindFramebuffer(GL_FRAMEBUFFER, glRenderer->fbo[GBA_GL_FBO_OUTPUT]);
 479	_initFramebufferTexture(glRenderer->outputTex, GL_RGB, GL_COLOR_ATTACHMENT0, glRenderer->scale);
 480
 481	glBindFramebuffer(GL_FRAMEBUFFER, 0);
 482
 483	glGenBuffers(1, &glRenderer->vbo);
 484	glBindBuffer(GL_ARRAY_BUFFER, glRenderer->vbo);
 485	glBufferData(GL_ARRAY_BUFFER, sizeof(_vertices), _vertices, GL_STATIC_DRAW);
 486
 487	int i;
 488	for (i = 0; i < 4; ++i) {
 489		struct GBAVideoGLBackground* bg = &glRenderer->bg[i];
 490		bg->index = i;
 491		bg->enabled = 0;
 492		bg->priority = 0;
 493		bg->charBase = 0;
 494		bg->mosaic = 0;
 495		bg->multipalette = 0;
 496		bg->screenBase = 0;
 497		bg->overflow = 0;
 498		bg->size = 0;
 499		bg->target1 = 0;
 500		bg->target2 = 0;
 501		bg->x = 0;
 502		bg->y = 0;
 503		bg->refx = 0;
 504		bg->refy = 0;
 505		bg->affine[0].dx = 256;
 506		bg->affine[0].dmx = 0;
 507		bg->affine[0].dy = 0;
 508		bg->affine[0].dmy = 256;
 509		bg->affine[0].sx = 0;
 510		bg->affine[0].sy = 0;
 511		glGenFramebuffers(1, &bg->fbo);
 512		glGenTextures(1, &bg->tex);
 513		glGenTextures(1, &bg->flags);
 514		glBindFramebuffer(GL_FRAMEBUFFER, bg->fbo);
 515		_initFramebufferTexture(bg->tex, GL_RGBA, GL_COLOR_ATTACHMENT0, glRenderer->scale);
 516		_initFramebufferTexture(bg->flags, GL_RGBA, GL_COLOR_ATTACHMENT1, glRenderer->scale);
 517		glBindFramebuffer(GL_FRAMEBUFFER, 0);
 518	}
 519
 520	char log[1024];
 521	const GLchar* shaderBuffer[8];
 522	shaderBuffer[0] = _gl3Header;
 523
 524	GLuint vs = glCreateShader(GL_VERTEX_SHADER);
 525	shaderBuffer[1] = _vertexShader;
 526	glShaderSource(vs, 2, shaderBuffer, 0);
 527	glCompileShader(vs);
 528	glGetShaderInfoLog(vs, 1024, 0, log);
 529	if (log[0]) {
 530		mLOG(GBA_VIDEO, ERROR, "Vertex shader compilation failure: %s", log);
 531	}
 532
 533	shaderBuffer[1] = _renderMode0;
 534
 535	shaderBuffer[2] = _renderTile16;
 536	_compileShader(glRenderer, &glRenderer->bgShader[0], shaderBuffer, 3, vs, _uniformsMode0, log);
 537
 538	shaderBuffer[2] = _renderTile256;
 539	_compileShader(glRenderer, &glRenderer->bgShader[1], shaderBuffer, 3, vs, _uniformsMode0, log);
 540
 541	shaderBuffer[1] = _renderMode2;
 542
 543	shaderBuffer[2] = _fetchTileOverflow;
 544	_compileShader(glRenderer, &glRenderer->bgShader[2], shaderBuffer, 3, vs, _uniformsMode2, log);
 545
 546	shaderBuffer[2] = _fetchTileNoOverflow;
 547	_compileShader(glRenderer, &glRenderer->bgShader[3], shaderBuffer, 3, vs, _uniformsMode2, log);
 548
 549	shaderBuffer[1] = _renderObj;
 550
 551	shaderBuffer[2] = _renderTile16;
 552	_compileShader(glRenderer, &glRenderer->objShader[0], shaderBuffer, 3, vs, _uniformsObj, log);
 553#ifndef BUILD_GLES3
 554	glBindFragDataLocation(glRenderer->objShader[0].program, 2, "window");
 555#endif
 556
 557	shaderBuffer[2] = _renderTile256;
 558	_compileShader(glRenderer, &glRenderer->objShader[1], shaderBuffer, 3, vs, _uniformsObj, log);
 559#ifndef BUILD_GLES3
 560	glBindFragDataLocation(glRenderer->objShader[1].program, 2, "window");
 561#endif
 562
 563	shaderBuffer[1] = _composite;
 564	_compileShader(glRenderer, &glRenderer->compositeShader, shaderBuffer, 2, vs, _uniformsComposite, log);
 565#ifndef BUILD_GLES3
 566	glBindFragDataLocation(glRenderer->compositeShader.program, 2, "oldColor");
 567	glBindFragDataLocation(glRenderer->compositeShader.program, 3, "oldFlags");
 568#endif
 569
 570	shaderBuffer[1] = _finalize;
 571	_compileShader(glRenderer, &glRenderer->finalizeShader, shaderBuffer, 2, vs, _uniformsFinalize, log);
 572
 573	glBindVertexArray(0);
 574	glDeleteShader(vs);
 575
 576	GBAVideoGLRendererReset(renderer);
 577}
 578
 579void GBAVideoGLRendererDeinit(struct GBAVideoRenderer* renderer) {
 580	struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;
 581	glDeleteFramebuffers(GBA_GL_FBO_MAX, glRenderer->fbo);
 582	glDeleteTextures(GBA_GL_TEX_MAX, glRenderer->layers);
 583	glDeleteTextures(1, &glRenderer->paletteTex);
 584	glDeleteTextures(1, &glRenderer->vramTex);
 585}
 586
 587void GBAVideoGLRendererReset(struct GBAVideoRenderer* renderer) {
 588	struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;
 589
 590	glRenderer->paletteDirty = true;
 591	glRenderer->vramDirty = 0xFFFFFF;
 592	glRenderer->firstAffine = -1;
 593}
 594
 595void GBAVideoGLRendererWriteVRAM(struct GBAVideoRenderer* renderer, uint32_t address) {
 596	struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;
 597	glRenderer->vramDirty |= 1 << (address >> 12);
 598}
 599
 600void GBAVideoGLRendererWriteOAM(struct GBAVideoRenderer* renderer, uint32_t oam) {
 601	UNUSED(oam);
 602	struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;
 603	glRenderer->oamDirty = true;
 604}
 605
 606void GBAVideoGLRendererWritePalette(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
 607	struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;
 608#ifdef BUILD_GLES3
 609	glRenderer->shadowPalette[address >> 1] = (value & 0x3F) | ((value & 0x7FE0) << 1);
 610#else
 611	UNUSED(address);
 612	UNUSED(value);
 613#endif
 614	glRenderer->paletteDirty = true;
 615}
 616
 617uint16_t GBAVideoGLRendererWriteVideoRegister(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
 618	struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;
 619	if (renderer->cache) {
 620		GBAVideoCacheWriteVideoRegister(renderer->cache, address, value);
 621	}
 622
 623	switch (address) {
 624	case REG_DISPCNT:
 625		value &= 0xFFF7;
 626		glRenderer->dispcnt = value;
 627		GBAVideoGLRendererUpdateDISPCNT(glRenderer);
 628		break;
 629	case REG_BG0CNT:
 630		value &= 0xDFFF;
 631		GBAVideoGLRendererWriteBGCNT(&glRenderer->bg[0], value);
 632		break;
 633	case REG_BG1CNT:
 634		value &= 0xDFFF;
 635		GBAVideoGLRendererWriteBGCNT(&glRenderer->bg[1], value);
 636		break;
 637	case REG_BG2CNT:
 638		value &= 0xFFFF;
 639		GBAVideoGLRendererWriteBGCNT(&glRenderer->bg[2], value);
 640		break;
 641	case REG_BG3CNT:
 642		value &= 0xFFFF;
 643		GBAVideoGLRendererWriteBGCNT(&glRenderer->bg[3], value);
 644		break;
 645	case REG_BG0HOFS:
 646		value &= 0x01FF;
 647		glRenderer->bg[0].x = value;
 648		break;
 649	case REG_BG0VOFS:
 650		value &= 0x01FF;
 651		glRenderer->bg[0].y = value;
 652		break;
 653	case REG_BG1HOFS:
 654		value &= 0x01FF;
 655		glRenderer->bg[1].x = value;
 656		break;
 657	case REG_BG1VOFS:
 658		value &= 0x01FF;
 659		glRenderer->bg[1].y = value;
 660		break;
 661	case REG_BG2HOFS:
 662		value &= 0x01FF;
 663		glRenderer->bg[2].x = value;
 664		break;
 665	case REG_BG2VOFS:
 666		value &= 0x01FF;
 667		glRenderer->bg[2].y = value;
 668		break;
 669	case REG_BG3HOFS:
 670		value &= 0x01FF;
 671		glRenderer->bg[3].x = value;
 672		break;
 673	case REG_BG3VOFS:
 674		value &= 0x01FF;
 675		glRenderer->bg[3].y = value;
 676		break;
 677	case REG_BG2PA:
 678		glRenderer->bg[2].affine[0].dx = value;
 679		break;
 680	case REG_BG2PB:
 681		glRenderer->bg[2].affine[0].dmx = value;
 682		break;
 683	case REG_BG2PC:
 684		glRenderer->bg[2].affine[0].dy = value;
 685		break;
 686	case REG_BG2PD:
 687		glRenderer->bg[2].affine[0].dmy = value;
 688		break;
 689	case REG_BG2X_LO:
 690		GBAVideoGLRendererWriteBGX_LO(&glRenderer->bg[2], value);
 691		break;
 692	case REG_BG2X_HI:
 693		GBAVideoGLRendererWriteBGX_HI(&glRenderer->bg[2], value);
 694		break;
 695	case REG_BG2Y_LO:
 696		GBAVideoGLRendererWriteBGY_LO(&glRenderer->bg[2], value);
 697		break;
 698	case REG_BG2Y_HI:
 699		GBAVideoGLRendererWriteBGY_HI(&glRenderer->bg[2], value);
 700		break;
 701	case REG_BG3PA:
 702		glRenderer->bg[3].affine[0].dx = value;
 703		break;
 704	case REG_BG3PB:
 705		glRenderer->bg[3].affine[0].dmx = value;
 706		break;
 707	case REG_BG3PC:
 708		glRenderer->bg[3].affine[0].dy = value;
 709		break;
 710	case REG_BG3PD:
 711		glRenderer->bg[3].affine[0].dmy = value;
 712		break;
 713	case REG_BG3X_LO:
 714		GBAVideoGLRendererWriteBGX_LO(&glRenderer->bg[3], value);
 715		break;
 716	case REG_BG3X_HI:
 717		GBAVideoGLRendererWriteBGX_HI(&glRenderer->bg[3], value);
 718		break;
 719	case REG_BG3Y_LO:
 720		GBAVideoGLRendererWriteBGY_LO(&glRenderer->bg[3], value);
 721		break;
 722	case REG_BG3Y_HI:
 723		GBAVideoGLRendererWriteBGY_HI(&glRenderer->bg[3], value);
 724		break;
 725	case REG_BLDCNT:
 726		GBAVideoGLRendererWriteBLDCNT(glRenderer, value);
 727		value &= 0x3FFF;
 728		break;
 729	case REG_BLDALPHA:
 730		glRenderer->blda = value & 0x1F;
 731		if (glRenderer->blda > 0x10) {
 732			glRenderer->blda = 0x10;
 733		}
 734		glRenderer->bldb = (value >> 8) & 0x1F;
 735		if (glRenderer->bldb > 0x10) {
 736			glRenderer->bldb = 0x10;
 737		}
 738		value &= 0x1F1F;
 739		break;
 740	case REG_BLDY:
 741		value &= 0x1F;
 742		if (value > 0x10) {
 743			value = 0x10;
 744		}
 745		glRenderer->bldy = value;
 746		break;
 747	case REG_WIN0H:
 748		glRenderer->winN[0].h.end = value;
 749		glRenderer->winN[0].h.start = value >> 8;
 750		if (glRenderer->winN[0].h.start > GBA_VIDEO_HORIZONTAL_PIXELS && glRenderer->winN[0].h.start > glRenderer->winN[0].h.end) {
 751			glRenderer->winN[0].h.start = 0;
 752		}
 753		if (glRenderer->winN[0].h.end > GBA_VIDEO_HORIZONTAL_PIXELS) {
 754			glRenderer->winN[0].h.end = GBA_VIDEO_HORIZONTAL_PIXELS;
 755			if (glRenderer->winN[0].h.start > GBA_VIDEO_HORIZONTAL_PIXELS) {
 756				glRenderer->winN[0].h.start = GBA_VIDEO_HORIZONTAL_PIXELS;
 757			}
 758		}
 759		break;
 760	case REG_WIN1H:
 761		glRenderer->winN[1].h.end = value;
 762		glRenderer->winN[1].h.start = value >> 8;
 763		if (glRenderer->winN[1].h.start > GBA_VIDEO_HORIZONTAL_PIXELS && glRenderer->winN[1].h.start > glRenderer->winN[1].h.end) {
 764			glRenderer->winN[1].h.start = 0;
 765		}
 766		if (glRenderer->winN[1].h.end > GBA_VIDEO_HORIZONTAL_PIXELS) {
 767			glRenderer->winN[1].h.end = GBA_VIDEO_HORIZONTAL_PIXELS;
 768			if (glRenderer->winN[1].h.start > GBA_VIDEO_HORIZONTAL_PIXELS) {
 769				glRenderer->winN[1].h.start = GBA_VIDEO_HORIZONTAL_PIXELS;
 770			}
 771		}
 772		break;
 773	case REG_WIN0V:
 774		glRenderer->winN[0].v.end = value;
 775		glRenderer->winN[0].v.start = value >> 8;
 776		if (glRenderer->winN[0].v.start > GBA_VIDEO_VERTICAL_PIXELS && glRenderer->winN[0].v.start > glRenderer->winN[0].v.end) {
 777			glRenderer->winN[0].v.start = 0;
 778		}
 779		if (glRenderer->winN[0].v.end > GBA_VIDEO_VERTICAL_PIXELS) {
 780			glRenderer->winN[0].v.end = GBA_VIDEO_VERTICAL_PIXELS;
 781			if (glRenderer->winN[0].v.start > GBA_VIDEO_VERTICAL_PIXELS) {
 782				glRenderer->winN[0].v.start = GBA_VIDEO_VERTICAL_PIXELS;
 783			}
 784		}
 785		break;
 786	case REG_WIN1V:
 787		glRenderer->winN[1].v.end = value;
 788		glRenderer->winN[1].v.start = value >> 8;
 789		if (glRenderer->winN[1].v.start > GBA_VIDEO_VERTICAL_PIXELS && glRenderer->winN[1].v.start > glRenderer->winN[1].v.end) {
 790			glRenderer->winN[1].v.start = 0;
 791		}
 792		if (glRenderer->winN[1].v.end > GBA_VIDEO_VERTICAL_PIXELS) {
 793			glRenderer->winN[1].v.end = GBA_VIDEO_VERTICAL_PIXELS;
 794			if (glRenderer->winN[1].v.start > GBA_VIDEO_VERTICAL_PIXELS) {
 795				glRenderer->winN[1].v.start = GBA_VIDEO_VERTICAL_PIXELS;
 796			}
 797		}
 798		break;
 799	case REG_WININ:
 800		value &= 0x3F3F;
 801		glRenderer->winN[0].control = value;
 802		glRenderer->winN[1].control = value >> 8;
 803		break;
 804	case REG_WINOUT:
 805		value &= 0x3F3F;
 806		glRenderer->winout = value;
 807		glRenderer->objwin = value >> 8;
 808		break;
 809	case REG_MOSAIC:
 810		glRenderer->mosaic = value;
 811		break;
 812	default:
 813		break;
 814	}
 815	return value;
 816}
 817
 818void GBAVideoGLRendererDrawScanline(struct GBAVideoRenderer* renderer, int y) {
 819	struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;
 820	if (glRenderer->paletteDirty) {
 821		glBindTexture(GL_TEXTURE_2D, glRenderer->paletteTex);
 822#ifdef BUILD_GLES3
 823		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB565, 16, 32, 0, GL_RGBA, GL_UNSIGNED_SHORT_5_6_5, glRenderer->shadowPalette);
 824#else
 825		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB5_A1, 16, 32, 0, GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV, glRenderer->d.palette);
 826#endif
 827		glRenderer->paletteDirty = false;
 828	}
 829	int i;
 830	for (i = 0; i < 24; ++i) {
 831		if (!(glRenderer->vramDirty & (1 << i))) {
 832			continue;
 833		}
 834		// TODO: PBOs
 835		glBindTexture(GL_TEXTURE_2D, glRenderer->vramTex);
 836		glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 8 * i, 256, 8, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, &glRenderer->d.vram[2048 * i]);
 837	}
 838	glRenderer->vramDirty = 0;
 839
 840	uint32_t backdrop = M_RGB5_TO_RGB8(renderer->palette[0]);
 841	glClearColor(((backdrop >> 16) & 0xFF) / 256., ((backdrop >> 8) & 0xFF) / 256., (backdrop & 0xFF) / 256., 0.f);
 842	glBindFramebuffer(GL_FRAMEBUFFER, glRenderer->fbo[GBA_GL_FBO_COMPOSITE]);
 843	glEnable(GL_SCISSOR_TEST);
 844	glScissor(0, y * glRenderer->scale, GBA_VIDEO_HORIZONTAL_PIXELS * glRenderer->scale, glRenderer->scale);
 845	glDrawBuffers(1, (GLenum[]) { GL_COLOR_ATTACHMENT0 });
 846	glClear(GL_COLOR_BUFFER_BIT);
 847	if (y == 0) {
 848		glDisable(GL_SCISSOR_TEST);
 849		glDrawBuffers(1, (GLenum[]) { GL_COLOR_ATTACHMENT1 });
 850		glClearColor(1, (glRenderer->target1Bd | (glRenderer->target2Bd * 2) | (glRenderer->blendEffect * 4)) / 32.f,
 851		             (glRenderer->blendEffect == BLEND_ALPHA ? glRenderer->blda : glRenderer->bldy) / 16.f, glRenderer->bldb / 16.f);
 852		glClear(GL_COLOR_BUFFER_BIT);
 853		
 854		glClearColor(0, 0, 0, 0);
 855		glDrawBuffers(1, (GLenum[]) { GL_COLOR_ATTACHMENT3 });
 856		glClear(GL_COLOR_BUFFER_BIT);
 857
 858		glBindFramebuffer(GL_FRAMEBUFFER, glRenderer->fbo[GBA_GL_FBO_OBJ]);
 859		glDrawBuffers(2, (GLenum[]) { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 });
 860		glClear(GL_COLOR_BUFFER_BIT);
 861
 862		for (i = 0; i < 4; ++i) {
 863			glBindFramebuffer(GL_FRAMEBUFFER, glRenderer->bg[i].fbo);
 864			glDrawBuffers(2, (GLenum[]) { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 });
 865			glClear(GL_COLOR_BUFFER_BIT);
 866		}
 867		glDrawBuffers(1, (GLenum[]) { GL_COLOR_ATTACHMENT0 });
 868		glEnable(GL_SCISSOR_TEST);
 869	}
 870	glBindFramebuffer(GL_FRAMEBUFFER, 0);
 871
 872	if (GBARegisterDISPCNTGetMode(glRenderer->dispcnt) != 0) {
 873		if (glRenderer->firstAffine < 0) {
 874			memcpy(&glRenderer->bg[2].affine[3], &glRenderer->bg[2].affine[0], sizeof(struct GBAVideoGLAffine));
 875			memcpy(&glRenderer->bg[3].affine[3], &glRenderer->bg[3].affine[0], sizeof(struct GBAVideoGLAffine));
 876			memcpy(&glRenderer->bg[2].affine[2], &glRenderer->bg[2].affine[0], sizeof(struct GBAVideoGLAffine));
 877			memcpy(&glRenderer->bg[3].affine[2], &glRenderer->bg[3].affine[0], sizeof(struct GBAVideoGLAffine));
 878			memcpy(&glRenderer->bg[2].affine[1], &glRenderer->bg[2].affine[0], sizeof(struct GBAVideoGLAffine));
 879			memcpy(&glRenderer->bg[3].affine[1], &glRenderer->bg[3].affine[0], sizeof(struct GBAVideoGLAffine));
 880			glRenderer->firstAffine = y;
 881		} else if (y - glRenderer->firstAffine == 1) {
 882			memcpy(&glRenderer->bg[2].affine[1], &glRenderer->bg[2].affine[0], sizeof(struct GBAVideoGLAffine));
 883			memcpy(&glRenderer->bg[3].affine[1], &glRenderer->bg[3].affine[0], sizeof(struct GBAVideoGLAffine));			
 884		}
 885	} else {
 886		glRenderer->firstAffine = -1;
 887	}
 888
 889	GBAVideoGLRendererDrawWindow(glRenderer, y);
 890	if (GBARegisterDISPCNTIsObjEnable(glRenderer->dispcnt) && !glRenderer->d.disableOBJ) {
 891		if (glRenderer->oamDirty) {
 892			glRenderer->oamMax = GBAVideoRendererCleanOAM(glRenderer->d.oam->obj, glRenderer->sprites, 0);
 893			glRenderer->oamDirty = false;
 894		}
 895		int i;
 896		for (i = glRenderer->oamMax; i--;) {
 897			struct GBAVideoRendererSprite* sprite = &glRenderer->sprites[i];
 898			if ((y < sprite->y && (sprite->endY - 256 < 0 || y >= sprite->endY - 256)) || y >= sprite->endY) {
 899				continue;
 900			}
 901
 902			GBAVideoGLRendererDrawSprite(glRenderer, &sprite->obj, y, sprite->y);
 903		}
 904	}
 905
 906	_compositeLayer(glRenderer, glRenderer->layers[GBA_GL_TEX_OBJ_COLOR], glRenderer->layers[GBA_GL_TEX_OBJ_FLAGS], 4, y);
 907	unsigned priority;
 908	for (priority = 4; priority--;) {
 909		if (TEST_LAYER_ENABLED(0) && GBARegisterDISPCNTGetMode(glRenderer->dispcnt) < 2) {
 910			GBAVideoGLRendererDrawBackgroundMode0(glRenderer, &glRenderer->bg[0], y);
 911		}
 912		if (TEST_LAYER_ENABLED(1) && GBARegisterDISPCNTGetMode(glRenderer->dispcnt) < 2) {
 913			GBAVideoGLRendererDrawBackgroundMode0(glRenderer, &glRenderer->bg[1], y);
 914		}
 915		if (TEST_LAYER_ENABLED(2)) {
 916			switch (GBARegisterDISPCNTGetMode(glRenderer->dispcnt)) {
 917			case 0:
 918				GBAVideoGLRendererDrawBackgroundMode0(glRenderer, &glRenderer->bg[2], y);
 919				break;
 920			case 1:
 921			case 2:
 922				GBAVideoGLRendererDrawBackgroundMode2(glRenderer, &glRenderer->bg[2], y);
 923				break;
 924			case 3:
 925				//GBAVideoGLRendererDrawBackgroundMode3(glRenderer, &glRenderer->bg[2], y);
 926				break;
 927			case 4:
 928				//GBAVideoGLRendererDrawBackgroundMode4(glRenderer, &glRenderer->bg[2], y);
 929				break;
 930			case 5:
 931				//GBAVideoGLRendererDrawBackgroundMode5(glRenderer, &glRenderer->bg[2], y);
 932				break;
 933			}
 934		}
 935		if (TEST_LAYER_ENABLED(3)) {
 936			switch (GBARegisterDISPCNTGetMode(glRenderer->dispcnt)) {
 937			case 0:
 938				GBAVideoGLRendererDrawBackgroundMode0(glRenderer, &glRenderer->bg[3], y);
 939				break;
 940			case 2:
 941				GBAVideoGLRendererDrawBackgroundMode2(glRenderer, &glRenderer->bg[3], y);
 942				break;
 943			}
 944		}
 945	}
 946	_finalizeLayers(glRenderer, y);
 947
 948	if (GBARegisterDISPCNTGetMode(glRenderer->dispcnt) != 0) {
 949		memcpy(&glRenderer->bg[2].affine[3], &glRenderer->bg[2].affine[2], sizeof(struct GBAVideoGLAffine));
 950		memcpy(&glRenderer->bg[3].affine[3], &glRenderer->bg[3].affine[2], sizeof(struct GBAVideoGLAffine));
 951		memcpy(&glRenderer->bg[2].affine[2], &glRenderer->bg[2].affine[1], sizeof(struct GBAVideoGLAffine));
 952		memcpy(&glRenderer->bg[3].affine[2], &glRenderer->bg[3].affine[1], sizeof(struct GBAVideoGLAffine));
 953		memcpy(&glRenderer->bg[2].affine[1], &glRenderer->bg[2].affine[0], sizeof(struct GBAVideoGLAffine));
 954		memcpy(&glRenderer->bg[3].affine[1], &glRenderer->bg[3].affine[0], sizeof(struct GBAVideoGLAffine));
 955
 956		glRenderer->bg[2].affine[0].sx += glRenderer->bg[2].affine[0].dmx;
 957		glRenderer->bg[2].affine[0].sy += glRenderer->bg[2].affine[0].dmy;
 958		glRenderer->bg[3].affine[0].sx += glRenderer->bg[3].affine[0].dmx;
 959		glRenderer->bg[3].affine[0].sy += glRenderer->bg[3].affine[0].dmy;
 960	}
 961}
 962
 963void GBAVideoGLRendererFinishFrame(struct GBAVideoRenderer* renderer) {
 964	struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;
 965	glRenderer->firstAffine = -1;
 966	glRenderer->bg[2].affine[0].sx = glRenderer->bg[2].refx;
 967	glRenderer->bg[2].affine[0].sy = glRenderer->bg[2].refy;
 968	glRenderer->bg[3].affine[0].sx = glRenderer->bg[3].refx;
 969	glRenderer->bg[3].affine[0].sy = glRenderer->bg[3].refy;
 970	glFlush();
 971}
 972
 973void GBAVideoGLRendererGetPixels(struct GBAVideoRenderer* renderer, size_t* stride, const void** pixels) {
 974
 975}
 976
 977void GBAVideoGLRendererPutPixels(struct GBAVideoRenderer* renderer, size_t stride, const void* pixels) {
 978
 979}
 980
 981static void _enableBg(struct GBAVideoGLRenderer* renderer, int bg, bool active) {
 982	int wasActive = renderer->bg[bg].enabled;
 983	if (!active) {
 984		renderer->bg[bg].enabled = 0;
 985	} else if (!wasActive && active) {
 986		/*if (renderer->nextY == 0 || GBARegisterDISPCNTGetMode(renderer->dispcnt) > 2) {
 987			// TODO: Investigate in more depth how switching background works in different modes
 988			renderer->bg[bg].enabled = 4;
 989		} else {
 990			renderer->bg[bg].enabled = 1;
 991		}*/
 992		renderer->bg[bg].enabled = 4;
 993	}
 994}
 995
 996static void GBAVideoGLRendererUpdateDISPCNT(struct GBAVideoGLRenderer* renderer) {
 997	_enableBg(renderer, 0, GBARegisterDISPCNTGetBg0Enable(renderer->dispcnt));
 998	_enableBg(renderer, 1, GBARegisterDISPCNTGetBg1Enable(renderer->dispcnt));
 999	_enableBg(renderer, 2, GBARegisterDISPCNTGetBg2Enable(renderer->dispcnt));
1000	_enableBg(renderer, 3, GBARegisterDISPCNTGetBg3Enable(renderer->dispcnt));
1001}
1002
1003static void GBAVideoGLRendererWriteBGCNT(struct GBAVideoGLBackground* bg, uint16_t value) {
1004	bg->priority = GBARegisterBGCNTGetPriority(value);
1005	bg->charBase = GBARegisterBGCNTGetCharBase(value) << 13;
1006	bg->mosaic = GBARegisterBGCNTGetMosaic(value);
1007	bg->multipalette = GBARegisterBGCNTGet256Color(value);
1008	bg->screenBase = GBARegisterBGCNTGetScreenBase(value) << 10;
1009	bg->overflow = GBARegisterBGCNTGetOverflow(value);
1010	bg->size = GBARegisterBGCNTGetSize(value);
1011}
1012
1013static void GBAVideoGLRendererWriteBGX_LO(struct GBAVideoGLBackground* bg, uint16_t value) {
1014	bg->refx = (bg->refx & 0xFFFF0000) | value;
1015	bg->affine[0].sx = bg->refx;
1016}
1017
1018static void GBAVideoGLRendererWriteBGX_HI(struct GBAVideoGLBackground* bg, uint16_t value) {
1019	bg->refx = (bg->refx & 0x0000FFFF) | (value << 16);
1020	bg->refx <<= 4;
1021	bg->refx >>= 4;
1022	bg->affine[0].sx = bg->refx;
1023}
1024
1025static void GBAVideoGLRendererWriteBGY_LO(struct GBAVideoGLBackground* bg, uint16_t value) {
1026	bg->refy = (bg->refy & 0xFFFF0000) | value;
1027	bg->affine[0].sy = bg->refy;
1028}
1029
1030static void GBAVideoGLRendererWriteBGY_HI(struct GBAVideoGLBackground* bg, uint16_t value) {
1031	bg->refy = (bg->refy & 0x0000FFFF) | (value << 16);
1032	bg->refy <<= 4;
1033	bg->refy >>= 4;
1034	bg->affine[0].sy = bg->refy;
1035}
1036
1037static void GBAVideoGLRendererWriteBLDCNT(struct GBAVideoGLRenderer* renderer, uint16_t value) {
1038	renderer->bg[0].target1 = GBARegisterBLDCNTGetTarget1Bg0(value);
1039	renderer->bg[1].target1 = GBARegisterBLDCNTGetTarget1Bg1(value);
1040	renderer->bg[2].target1 = GBARegisterBLDCNTGetTarget1Bg2(value);
1041	renderer->bg[3].target1 = GBARegisterBLDCNTGetTarget1Bg3(value);
1042	renderer->bg[0].target2 = GBARegisterBLDCNTGetTarget2Bg0(value);
1043	renderer->bg[1].target2 = GBARegisterBLDCNTGetTarget2Bg1(value);
1044	renderer->bg[2].target2 = GBARegisterBLDCNTGetTarget2Bg2(value);
1045	renderer->bg[3].target2 = GBARegisterBLDCNTGetTarget2Bg3(value);
1046
1047	renderer->blendEffect = GBARegisterBLDCNTGetEffect(value);
1048	renderer->target1Obj = GBARegisterBLDCNTGetTarget1Obj(value);
1049	renderer->target1Bd = GBARegisterBLDCNTGetTarget1Bd(value);
1050	renderer->target2Obj = GBARegisterBLDCNTGetTarget2Obj(value);
1051	renderer->target2Bd = GBARegisterBLDCNTGetTarget2Bd(value);
1052}
1053
1054static void _compositeLayer(struct GBAVideoGLRenderer* renderer, GLuint tex, GLuint flags, int id, int y) {
1055	if ((y & 0x1F) != 0x1F) {
1056		return;
1057	}
1058	const GLuint* uniforms = renderer->compositeShader.uniforms;
1059	glBindFramebuffer(GL_FRAMEBUFFER, renderer->fbo[GBA_GL_FBO_COMPOSITE]);
1060	glViewport(0, 0, GBA_VIDEO_HORIZONTAL_PIXELS * renderer->scale, GBA_VIDEO_VERTICAL_PIXELS * renderer->scale);
1061	glScissor(0, (y & ~0x1F) * renderer->scale, GBA_VIDEO_HORIZONTAL_PIXELS * renderer->scale, 0x20 * renderer->scale);
1062	glUseProgram(renderer->compositeShader.program);
1063	glBindVertexArray(renderer->compositeShader.vao);
1064	glActiveTexture(GL_TEXTURE0);
1065	glBindTexture(GL_TEXTURE_2D, tex);
1066	glActiveTexture(GL_TEXTURE0 + 1);
1067	glBindTexture(GL_TEXTURE_2D, flags);
1068	glActiveTexture(GL_TEXTURE0 + 2);
1069	glBindTexture(GL_TEXTURE_2D, renderer->layers[GBA_GL_TEX_COMPOSITE_COLOR]);
1070	glActiveTexture(GL_TEXTURE0 + 3);
1071	glBindTexture(GL_TEXTURE_2D, renderer->layers[GBA_GL_TEX_COMPOSITE_FLAGS]);
1072	glActiveTexture(GL_TEXTURE0 + 4);
1073	glBindTexture(GL_TEXTURE_2D, renderer->layers[GBA_GL_TEX_COMPOSITE_OLD_FLAGS]);
1074	glActiveTexture(GL_TEXTURE0 + 5);
1075	glBindTexture(GL_TEXTURE_2D, renderer->layers[GBA_GL_TEX_WINDOW]);
1076	glUniform2i(uniforms[GBA_GL_VS_LOC], 0x20, y & ~0x1F);
1077	glUniform2i(uniforms[GBA_GL_VS_MAXPOS], GBA_VIDEO_HORIZONTAL_PIXELS, GBA_VIDEO_VERTICAL_PIXELS);
1078	glUniform1i(uniforms[GBA_GL_COMPOSITE_SCALE], renderer->scale);
1079	glUniform1i(uniforms[GBA_GL_COMPOSITE_LAYERID], 1 << id);
1080	glUniform1i(uniforms[GBA_GL_COMPOSITE_LAYER], 0);
1081	glUniform1i(uniforms[GBA_GL_COMPOSITE_LAYERFLAGS], 1);
1082	glUniform1i(uniforms[GBA_GL_COMPOSITE_OLDLAYER], 2);
1083	glUniform1i(uniforms[GBA_GL_COMPOSITE_OLDLAYERFLAGS], 3);
1084	glUniform1i(uniforms[GBA_GL_COMPOSITE_OLDOLDFLAGS], 4);
1085	glUniform1i(uniforms[GBA_GL_COMPOSITE_WINDOW], 5);
1086	glDrawBuffers(4, (GLenum[]) { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3 });
1087	glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1088	glDrawBuffers(1, (GLenum[]) { GL_COLOR_ATTACHMENT0 });
1089}
1090
1091void _finalizeLayers(struct GBAVideoGLRenderer* renderer, int y) {
1092	if ((y & 0x1F) != 0x1F) {
1093		return;
1094	}
1095	const GLuint* uniforms = renderer->finalizeShader.uniforms;
1096	glBindFramebuffer(GL_FRAMEBUFFER, renderer->fbo[GBA_GL_FBO_OUTPUT]);
1097	glViewport(0, 0, GBA_VIDEO_HORIZONTAL_PIXELS * renderer->scale, GBA_VIDEO_VERTICAL_PIXELS * renderer->scale);
1098	glScissor(0, (y & ~0x1F) * renderer->scale, GBA_VIDEO_HORIZONTAL_PIXELS * renderer->scale, 0x20 * renderer->scale);
1099	glUseProgram(renderer->finalizeShader.program);
1100	glBindVertexArray(renderer->finalizeShader.vao);
1101	glActiveTexture(GL_TEXTURE0);
1102	glBindTexture(GL_TEXTURE_2D, renderer->layers[GBA_GL_TEX_COMPOSITE_COLOR]);
1103	glActiveTexture(GL_TEXTURE0 + 1);
1104	glBindTexture(GL_TEXTURE_2D, renderer->layers[GBA_GL_TEX_COMPOSITE_FLAGS]);
1105	glActiveTexture(GL_TEXTURE0 + 2);
1106	glBindTexture(GL_TEXTURE_2D, renderer->layers[GBA_GL_TEX_COMPOSITE_OLD_COLOR]);
1107	glActiveTexture(GL_TEXTURE0 + 3);
1108	glBindTexture(GL_TEXTURE_2D, renderer->layers[GBA_GL_TEX_COMPOSITE_OLD_FLAGS]);
1109	glUniform2i(uniforms[GBA_GL_VS_LOC], 0x20, y & ~0x1F);
1110	glUniform2i(uniforms[GBA_GL_VS_MAXPOS], GBA_VIDEO_HORIZONTAL_PIXELS, GBA_VIDEO_VERTICAL_PIXELS);
1111	glUniform1i(uniforms[GBA_GL_FINALIZE_SCALE], renderer->scale);
1112	glUniform1i(uniforms[GBA_GL_FINALIZE_LAYER], 0);
1113	glUniform1i(uniforms[GBA_GL_FINALIZE_LAYERFLAGS], 1);
1114	glUniform1i(uniforms[GBA_GL_FINALIZE_OLDLAYER], 2);
1115	glUniform1i(uniforms[GBA_GL_FINALIZE_OLDFLAGS], 3);
1116	glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1117	glBindFramebuffer(GL_FRAMEBUFFER, 0);
1118}
1119
1120void GBAVideoGLRendererDrawSprite(struct GBAVideoGLRenderer* renderer, struct GBAObj* sprite, int y, int spriteY) {
1121	int width = GBAVideoObjSizes[GBAObjAttributesAGetShape(sprite->a) * 4 + GBAObjAttributesBGetSize(sprite->b)][0];
1122	int height = GBAVideoObjSizes[GBAObjAttributesAGetShape(sprite->a) * 4 + GBAObjAttributesBGetSize(sprite->b)][1];
1123	int32_t x = (uint32_t) GBAObjAttributesBGetX(sprite->b) << 23;
1124	x >>= 23;
1125
1126	int align = GBAObjAttributesAIs256Color(sprite->a) && !GBARegisterDISPCNTIsObjCharacterMapping(renderer->dispcnt);
1127	unsigned charBase = (BASE_TILE >> 1) + (GBAObjAttributesCGetTile(sprite->c) & ~align) * 0x10;
1128	int stride = GBARegisterDISPCNTIsObjCharacterMapping(renderer->dispcnt) ? (width >> 3) : (0x40 >> !GBAObjAttributesAIs256Color(sprite->a));
1129
1130	if (spriteY + height >= 256) {
1131		spriteY -= 256;
1132	}
1133
1134	if (!GBAObjAttributesAIsTransformed(sprite->a) && GBAObjAttributesBIsVFlip(sprite->b)) {
1135		spriteY = (y - height) + (y - spriteY) + 1;
1136	}
1137
1138	int totalWidth = width;
1139	int totalHeight = height;
1140	if (GBAObjAttributesAIsTransformed(sprite->a) && GBAObjAttributesAIsDoubleSize(sprite->a)) {
1141		totalWidth <<= 1;
1142		totalHeight <<= 1;
1143	}
1144
1145	enum GBAVideoBlendEffect blendEffect = GBAObjAttributesAGetMode(sprite->a) == OBJ_MODE_SEMITRANSPARENT ? BLEND_ALPHA : renderer->blendEffect;
1146
1147	const struct GBAVideoGLShader* shader = &renderer->objShader[GBAObjAttributesAGet256Color(sprite->a)];
1148	const GLuint* uniforms = shader->uniforms;
1149	glBindFramebuffer(GL_FRAMEBUFFER, renderer->fbo[GBA_GL_FBO_OBJ]);
1150	glViewport(x * renderer->scale, spriteY * renderer->scale, totalWidth * renderer->scale, totalHeight * renderer->scale);
1151	glScissor(x * renderer->scale, y * renderer->scale, totalWidth * renderer->scale, renderer->scale);
1152	glUseProgram(shader->program);
1153	glBindVertexArray(shader->vao);
1154	glActiveTexture(GL_TEXTURE0);
1155	glBindTexture(GL_TEXTURE_2D, renderer->vramTex);
1156	glActiveTexture(GL_TEXTURE0 + 1);
1157	glBindTexture(GL_TEXTURE_2D, renderer->paletteTex);
1158	glUniform2i(uniforms[GBA_GL_VS_LOC], 1, y - spriteY);
1159	glUniform2i(uniforms[GBA_GL_VS_MAXPOS], (GBAObjAttributesBIsHFlip(sprite->b) && !GBAObjAttributesAIsTransformed(sprite->a)) ? -totalWidth : totalWidth, totalHeight);
1160	glUniform1i(uniforms[GBA_GL_OBJ_VRAM], 0);
1161	glUniform1i(uniforms[GBA_GL_OBJ_PALETTE], 1);
1162	glUniform1i(uniforms[GBA_GL_OBJ_CHARBASE], charBase);
1163	glUniform1i(uniforms[GBA_GL_OBJ_STRIDE], stride);
1164	glUniform1i(uniforms[GBA_GL_OBJ_LOCALPALETTE], GBAObjAttributesCGetPalette(sprite->c));
1165	glUniform4i(uniforms[GBA_GL_OBJ_INFLAGS], GBAObjAttributesCGetPriority(sprite->c) << 3,
1166	               (renderer->target1Obj || GBAObjAttributesAGetMode(sprite->a) == OBJ_MODE_SEMITRANSPARENT) | (renderer->target2Obj * 2) | (blendEffect * 4),
1167	               blendEffect == BLEND_ALPHA ? renderer->blda : renderer->bldy, renderer->bldb);
1168	if (GBAObjAttributesAIsTransformed(sprite->a)) {
1169		struct GBAOAMMatrix mat;
1170		LOAD_16(mat.a, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].a);
1171		LOAD_16(mat.b, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].b);
1172		LOAD_16(mat.c, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].c);
1173		LOAD_16(mat.d, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].d);
1174
1175		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 });
1176	} else {
1177		glUniformMatrix2fv(uniforms[GBA_GL_OBJ_TRANSFORM], 1, GL_FALSE, (GLfloat[]) { 1.f, 0, 0, 1.f });
1178	}
1179	glUniform4i(uniforms[GBA_GL_OBJ_DIMS], width, height, totalWidth, totalHeight);
1180	if (GBAObjAttributesAGetMode(sprite->a) == OBJ_MODE_OBJWIN) {
1181		int window = ~renderer->objwin & 0xFF;
1182		glUniform3f(uniforms[GBA_GL_OBJ_OBJWIN], 1, (window & 0xF) / 32.f, (window >> 4) / 32.f);
1183		glDrawBuffers(3, (GLenum[]) { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2 });
1184	} else {
1185		glUniform3f(uniforms[GBA_GL_OBJ_OBJWIN], 0, 0, 0);
1186		glDrawBuffers(2, (GLenum[]) { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 });
1187	}
1188	glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1189	glDrawBuffers(1, (GLenum[]) { GL_COLOR_ATTACHMENT0 });
1190}
1191
1192void GBAVideoGLRendererDrawBackgroundMode0(struct GBAVideoGLRenderer* renderer, struct GBAVideoGLBackground* background, int y) {
1193	int inY = y + background->y;
1194	int yBase = inY & 0xFF;
1195	if (background->size == 2) {
1196		yBase += inY & 0x100;
1197	} else if (background->size == 3) {
1198		yBase += (inY & 0x100) << 1;
1199	}
1200
1201	const struct GBAVideoGLShader* shader = &renderer->bgShader[background->multipalette ? 1 : 0];
1202	const GLuint* uniforms = shader->uniforms;
1203	glBindFramebuffer(GL_FRAMEBUFFER, background->fbo);
1204	glViewport(0, 0, GBA_VIDEO_HORIZONTAL_PIXELS * renderer->scale, GBA_VIDEO_VERTICAL_PIXELS * renderer->scale);
1205	glScissor(0, y * renderer->scale, GBA_VIDEO_HORIZONTAL_PIXELS * renderer->scale, renderer->scale);
1206	glUseProgram(shader->program);
1207	glBindVertexArray(shader->vao);
1208	glActiveTexture(GL_TEXTURE0);
1209	glBindTexture(GL_TEXTURE_2D, renderer->vramTex);
1210	glActiveTexture(GL_TEXTURE0 + 1);
1211	glBindTexture(GL_TEXTURE_2D, renderer->paletteTex);
1212	glUniform2i(uniforms[GBA_GL_VS_LOC], 1, y);
1213	glUniform2i(uniforms[GBA_GL_VS_MAXPOS], GBA_VIDEO_HORIZONTAL_PIXELS, GBA_VIDEO_VERTICAL_PIXELS);
1214	glUniform1i(uniforms[GBA_GL_BG_VRAM], 0);
1215	glUniform1i(uniforms[GBA_GL_BG_PALETTE], 1);
1216	glUniform1i(uniforms[GBA_GL_BG_SCREENBASE], background->screenBase);
1217	glUniform1i(uniforms[GBA_GL_BG_CHARBASE], background->charBase);
1218	glUniform1i(uniforms[GBA_GL_BG_SIZE], background->size);
1219	glUniform2i(uniforms[GBA_GL_BG_OFFSET], background->x, yBase - y);
1220	glUniform4i(uniforms[GBA_GL_BG_INFLAGS], (background->priority << 3) + (background->index << 1) + 1,
1221		                                     background->target1 | (background->target2 * 2) | (renderer->blendEffect * 4),
1222		                                     renderer->blendEffect == BLEND_ALPHA ? renderer->blda : renderer->bldy, renderer->bldb);
1223	glDrawBuffers(2, (GLenum[]) { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 });
1224	glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1225	glDrawBuffers(1, (GLenum[]) { GL_COLOR_ATTACHMENT0 });
1226
1227	_compositeLayer(renderer, background->tex, background->flags, background->index, y);
1228}
1229
1230void GBAVideoGLRendererDrawBackgroundMode2(struct GBAVideoGLRenderer* renderer, struct GBAVideoGLBackground* background, int y) {
1231	const struct GBAVideoGLShader* shader = &renderer->bgShader[background->overflow ? 2 : 3];
1232	const GLuint* uniforms = shader->uniforms;
1233	glBindFramebuffer(GL_FRAMEBUFFER, background->fbo);
1234	glViewport(0, 0, GBA_VIDEO_HORIZONTAL_PIXELS * renderer->scale, GBA_VIDEO_VERTICAL_PIXELS * renderer->scale);
1235	glScissor(0, y * renderer->scale, GBA_VIDEO_HORIZONTAL_PIXELS * renderer->scale, renderer->scale);
1236	glUseProgram(shader->program);
1237	glBindVertexArray(shader->vao);
1238	glActiveTexture(GL_TEXTURE0);
1239	glBindTexture(GL_TEXTURE_2D, renderer->vramTex);
1240	glActiveTexture(GL_TEXTURE0 + 1);
1241	glBindTexture(GL_TEXTURE_2D, renderer->paletteTex);
1242	glUniform2i(uniforms[GBA_GL_VS_LOC], 1, y);
1243	glUniform2i(uniforms[GBA_GL_VS_MAXPOS], GBA_VIDEO_HORIZONTAL_PIXELS, GBA_VIDEO_VERTICAL_PIXELS);
1244	glUniform1i(uniforms[GBA_GL_BG_VRAM], 0);
1245	glUniform1i(uniforms[GBA_GL_BG_PALETTE], 1);
1246	glUniform1i(uniforms[GBA_GL_BG_SCREENBASE], background->screenBase);
1247	glUniform1i(uniforms[GBA_GL_BG_CHARBASE], background->charBase);
1248	glUniform1i(uniforms[GBA_GL_BG_SIZE], background->size);
1249	glUniform4i(uniforms[GBA_GL_BG_INFLAGS], (background->priority << 3) + (background->index << 1) + 1,
1250		           background->target1 | (background->target2 * 2) | (renderer->blendEffect * 4),
1251		           renderer->blendEffect == BLEND_ALPHA ? renderer->blda : renderer->bldy, renderer->bldb);
1252	if (renderer->scale > 1) {
1253		glUniform2iv(uniforms[GBA_GL_BG_OFFSET], 4, (GLint[]) {
1254			background->affine[0].sx, background->affine[0].sy,
1255			background->affine[1].sx, background->affine[1].sy,
1256			background->affine[2].sx, background->affine[2].sy,
1257			background->affine[3].sx, background->affine[3].sy,
1258		});
1259		glUniform2iv(uniforms[GBA_GL_BG_TRANSFORM], 4, (GLint[]) {
1260			background->affine[0].dx, background->affine[0].dy,
1261			background->affine[1].dx, background->affine[1].dy,
1262			background->affine[2].dx, background->affine[2].dy,
1263			background->affine[3].dx, background->affine[3].dy,
1264		});
1265	} else {
1266		glUniform2iv(uniforms[GBA_GL_BG_OFFSET], 4, (GLint[]) {
1267			background->affine[0].sx, background->affine[0].sy,
1268			background->affine[0].sx, background->affine[0].sy,
1269			background->affine[0].sx, background->affine[0].sy,
1270			background->affine[0].sx, background->affine[0].sy,
1271		});
1272		glUniform2iv(uniforms[GBA_GL_BG_TRANSFORM], 4, (GLint[]) {
1273			background->affine[0].dx, background->affine[0].dy,
1274			background->affine[0].dx, background->affine[0].dy,
1275			background->affine[0].dx, background->affine[0].dy,
1276			background->affine[0].dx, background->affine[0].dy,
1277		});
1278	}
1279	glDrawBuffers(2, (GLenum[]) { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 });
1280	glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1281	glDrawBuffers(1, (GLenum[]) { GL_COLOR_ATTACHMENT0 });
1282
1283	_compositeLayer(renderer, background->tex, background->flags, background->index, y);
1284}
1285
1286static void _clearWindow(GBAWindowControl window, int start, int end, int y, int scale) {
1287	glScissor(start, y, end - start, scale);
1288	window = ~window & 0xFF;
1289	glClearColor((window & 0xF) / 32.f, (window >> 4) / 32.f, 0, 0);
1290	glClear(GL_COLOR_BUFFER_BIT);
1291}
1292
1293void GBAVideoGLRendererDrawWindow(struct GBAVideoGLRenderer* renderer, int y) {
1294	glBindFramebuffer(GL_FRAMEBUFFER, renderer->fbo[GBA_GL_FBO_WINDOW]);
1295	if (!(renderer->dispcnt & 0xE000)) {
1296		_clearWindow(0xFF, 0, GBA_VIDEO_HORIZONTAL_PIXELS * renderer->scale, y * renderer->scale, renderer->scale);
1297	} else {
1298		_clearWindow(renderer->winout, 0, GBA_VIDEO_HORIZONTAL_PIXELS * renderer->scale, y * renderer->scale, renderer->scale);
1299		if (GBARegisterDISPCNTIsWin1Enable(renderer->dispcnt) && y >= renderer->winN[1].v.start && y < renderer->winN[1].v.end) {
1300			_clearWindow(renderer->winN[1].control, renderer->winN[1].h.start * renderer->scale, renderer->winN[1].h.end * renderer->scale, y * renderer->scale, renderer->scale);
1301		}
1302		if (GBARegisterDISPCNTIsWin0Enable(renderer->dispcnt) && y >= renderer->winN[0].v.start && y < renderer->winN[0].v.end) {
1303			_clearWindow(renderer->winN[0].control, renderer->winN[0].h.start * renderer->scale, renderer->winN[0].h.end * renderer->scale, y * renderer->scale, renderer->scale);
1304		}
1305	}
1306	glBindFramebuffer(GL_FRAMEBUFFER, 0);
1307}
1308
1309#endif