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