all repos — mgba @ 8a26a7977c0dea8315ea9b1a6df9df7da27b1fe5

mGBA Game Boy Advance Emulator

src/gba/renderers/gl.c (view raw)

   1/* Copyright (c) 2013-2019 Jeffrey Pfau
   2 *
   3 * This Source Code Form is subject to the terms of the Mozilla Public
   4 * License, v. 2.0. If a copy of the MPL was not distributed with this
   5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
   6#include <mgba/internal/gba/renderers/gl.h>
   7
   8#if defined(BUILD_GLES2) || defined(BUILD_GLES3)
   9
  10#include <mgba/core/cache-set.h>
  11#include <mgba/internal/arm/macros.h>
  12#include <mgba/internal/gba/io.h>
  13#include <mgba/internal/gba/renderers/cache-set.h>
  14#include <mgba-util/memory.h>
  15
  16static void GBAVideoGLRendererInit(struct GBAVideoRenderer* renderer);
  17static void GBAVideoGLRendererDeinit(struct GBAVideoRenderer* renderer);
  18static void GBAVideoGLRendererReset(struct GBAVideoRenderer* renderer);
  19static void GBAVideoGLRendererWriteVRAM(struct GBAVideoRenderer* renderer, uint32_t address);
  20static void GBAVideoGLRendererWriteOAM(struct GBAVideoRenderer* renderer, uint32_t oam);
  21static void GBAVideoGLRendererWritePalette(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value);
  22static uint16_t GBAVideoGLRendererWriteVideoRegister(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value);
  23static void GBAVideoGLRendererDrawScanline(struct GBAVideoRenderer* renderer, int y);
  24static void GBAVideoGLRendererFinishFrame(struct GBAVideoRenderer* renderer);
  25static void GBAVideoGLRendererGetPixels(struct GBAVideoRenderer* renderer, size_t* stride, const void** pixels);
  26static void GBAVideoGLRendererPutPixels(struct GBAVideoRenderer* renderer, size_t stride, const void* pixels);
  27
  28static void GBAVideoGLRendererUpdateDISPCNT(struct GBAVideoGLRenderer* renderer);
  29static void GBAVideoGLRendererWriteBGCNT(struct GBAVideoGLBackground* bg, uint16_t value);
  30static void GBAVideoGLRendererWriteBGX_LO(struct GBAVideoGLBackground* bg, uint16_t value);
  31static void GBAVideoGLRendererWriteBGX_HI(struct GBAVideoGLBackground* bg, uint16_t value);
  32static void GBAVideoGLRendererWriteBGY_LO(struct GBAVideoGLBackground* bg, uint16_t value);
  33static void GBAVideoGLRendererWriteBGY_HI(struct GBAVideoGLBackground* bg, uint16_t value);
  34static void GBAVideoGLRendererWriteBLDCNT(struct GBAVideoGLRenderer* renderer, uint16_t value);
  35
  36static void GBAVideoGLRendererDrawSprite(struct GBAVideoGLRenderer* renderer, struct GBAObj* sprite, int y, int spriteY);
  37static void GBAVideoGLRendererDrawBackgroundMode0(struct GBAVideoGLRenderer* renderer, struct GBAVideoGLBackground* background, int y);
  38static void GBAVideoGLRendererDrawBackgroundMode2(struct GBAVideoGLRenderer* renderer, struct GBAVideoGLBackground* background, int y);
  39static void GBAVideoGLRendererDrawBackgroundMode3(struct GBAVideoGLRenderer* renderer, struct GBAVideoGLBackground* background, int y);
  40static void GBAVideoGLRendererDrawBackgroundMode4(struct GBAVideoGLRenderer* renderer, struct GBAVideoGLBackground* background, int y);
  41static void GBAVideoGLRendererDrawBackgroundMode5(struct GBAVideoGLRenderer* renderer, struct GBAVideoGLBackground* background, int y);
  42static void GBAVideoGLRendererDrawWindow(struct GBAVideoGLRenderer* renderer, int y);
  43
  44static void _finalizeLayers(struct GBAVideoGLRenderer* renderer, int y);
  45
  46#define TEST_LAYER_ENABLED(X) !renderer->disableBG[X] && glRenderer->bg[X].enabled == 4
  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 _uniformsFinalize[] = {
 274	{ "loc", GBA_GL_VS_LOC, },
 275	{ "maxPos", GBA_GL_VS_MAXPOS, },
 276	{ "scale", GBA_GL_FINALIZE_SCALE, },
 277	{ "layers", GBA_GL_FINALIZE_LAYERS, },
 278	{ "flags", GBA_GL_FINALIZE_FLAGS, },
 279	{ "window", GBA_GL_FINALIZE_WINDOW, },
 280	{ "backdrop", GBA_GL_FINALIZE_BACKDROP, },
 281	{ "backdropFlags", GBA_GL_FINALIZE_BACKDROPFLAGS, },
 282	{ 0 }
 283};
 284
 285static const char* const _finalize =
 286	"in vec2 texCoord;\n"
 287	"uniform int scale;\n"
 288	"uniform sampler2D layers[5];\n"
 289	"uniform sampler2D flags[5];\n"
 290	"uniform sampler2D window;\n"
 291	"uniform vec4 backdrop;\n"
 292	"uniform vec4 backdropFlags;\n"
 293	"const vec4 flagCoeff = vec4(32., 32., 16., 16.);\n"
 294	"out vec4 color;\n"
 295
 296	"void composite(vec4 pixel, ivec4 flags, inout vec4 topPixel, inout ivec4 topFlags, inout vec4 bottomPixel, inout ivec4 bottomFlags) {\n"
 297	"	if (pixel.a == 0) {\n"
 298	"		return;\n"
 299	"	}\n"
 300	"	if (flags.x >= topFlags.x) {\n"
 301	"		if (flags.x >= bottomFlags.x) {\n"
 302	"			return;\n"
 303	"		}\n"
 304	"		bottomFlags = flags;\n"
 305	"		bottomPixel = pixel;\n"
 306	"	} else {\n"
 307	"		bottomFlags = topFlags;\n"
 308	"		topFlags = flags;\n"
 309	"		bottomPixel = topPixel;\n"
 310	"		topPixel = pixel;\n"
 311	"	}\n"
 312	"}\n"
 313
 314	"void main() {\n"
 315	"	ivec2 windowFlags = ivec2(texelFetch(window, ivec2(texCoord * scale), 0).xy * 32);\n"
 316	"	int layerWindow = windowFlags.x | (windowFlags.y << 4);\n"
 317	"	vec4 topPixel = backdrop;\n"
 318	"	vec4 bottomPixel = backdrop;\n"
 319	"	ivec4 topFlags = ivec4(backdropFlags * flagCoeff);\n"
 320	"	ivec4 bottomFlags = ivec4(backdropFlags * flagCoeff);\n"
 321	"	if ((layerWindow & 1) == 0) {\n"
 322	"		vec4 pix = texelFetch(layers[0], ivec2(texCoord * scale), 0);\n"
 323	"		ivec4 inflags = ivec4(texelFetch(flags[0], ivec2(texCoord * scale), 0) * flagCoeff);\n"
 324	"		composite(pix, inflags, topPixel, topFlags, bottomPixel, bottomFlags);\n"
 325	"	}\n"
 326	"	if ((layerWindow & 2) == 0) {\n"
 327	"		vec4 pix = texelFetch(layers[1], ivec2(texCoord * scale), 0);\n"
 328	"		ivec4 inflags = ivec4(texelFetch(flags[1], ivec2(texCoord * scale), 0) * flagCoeff);\n"
 329	"		composite(pix, inflags, topPixel, topFlags, bottomPixel, bottomFlags);\n"
 330	"	}\n"
 331	"	if ((layerWindow & 4) == 0) {\n"
 332	"		vec4 pix = texelFetch(layers[2], ivec2(texCoord * scale), 0);\n"
 333	"		ivec4 inflags = ivec4(texelFetch(flags[2], ivec2(texCoord * scale), 0) * flagCoeff);\n"
 334	"		composite(pix, inflags, topPixel, topFlags, bottomPixel, bottomFlags);\n"
 335	"	}\n"
 336	"	if ((layerWindow & 8) == 0) {\n"
 337	"		vec4 pix = texelFetch(layers[3], ivec2(texCoord * scale), 0);\n"
 338	"		ivec4 inflags = ivec4(texelFetch(flags[3], ivec2(texCoord * scale), 0) * flagCoeff);\n"
 339	"		composite(pix, inflags, topPixel, topFlags, bottomPixel, bottomFlags);\n"
 340	"	}\n"
 341	"	if ((layerWindow & 32) != 0) {\n"
 342	"		topFlags.y &= ~1;\n"
 343	"	}\n"
 344	"	if ((layerWindow & 16) == 0) {\n"
 345	"		vec4 pix = texelFetch(layers[4], ivec2(texCoord * scale), 0);\n"
 346	"		ivec4 inflags = ivec4(texelFetch(flags[4], ivec2(texCoord * scale), 0) * flagCoeff);\n"
 347	"		composite(pix, inflags, topPixel, topFlags, bottomPixel, bottomFlags);\n"
 348	"	}\n"
 349	"	if ((topFlags.y & 13) == 5) {\n"
 350	"		if ((bottomFlags.y & 2) == 2) {\n"
 351	"			topPixel *= topFlags.z / 16.;\n"
 352	"			topPixel += bottomPixel * bottomFlags.w / 16.;\n"
 353	"		}\n"
 354	"	} else if ((topFlags.y & 13) == 9) {\n"
 355	"		topPixel += (1. - topPixel) * topFlags.z / 16.;\n"
 356	"	} else if ((topFlags.y & 13) == 13) {\n"
 357	"		topPixel -= topPixel * topFlags.z / 16.;\n"
 358	"	}\n"
 359	"	color = topPixel;\n"
 360	"}";
 361
 362static const GLint _vertices[] = {
 363	0, 0,
 364	0, 1,
 365	1, 1,
 366	1, 0,
 367};
 368
 369void GBAVideoGLRendererCreate(struct GBAVideoGLRenderer* renderer) {
 370	renderer->d.init = GBAVideoGLRendererInit;
 371	renderer->d.reset = GBAVideoGLRendererReset;
 372	renderer->d.deinit = GBAVideoGLRendererDeinit;
 373	renderer->d.writeVideoRegister = GBAVideoGLRendererWriteVideoRegister;
 374	renderer->d.writeVRAM = GBAVideoGLRendererWriteVRAM;
 375	renderer->d.writeOAM = GBAVideoGLRendererWriteOAM;
 376	renderer->d.writePalette = GBAVideoGLRendererWritePalette;
 377	renderer->d.drawScanline = GBAVideoGLRendererDrawScanline;
 378	renderer->d.finishFrame = GBAVideoGLRendererFinishFrame;
 379	renderer->d.getPixels = GBAVideoGLRendererGetPixels;
 380	renderer->d.putPixels = GBAVideoGLRendererPutPixels;
 381
 382	renderer->d.disableBG[0] = false;
 383	renderer->d.disableBG[1] = false;
 384	renderer->d.disableBG[2] = false;
 385	renderer->d.disableBG[3] = false;
 386	renderer->d.disableOBJ = false;
 387
 388	renderer->scale = 1;
 389}
 390
 391void _compileShader(struct GBAVideoGLRenderer* glRenderer, struct GBAVideoGLShader* shader, const char** shaderBuffer, int shaderBufferLines, GLuint vs, const struct GBAVideoGLUniform* uniforms, char* log) {
 392	GLuint program = glCreateProgram();
 393	shader->program = program;
 394
 395	GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
 396	glAttachShader(program, vs);
 397	glAttachShader(program, fs);
 398	glShaderSource(fs, shaderBufferLines, shaderBuffer, 0);
 399	glCompileShader(fs);
 400	glGetShaderInfoLog(fs, 1024, 0, log);
 401	if (log[0]) {
 402		mLOG(GBA_VIDEO, ERROR, "Fragment shader compilation failure: %s", log);
 403	}
 404	glLinkProgram(program);
 405	glGetProgramInfoLog(program, 1024, 0, log);
 406	if (log[0]) {
 407		mLOG(GBA_VIDEO, ERROR, "Program link failure: %s", log);
 408	}
 409	glDeleteShader(fs);
 410#ifndef BUILD_GLES3
 411	glBindFragDataLocation(program, 0, "color");
 412	glBindFragDataLocation(program, 1, "flags");
 413#endif
 414
 415	glGenVertexArrays(1, &shader->vao);
 416	glBindVertexArray(shader->vao);
 417	glBindBuffer(GL_ARRAY_BUFFER, glRenderer->vbo);
 418	GLuint positionLocation = glGetAttribLocation(program, "position");
 419	glVertexAttribPointer(positionLocation, 2, GL_INT, GL_FALSE, 0, NULL);
 420
 421	size_t i;
 422	for (i = 0; uniforms[i].name; ++i) {
 423		shader->uniforms[uniforms[i].type] = glGetUniformLocation(program, uniforms[i].name);
 424	}
 425}
 426
 427static void _initFramebufferTexture(GLuint tex, GLenum format, GLenum attachment, int scale) {
 428	glBindTexture(GL_TEXTURE_2D, tex);
 429	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
 430	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
 431	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
 432	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
 433	glTexImage2D(GL_TEXTURE_2D, 0, format, GBA_VIDEO_HORIZONTAL_PIXELS * scale, GBA_VIDEO_VERTICAL_PIXELS * scale, 0, format, GL_UNSIGNED_BYTE, 0);
 434	glFramebufferTexture2D(GL_FRAMEBUFFER, attachment, GL_TEXTURE_2D, tex, 0);	
 435}
 436
 437void GBAVideoGLRendererInit(struct GBAVideoRenderer* renderer) {
 438	struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;
 439	glRenderer->temporaryBuffer = NULL;
 440
 441	glGenFramebuffers(GBA_GL_FBO_MAX, glRenderer->fbo);
 442	glGenTextures(GBA_GL_TEX_MAX, glRenderer->layers);
 443
 444	glGenTextures(1, &glRenderer->paletteTex);
 445	glBindTexture(GL_TEXTURE_2D, glRenderer->paletteTex);
 446	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
 447	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
 448
 449	glGenTextures(1, &glRenderer->vramTex);
 450	glBindTexture(GL_TEXTURE_2D, glRenderer->vramTex);
 451	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
 452	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
 453	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA4, 256, 192, 0, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, 0);
 454
 455	glBindFramebuffer(GL_FRAMEBUFFER, glRenderer->fbo[GBA_GL_FBO_OBJ]);
 456	_initFramebufferTexture(glRenderer->layers[GBA_GL_TEX_OBJ_COLOR], GL_RGBA, GL_COLOR_ATTACHMENT0, glRenderer->scale);
 457	_initFramebufferTexture(glRenderer->layers[GBA_GL_TEX_OBJ_FLAGS], GL_RGBA, GL_COLOR_ATTACHMENT1, glRenderer->scale);
 458
 459	glBindFramebuffer(GL_FRAMEBUFFER, glRenderer->fbo[GBA_GL_FBO_WINDOW]);
 460	_initFramebufferTexture(glRenderer->layers[GBA_GL_TEX_WINDOW], GL_RG, GL_COLOR_ATTACHMENT0, glRenderer->scale);
 461
 462	glBindFramebuffer(GL_FRAMEBUFFER, glRenderer->fbo[GBA_GL_FBO_OUTPUT]);
 463	_initFramebufferTexture(glRenderer->outputTex, GL_RGB, GL_COLOR_ATTACHMENT0, glRenderer->scale);
 464
 465	glBindFramebuffer(GL_FRAMEBUFFER, 0);
 466
 467	glGenBuffers(1, &glRenderer->vbo);
 468	glBindBuffer(GL_ARRAY_BUFFER, glRenderer->vbo);
 469	glBufferData(GL_ARRAY_BUFFER, sizeof(_vertices), _vertices, GL_STATIC_DRAW);
 470
 471	int i;
 472	for (i = 0; i < 4; ++i) {
 473		struct GBAVideoGLBackground* bg = &glRenderer->bg[i];
 474		bg->index = i;
 475		bg->enabled = 0;
 476		bg->priority = 0;
 477		bg->charBase = 0;
 478		bg->mosaic = 0;
 479		bg->multipalette = 0;
 480		bg->screenBase = 0;
 481		bg->overflow = 0;
 482		bg->size = 0;
 483		bg->target1 = 0;
 484		bg->target2 = 0;
 485		bg->x = 0;
 486		bg->y = 0;
 487		bg->refx = 0;
 488		bg->refy = 0;
 489		bg->affine[0].dx = 256;
 490		bg->affine[0].dmx = 0;
 491		bg->affine[0].dy = 0;
 492		bg->affine[0].dmy = 256;
 493		bg->affine[0].sx = 0;
 494		bg->affine[0].sy = 0;
 495		glGenFramebuffers(1, &bg->fbo);
 496		glGenTextures(1, &bg->tex);
 497		glGenTextures(1, &bg->flags);
 498		glBindFramebuffer(GL_FRAMEBUFFER, bg->fbo);
 499		_initFramebufferTexture(bg->tex, GL_RGBA, GL_COLOR_ATTACHMENT0, glRenderer->scale);
 500		_initFramebufferTexture(bg->flags, GL_RGBA, GL_COLOR_ATTACHMENT1, glRenderer->scale);
 501		glBindFramebuffer(GL_FRAMEBUFFER, 0);
 502	}
 503
 504	char log[1024];
 505	const GLchar* shaderBuffer[8];
 506	shaderBuffer[0] = _gl3Header;
 507
 508	GLuint vs = glCreateShader(GL_VERTEX_SHADER);
 509	shaderBuffer[1] = _vertexShader;
 510	glShaderSource(vs, 2, shaderBuffer, 0);
 511	glCompileShader(vs);
 512	glGetShaderInfoLog(vs, 1024, 0, log);
 513	if (log[0]) {
 514		mLOG(GBA_VIDEO, ERROR, "Vertex shader compilation failure: %s", log);
 515	}
 516
 517	shaderBuffer[1] = _renderMode0;
 518
 519	shaderBuffer[2] = _renderTile16;
 520	_compileShader(glRenderer, &glRenderer->bgShader[0], shaderBuffer, 3, vs, _uniformsMode0, log);
 521
 522	shaderBuffer[2] = _renderTile256;
 523	_compileShader(glRenderer, &glRenderer->bgShader[1], shaderBuffer, 3, vs, _uniformsMode0, log);
 524
 525	shaderBuffer[1] = _renderMode2;
 526
 527	shaderBuffer[2] = _fetchTileOverflow;
 528	_compileShader(glRenderer, &glRenderer->bgShader[2], shaderBuffer, 3, vs, _uniformsMode2, log);
 529
 530	shaderBuffer[2] = _fetchTileNoOverflow;
 531	_compileShader(glRenderer, &glRenderer->bgShader[3], shaderBuffer, 3, vs, _uniformsMode2, log);
 532
 533	shaderBuffer[1] = _renderObj;
 534
 535	shaderBuffer[2] = _renderTile16;
 536	_compileShader(glRenderer, &glRenderer->objShader[0], shaderBuffer, 3, vs, _uniformsObj, log);
 537#ifndef BUILD_GLES3
 538	glBindFragDataLocation(glRenderer->objShader[0].program, 2, "window");
 539#endif
 540
 541	shaderBuffer[2] = _renderTile256;
 542	_compileShader(glRenderer, &glRenderer->objShader[1], shaderBuffer, 3, vs, _uniformsObj, log);
 543#ifndef BUILD_GLES3
 544	glBindFragDataLocation(glRenderer->objShader[1].program, 2, "window");
 545#endif
 546
 547	shaderBuffer[1] = _finalize;
 548	_compileShader(glRenderer, &glRenderer->finalizeShader, shaderBuffer, 2, vs, _uniformsFinalize, log);
 549
 550	glBindVertexArray(0);
 551	glDeleteShader(vs);
 552
 553	GBAVideoGLRendererReset(renderer);
 554}
 555
 556void GBAVideoGLRendererDeinit(struct GBAVideoRenderer* renderer) {
 557	struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;
 558	if (glRenderer->temporaryBuffer) {
 559		mappedMemoryFree(glRenderer->temporaryBuffer, GBA_VIDEO_HORIZONTAL_PIXELS * GBA_VIDEO_VERTICAL_PIXELS * glRenderer->scale * glRenderer->scale);
 560	}
 561	glDeleteFramebuffers(GBA_GL_FBO_MAX, glRenderer->fbo);
 562	glDeleteTextures(GBA_GL_TEX_MAX, glRenderer->layers);
 563	glDeleteTextures(1, &glRenderer->paletteTex);
 564	glDeleteTextures(1, &glRenderer->vramTex);
 565}
 566
 567void GBAVideoGLRendererReset(struct GBAVideoRenderer* renderer) {
 568	struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;
 569
 570	glRenderer->paletteDirty = true;
 571	glRenderer->vramDirty = 0xFFFFFF;
 572	glRenderer->firstAffine = -1;
 573}
 574
 575void GBAVideoGLRendererWriteVRAM(struct GBAVideoRenderer* renderer, uint32_t address) {
 576	struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;
 577	glRenderer->vramDirty |= 1 << (address >> 12);
 578}
 579
 580void GBAVideoGLRendererWriteOAM(struct GBAVideoRenderer* renderer, uint32_t oam) {
 581	UNUSED(oam);
 582	struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;
 583	glRenderer->oamDirty = true;
 584}
 585
 586void GBAVideoGLRendererWritePalette(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
 587	struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;
 588#ifdef BUILD_GLES3
 589	glRenderer->shadowPalette[address >> 1] = (value & 0x3F) | ((value & 0x7FE0) << 1);
 590#else
 591	UNUSED(address);
 592	UNUSED(value);
 593#endif
 594	glRenderer->paletteDirty = true;
 595}
 596
 597uint16_t GBAVideoGLRendererWriteVideoRegister(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
 598	struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;
 599	if (renderer->cache) {
 600		GBAVideoCacheWriteVideoRegister(renderer->cache, address, value);
 601	}
 602
 603	switch (address) {
 604	case REG_DISPCNT:
 605		value &= 0xFFF7;
 606		glRenderer->dispcnt = value;
 607		GBAVideoGLRendererUpdateDISPCNT(glRenderer);
 608		break;
 609	case REG_BG0CNT:
 610		value &= 0xDFFF;
 611		GBAVideoGLRendererWriteBGCNT(&glRenderer->bg[0], value);
 612		break;
 613	case REG_BG1CNT:
 614		value &= 0xDFFF;
 615		GBAVideoGLRendererWriteBGCNT(&glRenderer->bg[1], value);
 616		break;
 617	case REG_BG2CNT:
 618		value &= 0xFFFF;
 619		GBAVideoGLRendererWriteBGCNT(&glRenderer->bg[2], value);
 620		break;
 621	case REG_BG3CNT:
 622		value &= 0xFFFF;
 623		GBAVideoGLRendererWriteBGCNT(&glRenderer->bg[3], value);
 624		break;
 625	case REG_BG0HOFS:
 626		value &= 0x01FF;
 627		glRenderer->bg[0].x = value;
 628		break;
 629	case REG_BG0VOFS:
 630		value &= 0x01FF;
 631		glRenderer->bg[0].y = value;
 632		break;
 633	case REG_BG1HOFS:
 634		value &= 0x01FF;
 635		glRenderer->bg[1].x = value;
 636		break;
 637	case REG_BG1VOFS:
 638		value &= 0x01FF;
 639		glRenderer->bg[1].y = value;
 640		break;
 641	case REG_BG2HOFS:
 642		value &= 0x01FF;
 643		glRenderer->bg[2].x = value;
 644		break;
 645	case REG_BG2VOFS:
 646		value &= 0x01FF;
 647		glRenderer->bg[2].y = value;
 648		break;
 649	case REG_BG3HOFS:
 650		value &= 0x01FF;
 651		glRenderer->bg[3].x = value;
 652		break;
 653	case REG_BG3VOFS:
 654		value &= 0x01FF;
 655		glRenderer->bg[3].y = value;
 656		break;
 657	case REG_BG2PA:
 658		glRenderer->bg[2].affine[0].dx = value;
 659		break;
 660	case REG_BG2PB:
 661		glRenderer->bg[2].affine[0].dmx = value;
 662		break;
 663	case REG_BG2PC:
 664		glRenderer->bg[2].affine[0].dy = value;
 665		break;
 666	case REG_BG2PD:
 667		glRenderer->bg[2].affine[0].dmy = value;
 668		break;
 669	case REG_BG2X_LO:
 670		GBAVideoGLRendererWriteBGX_LO(&glRenderer->bg[2], value);
 671		break;
 672	case REG_BG2X_HI:
 673		GBAVideoGLRendererWriteBGX_HI(&glRenderer->bg[2], value);
 674		break;
 675	case REG_BG2Y_LO:
 676		GBAVideoGLRendererWriteBGY_LO(&glRenderer->bg[2], value);
 677		break;
 678	case REG_BG2Y_HI:
 679		GBAVideoGLRendererWriteBGY_HI(&glRenderer->bg[2], value);
 680		break;
 681	case REG_BG3PA:
 682		glRenderer->bg[3].affine[0].dx = value;
 683		break;
 684	case REG_BG3PB:
 685		glRenderer->bg[3].affine[0].dmx = value;
 686		break;
 687	case REG_BG3PC:
 688		glRenderer->bg[3].affine[0].dy = value;
 689		break;
 690	case REG_BG3PD:
 691		glRenderer->bg[3].affine[0].dmy = value;
 692		break;
 693	case REG_BG3X_LO:
 694		GBAVideoGLRendererWriteBGX_LO(&glRenderer->bg[3], value);
 695		break;
 696	case REG_BG3X_HI:
 697		GBAVideoGLRendererWriteBGX_HI(&glRenderer->bg[3], value);
 698		break;
 699	case REG_BG3Y_LO:
 700		GBAVideoGLRendererWriteBGY_LO(&glRenderer->bg[3], value);
 701		break;
 702	case REG_BG3Y_HI:
 703		GBAVideoGLRendererWriteBGY_HI(&glRenderer->bg[3], value);
 704		break;
 705	case REG_BLDCNT:
 706		GBAVideoGLRendererWriteBLDCNT(glRenderer, value);
 707		value &= 0x3FFF;
 708		break;
 709	case REG_BLDALPHA:
 710		glRenderer->blda = value & 0x1F;
 711		if (glRenderer->blda > 0x10) {
 712			glRenderer->blda = 0x10;
 713		}
 714		glRenderer->bldb = (value >> 8) & 0x1F;
 715		if (glRenderer->bldb > 0x10) {
 716			glRenderer->bldb = 0x10;
 717		}
 718		value &= 0x1F1F;
 719		break;
 720	case REG_BLDY:
 721		value &= 0x1F;
 722		if (value > 0x10) {
 723			value = 0x10;
 724		}
 725		glRenderer->bldy = value;
 726		break;
 727	case REG_WIN0H:
 728		glRenderer->winN[0].h.end = value;
 729		glRenderer->winN[0].h.start = value >> 8;
 730		if (glRenderer->winN[0].h.start > GBA_VIDEO_HORIZONTAL_PIXELS && glRenderer->winN[0].h.start > glRenderer->winN[0].h.end) {
 731			glRenderer->winN[0].h.start = 0;
 732		}
 733		if (glRenderer->winN[0].h.end > GBA_VIDEO_HORIZONTAL_PIXELS) {
 734			glRenderer->winN[0].h.end = GBA_VIDEO_HORIZONTAL_PIXELS;
 735			if (glRenderer->winN[0].h.start > GBA_VIDEO_HORIZONTAL_PIXELS) {
 736				glRenderer->winN[0].h.start = GBA_VIDEO_HORIZONTAL_PIXELS;
 737			}
 738		}
 739		break;
 740	case REG_WIN1H:
 741		glRenderer->winN[1].h.end = value;
 742		glRenderer->winN[1].h.start = value >> 8;
 743		if (glRenderer->winN[1].h.start > GBA_VIDEO_HORIZONTAL_PIXELS && glRenderer->winN[1].h.start > glRenderer->winN[1].h.end) {
 744			glRenderer->winN[1].h.start = 0;
 745		}
 746		if (glRenderer->winN[1].h.end > GBA_VIDEO_HORIZONTAL_PIXELS) {
 747			glRenderer->winN[1].h.end = GBA_VIDEO_HORIZONTAL_PIXELS;
 748			if (glRenderer->winN[1].h.start > GBA_VIDEO_HORIZONTAL_PIXELS) {
 749				glRenderer->winN[1].h.start = GBA_VIDEO_HORIZONTAL_PIXELS;
 750			}
 751		}
 752		break;
 753	case REG_WIN0V:
 754		glRenderer->winN[0].v.end = value;
 755		glRenderer->winN[0].v.start = value >> 8;
 756		if (glRenderer->winN[0].v.start > GBA_VIDEO_VERTICAL_PIXELS && glRenderer->winN[0].v.start > glRenderer->winN[0].v.end) {
 757			glRenderer->winN[0].v.start = 0;
 758		}
 759		if (glRenderer->winN[0].v.end > GBA_VIDEO_VERTICAL_PIXELS) {
 760			glRenderer->winN[0].v.end = GBA_VIDEO_VERTICAL_PIXELS;
 761			if (glRenderer->winN[0].v.start > GBA_VIDEO_VERTICAL_PIXELS) {
 762				glRenderer->winN[0].v.start = GBA_VIDEO_VERTICAL_PIXELS;
 763			}
 764		}
 765		break;
 766	case REG_WIN1V:
 767		glRenderer->winN[1].v.end = value;
 768		glRenderer->winN[1].v.start = value >> 8;
 769		if (glRenderer->winN[1].v.start > GBA_VIDEO_VERTICAL_PIXELS && glRenderer->winN[1].v.start > glRenderer->winN[1].v.end) {
 770			glRenderer->winN[1].v.start = 0;
 771		}
 772		if (glRenderer->winN[1].v.end > GBA_VIDEO_VERTICAL_PIXELS) {
 773			glRenderer->winN[1].v.end = GBA_VIDEO_VERTICAL_PIXELS;
 774			if (glRenderer->winN[1].v.start > GBA_VIDEO_VERTICAL_PIXELS) {
 775				glRenderer->winN[1].v.start = GBA_VIDEO_VERTICAL_PIXELS;
 776			}
 777		}
 778		break;
 779	case REG_WININ:
 780		value &= 0x3F3F;
 781		glRenderer->winN[0].control = value;
 782		glRenderer->winN[1].control = value >> 8;
 783		break;
 784	case REG_WINOUT:
 785		value &= 0x3F3F;
 786		glRenderer->winout = value;
 787		glRenderer->objwin = value >> 8;
 788		break;
 789	case REG_MOSAIC:
 790		glRenderer->mosaic = value;
 791		break;
 792	default:
 793		break;
 794	}
 795	return value;
 796}
 797
 798void GBAVideoGLRendererDrawScanline(struct GBAVideoRenderer* renderer, int y) {
 799	struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;
 800	if (glRenderer->paletteDirty) {
 801		glBindTexture(GL_TEXTURE_2D, glRenderer->paletteTex);
 802#ifdef BUILD_GLES3
 803		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB565, 16, 32, 0, GL_RGBA, GL_UNSIGNED_SHORT_5_6_5, glRenderer->shadowPalette);
 804#else
 805		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB5_A1, 16, 32, 0, GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV, glRenderer->d.palette);
 806#endif
 807		glRenderer->paletteDirty = false;
 808	}
 809	int i;
 810	for (i = 0; i < 24; ++i) {
 811		if (!(glRenderer->vramDirty & (1 << i))) {
 812			continue;
 813		}
 814		// TODO: PBOs
 815		glBindTexture(GL_TEXTURE_2D, glRenderer->vramTex);
 816		glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 8 * i, 256, 8, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, &glRenderer->d.vram[2048 * i]);
 817	}
 818	glRenderer->vramDirty = 0;
 819
 820	if (y == 0) {
 821		glDisable(GL_SCISSOR_TEST);		
 822		glClearColor(0, 0, 0, 0);
 823		glDrawBuffers(1, (GLenum[]) { GL_COLOR_ATTACHMENT3 });
 824		glClear(GL_COLOR_BUFFER_BIT);
 825
 826		glBindFramebuffer(GL_FRAMEBUFFER, glRenderer->fbo[GBA_GL_FBO_OBJ]);
 827		glDrawBuffers(2, (GLenum[]) { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 });
 828		glClear(GL_COLOR_BUFFER_BIT);
 829
 830		for (i = 0; i < 4; ++i) {
 831			glBindFramebuffer(GL_FRAMEBUFFER, glRenderer->bg[i].fbo);
 832			glDrawBuffers(2, (GLenum[]) { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 });
 833			glClear(GL_COLOR_BUFFER_BIT);
 834		}
 835		glDrawBuffers(1, (GLenum[]) { GL_COLOR_ATTACHMENT0 });
 836	}
 837	glEnable(GL_SCISSOR_TEST);
 838
 839	if (GBARegisterDISPCNTGetMode(glRenderer->dispcnt) != 0) {
 840		if (glRenderer->firstAffine < 0) {
 841			memcpy(&glRenderer->bg[2].affine[3], &glRenderer->bg[2].affine[0], sizeof(struct GBAVideoGLAffine));
 842			memcpy(&glRenderer->bg[3].affine[3], &glRenderer->bg[3].affine[0], sizeof(struct GBAVideoGLAffine));
 843			memcpy(&glRenderer->bg[2].affine[2], &glRenderer->bg[2].affine[0], sizeof(struct GBAVideoGLAffine));
 844			memcpy(&glRenderer->bg[3].affine[2], &glRenderer->bg[3].affine[0], sizeof(struct GBAVideoGLAffine));
 845			memcpy(&glRenderer->bg[2].affine[1], &glRenderer->bg[2].affine[0], sizeof(struct GBAVideoGLAffine));
 846			memcpy(&glRenderer->bg[3].affine[1], &glRenderer->bg[3].affine[0], sizeof(struct GBAVideoGLAffine));
 847			glRenderer->firstAffine = y;
 848		} else if (y - glRenderer->firstAffine == 1) {
 849			memcpy(&glRenderer->bg[2].affine[1], &glRenderer->bg[2].affine[0], sizeof(struct GBAVideoGLAffine));
 850			memcpy(&glRenderer->bg[3].affine[1], &glRenderer->bg[3].affine[0], sizeof(struct GBAVideoGLAffine));			
 851		}
 852	} else {
 853		glRenderer->firstAffine = -1;
 854	}
 855
 856	GBAVideoGLRendererDrawWindow(glRenderer, y);
 857	if (GBARegisterDISPCNTIsObjEnable(glRenderer->dispcnt) && !glRenderer->d.disableOBJ) {
 858		if (glRenderer->oamDirty) {
 859			glRenderer->oamMax = GBAVideoRendererCleanOAM(glRenderer->d.oam->obj, glRenderer->sprites, 0);
 860			glRenderer->oamDirty = false;
 861		}
 862		int i;
 863		for (i = glRenderer->oamMax; i--;) {
 864			struct GBAVideoRendererSprite* sprite = &glRenderer->sprites[i];
 865			if ((y < sprite->y && (sprite->endY - 256 < 0 || y >= sprite->endY - 256)) || y >= sprite->endY) {
 866				continue;
 867			}
 868
 869			GBAVideoGLRendererDrawSprite(glRenderer, &sprite->obj, y, sprite->y);
 870		}
 871	}
 872
 873	if (TEST_LAYER_ENABLED(0) && GBARegisterDISPCNTGetMode(glRenderer->dispcnt) < 2) {
 874		GBAVideoGLRendererDrawBackgroundMode0(glRenderer, &glRenderer->bg[0], y);
 875	}
 876	if (TEST_LAYER_ENABLED(1) && GBARegisterDISPCNTGetMode(glRenderer->dispcnt) < 2) {
 877		GBAVideoGLRendererDrawBackgroundMode0(glRenderer, &glRenderer->bg[1], y);
 878	}
 879	if (TEST_LAYER_ENABLED(2)) {
 880		switch (GBARegisterDISPCNTGetMode(glRenderer->dispcnt)) {
 881		case 0:
 882			GBAVideoGLRendererDrawBackgroundMode0(glRenderer, &glRenderer->bg[2], y);
 883			break;
 884		case 1:
 885		case 2:
 886			GBAVideoGLRendererDrawBackgroundMode2(glRenderer, &glRenderer->bg[2], y);
 887			break;
 888		case 3:
 889			//GBAVideoGLRendererDrawBackgroundMode3(glRenderer, &glRenderer->bg[2], y);
 890			break;
 891		case 4:
 892			//GBAVideoGLRendererDrawBackgroundMode4(glRenderer, &glRenderer->bg[2], y);
 893			break;
 894		case 5:
 895			//GBAVideoGLRendererDrawBackgroundMode5(glRenderer, &glRenderer->bg[2], y);
 896			break;
 897		}
 898	}
 899	if (TEST_LAYER_ENABLED(3)) {
 900		switch (GBARegisterDISPCNTGetMode(glRenderer->dispcnt)) {
 901		case 0:
 902			GBAVideoGLRendererDrawBackgroundMode0(glRenderer, &glRenderer->bg[3], y);
 903			break;
 904		case 2:
 905			GBAVideoGLRendererDrawBackgroundMode2(glRenderer, &glRenderer->bg[3], y);
 906			break;
 907		}
 908	}
 909	_finalizeLayers(glRenderer, y);
 910
 911	if (GBARegisterDISPCNTGetMode(glRenderer->dispcnt) != 0) {
 912		memcpy(&glRenderer->bg[2].affine[3], &glRenderer->bg[2].affine[2], sizeof(struct GBAVideoGLAffine));
 913		memcpy(&glRenderer->bg[3].affine[3], &glRenderer->bg[3].affine[2], sizeof(struct GBAVideoGLAffine));
 914		memcpy(&glRenderer->bg[2].affine[2], &glRenderer->bg[2].affine[1], sizeof(struct GBAVideoGLAffine));
 915		memcpy(&glRenderer->bg[3].affine[2], &glRenderer->bg[3].affine[1], sizeof(struct GBAVideoGLAffine));
 916		memcpy(&glRenderer->bg[2].affine[1], &glRenderer->bg[2].affine[0], sizeof(struct GBAVideoGLAffine));
 917		memcpy(&glRenderer->bg[3].affine[1], &glRenderer->bg[3].affine[0], sizeof(struct GBAVideoGLAffine));
 918
 919		glRenderer->bg[2].affine[0].sx += glRenderer->bg[2].affine[0].dmx;
 920		glRenderer->bg[2].affine[0].sy += glRenderer->bg[2].affine[0].dmy;
 921		glRenderer->bg[3].affine[0].sx += glRenderer->bg[3].affine[0].dmx;
 922		glRenderer->bg[3].affine[0].sy += glRenderer->bg[3].affine[0].dmy;
 923	}
 924}
 925
 926void GBAVideoGLRendererFinishFrame(struct GBAVideoRenderer* renderer) {
 927	struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;
 928	glRenderer->firstAffine = -1;
 929	glRenderer->bg[2].affine[0].sx = glRenderer->bg[2].refx;
 930	glRenderer->bg[2].affine[0].sy = glRenderer->bg[2].refy;
 931	glRenderer->bg[3].affine[0].sx = glRenderer->bg[3].refx;
 932	glRenderer->bg[3].affine[0].sy = glRenderer->bg[3].refy;
 933	glFlush();
 934}
 935
 936void GBAVideoGLRendererGetPixels(struct GBAVideoRenderer* renderer, size_t* stride, const void** pixels) {
 937	struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;
 938	*stride = GBA_VIDEO_HORIZONTAL_PIXELS * glRenderer->scale;
 939	if (!glRenderer->temporaryBuffer) {
 940		glRenderer->temporaryBuffer = anonymousMemoryMap(GBA_VIDEO_HORIZONTAL_PIXELS * GBA_VIDEO_VERTICAL_PIXELS * glRenderer->scale * glRenderer->scale * BYTES_PER_PIXEL);
 941	}
 942	glBindFramebuffer(GL_FRAMEBUFFER, glRenderer->fbo[GBA_GL_FBO_OUTPUT]);
 943	glPixelStorei(GL_PACK_ROW_LENGTH, GBA_VIDEO_HORIZONTAL_PIXELS * glRenderer->scale);
 944	glPixelStorei(GL_PACK_ALIGNMENT, 1);
 945	glReadPixels(0, 0, GBA_VIDEO_HORIZONTAL_PIXELS * glRenderer->scale, GBA_VIDEO_VERTICAL_PIXELS * glRenderer->scale, GL_RGBA, GL_UNSIGNED_BYTE, (void*) glRenderer->temporaryBuffer);
 946	*pixels = glRenderer->temporaryBuffer;
 947}
 948
 949void GBAVideoGLRendererPutPixels(struct GBAVideoRenderer* renderer, size_t stride, const void* pixels) {
 950
 951}
 952
 953static void _enableBg(struct GBAVideoGLRenderer* renderer, int bg, bool active) {
 954	int wasActive = renderer->bg[bg].enabled;
 955	if (!active) {
 956		renderer->bg[bg].enabled = 0;
 957	} else if (!wasActive && active) {
 958		/*if (renderer->nextY == 0 || GBARegisterDISPCNTGetMode(renderer->dispcnt) > 2) {
 959			// TODO: Investigate in more depth how switching background works in different modes
 960			renderer->bg[bg].enabled = 4;
 961		} else {
 962			renderer->bg[bg].enabled = 1;
 963		}*/
 964		renderer->bg[bg].enabled = 4;
 965	}
 966}
 967
 968static void GBAVideoGLRendererUpdateDISPCNT(struct GBAVideoGLRenderer* renderer) {
 969	_enableBg(renderer, 0, GBARegisterDISPCNTGetBg0Enable(renderer->dispcnt));
 970	_enableBg(renderer, 1, GBARegisterDISPCNTGetBg1Enable(renderer->dispcnt));
 971	_enableBg(renderer, 2, GBARegisterDISPCNTGetBg2Enable(renderer->dispcnt));
 972	_enableBg(renderer, 3, GBARegisterDISPCNTGetBg3Enable(renderer->dispcnt));
 973}
 974
 975static void GBAVideoGLRendererWriteBGCNT(struct GBAVideoGLBackground* bg, uint16_t value) {
 976	bg->priority = GBARegisterBGCNTGetPriority(value);
 977	bg->charBase = GBARegisterBGCNTGetCharBase(value) << 13;
 978	bg->mosaic = GBARegisterBGCNTGetMosaic(value);
 979	bg->multipalette = GBARegisterBGCNTGet256Color(value);
 980	bg->screenBase = GBARegisterBGCNTGetScreenBase(value) << 10;
 981	bg->overflow = GBARegisterBGCNTGetOverflow(value);
 982	bg->size = GBARegisterBGCNTGetSize(value);
 983}
 984
 985static void GBAVideoGLRendererWriteBGX_LO(struct GBAVideoGLBackground* bg, uint16_t value) {
 986	bg->refx = (bg->refx & 0xFFFF0000) | value;
 987	bg->affine[0].sx = bg->refx;
 988}
 989
 990static void GBAVideoGLRendererWriteBGX_HI(struct GBAVideoGLBackground* bg, uint16_t value) {
 991	bg->refx = (bg->refx & 0x0000FFFF) | (value << 16);
 992	bg->refx <<= 4;
 993	bg->refx >>= 4;
 994	bg->affine[0].sx = bg->refx;
 995}
 996
 997static void GBAVideoGLRendererWriteBGY_LO(struct GBAVideoGLBackground* bg, uint16_t value) {
 998	bg->refy = (bg->refy & 0xFFFF0000) | value;
 999	bg->affine[0].sy = bg->refy;
1000}
1001
1002static void GBAVideoGLRendererWriteBGY_HI(struct GBAVideoGLBackground* bg, uint16_t value) {
1003	bg->refy = (bg->refy & 0x0000FFFF) | (value << 16);
1004	bg->refy <<= 4;
1005	bg->refy >>= 4;
1006	bg->affine[0].sy = bg->refy;
1007}
1008
1009static void GBAVideoGLRendererWriteBLDCNT(struct GBAVideoGLRenderer* renderer, uint16_t value) {
1010	renderer->bg[0].target1 = GBARegisterBLDCNTGetTarget1Bg0(value);
1011	renderer->bg[1].target1 = GBARegisterBLDCNTGetTarget1Bg1(value);
1012	renderer->bg[2].target1 = GBARegisterBLDCNTGetTarget1Bg2(value);
1013	renderer->bg[3].target1 = GBARegisterBLDCNTGetTarget1Bg3(value);
1014	renderer->bg[0].target2 = GBARegisterBLDCNTGetTarget2Bg0(value);
1015	renderer->bg[1].target2 = GBARegisterBLDCNTGetTarget2Bg1(value);
1016	renderer->bg[2].target2 = GBARegisterBLDCNTGetTarget2Bg2(value);
1017	renderer->bg[3].target2 = GBARegisterBLDCNTGetTarget2Bg3(value);
1018
1019	renderer->blendEffect = GBARegisterBLDCNTGetEffect(value);
1020	renderer->target1Obj = GBARegisterBLDCNTGetTarget1Obj(value);
1021	renderer->target1Bd = GBARegisterBLDCNTGetTarget1Bd(value);
1022	renderer->target2Obj = GBARegisterBLDCNTGetTarget2Obj(value);
1023	renderer->target2Bd = GBARegisterBLDCNTGetTarget2Bd(value);
1024}
1025
1026void _finalizeLayers(struct GBAVideoGLRenderer* renderer, int y) {
1027	const GLuint* uniforms = renderer->finalizeShader.uniforms;
1028	glBindFramebuffer(GL_FRAMEBUFFER, renderer->fbo[GBA_GL_FBO_OUTPUT]);
1029	glViewport(0, 0, GBA_VIDEO_HORIZONTAL_PIXELS * renderer->scale, GBA_VIDEO_VERTICAL_PIXELS * renderer->scale);
1030	glScissor(0, y * renderer->scale, GBA_VIDEO_HORIZONTAL_PIXELS * renderer->scale, renderer->scale);
1031	glUseProgram(renderer->finalizeShader.program);
1032	glBindVertexArray(renderer->finalizeShader.vao);
1033	glActiveTexture(GL_TEXTURE0);
1034	glBindTexture(GL_TEXTURE_2D, renderer->layers[GBA_GL_TEX_WINDOW]);
1035	glActiveTexture(GL_TEXTURE0 + 1);
1036	glBindTexture(GL_TEXTURE_2D, renderer->layers[GBA_GL_TEX_OBJ_COLOR]);
1037	glActiveTexture(GL_TEXTURE0 + 2);
1038	glBindTexture(GL_TEXTURE_2D, renderer->layers[GBA_GL_TEX_OBJ_FLAGS]);
1039	glActiveTexture(GL_TEXTURE0 + 3);
1040	glBindTexture(GL_TEXTURE_2D, renderer->bg[0].tex);
1041	glActiveTexture(GL_TEXTURE0 + 4);
1042	glBindTexture(GL_TEXTURE_2D, renderer->bg[0].flags);
1043	glActiveTexture(GL_TEXTURE0 + 5);
1044	glBindTexture(GL_TEXTURE_2D, renderer->bg[1].tex);
1045	glActiveTexture(GL_TEXTURE0 + 6);
1046	glBindTexture(GL_TEXTURE_2D, renderer->bg[1].flags);
1047	glActiveTexture(GL_TEXTURE0 + 7);
1048	glBindTexture(GL_TEXTURE_2D, renderer->bg[2].tex);
1049	glActiveTexture(GL_TEXTURE0 + 8);
1050	glBindTexture(GL_TEXTURE_2D, renderer->bg[2].flags);
1051	glActiveTexture(GL_TEXTURE0 + 9);
1052	glBindTexture(GL_TEXTURE_2D, renderer->bg[3].tex);
1053	glActiveTexture(GL_TEXTURE0 + 10);
1054	glBindTexture(GL_TEXTURE_2D, renderer->bg[3].flags);
1055
1056	uint32_t backdrop = M_RGB5_TO_RGB8(renderer->d.palette[0]);
1057	glUniform2i(uniforms[GBA_GL_VS_LOC], 1, y);
1058	glUniform2i(uniforms[GBA_GL_VS_MAXPOS], GBA_VIDEO_HORIZONTAL_PIXELS, GBA_VIDEO_VERTICAL_PIXELS);
1059	glUniform1i(uniforms[GBA_GL_FINALIZE_SCALE], renderer->scale);
1060	glUniform1iv(uniforms[GBA_GL_FINALIZE_LAYERS], 5, (GLint[]) { 3, 5, 7, 9, 1 });
1061	glUniform1iv(uniforms[GBA_GL_FINALIZE_FLAGS], 5, (GLint[]) { 4, 6, 8, 10, 2 });
1062	glUniform1i(uniforms[GBA_GL_FINALIZE_WINDOW], 0);
1063	glUniform4f(uniforms[GBA_GL_FINALIZE_BACKDROP], ((backdrop >> 16) & 0xFF) / 256., ((backdrop >> 8) & 0xFF) / 256., (backdrop & 0xFF) / 256., 0.f);
1064	glUniform4f(uniforms[GBA_GL_FINALIZE_BACKDROPFLAGS], 1, (renderer->target1Bd | (renderer->target2Bd * 2) | (renderer->blendEffect * 4)) / 32.f,
1065		                                                 (renderer->blendEffect == BLEND_ALPHA ? renderer->blda : renderer->bldy) / 16.f, renderer->bldb / 16.f);
1066	glEnableVertexAttribArray(0);
1067	glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1068	glBindFramebuffer(GL_FRAMEBUFFER, 0);
1069}
1070
1071void GBAVideoGLRendererDrawSprite(struct GBAVideoGLRenderer* renderer, struct GBAObj* sprite, int y, int spriteY) {
1072	int width = GBAVideoObjSizes[GBAObjAttributesAGetShape(sprite->a) * 4 + GBAObjAttributesBGetSize(sprite->b)][0];
1073	int height = GBAVideoObjSizes[GBAObjAttributesAGetShape(sprite->a) * 4 + GBAObjAttributesBGetSize(sprite->b)][1];
1074	int32_t x = (uint32_t) GBAObjAttributesBGetX(sprite->b) << 23;
1075	x >>= 23;
1076
1077	int align = GBAObjAttributesAIs256Color(sprite->a) && !GBARegisterDISPCNTIsObjCharacterMapping(renderer->dispcnt);
1078	unsigned charBase = (BASE_TILE >> 1) + (GBAObjAttributesCGetTile(sprite->c) & ~align) * 0x10;
1079	int stride = GBARegisterDISPCNTIsObjCharacterMapping(renderer->dispcnt) ? (width >> 3) : (0x20 >> GBAObjAttributesAGet256Color(sprite->a));
1080
1081	if (spriteY + height >= 256) {
1082		spriteY -= 256;
1083	}
1084
1085	if (!GBAObjAttributesAIsTransformed(sprite->a) && GBAObjAttributesBIsVFlip(sprite->b)) {
1086		spriteY = (y - height) + (y - spriteY) + 1;
1087	}
1088
1089	int totalWidth = width;
1090	int totalHeight = height;
1091	if (GBAObjAttributesAIsTransformed(sprite->a) && GBAObjAttributesAIsDoubleSize(sprite->a)) {
1092		totalWidth <<= 1;
1093		totalHeight <<= 1;
1094	}
1095
1096	enum GBAVideoBlendEffect blendEffect = GBAObjAttributesAGetMode(sprite->a) == OBJ_MODE_SEMITRANSPARENT ? BLEND_ALPHA : renderer->blendEffect;
1097
1098	const struct GBAVideoGLShader* shader = &renderer->objShader[GBAObjAttributesAGet256Color(sprite->a)];
1099	const GLuint* uniforms = shader->uniforms;
1100	glBindFramebuffer(GL_FRAMEBUFFER, renderer->fbo[GBA_GL_FBO_OBJ]);
1101	glViewport(x * renderer->scale, spriteY * renderer->scale, totalWidth * renderer->scale, totalHeight * renderer->scale);
1102	glScissor(x * renderer->scale, y * renderer->scale, totalWidth * renderer->scale, renderer->scale);
1103	glUseProgram(shader->program);
1104	glBindVertexArray(shader->vao);
1105	glActiveTexture(GL_TEXTURE0);
1106	glBindTexture(GL_TEXTURE_2D, renderer->vramTex);
1107	glActiveTexture(GL_TEXTURE0 + 1);
1108	glBindTexture(GL_TEXTURE_2D, renderer->paletteTex);
1109	glUniform2i(uniforms[GBA_GL_VS_LOC], 1, y - spriteY);
1110	glUniform2i(uniforms[GBA_GL_VS_MAXPOS], (GBAObjAttributesBIsHFlip(sprite->b) && !GBAObjAttributesAIsTransformed(sprite->a)) ? -totalWidth : totalWidth, totalHeight);
1111	glUniform1i(uniforms[GBA_GL_OBJ_VRAM], 0);
1112	glUniform1i(uniforms[GBA_GL_OBJ_PALETTE], 1);
1113	glUniform1i(uniforms[GBA_GL_OBJ_CHARBASE], charBase);
1114	glUniform1i(uniforms[GBA_GL_OBJ_STRIDE], stride);
1115	glUniform1i(uniforms[GBA_GL_OBJ_LOCALPALETTE], GBAObjAttributesCGetPalette(sprite->c));
1116	glUniform4i(uniforms[GBA_GL_OBJ_INFLAGS], GBAObjAttributesCGetPriority(sprite->c) << 3,
1117	               (renderer->target1Obj || GBAObjAttributesAGetMode(sprite->a) == OBJ_MODE_SEMITRANSPARENT) | (renderer->target2Obj * 2) | (blendEffect * 4),
1118	               blendEffect == BLEND_ALPHA ? renderer->blda : renderer->bldy, renderer->bldb);
1119	if (GBAObjAttributesAIsTransformed(sprite->a)) {
1120		struct GBAOAMMatrix mat;
1121		LOAD_16(mat.a, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].a);
1122		LOAD_16(mat.b, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].b);
1123		LOAD_16(mat.c, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].c);
1124		LOAD_16(mat.d, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].d);
1125
1126		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 });
1127	} else {
1128		glUniformMatrix2fv(uniforms[GBA_GL_OBJ_TRANSFORM], 1, GL_FALSE, (GLfloat[]) { 1.f, 0, 0, 1.f });
1129	}
1130	glUniform4i(uniforms[GBA_GL_OBJ_DIMS], width, height, totalWidth, totalHeight);
1131	if (GBAObjAttributesAGetMode(sprite->a) == OBJ_MODE_OBJWIN) {
1132		int window = ~renderer->objwin & 0xFF;
1133		glUniform3f(uniforms[GBA_GL_OBJ_OBJWIN], 1, (window & 0xF) / 32.f, (window >> 4) / 32.f);
1134		glDrawBuffers(3, (GLenum[]) { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2 });
1135	} else {
1136		glUniform3f(uniforms[GBA_GL_OBJ_OBJWIN], 0, 0, 0);
1137		glDrawBuffers(2, (GLenum[]) { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 });
1138	}
1139	glEnableVertexAttribArray(0);
1140	glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1141	glDrawBuffers(1, (GLenum[]) { GL_COLOR_ATTACHMENT0 });
1142}
1143
1144void GBAVideoGLRendererDrawBackgroundMode0(struct GBAVideoGLRenderer* renderer, struct GBAVideoGLBackground* background, int y) {
1145	int inY = y + background->y;
1146	int yBase = inY & 0xFF;
1147	if (background->size == 2) {
1148		yBase += inY & 0x100;
1149	} else if (background->size == 3) {
1150		yBase += (inY & 0x100) << 1;
1151	}
1152
1153	const struct GBAVideoGLShader* shader = &renderer->bgShader[background->multipalette ? 1 : 0];
1154	const GLuint* uniforms = shader->uniforms;
1155	glBindFramebuffer(GL_FRAMEBUFFER, background->fbo);
1156	glViewport(0, 0, GBA_VIDEO_HORIZONTAL_PIXELS * renderer->scale, GBA_VIDEO_VERTICAL_PIXELS * renderer->scale);
1157	glScissor(0, y * renderer->scale, GBA_VIDEO_HORIZONTAL_PIXELS * renderer->scale, renderer->scale);
1158	glUseProgram(shader->program);
1159	glBindVertexArray(shader->vao);
1160	glActiveTexture(GL_TEXTURE0);
1161	glBindTexture(GL_TEXTURE_2D, renderer->vramTex);
1162	glActiveTexture(GL_TEXTURE0 + 1);
1163	glBindTexture(GL_TEXTURE_2D, renderer->paletteTex);
1164	glUniform2i(uniforms[GBA_GL_VS_LOC], 1, y);
1165	glUniform2i(uniforms[GBA_GL_VS_MAXPOS], GBA_VIDEO_HORIZONTAL_PIXELS, GBA_VIDEO_VERTICAL_PIXELS);
1166	glUniform1i(uniforms[GBA_GL_BG_VRAM], 0);
1167	glUniform1i(uniforms[GBA_GL_BG_PALETTE], 1);
1168	glUniform1i(uniforms[GBA_GL_BG_SCREENBASE], background->screenBase);
1169	glUniform1i(uniforms[GBA_GL_BG_CHARBASE], background->charBase);
1170	glUniform1i(uniforms[GBA_GL_BG_SIZE], background->size);
1171	glUniform2i(uniforms[GBA_GL_BG_OFFSET], background->x, yBase - y);
1172	glUniform4i(uniforms[GBA_GL_BG_INFLAGS], (background->priority << 3) + (background->index << 1) + 1,
1173		                                     background->target1 | (background->target2 * 2) | (renderer->blendEffect * 4),
1174		                                     renderer->blendEffect == BLEND_ALPHA ? renderer->blda : renderer->bldy, renderer->bldb);
1175	glDrawBuffers(2, (GLenum[]) { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 });
1176	glEnableVertexAttribArray(0);
1177	glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1178	glDrawBuffers(1, (GLenum[]) { GL_COLOR_ATTACHMENT0 });
1179}
1180
1181void GBAVideoGLRendererDrawBackgroundMode2(struct GBAVideoGLRenderer* renderer, struct GBAVideoGLBackground* background, int y) {
1182	const struct GBAVideoGLShader* shader = &renderer->bgShader[background->overflow ? 2 : 3];
1183	const GLuint* uniforms = shader->uniforms;
1184	glBindFramebuffer(GL_FRAMEBUFFER, background->fbo);
1185	glViewport(0, 0, GBA_VIDEO_HORIZONTAL_PIXELS * renderer->scale, GBA_VIDEO_VERTICAL_PIXELS * renderer->scale);
1186	glScissor(0, y * renderer->scale, GBA_VIDEO_HORIZONTAL_PIXELS * renderer->scale, renderer->scale);
1187	glUseProgram(shader->program);
1188	glBindVertexArray(shader->vao);
1189	glActiveTexture(GL_TEXTURE0);
1190	glBindTexture(GL_TEXTURE_2D, renderer->vramTex);
1191	glActiveTexture(GL_TEXTURE0 + 1);
1192	glBindTexture(GL_TEXTURE_2D, renderer->paletteTex);
1193	glUniform2i(uniforms[GBA_GL_VS_LOC], 1, y);
1194	glUniform2i(uniforms[GBA_GL_VS_MAXPOS], GBA_VIDEO_HORIZONTAL_PIXELS, GBA_VIDEO_VERTICAL_PIXELS);
1195	glUniform1i(uniforms[GBA_GL_BG_VRAM], 0);
1196	glUniform1i(uniforms[GBA_GL_BG_PALETTE], 1);
1197	glUniform1i(uniforms[GBA_GL_BG_SCREENBASE], background->screenBase);
1198	glUniform1i(uniforms[GBA_GL_BG_CHARBASE], background->charBase);
1199	glUniform1i(uniforms[GBA_GL_BG_SIZE], background->size);
1200	glUniform4i(uniforms[GBA_GL_BG_INFLAGS], (background->priority << 3) + (background->index << 1) + 1,
1201		           background->target1 | (background->target2 * 2) | (renderer->blendEffect * 4),
1202		           renderer->blendEffect == BLEND_ALPHA ? renderer->blda : renderer->bldy, renderer->bldb);
1203	if (renderer->scale > 1) {
1204		glUniform2iv(uniforms[GBA_GL_BG_OFFSET], 4, (GLint[]) {
1205			background->affine[0].sx, background->affine[0].sy,
1206			background->affine[1].sx, background->affine[1].sy,
1207			background->affine[2].sx, background->affine[2].sy,
1208			background->affine[3].sx, background->affine[3].sy,
1209		});
1210		glUniform2iv(uniforms[GBA_GL_BG_TRANSFORM], 4, (GLint[]) {
1211			background->affine[0].dx, background->affine[0].dy,
1212			background->affine[1].dx, background->affine[1].dy,
1213			background->affine[2].dx, background->affine[2].dy,
1214			background->affine[3].dx, background->affine[3].dy,
1215		});
1216	} else {
1217		glUniform2iv(uniforms[GBA_GL_BG_OFFSET], 4, (GLint[]) {
1218			background->affine[0].sx, background->affine[0].sy,
1219			background->affine[0].sx, background->affine[0].sy,
1220			background->affine[0].sx, background->affine[0].sy,
1221			background->affine[0].sx, background->affine[0].sy,
1222		});
1223		glUniform2iv(uniforms[GBA_GL_BG_TRANSFORM], 4, (GLint[]) {
1224			background->affine[0].dx, background->affine[0].dy,
1225			background->affine[0].dx, background->affine[0].dy,
1226			background->affine[0].dx, background->affine[0].dy,
1227			background->affine[0].dx, background->affine[0].dy,
1228		});
1229	}
1230	glDrawBuffers(2, (GLenum[]) { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 });
1231	glEnableVertexAttribArray(0);
1232	glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1233	glDrawBuffers(1, (GLenum[]) { GL_COLOR_ATTACHMENT0 });
1234}
1235
1236static void _clearWindow(GBAWindowControl window, int start, int end, int y, int scale) {
1237	glScissor(start, y, end - start, scale);
1238	window = ~window & 0xFF;
1239	glClearColor((window & 0xF) / 32.f, (window >> 4) / 32.f, 0, 0);
1240	glClear(GL_COLOR_BUFFER_BIT);
1241}
1242
1243void GBAVideoGLRendererDrawWindow(struct GBAVideoGLRenderer* renderer, int y) {
1244	glBindFramebuffer(GL_FRAMEBUFFER, renderer->fbo[GBA_GL_FBO_WINDOW]);
1245	int dispcnt = ((renderer->dispcnt >> 8) & 0x1F) | 0x20;
1246	if (!(renderer->dispcnt & 0xE000)) {
1247		_clearWindow(dispcnt, 0, GBA_VIDEO_HORIZONTAL_PIXELS * renderer->scale, y * renderer->scale, renderer->scale);
1248	} else {
1249		_clearWindow(renderer->winout & dispcnt, 0, GBA_VIDEO_HORIZONTAL_PIXELS * renderer->scale, y * renderer->scale, renderer->scale);
1250		if (GBARegisterDISPCNTIsWin1Enable(renderer->dispcnt) && y >= renderer->winN[1].v.start && y < renderer->winN[1].v.end) {
1251			_clearWindow(renderer->winN[1].control & dispcnt, renderer->winN[1].h.start * renderer->scale, renderer->winN[1].h.end * renderer->scale, y * renderer->scale, renderer->scale);
1252		}
1253		if (GBARegisterDISPCNTIsWin0Enable(renderer->dispcnt) && y >= renderer->winN[0].v.start && y < renderer->winN[0].v.end) {
1254			_clearWindow(renderer->winN[0].control & dispcnt, renderer->winN[0].h.start * renderer->scale, renderer->winN[0].h.end * renderer->scale, y * renderer->scale, renderer->scale);
1255		}
1256	}
1257}
1258
1259#endif