all repos — mgba @ dbef26759cdf27bd60ca603c76b415150cc89840

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) { \
 84		if (tileData) { \
 85			renderer->spriteLayer[outX] = palette[tileData] | flags; \
 86		} else if (current != FLAG_UNWRITTEN) { \
 87			renderer->spriteLayer[outX] = (current & ~(FLAG_ORDER_MASK | FLAG_REBLEND | FLAG_TARGET_1)) | (flags & (FLAG_ORDER_MASK | FLAG_REBLEND | FLAG_TARGET_1)); \
 88		} \
 89	}
 90
 91#define SPRITE_DRAW_PIXEL_16_NORMAL_OBJWIN(localX) \
 92	LOAD_16(tileData, ((yBase + charBase + xBase) & 0x7FFE), vramBase); \
 93	tileData = (tileData >> ((localX & 3) << 2)) & 0xF; \
 94	current = renderer->spriteLayer[outX]; \
 95	if ((current & FLAG_ORDER_MASK) > flags) { \
 96		if (tileData) { \
 97			unsigned color = (renderer->row[outX] & FLAG_OBJWIN) ? objwinPalette[tileData] : palette[tileData]; \
 98			renderer->spriteLayer[outX] = color | flags; \
 99		} else if (current != FLAG_UNWRITTEN) { \
100			renderer->spriteLayer[outX] = (current & ~(FLAG_ORDER_MASK | FLAG_REBLEND | FLAG_TARGET_1)) | (flags & (FLAG_ORDER_MASK | FLAG_REBLEND | FLAG_TARGET_1)); \
101		} \
102	}
103
104#define SPRITE_DRAW_PIXEL_16_OBJWIN(localX) \
105	LOAD_16(tileData, ((yBase + charBase + xBase) & 0x7FFE), vramBase); \
106	tileData = (tileData >> ((localX & 3) << 2)) & 0xF; \
107	if (tileData) { \
108		renderer->row[outX] |= FLAG_OBJWIN; \
109	}
110
111#define SPRITE_XBASE_256(localX) unsigned xBase = (localX & ~0x7) * 8 + (localX & 6);
112#define SPRITE_YBASE_256(localY) unsigned yBase = (localY & ~0x7) * stride + (localY & 0x7) * 8;
113
114#define SPRITE_DRAW_PIXEL_256_NORMAL(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			renderer->spriteLayer[outX] = palette[tileData] | flags; \
121		} else if (current != FLAG_UNWRITTEN) { \
122			renderer->spriteLayer[outX] = (current & ~(FLAG_ORDER_MASK | FLAG_REBLEND | FLAG_TARGET_1)) | (flags & (FLAG_ORDER_MASK | FLAG_REBLEND | FLAG_TARGET_1)); \
123		} \
124	}
125
126#define SPRITE_DRAW_PIXEL_256_NORMAL_OBJWIN(localX) \
127	LOAD_16(tileData, ((yBase + charBase + xBase) & 0x7FFE), vramBase);  \
128	tileData = (tileData >> ((localX & 1) << 3)) & 0xFF; \
129	current = renderer->spriteLayer[outX]; \
130	if ((current & FLAG_ORDER_MASK) > flags) { \
131		if (tileData) { \
132			unsigned color = (renderer->row[outX] & FLAG_OBJWIN) ? objwinPalette[tileData] : palette[tileData]; \
133			renderer->spriteLayer[outX] = color | flags; \
134		} else if (current != FLAG_UNWRITTEN) { \
135			renderer->spriteLayer[outX] = (current & ~(FLAG_ORDER_MASK | FLAG_REBLEND | FLAG_TARGET_1)) | (flags & (FLAG_ORDER_MASK | FLAG_REBLEND | FLAG_TARGET_1)); \
136		} \
137	}
138
139#define SPRITE_DRAW_PIXEL_256_OBJWIN(localX) \
140	LOAD_16(tileData, ((yBase + charBase + xBase) & 0x7FFE), vramBase); \
141	tileData = (tileData >> ((localX & 1) << 3)) & 0xFF; \
142	if (tileData) { \
143		renderer->row[outX] |= FLAG_OBJWIN; \
144	}
145
146int GBAVideoSoftwareRendererPreprocessSprite(struct GBAVideoSoftwareRenderer* renderer, struct GBAObj* sprite, int index, int y) {
147	int width = GBAVideoObjSizes[GBAObjAttributesAGetShape(sprite->a) * 4 + GBAObjAttributesBGetSize(sprite->b)][0];
148	int height = GBAVideoObjSizes[GBAObjAttributesAGetShape(sprite->a) * 4 + GBAObjAttributesBGetSize(sprite->b)][1];
149	int start = renderer->start;
150	int end = renderer->end;
151	uint32_t flags = GBAObjAttributesCGetPriority(sprite->c) << OFFSET_PRIORITY;
152	flags |= FLAG_TARGET_1 * ((GBAWindowControlIsBlendEnable(renderer->currentWindow.packed) && renderer->target1Obj && renderer->blendEffect == BLEND_ALPHA) || GBAObjAttributesAGetMode(sprite->a) == OBJ_MODE_SEMITRANSPARENT);
153	flags |= FLAG_OBJWIN * (GBAObjAttributesAGetMode(sprite->a) == OBJ_MODE_OBJWIN);
154	if ((flags & FLAG_OBJWIN) && renderer->currentWindow.priority < renderer->objwin.priority) {
155		return 0;
156	}
157	int32_t x = (uint32_t) GBAObjAttributesBGetX(sprite->b) << 23;
158	x >>= 23;
159	x += renderer->objOffsetX;
160	uint16_t* vramBase = &renderer->d.vram[BASE_TILE >> 1];
161	unsigned align = GBAObjAttributesAIs256Color(sprite->a) && !GBARegisterDISPCNTIsObjCharacterMapping(renderer->dispcnt);
162	unsigned charBase = (GBAObjAttributesCGetTile(sprite->c) & ~align) * 0x20;
163	if (GBARegisterDISPCNTGetMode(renderer->dispcnt) >= 3 && GBAObjAttributesCGetTile(sprite->c) < 512) {
164		return 0;
165	}
166
167	int objwinSlowPath = GBARegisterDISPCNTIsObjwinEnable(renderer->dispcnt) && GBAWindowControlGetBlendEnable(renderer->objwin.packed) != GBAWindowControlIsBlendEnable(renderer->currentWindow.packed);
168	int variant = (renderer->target1Obj || GBAObjAttributesAGetMode(sprite->a) == OBJ_MODE_SEMITRANSPARENT) &&
169	              GBAWindowControlIsBlendEnable(renderer->currentWindow.packed) &&
170	              (renderer->blendEffect == BLEND_BRIGHTEN || renderer->blendEffect == BLEND_DARKEN);
171	if (GBAObjAttributesAGetMode(sprite->a) == OBJ_MODE_SEMITRANSPARENT || objwinSlowPath) {
172		int target2 = renderer->target2Bd;
173		target2 |= renderer->bg[0].target2;
174		target2 |= renderer->bg[1].target2;
175		target2 |= renderer->bg[2].target2;
176		target2 |= renderer->bg[3].target2;
177		if (target2) {
178			renderer->forceTarget1 = true;
179			flags |= FLAG_REBLEND;
180			variant = 0;
181		} else {
182			flags &= ~FLAG_TARGET_1;
183		}
184	}
185
186	color_t* palette = &renderer->normalPalette[0x100];
187	if (renderer->d.highlightAmount && renderer->d.highlightOBJ[index]) {
188		palette = &renderer->highlightPalette[0x100];
189	}
190	color_t* objwinPalette = palette;
191
192	if (variant) {
193		palette = &renderer->variantPalette[0x100];
194		if (renderer->d.highlightAmount && renderer->d.highlightOBJ[index]) {
195			palette = &renderer->highlightVariantPalette[0x100];
196		}
197		if (GBAWindowControlIsBlendEnable(renderer->objwin.packed)) {
198			objwinPalette = palette;
199		}
200	}
201
202	int inY = y - ((int) GBAObjAttributesAGetY(sprite->a) + renderer->objOffsetY);
203	int stride = GBARegisterDISPCNTIsObjCharacterMapping(renderer->dispcnt) ? (width >> !GBAObjAttributesAIs256Color(sprite->a)) : 0x80;
204
205	uint32_t current;
206	if (GBAObjAttributesAIsTransformed(sprite->a)) {
207		int totalWidth = width << GBAObjAttributesAGetDoubleSize(sprite->a);
208		int totalHeight = height << GBAObjAttributesAGetDoubleSize(sprite->a);
209		struct GBAOAMMatrix mat;
210		LOAD_16(mat.a, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].a);
211		LOAD_16(mat.b, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].b);
212		LOAD_16(mat.c, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].c);
213		LOAD_16(mat.d, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].d);
214
215		if (inY < 0) {
216			inY += 256;
217		}
218		int outX = x >= start ? x : start;
219		int condition = x + totalWidth;
220		int inX = outX - x;
221		if (end < condition) {
222			condition = end;
223		}
224		int mosaicH = 1;
225		if (GBAObjAttributesAIsMosaic(sprite->a)) {
226			mosaicH = GBAMosaicControlGetObjH(renderer->mosaic) + 1;
227			if (condition % mosaicH) {
228				condition += mosaicH - (condition % mosaicH);
229			}
230		}
231
232		int xAccum = mat.a * (inX - 1 - (totalWidth >> 1)) + mat.b * (inY - (totalHeight >> 1)) + (width << 7);
233		int yAccum = mat.c * (inX - 1 - (totalWidth >> 1)) + mat.d * (inY - (totalHeight >> 1)) + (height << 7);
234
235		// Clip off early pixels
236		// TODO: Transform end coordinates too
237		if (mat.a) {
238			if ((xAccum >> 8) < 0) {
239				int32_t diffX = -xAccum - 1;
240				int32_t x = mat.a ? diffX / mat.a : 0;
241				xAccum += mat.a * x;
242				yAccum += mat.c * x;
243				outX += x;
244				inX += x;
245			} else if ((xAccum >> 8) >= width) {
246				int32_t diffX = (width << 8) - xAccum;
247				int32_t x = mat.a ? diffX / mat.a : 0;
248				xAccum += mat.a * x;
249				yAccum += mat.c * x;
250				outX += x;
251				inX += x;
252			}
253		}
254		if (mat.c) {
255			if ((yAccum >> 8) < 0) {
256				int32_t diffY = - yAccum - 1;
257				int32_t y = mat.c ? diffY / mat.c : 0;
258				xAccum += mat.a * y;
259				yAccum += mat.c * y;
260				outX += y;
261				inX += y;
262			} else if ((yAccum >> 8) >= height) {
263				int32_t diffY = (height << 8) - yAccum;
264				int32_t y = mat.c ? diffY / mat.c : 0;
265				xAccum += mat.a * y;
266				yAccum += mat.c * y;
267				outX += y;
268				inX += y;
269			}
270		}
271
272		if (outX < start || outX >= condition) {
273			return 0;
274		}
275		renderer->spriteCyclesRemaining -= 10;
276
277		if (!GBAObjAttributesAIs256Color(sprite->a)) {
278			palette = &palette[GBAObjAttributesCGetPalette(sprite->c) << 4];
279			if (flags & FLAG_OBJWIN) {
280				SPRITE_TRANSFORMED_LOOP(16, OBJWIN);
281			} else if (mosaicH > 1) {
282				if (objwinSlowPath) {
283					objwinPalette = &objwinPalette[GBAObjAttributesCGetPalette(sprite->c) << 4];
284					SPRITE_TRANSFORMED_MOSAIC_LOOP(16, NORMAL_OBJWIN);
285				} else {
286					SPRITE_TRANSFORMED_MOSAIC_LOOP(16, NORMAL);
287				}
288			} else if (objwinSlowPath) {
289				objwinPalette = &objwinPalette[GBAObjAttributesCGetPalette(sprite->c) << 4];
290				SPRITE_TRANSFORMED_LOOP(16, NORMAL_OBJWIN);
291			} else {
292				SPRITE_TRANSFORMED_LOOP(16, NORMAL);
293			}
294		} else {
295			if (flags & FLAG_OBJWIN) {
296				SPRITE_TRANSFORMED_LOOP(256, OBJWIN);
297			} else if (mosaicH > 1) {
298				if (objwinSlowPath) {
299					SPRITE_TRANSFORMED_MOSAIC_LOOP(256, NORMAL_OBJWIN);
300				} else {
301					SPRITE_TRANSFORMED_MOSAIC_LOOP(256, NORMAL);
302				}
303			} else if (objwinSlowPath) {
304				SPRITE_TRANSFORMED_LOOP(256, NORMAL_OBJWIN);
305			} else {
306				SPRITE_TRANSFORMED_LOOP(256, NORMAL);
307			}
308		}
309		if (end == GBA_VIDEO_HORIZONTAL_PIXELS && x + totalWidth > GBA_VIDEO_HORIZONTAL_PIXELS) {
310			renderer->spriteCyclesRemaining -= (x + totalWidth - GBA_VIDEO_HORIZONTAL_PIXELS) * 2;
311		}
312	} else {
313		int outX = x >= start ? x : start;
314		int condition = x + width;
315		int mosaicH = 1;
316		if (GBAObjAttributesAIsMosaic(sprite->a)) {
317			mosaicH = GBAMosaicControlGetObjH(renderer->mosaic) + 1;
318			if (condition % mosaicH) {
319				condition += mosaicH - (condition % mosaicH);
320			}
321		}
322		if ((int) GBAObjAttributesAGetY(sprite->a) + height - 256 >= 0) {
323			inY += 256;
324		}
325		if (GBAObjAttributesBIsVFlip(sprite->b)) {
326			inY = height - inY - 1;
327		}
328		if (end < condition) {
329			condition = end;
330		}
331		int inX = outX - x;
332		int xOffset = 1;
333		if (GBAObjAttributesBIsHFlip(sprite->b)) {
334			inX = width - inX - 1;
335			xOffset = -1;
336		}
337		if (!GBAObjAttributesAIs256Color(sprite->a)) {
338			palette = &palette[GBAObjAttributesCGetPalette(sprite->c) << 4];
339			if (flags & FLAG_OBJWIN) {
340				SPRITE_NORMAL_LOOP(16, OBJWIN);
341			} else if (mosaicH > 1) {
342				if (objwinSlowPath) {
343					objwinPalette = &objwinPalette[GBAObjAttributesCGetPalette(sprite->c) << 4];
344					SPRITE_MOSAIC_LOOP(16, NORMAL_OBJWIN);
345				} else {
346					SPRITE_MOSAIC_LOOP(16, NORMAL);
347				}
348			} else if (objwinSlowPath) {
349				objwinPalette = &objwinPalette[GBAObjAttributesCGetPalette(sprite->c) << 4];
350				SPRITE_NORMAL_LOOP(16, NORMAL_OBJWIN);
351			} else {
352				SPRITE_NORMAL_LOOP(16, NORMAL);
353			}
354		} else {
355			if (flags & FLAG_OBJWIN) {
356				SPRITE_NORMAL_LOOP(256, OBJWIN);
357			} else if (mosaicH > 1) {
358				if (objwinSlowPath) {
359					SPRITE_MOSAIC_LOOP(256, NORMAL_OBJWIN);
360				} else {
361					SPRITE_MOSAIC_LOOP(256, NORMAL);
362				}
363			} else if (objwinSlowPath) {
364				SPRITE_NORMAL_LOOP(256, NORMAL_OBJWIN);
365			} else {
366				SPRITE_NORMAL_LOOP(256, NORMAL);
367			}
368		}
369		if (end == GBA_VIDEO_HORIZONTAL_PIXELS && x + width > GBA_VIDEO_HORIZONTAL_PIXELS) {
370			renderer->spriteCyclesRemaining -= x + width - GBA_VIDEO_HORIZONTAL_PIXELS;
371		}
372	}
373	return 1;
374}
375
376void GBAVideoSoftwareRendererPostprocessSprite(struct GBAVideoSoftwareRenderer* renderer, unsigned priority) {
377	int x;
378	uint32_t* pixel = &renderer->row[renderer->start];
379	uint32_t flags = FLAG_TARGET_2 * renderer->target2Obj;
380
381	int objwinSlowPath = GBARegisterDISPCNTIsObjwinEnable(renderer->dispcnt);
382	bool objwinDisable = false;
383	bool objwinOnly = false;
384	if (objwinSlowPath) {
385		objwinDisable = !GBAWindowControlIsObjEnable(renderer->objwin.packed);
386		objwinOnly = !objwinDisable && !GBAWindowControlIsObjEnable(renderer->currentWindow.packed);
387		if (objwinDisable && !GBAWindowControlIsObjEnable(renderer->currentWindow.packed)) {
388			return;
389		}
390
391		if (objwinDisable) {
392			for (x = renderer->start; x < renderer->end; ++x, ++pixel) {
393				uint32_t color = renderer->spriteLayer[x] & ~FLAG_OBJWIN;
394				uint32_t current = *pixel;
395				if ((color & FLAG_UNWRITTEN) != FLAG_UNWRITTEN && !(current & FLAG_OBJWIN) && (color & FLAG_PRIORITY) >> OFFSET_PRIORITY == priority) {
396					_compositeBlendObjwin(renderer, pixel, color | flags, current);
397				}
398			}
399			return;
400		} else if (objwinOnly) {
401			for (x = renderer->start; x < renderer->end; ++x, ++pixel) {
402				uint32_t color = renderer->spriteLayer[x] & ~FLAG_OBJWIN;
403				uint32_t current = *pixel;
404				if ((color & FLAG_UNWRITTEN) != FLAG_UNWRITTEN && (current & FLAG_OBJWIN) && (color & FLAG_PRIORITY) >> OFFSET_PRIORITY == priority) {
405					_compositeBlendObjwin(renderer, pixel, color | flags, current);
406				}
407			}
408			return;
409		} else {
410			for (x = renderer->start; x < renderer->end; ++x, ++pixel) {
411				uint32_t color = renderer->spriteLayer[x] & ~FLAG_OBJWIN;
412				uint32_t current = *pixel;
413				if ((color & FLAG_UNWRITTEN) != FLAG_UNWRITTEN && (color & FLAG_PRIORITY) >> OFFSET_PRIORITY == priority) {
414					_compositeBlendObjwin(renderer, pixel, color | flags, current);
415				}
416			}
417			return;
418		}
419	} else if (!GBAWindowControlIsObjEnable(renderer->currentWindow.packed)) {
420		return;
421	}
422	for (x = renderer->start; x < renderer->end; ++x, ++pixel) {
423		uint32_t color = renderer->spriteLayer[x] & ~FLAG_OBJWIN;
424		uint32_t current = *pixel;
425		if ((color & FLAG_UNWRITTEN) != FLAG_UNWRITTEN && (color & FLAG_PRIORITY) >> OFFSET_PRIORITY == priority) {
426			_compositeBlendNoObjwin(renderer, pixel, color | flags, current);
427		}
428	}
429}