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