all repos — mgba @ 60577e83948647d36a2e6a8b4ec8f8556df3f72f

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		if (!(renderer->row[outX] & FLAG_UNWRITTEN)) { \
 31			continue; \
 32		} \
 33		int localX = inX - xOffset * (outX % mosaicH); \
 34		if (localX < 0 || localX > width - 1) { \
 35			continue; \
 36		} \
 37		SPRITE_XBASE_ ## DEPTH(localX); \
 38		SPRITE_DRAW_PIXEL_ ## DEPTH ## _ ## TYPE(localX); \
 39	}
 40
 41#define SPRITE_TRANSFORMED_LOOP(DEPTH, TYPE) \
 42	unsigned tileData; \
 43	unsigned widthMask = ~(width - 1); \
 44	unsigned heightMask = ~(height - 1); \
 45	for (; outX < condition; ++outX, ++inX) { \
 46		if (!(renderer->row[outX] & FLAG_UNWRITTEN)) { \
 47			continue; \
 48		} \
 49		renderer->spriteCyclesRemaining -= 2; \
 50		xAccum += mat.a; \
 51		yAccum += mat.c; \
 52		int localX = xAccum >> 8; \
 53		int localY = yAccum >> 8; \
 54		\
 55		if (localX & widthMask || localY & heightMask) { \
 56			break; \
 57		} \
 58		\
 59		SPRITE_YBASE_ ## DEPTH(localY); \
 60		SPRITE_XBASE_ ## DEPTH(localX); \
 61		SPRITE_DRAW_PIXEL_ ## DEPTH ## _ ## TYPE(localX); \
 62	}
 63
 64#define SPRITE_XBASE_16(localX) unsigned xBase = (localX & ~0x7) * 4 + ((localX >> 1) & 2);
 65#define SPRITE_YBASE_16(localY) unsigned yBase = (localY & ~0x7) * stride + (localY & 0x7) * 4;
 66
 67#define SPRITE_DRAW_PIXEL_16_NORMAL(localX) \
 68	LOAD_16(tileData, ((yBase + charBase + xBase) & 0x7FFE), vramBase); \
 69	tileData = (tileData >> ((localX & 3) << 2)) & 0xF; \
 70	current = renderer->spriteLayer[outX]; \
 71	if ((current & FLAG_ORDER_MASK) > flags) { \
 72		if (tileData) { \
 73			renderer->spriteLayer[outX] = palette[tileData] | flags; \
 74		} else if (current != FLAG_UNWRITTEN) { \
 75			renderer->spriteLayer[outX] = (current & ~FLAG_ORDER_MASK) | GBAObjAttributesCGetPriority(sprite->c) << OFFSET_PRIORITY; \
 76		} \
 77	}
 78
 79#define SPRITE_DRAW_PIXEL_16_NORMAL_OBJWIN(localX) \
 80	LOAD_16(tileData, ((yBase + charBase + xBase) & 0x7FFE), vramBase); \
 81	tileData = (tileData >> ((localX & 3) << 2)) & 0xF; \
 82	current = renderer->spriteLayer[outX]; \
 83	if ((current & FLAG_ORDER_MASK) > flags) { \
 84		if (tileData) { \
 85			unsigned color = (renderer->row[outX] & FLAG_OBJWIN) ? objwinPalette[tileData] : palette[tileData]; \
 86			renderer->spriteLayer[outX] = color | flags; \
 87		} else if (current != FLAG_UNWRITTEN) { \
 88			renderer->spriteLayer[outX] = (current & ~FLAG_ORDER_MASK) | GBAObjAttributesCGetPriority(sprite->c) << OFFSET_PRIORITY; \
 89		} \
 90	}
 91
 92#define SPRITE_DRAW_PIXEL_16_OBJWIN(localX) \
 93	LOAD_16(tileData, ((yBase + charBase + xBase) & 0x7FFE), vramBase); \
 94	tileData = (tileData >> ((localX & 3) << 2)) & 0xF; \
 95	if (tileData) { \
 96		renderer->row[outX] |= FLAG_OBJWIN; \
 97	}
 98
 99#define SPRITE_XBASE_256(localX) unsigned xBase = (localX & ~0x7) * 8 + (localX & 6);
