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