all repos — mgba @ 0c51bdf61828104fba2c3a707b1d507d98f8f72d

mGBA Game Boy Advance Emulator

src/gba/renderers/software-private.h (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#ifndef SOFTWARE_PRIVATE_H
  7#define SOFTWARE_PRIVATE_H
  8
  9#include <mgba/internal/arm/macros.h>
 10#include <mgba/internal/gba/renderers/video-software.h>
 11
 12#ifdef NDEBUG
 13#define VIDEO_CHECKS false
 14#else
 15#define VIDEO_CHECKS true
 16#endif
 17
 18void GBAVideoSoftwareRendererDrawBackgroundMode0(struct GBAVideoSoftwareRenderer* renderer,
 19                                                 struct GBAVideoSoftwareBackground* background, int y);
 20void GBAVideoSoftwareRendererDrawBackgroundMode2(struct GBAVideoSoftwareRenderer* renderer,
 21                                                 struct GBAVideoSoftwareBackground* background, int y);
 22void GBAVideoSoftwareRendererDrawBackgroundMode3(struct GBAVideoSoftwareRenderer* renderer,
 23                                                 struct GBAVideoSoftwareBackground* background, int y);
 24void GBAVideoSoftwareRendererDrawBackgroundMode4(struct GBAVideoSoftwareRenderer* renderer,
 25                                                 struct GBAVideoSoftwareBackground* background, int y);
 26void GBAVideoSoftwareRendererDrawBackgroundMode5(struct GBAVideoSoftwareRenderer* renderer,
 27                                                 struct GBAVideoSoftwareBackground* background, int y);
 28
 29int GBAVideoSoftwareRendererPreprocessSprite(struct GBAVideoSoftwareRenderer* renderer, struct GBAObj* sprite, int index, int y);
 30void GBAVideoSoftwareRendererPostprocessSprite(struct GBAVideoSoftwareRenderer* renderer, unsigned priority);
 31
 32static inline unsigned _brighten(unsigned color, int y);
 33static inline unsigned _darken(unsigned color, int y);
 34static unsigned _mix(int weightA, unsigned colorA, int weightB, unsigned colorB);
 35
 36
 37// We stash the priority on the top bits so we can do a one-operator comparison
 38// The lower the number, the higher the priority, and sprites take precedence over backgrounds
 39// We want to do special processing if the color pixel is target 1, however
 40
 41static inline void _compositeBlendObjwin(struct GBAVideoSoftwareRenderer* renderer, uint32_t* pixel, uint32_t color, uint32_t current) {
 42	if (color >= current) {
 43		if (current & FLAG_TARGET_1 && color & FLAG_TARGET_2) {
 44			color = _mix(renderer->blda, current, renderer->bldb, color);
 45		} else {
 46			color = (current & 0x00FFFFFF) | (current & (FLAG_REBLEND | FLAG_OBJWIN));
 47		}
 48	} else {
 49		color = (color & ~FLAG_TARGET_2) | (current & FLAG_OBJWIN);
 50	}
 51	*pixel = color;
 52}
 53
 54static inline void _compositeBlendNoObjwin(struct GBAVideoSoftwareRenderer* renderer, uint32_t* pixel, uint32_t color, uint32_t current) {
 55	if (!IS_WRITABLE(current)) { \
 56		return; \
 57	} \
 58	if (color >= current) {
 59		if (current & FLAG_TARGET_1 && color & FLAG_TARGET_2) {
 60			color = _mix(renderer->blda, current, renderer->bldb, color);
 61		} else {
 62			color = (current & 0x00FFFFFF) | (current & (FLAG_REBLEND | FLAG_OBJWIN));
 63		}
 64	} else {
 65		color = color & ~FLAG_TARGET_2;
 66	}
 67	*pixel = color;
 68}
 69
 70static inline void _compositeNoBlendObjwin(struct GBAVideoSoftwareRenderer* renderer, uint32_t* pixel, uint32_t color,
 71                                           uint32_t current) {
 72	UNUSED(renderer);
 73	if (color < current) {
 74		color |= (current & FLAG_OBJWIN);
 75	} else {
 76		color = (current & 0x00FFFFFF) | (current & (FLAG_REBLEND | FLAG_OBJWIN));
 77	}
 78	*pixel = color;
 79}
 80
 81static inline void _compositeNoBlendNoObjwin(struct GBAVideoSoftwareRenderer* renderer, uint32_t* pixel, uint32_t color,
 82                                             uint32_t current) {
 83	UNUSED(renderer);
 84	if (color >= current) {
 85		color = (current & 0x00FFFFFF) | (current & (FLAG_REBLEND | FLAG_OBJWIN));
 86	}
 87	*pixel = color;
 88}
 89
 90#define COMPOSITE_16_OBJWIN(BLEND, IDX)  \
 91	if (!IS_WRITABLE(current)) { \
 92		continue; \
 93	} \
 94	if (objwinForceEnable || (!(current & FLAG_OBJWIN)) == objwinOnly) {                                          \
 95		unsigned color = (current & FLAG_OBJWIN) ? objwinPalette[paletteData | pixelData] : palette[pixelData]; \
 96		unsigned mergedFlags = flags; \
 97		if (current & FLAG_OBJWIN) { \
 98			mergedFlags = objwinFlags; \
 99		} \
100		_composite ## BLEND ## Objwin(renderer, &pixel[IDX], color | mergedFlags, current); \
101	}
102
103#define COMPOSITE_16_NO_OBJWIN(BLEND, IDX) \
104	_composite ## BLEND ## NoObjwin(renderer, &pixel[IDX], palette[pixelData] | flags, current);
105
106#define COMPOSITE_256_OBJWIN(BLEND, IDX) \
107	if (!IS_WRITABLE(current)) { \
108		continue; \
109	} \
110	if (objwinForceEnable || (!(current & FLAG_OBJWIN)) == objwinOnly) { \
111		unsigned color = (current & FLAG_OBJWIN) ? objwinPalette[pixelData] : palette[pixelData]; \
112		unsigned mergedFlags = flags; \
113		if (current & FLAG_OBJWIN) { \
114			mergedFlags = objwinFlags; \
115		} \
116		_composite ## BLEND ## Objwin(renderer, &pixel[IDX], color | mergedFlags, current); \
117	}
118
119#define COMPOSITE_256_NO_OBJWIN COMPOSITE_16_NO_OBJWIN
120
121#define BACKGROUND_DRAW_PIXEL_16(BLEND, OBJWIN, IDX) \
122	pixelData = tileData & 0xF; \
123	current = pixel[IDX]; \
124	if (pixelData && IS_WRITABLE(current)) { \
125		COMPOSITE_16_ ## OBJWIN (BLEND, IDX); \
126	} \
127	tileData >>= 4;
128
129#define BACKGROUND_DRAW_PIXEL_256(BLEND, OBJWIN, IDX) \
130	pixelData = tileData & 0xFF; \
131	current = pixel[IDX]; \
132	if (pixelData && IS_WRITABLE(current)) { \
133		COMPOSITE_256_ ## OBJWIN (BLEND, IDX); \
134	} \
135	tileData >>= 8;
136
137// TODO: Remove UNUSEDs after implementing OBJWIN for modes 3 - 5
138#define PREPARE_OBJWIN                                                                            \
139	int objwinSlowPath = GBARegisterDISPCNTIsObjwinEnable(renderer->dispcnt);                     \
140	int objwinOnly = 0;                                                                           \
141	int objwinForceEnable = 0;                                                                    \
142	UNUSED(objwinForceEnable);                                                                    \
143	color_t* objwinPalette = renderer->normalPalette;                                             \
144	if (renderer->d.highlightAmount && background->highlight) {                                   \
145		objwinPalette = renderer->highlightPalette;                                               \
146	}                                                                                             \
147	UNUSED(objwinPalette);                                                                        \
148	if (objwinSlowPath) {                                                                         \
149		if (background->target1 && GBAWindowControlIsBlendEnable(renderer->objwin.packed) &&      \
150		    (renderer->blendEffect == BLEND_BRIGHTEN || renderer->blendEffect == BLEND_DARKEN)) { \
151			objwinPalette = renderer->variantPalette;                                             \
152			if (renderer->d.highlightAmount && background->highlight) {                           \
153				palette = renderer->highlightVariantPalette;                                      \
154			}                                                                                     \
155		}                                                                                         \
156		switch (background->index) {                                                              \
157		case 0:                                                                                   \
158			objwinForceEnable = GBAWindowControlIsBg0Enable(renderer->objwin.packed) &&           \
159			    GBAWindowControlIsBg0Enable(renderer->currentWindow.packed);                      \
160			objwinOnly = !GBAWindowControlIsBg0Enable(renderer->objwin.packed);                   \
161			break;                                                                                \
162		case 1:                                                                                   \
163			objwinForceEnable = GBAWindowControlIsBg1Enable(renderer->objwin.packed) &&           \
164			    GBAWindowControlIsBg1Enable(renderer->currentWindow.packed);                      \
165			objwinOnly = !GBAWindowControlIsBg1Enable(renderer->objwin.packed);                   \
166			break;                                                                                \
167		case 2:                                                                                   \
168			objwinForceEnable = GBAWindowControlIsBg2Enable(renderer->objwin.packed) &&           \
169			    GBAWindowControlIsBg2Enable(renderer->currentWindow.packed);                      \
170			objwinOnly = !GBAWindowControlIsBg2Enable(renderer->objwin.packed);                   \
171			break;                                                                                \
172		case 3:                                                                                   \
173			objwinForceEnable = GBAWindowControlIsBg3Enable(renderer->objwin.packed) &&           \
174			    GBAWindowControlIsBg3Enable(renderer->currentWindow.packed);                      \
175			objwinOnly = !GBAWindowControlIsBg3Enable(renderer->objwin.packed);                   \
176			break;                                                                                \
177		}                                                                                         \
178	}
179
180static inline unsigned _brighten(unsigned color, int y) {
181	unsigned c = 0;
182	unsigned a;
183#ifdef COLOR_16_BIT
184	a = color & 0x1F;
185	c |= (a + ((0x1F - a) * y) / 16) & 0x1F;
186
187#ifdef COLOR_5_6_5
188	a = color & 0x7C0;
189	c |= (a + ((0x7C0 - a) * y) / 16) & 0x7C0;
190
191	a = color & 0xF800;
192	c |= (a + ((0xF800 - a) * y) / 16) & 0xF800;
193#else
194	a = color & 0x3E0;
195	c |= (a + ((0x3E0 - a) * y) / 16) & 0x3E0;
196
197	a = color & 0x7C00;
198	c |= (a + ((0x7C00 - a) * y) / 16) & 0x7C00;
199#endif
200#else
201	a = color & 0xFF;
202	c |= (a + ((0xFF - a) * y) / 16) & 0xFF;
203
204	a = color & 0xFF00;
205	c |= (a + ((0xFF00 - a) * y) / 16) & 0xFF00;
206
207	a = color & 0xFF0000;
208	c |= (a + ((0xFF0000 - a) * y) / 16) & 0xFF0000;
209#endif
210	return c;
211}
212
213static inline unsigned _darken(unsigned color, int y) {
214	unsigned c = 0;
215	unsigned a;
216#ifdef COLOR_16_BIT
217	a = color & 0x1F;
218	c |= (a - (a * y) / 16) & 0x1F;
219
220#ifdef COLOR_5_6_5
221	a = color & 0x7C0;
222	c |= (a - (a * y) / 16) & 0x7C0;
223
224	a = color & 0xF800;
225	c |= (a - (a * y) / 16) & 0xF800;
226#else
227	a = color & 0x3E0;
228	c |= (a - (a * y) / 16) & 0x3E0;
229
230	a = color & 0x7C00;
231	c |= (a - (a * y) / 16) & 0x7C00;
232#endif
233#else
234	a = color & 0xFF;
235	c |= (a - (a * y) / 16) & 0xFF;
236
237	a = color & 0xFF00;
238	c |= (a - (a * y) / 16) & 0xFF00;
239
240	a = color & 0xFF0000;
241	c |= (a - (a * y) / 16) & 0xFF0000;
242#endif
243	return c;
244}
245
246static unsigned _mix(int weightA, unsigned colorA, int weightB, unsigned colorB) {
247	unsigned c = 0;
248	unsigned a, b;
249#ifdef COLOR_16_BIT
250#ifdef COLOR_5_6_5
251	a = colorA & 0xF81F;
252	b = colorB & 0xF81F;
253	a |= (colorA & 0x7C0) << 16;
254	b |= (colorB & 0x7C0) << 16;
255	c = ((a * weightA + b * weightB) / 16);
256	if (c & 0x08000000) {
257		c = (c & ~0x0FC00000) | 0x07C00000;
258	}
259	if (c & 0x0020) {
260		c = (c & ~0x003F) | 0x001F;
261	}
262	if (c & 0x10000) {
263		c = (c & ~0x1F800) | 0xF800;
264	}
265	c = (c & 0xF81F) | ((c >> 16) & 0x07C0);
266#else
267	a = colorA & 0x7C1F;
268	b = colorB & 0x7C1F;
269	a |= (colorA & 0x3E0) << 16;
270	b |= (colorB & 0x3E0) << 16;
271	c = ((a * weightA + b * weightB) / 16);
272	if (c & 0x04000000) {
273		c = (c & ~0x07E00000) | 0x03E00000;
274	}
275	if (c & 0x0020) {
276		c = (c & ~0x003F) | 0x001F;
277	}
278	if (c & 0x8000) {
279		c = (c & ~0xF800) | 0x7C00;
280	}
281	c = (c & 0x7C1F) | ((c >> 16) & 0x03E0);
282#endif
283#else
284	a = colorA & 0xFF;
285	b = colorB & 0xFF;
286	c |= ((a * weightA + b * weightB) / 16) & 0x1FF;
287	if (c & 0x00000100) {
288		c = 0x000000FF;
289	}
290
291	a = colorA & 0xFF00;
292	b = colorB & 0xFF00;
293	c |= ((a * weightA + b * weightB) / 16) & 0x1FF00;
294	if (c & 0x00010000) {
295		c = (c & 0x000000FF) | 0x0000FF00;
296	}
297
298	a = colorA & 0xFF0000;
299	b = colorB & 0xFF0000;
300	c |= ((a * weightA + b * weightB) / 16) & 0x1FF0000;
301	if (c & 0x01000000) {
302		c = (c & 0x0000FFFF) | 0x00FF0000;
303	}
304#endif
305	return c;
306}
307
308#endif