100#define SPRITE_YBASE_256(localY) unsigned yBase = (localY & ~0x7) * stride + (localY & 0x7) * 8;
101
102#define SPRITE_DRAW_PIXEL_256_NORMAL(localX) \
103	LOAD_16(tileData, ((yBase + charBase + xBase) & 0x7FFE), vramBase); \
104	tileData = (tileData >> ((localX & 1) << 3)) & 0xFF; \
105	current = renderer->spriteLayer[outX]; \
106	if ((current & FLAG_ORDER_MASK) > flags) { \
107		if (tileData) { \
108			renderer->spriteLayer[outX] = palette[tileData] | flags; \
109		} else if (current != FLAG_UNWRITTEN) { \
110			renderer->spriteLayer[outX] = (current & ~FLAG_ORDER_MASK) | GBAObjAttributesCGetPriority(sprite->c) << OFFSET_PRIORITY; \
111		} \
112	}
113
114#define SPRITE_DRAW_PIXEL_256_NORMAL_OBJWIN(localX) \
115	LOAD_16(tileData, ((yBase + charBase + xBase) & 0x7FFE), vramBase);  \
116	tileData = (tileData >> ((localX & 1) << 3)) & 0xFF; \
117	current = renderer->spriteLayer[outX]; \
118	if ((current & FLAG_ORDER_MASK) > flags) { \
119		if (tileData) { \
120			unsigned color = (renderer->row[outX] & FLAG_OBJWIN) ? objwinPalette[tileData] : palette[tileData]; \
121			renderer->spriteLayer[outX] = color | flags; \
122		} else if (current != FLAG_UNWRITTEN) { \
123			renderer->spriteLayer[outX] = (current & ~FLAG_ORDER_MASK) | GBAObjAttributesCGetPriority(sprite->c) << OFFSET_PRIORITY; \
124		} \
125	}
126
127#define SPRITE_DRAW_PIXEL_256_OBJWIN(localX) \
128	LOAD_16(tileData, ((yBase + charBase + xBase) & 0x7FFE), vramBase); \
129	tileData = (tileData >> ((localX & 1) << 3)) & 0xFF; \
130	if (tileData) { \
131		renderer->row[outX] |= FLAG_OBJWIN; \
132	}
133
134int GBAVideoSoftwareRendererPreprocessSprite(struct GBAVideoSoftwareRenderer* renderer, struct GBAObj* sprite, int y) {
135	int width = GBAVideoObjSizes[GBAObjAttributesAGetShape(sprite->a) * 4 + GBAObjAttributesBGetSize(sprite->b)][0];
136	int height = GBAVideoObjSizes[GBAObjAttributesAGetShape(sprite->a) * 4 + GBAObjAttributesBGetSize(sprite->b)][1];
137	int start = renderer->start;
138	int end = renderer->end;
139	uint32_t flags = GBAObjAttributesCGetPriority(sprite->c) << OFFSET_PRIORITY;
140	flags |= FLAG_TARGET_1 * ((GBAWindowControlIsBlendEnable(renderer->currentWindow.packed) && renderer->target1Obj && renderer->blendEffect == BLEND_ALPHA) || GBAObjAttributesAGetMode(sprite->a) == OBJ_MODE_SEMITRANSPARENT);
141	flags |= FLAG_OBJWIN * (GBAObjAttributesAGetMode(sprite->a) == OBJ_MODE_OBJWIN);
142	if ((flags & FLAG_OBJWIN) && renderer->currentWindow.priority < renderer->objwin.priority) {
143		return 0;
144	}
145	int32_t x = (uint32_t) GBAObjAttributesBGetX(sprite->b) << 23;
146	x >>= 23;
147	x += renderer->objOffsetX;
148	uint16_t* vramBase = &renderer->d.vram[BASE_TILE >> 1];
149	bool align = GBAObjAttributesAIs256Color(sprite->a) && !GBARegisterDISPCNTIsObjCharacterMapping(renderer->dispcnt);
150	unsigned charBase = (GBAObjAttributesCGetTile(sprite->c) & ~align) * 0x20;
151	if (GBARegisterDISPCNTGetMode(renderer->dispcnt) >= 3 && GBAObjAttributesCGetTile(sprite->c) < 512) {
152		return 0;
153	}
154
155	int objwinSlowPath = GBARegisterDISPCNTIsObjwinEnable(renderer->dispcnt) && GBAWindowControlGetBlendEnable(renderer->objwin.packed) != GBAWindowControlIsBlendEnable(renderer->currentWindow.packed);
156	int variant = renderer->target1Obj &&
157	              GBAWindowControlIsBlendEnable(renderer->currentWindow.packed) &&
158	              (renderer->blendEffect == BLEND_BRIGHTEN || renderer->blendEffect == BLEND_DARKEN);
159	if (GBAObjAttributesAGetMode(sprite->a) == OBJ_MODE_SEMITRANSPARENT || objwinSlowPath) {
160		int target2 = renderer->target2Bd;
161		target2 |= renderer->bg[0].target2;
162		target2 |= renderer->bg[1].target2;
163		target2 |= renderer->bg[2].target2;
164		target2 |= renderer->bg[3].target2;
165		if (target2) {
166			flags |= FLAG_REBLEND;
167			variant = 0;
168		} else {
169			flags &= ~FLAG_TARGET_1;
170		}
171	}
172
173	color_t* palette = &renderer->normalPalette[0x100];
174	color_t* objwinPalette = palette;
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) + renderer->objOffsetY);
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		struct GBAOAMMatrix mat;
191		LOAD_16(mat.a, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].a);
192		LOAD_16(mat.b, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].b);
193		LOAD_16(mat.c, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].c);
194		LOAD_16(mat.d, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].d);
195
196		if (inY < 0) {
197			inY += 256;
198		}
199		int outX = x >= start ? x : start;
200		int condition = x + totalWidth;
201		int inX = outX - x;
202		if (end < condition) {
203			condition = end;
204		}
205
206		int xAccum = mat.a * (inX - 1 - (totalWidth >> 1)) + mat.b * (inY - (totalHeight >> 1)) + (width << 7);
207		int yAccum = mat.c * (inX - 1 - (totalWidth >> 1)) + mat.d * (inY - (totalHeight >> 1)) + (height << 7);
208
209		// Clip off early pixels
210		// TODO: Transform end coordinates too
211		if (mat.a) {
212			if ((xAccum >> 8) < 0) {
213				int32_t diffX = -xAccum - 1;
214				int32_t x = mat.a ? diffX / mat.a : 0;
215				xAccum += mat.a * x;
216				yAccum += mat.c * x;
217				outX += x;
218				inX += x;
219			} else if ((xAccum >> 8) >= width) {
220				int32_t diffX = (width << 8) - xAccum;
221				int32_t x = mat.a ? diffX / mat.a : 0;
222				xAccum += mat.a * x;
223				yAccum += mat.c * x;
224				outX += x;
225				inX += x;
226			}
227		}
228		if (mat.c) {
229			if ((yAccum >> 8) < 0) {
230				int32_t diffY = - yAccum - 1;
231				int32_t y = mat.c ? diffY / mat.c : 0;
232				xAccum += mat.a * y;
233				yAccum += mat.c * y;
234				outX += y;
235				inX += y;
236			} else if ((yAccum >> 8) >= height) {
237				int32_t diffY = (height << 8) - yAccum;
238				int32_t y = mat.c ? diffY / mat.c : 0;
239				xAccum += mat.a * y;
240				yAccum += mat.c * y;
241				outX += y;
242				inX += y;
243			}
244		}
245
246		if (outX < start || outX >= condition) {
247			return 0;
248		}
249		renderer->spriteCyclesRemaining -= 10;
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 (end == VIDEO_HORIZONTAL_PIXELS && 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 (end == VIDEO_HORIZONTAL_PIXELS && 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}