src/gba/renderers/video-software.c (view raw)
1#include "video-software.h"
2
3#include "gba.h"
4#include "gba-io.h"
5
6#define UNUSED(X) (void) (X)
7
8static const int _objSizes[32] = {
9 8, 8,
10 16, 16,
11 32, 32,
12 64, 64,
13 16, 8,
14 32, 8,
15 32, 16,
16 64, 32,
17 8, 16,
18 8, 32,
19 16, 32,
20 32, 64,
21 0, 0,
22 0, 0,
23 0, 0,
24 0, 0
25};
26
27static void GBAVideoSoftwareRendererInit(struct GBAVideoRenderer* renderer);
28static void GBAVideoSoftwareRendererDeinit(struct GBAVideoRenderer* renderer);
29static void GBAVideoSoftwareRendererWriteOAM(struct GBAVideoRenderer* renderer, uint32_t oam);
30static void GBAVideoSoftwareRendererWritePalette(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value);
31static uint16_t GBAVideoSoftwareRendererWriteVideoRegister(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value);
32static void GBAVideoSoftwareRendererDrawScanline(struct GBAVideoRenderer* renderer, int y);
33static void GBAVideoSoftwareRendererFinishFrame(struct GBAVideoRenderer* renderer);
34
35static void GBAVideoSoftwareRendererUpdateDISPCNT(struct GBAVideoSoftwareRenderer* renderer);
36static void GBAVideoSoftwareRendererWriteBGCNT(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* bg, uint16_t value);
37static void GBAVideoSoftwareRendererWriteBGPA(struct GBAVideoSoftwareBackground* bg, uint16_t value);
38static void GBAVideoSoftwareRendererWriteBGPB(struct GBAVideoSoftwareBackground* bg, uint16_t value);
39static void GBAVideoSoftwareRendererWriteBGPC(struct GBAVideoSoftwareBackground* bg, uint16_t value);
40static void GBAVideoSoftwareRendererWriteBGPD(struct GBAVideoSoftwareBackground* bg, uint16_t value);
41static void GBAVideoSoftwareRendererWriteBGX_LO(struct GBAVideoSoftwareBackground* bg, uint16_t value);
42static void GBAVideoSoftwareRendererWriteBGX_HI(struct GBAVideoSoftwareBackground* bg, uint16_t value);
43static void GBAVideoSoftwareRendererWriteBGY_LO(struct GBAVideoSoftwareBackground* bg, uint16_t value);
44static void GBAVideoSoftwareRendererWriteBGY_HI(struct GBAVideoSoftwareBackground* bg, uint16_t value);
45static void GBAVideoSoftwareRendererWriteBLDCNT(struct GBAVideoSoftwareRenderer* renderer, uint16_t value);
46
47static void _drawScanline(struct GBAVideoSoftwareRenderer* renderer, int y);
48static void _drawBackgroundMode0(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* background, int y);
49static void _drawBackgroundMode2(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* background, int y);
50static void _drawBackgroundMode3(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* background, int y);
51static void _drawBackgroundMode4(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* background, int y);
52static void _drawBackgroundMode5(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* background, int y);
53static void _cleanOAM(struct GBAVideoSoftwareRenderer* renderer);
54static int _preprocessTransformedSprite(struct GBAVideoSoftwareRenderer* renderer, struct GBATransformedObj* sprite, int y);
55static int _preprocessSprite(struct GBAVideoSoftwareRenderer* renderer, struct GBAObj* sprite, int y);
56static void _postprocessSprite(struct GBAVideoSoftwareRenderer* renderer, unsigned priority);
57
58static void _updatePalettes(struct GBAVideoSoftwareRenderer* renderer);
59static inline unsigned _brighten(unsigned color, int y);
60static inline unsigned _darken(unsigned color, int y);
61static unsigned _mix(int weightA, unsigned colorA, int weightB, unsigned colorB);
62
63void GBAVideoSoftwareRendererCreate(struct GBAVideoSoftwareRenderer* renderer) {
64 renderer->d.init = GBAVideoSoftwareRendererInit;
65 renderer->d.deinit = GBAVideoSoftwareRendererDeinit;
66 renderer->d.writeVideoRegister = GBAVideoSoftwareRendererWriteVideoRegister;
67 renderer->d.writeOAM = GBAVideoSoftwareRendererWriteOAM;
68 renderer->d.writePalette = GBAVideoSoftwareRendererWritePalette;
69 renderer->d.drawScanline = GBAVideoSoftwareRendererDrawScanline;
70 renderer->d.finishFrame = GBAVideoSoftwareRendererFinishFrame;
71}
72
73static void GBAVideoSoftwareRendererInit(struct GBAVideoRenderer* renderer) {
74 struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
75 int i;
76
77 softwareRenderer->dispcnt.packed = 0x0080;
78
79 softwareRenderer->target1Obj = 0;
80 softwareRenderer->target1Bd = 0;
81 softwareRenderer->target2Obj = 0;
82 softwareRenderer->target2Bd = 0;
83 softwareRenderer->blendEffect = BLEND_NONE;
84 memset(softwareRenderer->normalPalette, 0, sizeof(softwareRenderer->normalPalette));
85 memset(softwareRenderer->variantPalette, 0, sizeof(softwareRenderer->variantPalette));
86
87 softwareRenderer->blda = 0;
88 softwareRenderer->bldb = 0;
89 softwareRenderer->bldy = 0;
90
91 softwareRenderer->winN[0].h.packed = 0;
92 softwareRenderer->winN[0].v.packed = 0;
93 softwareRenderer->winN[0].control.packed = 0;
94 softwareRenderer->winN[0].control.priority = 0;
95 softwareRenderer->winN[1].h.packed = 0;
96 softwareRenderer->winN[1].v.packed = 0;
97 softwareRenderer->winN[1].control.packed = 0;
98 softwareRenderer->winN[1].control.priority = 1;
99 softwareRenderer->objwin.packed = 0;
100 softwareRenderer->objwin.priority = 2;
101 softwareRenderer->winout.packed = 0;
102 softwareRenderer->winout.priority = 3;
103
104 softwareRenderer->mosaic.packed = 0;
105
106 for (i = 0; i < 4; ++i) {
107 struct GBAVideoSoftwareBackground* bg = &softwareRenderer->bg[i];
108 bg->index = i;
109 bg->enabled = 0;
110 bg->priority = 0;
111 bg->charBase = 0;
112 bg->mosaic = 0;
113 bg->multipalette = 0;
114 bg->screenBase = 0;
115 bg->overflow = 0;
116 bg->size = 0;
117 bg->target1 = 0;
118 bg->target2 = 0;
119 bg->x = 0;
120 bg->y = 0;
121 bg->refx = 0;
122 bg->refy = 0;
123 bg->dx = 256;
124 bg->dmx = 0;
125 bg->dy = 0;
126 bg->dmy = 256;
127 bg->sx = 0;
128 bg->sy = 0;
129 }
130}
131
132static void GBAVideoSoftwareRendererDeinit(struct GBAVideoRenderer* renderer) {
133 struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
134 UNUSED(softwareRenderer);
135}
136
137static uint16_t GBAVideoSoftwareRendererWriteVideoRegister(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
138 struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
139 switch (address) {
140 case REG_DISPCNT:
141 softwareRenderer->dispcnt.packed = value;
142 GBAVideoSoftwareRendererUpdateDISPCNT(softwareRenderer);
143 break;
144 case REG_BG0CNT:
145 value &= 0xFFCF;
146 GBAVideoSoftwareRendererWriteBGCNT(softwareRenderer, &softwareRenderer->bg[0], value);
147 break;
148 case REG_BG1CNT:
149 value &= 0xFFCF;
150 GBAVideoSoftwareRendererWriteBGCNT(softwareRenderer, &softwareRenderer->bg[1], value);
151 break;
152 case REG_BG2CNT:
153 value &= 0xFFCF;
154 GBAVideoSoftwareRendererWriteBGCNT(softwareRenderer, &softwareRenderer->bg[2], value);
155 break;
156 case REG_BG3CNT:
157 value &= 0xFFCF;
158 GBAVideoSoftwareRendererWriteBGCNT(softwareRenderer, &softwareRenderer->bg[3], value);
159 break;
160 case REG_BG0HOFS:
161 value &= 0x01FF;
162 softwareRenderer->bg[0].x = value;
163 break;
164 case REG_BG0VOFS:
165 value &= 0x01FF;
166 softwareRenderer->bg[0].y = value;
167 break;
168 case REG_BG1HOFS:
169 value &= 0x01FF;
170 softwareRenderer->bg[1].x = value;
171 break;
172 case REG_BG1VOFS:
173 value &= 0x01FF;
174 softwareRenderer->bg[1].y = value;
175 break;
176 case REG_BG2HOFS:
177 value &= 0x01FF;
178 softwareRenderer->bg[2].x = value;
179 break;
180 case REG_BG2VOFS:
181 value &= 0x01FF;
182 softwareRenderer->bg[2].y = value;
183 break;
184 case REG_BG3HOFS:
185 value &= 0x01FF;
186 softwareRenderer->bg[3].x = value;
187 break;
188 case REG_BG3VOFS:
189 value &= 0x01FF;
190 softwareRenderer->bg[3].y = value;
191 break;
192 case REG_BG2PA:
193 GBAVideoSoftwareRendererWriteBGPA(&softwareRenderer->bg[2], value);
194 break;
195 case REG_BG2PB:
196 GBAVideoSoftwareRendererWriteBGPB(&softwareRenderer->bg[2], value);
197 break;
198 case REG_BG2PC:
199 GBAVideoSoftwareRendererWriteBGPC(&softwareRenderer->bg[2], value);
200 break;
201 case REG_BG2PD:
202 GBAVideoSoftwareRendererWriteBGPD(&softwareRenderer->bg[2], value);
203 break;
204 case REG_BG2X_LO:
205 GBAVideoSoftwareRendererWriteBGX_LO(&softwareRenderer->bg[2], value);
206 break;
207 case REG_BG2X_HI:
208 GBAVideoSoftwareRendererWriteBGX_HI(&softwareRenderer->bg[2], value);
209 break;
210 case REG_BG2Y_LO:
211 GBAVideoSoftwareRendererWriteBGY_LO(&softwareRenderer->bg[2], value);
212 break;
213 case REG_BG2Y_HI:
214 GBAVideoSoftwareRendererWriteBGY_HI(&softwareRenderer->bg[2], value);
215 break;
216 case REG_BG3PA:
217 GBAVideoSoftwareRendererWriteBGPA(&softwareRenderer->bg[3], value);
218 break;
219 case REG_BG3PB:
220 GBAVideoSoftwareRendererWriteBGPB(&softwareRenderer->bg[3], value);
221 break;
222 case REG_BG3PC:
223 GBAVideoSoftwareRendererWriteBGPC(&softwareRenderer->bg[3], value);
224 break;
225 case REG_BG3PD:
226 GBAVideoSoftwareRendererWriteBGPD(&softwareRenderer->bg[3], value);
227 break;
228 case REG_BG3X_LO:
229 GBAVideoSoftwareRendererWriteBGX_LO(&softwareRenderer->bg[3], value);
230 break;
231 case REG_BG3X_HI:
232 GBAVideoSoftwareRendererWriteBGX_HI(&softwareRenderer->bg[3], value);
233 break;
234 case REG_BG3Y_LO:
235 GBAVideoSoftwareRendererWriteBGY_LO(&softwareRenderer->bg[3], value);
236 break;
237 case REG_BG3Y_HI:
238 GBAVideoSoftwareRendererWriteBGY_HI(&softwareRenderer->bg[3], value);
239 break;
240 case REG_BLDCNT:
241 GBAVideoSoftwareRendererWriteBLDCNT(softwareRenderer, value);
242 break;
243 case REG_BLDALPHA:
244 softwareRenderer->blda = value & 0x1F;
245 if (softwareRenderer->blda > 0x10) {
246 softwareRenderer->blda = 0x10;
247 }
248 softwareRenderer->bldb = (value >> 8) & 0x1F;
249 if (softwareRenderer->bldb > 0x10) {
250 softwareRenderer->bldb = 0x10;
251 }
252 break;
253 case REG_BLDY:
254 softwareRenderer->bldy = value & 0x1F;
255 if (softwareRenderer->bldy > 0x10) {
256 softwareRenderer->bldy = 0x10;
257 }
258 _updatePalettes(softwareRenderer);
259 break;
260 case REG_WIN0H:
261 softwareRenderer->winN[0].h.packed = value;
262 if (softwareRenderer->winN[0].h.start > VIDEO_HORIZONTAL_PIXELS) {
263 softwareRenderer->winN[0].h.start = 0;
264 }
265 if (softwareRenderer->winN[0].h.start > softwareRenderer->winN[0].h.end || softwareRenderer->winN[0].h.end > VIDEO_HORIZONTAL_PIXELS) {
266 softwareRenderer->winN[0].h.end = VIDEO_HORIZONTAL_PIXELS;
267 }
268 break;
269 case REG_WIN1H:
270 softwareRenderer->winN[1].h.packed = value;
271 if (softwareRenderer->winN[1].h.start > VIDEO_HORIZONTAL_PIXELS) {
272 softwareRenderer->winN[1].h.start = 0;
273 }
274 if (softwareRenderer->winN[1].h.start > softwareRenderer->winN[1].h.end || softwareRenderer->winN[1].h.end > VIDEO_HORIZONTAL_PIXELS) {
275 softwareRenderer->winN[1].h.end = VIDEO_HORIZONTAL_PIXELS;
276 }
277 break;
278 case REG_WIN0V:
279 softwareRenderer->winN[0].v.packed = value;
280 if (softwareRenderer->winN[0].v.start > VIDEO_VERTICAL_PIXELS) {
281 softwareRenderer->winN[0].v.start = 0;
282 }
283 if (softwareRenderer->winN[0].v.start > softwareRenderer->winN[0].v.end || softwareRenderer->winN[0].v.end > VIDEO_HORIZONTAL_PIXELS) {
284 softwareRenderer->winN[0].v.end = VIDEO_VERTICAL_PIXELS;
285 }
286 break;
287 case REG_WIN1V:
288 softwareRenderer->winN[1].v.packed = value;
289 if (softwareRenderer->winN[1].v.start > VIDEO_VERTICAL_PIXELS) {
290 softwareRenderer->winN[1].v.start = 0;
291 }
292 if (softwareRenderer->winN[1].v.start > softwareRenderer->winN[1].v.end || softwareRenderer->winN[1].v.end > VIDEO_HORIZONTAL_PIXELS) {
293 softwareRenderer->winN[1].v.end = VIDEO_VERTICAL_PIXELS;
294 }
295 break;
296 case REG_WININ:
297 softwareRenderer->winN[0].control.packed = value;
298 softwareRenderer->winN[1].control.packed = value >> 8;
299 break;
300 case REG_WINOUT:
301 softwareRenderer->winout.packed = value;
302 softwareRenderer->objwin.packed = value >> 8;
303 break;
304 case REG_MOSAIC:
305 softwareRenderer->mosaic.packed = value;
306 break;
307 case REG_GREENSWP:
308 GBALog(0, GBA_LOG_STUB, "Stub video register write: 0x%03X", address);
309 break;
310 default:
311 GBALog(0, GBA_LOG_GAME_ERROR, "Invalid video register: 0x%03X", address);
312 }
313 return value;
314}
315
316static void GBAVideoSoftwareRendererWriteOAM(struct GBAVideoRenderer* renderer, uint32_t oam) {
317 struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
318 softwareRenderer->oamDirty = 1;
319 UNUSED(oam);
320}
321
322static void GBAVideoSoftwareRendererWritePalette(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
323 struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
324#ifdef COLOR_16_BIT
325#ifdef COLOR_5_6_5
326 unsigned color = 0;
327 color |= (value & 0x001F) << 11;
328 color |= (value & 0x03E0) << 1;
329 color |= (value & 0x7C00) >> 10;
330#else
331 unsigned color = value;
332#endif
333#else
334 unsigned color = 0;
335 color |= (value << 3) & 0xF8;
336 color |= (value << 6) & 0xF800;
337 color |= (value << 9) & 0xF80000;
338#endif
339 softwareRenderer->normalPalette[address >> 1] = color;
340 if (softwareRenderer->blendEffect == BLEND_BRIGHTEN) {
341 softwareRenderer->variantPalette[address >> 1] = _brighten(color, softwareRenderer->bldy);
342 } else if (softwareRenderer->blendEffect == BLEND_DARKEN) {
343 softwareRenderer->variantPalette[address >> 1] = _darken(color, softwareRenderer->bldy);
344 }
345}
346
347static void _breakWindow(struct GBAVideoSoftwareRenderer* softwareRenderer, struct WindowN* win) {
348 int activeWindow;
349 int startX = 0;
350 if (win->h.end > 0) {
351 for (activeWindow = 0; activeWindow < softwareRenderer->nWindows; ++activeWindow) {
352 if (win->h.start < softwareRenderer->windows[activeWindow].endX) {
353 // Insert a window before the end of the active window
354 struct Window oldWindow = softwareRenderer->windows[activeWindow];
355 if (win->h.start > startX) {
356 // And after the start of the active window
357 int nextWindow = softwareRenderer->nWindows;
358 ++softwareRenderer->nWindows;
359 for (; nextWindow > activeWindow; --nextWindow) {
360 softwareRenderer->windows[nextWindow] = softwareRenderer->windows[nextWindow - 1];
361 }
362 softwareRenderer->windows[activeWindow].endX = win->h.start;
363 ++activeWindow;
364 }
365 softwareRenderer->windows[activeWindow].control = win->control;
366 softwareRenderer->windows[activeWindow].endX = win->h.end;
367 if (win->h.end >= oldWindow.endX) {
368 // Trim off extra windows we've overwritten
369 for (++activeWindow; win->h.end >= softwareRenderer->windows[activeWindow].endX && softwareRenderer->nWindows > activeWindow; ++activeWindow) {
370 softwareRenderer->windows[activeWindow] = softwareRenderer->windows[activeWindow + 1];
371 --softwareRenderer->nWindows;
372 }
373 } else {
374 ++activeWindow;
375 int nextWindow = softwareRenderer->nWindows;
376 ++softwareRenderer->nWindows;
377 for (; nextWindow > activeWindow; --nextWindow) {
378 softwareRenderer->windows[nextWindow] = softwareRenderer->windows[nextWindow - 1];
379 }
380 softwareRenderer->windows[activeWindow] = oldWindow;
381 }
382 break;
383 }
384 startX = softwareRenderer->windows[activeWindow].endX;
385 }
386 }
387}
388
389static void _cleanOAM(struct GBAVideoSoftwareRenderer* renderer) {
390 int i;
391 int oamMax = 0;
392 for (i = 0; i < 128; ++i) {
393 struct GBAObj* obj = &renderer->d.oam->obj[i];
394 if (obj->transformed || !obj->disable) {
395 int height = _objSizes[obj->shape * 8 + obj->size * 2 + 1];
396 if (obj->transformed) {
397 height <<= ((struct GBATransformedObj*) obj)->doublesize;
398 }
399 if (obj->y < VIDEO_VERTICAL_PIXELS || obj->y + height >= VIDEO_VERTICAL_TOTAL_PIXELS) {
400 renderer->sprites[oamMax].y = obj->y;
401 renderer->sprites[oamMax].endY = obj->y + height;
402 renderer->sprites[oamMax].obj = *obj;
403 ++oamMax;
404 }
405 }
406 }
407 renderer->oamMax = oamMax;
408 renderer->oamDirty = 0;
409}
410
411
412static void GBAVideoSoftwareRendererDrawScanline(struct GBAVideoRenderer* renderer, int y) {
413 struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
414
415 color_t* row = &softwareRenderer->outputBuffer[softwareRenderer->outputBufferStride * y];
416 if (softwareRenderer->dispcnt.forcedBlank) {
417 int x;
418 for (x = 0; x < VIDEO_HORIZONTAL_PIXELS; ++x) {
419 row[x] = GBA_COLOR_WHITE;
420 }
421 return;
422 }
423
424 int x;
425 for (x = 0; x < VIDEO_HORIZONTAL_PIXELS; x += 4) {
426 softwareRenderer->spriteLayer[x] = FLAG_UNWRITTEN;
427 softwareRenderer->spriteLayer[x + 1] = FLAG_UNWRITTEN;
428 softwareRenderer->spriteLayer[x + 2] = FLAG_UNWRITTEN;
429 softwareRenderer->spriteLayer[x + 3] = FLAG_UNWRITTEN;
430 }
431
432 softwareRenderer->windows[0].endX = VIDEO_HORIZONTAL_PIXELS;
433 softwareRenderer->nWindows = 1;
434 if (softwareRenderer->dispcnt.win0Enable || softwareRenderer->dispcnt.win1Enable || softwareRenderer->dispcnt.objwinEnable) {
435 softwareRenderer->windows[0].control = softwareRenderer->winout;
436 if (softwareRenderer->dispcnt.win1Enable && y < softwareRenderer->winN[1].v.end && y >= softwareRenderer->winN[1].v.start) {
437 _breakWindow(softwareRenderer, &softwareRenderer->winN[1]);
438 }
439 if (softwareRenderer->dispcnt.win0Enable && y < softwareRenderer->winN[0].v.end && y >= softwareRenderer->winN[0].v.start) {
440 _breakWindow(softwareRenderer, &softwareRenderer->winN[0]);
441 }
442 } else {
443 softwareRenderer->windows[0].control.packed = 0xFF;
444 }
445
446 int w;
447 x = 0;
448 for (w = 0; w < softwareRenderer->nWindows; ++w) {
449 // TOOD: handle objwin on backdrop
450 uint32_t backdrop = FLAG_UNWRITTEN | FLAG_PRIORITY | FLAG_IS_BACKGROUND;
451 if (!softwareRenderer->target1Bd || softwareRenderer->blendEffect == BLEND_NONE || softwareRenderer->blendEffect == BLEND_ALPHA || !softwareRenderer->windows[w].control.blendEnable) {
452 backdrop |= softwareRenderer->normalPalette[0];
453 } else {
454 backdrop |= softwareRenderer->variantPalette[0];
455 }
456 int end = softwareRenderer->windows[w].endX;
457 for (; x < end; ++x) {
458 softwareRenderer->row[x] = backdrop;
459 }
460 }
461
462 _drawScanline(softwareRenderer, y);
463
464 if (softwareRenderer->target2Bd) {
465 x = 0;
466 for (w = 0; w < softwareRenderer->nWindows; ++w) {
467 uint32_t backdrop = FLAG_UNWRITTEN;
468 if (!softwareRenderer->target1Bd || softwareRenderer->blendEffect == BLEND_NONE || softwareRenderer->blendEffect == BLEND_ALPHA || !softwareRenderer->windows[w].control.blendEnable) {
469 backdrop |= softwareRenderer->normalPalette[0];
470 } else {
471 backdrop |= softwareRenderer->variantPalette[0];
472 }
473 int end = softwareRenderer->windows[w].endX;
474 for (; x < end; ++x) {
475 uint32_t color = softwareRenderer->row[x];
476 if (color & FLAG_TARGET_1) {
477 softwareRenderer->row[x] = _mix(softwareRenderer->bldb, backdrop, softwareRenderer->blda, color);
478 }
479 }
480 }
481 }
482
483#ifdef COLOR_16_BIT
484#ifdef __arm__
485 _to16Bit(row, softwareRenderer->row, VIDEO_HORIZONTAL_PIXELS);
486#else
487 for (x = 0; x < VIDEO_HORIZONTAL_PIXELS; ++x) {
488 row[x] = softwareRenderer->row[x];
489 }
490#endif
491#else
492 memcpy(row, softwareRenderer->row, VIDEO_HORIZONTAL_PIXELS * sizeof(*row));
493#endif
494}
495
496static void GBAVideoSoftwareRendererFinishFrame(struct GBAVideoRenderer* renderer) {
497 struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
498
499 softwareRenderer->bg[2].sx = softwareRenderer->bg[2].refx;
500 softwareRenderer->bg[2].sy = softwareRenderer->bg[2].refy;
501 softwareRenderer->bg[3].sx = softwareRenderer->bg[3].refx;
502 softwareRenderer->bg[3].sy = softwareRenderer->bg[3].refy;
503}
504
505static void GBAVideoSoftwareRendererUpdateDISPCNT(struct GBAVideoSoftwareRenderer* renderer) {
506 renderer->bg[0].enabled = renderer->dispcnt.bg0Enable;
507 renderer->bg[1].enabled = renderer->dispcnt.bg1Enable;
508 renderer->bg[2].enabled = renderer->dispcnt.bg2Enable;
509 renderer->bg[3].enabled = renderer->dispcnt.bg3Enable;
510}
511
512static void GBAVideoSoftwareRendererWriteBGCNT(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* bg, uint16_t value) {
513 UNUSED(renderer);
514 union GBARegisterBGCNT reg = { .packed = value };
515 bg->priority = reg.priority;
516 bg->charBase = reg.charBase << 14;
517 bg->mosaic = reg.mosaic;
518 bg->multipalette = reg.multipalette;
519 bg->screenBase = reg.screenBase << 11;
520 bg->overflow = reg.overflow;
521 bg->size = reg.size;
522}
523
524static void GBAVideoSoftwareRendererWriteBGPA(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
525 bg->dx = value;
526}
527
528static void GBAVideoSoftwareRendererWriteBGPB(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
529 bg->dmx = value;
530}
531
532static void GBAVideoSoftwareRendererWriteBGPC(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
533 bg->dy = value;
534}
535
536static void GBAVideoSoftwareRendererWriteBGPD(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
537 bg->dmy = value;
538}
539
540static void GBAVideoSoftwareRendererWriteBGX_LO(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
541 bg->refx = (bg->refx & 0xFFFF0000) | value;
542 bg->sx = bg->refx;
543}
544
545static void GBAVideoSoftwareRendererWriteBGX_HI(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
546 bg->refx = (bg->refx & 0x0000FFFF) | (value << 16);
547 bg->refx <<= 4;
548 bg->refx >>= 4;
549 bg->sx = bg->refx;
550}
551
552static void GBAVideoSoftwareRendererWriteBGY_LO(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
553 bg->refy = (bg->refy & 0xFFFF0000) | value;
554 bg->sy = bg->refy;
555}
556
557static void GBAVideoSoftwareRendererWriteBGY_HI(struct GBAVideoSoftwareBackground* bg, uint16_t value) {
558 bg->refy = (bg->refy & 0x0000FFFF) | (value << 16);
559 bg->refy <<= 4;
560 bg->refy >>= 4;
561 bg->sy = bg->refy;
562}
563
564static void GBAVideoSoftwareRendererWriteBLDCNT(struct GBAVideoSoftwareRenderer* renderer, uint16_t value) {
565 union {
566 struct {
567 unsigned target1Bg0 : 1;
568 unsigned target1Bg1 : 1;
569 unsigned target1Bg2 : 1;
570 unsigned target1Bg3 : 1;
571 unsigned target1Obj : 1;
572 unsigned target1Bd : 1;
573 enum BlendEffect effect : 2;
574 unsigned target2Bg0 : 1;
575 unsigned target2Bg1 : 1;
576 unsigned target2Bg2 : 1;
577 unsigned target2Bg3 : 1;
578 unsigned target2Obj : 1;
579 unsigned target2Bd : 1;
580 };
581 uint16_t packed;
582 } bldcnt = { .packed = value };
583
584 enum BlendEffect oldEffect = renderer->blendEffect;
585
586 renderer->bg[0].target1 = bldcnt.target1Bg0;
587 renderer->bg[1].target1 = bldcnt.target1Bg1;
588 renderer->bg[2].target1 = bldcnt.target1Bg2;
589 renderer->bg[3].target1 = bldcnt.target1Bg3;
590 renderer->bg[0].target2 = bldcnt.target2Bg0;
591 renderer->bg[1].target2 = bldcnt.target2Bg1;
592 renderer->bg[2].target2 = bldcnt.target2Bg2;
593 renderer->bg[3].target2 = bldcnt.target2Bg3;
594
595 renderer->blendEffect = bldcnt.effect;
596 renderer->target1Obj = bldcnt.target1Obj;
597 renderer->target1Bd = bldcnt.target1Bd;
598 renderer->target2Obj = bldcnt.target2Obj;
599 renderer->target2Bd = bldcnt.target2Bd;
600
601 renderer->anyTarget2 = bldcnt.packed & 0x3F00;
602
603 if (oldEffect != renderer->blendEffect) {
604 _updatePalettes(renderer);
605 }
606}
607
608#define TEST_LAYER_ENABLED(X) \
609 (renderer->bg[X].enabled && \
610 (renderer->currentWindow.bg ## X ## Enable || \
611 (renderer->dispcnt.objwinEnable && renderer->objwin.bg ## X ## Enable)) && \
612 renderer->bg[X].priority == priority)
613
614static void _drawScanline(struct GBAVideoSoftwareRenderer* renderer, int y) {
615 int w;
616 renderer->end = 0;
617 int spriteLayers = 0;
618 if (renderer->dispcnt.objEnable) {
619 if (renderer->oamDirty) {
620 _cleanOAM(renderer);
621 }
622 int mosaicV = renderer->mosaic.objV + 1;
623 int mosaicY = y - (y % mosaicV);
624 for (w = 0; w < renderer->nWindows; ++w) {
625 renderer->start = renderer->end;
626 renderer->end = renderer->windows[w].endX;
627 renderer->currentWindow = renderer->windows[w].control;
628 if (!renderer->currentWindow.objEnable) {
629 continue;
630 }
631 int i;
632 int drawn;
633 for (i = 0; i < renderer->oamMax; ++i) {
634 int localY = y;
635 struct GBAVideoSoftwareSprite* sprite = &renderer->sprites[i];
636 if (sprite->obj.mosaic) {
637 localY = mosaicY;
638 }
639 if ((localY < sprite->y && (sprite->endY - 256 < 0 || localY >= sprite->endY - 256)) || localY >= sprite->endY) {
640 continue;
641 }
642 if (sprite->obj.transformed) {
643 drawn = _preprocessTransformedSprite(renderer, &sprite->tobj, localY);
644 } else {
645 drawn = _preprocessSprite(renderer, &sprite->obj, localY);
646 }
647 spriteLayers |= drawn << sprite->obj.priority;
648 }
649 }
650 }
651
652 int priority;
653 for (priority = 0; priority < 4; ++priority) {
654 if (spriteLayers & (1 << priority)) {
655 _postprocessSprite(renderer, priority);
656 }
657 renderer->end = 0;
658 for (w = 0; w < renderer->nWindows; ++w) {
659 renderer->start = renderer->end;
660 renderer->end = renderer->windows[w].endX;
661 renderer->currentWindow = renderer->windows[w].control;
662 if (TEST_LAYER_ENABLED(0) && renderer->dispcnt.mode < 2) {
663 _drawBackgroundMode0(renderer, &renderer->bg[0], y);
664 }
665 if (TEST_LAYER_ENABLED(1) && renderer->dispcnt.mode < 2) {
666 _drawBackgroundMode0(renderer, &renderer->bg[1], y);
667 }
668 if (TEST_LAYER_ENABLED(2)) {
669 switch (renderer->dispcnt.mode) {
670 case 0:
671 _drawBackgroundMode0(renderer, &renderer->bg[2], y);
672 break;
673 case 1:
674 case 2:
675 _drawBackgroundMode2(renderer, &renderer->bg[2], y);
676 break;
677 case 3:
678 _drawBackgroundMode3(renderer, &renderer->bg[2], y);
679 break;
680 case 4:
681 _drawBackgroundMode4(renderer, &renderer->bg[2], y);
682 break;
683 case 5:
684 _drawBackgroundMode5(renderer, &renderer->bg[2], y);
685 break;
686 }
687 }
688 if (TEST_LAYER_ENABLED(3)) {
689 switch (renderer->dispcnt.mode) {
690 case 0:
691 _drawBackgroundMode0(renderer, &renderer->bg[3], y);
692 break;
693 case 2:
694 _drawBackgroundMode2(renderer, &renderer->bg[3], y);
695 break;
696 }
697 }
698 }
699 }
700 renderer->bg[2].sx += renderer->bg[2].dmx;
701 renderer->bg[2].sy += renderer->bg[2].dmy;
702 renderer->bg[3].sx += renderer->bg[3].dmx;
703 renderer->bg[3].sy += renderer->bg[3].dmy;
704}
705
706// We stash the priority on the top bits so we can do a one-operator comparison
707// The lower the number, the higher the priority, and sprites take precendence over backgrounds
708// We want to do special processing if the color pixel is target 1, however
709
710static inline void _compositeBlendObjwin(struct GBAVideoSoftwareRenderer* renderer, uint32_t* pixel, uint32_t color, uint32_t current) {
711 if (color >= current) {
712 if (current & FLAG_TARGET_1 && color & FLAG_TARGET_2) {
713 color = _mix(renderer->blda, current, renderer->bldb, color);
714 } else {
715 color = current & 0x00FFFFFF;
716 }
717 } else {
718 color = (color & ~FLAG_TARGET_2) | (current & FLAG_OBJWIN);
719 }
720 *pixel = color;
721}
722
723static inline void _compositeBlendNoObjwin(struct GBAVideoSoftwareRenderer* renderer, uint32_t* pixel, uint32_t color, uint32_t current) {
724 // We stash the priority on the top bits so we can do a one-operator comparison
725 // The lower the number, the higher the priority, and sprites take precendence over backgrounds
726 // We want to do special processing if the color pixel is target 1, however
727 if (color >= current) {
728 if (current & FLAG_TARGET_1 && color & FLAG_TARGET_2) {
729 color = _mix(renderer->blda, current, renderer->bldb, color);
730 } else {
731 color = current & 0x00FFFFFF;
732 }
733 } else {
734 color = color & ~FLAG_TARGET_2;
735 }
736 *pixel = color;
737}
738
739static inline void _compositeNoBlendObjwin(struct GBAVideoSoftwareRenderer* renderer, uint32_t* pixel, uint32_t color, uint32_t current) {
740 UNUSED(renderer);
741 if (color < current) {
742 *pixel = color | (current & FLAG_OBJWIN);
743 }
744}
745
746static inline void _compositeNoBlendNoObjwin(struct GBAVideoSoftwareRenderer* renderer, uint32_t* pixel, uint32_t color, uint32_t current) {
747 UNUSED(renderer);
748 if (color < current) {
749 *pixel = color;
750 }
751}
752
753#define COMPOSITE_16_OBJWIN(BLEND) \
754 if (objwinForceEnable || !(current & FLAG_OBJWIN) == objwinOnly) { \
755 unsigned color = (current & FLAG_OBJWIN) ? objwinPalette[paletteData | pixelData] : palette[pixelData]; \
756 _composite ## BLEND ## Objwin(renderer, pixel, color | flags, current); \
757 }
758
759#define COMPOSITE_16_NO_OBJWIN(BLEND) \
760 _composite ## BLEND ## NoObjwin(renderer, pixel, palette[pixelData] | flags, current);
761
762#define COMPOSITE_256_OBJWIN(BLEND) \
763 if (objwinForceEnable || !(current & FLAG_OBJWIN) == objwinOnly) { \
764 unsigned color = (current & FLAG_OBJWIN) ? objwinPalette[pixelData] : palette[pixelData]; \
765 _composite ## BLEND ## Objwin(renderer, pixel, color | flags, current); \
766 }
767
768#define COMPOSITE_256_NO_OBJWIN(BLEND) \
769 COMPOSITE_16_NO_OBJWIN(BLEND)
770
771
772#define COMPOSITE_16_NO_OBJWIN(BLEND) \
773 _composite ## BLEND ## NoObjwin(renderer, pixel, palette[pixelData] | flags, current);
774
775#define BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN) \
776 pixelData = tileData & 0xF; \
777 current = *pixel; \
778 if (pixelData && IS_WRITABLE(current)) { \
779 COMPOSITE_16_ ## OBJWIN (BLEND); \
780 } \
781 tileData >>= 4;
782
783#define BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN) \
784 pixelData = tileData & 0xFF; \
785 current = *pixel; \
786 if (pixelData && IS_WRITABLE(current)) { \
787 COMPOSITE_256_ ## OBJWIN (BLEND); \
788 } \
789 tileData >>= 8;
790
791#define BACKGROUND_TEXT_SELECT_CHARACTER \
792 localX = tileX * 8 + inX; \
793 xBase = localX & 0xF8; \
794 if (background->size & 1) { \
795 xBase += (localX & 0x100) << 5; \
796 } \
797 screenBase = yBase + (xBase >> 3); \
798 mapData = renderer->d.vram[screenBase]; \
799 localY = inY & 0x7; \
800 if (GBA_TEXT_MAP_VFLIP(mapData)) { \
801 localY = 7 - localY; \
802 }
803
804#define PREPARE_OBJWIN \
805 int objwinSlowPath = renderer->dispcnt.objwinEnable; \
806 int objwinOnly = 0; \
807 int objwinForceEnable = 0; \
808 color_t* objwinPalette; \
809 if (objwinSlowPath) { \
810 if (background->target1 && renderer->objwin.blendEnable && (renderer->blendEffect == BLEND_BRIGHTEN || renderer->blendEffect == BLEND_DARKEN)) { \
811 objwinPalette = renderer->variantPalette; \
812 } else { \
813 objwinPalette = renderer->normalPalette; \
814 } \
815 switch (background->index) { \
816 case 0: \
817 objwinForceEnable = renderer->objwin.bg0Enable && renderer->currentWindow.bg0Enable; \
818 objwinOnly = !renderer->objwin.bg0Enable; \
819 break; \
820 case 1: \
821 objwinForceEnable = renderer->objwin.bg1Enable && renderer->currentWindow.bg1Enable; \
822 objwinOnly = !renderer->objwin.bg1Enable; \
823 break; \
824 case 2: \
825 objwinForceEnable = renderer->objwin.bg2Enable && renderer->currentWindow.bg2Enable; \
826 objwinOnly = !renderer->objwin.bg2Enable; \
827 break; \
828 case 3: \
829 objwinForceEnable = renderer->objwin.bg3Enable && renderer->currentWindow.bg3Enable; \
830 objwinOnly = !renderer->objwin.bg3Enable; \
831 break; \
832 } \
833 }
834
835#define DRAW_BACKGROUND_MODE_0_TILE_SUFFIX_16(BLEND, OBJWIN) \
836 paletteData = GBA_TEXT_MAP_PALETTE(mapData) << 4; \
837 palette = &mainPalette[paletteData]; \
838 charBase = ((background->charBase + (GBA_TEXT_MAP_TILE(mapData) << 5)) >> 2) + localY; \
839 tileData = ((uint32_t*)renderer->d.vram)[charBase]; \
840 if (!GBA_TEXT_MAP_HFLIP(mapData)) { \
841 tileData >>= 4 * mod8; \
842 for (; outX < end; ++outX) { \
843 uint32_t* pixel = &renderer->row[outX]; \
844 BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
845 } \
846 } else { \
847 for (outX = end - 1; outX >= renderer->start; --outX) { \
848 uint32_t* pixel = &renderer->row[outX]; \
849 BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
850 } \
851 }
852
853#define DRAW_BACKGROUND_MODE_0_TILE_PREFIX_16(BLEND, OBJWIN) \
854 charBase = ((background->charBase + (GBA_TEXT_MAP_TILE(mapData) << 5)) >> 2) + localY; \
855 tileData = ((uint32_t*)renderer->d.vram)[charBase]; \
856 paletteData = GBA_TEXT_MAP_PALETTE(mapData) << 4; \
857 palette = &mainPalette[paletteData]; \
858 if (!GBA_TEXT_MAP_HFLIP(mapData)) { \
859 outX = renderer->end - mod8; \
860 if (outX < renderer->start) { \
861 tileData >>= 4 * (renderer->start - outX); \
862 outX = renderer->start; \
863 } \
864 for (; outX < renderer->end; ++outX) { \
865 uint32_t* pixel = &renderer->row[outX]; \
866 BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
867 } \
868 } else { \
869 tileData >>= 4 * (0x8 - mod8); \
870 int end2 = renderer->end - 8; \
871 if (end2 < -1) { \
872 end2 = -1; \
873 } \
874 for (outX = renderer->end - 1; outX > end2; --outX) { \
875 uint32_t* pixel = &renderer->row[outX]; \
876 BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
877 } \
878 }
879
880#define DRAW_BACKGROUND_MODE_0_MOSAIC_16(BLEND, OBJWIN) \
881 for (; tileX < tileEnd; ++tileX) { \
882 BACKGROUND_TEXT_SELECT_CHARACTER; \
883 charBase = ((background->charBase + (GBA_TEXT_MAP_TILE(mapData) << 5)) >> 2) + localY; \
884 tileData = carryData; \
885 for (x = 0; x < 8; ++x) { \
886 if (!mosaicWait) { \
887 paletteData = GBA_TEXT_MAP_PALETTE(mapData) << 4; \
888 palette = &mainPalette[paletteData]; \
889 tileData = ((uint32_t*)renderer->d.vram)[charBase]; \
890 if (!GBA_TEXT_MAP_HFLIP(mapData)) { \
891 tileData >>= x * 4; \
892 } else { \
893 tileData >>= (7 - x) * 4; \
894 } \
895 tileData &= 0xF; \
896 tileData |= tileData << 4; \
897 tileData |= tileData << 8; \
898 tileData |= tileData << 12; \
899 tileData |= tileData << 16; \
900 tileData |= tileData << 20; \
901 tileData |= tileData << 24; \
902 tileData |= tileData << 28; \
903 carryData = tileData; \
904 mosaicWait = mosaicH; \
905 } \
906 --mosaicWait; \
907 BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
908 ++pixel; \
909 } \
910 }
911
912#define DRAW_BACKGROUND_MODE_0_TILES_16(BLEND, OBJWIN) \
913 for (; tileX < tileEnd; ++tileX) { \
914 BACKGROUND_TEXT_SELECT_CHARACTER; \
915 paletteData = GBA_TEXT_MAP_PALETTE(mapData) << 4; \
916 palette = &mainPalette[paletteData]; \
917 charBase = ((background->charBase + (GBA_TEXT_MAP_TILE(mapData) << 5)) >> 2) + localY; \
918 tileData = ((uint32_t*)renderer->d.vram)[charBase]; \
919 if (tileData) { \
920 if (!GBA_TEXT_MAP_HFLIP(mapData)) { \
921 BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
922 ++pixel; \
923 BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
924 ++pixel; \
925 BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
926 ++pixel; \
927 BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
928 ++pixel; \
929 BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
930 ++pixel; \
931 BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
932 ++pixel; \
933 BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
934 ++pixel; \
935 BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
936 ++pixel; \
937 } else { \
938 pixel += 7; \
939 BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
940 --pixel; \
941 BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
942 --pixel; \
943 BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
944 --pixel; \
945 BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
946 --pixel; \
947 BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
948 --pixel; \
949 BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
950 --pixel; \
951 BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
952 --pixel; \
953 BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN); \
954 pixel += 8; \
955 } \
956 } else { \
957 pixel += 8; \
958 } \
959 }
960
961#define DRAW_BACKGROUND_MODE_0_TILE_SUFFIX_256(BLEND, OBJWIN) \
962 /* TODO: hflip */ \
963 charBase = ((background->charBase + (GBA_TEXT_MAP_TILE(mapData) << 6)) >> 2) + (localY << 1); \
964 int end2 = end - 4; \
965 int shift = inX & 0x3; \
966 if (end2 > 0) { \
967 tileData = ((uint32_t*)renderer->d.vram)[charBase]; \
968 tileData >>= 8 * shift; \
969 shift = 0; \
970 for (; outX < end2; ++outX) { \
971 uint32_t* pixel = &renderer->row[outX]; \
972 BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
973 } \
974 } \
975 \
976 tileData = ((uint32_t*)renderer->d.vram)[charBase + 1]; \
977 tileData >>= 8 * shift; \
978 for (; outX < end; ++outX) { \
979 uint32_t* pixel = &renderer->row[outX]; \
980 BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
981 }
982
983#define DRAW_BACKGROUND_MODE_0_TILE_PREFIX_256(BLEND, OBJWIN) \
984 /* TODO: hflip */ \
985 charBase = ((background->charBase + (GBA_TEXT_MAP_TILE(mapData) << 6)) >> 2) + (localY << 1); \
986 outX = renderer->end - 8 + end; \
987 int end2 = 4 - end; \
988 if (end2 > 0) { \
989 tileData = ((uint32_t*)renderer->d.vram)[charBase]; \
990 for (; outX < renderer->end - end2; ++outX) { \
991 uint32_t* pixel = &renderer->row[outX]; \
992 BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
993 } \
994 ++charBase; \
995 } \
996 \
997 tileData = ((uint32_t*)renderer->d.vram)[charBase]; \
998 for (; outX < renderer->end; ++outX) { \
999 uint32_t* pixel = &renderer->row[outX]; \
1000 BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1001 }
1002
1003#define DRAW_BACKGROUND_MODE_0_TILES_256(BLEND, OBJWIN) \
1004 for (; tileX < tileEnd; ++tileX) { \
1005 BACKGROUND_TEXT_SELECT_CHARACTER; \
1006 charBase = ((background->charBase + (GBA_TEXT_MAP_TILE(mapData) << 6)) >> 2) + (localY << 1); \
1007 if (!GBA_TEXT_MAP_HFLIP(mapData)) { \
1008 tileData = ((uint32_t*)renderer->d.vram)[charBase]; \
1009 if (tileData) { \
1010 BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1011 ++pixel; \
1012 BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1013 ++pixel; \
1014 BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1015 ++pixel; \
1016 BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1017 ++pixel; \
1018 } else { \
1019 pixel += 4; \
1020 } \
1021 tileData = ((uint32_t*)renderer->d.vram)[charBase + 1]; \
1022 if (tileData) { \
1023 BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1024 ++pixel; \
1025 BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1026 ++pixel; \
1027 BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1028 ++pixel; \
1029 BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1030 ++pixel; \
1031 } else { \
1032 pixel += 4; \
1033 } \
1034 } else { \
1035 uint32_t tileData = ((uint32_t*)renderer->d.vram)[charBase + 1]; \
1036 if (tileData) { \
1037 pixel += 3; \
1038 BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1039 --pixel; \
1040 BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1041 --pixel; \
1042 BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1043 --pixel; \
1044 BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1045 } \
1046 pixel += 4; \
1047 tileData = ((uint32_t*)renderer->d.vram)[charBase]; \
1048 if (tileData) { \
1049 pixel += 3; \
1050 BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1051 --pixel; \
1052 BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1053 --pixel; \
1054 BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1055 --pixel; \
1056 BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1057 } \
1058 pixel += 4; \
1059 } \
1060 }
1061
1062#define DRAW_BACKGROUND_MODE_0_MOSAIC_256(BLEND, OBJWIN) \
1063 for (; tileX < tileEnd; ++tileX) { \
1064 BACKGROUND_TEXT_SELECT_CHARACTER; \
1065 charBase = ((background->charBase + (GBA_TEXT_MAP_TILE(mapData) << 6)) >> 2) + (localY << 1); \
1066 tileData = carryData; \
1067 for (x = 0; x < 8; ++x) { \
1068 if (!mosaicWait) { \
1069 if (x >= 4) { \
1070 tileData = ((uint32_t*)renderer->d.vram)[charBase + 1]; \
1071 if (!GBA_TEXT_MAP_HFLIP(mapData)) { \
1072 tileData >>= (x - 4) * 8; \
1073 } else { \
1074 tileData >>= (7 - x) * 8; \
1075 } \
1076 } else { \
1077 tileData = ((uint32_t*)renderer->d.vram)[charBase]; \
1078 if (!GBA_TEXT_MAP_HFLIP(mapData)) { \
1079 tileData >>= x * 8; \
1080 } else { \
1081 tileData >>= (3 - x) * 8; \
1082 } \
1083 } \
1084 tileData &= 0xFF; \
1085 carryData = tileData; \
1086 mosaicWait = mosaicH; \
1087 } \
1088 tileData |= tileData << 8; \
1089 --mosaicWait; \
1090 BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN); \
1091 ++pixel; \
1092 } \
1093 }
1094
1095#define DRAW_BACKGROUND_MODE_0(BPP, BLEND, OBJWIN) \
1096 if (inX & 0x7) { \
1097 int mod8 = inX & 0x7; \
1098 BACKGROUND_TEXT_SELECT_CHARACTER; \
1099 \
1100 int end = outX + 0x8 - mod8; \
1101 if (end > renderer->end) { \
1102 /* TODO: ensure tiles are properly aligned from this*/ \
1103 end = renderer->end; \
1104 } \
1105 DRAW_BACKGROUND_MODE_0_TILE_SUFFIX_ ## BPP (BLEND, OBJWIN) \
1106 } \
1107 if (inX & 0x7 || (renderer->end - renderer->start) & 0x7) { \
1108 tileX = tileEnd; \
1109 int pixelData; \
1110 int mod8 = (inX + renderer->end - renderer->start) & 0x7; \
1111 BACKGROUND_TEXT_SELECT_CHARACTER; \
1112 \
1113 int end = 0x8 - mod8; \
1114 UNUSED(end); \
1115 DRAW_BACKGROUND_MODE_0_TILE_PREFIX_ ## BPP (BLEND, OBJWIN) \
1116 \
1117 tileX = (inX & 0x7) != 0; \
1118 outX = renderer->start + tileX * 8 - (inX & 0x7); \
1119 } \
1120 \
1121 uint32_t* pixel = &renderer->row[outX]; \
1122 if (background->mosaic) { \
1123 int mosaicH = renderer->mosaic.bgH + 1; \
1124 int x; \
1125 int mosaicWait = outX % mosaicH; \
1126 int carryData = 0; \
1127 paletteData = 0; /* Quiets compiler warning */ \
1128 DRAW_BACKGROUND_MODE_0_MOSAIC_ ## BPP (BLEND, OBJWIN) \
1129 return; \
1130 } \
1131 \
1132 DRAW_BACKGROUND_MODE_0_TILES_ ## BPP (BLEND, OBJWIN)
1133
1134static void _drawBackgroundMode0(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* background, int y) {
1135 int inX = renderer->start + background->x;
1136 if (background->mosaic) {
1137 int mosaicV = renderer->mosaic.bgV + 1;
1138 y -= y % mosaicV;
1139 }
1140 int inY = y + background->y;
1141 uint16_t mapData;
1142
1143 unsigned yBase = inY & 0xF8;
1144 if (background->size == 2) {
1145 yBase += inY & 0x100;
1146 } else if (background->size == 3) {
1147 yBase += (inY & 0x100) << 1;
1148 }
1149 yBase = (background->screenBase >> 1) + (yBase << 2);
1150
1151 int localX;
1152 int localY;
1153
1154 unsigned xBase;
1155
1156 int flags = (background->priority << OFFSET_PRIORITY) | (background->index << OFFSET_INDEX) | FLAG_IS_BACKGROUND;
1157 flags |= FLAG_TARGET_1 * (background->target1 && renderer->blendEffect == BLEND_ALPHA);
1158 flags |= FLAG_TARGET_2 * background->target2;
1159
1160 uint32_t screenBase;
1161 uint32_t charBase;
1162 int variant = background->target1 && renderer->currentWindow.blendEnable && (renderer->blendEffect == BLEND_BRIGHTEN || renderer->blendEffect == BLEND_DARKEN);
1163 color_t* mainPalette = renderer->normalPalette;
1164 if (variant) {
1165 mainPalette = renderer->variantPalette;
1166 }
1167 color_t* palette = mainPalette;
1168 PREPARE_OBJWIN;
1169
1170 int outX = renderer->start;
1171
1172 uint32_t tileData;
1173 uint32_t current;
1174 int pixelData;
1175 int paletteData;
1176 int tileX = 0;
1177 int tileEnd = (renderer->end - renderer->start + (inX & 0x7)) >> 3;
1178
1179 if (!objwinSlowPath) {
1180 if (!(flags & FLAG_TARGET_2)) {
1181 if (!background->multipalette) {
1182 DRAW_BACKGROUND_MODE_0(16, NoBlend, NO_OBJWIN);
1183 } else {
1184 DRAW_BACKGROUND_MODE_0(256, NoBlend, NO_OBJWIN);
1185 }
1186 } else {
1187 if (!background->multipalette) {
1188 DRAW_BACKGROUND_MODE_0(16, Blend, NO_OBJWIN);
1189 } else {
1190 DRAW_BACKGROUND_MODE_0(256, Blend, NO_OBJWIN);
1191 }
1192 }
1193 } else {
1194 if (!(flags & FLAG_TARGET_2)) {
1195 if (!background->multipalette) {
1196 DRAW_BACKGROUND_MODE_0(16, NoBlend, OBJWIN);
1197 } else {
1198 DRAW_BACKGROUND_MODE_0(256, NoBlend, OBJWIN);
1199 }
1200 } else {
1201 if (!background->multipalette) {
1202 DRAW_BACKGROUND_MODE_0(16, Blend, OBJWIN);
1203 } else {
1204 DRAW_BACKGROUND_MODE_0(256, Blend, OBJWIN);
1205 }
1206 }
1207 }
1208}
1209
1210#define BACKGROUND_BITMAP_INIT \
1211 UNUSED(unused); \
1212 int32_t x = background->sx + (renderer->start - 1) * background->dx; \
1213 int32_t y = background->sy + (renderer->start - 1) * background->dy; \
1214 int32_t localX; \
1215 int32_t localY; \
1216 \
1217 int flags = (background->priority << OFFSET_PRIORITY) | (background->index << OFFSET_INDEX) | FLAG_IS_BACKGROUND; \
1218 flags |= FLAG_TARGET_1 * (background->target1 && renderer->blendEffect == BLEND_ALPHA); \
1219 flags |= FLAG_TARGET_2 * background->target2; \
1220 int variant = background->target1 && renderer->currentWindow.blendEnable && (renderer->blendEffect == BLEND_BRIGHTEN || renderer->blendEffect == BLEND_DARKEN); \
1221 color_t* palette = renderer->normalPalette; \
1222 if (variant) { \
1223 palette = renderer->variantPalette; \
1224 } \
1225 PREPARE_OBJWIN;
1226
1227#define BACKGROUND_BITMAP_ITERATE(W, H) \
1228 x += background->dx; \
1229 y += background->dy; \
1230 \
1231 if (x < 0 || y < 0 || (x >> 8) >= W || (y >> 8) >= H) { \
1232 continue; \
1233 } else { \
1234 localX = x; \
1235 localY = y; \
1236 }
1237
1238static void _drawBackgroundMode2(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* background, int unused) {
1239 int sizeAdjusted = 0x8000 << background->size;
1240
1241 BACKGROUND_BITMAP_INIT;
1242
1243 uint32_t screenBase = background->screenBase;
1244 uint32_t charBase = background->charBase;
1245 uint8_t mapData;
1246 uint8_t tileData;
1247
1248 int outX;
1249 uint32_t* pixel;
1250 for (outX = renderer->start, pixel = &renderer->row[outX]; outX < renderer->end; ++outX, ++pixel) {
1251 x += background->dx;
1252 y += background->dy;
1253
1254 if (background->overflow) {
1255 localX = x & (sizeAdjusted - 1);
1256 localY = y & (sizeAdjusted - 1);
1257 } else if ((x | y) & ~(sizeAdjusted - 1)) {
1258 continue;
1259 } else {
1260 localX = x;
1261 localY = y;
1262 }
1263 mapData = ((uint8_t*)renderer->d.vram)[screenBase + (localX >> 11) + (((localY >> 7) & 0x7F0) << background->size)];
1264 tileData = ((uint8_t*)renderer->d.vram)[charBase + (mapData << 6) + ((localY & 0x700) >> 5) + ((localX & 0x700) >> 8)];
1265
1266 uint32_t current = *pixel;
1267 if (tileData && IS_WRITABLE(current)) {
1268 if (!objwinSlowPath) {
1269 _compositeBlendNoObjwin(renderer, pixel, palette[tileData] | flags, current);
1270 } else if (objwinForceEnable || !(current & FLAG_OBJWIN) == objwinOnly) {
1271 color_t* currentPalette = (current & FLAG_OBJWIN) ? objwinPalette : palette;
1272 _compositeBlendObjwin(renderer, pixel, currentPalette[tileData] | flags, current);
1273 }
1274 }
1275 }
1276}
1277
1278static void _drawBackgroundMode3(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* background, int unused) {
1279 BACKGROUND_BITMAP_INIT;
1280
1281 uint32_t color;
1282
1283 int outX;
1284 uint32_t* pixel;
1285 for (outX = renderer->start, pixel = &renderer->row[outX]; outX < renderer->end; ++outX, ++pixel) {
1286 BACKGROUND_BITMAP_ITERATE(VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS);
1287
1288 color = ((uint16_t*)renderer->d.vram)[(localX >> 8) + (localY >> 8) * VIDEO_HORIZONTAL_PIXELS];
1289#ifndef COLOR_16_BIT
1290 unsigned color32;
1291 color32 = 0;
1292 color32 |= (color << 3) & 0xF8;
1293 color32 |= (color << 6) & 0xF800;
1294 color32 |= (color << 9) & 0xF80000;
1295 color = color32;
1296#endif
1297
1298 uint32_t current = *pixel;
1299 if (!objwinSlowPath || !(current & FLAG_OBJWIN) != objwinOnly) {
1300 if (!variant) {
1301 _compositeBlendObjwin(renderer, pixel, color | flags, current);
1302 } else if (renderer->blendEffect == BLEND_BRIGHTEN) {
1303 _compositeBlendObjwin(renderer, pixel, _brighten(color, renderer->bldy) | flags, current);
1304 } else if (renderer->blendEffect == BLEND_DARKEN) {
1305 _compositeBlendObjwin(renderer, pixel, _darken(color, renderer->bldy) | flags, current);
1306 }
1307 }
1308 }
1309}
1310
1311static void _drawBackgroundMode4(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* background, int unused) {
1312 BACKGROUND_BITMAP_INIT;
1313
1314 uint16_t color;
1315 uint32_t offset = 0;
1316 if (renderer->dispcnt.frameSelect) {
1317 offset = 0xA000;
1318 }
1319
1320 int outX;
1321 uint32_t* pixel;
1322 for (outX = renderer->start, pixel = &renderer->row[outX]; outX < renderer->end; ++outX, ++pixel) {
1323 BACKGROUND_BITMAP_ITERATE(VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS);
1324
1325 color = ((uint8_t*)renderer->d.vram)[offset + (localX >> 8) + (localY >> 8) * VIDEO_HORIZONTAL_PIXELS];
1326
1327 uint32_t current = *pixel;
1328 if (color && IS_WRITABLE(current)) {
1329 if (!objwinSlowPath) {
1330 _compositeBlendNoObjwin(renderer, pixel, palette[color] | flags, current);
1331 } else if (objwinForceEnable || !(current & FLAG_OBJWIN) == objwinOnly) {
1332 color_t* currentPalette = (current & FLAG_OBJWIN) ? objwinPalette : palette;
1333 _compositeBlendObjwin(renderer, pixel, currentPalette[color] | flags, current);
1334 }
1335 }
1336 }
1337}
1338
1339static void _drawBackgroundMode5(struct GBAVideoSoftwareRenderer* renderer, struct GBAVideoSoftwareBackground* background, int unused) {
1340 BACKGROUND_BITMAP_INIT;
1341
1342 uint32_t color;
1343 uint32_t offset = 0;
1344 if (renderer->dispcnt.frameSelect) {
1345 offset = 0xA000;
1346 }
1347
1348 int outX;
1349 uint32_t* pixel;
1350 for (outX = renderer->start, pixel = &renderer->row[outX]; outX < renderer->end; ++outX, ++pixel) {
1351 BACKGROUND_BITMAP_ITERATE(160, 128);
1352
1353 color = ((uint16_t*)renderer->d.vram)[offset + (localX >> 8) + (localY >> 8) * 160];
1354#ifndef COLOR_16_BIT
1355 unsigned color32 = 0;
1356 color32 |= (color << 9) & 0xF80000;
1357 color32 |= (color << 3) & 0xF8;
1358 color32 |= (color << 6) & 0xF800;
1359 color = color32;
1360#endif
1361
1362 uint32_t current = *pixel;
1363 if (!objwinSlowPath || !(current & FLAG_OBJWIN) != objwinOnly) {
1364 if (!variant) {
1365 _compositeBlendObjwin(renderer, pixel, color | flags, current);
1366 } else if (renderer->blendEffect == BLEND_BRIGHTEN) {
1367 _compositeBlendObjwin(renderer, pixel, _brighten(color, renderer->bldy) | flags, current);
1368 } else if (renderer->blendEffect == BLEND_DARKEN) {
1369 _compositeBlendObjwin(renderer, pixel, _darken(color, renderer->bldy) | flags, current);
1370 }
1371 }
1372 }
1373}
1374
1375#define SPRITE_NORMAL_LOOP(DEPTH, TYPE) \
1376 SPRITE_YBASE_ ## DEPTH(inY); \
1377 for (; outX < condition; ++outX, inX += xOffset) { \
1378 if (!(renderer->row[outX] & FLAG_UNWRITTEN)) { \
1379 continue; \
1380 } \
1381 SPRITE_XBASE_ ## DEPTH(inX); \
1382 SPRITE_DRAW_PIXEL_ ## DEPTH ## _ ## TYPE(inX); \
1383 }
1384
1385#define SPRITE_MOSAIC_LOOP(DEPTH, TYPE) \
1386 SPRITE_YBASE_ ## DEPTH(inY); \
1387 if (outX % mosaicH) { \
1388 inX += (mosaicH - (outX % mosaicH)) * xOffset; \
1389 outX += mosaicH - (outX % mosaicH); \
1390 } \
1391 for (; outX < condition; ++outX, inX += xOffset) { \
1392 if (!(renderer->row[outX] & FLAG_UNWRITTEN)) { \
1393 continue; \
1394 } \
1395 int localX = inX - xOffset * (outX % mosaicH); \
1396 SPRITE_XBASE_ ## DEPTH(localX); \
1397 SPRITE_DRAW_PIXEL_ ## DEPTH ## _ ## TYPE(localX); \
1398 }
1399
1400#define SPRITE_TRANSFORMED_LOOP(DEPTH, TYPE) \
1401 int outX; \
1402 for (outX = x >= start ? x : start; outX < x + totalWidth && outX < end; ++outX) { \
1403 if (!(renderer->row[outX] & FLAG_UNWRITTEN)) { \
1404 continue; \
1405 } \
1406 int inX = outX - x; \
1407 int localX = ((mat->a * (inX - (totalWidth >> 1)) + mat->b * (inY - (totalHeight >> 1))) >> 8) + (width >> 1); \
1408 int localY = ((mat->c * (inX - (totalWidth >> 1)) + mat->d * (inY - (totalHeight >> 1))) >> 8) + (height >> 1); \
1409 \
1410 if (localX < 0 || localX >= width || localY < 0 || localY >= height) { \
1411 continue; \
1412 } \
1413 \
1414 SPRITE_YBASE_ ## DEPTH(localY); \
1415 SPRITE_XBASE_ ## DEPTH(localX); \
1416 SPRITE_DRAW_PIXEL_ ## DEPTH ## _ ## TYPE(localX); \
1417 }
1418
1419#define SPRITE_XBASE_16(localX) unsigned xBase = (localX & ~0x7) * 4 + ((localX >> 1) & 2);
1420#define SPRITE_YBASE_16(localY) unsigned yBase = (localY & ~0x7) * (renderer->dispcnt.objCharacterMapping ? width >> 1 : 0x80) + (localY & 0x7) * 4;
1421
1422#define SPRITE_DRAW_PIXEL_16_NORMAL(localX) \
1423 unsigned tileData = renderer->d.vram[(yBase + charBase + xBase) >> 1]; \
1424 tileData = (tileData >> ((localX & 3) << 2)) & 0xF; \
1425 if (tileData && (renderer->spriteLayer[outX] & FLAG_ORDER_MASK) > flags) { \
1426 renderer->spriteLayer[outX] = palette[tileData] | flags; \
1427 }
1428
1429#define SPRITE_DRAW_PIXEL_16_OBJWIN(localX) \
1430 unsigned tileData = renderer->d.vram[(yBase + charBase + xBase) >> 1]; \
1431 tileData = (tileData >> ((localX & 3) << 2)) & 0xF; \
1432 if (tileData) { \
1433 renderer->row[outX] |= FLAG_OBJWIN; \
1434 }
1435
1436#define SPRITE_XBASE_256(localX) unsigned xBase = (localX & ~0x7) * 8 + (localX & 6);
1437#define SPRITE_YBASE_256(localY) unsigned yBase = (localY & ~0x7) * (renderer->dispcnt.objCharacterMapping ? width : 0x80) + (localY & 0x7) * 8;
1438
1439#define SPRITE_DRAW_PIXEL_256_NORMAL(localX) \
1440 unsigned tileData = renderer->d.vram[(yBase + charBase + xBase) >> 1]; \
1441 tileData = (tileData >> ((localX & 1) << 3)) & 0xFF; \
1442 if (tileData && (renderer->spriteLayer[outX] & FLAG_ORDER_MASK) > flags) { \
1443 renderer->spriteLayer[outX] = palette[tileData] | flags; \
1444 }
1445
1446#define SPRITE_DRAW_PIXEL_256_OBJWIN(localX) \
1447 unsigned tileData = renderer->d.vram[(yBase + charBase + xBase) >> 1]; \
1448 tileData = (tileData >> ((localX & 1) << 3)) & 0xFF; \
1449 if (tileData) { \
1450 renderer->row[outX] |= FLAG_OBJWIN; \
1451 }
1452
1453static int _preprocessSprite(struct GBAVideoSoftwareRenderer* renderer, struct GBAObj* sprite, int y) {
1454 int width = _objSizes[sprite->shape * 8 + sprite->size * 2];
1455 int height = _objSizes[sprite->shape * 8 + sprite->size * 2 + 1];
1456 int start = renderer->start;
1457 int end = renderer->end;
1458 uint32_t flags = sprite->priority << OFFSET_PRIORITY;
1459 flags |= FLAG_TARGET_1 * ((renderer->currentWindow.blendEnable && renderer->target1Obj && renderer->blendEffect == BLEND_ALPHA) || sprite->mode == OBJ_MODE_SEMITRANSPARENT);
1460 flags |= FLAG_OBJWIN * (sprite->mode == OBJ_MODE_OBJWIN);
1461 int x = sprite->x;
1462 unsigned charBase = BASE_TILE + sprite->tile * 0x20;
1463 int variant = renderer->target1Obj && renderer->currentWindow.blendEnable && (renderer->blendEffect == BLEND_BRIGHTEN || renderer->blendEffect == BLEND_DARKEN);
1464 if (sprite->mode == OBJ_MODE_SEMITRANSPARENT && renderer->target2Bd) {
1465 // Hack: if a sprite is blended, then the variant palette is not used, but we don't know if it's blended in advance
1466 variant = 0;
1467 }
1468 color_t* palette = &renderer->normalPalette[0x100];
1469 if (variant) {
1470 palette = &renderer->variantPalette[0x100];
1471 }
1472
1473 int outX = x >= start ? x : start;
1474 int condition = x + width;
1475 int mosaicH = 1;
1476 if (sprite->mosaic) {
1477 mosaicH = renderer->mosaic.objH + 1;
1478 if (condition % mosaicH) {
1479 condition += mosaicH - (condition % mosaicH);
1480 }
1481 }
1482 int inY = y - sprite->y;
1483 if (sprite->y + height - 256 >= 0) {
1484 inY += 256;
1485 }
1486 if (sprite->vflip) {
1487 inY = height - inY - 1;
1488 }
1489 if (end < condition) {
1490 condition = end;
1491 }
1492 int inX = outX - x;
1493 int xOffset = 1;
1494 if (sprite->hflip) {
1495 inX = width - inX - 1;
1496 xOffset = -1;
1497 }
1498 if (!sprite->multipalette) {
1499 palette = &palette[sprite->palette << 4];
1500 if (flags & FLAG_OBJWIN) {
1501 SPRITE_NORMAL_LOOP(16, OBJWIN);
1502 } else if (sprite->mosaic) {
1503 SPRITE_MOSAIC_LOOP(16, NORMAL);
1504 } else {
1505 SPRITE_NORMAL_LOOP(16, NORMAL);
1506 }
1507 } else {
1508 if (flags & FLAG_OBJWIN) {
1509 SPRITE_NORMAL_LOOP(256, OBJWIN);
1510 } else if (sprite->mosaic) {
1511 SPRITE_MOSAIC_LOOP(256, NORMAL);
1512 } else {
1513 SPRITE_NORMAL_LOOP(256, NORMAL);
1514 }
1515 }
1516 return 1;
1517}
1518
1519static int _preprocessTransformedSprite(struct GBAVideoSoftwareRenderer* renderer, struct GBATransformedObj* sprite, int y) {
1520 int width = _objSizes[sprite->shape * 8 + sprite->size * 2];
1521 int height = _objSizes[sprite->shape * 8 + sprite->size * 2 + 1];
1522 int totalWidth = width << sprite->doublesize;
1523 int totalHeight = height << sprite->doublesize;
1524 int start = renderer->start;
1525 int end = renderer->end;
1526 uint32_t flags = sprite->priority << OFFSET_PRIORITY;
1527 flags |= FLAG_TARGET_1 * ((renderer->currentWindow.blendEnable && renderer->target1Obj && renderer->blendEffect == BLEND_ALPHA) || sprite->mode == OBJ_MODE_SEMITRANSPARENT);
1528 flags |= FLAG_OBJWIN * (sprite->mode == OBJ_MODE_OBJWIN);
1529 int x = sprite->x;
1530 unsigned charBase = BASE_TILE + sprite->tile * 0x20;
1531 struct GBAOAMMatrix* mat = &renderer->d.oam->mat[sprite->matIndex];
1532 int variant = renderer->target1Obj && renderer->currentWindow.blendEnable && (renderer->blendEffect == BLEND_BRIGHTEN || renderer->blendEffect == BLEND_DARKEN);
1533 if (sprite->mode == OBJ_MODE_SEMITRANSPARENT && renderer->target2Bd) {
1534 // Hack: if a sprite is blended, then the variant palette is not used, but we don't know if it's blended in advance
1535 variant = 0;
1536 }
1537 color_t* palette = &renderer->normalPalette[0x100];
1538 if (variant) {
1539 palette = &renderer->variantPalette[0x100];
1540 }
1541 int inY = y - sprite->y;
1542 if (inY < 0) {
1543 inY += 256;
1544 }
1545 if (!sprite->multipalette) {
1546 palette = &palette[sprite->palette << 4];
1547 if (flags & FLAG_OBJWIN) {
1548 SPRITE_TRANSFORMED_LOOP(16, OBJWIN);
1549 } else {
1550 SPRITE_TRANSFORMED_LOOP(16, NORMAL);
1551 }
1552 } else {
1553 if (flags & FLAG_OBJWIN) {
1554 SPRITE_TRANSFORMED_LOOP(256, OBJWIN);
1555 } else {
1556 SPRITE_TRANSFORMED_LOOP(256, NORMAL);
1557 }
1558 }
1559 return 1;
1560}
1561
1562static void _postprocessSprite(struct GBAVideoSoftwareRenderer* renderer, unsigned priority) {
1563 int x;
1564 uint32_t* pixel = renderer->row;
1565 uint32_t flags = FLAG_TARGET_2 * renderer->target2Obj;
1566
1567 int objwinSlowPath = renderer->dispcnt.objwinEnable;
1568 int objwinDisable = 0;
1569 if (objwinSlowPath) {
1570 objwinDisable = !renderer->objwin.objEnable;
1571 }
1572 if (objwinSlowPath && objwinDisable) {
1573 for (x = 0; x < VIDEO_HORIZONTAL_PIXELS; ++x, ++pixel) {
1574 uint32_t color = renderer->spriteLayer[x] & ~FLAG_OBJWIN;
1575 uint32_t current = *pixel;
1576 if ((color & FLAG_UNWRITTEN) != FLAG_UNWRITTEN && !(current & FLAG_OBJWIN) && (color & FLAG_PRIORITY) >> OFFSET_PRIORITY == priority) {
1577 _compositeBlendObjwin(renderer, pixel, color | flags, current);
1578 }
1579 }
1580 } else {
1581 for (x = 0; x < VIDEO_HORIZONTAL_PIXELS; ++x, ++pixel) {
1582 uint32_t color = renderer->spriteLayer[x] & ~FLAG_OBJWIN;
1583 uint32_t current = *pixel;
1584 if ((color & FLAG_UNWRITTEN) != FLAG_UNWRITTEN && (color & FLAG_PRIORITY) >> OFFSET_PRIORITY == priority) {
1585 _compositeBlendNoObjwin(renderer, pixel, color | flags, current);
1586 }
1587 }
1588 }
1589}
1590
1591static void _updatePalettes(struct GBAVideoSoftwareRenderer* renderer) {
1592 int i;
1593 if (renderer->blendEffect == BLEND_BRIGHTEN) {
1594 for (i = 0; i < 512; ++i) {
1595 renderer->variantPalette[i] = _brighten(renderer->normalPalette[i], renderer->bldy);
1596 }
1597 } else if (renderer->blendEffect == BLEND_DARKEN) {
1598 for (i = 0; i < 512; ++i) {
1599 renderer->variantPalette[i] = _darken(renderer->normalPalette[i], renderer->bldy);
1600 }
1601 } else {
1602 for (i = 0; i < 512; ++i) {
1603 renderer->variantPalette[i] = renderer->normalPalette[i];
1604 }
1605 }
1606}
1607
1608static inline unsigned _brighten(unsigned color, int y) {
1609 unsigned c = 0;
1610 unsigned a;
1611#ifdef COLOR_16_BIT
1612 a = color & 0x1F;
1613 c |= (a + ((0x1F - a) * y) / 16) & 0x1F;
1614
1615#ifdef COLOR_5_6_5
1616 a = color & 0x7C0;
1617 c |= (a + ((0x7C0 - a) * y) / 16) & 0x7C0;
1618
1619 a = color & 0xF800;
1620 c |= (a + ((0xF800 - a) * y) / 16) & 0xF800;
1621#else
1622 a = color & 0x3E0;
1623 c |= (a + ((0x3E0 - a) * y) / 16) & 0x3E0;
1624
1625 a = color & 0x7C00;
1626 c |= (a + ((0x7C00 - a) * y) / 16) & 0x7C00;
1627#endif
1628#else
1629 a = color & 0xF8;
1630 c |= (a + ((0xF8 - a) * y) / 16) & 0xF8;
1631
1632 a = color & 0xF800;
1633 c |= (a + ((0xF800 - a) * y) / 16) & 0xF800;
1634
1635 a = color & 0xF80000;
1636 c |= (a + ((0xF80000 - a) * y) / 16) & 0xF80000;
1637#endif
1638 return c;
1639}
1640
1641static inline unsigned _darken(unsigned color, int y) {
1642 unsigned c = 0;
1643 unsigned a;
1644#ifdef COLOR_16_BIT
1645 a = color & 0x1F;
1646 c |= (a - (a * y) / 16) & 0x1F;
1647
1648#ifdef COLOR_5_6_5
1649 a = color & 0x7C0;
1650 c |= (a - (a * y) / 16) & 0x7C0;
1651
1652 a = color & 0xF800;
1653 c |= (a - (a * y) / 16) & 0xF800;
1654#else
1655 a = color & 0x3E0;
1656 c |= (a - (a * y) / 16) & 0x3E0;
1657
1658 a = color & 0x7C00;
1659 c |= (a - (a * y) / 16) & 0x7C00;
1660#endif
1661#else
1662 a = color & 0xF8;
1663 c |= (a - (a * y) / 16) & 0xF8;
1664
1665 a = color & 0xF800;
1666 c |= (a - (a * y) / 16) & 0xF800;
1667
1668 a = color & 0xF80000;
1669 c |= (a - (a * y) / 16) & 0xF80000;
1670#endif
1671 return c;
1672}
1673
1674static unsigned _mix(int weightA, unsigned colorA, int weightB, unsigned colorB) {
1675 unsigned c = 0;
1676 unsigned a, b;
1677#ifdef COLOR_16_BIT
1678#ifdef COLOR_5_6_5
1679 a = colorA & 0xF81F;
1680 b = colorB & 0xF81F;
1681 a |= (colorA & 0x7C0) << 16;
1682 b |= (colorB & 0x7C0) << 16;
1683 c = ((a * weightA + b * weightB) / 16);
1684 if (c & 0x08000000) {
1685 c = (c & ~0x0FC00000) | 0x07C00000;
1686 }
1687 if (c & 0x0020) {
1688 c = (c & ~0x003F) | 0x001F;
1689 }
1690 if (c & 0x10000) {
1691 c = (c & ~0x1F800) | 0xF800;
1692 }
1693 c = (c & 0xF81F) | ((c >> 16) & 0x07C0);
1694#else
1695 a = colorA & 0x7C1F;
1696 b = colorB & 0x7C1F;
1697 a |= (colorA & 0x3E0) << 16;
1698 b |= (colorB & 0x3E0) << 16;
1699 c = ((a * weightA + b * weightB) / 16);
1700 if (c & 0x04000000) {
1701 c = (c & ~0x07E00000) | 0x03E00000;
1702 }
1703 if (c & 0x0020) {
1704 c = (c & ~0x003F) | 0x001F;
1705 }
1706 if (c & 0x10000) {
1707 c = (c & ~0x1F800) | 0xF800;
1708 }
1709 c = (c & 0x7C1F) | ((c >> 16) & 0x03E0);
1710#endif
1711#else
1712 a = colorA & 0xF8;
1713 b = colorB & 0xF8;
1714 c |= ((a * weightA + b * weightB) / 16) & 0x1F8;
1715 if (c & 0x00000100) {
1716 c = 0x000000F8;
1717 }
1718
1719 a = colorA & 0xF800;
1720 b = colorB & 0xF800;
1721 c |= ((a * weightA + b * weightB) / 16) & 0x1F800;
1722 if (c & 0x00010000) {
1723 c = (c & 0x000000F8) | 0x0000F800;
1724 }
1725
1726 a = colorA & 0xF80000;
1727 b = colorB & 0xF80000;
1728 c |= ((a * weightA + b * weightB) / 16) & 0x1F80000;
1729 if (c & 0x01000000) {
1730 c = (c & 0x0000F8F8) | 0x00F80000;
1731 }
1732#endif
1733 return c;
1734}