all repos — mgba @ c6954bcb4aee26ba951f4a2b5dfcfa1f30bdd2c8

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		LOAD_16(obj.d, 0, &renderer->d.oam->obj[i].d);
 401		if (GBAObjAttributesAIsTransformed(obj.a) || !GBAObjAttributesAIsDisable(obj.a)) {
 402			int height = _objSizes[GBAObjAttributesAGetShape(obj.a) * 8 + GBAObjAttributesBGetSize(obj.b) * 2 + 1];
 403			if (GBAObjAttributesAIsTransformed(obj.a)) {
 404				height <<= GBAObjAttributesAGetDoubleSize(obj.a);
 405			}
 406			if (GBAObjAttributesAGetY(obj.a) < VIDEO_VERTICAL_PIXELS || GBAObjAttributesAGetY(obj.a) + height >= VIDEO_VERTICAL_TOTAL_PIXELS) {
 407				renderer->sprites[oamMax].y = GBAObjAttributesAGetY(obj.a);
 408				renderer->sprites[oamMax].endY = GBAObjAttributesAGetY(obj.a) + height;
 409				renderer->sprites[oamMax].obj = obj;
 410				++oamMax;
 411			}
 412		}
 413	}
 414	renderer->oamMax = oamMax;
 415	renderer->oamDirty = 0;
 416}
 417
 418
 419static void GBAVideoSoftwareRendererDrawScanline(struct GBAVideoRenderer* renderer, int y) {
 420	struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
 421
 422	color_t* row = &softwareRenderer->outputBuffer[softwareRenderer->outputBufferStride * y];
 423	if (GBARegisterDISPCNTIsForcedBlank(softwareRenderer->dispcnt)) {
 424		int x;
 425		for (x = 0; x < VIDEO_HORIZONTAL_PIXELS; ++x) {
 426			row[x] = GBA_COLOR_WHITE;
 427		}
 428		return;
 429	}
 430
 431	int x;
 432	for (x = 0; x < VIDEO_HORIZONTAL_PIXELS; x += 4) {
 433		softwareRenderer->spriteLayer[x] = FLAG_UNWRITTEN;
 434		softwareRenderer->spriteLayer[x + 1] = FLAG_UNWRITTEN;
 435		softwareRenderer->spriteLayer[x + 2] = FLAG_UNWRITTEN;
 436		softwareRenderer->spriteLayer[x + 3] = FLAG_UNWRITTEN;
 437	}
 438
 439	softwareRenderer->windows[0].endX = VIDEO_HORIZONTAL_PIXELS;
 440	softwareRenderer->nWindows = 1;
 441	if (GBARegisterDISPCNTIsWin0Enable(softwareRenderer->dispcnt) || GBARegisterDISPCNTIsWin1Enable(softwareRenderer->dispcnt) || GBARegisterDISPCNTIsObjwinEnable(softwareRenderer->dispcnt)) {
 442		softwareRenderer->windows[0].control = softwareRenderer->winout;
 443		if (GBARegisterDISPCNTIsWin1Enable(softwareRenderer->dispcnt) && y < softwareRenderer->winN[1].v.end && y >= softwareRenderer->winN[1].v.start) {
 444			_breakWindow(softwareRenderer, &softwareRenderer->winN[1]);
 445		}
 446		if (GBARegisterDISPCNTIsWin0Enable(softwareRenderer->dispcnt) && y < softwareRenderer->winN[0].v.end && y >= softwareRenderer->winN[0].v.start) {
 447			_breakWindow(softwareRenderer, &softwareRenderer->winN[0]);
 448		}
 449	} else {
 450		softwareRenderer->windows[0].control.packed = 0xFF;
 451	}
 452
 453	int w;
 454	x = 0;
 455	for (w = 0; w < softwareRenderer->nWindows; ++w) {
 456		// TOOD: handle objwin on backdrop
 457		uint32_t backdrop = FLAG_UNWRITTEN | FLAG_PRIORITY | FLAG_IS_BACKGROUND;
 458		if (!softwareRenderer->target1Bd || softwareRenderer->blendEffect == BLEND_NONE || softwareRenderer->blendEffect == BLEND_ALPHA || !softwareRenderer->windows[w].control.blendEnable) {
 459			backdrop |= softwareRenderer->normalPalette[0];
 460		} else {
 461			backdrop |= softwareRenderer->variantPalette[0];
 462		}
 463		int end = softwareRenderer->windows[w].endX;
 464		for (; x < end; ++x) {
 465			softwareRenderer->row[x] = backdrop;
 466		}
 467	}
 468
 469	_drawScanline(softwareRenderer, y);
 470
 471	if (softwareRenderer->target2Bd) {
 472		x = 0;
 473		for (w = 0; w < softwareRenderer->nWindows; ++w) {
 474		uint32_t backdrop = FLAG_UNWRITTEN;
 475			if (!softwareRenderer->target1Bd || softwareRenderer->blendEffect == BLEND_NONE || softwareRenderer->blendEffect == BLEND_ALPHA || !softwareRenderer->windows[w].control.blendEnable) {
 476				backdrop |= softwareRenderer->normalPalette[0];
 477			} else {
 478				backdrop |= softwareRenderer->variantPalette[0];
 479			}
 480			int end = softwareRenderer->windows[w].endX;
 481			for (; x < end; ++x) {
 482				uint32_t color = softwareRenderer->row[x];
 483				if (color & FLAG_TARGET_1) {
 484					softwareRenderer->row[x] = _mix(softwareRenderer->bldb, backdrop, softwareRenderer->blda, color);
 485				}
 486			}
 487		}
 488	}
 489
 490#ifdef COLOR_16_BIT
 491#ifdef __arm__
 492	_to16Bit(row, softwareRenderer->row, VIDEO_HORIZONTAL_PIXELS);
 493#else
 494	for (x = 0; x < VIDEO_HORIZONTAL_PIXELS; ++x) {
 495		row[x] = softwareRenderer->row[x];
 496	}
 497#endif
 498#else
 499	memcpy(row, softwareRenderer->row, VIDEO_HORIZONTAL_PIXELS * sizeof(*row));
 500#endif
 501}
 502
 503static void GBAVideoSoftwareRendererFinishFrame(struct GBAVideoRenderer* renderer) {
 504	struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
 505
 506	softwareRenderer->bg[2].sx = softwareRenderer->bg[2].refx;
 507	softwareRenderer->bg[2].sy = softwareRenderer->bg[2].refy;
 508	softwareRenderer->bg[3].sx = softwareRenderer->bg[3].refx;
 509	softwareRenderer->bg[3].sy = softwareRenderer->bg[3].refy;
 510}
 511
 512static void GBAVideoSoftwareRendererGetPixels(struct GBAVideoRenderer* renderer, unsigned* stride, void** pixels) {
 513	struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
 514
 515	*stride = softwareRenderer->outputBufferStride;
 516	*pixels = softwareRenderer->outputBuffer;
 517}
 518
 519static void GBAVideoSoftwareRendererPutPixels(struct GBAVideoRenderer* renderer, unsigned stride, void* pixels) {
 520	struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
 521
 522	uint32_t* colorPixels = pixels;
 523	unsigned i;
 524	for (i = 0; i < VIDEO_VERTICAL_PIXELS; ++i) {
 525		memmove(&softwareRenderer->outputBuffer[softwareRenderer->outputBufferStride * i], &colorPixels[stride * i], VIDEO_HORIZONTAL_PIXELS * BYTES_PER_PIXEL);
 526	}
 527}
 528
 529static void GBAVideoSoftwareRendererUpdateDISPCNT(struct GBAVideoSoftwareRenderer* renderer) {
 530	renderer->bg[0].enabled = GBARegisterDISPCNTGetBg0Enable(renderer->dispcnt);
 531	renderer->bg[1].enabled = GBARegisterDISPCNTGetBg1Enable(renderer->dispcnt);
 532	renderer->bg[2].enabled = GBARegisterDISPCNTGetBg2Enable(renderer->dispcnt);
 533	renderer->bg[3].enabled = GBARegisterDISPCNTGetBg3Enable(renderer->dispcnt);
 534}
 535
 536static void GBAVideoSoftwareRendererWriteBGCNT(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* bg, uint16_t value) {
 537	UNUSED(renderer);
 538	bg->priority = GBARegisterBGCNTGetPriority(value);
 539	bg->charBase = GBARegisterBGCNTGetCharBase(value) << 14;
 540	bg->mosaic = GBARegisterBGCNTGetMosaic(value);
 541	bg->multipalette = GBARegisterBGCNTGet256Color(value);
 542	bg->screenBase = GBARegisterBGCNTGetScreenBase(value) << 11;
 543	bg->overflow = GBARegisterBGCNTGetOverflow(value);
 544	bg->size = GBARegisterBGCNTGetSize(value);
 545}
 546
 547static void GBAVideoSoftwareRendererWriteBGPA(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
 548	bg->dx = value;
 549}
 550
 551static void GBAVideoSoftwareRendererWriteBGPB(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
 552	bg->dmx = value;
 553}
 554
 555static void GBAVideoSoftwareRendererWriteBGPC(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
 556	bg->dy = value;
 557}
 558
 559static void GBAVideoSoftwareRendererWriteBGPD(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
 560	bg->dmy = value;
 561}
 562
 563static void GBAVideoSoftwareRendererWriteBGX_LO(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
 564	bg->refx = (bg->refx & 0xFFFF0000) | value;
 565	bg->sx = bg->refx;
 566}
 567
 568static void GBAVideoSoftwareRendererWriteBGX_HI(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
 569	bg->refx = (bg->refx & 0x0000FFFF) | (value << 16);
 570	bg->refx <<= 4;
 571	bg->refx >>= 4;
 572	bg->sx = bg->refx;
 573}
 574
 575static void GBAVideoSoftwareRendererWriteBGY_LO(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
 576	bg->refy = (bg->refy & 0xFFFF0000) | value;
 577	bg->sy = bg->refy;
 578}
 579
 580static void GBAVideoSoftwareRendererWriteBGY_HI(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
 581	bg->refy = (bg->refy & 0x0000FFFF) | (value << 16);
 582	bg->refy <<= 4;
 583	bg->refy >>= 4;
 584	bg->sy = bg->refy;
 585}
 586
 587static void GBAVideoSoftwareRendererWriteBLDCNT(struct GBAVideoSoftwareRenderer* renderer, uint16_t value) {
 588	union {
 589		struct {
 590			unsigned target1Bg0 : 1;
 591			unsigned target1Bg1 : 1;
 592			unsigned target1Bg2 : 1;
 593			unsigned target1Bg3 : 1;
 594			unsigned target1Obj : 1;
 595			unsigned target1Bd : 1;
 596			enum BlendEffect effect : 2;
 597			unsigned target2Bg0 : 1;
 598			unsigned target2Bg1 : 1;
 599			unsigned target2Bg2 : 1;
 600			unsigned target2Bg3 : 1;
 601			unsigned target2Obj : 1;
 602			unsigned target2Bd : 1;
 603		};
 604		uint16_t packed;
 605	} bldcnt = { .packed = value };
 606
 607	enum BlendEffect oldEffect = renderer->blendEffect;
 608
 609	renderer->bg[0].target1 = bldcnt.target1Bg0;
 610	renderer->bg[1].target1 = bldcnt.target1Bg1;
 611	renderer->bg[2].target1 = bldcnt.target1Bg2;
 612	renderer->bg[3].target1 = bldcnt.target1Bg3;
 613	renderer->bg[0].target2 = bldcnt.target2Bg0;
 614	renderer->bg[1].target2 = bldcnt.target2Bg1;
 615	renderer->bg[2].target2 = bldcnt.target2Bg2;
 616	renderer->bg[3].target2 = bldcnt.target2Bg3;
 617
 618	renderer->blendEffect = bldcnt.effect;
 619	renderer->target1Obj = bldcnt.target1Obj;
 620	renderer->target1Bd = bldcnt.target1Bd;
 621	renderer->target2Obj = bldcnt.target2Obj;
 622	renderer->target2Bd = bldcnt.target2Bd;
 623
 624	renderer->anyTarget2 = bldcnt.packed & 0x3F00;
 625
 626	if (oldEffect != renderer->blendEffect) {
 627		_updatePalettes(renderer);
 628	}
 629}
 630
 631#define TEST_LAYER_ENABLED(X) \
 632	(renderer->bg[X].enabled && \
 633	(renderer->currentWindow.bg ## X ## Enable || \
 634	(GBARegisterDISPCNTIsObjwinEnable(renderer->dispcnt) && renderer->objwin.bg ## X ## Enable)) && \
 635	renderer->bg[X].priority == priority)
 636
 637static void _drawScanline(struct GBAVideoSoftwareRenderer* renderer, int y) {
 638	int w;
 639	renderer->end = 0;
 640	int spriteLayers = 0;
 641	if (GBARegisterDISPCNTIsObjEnable(renderer->dispcnt)) {
 642		if (renderer->oamDirty) {
 643			_cleanOAM(renderer);
 644		}
 645		int mosaicV = renderer->mosaic.objV + 1;
 646		int mosaicY = y - (y % mosaicV);
 647		for (w = 0; w < renderer->nWindows; ++w) {
 648			renderer->start = renderer->end;
 649			renderer->end = renderer->windows[w].endX;
 650			renderer->currentWindow = renderer->windows[w].control;
 651			if (!renderer->currentWindow.objEnable) {
 652				continue;
 653			}
 654			int i;
 655			int drawn;
 656			for (i = 0; i < renderer->oamMax; ++i) {
 657				int localY = y;
 658				struct GBAVideoSoftwareSprite* sprite = &renderer->sprites[i];
 659				if (GBAObjAttributesAIsMosaic(sprite->obj.a)) {
 660					localY = mosaicY;
 661				}
 662				if ((localY < sprite->y && (sprite->endY - 256 < 0 || localY >= sprite->endY - 256)) || localY >= sprite->endY) {
 663					continue;
 664				}
 665				drawn = _preprocessSprite(renderer, &sprite->obj, localY);
 666				spriteLayers |= drawn << GBAObjAttributesCGetPriority(sprite->obj.c);
 667			}
 668		}
 669	}
 670
 671	int priority;
 672	for (priority = 0; priority < 4; ++priority) {
 673		if (spriteLayers & (1 << priority)) {
 674			_postprocessSprite(renderer, priority);
 675		}
 676		renderer->end = 0;
 677		for (w = 0; w < renderer->nWindows; ++w) {
 678			renderer->start = renderer->end;
 679			renderer->end = renderer->windows[w].endX;
 680			renderer->currentWindow = renderer->windows[w].control;
 681			if (TEST_LAYER_ENABLED(0) && GBARegisterDISPCNTGetMode(renderer->dispcnt) < 2) {
 682				_drawBackgroundMode0(renderer, &renderer->bg[0], y);
 683			}
 684			if (TEST_LAYER_ENABLED(1) && GBARegisterDISPCNTGetMode(renderer->dispcnt) < 2) {
 685				_drawBackgroundMode0(renderer, &renderer->bg[1], y);
 686			}
 687			if (TEST_LAYER_ENABLED(2)) {
 688				switch (GBARegisterDISPCNTGetMode(renderer->dispcnt)) {
 689				case 0:
 690					_drawBackgroundMode0(renderer, &renderer->bg[2], y);
 691					break;
 692				case 1:
 693				case 2:
 694					_drawBackgroundMode2(renderer, &renderer->bg[2], y);
 695					break;
 696				case 3:
 697					_drawBackgroundMode3(renderer, &renderer->bg[2], y);
 698					break;
 699				case 4:
 700					_drawBackgroundMode4(renderer, &renderer->bg[2], y);
 701					break;
 702				case 5:
 703					_drawBackgroundMode5(renderer, &renderer->bg[2], y);
 704					break;
 705				}
 706			}
 707			if (TEST_LAYER_ENABLED(3)) {
 708				switch (GBARegisterDISPCNTGetMode(renderer->dispcnt)) {
 709				case 0:
 710					_drawBackgroundMode0(renderer, &renderer->bg[3], y);
 711					break;
 712				case 2:
 713					_drawBackgroundMode2(renderer, &renderer->bg[3], y);
 714					break;
 715				}
 716			}
 717		}
 718	}
 719	renderer->bg[2].sx += renderer->bg[2].dmx;
 720	renderer->bg[2].sy += renderer->bg[2].dmy;
 721	renderer->bg[3].sx += renderer->bg[3].dmx;
 722	renderer->bg[3].sy += renderer->bg[3].dmy;
 723}
 724
 725// We stash the priority on the top bits so we can do a one-operator comparison
 726// The lower the number, the higher the priority, and sprites take precendence over backgrounds
 727// We want to do special processing if the color pixel is target 1, however
 728
 729static inline void _compositeBlendObjwin(struct GBAVideoSoftwareRenderer* renderer, uint32_t* pixel, uint32_t color, uint32_t current) {
 730	if (color >= current) {
 731		if (current & FLAG_TARGET_1 && color & FLAG_TARGET_2) {
 732			color = _mix(renderer->blda, current, renderer->bldb, color);
 733		} else {
 734			color = current & 0x00FFFFFF;
 735		}
 736	} else {
 737		color = (color & ~FLAG_TARGET_2) | (current & FLAG_OBJWIN);
 738	}
 739	*pixel = color;
 740}
 741
 742static inline void _compositeBlendNoObjwin(struct GBAVideoSoftwareRenderer* renderer, uint32_t* pixel, uint32_t color, uint32_t current) {
 743	// We stash the priority on the top bits so we can do a one-operator comparison
 744	// The lower the number, the higher the priority, and sprites take precendence over backgrounds
 745	// We want to do special processing if the color pixel is target 1, however
 746	if (color >= current) {
 747		if (current & FLAG_TARGET_1 && color & FLAG_TARGET_2) {
 748			color = _mix(renderer->blda, current, renderer->bldb, color);
 749		} else {
 750			color = current & 0x00FFFFFF;
 751		}
 752	} else {
 753		color = color & ~FLAG_TARGET_2;
 754	}
 755	*pixel = color;
 756}
 757
 758static inline void _compositeNoBlendObjwin(struct GBAVideoSoftwareRenderer* renderer, uint32_t* pixel, uint32_t color, uint32_t current) {
 759	UNUSED(renderer);
 760	if (color < current) {
 761		*pixel = color | (current & FLAG_OBJWIN);
 762	}
 763}
 764
 765static inline void _compositeNoBlendNoObjwin(struct GBAVideoSoftwareRenderer* renderer, uint32_t* pixel, uint32_t color, uint32_t current) {
 766	UNUSED(renderer);
 767	if (color < current) {
 768		*pixel = color;
 769	}
 770}
 771
 772#define COMPOSITE_16_OBJWIN(BLEND) \
 773	if (objwinForceEnable || !(current & FLAG_OBJWIN) == objwinOnly) { \
 774		unsigned color = (current & FLAG_OBJWIN) ? objwinPalette[paletteData | pixelData] : palette[pixelData]; \
 775		_composite ## BLEND ## Objwin(renderer, pixel, color | flags, current); \
 776	}
 777
 778#define COMPOSITE_16_NO_OBJWIN(BLEND) \
 779	_composite ## BLEND ## NoObjwin(renderer, pixel, palette[pixelData] | flags, current);
 780
 781#define COMPOSITE_256_OBJWIN(BLEND) \
 782	if (objwinForceEnable || !(current & FLAG_OBJWIN) == objwinOnly) { \
 783		unsigned color = (current & FLAG_OBJWIN) ? objwinPalette[pixelData] : palette[pixelData]; \
 784		_composite ## BLEND ## Objwin(renderer, pixel, color | flags, current); \
 785	}
 786
 787#define COMPOSITE_256_NO_OBJWIN(BLEND) \
 788	COMPOSITE_16_NO_OBJWIN(BLEND)
 789
 790#define BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN) \
 791	pixelData = tileData & 0xF; \
 792	current = *pixel; \
 793	if (pixelData && IS_WRITABLE(current)) { \
 794		COMPOSITE_16_ ## OBJWIN (BLEND); \
 795	} \
 796	tileData >>= 4;
 797
 798#define BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN) \
 799	pixelData = tileData & 0xFF; \
 800	current = *pixel; \
 801	if (pixelData && IS_WRITABLE(current)) { \
 802		COMPOSITE_256_ ## OBJWIN (BLEND); \
 803	} \
 804	tileData >>= 8;
 805
 806#define BACKGROUND_TEXT_SELECT_CHARACTER \
 807	localX = tileX * 8 + inX; \
 808	xBase = localX & 0xF8; \
 809	if (background->size & 1) { \
 810		xBase += (localX & 0x100) << 5; \
 811	} \
 812	screenBase = yBase + (xBase >> 3); \
 813	LOAD_16(mapData, screenBase << 1, vram); \
 814	localY = inY & 0x7; \
 815	if (GBA_TEXT_MAP_VFLIP(mapData)) { \
 816		localY = 7 - localY; \
 817	}
 818
 819#define PREPARE_OBJWIN \
 820	int objwinSlowPath = GBARegisterDISPCNTIsObjwinEnable(renderer->dispcnt); \
 821	int objwinOnly = 0; \
 822	int objwinForceEnable = 0; \
 823	color_t* objwinPalette; \
 824	if (objwinSlowPath) { \
 825		if (background->target1 && renderer->objwin.blendEnable && (renderer->blendEffect == BLEND_BRIGHTEN || renderer->blendEffect == BLEND_DARKEN)) { \
 826			objwinPalette = renderer->variantPalette; \
 827		} else { \
 828			objwinPalette = renderer->normalPalette; \
 829		} \
 830		switch (background->index) { \
 831		case 0: \
 832			objwinForceEnable = renderer->objwin.bg0Enable && renderer->currentWindow.bg0Enable; \
 833			objwinOnly = !renderer->objwin.bg0Enable; \
 834			break; \
 835		case 1: \
 836			objwinForceEnable = renderer->objwin.bg1Enable && renderer->currentWindow.bg1Enable; \
 837			objwinOnly = !renderer->objwin.bg1Enable; \
 838			break; \
 839		case 2: \
 840			objwinForceEnable = renderer->objwin.bg2Enable && renderer->currentWindow.bg2Enable; \
 841			objwinOnly = !renderer->objwin.bg2Enable; \
 842			break; \
 843		case 3: \
 844			objwinForceEnable = renderer->objwin.bg3Enable && renderer->currentWindow.bg3Enable; \
 845			objwinOnly = !renderer->objwin.bg3Enable; \
 846			break; \
 847		} \
 848	}
 849
 850#define DRAW_BACKGROUND_MODE_0_TILE_SUFFIX_16(BLEND, OBJWIN) \
 851	paletteData = GBA_TEXT_MAP_PALETTE(mapData) << 4; \
 852	palette = &mainPalette[paletteData]; \
 853	charBase = (background->charBase + (GBA_TEXT_MAP_TILE(mapData) << 5)) + (localY << 2); \
 854	LOAD_32(tileData, charBase, vram); \
 855	if (!GBA_TEXT_MAP_HFLIP(mapData)) { \
 856		tileData >>= 4 * mod8; \
 857		for (; outX < end; ++outX) { \
 858			uint32_t* pixel = &renderer->row[outX]; \
 859			BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 860		} \
 861	} else { \
 862		for (outX = end - 1; outX >= renderer->start; --outX) { \
 863			uint32_t* pixel = &renderer->row[outX]; \
 864			BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 865		} \
 866	}
 867
 868#define DRAW_BACKGROUND_MODE_0_TILE_PREFIX_16(BLEND, OBJWIN) \
 869	charBase = (background->charBase + (GBA_TEXT_MAP_TILE(mapData) << 5)) + (localY << 2); \
 870	LOAD_32(tileData, charBase, vram); \
 871	paletteData = GBA_TEXT_MAP_PALETTE(mapData) << 4; \
 872	palette = &mainPalette[paletteData]; \
 873	if (!GBA_TEXT_MAP_HFLIP(mapData)) { \
 874		outX = renderer->end - mod8; \
 875		if (outX < renderer->start) { \
 876			tileData >>= 4 * (renderer->start - outX); \
 877			outX = renderer->start; \
 878		} \
 879		for (; outX < renderer->end; ++outX) { \
 880			uint32_t* pixel = &renderer->row[outX]; \
 881			BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 882		} \
 883	} else { \
 884		tileData >>= 4 * (0x8 - mod8); \
 885		int end2 = renderer->end - 8; \
 886		if (end2 < -1) { \
 887			end2 = -1; \
 888		} \
 889		for (outX = renderer->end - 1; outX > end2; --outX) { \
 890			uint32_t* pixel = &renderer->row[outX]; \
 891			BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 892		} \
 893	}
 894
 895#define DRAW_BACKGROUND_MODE_0_MOSAIC_16(BLEND, OBJWIN) \
 896	for (; tileX < tileEnd; ++tileX) { \
 897		BACKGROUND_TEXT_SELECT_CHARACTER; \
 898		charBase = (background->charBase + (GBA_TEXT_MAP_TILE(mapData) << 5)) + (localY << 2); \
 899		tileData = carryData; \
 900		for (x = 0; x < 8; ++x) { \
 901			if (!mosaicWait) { \
 902				paletteData = GBA_TEXT_MAP_PALETTE(mapData) << 4; \
 903				palette = &mainPalette[paletteData]; \
 904				LOAD_32(tileData, charBase, vram); \
 905				if (!GBA_TEXT_MAP_HFLIP(mapData)) { \
 906					tileData >>= x * 4; \
 907				} else { \
 908					tileData >>= (7 - x) * 4; \
 909				} \
 910				tileData &= 0xF; \
 911				tileData |= tileData << 4; \
 912				tileData |= tileData << 8; \
 913				tileData |= tileData << 12; \
 914				tileData |= tileData << 16; \
 915				tileData |= tileData << 20; \
 916				tileData |= tileData << 24; \
 917				tileData |= tileData << 28; \
 918				carryData = tileData; \
 919				mosaicWait = mosaicH; \
 920			} \
 921			--mosaicWait; \
 922			BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 923			++pixel; \
 924		} \
 925	}
 926
 927#define DRAW_BACKGROUND_MODE_0_TILES_16(BLEND, OBJWIN) \
 928	for (; tileX < tileEnd; ++tileX) { \
 929		BACKGROUND_TEXT_SELECT_CHARACTER; \
 930		paletteData = GBA_TEXT_MAP_PALETTE(mapData) << 4; \
 931		palette = &mainPalette[paletteData]; \
 932		charBase = (background->charBase + (GBA_TEXT_MAP_TILE(mapData) << 5)) + (localY << 2); \
 933		LOAD_32(tileData, charBase, vram); \
 934		if (tileData) { \
 935			if (!GBA_TEXT_MAP_HFLIP(mapData)) { \
 936				BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 937				++pixel; \
 938				BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 939				++pixel; \
 940				BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 941				++pixel; \
 942				BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 943				++pixel; \
 944				BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 945				++pixel; \
 946				BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 947				++pixel; \
 948				BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 949				++pixel; \
 950				BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 951				++pixel; \
 952			} else { \
 953				pixel += 7; \
 954				BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 955				--pixel; \
 956				BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 957				--pixel; \
 958				BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 959				--pixel; \
 960				BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 961				--pixel; \
 962				BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 963				--pixel; \
 964				BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 965				--pixel; \
 966				BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 967				--pixel; \
 968				BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
 969				pixel += 8; \
 970			} \
 971		} else { \
 972			pixel += 8; \
 973		} \
 974	}
 975
 976#define DRAW_BACKGROUND_MODE_0_TILE_SUFFIX_256(BLEND, OBJWIN) \
 977	charBase = (background->charBase + (GBA_TEXT_MAP_TILE(mapData) << 6)) + (localY << 3); \
 978	int end2 = end - 4; \
 979	if (!GBA_TEXT_MAP_HFLIP(mapData)) { \
 980		int shift = inX & 0x3; \
 981		if (end2 > outX) { \
 982			LOAD_32(tileData, charBase, vram); \
 983			tileData >>= 8 * shift; \
 984			shift = 0; \
 985			for (; outX < end2; ++outX) { \
 986				pixel = &renderer->row[outX]; \
 987				BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
 988			} \
 989		} \
 990		\
 991		LOAD_32(tileData, charBase + 4, vram); \
 992		tileData >>= 8 * shift; \
 993		for (; outX < end; ++outX) { \
 994			pixel = &renderer->row[outX]; \
 995			BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
 996		} \
 997	} else { \
 998		int start = outX; \
 999		outX = end - 1; \
1000		if (end2 > start) { \
1001			LOAD_32(tileData, charBase, vram); \
1002			for (; outX >= end2; --outX) { \
1003				pixel = &renderer->row[outX]; \
1004				BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1005			} \
1006			charBase += 4; \
1007		} \
1008		\
1009		LOAD_32(tileData, charBase, vram); \
1010		for (; outX >= renderer->start; --outX) { \
1011			pixel = &renderer->row[outX]; \
1012			BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1013		} \
1014		outX = end; \
1015	}
1016
1017#define DRAW_BACKGROUND_MODE_0_TILE_PREFIX_256(BLEND, OBJWIN) \
1018	charBase = (background->charBase + (GBA_TEXT_MAP_TILE(mapData) << 6)) + (localY << 3); \
1019	outX = renderer->end - 8 + end; \
1020	int end2 = 4 - end; \
1021	if (!GBA_TEXT_MAP_HFLIP(mapData)) { \
1022		if (end2 > 0) { \
1023			LOAD_32(tileData, charBase, vram); \
1024			for (; outX < renderer->end - end2; ++outX) { \
1025				pixel = &renderer->row[outX]; \
1026				BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1027			} \
1028			charBase += 4; \
1029		} \
1030		\
1031		LOAD_32(tileData, charBase, vram); \
1032		for (; outX < renderer->end; ++outX) { \
1033			pixel = &renderer->row[outX]; \
1034			BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1035		} \
1036	} else { \
1037		int shift = end & 0x3; \
1038		int start = outX; \
1039		outX = renderer->end - 1; \
1040		if (end2 > 0) { \
1041			LOAD_32(tileData, charBase, vram); \
1042			tileData >>= 8 * shift; \
1043			for (; outX >= start + 4; --outX) { \
1044				pixel = &renderer->row[outX]; \
1045				BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1046			} \
1047			shift = 0; \
1048		} \
1049		\
1050		LOAD_32(tileData, charBase + 4, vram); \
1051		tileData >>= 8 * shift; \
1052		for (; outX >= start; --outX) { \
1053			pixel = &renderer->row[outX]; \
1054			BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1055		} \
1056	}
1057
1058#define DRAW_BACKGROUND_MODE_0_TILES_256(BLEND, OBJWIN) \
1059	for (; tileX < tileEnd; ++tileX) { \
1060		BACKGROUND_TEXT_SELECT_CHARACTER; \
1061		charBase = (background->charBase + (GBA_TEXT_MAP_TILE(mapData) << 6)) + (localY << 3); \
1062		if (!GBA_TEXT_MAP_HFLIP(mapData)) { \
1063			LOAD_32(tileData, charBase, vram); \
1064			if (tileData) { \
1065					BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1066					++pixel; \
1067					BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1068					++pixel; \
1069					BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1070					++pixel; \
1071					BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1072					++pixel; \
1073			} else { \
1074				pixel += 4; \
1075			} \
1076			LOAD_32(tileData, charBase + 4, vram); \
1077			if (tileData) { \
1078					BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1079					++pixel; \
1080					BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1081					++pixel; \
1082					BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1083					++pixel; \
1084					BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1085					++pixel; \
1086			} else { \
1087				pixel += 4; \
1088			} \
1089		} else { \
1090			LOAD_32(tileData, charBase + 4, vram); \
1091			if (tileData) { \
1092				pixel += 3; \
1093				BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1094				--pixel; \
1095				BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1096				--pixel; \
1097				BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1098				--pixel; \
1099				BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1100			} \
1101			pixel += 4; \
1102			LOAD_32(tileData, charBase, vram); \
1103			if (tileData) { \
1104				pixel += 3; \
1105				BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1106				--pixel; \
1107				BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1108				--pixel; \
1109				BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1110				--pixel; \
1111				BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1112			} \
1113			pixel += 4; \
1114		} \
1115	}
1116
1117#define DRAW_BACKGROUND_MODE_0_MOSAIC_256(BLEND, OBJWIN) \
1118	for (; tileX < tileEnd; ++tileX) { \
1119		BACKGROUND_TEXT_SELECT_CHARACTER; \
1120		charBase = (background->charBase + (GBA_TEXT_MAP_TILE(mapData) << 6)) + (localY << 3); \
1121		tileData = carryData; \
1122		for (x = 0; x < 8; ++x) { \
1123			if (!mosaicWait) { \
1124				if (!GBA_TEXT_MAP_HFLIP(mapData)) { \
1125					if (x >= 4) { \
1126						LOAD_32(tileData, charBase + 4, vram); \
1127						tileData >>= (x - 4) * 8; \
1128					} else { \
1129						LOAD_32(tileData, charBase, vram); \
1130						tileData >>= x * 8; \
1131					} \
1132				} else { \
1133					if (x >= 4) { \
1134						LOAD_32(tileData, charBase, vram); \
1135						tileData >>= (7 - x) * 8; \
1136					} else { \
1137						LOAD_32(tileData, charBase + 4, vram); \
1138						tileData >>= (3 - x) * 8; \
1139					} \
1140				} \
1141				tileData &= 0xFF; \
1142				carryData = tileData; \
1143				mosaicWait = mosaicH; \
1144			} \
1145			tileData |= tileData << 8; \
1146			--mosaicWait; \
1147			BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1148			++pixel; \
1149		} \
1150	}
1151
1152#define DRAW_BACKGROUND_MODE_0(BPP, BLEND, OBJWIN) \
1153	uint32_t* pixel = &renderer->row[outX]; \
1154	if (background->mosaic && renderer->mosaic.bgH) { \
1155		int mosaicH = renderer->mosaic.bgH + 1; \
1156		int x; \
1157		int mosaicWait = outX % mosaicH; \
1158		int carryData = 0; \
1159		paletteData = 0; /* Quiets compiler warning */ \
1160		DRAW_BACKGROUND_MODE_0_MOSAIC_ ## BPP (BLEND, OBJWIN) \
1161		return; \
1162	} \
1163	\
1164	if (inX & 0x7) { \
1165		int mod8 = inX & 0x7; \
1166		BACKGROUND_TEXT_SELECT_CHARACTER; \
1167		\
1168		int end = outX + 0x8 - mod8; \
1169		if (end > renderer->end) { \
1170			/* TODO: ensure tiles are properly aligned from this*/ \
1171			end = renderer->end; \
1172		} \
1173		if (end == outX) { \
1174			return; \
1175		} \
1176		DRAW_BACKGROUND_MODE_0_TILE_SUFFIX_ ## BPP (BLEND, OBJWIN) \
1177	} \
1178	if (inX & 0x7 || (renderer->end - renderer->start) & 0x7) { \
1179		tileX = tileEnd; \
1180		int pixelData; \
1181		int mod8 = (inX + renderer->end - renderer->start) & 0x7; \
1182		BACKGROUND_TEXT_SELECT_CHARACTER; \
1183		\
1184		int end = 0x8 - mod8; \
1185		UNUSED(end); \
1186		DRAW_BACKGROUND_MODE_0_TILE_PREFIX_ ## BPP (BLEND, OBJWIN) \
1187		\
1188		tileX = (inX & 0x7) != 0; \
1189		outX = renderer->start + tileX * 8 - (inX & 0x7); \
1190	} \
1191	\
1192	pixel = &renderer->row[outX]; \
1193	DRAW_BACKGROUND_MODE_0_TILES_ ## BPP (BLEND, OBJWIN)
1194
1195static void _drawBackgroundMode0(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* background, int y) {
1196	int inX = renderer->start + background->x;
1197	if (background->mosaic) {
1198		int mosaicV = renderer->mosaic.bgV + 1;
1199		y -= y % mosaicV;
1200	}
1201	int inY = y + background->y;
1202	uint16_t mapData;
1203
1204	unsigned yBase = inY & 0xF8;
1205	if (background->size == 2) {
1206		yBase += inY & 0x100;
1207	} else if (background->size == 3) {
1208		yBase += (inY & 0x100) << 1;
1209	}
1210	yBase = (background->screenBase >> 1) + (yBase << 2);
1211
1212	int localX;
1213	int localY;
1214
1215	unsigned xBase;
1216
1217	int flags = (background->priority << OFFSET_PRIORITY) | (background->index << OFFSET_INDEX) | FLAG_IS_BACKGROUND;
1218	flags |= FLAG_TARGET_1 * (background->target1 && renderer->blendEffect == BLEND_ALPHA);
1219	flags |= FLAG_TARGET_2 * background->target2;
1220
1221	uint32_t screenBase;
1222	uint32_t charBase;
1223	int variant = background->target1 && renderer->currentWindow.blendEnable && (renderer->blendEffect == BLEND_BRIGHTEN || renderer->blendEffect == BLEND_DARKEN);
1224	color_t* mainPalette = renderer->normalPalette;
1225	if (variant) {
1226		mainPalette = renderer->variantPalette;
1227	}
1228	color_t* palette = mainPalette;
1229	PREPARE_OBJWIN;
1230
1231	int outX = renderer->start;
1232
1233	uint32_t tileData;
1234	uint32_t current;
1235	int pixelData;
1236	int paletteData;
1237	int tileX = 0;
1238	int tileEnd = (renderer->end - renderer->start + (inX & 0x7)) >> 3;
1239	uint16_t* vram = renderer->d.vram;
1240
1241	if (!objwinSlowPath) {
1242		if (!(flags & FLAG_TARGET_2)) {
1243			if (!background->multipalette) {
1244				DRAW_BACKGROUND_MODE_0(16, NoBlend, NO_OBJWIN);
1245			} else {
1246				DRAW_BACKGROUND_MODE_0(256, NoBlend, NO_OBJWIN);
1247			}
1248		} else {
1249			if (!background->multipalette) {
1250				DRAW_BACKGROUND_MODE_0(16, Blend, NO_OBJWIN);
1251			} else {
1252				DRAW_BACKGROUND_MODE_0(256, Blend, NO_OBJWIN);
1253			}
1254		}
1255	} else {
1256		if (!(flags & FLAG_TARGET_2)) {
1257			if (!background->multipalette) {
1258				DRAW_BACKGROUND_MODE_0(16, NoBlend, OBJWIN);
1259			} else {
1260				DRAW_BACKGROUND_MODE_0(256, NoBlend, OBJWIN);
1261			}
1262		} else {
1263			if (!background->multipalette) {
1264				DRAW_BACKGROUND_MODE_0(16, Blend, OBJWIN);
1265			} else {
1266				DRAW_BACKGROUND_MODE_0(256, Blend, OBJWIN);
1267			}
1268		}
1269	}
1270}
1271
1272#define BACKGROUND_BITMAP_INIT \
1273	UNUSED(unused); \
1274	int32_t x = background->sx + (renderer->start - 1) * background->dx; \
1275	int32_t y = background->sy + (renderer->start - 1) * background->dy; \
1276	int32_t localX; \
1277	int32_t localY; \
1278	\
1279	int flags = (background->priority << OFFSET_PRIORITY) | (background->index << OFFSET_INDEX) | FLAG_IS_BACKGROUND; \
1280	flags |= FLAG_TARGET_1 * (background->target1 && renderer->blendEffect == BLEND_ALPHA); \
1281	flags |= FLAG_TARGET_2 * background->target2; \
1282	int variant = background->target1 && renderer->currentWindow.blendEnable && (renderer->blendEffect == BLEND_BRIGHTEN || renderer->blendEffect == BLEND_DARKEN); \
1283	color_t* palette = renderer->normalPalette; \
1284	if (variant) { \
1285		palette = renderer->variantPalette; \
1286	} \
1287	PREPARE_OBJWIN;
1288
1289#define BACKGROUND_BITMAP_ITERATE(W, H) \
1290	x += background->dx; \
1291	y += background->dy; \
1292	\
1293	if (x < 0 || y < 0 || (x >> 8) >= W || (y >> 8) >= H) { \
1294		continue; \
1295	} else { \
1296		localX = x; \
1297		localY = y; \
1298	}
1299
1300static void _drawBackgroundMode2(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* background, int unused) {
1301	int sizeAdjusted = 0x8000 << background->size;
1302
1303	BACKGROUND_BITMAP_INIT;
1304
1305	uint32_t screenBase = background->screenBase;
1306	uint32_t charBase = background->charBase;
1307	uint8_t mapData;
1308	uint8_t tileData;
1309
1310	int outX;
1311	uint32_t* pixel;
1312	for (outX = renderer->start, pixel = &renderer->row[outX]; outX < renderer->end; ++outX, ++pixel) {
1313		x += background->dx;
1314		y += background->dy;
1315
1316		if (background->overflow) {
1317			localX = x & (sizeAdjusted - 1);
1318			localY = y & (sizeAdjusted - 1);
1319		} else if ((x | y) & ~(sizeAdjusted - 1)) {
1320			continue;
1321		} else {
1322			localX = x;
1323			localY = y;
1324		}
1325		mapData = ((uint8_t*)renderer->d.vram)[screenBase + (localX >> 11) + (((localY >> 7) & 0x7F0) << background->size)];
1326		tileData = ((uint8_t*)renderer->d.vram)[charBase + (mapData << 6) + ((localY & 0x700) >> 5) + ((localX & 0x700) >> 8)];
1327
1328		uint32_t current = *pixel;
1329		if (tileData && IS_WRITABLE(current)) {
1330			if (!objwinSlowPath) {
1331				_compositeBlendNoObjwin(renderer, pixel, palette[tileData] | flags, current);
1332			} else if (objwinForceEnable || !(current & FLAG_OBJWIN) == objwinOnly) {
1333				color_t* currentPalette = (current & FLAG_OBJWIN) ? objwinPalette : palette;
1334				_compositeBlendObjwin(renderer, pixel, currentPalette[tileData] | flags, current);
1335			}
1336		}
1337	}
1338}
1339
1340static void _drawBackgroundMode3(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* background, int unused) {
1341	BACKGROUND_BITMAP_INIT;
1342
1343	uint32_t color;
1344
1345	int outX;
1346	uint32_t* pixel;
1347	for (outX = renderer->start, pixel = &renderer->row[outX]; outX < renderer->end; ++outX, ++pixel) {
1348		BACKGROUND_BITMAP_ITERATE(VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS);
1349
1350		LOAD_16(color, ((localX >> 8) + (localY >> 8) * VIDEO_HORIZONTAL_PIXELS) << 1, renderer->d.vram);
1351#ifndef COLOR_16_BIT
1352		unsigned color32;
1353		color32 = 0;
1354		color32 |= (color << 3) & 0xF8;
1355		color32 |= (color << 6) & 0xF800;
1356		color32 |= (color << 9) & 0xF80000;
1357		color = color32;
1358#endif
1359
1360		uint32_t current = *pixel;
1361		if (!objwinSlowPath || !(current & FLAG_OBJWIN) != objwinOnly) {
1362			if (!variant) {
1363				_compositeBlendObjwin(renderer, pixel, color | flags, current);
1364			} else if (renderer->blendEffect == BLEND_BRIGHTEN) {
1365				_compositeBlendObjwin(renderer, pixel, _brighten(color, renderer->bldy) | flags, current);
1366			} else if (renderer->blendEffect == BLEND_DARKEN) {
1367				_compositeBlendObjwin(renderer, pixel, _darken(color, renderer->bldy) | flags, current);
1368			}
1369		}
1370	}
1371}
1372
1373static void _drawBackgroundMode4(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* background, int unused) {
1374	BACKGROUND_BITMAP_INIT;
1375
1376	uint16_t color;
1377	uint32_t offset = 0;
1378	if (GBARegisterDISPCNTIsFrameSelect(renderer->dispcnt)) {
1379		offset = 0xA000;
1380	}
1381
1382	int outX;
1383	uint32_t* pixel;
1384	for (outX = renderer->start, pixel = &renderer->row[outX]; outX < renderer->end; ++outX, ++pixel) {
1385		BACKGROUND_BITMAP_ITERATE(VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS);
1386
1387		color = ((uint8_t*)renderer->d.vram)[offset + (localX >> 8) + (localY >> 8) * VIDEO_HORIZONTAL_PIXELS];
1388
1389		uint32_t current = *pixel;
1390		if (color && IS_WRITABLE(current)) {
1391			if (!objwinSlowPath) {
1392				_compositeBlendNoObjwin(renderer, pixel, palette[color] | flags, current);
1393			} else if (objwinForceEnable || !(current & FLAG_OBJWIN) == objwinOnly) {
1394				color_t* currentPalette = (current & FLAG_OBJWIN) ? objwinPalette : palette;
1395				_compositeBlendObjwin(renderer, pixel, currentPalette[color] | flags, current);
1396			}
1397		}
1398	}
1399}
1400
1401static void _drawBackgroundMode5(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* background, int unused) {
1402	BACKGROUND_BITMAP_INIT;
1403
1404	uint32_t color;
1405	uint32_t offset = 0;
1406	if (GBARegisterDISPCNTIsFrameSelect(renderer->dispcnt)) {
1407		offset = 0xA000;
1408	}
1409
1410	int outX;
1411	uint32_t* pixel;
1412	for (outX = renderer->start, pixel = &renderer->row[outX]; outX < renderer->end; ++outX, ++pixel) {
1413		BACKGROUND_BITMAP_ITERATE(160, 128);
1414
1415		LOAD_16(color, (offset + (localX >> 8) + (localY >> 8) * 160) << 1, renderer->d.vram);
1416#ifndef COLOR_16_BIT
1417		unsigned color32 = 0;
1418		color32 |= (color << 9) & 0xF80000;
1419		color32 |= (color << 3) & 0xF8;
1420		color32 |= (color << 6) & 0xF800;
1421		color = color32;
1422#endif
1423
1424		uint32_t current = *pixel;
1425		if (!objwinSlowPath || !(current & FLAG_OBJWIN) != objwinOnly) {
1426			if (!variant) {
1427				_compositeBlendObjwin(renderer, pixel, color | flags, current);
1428			} else if (renderer->blendEffect == BLEND_BRIGHTEN) {
1429				_compositeBlendObjwin(renderer, pixel, _brighten(color, renderer->bldy) | flags, current);
1430			} else if (renderer->blendEffect == BLEND_DARKEN) {
1431				_compositeBlendObjwin(renderer, pixel, _darken(color, renderer->bldy) | flags, current);
1432			}
1433		}
1434	}
1435}
1436
1437#define SPRITE_NORMAL_LOOP(DEPTH, TYPE) \
1438	SPRITE_YBASE_ ## DEPTH(inY); \
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	if (outX % mosaicH) { \
1450		inX += (mosaicH - (outX % mosaicH)) * xOffset; \
1451		outX += mosaicH - (outX % mosaicH); \
1452	} \
1453	for (; outX < condition; ++outX, inX += xOffset) { \
1454		if (!(renderer->row[outX] & FLAG_UNWRITTEN)) { \
1455			continue; \
1456		} \
1457		int localX = inX - xOffset * (outX % mosaicH); \
1458		SPRITE_XBASE_ ## DEPTH(localX); \
1459		SPRITE_DRAW_PIXEL_ ## DEPTH ## _ ## TYPE(localX); \
1460	}
1461
1462#define SPRITE_TRANSFORMED_LOOP(DEPTH, TYPE) \
1463	int outX; \
1464	for (outX = x >= start ? x : start; outX < x + totalWidth && outX < end; ++outX) { \
1465		if (!(renderer->row[outX] & FLAG_UNWRITTEN)) { \
1466			continue; \
1467		} \
1468		int inX = outX - x; \
1469		int localX = ((mat->a * (inX - (totalWidth >> 1)) + mat->b * (inY - (totalHeight >> 1))) >> 8) + (width >> 1); \
1470		int localY = ((mat->c * (inX - (totalWidth >> 1)) + mat->d * (inY - (totalHeight >> 1))) >> 8) + (height >> 1); \
1471		\
1472		if (localX < 0 || localX >= width || localY < 0 || localY >= height) { \
1473			continue; \
1474		} \
1475		\
1476		SPRITE_YBASE_ ## DEPTH(localY); \
1477		SPRITE_XBASE_ ## DEPTH(localX); \
1478		SPRITE_DRAW_PIXEL_ ## DEPTH ## _ ## TYPE(localX); \
1479	}
1480
1481#define SPRITE_XBASE_16(localX) unsigned xBase = (localX & ~0x7) * 4 + ((localX >> 1) & 2);
1482#define SPRITE_YBASE_16(localY) unsigned yBase = (localY & ~0x7) * (GBARegisterDISPCNTIsObjCharacterMapping(renderer->dispcnt) ? width >> 1 : 0x80) + (localY & 0x7) * 4;
1483
1484#define SPRITE_DRAW_PIXEL_16_NORMAL(localX) \
1485	unsigned tileData = vramBase[((yBase + charBase + xBase) & 0x7FFF) >> 1]; \
1486	tileData = (tileData >> ((localX & 3) << 2)) & 0xF; \
1487	if (tileData && (renderer->spriteLayer[outX] & FLAG_ORDER_MASK) > flags) { \
1488		renderer->spriteLayer[outX] = palette[tileData] | flags; \
1489	}
1490
1491#define SPRITE_DRAW_PIXEL_16_OBJWIN(localX) \
1492	unsigned tileData = vramBase[((yBase + charBase + xBase) & 0x7FFF) >> 1]; \
1493	tileData = (tileData >> ((localX & 3) << 2)) & 0xF; \
1494	if (tileData) { \
1495		renderer->row[outX] |= FLAG_OBJWIN; \
1496	}
1497
1498#define SPRITE_XBASE_256(localX) unsigned xBase = (localX & ~0x7) * 8 + (localX & 6);
1499#define SPRITE_YBASE_256(localY) unsigned yBase = (localY & ~0x7) * (GBARegisterDISPCNTIsObjCharacterMapping(renderer->dispcnt) ? width : 0x80) + (localY & 0x7) * 8;
1500
1501#define SPRITE_DRAW_PIXEL_256_NORMAL(localX) \
1502	unsigned tileData = vramBase[((yBase + charBase + xBase) & 0x7FFF) >> 1]; \
1503	tileData = (tileData >> ((localX & 1) << 3)) & 0xFF; \
1504	if (tileData && (renderer->spriteLayer[outX] & FLAG_ORDER_MASK) > flags) { \
1505		renderer->spriteLayer[outX] = palette[tileData] | flags; \
1506	}
1507
1508#define SPRITE_DRAW_PIXEL_256_OBJWIN(localX) \
1509	unsigned tileData = vramBase[((yBase + charBase + xBase) & 0x7FFF) >> 1]; \
1510	tileData = (tileData >> ((localX & 1) << 3)) & 0xFF; \
1511	if (tileData) { \
1512		renderer->row[outX] |= FLAG_OBJWIN; \
1513	}
1514
1515static int _preprocessSprite(struct GBAVideoSoftwareRenderer* renderer, struct GBAObj* sprite, int y) {
1516	int width = _objSizes[GBAObjAttributesAGetShape(sprite->a) * 8 + GBAObjAttributesBGetSize(sprite->b) * 2];
1517	int height = _objSizes[GBAObjAttributesAGetShape(sprite->a) * 8 + GBAObjAttributesBGetSize(sprite->b) * 2 + 1];
1518	int start = renderer->start;
1519	int end = renderer->end;
1520	uint32_t flags = GBAObjAttributesCGetPriority(sprite->c) << OFFSET_PRIORITY;
1521	flags |= FLAG_TARGET_1 * ((renderer->currentWindow.blendEnable && renderer->target1Obj && renderer->blendEffect == BLEND_ALPHA) || GBAObjAttributesAGetMode(sprite->a) == OBJ_MODE_SEMITRANSPARENT);
1522	flags |= FLAG_OBJWIN * (GBAObjAttributesAGetMode(sprite->a) == OBJ_MODE_OBJWIN);
1523	int32_t x = GBAObjAttributesBGetX(sprite->b) << 23;
1524	x >>= 23;
1525	uint16_t* vramBase = &renderer->d.vram[BASE_TILE >> 1];
1526	unsigned charBase = GBAObjAttributesCGetTile(sprite->c) * 0x20;
1527	int variant = renderer->target1Obj && renderer->currentWindow.blendEnable && (renderer->blendEffect == BLEND_BRIGHTEN || renderer->blendEffect == BLEND_DARKEN);
1528	if (GBAObjAttributesAGetMode(sprite->a) == OBJ_MODE_SEMITRANSPARENT && renderer->target2Bd) {
1529		// Hack: if a sprite is blended, then the variant palette is not used, but we don't know if it's blended in advance
1530		variant = 0;
1531	}
1532	color_t* palette = &renderer->normalPalette[0x100];
1533	if (variant) {
1534		palette = &renderer->variantPalette[0x100];
1535	}
1536
1537	int inY = y - (int) GBAObjAttributesAGetY(sprite->a);
1538
1539	if (GBAObjAttributesAIsTransformed(sprite->a)) {
1540		int totalWidth = width << GBAObjAttributesAGetDoubleSize(sprite->a);
1541		int totalHeight = height << GBAObjAttributesAGetDoubleSize(sprite->a);
1542		struct GBAOAMMatrix* mat = &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)];
1543
1544		if (inY < 0) {
1545			inY += 256;
1546		}
1547
1548		if (!GBAObjAttributesAIs256Color(sprite->a)) {
1549			palette = &palette[GBAObjAttributesCGetPalette(sprite->c) << 4];
1550			if (flags & FLAG_OBJWIN) {
1551				SPRITE_TRANSFORMED_LOOP(16, OBJWIN);
1552			} else {
1553				SPRITE_TRANSFORMED_LOOP(16, NORMAL);
1554			}
1555		} else {
1556			if (flags & FLAG_OBJWIN) {
1557				SPRITE_TRANSFORMED_LOOP(256, OBJWIN);
1558			} else {
1559				SPRITE_TRANSFORMED_LOOP(256, NORMAL);
1560			}
1561		}
1562	} else {
1563		int outX = x >= start ? x : start;
1564		int condition = x + width;
1565		int mosaicH = 1;
1566		if (GBAObjAttributesAIsMosaic(sprite->a)) {
1567			mosaicH = renderer->mosaic.objH + 1;
1568			if (condition % mosaicH) {
1569				condition += mosaicH - (condition % mosaicH);
1570			}
1571		}
1572		if ((int) GBAObjAttributesAGetY(sprite->a) + height - 256 >= 0) {
1573			inY += 256;
1574		}
1575		if (GBAObjAttributesBIsVFlip(sprite->b)) {
1576			inY = height - inY - 1;
1577		}
1578		if (end < condition) {
1579			condition = end;
1580		}
1581		int inX = outX - x;
1582		int xOffset = 1;
1583		if (GBAObjAttributesBIsHFlip(sprite->b)) {
1584			inX = width - inX - 1;
1585			xOffset = -1;
1586		}
1587		if (!GBAObjAttributesAIs256Color(sprite->a)) {
1588			palette = &palette[GBAObjAttributesCGetPalette(sprite->c) << 4];
1589			if (flags & FLAG_OBJWIN) {
1590				SPRITE_NORMAL_LOOP(16, OBJWIN);
1591			} else if (GBAObjAttributesAIsMosaic(sprite->a)) {
1592				SPRITE_MOSAIC_LOOP(16, NORMAL);
1593			} else {
1594				SPRITE_NORMAL_LOOP(16, NORMAL);
1595			}
1596		} else {
1597			if (flags & FLAG_OBJWIN) {
1598				SPRITE_NORMAL_LOOP(256, OBJWIN);
1599			} else if (GBAObjAttributesAIsMosaic(sprite->a)) {
1600				SPRITE_MOSAIC_LOOP(256, NORMAL);
1601			} else {
1602				SPRITE_NORMAL_LOOP(256, NORMAL);
1603			}
1604		}
1605	}
1606	return 1;
1607}
1608
1609static void _postprocessSprite(struct GBAVideoSoftwareRenderer* renderer, unsigned priority) {
1610	int x;
1611	uint32_t* pixel = renderer->row;
1612	uint32_t flags = FLAG_TARGET_2 * renderer->target2Obj;
1613
1614	int objwinSlowPath = GBARegisterDISPCNTIsObjwinEnable(renderer->dispcnt);
1615	int objwinDisable = 0;
1616	if (objwinSlowPath) {
1617		objwinDisable = !renderer->objwin.objEnable;
1618	}
1619	if (objwinSlowPath && objwinDisable) {
1620		for (x = 0; x < VIDEO_HORIZONTAL_PIXELS; ++x, ++pixel) {
1621			uint32_t color = renderer->spriteLayer[x] & ~FLAG_OBJWIN;
1622			uint32_t current = *pixel;
1623			if ((color & FLAG_UNWRITTEN) != FLAG_UNWRITTEN && !(current & FLAG_OBJWIN) && (color & FLAG_PRIORITY) >> OFFSET_PRIORITY == priority) {
1624				_compositeBlendObjwin(renderer, pixel, color | flags, current);
1625			}
1626		}
1627	} else {
1628		for (x = 0; x < VIDEO_HORIZONTAL_PIXELS; ++x, ++pixel) {
1629			uint32_t color = renderer->spriteLayer[x] & ~FLAG_OBJWIN;
1630			uint32_t current = *pixel;
1631			if ((color & FLAG_UNWRITTEN) != FLAG_UNWRITTEN && (color & FLAG_PRIORITY) >> OFFSET_PRIORITY == priority) {
1632				_compositeBlendNoObjwin(renderer, pixel, color | flags, current);
1633			}
1634		}
1635	}
1636}
1637
1638static void _updatePalettes(struct GBAVideoSoftwareRenderer* renderer) {
1639	int i;
1640	if (renderer->blendEffect == BLEND_BRIGHTEN) {
1641		for (i = 0; i < 512; ++i) {
1642			renderer->variantPalette[i] = _brighten(renderer->normalPalette[i], renderer->bldy);
1643		}
1644	} else if (renderer->blendEffect == BLEND_DARKEN) {
1645		for (i = 0; i < 512; ++i) {
1646			renderer->variantPalette[i] = _darken(renderer->normalPalette[i], renderer->bldy);
1647		}
1648	} else {
1649		for (i = 0; i < 512; ++i) {
1650			renderer->variantPalette[i] = renderer->normalPalette[i];
1651		}
1652	}
1653}
1654
1655static inline unsigned _brighten(unsigned color, int y) {
1656	unsigned c = 0;
1657	unsigned a;
1658#ifdef COLOR_16_BIT
1659	a = color & 0x1F;
1660	c |= (a + ((0x1F - a) * y) / 16) & 0x1F;
1661
1662#ifdef COLOR_5_6_5
1663	a = color & 0x7C0;
1664	c |= (a + ((0x7C0 - a) * y) / 16) & 0x7C0;
1665
1666	a = color & 0xF800;
1667	c |= (a + ((0xF800 - a) * y) / 16) & 0xF800;
1668#else
1669	a = color & 0x3E0;
1670	c |= (a + ((0x3E0 - a) * y) / 16) & 0x3E0;
1671
1672	a = color & 0x7C00;
1673	c |= (a + ((0x7C00 - a) * y) / 16) & 0x7C00;
1674#endif
1675#else
1676	a = color & 0xF8;
1677	c |= (a + ((0xF8 - a) * y) / 16) & 0xF8;
1678
1679	a = color & 0xF800;
1680	c |= (a + ((0xF800 - a) * y) / 16) & 0xF800;
1681
1682	a = color & 0xF80000;
1683	c |= (a + ((0xF80000 - a) * y) / 16) & 0xF80000;
1684#endif
1685	return c;
1686}
1687
1688static inline unsigned _darken(unsigned color, int y) {
1689	unsigned c = 0;
1690	unsigned a;
1691#ifdef COLOR_16_BIT
1692	a = color & 0x1F;
1693	c |= (a - (a * y) / 16) & 0x1F;
1694
1695#ifdef COLOR_5_6_5
1696	a = color & 0x7C0;
1697	c |= (a - (a * y) / 16) & 0x7C0;
1698
1699	a = color & 0xF800;
1700	c |= (a - (a * y) / 16) & 0xF800;
1701#else
1702	a = color & 0x3E0;
1703	c |= (a - (a * y) / 16) & 0x3E0;
1704
1705	a = color & 0x7C00;
1706	c |= (a - (a * y) / 16) & 0x7C00;
1707#endif
1708#else
1709	a = color & 0xF8;
1710	c |= (a - (a * y) / 16) & 0xF8;
1711
1712	a = color & 0xF800;
1713	c |= (a - (a * y) / 16) & 0xF800;
1714
1715	a = color & 0xF80000;
1716	c |= (a - (a * y) / 16) & 0xF80000;
1717#endif
1718	return c;
1719}
1720
1721static unsigned _mix(int weightA, unsigned colorA, int weightB, unsigned colorB) {
1722	unsigned c = 0;
1723	unsigned a, b;
1724#ifdef COLOR_16_BIT
1725#ifdef COLOR_5_6_5
1726	a = colorA & 0xF81F;
1727	b = colorB & 0xF81F;
1728	a |= (colorA & 0x7C0) << 16;
1729	b |= (colorB & 0x7C0) << 16;
1730	c = ((a * weightA + b * weightB) / 16);
1731	if (c & 0x08000000) {
1732		c = (c & ~0x0FC00000) | 0x07C00000;
1733	}
1734	if (c & 0x0020) {
1735		c = (c & ~0x003F) | 0x001F;
1736	}
1737	if (c & 0x10000) {
1738		c = (c & ~0x1F800) | 0xF800;
1739	}
1740	c = (c & 0xF81F) | ((c >> 16) & 0x07C0);
1741#else
1742	a = colorA & 0x7C1F;
1743	b = colorB & 0x7C1F;
1744	a |= (colorA & 0x3E0) << 16;
1745	b |= (colorB & 0x3E0) << 16;
1746	c = ((a * weightA + b * weightB) / 16);
1747	if (c & 0x04000000) {
1748		c = (c & ~0x07E00000) | 0x03E00000;
1749	}
1750	if (c & 0x0020) {
1751		c = (c & ~0x003F) | 0x001F;
1752	}
1753	if (c & 0x10000) {
1754		c = (c & ~0x1F800) | 0xF800;
1755	}
1756	c = (c & 0x7C1F) | ((c >> 16) & 0x03E0);
1757#endif
1758#else
1759	a = colorA & 0xF8;
1760	b = colorB & 0xF8;
1761	c |= ((a * weightA + b * weightB) / 16) & 0x1F8;
1762	if (c & 0x00000100) {
1763		c = 0x000000F8;
1764	}
1765
1766	a = colorA & 0xF800;
1767	b = colorB & 0xF800;
1768	c |= ((a * weightA + b * weightB) / 16) & 0x1F800;
1769	if (c & 0x00010000) {
1770		c = (c & 0x000000F8) | 0x0000F800;
1771	}
1772
1773	a = colorA & 0xF80000;
1774	b = colorB & 0xF80000;
1775	c |= ((a * weightA + b * weightB) / 16) & 0x1F80000;
1776	if (c & 0x01000000) {
1777		c = (c & 0x0000F8F8) | 0x00F80000;
1778	}
1779#endif
1780	return c;
1781}