all repos — mgba @ 1bc6c13e6bbf341953530f49ec2bc9ce3428bca5

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	unsigned charBase = GBAObjAttributesCGetTile(sprite->c) * 0x20;
152	if (GBARegisterDISPCNTGetMode(renderer->dispcnt) >= 3 && GBAObjAttributesCGetTile(sprite->c) < 512) {
153		return 0;
154	}
155	if (renderer->spriteCyclesRemaining <= 0) {
156		return 0;
157	}
158
159	int variant = renderer->target1Obj &&
160	              GBAWindowControlIsBlendEnable(renderer->currentWindow.packed) &&
161	              (renderer->blendEffect == BLEND_BRIGHTEN || renderer->blendEffect == BLEND_DARKEN);
162	if (GBAObjAttributesAGetMode(sprite->a) == OBJ_MODE_SEMITRANSPARENT) {
163		int target2 = renderer->target2Bd << 4;
164		target2 |= renderer->bg[0].target2 << (renderer->bg[0].priority);
165		target2 |= renderer->bg[1].target2 << (renderer->bg[1].priority);
166		target2 |= renderer->bg[2].target2 << (renderer->bg[2].priority);
167		target2 |= renderer->bg[3].target2 << (renderer->bg[3].priority);
168		if ((1 << GBAObjAttributesCGetPriority(sprite->c)) <= target2) {
169			variant = 0;
170		}
171	}
172	color_t* palette = &renderer->normalPalette[0x100];
173	color_t* objwinPalette = palette;
174	int objwinSlowPath = GBARegisterDISPCNTIsObjwinEnable(renderer->dispcnt) && GBAWindowControlGetBlendEnable(renderer->objwin.packed) != GBAWindowControlIsBlendEnable(renderer->currentWindow.packed);
175
176	if (variant) {
177		palette = &renderer->variantPalette[0x100];
178		if (GBAWindowControlIsBlendEnable(renderer->objwin.packed)) {
179			objwinPalette = palette;
180		}
181	}
182
183	int inY = y - (int) GBAObjAttributesAGetY(sprite->a);
184	int stride = GBARegisterDISPCNTIsObjCharacterMapping(renderer->dispcnt) ? (width >> !GBAObjAttributesAIs256Color(sprite->a)) : 0x80;
185
186	uint32_t current;
187	if (GBAObjAttributesAIsTransformed(sprite->a)) {
188		int totalWidth = width << GBAObjAttributesAGetDoubleSize(sprite->a);
189		int totalHeight = height << GBAObjAttributesAGetDoubleSize(sprite->a);
190		renderer->spriteCyclesRemaining -= 10;
191		struct GBAOAMMatrix mat;
192		LOAD_16(mat.a, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].a);
193		LOAD_16(mat.b, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].b);
194		LOAD_16(mat.c, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].c);
195		LOAD_16(mat.d, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].d);
196
197		if (inY < 0) {
198			inY += 256;
199		}
200		int outX = x >= start ? x : start;
201		int condition = x + totalWidth;
202		int inX = outX - x;
203		if (end < condition) {
204			condition = end;
205		}
206
207		int xAccum = mat.a * (inX - 1 - (totalWidth >> 1)) + mat.b * (inY - (totalHeight >> 1)) + (width << 7);
208		int yAccum = mat.c * (inX - 1 - (totalWidth >> 1)) + mat.d * (inY - (totalHeight >> 1)) + (height << 7);
209
210		// Clip off early pixels
211		// TODO: Transform end coordinates too
212		if (mat.a) {
213			if ((xAccum >> 8) < 0) {
214				int32_t diffX = -xAccum - 1;
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			} else if ((xAccum >> 8) >= width) {
221				int32_t diffX = (width << 8) - xAccum;
222				int32_t x = mat.a ? diffX / mat.a : 0;
223				xAccum += mat.a * x;
224				yAccum += mat.c * x;
225				outX += x;
226				inX += x;
227			}
228		}
229		if (mat.c) {
230			if ((yAccum >> 8) < 0) {
231				int32_t diffY = - yAccum - 1;
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			} else if ((yAccum >> 8) >= height) {
238				int32_t diffY = (height << 8) - yAccum;
239				int32_t y = mat.c ? diffY / mat.c : 0;
240				xAccum += mat.a * y;
241				yAccum += mat.c * y;
242				outX += y;
243				inX += y;
244			}
245		}
246
247		if (outX < start || outX >= condition) {
248			return 0;
249		}
250
251		if (!GBAObjAttributesAIs256Color(sprite->a)) {
252			palette = &palette[GBAObjAttributesCGetPalette(sprite->c) << 4];
253			if (flags & FLAG_OBJWIN) {
254				SPRITE_TRANSFORMED_LOOP(16, OBJWIN);
255			} else if (objwinSlowPath) {
256				objwinPalette = &objwinPalette[GBAObjAttributesCGetPalette(sprite->c) << 4];
257				SPRITE_TRANSFORMED_LOOP(16, NORMAL_OBJWIN);
258			} else {
259				SPRITE_TRANSFORMED_LOOP(16, NORMAL);
260			}
261		} else {
262			if (flags & FLAG_OBJWIN) {
263				SPRITE_TRANSFORMED_LOOP(256, OBJWIN);
264			} else if (objwinSlowPath) {
265				SPRITE_TRANSFORMED_LOOP(256, NORMAL_OBJWIN);
266			} else {
267				SPRITE_TRANSFORMED_LOOP(256, NORMAL);
268			}
269		}
270		if (x + totalWidth > VIDEO_HORIZONTAL_PIXELS) {
271			renderer->spriteCyclesRemaining -= (x + totalWidth - VIDEO_HORIZONTAL_PIXELS) * 2;
272		}
273	} else {
274		int outX = x >= start ? x : start;
275		int condition = x + width;
276		int mosaicH = 1;
277		if (GBAObjAttributesAIsMosaic(sprite->a)) {
278			mosaicH = GBAMosaicControlGetObjH(renderer->mosaic) + 1;
279			if (condition % mosaicH) {
280				condition += mosaicH - (condition % mosaicH);
281			}
282		}
283		if ((int) GBAObjAttributesAGetY(sprite->a) + height - 256 >= 0) {
284			inY += 256;
285		}
286		if (GBAObjAttributesBIsVFlip(sprite->b)) {
287			inY = height - inY - 1;
288		}
289		if (end < condition) {
290			condition = end;
291		}
292		int inX = outX - x;
293		int xOffset = 1;
294		if (GBAObjAttributesBIsHFlip(sprite->b)) {
295			inX = width - inX - 1;
296			xOffset = -1;
297		}
298		if (!GBAObjAttributesAIs256Color(sprite->a)) {
299			palette = &palette[GBAObjAttributesCGetPalette(sprite->c) << 4];
300			if (flags & FLAG_OBJWIN) {
301				SPRITE_NORMAL_LOOP(16, OBJWIN);
302			} else if (mosaicH > 1) {
303				if (objwinSlowPath) {
304					objwinPalette = &objwinPalette[GBAObjAttributesCGetPalette(sprite->c) << 4];
305					SPRITE_MOSAIC_LOOP(16, NORMAL_OBJWIN);
306				} else {
307					SPRITE_MOSAIC_LOOP(16, NORMAL);
308				}
309			} else if (objwinSlowPath) {
310				objwinPalette = &objwinPalette[GBAObjAttributesCGetPalette(sprite->c) << 4];
311				SPRITE_NORMAL_LOOP(16, NORMAL_OBJWIN);
312			} else {
313				SPRITE_NORMAL_LOOP(16, NORMAL);
314			}
315		} else {
316			if (flags & FLAG_OBJWIN) {
317				SPRITE_NORMAL_LOOP(256, OBJWIN);
318			} else if (mosaicH > 1) {
319				if (objwinSlowPath) {
320					SPRITE_MOSAIC_LOOP(256, NORMAL_OBJWIN);
321				} else {
322					SPRITE_MOSAIC_LOOP(256, NORMAL);
323				}
324			} else if (objwinSlowPath) {
325				SPRITE_NORMAL_LOOP(256, NORMAL_OBJWIN);
326			} else {
327				SPRITE_NORMAL_LOOP(256, NORMAL);
328			}
329		}
330		if (x + width > VIDEO_HORIZONTAL_PIXELS) {
331			renderer->spriteCyclesRemaining -= x + width - VIDEO_HORIZONTAL_PIXELS;
332		}
333	}
334	return 1;
335}
336
337void GBAVideoSoftwareRendererPostprocessSprite(struct GBAVideoSoftwareRenderer* renderer, unsigned priority) {
338	int x;
339	uint32_t* pixel = &renderer->row[renderer->start];
340	uint32_t flags = FLAG_TARGET_2 * renderer->target2Obj;
341
342	int objwinSlowPath = GBARegisterDISPCNTIsObjwinEnable(renderer->dispcnt);
343	bool objwinDisable = false;
344	bool objwinOnly = false;
345	if (objwinSlowPath) {
346		objwinDisable = !GBAWindowControlIsObjEnable(renderer->objwin.packed);
347		objwinOnly = !objwinDisable && !GBAWindowControlIsObjEnable(renderer->currentWindow.packed);
348		if (objwinDisable && !GBAWindowControlIsObjEnable(renderer->currentWindow.packed)) {
349			return;
350		}
351
352		if (objwinDisable) {
353			for (x = renderer->start; x < renderer->end; ++x, ++pixel) {
354				uint32_t color = renderer->spriteLayer[x] & ~FLAG_OBJWIN;
355				uint32_t current = *pixel;
356				if ((color & FLAG_UNWRITTEN) != FLAG_UNWRITTEN && !(current & FLAG_OBJWIN) && (color & FLAG_PRIORITY) >> OFFSET_PRIORITY == priority) {
357					_compositeBlendObjwin(renderer, pixel, color | flags, current);
358				}
359			}
360			return;
361		} else if (objwinOnly) {
362			for (x = renderer->start; x < renderer->end; ++x, ++pixel) {
363				uint32_t color = renderer->spriteLayer[x] & ~FLAG_OBJWIN;
364				uint32_t current = *pixel;
365				if ((color & FLAG_UNWRITTEN) != FLAG_UNWRITTEN && (current & FLAG_OBJWIN) && (color & FLAG_PRIORITY) >> OFFSET_PRIORITY == priority) {
366					_compositeBlendObjwin(renderer, pixel, color | flags, current);
367				}
368			}
369			return;
370		} else {
371			for (x = renderer->start; x < renderer->end; ++x, ++pixel) {
372				uint32_t color = renderer->spriteLayer[x] & ~FLAG_OBJWIN;
373				uint32_t current = *pixel;
374				if ((color & FLAG_UNWRITTEN) != FLAG_UNWRITTEN && (color & FLAG_PRIORITY) >> OFFSET_PRIORITY == priority) {
375					_compositeBlendObjwin(renderer, pixel, color | flags, current);
376				}
377			}
378			return;
379		}
380	} else if (!GBAWindowControlIsObjEnable(renderer->currentWindow.packed)) {
381		return;
382	}
383	for (x = renderer->start; x < renderer->end; ++x, ++pixel) {
384		uint32_t color = renderer->spriteLayer[x] & ~FLAG_OBJWIN;
385		uint32_t current = *pixel;
386		if ((color & FLAG_UNWRITTEN) != FLAG_UNWRITTEN && (color & FLAG_PRIORITY) >> OFFSET_PRIORITY == priority) {
387			_compositeBlendNoObjwin(renderer, pixel, color | flags, current);
388		}
389	}
390}