src/gba/renderers/gl.c (view raw)
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#include <mgba/internal/gba/renderers/gl.h>
7
8#if defined(BUILD_GLES2) || defined(BUILD_GLES3)
9
10#include <mgba/core/cache-set.h>
11#include <mgba/internal/arm/macros.h>
12#include <mgba/internal/gba/io.h>
13#include <mgba/internal/gba/renderers/cache-set.h>
14#include <mgba-util/memory.h>
15
16static void GBAVideoGLRendererInit(struct GBAVideoRenderer* renderer);
17static void GBAVideoGLRendererDeinit(struct GBAVideoRenderer* renderer);
18static void GBAVideoGLRendererReset(struct GBAVideoRenderer* renderer);
19static void GBAVideoGLRendererWriteVRAM(struct GBAVideoRenderer* renderer, uint32_t address);
20static void GBAVideoGLRendererWriteOAM(struct GBAVideoRenderer* renderer, uint32_t oam);
21static void GBAVideoGLRendererWritePalette(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value);
22static uint16_t GBAVideoGLRendererWriteVideoRegister(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value);
23static void GBAVideoGLRendererDrawScanline(struct GBAVideoRenderer* renderer, int y);
24static void GBAVideoGLRendererFinishFrame(struct GBAVideoRenderer* renderer);
25static void GBAVideoGLRendererGetPixels(struct GBAVideoRenderer* renderer, size_t* stride, const void** pixels);
26static void GBAVideoGLRendererPutPixels(struct GBAVideoRenderer* renderer, size_t stride, const void* pixels);
27
28static void GBAVideoGLRendererUpdateDISPCNT(struct GBAVideoGLRenderer* renderer);
29static void GBAVideoGLRendererWriteBGCNT(struct GBAVideoGLBackground* bg, uint16_t value);
30static void GBAVideoGLRendererWriteBGX_LO(struct GBAVideoGLBackground* bg, uint16_t value);
31static void GBAVideoGLRendererWriteBGX_HI(struct GBAVideoGLBackground* bg, uint16_t value);
32static void GBAVideoGLRendererWriteBGY_LO(struct GBAVideoGLBackground* bg, uint16_t value);
33static void GBAVideoGLRendererWriteBGY_HI(struct GBAVideoGLBackground* bg, uint16_t value);
34static void GBAVideoGLRendererWriteBLDCNT(struct GBAVideoGLRenderer* renderer, uint16_t value);
35
36static void GBAVideoGLRendererDrawSprite(struct GBAVideoGLRenderer* renderer, struct GBAObj* sprite, int y, int spriteY);
37static void GBAVideoGLRendererDrawBackgroundMode0(struct GBAVideoGLRenderer* renderer, struct GBAVideoGLBackground* background, int y);
38static void GBAVideoGLRendererDrawBackgroundMode2(struct GBAVideoGLRenderer* renderer, struct GBAVideoGLBackground* background, int y);
39static void GBAVideoGLRendererDrawBackgroundMode3(struct GBAVideoGLRenderer* renderer, struct GBAVideoGLBackground* background, int y);
40static void GBAVideoGLRendererDrawBackgroundMode4(struct GBAVideoGLRenderer* renderer, struct GBAVideoGLBackground* background, int y);
41static void GBAVideoGLRendererDrawBackgroundMode5(struct GBAVideoGLRenderer* renderer, struct GBAVideoGLBackground* background, int y);
42static void GBAVideoGLRendererDrawWindow(struct GBAVideoGLRenderer* renderer, int y);
43
44static void _cleanRegister(struct GBAVideoGLRenderer* renderer, int address, uint16_t value);
45static void _drawScanlines(struct GBAVideoGLRenderer* renderer, int lastY);
46static void _finalizeLayers(struct GBAVideoGLRenderer* renderer);
47
48#define TEST_LAYER_ENABLED(X) !glRenderer->d.disableBG[X] && glRenderer->bg[X].enabled == 4
49
50struct GBAVideoGLUniform {
51 const char* name;
52 int type;
53};
54
55#define PALETTE_ENTRY "#define PALETTE_ENTRY(x) (vec3((ivec3(0x1F, 0x3E0, 0x7C00) & (x)) >> ivec3(0, 5, 10)) / 31.)\n"
56
57static const GLchar* const _gles3Header =
58 "#version 300 es\n"
59 "#define OUT(n) layout(location = n)\n"
60 PALETTE_ENTRY
61 "precision highp float;\n"
62 "precision highp int;\n"
63 "precision highp sampler2D;\n"
64 "precision highp isampler2D;\n";
65
66static const GLchar* const _gl3Header =
67 "#version 130\n"
68 "#define OUT(n)\n"
69 PALETTE_ENTRY
70 "precision highp float;\n";
71
72static const char* const _vertexShader =
73 "in vec2 position;\n"
74 "uniform ivec2 loc;\n"
75 "uniform ivec2 maxPos;\n"
76 "out vec2 texCoord;\n"
77
78 "void main() {\n"
79 " vec2 local = vec2(position.x, float(position.y * float(loc.x) + float(loc.y)) / float(maxPos.y));\n"
80 " gl_Position = vec4((local * 2. - 1.) * vec2(sign(maxPos)), 0., 1.);\n"
81 " texCoord = local * vec2(abs(maxPos));\n"
82 "}";
83
84static const char* const _renderTile16 =
85 "vec4 renderTile(int tile, int paletteId, ivec2 localCoord) {\n"
86 " int address = charBase + tile * 16 + (localCoord.x >> 2) + (localCoord.y << 1);\n"
87 " vec4 halfrow = texelFetch(vram, ivec2(address & 255, address >> 8), 0);\n"
88 " int entry = int(halfrow[3 - (localCoord.x & 3)] * 15.9);\n"
89 " if (entry == 0) {\n"
90 " discard;\n"
91 " }\n"
92 " int paletteEntry = palette[paletteId * 16 + entry];\n"
93 " vec4 color = vec4(PALETTE_ENTRY(paletteEntry), 1.);\n"
94 " return color;\n"
95 "}";
96
97static const char* const _renderTile256 =
98 "vec4 renderTile(int tile, int paletteId, ivec2 localCoord) {\n"
99 " int address = charBase + tile * 32 + (localCoord.x >> 1) + (localCoord.y << 2);\n"
100 " vec4 halfrow = texelFetch(vram, ivec2(address & 255, address >> 8), 0);\n"
101 " int entry = int(halfrow[3 - 2 * (localCoord.x & 1)] * 15.9);\n"
102 " int pal2 = int(halfrow[2 - 2 * (localCoord.x & 1)] * 15.9);\n"
103 " if ((pal2 | entry) == 0) {\n"
104 " discard;\n"
105 " }\n"
106 " int paletteEntry = palette[pal2 * 16 + entry];\n"
107 " vec4 color = vec4(PALETTE_ENTRY(paletteEntry), 1.);\n"
108 " return color;\n"
109 "}";
110
111static const struct GBAVideoGLUniform _uniformsMode0[] = {
112 { "loc", GBA_GL_VS_LOC, },
113 { "maxPos", GBA_GL_VS_MAXPOS, },
114 { "vram", GBA_GL_BG_VRAM, },
115 { "palette", GBA_GL_BG_PALETTE, },
116 { "screenBase", GBA_GL_BG_SCREENBASE, },
117 { "charBase", GBA_GL_BG_CHARBASE, },
118 { "size", GBA_GL_BG_SIZE, },
119 { "offset", GBA_GL_BG_OFFSET, },
120 { "inflags", GBA_GL_BG_INFLAGS, },
121 { "mosaic", GBA_GL_BG_MOSAIC, },
122 { 0 }
123};
124
125static const char* const _renderMode0 =
126 "in vec2 texCoord;\n"
127 "uniform sampler2D vram;\n"
128 "uniform int palette[256];\n"
129 "uniform int screenBase;\n"
130 "uniform int charBase;\n"
131 "uniform int size;\n"
132 "uniform ivec2 offset;\n"
133 "uniform ivec4 inflags;\n"
134 "uniform ivec2 mosaic;\n"
135 "OUT(0) out vec4 color;\n"
136 "OUT(1) out ivec4 flags;\n"
137
138 "vec4 renderTile(int tile, int paletteId, ivec2 localCoord);\n"
139
140 "void main() {\n"
141 " ivec2 coord = ivec2(texCoord);\n"
142 " if (mosaic.x > 1) {\n"
143 " coord.x -= coord.x % mosaic.x;\n"
144 " }\n"
145 " if (mosaic.y > 1) {\n"
146 " coord.y -= coord.y % mosaic.y;\n"
147 " }\n"
148 " coord += offset;\n"
149 " if ((size & 1) == 1) {\n"
150 " coord.y += coord.x & 256;\n"
151 " }\n"
152 " coord.x &= 255;\n"
153 " int mapAddress = screenBase + (coord.x >> 3) + (coord.y >> 3) * 32;\n"
154 " vec4 map = texelFetch(vram, ivec2(mapAddress & 255, mapAddress >> 8), 0);\n"
155 " int tileFlags = int(map.g * 15.9);\n"
156 " if ((tileFlags & 4) == 4) {\n"
157 " coord.x ^= 7;\n"
158 " }\n"
159 " if ((tileFlags & 8) == 8) {\n"
160 " coord.y ^= 7;\n"
161 " }\n"
162 " int tile = int(map.a * 15.9) + int(map.b * 15.9) * 16 + (tileFlags & 0x3) * 256;\n"
163 " color = renderTile(tile, int(map.r * 15.9), coord & 7);\n"
164 " flags = inflags;\n"
165 "}";
166
167static const char* const _fetchTileOverflow =
168 "vec4 fetchTile(ivec2 coord) {\n"
169 " int sizeAdjusted = (0x8000 << size) - 1;\n"
170 " coord &= sizeAdjusted;\n"
171 " return renderTile(coord);\n"
172 "}";
173
174static const char* const _fetchTileNoOverflow =
175 "vec4 fetchTile(ivec2 coord) {\n"
176 " int sizeAdjusted = (0x8000 << size) - 1;\n"
177 " ivec2 outerCoord = coord & ~sizeAdjusted;\n"
178 " if ((outerCoord.x | outerCoord.y) != 0) {\n"
179 " discard;\n"
180 " }\n"
181 " return renderTile(coord);\n"
182 "}";
183
184static const struct GBAVideoGLUniform _uniformsMode2[] = {
185 { "loc", GBA_GL_VS_LOC, },
186 { "maxPos", GBA_GL_VS_MAXPOS, },
187 { "vram", GBA_GL_BG_VRAM, },
188 { "palette", GBA_GL_BG_PALETTE, },
189 { "screenBase", GBA_GL_BG_SCREENBASE, },
190 { "charBase", GBA_GL_BG_CHARBASE, },
191 { "size", GBA_GL_BG_SIZE, },
192 { "inflags", GBA_GL_BG_INFLAGS, },
193 { "offset", GBA_GL_BG_OFFSET, },
194 { "transform", GBA_GL_BG_TRANSFORM, },
195 { "range", GBA_GL_BG_RANGE, },
196 { "mosaic", GBA_GL_BG_MOSAIC, },
197 { 0 }
198};
199
200static const char* const _interpolate =
201 "vec2 interpolate(ivec2 arr[4], float x) {\n"
202 " float x1m = 1. - x;\n"
203 " return x1m * x1m * x1m * vec2(arr[0]) +"
204 " 3. * x1m * x1m * x * vec2(arr[1]) +"
205 " 3. * x1m * x * x * vec2(arr[2]) +"
206 " x * x * x * vec2(arr[3]);\n"
207 "}\n"
208
209 "void loadAffine(int y, out ivec2 mat[4], out ivec2 aff[4]) {\n"
210 " int start = max(range.x, y - 3);\n"
211 " ivec4 splitOffset[4];\n"
212
213 " mat[0] = texelFetch(transform, ivec2(0, start), 0).xz;\n"
214 " mat[1] = texelFetch(transform, ivec2(0, start + 1), 0).xz;\n"
215 " mat[2] = texelFetch(transform, ivec2(0, start + 2), 0).xz;\n"
216 " mat[3] = texelFetch(transform, ivec2(0, start + 3), 0).xz;\n"
217
218 " splitOffset[0] = texelFetch(transform, ivec2(1, start + 0), 0);\n"
219 " splitOffset[1] = texelFetch(transform, ivec2(1, start + 1), 0);\n"
220 " splitOffset[2] = texelFetch(transform, ivec2(1, start + 2), 0);\n"
221 " splitOffset[3] = texelFetch(transform, ivec2(1, start + 3), 0);\n"
222
223 " aff[0] = (splitOffset[0].xz & 0xFFFF) + (splitOffset[0].yw << 16);\n"
224 " aff[1] = (splitOffset[1].xz & 0xFFFF) + (splitOffset[1].yw << 16);\n"
225 " aff[2] = (splitOffset[2].xz & 0xFFFF) + (splitOffset[2].yw << 16);\n"
226 " aff[3] = (splitOffset[3].xz & 0xFFFF) + (splitOffset[3].yw << 16);\n"
227
228 " if (y - 3 < range.x) {\n"
229 " ivec2 tempMat[3];\n"
230 " ivec2 tempAff[3];\n"
231 " tempMat[0] = ivec2(interpolate(mat, -0.75));\n"
232 " tempMat[1] = ivec2(interpolate(mat, -0.5));\n"
233 " tempMat[2] = ivec2(interpolate(mat, -0.25));\n"
234 " tempAff[0] = ivec2(interpolate(aff, -0.75));\n"
235 " tempAff[1] = ivec2(interpolate(aff, -0.5));\n"
236 " tempAff[2] = ivec2(interpolate(aff, -0.25));\n"
237 " if (range.x == y) {\n"
238 " mat[3] = mat[0];\n"
239 " mat[2] = tempMat[2];\n"
240 " mat[1] = tempMat[1];\n"
241 " mat[0] = tempMat[0];\n"
242 " aff[3] = aff[0];\n"
243 " aff[2] = tempAff[2];\n"
244 " aff[1] = tempAff[1];\n"
245 " aff[0] = tempAff[0];\n"
246 " } else if (range.x == y - 1) {\n"
247 " mat[3] = mat[1];\n"
248 " mat[2] = mat[0];\n"
249 " mat[1] = tempMat[2];\n"
250 " mat[0] = tempMat[1];\n"
251 " aff[3] = aff[1];\n"
252 " aff[2] = aff[0];\n"
253 " aff[1] = tempAff[2];\n"
254 " aff[0] = tempAff[1];\n"
255 " } else if (range.x == y - 2) {\n"
256 " mat[3] = mat[2];\n"
257 " mat[2] = mat[1];\n"
258 " mat[1] = mat[0];\n"
259 " mat[0] = tempMat[0];\n"
260 " aff[3] = aff[2];\n"
261 " aff[2] = aff[1];\n"
262 " aff[1] = aff[0];\n"
263 " aff[0] = tempAff[0];\n"
264 " }\n"
265 " }\n"
266 "}\n";
267
268static const char* const _renderMode2 =
269 "in vec2 texCoord;\n"
270 "uniform sampler2D vram;\n"
271 "uniform int palette[256];\n"
272 "uniform int screenBase;\n"
273 "uniform int charBase;\n"
274 "uniform int size;\n"
275 "uniform ivec4 inflags;\n"
276 "uniform isampler2D transform;\n"
277 "uniform ivec2 range;\n"
278 "uniform ivec2 mosaic;\n"
279 "OUT(0) out vec4 color;\n"
280 "OUT(1) out ivec4 flags;\n"
281
282 "vec4 fetchTile(ivec2 coord);\n"
283 "vec2 interpolate(ivec2 arr[4], float x);\n"
284 "void loadAffine(int y, out ivec2 mat[4], out ivec2 aff[4]);\n"
285
286 "vec4 renderTile(ivec2 coord) {\n"
287 " int map = (coord.x >> 11) + (((coord.y >> 7) & 0x7F0) << size);\n"
288 " int mapAddress = screenBase + (map >> 1);\n"
289 " vec4 twomaps = texelFetch(vram, ivec2(mapAddress & 255, mapAddress >> 8), 0);\n"
290 " int tile = int(twomaps[3 - 2 * (map & 1)] * 15.9) + int(twomaps[2 - 2 * (map & 1)] * 15.9) * 16;\n"
291 " int address = charBase + tile * 32 + ((coord.x >> 9) & 3) + ((coord.y >> 6) & 0x1C);\n"
292 " vec4 halfrow = texelFetch(vram, ivec2(address & 255, address >> 8), 0);\n"
293 " int entry = int(halfrow[3 - ((coord.x >> 7) & 2)] * 15.9);\n"
294 " int pal2 = int(halfrow[2 - ((coord.x >> 7) & 2)] * 15.9);\n"
295 " if ((pal2 | entry) == 0) {\n"
296 " discard;\n"
297 " }\n"
298 " int paletteEntry = palette[pal2 * 16 + entry];\n"
299 " vec4 color = vec4(PALETTE_ENTRY(paletteEntry), 1.);\n"
300 " return color;\n"
301 "}\n"
302
303 "void main() {\n"
304 " ivec2 mat[4];\n"
305 " ivec2 offset[4];\n"
306 " vec2 incoord = texCoord;\n"
307 " if (mosaic.x > 1) {\n"
308 " incoord.x = float(int(incoord.x) % mosaic.x);\n"
309 " }\n"
310 " if (mosaic.y > 1) {\n"
311 " incoord.y = float(int(incoord.y) % mosaic.y);\n"
312 " }\n"
313 " loadAffine(int(incoord.y), mat, offset);\n"
314 " float y = fract(incoord.y);\n"
315 " float lin = 0.75 + y * 0.25;\n"
316 " vec2 mixedTransform = interpolate(mat, lin);\n"
317 " vec2 mixedOffset = interpolate(offset, lin);\n"
318 " color = fetchTile(ivec2(mixedTransform * incoord.x + mixedOffset));\n"
319 " flags = inflags;\n"
320 "}";
321
322static const struct GBAVideoGLUniform _uniformsMode35[] = {
323 { "loc", GBA_GL_VS_LOC, },
324 { "maxPos", GBA_GL_VS_MAXPOS, },
325 { "vram", GBA_GL_BG_VRAM, },
326 { "charBase", GBA_GL_BG_CHARBASE, },
327 { "size", GBA_GL_BG_SIZE, },
328 { "inflags", GBA_GL_BG_INFLAGS, },
329 { "offset", GBA_GL_BG_OFFSET, },
330 { "transform", GBA_GL_BG_TRANSFORM, },
331 { "range", GBA_GL_BG_RANGE, },
332 { "mosaic", GBA_GL_BG_MOSAIC, },
333 { 0 }
334};
335
336static const char* const _renderMode35 =
337 "in vec2 texCoord;\n"
338 "uniform sampler2D vram;\n"
339 "uniform int charBase;\n"
340 "uniform ivec2 size;\n"
341 "uniform ivec4 inflags;\n"
342 "uniform isampler2D transform;\n"
343 "uniform ivec2 range;\n"
344 "uniform ivec2 mosaic;\n"
345 "OUT(0) out vec4 color;\n"
346 "OUT(1) out ivec4 flags;\n"
347
348 "vec2 interpolate(ivec2 arr[4], float x);\n"
349 "void loadAffine(int y, out ivec2 mat[4], out ivec2 aff[4]);\n"
350
351 "void main() {\n"
352 " ivec2 mat[4];\n"
353 " ivec2 offset[4];\n"
354 " vec2 incoord = texCoord;\n"
355 " if (mosaic.x > 1) {\n"
356 " incoord.x = float(int(incoord.x) % mosaic.x);\n"
357 " }\n"
358 " if (mosaic.y > 1) {\n"
359 " incoord.y = float(int(incoord.y) % mosaic.y);\n"
360 " }\n"
361 " loadAffine(int(incoord.y), mat, offset);\n"
362 " float y = fract(incoord.y);\n"
363 " float lin = 0.75 + y * 0.25;\n"
364 " vec2 mixedTransform = interpolate(mat, lin);\n"
365 " vec2 mixedOffset = interpolate(offset, lin);\n"
366 " ivec2 coord = ivec2(mixedTransform * incoord.x + mixedOffset);\n"
367 " if (coord.x < 0 || coord.x >= (size.x << 8)) {\n"
368 " discard;\n"
369 " }\n"
370 " if (coord.y < 0 || coord.y >= (size.y << 8)) {\n"
371 " discard;\n"
372 " }\n"
373 " int address = charBase + (coord.x >> 8) + (coord.y >> 8) * size.x;\n"
374 " ivec4 entry = ivec4(texelFetch(vram, ivec2(address & 255, address >> 8), 0) * 15.9);\n"
375 " int sixteen = (entry.x << 12) | (entry.y << 8) | (entry.z << 4) | entry.w;\n"
376 " color = vec4(float(sixteen & 0x1F) / 31., float((sixteen >> 5) & 0x1F) / 31., float((sixteen >> 10) & 0x1F) / 31., 1.);\n"
377 " flags = inflags;\n"
378 "}";
379
380static const struct GBAVideoGLUniform _uniformsMode4[] = {
381 { "loc", GBA_GL_VS_LOC, },
382 { "maxPos", GBA_GL_VS_MAXPOS, },
383 { "vram", GBA_GL_BG_VRAM, },
384 { "palette", GBA_GL_BG_PALETTE, },
385 { "charBase", GBA_GL_BG_CHARBASE, },
386 { "size", GBA_GL_BG_SIZE, },
387 { "inflags", GBA_GL_BG_INFLAGS, },
388 { "offset", GBA_GL_BG_OFFSET, },
389 { "transform", GBA_GL_BG_TRANSFORM, },
390 { "range", GBA_GL_BG_RANGE, },
391 { "mosaic", GBA_GL_BG_MOSAIC, },
392 { 0 }
393};
394
395static const char* const _renderMode4 =
396 "in vec2 texCoord;\n"
397 "uniform sampler2D vram;\n"
398 "uniform int palette[256];\n"
399 "uniform int charBase;\n"
400 "uniform ivec2 size;\n"
401 "uniform ivec4 inflags;\n"
402 "uniform isampler2D transform;\n"
403 "uniform ivec2 range;\n"
404 "uniform ivec2 mosaic;\n"
405 "OUT(0) out vec4 color;\n"
406 "OUT(1) out ivec4 flags;\n"
407
408 "vec2 interpolate(ivec2 arr[4], float x);\n"
409 "void loadAffine(int y, out ivec2 mat[4], out ivec2 aff[4]);\n"
410
411 "void main() {\n"
412 " ivec2 mat[4];\n"
413 " ivec2 offset[4];\n"
414 " vec2 incoord = texCoord;\n"
415 " if (mosaic.x > 1) {\n"
416 " incoord.x = float(int(incoord.x) % mosaic.x);\n"
417 " }\n"
418 " if (mosaic.y > 1) {\n"
419 " incoord.y = float(int(incoord.y) % mosaic.y);\n"
420 " }\n"
421 " loadAffine(int(incoord.y), mat, offset);\n"
422 " float y = fract(incoord.y);\n"
423 " float lin = 0.75 + y * 0.25;\n"
424 " vec2 mixedTransform = interpolate(mat, lin);\n"
425 " vec2 mixedOffset = interpolate(offset, lin);\n"
426 " ivec2 coord = ivec2(mixedTransform * incoord.x + mixedOffset);\n"
427 " if (coord.x < 0 || coord.x >= (size.x << 8)) {\n"
428 " discard;\n"
429 " }\n"
430 " if (coord.y < 0 || coord.y >= (size.y << 8)) {\n"
431 " discard;\n"
432 " }\n"
433 " int address = charBase + (coord.x >> 8) + (coord.y >> 8) * size.x;\n"
434 " vec4 twoEntries = texelFetch(vram, ivec2((address >> 1) & 255, address >> 9), 0);\n"
435 " ivec2 entry = ivec2(twoEntries[3 - 2 * (address & 1)] * 15.9, twoEntries[2 - 2 * (address & 1)] * 15.9);\n"
436 " int paletteEntry = palette[entry.y * 16 + entry.x];\n"
437 " color = vec4(PALETTE_ENTRY(paletteEntry), 1.);\n"
438 " flags = inflags;\n"
439 "}";
440
441static const struct GBAVideoGLUniform _uniformsObj[] = {
442 { "loc", GBA_GL_VS_LOC, },
443 { "maxPos", GBA_GL_VS_MAXPOS, },
444 { "vram", GBA_GL_OBJ_VRAM, },
445 { "palette", GBA_GL_OBJ_PALETTE, },
446 { "charBase", GBA_GL_OBJ_CHARBASE, },
447 { "stride", GBA_GL_OBJ_STRIDE, },
448 { "localPalette", GBA_GL_OBJ_LOCALPALETTE, },
449 { "inflags", GBA_GL_OBJ_INFLAGS, },
450 { "transform", GBA_GL_OBJ_TRANSFORM, },
451 { "dims", GBA_GL_OBJ_DIMS, },
452 { "objwin", GBA_GL_OBJ_OBJWIN, },
453 { "mosaic", GBA_GL_OBJ_MOSAIC, },
454 { 0 }
455};
456
457static const char* const _renderObj =
458 "in vec2 texCoord;\n"
459 "uniform sampler2D vram;\n"
460 "uniform int palette[256];\n"
461 "uniform int charBase;\n"
462 "uniform int stride;\n"
463 "uniform int localPalette;\n"
464 "uniform ivec4 inflags;\n"
465 "uniform mat2x2 transform;\n"
466 "uniform ivec4 dims;\n"
467 "uniform ivec4 objwin;\n"
468 "uniform ivec4 mosaic;\n"
469 "OUT(0) out vec4 color;\n"
470 "OUT(1) out ivec4 flags;\n"
471 "OUT(2) out ivec3 window;\n"
472
473 "vec4 renderTile(int tile, int paletteId, ivec2 localCoord);\n"
474
475 "void main() {\n"
476 " vec2 incoord = texCoord;\n"
477 " if (mosaic.x > 1) {\n"
478 " int x = int(incoord.x);\n"
479 " incoord.x = float(clamp(x - (mosaic.z + x) % mosaic.x, 0, dims.z - 1));\n"
480 " } else if (mosaic.x < -1) {\n"
481 " int x = dims.z - int(incoord.x) - 1;\n"
482 " incoord.x = float(clamp(dims.z - x + (mosaic.z + x) % -mosaic.x - 1, 0, dims.z - 1));\n"
483 " }\n"
484 " if (mosaic.y > 1) {\n"
485 " int y = int(incoord.y);\n"
486 " incoord.y = float(clamp(y - (mosaic.w + y) % mosaic.y, 0, dims.w - 1));\n"
487 " }\n"
488 " ivec2 coord = ivec2(transform * (incoord - vec2(dims.zw) / 2.) + vec2(dims.xy) / 2.);\n"
489 " if ((coord & ~(dims.xy - 1)) != ivec2(0, 0)) {\n"
490 " discard;\n"
491 " }\n"
492 " vec4 pix = renderTile((coord.x >> 3) + (coord.y >> 3) * stride, localPalette, coord & 7);\n"
493 " color = pix;\n"
494 " flags = inflags;\n"
495 " gl_FragDepth = float(flags.x) / 16.;\n"
496 " window = objwin.yzw;\n"
497 "}";
498
499static const struct GBAVideoGLUniform _uniformsWindow[] = {
500 { "loc", GBA_GL_VS_LOC, },
501 { "maxPos", GBA_GL_VS_MAXPOS, },
502 { "dispcnt", GBA_GL_WIN_DISPCNT, },
503 { "blend", GBA_GL_WIN_BLEND, },
504 { "flags", GBA_GL_WIN_FLAGS, },
505 { "win0", GBA_GL_WIN_WIN0, },
506 { "win1", GBA_GL_WIN_WIN1, },
507 { 0 }
508};
509
510static const char* const _renderWindow =
511 "in vec2 texCoord;\n"
512 "uniform int dispcnt;\n"
513 "uniform ivec2 blend;\n"
514 "uniform ivec3 flags;\n"
515 "uniform ivec4 win0[160];\n"
516 "uniform ivec4 win1[160];\n"
517 "OUT(0) out ivec3 window;\n"
518
519 "void crop(vec4 windowParams, int flags, inout ivec3 windowFlags) {\n"
520 " bvec4 compare = lessThan(texCoord.xxyy, windowParams);\n"
521 " compare = equal(compare, bvec4(true, false, true, false));\n"
522 " if (any(compare)) {\n"
523 " vec2 h = windowParams.xy;\n"
524 " vec2 v = windowParams.zw;\n"
525 " if (v.x > v.y) {\n"
526 " if (compare.z && compare.w) {\n"
527 " return;\n"
528 " }\n"
529 " } else if (compare.z || compare.w) {\n"
530 " return;\n"
531 " }\n"
532 " if (h.x > h.y) {\n"
533 " if (compare.x && compare.y) {\n"
534 " return;\n"
535 " }\n"
536 " } else if (compare.x || compare.y) {\n"
537 " return;\n"
538 " }\n"
539 " }\n"
540 " windowFlags.x = flags;\n"
541 "}\n"
542
543 "vec4 interpolate(ivec4 win[160]) {\n"
544 " vec4 bottom = vec4(win[int(texCoord.y) - 1]);\n"
545 " vec4 top = vec4(win[int(texCoord.y)]);\n"
546 " if (distance(top, bottom) > 40.) {\n"
547 " return top;\n"
548 " }\n"
549 " return vec4(mix(bottom.xy, top.xy, fract(texCoord.y)), top.zw);\n"
550 "}\n"
551
552 "void main() {\n"
553 " int dispflags = (dispcnt & 0x1F) | 0x20;\n"
554 " if ((dispcnt & 0xE0) == 0) {\n"
555 " window = ivec3(dispflags, blend);\n"
556 " } else {\n"
557 " ivec3 windowFlags = ivec3(flags.z, blend);\n"
558 " if ((dispcnt & 0x40) != 0) { \n"
559 " crop(interpolate(win1), flags.y, windowFlags);\n"
560 " }\n"
561 " if ((dispcnt & 0x20) != 0) { \n"
562 " crop(interpolate(win0), flags.x, windowFlags);\n"
563 " }\n"
564 " window = windowFlags;\n"
565 " }\n"
566 "}\n";
567
568static const struct GBAVideoGLUniform _uniformsFinalize[] = {
569 { "loc", GBA_GL_VS_LOC, },
570 { "maxPos", GBA_GL_VS_MAXPOS, },
571 { "scale", GBA_GL_FINALIZE_SCALE, },
572 { "layers", GBA_GL_FINALIZE_LAYERS, },
573 { "flags", GBA_GL_FINALIZE_FLAGS, },
574 { "window", GBA_GL_FINALIZE_WINDOW, },
575 { "backdrop", GBA_GL_FINALIZE_BACKDROP, },
576 { "backdropFlags", GBA_GL_FINALIZE_BACKDROPFLAGS, },
577 { 0 }
578};
579
580static const char* const _finalize =
581 "in vec2 texCoord;\n"
582 "uniform int scale;\n"
583 "uniform sampler2D layers[5];\n"
584 "uniform isampler2D flags[5];\n"
585 "uniform isampler2D window;\n"
586 "uniform sampler2D backdrop;\n"
587 "uniform isampler2D backdropFlags;\n"
588 "out vec4 color;\n"
589
590 "void composite(vec4 pixel, ivec4 flags, inout vec4 topPixel, inout ivec4 topFlags, inout vec4 bottomPixel, inout ivec4 bottomFlags) {\n"
591 " if (pixel.a == 0.) {\n"
592 " return;\n"
593 " }\n"
594 " if (flags.x >= topFlags.x) {\n"
595 " if (flags.x >= bottomFlags.x) {\n"
596 " return;\n"
597 " }\n"
598 " bottomFlags = flags;\n"
599 " bottomPixel = pixel;\n"
600 " } else {\n"
601 " bottomFlags = topFlags;\n"
602 " topFlags = flags;\n"
603 " bottomPixel = topPixel;\n"
604 " topPixel = pixel;\n"
605 " }\n"
606 "}\n"
607
608 "void main() {\n"
609 " vec4 topPixel = texelFetch(backdrop, ivec2(0, texCoord.y), 0);\n"
610 " vec4 bottomPixel = topPixel;\n"
611 " ivec4 topFlags = ivec4(texelFetch(backdropFlags, ivec2(0, texCoord.y), 0));\n"
612 " ivec4 bottomFlags = topFlags;\n"
613 " ivec4 windowFlags = texelFetch(window, ivec2(texCoord * float(scale)), 0);\n"
614 " int layerWindow = windowFlags.x;\n"
615 " if ((layerWindow & 16) != 0) {\n"
616 " vec4 pix = texelFetch(layers[4], ivec2(texCoord * float(scale)), 0);\n"
617 " ivec4 inflags = ivec4(texelFetch(flags[4], ivec2(texCoord * float(scale)), 0));\n"
618 " composite(pix, inflags, topPixel, topFlags, bottomPixel, bottomFlags);\n"
619 " }\n"
620 " if ((layerWindow & 1) != 0) {\n"
621 " vec4 pix = texelFetch(layers[0], ivec2(texCoord * float(scale)), 0);\n"
622 " ivec4 inflags = ivec4(texelFetch(flags[0], ivec2(texCoord * float(scale)), 0).xyz, 0);\n"
623 " composite(pix, inflags, topPixel, topFlags, bottomPixel, bottomFlags);\n"
624 " }\n"
625 " if ((layerWindow & 2) != 0) {\n"
626 " vec4 pix = texelFetch(layers[1], ivec2(texCoord * float(scale)), 0);\n"
627 " ivec4 inflags = ivec4(texelFetch(flags[1], ivec2(texCoord * float(scale)), 0).xyz, 0);\n"
628 " composite(pix, inflags, topPixel, topFlags, bottomPixel, bottomFlags);\n"
629 " }\n"
630 " if ((layerWindow & 4) != 0) {\n"
631 " vec4 pix = texelFetch(layers[2], ivec2(texCoord * float(scale)), 0);\n"
632 " ivec4 inflags = ivec4(texelFetch(flags[2], ivec2(texCoord * float(scale)), 0).xyz.xyz, 0);\n"
633 " composite(pix, inflags, topPixel, topFlags, bottomPixel, bottomFlags);\n"
634 " }\n"
635 " if ((layerWindow & 8) != 0) {\n"
636 " vec4 pix = texelFetch(layers[3], ivec2(texCoord * float(scale)), 0);\n"
637 " ivec4 inflags = ivec4(texelFetch(flags[3], ivec2(texCoord * float(scale)), 0).xyz, 0);\n"
638 " composite(pix, inflags, topPixel, topFlags, bottomPixel, bottomFlags);\n"
639 " }\n"
640 " if ((layerWindow & 32) == 0) {\n"
641 " topFlags.y &= ~1;\n"
642 " }\n"
643 " if (((topFlags.y & 13) == 5 || topFlags.w > 0) && (bottomFlags.y & 2) == 2) {\n"
644 " topPixel.rgb *= float(topFlags.z) / 16.;\n"
645 " topPixel.rgb += bottomPixel.rgb * float(windowFlags.y) / 16.;\n"
646 " } else if ((topFlags.y & 13) == 9) {\n"
647 " topPixel.rgb += (1. - topPixel.rgb) * float(windowFlags.z) / 16.;\n"
648 " } else if ((topFlags.y & 13) == 13) {\n"
649 " topPixel.rgb -= topPixel.rgb * float(windowFlags.z) / 16.;\n"
650 " }\n"
651 " color = topPixel;\n"
652 "}";
653
654static const GLint _vertices[] = {
655 0, 0,
656 0, 1,
657 1, 1,
658 1, 0,
659};
660
661void GBAVideoGLRendererCreate(struct GBAVideoGLRenderer* renderer) {
662 renderer->d.init = GBAVideoGLRendererInit;
663 renderer->d.reset = GBAVideoGLRendererReset;
664 renderer->d.deinit = GBAVideoGLRendererDeinit;
665 renderer->d.writeVideoRegister = GBAVideoGLRendererWriteVideoRegister;
666 renderer->d.writeVRAM = GBAVideoGLRendererWriteVRAM;
667 renderer->d.writeOAM = GBAVideoGLRendererWriteOAM;
668 renderer->d.writePalette = GBAVideoGLRendererWritePalette;
669 renderer->d.drawScanline = GBAVideoGLRendererDrawScanline;
670 renderer->d.finishFrame = GBAVideoGLRendererFinishFrame;
671 renderer->d.getPixels = GBAVideoGLRendererGetPixels;
672 renderer->d.putPixels = GBAVideoGLRendererPutPixels;
673
674 renderer->d.disableBG[0] = false;
675 renderer->d.disableBG[1] = false;
676 renderer->d.disableBG[2] = false;
677 renderer->d.disableBG[3] = false;
678 renderer->d.disableOBJ = false;
679
680 renderer->scale = 1;
681}
682
683static void _compileShader(struct GBAVideoGLRenderer* glRenderer, struct GBAVideoGLShader* shader, const char** shaderBuffer, int shaderBufferLines, GLuint vs, const struct GBAVideoGLUniform* uniforms, char* log) {
684 GLuint program = glCreateProgram();
685 shader->program = program;
686
687 GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
688 glAttachShader(program, vs);
689 glAttachShader(program, fs);
690 glShaderSource(fs, shaderBufferLines, shaderBuffer, 0);
691 glCompileShader(fs);
692 glGetShaderInfoLog(fs, 2048, 0, log);
693 if (log[0]) {
694 mLOG(GBA_VIDEO, ERROR, "Fragment shader compilation failure: %s", log);
695 }
696 glLinkProgram(program);
697 glGetProgramInfoLog(program, 2048, 0, log);
698 if (log[0]) {
699 mLOG(GBA_VIDEO, ERROR, "Program link failure: %s", log);
700 }
701 glDeleteShader(fs);
702#ifndef BUILD_GLES3
703 glBindFragDataLocation(program, 0, "color");
704 glBindFragDataLocation(program, 1, "flags");
705#endif
706
707 glGenVertexArrays(1, &shader->vao);
708 glBindVertexArray(shader->vao);
709 glBindBuffer(GL_ARRAY_BUFFER, glRenderer->vbo);
710 GLuint positionLocation = glGetAttribLocation(program, "position");
711 glEnableVertexAttribArray(positionLocation);
712 glVertexAttribPointer(positionLocation, 2, GL_INT, GL_FALSE, 0, NULL);
713
714 size_t i;
715 for (i = 0; uniforms[i].name; ++i) {
716 shader->uniforms[uniforms[i].type] = glGetUniformLocation(program, uniforms[i].name);
717 }
718}
719
720static void _deleteShader(struct GBAVideoGLShader* shader) {
721 glDeleteProgram(shader->program);
722 glDeleteVertexArrays(1, &shader->vao);
723}
724
725static void _initFramebufferTextureEx(GLuint tex, GLenum internalFormat, GLenum format, GLenum type, GLenum attachment, int scale) {
726 glBindTexture(GL_TEXTURE_2D, tex);
727 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
728 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
729 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
730 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
731 glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, scale > 0 ? GBA_VIDEO_HORIZONTAL_PIXELS * scale : 1, GBA_VIDEO_VERTICAL_PIXELS * (scale > 0 ? scale : 1), 0, format, type, 0);
732 glFramebufferTexture2D(GL_FRAMEBUFFER, attachment, GL_TEXTURE_2D, tex, 0);
733}
734
735static void _initFramebufferTexture(GLuint tex, GLenum format, GLenum attachment, int scale) {
736 _initFramebufferTextureEx(tex, format, format, GL_UNSIGNED_BYTE, attachment, scale);
737}
738
739void GBAVideoGLRendererInit(struct GBAVideoRenderer* renderer) {
740 struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;
741 glRenderer->temporaryBuffer = NULL;
742
743 glGenFramebuffers(GBA_GL_FBO_MAX, glRenderer->fbo);
744 glGenTextures(GBA_GL_TEX_MAX, glRenderer->layers);
745
746 glGenTextures(1, &glRenderer->vramTex);
747 glBindTexture(GL_TEXTURE_2D, glRenderer->vramTex);
748 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
749 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
750 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA4, 256, 192, 0, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, 0);
751
752 glBindTexture(GL_TEXTURE_2D, glRenderer->layers[GBA_GL_TEX_AFFINE_2]);
753 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
754 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
755 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
756 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
757 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16I, 2, GBA_VIDEO_VERTICAL_PIXELS, 0, GL_RGBA_INTEGER, GL_SHORT, NULL);
758 glBindTexture(GL_TEXTURE_2D, glRenderer->layers[GBA_GL_TEX_AFFINE_3]);
759 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
760 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
761 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
762 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
763 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16I, 2, GBA_VIDEO_VERTICAL_PIXELS, 0, GL_RGBA_INTEGER, GL_SHORT, NULL);
764
765 glBindFramebuffer(GL_FRAMEBUFFER, glRenderer->fbo[GBA_GL_FBO_OBJ]);
766 _initFramebufferTexture(glRenderer->layers[GBA_GL_TEX_OBJ_COLOR], GL_RGBA, GL_COLOR_ATTACHMENT0, glRenderer->scale);
767 _initFramebufferTextureEx(glRenderer->layers[GBA_GL_TEX_OBJ_FLAGS], GL_RGBA8I, GL_RGBA_INTEGER, GL_BYTE, GL_COLOR_ATTACHMENT1, glRenderer->scale);
768 _initFramebufferTextureEx(glRenderer->layers[GBA_GL_TEX_WINDOW], GL_RGBA8I, GL_RGBA_INTEGER, GL_BYTE, GL_COLOR_ATTACHMENT2, glRenderer->scale);
769 _initFramebufferTextureEx(glRenderer->layers[GBA_GL_TEX_OBJ_DEPTH], GL_DEPTH_COMPONENT16, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, GL_DEPTH_ATTACHMENT, glRenderer->scale);
770
771 glBindFramebuffer(GL_FRAMEBUFFER, glRenderer->fbo[GBA_GL_FBO_BACKDROP]);
772 _initFramebufferTexture(glRenderer->layers[GBA_GL_TEX_BACKDROP_COLOR], GL_RGB, GL_COLOR_ATTACHMENT0, 0);
773 _initFramebufferTextureEx(glRenderer->layers[GBA_GL_TEX_BACKDROP_FLAGS], GL_RGBA8I, GL_RGBA_INTEGER, GL_BYTE, GL_COLOR_ATTACHMENT1, glRenderer->scale);
774
775 glBindFramebuffer(GL_FRAMEBUFFER, glRenderer->fbo[GBA_GL_FBO_WINDOW]);
776 _initFramebufferTextureEx(glRenderer->layers[GBA_GL_TEX_WINDOW], GL_RGBA8I, GL_RGBA_INTEGER, GL_BYTE, GL_COLOR_ATTACHMENT0, glRenderer->scale);
777
778 glBindFramebuffer(GL_FRAMEBUFFER, glRenderer->fbo[GBA_GL_FBO_OUTPUT]);
779 _initFramebufferTexture(glRenderer->outputTex, GL_RGB, GL_COLOR_ATTACHMENT0, glRenderer->scale);
780
781 glBindFramebuffer(GL_FRAMEBUFFER, 0);
782
783 glGenBuffers(1, &glRenderer->vbo);
784 glBindBuffer(GL_ARRAY_BUFFER, glRenderer->vbo);
785 glBufferData(GL_ARRAY_BUFFER, sizeof(_vertices), _vertices, GL_STATIC_DRAW);
786
787 int i;
788 for (i = 0; i < 4; ++i) {
789 struct GBAVideoGLBackground* bg = &glRenderer->bg[i];
790 bg->index = i;
791 bg->enabled = 0;
792 bg->priority = 0;
793 bg->charBase = 0;
794 bg->mosaic = 0;
795 bg->multipalette = 0;
796 bg->screenBase = 0;
797 bg->overflow = 0;
798 bg->size = 0;
799 bg->target1 = 0;
800 bg->target2 = 0;
801 bg->x = 0;
802 bg->y = 0;
803 bg->refx = 0;
804 bg->refy = 0;
805 bg->affine.dx = 256;
806 bg->affine.dmx = 0;
807 bg->affine.dy = 0;
808 bg->affine.dmy = 256;
809 bg->affine.sx = 0;
810 bg->affine.sy = 0;
811 glGenFramebuffers(1, &bg->fbo);
812 glGenTextures(1, &bg->tex);
813 glGenTextures(1, &bg->flags);
814 glBindFramebuffer(GL_FRAMEBUFFER, bg->fbo);
815 _initFramebufferTexture(bg->tex, GL_RGBA, GL_COLOR_ATTACHMENT0, glRenderer->scale);
816 _initFramebufferTextureEx(bg->flags, GL_RGBA8I, GL_RGBA_INTEGER, GL_BYTE, GL_COLOR_ATTACHMENT1, glRenderer->scale);
817 glBindFramebuffer(GL_FRAMEBUFFER, 0);
818 }
819
820 char log[2048];
821 const GLchar* shaderBuffer[4];
822 const GLubyte* version = glGetString(GL_VERSION);
823 if (strncmp((const char*) version, "OpenGL ES ", strlen("OpenGL ES "))) {
824 shaderBuffer[0] = _gl3Header;
825 } else {
826 shaderBuffer[0] = _gles3Header;
827 }
828
829 GLuint vs = glCreateShader(GL_VERTEX_SHADER);
830 shaderBuffer[1] = _vertexShader;
831 glShaderSource(vs, 2, shaderBuffer, 0);
832 glCompileShader(vs);
833 glGetShaderInfoLog(vs, 2048, 0, log);
834 if (log[0]) {
835 mLOG(GBA_VIDEO, ERROR, "Vertex shader compilation failure: %s", log);
836 }
837
838 shaderBuffer[1] = _renderMode0;
839
840 shaderBuffer[2] = _renderTile16;
841 _compileShader(glRenderer, &glRenderer->bgShader[0], shaderBuffer, 3, vs, _uniformsMode0, log);
842
843 shaderBuffer[2] = _renderTile256;
844 _compileShader(glRenderer, &glRenderer->bgShader[1], shaderBuffer, 3, vs, _uniformsMode0, log);
845
846 shaderBuffer[1] = _renderMode2;
847 shaderBuffer[2] = _interpolate;
848
849 shaderBuffer[3] = _fetchTileOverflow;
850 _compileShader(glRenderer, &glRenderer->bgShader[2], shaderBuffer, 4, vs, _uniformsMode2, log);
851
852 shaderBuffer[3] = _fetchTileNoOverflow;
853 _compileShader(glRenderer, &glRenderer->bgShader[3], shaderBuffer, 4, vs, _uniformsMode2, log);
854
855 shaderBuffer[1] = _renderMode4;
856 shaderBuffer[2] = _interpolate;
857 _compileShader(glRenderer, &glRenderer->bgShader[4], shaderBuffer, 3, vs, _uniformsMode4, log);
858
859 shaderBuffer[1] = _renderMode35;
860 shaderBuffer[2] = _interpolate;
861 _compileShader(glRenderer, &glRenderer->bgShader[5], shaderBuffer, 3, vs, _uniformsMode35, log);
862
863 shaderBuffer[1] = _renderObj;
864
865 shaderBuffer[2] = _renderTile16;
866 _compileShader(glRenderer, &glRenderer->objShader[0], shaderBuffer, 3, vs, _uniformsObj, log);
867#ifndef BUILD_GLES3
868 glBindFragDataLocation(glRenderer->objShader[0].program, 2, "window");
869#endif
870
871 shaderBuffer[2] = _renderTile256;
872 _compileShader(glRenderer, &glRenderer->objShader[1], shaderBuffer, 3, vs, _uniformsObj, log);
873#ifndef BUILD_GLES3
874 glBindFragDataLocation(glRenderer->objShader[1].program, 2, "window");
875#endif
876
877 shaderBuffer[1] = _renderWindow;
878 _compileShader(glRenderer, &glRenderer->windowShader, shaderBuffer, 2, vs, _uniformsWindow, log);
879#ifndef BUILD_GLES3
880 glBindFragDataLocation(glRenderer->windowShader.program, 0, "window");
881#endif
882
883 shaderBuffer[1] = _finalize;
884 _compileShader(glRenderer, &glRenderer->finalizeShader, shaderBuffer, 2, vs, _uniformsFinalize, log);
885
886 glBindVertexArray(0);
887 glDeleteShader(vs);
888
889 GBAVideoGLRendererReset(renderer);
890}
891
892void GBAVideoGLRendererDeinit(struct GBAVideoRenderer* renderer) {
893 struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;
894 if (glRenderer->temporaryBuffer) {
895 mappedMemoryFree(glRenderer->temporaryBuffer, GBA_VIDEO_HORIZONTAL_PIXELS * GBA_VIDEO_VERTICAL_PIXELS * glRenderer->scale * glRenderer->scale);
896 }
897 glDeleteFramebuffers(GBA_GL_FBO_MAX, glRenderer->fbo);
898 glDeleteTextures(GBA_GL_TEX_MAX, glRenderer->layers);
899 glDeleteTextures(1, &glRenderer->vramTex);
900 glDeleteBuffers(1, &glRenderer->vbo);
901
902 _deleteShader(&glRenderer->bgShader[0]);
903 _deleteShader(&glRenderer->bgShader[1]);
904 _deleteShader(&glRenderer->bgShader[2]);
905 _deleteShader(&glRenderer->bgShader[3]);
906 _deleteShader(&glRenderer->objShader[0]);
907 _deleteShader(&glRenderer->objShader[1]);
908 _deleteShader(&glRenderer->finalizeShader);
909
910 int i;
911 for (i = 0; i < 4; ++i) {
912 struct GBAVideoGLBackground* bg = &glRenderer->bg[i];
913 glDeleteFramebuffers(1, &bg->fbo);
914 glDeleteTextures(1, &bg->tex);
915 glDeleteTextures(1, &bg->flags);
916 }
917}
918
919void GBAVideoGLRendererReset(struct GBAVideoRenderer* renderer) {
920 struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;
921
922 glRenderer->paletteDirty = true;
923 glRenderer->vramDirty = 0xFFFFFF;
924 glRenderer->firstAffine = -1;
925 glRenderer->firstY = -1;
926 glRenderer->dispcnt = 0x0080;
927 glRenderer->mosaic = 0;
928 memset(glRenderer->shadowRegs, 0, sizeof(glRenderer->shadowRegs));
929 glRenderer->regsDirty = 0xFFFFFFFFFFFEULL;
930}
931
932void GBAVideoGLRendererWriteVRAM(struct GBAVideoRenderer* renderer, uint32_t address) {
933 struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;
934 glRenderer->vramDirty |= 1 << (address >> 12);
935}
936
937void GBAVideoGLRendererWriteOAM(struct GBAVideoRenderer* renderer, uint32_t oam) {
938 UNUSED(oam);
939 struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;
940 glRenderer->oamDirty = true;
941}
942
943void GBAVideoGLRendererWritePalette(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
944 struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;
945 UNUSED(address);
946 UNUSED(value);
947 glRenderer->paletteDirty = true;
948}
949
950uint16_t GBAVideoGLRendererWriteVideoRegister(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
951 struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;
952 if (renderer->cache) {
953 GBAVideoCacheWriteVideoRegister(renderer->cache, address, value);
954 }
955
956 bool dirty = false;
957 switch (address) {
958 case REG_DISPCNT:
959 value &= 0xFFF7;
960 dirty = true;
961 break;
962 case REG_BG0CNT:
963 case REG_BG1CNT:
964 value &= 0xDFFF;
965 dirty = true;
966 break;
967 case REG_BG0HOFS:
968 case REG_BG0VOFS:
969 case REG_BG1HOFS:
970 case REG_BG1VOFS:
971 case REG_BG2HOFS:
972 case REG_BG2VOFS:
973 case REG_BG3HOFS:
974 case REG_BG3VOFS:
975 value &= 0x01FF;
976 dirty = true;
977 break;
978 case REG_BG2PA:
979 glRenderer->bg[2].affine.dx = value;
980 break;
981 case REG_BG2PB:
982 glRenderer->bg[2].affine.dmx = value;
983 break;
984 case REG_BG2PC:
985 glRenderer->bg[2].affine.dy = value;
986 break;
987 case REG_BG2PD:
988 glRenderer->bg[2].affine.dmy = value;
989 break;
990 case REG_BG2X_LO:
991 GBAVideoGLRendererWriteBGX_LO(&glRenderer->bg[2], value);
992 break;
993 case REG_BG2X_HI:
994 GBAVideoGLRendererWriteBGX_HI(&glRenderer->bg[2], value);
995 break;
996 case REG_BG2Y_LO:
997 GBAVideoGLRendererWriteBGY_LO(&glRenderer->bg[2], value);
998 break;
999 case REG_BG2Y_HI:
1000 GBAVideoGLRendererWriteBGY_HI(&glRenderer->bg[2], value);
1001 break;
1002 case REG_BG3PA:
1003 glRenderer->bg[3].affine.dx = value;
1004 break;
1005 case REG_BG3PB:
1006 glRenderer->bg[3].affine.dmx = value;
1007 break;
1008 case REG_BG3PC:
1009 glRenderer->bg[3].affine.dy = value;
1010 break;
1011 case REG_BG3PD:
1012 glRenderer->bg[3].affine.dmy = value;
1013 break;
1014 case REG_BG3X_LO:
1015 GBAVideoGLRendererWriteBGX_LO(&glRenderer->bg[3], value);
1016 break;
1017 case REG_BG3X_HI:
1018 GBAVideoGLRendererWriteBGX_HI(&glRenderer->bg[3], value);
1019 break;
1020 case REG_BG3Y_LO:
1021 GBAVideoGLRendererWriteBGY_LO(&glRenderer->bg[3], value);
1022 break;
1023 case REG_BG3Y_HI:
1024 GBAVideoGLRendererWriteBGY_HI(&glRenderer->bg[3], value);
1025 break;
1026 case REG_BLDALPHA:
1027 value &= 0x1F1F;
1028 dirty = true;
1029 break;
1030 case REG_BLDY:
1031 value &= 0x1F;
1032 if (value > 0x10) {
1033 value = 0x10;
1034 }
1035 dirty = true;
1036 break;
1037 case REG_WIN0H:
1038 glRenderer->winN[0].h.end = value;
1039 glRenderer->winN[0].h.start = value >> 8;
1040 if (glRenderer->winN[0].h.start > GBA_VIDEO_HORIZONTAL_PIXELS && glRenderer->winN[0].h.start > glRenderer->winN[0].h.end) {
1041 glRenderer->winN[0].h.start = 0;
1042 }
1043 if (glRenderer->winN[0].h.end > GBA_VIDEO_HORIZONTAL_PIXELS) {
1044 glRenderer->winN[0].h.end = GBA_VIDEO_HORIZONTAL_PIXELS;
1045 if (glRenderer->winN[0].h.start > GBA_VIDEO_HORIZONTAL_PIXELS) {
1046 glRenderer->winN[0].h.start = GBA_VIDEO_HORIZONTAL_PIXELS;
1047 }
1048 }
1049 break;
1050 case REG_WIN1H:
1051 glRenderer->winN[1].h.end = value;
1052 glRenderer->winN[1].h.start = value >> 8;
1053 if (glRenderer->winN[1].h.start > GBA_VIDEO_HORIZONTAL_PIXELS && glRenderer->winN[1].h.start > glRenderer->winN[1].h.end) {
1054 glRenderer->winN[1].h.start = 0;
1055 }
1056 if (glRenderer->winN[1].h.end > GBA_VIDEO_HORIZONTAL_PIXELS) {
1057 glRenderer->winN[1].h.end = GBA_VIDEO_HORIZONTAL_PIXELS;
1058 if (glRenderer->winN[1].h.start > GBA_VIDEO_HORIZONTAL_PIXELS) {
1059 glRenderer->winN[1].h.start = GBA_VIDEO_HORIZONTAL_PIXELS;
1060 }
1061 }
1062 break;
1063 case REG_WIN0V:
1064 glRenderer->winN[0].v.end = value;
1065 glRenderer->winN[0].v.start = value >> 8;
1066 if (glRenderer->winN[0].v.start > GBA_VIDEO_VERTICAL_PIXELS && glRenderer->winN[0].v.start > glRenderer->winN[0].v.end) {
1067 glRenderer->winN[0].v.start = 0;
1068 }
1069 if (glRenderer->winN[0].v.end > GBA_VIDEO_VERTICAL_PIXELS) {
1070 glRenderer->winN[0].v.end = GBA_VIDEO_VERTICAL_PIXELS;
1071 if (glRenderer->winN[0].v.start > GBA_VIDEO_VERTICAL_PIXELS) {
1072 glRenderer->winN[0].v.start = GBA_VIDEO_VERTICAL_PIXELS;
1073 }
1074 }
1075 break;
1076 case REG_WIN1V:
1077 glRenderer->winN[1].v.end = value;
1078 glRenderer->winN[1].v.start = value >> 8;
1079 if (glRenderer->winN[1].v.start > GBA_VIDEO_VERTICAL_PIXELS && glRenderer->winN[1].v.start > glRenderer->winN[1].v.end) {
1080 glRenderer->winN[1].v.start = 0;
1081 }
1082 if (glRenderer->winN[1].v.end > GBA_VIDEO_VERTICAL_PIXELS) {
1083 glRenderer->winN[1].v.end = GBA_VIDEO_VERTICAL_PIXELS;
1084 if (glRenderer->winN[1].v.start > GBA_VIDEO_VERTICAL_PIXELS) {
1085 glRenderer->winN[1].v.start = GBA_VIDEO_VERTICAL_PIXELS;
1086 }
1087 }
1088 break;
1089 case REG_WININ:
1090 case REG_WINOUT:
1091 value &= 0x3F3F;
1092 dirty = true;
1093 break;
1094 default:
1095 dirty = true;
1096 break;
1097 }
1098 if (glRenderer->shadowRegs[address >> 1] == value) {
1099 dirty = false;
1100 } else {
1101 glRenderer->shadowRegs[address >> 1] = value;
1102 }
1103 if (dirty) {
1104 glRenderer->regsDirty |= 1ULL << (address >> 1);
1105 }
1106 return value;
1107}
1108
1109void _cleanRegister(struct GBAVideoGLRenderer* glRenderer, int address, uint16_t value) {
1110 switch (address) {
1111 case REG_DISPCNT:
1112 glRenderer->dispcnt = value;
1113 GBAVideoGLRendererUpdateDISPCNT(glRenderer);
1114 break;
1115 case REG_BG0CNT:
1116 GBAVideoGLRendererWriteBGCNT(&glRenderer->bg[0], value);
1117 break;
1118 case REG_BG1CNT:
1119 GBAVideoGLRendererWriteBGCNT(&glRenderer->bg[1], value);
1120 break;
1121 case REG_BG2CNT:
1122 GBAVideoGLRendererWriteBGCNT(&glRenderer->bg[2], value);
1123 break;
1124 case REG_BG3CNT:
1125 GBAVideoGLRendererWriteBGCNT(&glRenderer->bg[3], value);
1126 break;
1127 case REG_BG0HOFS:
1128 glRenderer->bg[0].x = value;
1129 break;
1130 case REG_BG0VOFS:
1131 glRenderer->bg[0].y = value;
1132 break;
1133 case REG_BG1HOFS:
1134 glRenderer->bg[1].x = value;
1135 break;
1136 case REG_BG1VOFS:
1137 glRenderer->bg[1].y = value;
1138 break;
1139 case REG_BG2HOFS:
1140 glRenderer->bg[2].x = value;
1141 break;
1142 case REG_BG2VOFS:
1143 glRenderer->bg[2].y = value;
1144 break;
1145 case REG_BG3HOFS:
1146 glRenderer->bg[3].x = value;
1147 break;
1148 case REG_BG3VOFS:
1149 glRenderer->bg[3].y = value;
1150 break;
1151 case REG_BLDCNT:
1152 GBAVideoGLRendererWriteBLDCNT(glRenderer, value);
1153 value &= 0x3FFF;
1154 break;
1155 case REG_BLDALPHA:
1156 glRenderer->blda = value & 0x1F;
1157 if (glRenderer->blda > 0x10) {
1158 glRenderer->blda = 0x10;
1159 }
1160 glRenderer->bldb = (value >> 8) & 0x1F;
1161 if (glRenderer->bldb > 0x10) {
1162 glRenderer->bldb = 0x10;
1163 }
1164 value &= 0x1F1F;
1165 break;
1166 case REG_BLDY:
1167 glRenderer->bldy = value;
1168 break;
1169 case REG_WININ:
1170 glRenderer->winN[0].control = value;
1171 glRenderer->winN[1].control = value >> 8;
1172 break;
1173 case REG_WINOUT:
1174 glRenderer->winout = value;
1175 glRenderer->objwin = value >> 8;
1176 break;
1177 case REG_MOSAIC:
1178 glRenderer->mosaic = value;
1179 break;
1180 default:
1181 break;
1182 }
1183}
1184
1185static bool _dirtyMode0(struct GBAVideoGLRenderer* renderer, struct GBAVideoGLBackground* background, int y) {
1186 UNUSED(y);
1187 if (!background->enabled) {
1188 return false;
1189 }
1190 unsigned screenBase = background->screenBase >> 11; // Lops off one extra bit
1191 unsigned screenMask = (7 << screenBase) & 0xFFFF; // Technically overzealous
1192 if (renderer->vramDirty & screenMask) {
1193 return true;
1194 }
1195 unsigned charBase = background->charBase >> 11;
1196 unsigned charMask = (0xFFFF << charBase) & 0xFFFF;
1197 if (renderer->vramDirty & charMask) {
1198 return true;
1199 }
1200 return false;
1201}
1202
1203static bool _dirtyMode2(struct GBAVideoGLRenderer* renderer, struct GBAVideoGLBackground* background, int y) {
1204 UNUSED(y);
1205 if (!background->enabled) {
1206 return false;
1207 }
1208 unsigned screenBase = background->screenBase >> 11; // Lops off one extra bit
1209 unsigned screenMask = (0xF << screenBase) & 0xFFFF;
1210 if (renderer->vramDirty & screenMask) {
1211 return true;
1212 }
1213 unsigned charBase = background->charBase >> 11;
1214 unsigned charMask = (0x3FFF << charBase) & 0xFFFF;
1215 if (renderer->vramDirty & charMask) {
1216 return true;
1217 }
1218 return false;
1219}
1220
1221static bool _dirtyMode3(struct GBAVideoGLRenderer* renderer, struct GBAVideoGLBackground* background, int y) {
1222 UNUSED(y);
1223 if (!background->enabled) {
1224 return false;
1225 }
1226 if (renderer->vramDirty & 0xFFFFF) {
1227 return true;
1228 }
1229 return false;
1230}
1231
1232static bool _dirtyMode45(struct GBAVideoGLRenderer* renderer, struct GBAVideoGLBackground* background, int y) {
1233 UNUSED(y);
1234 if (!background->enabled) {
1235 return false;
1236 }
1237 int start = GBARegisterDISPCNTIsFrameSelect(renderer->dispcnt) ? 5 : 0;
1238 int mask = 0x3FF << start;
1239 if (renderer->vramDirty & mask) {
1240 return true;
1241 }
1242 return false;
1243}
1244
1245static bool _needsVramUpload(struct GBAVideoGLRenderer* renderer, int y) {
1246 if (!renderer->vramDirty) {
1247 return false;
1248 }
1249 if (y == 0) {
1250 return true;
1251 }
1252
1253 if (GBARegisterDISPCNTIsObjEnable(renderer->dispcnt) && renderer->vramDirty & 0xFF0000) {
1254 return true;
1255 }
1256
1257 bool dirty = false;
1258 switch (GBARegisterDISPCNTGetMode(renderer->dispcnt)) {
1259 case 0:
1260 dirty = dirty || _dirtyMode0(renderer, &renderer->bg[0], y);
1261 dirty = dirty || _dirtyMode0(renderer, &renderer->bg[1], y);
1262 dirty = dirty || _dirtyMode0(renderer, &renderer->bg[2], y);
1263 dirty = dirty || _dirtyMode0(renderer, &renderer->bg[3], y);
1264 break;
1265 case 1:
1266 dirty = dirty || _dirtyMode0(renderer, &renderer->bg[0], y);
1267 dirty = dirty || _dirtyMode0(renderer, &renderer->bg[1], y);
1268 dirty = dirty || _dirtyMode2(renderer, &renderer->bg[2], y);
1269 break;
1270 case 2:
1271 dirty = dirty || _dirtyMode2(renderer, &renderer->bg[2], y);
1272 dirty = dirty || _dirtyMode2(renderer, &renderer->bg[3], y);
1273 break;
1274 case 3:
1275 dirty = _dirtyMode3(renderer, &renderer->bg[2], y);
1276 break;
1277 case 4:
1278 dirty = _dirtyMode45(renderer, &renderer->bg[2], y);
1279 break;
1280 case 5:
1281 dirty = _dirtyMode45(renderer, &renderer->bg[2], y);
1282 break;
1283 }
1284 return dirty;
1285}
1286
1287void GBAVideoGLRendererDrawScanline(struct GBAVideoRenderer* renderer, int y) {
1288 struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;
1289
1290 if (GBARegisterDISPCNTGetMode(glRenderer->dispcnt) != 0) {
1291 if (glRenderer->firstAffine < 0) {
1292 glRenderer->firstAffine = y;
1293 }
1294 } else {
1295 glRenderer->firstAffine = -1;
1296 }
1297
1298 if (glRenderer->paletteDirty || _needsVramUpload(glRenderer, y) || glRenderer->oamDirty || glRenderer->regsDirty) {
1299 if (glRenderer->firstY >= 0) {
1300 _drawScanlines(glRenderer, y - 1);
1301 glBindVertexArray(0);
1302 }
1303 }
1304 if (glRenderer->firstY < 0) {
1305 glRenderer->firstY = y;
1306 }
1307
1308 int i;
1309 for (i = 0; i < 0x30; ++i) {
1310 if (!(glRenderer->regsDirty & (1ULL << i))) {
1311 continue;
1312 }
1313 _cleanRegister(glRenderer, i << 1, glRenderer->shadowRegs[i]);
1314 }
1315 glRenderer->regsDirty = 0;
1316
1317 memcpy(&glRenderer->affine[0][y], &glRenderer->bg[2].affine, sizeof(struct GBAVideoGLAffine));
1318 memcpy(&glRenderer->affine[1][y], &glRenderer->bg[3].affine, sizeof(struct GBAVideoGLAffine));
1319 glRenderer->winNHistory[0][y * 4 + 0] = glRenderer->winN[0].h.start;
1320 glRenderer->winNHistory[0][y * 4 + 1] = glRenderer->winN[0].h.end;
1321 glRenderer->winNHistory[0][y * 4 + 2] = glRenderer->winN[0].v.start;
1322 glRenderer->winNHistory[0][y * 4 + 3] = glRenderer->winN[0].v.end;
1323 glRenderer->winNHistory[1][y * 4 + 0] = glRenderer->winN[1].h.start;
1324 glRenderer->winNHistory[1][y * 4 + 1] = glRenderer->winN[1].h.end;
1325 glRenderer->winNHistory[1][y * 4 + 2] = glRenderer->winN[1].v.start;
1326 glRenderer->winNHistory[1][y * 4 + 3] = glRenderer->winN[1].v.end;
1327
1328 if (glRenderer->paletteDirty) {
1329 for (i = 0; i < 512; ++i) {
1330 glRenderer->shadowPalette[i] = glRenderer->d.palette[i];
1331 }
1332 glRenderer->paletteDirty = false;
1333 }
1334
1335 if (_needsVramUpload(glRenderer, y)) {
1336 int first = -1;
1337 glBindTexture(GL_TEXTURE_2D, glRenderer->vramTex);
1338 for (i = 0; i < 25; ++i) {
1339 if (!(glRenderer->vramDirty & (1 << i))) {
1340 if (first >= 0) {
1341 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 8 * first, 256, 8 * (i - first), GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, &glRenderer->d.vram[2048 * first]);
1342 first = -1;
1343 }
1344 } else if (first < 0) {
1345 first = i;
1346 }
1347 }
1348 glRenderer->vramDirty = 0;
1349 }
1350
1351 if (glRenderer->oamDirty) {
1352 glRenderer->oamMax = GBAVideoRendererCleanOAM(glRenderer->d.oam->obj, glRenderer->sprites, 0);
1353 glRenderer->oamDirty = false;
1354 }
1355
1356 if (y == 0) {
1357 glDisable(GL_SCISSOR_TEST);
1358 glClearColor(0, 0, 0, 0);
1359#ifdef BUILD_GLES3
1360 glClearDepthf(1.f);
1361#else
1362 glClearDepth(1);
1363#endif
1364 glBindFramebuffer(GL_FRAMEBUFFER, glRenderer->fbo[GBA_GL_FBO_OBJ]);
1365 glDrawBuffers(2, (GLenum[]) { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 });
1366 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1367
1368 for (i = 0; i < 4; ++i) {
1369 glBindFramebuffer(GL_FRAMEBUFFER, glRenderer->bg[i].fbo);
1370 glDrawBuffers(2, (GLenum[]) { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 });
1371 glClear(GL_COLOR_BUFFER_BIT);
1372 }
1373 }
1374
1375 if (GBARegisterDISPCNTGetMode(glRenderer->dispcnt) != 0) {
1376 glRenderer->bg[2].affine.sx += glRenderer->bg[2].affine.dmx;
1377 glRenderer->bg[2].affine.sy += glRenderer->bg[2].affine.dmy;
1378 glRenderer->bg[3].affine.sx += glRenderer->bg[3].affine.dmx;
1379 glRenderer->bg[3].affine.sy += glRenderer->bg[3].affine.dmy;
1380 }
1381}
1382
1383void _drawScanlines(struct GBAVideoGLRenderer* glRenderer, int y) {
1384 if (glRenderer->firstAffine >= 0) {
1385 glBindTexture(GL_TEXTURE_2D, glRenderer->layers[GBA_GL_TEX_AFFINE_2]);
1386 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16I, 2, GBA_VIDEO_VERTICAL_PIXELS, 0, GL_RGBA_INTEGER, GL_SHORT, glRenderer->affine[0]);
1387 glBindTexture(GL_TEXTURE_2D, glRenderer->layers[GBA_GL_TEX_AFFINE_3]);
1388 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16I, 2, GBA_VIDEO_VERTICAL_PIXELS, 0, GL_RGBA_INTEGER, GL_SHORT, glRenderer->affine[1]);
1389 }
1390 glEnable(GL_SCISSOR_TEST);
1391
1392 uint32_t backdrop = M_RGB5_TO_RGB8(glRenderer->shadowPalette[0]);
1393 glViewport(0, 0, 1, GBA_VIDEO_VERTICAL_PIXELS);
1394 glScissor(0, glRenderer->firstY, 1, y - glRenderer->firstY + 1);
1395 glBindFramebuffer(GL_FRAMEBUFFER, glRenderer->fbo[GBA_GL_FBO_BACKDROP]);
1396 glDrawBuffers(2, (GLenum[]) { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 });
1397 glClearBufferfv(GL_COLOR, 0, (GLfloat[]) { ((backdrop >> 16) & 0xFF) / 256., ((backdrop >> 8) & 0xFF) / 256., (backdrop & 0xFF) / 256., 1.f });
1398 glClearBufferiv(GL_COLOR, 1, (GLint[]) { 32, glRenderer->target1Bd | (glRenderer->target2Bd * 2) | (glRenderer->blendEffect * 4), glRenderer->blda, 0 });
1399 glDrawBuffers(1, (GLenum[]) { GL_COLOR_ATTACHMENT0 });
1400
1401 GBAVideoGLRendererDrawWindow(glRenderer, y);
1402 if (GBARegisterDISPCNTIsObjEnable(glRenderer->dispcnt) && !glRenderer->d.disableOBJ) {
1403 int i;
1404 glDepthFunc(GL_LESS);
1405 for (i = 0; i < glRenderer->oamMax; ++i) {
1406 struct GBAVideoRendererSprite* sprite = &glRenderer->sprites[i];
1407 if ((y < sprite->y && (sprite->endY - 256 < 0 || glRenderer->firstY >= sprite->endY - 256)) || glRenderer->firstY >= sprite->endY) {
1408 continue;
1409 }
1410
1411 GBAVideoGLRendererDrawSprite(glRenderer, &sprite->obj, y, sprite->y);
1412 }
1413 glDisable(GL_DEPTH_TEST);
1414 }
1415
1416 if (TEST_LAYER_ENABLED(0) && GBARegisterDISPCNTGetMode(glRenderer->dispcnt) < 2) {
1417 GBAVideoGLRendererDrawBackgroundMode0(glRenderer, &glRenderer->bg[0], y);
1418 }
1419 if (TEST_LAYER_ENABLED(1) && GBARegisterDISPCNTGetMode(glRenderer->dispcnt) < 2) {
1420 GBAVideoGLRendererDrawBackgroundMode0(glRenderer, &glRenderer->bg[1], y);
1421 }
1422 if (TEST_LAYER_ENABLED(2)) {
1423 switch (GBARegisterDISPCNTGetMode(glRenderer->dispcnt)) {
1424 case 0:
1425 GBAVideoGLRendererDrawBackgroundMode0(glRenderer, &glRenderer->bg[2], y);
1426 break;
1427 case 1:
1428 case 2:
1429 GBAVideoGLRendererDrawBackgroundMode2(glRenderer, &glRenderer->bg[2], y);
1430 break;
1431 case 3:
1432 GBAVideoGLRendererDrawBackgroundMode3(glRenderer, &glRenderer->bg[2], y);
1433 break;
1434 case 4:
1435 GBAVideoGLRendererDrawBackgroundMode4(glRenderer, &glRenderer->bg[2], y);
1436 break;
1437 case 5:
1438 GBAVideoGLRendererDrawBackgroundMode5(glRenderer, &glRenderer->bg[2], y);
1439 break;
1440 }
1441 }
1442 if (TEST_LAYER_ENABLED(3)) {
1443 switch (GBARegisterDISPCNTGetMode(glRenderer->dispcnt)) {
1444 case 0:
1445 GBAVideoGLRendererDrawBackgroundMode0(glRenderer, &glRenderer->bg[3], y);
1446 break;
1447 case 2:
1448 GBAVideoGLRendererDrawBackgroundMode2(glRenderer, &glRenderer->bg[3], y);
1449 break;
1450 }
1451 }
1452 glRenderer->firstY = -1;
1453}
1454
1455void GBAVideoGLRendererFinishFrame(struct GBAVideoRenderer* renderer) {
1456 struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;
1457 _drawScanlines(glRenderer, GBA_VIDEO_VERTICAL_PIXELS - 1);
1458 _finalizeLayers(glRenderer);
1459 glDisable(GL_SCISSOR_TEST);
1460 glBindVertexArray(0);
1461 glRenderer->firstAffine = -1;
1462 glRenderer->firstY = -1;
1463 glRenderer->bg[2].affine.sx = glRenderer->bg[2].refx;
1464 glRenderer->bg[2].affine.sy = glRenderer->bg[2].refy;
1465 glRenderer->bg[3].affine.sx = glRenderer->bg[3].refx;
1466 glRenderer->bg[3].affine.sy = glRenderer->bg[3].refy;
1467}
1468
1469void GBAVideoGLRendererGetPixels(struct GBAVideoRenderer* renderer, size_t* stride, const void** pixels) {
1470 struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;
1471 *stride = GBA_VIDEO_HORIZONTAL_PIXELS * glRenderer->scale;
1472 if (!glRenderer->temporaryBuffer) {
1473 glRenderer->temporaryBuffer = anonymousMemoryMap(GBA_VIDEO_HORIZONTAL_PIXELS * GBA_VIDEO_VERTICAL_PIXELS * glRenderer->scale * glRenderer->scale * BYTES_PER_PIXEL);
1474 }
1475 glFinish();
1476 glBindFramebuffer(GL_FRAMEBUFFER, glRenderer->fbo[GBA_GL_FBO_OUTPUT]);
1477 glPixelStorei(GL_PACK_ROW_LENGTH, GBA_VIDEO_HORIZONTAL_PIXELS * glRenderer->scale);
1478 glPixelStorei(GL_PACK_ALIGNMENT, 1);
1479 glReadPixels(0, 0, GBA_VIDEO_HORIZONTAL_PIXELS * glRenderer->scale, GBA_VIDEO_VERTICAL_PIXELS * glRenderer->scale, GL_RGBA, GL_UNSIGNED_BYTE, (void*) glRenderer->temporaryBuffer);
1480 *pixels = glRenderer->temporaryBuffer;
1481}
1482
1483void GBAVideoGLRendererPutPixels(struct GBAVideoRenderer* renderer, size_t stride, const void* pixels) {
1484
1485}
1486
1487static void _enableBg(struct GBAVideoGLRenderer* renderer, int bg, bool active) {
1488 int wasActive = renderer->bg[bg].enabled;
1489 if (!active) {
1490 renderer->bg[bg].enabled = 0;
1491 } else if (!wasActive && active) {
1492 /*if (renderer->nextY == 0 || GBARegisterDISPCNTGetMode(renderer->dispcnt) > 2) {
1493 // TODO: Investigate in more depth how switching background works in different modes
1494 renderer->bg[bg].enabled = 4;
1495 } else {
1496 renderer->bg[bg].enabled = 1;
1497 }*/
1498 renderer->bg[bg].enabled = 4;
1499 }
1500}
1501
1502static void GBAVideoGLRendererUpdateDISPCNT(struct GBAVideoGLRenderer* renderer) {
1503 _enableBg(renderer, 0, GBARegisterDISPCNTGetBg0Enable(renderer->dispcnt));
1504 _enableBg(renderer, 1, GBARegisterDISPCNTGetBg1Enable(renderer->dispcnt));
1505 _enableBg(renderer, 2, GBARegisterDISPCNTGetBg2Enable(renderer->dispcnt));
1506 _enableBg(renderer, 3, GBARegisterDISPCNTGetBg3Enable(renderer->dispcnt));
1507}
1508
1509static void GBAVideoGLRendererWriteBGCNT(struct GBAVideoGLBackground* bg, uint16_t value) {
1510 bg->priority = GBARegisterBGCNTGetPriority(value);
1511 bg->charBase = GBARegisterBGCNTGetCharBase(value) << 13;
1512 bg->mosaic = GBARegisterBGCNTGetMosaic(value);
1513 bg->multipalette = GBARegisterBGCNTGet256Color(value);
1514 bg->screenBase = GBARegisterBGCNTGetScreenBase(value) << 10;
1515 bg->overflow = GBARegisterBGCNTGetOverflow(value);
1516 bg->size = GBARegisterBGCNTGetSize(value);
1517}
1518
1519static void GBAVideoGLRendererWriteBGX_LO(struct GBAVideoGLBackground* bg, uint16_t value) {
1520 bg->refx = (bg->refx & 0xFFFF0000) | value;
1521 bg->affine.sx = bg->refx;
1522}
1523
1524static void GBAVideoGLRendererWriteBGX_HI(struct GBAVideoGLBackground* bg, uint16_t value) {
1525 bg->refx = (bg->refx & 0x0000FFFF) | (value << 16);
1526 bg->refx <<= 4;
1527 bg->refx >>= 4;
1528 bg->affine.sx = bg->refx;
1529}
1530
1531static void GBAVideoGLRendererWriteBGY_LO(struct GBAVideoGLBackground* bg, uint16_t value) {
1532 bg->refy = (bg->refy & 0xFFFF0000) | value;
1533 bg->affine.sy = bg->refy;
1534}
1535
1536static void GBAVideoGLRendererWriteBGY_HI(struct GBAVideoGLBackground* bg, uint16_t value) {
1537 bg->refy = (bg->refy & 0x0000FFFF) | (value << 16);
1538 bg->refy <<= 4;
1539 bg->refy >>= 4;
1540 bg->affine.sy = bg->refy;
1541}
1542
1543static void GBAVideoGLRendererWriteBLDCNT(struct GBAVideoGLRenderer* renderer, uint16_t value) {
1544 renderer->bg[0].target1 = GBARegisterBLDCNTGetTarget1Bg0(value);
1545 renderer->bg[1].target1 = GBARegisterBLDCNTGetTarget1Bg1(value);
1546 renderer->bg[2].target1 = GBARegisterBLDCNTGetTarget1Bg2(value);
1547 renderer->bg[3].target1 = GBARegisterBLDCNTGetTarget1Bg3(value);
1548 renderer->bg[0].target2 = GBARegisterBLDCNTGetTarget2Bg0(value);
1549 renderer->bg[1].target2 = GBARegisterBLDCNTGetTarget2Bg1(value);
1550 renderer->bg[2].target2 = GBARegisterBLDCNTGetTarget2Bg2(value);
1551 renderer->bg[3].target2 = GBARegisterBLDCNTGetTarget2Bg3(value);
1552
1553 renderer->blendEffect = GBARegisterBLDCNTGetEffect(value);
1554 renderer->target1Obj = GBARegisterBLDCNTGetTarget1Obj(value);
1555 renderer->target1Bd = GBARegisterBLDCNTGetTarget1Bd(value);
1556 renderer->target2Obj = GBARegisterBLDCNTGetTarget2Obj(value);
1557 renderer->target2Bd = GBARegisterBLDCNTGetTarget2Bd(value);
1558}
1559
1560void _finalizeLayers(struct GBAVideoGLRenderer* renderer) {
1561 const GLuint* uniforms = renderer->finalizeShader.uniforms;
1562 glBindFramebuffer(GL_FRAMEBUFFER, renderer->fbo[GBA_GL_FBO_OUTPUT]);
1563 glViewport(0, 0, GBA_VIDEO_HORIZONTAL_PIXELS * renderer->scale, GBA_VIDEO_VERTICAL_PIXELS * renderer->scale);
1564 glScissor(0, 0, GBA_VIDEO_HORIZONTAL_PIXELS * renderer->scale, GBA_VIDEO_VERTICAL_PIXELS * renderer->scale);
1565 if (GBARegisterDISPCNTIsForcedBlank(renderer->dispcnt)) {
1566 glClearColor(1.f, 1.f, 1.f, 1.f);
1567 glClear(GL_COLOR_BUFFER_BIT);
1568 } else {
1569 glUseProgram(renderer->finalizeShader.program);
1570 glBindVertexArray(renderer->finalizeShader.vao);
1571 glActiveTexture(GL_TEXTURE0);
1572 glBindTexture(GL_TEXTURE_2D, renderer->layers[GBA_GL_TEX_WINDOW]);
1573 glActiveTexture(GL_TEXTURE0 + 1);
1574 glBindTexture(GL_TEXTURE_2D, renderer->layers[GBA_GL_TEX_OBJ_COLOR]);
1575 glActiveTexture(GL_TEXTURE0 + 2);
1576 glBindTexture(GL_TEXTURE_2D, renderer->layers[GBA_GL_TEX_OBJ_FLAGS]);
1577 glActiveTexture(GL_TEXTURE0 + 3);
1578 glBindTexture(GL_TEXTURE_2D, renderer->bg[0].tex);
1579 glActiveTexture(GL_TEXTURE0 + 4);
1580 glBindTexture(GL_TEXTURE_2D, renderer->bg[0].flags);
1581 glActiveTexture(GL_TEXTURE0 + 5);
1582 glBindTexture(GL_TEXTURE_2D, renderer->bg[1].tex);
1583 glActiveTexture(GL_TEXTURE0 + 6);
1584 glBindTexture(GL_TEXTURE_2D, renderer->bg[1].flags);
1585 glActiveTexture(GL_TEXTURE0 + 7);
1586 glBindTexture(GL_TEXTURE_2D, renderer->bg[2].tex);
1587 glActiveTexture(GL_TEXTURE0 + 8);
1588 glBindTexture(GL_TEXTURE_2D, renderer->bg[2].flags);
1589 glActiveTexture(GL_TEXTURE0 + 9);
1590 glBindTexture(GL_TEXTURE_2D, renderer->bg[3].tex);
1591 glActiveTexture(GL_TEXTURE0 + 10);
1592 glBindTexture(GL_TEXTURE_2D, renderer->bg[3].flags);
1593 glActiveTexture(GL_TEXTURE0 + 11);
1594 glBindTexture(GL_TEXTURE_2D, renderer->layers[GBA_GL_TEX_BACKDROP_COLOR]);
1595 glActiveTexture(GL_TEXTURE0 + 12);
1596 glBindTexture(GL_TEXTURE_2D, renderer->layers[GBA_GL_TEX_BACKDROP_FLAGS]);
1597
1598 glUniform2i(uniforms[GBA_GL_VS_LOC], GBA_VIDEO_VERTICAL_PIXELS, 0);
1599 glUniform2i(uniforms[GBA_GL_VS_MAXPOS], GBA_VIDEO_HORIZONTAL_PIXELS, GBA_VIDEO_VERTICAL_PIXELS);
1600 glUniform1i(uniforms[GBA_GL_FINALIZE_SCALE], renderer->scale);
1601 glUniform1iv(uniforms[GBA_GL_FINALIZE_LAYERS], 5, (GLint[]) { 3, 5, 7, 9, 1 });
1602 glUniform1iv(uniforms[GBA_GL_FINALIZE_FLAGS], 5, (GLint[]) { 4, 6, 8, 10, 2 });
1603 glUniform1i(uniforms[GBA_GL_FINALIZE_WINDOW], 0);
1604 glUniform1i(uniforms[GBA_GL_FINALIZE_WINDOW], 0);
1605 glUniform1i(uniforms[GBA_GL_FINALIZE_BACKDROP], 11);
1606 glUniform1i(uniforms[GBA_GL_FINALIZE_BACKDROPFLAGS], 12);
1607 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1608 }
1609 glBindFramebuffer(GL_FRAMEBUFFER, 0);
1610}
1611
1612void GBAVideoGLRendererDrawSprite(struct GBAVideoGLRenderer* renderer, struct GBAObj* sprite, int y, int spriteY) {
1613 int width = GBAVideoObjSizes[GBAObjAttributesAGetShape(sprite->a) * 4 + GBAObjAttributesBGetSize(sprite->b)][0];
1614 int height = GBAVideoObjSizes[GBAObjAttributesAGetShape(sprite->a) * 4 + GBAObjAttributesBGetSize(sprite->b)][1];
1615 int32_t x = (uint32_t) GBAObjAttributesBGetX(sprite->b) << 23;
1616 x >>= 23;
1617
1618 int align = GBAObjAttributesAIs256Color(sprite->a) && !GBARegisterDISPCNTIsObjCharacterMapping(renderer->dispcnt);
1619 unsigned charBase = (BASE_TILE >> 1) + (GBAObjAttributesCGetTile(sprite->c) & ~align) * 0x10;
1620 int stride = GBARegisterDISPCNTIsObjCharacterMapping(renderer->dispcnt) ? (width >> 3) : (0x20 >> GBAObjAttributesAGet256Color(sprite->a));
1621
1622 if (spriteY + height >= 256) {
1623 spriteY -= 256;
1624 }
1625
1626 int totalWidth = width;
1627 int totalHeight = height;
1628 if (GBAObjAttributesAIsTransformed(sprite->a) && GBAObjAttributesAIsDoubleSize(sprite->a)) {
1629 totalWidth <<= 1;
1630 totalHeight <<= 1;
1631 }
1632
1633 const struct GBAVideoGLShader* shader = &renderer->objShader[GBAObjAttributesAGet256Color(sprite->a)];
1634 const GLuint* uniforms = shader->uniforms;
1635 glBindFramebuffer(GL_FRAMEBUFFER, renderer->fbo[GBA_GL_FBO_OBJ]);
1636 glViewport(x * renderer->scale, spriteY * renderer->scale, totalWidth * renderer->scale, totalHeight * renderer->scale);
1637 glScissor(x * renderer->scale, renderer->firstY * renderer->scale, totalWidth * renderer->scale, (y - renderer->firstY + 1) * renderer->scale);
1638 glUseProgram(shader->program);
1639 glBindVertexArray(shader->vao);
1640 glActiveTexture(GL_TEXTURE0);
1641 glBindTexture(GL_TEXTURE_2D, renderer->vramTex);
1642 glUniform2i(uniforms[GBA_GL_VS_LOC], totalHeight, 0);
1643 glUniform2i(uniforms[GBA_GL_VS_MAXPOS], totalWidth, totalHeight);
1644 glUniform1i(uniforms[GBA_GL_OBJ_VRAM], 0);
1645 glUniform1iv(uniforms[GBA_GL_OBJ_PALETTE], 256, &renderer->shadowPalette[256]);
1646 glUniform1i(uniforms[GBA_GL_OBJ_CHARBASE], charBase);
1647 glUniform1i(uniforms[GBA_GL_OBJ_STRIDE], stride);
1648 glUniform1i(uniforms[GBA_GL_OBJ_LOCALPALETTE], GBAObjAttributesCGetPalette(sprite->c));
1649 glUniform4i(uniforms[GBA_GL_OBJ_INFLAGS], GBAObjAttributesCGetPriority(sprite->c),
1650 (renderer->target1Obj || GBAObjAttributesAGetMode(sprite->a) == OBJ_MODE_SEMITRANSPARENT) | (renderer->target2Obj * 2) | (renderer->blendEffect * 4),
1651 renderer->blda, GBAObjAttributesAGetMode(sprite->a) == OBJ_MODE_SEMITRANSPARENT);
1652 if (GBAObjAttributesAIsTransformed(sprite->a)) {
1653 struct GBAOAMMatrix mat;
1654 LOAD_16(mat.a, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].a);
1655 LOAD_16(mat.b, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].b);
1656 LOAD_16(mat.c, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].c);
1657 LOAD_16(mat.d, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].d);
1658
1659 glUniformMatrix2fv(uniforms[GBA_GL_OBJ_TRANSFORM], 1, GL_FALSE, (GLfloat[]) { mat.a / 256.f, mat.c / 256.f, mat.b / 256.f, mat.d / 256.f });
1660 } else {
1661 int flipX = 1;
1662 int flipY = 1;
1663 if (GBAObjAttributesBIsHFlip(sprite->b)) {
1664 flipX = -1;
1665 }
1666 if (GBAObjAttributesBIsVFlip(sprite->b)) {
1667 flipY = -1;
1668 }
1669 glUniformMatrix2fv(uniforms[GBA_GL_OBJ_TRANSFORM], 1, GL_FALSE, (GLfloat[]) { flipX, 0, 0, flipY });
1670 }
1671 glUniform4i(uniforms[GBA_GL_OBJ_DIMS], width, height, totalWidth, totalHeight);
1672 if (GBAObjAttributesAGetMode(sprite->a) == OBJ_MODE_OBJWIN) {
1673 glDisable(GL_DEPTH_TEST);
1674 int window = renderer->objwin & 0x3F;
1675 glUniform4i(uniforms[GBA_GL_OBJ_OBJWIN], 1, window, renderer->bldb, renderer->bldy);
1676 glDrawBuffers(3, (GLenum[]) { GL_NONE, GL_NONE, GL_COLOR_ATTACHMENT2 });
1677 } else {
1678 glEnable(GL_DEPTH_TEST);
1679 glUniform4i(uniforms[GBA_GL_OBJ_OBJWIN], 0, 0, 0, 0);
1680 glDrawBuffers(2, (GLenum[]) { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 });
1681 }
1682 if (GBAObjAttributesAIsMosaic(sprite->a) && GBAObjAttributesAGetMode(sprite->a) != OBJ_MODE_OBJWIN) {
1683 int mosaicH = GBAMosaicControlGetObjH(renderer->mosaic) + 1;
1684 if (GBAObjAttributesBIsHFlip(sprite->b)) {
1685 mosaicH = -mosaicH;
1686 }
1687 glUniform4i(uniforms[GBA_GL_OBJ_MOSAIC], mosaicH, GBAMosaicControlGetObjV(renderer->mosaic) + 1, x, spriteY);
1688 } else {
1689 glUniform4i(uniforms[GBA_GL_OBJ_MOSAIC], 0, 0, 0, 0);
1690 }
1691 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1692 glDrawBuffers(1, (GLenum[]) { GL_COLOR_ATTACHMENT0 });
1693}
1694
1695void _prepareBackground(struct GBAVideoGLRenderer* renderer, struct GBAVideoGLBackground* background, const GLuint* uniforms) {
1696 glBindFramebuffer(GL_FRAMEBUFFER, background->fbo);
1697 glViewport(0, 0, GBA_VIDEO_HORIZONTAL_PIXELS * renderer->scale, GBA_VIDEO_VERTICAL_PIXELS * renderer->scale);
1698 glActiveTexture(GL_TEXTURE0);
1699 glBindTexture(GL_TEXTURE_2D, renderer->vramTex);
1700 glUniform2i(uniforms[GBA_GL_VS_MAXPOS], GBA_VIDEO_HORIZONTAL_PIXELS, GBA_VIDEO_VERTICAL_PIXELS);
1701 glUniform1i(uniforms[GBA_GL_BG_VRAM], 0);
1702 glUniform1iv(uniforms[GBA_GL_OBJ_PALETTE], 256, renderer->shadowPalette);
1703 if (background->mosaic) {
1704 glUniform2i(uniforms[GBA_GL_BG_MOSAIC], GBAMosaicControlGetBgV(renderer->mosaic) + 1, GBAMosaicControlGetBgH(renderer->mosaic) + 1);
1705 } else {
1706 glUniform2i(uniforms[GBA_GL_BG_MOSAIC], 0, 0);
1707 }
1708 glUniform4i(uniforms[GBA_GL_BG_INFLAGS], background->priority,
1709 background->target1 | (background->target2 * 2) | (renderer->blendEffect * 4),
1710 renderer->blda, 0);
1711 glDrawBuffers(2, (GLenum[]) { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 });
1712}
1713
1714void GBAVideoGLRendererDrawBackgroundMode0(struct GBAVideoGLRenderer* renderer, struct GBAVideoGLBackground* background, int y) {
1715 int inY0 = renderer->firstY + background->y;
1716 int yDiv = (((y + background->y) & ~0xFF) - background->y) & 0xFF;
1717 int inY1 = yDiv + background->y;
1718 int yBase0 = inY0 & 0xFF;
1719 int yBase1 = inY1 & 0xFF;
1720 if (background->size == 2) {
1721 yBase0 += inY0 & 0x100;
1722 yBase1 += inY1 & 0x100;
1723 } else if (background->size == 3) {
1724 yBase0 += (inY0 & 0x100) << 1;
1725 yBase1 += (inY1 & 0x100) << 1;
1726 }
1727
1728 const struct GBAVideoGLShader* shader = &renderer->bgShader[background->multipalette ? 1 : 0];
1729 const GLuint* uniforms = shader->uniforms;
1730 glUseProgram(shader->program);
1731 glBindVertexArray(shader->vao);
1732 _prepareBackground(renderer, background, uniforms);
1733 glUniform1i(uniforms[GBA_GL_BG_SCREENBASE], background->screenBase);
1734 glUniform1i(uniforms[GBA_GL_BG_CHARBASE], background->charBase);
1735 glUniform1i(uniforms[GBA_GL_BG_SIZE], background->size);
1736
1737 if (yDiv > renderer->firstY) {
1738 int end = yDiv - 1;
1739 if (end > y) {
1740 end = y;
1741 }
1742 glScissor(0, renderer->firstY * renderer->scale, GBA_VIDEO_HORIZONTAL_PIXELS * renderer->scale, (end - renderer->firstY + 1) * renderer->scale);
1743 glUniform2i(uniforms[GBA_GL_VS_LOC], end - renderer->firstY + 1, renderer->firstY);
1744 glUniform2i(uniforms[GBA_GL_BG_OFFSET], background->x, yBase0 - renderer->firstY);
1745 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1746 }
1747
1748 if (y >= yDiv) {
1749 int start = yDiv;
1750 if (yDiv < renderer->firstY) {
1751 start = renderer->firstY;
1752 }
1753 glScissor(0, start * renderer->scale, GBA_VIDEO_HORIZONTAL_PIXELS * renderer->scale, (y - start + 1) * renderer->scale);
1754 glUniform2i(uniforms[GBA_GL_VS_LOC], y - start + 1, start);
1755 glUniform2i(uniforms[GBA_GL_BG_OFFSET], background->x, yBase1 - yDiv);
1756 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1757 }
1758
1759 glDrawBuffers(1, (GLenum[]) { GL_COLOR_ATTACHMENT0 });
1760}
1761
1762void _prepareTransform(struct GBAVideoGLRenderer* renderer, struct GBAVideoGLBackground* background, const GLuint* uniforms, int y) {
1763 glScissor(0, renderer->firstY * renderer->scale, GBA_VIDEO_HORIZONTAL_PIXELS * renderer->scale, renderer->scale * (y - renderer->firstY + 1));
1764 glUniform2i(uniforms[GBA_GL_VS_LOC], y - renderer->firstY + 1, renderer->firstY);
1765 glUniform2i(uniforms[GBA_GL_BG_RANGE], renderer->firstAffine, y);
1766
1767 glActiveTexture(GL_TEXTURE0 + 2);
1768 glBindTexture(GL_TEXTURE_2D, renderer->layers[GBA_GL_TEX_AFFINE_2 + background->index - 2]);
1769 glUniform1i(uniforms[GBA_GL_BG_TRANSFORM], 2);
1770 _prepareBackground(renderer, background, uniforms);
1771}
1772
1773void GBAVideoGLRendererDrawBackgroundMode2(struct GBAVideoGLRenderer* renderer, struct GBAVideoGLBackground* background, int y) {
1774 const struct GBAVideoGLShader* shader = &renderer->bgShader[background->overflow ? 2 : 3];
1775 const GLuint* uniforms = shader->uniforms;
1776 glUseProgram(shader->program);
1777 glBindVertexArray(shader->vao);
1778 _prepareTransform(renderer, background, uniforms, y);
1779 glUniform1i(uniforms[GBA_GL_BG_SCREENBASE], background->screenBase);
1780 glUniform1i(uniforms[GBA_GL_BG_CHARBASE], background->charBase);
1781 glUniform1i(uniforms[GBA_GL_BG_SIZE], background->size);
1782 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1783 glDrawBuffers(1, (GLenum[]) { GL_COLOR_ATTACHMENT0 });
1784}
1785
1786void GBAVideoGLRendererDrawBackgroundMode3(struct GBAVideoGLRenderer* renderer, struct GBAVideoGLBackground* background, int y) {
1787 const struct GBAVideoGLShader* shader = &renderer->bgShader[5];
1788 const GLuint* uniforms = shader->uniforms;
1789 glBindFramebuffer(GL_FRAMEBUFFER, background->fbo);
1790 glViewport(0, 0, GBA_VIDEO_HORIZONTAL_PIXELS * renderer->scale, GBA_VIDEO_VERTICAL_PIXELS * renderer->scale);
1791 glUseProgram(shader->program);
1792 glBindVertexArray(shader->vao);
1793 _prepareTransform(renderer, background, uniforms, y);
1794 glUniform1i(uniforms[GBA_GL_BG_CHARBASE], 0);
1795 glUniform2i(uniforms[GBA_GL_BG_SIZE], GBA_VIDEO_HORIZONTAL_PIXELS, GBA_VIDEO_VERTICAL_PIXELS);
1796 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1797 glDrawBuffers(1, (GLenum[]) { GL_COLOR_ATTACHMENT0 });
1798}
1799
1800void GBAVideoGLRendererDrawBackgroundMode4(struct GBAVideoGLRenderer* renderer, struct GBAVideoGLBackground* background, int y) {
1801 const struct GBAVideoGLShader* shader = &renderer->bgShader[4];
1802 const GLuint* uniforms = shader->uniforms;
1803 glBindFramebuffer(GL_FRAMEBUFFER, background->fbo);
1804 glViewport(0, 0, GBA_VIDEO_HORIZONTAL_PIXELS * renderer->scale, GBA_VIDEO_VERTICAL_PIXELS * renderer->scale);
1805 glUseProgram(shader->program);
1806 glBindVertexArray(shader->vao);
1807 _prepareTransform(renderer, background, uniforms, y);
1808 glUniform1i(uniforms[GBA_GL_BG_CHARBASE], GBARegisterDISPCNTIsFrameSelect(renderer->dispcnt) ? 0xA000 : 0);
1809 glUniform2i(uniforms[GBA_GL_BG_SIZE], GBA_VIDEO_HORIZONTAL_PIXELS, GBA_VIDEO_VERTICAL_PIXELS);
1810 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1811 glDrawBuffers(1, (GLenum[]) { GL_COLOR_ATTACHMENT0 });
1812}
1813
1814void GBAVideoGLRendererDrawBackgroundMode5(struct GBAVideoGLRenderer* renderer, struct GBAVideoGLBackground* background, int y) {
1815 const struct GBAVideoGLShader* shader = &renderer->bgShader[5];
1816 const GLuint* uniforms = shader->uniforms;
1817 glBindFramebuffer(GL_FRAMEBUFFER, background->fbo);
1818 glViewport(0, 0, GBA_VIDEO_HORIZONTAL_PIXELS * renderer->scale, GBA_VIDEO_VERTICAL_PIXELS * renderer->scale);
1819 glUseProgram(shader->program);
1820 glBindVertexArray(shader->vao);
1821 _prepareTransform(renderer, background, uniforms, y);
1822 glUniform1i(uniforms[GBA_GL_BG_CHARBASE], GBARegisterDISPCNTIsFrameSelect(renderer->dispcnt) ? 0x5000 : 0);
1823 glUniform2i(uniforms[GBA_GL_BG_SIZE], 160, 128);
1824 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1825 glDrawBuffers(1, (GLenum[]) { GL_COLOR_ATTACHMENT0 });
1826}
1827
1828void GBAVideoGLRendererDrawWindow(struct GBAVideoGLRenderer* renderer, int y) {
1829 const struct GBAVideoGLShader* shader = &renderer->windowShader;
1830 const GLuint* uniforms = shader->uniforms;
1831 glBindFramebuffer(GL_FRAMEBUFFER, renderer->fbo[GBA_GL_FBO_WINDOW]);
1832 glViewport(0, 0, GBA_VIDEO_HORIZONTAL_PIXELS * renderer->scale, GBA_VIDEO_VERTICAL_PIXELS * renderer->scale);
1833 glScissor(0, renderer->firstY * renderer->scale, GBA_VIDEO_HORIZONTAL_PIXELS * renderer->scale, renderer->scale * (y - renderer->firstY + 1));
1834 glUseProgram(shader->program);
1835 glBindVertexArray(shader->vao);
1836 glUniform2i(uniforms[GBA_GL_VS_LOC], y - renderer->firstY + 1, renderer->firstY);
1837 glUniform2i(uniforms[GBA_GL_VS_MAXPOS], GBA_VIDEO_HORIZONTAL_PIXELS, GBA_VIDEO_VERTICAL_PIXELS);
1838 glUniform1i(uniforms[GBA_GL_WIN_DISPCNT], renderer->dispcnt >> 8);
1839 glUniform2i(uniforms[GBA_GL_WIN_BLEND], renderer->bldb, renderer->bldy);
1840 glUniform3i(uniforms[GBA_GL_WIN_FLAGS], renderer->winN[0].control, renderer->winN[1].control, renderer->winout);
1841 glUniform4iv(uniforms[GBA_GL_WIN_WIN0], GBA_VIDEO_VERTICAL_PIXELS, renderer->winNHistory[0]);
1842 glUniform4iv(uniforms[GBA_GL_WIN_WIN1], GBA_VIDEO_VERTICAL_PIXELS, renderer->winNHistory[1]);
1843 glDrawBuffers(1, (GLenum[]) { GL_COLOR_ATTACHMENT0 });
1844 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1845}
1846
1847#endif