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