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 for (; outX < x + totalWidth && outX < end; ++outX, ++inX) { \
46 if (!(renderer->row[outX] & FLAG_UNWRITTEN)) { \
47 continue; \
48 } \
49 xAccum += mat.a; \
50 yAccum += mat.c; \
51 int localX = (xAccum >> 8) + (width >> 1); \
52 int localY = (yAccum >> 8) + (height >> 1); \
53 \
54 if (localX < 0 || localX >= width || localY < 0 || localY >= height) { \
55 continue; \
56 } \
57 \
58 SPRITE_YBASE_ ## DEPTH(localY); \
59 SPRITE_XBASE_ ## DEPTH(localX); \
60 SPRITE_DRAW_PIXEL_ ## DEPTH ## _ ## TYPE(localX); \
61 }
62
63#define SPRITE_XBASE_16(localX) unsigned xBase = (localX & ~0x7) * 4 + ((localX >> 1) & 2);
64#define SPRITE_YBASE_16(localY) unsigned yBase = (localY & ~0x7) * (GBARegisterDISPCNTIsObjCharacterMapping(renderer->dispcnt) ? width >> 1 : 0x80) + (localY & 0x7) * 4;
65
66#define SPRITE_DRAW_PIXEL_16_NORMAL(localX) \
67 LOAD_16(tileData, ((yBase + charBase + xBase) & 0x7FFE), vramBase); \
68 tileData = (tileData >> ((localX & 3) << 2)) & 0xF; \
69 current = renderer->spriteLayer[outX]; \
70 if ((current & FLAG_ORDER_MASK) > flags) { \
71 if (tileData) { \
72 renderer->spriteLayer[outX] = palette[tileData] | flags; \
73 } else if (current != FLAG_UNWRITTEN) { \
74 renderer->spriteLayer[outX] = (current & ~FLAG_ORDER_MASK) | GBAObjAttributesCGetPriority(sprite->c) << OFFSET_PRIORITY; \
75 } \
76 }
77
78#define SPRITE_DRAW_PIXEL_16_OBJWIN(localX) \
79 LOAD_16(tileData, ((yBase + charBase + xBase) & 0x7FFE), vramBase); \
80 tileData = (tileData >> ((localX & 3) << 2)) & 0xF; \
81 if (tileData) { \
82 renderer->row[outX] |= FLAG_OBJWIN; \
83 }
84
85#define SPRITE_XBASE_256(localX) unsigned xBase = (localX & ~0x7) * 8 + (localX & 6);
86#define SPRITE_YBASE_256(localY) unsigned yBase = (localY & ~0x7) * (GBARegisterDISPCNTIsObjCharacterMapping(renderer->dispcnt) ? width : 0x80) + (localY & 0x7) * 8;
87
88#define SPRITE_DRAW_PIXEL_256_NORMAL(localX) \
89 LOAD_16(tileData, ((yBase + charBase + xBase) & 0x7FFE), vramBase); \
90 tileData = (tileData >> ((localX & 1) << 3)) & 0xFF; \
91 current = renderer->spriteLayer[outX]; \
92 if ((current & FLAG_ORDER_MASK) > flags) { \
93 if (tileData) { \
94 renderer->spriteLayer[outX] = palette[tileData] | flags; \
95 } else if (current != FLAG_UNWRITTEN) { \
96 renderer->spriteLayer[outX] = (current & ~FLAG_ORDER_MASK) | GBAObjAttributesCGetPriority(sprite->c) << OFFSET_PRIORITY; \
97 } \
98 }
99
100#define SPRITE_DRAW_PIXEL_256_OBJWIN(localX) \
101 LOAD_16(tileData, ((yBase + charBase + xBase) & 0x7FFE), vramBase); \
102 tileData = (tileData >> ((localX & 1) << 3)) & 0xFF; \
103 if (tileData) { \
104 renderer->row[outX] |= FLAG_OBJWIN; \
105 }
106
107int GBAVideoSoftwareRendererPreprocessSprite(struct GBAVideoSoftwareRenderer* renderer, struct GBAObj* sprite, int y) {
108 int width = GBAVideoObjSizes[GBAObjAttributesAGetShape(sprite->a) * 4 + GBAObjAttributesBGetSize(sprite->b)][0];
109 int height = GBAVideoObjSizes[GBAObjAttributesAGetShape(sprite->a) * 4 + GBAObjAttributesBGetSize(sprite->b)][1];
110 int start = renderer->start;
111 int end = renderer->end;
112 uint32_t flags = GBAObjAttributesCGetPriority(sprite->c) << OFFSET_PRIORITY;
113 flags |= FLAG_TARGET_1 * ((GBAWindowControlIsBlendEnable(renderer->currentWindow.packed) && renderer->target1Obj && renderer->blendEffect == BLEND_ALPHA) || GBAObjAttributesAGetMode(sprite->a) == OBJ_MODE_SEMITRANSPARENT);
114 flags |= FLAG_OBJWIN * (GBAObjAttributesAGetMode(sprite->a) == OBJ_MODE_OBJWIN);
115 int32_t x = GBAObjAttributesBGetX(sprite->b) << 23;
116 x >>= 23;
117 uint16_t* vramBase = &renderer->d.vram[BASE_TILE >> 1];
118 unsigned charBase = GBAObjAttributesCGetTile(sprite->c) * 0x20;
119 if (GBARegisterDISPCNTGetMode(renderer->dispcnt) >= 3 && GBAObjAttributesCGetTile(sprite->c) < 512) {
120 return 0;
121 }
122 int variant = renderer->target1Obj && GBAWindowControlIsBlendEnable(renderer->currentWindow.packed) && (renderer->blendEffect == BLEND_BRIGHTEN || renderer->blendEffect == BLEND_DARKEN);
123 if (GBAObjAttributesAGetMode(sprite->a) == OBJ_MODE_SEMITRANSPARENT) {
124 int target2 = renderer->target2Bd << 4;
125 target2 |= renderer->bg[0].target2 << (renderer->bg[0].priority);
126 target2 |= renderer->bg[1].target2 << (renderer->bg[1].priority);
127 target2 |= renderer->bg[2].target2 << (renderer->bg[2].priority);
128 target2 |= renderer->bg[3].target2 << (renderer->bg[3].priority);
129 if (GBAObjAttributesCGetPriority(sprite->c) < target2) {
130 variant = 0;
131 }
132 }
133 color_t* palette = &renderer->normalPalette[0x100];
134 if (variant) {
135 palette = &renderer->variantPalette[0x100];
136 }
137
138 int inY = y - (int) GBAObjAttributesAGetY(sprite->a);
139
140 uint32_t current;
141 if (GBAObjAttributesAIsTransformed(sprite->a)) {
142 int totalWidth = width << GBAObjAttributesAGetDoubleSize(sprite->a);
143 int totalHeight = height << GBAObjAttributesAGetDoubleSize(sprite->a);
144 struct GBAOAMMatrix mat;
145 LOAD_16(mat.a, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].a);
146 LOAD_16(mat.b, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].b);
147 LOAD_16(mat.c, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].c);
148 LOAD_16(mat.d, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].d);
149
150 if (inY < 0) {
151 inY += 256;
152 }
153 int outX = x >= start ? x : start;
154 int inX = outX - x;
155 int xAccum = mat.a * (inX - 1 - (totalWidth >> 1)) + mat.b * (inY - (totalHeight >> 1));
156 int yAccum = mat.c * (inX - 1 - (totalWidth >> 1)) + mat.d * (inY - (totalHeight >> 1));
157
158 if (!GBAObjAttributesAIs256Color(sprite->a)) {
159 palette = &palette[GBAObjAttributesCGetPalette(sprite->c) << 4];
160 if (flags & FLAG_OBJWIN) {
161 SPRITE_TRANSFORMED_LOOP(16, OBJWIN);
162 } else {
163 SPRITE_TRANSFORMED_LOOP(16, NORMAL);
164 }
165 } else {
166 if (flags & FLAG_OBJWIN) {
167 SPRITE_TRANSFORMED_LOOP(256, OBJWIN);
168 } else {
169 SPRITE_TRANSFORMED_LOOP(256, NORMAL);
170 }
171 }
172 } else {
173 int outX = x >= start ? x : start;
174 int condition = x + width;
175 int mosaicH = 1;
176 if (GBAObjAttributesAIsMosaic(sprite->a)) {
177 mosaicH = GBAMosaicControlGetObjH(renderer->mosaic) + 1;
178 if (condition % mosaicH) {
179 condition += mosaicH - (condition % mosaicH);
180 }
181 }
182 if ((int) GBAObjAttributesAGetY(sprite->a) + height - 256 >= 0) {
183 inY += 256;
184 }
185 if (GBAObjAttributesBIsVFlip(sprite->b)) {
186 inY = height - inY - 1;
187 }
188 if (end < condition) {
189 condition = end;
190 }
191 int inX = outX - x;
192 int xOffset = 1;
193 if (GBAObjAttributesBIsHFlip(sprite->b)) {
194 inX = width - inX - 1;
195 xOffset = -1;
196 }
197 if (!GBAObjAttributesAIs256Color(sprite->a)) {
198 palette = &palette[GBAObjAttributesCGetPalette(sprite->c) << 4];
199 if (flags & FLAG_OBJWIN) {
200 SPRITE_NORMAL_LOOP(16, OBJWIN);
201 } else if (GBAObjAttributesAIsMosaic(sprite->a)) {
202 SPRITE_MOSAIC_LOOP(16, NORMAL);
203 } else {
204 SPRITE_NORMAL_LOOP(16, NORMAL);
205 }
206 } else {
207 if (flags & FLAG_OBJWIN) {
208 SPRITE_NORMAL_LOOP(256, OBJWIN);
209 } else if (GBAObjAttributesAIsMosaic(sprite->a)) {
210 SPRITE_MOSAIC_LOOP(256, NORMAL);
211 } else {
212 SPRITE_NORMAL_LOOP(256, NORMAL);
213 }
214 }
215 }
216 return 1;
217}
218
219void GBAVideoSoftwareRendererPostprocessSprite(struct GBAVideoSoftwareRenderer* renderer, unsigned priority) {
220 int x;
221 uint32_t* pixel = &renderer->row[renderer->start];
222 uint32_t flags = FLAG_TARGET_2 * renderer->target2Obj;
223
224 int objwinSlowPath = GBARegisterDISPCNTIsObjwinEnable(renderer->dispcnt);
225 bool objwinDisable = false;
226 bool objwinOnly = false;
227 if (objwinSlowPath) {
228 objwinDisable = !GBAWindowControlIsObjEnable(renderer->objwin.packed);
229 objwinOnly = !objwinDisable && !GBAWindowControlIsObjEnable(renderer->currentWindow.packed);
230 if (objwinDisable && !GBAWindowControlIsObjEnable(renderer->currentWindow.packed)) {
231 return;
232 }
233
234 if (objwinDisable) {
235 for (x = renderer->start; x < renderer->end; ++x, ++pixel) {
236 uint32_t color = renderer->spriteLayer[x] & ~FLAG_OBJWIN;
237 uint32_t current = *pixel;
238 if ((color & FLAG_UNWRITTEN) != FLAG_UNWRITTEN && !(current & FLAG_OBJWIN) && (color & FLAG_PRIORITY) >> OFFSET_PRIORITY == priority) {
239 _compositeBlendObjwin(renderer, pixel, color | flags, current);
240 }
241 }
242 return;
243 } else if (objwinOnly) {
244 for (x = renderer->start; x < renderer->end; ++x, ++pixel) {
245 uint32_t color = renderer->spriteLayer[x] & ~FLAG_OBJWIN;
246 uint32_t current = *pixel;
247 if ((color & FLAG_UNWRITTEN) != FLAG_UNWRITTEN && (current & FLAG_OBJWIN) && (color & FLAG_PRIORITY) >> OFFSET_PRIORITY == priority) {
248 _compositeBlendObjwin(renderer, pixel, color | flags, current);
249 }
250 }
251 return;
252 } else {
253 for (x = renderer->start; x < renderer->end; ++x, ++pixel) {
254 uint32_t color = renderer->spriteLayer[x] & ~FLAG_OBJWIN;
255 uint32_t current = *pixel;
256 if ((color & FLAG_UNWRITTEN) != FLAG_UNWRITTEN && (color & FLAG_PRIORITY) >> OFFSET_PRIORITY == priority) {
257 _compositeBlendObjwin(renderer, pixel, color | flags, current);
258 }
259 }
260 return;
261 }
262 } else if (!GBAWindowControlIsObjEnable(renderer->currentWindow.packed)) {
263 return;
264 }
265 for (x = renderer->start; x < renderer->end; ++x, ++pixel) {
266 uint32_t color = renderer->spriteLayer[x] & ~FLAG_OBJWIN;
267 uint32_t current = *pixel;
268 if ((color & FLAG_UNWRITTEN) != FLAG_UNWRITTEN && (color & FLAG_PRIORITY) >> OFFSET_PRIORITY == priority) {
269 _compositeBlendNoObjwin(renderer, pixel, color | flags, current);
270 }
271 }
272}