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 softwareRenderer->cache[y].scale[0][0] = softwareRenderer->bg[2].sx;
543 softwareRenderer->cache[y].scale[0][1] = softwareRenderer->bg[2].sy;
544 softwareRenderer->cache[y].scale[1][0] = softwareRenderer->bg[3].sx;
545 softwareRenderer->cache[y].scale[1][1] = softwareRenderer->bg[3].sy;
546
547 if (!dirty) {
548 if (GBARegisterDISPCNTGetMode(softwareRenderer->dispcnt) != 0) {
549 softwareRenderer->bg[2].sx += softwareRenderer->bg[2].dmx;
550 softwareRenderer->bg[2].sy += softwareRenderer->bg[2].dmy;
551 softwareRenderer->bg[3].sx += softwareRenderer->bg[3].dmx;
552 softwareRenderer->bg[3].sy += softwareRenderer->bg[3].dmy;
553 }
554 return;
555 }
556
557 CLEAN_SCANLINE(softwareRenderer, y);
558
559 color_t* row = &softwareRenderer->outputBuffer[softwareRenderer->outputBufferStride * y];
560 if (GBARegisterDISPCNTIsForcedBlank(softwareRenderer->dispcnt)) {
561 int x;
562 for (x = 0; x < VIDEO_HORIZONTAL_PIXELS; ++x) {
563 row[x] = GBA_COLOR_WHITE;
564 }
565 return;
566 }
567
568 int x;
569 for (x = 0; x < VIDEO_HORIZONTAL_PIXELS; x += 4) {
570 softwareRenderer->spriteLayer[x] = FLAG_UNWRITTEN;
571 softwareRenderer->spriteLayer[x + 1] = FLAG_UNWRITTEN;
572 softwareRenderer->spriteLayer[x + 2] = FLAG_UNWRITTEN;
573 softwareRenderer->spriteLayer[x + 3] = FLAG_UNWRITTEN;
574 }
575
576 softwareRenderer->windows[0].endX = VIDEO_HORIZONTAL_PIXELS;
577 softwareRenderer->nWindows = 1;
578 if (GBARegisterDISPCNTIsWin0Enable(softwareRenderer->dispcnt) || GBARegisterDISPCNTIsWin1Enable(softwareRenderer->dispcnt) || GBARegisterDISPCNTIsObjwinEnable(softwareRenderer->dispcnt)) {
579 softwareRenderer->windows[0].control = softwareRenderer->winout;
580 if (GBARegisterDISPCNTIsWin1Enable(softwareRenderer->dispcnt)) {
581 _breakWindow(softwareRenderer, &softwareRenderer->winN[1], y);
582 }
583 if (GBARegisterDISPCNTIsWin0Enable(softwareRenderer->dispcnt)) {
584 _breakWindow(softwareRenderer, &softwareRenderer->winN[0], y);
585 }
586 } else {
587 softwareRenderer->windows[0].control.packed = 0xFF;
588 }
589
590 if (softwareRenderer->blendDirty) {
591 _updatePalettes(softwareRenderer);
592 softwareRenderer->blendDirty = false;
593 }
594
595 int w;
596 x = 0;
597 for (w = 0; w < softwareRenderer->nWindows; ++w) {
598 // TOOD: handle objwin on backdrop
599 uint32_t backdrop = FLAG_UNWRITTEN | FLAG_PRIORITY | FLAG_IS_BACKGROUND;
600 if (!softwareRenderer->target1Bd || softwareRenderer->blendEffect == BLEND_NONE || softwareRenderer->blendEffect == BLEND_ALPHA || !GBAWindowControlIsBlendEnable(softwareRenderer->windows[w].control.packed)) {
601 backdrop |= softwareRenderer->normalPalette[0];
602 } else {
603 backdrop |= softwareRenderer->variantPalette[0];
604 }
605 int end = softwareRenderer->windows[w].endX;
606 for (; x < end - 3; x += 4) {
607 softwareRenderer->row[x] = backdrop;
608 softwareRenderer->row[x + 1] = backdrop;
609 softwareRenderer->row[x + 2] = backdrop;
610 softwareRenderer->row[x + 3] = backdrop;
611 }
612 for (; x < end; ++x) {
613 softwareRenderer->row[x] = backdrop;
614 }
615 }
616
617 _drawScanline(softwareRenderer, y);
618
619 if (softwareRenderer->target2Bd) {
620 x = 0;
621 for (w = 0; w < softwareRenderer->nWindows; ++w) {
622 uint32_t backdrop = 0;
623 if (!softwareRenderer->target1Bd || softwareRenderer->blendEffect == BLEND_NONE || softwareRenderer->blendEffect == BLEND_ALPHA || !GBAWindowControlIsBlendEnable(softwareRenderer->windows[w].control.packed)) {
624 backdrop |= softwareRenderer->normalPalette[0];
625 } else {
626 backdrop |= softwareRenderer->variantPalette[0];
627 }
628 int end = softwareRenderer->windows[w].endX;
629 for (; x < end; ++x) {
630 uint32_t color = softwareRenderer->row[x];
631 if (color & FLAG_TARGET_1) {
632 softwareRenderer->row[x] = _mix(softwareRenderer->bldb, backdrop, softwareRenderer->blda, color);
633 }
634 }
635 }
636 }
637 if (softwareRenderer->target1Obj && (softwareRenderer->blendEffect == BLEND_DARKEN || softwareRenderer->blendEffect == BLEND_BRIGHTEN)) {
638 x = 0;
639 uint32_t mask = FLAG_REBLEND | FLAG_TARGET_1 | FLAG_IS_BACKGROUND;
640 uint32_t match = FLAG_REBLEND;
641 if (GBARegisterDISPCNTIsObjwinEnable(softwareRenderer->dispcnt)) {
642 mask |= FLAG_OBJWIN;
643 if (GBAWindowControlIsBlendEnable(softwareRenderer->objwin.packed)) {
644 match |= FLAG_OBJWIN;
645 }
646 }
647 for (w = 0; w < softwareRenderer->nWindows; ++w) {
648 if (!GBAWindowControlIsBlendEnable(softwareRenderer->windows[w].control.packed)) {
649 continue;
650 }
651 int end = softwareRenderer->windows[w].endX;
652 if (softwareRenderer->blendEffect == BLEND_DARKEN) {
653 for (; x < end; ++x) {
654 uint32_t color = softwareRenderer->row[x];
655 if ((color & mask) == match) {
656 softwareRenderer->row[x] = _darken(color, softwareRenderer->bldy);
657 }
658 }
659 } else if (softwareRenderer->blendEffect == BLEND_BRIGHTEN) {
660 for (; x < end; ++x) {
661 uint32_t color = softwareRenderer->row[x];
662 if ((color & mask) == match) {
663 softwareRenderer->row[x] = _brighten(color, softwareRenderer->bldy);
664 }
665 }
666 }
667 }
668 }
669
670#ifdef COLOR_16_BIT
671 for (x = 0; x < VIDEO_HORIZONTAL_PIXELS; x += 4) {
672 row[x] = softwareRenderer->row[x];
673 row[x + 1] = softwareRenderer->row[x + 1];
674 row[x + 2] = softwareRenderer->row[x + 2];
675 row[x + 3] = softwareRenderer->row[x + 3];
676 }
677#else
678 memcpy(row, softwareRenderer->row, VIDEO_HORIZONTAL_PIXELS * sizeof(*row));
679#endif
680}
681
682static void GBAVideoSoftwareRendererFinishFrame(struct GBAVideoRenderer* renderer) {
683 struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
684
685 softwareRenderer->nextY = 0;
686 if (softwareRenderer->temporaryBuffer) {
687 mappedMemoryFree(softwareRenderer->temporaryBuffer, VIDEO_HORIZONTAL_PIXELS * VIDEO_VERTICAL_PIXELS * 4);
688 softwareRenderer->temporaryBuffer = 0;
689 }
690 softwareRenderer->bg[2].sx = softwareRenderer->bg[2].refx;
691 softwareRenderer->bg[2].sy = softwareRenderer->bg[2].refy;
692 softwareRenderer->bg[3].sx = softwareRenderer->bg[3].refx;
693 softwareRenderer->bg[3].sy = softwareRenderer->bg[3].refy;
694
695 if (softwareRenderer->bg[0].enabled > 0) {
696 softwareRenderer->bg[0].enabled = 4;
697 }
698 if (softwareRenderer->bg[1].enabled > 0) {
699 softwareRenderer->bg[1].enabled = 4;
700 }
701 if (softwareRenderer->bg[2].enabled > 0) {
702 softwareRenderer->bg[2].enabled = 4;
703 }
704 if (softwareRenderer->bg[3].enabled > 0) {
705 softwareRenderer->bg[3].enabled = 4;
706 }
707}
708
709static void GBAVideoSoftwareRendererGetPixels(struct GBAVideoRenderer* renderer, size_t* stride, const void** pixels) {
710 struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
711 *stride = softwareRenderer->outputBufferStride;
712 *pixels = softwareRenderer->outputBuffer;
713}
714
715static void GBAVideoSoftwareRendererPutPixels(struct GBAVideoRenderer* renderer, size_t stride, const void* pixels) {
716 struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
717
718 const color_t* colorPixels = pixels;
719 unsigned i;
720 for (i = 0; i < VIDEO_VERTICAL_PIXELS; ++i) {
721 memmove(&softwareRenderer->outputBuffer[softwareRenderer->outputBufferStride * i], &colorPixels[stride * i], VIDEO_HORIZONTAL_PIXELS * BYTES_PER_PIXEL);
722 }
723}
724
725static void _enableBg(struct GBAVideoSoftwareRenderer* renderer, int bg, bool active) {
726 int wasActive = renderer->bg[bg].enabled;
727 if (!active) {
728 renderer->bg[bg].enabled = 0;
729 } else if (!wasActive && active) {
730 if (renderer->nextY == 0) {
731 renderer->bg[bg].enabled = 4;
732 } else {
733 renderer->bg[bg].enabled = 1;
734 }
735 }
736}
737
738static void GBAVideoSoftwareRendererUpdateDISPCNT(struct GBAVideoSoftwareRenderer* renderer) {
739 _enableBg(renderer, 0, GBARegisterDISPCNTGetBg0Enable(renderer->dispcnt));
740 _enableBg(renderer, 1, GBARegisterDISPCNTGetBg1Enable(renderer->dispcnt));
741 _enableBg(renderer, 2, GBARegisterDISPCNTGetBg2Enable(renderer->dispcnt));
742 _enableBg(renderer, 3, GBARegisterDISPCNTGetBg3Enable(renderer->dispcnt));
743}
744
745static void GBAVideoSoftwareRendererWriteBGCNT(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* bg, uint16_t value) {
746 UNUSED(renderer);
747 bg->priority = GBARegisterBGCNTGetPriority(value);
748 bg->charBase = GBARegisterBGCNTGetCharBase(value) << 14;
749 bg->mosaic = GBARegisterBGCNTGetMosaic(value);
750 bg->multipalette = GBARegisterBGCNTGet256Color(value);
751 bg->screenBase = GBARegisterBGCNTGetScreenBase(value) << 11;
752 bg->overflow = GBARegisterBGCNTGetOverflow(value);
753 bg->size = GBARegisterBGCNTGetSize(value);
754}
755
756static void GBAVideoSoftwareRendererWriteBGX_LO(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
757 bg->refx = (bg->refx & 0xFFFF0000) | value;
758 bg->sx = bg->refx;
759}
760
761static void GBAVideoSoftwareRendererWriteBGX_HI(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
762 bg->refx = (bg->refx & 0x0000FFFF) | (value << 16);
763 bg->refx <<= 4;
764 bg->refx >>= 4;
765 bg->sx = bg->refx;
766}
767
768static void GBAVideoSoftwareRendererWriteBGY_LO(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
769 bg->refy = (bg->refy & 0xFFFF0000) | value;
770 bg->sy = bg->refy;
771}
772
773static void GBAVideoSoftwareRendererWriteBGY_HI(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
774 bg->refy = (bg->refy & 0x0000FFFF) | (value << 16);
775 bg->refy <<= 4;
776 bg->refy >>= 4;
777 bg->sy = bg->refy;
778}
779
780static void GBAVideoSoftwareRendererWriteBLDCNT(struct GBAVideoSoftwareRenderer* renderer, uint16_t value) {
781 enum BlendEffect oldEffect = renderer->blendEffect;
782
783 renderer->bg[0].target1 = GBARegisterBLDCNTGetTarget1Bg0(value);
784 renderer->bg[1].target1 = GBARegisterBLDCNTGetTarget1Bg1(value);
785 renderer->bg[2].target1 = GBARegisterBLDCNTGetTarget1Bg2(value);
786 renderer->bg[3].target1 = GBARegisterBLDCNTGetTarget1Bg3(value);
787 renderer->bg[0].target2 = GBARegisterBLDCNTGetTarget2Bg0(value);
788 renderer->bg[1].target2 = GBARegisterBLDCNTGetTarget2Bg1(value);
789 renderer->bg[2].target2 = GBARegisterBLDCNTGetTarget2Bg2(value);
790 renderer->bg[3].target2 = GBARegisterBLDCNTGetTarget2Bg3(value);
791
792 renderer->blendEffect = GBARegisterBLDCNTGetEffect(value);
793 renderer->target1Obj = GBARegisterBLDCNTGetTarget1Obj(value);
794 renderer->target1Bd = GBARegisterBLDCNTGetTarget1Bd(value);
795 renderer->target2Obj = GBARegisterBLDCNTGetTarget2Obj(value);
796 renderer->target2Bd = GBARegisterBLDCNTGetTarget2Bd(value);
797
798 if (oldEffect != renderer->blendEffect) {
799 renderer->blendDirty = true;
800 }
801}
802
803#define TEST_LAYER_ENABLED(X) \
804 !renderer->d.disableBG[X] && \
805 (renderer->bg[X].enabled == 4 && \
806 (GBAWindowControlIsBg ## X ## Enable(renderer->currentWindow.packed) || \
807 (GBARegisterDISPCNTIsObjwinEnable(renderer->dispcnt) && GBAWindowControlIsBg ## X ## Enable (renderer->objwin.packed))) && \
808 renderer->bg[X].priority == priority)
809
810static void _drawScanline(struct GBAVideoSoftwareRenderer* renderer, int y) {
811 int w;
812 int spriteLayers = 0;
813 if (GBARegisterDISPCNTIsObjEnable(renderer->dispcnt) && !renderer->d.disableOBJ) {
814 if (renderer->oamDirty) {
815 _cleanOAM(renderer);
816 }
817 renderer->spriteCyclesRemaining = GBARegisterDISPCNTIsHblankIntervalFree(renderer->dispcnt) ? OBJ_HBLANK_FREE_LENGTH : OBJ_LENGTH;
818 int mosaicV = GBAMosaicControlGetObjV(renderer->mosaic) + 1;
819 int mosaicY = y - (y % mosaicV);
820 int i;
821 for (i = 0; i < renderer->oamMax; ++i) {
822 struct GBAVideoSoftwareSprite* sprite = &renderer->sprites[i];
823 int localY = y;
824 renderer->end = 0;
825 if (GBAObjAttributesAIsMosaic(sprite->obj.a)) {
826 localY = mosaicY;
827 }
828 if ((localY < sprite->y && (sprite->endY - 256 < 0 || localY >= sprite->endY - 256)) || localY >= sprite->endY) {
829 continue;
830 }
831 for (w = 0; w < renderer->nWindows; ++w) {
832 if (renderer->spriteCyclesRemaining <= 0) {
833 break;
834 }
835 renderer->currentWindow = renderer->windows[w].control;
836 renderer->start = renderer->end;
837 renderer->end = renderer->windows[w].endX;
838 if (!GBAWindowControlIsObjEnable(renderer->currentWindow.packed) && !GBARegisterDISPCNTIsObjwinEnable(renderer->dispcnt)) {
839 continue;
840 }
841
842 int drawn = GBAVideoSoftwareRendererPreprocessSprite(renderer, &sprite->obj, localY);
843 spriteLayers |= drawn << GBAObjAttributesCGetPriority(sprite->obj.c);
844 }
845 if (renderer->spriteCyclesRemaining <= 0) {
846 break;
847 }
848 }
849 }
850
851 unsigned priority;
852 for (priority = 0; priority < 4; ++priority) {
853 renderer->end = 0;
854 for (w = 0; w < renderer->nWindows; ++w) {
855 renderer->start = renderer->end;
856 renderer->end = renderer->windows[w].endX;
857 renderer->currentWindow = renderer->windows[w].control;
858 if (spriteLayers & (1 << priority)) {
859 GBAVideoSoftwareRendererPostprocessSprite(renderer, priority);
860 }
861 if (TEST_LAYER_ENABLED(0) && GBARegisterDISPCNTGetMode(renderer->dispcnt) < 2) {
862 GBAVideoSoftwareRendererDrawBackgroundMode0(renderer, &renderer->bg[0], y);
863 }
864 if (TEST_LAYER_ENABLED(1) && GBARegisterDISPCNTGetMode(renderer->dispcnt) < 2) {
865 GBAVideoSoftwareRendererDrawBackgroundMode0(renderer, &renderer->bg[1], y);
866 }
867 if (TEST_LAYER_ENABLED(2)) {
868 switch (GBARegisterDISPCNTGetMode(renderer->dispcnt)) {
869 case 0:
870 GBAVideoSoftwareRendererDrawBackgroundMode0(renderer, &renderer->bg[2], y);
871 break;
872 case 1:
873 case 2:
874 GBAVideoSoftwareRendererDrawBackgroundMode2(renderer, &renderer->bg[2], y);
875 break;
876 case 3:
877 GBAVideoSoftwareRendererDrawBackgroundMode3(renderer, &renderer->bg[2], y);
878 break;
879 case 4:
880 GBAVideoSoftwareRendererDrawBackgroundMode4(renderer, &renderer->bg[2], y);
881 break;
882 case 5:
883 GBAVideoSoftwareRendererDrawBackgroundMode5(renderer, &renderer->bg[2], y);
884 break;
885 }
886 }
887 if (TEST_LAYER_ENABLED(3)) {
888 switch (GBARegisterDISPCNTGetMode(renderer->dispcnt)) {
889 case 0:
890 GBAVideoSoftwareRendererDrawBackgroundMode0(renderer, &renderer->bg[3], y);
891 break;
892 case 2:
893 GBAVideoSoftwareRendererDrawBackgroundMode2(renderer, &renderer->bg[3], y);
894 break;
895 }
896 }
897 }
898 }
899 if (GBARegisterDISPCNTGetMode(renderer->dispcnt) != 0) {
900 renderer->bg[2].sx += renderer->bg[2].dmx;
901 renderer->bg[2].sy += renderer->bg[2].dmy;
902 renderer->bg[3].sx += renderer->bg[3].dmx;
903 renderer->bg[3].sy += renderer->bg[3].dmy;
904 }
905
906 if (renderer->bg[0].enabled > 0 && renderer->bg[0].enabled < 4) {
907 ++renderer->bg[0].enabled;
908 DIRTY_SCANLINE(renderer, y);
909 }
910 if (renderer->bg[1].enabled > 0 && renderer->bg[1].enabled < 4) {
911 ++renderer->bg[1].enabled;
912 DIRTY_SCANLINE(renderer, y);
913 }
914 if (renderer->bg[2].enabled > 0 && renderer->bg[2].enabled < 4) {
915 ++renderer->bg[2].enabled;
916 DIRTY_SCANLINE(renderer, y);
917 }
918 if (renderer->bg[3].enabled > 0 && renderer->bg[3].enabled < 4) {
919 ++renderer->bg[3].enabled;
920 DIRTY_SCANLINE(renderer, y);
921 }
922}
923
924static void _updatePalettes(struct GBAVideoSoftwareRenderer* renderer) {
925 int i;
926 if (renderer->blendEffect == BLEND_BRIGHTEN) {
927 for (i = 0; i < 512; ++i) {
928 renderer->variantPalette[i] = _brighten(renderer->normalPalette[i], renderer->bldy);
929 }
930 } else if (renderer->blendEffect == BLEND_DARKEN) {
931 for (i = 0; i < 512; ++i) {
932 renderer->variantPalette[i] = _darken(renderer->normalPalette[i], renderer->bldy);
933 }
934 } else {
935 for (i = 0; i < 512; ++i) {
936 renderer->variantPalette[i] = renderer->normalPalette[i];
937 }
938 }
939}