all repos — mgba @ a33e9d375ca1f3e2a918ab2c27962b37de530a8d

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	for (; outX < condition; ++outX, inX += xOffset) { \
 21		int localX = inX - xOffset * (outX % mosaicH); \
 22		if (localX < 0) { \
 23			localX = 0; \
 24		} else if (localX > width - 1) {\
 25			localX = width - 1; \
 26		} \
 27		SPRITE_XBASE_ ## DEPTH(localX); \
 28		SPRITE_DRAW_PIXEL_ ## DEPTH ## _ ## TYPE(localX); \
 29	}
 30
 31#define SPRITE_TRANSFORMED_LOOP(DEPTH, TYPE) \
 32	unsigned tileData; \
 33	unsigned widthMask = ~(width - 1); \
 34	unsigned heightMask = ~(height - 1); \
 35	for (; outX < condition; ++outX, ++inX) { \
 36		renderer->spriteCyclesRemaining -= 2; \
 37		xAccum += mat.a; \
 38		yAccum += mat.c; \
 39		int localX = xAccum >> 8; \
 40		int localY = yAccum >> 8; \
 41		\
 42		if (localX & widthMask || localY & heightMask) { \
 43			break; \
 44		} \
 45		\
 46		SPRITE_YBASE_ ## DEPTH(localY); \
 47		SPRITE_XBASE_ ## DEPTH(localX); \
 48		SPRITE_DRAW_PIXEL_ ## DEPTH ## _ ## TYPE(localX); \
 49	}
 50
 51#define SPRITE_TRANSFORMED_MOSAIC_LOOP(DEPTH, TYPE) \
 52	unsigned tileData; \
 53	unsigned widthMask = ~(width - 1); \
 54	unsigned heightMask = ~(height - 1); \
 55	int localX = xAccum >> 8; \
 56	int localY = yAccum >> 8; \
 57	for (; outX < condition; ++outX, ++inX) { \
 58		renderer->spriteCyclesRemaining -= 2; \
 59		xAccum += mat.a; \
 60		yAccum += mat.c; \
 61		\
 62		if (outX % mosaicH == 0) { \
 63			localX = xAccum >> 8; \
 64			localY = yAccum >> 8; \
 65		} \
 66		\
 67		if (localX & widthMask || localY & heightMask) { \
 68			continue; \
 69		} \
 70		\
 71		SPRITE_YBASE_ ## DEPTH(localY); \
 72		SPRITE_XBASE_ ## DEPTH(localX); \
 73		SPRITE_DRAW_PIXEL_ ## DEPTH ## _ ## TYPE(localX); \
 74	}
 75
 76#define SPRITE_XBASE_16(localX) unsigned xBase = (localX & ~0x7) * 4 + ((localX >> 1) & 2);
 77#define SPRITE_YBASE_16(localY) unsigned yBase = (localY & ~0x7) * stride + (localY & 0x7) * 4;
 78
 79#define SPRITE_DRAW_PIXEL_16_NORMAL(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 && tileData) { \
 84		renderer->spriteLayer[outX] = palette[tileData] | flags; \
 85	}
 86
 87#define SPRITE_DRAW_PIXEL_16_NORMAL_OBJWIN(localX) \
 88	LOAD_16(tileData, ((yBase + charBase + xBase) & 0x7FFE), vramBase); \
 89	tileData = (tileData >> ((localX & 3) << 2)) & 0xF; \
 90	current = renderer->spriteLayer[outX]; \
 91	if ((current & FLAG_ORDER_MASK) > flags && tileData) { \
 92		unsigned color = (renderer->row[outX] & FLAG_OBJWIN) ? objwinPalette[tileData] : palette[tileData]; \
 93		renderer->spriteLayer[outX] = color | flags; \
 94	}
 95
 96#define SPRITE_DRAW_PIXEL_16_OBJWIN(localX) \
 97	LOAD_16(tileData, ((yBase + charBase + xBase) & 0x7FFE), vramBase); \
 98	tileData = (tileData >> ((localX & 3) << 2)) & 0xF; \
 99	if (tileData) { \
100		renderer->row[outX] |= FLAG_OBJWIN; \
101	}
102
103#define SPRITE_XBASE_256(localX) unsigned xBase = (localX & ~0x7) * 8 + (localX & 6);
104#define SPRITE_YBASE_256(localY) unsigned yBase = (localY & ~0x7) * stride + (localY & 0x7) * 8;
105
106#define SPRITE_DRAW_PIXEL_256_NORMAL(localX) \
107	LOAD_16(tileData, ((yBase + charBase + xBase) & 0x7FFE), vramBase); \
108	tileData = (tileData >> ((localX & 1) << 3)) & 0xFF; \
109	current = renderer->spriteLayer[outX]; \
110	if ((current & FLAG_ORDER_MASK) > flags && tileData) { \
111		renderer->spriteLayer[outX] = palette[tileData] | flags; \
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 && tileData) { \
119		unsigned color = (renderer->row[outX] & FLAG_OBJWIN) ? objwinPalette[tileData] : palette[tileData]; \
120		renderer->spriteLayer[outX] = color | flags; \
121	}
122
123#define SPRITE_DRAW_PIXEL_256_OBJWIN(localX) \
124	LOAD_16(tileData, ((yBase + charBase + xBase) & 0x7FFE), vramBase); \
125	tileData = (tileData >> ((localX & 1) << 3)) & 0xFF; \
126	if (tileData) { \
127		renderer->row[outX] |= FLAG_OBJWIN; \
128	}
129
130int GBAVideoSoftwareRendererPreprocessSprite(struct GBAVideoSoftwareRenderer* renderer, struct GBAObj* sprite, int y) {
131	int width = GBAVideoObjSizes[GBAObjAttributesAGetShape(sprite->a) * 4 + GBAObjAttributesBGetSize(sprite->b)][0];
132	int height = GBAVideoObjSizes[GBAObjAttributesAGetShape(sprite->a) * 4 + GBAObjAttributesBGetSize(sprite->b)][1];
133	int start = renderer->start;
134	int end = renderer->end;
135	uint32_t flags = GBAObjAttributesCGetPriority(sprite->c) << OFFSET_PRIORITY;
136	flags |= FLAG_TARGET_1 * ((GBAWindowControlIsBlendEnable(renderer->currentWindow.packed) && renderer->target1Obj && renderer->blendEffect == BLEND_ALPHA) || GBAObjAttributesAGetMode(sprite->a) == OBJ_MODE_SEMITRANSPARENT);
137	flags |= FLAG_OBJWIN * (GBAObjAttributesAGetMode(sprite->a) == OBJ_MODE_OBJWIN);
138	if ((flags & FLAG_OBJWIN) && renderer->currentWindow.priority < renderer->objwin.priority) {
139		return 0;
140	}
141	int32_t x = (uint32_t) GBAObjAttributesBGetX(sprite->b) << 23;
142	x >>= 23;
143	x += renderer->objOffsetX;
144	uint16_t* vramBase = &renderer->d.vram[BASE_TILE >> 1];
145	bool align = GBAObjAttributesAIs256Color(sprite->a) && !GBARegisterDISPCNTIsObjCharacterMapping(renderer->dispcnt);
146	unsigned charBase = (GBAObjAttributesCGetTile(sprite->c) & ~align) * 0x20;
147	if (GBARegisterDISPCNTGetMode(renderer->dispcnt) >= 3 && GBAObjAttributesCGetTile(sprite->c) < 512) {
148		return 0;
149	}
150
151	int objwinSlowPath = GBARegisterDISPCNTIsObjwinEnable(renderer->dispcnt) && GBAWindowControlGetBlendEnable(renderer->objwin.packed) != GBAWindowControlIsBlendEnable(renderer->currentWindow.packed);
152	int variant = renderer->target1Obj &&
153	              GBAWindowControlIsBlendEnable(renderer->currentWindow.packed) &&
154	              (renderer->blendEffect == BLEND_BRIGHTEN || renderer->blendEffect == BLEND_DARKEN);
155	if (GBAObjAttributesAGetMode(sprite->a) == OBJ_MODE_SEMITRANSPARENT || objwinSlowPath) {
156		int target2 = renderer->target2Bd;
157		target2 |= renderer->bg[0].target2;
158		target2 |= renderer->bg[1].target2;
159		target2 |= renderer->bg[2].target2;
160		target2 |= renderer->bg[3].target2;
161		if (target2) {
162			flags |= FLAG_REBLEND;
163			variant = 0;
164		} else {
165			flags &= ~FLAG_TARGET_1;
166		}
167	}
168
169	color_t* palette = &renderer->normalPalette[0x100];
170	color_t* objwinPalette = palette;
171
172	if (variant) {
173		palette = &renderer->variantPalette[0x100];
174		if (GBAWindowControlIsBlendEnable(renderer->objwin.packed)) {
175			objwinPalette = palette;
176		}
177	}
178
179	int inY = y - ((int) GBAObjAttributesAGetY(sprite->a) + renderer->objOffsetY);
180	int stride = GBARegisterDISPCNTIsObjCharacterMapping(renderer->dispcnt) ? (width >> !GBAObjAttributesAIs256Color(sprite->a)) : 0x80;
181
182	uint32_t current;
183	if (GBAObjAttributesAIsTransformed(sprite->a)) {
184		int totalWidth = width << GBAObjAttributesAGetDoubleSize(sprite->a);
185		int totalHeight = height << GBAObjAttributesAGetDoubleSize(sprite->a);
186		struct GBAOAMMatrix mat;
187		LOAD_16(mat.a, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].a);
188		LOAD_16(mat.b, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].b);
189		LOAD_16(mat.c, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].c);
190		LOAD_16(mat.d, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].d);
191
192		if (inY < 0) {
193			inY += 256;
194		}
195		int outX = x >= start ? x : start;
196		int condition = x + totalWidth;
197		int inX = outX - x;
198		if (end < condition) {
199			condition = end;
200		}
201		int mosaicH = 1;
202		if (GBAObjAttributesAIsMosaic(sprite->a)) {
203			mosaicH = GBAMosaicControlGetObjH(renderer->mosaic) + 1;
204			if (condition % mosaicH) {
205				condition += mosaicH - (condition % mosaicH);
206			}
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 (mosaicH > 1) {
259				if (objwinSlowPath) {
260					objwinPalette = &objwinPalette[GBAObjAttributesCGetPalette(sprite->c) << 4];
261					SPRITE_TRANSFORMED_MOSAIC_LOOP(16, NORMAL_OBJWIN);
262				} else {
263					SPRITE_TRANSFORMED_MOSAIC_LOOP(16, NORMAL);
264				}
265			} else if (objwinSlowPath) {
266				objwinPalette = &objwinPalette[GBAObjAttributesCGetPalette(sprite->c) << 4];
267				SPRITE_TRANSFORMED_LOOP(16, NORMAL_OBJWIN);
268			} else {
269				SPRITE_TRANSFORMED_LOOP(16, NORMAL);
270			}
271		} else {
272			if (flags & FLAG_OBJWIN) {
273				SPRITE_TRANSFORMED_LOOP(256, OBJWIN);
274			} else if (mosaicH > 1) {
275				if (objwinSlowPath) {
276					SPRITE_TRANSFORMED_MOSAIC_LOOP(256, NORMAL_OBJWIN);
277				} else {
278					SPRITE_TRANSFORMED_MOSAIC_LOOP(256, NORMAL);
279				}
280			} else if (objwinSlowPath) {
281				SPRITE_TRANSFORMED_LOOP(256, NORMAL_OBJWIN);
282			} else {
283				SPRITE_TRANSFORMED_LOOP(256, NORMAL);
284			}
285		}
286		if (end == GBA_VIDEO_HORIZONTAL_PIXELS && x + totalWidth > GBA_VIDEO_HORIZONTAL_PIXELS) {
287			renderer->spriteCyclesRemaining -= (x + totalWidth - GBA_VIDEO_HORIZONTAL_PIXELS) * 2;
288		}
289	} else {
290		int outX = x >= start ? x : start;
291		int condition = x + width;
292		int mosaicH = 1;
293		if (GBAObjAttributesAIsMosaic(sprite->a)) {
294			mosaicH = GBAMosaicControlGetObjH(renderer->mosaic) + 1;
295			if (condition % mosaicH) {
296				condition += mosaicH - (condition % mosaicH);
297			}
298		}
299		if ((int) GBAObjAttributesAGetY(sprite->a) + height - 256 >= 0) {
300			inY += 256;
301		}
302		if (GBAObjAttributesBIsVFlip(sprite->b)) {
303			inY = height - inY - 1;
304		}
305		if (end < condition) {
306			condition = end;
307		}
308		int inX = outX - x;
309		int xOffset = 1;
310		if (GBAObjAttributesBIsHFlip(sprite->b)) {
311			inX = width - inX - 1;
312			xOffset = -1;
313		}
314		if (!GBAObjAttributesAIs256Color(sprite->a)) {
315			palette = &palette[GBAObjAttributesCGetPalette(sprite->c) << 4];
316			if (flags & FLAG_OBJWIN) {
317				SPRITE_NORMAL_LOOP(16, OBJWIN);
318			} else if (mosaicH > 1) {
319				if (objwinSlowPath) {
320					objwinPalette = &objwinPalette[GBAObjAttributesCGetPalette(sprite->c) << 4];
321					SPRITE_MOSAIC_LOOP(16, NORMAL_OBJWIN);
322				} else {
323					SPRITE_MOSAIC_LOOP(16, NORMAL);
324				}
325			} else if (objwinSlowPath) {
326				objwinPalette = &objwinPalette[GBAObjAttributesCGetPalette(sprite->c) << 4];
327				SPRITE_NORMAL_LOOP(16, NORMAL_OBJWIN);
328			} else {
329				SPRITE_NORMAL_LOOP(16, NORMAL);
330			}
331		} else {
332			if (flags & FLAG_OBJWIN) {
333				SPRITE_NORMAL_LOOP(256, OBJWIN);
334			} else if (mosaicH > 1) {
335				if (objwinSlowPath) {
336					SPRITE_MOSAIC_LOOP(256, NORMAL_OBJWIN);
337				} else {
338					SPRITE_MOSAIC_LOOP(256, NORMAL);
339				}
340			} else if (objwinSlowPath) {
341				SPRITE_NORMAL_LOOP(256, NORMAL_OBJWIN);
342			} else {
343				SPRITE_NORMAL_LOOP(256, NORMAL);
344			}
345		}
346		if (end == GBA_VIDEO_HORIZONTAL_PIXELS && x + width > GBA_VIDEO_HORIZONTAL_PIXELS) {
347			renderer->spriteCyclesRemaining -= x + width - GBA_VIDEO_HORIZONTAL_PIXELS;
348		}
349	}
350	return 1;
351}
352
353void GBAVideoSoftwareRendererPostprocessSprite(struct GBAVideoSoftwareRenderer* renderer, unsigned priority) {
354	int x;
355	uint32_t* pixel = &renderer->row[renderer->start];
356	uint32_t flags = FLAG_TARGET_2 * renderer->target2Obj;
357
358	int objwinSlowPath = GBARegisterDISPCNTIsObjwinEnable(renderer->dispcnt);
359	bool objwinDisable = false;
360	bool objwinOnly = false;
361	if (objwinSlowPath) {
362		objwinDisable = !GBAWindowControlIsObjEnable(renderer->objwin.packed);
363		objwinOnly = !objwinDisable && !GBAWindowControlIsObjEnable(renderer->currentWindow.packed);
364		if (objwinDisable && !GBAWindowControlIsObjEnable(renderer->currentWindow.packed)) {
365			return;
366		}
367
368		if (objwinDisable) {
369			for (x = renderer->start; x < renderer->end; ++x, ++pixel) {
370				uint32_t color = renderer->spriteLayer[x] & ~FLAG_OBJWIN;
371				uint32_t current = *pixel;
372				if ((color & FLAG_UNWRITTEN) != FLAG_UNWRITTEN && !(current & FLAG_OBJWIN) && (color & FLAG_PRIORITY) >> OFFSET_PRIORITY == priority) {
373					_compositeBlendObjwin(renderer, pixel, color | flags, current);
374				}
375			}
376			return;
377		} else if (objwinOnly) {
378			for (x = renderer->start; x < renderer->end; ++x, ++pixel) {
379				uint32_t color = renderer->spriteLayer[x] & ~FLAG_OBJWIN;
380				uint32_t current = *pixel;
381				if ((color & FLAG_UNWRITTEN) != FLAG_UNWRITTEN && (current & FLAG_OBJWIN) && (color & FLAG_PRIORITY) >> OFFSET_PRIORITY == priority) {
382					_compositeBlendObjwin(renderer, pixel, color | flags, current);
383				}
384			}
385			return;
386		} else {
387			for (x = renderer->start; x < renderer->end; ++x, ++pixel) {
388				uint32_t color = renderer->spriteLayer[x] & ~FLAG_OBJWIN;
389				uint32_t current = *pixel;
390				if ((color & FLAG_UNWRITTEN) != FLAG_UNWRITTEN && (color & FLAG_PRIORITY) >> OFFSET_PRIORITY == priority) {
391					_compositeBlendObjwin(renderer, pixel, color | flags, current);
392				}
393			}
394			return;
395		}
396	} else if (!GBAWindowControlIsObjEnable(renderer->currentWindow.packed)) {
397		return;
398	}
399	for (x = renderer->start; x < renderer->end; ++x, ++pixel) {
400		uint32_t color = renderer->spriteLayer[x] & ~FLAG_OBJWIN;
401		uint32_t current = *pixel;
402		if ((color & FLAG_UNWRITTEN) != FLAG_UNWRITTEN && (color & FLAG_PRIORITY) >> OFFSET_PRIORITY == priority) {
403			_compositeBlendNoObjwin(renderer, pixel, color | flags, current);
404		}
405	}
406}