all repos — mgba @ 14b6393b51a3e8ea38e83d2bef8c30db7d83cae2

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