all repos — mgba @ b92a08e6f1fddee946c93030fceab0af83aea7a0

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