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 "software-private.h"
7
8#include "core/tile-cache.h"
9#include "gba/gba.h"
10#include "gba/io.h"
11#include "gba/renderers/tile-cache.h"
12
13#include "util/arm-algo.h"
14#include "util/memory.h"
15
16static void GBAVideoSoftwareRendererInit(struct GBAVideoRenderer* renderer);
17static void GBAVideoSoftwareRendererDeinit(struct GBAVideoRenderer* renderer);
18static void GBAVideoSoftwareRendererReset(struct GBAVideoRenderer* renderer);
19static void GBAVideoSoftwareRendererWriteVRAM(struct GBAVideoRenderer* renderer, uint32_t address);
20static void GBAVideoSoftwareRendererWriteOAM(struct GBAVideoRenderer* renderer, uint32_t oam);
21static void GBAVideoSoftwareRendererWritePalette(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value);
22static uint16_t GBAVideoSoftwareRendererWriteVideoRegister(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value);
23static void GBAVideoSoftwareRendererDrawScanline(struct GBAVideoRenderer* renderer, int y);
24static void GBAVideoSoftwareRendererFinishFrame(struct GBAVideoRenderer* renderer);
25static void GBAVideoSoftwareRendererGetPixels(struct GBAVideoRenderer* renderer, size_t* stride, const void** pixels);
26static void GBAVideoSoftwareRendererPutPixels(struct GBAVideoRenderer* renderer, size_t stride, const void* pixels);
27
28static void GBAVideoSoftwareRendererUpdateDISPCNT(struct GBAVideoSoftwareRenderer* renderer);
29static void GBAVideoSoftwareRendererWriteBGCNT(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* bg, uint16_t value);
30static void GBAVideoSoftwareRendererWriteBGPA(struct GBAVideoSoftwareBackground* bg, uint16_t value);
31static void GBAVideoSoftwareRendererWriteBGPB(struct GBAVideoSoftwareBackground* bg, uint16_t value);
32static void GBAVideoSoftwareRendererWriteBGPC(struct GBAVideoSoftwareBackground* bg, uint16_t value);
33static void GBAVideoSoftwareRendererWriteBGPD(struct GBAVideoSoftwareBackground* bg, uint16_t value);
34static void GBAVideoSoftwareRendererWriteBGX_LO(struct GBAVideoSoftwareBackground* bg, uint16_t value);
35static void GBAVideoSoftwareRendererWriteBGX_HI(struct GBAVideoSoftwareBackground* bg, uint16_t value);
36static void GBAVideoSoftwareRendererWriteBGY_LO(struct GBAVideoSoftwareBackground* bg, uint16_t value);
37static void GBAVideoSoftwareRendererWriteBGY_HI(struct GBAVideoSoftwareBackground* bg, uint16_t value);
38static void GBAVideoSoftwareRendererWriteBLDCNT(struct GBAVideoSoftwareRenderer* renderer, uint16_t value);
39
40static void _cleanOAM(struct GBAVideoSoftwareRenderer* renderer);
41static void _drawScanline(struct GBAVideoSoftwareRenderer* renderer, int y);
42
43static void _updatePalettes(struct GBAVideoSoftwareRenderer* renderer);
44
45static void _breakWindow(struct GBAVideoSoftwareRenderer* softwareRenderer, struct WindowN* win, int y);
46static void _breakWindowInner(struct GBAVideoSoftwareRenderer* softwareRenderer, struct WindowN* win);
47
48void GBAVideoSoftwareRendererCreate(struct GBAVideoSoftwareRenderer* renderer) {
49 renderer->d.init = GBAVideoSoftwareRendererInit;
50 renderer->d.reset = GBAVideoSoftwareRendererReset;
51 renderer->d.deinit = GBAVideoSoftwareRendererDeinit;
52 renderer->d.writeVideoRegister = GBAVideoSoftwareRendererWriteVideoRegister;
53 renderer->d.writeVRAM = GBAVideoSoftwareRendererWriteVRAM;
54 renderer->d.writeOAM = GBAVideoSoftwareRendererWriteOAM;
55 renderer->d.writePalette = GBAVideoSoftwareRendererWritePalette;
56 renderer->d.drawScanline = GBAVideoSoftwareRendererDrawScanline;
57 renderer->d.finishFrame = GBAVideoSoftwareRendererFinishFrame;
58 renderer->d.getPixels = GBAVideoSoftwareRendererGetPixels;
59 renderer->d.putPixels = GBAVideoSoftwareRendererPutPixels;
60
61 renderer->d.disableBG[0] = false;
62 renderer->d.disableBG[1] = false;
63 renderer->d.disableBG[2] = false;
64 renderer->d.disableBG[3] = false;
65 renderer->d.disableOBJ = false;
66
67 renderer->temporaryBuffer = 0;
68}
69
70static void GBAVideoSoftwareRendererInit(struct GBAVideoRenderer* renderer) {
71 GBAVideoSoftwareRendererReset(renderer);
72
73 struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
74
75 int y;
76 for (y = 0; y < VIDEO_VERTICAL_PIXELS; ++y) {
77 color_t* row = &softwareRenderer->outputBuffer[softwareRenderer->outputBufferStride * y];
78 int x;
79 for (x = 0; x < VIDEO_HORIZONTAL_PIXELS; ++x) {
80 row[x] = GBA_COLOR_WHITE;
81 }
82 }
83}
84
85static void GBAVideoSoftwareRendererReset(struct GBAVideoRenderer* renderer) {
86 struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
87 int i;
88
89 softwareRenderer->dispcnt = 0x0080;
90
91 softwareRenderer->target1Obj = 0;
92 softwareRenderer->target1Bd = 0;
93 softwareRenderer->target2Obj = 0;
94 softwareRenderer->target2Bd = 0;
95 softwareRenderer->blendEffect = BLEND_NONE;
96 for (i = 0; i < 1024; i += 2) {
97 uint16_t entry;
98 LOAD_16(entry, i, softwareRenderer->d.palette);
99 GBAVideoSoftwareRendererWritePalette(renderer, i, entry);
100 }
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 softwareRenderer->bldy = value & 0x1F;
266 if (softwareRenderer->bldy > 0x10) {
267 softwareRenderer->bldy = 0x10;
268 }
269 _updatePalettes(softwareRenderer);
270 break;
271 case REG_WIN0H:
272 softwareRenderer->winN[0].h.end = value;
273 softwareRenderer->winN[0].h.start = value >> 8;
274 if (softwareRenderer->winN[0].h.start > VIDEO_HORIZONTAL_PIXELS && softwareRenderer->winN[0].h.start > softwareRenderer->winN[0].h.end) {
275 softwareRenderer->winN[0].h.start = 0;
276 }
277 if (softwareRenderer->winN[0].h.end > VIDEO_HORIZONTAL_PIXELS) {
278 softwareRenderer->winN[0].h.end = VIDEO_HORIZONTAL_PIXELS;
279 if (softwareRenderer->winN[0].h.start > VIDEO_HORIZONTAL_PIXELS) {
280 softwareRenderer->winN[0].h.start = VIDEO_HORIZONTAL_PIXELS;
281 }
282 }
283 break;
284 case REG_WIN1H:
285 softwareRenderer->winN[1].h.end = value;
286 softwareRenderer->winN[1].h.start = value >> 8;
287 if (softwareRenderer->winN[1].h.start > VIDEO_HORIZONTAL_PIXELS && softwareRenderer->winN[1].h.start > softwareRenderer->winN[1].h.end) {
288 softwareRenderer->winN[1].h.start = 0;
289 }
290 if (softwareRenderer->winN[1].h.end > VIDEO_HORIZONTAL_PIXELS) {
291 softwareRenderer->winN[1].h.end = VIDEO_HORIZONTAL_PIXELS;
292 if (softwareRenderer->winN[1].h.start > VIDEO_HORIZONTAL_PIXELS) {
293 softwareRenderer->winN[1].h.start = VIDEO_HORIZONTAL_PIXELS;
294 }
295 }
296 break;
297 case REG_WIN0V:
298 softwareRenderer->winN[0].v.end = value;
299 softwareRenderer->winN[0].v.start = value >> 8;
300 if (softwareRenderer->winN[0].v.start > VIDEO_VERTICAL_PIXELS && softwareRenderer->winN[0].v.start > softwareRenderer->winN[0].v.end) {
301 softwareRenderer->winN[0].v.start = 0;
302 }
303 if (softwareRenderer->winN[0].v.end > VIDEO_VERTICAL_PIXELS) {
304 softwareRenderer->winN[0].v.end = VIDEO_VERTICAL_PIXELS;
305 if (softwareRenderer->winN[0].v.start > VIDEO_VERTICAL_PIXELS) {
306 softwareRenderer->winN[0].v.start = VIDEO_VERTICAL_PIXELS;
307 }
308 }
309 break;
310 case REG_WIN1V:
311 softwareRenderer->winN[1].v.end = value;
312 softwareRenderer->winN[1].v.start = value >> 8;
313 if (softwareRenderer->winN[1].v.start > VIDEO_VERTICAL_PIXELS && softwareRenderer->winN[1].v.start > softwareRenderer->winN[1].v.end) {
314 softwareRenderer->winN[1].v.start = 0;
315 }
316 if (softwareRenderer->winN[1].v.end > VIDEO_VERTICAL_PIXELS) {
317 softwareRenderer->winN[1].v.end = VIDEO_VERTICAL_PIXELS;
318 if (softwareRenderer->winN[1].v.start > VIDEO_VERTICAL_PIXELS) {
319 softwareRenderer->winN[1].v.start = VIDEO_VERTICAL_PIXELS;
320 }
321 }
322 break;
323 case REG_WININ:
324 value &= 0x3F3F;
325 softwareRenderer->winN[0].control.packed = value;
326 softwareRenderer->winN[1].control.packed = value >> 8;
327 break;
328 case REG_WINOUT:
329 value &= 0x3F3F;
330 softwareRenderer->winout.packed = value;
331 softwareRenderer->objwin.packed = value >> 8;
332 break;
333 case REG_MOSAIC:
334 softwareRenderer->mosaic = value;
335 break;
336 case REG_GREENSWP:
337 mLOG(GBA_VIDEO, STUB, "Stub video register write: 0x%03X", address);
338 break;
339 default:
340 mLOG(GBA_VIDEO, GAME_ERROR, "Invalid video register: 0x%03X", address);
341 }
342 return value;
343}
344
345static void GBAVideoSoftwareRendererWriteVRAM(struct GBAVideoRenderer* renderer, uint32_t address) {
346 if (renderer->cache) {
347 mTileCacheWriteVRAM(renderer->cache, address);
348 }
349}
350
351static void GBAVideoSoftwareRendererWriteOAM(struct GBAVideoRenderer* renderer, uint32_t oam) {
352 struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
353 softwareRenderer->oamDirty = 1;
354 UNUSED(oam);
355}
356
357static void GBAVideoSoftwareRendererWritePalette(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
358 struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
359#ifdef COLOR_16_BIT
360#ifdef COLOR_5_6_5
361 unsigned color = 0;
362 color |= (value & 0x001F) << 11;
363 color |= (value & 0x03E0) << 1;
364 color |= (value & 0x7C00) >> 10;
365#else
366 unsigned color = value;
367#endif
368#else
369 unsigned color = 0;
370 color |= (value << 3) & 0xF8;
371 color |= (value << 6) & 0xF800;
372 color |= (value << 9) & 0xF80000;
373 color |= (color >> 5) & 0x070707;
374#endif
375 softwareRenderer->normalPalette[address >> 1] = color;
376 if (softwareRenderer->blendEffect == BLEND_BRIGHTEN) {
377 softwareRenderer->variantPalette[address >> 1] = _brighten(color, softwareRenderer->bldy);
378 } else if (softwareRenderer->blendEffect == BLEND_DARKEN) {
379 softwareRenderer->variantPalette[address >> 1] = _darken(color, softwareRenderer->bldy);
380 }
381 if (renderer->cache) {
382 mTileCacheWritePalette(renderer->cache, address);
383 }
384}
385
386static void _breakWindow(struct GBAVideoSoftwareRenderer* softwareRenderer, struct WindowN* win, int y) {
387 if (win->v.end >= win->v.start) {
388 if (y >= win->v.end) {
389 return;
390 }
391 if (y < win->v.start) {
392 return;
393 }
394 } else if (y >= win->v.end && y < win->v.start) {
395 return;
396 }
397 if (win->h.end > VIDEO_HORIZONTAL_PIXELS || win->h.end < win->h.start) {
398 struct WindowN splits[2] = { *win, *win };
399 splits[0].h.start = 0;
400 splits[1].h.end = VIDEO_HORIZONTAL_PIXELS;
401 _breakWindowInner(softwareRenderer, &splits[0]);
402 _breakWindowInner(softwareRenderer, &splits[1]);
403 } else {
404 _breakWindowInner(softwareRenderer, win);
405 }
406}
407
408static void _breakWindowInner(struct GBAVideoSoftwareRenderer* softwareRenderer, struct WindowN* win) {
409 int activeWindow;
410 int startX = 0;
411 if (win->h.end > 0) {
412 for (activeWindow = 0; activeWindow < softwareRenderer->nWindows; ++activeWindow) {
413 if (win->h.start < softwareRenderer->windows[activeWindow].endX) {
414 // Insert a window before the end of the active window
415 struct Window oldWindow = softwareRenderer->windows[activeWindow];
416 if (win->h.start > startX) {
417 // And after the start of the active window
418 int nextWindow = softwareRenderer->nWindows;
419 ++softwareRenderer->nWindows;
420 for (; nextWindow > activeWindow; --nextWindow) {
421 softwareRenderer->windows[nextWindow] = softwareRenderer->windows[nextWindow - 1];
422 }
423 softwareRenderer->windows[activeWindow].endX = win->h.start;
424 ++activeWindow;
425 }
426 softwareRenderer->windows[activeWindow].control = win->control;
427 softwareRenderer->windows[activeWindow].endX = win->h.end;
428 if (win->h.end >= oldWindow.endX) {
429 // Trim off extra windows we've overwritten
430 for (++activeWindow; softwareRenderer->nWindows > activeWindow + 1 && win->h.end >= softwareRenderer->windows[activeWindow].endX; ++activeWindow) {
431 if (VIDEO_CHECKS && activeWindow >= MAX_WINDOW) {
432 mLOG(GBA_VIDEO, FATAL, "Out of bounds window write will occur");
433 return;
434 }
435 softwareRenderer->windows[activeWindow] = softwareRenderer->windows[activeWindow + 1];
436 --softwareRenderer->nWindows;
437 }
438 } else {
439 ++activeWindow;
440 int nextWindow = softwareRenderer->nWindows;
441 ++softwareRenderer->nWindows;
442 for (; nextWindow > activeWindow; --nextWindow) {
443 softwareRenderer->windows[nextWindow] = softwareRenderer->windows[nextWindow - 1];
444 }
445 softwareRenderer->windows[activeWindow] = oldWindow;
446 }
447 break;
448 }
449 startX = softwareRenderer->windows[activeWindow].endX;
450 }
451 }
452#ifdef DEBUG
453 if (softwareRenderer->nWindows > MAX_WINDOW) {
454 mLOG(GBA_VIDEO, FATAL, "Out of bounds window write occurred!");
455 }
456#endif
457}
458
459static void _cleanOAM(struct GBAVideoSoftwareRenderer* renderer) {
460 int i;
461 int oamMax = 0;
462 int objwinMax = 0;
463 for (i = 0; i < 128; ++i) {
464 struct GBAObj obj;
465 LOAD_16(obj.a, 0, &renderer->d.oam->obj[i].a);
466 LOAD_16(obj.b, 0, &renderer->d.oam->obj[i].b);
467 LOAD_16(obj.c, 0, &renderer->d.oam->obj[i].c);
468 if (GBAObjAttributesAIsTransformed(obj.a) || !GBAObjAttributesAIsDisable(obj.a)) {
469 int height = GBAVideoObjSizes[GBAObjAttributesAGetShape(obj.a) * 4 + GBAObjAttributesBGetSize(obj.b)][1];
470 if (GBAObjAttributesAIsTransformed(obj.a)) {
471 height <<= GBAObjAttributesAGetDoubleSize(obj.a);
472 }
473 if (GBAObjAttributesAGetY(obj.a) < VIDEO_VERTICAL_PIXELS || GBAObjAttributesAGetY(obj.a) + height >= VIDEO_VERTICAL_TOTAL_PIXELS) {
474 if (GBAObjAttributesAGetMode(obj.a) == OBJ_MODE_OBJWIN) {
475 renderer->objwinSprites[objwinMax].y = GBAObjAttributesAGetY(obj.a);
476 renderer->objwinSprites[objwinMax].endY = GBAObjAttributesAGetY(obj.a) + height;
477 renderer->objwinSprites[objwinMax].obj = obj;
478 ++objwinMax;
479 } else {
480 renderer->sprites[oamMax].y = GBAObjAttributesAGetY(obj.a);
481 renderer->sprites[oamMax].endY = GBAObjAttributesAGetY(obj.a) + height;
482 renderer->sprites[oamMax].obj = obj;
483 ++oamMax;
484 }
485 }
486 }
487 }
488 renderer->oamMax = oamMax;
489 renderer->objwinMax = objwinMax;
490 renderer->oamDirty = 0;
491}
492
493static void GBAVideoSoftwareRendererDrawScanline(struct GBAVideoRenderer* renderer, int y) {
494 struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
495
496 color_t* row = &softwareRenderer->outputBuffer[softwareRenderer->outputBufferStride * y];
497 if (GBARegisterDISPCNTIsForcedBlank(softwareRenderer->dispcnt)) {
498 int x;
499 for (x = 0; x < VIDEO_HORIZONTAL_PIXELS; ++x) {
500 row[x] = GBA_COLOR_WHITE;
501 }
502 return;
503 }
504
505 int x;
506 for (x = 0; x < VIDEO_HORIZONTAL_PIXELS; x += 4) {
507 softwareRenderer->spriteLayer[x] = FLAG_UNWRITTEN;
508 softwareRenderer->spriteLayer[x + 1] = FLAG_UNWRITTEN;
509 softwareRenderer->spriteLayer[x + 2] = FLAG_UNWRITTEN;
510 softwareRenderer->spriteLayer[x + 3] = FLAG_UNWRITTEN;
511 }
512
513 softwareRenderer->windows[0].endX = VIDEO_HORIZONTAL_PIXELS;
514 softwareRenderer->nWindows = 1;
515 if (GBARegisterDISPCNTIsWin0Enable(softwareRenderer->dispcnt) || GBARegisterDISPCNTIsWin1Enable(softwareRenderer->dispcnt) || GBARegisterDISPCNTIsObjwinEnable(softwareRenderer->dispcnt)) {
516 softwareRenderer->windows[0].control = softwareRenderer->winout;
517 if (GBARegisterDISPCNTIsWin1Enable(softwareRenderer->dispcnt)) {
518 _breakWindow(softwareRenderer, &softwareRenderer->winN[1], y);
519 }
520 if (GBARegisterDISPCNTIsWin0Enable(softwareRenderer->dispcnt)) {
521 _breakWindow(softwareRenderer, &softwareRenderer->winN[0], y);
522 }
523 } else {
524 softwareRenderer->windows[0].control.packed = 0xFF;
525 }
526
527 GBAVideoSoftwareRendererUpdateDISPCNT(softwareRenderer);
528
529 int w;
530 x = 0;
531 for (w = 0; w < softwareRenderer->nWindows; ++w) {
532 // TOOD: handle objwin on backdrop
533 uint32_t backdrop = FLAG_UNWRITTEN | FLAG_PRIORITY | FLAG_IS_BACKGROUND;
534 if (!softwareRenderer->target1Bd || softwareRenderer->blendEffect == BLEND_NONE || softwareRenderer->blendEffect == BLEND_ALPHA || !GBAWindowControlIsBlendEnable(softwareRenderer->windows[w].control.packed)) {
535 backdrop |= softwareRenderer->normalPalette[0];
536 } else {
537 backdrop |= softwareRenderer->variantPalette[0];
538 }
539 int end = softwareRenderer->windows[w].endX;
540 for (; x < end - 3; x += 4) {
541 softwareRenderer->row[x] = backdrop;
542 softwareRenderer->row[x + 1] = backdrop;
543 softwareRenderer->row[x + 2] = backdrop;
544 softwareRenderer->row[x + 3] = backdrop;
545 }
546 for (; x < end; ++x) {
547 softwareRenderer->row[x] = backdrop;
548 }
549 }
550
551 _drawScanline(softwareRenderer, y);
552
553 if (softwareRenderer->target2Bd) {
554 x = 0;
555 for (w = 0; w < softwareRenderer->nWindows; ++w) {
556 uint32_t backdrop = 0;
557 if (!softwareRenderer->target1Bd || softwareRenderer->blendEffect == BLEND_NONE || softwareRenderer->blendEffect == BLEND_ALPHA || !GBAWindowControlIsBlendEnable(softwareRenderer->windows[w].control.packed)) {
558 backdrop |= softwareRenderer->normalPalette[0];
559 } else {
560 backdrop |= softwareRenderer->variantPalette[0];
561 }
562 int end = softwareRenderer->windows[w].endX;
563 for (; x < end; ++x) {
564 uint32_t color = softwareRenderer->row[x];
565 if (color & FLAG_TARGET_1) {
566 softwareRenderer->row[x] = _mix(softwareRenderer->bldb, backdrop, softwareRenderer->blda, color);
567 }
568 }
569 }
570 }
571 if (softwareRenderer->target1Obj && (softwareRenderer->blendEffect == BLEND_DARKEN || softwareRenderer->blendEffect == BLEND_BRIGHTEN)) {
572 x = 0;
573 for (w = 0; w < softwareRenderer->nWindows; ++w) {
574 if (!GBAWindowControlIsBlendEnable(softwareRenderer->windows[w].control.packed)) {
575 continue;
576 }
577 int end = softwareRenderer->windows[w].endX;
578 if (softwareRenderer->blendEffect == BLEND_DARKEN) {
579 for (; x < end; ++x) {
580 uint32_t color = softwareRenderer->row[x];
581 if ((color & 0xFF000000) == FLAG_REBLEND) {
582 softwareRenderer->row[x] = _darken(color, softwareRenderer->bldy);
583 }
584 }
585 } else if (softwareRenderer->blendEffect == BLEND_BRIGHTEN) {
586 for (; x < end; ++x) {
587 uint32_t color = softwareRenderer->row[x];
588 if ((color & 0xFF000000) == FLAG_REBLEND) {
589 softwareRenderer->row[x] = _brighten(color, softwareRenderer->bldy);
590 }
591 }
592 }
593 }
594 }
595
596#ifdef COLOR_16_BIT
597#if defined(__ARM_NEON) && !defined(__APPLE__)
598 _to16Bit(row, softwareRenderer->row, VIDEO_HORIZONTAL_PIXELS);
599#else
600 for (x = 0; x < VIDEO_HORIZONTAL_PIXELS; ++x) {
601 row[x] = softwareRenderer->row[x];
602 }
603#endif
604#else
605 memcpy(row, softwareRenderer->row, VIDEO_HORIZONTAL_PIXELS * sizeof(*row));
606#endif
607}
608
609static void GBAVideoSoftwareRendererFinishFrame(struct GBAVideoRenderer* renderer) {
610 struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
611
612 if (softwareRenderer->temporaryBuffer) {
613 mappedMemoryFree(softwareRenderer->temporaryBuffer, VIDEO_HORIZONTAL_PIXELS * VIDEO_VERTICAL_PIXELS * 4);
614 softwareRenderer->temporaryBuffer = 0;
615 }
616 softwareRenderer->bg[2].sx = softwareRenderer->bg[2].refx;
617 softwareRenderer->bg[2].sy = softwareRenderer->bg[2].refy;
618 softwareRenderer->bg[3].sx = softwareRenderer->bg[3].refx;
619 softwareRenderer->bg[3].sy = softwareRenderer->bg[3].refy;
620}
621
622static void GBAVideoSoftwareRendererGetPixels(struct GBAVideoRenderer* renderer, size_t* stride, const void** pixels) {
623 struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
624 *stride = softwareRenderer->outputBufferStride;
625 *pixels = softwareRenderer->outputBuffer;
626}
627
628static void GBAVideoSoftwareRendererPutPixels(struct GBAVideoRenderer* renderer, size_t stride, const void* pixels) {
629 struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
630
631 const color_t* colorPixels = pixels;
632 unsigned i;
633 for (i = 0; i < VIDEO_VERTICAL_PIXELS; ++i) {
634 memmove(&softwareRenderer->outputBuffer[softwareRenderer->outputBufferStride * i], &colorPixels[stride * i], VIDEO_HORIZONTAL_PIXELS * BYTES_PER_PIXEL);
635 }
636}
637
638static void GBAVideoSoftwareRendererUpdateDISPCNT(struct GBAVideoSoftwareRenderer* renderer) {
639 renderer->bg[0].enabled = GBARegisterDISPCNTGetBg0Enable(renderer->dispcnt) && !renderer->d.disableBG[0];
640 renderer->bg[1].enabled = GBARegisterDISPCNTGetBg1Enable(renderer->dispcnt) && !renderer->d.disableBG[1];
641 renderer->bg[2].enabled = GBARegisterDISPCNTGetBg2Enable(renderer->dispcnt) && !renderer->d.disableBG[2];
642 renderer->bg[3].enabled = GBARegisterDISPCNTGetBg3Enable(renderer->dispcnt) && !renderer->d.disableBG[3];
643}
644
645static void GBAVideoSoftwareRendererWriteBGCNT(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* bg, uint16_t value) {
646 UNUSED(renderer);
647 bg->priority = GBARegisterBGCNTGetPriority(value);
648 bg->charBase = GBARegisterBGCNTGetCharBase(value) << 14;
649 bg->mosaic = GBARegisterBGCNTGetMosaic(value);
650 bg->multipalette = GBARegisterBGCNTGet256Color(value);
651 bg->screenBase = GBARegisterBGCNTGetScreenBase(value) << 11;
652 bg->overflow = GBARegisterBGCNTGetOverflow(value);
653 bg->size = GBARegisterBGCNTGetSize(value);
654}
655
656static void GBAVideoSoftwareRendererWriteBGPA(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
657 bg->dx = value;
658}
659
660static void GBAVideoSoftwareRendererWriteBGPB(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
661 bg->dmx = value;
662}
663
664static void GBAVideoSoftwareRendererWriteBGPC(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
665 bg->dy = value;
666}
667
668static void GBAVideoSoftwareRendererWriteBGPD(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
669 bg->dmy = value;
670}
671
672static void GBAVideoSoftwareRendererWriteBGX_LO(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
673 bg->refx = (bg->refx & 0xFFFF0000) | value;
674 bg->sx = bg->refx;
675}
676
677static void GBAVideoSoftwareRendererWriteBGX_HI(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
678 bg->refx = (bg->refx & 0x0000FFFF) | (value << 16);
679 bg->refx <<= 4;
680 bg->refx >>= 4;
681 bg->sx = bg->refx;
682}
683
684static void GBAVideoSoftwareRendererWriteBGY_LO(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
685 bg->refy = (bg->refy & 0xFFFF0000) | value;
686 bg->sy = bg->refy;
687}
688
689static void GBAVideoSoftwareRendererWriteBGY_HI(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
690 bg->refy = (bg->refy & 0x0000FFFF) | (value << 16);
691 bg->refy <<= 4;
692 bg->refy >>= 4;
693 bg->sy = bg->refy;
694}
695
696static void GBAVideoSoftwareRendererWriteBLDCNT(struct GBAVideoSoftwareRenderer* renderer, uint16_t value) {
697 enum BlendEffect oldEffect = renderer->blendEffect;
698
699 renderer->bg[0].target1 = GBARegisterBLDCNTGetTarget1Bg0(value);
700 renderer->bg[1].target1 = GBARegisterBLDCNTGetTarget1Bg1(value);
701 renderer->bg[2].target1 = GBARegisterBLDCNTGetTarget1Bg2(value);
702 renderer->bg[3].target1 = GBARegisterBLDCNTGetTarget1Bg3(value);
703 renderer->bg[0].target2 = GBARegisterBLDCNTGetTarget2Bg0(value);
704 renderer->bg[1].target2 = GBARegisterBLDCNTGetTarget2Bg1(value);
705 renderer->bg[2].target2 = GBARegisterBLDCNTGetTarget2Bg2(value);
706 renderer->bg[3].target2 = GBARegisterBLDCNTGetTarget2Bg3(value);
707
708 renderer->blendEffect = GBARegisterBLDCNTGetEffect(value);
709 renderer->target1Obj = GBARegisterBLDCNTGetTarget1Obj(value);
710 renderer->target1Bd = GBARegisterBLDCNTGetTarget1Bd(value);
711 renderer->target2Obj = GBARegisterBLDCNTGetTarget2Obj(value);
712 renderer->target2Bd = GBARegisterBLDCNTGetTarget2Bd(value);
713
714 if (oldEffect != renderer->blendEffect) {
715 _updatePalettes(renderer);
716 }
717}
718
719#define TEST_LAYER_ENABLED(X) \
720 (renderer->bg[X].enabled && \
721 (GBAWindowControlIsBg ## X ## Enable(renderer->currentWindow.packed) || \
722 (GBARegisterDISPCNTIsObjwinEnable(renderer->dispcnt) && GBAWindowControlIsBg ## X ## Enable (renderer->objwin.packed))) && \
723 renderer->bg[X].priority == priority)
724
725static void _drawScanline(struct GBAVideoSoftwareRenderer* renderer, int y) {
726 int w;
727 renderer->end = 0;
728 int spriteLayers = 0;
729 if (GBARegisterDISPCNTIsObjEnable(renderer->dispcnt) && !renderer->d.disableOBJ) {
730 if (renderer->oamDirty) {
731 _cleanOAM(renderer);
732 }
733 renderer->spriteCyclesRemaining = GBARegisterDISPCNTIsHblankIntervalFree(renderer->dispcnt) ? OBJ_HBLANK_FREE_LENGTH : OBJ_LENGTH;
734 int mosaicV = GBAMosaicControlGetObjV(renderer->mosaic) + 1;
735 int mosaicY = y - (y % mosaicV);
736 for (w = 0; w < renderer->nWindows; ++w) {
737 renderer->start = renderer->end;
738 renderer->end = renderer->windows[w].endX;
739 renderer->currentWindow = renderer->windows[w].control;
740 if (!GBAWindowControlIsObjEnable(renderer->currentWindow.packed) && !GBARegisterDISPCNTIsObjwinEnable(renderer->dispcnt)) {
741 continue;
742 }
743 int i;
744 int drawn;
745 for (i = 0; i < renderer->objwinMax; ++i) {
746 int localY = y;
747 if (renderer->spriteCyclesRemaining <= 0) {
748 break;
749 }
750 struct GBAVideoSoftwareSprite* sprite = &renderer->objwinSprites[i];
751 if (GBAObjAttributesAIsMosaic(sprite->obj.a)) {
752 localY = mosaicY;
753 }
754 if ((localY < sprite->y && (sprite->endY - 256 < 0 || localY >= sprite->endY - 256)) || localY >= sprite->endY) {
755 continue;
756 }
757 GBAVideoSoftwareRendererPreprocessSprite(renderer, &sprite->obj, localY);
758 }
759 for (i = 0; i < renderer->oamMax; ++i) {
760 int localY = y;
761 if (renderer->spriteCyclesRemaining <= 0) {
762 break;
763 }
764 struct GBAVideoSoftwareSprite* sprite = &renderer->sprites[i];
765 if (GBAObjAttributesAIsMosaic(sprite->obj.a)) {
766 localY = mosaicY;
767 }
768 if ((localY < sprite->y && (sprite->endY - 256 < 0 || localY >= sprite->endY - 256)) || localY >= sprite->endY) {
769 continue;
770 }
771 drawn = GBAVideoSoftwareRendererPreprocessSprite(renderer, &sprite->obj, localY);
772 spriteLayers |= drawn << GBAObjAttributesCGetPriority(sprite->obj.c);
773 }
774 }
775 }
776
777 unsigned priority;
778 for (priority = 0; priority < 4; ++priority) {
779 renderer->end = 0;
780 for (w = 0; w < renderer->nWindows; ++w) {
781 renderer->start = renderer->end;
782 renderer->end = renderer->windows[w].endX;
783 renderer->currentWindow = renderer->windows[w].control;
784 if (spriteLayers & (1 << priority)) {
785 GBAVideoSoftwareRendererPostprocessSprite(renderer, priority);
786 }
787 if (TEST_LAYER_ENABLED(0) && GBARegisterDISPCNTGetMode(renderer->dispcnt) < 2) {
788 GBAVideoSoftwareRendererDrawBackgroundMode0(renderer, &renderer->bg[0], y);
789 }
790 if (TEST_LAYER_ENABLED(1) && GBARegisterDISPCNTGetMode(renderer->dispcnt) < 2) {
791 GBAVideoSoftwareRendererDrawBackgroundMode0(renderer, &renderer->bg[1], y);
792 }
793 if (TEST_LAYER_ENABLED(2)) {
794 switch (GBARegisterDISPCNTGetMode(renderer->dispcnt)) {
795 case 0:
796 GBAVideoSoftwareRendererDrawBackgroundMode0(renderer, &renderer->bg[2], y);
797 break;
798 case 1:
799 case 2:
800 GBAVideoSoftwareRendererDrawBackgroundMode2(renderer, &renderer->bg[2], y);
801 break;
802 case 3:
803 GBAVideoSoftwareRendererDrawBackgroundMode3(renderer, &renderer->bg[2], y);
804 break;
805 case 4:
806 GBAVideoSoftwareRendererDrawBackgroundMode4(renderer, &renderer->bg[2], y);
807 break;
808 case 5:
809 GBAVideoSoftwareRendererDrawBackgroundMode5(renderer, &renderer->bg[2], y);
810 break;
811 }
812 }
813 if (TEST_LAYER_ENABLED(3)) {
814 switch (GBARegisterDISPCNTGetMode(renderer->dispcnt)) {
815 case 0:
816 GBAVideoSoftwareRendererDrawBackgroundMode0(renderer, &renderer->bg[3], y);
817 break;
818 case 2:
819 GBAVideoSoftwareRendererDrawBackgroundMode2(renderer, &renderer->bg[3], y);
820 break;
821 }
822 }
823 }
824 }
825 renderer->bg[2].sx += renderer->bg[2].dmx;
826 renderer->bg[2].sy += renderer->bg[2].dmy;
827 renderer->bg[3].sx += renderer->bg[3].dmx;
828 renderer->bg[3].sy += renderer->bg[3].dmy;
829}
830
831static void _updatePalettes(struct GBAVideoSoftwareRenderer* renderer) {
832 int i;
833 if (renderer->blendEffect == BLEND_BRIGHTEN) {
834 for (i = 0; i < 512; ++i) {
835 renderer->variantPalette[i] = _brighten(renderer->normalPalette[i], renderer->bldy);
836 }
837 } else if (renderer->blendEffect == BLEND_DARKEN) {
838 for (i = 0; i < 512; ++i) {
839 renderer->variantPalette[i] = _darken(renderer->normalPalette[i], renderer->bldy);
840 }
841 } else {
842 for (i = 0; i < 512; ++i) {
843 renderer->variantPalette[i] = renderer->normalPalette[i];
844 }
845 }
846}