1 /* Copyright (c) 2013-2019 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_GL_H
7 #define VIDEO_GL_H
8 
9 #include <mgba-util/common.h>
10 
11 CXX_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/renderers/common.h>
17 #include <mgba/internal/gba/video.h>
18 
19 #if defined(BUILD_GLES2) || defined(BUILD_GLES3)
20 
21 #ifdef USE_EPOXY
22 #include <epoxy/gl.h>
23 #elif defined(BUILD_GL)
24 #ifdef __APPLE__
25 #include <OpenGL/gl3.h>
26 #else
27 #define GL_GLEXT_PROTOTYPES
28 #include <GL/gl.h>
29 #include <GL/glext.h>
30 #endif
31 #else
32 #include <GLES3/gl3.h>
33 #endif
34 
35 struct GBAVideoGLAffine {
36 	int16_t dx;
37 	int16_t dmx;
38 	int16_t dy;
39 	int16_t dmy;
40 	int32_t sx;
41 	int32_t sy;
42 };
43 
44 struct GBAVideoGLBackground {
45 	GLuint fbo;
46 	GLuint tex;
47 
48 	unsigned index;
49 	int enabled;
50 	unsigned priority;
51 	uint32_t charBase;
52 	int mosaic;
53 	int multipalette;
54 	uint32_t screenBase;
55 	int overflow;
56 	int size;
57 	int target1;
58 	int target2;
59 	uint16_t x;
60 	uint16_t y;
61 	int32_t refx;
62 	int32_t refy;
63 
64 	struct GBAVideoGLAffine affine;
65 
66 	GLint scanlineAffine[GBA_VIDEO_VERTICAL_PIXELS * 4];
67 	GLint scanlineOffset[GBA_VIDEO_VERTICAL_PIXELS];
68 };
69 
70 enum {
71 	GBA_GL_FBO_OBJ = 0,
72 	GBA_GL_FBO_BACKDROP,
73 	GBA_GL_FBO_WINDOW,
74 	GBA_GL_FBO_OUTPUT,
75 	GBA_GL_FBO_MAX
76 };
77 
78 enum {
79 	GBA_GL_TEX_OBJ_COLOR = 0,
80 	GBA_GL_TEX_OBJ_FLAGS,
81 	GBA_GL_TEX_OBJ_DEPTH,
82 	GBA_GL_TEX_BACKDROP,
83 	GBA_GL_TEX_WINDOW,
84 	GBA_GL_TEX_MAX
85 };
86 
87 enum {
88 	GBA_GL_VS_LOC = 0,
89 	GBA_GL_VS_MAXPOS,
90 
91 	GBA_GL_BG_VRAM = 2,
92 	GBA_GL_BG_PALETTE,
93 	GBA_GL_BG_SCREENBASE,
94 	GBA_GL_BG_CHARBASE,
95 	GBA_GL_BG_SIZE,
96 	GBA_GL_BG_OFFSET,
97 	GBA_GL_BG_TRANSFORM,
98 	GBA_GL_BG_RANGE,
99 	GBA_GL_BG_MOSAIC,
100 
101 	GBA_GL_OBJ_VRAM = 2,
102 	GBA_GL_OBJ_PALETTE,
103 	GBA_GL_OBJ_CHARBASE,
104 	GBA_GL_OBJ_STRIDE,
105 	GBA_GL_OBJ_LOCALPALETTE,
106 	GBA_GL_OBJ_INFLAGS,
107 	GBA_GL_OBJ_TRANSFORM,
108 	GBA_GL_OBJ_DIMS,
109 	GBA_GL_OBJ_OBJWIN,
110 	GBA_GL_OBJ_MOSAIC,
111 	GBA_GL_OBJ_CYCLES,
112 
113 	GBA_GL_WIN_DISPCNT = 2,
114 	GBA_GL_WIN_BLEND,
115 	GBA_GL_WIN_FLAGS,
116 	GBA_GL_WIN_WIN0,
117 	GBA_GL_WIN_WIN1,
118 
119 	GBA_GL_FINALIZE_SCALE = 2,
120 	GBA_GL_FINALIZE_LAYERS,
121 	GBA_GL_FINALIZE_FLAGS,
122 	GBA_GL_FINALIZE_WINDOW,
123 	GBA_GL_FINALIZE_PALETTE,
124 	GBA_GL_FINALIZE_BACKDROP,
125 
126 	GBA_GL_UNIFORM_MAX = 14
127 };
128 
129 struct GBAVideoGLShader {
130 	GLuint program;
131 	GLuint vao;
132 	GLuint uniforms[GBA_GL_UNIFORM_MAX];
133 };
134 
135 struct GBAVideoGLRenderer {
136 	struct GBAVideoRenderer d;
137 
138 	uint32_t* temporaryBuffer;
139 
140 	struct GBAVideoGLBackground bg[4];
141 
142 	int oamMax;
143 	bool oamDirty;
144 	struct GBAVideoRendererSprite sprites[128];
145 
146 	GLuint fbo[GBA_GL_FBO_MAX];
147 	GLuint layers[GBA_GL_TEX_MAX];
148 	GLuint vbo;
149 
150 	GLuint outputTex;
151 
152 	GLuint paletteTex;
153 	uint16_t shadowPalette[GBA_VIDEO_VERTICAL_PIXELS][512];
154 	int nextPalette;
155 	int paletteDirtyScanlines;
156 	bool paletteDirty;
157 
158 	GLuint vramTex;
159 	unsigned vramDirty;
160 
161 	uint16_t shadowRegs[0x30];
162 	uint64_t regsDirty;
163 
164 	struct GBAVideoGLShader bgShader[6];
165 	struct GBAVideoGLShader objShader[3];
166 	struct GBAVideoGLShader windowShader;
167 	struct GBAVideoGLShader finalizeShader;
168 
169 	GBARegisterDISPCNT dispcnt;
170 
171 	unsigned target1Obj;
172 	unsigned target1Bd;
173 	unsigned target2Obj;
174 	unsigned target2Bd;
175 	enum GBAVideoBlendEffect blendEffect;
176 	uint16_t blda;
177 	uint16_t bldb;
178 	uint16_t bldy;
179 
180 	GBAMosaicControl mosaic;
181 
182 	struct GBAVideoGLWindowN {
183 		struct GBAVideoWindowRegion h;
184 		struct GBAVideoWindowRegion v;
185 		GBAWindowControl control;
186 	} winN[2];
187 
188 	GLint winNHistory[2][GBA_VIDEO_VERTICAL_PIXELS * 4];
189 	GLint spriteCycles[GBA_VIDEO_VERTICAL_PIXELS];
190 
191 	GBAWindowControl winout;
192 	GBAWindowControl objwin;
193 
194 	int firstAffine;
195 	int firstY;
196 
197 	int scale;
198 };
199 
200 void GBAVideoGLRendererCreate(struct GBAVideoGLRenderer* renderer);
201 void GBAVideoGLRendererSetScale(struct GBAVideoGLRenderer* renderer, int scale);
202 
203 #endif
204 
205 CXX_GUARD_END
206 
207 #endif
208