all repos — mgba @ b762ce41a03841fe1e76c072d4c232d452290c8d

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