all repos — mgba @ c6556260955cb92d5f42e95d248e91c44515a915

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