all repos — mgba @ 610982fdcf357d7699a1a9c02e5f4d2e71d8a4f3

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