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