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