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