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