all repos — mgba @ fd5b95024eaf0564ac8fb550763af6bb76c5dfa6

mGBA Game Boy Advance Emulator

src/gba/renderers/video-software.c (view raw)

   1#include "video-software.h"
   2
   3#include "gba.h"
   4#include "gba-io.h"
   5
   6static const int _objSizes[32] = {
   7	8, 8,
   8	16, 16,
   9	32, 32,
  10	64, 64,
  11	16, 8,
  12	32, 8,
  13	32, 16,
  14	64, 32,
  15	8, 16,
  16	8, 32,
  17	16, 32,
  18	32, 64,
  19	0, 0,
  20	0, 0,
  21	0, 0,
  22	0, 0
  23};
  24
  25static void GBAVideoSoftwareRendererInit(struct GBAVideoRenderer* renderer);
  26static void GBAVideoSoftwareRendererDeinit(struct GBAVideoRenderer* renderer);
  27static void GBAVideoSoftwareRendererWriteOAM(struct GBAVideoRenderer* renderer, uint32_t oam);
  28static void GBAVideoSoftwareRendererWritePalette(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value);
  29static uint16_t GBAVideoSoftwareRendererWriteVideoRegister(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value);
  30static void GBAVideoSoftwareRendererDrawScanline(struct GBAVideoRenderer* renderer, int y);
  31static void GBAVideoSoftwareRendererFinishFrame(struct GBAVideoRenderer* renderer);
  32static void GBAVideoSoftwareRendererGetPixels(struct GBAVideoRenderer* renderer, unsigned* stride, void** pixels);
  33static void GBAVideoSoftwareRendererPutPixels(struct GBAVideoRenderer* renderer, unsigned stride, void* pixels);
  34
  35static void GBAVideoSoftwareRendererUpdateDISPCNT(struct GBAVideoSoftwareRenderer* renderer);
  36static void GBAVideoSoftwareRendererWriteBGCNT(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* bg, uint16_t value);
  37static void GBAVideoSoftwareRendererWriteBGPA(struct GBAVideoSoftwareBackground* bg, uint16_t value);
  38static void GBAVideoSoftwareRendererWriteBGPB(struct GBAVideoSoftwareBackground* bg, uint16_t value);
  39static void GBAVideoSoftwareRendererWriteBGPC(struct GBAVideoSoftwareBackground* bg, uint16_t value);
  40static void GBAVideoSoftwareRendererWriteBGPD(struct GBAVideoSoftwareBackground* bg, uint16_t value);
  41static void GBAVideoSoftwareRendererWriteBGX_LO(struct GBAVideoSoftwareBackground* bg, uint16_t value);
  42static void GBAVideoSoftwareRendererWriteBGX_HI(struct GBAVideoSoftwareBackground* bg, uint16_t value);
  43static void GBAVideoSoftwareRendererWriteBGY_LO(struct GBAVideoSoftwareBackground* bg, uint16_t value);
  44static void GBAVideoSoftwareRendererWriteBGY_HI(struct GBAVideoSoftwareBackground* bg, uint16_t value);
  45static void GBAVideoSoftwareRendererWriteBLDCNT(struct GBAVideoSoftwareRenderer* renderer, uint16_t value);
  46
  47static void _drawScanline(struct GBAVideoSoftwareRenderer* renderer, int y);
  48static void _drawBackgroundMode0(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* background, int y);
  49static void _drawBackgroundMode2(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* background, int y);
  50static void _drawBackgroundMode3(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* background, int y);
  51static void _drawBackgroundMode4(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* background, int y);
  52static void _drawBackgroundMode5(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* background, int y);
  53static void _cleanOAM(struct GBAVideoSoftwareRenderer* renderer);
  54static int _preprocessSprite(struct GBAVideoSoftwareRenderer* renderer, struct GBAObj* sprite, int y);
  55static void _postprocessSprite(struct GBAVideoSoftwareRenderer* renderer, unsigned priority);
  56
  57static void _updatePalettes(struct GBAVideoSoftwareRenderer* renderer);
  58static inline unsigned _brighten(unsigned color, int y);
  59static inline unsigned _darken(unsigned color, int y);
  60static unsigned _mix(int weightA, unsigned colorA, int weightB, unsigned colorB);
  61
  62void GBAVideoSoftwareRendererCreate(struct GBAVideoSoftwareRenderer* renderer) {
  63	renderer->d.init = GBAVideoSoftwareRendererInit;
  64	renderer->d.reset = GBAVideoSoftwareRendererInit;
  65	renderer->d.deinit = GBAVideoSoftwareRendererDeinit;
  66	renderer->d.writeVideoRegister = GBAVideoSoftwareRendererWriteVideoRegister;
  67	renderer->d.writeOAM = GBAVideoSoftwareRendererWriteOAM;
  68	renderer->d.writePalette = GBAVideoSoftwareRendererWritePalette;
  69	renderer->d.drawScanline = GBAVideoSoftwareRendererDrawScanline;
  70	renderer->d.finishFrame = GBAVideoSoftwareRendererFinishFrame;
  71	renderer->d.getPixels = GBAVideoSoftwareRendererGetPixels;
  72	renderer->d.putPixels = GBAVideoSoftwareRendererPutPixels;
  73}
  74
  75static void GBAVideoSoftwareRendererInit(struct GBAVideoRenderer* renderer) {
  76	struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
  77	int i;
  78
  79	softwareRenderer->dispcnt = 0x0080;
  80
  81	softwareRenderer->target1Obj = 0;
  82	softwareRenderer->target1Bd = 0;
  83	softwareRenderer->target2Obj = 0;
  84	softwareRenderer->target2Bd = 0;
  85	softwareRenderer->blendEffect = BLEND_NONE;
  86	memset(softwareRenderer->normalPalette, 0, sizeof(softwareRenderer->normalPalette));
  87	memset(softwareRenderer->variantPalette, 0, sizeof(softwareRenderer->variantPalette));
  88
  89	softwareRenderer->blda = 0;
  90	softwareRenderer->bldb = 0;
  91	softwareRenderer->bldy = 0;
  92
  93	softwareRenderer->winN[0].h.packed = 0;
  94	softwareRenderer->winN[0].v.packed = 0;
  95	softwareRenderer->winN[0].control.packed = 0;
  96	softwareRenderer->winN[0].control.priority = 0;
  97	softwareRenderer->winN[1].h.packed = 0;
  98	softwareRenderer->winN[1].v.packed = 0;
  99	softwareRenderer->winN[1].control.packed = 0;
 100	softwareRenderer->winN[1].control.priority = 1;
 101	softwareRenderer->objwin.packed = 0;
 102	softwareRenderer->objwin.priority = 2;
 103	softwareRenderer->winout.packed = 0;
 104	softwareRenderer->winout.priority = 3;
 105	softwareRenderer->oamMax = 0;
 106
 107	softwareRenderer->mosaic.packed = 0;
 108
 109	for (i = 0; i < 4; ++i) {
 110		struct GBAVideoSoftwareBackground* bg = &softwareRenderer->bg[i];
 111		bg->index = i;
 112		bg->enabled = 0;
 113		bg->priority = 0;
 114		bg->charBase = 0;
 115		bg->mosaic = 0;
 116		bg->multipalette = 0;
 117		bg->screenBase = 0;
 118		bg->overflow = 0;
 119		bg->size = 0;
 120		bg->target1 = 0;
 121		bg->target2 = 0;
 122		bg->x = 0;
 123		bg->y = 0;
 124		bg->refx = 0;
 125		bg->refy = 0;
 126		bg->dx = 256;
 127		bg->dmx = 0;
 128		bg->dy = 0;
 129		bg->dmy = 256;
 130		bg->sx = 0;
 131		bg->sy = 0;
 132	}
 133}
 134
 135static void GBAVideoSoftwareRendererDeinit(struct GBAVideoRenderer* renderer) {
 136	struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
 137	UNUSED(softwareRenderer);
 138}
 139
 140static uint16_t GBAVideoSoftwareRendererWriteVideoRegister(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
 141	struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
 142	switch (address) {
 143	case REG_DISPCNT:
 144		softwareRenderer->dispcnt = value;
 145		GBAVideoSoftwareRendererUpdateDISPCNT(softwareRenderer);
 146		break;
 147	case REG_BG0CNT:
 148		value &= 0xFFCF;
 149		GBAVideoSoftwareRendererWriteBGCNT(softwareRenderer, &softwareRenderer->bg[0], value);
 150		break;
 151	case REG_BG1CNT:
 152		value &= 0xFFCF;
 153		GBAVideoSoftwareRendererWriteBGCNT(softwareRenderer, &softwareRenderer->bg[1], value);
 154		break;
 155	case REG_BG2CNT:
 156		value &= 0xFFCF;
 157		GBAVideoSoftwareRendererWriteBGCNT(softwareRenderer, &softwareRenderer->bg[2], value);
 158		break;
 159	case REG_BG3CNT:
 160		value &= 0xFFCF;
 161		GBAVideoSoftwareRendererWriteBGCNT(softwareRenderer, &softwareRenderer->bg[3], value);
 162		break;
 163	case REG_BG0HOFS:
 164		value &= 0x01FF;
 165		softwareRenderer->bg[0].x = value;
 166		break;
 167	case REG_BG0VOFS:
 168		value &= 0x01FF;
 169		softwareRenderer->bg[0].y = value;
 170		break;
 171	case REG_BG1HOFS:
 172		value &= 0x01FF;
 173		softwareRenderer->bg[1].x = value;
 174		break;
 175	case REG_BG1VOFS:
 176		value &= 0x01FF;
 177		softwareRenderer->bg[1].y = value;
 178		break;
 179	case REG_BG2HOFS:
 180		value &= 0x01FF;
 181		softwareRenderer->bg[2].x = value;
 182		break;
 183	case REG_BG2VOFS:
 184		value &= 0x01FF;
 185		softwareRenderer->bg[2].y = value;
 186		break;
 187	case REG_BG3HOFS:
 188		value &= 0x01FF;
 189		softwareRenderer->bg[3].x = value;
 190		break;
 191	case REG_BG3VOFS:
 192		value &= 0x01FF;
 193		softwareRenderer->bg[3].y = value;
 194		break;
 195	case REG_BG2PA:
 196		GBAVideoSoftwareRendererWriteBGPA(&softwareRenderer->bg[2], value);
 197		break;
 198	case REG_BG2PB:
 199		GBAVideoSoftwareRendererWriteBGPB(&softwareRenderer->bg[2], value);
 200		break;
 201	case REG_BG2PC:
 202		GBAVideoSoftwareRendererWriteBGPC(&softwareRenderer->bg[2], value);
 203		break;
 204	case REG_BG2PD:
 205		GBAVideoSoftwareRendererWriteBGPD(&softwareRenderer->bg[2], value);
 206		break;
 207	case REG_BG2X_LO:
 208		GBAVideoSoftwareRendererWriteBGX_LO(&softwareRenderer->bg[2], value);
 209		break;
 210	case REG_BG2X_HI:
 211		GBAVideoSoftwareRendererWriteBGX_HI(&softwareRenderer->bg[2], value);
 212		break;
 213	case REG_BG2Y_LO:
 214		GBAVideoSoftwareRendererWriteBGY_LO(&softwareRenderer->bg[2], value);
 215		break;
 216	case REG_BG2Y_HI:
 217		GBAVideoSoftwareRendererWriteBGY_HI(&softwareRenderer->bg[2], value);
 218		break;
 219	case REG_BG3PA:
 220		GBAVideoSoftwareRendererWriteBGPA(&softwareRenderer->bg[3], value);
 221		break;
 222	case REG_BG3PB:
 223		GBAVideoSoftwareRendererWriteBGPB(&softwareRenderer->bg[3], value);
 224		break;
 225	case REG_BG3PC:
 226		GBAVideoSoftwareRendererWriteBGPC(&softwareRenderer->bg[3], value);
 227		break;
 228	case REG_BG3PD:
 229		GBAVideoSoftwareRendererWriteBGPD(&softwareRenderer->bg[3], value);
 230		break;
 231	case REG_BG3X_LO:
 232		GBAVideoSoftwareRendererWriteBGX_LO(&softwareRenderer->bg[3], value);
 233		break;
 234	case REG_BG3X_HI:
 235		GBAVideoSoftwareRendererWriteBGX_HI(&softwareRenderer->bg[3], value);
 236		break;
 237	case REG_BG3Y_LO:
 238		GBAVideoSoftwareRendererWriteBGY_LO(&softwareRenderer->bg[3], value);
 239		break;
 240	case REG_BG3Y_HI:
 241		GBAVideoSoftwareRendererWriteBGY_HI(&softwareRenderer->bg[3], value);
 242		break;
 243	case REG_BLDCNT:
 244		GBAVideoSoftwareRendererWriteBLDCNT(softwareRenderer, value);
 245		break;
 246	case REG_BLDALPHA:
 247		softwareRenderer->blda = value & 0x1F;
 248		if (softwareRenderer->blda > 0x10) {
 249			softwareRenderer->blda = 0x10;
 250		}
 251		softwareRenderer->bldb = (value >> 8) & 0x1F;
 252		if (softwareRenderer->bldb > 0x10) {
 253			softwareRenderer->bldb = 0x10;
 254		}
 255		break;
 256	case REG_BLDY:
 257		softwareRenderer->bldy = value & 0x1F;
 258		if (softwareRenderer->bldy > 0x10) {
 259			softwareRenderer->bldy = 0x10;
 260		}
 261		_updatePalettes(softwareRenderer);
 262		break;
 263	case REG_WIN0H:
 264		softwareRenderer->winN[0].h.packed = value;
 265		if (softwareRenderer->winN[0].h.start > VIDEO_HORIZONTAL_PIXELS && softwareRenderer->winN[0].h.start > softwareRenderer->winN[0].h.end) {
 266			softwareRenderer->winN[0].h.start = 0;
 267		}
 268		if (softwareRenderer->winN[0].h.start > softwareRenderer->winN[0].h.end || softwareRenderer->winN[0].h.end > VIDEO_HORIZONTAL_PIXELS) {
 269			softwareRenderer->winN[0].h.end = VIDEO_HORIZONTAL_PIXELS;
 270		}
 271		break;
 272	case REG_WIN1H:
 273		softwareRenderer->winN[1].h.packed = value;
 274		if (softwareRenderer->winN[1].h.start > VIDEO_HORIZONTAL_PIXELS && softwareRenderer->winN[1].h.start > softwareRenderer->winN[1].h.end) {
 275			softwareRenderer->winN[1].h.start = 0;
 276		}
 277		if (softwareRenderer->winN[1].h.start > softwareRenderer->winN[1].h.end || softwareRenderer->winN[1].h.end > VIDEO_HORIZONTAL_PIXELS) {
 278			softwareRenderer->winN[1].h.end = VIDEO_HORIZONTAL_PIXELS;
 279		}
 280		break;
 281	case REG_WIN0V:
 282		softwareRenderer->winN[0].v.packed = value;
 283		if (softwareRenderer->winN[0].v.start > VIDEO_VERTICAL_PIXELS && softwareRenderer->winN[0].v.start > softwareRenderer->winN[0].v.end) {
 284			softwareRenderer->winN[0].v.start = 0;
 285		}
 286		if (softwareRenderer->winN[0].v.start > softwareRenderer->winN[0].v.end || softwareRenderer->winN[0].v.end > VIDEO_HORIZONTAL_PIXELS) {
 287			softwareRenderer->winN[0].v.end = VIDEO_VERTICAL_PIXELS;
 288		}
 289		break;
 290	case REG_WIN1V:
 291		softwareRenderer->winN[1].v.packed = value;
 292		if (softwareRenderer->winN[1].v.start > VIDEO_VERTICAL_PIXELS && softwareRenderer->winN[1].v.start > softwareRenderer->winN[1].v.end) {
 293			softwareRenderer->winN[1].v.start = 0;
 294		}
 295		if (softwareRenderer->winN[1].v.start > softwareRenderer->winN[1].v.end || softwareRenderer->winN[1].v.end > VIDEO_HORIZONTAL_PIXELS) {
 296			softwareRenderer->winN[1].v.end = VIDEO_VERTICAL_PIXELS;
 297		}
 298		break;
 299	case REG_WININ:
 300		softwareRenderer->winN[0].control.packed = value;
 301		softwareRenderer->winN[1].control.packed = value >> 8;
 302		break;
 303	case REG_WINOUT:
 304		softwareRenderer->winout.packed = value;
 305		softwareRenderer->objwin.packed = value >> 8;
 306		break;
 307	case REG_MOSAIC:
 308		softwareRenderer->mosaic.packed = value;
 309		break;
 310	case REG_GREENSWP:
 311		GBALog(0, GBA_LOG_STUB, "Stub video register write: 0x%03X", address);
 312		break;
 313	default:
 314		GBALog(0, GBA_LOG_GAME_ERROR, "Invalid video register: 0x%03X", address);
 315	}
 316	return value;
 317}
 318
 319static void GBAVideoSoftwareRendererWriteOAM(struct GBAVideoRenderer* renderer, uint32_t oam) {
 320	struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
 321	softwareRenderer->oamDirty = 1;
 322	UNUSED(oam);
 323}
 324
 325static void GBAVideoSoftwareRendererWritePalette(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
 326	struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
 327#ifdef COLOR_16_BIT
 328#ifdef COLOR_5_6_5
 329	unsigned color = 0;
 330	color |= (value & 0x001F) << 11;
 331	color |= (value & 0x03E0) << 1;
 332	color |= (value & 0x7C00) >> 10;
 333#else
 334	unsigned color = value;
 335#endif
 336#else
 337	unsigned color = 0;
 338	color |= (value << 3) & 0xF8;
 339	color |= (value << 6) & 0xF800;
 340	color |= (value << 9) & 0xF80000;
 341#endif
 342	softwareRenderer->normalPalette[address >> 1] = color;
 343	if (softwareRenderer->blendEffect == BLEND_BRIGHTEN) {
 344		softwareRenderer->variantPalette[address >> 1] = _brighten(color, softwareRenderer->bldy);
 345	} else if (softwareRenderer->blendEffect == BLEND_DARKEN) {
 346		softwareRenderer->variantPalette[address >> 1] = _darken(color, softwareRenderer->bldy);
 347	}
 348}
 349
 350static void _breakWindow(struct GBAVideoSoftwareRenderer* softwareRenderer, struct WindowN* win) {
 351	int activeWindow;
 352	int startX = 0;
 353	if (win->h.end > 0) {
 354		for (activeWindow = 0; activeWindow < softwareRenderer->nWindows; ++activeWindow) {
 355			if (win->h.start < softwareRenderer->windows[activeWindow].endX) {
 356				// Insert a window before the end of the active window
 357				struct Window oldWindow = softwareRenderer->windows[activeWindow];
 358				if (win->h.start > startX) {
 359					// And after the start of the active window
 360					int nextWindow = softwareRenderer->nWindows;
 361					++softwareRenderer->nWindows;
 362					for (; nextWindow > activeWindow; --nextWindow) {
 363						softwareRenderer->windows[nextWindow] = softwareRenderer->windows[nextWindow - 1];
 364					}
 365					softwareRenderer->windows[activeWindow].endX = win->h.start;
 366					++activeWindow;
 367				}
 368				softwareRenderer->windows[activeWindow].control = win->control;
 369				softwareRenderer->windows[activeWindow].endX = win->h.end;
 370				if (win->h.end >= oldWindow.endX) {
 371					// Trim off extra windows we've overwritten
 372					for (++activeWindow; win->h.end >= softwareRenderer->windows[activeWindow].endX && softwareRenderer->nWindows > activeWindow; ++activeWindow) {
 373						softwareRenderer->windows[activeWindow] = softwareRenderer->windows[activeWindow + 1];
 374						--softwareRenderer->nWindows;
 375					}
 376				} else {
 377					++activeWindow;
 378					int nextWindow = softwareRenderer->nWindows;
 379					++softwareRenderer->nWindows;
 380					for (; nextWindow > activeWindow; --nextWindow) {
 381						softwareRenderer->windows[nextWindow] = softwareRenderer->windows[nextWindow - 1];
 382					}
 383					softwareRenderer->windows[activeWindow] = oldWindow;
 384				}
 385				break;
 386			}
 387			startX = softwareRenderer->windows[activeWindow].endX;
 388		}
 389	}
 390}
 391
 392static void _cleanOAM(struct GBAVideoSoftwareRenderer* renderer) {
 393	int i;
 394	int oamMax = 0;
 395	for (i = 0; i < 128; ++i) {
 396		struct GBAObj obj;
 397		LOAD_16(obj.a, 0, &renderer->d.oam->obj[i].a);
 398		LOAD_16(obj.b, 0, &renderer->d.oam->obj[i].b);
 399		LOAD_16(obj.c, 0, &renderer->d.oam->obj[i].c);
 400		if (GBAObjAttributesAIsTransformed(obj.a) || !GBAObjAttributesAIsDisable(obj.a)) {
 401			int height = _objSizes[GBAObjAttributesAGetShape(obj.a) * 8 + GBAObjAttributesBGetSize(obj.b) * 2 + 1];
 402			if (GBAObjAttributesAIsTransformed(obj.a)) {
 403				height <<= GBAObjAttributesAGetDoubleSize(obj.a);
 404			}
 405			if (GBAObjAttributesAGetY(obj.a) < VIDEO_VERTICAL_PIXELS || GBAObjAttributesAGetY(obj.a) + height >= VIDEO_VERTICAL_TOTAL_PIXELS) {
 406				renderer->sprites[oamMax].y = GBAObjAttributesAGetY(obj.a);
 407				renderer->sprites[oamMax].endY = GBAObjAttributesAGetY(obj.a) + height;
 408				renderer->sprites[oamMax].obj = obj;
 409				++oamMax;
 410			}
 411		}
 412	}
 413	renderer->oamMax = oamMax;
 414	renderer->oamDirty = 0;
 415}
 416
 417
 418static void GBAVideoSoftwareRendererDrawScanline(struct GBAVideoRenderer* renderer, int y) {
 419	struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
 420
 421	color_t* row = &softwareRenderer->outputBuffer[softwareRenderer->outputBufferStride * y];
 422	if (GBARegisterDISPCNTIsForcedBlank(softwareRenderer->dispcnt)) {
 423		int x;
 424		for (x = 0; x < VIDEO_HORIZONTAL_PIXELS; ++x) {
 425			row[x] = GBA_COLOR_WHITE;
 426		}
 427		return;
 428	}
 429
 430	int x;
 431	for (x = 0; x < VIDEO_HORIZONTAL_PIXELS; x += 4) {
 432		softwareRenderer->spriteLayer[x] = FLAG_UNWRITTEN;
 433		softwareRenderer->spriteLayer[x + 1] = FLAG_UNWRITTEN;
 434		softwareRenderer->spriteLayer[x + 2] = FLAG_UNWRITTEN;
 435		softwareRenderer->spriteLayer[x + 3] = FLAG_UNWRITTEN;
 436	}
 437
 438	softwareRenderer->windows[0].endX = VIDEO_HORIZONTAL_PIXELS;
 439	softwareRenderer->nWindows = 1;
 440	if (GBARegisterDISPCNTIsWin0Enable(softwareRenderer->dispcnt) || GBARegisterDISPCNTIsWin1Enable(softwareRenderer->dispcnt) || GBARegisterDISPCNTIsObjwinEnable(softwareRenderer->dispcnt)) {
 441		softwareRenderer->windows[0].control = softwareRenderer->winout;
 442		if (GBARegisterDISPCNTIsWin1Enable(softwareRenderer->dispcnt) && y < softwareRenderer->winN[1].v.end && y >= softwareRenderer->winN[1].v.start) {
 443			_breakWindow(softwareRenderer, &softwareRenderer->winN[1]);
 444		}
 445		if (GBARegisterDISPCNTIsWin0Enable(softwareRenderer->dispcnt) && y < softwareRenderer->winN[0].v.end && y >= softwareRenderer->winN[0].v.start) {
 446			_breakWindow(softwareRenderer, &softwareRenderer->winN[0]);
 447		}
 448	} else {
 449		softwareRenderer->windows[0].control.packed = 0xFF;
 450	}
 451
 452	int w;
 453	x = 0;
 454	for (w = 0; w < softwareRenderer->nWindows; ++w) {
 455		// TOOD: handle objwin on backdrop
 456		uint32_t backdrop = FLAG_UNWRITTEN | FLAG_PRIORITY | FLAG_IS_BACKGROUND;
 457		if (!softwareRenderer->target1Bd || softwareRenderer->blendEffect == BLEND_NONE || softwareRenderer->blendEffect == BLEND_ALPHA || !softwareRenderer->windows[w].control.blendEnable) {
 458			backdrop |= softwareRenderer->normalPalette[0];
 459		} else {
 460			backdrop |= softwareRenderer->variantPalette[0];
 461		}
 462		int end = softwareRenderer->windows[w].endX;
 463		for (; x < end; ++x) {
 464			softwareRenderer->row[x] = backdrop;
 465		}
 466	}
 467
 468	_drawScanline(softwareRenderer, y);
 469
 470	if (softwareRenderer->target2Bd) {
 471		x = 0;
 472		for (w = 0; w < softwareRenderer->nWindows; ++w) {
 473		uint32_t backdrop = FLAG_UNWRITTEN;
 474			if (!softwareRenderer->target1Bd || softwareRenderer->blendEffect == BLEND_NONE || softwareRenderer->blendEffect == BLEND_ALPHA || !softwareRenderer->windows[w].control.blendEnable) {
 475				backdrop |= softwareRenderer->normalPalette[0];
 476			} else {
 477				backdrop |= softwareRenderer->variantPalette[0];
 478			}
 479			int end = softwareRenderer->windows[w].endX;
 480			for (; x < end; ++x) {
 481				uint32_t color = softwareRenderer->row[x];
 482				if (color & FLAG_TARGET_1) {
 483					softwareRenderer->row[x] = _mix(softwareRenderer->bldb, backdrop, softwareRenderer->blda, color);
 484				}
 485			}
 486		}
 487	}
 488
 489#ifdef COLOR_16_BIT
 490#ifdef __arm__
 491	_to16Bit(row, softwareRenderer->row, VIDEO_HORIZONTAL_PIXELS);
 492#else
 493	for (x = 0; x < VIDEO_HORIZONTAL_PIXELS; ++x) {
 494		row[x] = softwareRenderer->row[x];
 495	}
 496#endif
 497#else
 498	memcpy(row, softwareRenderer->row, VIDEO_HORIZONTAL_PIXELS * sizeof(*row));
 499#endif
 500}
 501
 502static void GBAVideoSoftwareRendererFinishFrame(struct GBAVideoRenderer* renderer) {
 503	struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
 504
 505	softwareRenderer->bg[2].sx = softwareRenderer->bg[2].refx;
 506	softwareRenderer->bg[2].sy = softwareRenderer->bg[2].refy;
 507	softwareRenderer->bg[3].sx = softwareRenderer->bg[3].refx;
 508	softwareRenderer->bg[3].sy = softwareRenderer->bg[3].refy;
 509}
 510
 511static void GBAVideoSoftwareRendererGetPixels(struct GBAVideoRenderer* renderer, unsigned* stride, void** pixels) {
 512	struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
 513
 514	*stride = softwareRenderer->outputBufferStride;
 515	*pixels = softwareRenderer->outputBuffer;
 516}
 517
 518static void GBAVideoSoftwareRendererPutPixels(struct GBAVideoRenderer* renderer, unsigned stride, void* pixels) {
 519	struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
 520
 521	uint32_t* colorPixels = pixels;
 522	unsigned i;
 523	for (i = 0; i < VIDEO_VERTICAL_PIXELS; ++i) {
 524		memmove(&softwareRenderer->outputBuffer[softwareRenderer->outputBufferStride * i], &colorPixels[stride * i], VIDEO_HORIZONTAL_PIXELS * BYTES_PER_PIXEL);
 525	}
 526}
 527
 528static void GBAVideoSoftwareRendererUpdateDISPCNT(struct GBAVideoSoftwareRenderer* renderer) {
 529	renderer->bg[0].enabled = GBARegisterDISPCNTGetBg0Enable(renderer->dispcnt);
 530	renderer->bg[1].enabled = GBARegisterDISPCNTGetBg1Enable(renderer->dispcnt);
 531	renderer->bg[2].enabled = GBARegisterDISPCNTGetBg2Enable(renderer->dispcnt);
 532	renderer->bg[3].enabled = GBARegisterDISPCNTGetBg3Enable(renderer->dispcnt);
 533}
 534
 535static void GBAVideoSoftwareRendererWriteBGCNT(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* bg, uint16_t value) {
 536	UNUSED(renderer);
 537	bg->priority = GBARegisterBGCNTGetPriority(value);
 538	bg->charBase = GBARegisterBGCNTGetCharBase(value) << 14;
 539	bg->mosaic = GBARegisterBGCNTGetMosaic(value);
 540	bg->multipalette = GBARegisterBGCNTGet256Color(value);
 541	bg->screenBase = GBARegisterBGCNTGetScreenBase(value) << 11;
 542	bg->overflow = GBARegisterBGCNTGetOverflow(value);
 543	bg->size = GBARegisterBGCNTGetSize(value);
 544}
 545
 546static void GBAVideoSoftwareRendererWriteBGPA(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
 547	bg->dx = value;
 548}
 549
 550static void GBAVideoSoftwareRendererWriteBGPB(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
 551	bg->dmx = value;
 552}
 553
 554static void GBAVideoSoftwareRendererWriteBGPC(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
 555	bg->dy = value;
 556}
 557
 558static void GBAVideoSoftwareRendererWriteBGPD(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
 559	bg->dmy = value;
 560}
 561
 562static void GBAVideoSoftwareRendererWriteBGX_LO(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
 563	bg->refx = (bg->refx & 0xFFFF0000) | value;
 564	bg->sx = bg->refx;
 565}
 566
 567static void GBAVideoSoftwareRendererWriteBGX_HI(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
 568	bg->refx = (bg->refx & 0x0000FFFF) | (value << 16);
 569	bg->refx <<= 4;
 570	bg->refx >>= 4;
 571	bg->sx = bg->refx;
 572}
 573
 574static void GBAVideoSoftwareRendererWriteBGY_LO(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
 575	bg->refy = (bg->refy & 0xFFFF0000) | value;
 576	bg->sy = bg->refy;
 577}
 578
 579static void GBAVideoSoftwareRendererWriteBGY_HI(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
 580	bg->refy = (bg->refy & 0x0000FFFF) | (value << 16);
 581	bg->refy <<= 4;
 582	bg->refy >>= 4;
 583	bg->sy = bg->refy;
 584}
 585
 586static void GBAVideoSoftwareRendererWriteBLDCNT(struct GBAVideoSoftwareRenderer* renderer, uint16_t value) {
 587	union {
 588		struct {
 589			unsigned target1Bg0 : 1;
 590			unsigned target1Bg1 : 1;
 591			unsigned target1Bg2 : 1;
 592			unsigned target1Bg3 : 1;
 593			unsigned target1Obj : 1;
 594			unsigned target1Bd : 1;
 595			enum BlendEffect effect : 2;
 596			unsigned target2Bg0 : 1;
 597			unsigned target2Bg1 : 1;
 598			unsigned target2Bg2 : 1;
 599			unsigned target2Bg3 : 1;
 600			unsigned target2Obj : 1;
 601			unsigned target2Bd : 1;
 602		};
 603		uint16_t packed;
 604	} bldcnt = { .packed = value };
 605
 606	enum BlendEffect oldEffect = renderer->blendEffect;
 607
 608	renderer->bg[0].target1 = bldcnt.target1Bg0;
 609	renderer->bg[1].target1 = bldcnt.target1Bg1;
 610	renderer->bg[2].target1 = bldcnt.target1Bg2;
 611	renderer->bg[3].target1 = bldcnt.target1Bg3;
 612	renderer->bg[0].target2 = bldcnt.target2Bg0;
 613	renderer->bg[1].target2 = bldcnt.target2Bg1;
 614	renderer->bg[2].target2 = bldcnt.target2Bg2;
 615	renderer->bg[3].target2 = bldcnt.target2Bg3;
 616
 617	renderer->blendEffect = bldcnt.effect;
 618	renderer->target1Obj = bldcnt.target1Obj;
 619	renderer->target1Bd = bldcnt.target1Bd;
 620	renderer->target2Obj = bldcnt.target2Obj;
 621	renderer->target2Bd = bldcnt.target2Bd;
 622
 623	renderer->anyTarget2 = bldcnt.packed & 0x3F00;
 624
 625	if (oldEffect != renderer->blendEffect) {
 626		_updatePalettes(renderer);
 627	}
 628}
 629
 630#define TEST_LAYER_ENABLED(X) \
 631	(renderer->bg[X].enabled && \
 632	(renderer->currentWindow.bg ## X ## Enable || \
 633	(GBARegisterDISPCNTIsObjwinEnable(renderer->dispcnt) && renderer->objwin.bg ## X ## Enable)) && \
 634	renderer->bg[X].priority == priority)
 635
 636static void _drawScanline(struct GBAVideoSoftwareRenderer* renderer, int y) {
 637	int w;
 638	renderer->end = 0;
 639	int spriteLayers = 0;
 640	if (GBARegisterDISPCNTIsObjEnable(renderer->dispcnt)) {
 641		if (renderer->oamDirty) {
 642			_cleanOAM(renderer);
 643		}
 644		int mosaicV = renderer->mosaic.objV + 1;
 645		int mosaicY = y - (y % mosaicV);
 646		for (w = 0; w < renderer->nWindows; ++w) {
 647			renderer->start = renderer->end;
 648			renderer->end = renderer->windows[w].endX;
 649			renderer->currentWindow = renderer->windows[w].control;
 650			if (!renderer->currentWindow.objEnable) {
 651				continue;
 652			}
 653			int i;
 654			int drawn;
 655			for (i = 0; i < renderer->oamMax; ++i) {
 656				int localY = y;
 657				struct GBAVideoSoftwareSprite* sprite = &renderer->sprites[i];
 658				if (GBAObjAttributesAIsMosaic(sprite->obj.a)) {
 659					localY = mosaicY;
 660				}
 661				if ((localY < sprite->y && (sprite->endY - 256 < 0 || localY >= sprite->endY - 256)) || localY >= sprite->endY) {
 662					continue;
 663				}
 664				drawn = _preprocessSprite(renderer, &sprite->obj, localY);
 665				spriteLayers |= drawn << GBAObjAttributesCGetPriority(sprite->obj.c);
 666			}
 667		}
 668	}
 669
 670	int priority;
 671	for (priority = 0; priority < 4; ++priority) {
 672		if (spriteLayers & (1 << priority)) {
 673			_postprocessSprite(renderer, priority);
 674		}
 675		renderer->end = 0;
 676		for (w = 0; w < renderer->nWindows; ++w) {
 677			renderer->start = renderer->end;
 678			renderer->end = renderer->windows[w].endX;
 679			renderer->currentWindow = renderer->windows[w].control;
 680			if (TEST_LAYER_ENABLED(0) && GBARegisterDISPCNTGetMode(renderer->dispcnt) < 2) {
 681				_drawBackgroundMode0(renderer, &renderer->bg[0], y);
 682			}
 683			if (TEST_LAYER_ENABLED(1) && GBARegisterDISPCNTGetMode(renderer->dispcnt) < 2) {
 684				_drawBackgroundMode0(renderer, &renderer->bg[1], y);
 685			}
 686			if (TEST_LAYER_ENABLED(2)) {
 687				switch (GBARegisterDISPCNTGetMode(renderer->dispcnt)) {
 688				case 0:
 689					_drawBackgroundMode0(renderer, &renderer->bg[2], y);
 690					break;
 691				case 1:
 692				case 2:
 693					_drawBackgroundMode2(renderer, &renderer->bg[2], y);
 694					break;
 695				case 3:
 696					_drawBackgroundMode3(renderer, &renderer->bg[2], y);
 697					break;
 698				case 4:
 699					_drawBackgroundMode4(renderer, &renderer->bg[2], y);
 700					break;
 701				case 5:
 702					_drawBackgroundMode5(renderer, &renderer->bg[2], y);
 703					break;
 704				}
 705			}
 706			if (TEST_LAYER_ENABLED(3)) {
 707				switch (GBARegisterDISPCNTGetMode(renderer->dispcnt)) {
 708				case 0:
 709					_drawBackgroundMode0(renderer, &renderer->bg[3], y);
 710					break;
 711				case 2:
 712					_drawBackgroundMode2(renderer, &renderer->bg[3], y);
 713					break;
 714				}
 715			}
 716		}
 717	}
 718	renderer->bg[2].sx += renderer->bg[2].dmx;
 719	renderer->bg[2].sy += renderer->bg[2].dmy;
 720	renderer->bg[3].sx += renderer->bg[3].dmx;
 721	renderer->bg[3].sy += renderer->bg[3].dmy;
 722}
 723
 724// We stash the priority on the top bits so we can do a one-operator comparison
 725// The lower the number, the higher the priority, and sprites take precendence over backgrounds
 726// We want to do special processing if the color pixel is target 1, however
 727
 728static inline void _compositeBlendObjwin(struct GBAVideoSoftwareRenderer* renderer, uint32_t* pixel, uint32_t color, uint32_t current) {
 729	if (color >= current) {
 730		if (current & FLAG_TARGET_1 && color & FLAG_TARGET_2) {
 731			color = _mix(renderer->blda, current, renderer->bldb, color);
 732		} else {
 733			color = current & 0x00FFFFFF;
 734		}
 735	} else {
 736		color = (color & ~FLAG_TARGET_2) | (current & FLAG_OBJWIN);
 737	}
 738	*pixel = color;
 739}
 740
 741static inline void _compositeBlendNoObjwin(struct GBAVideoSoftwareRenderer* renderer, uint32_t* pixel, uint32_t color, uint32_t current) {
 742	// We stash the priority on the top bits so we can do a one-operator comparison
 743	// The lower the number, the higher the priority, and sprites take precendence over backgrounds
 744	// We want to do special processing if the color pixel is target 1, however
 745	if (color >= current) {
 746		if (current & FLAG_TARGET_1 && color & FLAG_TARGET_2) {
 747			color = _mix(renderer->blda, current, renderer->bldb, color);
 748		} else {
 749			color = current & 0x00FFFFFF;
 750		}
 751	} else {
 752		color = color & ~FLAG_TARGET_2;
 753	}
 754	*pixel = color;
 755}
 756
 757static inline void _compositeNoBlendObjwin(struct GBAVideoSoftwareRenderer* renderer, uint32_t* pixel, uint32_t color, uint32_t current) {
 758	UNUSED(renderer);
 759	if (color < current) {
 760		*pixel = color | (current & FLAG_OBJWIN);
 761	}
 762}
 763
 764static inline void _compositeNoBlendNoObjwin(struct GBAVideoSoftwareRenderer* renderer, uint32_t* pixel, uint32_t color, uint32_t current) {
 765	UNUSED(renderer);
 766	if (color < current) {
 767		*pixel = color;
 768	}
 769}
 770
 771#define COMPOSITE_16_OBJWIN(BLEND) \
 772	if (objwinForceEnable || !(current & FLAG_OBJWIN) == objwinOnly) { \
 773		unsigned color = (current & FLAG_OBJWIN) ? objwinPalette[paletteData | pixelData] : palette[pixelData]; \
 774		_composite ## BLEND ## Objwin(renderer, pixel, color | flags, current); \
 775	}
 776
 777#define COMPOSITE_16_NO_OBJWIN(BLEND) \
 778	_composite ## BLEND ## NoObjwin(renderer, pixel, palette[pixelData] | flags, current);
 779
 780#define COMPOSITE_256_OBJWIN(BLEND) \
 781	if (objwinForceEnable || !(current & FLAG_OBJWIN) == objwinOnly) { \
 782		unsigned color = (current & FLAG_OBJWIN) ? objwinPalette[pixelData] : palette[pixelData]; \
 783		_composite ## BLEND ## Objwin(renderer, pixel, color | flags, current); \
 784	}
 785
 786#define COMPOSITE_256_NO_OBJWIN(BLEND) \
 787	COMPOSITE_16_NO_OBJWIN(BLEND)
 788
 789#define BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN) \
 790	pixelData = tileData & 0xF; \
 791	current = *pixel; \
 792	if (pixelData && IS_WRITABLE(current)) { \
 793		COMPOSITE_16_ ## OBJWIN (BLEND); \
 794	} \
 795	tileData >>= 4;
 796
 797#define BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN) \
 798	pixelData = tileData & 0xFF; \
 799	current = *pixel; \
 800	if (pixelData && IS_WRITABLE(current)) { \
 801		COMPOSITE_256_ ## OBJWIN (BLEND); \
 802	} \
 803	tileData >>= 8;
 804
 805#define BACKGROUND_TEXT_SELECT_CHARACTER \
 806	localX = tileX * 8 + inX; \
 807	xBase = localX & 0xF8; \
 808	if (background->size & 1) { \
 809		xBase += (localX & 0x100) << 5; \
 810	} \
 811	screenBase = yBase + (xBase >> 3); \
 812	LOAD_16(mapData, screenBase << 1, vram); \
 813	localY = inY & 0x7; \
 814	if (GBA_TEXT_MAP_VFLIP(mapData)) { \
 815		localY = 7 - localY; \
 816	}
 817
 818#define PREPARE_OBJWIN \
 819	int objwinSlowPath = GBARegisterDISPCNTIsObjwinEnable(renderer->dispcnt); \
 820	int objwinOnly = 0; \
 821	int objwinForceEnable = 0; \
 822	color_t* objwinPalette; \
 823	if (objwinSlowPath) { \
 824		if (background->target1 && renderer->objwin.blendEnable && (renderer->blendEffect == BLEND_BRIGHTEN || renderer->blendEffect == BLEND_DARKEN)) { \
 825			objwinPalette = renderer->variantPalette; \
 826		} else { \
 827			objwinPalette = renderer->normalPalette; \
 828		} \
 829		switch (background->index) { \
 830		case 0: \
 831			objwinForceEnable = renderer->objwin.bg0Enable && renderer->currentWindow.bg0Enable; \
 832			objwinOnly = !renderer->objwin.bg0Enable; \
 833			break; \
 834		case 1: \
 835			objwinForceEnable = renderer->objwin.bg1Enable && renderer->currentWindow.bg1Enable; \
 836			objwinOnly = !renderer->objwin.bg1Enable; \
 837			break; \
 838		case 2: \
 839			objwinForceEnable = renderer->objwin.bg2Enable && renderer->currentWindow.bg2Enable; \
 840			objwinOnly = !renderer->objwin.bg2Enable; \
 841			break; \
 842		case 3: \
 843			objwinForceEnable = renderer->objwin.bg3Enable && renderer->currentWindow.bg3Enable; \
 844			objwinOnly = !renderer->objwin.bg3Enable; \
 845			break; \
 846		} \
 847	}
 848
 849#define DRAW_BACKGROUND_MODE_0_TILE_SUFFIX_16(BLEND, OBJWIN) \
 850	paletteData = GBA_TEXT_MAP_PALETTE(mapData) << 4; \
 851	palette = &mainPalette[paletteData]; \
 852	charBase = (background->charBase + (GBA_TEXT_MAP_TILE(mapData) << 5)) + (localY << 2); \
 853	LOAD_32(tileData, charBase, vram); \
 854	if (!GBA_TEXT_MAP_HFLIP(mapData)) { \
 855		tileData >>= 4 * mod8; \
 856		for (; outX < end; ++outX) { \
 857			uint32_t* pixel = &renderer->row[outX]; \
 858			BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 859		} \
 860	} else { \
 861		for (outX = end - 1; outX >= renderer->start; --outX) { \
 862			uint32_t* pixel = &renderer->row[outX]; \
 863			BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 864		} \
 865	}
 866
 867#define DRAW_BACKGROUND_MODE_0_TILE_PREFIX_16(BLEND, OBJWIN) \
 868	charBase = (background->charBase + (GBA_TEXT_MAP_TILE(mapData) << 5)) + (localY << 2); \
 869	LOAD_32(tileData, charBase, vram); \
 870	paletteData = GBA_TEXT_MAP_PALETTE(mapData) << 4; \
 871	palette = &mainPalette[paletteData]; \
 872	if (!GBA_TEXT_MAP_HFLIP(mapData)) { \
 873		outX = renderer->end - mod8; \
 874		if (outX < renderer->start) { \
 875			tileData >>= 4 * (renderer->start - outX); \
 876			outX = renderer->start; \
 877		} \
 878		for (; outX < renderer->end; ++outX) { \
 879			uint32_t* pixel = &renderer->row[outX]; \
 880			BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 881		} \
 882	} else { \
 883		tileData >>= 4 * (0x8 - mod8); \
 884		int end2 = renderer->end - 8; \
 885		if (end2 < -1) { \
 886			end2 = -1; \
 887		} \
 888		for (outX = renderer->end - 1; outX > end2; --outX) { \
 889			uint32_t* pixel = &renderer->row[outX]; \
 890			BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 891		} \
 892	}
 893
 894#define DRAW_BACKGROUND_MODE_0_MOSAIC_16(BLEND, OBJWIN) \
 895	for (; tileX < tileEnd; ++tileX) { \
 896		BACKGROUND_TEXT_SELECT_CHARACTER; \
 897		charBase = (background->charBase + (GBA_TEXT_MAP_TILE(mapData) << 5)) + (localY << 2); \
 898		tileData = carryData; \
 899		for (x = 0; x < 8; ++x) { \
 900			if (!mosaicWait) { \
 901				paletteData = GBA_TEXT_MAP_PALETTE(mapData) << 4; \
 902				palette = &mainPalette[paletteData]; \
 903				LOAD_32(tileData, charBase, vram); \
 904				if (!GBA_TEXT_MAP_HFLIP(mapData)) { \
 905					tileData >>= x * 4; \
 906				} else { \
 907					tileData >>= (7 - x) * 4; \
 908				} \
 909				tileData &= 0xF; \
 910				tileData |= tileData << 4; \
 911				tileData |= tileData << 8; \
 912				tileData |= tileData << 12; \
 913				tileData |= tileData << 16; \
 914				tileData |= tileData << 20; \
 915				tileData |= tileData << 24; \
 916				tileData |= tileData << 28; \
 917				carryData = tileData; \
 918				mosaicWait = mosaicH; \
 919			} \
 920			--mosaicWait; \
 921			BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 922			++pixel; \
 923		} \
 924	}
 925
 926#define DRAW_BACKGROUND_MODE_0_TILES_16(BLEND, OBJWIN) \
 927	for (; tileX < tileEnd; ++tileX) { \
 928		BACKGROUND_TEXT_SELECT_CHARACTER; \
 929		paletteData = GBA_TEXT_MAP_PALETTE(mapData) << 4; \
 930		palette = &mainPalette[paletteData]; \
 931		charBase = (background->charBase + (GBA_TEXT_MAP_TILE(mapData) << 5)) + (localY << 2); \
 932		LOAD_32(tileData, charBase, vram); \
 933		if (tileData) { \
 934			if (!GBA_TEXT_MAP_HFLIP(mapData)) { \
 935				BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 936				++pixel; \
 937				BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 938				++pixel; \
 939				BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 940				++pixel; \
 941				BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 942				++pixel; \
 943				BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 944				++pixel; \
 945				BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 946				++pixel; \
 947				BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 948				++pixel; \
 949				BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 950				++pixel; \
 951			} else { \
 952				pixel += 7; \
 953				BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 954				--pixel; \
 955				BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 956				--pixel; \
 957				BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 958				--pixel; \
 959				BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 960				--pixel; \
 961				BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 962				--pixel; \
 963				BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 964				--pixel; \
 965				BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 966				--pixel; \
 967				BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 968				pixel += 8; \
 969			} \
 970		} else { \
 971			pixel += 8; \
 972		} \
 973	}
 974
 975#define DRAW_BACKGROUND_MODE_0_TILE_SUFFIX_256(BLEND, OBJWIN) \
 976	charBase = (background->charBase + (GBA_TEXT_MAP_TILE(mapData) << 6)) + (localY << 3); \
 977	int end2 = end - 4; \
 978	if (!GBA_TEXT_MAP_HFLIP(mapData)) { \
 979		int shift = inX & 0x3; \
 980		if (end2 > outX) { \
 981			LOAD_32(tileData, charBase, vram); \
 982			tileData >>= 8 * shift; \
 983			shift = 0; \
 984			for (; outX < end2; ++outX) { \
 985				pixel = &renderer->row[outX]; \
 986				BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
 987			} \
 988		} \
 989		\
 990		LOAD_32(tileData, charBase + 4, vram); \
 991		tileData >>= 8 * shift; \
 992		for (; outX < end; ++outX) { \
 993			pixel = &renderer->row[outX]; \
 994			BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
 995		} \
 996	} else { \
 997		int start = outX; \
 998		outX = end - 1; \
 999		if (end2 > start) { \
1000			LOAD_32(tileData, charBase, vram); \
1001			for (; outX >= end2; --outX) { \
1002				pixel = &renderer->row[outX]; \
1003				BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1004			} \
1005			charBase += 4; \
1006		} \
1007		\
1008		LOAD_32(tileData, charBase, vram); \
1009		for (; outX >= renderer->start; --outX) { \
1010			pixel = &renderer->row[outX]; \
1011			BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1012		} \
1013		outX = end; \
1014	}
1015
1016#define DRAW_BACKGROUND_MODE_0_TILE_PREFIX_256(BLEND, OBJWIN) \
1017	charBase = (background->charBase + (GBA_TEXT_MAP_TILE(mapData) << 6)) + (localY << 3); \
1018	outX = renderer->end - 8 + end; \
1019	int end2 = 4 - end; \
1020	if (!GBA_TEXT_MAP_HFLIP(mapData)) { \
1021		if (end2 > 0) { \
1022			LOAD_32(tileData, charBase, vram); \
1023			for (; outX < renderer->end - end2; ++outX) { \
1024				pixel = &renderer->row[outX]; \
1025				BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1026			} \
1027			charBase += 4; \
1028		} \
1029		\
1030		LOAD_32(tileData, charBase, vram); \
1031		for (; outX < renderer->end; ++outX) { \
1032			pixel = &renderer->row[outX]; \
1033			BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1034		} \
1035	} else { \
1036		int shift = end & 0x3; \
1037		int start = outX; \
1038		outX = renderer->end - 1; \
1039		if (end2 > 0) { \
1040			LOAD_32(tileData, charBase, vram); \
1041			tileData >>= 8 * shift; \
1042			for (; outX >= start + 4; --outX) { \
1043				pixel = &renderer->row[outX]; \
1044				BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1045			} \
1046			shift = 0; \
1047		} \
1048		\
1049		LOAD_32(tileData, charBase + 4, vram); \
1050		tileData >>= 8 * shift; \
1051		for (; outX >= start; --outX) { \
1052			pixel = &renderer->row[outX]; \
1053			BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1054		} \
1055	}
1056
1057#define DRAW_BACKGROUND_MODE_0_TILES_256(BLEND, OBJWIN) \
1058	for (; tileX < tileEnd; ++tileX) { \
1059		BACKGROUND_TEXT_SELECT_CHARACTER; \
1060		charBase = (background->charBase + (GBA_TEXT_MAP_TILE(mapData) << 6)) + (localY << 3); \
1061		if (!GBA_TEXT_MAP_HFLIP(mapData)) { \
1062			LOAD_32(tileData, charBase, vram); \
1063			if (tileData) { \
1064					BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1065					++pixel; \
1066					BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1067					++pixel; \
1068					BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1069					++pixel; \
1070					BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1071					++pixel; \
1072			} else { \
1073				pixel += 4; \
1074			} \
1075			LOAD_32(tileData, charBase + 4, vram); \
1076			if (tileData) { \
1077					BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1078					++pixel; \
1079					BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1080					++pixel; \
1081					BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1082					++pixel; \
1083					BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1084					++pixel; \
1085			} else { \
1086				pixel += 4; \
1087			} \
1088		} else { \
1089			LOAD_32(tileData, charBase + 4, vram); \
1090			if (tileData) { \
1091				pixel += 3; \
1092				BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1093				--pixel; \
1094				BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1095				--pixel; \
1096				BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1097				--pixel; \
1098				BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1099			} \
1100			pixel += 4; \
1101			LOAD_32(tileData, charBase, vram); \
1102			if (tileData) { \
1103				pixel += 3; \
1104				BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1105				--pixel; \
1106				BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1107				--pixel; \
1108				BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1109				--pixel; \
1110				BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1111			} \
1112			pixel += 4; \
1113		} \
1114	}
1115
1116#define DRAW_BACKGROUND_MODE_0_MOSAIC_256(BLEND, OBJWIN) \
1117	for (; tileX < tileEnd; ++tileX) { \
1118		BACKGROUND_TEXT_SELECT_CHARACTER; \
1119		charBase = (background->charBase + (GBA_TEXT_MAP_TILE(mapData) << 6)) + (localY << 3); \
1120		tileData = carryData; \
1121		for (x = 0; x < 8; ++x) { \
1122			if (!mosaicWait) { \
1123				if (!GBA_TEXT_MAP_HFLIP(mapData)) { \
1124					if (x >= 4) { \
1125						LOAD_32(tileData, charBase + 4, vram); \
1126						tileData >>= (x - 4) * 8; \
1127					} else { \
1128						LOAD_32(tileData, charBase, vram); \
1129						tileData >>= x * 8; \
1130					} \
1131				} else { \
1132					if (x >= 4) { \
1133						LOAD_32(tileData, charBase, vram); \
1134						tileData >>= (7 - x) * 8; \
1135					} else { \
1136						LOAD_32(tileData, charBase + 4, vram); \
1137						tileData >>= (3 - x) * 8; \
1138					} \
1139				} \
1140				tileData &= 0xFF; \
1141				carryData = tileData; \
1142				mosaicWait = mosaicH; \
1143			} \
1144			tileData |= tileData << 8; \
1145			--mosaicWait; \
1146			BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1147			++pixel; \
1148		} \
1149	}
1150
1151#define DRAW_BACKGROUND_MODE_0(BPP, BLEND, OBJWIN) \
1152	uint32_t* pixel = &renderer->row[outX]; \
1153	if (background->mosaic && renderer->mosaic.bgH) { \
1154		int mosaicH = renderer->mosaic.bgH + 1; \
1155		int x; \
1156		int mosaicWait = outX % mosaicH; \
1157		int carryData = 0; \
1158		paletteData = 0; /* Quiets compiler warning */ \
1159		DRAW_BACKGROUND_MODE_0_MOSAIC_ ## BPP (BLEND, OBJWIN) \
1160		return; \
1161	} \
1162	\
1163	if (inX & 0x7) { \
1164		int mod8 = inX & 0x7; \
1165		BACKGROUND_TEXT_SELECT_CHARACTER; \
1166		\
1167		int end = outX + 0x8 - mod8; \
1168		if (end > renderer->end) { \
1169			/* TODO: ensure tiles are properly aligned from this*/ \
1170			end = renderer->end; \
1171		} \
1172		if (end == outX) { \
1173			return; \
1174		} \
1175		DRAW_BACKGROUND_MODE_0_TILE_SUFFIX_ ## BPP (BLEND, OBJWIN) \
1176	} \
1177	if (inX & 0x7 || (renderer->end - renderer->start) & 0x7) { \
1178		tileX = tileEnd; \
1179		int pixelData; \
1180		int mod8 = (inX + renderer->end - renderer->start) & 0x7; \
1181		BACKGROUND_TEXT_SELECT_CHARACTER; \
1182		\
1183		int end = 0x8 - mod8; \
1184		UNUSED(end); \
1185		DRAW_BACKGROUND_MODE_0_TILE_PREFIX_ ## BPP (BLEND, OBJWIN) \
1186		\
1187		tileX = (inX & 0x7) != 0; \
1188		outX = renderer->start + tileX * 8 - (inX & 0x7); \
1189	} \
1190	\
1191	pixel = &renderer->row[outX]; \
1192	DRAW_BACKGROUND_MODE_0_TILES_ ## BPP (BLEND, OBJWIN)
1193
1194static void _drawBackgroundMode0(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* background, int y) {
1195	int inX = renderer->start + background->x;
1196	if (background->mosaic) {
1197		int mosaicV = renderer->mosaic.bgV + 1;
1198		y -= y % mosaicV;
1199	}
1200	int inY = y + background->y;
1201	uint16_t mapData;
1202
1203	unsigned yBase = inY & 0xF8;
1204	if (background->size == 2) {
1205		yBase += inY & 0x100;
1206	} else if (background->size == 3) {
1207		yBase += (inY & 0x100) << 1;
1208	}
1209	yBase = (background->screenBase >> 1) + (yBase << 2);
1210
1211	int localX;
1212	int localY;
1213
1214	unsigned xBase;
1215
1216	int flags = (background->priority << OFFSET_PRIORITY) | (background->index << OFFSET_INDEX) | FLAG_IS_BACKGROUND;
1217	flags |= FLAG_TARGET_1 * (background->target1 && renderer->blendEffect == BLEND_ALPHA);
1218	flags |= FLAG_TARGET_2 * background->target2;
1219
1220	uint32_t screenBase;
1221	uint32_t charBase;
1222	int variant = background->target1 && renderer->currentWindow.blendEnable && (renderer->blendEffect == BLEND_BRIGHTEN || renderer->blendEffect == BLEND_DARKEN);
1223	color_t* mainPalette = renderer->normalPalette;
1224	if (variant) {
1225		mainPalette = renderer->variantPalette;
1226	}
1227	color_t* palette = mainPalette;
1228	PREPARE_OBJWIN;
1229
1230	int outX = renderer->start;
1231
1232	uint32_t tileData;
1233	uint32_t current;
1234	int pixelData;
1235	int paletteData;
1236	int tileX = 0;
1237	int tileEnd = (renderer->end - renderer->start + (inX & 0x7)) >> 3;
1238	uint16_t* vram = renderer->d.vram;
1239
1240	if (!objwinSlowPath) {
1241		if (!(flags & FLAG_TARGET_2)) {
1242			if (!background->multipalette) {
1243				DRAW_BACKGROUND_MODE_0(16, NoBlend, NO_OBJWIN);
1244			} else {
1245				DRAW_BACKGROUND_MODE_0(256, NoBlend, NO_OBJWIN);
1246			}
1247		} else {
1248			if (!background->multipalette) {
1249				DRAW_BACKGROUND_MODE_0(16, Blend, NO_OBJWIN);
1250			} else {
1251				DRAW_BACKGROUND_MODE_0(256, Blend, NO_OBJWIN);
1252			}
1253		}
1254	} else {
1255		if (!(flags & FLAG_TARGET_2)) {
1256			if (!background->multipalette) {
1257				DRAW_BACKGROUND_MODE_0(16, NoBlend, OBJWIN);
1258			} else {
1259				DRAW_BACKGROUND_MODE_0(256, NoBlend, OBJWIN);
1260			}
1261		} else {
1262			if (!background->multipalette) {
1263				DRAW_BACKGROUND_MODE_0(16, Blend, OBJWIN);
1264			} else {
1265				DRAW_BACKGROUND_MODE_0(256, Blend, OBJWIN);
1266			}
1267		}
1268	}
1269}
1270
1271#define BACKGROUND_BITMAP_INIT \
1272	UNUSED(unused); \
1273	int32_t x = background->sx + (renderer->start - 1) * background->dx; \
1274	int32_t y = background->sy + (renderer->start - 1) * background->dy; \
1275	int32_t localX; \
1276	int32_t localY; \
1277	\
1278	int flags = (background->priority << OFFSET_PRIORITY) | (background->index << OFFSET_INDEX) | FLAG_IS_BACKGROUND; \
1279	flags |= FLAG_TARGET_1 * (background->target1 && renderer->blendEffect == BLEND_ALPHA); \
1280	flags |= FLAG_TARGET_2 * background->target2; \
1281	int variant = background->target1 && renderer->currentWindow.blendEnable && (renderer->blendEffect == BLEND_BRIGHTEN || renderer->blendEffect == BLEND_DARKEN); \
1282	color_t* palette = renderer->normalPalette; \
1283	if (variant) { \
1284		palette = renderer->variantPalette; \
1285	} \
1286	PREPARE_OBJWIN;
1287
1288#define BACKGROUND_BITMAP_ITERATE(W, H) \
1289	x += background->dx; \
1290	y += background->dy; \
1291	\
1292	if (x < 0 || y < 0 || (x >> 8) >= W || (y >> 8) >= H) { \
1293		continue; \
1294	} else { \
1295		localX = x; \
1296		localY = y; \
1297	}
1298
1299static void _drawBackgroundMode2(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* background, int unused) {
1300	int sizeAdjusted = 0x8000 << background->size;
1301
1302	BACKGROUND_BITMAP_INIT;
1303
1304	uint32_t screenBase = background->screenBase;
1305	uint32_t charBase = background->charBase;
1306	uint8_t mapData;
1307	uint8_t tileData;
1308
1309	int outX;
1310	uint32_t* pixel;
1311	for (outX = renderer->start, pixel = &renderer->row[outX]; outX < renderer->end; ++outX, ++pixel) {
1312		x += background->dx;
1313		y += background->dy;
1314
1315		if (background->overflow) {
1316			localX = x & (sizeAdjusted - 1);
1317			localY = y & (sizeAdjusted - 1);
1318		} else if ((x | y) & ~(sizeAdjusted - 1)) {
1319			continue;
1320		} else {
1321			localX = x;
1322			localY = y;
1323		}
1324		mapData = ((uint8_t*)renderer->d.vram)[screenBase + (localX >> 11) + (((localY >> 7) & 0x7F0) << background->size)];
1325		tileData = ((uint8_t*)renderer->d.vram)[charBase + (mapData << 6) + ((localY & 0x700) >> 5) + ((localX & 0x700) >> 8)];
1326
1327		uint32_t current = *pixel;
1328		if (tileData && IS_WRITABLE(current)) {
1329			if (!objwinSlowPath) {
1330				_compositeBlendNoObjwin(renderer, pixel, palette[tileData] | flags, current);
1331			} else if (objwinForceEnable || !(current & FLAG_OBJWIN) == objwinOnly) {
1332				color_t* currentPalette = (current & FLAG_OBJWIN) ? objwinPalette : palette;
1333				_compositeBlendObjwin(renderer, pixel, currentPalette[tileData] | flags, current);
1334			}
1335		}
1336	}
1337}
1338
1339static void _drawBackgroundMode3(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* background, int unused) {
1340	BACKGROUND_BITMAP_INIT;
1341
1342	uint32_t color;
1343
1344	int outX;
1345	uint32_t* pixel;
1346	for (outX = renderer->start, pixel = &renderer->row[outX]; outX < renderer->end; ++outX, ++pixel) {
1347		BACKGROUND_BITMAP_ITERATE(VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS);
1348
1349		LOAD_16(color, ((localX >> 8) + (localY >> 8) * VIDEO_HORIZONTAL_PIXELS) << 1, renderer->d.vram);
1350#ifndef COLOR_16_BIT
1351		unsigned color32;
1352		color32 = 0;
1353		color32 |= (color << 3) & 0xF8;
1354		color32 |= (color << 6) & 0xF800;
1355		color32 |= (color << 9) & 0xF80000;
1356		color = color32;
1357#endif
1358
1359		uint32_t current = *pixel;
1360		if (!objwinSlowPath || !(current & FLAG_OBJWIN) != objwinOnly) {
1361			if (!variant) {
1362				_compositeBlendObjwin(renderer, pixel, color | flags, current);
1363			} else if (renderer->blendEffect == BLEND_BRIGHTEN) {
1364				_compositeBlendObjwin(renderer, pixel, _brighten(color, renderer->bldy) | flags, current);
1365			} else if (renderer->blendEffect == BLEND_DARKEN) {
1366				_compositeBlendObjwin(renderer, pixel, _darken(color, renderer->bldy) | flags, current);
1367			}
1368		}
1369	}
1370}
1371
1372static void _drawBackgroundMode4(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* background, int unused) {
1373	BACKGROUND_BITMAP_INIT;
1374
1375	uint16_t color;
1376	uint32_t offset = 0;
1377	if (GBARegisterDISPCNTIsFrameSelect(renderer->dispcnt)) {
1378		offset = 0xA000;
1379	}
1380
1381	int outX;
1382	uint32_t* pixel;
1383	for (outX = renderer->start, pixel = &renderer->row[outX]; outX < renderer->end; ++outX, ++pixel) {
1384		BACKGROUND_BITMAP_ITERATE(VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS);
1385
1386		color = ((uint8_t*)renderer->d.vram)[offset + (localX >> 8) + (localY >> 8) * VIDEO_HORIZONTAL_PIXELS];
1387
1388		uint32_t current = *pixel;
1389		if (color && IS_WRITABLE(current)) {
1390			if (!objwinSlowPath) {
1391				_compositeBlendNoObjwin(renderer, pixel, palette[color] | flags, current);
1392			} else if (objwinForceEnable || !(current & FLAG_OBJWIN) == objwinOnly) {
1393				color_t* currentPalette = (current & FLAG_OBJWIN) ? objwinPalette : palette;
1394				_compositeBlendObjwin(renderer, pixel, currentPalette[color] | flags, current);
1395			}
1396		}
1397	}
1398}
1399
1400static void _drawBackgroundMode5(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* background, int unused) {
1401	BACKGROUND_BITMAP_INIT;
1402
1403	uint32_t color;
1404	uint32_t offset = 0;
1405	if (GBARegisterDISPCNTIsFrameSelect(renderer->dispcnt)) {
1406		offset = 0xA000;
1407	}
1408
1409	int outX;
1410	uint32_t* pixel;
1411	for (outX = renderer->start, pixel = &renderer->row[outX]; outX < renderer->end; ++outX, ++pixel) {
1412		BACKGROUND_BITMAP_ITERATE(160, 128);
1413
1414		LOAD_16(color, (offset + (localX >> 8) + (localY >> 8) * 160) << 1, renderer->d.vram);
1415#ifndef COLOR_16_BIT
1416		unsigned color32 = 0;
1417		color32 |= (color << 9) & 0xF80000;
1418		color32 |= (color << 3) & 0xF8;
1419		color32 |= (color << 6) & 0xF800;
1420		color = color32;
1421#endif
1422
1423		uint32_t current = *pixel;
1424		if (!objwinSlowPath || !(current & FLAG_OBJWIN) != objwinOnly) {
1425			if (!variant) {
1426				_compositeBlendObjwin(renderer, pixel, color | flags, current);
1427			} else if (renderer->blendEffect == BLEND_BRIGHTEN) {
1428				_compositeBlendObjwin(renderer, pixel, _brighten(color, renderer->bldy) | flags, current);
1429			} else if (renderer->blendEffect == BLEND_DARKEN) {
1430				_compositeBlendObjwin(renderer, pixel, _darken(color, renderer->bldy) | flags, current);
1431			}
1432		}
1433	}
1434}
1435
1436#define SPRITE_NORMAL_LOOP(DEPTH, TYPE) \
1437	SPRITE_YBASE_ ## DEPTH(inY); \
1438	unsigned tileData; \
1439	for (; outX < condition; ++outX, inX += xOffset) { \
1440		if (!(renderer->row[outX] & FLAG_UNWRITTEN)) { \
1441			continue; \
1442		} \
1443		SPRITE_XBASE_ ## DEPTH(inX); \
1444		SPRITE_DRAW_PIXEL_ ## DEPTH ## _ ## TYPE(inX); \
1445	}
1446
1447#define SPRITE_MOSAIC_LOOP(DEPTH, TYPE) \
1448	SPRITE_YBASE_ ## DEPTH(inY); \
1449	unsigned tileData; \
1450	if (outX % mosaicH) { \
1451		inX += (mosaicH - (outX % mosaicH)) * xOffset; \
1452		outX += mosaicH - (outX % mosaicH); \
1453	} \
1454	for (; outX < condition; ++outX, inX += xOffset) { \
1455		if (!(renderer->row[outX] & FLAG_UNWRITTEN)) { \
1456			continue; \
1457		} \
1458		int localX = inX - xOffset * (outX % mosaicH); \
1459		SPRITE_XBASE_ ## DEPTH(localX); \
1460		SPRITE_DRAW_PIXEL_ ## DEPTH ## _ ## TYPE(localX); \
1461	}
1462
1463#define SPRITE_TRANSFORMED_LOOP(DEPTH, TYPE) \
1464	unsigned tileData; \
1465	for (; outX < x + totalWidth && outX < end; ++outX, ++inX) { \
1466		if (!(renderer->row[outX] & FLAG_UNWRITTEN)) { \
1467			continue; \
1468		} \
1469		xAccum += mat.a; \
1470		yAccum += mat.c; \
1471		int localX = (xAccum >> 8) + (width >> 1); \
1472		int localY = (yAccum >> 8) + (height >> 1); \
1473		\
1474		if (localX < 0 || localX >= width || localY < 0 || localY >= height) { \
1475			continue; \
1476		} \
1477		\
1478		SPRITE_YBASE_ ## DEPTH(localY); \
1479		SPRITE_XBASE_ ## DEPTH(localX); \
1480		SPRITE_DRAW_PIXEL_ ## DEPTH ## _ ## TYPE(localX); \
1481	}
1482
1483#define SPRITE_XBASE_16(localX) unsigned xBase = (localX & ~0x7) * 4 + ((localX >> 1) & 2);
1484#define SPRITE_YBASE_16(localY) unsigned yBase = (localY & ~0x7) * (GBARegisterDISPCNTIsObjCharacterMapping(renderer->dispcnt) ? width >> 1 : 0x80) + (localY & 0x7) * 4;
1485
1486#define SPRITE_DRAW_PIXEL_16_NORMAL(localX) \
1487	LOAD_16(tileData, ((yBase + charBase + xBase) & 0x7FFF), vramBase); \
1488	tileData = (tileData >> ((localX & 3) << 2)) & 0xF; \
1489	if (tileData && (renderer->spriteLayer[outX] & FLAG_ORDER_MASK) > flags) { \
1490		renderer->spriteLayer[outX] = palette[tileData] | flags; \
1491	}
1492
1493#define SPRITE_DRAW_PIXEL_16_OBJWIN(localX) \
1494	LOAD_16(tileData, ((yBase + charBase + xBase) & 0x7FFF), vramBase); \
1495	tileData = (tileData >> ((localX & 3) << 2)) & 0xF; \
1496	if (tileData) { \
1497		renderer->row[outX] |= FLAG_OBJWIN; \
1498	}
1499
1500#define SPRITE_XBASE_256(localX) unsigned xBase = (localX & ~0x7) * 8 + (localX & 6);
1501#define SPRITE_YBASE_256(localY) unsigned yBase = (localY & ~0x7) * (GBARegisterDISPCNTIsObjCharacterMapping(renderer->dispcnt) ? width : 0x80) + (localY & 0x7) * 8;
1502
1503#define SPRITE_DRAW_PIXEL_256_NORMAL(localX) \
1504	LOAD_16(tileData, ((yBase + charBase + xBase) & 0x7FFF), vramBase); \
1505	tileData = (tileData >> ((localX & 1) << 3)) & 0xFF; \
1506	if (tileData && (renderer->spriteLayer[outX] & FLAG_ORDER_MASK) > flags) { \
1507		renderer->spriteLayer[outX] = palette[tileData] | flags; \
1508	}
1509
1510#define SPRITE_DRAW_PIXEL_256_OBJWIN(localX) \
1511	LOAD_16(tileData, ((yBase + charBase + xBase) & 0x7FFF), vramBase); \
1512	tileData = (tileData >> ((localX & 1) << 3)) & 0xFF; \
1513	if (tileData) { \
1514		renderer->row[outX] |= FLAG_OBJWIN; \
1515	}
1516
1517static int _preprocessSprite(struct GBAVideoSoftwareRenderer* renderer, struct GBAObj* sprite, int y) {
1518	int width = _objSizes[GBAObjAttributesAGetShape(sprite->a) * 8 + GBAObjAttributesBGetSize(sprite->b) * 2];
1519	int height = _objSizes[GBAObjAttributesAGetShape(sprite->a) * 8 + GBAObjAttributesBGetSize(sprite->b) * 2 + 1];
1520	int start = renderer->start;
1521	int end = renderer->end;
1522	uint32_t flags = GBAObjAttributesCGetPriority(sprite->c) << OFFSET_PRIORITY;
1523	flags |= FLAG_TARGET_1 * ((renderer->currentWindow.blendEnable && renderer->target1Obj && renderer->blendEffect == BLEND_ALPHA) || GBAObjAttributesAGetMode(sprite->a) == OBJ_MODE_SEMITRANSPARENT);
1524	flags |= FLAG_OBJWIN * (GBAObjAttributesAGetMode(sprite->a) == OBJ_MODE_OBJWIN);
1525	int32_t x = GBAObjAttributesBGetX(sprite->b) << 23;
1526	x >>= 23;
1527	uint16_t* vramBase = &renderer->d.vram[BASE_TILE >> 1];
1528	unsigned charBase = GBAObjAttributesCGetTile(sprite->c) * 0x20;
1529	int variant = renderer->target1Obj && renderer->currentWindow.blendEnable && (renderer->blendEffect == BLEND_BRIGHTEN || renderer->blendEffect == BLEND_DARKEN);
1530	if (GBAObjAttributesAGetMode(sprite->a) == OBJ_MODE_SEMITRANSPARENT && renderer->target2Bd) {
1531		// Hack: if a sprite is blended, then the variant palette is not used, but we don't know if it's blended in advance
1532		variant = 0;
1533	}
1534	color_t* palette = &renderer->normalPalette[0x100];
1535	if (variant) {
1536		palette = &renderer->variantPalette[0x100];
1537	}
1538
1539	int inY = y - (int) GBAObjAttributesAGetY(sprite->a);
1540
1541	if (GBAObjAttributesAIsTransformed(sprite->a)) {
1542		int totalWidth = width << GBAObjAttributesAGetDoubleSize(sprite->a);
1543		int totalHeight = height << GBAObjAttributesAGetDoubleSize(sprite->a);
1544		struct GBAOAMMatrix mat;
1545		LOAD_16(mat.a, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].a);
1546		LOAD_16(mat.b, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].b);
1547		LOAD_16(mat.c, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].c);
1548		LOAD_16(mat.d, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].d);
1549
1550		if (inY < 0) {
1551			inY += 256;
1552		}
1553		int outX = x >= start ? x : start;
1554		int inX = outX - x;
1555		int xAccum = mat.a * (inX - 1 - (totalWidth >> 1)) + mat.b * (inY - (totalHeight >> 1));
1556		int yAccum = mat.c * (inX - 1 - (totalWidth >> 1)) + mat.d * (inY - (totalHeight >> 1));
1557
1558		if (!GBAObjAttributesAIs256Color(sprite->a)) {
1559			palette = &palette[GBAObjAttributesCGetPalette(sprite->c) << 4];
1560			if (flags & FLAG_OBJWIN) {
1561				SPRITE_TRANSFORMED_LOOP(16, OBJWIN);
1562			} else {
1563				SPRITE_TRANSFORMED_LOOP(16, NORMAL);
1564			}
1565		} else {
1566			if (flags & FLAG_OBJWIN) {
1567				SPRITE_TRANSFORMED_LOOP(256, OBJWIN);
1568			} else {
1569				SPRITE_TRANSFORMED_LOOP(256, NORMAL);
1570			}
1571		}
1572	} else {
1573		int outX = x >= start ? x : start;
1574		int condition = x + width;
1575		int mosaicH = 1;
1576		if (GBAObjAttributesAIsMosaic(sprite->a)) {
1577			mosaicH = renderer->mosaic.objH + 1;
1578			if (condition % mosaicH) {
1579				condition += mosaicH - (condition % mosaicH);
1580			}
1581		}
1582		if ((int) GBAObjAttributesAGetY(sprite->a) + height - 256 >= 0) {
1583			inY += 256;
1584		}
1585		if (GBAObjAttributesBIsVFlip(sprite->b)) {
1586			inY = height - inY - 1;
1587		}
1588		if (end < condition) {
1589			condition = end;
1590		}
1591		int inX = outX - x;
1592		int xOffset = 1;
1593		if (GBAObjAttributesBIsHFlip(sprite->b)) {
1594			inX = width - inX - 1;
1595			xOffset = -1;
1596		}
1597		if (!GBAObjAttributesAIs256Color(sprite->a)) {
1598			palette = &palette[GBAObjAttributesCGetPalette(sprite->c) << 4];
1599			if (flags & FLAG_OBJWIN) {
1600				SPRITE_NORMAL_LOOP(16, OBJWIN);
1601			} else if (GBAObjAttributesAIsMosaic(sprite->a)) {
1602				SPRITE_MOSAIC_LOOP(16, NORMAL);
1603			} else {
1604				SPRITE_NORMAL_LOOP(16, NORMAL);
1605			}
1606		} else {
1607			if (flags & FLAG_OBJWIN) {
1608				SPRITE_NORMAL_LOOP(256, OBJWIN);
1609			} else if (GBAObjAttributesAIsMosaic(sprite->a)) {
1610				SPRITE_MOSAIC_LOOP(256, NORMAL);
1611			} else {
1612				SPRITE_NORMAL_LOOP(256, NORMAL);
1613			}
1614		}
1615	}
1616	return 1;
1617}
1618
1619static void _postprocessSprite(struct GBAVideoSoftwareRenderer* renderer, unsigned priority) {
1620	int x;
1621	uint32_t* pixel = renderer->row;
1622	uint32_t flags = FLAG_TARGET_2 * renderer->target2Obj;
1623
1624	int objwinSlowPath = GBARegisterDISPCNTIsObjwinEnable(renderer->dispcnt);
1625	int objwinDisable = 0;
1626	if (objwinSlowPath) {
1627		objwinDisable = !renderer->objwin.objEnable;
1628	}
1629	if (objwinSlowPath && objwinDisable) {
1630		for (x = 0; x < VIDEO_HORIZONTAL_PIXELS; ++x, ++pixel) {
1631			uint32_t color = renderer->spriteLayer[x] & ~FLAG_OBJWIN;
1632			uint32_t current = *pixel;
1633			if ((color & FLAG_UNWRITTEN) != FLAG_UNWRITTEN && !(current & FLAG_OBJWIN) && (color & FLAG_PRIORITY) >> OFFSET_PRIORITY == priority) {
1634				_compositeBlendObjwin(renderer, pixel, color | flags, current);
1635			}
1636		}
1637	} else {
1638		for (x = 0; x < VIDEO_HORIZONTAL_PIXELS; ++x, ++pixel) {
1639			uint32_t color = renderer->spriteLayer[x] & ~FLAG_OBJWIN;
1640			uint32_t current = *pixel;
1641			if ((color & FLAG_UNWRITTEN) != FLAG_UNWRITTEN && (color & FLAG_PRIORITY) >> OFFSET_PRIORITY == priority) {
1642				_compositeBlendNoObjwin(renderer, pixel, color | flags, current);
1643			}
1644		}
1645	}
1646}
1647
1648static void _updatePalettes(struct GBAVideoSoftwareRenderer* renderer) {
1649	int i;
1650	if (renderer->blendEffect == BLEND_BRIGHTEN) {
1651		for (i = 0; i < 512; ++i) {
1652			renderer->variantPalette[i] = _brighten(renderer->normalPalette[i], renderer->bldy);
1653		}
1654	} else if (renderer->blendEffect == BLEND_DARKEN) {
1655		for (i = 0; i < 512; ++i) {
1656			renderer->variantPalette[i] = _darken(renderer->normalPalette[i], renderer->bldy);
1657		}
1658	} else {
1659		for (i = 0; i < 512; ++i) {
1660			renderer->variantPalette[i] = renderer->normalPalette[i];
1661		}
1662	}
1663}
1664
1665static inline unsigned _brighten(unsigned color, int y) {
1666	unsigned c = 0;
1667	unsigned a;
1668#ifdef COLOR_16_BIT
1669	a = color & 0x1F;
1670	c |= (a + ((0x1F - a) * y) / 16) & 0x1F;
1671
1672#ifdef COLOR_5_6_5
1673	a = color & 0x7C0;
1674	c |= (a + ((0x7C0 - a) * y) / 16) & 0x7C0;
1675
1676	a = color & 0xF800;
1677	c |= (a + ((0xF800 - a) * y) / 16) & 0xF800;
1678#else
1679	a = color & 0x3E0;
1680	c |= (a + ((0x3E0 - a) * y) / 16) & 0x3E0;
1681
1682	a = color & 0x7C00;
1683	c |= (a + ((0x7C00 - a) * y) / 16) & 0x7C00;
1684#endif
1685#else
1686	a = color & 0xF8;
1687	c |= (a + ((0xF8 - a) * y) / 16) & 0xF8;
1688
1689	a = color & 0xF800;
1690	c |= (a + ((0xF800 - a) * y) / 16) & 0xF800;
1691
1692	a = color & 0xF80000;
1693	c |= (a + ((0xF80000 - a) * y) / 16) & 0xF80000;
1694#endif
1695	return c;
1696}
1697
1698static inline unsigned _darken(unsigned color, int y) {
1699	unsigned c = 0;
1700	unsigned a;
1701#ifdef COLOR_16_BIT
1702	a = color & 0x1F;
1703	c |= (a - (a * y) / 16) & 0x1F;
1704
1705#ifdef COLOR_5_6_5
1706	a = color & 0x7C0;
1707	c |= (a - (a * y) / 16) & 0x7C0;
1708
1709	a = color & 0xF800;
1710	c |= (a - (a * y) / 16) & 0xF800;
1711#else
1712	a = color & 0x3E0;
1713	c |= (a - (a * y) / 16) & 0x3E0;
1714
1715	a = color & 0x7C00;
1716	c |= (a - (a * y) / 16) & 0x7C00;
1717#endif
1718#else
1719	a = color & 0xF8;
1720	c |= (a - (a * y) / 16) & 0xF8;
1721
1722	a = color & 0xF800;
1723	c |= (a - (a * y) / 16) & 0xF800;
1724
1725	a = color & 0xF80000;
1726	c |= (a - (a * y) / 16) & 0xF80000;
1727#endif
1728	return c;
1729}
1730
1731static unsigned _mix(int weightA, unsigned colorA, int weightB, unsigned colorB) {
1732	unsigned c = 0;
1733	unsigned a, b;
1734#ifdef COLOR_16_BIT
1735#ifdef COLOR_5_6_5
1736	a = colorA & 0xF81F;
1737	b = colorB & 0xF81F;
1738	a |= (colorA & 0x7C0) << 16;
1739	b |= (colorB & 0x7C0) << 16;
1740	c = ((a * weightA + b * weightB) / 16);
1741	if (c & 0x08000000) {
1742		c = (c & ~0x0FC00000) | 0x07C00000;
1743	}
1744	if (c & 0x0020) {
1745		c = (c & ~0x003F) | 0x001F;
1746	}
1747	if (c & 0x10000) {
1748		c = (c & ~0x1F800) | 0xF800;
1749	}
1750	c = (c & 0xF81F) | ((c >> 16) & 0x07C0);
1751#else
1752	a = colorA & 0x7C1F;
1753	b = colorB & 0x7C1F;
1754	a |= (colorA & 0x3E0) << 16;
1755	b |= (colorB & 0x3E0) << 16;
1756	c = ((a * weightA + b * weightB) / 16);
1757	if (c & 0x04000000) {
1758		c = (c & ~0x07E00000) | 0x03E00000;
1759	}
1760	if (c & 0x0020) {
1761		c = (c & ~0x003F) | 0x001F;
1762	}
1763	if (c & 0x10000) {
1764		c = (c & ~0x1F800) | 0xF800;
1765	}
1766	c = (c & 0x7C1F) | ((c >> 16) & 0x03E0);
1767#endif
1768#else
1769	a = colorA & 0xF8;
1770	b = colorB & 0xF8;
1771	c |= ((a * weightA + b * weightB) / 16) & 0x1F8;
1772	if (c & 0x00000100) {
1773		c = 0x000000F8;
1774	}
1775
1776	a = colorA & 0xF800;
1777	b = colorB & 0xF800;
1778	c |= ((a * weightA + b * weightB) / 16) & 0x1F800;
1779	if (c & 0x00010000) {
1780		c = (c & 0x000000F8) | 0x0000F800;
1781	}
1782
1783	a = colorA & 0xF80000;
1784	b = colorB & 0xF80000;
1785	c |= ((a * weightA + b * weightB) / 16) & 0x1F80000;
1786	if (c & 0x01000000) {
1787		c = (c & 0x0000F8F8) | 0x00F80000;
1788	}
1789#endif
1790	return c;
1791}