all repos — mgba @ bfb674fb4f42fd7050257aeb196c3831ab89feb0

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