all repos — mgba @ f26982e0139b313b670b82becb5e54cb87b05341

mGBA Game Boy Advance Emulator

src/gba/renderers/video-software.c (view raw)

  1/* Copyright (c) 2013-2015 Jeffrey Pfau
  2 *
  3 * This Source Code Form is subject to the terms of the Mozilla Public
  4 * License, v. 2.0. If a copy of the MPL was not distributed with this
  5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6#include "gba/renderers/software-private.h"
  7
  8#include <mgba/core/tile-cache.h>
  9#include <mgba/internal/arm/macros.h>
 10#include <mgba/internal/gba/io.h>
 11
 12#include <mgba-util/arm-algo.h>
 13#include <mgba-util/memory.h>
 14
 15static void GBAVideoSoftwareRendererInit(struct GBAVideoRenderer* renderer);
 16static void GBAVideoSoftwareRendererDeinit(struct GBAVideoRenderer* renderer);
 17static void GBAVideoSoftwareRendererReset(struct GBAVideoRenderer* renderer);
 18static void GBAVideoSoftwareRendererWriteVRAM(struct GBAVideoRenderer* renderer, uint32_t address);
 19static void GBAVideoSoftwareRendererWriteOAM(struct GBAVideoRenderer* renderer, uint32_t oam);
 20static void GBAVideoSoftwareRendererWritePalette(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value);
 21static uint16_t GBAVideoSoftwareRendererWriteVideoRegister(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value);
 22static void GBAVideoSoftwareRendererDrawScanline(struct GBAVideoRenderer* renderer, int y);
 23static void GBAVideoSoftwareRendererFinishFrame(struct GBAVideoRenderer* renderer);
 24static void GBAVideoSoftwareRendererGetPixels(struct GBAVideoRenderer* renderer, size_t* stride, const void** pixels);
 25static void GBAVideoSoftwareRendererPutPixels(struct GBAVideoRenderer* renderer, size_t stride, const void* pixels);
 26
 27static void GBAVideoSoftwareRendererUpdateDISPCNT(struct GBAVideoSoftwareRenderer* renderer);
 28static void GBAVideoSoftwareRendererWriteBGCNT(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* bg, uint16_t value);
 29static void GBAVideoSoftwareRendererWriteBGX_LO(struct GBAVideoSoftwareBackground* bg, uint16_t value);
 30static void GBAVideoSoftwareRendererWriteBGX_HI(struct GBAVideoSoftwareBackground* bg, uint16_t value);
 31static void GBAVideoSoftwareRendererWriteBGY_LO(struct GBAVideoSoftwareBackground* bg, uint16_t value);
 32static void GBAVideoSoftwareRendererWriteBGY_HI(struct GBAVideoSoftwareBackground* bg, uint16_t value);
 33static void GBAVideoSoftwareRendererWriteBLDCNT(struct GBAVideoSoftwareRenderer* renderer, uint16_t value);
 34
 35static void _cleanOAM(struct GBAVideoSoftwareRenderer* renderer);
 36static void _drawScanline(struct GBAVideoSoftwareRenderer* renderer, int y);
 37
 38static void _updatePalettes(struct GBAVideoSoftwareRenderer* renderer);
 39
 40static void _breakWindow(struct GBAVideoSoftwareRenderer* softwareRenderer, struct WindowN* win, int y);
 41static void _breakWindowInner(struct GBAVideoSoftwareRenderer* softwareRenderer, struct WindowN* win);
 42
 43void GBAVideoSoftwareRendererCreate(struct GBAVideoSoftwareRenderer* renderer) {
 44	renderer->d.init = GBAVideoSoftwareRendererInit;
 45	renderer->d.reset = GBAVideoSoftwareRendererReset;
 46	renderer->d.deinit = GBAVideoSoftwareRendererDeinit;
 47	renderer->d.writeVideoRegister = GBAVideoSoftwareRendererWriteVideoRegister;
 48	renderer->d.writeVRAM = GBAVideoSoftwareRendererWriteVRAM;
 49	renderer->d.writeOAM = GBAVideoSoftwareRendererWriteOAM;
 50	renderer->d.writePalette = GBAVideoSoftwareRendererWritePalette;
 51	renderer->d.drawScanline = GBAVideoSoftwareRendererDrawScanline;
 52	renderer->d.finishFrame = GBAVideoSoftwareRendererFinishFrame;
 53	renderer->d.getPixels = GBAVideoSoftwareRendererGetPixels;
 54	renderer->d.putPixels = GBAVideoSoftwareRendererPutPixels;
 55
 56	renderer->d.disableBG[0] = false;
 57	renderer->d.disableBG[1] = false;
 58	renderer->d.disableBG[2] = false;
 59	renderer->d.disableBG[3] = false;
 60	renderer->d.disableOBJ = false;
 61
 62	renderer->temporaryBuffer = 0;
 63}
 64
 65static void GBAVideoSoftwareRendererInit(struct GBAVideoRenderer* renderer) {
 66	GBAVideoSoftwareRendererReset(renderer);
 67
 68	struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
 69
 70	int y;
 71	for (y = 0; y < VIDEO_VERTICAL_PIXELS; ++y) {
 72		color_t* row = &softwareRenderer->outputBuffer[softwareRenderer->outputBufferStride * y];
 73		int x;
 74		for (x = 0; x < VIDEO_HORIZONTAL_PIXELS; ++x) {
 75			row[x] = GBA_COLOR_WHITE;
 76		}
 77	}
 78}
 79
 80static void GBAVideoSoftwareRendererReset(struct GBAVideoRenderer* renderer) {
 81	struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
 82	int i;
 83
 84	softwareRenderer->dispcnt = 0x0080;
 85
 86	softwareRenderer->target1Obj = 0;
 87	softwareRenderer->target1Bd = 0;
 88	softwareRenderer->target2Obj = 0;
 89	softwareRenderer->target2Bd = 0;
 90	softwareRenderer->blendEffect = BLEND_NONE;
 91	for (i = 0; i < 1024; i += 2) {
 92		uint16_t entry;
 93		LOAD_16(entry, i, softwareRenderer->d.palette);
 94		GBAVideoSoftwareRendererWritePalette(renderer, i, entry);
 95	}
 96	softwareRenderer->blendDirty = false;
 97	_updatePalettes(softwareRenderer);
 98
 99	softwareRenderer->blda = 0;
100	softwareRenderer->bldb = 0;
101	softwareRenderer->bldy = 0;
102
103	softwareRenderer->winN[0] = (struct WindowN) { .control = { .priority = 0 } };
104	softwareRenderer->winN[1] = (struct WindowN) { .control = { .priority = 1 } };
105	softwareRenderer->objwin = (struct WindowControl) { .priority = 2 };
106	softwareRenderer->winout = (struct WindowControl) { .priority = 3 };
107	softwareRenderer->oamMax = 0;
108
109	softwareRenderer->mosaic = 0;
110
111	memset(softwareRenderer->scanlineDirty, 0xFFFFFFFF, sizeof(softwareRenderer->scanlineDirty));
112	memset(softwareRenderer->ioCache, 0, sizeof(softwareRenderer->ioCache));
113
114	for (i = 0; i < 4; ++i) {
115		struct GBAVideoSoftwareBackground* bg = &softwareRenderer->bg[i];
116		bg->index = i;
117		bg->enabled = 0;
118		bg->priority = 0;
119		bg->charBase = 0;
120		bg->mosaic = 0;
121		bg->multipalette = 0;
122		bg->screenBase = 0;
123		bg->overflow = 0;
124		bg->size = 0;
125		bg->target1 = 0;
126		bg->target2 = 0;
127		bg->x = 0;
128		bg->y = 0;
129		bg->refx = 0;
130		bg->refy = 0;
131		bg->dx = 256;
132		bg->dmx = 0;
133		bg->dy = 0;
134		bg->dmy = 256;
135		bg->sx = 0;
136		bg->sy = 0;
137	}
138}
139
140static void GBAVideoSoftwareRendererDeinit(struct GBAVideoRenderer* renderer) {
141	struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
142	UNUSED(softwareRenderer);
143}
144
145static uint16_t GBAVideoSoftwareRendererWriteVideoRegister(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
146	struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
147	if (softwareRenderer->ioCache[address >> 1] == value) {
148		return value;
149	}
150	switch (address) {
151	case REG_DISPCNT:
152		softwareRenderer->dispcnt = value;
153		GBAVideoSoftwareRendererUpdateDISPCNT(softwareRenderer);
154		memset(softwareRenderer->scanlineDirty, 0xFFFFFFFF, sizeof(softwareRenderer->scanlineDirty));
155		break;
156	case REG_BG0CNT:
157		value &= 0xDFFF;
158		GBAVideoSoftwareRendererWriteBGCNT(softwareRenderer, &softwareRenderer->bg[0], value);
159		if (softwareRenderer->bg[0].enabled) {
160			memset(softwareRenderer->scanlineDirty, 0xFFFFFFFF, sizeof(softwareRenderer->scanlineDirty));
161		}
162		break;
163	case REG_BG1CNT:
164		value &= 0xDFFF;
165		GBAVideoSoftwareRendererWriteBGCNT(softwareRenderer, &softwareRenderer->bg[1], value);
166		if (softwareRenderer->bg[1].enabled) {
167			memset(softwareRenderer->scanlineDirty, 0xFFFFFFFF, sizeof(softwareRenderer->scanlineDirty));
168		}
169		break;
170	case REG_BG2CNT:
171		value &= 0xFFFF;
172		GBAVideoSoftwareRendererWriteBGCNT(softwareRenderer, &softwareRenderer->bg[2], value);
173		if (softwareRenderer->bg[2].enabled) {
174			memset(softwareRenderer->scanlineDirty, 0xFFFFFFFF, sizeof(softwareRenderer->scanlineDirty));
175		}
176		break;
177	case REG_BG3CNT:
178		value &= 0xFFFF;
179		GBAVideoSoftwareRendererWriteBGCNT(softwareRenderer, &softwareRenderer->bg[3], value);
180		if (softwareRenderer->bg[3].enabled) {
181			memset(softwareRenderer->scanlineDirty, 0xFFFFFFFF, sizeof(softwareRenderer->scanlineDirty));
182		}
183		break;
184	case REG_BG0HOFS:
185		value &= 0x01FF;
186		softwareRenderer->bg[0].x = value;
187		if (softwareRenderer->bg[0].enabled && GBARegisterDISPCNTGetMode(softwareRenderer->dispcnt) < 2) {
188			memset(softwareRenderer->scanlineDirty, 0xFFFFFFFF, sizeof(softwareRenderer->scanlineDirty));
189		}
190		break;
191	case REG_BG0VOFS:
192		value &= 0x01FF;
193		softwareRenderer->bg[0].y = value;
194		if (softwareRenderer->bg[0].enabled && GBARegisterDISPCNTGetMode(softwareRenderer->dispcnt) < 2) {
195			memset(softwareRenderer->scanlineDirty, 0xFFFFFFFF, sizeof(softwareRenderer->scanlineDirty));
196		}
197		break;
198	case REG_BG1HOFS:
199		value &= 0x01FF;
200		softwareRenderer->bg[1].x = value;
201		if (softwareRenderer->bg[1].enabled && GBARegisterDISPCNTGetMode(softwareRenderer->dispcnt) < 2) {
202			memset(softwareRenderer->scanlineDirty, 0xFFFFFFFF, sizeof(softwareRenderer->scanlineDirty));
203		}
204		break;
205	case REG_BG1VOFS:
206		value &= 0x01FF;
207		softwareRenderer->bg[1].y = value;
208		if (softwareRenderer->bg[1].enabled && GBARegisterDISPCNTGetMode(softwareRenderer->dispcnt) < 2) {
209			memset(softwareRenderer->scanlineDirty, 0xFFFFFFFF, sizeof(softwareRenderer->scanlineDirty));
210		}
211		break;
212	case REG_BG2HOFS:
213		value &= 0x01FF;
214		softwareRenderer->bg[2].x = value;
215		if (softwareRenderer->bg[2].enabled && GBARegisterDISPCNTGetMode(softwareRenderer->dispcnt) == 0) {
216			memset(softwareRenderer->scanlineDirty, 0xFFFFFFFF, sizeof(softwareRenderer->scanlineDirty));
217		}
218		break;
219	case REG_BG2VOFS:
220		value &= 0x01FF;
221		softwareRenderer->bg[2].y = value;
222		if (softwareRenderer->bg[2].enabled && GBARegisterDISPCNTGetMode(softwareRenderer->dispcnt) == 0) {
223			memset(softwareRenderer->scanlineDirty, 0xFFFFFFFF, sizeof(softwareRenderer->scanlineDirty));
224		}
225		break;
226	case REG_BG3HOFS:
227		value &= 0x01FF;
228		softwareRenderer->bg[3].x = value;
229		if (softwareRenderer->bg[3].enabled && GBARegisterDISPCNTGetMode(softwareRenderer->dispcnt) == 0) {
230			memset(softwareRenderer->scanlineDirty, 0xFFFFFFFF, sizeof(softwareRenderer->scanlineDirty));
231		}
232		break;
233	case REG_BG3VOFS:
234		value &= 0x01FF;
235		softwareRenderer->bg[3].y = value;
236		if (softwareRenderer->bg[2].enabled && GBARegisterDISPCNTGetMode(softwareRenderer->dispcnt) == 0) {
237			memset(softwareRenderer->scanlineDirty, 0xFFFFFFFF, sizeof(softwareRenderer->scanlineDirty));
238		}
239		break;
240	case REG_BG2PA:
241		softwareRenderer->bg[2].dx = value;
242		memset(softwareRenderer->scanlineDirty, 0xFFFFFFFF, sizeof(softwareRenderer->scanlineDirty));
243		break;
244	case REG_BG2PB:
245		softwareRenderer->bg[2].dmx = value;
246		memset(softwareRenderer->scanlineDirty, 0xFFFFFFFF, sizeof(softwareRenderer->scanlineDirty));
247		break;
248	case REG_BG2PC:
249		softwareRenderer->bg[2].dy = value;
250		memset(softwareRenderer->scanlineDirty, 0xFFFFFFFF, sizeof(softwareRenderer->scanlineDirty));
251		break;
252	case REG_BG2PD:
253		softwareRenderer->bg[2].dmy = value;
254		memset(softwareRenderer->scanlineDirty, 0xFFFFFFFF, sizeof(softwareRenderer->scanlineDirty));
255		break;
256	case REG_BG2X_LO:
257		GBAVideoSoftwareRendererWriteBGX_LO(&softwareRenderer->bg[2], value);
258		memset(softwareRenderer->scanlineDirty, 0xFFFFFFFF, sizeof(softwareRenderer->scanlineDirty));
259		break;
260	case REG_BG2X_HI:
261		GBAVideoSoftwareRendererWriteBGX_HI(&softwareRenderer->bg[2], value);
262		memset(softwareRenderer->scanlineDirty, 0xFFFFFFFF, sizeof(softwareRenderer->scanlineDirty));
263		break;
264	case REG_BG2Y_LO:
265		GBAVideoSoftwareRendererWriteBGY_LO(&softwareRenderer->bg[2], value);
266		memset(softwareRenderer->scanlineDirty, 0xFFFFFFFF, sizeof(softwareRenderer->scanlineDirty));
267		break;
268	case REG_BG2Y_HI:
269		GBAVideoSoftwareRendererWriteBGY_HI(&softwareRenderer->bg[2], value);
270		memset(softwareRenderer->scanlineDirty, 0xFFFFFFFF, sizeof(softwareRenderer->scanlineDirty));
271		break;
272	case REG_BG3PA:
273		softwareRenderer->bg[3].dx = value;
274		memset(softwareRenderer->scanlineDirty, 0xFFFFFFFF, sizeof(softwareRenderer->scanlineDirty));
275		break;
276	case REG_BG3PB:
277		softwareRenderer->bg[3].dmx = value;
278		memset(softwareRenderer->scanlineDirty, 0xFFFFFFFF, sizeof(softwareRenderer->scanlineDirty));
279		break;
280	case REG_BG3PC:
281		softwareRenderer->bg[3].dy = value;
282		memset(softwareRenderer->scanlineDirty, 0xFFFFFFFF, sizeof(softwareRenderer->scanlineDirty));
283		break;
284	case REG_BG3PD:
285		softwareRenderer->bg[3].dmy = value;
286		memset(softwareRenderer->scanlineDirty, 0xFFFFFFFF, sizeof(softwareRenderer->scanlineDirty));
287		break;
288	case REG_BG3X_LO:
289		GBAVideoSoftwareRendererWriteBGX_LO(&softwareRenderer->bg[3], value);
290		memset(softwareRenderer->scanlineDirty, 0xFFFFFFFF, sizeof(softwareRenderer->scanlineDirty));
291		break;
292	case REG_BG3X_HI:
293		GBAVideoSoftwareRendererWriteBGX_HI(&softwareRenderer->bg[3], value);
294		memset(softwareRenderer->scanlineDirty, 0xFFFFFFFF, sizeof(softwareRenderer->scanlineDirty));
295		break;
296	case REG_BG3Y_LO:
297		GBAVideoSoftwareRendererWriteBGY_LO(&softwareRenderer->bg[3], value);
298		memset(softwareRenderer->scanlineDirty, 0xFFFFFFFF, sizeof(softwareRenderer->scanlineDirty));
299		break;
300	case REG_BG3Y_HI:
301		GBAVideoSoftwareRendererWriteBGY_HI(&softwareRenderer->bg[3], value);
302		memset(softwareRenderer->scanlineDirty, 0xFFFFFFFF, sizeof(softwareRenderer->scanlineDirty));
303		break;
304	case REG_BLDCNT:
305		GBAVideoSoftwareRendererWriteBLDCNT(softwareRenderer, value);
306		value &= 0x3FFF;
307		break;
308	case REG_BLDALPHA:
309		softwareRenderer->blda = value & 0x1F;
310		if (softwareRenderer->blda > 0x10) {
311			softwareRenderer->blda = 0x10;
312		}
313		softwareRenderer->bldb = (value >> 8) & 0x1F;
314		if (softwareRenderer->bldb > 0x10) {
315			softwareRenderer->bldb = 0x10;
316		}
317		value &= 0x1F1F;
318		memset(softwareRenderer->scanlineDirty, 0xFFFFFFFF, sizeof(softwareRenderer->scanlineDirty));
319		break;
320	case REG_BLDY:
321		value &= 0x1F;
322		if (value > 0x10) {
323			value = 0x10;
324		}
325		if (softwareRenderer->bldy != value) {
326			softwareRenderer->bldy = value;
327			softwareRenderer->blendDirty = true;
328		}
329		memset(softwareRenderer->scanlineDirty, 0xFFFFFFFF, sizeof(softwareRenderer->scanlineDirty));
330		break;
331	case REG_WIN0H:
332		softwareRenderer->winN[0].h.end = value;
333		softwareRenderer->winN[0].h.start = value >> 8;
334		if (softwareRenderer->winN[0].h.start > VIDEO_HORIZONTAL_PIXELS && softwareRenderer->winN[0].h.start > softwareRenderer->winN[0].h.end) {
335			softwareRenderer->winN[0].h.start = 0;
336		}
337		if (softwareRenderer->winN[0].h.end > VIDEO_HORIZONTAL_PIXELS) {
338			softwareRenderer->winN[0].h.end = VIDEO_HORIZONTAL_PIXELS;
339			if (softwareRenderer->winN[0].h.start > VIDEO_HORIZONTAL_PIXELS) {
340				softwareRenderer->winN[0].h.start = VIDEO_HORIZONTAL_PIXELS;
341			}
342		}
343		memset(softwareRenderer->scanlineDirty, 0xFFFFFFFF, sizeof(softwareRenderer->scanlineDirty));
344		break;
345	case REG_WIN1H:
346		softwareRenderer->winN[1].h.end = value;
347		softwareRenderer->winN[1].h.start = value >> 8;
348		if (softwareRenderer->winN[1].h.start > VIDEO_HORIZONTAL_PIXELS && softwareRenderer->winN[1].h.start > softwareRenderer->winN[1].h.end) {
349			softwareRenderer->winN[1].h.start = 0;
350		}
351		if (softwareRenderer->winN[1].h.end > VIDEO_HORIZONTAL_PIXELS) {
352			softwareRenderer->winN[1].h.end = VIDEO_HORIZONTAL_PIXELS;
353			if (softwareRenderer->winN[1].h.start > VIDEO_HORIZONTAL_PIXELS) {
354				softwareRenderer->winN[1].h.start = VIDEO_HORIZONTAL_PIXELS;
355			}
356		}
357		memset(softwareRenderer->scanlineDirty, 0xFFFFFFFF, sizeof(softwareRenderer->scanlineDirty));
358		break;
359	case REG_WIN0V:
360		softwareRenderer->winN[0].v.end = value;
361		softwareRenderer->winN[0].v.start = value >> 8;
362		if (softwareRenderer->winN[0].v.start > VIDEO_VERTICAL_PIXELS && softwareRenderer->winN[0].v.start > softwareRenderer->winN[0].v.end) {
363			softwareRenderer->winN[0].v.start = 0;
364		}
365		if (softwareRenderer->winN[0].v.end > VIDEO_VERTICAL_PIXELS) {
366			softwareRenderer->winN[0].v.end = VIDEO_VERTICAL_PIXELS;
367			if (softwareRenderer->winN[0].v.start > VIDEO_VERTICAL_PIXELS) {
368				softwareRenderer->winN[0].v.start = VIDEO_VERTICAL_PIXELS;
369			}
370		}
371		memset(softwareRenderer->scanlineDirty, 0xFFFFFFFF, sizeof(softwareRenderer->scanlineDirty));
372		break;
373	case REG_WIN1V:
374		softwareRenderer->winN[1].v.end = value;
375		softwareRenderer->winN[1].v.start = value >> 8;
376		if (softwareRenderer->winN[1].v.start > VIDEO_VERTICAL_PIXELS && softwareRenderer->winN[1].v.start > softwareRenderer->winN[1].v.end) {
377			softwareRenderer->winN[1].v.start = 0;
378		}
379		if (softwareRenderer->winN[1].v.end > VIDEO_VERTICAL_PIXELS) {
380			softwareRenderer->winN[1].v.end = VIDEO_VERTICAL_PIXELS;
381			if (softwareRenderer->winN[1].v.start > VIDEO_VERTICAL_PIXELS) {
382				softwareRenderer->winN[1].v.start = VIDEO_VERTICAL_PIXELS;
383			}
384		}
385		memset(softwareRenderer->scanlineDirty, 0xFFFFFFFF, sizeof(softwareRenderer->scanlineDirty));
386		break;
387	case REG_WININ:
388		value &= 0x3F3F;
389		softwareRenderer->winN[0].control.packed = value;
390		softwareRenderer->winN[1].control.packed = value >> 8;
391		memset(softwareRenderer->scanlineDirty, 0xFFFFFFFF, sizeof(softwareRenderer->scanlineDirty));
392		break;
393	case REG_WINOUT:
394		value &= 0x3F3F;
395		softwareRenderer->winout.packed = value;
396		softwareRenderer->objwin.packed = value >> 8;
397		memset(softwareRenderer->scanlineDirty, 0xFFFFFFFF, sizeof(softwareRenderer->scanlineDirty));
398		break;
399	case REG_MOSAIC:
400		softwareRenderer->mosaic = value;
401		memset(softwareRenderer->scanlineDirty, 0xFFFFFFFF, sizeof(softwareRenderer->scanlineDirty));
402		break;
403	case REG_GREENSWP:
404		mLOG(GBA_VIDEO, STUB, "Stub video register write: 0x%03X", address);
405		break;
406	default:
407		mLOG(GBA_VIDEO, GAME_ERROR, "Invalid video register: 0x%03X", address);
408	}
409	softwareRenderer->ioCache[address >> 1] = value;
410	return value;
411}
412
413static void GBAVideoSoftwareRendererWriteVRAM(struct GBAVideoRenderer* renderer, uint32_t address) {
414	struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
415	if (renderer->cache) {
416		mTileCacheWriteVRAM(renderer->cache, address);
417	}
418	memset(softwareRenderer->scanlineDirty, 0xFFFFFFFF, sizeof(softwareRenderer->scanlineDirty));
419}
420
421static void GBAVideoSoftwareRendererWriteOAM(struct GBAVideoRenderer* renderer, uint32_t oam) {
422	struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
423	UNUSED(oam);
424	softwareRenderer->oamDirty = 1;
425	memset(softwareRenderer->scanlineDirty, 0xFFFFFFFF, sizeof(softwareRenderer->scanlineDirty));
426}
427
428static void GBAVideoSoftwareRendererWritePalette(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
429	struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
430	color_t color = mColorFrom555(value);
431	softwareRenderer->normalPalette[address >> 1] = color;
432	if (softwareRenderer->blendEffect == BLEND_BRIGHTEN) {
433		softwareRenderer->variantPalette[address >> 1] = _brighten(color, softwareRenderer->bldy);
434	} else if (softwareRenderer->blendEffect == BLEND_DARKEN) {
435		softwareRenderer->variantPalette[address >> 1] = _darken(color, softwareRenderer->bldy);
436	}
437	if (renderer->cache) {
438		mTileCacheWritePalette(renderer->cache, address);
439	}
440	memset(softwareRenderer->scanlineDirty, 0xFFFFFFFF, sizeof(softwareRenderer->scanlineDirty));
441}
442
443static void _breakWindow(struct GBAVideoSoftwareRenderer* softwareRenderer, struct WindowN* win, int y) {
444	if (win->v.end >= win->v.start) {
445		if (y >= win->v.end) {
446			return;
447		}
448		if (y < win->v.start) {
449			return;
450		}
451	} else if (y >= win->v.end && y < win->v.start) {
452		return;
453	}
454	if (win->h.end > VIDEO_HORIZONTAL_PIXELS || win->h.end < win->h.start) {
455		struct WindowN splits[2] = { *win, *win };
456		splits[0].h.start = 0;
457		splits[1].h.end = VIDEO_HORIZONTAL_PIXELS;
458		_breakWindowInner(softwareRenderer, &splits[0]);
459		_breakWindowInner(softwareRenderer, &splits[1]);
460	} else {
461		_breakWindowInner(softwareRenderer, win);
462	}
463}
464
465static void _breakWindowInner(struct GBAVideoSoftwareRenderer* softwareRenderer, struct WindowN* win) {
466	int activeWindow;
467	int startX = 0;
468	if (win->h.end > 0) {
469		for (activeWindow = 0; activeWindow < softwareRenderer->nWindows; ++activeWindow) {
470			if (win->h.start < softwareRenderer->windows[activeWindow].endX) {
471				// Insert a window before the end of the active window
472				struct Window oldWindow = softwareRenderer->windows[activeWindow];
473				if (win->h.start > startX) {
474					// And after the start of the active window
475					int nextWindow = softwareRenderer->nWindows;
476					++softwareRenderer->nWindows;
477					for (; nextWindow > activeWindow; --nextWindow) {
478						softwareRenderer->windows[nextWindow] = softwareRenderer->windows[nextWindow - 1];
479					}
480					softwareRenderer->windows[activeWindow].endX = win->h.start;
481					++activeWindow;
482				}
483				softwareRenderer->windows[activeWindow].control = win->control;
484				softwareRenderer->windows[activeWindow].endX = win->h.end;
485				if (win->h.end >= oldWindow.endX) {
486					// Trim off extra windows we've overwritten
487					for (++activeWindow; softwareRenderer->nWindows > activeWindow + 1 && win->h.end >= softwareRenderer->windows[activeWindow].endX; ++activeWindow) {
488						if (VIDEO_CHECKS && activeWindow >= MAX_WINDOW) {
489							mLOG(GBA_VIDEO, FATAL, "Out of bounds window write will occur");
490							return;
491						}
492						softwareRenderer->windows[activeWindow] = softwareRenderer->windows[activeWindow + 1];
493						--softwareRenderer->nWindows;
494					}
495				} else {
496					++activeWindow;
497					int nextWindow = softwareRenderer->nWindows;
498					++softwareRenderer->nWindows;
499					for (; nextWindow > activeWindow; --nextWindow) {
500						softwareRenderer->windows[nextWindow] = softwareRenderer->windows[nextWindow - 1];
501					}
502					softwareRenderer->windows[activeWindow] = oldWindow;
503				}
504				break;
505			}
506			startX = softwareRenderer->windows[activeWindow].endX;
507		}
508	}
509#ifdef DEBUG
510	if (softwareRenderer->nWindows > MAX_WINDOW) {
511		mLOG(GBA_VIDEO, FATAL, "Out of bounds window write occurred!");
512	}
513#endif
514}
515
516static void _cleanOAM(struct GBAVideoSoftwareRenderer* renderer) {
517	int i;
518	int oamMax = 0;
519	for (i = 0; i < 128; ++i) {
520		struct GBAObj obj;
521		LOAD_16(obj.a, 0, &renderer->d.oam->obj[i].a);
522		LOAD_16(obj.b, 0, &renderer->d.oam->obj[i].b);
523		LOAD_16(obj.c, 0, &renderer->d.oam->obj[i].c);
524		if (GBAObjAttributesAIsTransformed(obj.a) || !GBAObjAttributesAIsDisable(obj.a)) {
525			int height = GBAVideoObjSizes[GBAObjAttributesAGetShape(obj.a) * 4 + GBAObjAttributesBGetSize(obj.b)][1];
526			if (GBAObjAttributesAIsTransformed(obj.a)) {
527				height <<= GBAObjAttributesAGetDoubleSize(obj.a);
528			}
529			if (GBAObjAttributesAGetY(obj.a) < VIDEO_VERTICAL_PIXELS || GBAObjAttributesAGetY(obj.a) + height >= VIDEO_VERTICAL_TOTAL_PIXELS) {
530				renderer->sprites[oamMax].y = GBAObjAttributesAGetY(obj.a);
531				renderer->sprites[oamMax].endY = GBAObjAttributesAGetY(obj.a) + height;
532				renderer->sprites[oamMax].obj = obj;
533				++oamMax;
534			}
535		}
536	}
537	renderer->oamMax = oamMax;
538	renderer->oamDirty = 0;
539}
540
541static void GBAVideoSoftwareRendererDrawScanline(struct GBAVideoRenderer* renderer, int y) {
542	struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
543
544	if (!(softwareRenderer->scanlineDirty[y >> 5] & (1 << (y & 0x1F)))) {
545		if (GBARegisterDISPCNTGetMode(softwareRenderer->dispcnt) != 0) {
546			softwareRenderer->bg[2].sx += softwareRenderer->bg[2].dmx;
547			softwareRenderer->bg[2].sy += softwareRenderer->bg[2].dmy;
548			softwareRenderer->bg[3].sx += softwareRenderer->bg[3].dmx;
549			softwareRenderer->bg[3].sy += softwareRenderer->bg[3].dmy;
550		}
551		return;
552	}
553
554	color_t* row = &softwareRenderer->outputBuffer[softwareRenderer->outputBufferStride * y];
555	if (GBARegisterDISPCNTIsForcedBlank(softwareRenderer->dispcnt)) {
556		int x;
557		for (x = 0; x < VIDEO_HORIZONTAL_PIXELS; ++x) {
558			row[x] = GBA_COLOR_WHITE;
559		}
560		return;
561	}
562
563	int x;
564	for (x = 0; x < VIDEO_HORIZONTAL_PIXELS; x += 4) {
565		softwareRenderer->spriteLayer[x] = FLAG_UNWRITTEN;
566		softwareRenderer->spriteLayer[x + 1] = FLAG_UNWRITTEN;
567		softwareRenderer->spriteLayer[x + 2] = FLAG_UNWRITTEN;
568		softwareRenderer->spriteLayer[x + 3] = FLAG_UNWRITTEN;
569	}
570
571	softwareRenderer->windows[0].endX = VIDEO_HORIZONTAL_PIXELS;
572	softwareRenderer->nWindows = 1;
573	if (GBARegisterDISPCNTIsWin0Enable(softwareRenderer->dispcnt) || GBARegisterDISPCNTIsWin1Enable(softwareRenderer->dispcnt) || GBARegisterDISPCNTIsObjwinEnable(softwareRenderer->dispcnt)) {
574		softwareRenderer->windows[0].control = softwareRenderer->winout;
575		if (GBARegisterDISPCNTIsWin1Enable(softwareRenderer->dispcnt)) {
576			_breakWindow(softwareRenderer, &softwareRenderer->winN[1], y);
577		}
578		if (GBARegisterDISPCNTIsWin0Enable(softwareRenderer->dispcnt)) {
579			_breakWindow(softwareRenderer, &softwareRenderer->winN[0], y);
580		}
581	} else {
582		softwareRenderer->windows[0].control.packed = 0xFF;
583	}
584
585	GBAVideoSoftwareRendererUpdateDISPCNT(softwareRenderer);
586	if (softwareRenderer->blendDirty) {
587		_updatePalettes(softwareRenderer);
588		softwareRenderer->blendDirty = false;
589	}
590
591	int w;
592	x = 0;
593	for (w = 0; w < softwareRenderer->nWindows; ++w) {
594		// TOOD: handle objwin on backdrop
595		uint32_t backdrop = FLAG_UNWRITTEN | FLAG_PRIORITY | FLAG_IS_BACKGROUND;
596		if (!softwareRenderer->target1Bd || softwareRenderer->blendEffect == BLEND_NONE || softwareRenderer->blendEffect == BLEND_ALPHA || !GBAWindowControlIsBlendEnable(softwareRenderer->windows[w].control.packed)) {
597			backdrop |= softwareRenderer->normalPalette[0];
598		} else {
599			backdrop |= softwareRenderer->variantPalette[0];
600		}
601		int end = softwareRenderer->windows[w].endX;
602		for (; x < end - 3; x += 4) {
603			softwareRenderer->row[x] = backdrop;
604			softwareRenderer->row[x + 1] = backdrop;
605			softwareRenderer->row[x + 2] = backdrop;
606			softwareRenderer->row[x + 3] = backdrop;
607		}
608		for (; x < end; ++x) {
609			softwareRenderer->row[x] = backdrop;
610		}
611	}
612
613	_drawScanline(softwareRenderer, y);
614
615	if (softwareRenderer->target2Bd) {
616		x = 0;
617		for (w = 0; w < softwareRenderer->nWindows; ++w) {
618			uint32_t backdrop = 0;
619			if (!softwareRenderer->target1Bd || softwareRenderer->blendEffect == BLEND_NONE || softwareRenderer->blendEffect == BLEND_ALPHA || !GBAWindowControlIsBlendEnable(softwareRenderer->windows[w].control.packed)) {
620				backdrop |= softwareRenderer->normalPalette[0];
621			} else {
622				backdrop |= softwareRenderer->variantPalette[0];
623			}
624			int end = softwareRenderer->windows[w].endX;
625			for (; x < end; ++x) {
626				uint32_t color = softwareRenderer->row[x];
627				if (color & FLAG_TARGET_1) {
628					softwareRenderer->row[x] = _mix(softwareRenderer->bldb, backdrop, softwareRenderer->blda, color);
629				}
630			}
631		}
632	}
633	if (softwareRenderer->target1Obj && (softwareRenderer->blendEffect == BLEND_DARKEN || softwareRenderer->blendEffect == BLEND_BRIGHTEN)) {
634		x = 0;
635		for (w = 0; w < softwareRenderer->nWindows; ++w) {
636			if (!GBAWindowControlIsBlendEnable(softwareRenderer->windows[w].control.packed)) {
637				continue;
638			}
639			int end = softwareRenderer->windows[w].endX;
640			if (softwareRenderer->blendEffect == BLEND_DARKEN) {
641				for (; x < end; ++x) {
642					uint32_t color = softwareRenderer->row[x];
643					if ((color & 0xFF000000) == FLAG_REBLEND) {
644						softwareRenderer->row[x] = _darken(color, softwareRenderer->bldy);
645					}
646				}
647			} else if (softwareRenderer->blendEffect == BLEND_BRIGHTEN) {
648				for (; x < end; ++x) {
649					uint32_t color = softwareRenderer->row[x];
650					if ((color & 0xFF000000) == FLAG_REBLEND) {
651						softwareRenderer->row[x] = _brighten(color, softwareRenderer->bldy);
652					}
653				}
654			}
655		}
656	}
657
658#ifdef COLOR_16_BIT
659#if defined(__ARM_NEON) && !defined(__APPLE__)
660	_to16Bit(row, softwareRenderer->row, VIDEO_HORIZONTAL_PIXELS);
661#else
662	for (x = 0; x < VIDEO_HORIZONTAL_PIXELS; ++x) {
663		row[x] = softwareRenderer->row[x];
664	}
665#endif
666#else
667	memcpy(row, softwareRenderer->row, VIDEO_HORIZONTAL_PIXELS * sizeof(*row));
668#endif
669	softwareRenderer->scanlineDirty[y >> 5] &= ~(1 << (y & 0x1F));
670}
671
672static void GBAVideoSoftwareRendererFinishFrame(struct GBAVideoRenderer* renderer) {
673	struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
674
675	if (softwareRenderer->temporaryBuffer) {
676		mappedMemoryFree(softwareRenderer->temporaryBuffer, VIDEO_HORIZONTAL_PIXELS * VIDEO_VERTICAL_PIXELS * 4);
677		softwareRenderer->temporaryBuffer = 0;
678	}
679	softwareRenderer->bg[2].sx = softwareRenderer->bg[2].refx;
680	softwareRenderer->bg[2].sy = softwareRenderer->bg[2].refy;
681	softwareRenderer->bg[3].sx = softwareRenderer->bg[3].refx;
682	softwareRenderer->bg[3].sy = softwareRenderer->bg[3].refy;
683}
684
685static void GBAVideoSoftwareRendererGetPixels(struct GBAVideoRenderer* renderer, size_t* stride, const void** pixels) {
686	struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
687	*stride = softwareRenderer->outputBufferStride;
688	*pixels = softwareRenderer->outputBuffer;
689}
690
691static void GBAVideoSoftwareRendererPutPixels(struct GBAVideoRenderer* renderer, size_t stride, const void* pixels) {
692	struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
693
694	const color_t* colorPixels = pixels;
695	unsigned i;
696	for (i = 0; i < VIDEO_VERTICAL_PIXELS; ++i) {
697		memmove(&softwareRenderer->outputBuffer[softwareRenderer->outputBufferStride * i], &colorPixels[stride * i], VIDEO_HORIZONTAL_PIXELS * BYTES_PER_PIXEL);
698	}
699}
700
701static void GBAVideoSoftwareRendererUpdateDISPCNT(struct GBAVideoSoftwareRenderer* renderer) {
702	renderer->bg[0].enabled = GBARegisterDISPCNTGetBg0Enable(renderer->dispcnt) && !renderer->d.disableBG[0];
703	renderer->bg[1].enabled = GBARegisterDISPCNTGetBg1Enable(renderer->dispcnt) && !renderer->d.disableBG[1];
704	renderer->bg[2].enabled = GBARegisterDISPCNTGetBg2Enable(renderer->dispcnt) && !renderer->d.disableBG[2];
705	renderer->bg[3].enabled = GBARegisterDISPCNTGetBg3Enable(renderer->dispcnt) && !renderer->d.disableBG[3];
706}
707
708static void GBAVideoSoftwareRendererWriteBGCNT(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* bg, uint16_t value) {
709	UNUSED(renderer);
710	bg->priority = GBARegisterBGCNTGetPriority(value);
711	bg->charBase = GBARegisterBGCNTGetCharBase(value) << 14;
712	bg->mosaic = GBARegisterBGCNTGetMosaic(value);
713	bg->multipalette = GBARegisterBGCNTGet256Color(value);
714	bg->screenBase = GBARegisterBGCNTGetScreenBase(value) << 11;
715	bg->overflow = GBARegisterBGCNTGetOverflow(value);
716	bg->size = GBARegisterBGCNTGetSize(value);
717}
718
719static void GBAVideoSoftwareRendererWriteBGX_LO(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
720	bg->refx = (bg->refx & 0xFFFF0000) | value;
721	bg->sx = bg->refx;
722}
723
724static void GBAVideoSoftwareRendererWriteBGX_HI(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
725	bg->refx = (bg->refx & 0x0000FFFF) | (value << 16);
726	bg->refx <<= 4;
727	bg->refx >>= 4;
728	bg->sx = bg->refx;
729}
730
731static void GBAVideoSoftwareRendererWriteBGY_LO(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
732	bg->refy = (bg->refy & 0xFFFF0000) | value;
733	bg->sy = bg->refy;
734}
735
736static void GBAVideoSoftwareRendererWriteBGY_HI(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
737	bg->refy = (bg->refy & 0x0000FFFF) | (value << 16);
738	bg->refy <<= 4;
739	bg->refy >>= 4;
740	bg->sy = bg->refy;
741}
742
743static void GBAVideoSoftwareRendererWriteBLDCNT(struct GBAVideoSoftwareRenderer* renderer, uint16_t value) {
744	enum BlendEffect oldEffect = renderer->blendEffect;
745
746	renderer->bg[0].target1 = GBARegisterBLDCNTGetTarget1Bg0(value);
747	renderer->bg[1].target1 = GBARegisterBLDCNTGetTarget1Bg1(value);
748	renderer->bg[2].target1 = GBARegisterBLDCNTGetTarget1Bg2(value);
749	renderer->bg[3].target1 = GBARegisterBLDCNTGetTarget1Bg3(value);
750	renderer->bg[0].target2 = GBARegisterBLDCNTGetTarget2Bg0(value);
751	renderer->bg[1].target2 = GBARegisterBLDCNTGetTarget2Bg1(value);
752	renderer->bg[2].target2 = GBARegisterBLDCNTGetTarget2Bg2(value);
753	renderer->bg[3].target2 = GBARegisterBLDCNTGetTarget2Bg3(value);
754
755	renderer->blendEffect = GBARegisterBLDCNTGetEffect(value);
756	renderer->target1Obj = GBARegisterBLDCNTGetTarget1Obj(value);
757	renderer->target1Bd = GBARegisterBLDCNTGetTarget1Bd(value);
758	renderer->target2Obj = GBARegisterBLDCNTGetTarget2Obj(value);
759	renderer->target2Bd = GBARegisterBLDCNTGetTarget2Bd(value);
760
761	if (oldEffect != renderer->blendEffect) {
762		renderer->blendDirty = true;
763	}
764}
765
766#define TEST_LAYER_ENABLED(X) \
767	(renderer->bg[X].enabled && \
768	(GBAWindowControlIsBg ## X ## Enable(renderer->currentWindow.packed) || \
769	(GBARegisterDISPCNTIsObjwinEnable(renderer->dispcnt) && GBAWindowControlIsBg ## X ## Enable (renderer->objwin.packed))) && \
770	renderer->bg[X].priority == priority)
771
772static void _drawScanline(struct GBAVideoSoftwareRenderer* renderer, int y) {
773	int w;
774	renderer->end = 0;
775	int spriteLayers = 0;
776	if (GBARegisterDISPCNTIsObjEnable(renderer->dispcnt) && !renderer->d.disableOBJ) {
777		if (renderer->oamDirty) {
778			_cleanOAM(renderer);
779		}
780		renderer->spriteCyclesRemaining = GBARegisterDISPCNTIsHblankIntervalFree(renderer->dispcnt) ? OBJ_HBLANK_FREE_LENGTH : OBJ_LENGTH;
781		int mosaicV = GBAMosaicControlGetObjV(renderer->mosaic) + 1;
782		int mosaicY = y - (y % mosaicV);
783		for (w = 0; w < renderer->nWindows; ++w) {
784			renderer->start = renderer->end;
785			renderer->end = renderer->windows[w].endX;
786			renderer->currentWindow = renderer->windows[w].control;
787			if (!GBAWindowControlIsObjEnable(renderer->currentWindow.packed) && !GBARegisterDISPCNTIsObjwinEnable(renderer->dispcnt)) {
788				continue;
789			}
790			int i;
791			int drawn;
792
793			for (i = 0; i < renderer->oamMax; ++i) {
794				int localY = y;
795				if (renderer->spriteCyclesRemaining <= 0) {
796					break;
797				}
798				struct GBAVideoSoftwareSprite* sprite = &renderer->sprites[i];
799				if (GBAObjAttributesAIsMosaic(sprite->obj.a)) {
800					localY = mosaicY;
801				}
802				if ((localY < sprite->y && (sprite->endY - 256 < 0 || localY >= sprite->endY - 256)) || localY >= sprite->endY) {
803					continue;
804				}
805				drawn = GBAVideoSoftwareRendererPreprocessSprite(renderer, &sprite->obj, localY);
806				spriteLayers |= drawn << GBAObjAttributesCGetPriority(sprite->obj.c);
807			}
808		}
809	}
810
811	unsigned priority;
812	for (priority = 0; priority < 4; ++priority) {
813		renderer->end = 0;
814		for (w = 0; w < renderer->nWindows; ++w) {
815			renderer->start = renderer->end;
816			renderer->end = renderer->windows[w].endX;
817			renderer->currentWindow = renderer->windows[w].control;
818			if (spriteLayers & (1 << priority)) {
819				GBAVideoSoftwareRendererPostprocessSprite(renderer, priority);
820			}
821			if (TEST_LAYER_ENABLED(0) && GBARegisterDISPCNTGetMode(renderer->dispcnt) < 2) {
822				GBAVideoSoftwareRendererDrawBackgroundMode0(renderer, &renderer->bg[0], y);
823			}
824			if (TEST_LAYER_ENABLED(1) && GBARegisterDISPCNTGetMode(renderer->dispcnt) < 2) {
825				GBAVideoSoftwareRendererDrawBackgroundMode0(renderer, &renderer->bg[1], y);
826			}
827			if (TEST_LAYER_ENABLED(2)) {
828				switch (GBARegisterDISPCNTGetMode(renderer->dispcnt)) {
829				case 0:
830					GBAVideoSoftwareRendererDrawBackgroundMode0(renderer, &renderer->bg[2], y);
831					break;
832				case 1:
833				case 2:
834					GBAVideoSoftwareRendererDrawBackgroundMode2(renderer, &renderer->bg[2], y);
835					break;
836				case 3:
837					GBAVideoSoftwareRendererDrawBackgroundMode3(renderer, &renderer->bg[2], y);
838					break;
839				case 4:
840					GBAVideoSoftwareRendererDrawBackgroundMode4(renderer, &renderer->bg[2], y);
841					break;
842				case 5:
843					GBAVideoSoftwareRendererDrawBackgroundMode5(renderer, &renderer->bg[2], y);
844					break;
845				}
846			}
847			if (TEST_LAYER_ENABLED(3)) {
848				switch (GBARegisterDISPCNTGetMode(renderer->dispcnt)) {
849				case 0:
850					GBAVideoSoftwareRendererDrawBackgroundMode0(renderer, &renderer->bg[3], y);
851					break;
852				case 2:
853					GBAVideoSoftwareRendererDrawBackgroundMode2(renderer, &renderer->bg[3], y);
854					break;
855				}
856			}
857		}
858	}
859	if (GBARegisterDISPCNTGetMode(renderer->dispcnt) != 0) {
860		renderer->bg[2].sx += renderer->bg[2].dmx;
861		renderer->bg[2].sy += renderer->bg[2].dmy;
862		renderer->bg[3].sx += renderer->bg[3].dmx;
863		renderer->bg[3].sy += renderer->bg[3].dmy;
864	}
865}
866
867static void _updatePalettes(struct GBAVideoSoftwareRenderer* renderer) {
868	int i;
869	if (renderer->blendEffect == BLEND_BRIGHTEN) {
870		for (i = 0; i < 512; ++i) {
871			renderer->variantPalette[i] = _brighten(renderer->normalPalette[i], renderer->bldy);
872		}
873	} else if (renderer->blendEffect == BLEND_DARKEN) {
874		for (i = 0; i < 512; ++i) {
875			renderer->variantPalette[i] = _darken(renderer->normalPalette[i], renderer->bldy);
876		}
877	} else {
878		for (i = 0; i < 512; ++i) {
879			renderer->variantPalette[i] = renderer->normalPalette[i];
880		}
881	}
882}