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 = 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 struct GBAOAMMatrix mat;
182 LOAD_16(mat.a, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].a);
183 LOAD_16(mat.b, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].b);
184 LOAD_16(mat.c, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].c);
185 LOAD_16(mat.d, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].d);
186
187 if (inY < 0) {
188 inY += 256;
189 }
190 int outX = x >= start ? x : start;
191 int inX = outX - x;
192 int xAccum = mat.a * (inX - 1 - (totalWidth >> 1)) + mat.b * (inY - (totalHeight >> 1));
193 int yAccum = mat.c * (inX - 1 - (totalWidth >> 1)) + mat.d * (inY - (totalHeight >> 1));
194
195 if (!GBAObjAttributesAIs256Color(sprite->a)) {
196 palette = &palette[GBAObjAttributesCGetPalette(sprite->c) << 4];
197 if (flags & FLAG_OBJWIN) {
198 SPRITE_TRANSFORMED_LOOP(16, OBJWIN);
199 } else if (objwinSlowPath) {
200 objwinPalette = &objwinPalette[GBAObjAttributesCGetPalette(sprite->c) << 4];
201 SPRITE_TRANSFORMED_LOOP(16, NORMAL_OBJWIN);
202 } else {
203 SPRITE_TRANSFORMED_LOOP(16, NORMAL);
204 }
205 } else {
206 if (flags & FLAG_OBJWIN) {
207 SPRITE_TRANSFORMED_LOOP(256, OBJWIN);
208 } else if (objwinSlowPath) {
209 SPRITE_TRANSFORMED_LOOP(256, NORMAL_OBJWIN);
210 } else {
211 SPRITE_TRANSFORMED_LOOP(256, NORMAL);
212 }
213 }
214 } else {
215 int outX = x >= start ? x : start;
216 int condition = x + width;
217 int mosaicH = 1;
218 if (GBAObjAttributesAIsMosaic(sprite->a)) {
219 mosaicH = GBAMosaicControlGetObjH(renderer->mosaic) + 1;
220 if (condition % mosaicH) {
221 condition += mosaicH - (condition % mosaicH);
222 }
223 }
224 if ((int) GBAObjAttributesAGetY(sprite->a) + height - 256 >= 0) {
225 inY += 256;
226 }
227 if (GBAObjAttributesBIsVFlip(sprite->b)) {
228 inY = height - inY - 1;
229 }
230 if (end < condition) {
231 condition = end;
232 }
233 int inX = outX - x;
234 int xOffset = 1;
235 if (GBAObjAttributesBIsHFlip(sprite->b)) {
236 inX = width - inX - 1;
237 xOffset = -1;
238 }
239 if (!GBAObjAttributesAIs256Color(sprite->a)) {
240 palette = &palette[GBAObjAttributesCGetPalette(sprite->c) << 4];
241 if (flags & FLAG_OBJWIN) {
242 SPRITE_NORMAL_LOOP(16, OBJWIN);
243 } else if (GBAObjAttributesAIsMosaic(sprite->a)) {
244 if (objwinSlowPath) {
245 objwinPalette = &objwinPalette[GBAObjAttributesCGetPalette(sprite->c) << 4];
246 SPRITE_MOSAIC_LOOP(16, NORMAL_OBJWIN);
247 } else {
248 SPRITE_MOSAIC_LOOP(16, NORMAL);
249 }
250 } else if (objwinSlowPath) {
251 objwinPalette = &objwinPalette[GBAObjAttributesCGetPalette(sprite->c) << 4];
252 SPRITE_NORMAL_LOOP(16, NORMAL_OBJWIN);
253 } else {
254 SPRITE_NORMAL_LOOP(16, NORMAL);
255 }
256 } else {
257 if (flags & FLAG_OBJWIN) {
258 SPRITE_NORMAL_LOOP(256, OBJWIN);
259 } else if (GBAObjAttributesAIsMosaic(sprite->a)) {
260 if (objwinSlowPath) {
261 objwinPalette = &objwinPalette[GBAObjAttributesCGetPalette(sprite->c) << 4];
262 SPRITE_MOSAIC_LOOP(256, NORMAL_OBJWIN);
263 } else {
264 SPRITE_MOSAIC_LOOP(256, NORMAL);
265 }
266 } else if (objwinSlowPath) {
267 SPRITE_NORMAL_LOOP(256, NORMAL_OBJWIN);
268 } else {
269 SPRITE_NORMAL_LOOP(256, NORMAL);
270 }
271 }
272 }
273 return 1;
274}
275
276void GBAVideoSoftwareRendererPostprocessSprite(struct GBAVideoSoftwareRenderer* renderer, unsigned priority) {
277 int x;
278 uint32_t* pixel = &renderer->row[renderer->start];
279 uint32_t flags = FLAG_TARGET_2 * renderer->target2Obj;
280
281 int objwinSlowPath = GBARegisterDISPCNTIsObjwinEnable(renderer->dispcnt);
282 bool objwinDisable = false;
283 bool objwinOnly = false;
284 if (objwinSlowPath) {
285 objwinDisable = !GBAWindowControlIsObjEnable(renderer->objwin.packed);
286 objwinOnly = !objwinDisable && !GBAWindowControlIsObjEnable(renderer->currentWindow.packed);
287 if (objwinDisable && !GBAWindowControlIsObjEnable(renderer->currentWindow.packed)) {
288 return;
289 }
290
291 if (objwinDisable) {
292 for (x = renderer->start; x < renderer->end; ++x, ++pixel) {
293 uint32_t color = renderer->spriteLayer[x] & ~FLAG_OBJWIN;
294 uint32_t current = *pixel;
295 if ((color & FLAG_UNWRITTEN) != FLAG_UNWRITTEN && !(current & FLAG_OBJWIN) && (color & FLAG_PRIORITY) >> OFFSET_PRIORITY == priority) {
296 _compositeBlendObjwin(renderer, pixel, color | flags, current);
297 }
298 }
299 return;
300 } else if (objwinOnly) {
301 for (x = renderer->start; x < renderer->end; ++x, ++pixel) {
302 uint32_t color = renderer->spriteLayer[x] & ~FLAG_OBJWIN;
303 uint32_t current = *pixel;
304 if ((color & FLAG_UNWRITTEN) != FLAG_UNWRITTEN && (current & FLAG_OBJWIN) && (color & FLAG_PRIORITY) >> OFFSET_PRIORITY == priority) {
305 _compositeBlendObjwin(renderer, pixel, color | flags, current);
306 }
307 }
308 return;
309 } else {
310 for (x = renderer->start; x < renderer->end; ++x, ++pixel) {
311 uint32_t color = renderer->spriteLayer[x] & ~FLAG_OBJWIN;
312 uint32_t current = *pixel;
313 if ((color & FLAG_UNWRITTEN) != FLAG_UNWRITTEN && (color & FLAG_PRIORITY) >> OFFSET_PRIORITY == priority) {
314 _compositeBlendObjwin(renderer, pixel, color | flags, current);
315 }
316 }
317 return;
318 }
319 } else if (!GBAWindowControlIsObjEnable(renderer->currentWindow.packed)) {
320 return;
321 }
322 for (x = renderer->start; x < renderer->end; ++x, ++pixel) {
323 uint32_t color = renderer->spriteLayer[x] & ~FLAG_OBJWIN;
324 uint32_t current = *pixel;
325 if ((color & FLAG_UNWRITTEN) != FLAG_UNWRITTEN && (color & FLAG_PRIORITY) >> OFFSET_PRIORITY == priority) {
326 _compositeBlendNoObjwin(renderer, pixel, color | flags, current);
327 }
328 }
329}