all repos — mgba @ b4d90e7e8455ecc29ea7380718b007ada083e7ee

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.packed = 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
 107	softwareRenderer->mosaic.packed = 0;
 108
 109	for (i = 0; i < 4; ++i) {
 110		struct GBAVideoSoftwareBackground* bg = &softwareRenderer->bg[i];
 111		bg->index = i;
 112		bg->enabled = 0;
 113		bg->priority = 0;
 114		bg->charBase = 0;
 115		bg->mosaic = 0;
 116		bg->multipalette = 0;
 117		bg->screenBase = 0;
 118		bg->overflow = 0;
 119		bg->size = 0;
 120		bg->target1 = 0;
 121		bg->target2 = 0;
 122		bg->x = 0;
 123		bg->y = 0;
 124		bg->refx = 0;
 125		bg->refy = 0;
 126		bg->dx = 256;
 127		bg->dmx = 0;
 128		bg->dy = 0;
 129		bg->dmy = 256;
 130		bg->sx = 0;
 131		bg->sy = 0;
 132	}
 133}
 134
 135static void GBAVideoSoftwareRendererDeinit(struct GBAVideoRenderer* renderer) {
 136	struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
 137	UNUSED(softwareRenderer);
 138}
 139
 140static uint16_t GBAVideoSoftwareRendererWriteVideoRegister(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
 141	struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
 142	switch (address) {
 143	case REG_DISPCNT:
 144		softwareRenderer->dispcnt.packed = value;
 145		GBAVideoSoftwareRendererUpdateDISPCNT(softwareRenderer);
 146		break;
 147	case REG_BG0CNT:
 148		value &= 0xFFCF;
 149		GBAVideoSoftwareRendererWriteBGCNT(softwareRenderer, &softwareRenderer->bg[0], value);
 150		break;
 151	case REG_BG1CNT:
 152		value &= 0xFFCF;
 153		GBAVideoSoftwareRendererWriteBGCNT(softwareRenderer, &softwareRenderer->bg[1], value);
 154		break;
 155	case REG_BG2CNT:
 156		value &= 0xFFCF;
 157		GBAVideoSoftwareRendererWriteBGCNT(softwareRenderer, &softwareRenderer->bg[2], value);
 158		break;
 159	case REG_BG3CNT:
 160		value &= 0xFFCF;
 161		GBAVideoSoftwareRendererWriteBGCNT(softwareRenderer, &softwareRenderer->bg[3], value);
 162		break;
 163	case REG_BG0HOFS:
 164		value &= 0x01FF;
 165		softwareRenderer->bg[0].x = value;
 166		break;
 167	case REG_BG0VOFS:
 168		value &= 0x01FF;
 169		softwareRenderer->bg[0].y = value;
 170		break;
 171	case REG_BG1HOFS:
 172		value &= 0x01FF;
 173		softwareRenderer->bg[1].x = value;
 174		break;
 175	case REG_BG1VOFS:
 176		value &= 0x01FF;
 177		softwareRenderer->bg[1].y = value;
 178		break;
 179	case REG_BG2HOFS:
 180		value &= 0x01FF;
 181		softwareRenderer->bg[2].x = value;
 182		break;
 183	case REG_BG2VOFS:
 184		value &= 0x01FF;
 185		softwareRenderer->bg[2].y = value;
 186		break;
 187	case REG_BG3HOFS:
 188		value &= 0x01FF;
 189		softwareRenderer->bg[3].x = value;
 190		break;
 191	case REG_BG3VOFS:
 192		value &= 0x01FF;
 193		softwareRenderer->bg[3].y = value;
 194		break;
 195	case REG_BG2PA:
 196		GBAVideoSoftwareRendererWriteBGPA(&softwareRenderer->bg[2], value);
 197		break;
 198	case REG_BG2PB:
 199		GBAVideoSoftwareRendererWriteBGPB(&softwareRenderer->bg[2], value);
 200		break;
 201	case REG_BG2PC:
 202		GBAVideoSoftwareRendererWriteBGPC(&softwareRenderer->bg[2], value);
 203		break;
 204	case REG_BG2PD:
 205		GBAVideoSoftwareRendererWriteBGPD(&softwareRenderer->bg[2], value);
 206		break;
 207	case REG_BG2X_LO:
 208		GBAVideoSoftwareRendererWriteBGX_LO(&softwareRenderer->bg[2], value);
 209		break;
 210	case REG_BG2X_HI:
 211		GBAVideoSoftwareRendererWriteBGX_HI(&softwareRenderer->bg[2], value);
 212		break;
 213	case REG_BG2Y_LO:
 214		GBAVideoSoftwareRendererWriteBGY_LO(&softwareRenderer->bg[2], value);
 215		break;
 216	case REG_BG2Y_HI:
 217		GBAVideoSoftwareRendererWriteBGY_HI(&softwareRenderer->bg[2], value);
 218		break;
 219	case REG_BG3PA:
 220		GBAVideoSoftwareRendererWriteBGPA(&softwareRenderer->bg[3], value);
 221		break;
 222	case REG_BG3PB:
 223		GBAVideoSoftwareRendererWriteBGPB(&softwareRenderer->bg[3], value);
 224		break;
 225	case REG_BG3PC:
 226		GBAVideoSoftwareRendererWriteBGPC(&softwareRenderer->bg[3], value);
 227		break;
 228	case REG_BG3PD:
 229		GBAVideoSoftwareRendererWriteBGPD(&softwareRenderer->bg[3], value);
 230		break;
 231	case REG_BG3X_LO:
 232		GBAVideoSoftwareRendererWriteBGX_LO(&softwareRenderer->bg[3], value);
 233		break;
 234	case REG_BG3X_HI:
 235		GBAVideoSoftwareRendererWriteBGX_HI(&softwareRenderer->bg[3], value);
 236		break;
 237	case REG_BG3Y_LO:
 238		GBAVideoSoftwareRendererWriteBGY_LO(&softwareRenderer->bg[3], value);
 239		break;
 240	case REG_BG3Y_HI:
 241		GBAVideoSoftwareRendererWriteBGY_HI(&softwareRenderer->bg[3], value);
 242		break;
 243	case REG_BLDCNT:
 244		GBAVideoSoftwareRendererWriteBLDCNT(softwareRenderer, value);
 245		break;
 246	case REG_BLDALPHA:
 247		softwareRenderer->blda = value & 0x1F;
 248		if (softwareRenderer->blda > 0x10) {
 249			softwareRenderer->blda = 0x10;
 250		}
 251		softwareRenderer->bldb = (value >> 8) & 0x1F;
 252		if (softwareRenderer->bldb > 0x10) {
 253			softwareRenderer->bldb = 0x10;
 254		}
 255		break;
 256	case REG_BLDY:
 257		softwareRenderer->bldy = value & 0x1F;
 258		if (softwareRenderer->bldy > 0x10) {
 259			softwareRenderer->bldy = 0x10;
 260		}
 261		_updatePalettes(softwareRenderer);
 262		break;
 263	case REG_WIN0H:
 264		softwareRenderer->winN[0].h.packed = value;
 265		if (softwareRenderer->winN[0].h.start > VIDEO_HORIZONTAL_PIXELS && softwareRenderer->winN[0].h.start > softwareRenderer->winN[0].h.end) {
 266			softwareRenderer->winN[0].h.start = 0;
 267		}
 268		if (softwareRenderer->winN[0].h.start > softwareRenderer->winN[0].h.end || softwareRenderer->winN[0].h.end > VIDEO_HORIZONTAL_PIXELS) {
 269			softwareRenderer->winN[0].h.end = VIDEO_HORIZONTAL_PIXELS;
 270		}
 271		break;
 272	case REG_WIN1H:
 273		softwareRenderer->winN[1].h.packed = value;
 274		if (softwareRenderer->winN[1].h.start > VIDEO_HORIZONTAL_PIXELS && softwareRenderer->winN[1].h.start > softwareRenderer->winN[1].h.end) {
 275			softwareRenderer->winN[1].h.start = 0;
 276		}
 277		if (softwareRenderer->winN[1].h.start > softwareRenderer->winN[1].h.end || softwareRenderer->winN[1].h.end > VIDEO_HORIZONTAL_PIXELS) {
 278			softwareRenderer->winN[1].h.end = VIDEO_HORIZONTAL_PIXELS;
 279		}
 280		break;
 281	case REG_WIN0V:
 282		softwareRenderer->winN[0].v.packed = value;
 283		if (softwareRenderer->winN[0].v.start > VIDEO_VERTICAL_PIXELS && softwareRenderer->winN[0].v.start > softwareRenderer->winN[0].v.end) {
 284			softwareRenderer->winN[0].v.start = 0;
 285		}
 286		if (softwareRenderer->winN[0].v.start > softwareRenderer->winN[0].v.end || softwareRenderer->winN[0].v.end > VIDEO_HORIZONTAL_PIXELS) {
 287			softwareRenderer->winN[0].v.end = VIDEO_VERTICAL_PIXELS;
 288		}
 289		break;
 290	case REG_WIN1V:
 291		softwareRenderer->winN[1].v.packed = value;
 292		if (softwareRenderer->winN[1].v.start > VIDEO_VERTICAL_PIXELS && softwareRenderer->winN[1].v.start > softwareRenderer->winN[1].v.end) {
 293			softwareRenderer->winN[1].v.start = 0;
 294		}
 295		if (softwareRenderer->winN[1].v.start > softwareRenderer->winN[1].v.end || softwareRenderer->winN[1].v.end > VIDEO_HORIZONTAL_PIXELS) {
 296			softwareRenderer->winN[1].v.end = VIDEO_VERTICAL_PIXELS;
 297		}
 298		break;
 299	case REG_WININ:
 300		softwareRenderer->winN[0].control.packed = value;
 301		softwareRenderer->winN[1].control.packed = value >> 8;
 302		break;
 303	case REG_WINOUT:
 304		softwareRenderer->winout.packed = value;
 305		softwareRenderer->objwin.packed = value >> 8;
 306		break;
 307	case REG_MOSAIC:
 308		softwareRenderer->mosaic.packed = value;
 309		break;
 310	case REG_GREENSWP:
 311		GBALog(0, GBA_LOG_STUB, "Stub video register write: 0x%03X", address);
 312		break;
 313	default:
 314		GBALog(0, GBA_LOG_GAME_ERROR, "Invalid video register: 0x%03X", address);
 315	}
 316	return value;
 317}
 318
 319static void GBAVideoSoftwareRendererWriteOAM(struct GBAVideoRenderer* renderer, uint32_t oam) {
 320	struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
 321	softwareRenderer->oamDirty = 1;
 322	UNUSED(oam);
 323}
 324
 325static void GBAVideoSoftwareRendererWritePalette(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
 326	struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
 327#ifdef COLOR_16_BIT
 328#ifdef COLOR_5_6_5
 329	unsigned color = 0;
 330	color |= (value & 0x001F) << 11;
 331	color |= (value & 0x03E0) << 1;
 332	color |= (value & 0x7C00) >> 10;
 333#else
 334	unsigned color = value;
 335#endif
 336#else
 337	unsigned color = 0;
 338	color |= (value << 3) & 0xF8;
 339	color |= (value << 6) & 0xF800;
 340	color |= (value << 9) & 0xF80000;
 341#endif
 342	softwareRenderer->normalPalette[address >> 1] = color;
 343	if (softwareRenderer->blendEffect == BLEND_BRIGHTEN) {
 344		softwareRenderer->variantPalette[address >> 1] = _brighten(color, softwareRenderer->bldy);
 345	} else if (softwareRenderer->blendEffect == BLEND_DARKEN) {
 346		softwareRenderer->variantPalette[address >> 1] = _darken(color, softwareRenderer->bldy);
 347	}
 348}
 349
 350static void _breakWindow(struct GBAVideoSoftwareRenderer* softwareRenderer, struct WindowN* win) {
 351	int activeWindow;
 352	int startX = 0;
 353	if (win->h.end > 0) {
 354		for (activeWindow = 0; activeWindow < softwareRenderer->nWindows; ++activeWindow) {
 355			if (win->h.start < softwareRenderer->windows[activeWindow].endX) {
 356				// Insert a window before the end of the active window
 357				struct Window oldWindow = softwareRenderer->windows[activeWindow];
 358				if (win->h.start > startX) {
 359					// And after the start of the active window
 360					int nextWindow = softwareRenderer->nWindows;
 361					++softwareRenderer->nWindows;
 362					for (; nextWindow > activeWindow; --nextWindow) {
 363						softwareRenderer->windows[nextWindow] = softwareRenderer->windows[nextWindow - 1];
 364					}
 365					softwareRenderer->windows[activeWindow].endX = win->h.start;
 366					++activeWindow;
 367				}
 368				softwareRenderer->windows[activeWindow].control = win->control;
 369				softwareRenderer->windows[activeWindow].endX = win->h.end;
 370				if (win->h.end >= oldWindow.endX) {
 371					// Trim off extra windows we've overwritten
 372					for (++activeWindow; win->h.end >= softwareRenderer->windows[activeWindow].endX && softwareRenderer->nWindows > activeWindow; ++activeWindow) {
 373						softwareRenderer->windows[activeWindow] = softwareRenderer->windows[activeWindow + 1];
 374						--softwareRenderer->nWindows;
 375					}
 376				} else {
 377					++activeWindow;
 378					int nextWindow = softwareRenderer->nWindows;
 379					++softwareRenderer->nWindows;
 380					for (; nextWindow > activeWindow; --nextWindow) {
 381						softwareRenderer->windows[nextWindow] = softwareRenderer->windows[nextWindow - 1];
 382					}
 383					softwareRenderer->windows[activeWindow] = oldWindow;
 384				}
 385				break;
 386			}
 387			startX = softwareRenderer->windows[activeWindow].endX;
 388		}
 389	}
 390}
 391
 392static void _cleanOAM(struct GBAVideoSoftwareRenderer* renderer) {
 393	int i;
 394	int oamMax = 0;
 395	for (i = 0; i < 128; ++i) {
 396		struct GBAObj* obj = &renderer->d.oam->obj[i];
 397		if (obj->transformed || !obj->disable) {
 398			int height = _objSizes[obj->shape * 8 + obj->size * 2 + 1];
 399			if (obj->transformed) {
 400				height <<= ((struct GBATransformedObj*) obj)->doublesize;
 401			}
 402			if (obj->y < VIDEO_VERTICAL_PIXELS || obj->y + height >= VIDEO_VERTICAL_TOTAL_PIXELS) {
 403				renderer->sprites[oamMax].y = obj->y;
 404				renderer->sprites[oamMax].endY = obj->y + height;
 405				renderer->sprites[oamMax].obj = *obj;
 406				++oamMax;
 407			}
 408		}
 409	}
 410	renderer->oamMax = oamMax;
 411	renderer->oamDirty = 0;
 412}
 413
 414
 415static void GBAVideoSoftwareRendererDrawScanline(struct GBAVideoRenderer* renderer, int y) {
 416	struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
 417
 418	color_t* row = &softwareRenderer->outputBuffer[softwareRenderer->outputBufferStride * y];
 419	if (softwareRenderer->dispcnt.forcedBlank) {
 420		int x;
 421		for (x = 0; x < VIDEO_HORIZONTAL_PIXELS; ++x) {
 422			row[x] = GBA_COLOR_WHITE;
 423		}
 424		return;
 425	}
 426
 427	int x;
 428	for (x = 0; x < VIDEO_HORIZONTAL_PIXELS; x += 4) {
 429		softwareRenderer->spriteLayer[x] = FLAG_UNWRITTEN;
 430		softwareRenderer->spriteLayer[x + 1] = FLAG_UNWRITTEN;
 431		softwareRenderer->spriteLayer[x + 2] = FLAG_UNWRITTEN;
 432		softwareRenderer->spriteLayer[x + 3] = FLAG_UNWRITTEN;
 433	}
 434
 435	softwareRenderer->windows[0].endX = VIDEO_HORIZONTAL_PIXELS;
 436	softwareRenderer->nWindows = 1;
 437	if (softwareRenderer->dispcnt.win0Enable || softwareRenderer->dispcnt.win1Enable || softwareRenderer->dispcnt.objwinEnable) {
 438		softwareRenderer->windows[0].control = softwareRenderer->winout;
 439		if (softwareRenderer->dispcnt.win1Enable && y < softwareRenderer->winN[1].v.end && y >= softwareRenderer->winN[1].v.start) {
 440			_breakWindow(softwareRenderer, &softwareRenderer->winN[1]);
 441		}
 442		if (softwareRenderer->dispcnt.win0Enable && y < softwareRenderer->winN[0].v.end && y >= softwareRenderer->winN[0].v.start) {
 443			_breakWindow(softwareRenderer, &softwareRenderer->winN[0]);
 444		}
 445	} else {
 446		softwareRenderer->windows[0].control.packed = 0xFF;
 447	}
 448
 449	int w;
 450	x = 0;
 451	for (w = 0; w < softwareRenderer->nWindows; ++w) {
 452		// TOOD: handle objwin on backdrop
 453		uint32_t backdrop = FLAG_UNWRITTEN | FLAG_PRIORITY | FLAG_IS_BACKGROUND;
 454		if (!softwareRenderer->target1Bd || softwareRenderer->blendEffect == BLEND_NONE || softwareRenderer->blendEffect == BLEND_ALPHA || !softwareRenderer->windows[w].control.blendEnable) {
 455			backdrop |= softwareRenderer->normalPalette[0];
 456		} else {
 457			backdrop |= softwareRenderer->variantPalette[0];
 458		}
 459		int end = softwareRenderer->windows[w].endX;
 460		for (; x < end; ++x) {
 461			softwareRenderer->row[x] = backdrop;
 462		}
 463	}
 464
 465	_drawScanline(softwareRenderer, y);
 466
 467	if (softwareRenderer->target2Bd) {
 468		x = 0;
 469		for (w = 0; w < softwareRenderer->nWindows; ++w) {
 470		uint32_t backdrop = FLAG_UNWRITTEN;
 471			if (!softwareRenderer->target1Bd || softwareRenderer->blendEffect == BLEND_NONE || softwareRenderer->blendEffect == BLEND_ALPHA || !softwareRenderer->windows[w].control.blendEnable) {
 472				backdrop |= softwareRenderer->normalPalette[0];
 473			} else {
 474				backdrop |= softwareRenderer->variantPalette[0];
 475			}
 476			int end = softwareRenderer->windows[w].endX;
 477			for (; x < end; ++x) {
 478				uint32_t color = softwareRenderer->row[x];
 479				if (color & FLAG_TARGET_1) {
 480					softwareRenderer->row[x] = _mix(softwareRenderer->bldb, backdrop, softwareRenderer->blda, color);
 481				}
 482			}
 483		}
 484	}
 485
 486#ifdef COLOR_16_BIT
 487#ifdef __arm__
 488	_to16Bit(row, softwareRenderer->row, VIDEO_HORIZONTAL_PIXELS);
 489#else
 490	for (x = 0; x < VIDEO_HORIZONTAL_PIXELS; ++x) {
 491		row[x] = softwareRenderer->row[x];
 492	}
 493#endif
 494#else
 495	memcpy(row, softwareRenderer->row, VIDEO_HORIZONTAL_PIXELS * sizeof(*row));
 496#endif
 497}
 498
 499static void GBAVideoSoftwareRendererFinishFrame(struct GBAVideoRenderer* renderer) {
 500	struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
 501
 502	softwareRenderer->bg[2].sx = softwareRenderer->bg[2].refx;
 503	softwareRenderer->bg[2].sy = softwareRenderer->bg[2].refy;
 504	softwareRenderer->bg[3].sx = softwareRenderer->bg[3].refx;
 505	softwareRenderer->bg[3].sy = softwareRenderer->bg[3].refy;
 506}
 507
 508static void GBAVideoSoftwareRendererGetPixels(struct GBAVideoRenderer* renderer, unsigned* stride, void** pixels) {
 509	struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
 510
 511	*stride = softwareRenderer->outputBufferStride;
 512	*pixels = softwareRenderer->outputBuffer;
 513}
 514
 515static void GBAVideoSoftwareRendererPutPixels(struct GBAVideoRenderer* renderer, unsigned stride, void* pixels) {
 516	struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
 517
 518	uint32_t* colorPixels = pixels;
 519	unsigned i;
 520	for (i = 0; i < VIDEO_VERTICAL_PIXELS; ++i) {
 521		memmove(&softwareRenderer->outputBuffer[softwareRenderer->outputBufferStride * i], &colorPixels[stride * i], VIDEO_HORIZONTAL_PIXELS * BYTES_PER_PIXEL);
 522	}
 523}
 524
 525static void GBAVideoSoftwareRendererUpdateDISPCNT(struct GBAVideoSoftwareRenderer* renderer) {
 526	renderer->bg[0].enabled = renderer->dispcnt.bg0Enable;
 527	renderer->bg[1].enabled = renderer->dispcnt.bg1Enable;
 528	renderer->bg[2].enabled = renderer->dispcnt.bg2Enable;
 529	renderer->bg[3].enabled = renderer->dispcnt.bg3Enable;
 530}
 531
 532static void GBAVideoSoftwareRendererWriteBGCNT(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* bg, uint16_t value) {
 533	UNUSED(renderer);
 534	union GBARegisterBGCNT reg = { .packed = value };
 535	bg->priority = reg.priority;
 536	bg->charBase = reg.charBase << 14;
 537	bg->mosaic = reg.mosaic;
 538	bg->multipalette = reg.multipalette;
 539	bg->screenBase = reg.screenBase << 11;
 540	bg->overflow = reg.overflow;
 541	bg->size = reg.size;
 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	(renderer->dispcnt.objwinEnable && 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 (renderer->dispcnt.objEnable) {
 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) && renderer->dispcnt.mode < 2) {
 683				_drawBackgroundMode0(renderer, &renderer->bg[0], y);
 684			}
 685			if (TEST_LAYER_ENABLED(1) && renderer->dispcnt.mode < 2) {
 686				_drawBackgroundMode0(renderer, &renderer->bg[1], y);
 687			}
 688			if (TEST_LAYER_ENABLED(2)) {
 689				switch (renderer->dispcnt.mode) {
 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 (renderer->dispcnt.mode) {
 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 = renderer->dispcnt.objwinEnable; \
 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	/* TODO: hflip */ \
 979	charBase = ((background->charBase + (GBA_TEXT_MAP_TILE(mapData) << 6)) >> 2) + (localY << 1); \
 980	int end2 = end - 4; \
 981	int shift = inX & 0x3; \
 982	if (end2 > 0) { \
 983		tileData = ((uint32_t*) vram)[charBase]; \
 984		tileData >>= 8 * shift; \
 985		shift = 0; \
 986		for (; outX < end2; ++outX) { \
 987			uint32_t* 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		uint32_t* pixel = &renderer->row[outX]; \
 996		BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
 997	}
 998
 999#define DRAW_BACKGROUND_MODE_0_TILE_PREFIX_256(BLEND, OBJWIN) \
1000	/* TODO: hflip */ \
1001	charBase = ((background->charBase + (GBA_TEXT_MAP_TILE(mapData) << 6)) >> 2) + (localY << 1); \
1002	outX = renderer->end - 8 + end; \
1003	int end2 = 4 - end; \
1004	if (end2 > 0) { \
1005		tileData = ((uint32_t*) vram)[charBase]; \
1006		for (; outX < renderer->end - end2; ++outX) { \
1007			uint32_t* pixel = &renderer->row[outX]; \
1008			BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1009		} \
1010		++charBase; \
1011	} \
1012	\
1013	tileData = ((uint32_t*) vram)[charBase]; \
1014	for (; outX < renderer->end; ++outX) { \
1015		uint32_t* pixel = &renderer->row[outX]; \
1016		BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1017	}
1018
1019#define DRAW_BACKGROUND_MODE_0_TILES_256(BLEND, OBJWIN) \
1020	for (; tileX < tileEnd; ++tileX) { \
1021		BACKGROUND_TEXT_SELECT_CHARACTER; \
1022		charBase = ((background->charBase + (GBA_TEXT_MAP_TILE(mapData) << 6)) >> 2) + (localY << 1); \
1023		if (!GBA_TEXT_MAP_HFLIP(mapData)) { \
1024			tileData = ((uint32_t*) vram)[charBase]; \
1025			if (tileData) { \
1026					BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1027					++pixel; \
1028					BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1029					++pixel; \
1030					BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1031					++pixel; \
1032					BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1033					++pixel; \
1034			} else { \
1035				pixel += 4; \
1036			} \
1037			tileData = ((uint32_t*) vram)[charBase + 1]; \
1038			if (tileData) { \
1039					BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1040					++pixel; \
1041					BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1042					++pixel; \
1043					BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1044					++pixel; \
1045					BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1046					++pixel; \
1047			} else { \
1048				pixel += 4; \
1049			} \
1050		} else { \
1051			uint32_t tileData = ((uint32_t*) vram)[charBase + 1]; \
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			tileData = ((uint32_t*) vram)[charBase]; \
1064			if (tileData) { \
1065				pixel += 3; \
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			} \
1074			pixel += 4; \
1075		} \
1076	}
1077
1078#define DRAW_BACKGROUND_MODE_0_MOSAIC_256(BLEND, OBJWIN) \
1079	for (; tileX < tileEnd; ++tileX) { \
1080		BACKGROUND_TEXT_SELECT_CHARACTER; \
1081		charBase = ((background->charBase + (GBA_TEXT_MAP_TILE(mapData) << 6)) >> 2) + (localY << 1); \
1082		tileData = carryData; \
1083		for (x = 0; x < 8; ++x) { \
1084			if (!mosaicWait) { \
1085				if (!GBA_TEXT_MAP_HFLIP(mapData)) { \
1086					if (x >= 4) { \
1087						tileData = ((uint32_t*) vram)[charBase + 1]; \
1088						tileData >>= (x - 4) * 8; \
1089					} else { \
1090						tileData = ((uint32_t*) vram)[charBase]; \
1091						tileData >>= x * 8; \
1092					} \
1093				} else { \
1094					if (x >= 4) { \
1095						tileData = ((uint32_t*) vram)[charBase]; \
1096						tileData >>= (7 - x) * 8; \
1097					} else { \
1098						tileData = ((uint32_t*) vram)[charBase + 1]; \
1099						tileData >>= (3 - x) * 8; \
1100					} \
1101				} \
1102				tileData &= 0xFF; \
1103				carryData = tileData; \
1104				mosaicWait = mosaicH; \
1105			} \
1106			tileData |= tileData << 8; \
1107			--mosaicWait; \
1108			BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1109			++pixel; \
1110		} \
1111	}
1112
1113#define DRAW_BACKGROUND_MODE_0(BPP, BLEND, OBJWIN) \
1114	uint32_t* pixel = &renderer->row[outX]; \
1115	if (background->mosaic && renderer->mosaic.bgH) { \
1116		int mosaicH = renderer->mosaic.bgH + 1; \
1117		int x; \
1118		int mosaicWait = outX % mosaicH; \
1119		int carryData = 0; \
1120		paletteData = 0; /* Quiets compiler warning */ \
1121		DRAW_BACKGROUND_MODE_0_MOSAIC_ ## BPP (BLEND, OBJWIN) \
1122		return; \
1123	} \
1124	\
1125	if (inX & 0x7) { \
1126		int mod8 = inX & 0x7; \
1127		BACKGROUND_TEXT_SELECT_CHARACTER; \
1128		\
1129		int end = outX + 0x8 - mod8; \
1130		if (end > renderer->end) { \
1131			/* TODO: ensure tiles are properly aligned from this*/ \
1132			end = renderer->end; \
1133		} \
1134		if (end == outX) { \
1135			return; \
1136		} \
1137		DRAW_BACKGROUND_MODE_0_TILE_SUFFIX_ ## BPP (BLEND, OBJWIN) \
1138	} \
1139	if (inX & 0x7 || (renderer->end - renderer->start) & 0x7) { \
1140		tileX = tileEnd; \
1141		int pixelData; \
1142		int mod8 = (inX + renderer->end - renderer->start) & 0x7; \
1143		BACKGROUND_TEXT_SELECT_CHARACTER; \
1144		\
1145		int end = 0x8 - mod8; \
1146		UNUSED(end); \
1147		DRAW_BACKGROUND_MODE_0_TILE_PREFIX_ ## BPP (BLEND, OBJWIN) \
1148		\
1149		tileX = (inX & 0x7) != 0; \
1150		outX = renderer->start + tileX * 8 - (inX & 0x7); \
1151	} \
1152	\
1153	pixel = &renderer->row[outX]; \
1154	DRAW_BACKGROUND_MODE_0_TILES_ ## BPP (BLEND, OBJWIN)
1155
1156static void _drawBackgroundMode0(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* background, int y) {
1157	int inX = renderer->start + background->x;
1158	if (background->mosaic) {
1159		int mosaicV = renderer->mosaic.bgV + 1;
1160		y -= y % mosaicV;
1161	}
1162	int inY = y + background->y;
1163	uint16_t mapData;
1164
1165	unsigned yBase = inY & 0xF8;
1166	if (background->size == 2) {
1167		yBase += inY & 0x100;
1168	} else if (background->size == 3) {
1169		yBase += (inY & 0x100) << 1;
1170	}
1171	yBase = (background->screenBase >> 1) + (yBase << 2);
1172
1173	int localX;
1174	int localY;
1175
1176	unsigned xBase;
1177
1178	int flags = (background->priority << OFFSET_PRIORITY) | (background->index << OFFSET_INDEX) | FLAG_IS_BACKGROUND;
1179	flags |= FLAG_TARGET_1 * (background->target1 && renderer->blendEffect == BLEND_ALPHA);
1180	flags |= FLAG_TARGET_2 * background->target2;
1181
1182	uint32_t screenBase;
1183	uint32_t charBase;
1184	int variant = background->target1 && renderer->currentWindow.blendEnable && (renderer->blendEffect == BLEND_BRIGHTEN || renderer->blendEffect == BLEND_DARKEN);
1185	color_t* mainPalette = renderer->normalPalette;
1186	if (variant) {
1187		mainPalette = renderer->variantPalette;
1188	}
1189	color_t* palette = mainPalette;
1190	PREPARE_OBJWIN;
1191
1192	int outX = renderer->start;
1193
1194	uint32_t tileData;
1195	uint32_t current;
1196	int pixelData;
1197	int paletteData;
1198	int tileX = 0;
1199	int tileEnd = (renderer->end - renderer->start + (inX & 0x7)) >> 3;
1200	uint16_t* vram = renderer->d.vram;
1201
1202	if (!objwinSlowPath) {
1203		if (!(flags & FLAG_TARGET_2)) {
1204			if (!background->multipalette) {
1205				DRAW_BACKGROUND_MODE_0(16, NoBlend, NO_OBJWIN);
1206			} else {
1207				DRAW_BACKGROUND_MODE_0(256, NoBlend, NO_OBJWIN);
1208			}
1209		} else {
1210			if (!background->multipalette) {
1211				DRAW_BACKGROUND_MODE_0(16, Blend, NO_OBJWIN);
1212			} else {
1213				DRAW_BACKGROUND_MODE_0(256, Blend, NO_OBJWIN);
1214			}
1215		}
1216	} else {
1217		if (!(flags & FLAG_TARGET_2)) {
1218			if (!background->multipalette) {
1219				DRAW_BACKGROUND_MODE_0(16, NoBlend, OBJWIN);
1220			} else {
1221				DRAW_BACKGROUND_MODE_0(256, NoBlend, OBJWIN);
1222			}
1223		} else {
1224			if (!background->multipalette) {
1225				DRAW_BACKGROUND_MODE_0(16, Blend, OBJWIN);
1226			} else {
1227				DRAW_BACKGROUND_MODE_0(256, Blend, OBJWIN);
1228			}
1229		}
1230	}
1231}
1232
1233#define BACKGROUND_BITMAP_INIT \
1234	UNUSED(unused); \
1235	int32_t x = background->sx + (renderer->start - 1) * background->dx; \
1236	int32_t y = background->sy + (renderer->start - 1) * background->dy; \
1237	int32_t localX; \
1238	int32_t localY; \
1239	\
1240	int flags = (background->priority << OFFSET_PRIORITY) | (background->index << OFFSET_INDEX) | FLAG_IS_BACKGROUND; \
1241	flags |= FLAG_TARGET_1 * (background->target1 && renderer->blendEffect == BLEND_ALPHA); \
1242	flags |= FLAG_TARGET_2 * background->target2; \
1243	int variant = background->target1 && renderer->currentWindow.blendEnable && (renderer->blendEffect == BLEND_BRIGHTEN || renderer->blendEffect == BLEND_DARKEN); \
1244	color_t* palette = renderer->normalPalette; \
1245	if (variant) { \
1246		palette = renderer->variantPalette; \
1247	} \
1248	PREPARE_OBJWIN;
1249
1250#define BACKGROUND_BITMAP_ITERATE(W, H) \
1251	x += background->dx; \
1252	y += background->dy; \
1253	\
1254	if (x < 0 || y < 0 || (x >> 8) >= W || (y >> 8) >= H) { \
1255		continue; \
1256	} else { \
1257		localX = x; \
1258		localY = y; \
1259	}
1260
1261static void _drawBackgroundMode2(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* background, int unused) {
1262	int sizeAdjusted = 0x8000 << background->size;
1263
1264	BACKGROUND_BITMAP_INIT;
1265
1266	uint32_t screenBase = background->screenBase;
1267	uint32_t charBase = background->charBase;
1268	uint8_t mapData;
1269	uint8_t tileData;
1270
1271	int outX;
1272	uint32_t* pixel;
1273	for (outX = renderer->start, pixel = &renderer->row[outX]; outX < renderer->end; ++outX, ++pixel) {
1274		x += background->dx;
1275		y += background->dy;
1276
1277		if (background->overflow) {
1278			localX = x & (sizeAdjusted - 1);
1279			localY = y & (sizeAdjusted - 1);
1280		} else if ((x | y) & ~(sizeAdjusted - 1)) {
1281			continue;
1282		} else {
1283			localX = x;
1284			localY = y;
1285		}
1286		mapData = ((uint8_t*)renderer->d.vram)[screenBase + (localX >> 11) + (((localY >> 7) & 0x7F0) << background->size)];
1287		tileData = ((uint8_t*)renderer->d.vram)[charBase + (mapData << 6) + ((localY & 0x700) >> 5) + ((localX & 0x700) >> 8)];
1288
1289		uint32_t current = *pixel;
1290		if (tileData && IS_WRITABLE(current)) {
1291			if (!objwinSlowPath) {
1292				_compositeBlendNoObjwin(renderer, pixel, palette[tileData] | flags, current);
1293			} else if (objwinForceEnable || !(current & FLAG_OBJWIN) == objwinOnly) {
1294				color_t* currentPalette = (current & FLAG_OBJWIN) ? objwinPalette : palette;
1295				_compositeBlendObjwin(renderer, pixel, currentPalette[tileData] | flags, current);
1296			}
1297		}
1298	}
1299}
1300
1301static void _drawBackgroundMode3(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* background, int unused) {
1302	BACKGROUND_BITMAP_INIT;
1303
1304	uint32_t color;
1305
1306	int outX;
1307	uint32_t* pixel;
1308	for (outX = renderer->start, pixel = &renderer->row[outX]; outX < renderer->end; ++outX, ++pixel) {
1309		BACKGROUND_BITMAP_ITERATE(VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS);
1310
1311		color = ((uint16_t*)renderer->d.vram)[(localX >> 8) + (localY >> 8) * VIDEO_HORIZONTAL_PIXELS];
1312#ifndef COLOR_16_BIT
1313		unsigned color32;
1314		color32 = 0;
1315		color32 |= (color << 3) & 0xF8;
1316		color32 |= (color << 6) & 0xF800;
1317		color32 |= (color << 9) & 0xF80000;
1318		color = color32;
1319#endif
1320
1321		uint32_t current = *pixel;
1322		if (!objwinSlowPath || !(current & FLAG_OBJWIN) != objwinOnly) {
1323			if (!variant) {
1324				_compositeBlendObjwin(renderer, pixel, color | flags, current);
1325			} else if (renderer->blendEffect == BLEND_BRIGHTEN) {
1326				_compositeBlendObjwin(renderer, pixel, _brighten(color, renderer->bldy) | flags, current);
1327			} else if (renderer->blendEffect == BLEND_DARKEN) {
1328				_compositeBlendObjwin(renderer, pixel, _darken(color, renderer->bldy) | flags, current);
1329			}
1330		}
1331	}
1332}
1333
1334static void _drawBackgroundMode4(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* background, int unused) {
1335	BACKGROUND_BITMAP_INIT;
1336
1337	uint16_t color;
1338	uint32_t offset = 0;
1339	if (renderer->dispcnt.frameSelect) {
1340		offset = 0xA000;
1341	}
1342
1343	int outX;
1344	uint32_t* pixel;
1345	for (outX = renderer->start, pixel = &renderer->row[outX]; outX < renderer->end; ++outX, ++pixel) {
1346		BACKGROUND_BITMAP_ITERATE(VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS);
1347
1348		color = ((uint8_t*)renderer->d.vram)[offset + (localX >> 8) + (localY >> 8) * VIDEO_HORIZONTAL_PIXELS];
1349
1350		uint32_t current = *pixel;
1351		if (color && IS_WRITABLE(current)) {
1352			if (!objwinSlowPath) {
1353				_compositeBlendNoObjwin(renderer, pixel, palette[color] | flags, current);
1354			} else if (objwinForceEnable || !(current & FLAG_OBJWIN) == objwinOnly) {
1355				color_t* currentPalette = (current & FLAG_OBJWIN) ? objwinPalette : palette;
1356				_compositeBlendObjwin(renderer, pixel, currentPalette[color] | flags, current);
1357			}
1358		}
1359	}
1360}
1361
1362static void _drawBackgroundMode5(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* background, int unused) {
1363	BACKGROUND_BITMAP_INIT;
1364
1365	uint32_t color;
1366	uint32_t offset = 0;
1367	if (renderer->dispcnt.frameSelect) {
1368		offset = 0xA000;
1369	}
1370
1371	int outX;
1372	uint32_t* pixel;
1373	for (outX = renderer->start, pixel = &renderer->row[outX]; outX < renderer->end; ++outX, ++pixel) {
1374		BACKGROUND_BITMAP_ITERATE(160, 128);
1375
1376		color = ((uint16_t*)renderer->d.vram)[offset + (localX >> 8) + (localY >> 8) * 160];
1377#ifndef COLOR_16_BIT
1378		unsigned color32 = 0;
1379		color32 |= (color << 9) & 0xF80000;
1380		color32 |= (color << 3) & 0xF8;
1381		color32 |= (color << 6) & 0xF800;
1382		color = color32;
1383#endif
1384
1385		uint32_t current = *pixel;
1386		if (!objwinSlowPath || !(current & FLAG_OBJWIN) != objwinOnly) {
1387			if (!variant) {
1388				_compositeBlendObjwin(renderer, pixel, color | flags, current);
1389			} else if (renderer->blendEffect == BLEND_BRIGHTEN) {
1390				_compositeBlendObjwin(renderer, pixel, _brighten(color, renderer->bldy) | flags, current);
1391			} else if (renderer->blendEffect == BLEND_DARKEN) {
1392				_compositeBlendObjwin(renderer, pixel, _darken(color, renderer->bldy) | flags, current);
1393			}
1394		}
1395	}
1396}
1397
1398#define SPRITE_NORMAL_LOOP(DEPTH, TYPE) \
1399	SPRITE_YBASE_ ## DEPTH(inY); \
1400	for (; outX < condition; ++outX, inX += xOffset) { \
1401		if (!(renderer->row[outX] & FLAG_UNWRITTEN)) { \
1402			continue; \
1403		} \
1404		SPRITE_XBASE_ ## DEPTH(inX); \
1405		SPRITE_DRAW_PIXEL_ ## DEPTH ## _ ## TYPE(inX); \
1406	}
1407
1408#define SPRITE_MOSAIC_LOOP(DEPTH, TYPE) \
1409	SPRITE_YBASE_ ## DEPTH(inY); \
1410	if (outX % mosaicH) { \
1411		inX += (mosaicH - (outX % mosaicH)) * xOffset; \
1412		outX += mosaicH - (outX % mosaicH); \
1413	} \
1414	for (; outX < condition; ++outX, inX += xOffset) { \
1415		if (!(renderer->row[outX] & FLAG_UNWRITTEN)) { \
1416			continue; \
1417		} \
1418		int localX = inX - xOffset * (outX % mosaicH); \
1419		SPRITE_XBASE_ ## DEPTH(localX); \
1420		SPRITE_DRAW_PIXEL_ ## DEPTH ## _ ## TYPE(localX); \
1421	}
1422
1423#define SPRITE_TRANSFORMED_LOOP(DEPTH, TYPE) \
1424	int outX; \
1425	for (outX = x >= start ? x : start; outX < x + totalWidth && outX < end; ++outX) { \
1426		if (!(renderer->row[outX] & FLAG_UNWRITTEN)) { \
1427			continue; \
1428		} \
1429		int inX = outX - x; \
1430		int localX = ((mat->a * (inX - (totalWidth >> 1)) + mat->b * (inY - (totalHeight >> 1))) >> 8) + (width >> 1); \
1431		int localY = ((mat->c * (inX - (totalWidth >> 1)) + mat->d * (inY - (totalHeight >> 1))) >> 8) + (height >> 1); \
1432		\
1433		if (localX < 0 || localX >= width || localY < 0 || localY >= height) { \
1434			continue; \
1435		} \
1436		\
1437		SPRITE_YBASE_ ## DEPTH(localY); \
1438		SPRITE_XBASE_ ## DEPTH(localX); \
1439		SPRITE_DRAW_PIXEL_ ## DEPTH ## _ ## TYPE(localX); \
1440	}
1441
1442#define SPRITE_XBASE_16(localX) unsigned xBase = (localX & ~0x7) * 4 + ((localX >> 1) & 2);
1443#define SPRITE_YBASE_16(localY) unsigned yBase = (localY & ~0x7) * (renderer->dispcnt.objCharacterMapping ? width >> 1 : 0x80) + (localY & 0x7) * 4;
1444
1445#define SPRITE_DRAW_PIXEL_16_NORMAL(localX) \
1446	unsigned tileData = vramBase[((yBase + charBase + xBase) & 0x7FFF) >> 1]; \
1447	tileData = (tileData >> ((localX & 3) << 2)) & 0xF; \
1448	if (tileData && (renderer->spriteLayer[outX] & FLAG_ORDER_MASK) > flags) { \
1449		renderer->spriteLayer[outX] = palette[tileData] | flags; \
1450	}
1451
1452#define SPRITE_DRAW_PIXEL_16_OBJWIN(localX) \
1453	unsigned tileData = vramBase[((yBase + charBase + xBase) & 0x7FFF) >> 1]; \
1454	tileData = (tileData >> ((localX & 3) << 2)) & 0xF; \
1455	if (tileData) { \
1456		renderer->row[outX] |= FLAG_OBJWIN; \
1457	}
1458
1459#define SPRITE_XBASE_256(localX) unsigned xBase = (localX & ~0x7) * 8 + (localX & 6);
1460#define SPRITE_YBASE_256(localY) unsigned yBase = (localY & ~0x7) * (renderer->dispcnt.objCharacterMapping ? width : 0x80) + (localY & 0x7) * 8;
1461
1462#define SPRITE_DRAW_PIXEL_256_NORMAL(localX) \
1463	unsigned tileData = vramBase[((yBase + charBase + xBase) & 0x7FFF) >> 1]; \
1464	tileData = (tileData >> ((localX & 1) << 3)) & 0xFF; \
1465	if (tileData && (renderer->spriteLayer[outX] & FLAG_ORDER_MASK) > flags) { \
1466		renderer->spriteLayer[outX] = palette[tileData] | flags; \
1467	}
1468
1469#define SPRITE_DRAW_PIXEL_256_OBJWIN(localX) \
1470	unsigned tileData = vramBase[((yBase + charBase + xBase) & 0x7FFF) >> 1]; \
1471	tileData = (tileData >> ((localX & 1) << 3)) & 0xFF; \
1472	if (tileData) { \
1473		renderer->row[outX] |= FLAG_OBJWIN; \
1474	}
1475
1476static int _preprocessSprite(struct GBAVideoSoftwareRenderer* renderer, struct GBAObj* sprite, int y) {
1477	int width = _objSizes[sprite->shape * 8 + sprite->size * 2];
1478	int height = _objSizes[sprite->shape * 8 + sprite->size * 2 + 1];
1479	int start = renderer->start;
1480	int end = renderer->end;
1481	uint32_t flags = sprite->priority << OFFSET_PRIORITY;
1482	flags |= FLAG_TARGET_1 * ((renderer->currentWindow.blendEnable && renderer->target1Obj && renderer->blendEffect == BLEND_ALPHA) || sprite->mode == OBJ_MODE_SEMITRANSPARENT);
1483	flags |= FLAG_OBJWIN * (sprite->mode == OBJ_MODE_OBJWIN);
1484	int x = sprite->x;
1485	uint16_t* vramBase = &renderer->d.vram[BASE_TILE >> 1];
1486	unsigned charBase = sprite->tile * 0x20;
1487	int variant = renderer->target1Obj && renderer->currentWindow.blendEnable && (renderer->blendEffect == BLEND_BRIGHTEN || renderer->blendEffect == BLEND_DARKEN);
1488	if (sprite->mode == OBJ_MODE_SEMITRANSPARENT && renderer->target2Bd) {
1489		// Hack: if a sprite is blended, then the variant palette is not used, but we don't know if it's blended in advance
1490		variant = 0;
1491	}
1492	color_t* palette = &renderer->normalPalette[0x100];
1493	if (variant) {
1494		palette = &renderer->variantPalette[0x100];
1495	}
1496
1497	int outX = x >= start ? x : start;
1498	int condition = x + width;
1499	int mosaicH = 1;
1500	if (sprite->mosaic) {
1501		mosaicH = renderer->mosaic.objH + 1;
1502		if (condition % mosaicH) {
1503			condition += mosaicH - (condition % mosaicH);
1504		}
1505	}
1506	int inY = y - sprite->y;
1507	if (sprite->y + height - 256 >= 0) {
1508		inY += 256;
1509	}
1510	if (sprite->vflip) {
1511		inY = height - inY - 1;
1512	}
1513	if (end < condition) {
1514		condition = end;
1515	}
1516	int inX = outX - x;
1517	int xOffset = 1;
1518	if (sprite->hflip) {
1519		inX = width - inX - 1;
1520		xOffset = -1;
1521	}
1522	if (!sprite->multipalette) {
1523		palette = &palette[sprite->palette << 4];
1524		if (flags & FLAG_OBJWIN) {
1525			SPRITE_NORMAL_LOOP(16, OBJWIN);
1526		} else if (sprite->mosaic) {
1527			SPRITE_MOSAIC_LOOP(16, NORMAL);
1528		} else {
1529			SPRITE_NORMAL_LOOP(16, NORMAL);
1530		}
1531	} else {
1532		if (flags & FLAG_OBJWIN) {
1533			SPRITE_NORMAL_LOOP(256, OBJWIN);
1534		} else if (sprite->mosaic) {
1535			SPRITE_MOSAIC_LOOP(256, NORMAL);
1536		} else {
1537			SPRITE_NORMAL_LOOP(256, NORMAL);
1538		}
1539	}
1540	return 1;
1541}
1542
1543static int _preprocessTransformedSprite(struct GBAVideoSoftwareRenderer* renderer, struct GBATransformedObj* sprite, int y) {
1544	int width = _objSizes[sprite->shape * 8 + sprite->size * 2];
1545	int height = _objSizes[sprite->shape * 8 + sprite->size * 2 + 1];
1546	int totalWidth = width << sprite->doublesize;
1547	int totalHeight = height << sprite->doublesize;
1548	int start = renderer->start;
1549	int end = renderer->end;
1550	uint32_t flags = sprite->priority << OFFSET_PRIORITY;
1551	flags |= FLAG_TARGET_1 * ((renderer->currentWindow.blendEnable && renderer->target1Obj && renderer->blendEffect == BLEND_ALPHA) || sprite->mode == OBJ_MODE_SEMITRANSPARENT);
1552	flags |= FLAG_OBJWIN * (sprite->mode == OBJ_MODE_OBJWIN);
1553	int x = sprite->x;
1554	uint16_t* vramBase = &renderer->d.vram[BASE_TILE >> 1];
1555	unsigned charBase = sprite->tile * 0x20;
1556	struct GBAOAMMatrix* mat = &renderer->d.oam->mat[sprite->matIndex];
1557	int variant = renderer->target1Obj && renderer->currentWindow.blendEnable && (renderer->blendEffect == BLEND_BRIGHTEN || renderer->blendEffect == BLEND_DARKEN);
1558	if (sprite->mode == OBJ_MODE_SEMITRANSPARENT && renderer->target2Bd) {
1559		// Hack: if a sprite is blended, then the variant palette is not used, but we don't know if it's blended in advance
1560		variant = 0;
1561	}
1562	color_t* palette = &renderer->normalPalette[0x100];
1563	if (variant) {
1564		palette = &renderer->variantPalette[0x100];
1565	}
1566	int inY = y - sprite->y;
1567	if (inY < 0) {
1568		inY += 256;
1569	}
1570	if (!sprite->multipalette) {
1571		palette = &palette[sprite->palette << 4];
1572		if (flags & FLAG_OBJWIN) {
1573			SPRITE_TRANSFORMED_LOOP(16, OBJWIN);
1574		} else {
1575			SPRITE_TRANSFORMED_LOOP(16, NORMAL);
1576		}
1577	} else {
1578		if (flags & FLAG_OBJWIN) {
1579			SPRITE_TRANSFORMED_LOOP(256, OBJWIN);
1580		} else {
1581			SPRITE_TRANSFORMED_LOOP(256, NORMAL);
1582		}
1583	}
1584	return 1;
1585}
1586
1587static void _postprocessSprite(struct GBAVideoSoftwareRenderer* renderer, unsigned priority) {
1588	int x;
1589	uint32_t* pixel = renderer->row;
1590	uint32_t flags = FLAG_TARGET_2 * renderer->target2Obj;
1591
1592	int objwinSlowPath = renderer->dispcnt.objwinEnable;
1593	int objwinDisable = 0;
1594	if (objwinSlowPath) {
1595		objwinDisable = !renderer->objwin.objEnable;
1596	}
1597	if (objwinSlowPath && objwinDisable) {
1598		for (x = 0; x < VIDEO_HORIZONTAL_PIXELS; ++x, ++pixel) {
1599			uint32_t color = renderer->spriteLayer[x] & ~FLAG_OBJWIN;
1600			uint32_t current = *pixel;
1601			if ((color & FLAG_UNWRITTEN) != FLAG_UNWRITTEN && !(current & FLAG_OBJWIN) && (color & FLAG_PRIORITY) >> OFFSET_PRIORITY == priority) {
1602				_compositeBlendObjwin(renderer, pixel, color | flags, current);
1603			}
1604		}
1605	} else {
1606		for (x = 0; x < VIDEO_HORIZONTAL_PIXELS; ++x, ++pixel) {
1607			uint32_t color = renderer->spriteLayer[x] & ~FLAG_OBJWIN;
1608			uint32_t current = *pixel;
1609			if ((color & FLAG_UNWRITTEN) != FLAG_UNWRITTEN && (color & FLAG_PRIORITY) >> OFFSET_PRIORITY == priority) {
1610				_compositeBlendNoObjwin(renderer, pixel, color | flags, current);
1611			}
1612		}
1613	}
1614}
1615
1616static void _updatePalettes(struct GBAVideoSoftwareRenderer* renderer) {
1617	int i;
1618	if (renderer->blendEffect == BLEND_BRIGHTEN) {
1619		for (i = 0; i < 512; ++i) {
1620			renderer->variantPalette[i] = _brighten(renderer->normalPalette[i], renderer->bldy);
1621		}
1622	} else if (renderer->blendEffect == BLEND_DARKEN) {
1623		for (i = 0; i < 512; ++i) {
1624			renderer->variantPalette[i] = _darken(renderer->normalPalette[i], renderer->bldy);
1625		}
1626	} else {
1627		for (i = 0; i < 512; ++i) {
1628			renderer->variantPalette[i] = renderer->normalPalette[i];
1629		}
1630	}
1631}
1632
1633static inline unsigned _brighten(unsigned color, int y) {
1634	unsigned c = 0;
1635	unsigned a;
1636#ifdef COLOR_16_BIT
1637	a = color & 0x1F;
1638	c |= (a + ((0x1F - a) * y) / 16) & 0x1F;
1639
1640#ifdef COLOR_5_6_5
1641	a = color & 0x7C0;
1642	c |= (a + ((0x7C0 - a) * y) / 16) & 0x7C0;
1643
1644	a = color & 0xF800;
1645	c |= (a + ((0xF800 - a) * y) / 16) & 0xF800;
1646#else
1647	a = color & 0x3E0;
1648	c |= (a + ((0x3E0 - a) * y) / 16) & 0x3E0;
1649
1650	a = color & 0x7C00;
1651	c |= (a + ((0x7C00 - a) * y) / 16) & 0x7C00;
1652#endif
1653#else
1654	a = color & 0xF8;
1655	c |= (a + ((0xF8 - a) * y) / 16) & 0xF8;
1656
1657	a = color & 0xF800;
1658	c |= (a + ((0xF800 - a) * y) / 16) & 0xF800;
1659
1660	a = color & 0xF80000;
1661	c |= (a + ((0xF80000 - a) * y) / 16) & 0xF80000;
1662#endif
1663	return c;
1664}
1665
1666static inline unsigned _darken(unsigned color, int y) {
1667	unsigned c = 0;
1668	unsigned a;
1669#ifdef COLOR_16_BIT
1670	a = color & 0x1F;
1671	c |= (a - (a * y) / 16) & 0x1F;
1672
1673#ifdef COLOR_5_6_5
1674	a = color & 0x7C0;
1675	c |= (a - (a * y) / 16) & 0x7C0;
1676
1677	a = color & 0xF800;
1678	c |= (a - (a * y) / 16) & 0xF800;
1679#else
1680	a = color & 0x3E0;
1681	c |= (a - (a * y) / 16) & 0x3E0;
1682
1683	a = color & 0x7C00;
1684	c |= (a - (a * y) / 16) & 0x7C00;
1685#endif
1686#else
1687	a = color & 0xF8;
1688	c |= (a - (a * y) / 16) & 0xF8;
1689
1690	a = color & 0xF800;
1691	c |= (a - (a * y) / 16) & 0xF800;
1692
1693	a = color & 0xF80000;
1694	c |= (a - (a * y) / 16) & 0xF80000;
1695#endif
1696	return c;
1697}
1698
1699static unsigned _mix(int weightA, unsigned colorA, int weightB, unsigned colorB) {
1700	unsigned c = 0;
1701	unsigned a, b;
1702#ifdef COLOR_16_BIT
1703#ifdef COLOR_5_6_5
1704	a = colorA & 0xF81F;
1705	b = colorB & 0xF81F;
1706	a |= (colorA & 0x7C0) << 16;
1707	b |= (colorB & 0x7C0) << 16;
1708	c = ((a * weightA + b * weightB) / 16);
1709	if (c & 0x08000000) {
1710		c = (c & ~0x0FC00000) | 0x07C00000;
1711	}
1712	if (c & 0x0020) {
1713		c = (c & ~0x003F) | 0x001F;
1714	}
1715	if (c & 0x10000) {
1716		c = (c & ~0x1F800) | 0xF800;
1717	}
1718	c = (c & 0xF81F) | ((c >> 16) & 0x07C0);
1719#else
1720	a = colorA & 0x7C1F;
1721	b = colorB & 0x7C1F;
1722	a |= (colorA & 0x3E0) << 16;
1723	b |= (colorB & 0x3E0) << 16;
1724	c = ((a * weightA + b * weightB) / 16);
1725	if (c & 0x04000000) {
1726		c = (c & ~0x07E00000) | 0x03E00000;
1727	}
1728	if (c & 0x0020) {
1729		c = (c & ~0x003F) | 0x001F;
1730	}
1731	if (c & 0x10000) {
1732		c = (c & ~0x1F800) | 0xF800;
1733	}
1734	c = (c & 0x7C1F) | ((c >> 16) & 0x03E0);
1735#endif
1736#else
1737	a = colorA & 0xF8;
1738	b = colorB & 0xF8;
1739	c |= ((a * weightA + b * weightB) / 16) & 0x1F8;
1740	if (c & 0x00000100) {
1741		c = 0x000000F8;
1742	}
1743
1744	a = colorA & 0xF800;
1745	b = colorB & 0xF800;
1746	c |= ((a * weightA + b * weightB) / 16) & 0x1F800;
1747	if (c & 0x00010000) {
1748		c = (c & 0x000000F8) | 0x0000F800;
1749	}
1750
1751	a = colorA & 0xF80000;
1752	b = colorB & 0xF80000;
1753	c |= ((a * weightA + b * weightB) / 16) & 0x1F80000;
1754	if (c & 0x01000000) {
1755		c = (c & 0x0000F8F8) | 0x00F80000;
1756	}
1757#endif
1758	return c;
1759}