all repos — mgba @ 43e2a6ab5d32d1a1165bc37699bcf9dfc6e1992e

mGBA Game Boy Advance Emulator

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