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 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 UNUSED(objwinPalette); \
145 if (objwinSlowPath) { \
146 if (background->target1 && GBAWindowControlIsBlendEnable(renderer->objwin.packed) && \
147 (renderer->blendEffect == BLEND_BRIGHTEN || renderer->blendEffect == BLEND_DARKEN)) { \
148 objwinPalette = renderer->variantPalette; \
149 } \
150 switch (background->index) { \
151 case 0: \
152 objwinForceEnable = GBAWindowControlIsBg0Enable(renderer->objwin.packed) && \
153 GBAWindowControlIsBg0Enable(renderer->currentWindow.packed); \
154 objwinOnly = !GBAWindowControlIsBg0Enable(renderer->objwin.packed); \
155 break; \
156 case 1: \
157 objwinForceEnable = GBAWindowControlIsBg1Enable(renderer->objwin.packed) && \
158 GBAWindowControlIsBg1Enable(renderer->currentWindow.packed); \
159 objwinOnly = !GBAWindowControlIsBg1Enable(renderer->objwin.packed); \
160 break; \
161 case 2: \
162 objwinForceEnable = GBAWindowControlIsBg2Enable(renderer->objwin.packed) && \
163 GBAWindowControlIsBg2Enable(renderer->currentWindow.packed); \
164 objwinOnly = !GBAWindowControlIsBg2Enable(renderer->objwin.packed); \
165 break; \
166 case 3: \
167 objwinForceEnable = GBAWindowControlIsBg3Enable(renderer->objwin.packed) && \
168 GBAWindowControlIsBg3Enable(renderer->currentWindow.packed); \
169 objwinOnly = !GBAWindowControlIsBg3Enable(renderer->objwin.packed); \
170 break; \
171 } \
172 }
173
174#define BACKGROUND_BITMAP_INIT \
175 int32_t x = background->sx + (renderer->start - 1) * background->dx; \
176 int32_t y = background->sy + (renderer->start - 1) * background->dy; \
177 int mosaicH = 0; \
178 int mosaicWait = 0; \
179 if (background->mosaic) { \
180 int mosaicV = GBAMosaicControlGetBgV(renderer->mosaic) + 1; \
181 y -= (inY % mosaicV) * background->dmy; \
182 x -= (inY % mosaicV) * background->dmx; \
183 mosaicH = GBAMosaicControlGetBgH(renderer->mosaic); \
184 mosaicWait = renderer->start % (mosaicH + 1); \
185 } \
186 int32_t localX; \
187 int32_t localY; \
188 \
189 uint32_t flags = (background->priority << OFFSET_PRIORITY) | (background->index << OFFSET_INDEX) | FLAG_IS_BACKGROUND; \
190 flags |= FLAG_TARGET_2 * background->target2; \
191 int objwinFlags = FLAG_TARGET_1 * (background->target1 && renderer->blendEffect == BLEND_ALPHA && \
192 GBAWindowControlIsBlendEnable(renderer->objwin.packed)); \
193 objwinFlags |= flags; \
194 flags |= FLAG_TARGET_1 * (background->target1 && renderer->blendEffect == BLEND_ALPHA && \
195 GBAWindowControlIsBlendEnable(renderer->currentWindow.packed)); \
196 if (renderer->blendEffect == BLEND_ALPHA && renderer->blda == 0x10 && renderer->bldb == 0) { \
197 flags &= ~(FLAG_TARGET_1 | FLAG_TARGET_2); \
198 objwinFlags &= ~(FLAG_TARGET_1 | FLAG_TARGET_2); \
199 } \
200 int variant = background->target1 && GBAWindowControlIsBlendEnable(renderer->currentWindow.packed) && \
201 (renderer->blendEffect == BLEND_BRIGHTEN || renderer->blendEffect == BLEND_DARKEN); \
202 color_t* palette = renderer->normalPalette; \
203 if (variant) { \
204 palette = renderer->variantPalette; \
205 } \
206 UNUSED(palette); \
207 PREPARE_OBJWIN;
208
209#define BACKGROUND_BITMAP_ITERATE(W, H) \
210 x += background->dx; \
211 y += background->dy; \
212 \
213 if (x < 0 || y < 0 || (x >> 8) >= W || (y >> 8) >= H) { \
214 continue; \
215 } else { \
216 localX = x; \
217 localY = y; \
218 }
219
220static inline unsigned _brighten(unsigned color, int y) {
221 unsigned c = 0;
222 unsigned a;
223#ifdef COLOR_16_BIT
224 a = color & 0x1F;
225 c |= (a + ((0x1F - a) * y) / 16) & 0x1F;
226
227#ifdef COLOR_5_6_5
228 a = color & 0x7C0;
229 c |= (a + ((0x7C0 - a) * y) / 16) & 0x7C0;
230
231 a = color & 0xF800;
232 c |= (a + ((0xF800 - a) * y) / 16) & 0xF800;
233#else
234 a = color & 0x3E0;
235 c |= (a + ((0x3E0 - a) * y) / 16) & 0x3E0;
236
237 a = color & 0x7C00;
238 c |= (a + ((0x7C00 - a) * y) / 16) & 0x7C00;
239#endif
240#else
241 a = color & 0xFF;
242 c |= (a + ((0xFF - a) * y) / 16) & 0xFF;
243
244 a = color & 0xFF00;
245 c |= (a + ((0xFF00 - a) * y) / 16) & 0xFF00;
246
247 a = color & 0xFF0000;
248 c |= (a + ((0xFF0000 - a) * y) / 16) & 0xFF0000;
249#endif
250 return c;
251}
252
253static inline unsigned _darken(unsigned color, int y) {
254 unsigned c = 0;
255 unsigned a;
256#ifdef COLOR_16_BIT
257 a = color & 0x1F;
258 c |= (a - (a * y) / 16) & 0x1F;
259
260#ifdef COLOR_5_6_5
261 a = color & 0x7C0;
262 c |= (a - (a * y) / 16) & 0x7C0;
263
264 a = color & 0xF800;
265 c |= (a - (a * y) / 16) & 0xF800;
266#else
267 a = color & 0x3E0;
268 c |= (a - (a * y) / 16) & 0x3E0;
269
270 a = color & 0x7C00;
271 c |= (a - (a * y) / 16) & 0x7C00;
272#endif
273#else
274 a = color & 0xFF;
275 c |= (a - (a * y) / 16) & 0xFF;
276
277 a = color & 0xFF00;
278 c |= (a - (a * y) / 16) & 0xFF00;
279
280 a = color & 0xFF0000;
281 c |= (a - (a * y) / 16) & 0xFF0000;
282#endif
283 return c;
284}
285
286static unsigned _mix(int weightA, unsigned colorA, int weightB, unsigned colorB) {
287 unsigned c = 0;
288 unsigned a, b;
289#ifdef COLOR_16_BIT
290#ifdef COLOR_5_6_5
291 a = colorA & 0xF81F;
292 b = colorB & 0xF81F;
293 a |= (colorA & 0x7C0) << 16;
294 b |= (colorB & 0x7C0) << 16;
295 c = ((a * weightA + b * weightB) / 16);
296 if (c & 0x08000000) {
297 c = (c & ~0x0FC00000) | 0x07C00000;
298 }
299 if (c & 0x0020) {
300 c = (c & ~0x003F) | 0x001F;
301 }
302 if (c & 0x10000) {
303 c = (c & ~0x1F800) | 0xF800;
304 }
305 c = (c & 0xF81F) | ((c >> 16) & 0x07C0);
306#else
307 a = colorA & 0x7C1F;
308 b = colorB & 0x7C1F;
309 a |= (colorA & 0x3E0) << 16;
310 b |= (colorB & 0x3E0) << 16;
311 c = ((a * weightA + b * weightB) / 16);
312 if (c & 0x04000000) {
313 c = (c & ~0x07E00000) | 0x03E00000;
314 }
315 if (c & 0x0020) {
316 c = (c & ~0x003F) | 0x001F;
317 }
318 if (c & 0x8000) {
319 c = (c & ~0xF800) | 0x7C00;
320 }
321 c = (c & 0x7C1F) | ((c >> 16) & 0x03E0);
322#endif
323#else
324 a = colorA & 0xFF;
325 b = colorB & 0xFF;
326 c |= ((a * weightA + b * weightB) / 16) & 0x1FF;
327 if (c & 0x00000100) {
328 c = 0x000000FF;
329 }
330
331 a = colorA & 0xFF00;
332 b = colorB & 0xFF00;
333 c |= ((a * weightA + b * weightB) / 16) & 0x1FF00;
334 if (c & 0x00010000) {
335 c = (c & 0x000000FF) | 0x0000FF00;
336 }
337
338 a = colorA & 0xFF0000;
339 b = colorB & 0xFF0000;
340 c |= ((a * weightA + b * weightB) / 16) & 0x1FF0000;
341 if (c & 0x01000000) {
342 c = (c & 0x0000FFFF) | 0x00FF0000;
343 }
344#endif
345 return c;
346}
347
348#endif