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