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