all repos — mgba @ a872bd36421a63b70f6d428dc1886b73ceb607ec

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