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 color_t color = mColorFrom555(value);
363 softwareRenderer->normalPalette[address >> 1] = color;
364 if (softwareRenderer->blendEffect == BLEND_BRIGHTEN) {
365 softwareRenderer->variantPalette[address >> 1] = _brighten(color, softwareRenderer->bldy);
366 } else if (softwareRenderer->blendEffect == BLEND_DARKEN) {
367 softwareRenderer->variantPalette[address >> 1] = _darken(color, softwareRenderer->bldy);
368 }
369 if (renderer->cache) {
370 mTileCacheWritePalette(renderer->cache, address);
371 }
372}
373
374static void _breakWindow(struct GBAVideoSoftwareRenderer* softwareRenderer, struct WindowN* win, int y) {
375 if (win->v.end >= win->v.start) {
376 if (y >= win->v.end) {
377 return;
378 }
379 if (y < win->v.start) {
380 return;
381 }
382 } else if (y >= win->v.end && y < win->v.start) {
383 return;
384 }
385 if (win->h.end > VIDEO_HORIZONTAL_PIXELS || win->h.end < win->h.start) {
386 struct WindowN splits[2] = { *win, *win };
387 splits[0].h.start = 0;
388 splits[1].h.end = VIDEO_HORIZONTAL_PIXELS;
389 _breakWindowInner(softwareRenderer, &splits[0]);
390 _breakWindowInner(softwareRenderer, &splits[1]);
391 } else {
392 _breakWindowInner(softwareRenderer, win);
393 }
394}
395
396static void _breakWindowInner(struct GBAVideoSoftwareRenderer* softwareRenderer, struct WindowN* win) {
397 int activeWindow;
398 int startX = 0;
399 if (win->h.end > 0) {
400 for (activeWindow = 0; activeWindow < softwareRenderer->nWindows; ++activeWindow) {
401 if (win->h.start < softwareRenderer->windows[activeWindow].endX) {
402 // Insert a window before the end of the active window
403 struct Window oldWindow = softwareRenderer->windows[activeWindow];
404 if (win->h.start > startX) {
405 // And after the start of the active window
406 int nextWindow = softwareRenderer->nWindows;
407 ++softwareRenderer->nWindows;
408 for (; nextWindow > activeWindow; --nextWindow) {
409 softwareRenderer->windows[nextWindow] = softwareRenderer->windows[nextWindow - 1];
410 }
411 softwareRenderer->windows[activeWindow].endX = win->h.start;
412 ++activeWindow;
413 }
414 softwareRenderer->windows[activeWindow].control = win->control;
415 softwareRenderer->windows[activeWindow].endX = win->h.end;
416 if (win->h.end >= oldWindow.endX) {
417 // Trim off extra windows we've overwritten
418 for (++activeWindow; softwareRenderer->nWindows > activeWindow + 1 && win->h.end >= softwareRenderer->windows[activeWindow].endX; ++activeWindow) {
419 if (VIDEO_CHECKS && activeWindow >= MAX_WINDOW) {
420 mLOG(GBA_VIDEO, FATAL, "Out of bounds window write will occur");
421 return;
422 }
423 softwareRenderer->windows[activeWindow] = softwareRenderer->windows[activeWindow + 1];
424 --softwareRenderer->nWindows;
425 }
426 } else {
427 ++activeWindow;
428 int nextWindow = softwareRenderer->nWindows;
429 ++softwareRenderer->nWindows;
430 for (; nextWindow > activeWindow; --nextWindow) {
431 softwareRenderer->windows[nextWindow] = softwareRenderer->windows[nextWindow - 1];
432 }
433 softwareRenderer->windows[activeWindow] = oldWindow;
434 }
435 break;
436 }
437 startX = softwareRenderer->windows[activeWindow].endX;
438 }
439 }
440#ifdef DEBUG
441 if (softwareRenderer->nWindows > MAX_WINDOW) {
442 mLOG(GBA_VIDEO, FATAL, "Out of bounds window write occurred!");
443 }
444#endif
445}
446
447static void _cleanOAM(struct GBAVideoSoftwareRenderer* renderer) {
448 int i;
449 int oamMax = 0;
450 for (i = 0; i < 128; ++i) {
451 struct GBAObj obj;
452 LOAD_16(obj.a, 0, &renderer->d.oam->obj[i].a);
453 LOAD_16(obj.b, 0, &renderer->d.oam->obj[i].b);
454 LOAD_16(obj.c, 0, &renderer->d.oam->obj[i].c);
455 if (GBAObjAttributesAIsTransformed(obj.a) || !GBAObjAttributesAIsDisable(obj.a)) {
456 int height = GBAVideoObjSizes[GBAObjAttributesAGetShape(obj.a) * 4 + GBAObjAttributesBGetSize(obj.b)][1];
457 if (GBAObjAttributesAIsTransformed(obj.a)) {
458 height <<= GBAObjAttributesAGetDoubleSize(obj.a);
459 }
460 if (GBAObjAttributesAGetY(obj.a) < VIDEO_VERTICAL_PIXELS || GBAObjAttributesAGetY(obj.a) + height >= VIDEO_VERTICAL_TOTAL_PIXELS) {
461 renderer->sprites[oamMax].y = GBAObjAttributesAGetY(obj.a);
462 renderer->sprites[oamMax].endY = GBAObjAttributesAGetY(obj.a) + height;
463 renderer->sprites[oamMax].obj = obj;
464 ++oamMax;
465 }
466 }
467 }
468 renderer->oamMax = oamMax;
469 renderer->oamDirty = 0;
470}
471
472static void GBAVideoSoftwareRendererDrawScanline(struct GBAVideoRenderer* renderer, int y) {
473 struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
474
475 color_t* row = &softwareRenderer->outputBuffer[softwareRenderer->outputBufferStride * y];
476 if (GBARegisterDISPCNTIsForcedBlank(softwareRenderer->dispcnt)) {
477 int x;
478 for (x = 0; x < VIDEO_HORIZONTAL_PIXELS; ++x) {
479 row[x] = GBA_COLOR_WHITE;
480 }
481 return;
482 }
483
484 int x;
485 for (x = 0; x < VIDEO_HORIZONTAL_PIXELS; x += 4) {
486 softwareRenderer->spriteLayer[x] = FLAG_UNWRITTEN;
487 softwareRenderer->spriteLayer[x + 1] = FLAG_UNWRITTEN;
488 softwareRenderer->spriteLayer[x + 2] = FLAG_UNWRITTEN;
489 softwareRenderer->spriteLayer[x + 3] = FLAG_UNWRITTEN;
490 }
491
492 softwareRenderer->windows[0].endX = VIDEO_HORIZONTAL_PIXELS;
493 softwareRenderer->nWindows = 1;
494 if (GBARegisterDISPCNTIsWin0Enable(softwareRenderer->dispcnt) || GBARegisterDISPCNTIsWin1Enable(softwareRenderer->dispcnt) || GBARegisterDISPCNTIsObjwinEnable(softwareRenderer->dispcnt)) {
495 softwareRenderer->windows[0].control = softwareRenderer->winout;
496 if (GBARegisterDISPCNTIsWin1Enable(softwareRenderer->dispcnt)) {
497 _breakWindow(softwareRenderer, &softwareRenderer->winN[1], y);
498 }
499 if (GBARegisterDISPCNTIsWin0Enable(softwareRenderer->dispcnt)) {
500 _breakWindow(softwareRenderer, &softwareRenderer->winN[0], y);
501 }
502 } else {
503 softwareRenderer->windows[0].control.packed = 0xFF;
504 }
505
506 GBAVideoSoftwareRendererUpdateDISPCNT(softwareRenderer);
507 if (softwareRenderer->blendDirty) {
508 _updatePalettes(softwareRenderer);
509 softwareRenderer->blendDirty = false;
510 }
511
512 int w;
513 x = 0;
514 for (w = 0; w < softwareRenderer->nWindows; ++w) {
515 // TOOD: handle objwin on backdrop
516 uint32_t backdrop = FLAG_UNWRITTEN | FLAG_PRIORITY | FLAG_IS_BACKGROUND;
517 if (!softwareRenderer->target1Bd || softwareRenderer->blendEffect == BLEND_NONE || softwareRenderer->blendEffect == BLEND_ALPHA || !GBAWindowControlIsBlendEnable(softwareRenderer->windows[w].control.packed)) {
518 backdrop |= softwareRenderer->normalPalette[0];
519 } else {
520 backdrop |= softwareRenderer->variantPalette[0];
521 }
522 int end = softwareRenderer->windows[w].endX;
523 for (; x < end - 3; x += 4) {
524 softwareRenderer->row[x] = backdrop;
525 softwareRenderer->row[x + 1] = backdrop;
526 softwareRenderer->row[x + 2] = backdrop;
527 softwareRenderer->row[x + 3] = backdrop;
528 }
529 for (; x < end; ++x) {
530 softwareRenderer->row[x] = backdrop;
531 }
532 }
533
534 _drawScanline(softwareRenderer, y);
535
536 if (softwareRenderer->target2Bd) {
537 x = 0;
538 for (w = 0; w < softwareRenderer->nWindows; ++w) {
539 uint32_t backdrop = 0;
540 if (!softwareRenderer->target1Bd || softwareRenderer->blendEffect == BLEND_NONE || softwareRenderer->blendEffect == BLEND_ALPHA || !GBAWindowControlIsBlendEnable(softwareRenderer->windows[w].control.packed)) {
541 backdrop |= softwareRenderer->normalPalette[0];
542 } else {
543 backdrop |= softwareRenderer->variantPalette[0];
544 }
545 int end = softwareRenderer->windows[w].endX;
546 for (; x < end; ++x) {
547 uint32_t color = softwareRenderer->row[x];
548 if (color & FLAG_TARGET_1) {
549 softwareRenderer->row[x] = _mix(softwareRenderer->bldb, backdrop, softwareRenderer->blda, color);
550 }
551 }
552 }
553 }
554 if (softwareRenderer->target1Obj && (softwareRenderer->blendEffect == BLEND_DARKEN || softwareRenderer->blendEffect == BLEND_BRIGHTEN)) {
555 x = 0;
556 for (w = 0; w < softwareRenderer->nWindows; ++w) {
557 if (!GBAWindowControlIsBlendEnable(softwareRenderer->windows[w].control.packed)) {
558 continue;
559 }
560 int end = softwareRenderer->windows[w].endX;
561 if (softwareRenderer->blendEffect == BLEND_DARKEN) {
562 for (; x < end; ++x) {
563 uint32_t color = softwareRenderer->row[x];
564 if ((color & 0xFF000000) == FLAG_REBLEND) {
565 softwareRenderer->row[x] = _darken(color, softwareRenderer->bldy);
566 }
567 }
568 } else if (softwareRenderer->blendEffect == BLEND_BRIGHTEN) {
569 for (; x < end; ++x) {
570 uint32_t color = softwareRenderer->row[x];
571 if ((color & 0xFF000000) == FLAG_REBLEND) {
572 softwareRenderer->row[x] = _brighten(color, softwareRenderer->bldy);
573 }
574 }
575 }
576 }
577 }
578
579#ifdef COLOR_16_BIT
580#if defined(__ARM_NEON) && !defined(__APPLE__)
581 _to16Bit(row, softwareRenderer->row, VIDEO_HORIZONTAL_PIXELS);
582#else
583 for (x = 0; x < VIDEO_HORIZONTAL_PIXELS; ++x) {
584 row[x] = softwareRenderer->row[x];
585 }
586#endif
587#else
588 memcpy(row, softwareRenderer->row, VIDEO_HORIZONTAL_PIXELS * sizeof(*row));
589#endif
590}
591
592static void GBAVideoSoftwareRendererFinishFrame(struct GBAVideoRenderer* renderer) {
593 struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
594
595 if (softwareRenderer->temporaryBuffer) {
596 mappedMemoryFree(softwareRenderer->temporaryBuffer, VIDEO_HORIZONTAL_PIXELS * VIDEO_VERTICAL_PIXELS * 4);
597 softwareRenderer->temporaryBuffer = 0;
598 }
599 softwareRenderer->bg[2].sx = softwareRenderer->bg[2].refx;
600 softwareRenderer->bg[2].sy = softwareRenderer->bg[2].refy;
601 softwareRenderer->bg[3].sx = softwareRenderer->bg[3].refx;
602 softwareRenderer->bg[3].sy = softwareRenderer->bg[3].refy;
603}
604
605static void GBAVideoSoftwareRendererGetPixels(struct GBAVideoRenderer* renderer, size_t* stride, const void** pixels) {
606 struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
607 *stride = softwareRenderer->outputBufferStride;
608 *pixels = softwareRenderer->outputBuffer;
609}
610
611static void GBAVideoSoftwareRendererPutPixels(struct GBAVideoRenderer* renderer, size_t stride, const void* pixels) {
612 struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
613
614 const color_t* colorPixels = pixels;
615 unsigned i;
616 for (i = 0; i < VIDEO_VERTICAL_PIXELS; ++i) {
617 memmove(&softwareRenderer->outputBuffer[softwareRenderer->outputBufferStride * i], &colorPixels[stride * i], VIDEO_HORIZONTAL_PIXELS * BYTES_PER_PIXEL);
618 }
619}
620
621static void GBAVideoSoftwareRendererUpdateDISPCNT(struct GBAVideoSoftwareRenderer* renderer) {
622 renderer->bg[0].enabled = GBARegisterDISPCNTGetBg0Enable(renderer->dispcnt) && !renderer->d.disableBG[0];
623 renderer->bg[1].enabled = GBARegisterDISPCNTGetBg1Enable(renderer->dispcnt) && !renderer->d.disableBG[1];
624 renderer->bg[2].enabled = GBARegisterDISPCNTGetBg2Enable(renderer->dispcnt) && !renderer->d.disableBG[2];
625 renderer->bg[3].enabled = GBARegisterDISPCNTGetBg3Enable(renderer->dispcnt) && !renderer->d.disableBG[3];
626}
627
628static void GBAVideoSoftwareRendererWriteBGCNT(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* bg, uint16_t value) {
629 UNUSED(renderer);
630 bg->priority = GBARegisterBGCNTGetPriority(value);
631 bg->charBase = GBARegisterBGCNTGetCharBase(value) << 14;
632 bg->mosaic = GBARegisterBGCNTGetMosaic(value);
633 bg->multipalette = GBARegisterBGCNTGet256Color(value);
634 bg->screenBase = GBARegisterBGCNTGetScreenBase(value) << 11;
635 bg->overflow = GBARegisterBGCNTGetOverflow(value);
636 bg->size = GBARegisterBGCNTGetSize(value);
637}
638
639static void GBAVideoSoftwareRendererWriteBGPA(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
640 bg->dx = value;
641}
642
643static void GBAVideoSoftwareRendererWriteBGPB(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
644 bg->dmx = value;
645}
646
647static void GBAVideoSoftwareRendererWriteBGPC(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
648 bg->dy = value;
649}
650
651static void GBAVideoSoftwareRendererWriteBGPD(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
652 bg->dmy = value;
653}
654
655static void GBAVideoSoftwareRendererWriteBGX_LO(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
656 bg->refx = (bg->refx & 0xFFFF0000) | value;
657 bg->sx = bg->refx;
658}
659
660static void GBAVideoSoftwareRendererWriteBGX_HI(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
661 bg->refx = (bg->refx & 0x0000FFFF) | (value << 16);
662 bg->refx <<= 4;
663 bg->refx >>= 4;
664 bg->sx = bg->refx;
665}
666
667static void GBAVideoSoftwareRendererWriteBGY_LO(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
668 bg->refy = (bg->refy & 0xFFFF0000) | value;
669 bg->sy = bg->refy;
670}
671
672static void GBAVideoSoftwareRendererWriteBGY_HI(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
673 bg->refy = (bg->refy & 0x0000FFFF) | (value << 16);
674 bg->refy <<= 4;
675 bg->refy >>= 4;
676 bg->sy = bg->refy;
677}
678
679static void GBAVideoSoftwareRendererWriteBLDCNT(struct GBAVideoSoftwareRenderer* renderer, uint16_t value) {
680 enum BlendEffect oldEffect = renderer->blendEffect;
681
682 renderer->bg[0].target1 = GBARegisterBLDCNTGetTarget1Bg0(value);
683 renderer->bg[1].target1 = GBARegisterBLDCNTGetTarget1Bg1(value);
684 renderer->bg[2].target1 = GBARegisterBLDCNTGetTarget1Bg2(value);
685 renderer->bg[3].target1 = GBARegisterBLDCNTGetTarget1Bg3(value);
686 renderer->bg[0].target2 = GBARegisterBLDCNTGetTarget2Bg0(value);
687 renderer->bg[1].target2 = GBARegisterBLDCNTGetTarget2Bg1(value);
688 renderer->bg[2].target2 = GBARegisterBLDCNTGetTarget2Bg2(value);
689 renderer->bg[3].target2 = GBARegisterBLDCNTGetTarget2Bg3(value);
690
691 renderer->blendEffect = GBARegisterBLDCNTGetEffect(value);
692 renderer->target1Obj = GBARegisterBLDCNTGetTarget1Obj(value);
693 renderer->target1Bd = GBARegisterBLDCNTGetTarget1Bd(value);
694 renderer->target2Obj = GBARegisterBLDCNTGetTarget2Obj(value);
695 renderer->target2Bd = GBARegisterBLDCNTGetTarget2Bd(value);
696
697 if (oldEffect != renderer->blendEffect) {
698 renderer->blendDirty = true;
699 }
700}
701
702#define TEST_LAYER_ENABLED(X) \
703 (renderer->bg[X].enabled && \
704 (GBAWindowControlIsBg ## X ## Enable(renderer->currentWindow.packed) || \
705 (GBARegisterDISPCNTIsObjwinEnable(renderer->dispcnt) && GBAWindowControlIsBg ## X ## Enable (renderer->objwin.packed))) && \
706 renderer->bg[X].priority == priority)
707
708static void _drawScanline(struct GBAVideoSoftwareRenderer* renderer, int y) {
709 int w;
710 renderer->end = 0;
711 int spriteLayers = 0;
712 if (GBARegisterDISPCNTIsObjEnable(renderer->dispcnt) && !renderer->d.disableOBJ) {
713 if (renderer->oamDirty) {
714 _cleanOAM(renderer);
715 }
716 renderer->spriteCyclesRemaining = GBARegisterDISPCNTIsHblankIntervalFree(renderer->dispcnt) ? OBJ_HBLANK_FREE_LENGTH : OBJ_LENGTH;
717 int mosaicV = GBAMosaicControlGetObjV(renderer->mosaic) + 1;
718 int mosaicY = y - (y % mosaicV);
719 for (w = 0; w < renderer->nWindows; ++w) {
720 renderer->start = renderer->end;
721 renderer->end = renderer->windows[w].endX;
722 renderer->currentWindow = renderer->windows[w].control;
723 if (!GBAWindowControlIsObjEnable(renderer->currentWindow.packed) && !GBARegisterDISPCNTIsObjwinEnable(renderer->dispcnt)) {
724 continue;
725 }
726 int i;
727 int drawn;
728
729 for (i = 0; i < renderer->oamMax; ++i) {
730 int localY = y;
731 if (renderer->spriteCyclesRemaining <= 0) {
732 break;
733 }
734 struct GBAVideoSoftwareSprite* sprite = &renderer->sprites[i];
735 if (GBAObjAttributesAIsMosaic(sprite->obj.a)) {
736 localY = mosaicY;
737 }
738 if ((localY < sprite->y && (sprite->endY - 256 < 0 || localY >= sprite->endY - 256)) || localY >= sprite->endY) {
739 continue;
740 }
741 drawn = GBAVideoSoftwareRendererPreprocessSprite(renderer, &sprite->obj, localY);
742 spriteLayers |= drawn << GBAObjAttributesCGetPriority(sprite->obj.c);
743 }
744 }
745 }
746
747 unsigned priority;
748 for (priority = 0; priority < 4; ++priority) {
749 renderer->end = 0;
750 for (w = 0; w < renderer->nWindows; ++w) {
751 renderer->start = renderer->end;
752 renderer->end = renderer->windows[w].endX;
753 renderer->currentWindow = renderer->windows[w].control;
754 if (spriteLayers & (1 << priority)) {
755 GBAVideoSoftwareRendererPostprocessSprite(renderer, priority);
756 }
757 if (TEST_LAYER_ENABLED(0) && GBARegisterDISPCNTGetMode(renderer->dispcnt) < 2) {
758 GBAVideoSoftwareRendererDrawBackgroundMode0(renderer, &renderer->bg[0], y);
759 }
760 if (TEST_LAYER_ENABLED(1) && GBARegisterDISPCNTGetMode(renderer->dispcnt) < 2) {
761 GBAVideoSoftwareRendererDrawBackgroundMode0(renderer, &renderer->bg[1], y);
762 }
763 if (TEST_LAYER_ENABLED(2)) {
764 switch (GBARegisterDISPCNTGetMode(renderer->dispcnt)) {
765 case 0:
766 GBAVideoSoftwareRendererDrawBackgroundMode0(renderer, &renderer->bg[2], y);
767 break;
768 case 1:
769 case 2:
770 GBAVideoSoftwareRendererDrawBackgroundMode2(renderer, &renderer->bg[2], y);
771 break;
772 case 3:
773 GBAVideoSoftwareRendererDrawBackgroundMode3(renderer, &renderer->bg[2], y);
774 break;
775 case 4:
776 GBAVideoSoftwareRendererDrawBackgroundMode4(renderer, &renderer->bg[2], y);
777 break;
778 case 5:
779 GBAVideoSoftwareRendererDrawBackgroundMode5(renderer, &renderer->bg[2], y);
780 break;
781 }
782 }
783 if (TEST_LAYER_ENABLED(3)) {
784 switch (GBARegisterDISPCNTGetMode(renderer->dispcnt)) {
785 case 0:
786 GBAVideoSoftwareRendererDrawBackgroundMode0(renderer, &renderer->bg[3], y);
787 break;
788 case 2:
789 GBAVideoSoftwareRendererDrawBackgroundMode2(renderer, &renderer->bg[3], y);
790 break;
791 }
792 }
793 }
794 }
795 if (GBARegisterDISPCNTGetMode(renderer->dispcnt) != 0) {
796 renderer->bg[2].sx += renderer->bg[2].dmx;
797 renderer->bg[2].sy += renderer->bg[2].dmy;
798 renderer->bg[3].sx += renderer->bg[3].dmx;
799 renderer->bg[3].sy += renderer->bg[3].dmy;
800 }
801}
802
803static void _updatePalettes(struct GBAVideoSoftwareRenderer* renderer) {
804 int i;
805 if (renderer->blendEffect == BLEND_BRIGHTEN) {
806 for (i = 0; i < 512; ++i) {
807 renderer->variantPalette[i] = _brighten(renderer->normalPalette[i], renderer->bldy);
808 }
809 } else if (renderer->blendEffect == BLEND_DARKEN) {
810 for (i = 0; i < 512; ++i) {
811 renderer->variantPalette[i] = _darken(renderer->normalPalette[i], renderer->bldy);
812 }
813 } else {
814 for (i = 0; i < 512; ++i) {
815 renderer->variantPalette[i] = renderer->normalPalette[i];
816 }
817 }
818}