all repos — mgba @ 4d1b7b4591261bf9ef97774849e7fed8b801387a

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