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 int variant = renderer->target1Obj && GBAWindowControlIsBlendEnable(renderer->currentWindow.packed) && (renderer->blendEffect == BLEND_BRIGHTEN || renderer->blendEffect == BLEND_DARKEN);
120 if (GBAObjAttributesAGetMode(sprite->a) == OBJ_MODE_SEMITRANSPARENT) {
121 int target2 = renderer->target2Bd << 4;
122 target2 |= renderer->bg[0].target2 << (renderer->bg[0].priority);
123 target2 |= renderer->bg[1].target2 << (renderer->bg[1].priority);
124 target2 |= renderer->bg[2].target2 << (renderer->bg[2].priority);
125 target2 |= renderer->bg[3].target2 << (renderer->bg[3].priority);
126 if (GBAObjAttributesCGetPriority(sprite->c) < target2) {
127 variant = 0;
128 }
129 }
130 color_t* palette = &renderer->normalPalette[0x100];
131 if (variant) {
132 palette = &renderer->variantPalette[0x100];
133 }
134
135 int inY = y - (int) GBAObjAttributesAGetY(sprite->a);
136
137 uint32_t current;
138 if (GBAObjAttributesAIsTransformed(sprite->a)) {
139 int totalWidth = width << GBAObjAttributesAGetDoubleSize(sprite->a);
140 int totalHeight = height << GBAObjAttributesAGetDoubleSize(sprite->a);
141 struct GBAOAMMatrix mat;
142 LOAD_16(mat.a, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].a);
143 LOAD_16(mat.b, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].b);
144 LOAD_16(mat.c, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].c);
145 LOAD_16(mat.d, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].d);
146
147 if (inY < 0) {
148 inY += 256;
149 }
150 int outX = x >= start ? x : start;
151 int inX = outX - x;
152 int xAccum = mat.a * (inX - 1 - (totalWidth >> 1)) + mat.b * (inY - (totalHeight >> 1));
153 int yAccum = mat.c * (inX - 1 - (totalWidth >> 1)) + mat.d * (inY - (totalHeight >> 1));
154
155 if (!GBAObjAttributesAIs256Color(sprite->a)) {
156 palette = &palette[GBAObjAttributesCGetPalette(sprite->c) << 4];
157 if (flags & FLAG_OBJWIN) {
158 SPRITE_TRANSFORMED_LOOP(16, OBJWIN);
159 } else {
160 SPRITE_TRANSFORMED_LOOP(16, NORMAL);
161 }
162 } else {
163 if (flags & FLAG_OBJWIN) {
164 SPRITE_TRANSFORMED_LOOP(256, OBJWIN);
165 } else {
166 SPRITE_TRANSFORMED_LOOP(256, NORMAL);
167 }
168 }
169 } else {
170 int outX = x >= start ? x : start;
171 int condition = x + width;
172 int mosaicH = 1;
173 if (GBAObjAttributesAIsMosaic(sprite->a)) {
174 mosaicH = GBAMosaicControlGetObjH(renderer->mosaic) + 1;
175 if (condition % mosaicH) {
176 condition += mosaicH - (condition % mosaicH);
177 }
178 }
179 if ((int) GBAObjAttributesAGetY(sprite->a) + height - 256 >= 0) {
180 inY += 256;
181 }
182 if (GBAObjAttributesBIsVFlip(sprite->b)) {
183 inY = height - inY - 1;
184 }
185 if (end < condition) {
186 condition = end;
187 }
188 int inX = outX - x;
189 int xOffset = 1;
190 if (GBAObjAttributesBIsHFlip(sprite->b)) {
191 inX = width - inX - 1;
192 xOffset = -1;
193 }
194 if (!GBAObjAttributesAIs256Color(sprite->a)) {
195 palette = &palette[GBAObjAttributesCGetPalette(sprite->c) << 4];
196 if (flags & FLAG_OBJWIN) {
197 SPRITE_NORMAL_LOOP(16, OBJWIN);
198 } else if (GBAObjAttributesAIsMosaic(sprite->a)) {
199 SPRITE_MOSAIC_LOOP(16, NORMAL);
200 } else {
201 SPRITE_NORMAL_LOOP(16, NORMAL);
202 }
203 } else {
204 if (flags & FLAG_OBJWIN) {
205 SPRITE_NORMAL_LOOP(256, OBJWIN);
206 } else if (GBAObjAttributesAIsMosaic(sprite->a)) {
207 SPRITE_MOSAIC_LOOP(256, NORMAL);
208 } else {
209 SPRITE_NORMAL_LOOP(256, NORMAL);
210 }
211 }
212 }
213 return 1;
214}
215
216void GBAVideoSoftwareRendererPostprocessSprite(struct GBAVideoSoftwareRenderer* renderer, unsigned priority) {
217 int x;
218 uint32_t* pixel = &renderer->row[renderer->start];
219 uint32_t flags = FLAG_TARGET_2 * renderer->target2Obj;
220
221 int objwinSlowPath = GBARegisterDISPCNTIsObjwinEnable(renderer->dispcnt);
222 bool objwinDisable = false;
223 bool objwinOnly = false;
224 if (objwinSlowPath) {
225 objwinDisable = !GBAWindowControlIsObjEnable(renderer->objwin.packed);
226 objwinOnly = !objwinDisable && !GBAWindowControlIsObjEnable(renderer->currentWindow.packed);
227 if (objwinDisable && !GBAWindowControlIsObjEnable(renderer->currentWindow.packed)) {
228 return;
229 }
230
231 if (objwinDisable) {
232 for (x = renderer->start; x < renderer->end; ++x, ++pixel) {
233 uint32_t color = renderer->spriteLayer[x] & ~FLAG_OBJWIN;
234 uint32_t current = *pixel;
235 if ((color & FLAG_UNWRITTEN) != FLAG_UNWRITTEN && !(current & FLAG_OBJWIN) && (color & FLAG_PRIORITY) >> OFFSET_PRIORITY == priority) {
236 _compositeBlendObjwin(renderer, pixel, color | flags, current);
237 }
238 }
239 return;
240 } else if (objwinOnly) {
241 for (x = renderer->start; x < renderer->end; ++x, ++pixel) {
242 uint32_t color = renderer->spriteLayer[x] & ~FLAG_OBJWIN;
243 uint32_t current = *pixel;
244 if ((color & FLAG_UNWRITTEN) != FLAG_UNWRITTEN && (current & FLAG_OBJWIN) && (color & FLAG_PRIORITY) >> OFFSET_PRIORITY == priority) {
245 _compositeBlendObjwin(renderer, pixel, color | flags, current);
246 }
247 }
248 return;
249 } else {
250 for (x = renderer->start; x < renderer->end; ++x, ++pixel) {
251 uint32_t color = renderer->spriteLayer[x] & ~FLAG_OBJWIN;
252 uint32_t current = *pixel;
253 if ((color & FLAG_UNWRITTEN) != FLAG_UNWRITTEN && (color & FLAG_PRIORITY) >> OFFSET_PRIORITY == priority) {
254 _compositeBlendObjwin(renderer, pixel, color | flags, current);
255 }
256 }
257 return;
258 }
259 } else if (!GBAWindowControlIsObjEnable(renderer->currentWindow.packed)) {
260 return;
261 }
262 for (x = renderer->start; x < renderer->end; ++x, ++pixel) {
263 uint32_t color = renderer->spriteLayer[x] & ~FLAG_OBJWIN;
264 uint32_t current = *pixel;
265 if ((color & FLAG_UNWRITTEN) != FLAG_UNWRITTEN && (color & FLAG_PRIORITY) >> OFFSET_PRIORITY == priority) {
266 _compositeBlendNoObjwin(renderer, pixel, color | flags, current);
267 }
268 }
269}