all repos — mgba @ 6560db2ef58e1a192a2356fd8ae1de65379b838c

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