src/gba/renderers/software-obj.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 "gba/renderers/software-private.h"
7
8#define SPRITE_NORMAL_LOOP(DEPTH, TYPE) \
9 SPRITE_YBASE_ ## DEPTH(inY); \
10 unsigned tileData; \
11 for (; outX < condition; ++outX, inX += xOffset) { \
12 renderer->spriteCyclesRemaining -= 1; \
13 SPRITE_XBASE_ ## DEPTH(inX); \
14 SPRITE_DRAW_PIXEL_ ## DEPTH ## _ ## TYPE(inX); \
15 }
16
17#define SPRITE_MOSAIC_LOOP(DEPTH, TYPE) \
18 SPRITE_YBASE_ ## DEPTH(inY); \
19 unsigned tileData; \
20 for (; outX < condition; ++outX, inX += xOffset) { \
21 int localX = inX - xOffset * (outX % mosaicH); \
22 if (localX < 0) { \
23 localX = 0; \
24 } else if (localX > width - 1) {\
25 localX = width - 1; \
26 } \
27 SPRITE_XBASE_ ## DEPTH(localX); \
28 SPRITE_DRAW_PIXEL_ ## DEPTH ## _ ## TYPE(localX); \
29 }
30
31#define SPRITE_TRANSFORMED_LOOP(DEPTH, TYPE) \
32 unsigned tileData; \
33 unsigned widthMask = ~(width - 1); \
34 unsigned heightMask = ~(height - 1); \
35 for (; outX < condition; ++outX, ++inX) { \
36 renderer->spriteCyclesRemaining -= 2; \
37 xAccum += mat.a; \
38 yAccum += mat.c; \
39 int localX = xAccum >> 8; \
40 int localY = yAccum >> 8; \
41 \
42 if (localX & widthMask || localY & heightMask) { \
43 break; \
44 } \
45 \
46 SPRITE_YBASE_ ## DEPTH(localY); \
47 SPRITE_XBASE_ ## DEPTH(localX); \
48 SPRITE_DRAW_PIXEL_ ## DEPTH ## _ ## TYPE(localX); \
49 }
50
51#define SPRITE_TRANSFORMED_MOSAIC_LOOP(DEPTH, TYPE) \
52 unsigned tileData; \
53 unsigned widthMask = ~(width - 1); \
54 unsigned heightMask = ~(height - 1); \
55 int localX = xAccum >> 8; \
56 int localY = yAccum >> 8; \
57 for (; outX < condition; ++outX, ++inX) { \
58 renderer->spriteCyclesRemaining -= 2; \
59 xAccum += mat.a; \
60 yAccum += mat.c; \
61 \
62 if (outX % mosaicH == 0) { \
63 localX = xAccum >> 8; \
64 localY = yAccum >> 8; \
65 } \
66 \
67 if (localX & widthMask || localY & heightMask) { \
68 continue; \
69 } \
70 \
71 SPRITE_YBASE_ ## DEPTH(localY); \
72 SPRITE_XBASE_ ## DEPTH(localX); \
73 SPRITE_DRAW_PIXEL_ ## DEPTH ## _ ## TYPE(localX); \
74 }
75
76#define SPRITE_XBASE_16(localX) unsigned xBase = (localX & ~0x7) * 4 + ((localX >> 1) & 2);
77#define SPRITE_YBASE_16(localY) unsigned yBase = (localY & ~0x7) * stride + (localY & 0x7) * 4;
78
79#define SPRITE_DRAW_PIXEL_16_NORMAL(localX) \
80 LOAD_16(tileData, ((yBase + charBase + xBase) & 0x7FFE), vramBase); \
81 tileData = (tileData >> ((localX & 3) << 2)) & 0xF; \
82 current = renderer->spriteLayer[outX]; \
83 if ((current & FLAG_ORDER_MASK) > flags) { \
84 if (tileData) { \
85 renderer->spriteLayer[outX] = palette[tileData] | flags; \
86 } else if (current != FLAG_UNWRITTEN) { \
87 renderer->spriteLayer[outX] = (current & ~FLAG_ORDER_MASK) | GBAObjAttributesCGetPriority(sprite->c) << OFFSET_PRIORITY; \
88 } \
89 }
90
91#define SPRITE_DRAW_PIXEL_16_NORMAL_OBJWIN(localX) \
92 LOAD_16(tileData, ((yBase + charBase + xBase) & 0x7FFE), vramBase); \
93 tileData = (tileData >> ((localX & 3) << 2)) & 0xF; \
94 current = renderer->spriteLayer[outX]; \
95 if ((current & FLAG_ORDER_MASK) > flags) { \
96 if (tileData) { \
97 unsigned color = (renderer->row[outX] & FLAG_OBJWIN) ? objwinPalette[tileData] : palette[tileData]; \
98 renderer->spriteLayer[outX] = color | flags; \
99 } else if (current != FLAG_UNWRITTEN) { \
100 renderer->spriteLayer[outX] = (current & ~FLAG_ORDER_MASK) | GBAObjAttributesCGetPriority(sprite->c) << OFFSET_PRIORITY; \
101 } \
102 }
103
104#define SPRITE_DRAW_PIXEL_16_OBJWIN(localX) \
105 LOAD_16(tileData, ((yBase + charBase + xBase) & 0x7FFE), vramBase); \
106 tileData = (tileData >> ((localX & 3) << 2)) & 0xF; \
107 if (tileData) { \
108 renderer->row[outX] |= FLAG_OBJWIN; \
109 }
110
111#define SPRITE_XBASE_256(localX) unsigned xBase = (localX & ~0x7) * 8 + (localX & 6);
112#define SPRITE_YBASE_256(localY) unsigned yBase = (localY & ~0x7) * stride + (localY & 0x7) * 8;
113
114#define SPRITE_DRAW_PIXEL_256_NORMAL(localX) \
115 LOAD_16(tileData, ((yBase + charBase + xBase) & 0x7FFE), vramBase); \
116 tileData = (tileData >> ((localX & 1) << 3)) & 0xFF; \
117 current = renderer->spriteLayer[outX]; \
118 if ((current & FLAG_ORDER_MASK) > flags) { \
119 if (tileData) { \
120 renderer->spriteLayer[outX] = palette[tileData] | flags; \
121 } else if (current != FLAG_UNWRITTEN) { \
122 renderer->spriteLayer[outX] = (current & ~FLAG_ORDER_MASK) | GBAObjAttributesCGetPriority(sprite->c) << OFFSET_PRIORITY; \
123 } \
124 }
125
126#define SPRITE_DRAW_PIXEL_256_NORMAL_OBJWIN(localX) \
127 LOAD_16(tileData, ((yBase + charBase + xBase) & 0x7FFE), vramBase); \
128 tileData = (tileData >> ((localX & 1) << 3)) & 0xFF; \
129 current = renderer->spriteLayer[outX]; \
130 if ((current & FLAG_ORDER_MASK) > flags) { \
131 if (tileData) { \
132 unsigned color = (renderer->row[outX] & FLAG_OBJWIN) ? objwinPalette[tileData] : palette[tileData]; \
133 renderer->spriteLayer[outX] = color | flags; \
134 } else if (current != FLAG_UNWRITTEN) { \
135 renderer->spriteLayer[outX] = (current & ~FLAG_ORDER_MASK) | GBAObjAttributesCGetPriority(sprite->c) << OFFSET_PRIORITY; \
136 } \
137 }
138
139#define SPRITE_DRAW_PIXEL_256_OBJWIN(localX) \
140 LOAD_16(tileData, ((yBase + charBase + xBase) & 0x7FFE), vramBase); \
141 tileData = (tileData >> ((localX & 1) << 3)) & 0xFF; \
142 if (tileData) { \
143 renderer->row[outX] |= FLAG_OBJWIN; \
144 }
145
146int GBAVideoSoftwareRendererPreprocessSprite(struct GBAVideoSoftwareRenderer* renderer, struct GBAObj* sprite, int index, int y) {
147 int width = GBAVideoObjSizes[GBAObjAttributesAGetShape(sprite->a) * 4 + GBAObjAttributesBGetSize(sprite->b)][0];
148 int height = GBAVideoObjSizes[GBAObjAttributesAGetShape(sprite->a) * 4 + GBAObjAttributesBGetSize(sprite->b)][1];
149 int start = renderer->start;
150 int end = renderer->end;
151 uint32_t flags = GBAObjAttributesCGetPriority(sprite->c) << OFFSET_PRIORITY;
152 flags |= FLAG_TARGET_1 * ((GBAWindowControlIsBlendEnable(renderer->currentWindow.packed) && renderer->target1Obj && renderer->blendEffect == BLEND_ALPHA) || GBAObjAttributesAGetMode(sprite->a) == OBJ_MODE_SEMITRANSPARENT);
153 flags |= FLAG_OBJWIN * (GBAObjAttributesAGetMode(sprite->a) == OBJ_MODE_OBJWIN);
154 if ((flags & FLAG_OBJWIN) && renderer->currentWindow.priority < renderer->objwin.priority) {
155 return 0;
156 }
157 int32_t x = (uint32_t) GBAObjAttributesBGetX(sprite->b) << 23;
158 x >>= 23;
159 x += renderer->objOffsetX;
160 uint16_t* vramBase = &renderer->d.vram[BASE_TILE >> 1];
161 bool align = GBAObjAttributesAIs256Color(sprite->a) && !GBARegisterDISPCNTIsObjCharacterMapping(renderer->dispcnt);
162 unsigned charBase = (GBAObjAttributesCGetTile(sprite->c) & ~align) * 0x20;
163 if (GBARegisterDISPCNTGetMode(renderer->dispcnt) >= 3 && GBAObjAttributesCGetTile(sprite->c) < 512) {
164 return 0;
165 }
166
167 int objwinSlowPath = GBARegisterDISPCNTIsObjwinEnable(renderer->dispcnt) && GBAWindowControlGetBlendEnable(renderer->objwin.packed) != GBAWindowControlIsBlendEnable(renderer->currentWindow.packed);
168 int variant = renderer->target1Obj &&
169 GBAWindowControlIsBlendEnable(renderer->currentWindow.packed) &&
170 (renderer->blendEffect == BLEND_BRIGHTEN || renderer->blendEffect == BLEND_DARKEN);
171 if (GBAObjAttributesAGetMode(sprite->a) == OBJ_MODE_SEMITRANSPARENT || objwinSlowPath) {
172 int target2 = renderer->target2Bd;
173 target2 |= renderer->bg[0].target2;
174 target2 |= renderer->bg[1].target2;
175 target2 |= renderer->bg[2].target2;
176 target2 |= renderer->bg[3].target2;
177 if (target2) {
178 flags |= FLAG_REBLEND;
179 variant = 0;
180 } else {
181 flags &= ~FLAG_TARGET_1;
182 }
183 }
184
185 color_t* palette = &renderer->normalPalette[0x100];
186 if (renderer->d.highlightAmount && renderer->d.highlightOBJ[index]) {
187 palette = &renderer->highlightPalette[0x100];
188 }
189 color_t* objwinPalette = palette;
190
191 if (variant) {
192 palette = &renderer->variantPalette[0x100];
193 if (renderer->d.highlightAmount && renderer->d.highlightOBJ[index]) {
194 palette = &renderer->highlightVariantPalette[0x100];
195 }
196 if (GBAWindowControlIsBlendEnable(renderer->objwin.packed)) {
197 objwinPalette = palette;
198 }
199 }
200
201 int inY = y - ((int) GBAObjAttributesAGetY(sprite->a) + renderer->objOffsetY);
202 int stride = GBARegisterDISPCNTIsObjCharacterMapping(renderer->dispcnt) ? (width >> !GBAObjAttributesAIs256Color(sprite->a)) : 0x80;
203
204 uint32_t current;
205 if (GBAObjAttributesAIsTransformed(sprite->a)) {
206 int totalWidth = width << GBAObjAttributesAGetDoubleSize(sprite->a);
207 int totalHeight = height << GBAObjAttributesAGetDoubleSize(sprite->a);
208 struct GBAOAMMatrix mat;
209 LOAD_16(mat.a, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].a);
210 LOAD_16(mat.b, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].b);
211 LOAD_16(mat.c, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].c);
212 LOAD_16(mat.d, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].d);
213
214 if (inY < 0) {
215 inY += 256;
216 }
217 int outX = x >= start ? x : start;
218 int condition = x + totalWidth;
219 int inX = outX - x;
220 if (end < condition) {
221 condition = end;
222 }
223 int mosaicH = 1;
224 if (GBAObjAttributesAIsMosaic(sprite->a)) {
225 mosaicH = GBAMosaicControlGetObjH(renderer->mosaic) + 1;
226 if (condition % mosaicH) {
227 condition += mosaicH - (condition % mosaicH);
228 }
229 }
230
231 int xAccum = mat.a * (inX - 1 - (totalWidth >> 1)) + mat.b * (inY - (totalHeight >> 1)) + (width << 7);
232 int yAccum = mat.c * (inX - 1 - (totalWidth >> 1)) + mat.d * (inY - (totalHeight >> 1)) + (height << 7);
233
234 // Clip off early pixels
235 // TODO: Transform end coordinates too
236 if (mat.a) {
237 if ((xAccum >> 8) < 0) {
238 int32_t diffX = -xAccum - 1;
239 int32_t x = mat.a ? diffX / mat.a : 0;
240 xAccum += mat.a * x;
241 yAccum += mat.c * x;
242 outX += x;
243 inX += x;
244 } else if ((xAccum >> 8) >= width) {
245 int32_t diffX = (width << 8) - xAccum;
246 int32_t x = mat.a ? diffX / mat.a : 0;
247 xAccum += mat.a * x;
248 yAccum += mat.c * x;
249 outX += x;
250 inX += x;
251 }
252 }
253 if (mat.c) {
254 if ((yAccum >> 8) < 0) {
255 int32_t diffY = - yAccum - 1;
256 int32_t y = mat.c ? diffY / mat.c : 0;
257 xAccum += mat.a * y;
258 yAccum += mat.c * y;
259 outX += y;
260 inX += y;
261 } else if ((yAccum >> 8) >= height) {
262 int32_t diffY = (height << 8) - yAccum;
263 int32_t y = mat.c ? diffY / mat.c : 0;
264 xAccum += mat.a * y;
265 yAccum += mat.c * y;
266 outX += y;
267 inX += y;
268 }
269 }
270
271 if (outX < start || outX >= condition) {
272 return 0;
273 }
274 renderer->spriteCyclesRemaining -= 10;
275
276 if (!GBAObjAttributesAIs256Color(sprite->a)) {
277 palette = &palette[GBAObjAttributesCGetPalette(sprite->c) << 4];
278 if (flags & FLAG_OBJWIN) {
279 SPRITE_TRANSFORMED_LOOP(16, OBJWIN);
280 } else if (mosaicH > 1) {
281 if (objwinSlowPath) {
282 objwinPalette = &objwinPalette[GBAObjAttributesCGetPalette(sprite->c) << 4];
283 SPRITE_TRANSFORMED_MOSAIC_LOOP(16, NORMAL_OBJWIN);
284 } else {
285 SPRITE_TRANSFORMED_MOSAIC_LOOP(16, NORMAL);
286 }
287 } else if (objwinSlowPath) {
288 objwinPalette = &objwinPalette[GBAObjAttributesCGetPalette(sprite->c) << 4];
289 SPRITE_TRANSFORMED_LOOP(16, NORMAL_OBJWIN);
290 } else {
291 SPRITE_TRANSFORMED_LOOP(16, NORMAL);
292 }
293 } else {
294 if (flags & FLAG_OBJWIN) {
295 SPRITE_TRANSFORMED_LOOP(256, OBJWIN);
296 } else if (mosaicH > 1) {
297 if (objwinSlowPath) {
298 SPRITE_TRANSFORMED_MOSAIC_LOOP(256, NORMAL_OBJWIN);
299 } else {
300 SPRITE_TRANSFORMED_MOSAIC_LOOP(256, NORMAL);
301 }
302 } else if (objwinSlowPath) {
303 SPRITE_TRANSFORMED_LOOP(256, NORMAL_OBJWIN);
304 } else {
305 SPRITE_TRANSFORMED_LOOP(256, NORMAL);
306 }
307 }
308 if (end == GBA_VIDEO_HORIZONTAL_PIXELS && x + totalWidth > GBA_VIDEO_HORIZONTAL_PIXELS) {
309 renderer->spriteCyclesRemaining -= (x + totalWidth - GBA_VIDEO_HORIZONTAL_PIXELS) * 2;
310 }
311 } else {
312 int outX = x >= start ? x : start;
313 int condition = x + width;
314 int mosaicH = 1;
315 if (GBAObjAttributesAIsMosaic(sprite->a)) {
316 mosaicH = GBAMosaicControlGetObjH(renderer->mosaic) + 1;
317 if (condition % mosaicH) {
318 condition += mosaicH - (condition % mosaicH);
319 }
320 }
321 if ((int) GBAObjAttributesAGetY(sprite->a) + height - 256 >= 0) {
322 inY += 256;
323 }
324 if (GBAObjAttributesBIsVFlip(sprite->b)) {
325 inY = height - inY - 1;
326 }
327 if (end < condition) {
328 condition = end;
329 }
330 int inX = outX - x;
331 int xOffset = 1;
332 if (GBAObjAttributesBIsHFlip(sprite->b)) {
333 inX = width - inX - 1;
334 xOffset = -1;
335 }
336 if (!GBAObjAttributesAIs256Color(sprite->a)) {
337 palette = &palette[GBAObjAttributesCGetPalette(sprite->c) << 4];
338 if (flags & FLAG_OBJWIN) {
339 SPRITE_NORMAL_LOOP(16, OBJWIN);
340 } else if (mosaicH > 1) {
341 if (objwinSlowPath) {
342 objwinPalette = &objwinPalette[GBAObjAttributesCGetPalette(sprite->c) << 4];
343 SPRITE_MOSAIC_LOOP(16, NORMAL_OBJWIN);
344 } else {
345 SPRITE_MOSAIC_LOOP(16, NORMAL);
346 }
347 } else if (objwinSlowPath) {
348 objwinPalette = &objwinPalette[GBAObjAttributesCGetPalette(sprite->c) << 4];
349 SPRITE_NORMAL_LOOP(16, NORMAL_OBJWIN);
350 } else {
351 SPRITE_NORMAL_LOOP(16, NORMAL);
352 }
353 } else {
354 if (flags & FLAG_OBJWIN) {
355 SPRITE_NORMAL_LOOP(256, OBJWIN);
356 } else if (mosaicH > 1) {
357 if (objwinSlowPath) {
358 SPRITE_MOSAIC_LOOP(256, NORMAL_OBJWIN);
359 } else {
360 SPRITE_MOSAIC_LOOP(256, NORMAL);
361 }
362 } else if (objwinSlowPath) {
363 SPRITE_NORMAL_LOOP(256, NORMAL_OBJWIN);
364 } else {
365 SPRITE_NORMAL_LOOP(256, NORMAL);
366 }
367 }
368 if (end == GBA_VIDEO_HORIZONTAL_PIXELS && x + width > GBA_VIDEO_HORIZONTAL_PIXELS) {
369 renderer->spriteCyclesRemaining -= x + width - GBA_VIDEO_HORIZONTAL_PIXELS;
370 }
371 }
372 return 1;
373}
374
375void GBAVideoSoftwareRendererPostprocessSprite(struct GBAVideoSoftwareRenderer* renderer, unsigned priority) {
376 int x;
377 uint32_t* pixel = &renderer->row[renderer->start];
378 uint32_t flags = FLAG_TARGET_2 * renderer->target2Obj;
379
380 int objwinSlowPath = GBARegisterDISPCNTIsObjwinEnable(renderer->dispcnt);
381 bool objwinDisable = false;
382 bool objwinOnly = false;
383 if (objwinSlowPath) {
384 objwinDisable = !GBAWindowControlIsObjEnable(renderer->objwin.packed);
385 objwinOnly = !objwinDisable && !GBAWindowControlIsObjEnable(renderer->currentWindow.packed);
386 if (objwinDisable && !GBAWindowControlIsObjEnable(renderer->currentWindow.packed)) {
387 return;
388 }
389
390 if (objwinDisable) {
391 for (x = renderer->start; x < renderer->end; ++x, ++pixel) {
392 uint32_t color = renderer->spriteLayer[x] & ~FLAG_OBJWIN;
393 uint32_t current = *pixel;
394 if ((color & FLAG_UNWRITTEN) != FLAG_UNWRITTEN && !(current & FLAG_OBJWIN) && (color & FLAG_PRIORITY) >> OFFSET_PRIORITY == priority) {
395 _compositeBlendObjwin(renderer, pixel, color | flags, current);
396 }
397 }
398 return;
399 } else if (objwinOnly) {
400 for (x = renderer->start; x < renderer->end; ++x, ++pixel) {
401 uint32_t color = renderer->spriteLayer[x] & ~FLAG_OBJWIN;
402 uint32_t current = *pixel;
403 if ((color & FLAG_UNWRITTEN) != FLAG_UNWRITTEN && (current & FLAG_OBJWIN) && (color & FLAG_PRIORITY) >> OFFSET_PRIORITY == priority) {
404 _compositeBlendObjwin(renderer, pixel, color | flags, current);
405 }
406 }
407 return;
408 } else {
409 for (x = renderer->start; x < renderer->end; ++x, ++pixel) {
410 uint32_t color = renderer->spriteLayer[x] & ~FLAG_OBJWIN;
411 uint32_t current = *pixel;
412 if ((color & FLAG_UNWRITTEN) != FLAG_UNWRITTEN && (color & FLAG_PRIORITY) >> OFFSET_PRIORITY == priority) {
413 _compositeBlendObjwin(renderer, pixel, color | flags, current);
414 }
415 }
416 return;
417 }
418 } else if (!GBAWindowControlIsObjEnable(renderer->currentWindow.packed)) {
419 return;
420 }
421 for (x = renderer->start; x < renderer->end; ++x, ++pixel) {
422 uint32_t color = renderer->spriteLayer[x] & ~FLAG_OBJWIN;
423 uint32_t current = *pixel;
424 if ((color & FLAG_UNWRITTEN) != FLAG_UNWRITTEN && (color & FLAG_PRIORITY) >> OFFSET_PRIORITY == priority) {
425 _compositeBlendNoObjwin(renderer, pixel, color | flags, current);
426 }
427 }
428}