all repos — mgba @ 88ba6ee0b0e9872a1598a815d06c35529ee5d12f

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