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