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