include/mgba/internal/gba/renderers/video-software.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 VIDEO_SOFTWARE_H
7#define VIDEO_SOFTWARE_H
8
9#include <mgba-util/common.h>
10
11CXX_GUARD_START
12
13#include <mgba/core/core.h>
14#include <mgba/gba/interface.h>
15#include <mgba/internal/gba/io.h>
16#include <mgba/internal/gba/video.h>
17
18struct GBAVideoSoftwareSprite {
19 struct GBAObj obj;
20 int y;
21 int endY;
22};
23
24struct GBAVideoSoftwareBackground {
25 unsigned index;
26 int enabled;
27 unsigned priority;
28 uint32_t charBase;
29 uint16_t control;
30 int mosaic;
31 int multipalette;
32 uint32_t screenBase;
33 int overflow;
34 int size;
35 int target1;
36 int target2;
37 uint16_t x;
38 uint16_t y;
39 int32_t refx;
40 int32_t refy;
41 int16_t dx;
42 int16_t dmx;
43 int16_t dy;
44 int16_t dmy;
45 int32_t sx;
46 int32_t sy;
47 int yCache;
48 uint16_t mapCache[64];
49 color_t* extPalette;
50 color_t* variantPalette;
51 int32_t offsetX;
52 int32_t offsetY;
53};
54
55enum BlendEffect {
56 BLEND_NONE = 0,
57 BLEND_ALPHA = 1,
58 BLEND_BRIGHTEN = 2,
59 BLEND_DARKEN = 3
60};
61
62enum {
63#ifdef COLOR_16_BIT
64#ifdef COLOR_5_6_5
65 GBA_COLOR_WHITE = 0xFFDF,
66#else
67 GBA_COLOR_WHITE = 0x7FFF,
68#endif
69#else
70 GBA_COLOR_WHITE = 0x00F8F8F8,
71#endif
72 OFFSET_PRIORITY = 30,
73 OFFSET_INDEX = 28,
74};
75
76#define FLAG_PRIORITY 0xC0000000
77#define FLAG_INDEX 0x30000000
78#define FLAG_IS_BACKGROUND 0x08000000
79#define FLAG_UNWRITTEN 0xFC000000
80#define FLAG_REBLEND 0x04000000
81#define FLAG_TARGET_1 0x02000000
82#define FLAG_TARGET_2 0x01000000
83#define FLAG_OBJWIN 0x01000000
84#define FLAG_ORDER_MASK 0xF8000000
85
86#define IS_WRITABLE(PIXEL) ((PIXEL) & 0xFE000000)
87
88struct WindowRegion {
89 int end;
90 int start;
91};
92
93DECL_BITFIELD(GBAWindowControl, uint8_t);
94DECL_BIT(GBAWindowControl, Bg0Enable, 0);
95DECL_BIT(GBAWindowControl, Bg1Enable, 1);
96DECL_BIT(GBAWindowControl, Bg2Enable, 2);
97DECL_BIT(GBAWindowControl, Bg3Enable, 3);
98DECL_BIT(GBAWindowControl, ObjEnable, 4);
99DECL_BIT(GBAWindowControl, BlendEnable, 5);
100
101DECL_BITFIELD(GBAMosaicControl, uint16_t);
102DECL_BITS(GBAMosaicControl, BgH, 0, 4);
103DECL_BITS(GBAMosaicControl, BgV, 4, 4);
104DECL_BITS(GBAMosaicControl, ObjH, 8, 4);
105DECL_BITS(GBAMosaicControl, ObjV, 12, 4);
106
107struct WindowControl {
108 GBAWindowControl packed;
109 int8_t priority;
110};
111
112#define MAX_WINDOW 5
113
114struct Window {
115 uint16_t endX;
116 struct WindowControl control;
117};
118
119struct GBAVideoSoftwareRenderer {
120 struct GBAVideoRenderer d;
121
122 color_t* outputBuffer;
123 int outputBufferStride;
124
125 uint32_t* temporaryBuffer;
126
127 uint32_t dispcnt;
128
129 uint32_t row[256];
130 uint32_t spriteLayer[256];
131 uint8_t alphaA[256];
132 uint8_t alphaB[256];
133 int32_t spriteCyclesRemaining;
134
135 // BLDCNT
136 unsigned target1Obj;
137 unsigned target1Bd;
138 unsigned target2Obj;
139 unsigned target2Bd;
140 bool blendDirty;
141 enum BlendEffect blendEffect;
142 color_t normalPalette[512];
143 color_t variantPalette[512];
144 color_t* objExtPalette;
145 color_t* objExtVariantPalette;
146
147 uint16_t blda;
148 uint16_t bldb;
149 uint16_t bldy;
150
151 GBAMosaicControl mosaic;
152
153 struct WindowN {
154 struct WindowRegion h;
155 struct WindowRegion v;
156 struct WindowControl control;
157 } winN[2];
158
159 struct WindowControl winout;
160 struct WindowControl objwin;
161
162 struct WindowControl currentWindow;
163
164 int nWindows;
165 struct Window windows[MAX_WINDOW];
166
167 struct GBAVideoSoftwareBackground bg[4];
168
169 int oamDirty;
170 int oamMax;
171 struct GBAVideoSoftwareSprite sprites[128];
172 int tileStride;
173 int bitmapStride;
174 bool combinedObjSort;
175 int16_t objOffsetX;
176 int16_t objOffsetY;
177
178 uint32_t scanlineDirty[5];
179 uint16_t nextIo[REG_SOUND1CNT_LO];
180 struct ScanlineCache {
181 uint16_t io[REG_SOUND1CNT_LO];
182 int32_t scale[2][2];
183 } cache[GBA_VIDEO_VERTICAL_PIXELS];
184 int nextY;
185
186 int start;
187 int end;
188 int masterEnd;
189 int masterHeight;
190 int masterScanlines;
191
192 int masterBright;
193 int masterBrightY;
194};
195
196void GBAVideoSoftwareRendererCreate(struct GBAVideoSoftwareRenderer* renderer);
197
198CXX_GUARD_START
199
200#endif