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