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