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_XBASE_16(localX) unsigned xBase = (localX & ~0x7) * 4 + ((localX >> 1) & 2);
52#define SPRITE_YBASE_16(localY) unsigned yBase = (localY & ~0x7) * stride + (localY & 0x7) * 4;
53
54#define SPRITE_DRAW_PIXEL_16_NORMAL(localX) \
55 LOAD_16(tileData, ((yBase + charBase + xBase) & 0x7FFE), vramBase); \
56 tileData = (tileData >> ((localX & 3) << 2)) & 0xF; \
57 current = renderer->spriteLayer[outX]; \
58 if ((current & FLAG_UNWRITTEN) == FLAG_UNWRITTEN && tileData) { \
59 renderer->spriteLayer[outX] = palette[tileData] | flags; \
60 }
61
62#define SPRITE_DRAW_PIXEL_16_NORMAL_OBJWIN(localX) \
63 LOAD_16(tileData, ((yBase + charBase + xBase) & 0x7FFE), vramBase); \
64 tileData = (tileData >> ((localX & 3) << 2)) & 0xF; \
65 current = renderer->spriteLayer[outX]; \
66 if ((current & FLAG_UNWRITTEN) == FLAG_UNWRITTEN && tileData) { \
67 unsigned color = (renderer->row[outX] & FLAG_OBJWIN) ? objwinPalette[tileData] : palette[tileData]; \
68 renderer->spriteLayer[outX] = color | flags; \
69 }
70
71#define SPRITE_DRAW_PIXEL_16_OBJWIN(localX) \
72 LOAD_16(tileData, ((yBase + charBase + xBase) & 0x7FFE), vramBase); \
73 tileData = (tileData >> ((localX & 3) << 2)) & 0xF; \
74 if (tileData) { \
75 renderer->row[outX] |= FLAG_OBJWIN; \
76 }
77
78#define SPRITE_XBASE_256(localX) unsigned xBase = (localX & ~0x7) * 8 + (localX & 6);
79#define SPRITE_YBASE_256(localY) unsigned yBase = (localY & ~0x7) * stride + (localY & 0x7) * 8;
80
81#define SPRITE_DRAW_PIXEL_256_NORMAL(localX) \
82 LOAD_16(tileData, ((yBase + charBase + xBase) & 0x7FFE), vramBase); \
83 tileData = (tileData >> ((localX & 1) << 3)) & 0xFF; \
84 current = renderer->spriteLayer[outX]; \
85 if ((current & FLAG_UNWRITTEN) == FLAG_UNWRITTEN && tileData) { \
86 renderer->spriteLayer[outX] = palette[tileData] | flags; \
87 }
88
89#define SPRITE_DRAW_PIXEL_256_NORMAL_OBJWIN(localX) \
90 LOAD_16(tileData, ((yBase + charBase + xBase) & 0x7FFE), vramBase); \
91 tileData = (tileData >> ((localX & 1) << 3)) & 0xFF; \
92 current = renderer->spriteLayer[outX]; \
93 if ((current & FLAG_UNWRITTEN) == FLAG_UNWRITTEN && tileData) { \
94 unsigned color = (renderer->row[outX] & FLAG_OBJWIN) ? objwinPalette[tileData] : palette[tileData]; \
95 renderer->spriteLayer[outX] = color | flags; \
96 }
97
98#define SPRITE_DRAW_PIXEL_256_OBJWIN(localX) \
99 LOAD_16(tileData, ((yBase + charBase + xBase) & 0x7FFE), vramBase); \
100 tileData = (tileData >> ((localX & 1) << 3)) & 0xFF; \
101 if (tileData) { \
102 renderer->row[outX] |= FLAG_OBJWIN; \
103 }
104
105int GBAVideoSoftwareRendererPreprocessSprite(struct GBAVideoSoftwareRenderer* renderer, struct GBAObj* sprite, int y) {
106 int width = GBAVideoObjSizes[GBAObjAttributesAGetShape(sprite->a) * 4 + GBAObjAttributesBGetSize(sprite->b)][0];
107 int height = GBAVideoObjSizes[GBAObjAttributesAGetShape(sprite->a) * 4 + GBAObjAttributesBGetSize(sprite->b)][1];
108 int start = renderer->start;
109 int end = renderer->end;
110 uint32_t flags = GBAObjAttributesCGetPriority(sprite->c) << OFFSET_PRIORITY;
111 flags |= FLAG_TARGET_1 * ((GBAWindowControlIsBlendEnable(renderer->currentWindow.packed) && renderer->target1Obj && renderer->blendEffect == BLEND_ALPHA) || GBAObjAttributesAGetMode(sprite->a) == OBJ_MODE_SEMITRANSPARENT);
112 flags |= FLAG_OBJWIN * (GBAObjAttributesAGetMode(sprite->a) == OBJ_MODE_OBJWIN);
113 if ((flags & FLAG_OBJWIN) && renderer->currentWindow.priority < renderer->objwin.priority) {
114 return 0;
115 }
116 int32_t x = (uint32_t) GBAObjAttributesBGetX(sprite->b) << 23;
117 x >>= 23;
118 x += renderer->objOffsetX;
119 uint16_t* vramBase = &renderer->d.vram[BASE_TILE >> 1];
120 bool align = GBAObjAttributesAIs256Color(sprite->a) && !GBARegisterDISPCNTIsObjCharacterMapping(renderer->dispcnt);
121 unsigned charBase = (GBAObjAttributesCGetTile(sprite->c) & ~align) * 0x20;
122 if (GBARegisterDISPCNTGetMode(renderer->dispcnt) >= 3 && GBAObjAttributesCGetTile(sprite->c) < 512) {
123 return 0;
124 }
125
126 int objwinSlowPath = GBARegisterDISPCNTIsObjwinEnable(renderer->dispcnt) && GBAWindowControlGetBlendEnable(renderer->objwin.packed) != GBAWindowControlIsBlendEnable(renderer->currentWindow.packed);
127 int variant = renderer->target1Obj &&
128 GBAWindowControlIsBlendEnable(renderer->currentWindow.packed) &&
129 (renderer->blendEffect == BLEND_BRIGHTEN || renderer->blendEffect == BLEND_DARKEN);
130 if (GBAObjAttributesAGetMode(sprite->a) == OBJ_MODE_SEMITRANSPARENT || objwinSlowPath) {
131 int target2 = renderer->target2Bd;
132 target2 |= renderer->bg[0].target2;
133 target2 |= renderer->bg[1].target2;
134 target2 |= renderer->bg[2].target2;
135 target2 |= renderer->bg[3].target2;
136 if (target2) {
137 flags |= FLAG_REBLEND;
138 variant = 0;
139 } else {
140 flags &= ~FLAG_TARGET_1;
141 }
142 }
143
144 color_t* palette = &renderer->normalPalette[0x100];
145 color_t* objwinPalette = palette;
146
147 if (variant) {
148 palette = &renderer->variantPalette[0x100];
149 if (GBAWindowControlIsBlendEnable(renderer->objwin.packed)) {
150 objwinPalette = palette;
151 }
152 }
153
154 int inY = y - ((int) GBAObjAttributesAGetY(sprite->a) + renderer->objOffsetY);
155 int stride = GBARegisterDISPCNTIsObjCharacterMapping(renderer->dispcnt) ? (width >> !GBAObjAttributesAIs256Color(sprite->a)) : 0x80;
156
157 uint32_t current;
158 if (GBAObjAttributesAIsTransformed(sprite->a)) {
159 int totalWidth = width << GBAObjAttributesAGetDoubleSize(sprite->a);
160 int totalHeight = height << GBAObjAttributesAGetDoubleSize(sprite->a);
161 struct GBAOAMMatrix mat;
162 LOAD_16(mat.a, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].a);
163 LOAD_16(mat.b, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].b);
164 LOAD_16(mat.c, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].c);
165 LOAD_16(mat.d, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].d);
166
167 if (inY < 0) {
168 inY += 256;
169 }
170 int outX = x >= start ? x : start;
171 int condition = x + totalWidth;
172 int inX = outX - x;
173 if (end < condition) {
174 condition = end;
175 }
176
177 int xAccum = mat.a * (inX - 1 - (totalWidth >> 1)) + mat.b * (inY - (totalHeight >> 1)) + (width << 7);
178 int yAccum = mat.c * (inX - 1 - (totalWidth >> 1)) + mat.d * (inY - (totalHeight >> 1)) + (height << 7);
179
180 // Clip off early pixels
181 // TODO: Transform end coordinates too
182 if (mat.a) {
183 if ((xAccum >> 8) < 0) {
184 int32_t diffX = -xAccum - 1;
185 int32_t x = mat.a ? diffX / mat.a : 0;
186 xAccum += mat.a * x;
187 yAccum += mat.c * x;
188 outX += x;
189 inX += x;
190 } else if ((xAccum >> 8) >= width) {
191 int32_t diffX = (width << 8) - xAccum;
192 int32_t x = mat.a ? diffX / mat.a : 0;
193 xAccum += mat.a * x;
194 yAccum += mat.c * x;
195 outX += x;
196 inX += x;
197 }
198 }
199 if (mat.c) {
200 if ((yAccum >> 8) < 0) {
201 int32_t diffY = - yAccum - 1;
202 int32_t y = mat.c ? diffY / mat.c : 0;
203 xAccum += mat.a * y;
204 yAccum += mat.c * y;
205 outX += y;
206 inX += y;
207 } else if ((yAccum >> 8) >= height) {
208 int32_t diffY = (height << 8) - yAccum;
209 int32_t y = mat.c ? diffY / mat.c : 0;
210 xAccum += mat.a * y;
211 yAccum += mat.c * y;
212 outX += y;
213 inX += y;
214 }
215 }
216
217 if (outX < start || outX >= condition) {
218 return 0;
219 }
220 renderer->spriteCyclesRemaining -= 10;
221
222 if (!GBAObjAttributesAIs256Color(sprite->a)) {
223 palette = &palette[GBAObjAttributesCGetPalette(sprite->c) << 4];
224 if (flags & FLAG_OBJWIN) {
225 SPRITE_TRANSFORMED_LOOP(16, OBJWIN);
226 } else if (objwinSlowPath) {
227 objwinPalette = &objwinPalette[GBAObjAttributesCGetPalette(sprite->c) << 4];
228 SPRITE_TRANSFORMED_LOOP(16, NORMAL_OBJWIN);
229 } else {
230 SPRITE_TRANSFORMED_LOOP(16, NORMAL);
231 }
232 } else {
233 if (flags & FLAG_OBJWIN) {
234 SPRITE_TRANSFORMED_LOOP(256, OBJWIN);
235 } else if (objwinSlowPath) {
236 SPRITE_TRANSFORMED_LOOP(256, NORMAL_OBJWIN);
237 } else {
238 SPRITE_TRANSFORMED_LOOP(256, NORMAL);
239 }
240 }
241 if (end == GBA_VIDEO_HORIZONTAL_PIXELS && x + totalWidth > GBA_VIDEO_HORIZONTAL_PIXELS) {
242 renderer->spriteCyclesRemaining -= (x + totalWidth - GBA_VIDEO_HORIZONTAL_PIXELS) * 2;
243 }
244 } else {
245 int outX = x >= start ? x : start;
246 int condition = x + width;
247 int mosaicH = 1;
248 if (GBAObjAttributesAIsMosaic(sprite->a)) {
249 mosaicH = GBAMosaicControlGetObjH(renderer->mosaic) + 1;
250 if (condition % mosaicH) {
251 condition += mosaicH - (condition % mosaicH);
252 }
253 }
254 if ((int) GBAObjAttributesAGetY(sprite->a) + height - 256 >= 0) {
255 inY += 256;
256 }
257 if (GBAObjAttributesBIsVFlip(sprite->b)) {
258 inY = height - inY - 1;
259 }
260 if (end < condition) {
261 condition = end;
262 }
263 int inX = outX - x;
264 int xOffset = 1;
265 if (GBAObjAttributesBIsHFlip(sprite->b)) {
266 inX = width - inX - 1;
267 xOffset = -1;
268 }
269 if (!GBAObjAttributesAIs256Color(sprite->a)) {
270 palette = &palette[GBAObjAttributesCGetPalette(sprite->c) << 4];
271 if (flags & FLAG_OBJWIN) {
272 SPRITE_NORMAL_LOOP(16, OBJWIN);
273 } else if (mosaicH > 1) {
274 if (objwinSlowPath) {
275 objwinPalette = &objwinPalette[GBAObjAttributesCGetPalette(sprite->c) << 4];
276 SPRITE_MOSAIC_LOOP(16, NORMAL_OBJWIN);
277 } else {
278 SPRITE_MOSAIC_LOOP(16, NORMAL);
279 }
280 } else if (objwinSlowPath) {
281 objwinPalette = &objwinPalette[GBAObjAttributesCGetPalette(sprite->c) << 4];
282 SPRITE_NORMAL_LOOP(16, NORMAL_OBJWIN);
283 } else {
284 SPRITE_NORMAL_LOOP(16, NORMAL);
285 }
286 } else {
287 if (flags & FLAG_OBJWIN) {
288 SPRITE_NORMAL_LOOP(256, OBJWIN);
289 } else if (mosaicH > 1) {
290 if (objwinSlowPath) {
291 SPRITE_MOSAIC_LOOP(256, NORMAL_OBJWIN);
292 } else {
293 SPRITE_MOSAIC_LOOP(256, NORMAL);
294 }
295 } else if (objwinSlowPath) {
296 SPRITE_NORMAL_LOOP(256, NORMAL_OBJWIN);
297 } else {
298 SPRITE_NORMAL_LOOP(256, NORMAL);
299 }
300 }
301 if (end == GBA_VIDEO_HORIZONTAL_PIXELS && x + width > GBA_VIDEO_HORIZONTAL_PIXELS) {
302 renderer->spriteCyclesRemaining -= x + width - GBA_VIDEO_HORIZONTAL_PIXELS;
303 }
304 }
305 return 1;
306}
307
308void GBAVideoSoftwareRendererPostprocessSprite(struct GBAVideoSoftwareRenderer* renderer, unsigned priority) {
309 int x;
310 uint32_t* pixel = &renderer->row[renderer->start];
311 uint32_t flags = FLAG_TARGET_2 * renderer->target2Obj;
312
313 int objwinSlowPath = GBARegisterDISPCNTIsObjwinEnable(renderer->dispcnt);
314 bool objwinDisable = false;
315 bool objwinOnly = false;
316 if (objwinSlowPath) {
317 objwinDisable = !GBAWindowControlIsObjEnable(renderer->objwin.packed);
318 objwinOnly = !objwinDisable && !GBAWindowControlIsObjEnable(renderer->currentWindow.packed);
319 if (objwinDisable && !GBAWindowControlIsObjEnable(renderer->currentWindow.packed)) {
320 return;
321 }
322
323 if (objwinDisable) {
324 for (x = renderer->start; x < renderer->end; ++x, ++pixel) {
325 uint32_t color = renderer->spriteLayer[x] & ~FLAG_OBJWIN;
326 uint32_t current = *pixel;
327 if ((color & FLAG_UNWRITTEN) != FLAG_UNWRITTEN && !(current & FLAG_OBJWIN) && (color & FLAG_PRIORITY) >> OFFSET_PRIORITY == priority) {
328 _compositeBlendObjwin(renderer, pixel, color | flags, current);
329 }
330 }
331 return;
332 } else if (objwinOnly) {
333 for (x = renderer->start; x < renderer->end; ++x, ++pixel) {
334 uint32_t color = renderer->spriteLayer[x] & ~FLAG_OBJWIN;
335 uint32_t current = *pixel;
336 if ((color & FLAG_UNWRITTEN) != FLAG_UNWRITTEN && (current & FLAG_OBJWIN) && (color & FLAG_PRIORITY) >> OFFSET_PRIORITY == priority) {
337 _compositeBlendObjwin(renderer, pixel, color | flags, current);
338 }
339 }
340 return;
341 } else {
342 for (x = renderer->start; x < renderer->end; ++x, ++pixel) {
343 uint32_t color = renderer->spriteLayer[x] & ~FLAG_OBJWIN;
344 uint32_t current = *pixel;
345 if ((color & FLAG_UNWRITTEN) != FLAG_UNWRITTEN && (color & FLAG_PRIORITY) >> OFFSET_PRIORITY == priority) {
346 _compositeBlendObjwin(renderer, pixel, color | flags, current);
347 }
348 }
349 return;
350 }
351 } else if (!GBAWindowControlIsObjEnable(renderer->currentWindow.packed)) {
352 return;
353 }
354 for (x = renderer->start; x < renderer->end; ++x, ++pixel) {
355 uint32_t color = renderer->spriteLayer[x] & ~FLAG_OBJWIN;
356 uint32_t current = *pixel;
357 if ((color & FLAG_UNWRITTEN) != FLAG_UNWRITTEN && (color & FLAG_PRIORITY) >> OFFSET_PRIORITY == priority) {
358 _compositeBlendNoObjwin(renderer, pixel, color | flags, current);
359 }
360 }
361}