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 (!renderer->d.vramOBJ[charBase >> VRAM_BLOCK_OFFSET]) {
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 (GBAObjAttributesAIs256Color(sprite->a) && renderer->objExtPalette) {
206 palette = renderer->objExtPalette;
207 } else if (variant) {
208 palette = &renderer->variantPalette[0x100];
209 if (GBAWindowControlIsBlendEnable(renderer->objwin.packed)) {
210 objwinPalette = palette;
211 }
212 }
213
214 int inY = y - (int) GBAObjAttributesAGetY(sprite->a);
215 int stride = GBARegisterDISPCNTIsObjCharacterMapping(renderer->dispcnt) ? (width >> !GBAObjAttributesAIs256Color(sprite->a)) : 0x80;
216
217 uint32_t current;
218 if (GBAObjAttributesAIsTransformed(sprite->a)) {
219 int totalWidth = width << GBAObjAttributesAGetDoubleSize(sprite->a);
220 int totalHeight = height << GBAObjAttributesAGetDoubleSize(sprite->a);
221 renderer->spriteCyclesRemaining -= 10;
222 struct GBAOAMMatrix mat;
223 LOAD_16(mat.a, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].a);
224 LOAD_16(mat.b, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].b);
225 LOAD_16(mat.c, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].c);
226 LOAD_16(mat.d, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].d);
227
228 if (inY < 0) {
229 inY += 256;
230 }
231 int outX = x >= start ? x : start;
232 int condition = x + totalWidth;
233 int inX = outX - x;
234 if (end < condition) {
235 condition = end;
236 }
237
238 int xAccum = mat.a * (inX - 1 - (totalWidth >> 1)) + mat.b * (inY - (totalHeight >> 1)) + (width << 7);
239 int yAccum = mat.c * (inX - 1 - (totalWidth >> 1)) + mat.d * (inY - (totalHeight >> 1)) + (height << 7);
240
241 // Clip off early pixels
242 // TODO: Transform end coordinates too
243 if (mat.a) {
244 if ((xAccum >> 8) < 0) {
245 int32_t diffX = -xAccum - 1;
246 int32_t x = mat.a ? diffX / mat.a : 0;
247 xAccum += mat.a * x;
248 yAccum += mat.c * x;
249 outX += x;
250 inX += x;
251 } else if ((xAccum >> 8) >= width) {
252 int32_t diffX = (width << 8) - xAccum;
253 int32_t x = mat.a ? diffX / mat.a : 0;
254 xAccum += mat.a * x;
255 yAccum += mat.c * x;
256 outX += x;
257 inX += x;
258 }
259 }
260 if (mat.c) {
261 if ((yAccum >> 8) < 0) {
262 int32_t diffY = - yAccum - 1;
263 int32_t y = mat.c ? diffY / mat.c : 0;
264 xAccum += mat.a * y;
265 yAccum += mat.c * y;
266 outX += y;
267 inX += y;
268 } else if ((yAccum >> 8) >= height) {
269 int32_t diffY = (height << 8) - yAccum;
270 int32_t y = mat.c ? diffY / mat.c : 0;
271 xAccum += mat.a * y;
272 yAccum += mat.c * y;
273 outX += y;
274 inX += y;
275 }
276 }
277
278 if (outX < start || outX >= condition) {
279 return 0;
280 }
281
282 if (!GBAObjAttributesAIs256Color(sprite->a)) {
283 palette = &palette[GBAObjAttributesCGetPalette(sprite->c) << 4];
284 if (flags & FLAG_OBJWIN) {
285 SPRITE_TRANSFORMED_LOOP(16, OBJWIN);
286 } else if (objwinSlowPath) {
287 objwinPalette = &objwinPalette[GBAObjAttributesCGetPalette(sprite->c) << 4];
288 SPRITE_TRANSFORMED_LOOP(16, NORMAL_OBJWIN);
289 } else {
290 SPRITE_TRANSFORMED_LOOP(16, NORMAL);
291 }
292 } else {
293 if (flags & FLAG_OBJWIN) {
294 SPRITE_TRANSFORMED_LOOP(256, OBJWIN);
295 } else if (objwinSlowPath) {
296 SPRITE_TRANSFORMED_LOOP(256, NORMAL_OBJWIN);
297 } else {
298 SPRITE_TRANSFORMED_LOOP(256, NORMAL);
299 }
300 }
301 if (x + totalWidth > renderer->masterEnd) {
302 renderer->spriteCyclesRemaining -= (x + totalWidth - renderer->masterEnd) * 2;
303 }
304 } else {
305 int outX = x >= start ? x : start;
306 int condition = x + width;
307 int mosaicH = 1;
308 if (GBAObjAttributesAIsMosaic(sprite->a)) {
309 mosaicH = GBAMosaicControlGetObjH(renderer->mosaic) + 1;
310 if (condition % mosaicH) {
311 condition += mosaicH - (condition % mosaicH);
312 }
313 }
314 if ((int) GBAObjAttributesAGetY(sprite->a) + height - 256 >= 0) {
315 inY += 256;
316 }
317 if (GBAObjAttributesBIsVFlip(sprite->b)) {
318 inY = height - inY - 1;
319 }
320 if (end < condition) {
321 condition = end;
322 }
323 int inX = outX - x;
324 int xOffset = 1;
325 if (GBAObjAttributesBIsHFlip(sprite->b)) {
326 inX = width - inX - 1;
327 xOffset = -1;
328 }
329 if (!GBAObjAttributesAIs256Color(sprite->a)) {
330 palette = &palette[GBAObjAttributesCGetPalette(sprite->c) << 4];
331 if (flags & FLAG_OBJWIN) {
332 SPRITE_NORMAL_LOOP(16, OBJWIN);
333 } else if (mosaicH > 1) {
334 if (objwinSlowPath) {
335 objwinPalette = &objwinPalette[GBAObjAttributesCGetPalette(sprite->c) << 4];
336 SPRITE_MOSAIC_LOOP(16, NORMAL_OBJWIN);
337 } else {
338 SPRITE_MOSAIC_LOOP(16, NORMAL);
339 }
340 } else if (objwinSlowPath) {
341 objwinPalette = &objwinPalette[GBAObjAttributesCGetPalette(sprite->c) << 4];
342 SPRITE_NORMAL_LOOP(16, NORMAL_OBJWIN);
343 } else {
344 SPRITE_NORMAL_LOOP(16, NORMAL);
345 }
346 } else {
347 if (flags & FLAG_OBJWIN) {
348 SPRITE_NORMAL_LOOP(256, OBJWIN);
349 } else if (mosaicH > 1) {
350 if (objwinSlowPath) {
351 objwinPalette = &objwinPalette[GBAObjAttributesCGetPalette(sprite->c) << 4];
352 SPRITE_MOSAIC_LOOP(256, NORMAL_OBJWIN);
353 } else {
354 SPRITE_MOSAIC_LOOP(256, NORMAL);
355 }
356 } else if (objwinSlowPath) {
357 SPRITE_NORMAL_LOOP(256, NORMAL_OBJWIN);
358 } else {
359 SPRITE_NORMAL_LOOP(256, NORMAL);
360 }
361 }
362 if (x + width > renderer->masterEnd) {
363 renderer->spriteCyclesRemaining -= x + width - renderer->masterEnd;
364 }
365 }
366 return 1;
367}
368
369void GBAVideoSoftwareRendererPostprocessSprite(struct GBAVideoSoftwareRenderer* renderer, unsigned priority) {
370 int x;
371 uint32_t* pixel = &renderer->row[renderer->start];
372 uint32_t flags = FLAG_TARGET_2 * renderer->target2Obj;
373
374 int objwinSlowPath = GBARegisterDISPCNTIsObjwinEnable(renderer->dispcnt);
375 bool objwinDisable = false;
376 bool objwinOnly = false;
377 if (objwinSlowPath) {
378 objwinDisable = !GBAWindowControlIsObjEnable(renderer->objwin.packed);
379 objwinOnly = !objwinDisable && !GBAWindowControlIsObjEnable(renderer->currentWindow.packed);
380 if (objwinDisable && !GBAWindowControlIsObjEnable(renderer->currentWindow.packed)) {
381 return;
382 }
383
384 if (objwinDisable) {
385 for (x = renderer->start; x < renderer->end; ++x, ++pixel) {
386 uint32_t color = renderer->spriteLayer[x] & ~FLAG_OBJWIN;
387 uint32_t current = *pixel;
388 if ((color & FLAG_UNWRITTEN) != FLAG_UNWRITTEN && !(current & FLAG_OBJWIN) && (color & FLAG_PRIORITY) >> OFFSET_PRIORITY == priority) {
389 _compositeBlendObjwin(renderer, pixel, color | flags, current);
390 }
391 }
392 return;
393 } else if (objwinOnly) {
394 for (x = renderer->start; x < renderer->end; ++x, ++pixel) {
395 uint32_t color = renderer->spriteLayer[x] & ~FLAG_OBJWIN;
396 uint32_t current = *pixel;
397 if ((color & FLAG_UNWRITTEN) != FLAG_UNWRITTEN && (current & FLAG_OBJWIN) && (color & FLAG_PRIORITY) >> OFFSET_PRIORITY == priority) {
398 _compositeBlendObjwin(renderer, pixel, color | flags, current);
399 }
400 }
401 return;
402 } else {
403 for (x = renderer->start; x < renderer->end; ++x, ++pixel) {
404 uint32_t color = renderer->spriteLayer[x] & ~FLAG_OBJWIN;
405 uint32_t current = *pixel;
406 if ((color & FLAG_UNWRITTEN) != FLAG_UNWRITTEN && (color & FLAG_PRIORITY) >> OFFSET_PRIORITY == priority) {
407 _compositeBlendObjwin(renderer, pixel, color | flags, current);
408 }
409 }
410 return;
411 }
412 } else if (!GBAWindowControlIsObjEnable(renderer->currentWindow.packed)) {
413 return;
414 }
415 for (x = renderer->start; x < renderer->end; ++x, ++pixel) {
416 uint32_t color = renderer->spriteLayer[x] & ~FLAG_OBJWIN;
417 uint32_t current = *pixel;
418 if ((color & FLAG_UNWRITTEN) != FLAG_UNWRITTEN && (color & FLAG_PRIORITY) >> OFFSET_PRIORITY == priority) {
419 _compositeBlendNoObjwin(renderer, pixel, color | flags, current);
420 }
421 }
422}