all repos — mgba @ 2122ff1f43a4113a36b8a3e571c6f4ac008ea312

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