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