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 = float(int(incoord.x) % mosaic.x);\n"
272 " }\n"
273 " if (mosaic.y > 1) {\n"
274 " 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 = float(int(incoord.x) % mosaic.x);\n"
325 " }\n"
326 " if (mosaic.y > 1) {\n"
327 " 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 = float(int(incoord.x) % mosaic.x);\n"
390 " }\n"
391 " if (mosaic.y > 1) {\n"
392 " 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->paletteDirty = true;
935 glRenderer->vramDirty = 0xFFFFFF;
936 glRenderer->firstAffine = -1;
937 glRenderer->firstY = -1;
938 glRenderer->dispcnt = 0x0080;
939 glRenderer->mosaic = 0;
940 memset(glRenderer->shadowRegs, 0, sizeof(glRenderer->shadowRegs));
941 glRenderer->regsDirty = 0xFFFFFFFFFFFEULL;
942}
943
944void GBAVideoGLRendererWriteVRAM(struct GBAVideoRenderer* renderer, uint32_t address) {
945 struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;
946 glRenderer->vramDirty |= 1 << (address >> 12);
947}
948
949void GBAVideoGLRendererWriteOAM(struct GBAVideoRenderer* renderer, uint32_t oam) {
950 UNUSED(oam);
951 struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;
952 glRenderer->oamDirty = true;
953}
954
955void GBAVideoGLRendererWritePalette(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
956 struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;
957 UNUSED(address);
958 UNUSED(value);
959 glRenderer->paletteDirty = true;
960}
961
962uint16_t GBAVideoGLRendererWriteVideoRegister(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
963 struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;
964 if (renderer->cache) {
965 GBAVideoCacheWriteVideoRegister(renderer->cache, address, value);
966 }
967
968 bool dirty = false;
969 switch (address) {
970 case REG_DISPCNT:
971 value &= 0xFFF7;
972 dirty = true;
973 break;
974 case REG_BG0CNT:
975 case REG_BG1CNT:
976 value &= 0xDFFF;
977 dirty = true;
978 break;
979 case REG_BG0HOFS:
980 value &= 0x01FF;
981 glRenderer->bg[0].x = value;
982 dirty = false;
983 break;
984 case REG_BG0VOFS:
985 value &= 0x01FF;
986 glRenderer->bg[0].y = value;
987 dirty = false;
988 break;
989 case REG_BG1HOFS:
990 value &= 0x01FF;
991 glRenderer->bg[1].x = value;
992 dirty = false;
993 break;
994 case REG_BG1VOFS:
995 value &= 0x01FF;
996 glRenderer->bg[1].y = value;
997 dirty = false;
998 break;
999 case REG_BG2HOFS:
1000 value &= 0x01FF;
1001 glRenderer->bg[2].x = value;
1002 dirty = false;
1003 break;
1004 case REG_BG2VOFS:
1005 value &= 0x01FF;
1006 glRenderer->bg[2].y = value;
1007 dirty = false;
1008 break;
1009 case REG_BG3HOFS:
1010 value &= 0x01FF;
1011 glRenderer->bg[3].x = value;
1012 dirty = false;
1013 break;
1014 case REG_BG3VOFS:
1015 value &= 0x01FF;
1016 glRenderer->bg[3].y = value;
1017 dirty = false;
1018 break;
1019 case REG_BG2PA:
1020 glRenderer->bg[2].affine.dx = value;
1021 break;
1022 case REG_BG2PB:
1023 glRenderer->bg[2].affine.dmx = value;
1024 break;
1025 case REG_BG2PC:
1026 glRenderer->bg[2].affine.dy = value;
1027 break;
1028 case REG_BG2PD:
1029 glRenderer->bg[2].affine.dmy = value;
1030 break;
1031 case REG_BG2X_LO:
1032 GBAVideoGLRendererWriteBGX_LO(&glRenderer->bg[2], value);
1033 break;
1034 case REG_BG2X_HI:
1035 GBAVideoGLRendererWriteBGX_HI(&glRenderer->bg[2], value);
1036 break;
1037 case REG_BG2Y_LO:
1038 GBAVideoGLRendererWriteBGY_LO(&glRenderer->bg[2], value);
1039 break;
1040 case REG_BG2Y_HI:
1041 GBAVideoGLRendererWriteBGY_HI(&glRenderer->bg[2], value);
1042 break;
1043 case REG_BG3PA:
1044 glRenderer->bg[3].affine.dx = value;
1045 break;
1046 case REG_BG3PB:
1047 glRenderer->bg[3].affine.dmx = value;
1048 break;
1049 case REG_BG3PC:
1050 glRenderer->bg[3].affine.dy = value;
1051 break;
1052 case REG_BG3PD:
1053 glRenderer->bg[3].affine.dmy = value;
1054 break;
1055 case REG_BG3X_LO:
1056 GBAVideoGLRendererWriteBGX_LO(&glRenderer->bg[3], value);
1057 break;
1058 case REG_BG3X_HI:
1059 GBAVideoGLRendererWriteBGX_HI(&glRenderer->bg[3], value);
1060 break;
1061 case REG_BG3Y_LO:
1062 GBAVideoGLRendererWriteBGY_LO(&glRenderer->bg[3], value);
1063 break;
1064 case REG_BG3Y_HI:
1065 GBAVideoGLRendererWriteBGY_HI(&glRenderer->bg[3], value);
1066 break;
1067 case REG_BLDALPHA:
1068 value &= 0x1F1F;
1069 dirty = true;
1070 break;
1071 case REG_BLDY:
1072 value &= 0x1F;
1073 if (value > 0x10) {
1074 value = 0x10;
1075 }
1076 dirty = true;
1077 break;
1078 case REG_WIN0H:
1079 glRenderer->winN[0].h.end = value;
1080 glRenderer->winN[0].h.start = value >> 8;
1081 if (glRenderer->winN[0].h.start > GBA_VIDEO_HORIZONTAL_PIXELS && glRenderer->winN[0].h.start > glRenderer->winN[0].h.end) {
1082 glRenderer->winN[0].h.start = 0;
1083 }
1084 if (glRenderer->winN[0].h.end > GBA_VIDEO_HORIZONTAL_PIXELS) {
1085 glRenderer->winN[0].h.end = GBA_VIDEO_HORIZONTAL_PIXELS;
1086 if (glRenderer->winN[0].h.start > GBA_VIDEO_HORIZONTAL_PIXELS) {
1087 glRenderer->winN[0].h.start = GBA_VIDEO_HORIZONTAL_PIXELS;
1088 }
1089 }
1090 break;
1091 case REG_WIN1H:
1092 glRenderer->winN[1].h.end = value;
1093 glRenderer->winN[1].h.start = value >> 8;
1094 if (glRenderer->winN[1].h.start > GBA_VIDEO_HORIZONTAL_PIXELS && glRenderer->winN[1].h.start > glRenderer->winN[1].h.end) {
1095 glRenderer->winN[1].h.start = 0;
1096 }
1097 if (glRenderer->winN[1].h.end > GBA_VIDEO_HORIZONTAL_PIXELS) {
1098 glRenderer->winN[1].h.end = GBA_VIDEO_HORIZONTAL_PIXELS;
1099 if (glRenderer->winN[1].h.start > GBA_VIDEO_HORIZONTAL_PIXELS) {
1100 glRenderer->winN[1].h.start = GBA_VIDEO_HORIZONTAL_PIXELS;
1101 }
1102 }
1103 break;
1104 case REG_WIN0V:
1105 glRenderer->winN[0].v.end = value;
1106 glRenderer->winN[0].v.start = value >> 8;
1107 if (glRenderer->winN[0].v.start > GBA_VIDEO_VERTICAL_PIXELS && glRenderer->winN[0].v.start > glRenderer->winN[0].v.end) {
1108 glRenderer->winN[0].v.start = 0;
1109 }
1110 if (glRenderer->winN[0].v.end > GBA_VIDEO_VERTICAL_PIXELS) {
1111 glRenderer->winN[0].v.end = GBA_VIDEO_VERTICAL_PIXELS;
1112 if (glRenderer->winN[0].v.start > GBA_VIDEO_VERTICAL_PIXELS) {
1113 glRenderer->winN[0].v.start = GBA_VIDEO_VERTICAL_PIXELS;
1114 }
1115 }
1116 break;
1117 case REG_WIN1V:
1118 glRenderer->winN[1].v.end = value;
1119 glRenderer->winN[1].v.start = value >> 8;
1120 if (glRenderer->winN[1].v.start > GBA_VIDEO_VERTICAL_PIXELS && glRenderer->winN[1].v.start > glRenderer->winN[1].v.end) {
1121 glRenderer->winN[1].v.start = 0;
1122 }
1123 if (glRenderer->winN[1].v.end > GBA_VIDEO_VERTICAL_PIXELS) {
1124 glRenderer->winN[1].v.end = GBA_VIDEO_VERTICAL_PIXELS;
1125 if (glRenderer->winN[1].v.start > GBA_VIDEO_VERTICAL_PIXELS) {
1126 glRenderer->winN[1].v.start = GBA_VIDEO_VERTICAL_PIXELS;
1127 }
1128 }
1129 break;
1130 case REG_WININ:
1131 case REG_WINOUT:
1132 value &= 0x3F3F;
1133 dirty = true;
1134 break;
1135 default:
1136 dirty = true;
1137 break;
1138 }
1139 if (glRenderer->shadowRegs[address >> 1] == value) {
1140 dirty = false;
1141 } else {
1142 glRenderer->shadowRegs[address >> 1] = value;
1143 }
1144 if (dirty) {
1145 glRenderer->regsDirty |= 1ULL << (address >> 1);
1146 }
1147 return value;
1148}
1149
1150void _cleanRegister(struct GBAVideoGLRenderer* glRenderer, int address, uint16_t value) {
1151 switch (address) {
1152 case REG_DISPCNT:
1153 glRenderer->dispcnt = value;
1154 GBAVideoGLRendererUpdateDISPCNT(glRenderer);
1155 break;
1156 case REG_BG0CNT:
1157 GBAVideoGLRendererWriteBGCNT(&glRenderer->bg[0], value);
1158 break;
1159 case REG_BG1CNT:
1160 GBAVideoGLRendererWriteBGCNT(&glRenderer->bg[1], value);
1161 break;
1162 case REG_BG2CNT:
1163 GBAVideoGLRendererWriteBGCNT(&glRenderer->bg[2], value);
1164 break;
1165 case REG_BG3CNT:
1166 GBAVideoGLRendererWriteBGCNT(&glRenderer->bg[3], value);
1167 break;
1168 case REG_BLDCNT:
1169 GBAVideoGLRendererWriteBLDCNT(glRenderer, value);
1170 break;
1171 case REG_BLDALPHA:
1172 glRenderer->blda = value & 0x1F;
1173 if (glRenderer->blda > 0x10) {
1174 glRenderer->blda = 0x10;
1175 }
1176 glRenderer->bldb = (value >> 8) & 0x1F;
1177 if (glRenderer->bldb > 0x10) {
1178 glRenderer->bldb = 0x10;
1179 }
1180 break;
1181 case REG_BLDY:
1182 glRenderer->bldy = value;
1183 break;
1184 case REG_WININ:
1185 glRenderer->winN[0].control = value;
1186 glRenderer->winN[1].control = value >> 8;
1187 break;
1188 case REG_WINOUT:
1189 glRenderer->winout = value;
1190 glRenderer->objwin = value >> 8;
1191 break;
1192 case REG_MOSAIC:
1193 glRenderer->mosaic = value;
1194 break;
1195 default:
1196 break;
1197 }
1198}
1199
1200static bool _dirtyMode0(struct GBAVideoGLRenderer* renderer, struct GBAVideoGLBackground* background, int y) {
1201 UNUSED(y);
1202 if (!background->enabled) {
1203 return false;
1204 }
1205 unsigned screenBase = background->screenBase >> 11; // Lops off one extra bit
1206 unsigned screenMask = (7 << screenBase) & 0xFFFF; // Technically overzealous
1207 if (renderer->vramDirty & screenMask) {
1208 return true;
1209 }
1210 unsigned charBase = background->charBase >> 11;
1211 unsigned charMask = (0xFFFF << charBase) & 0xFFFF;
1212 if (renderer->vramDirty & charMask) {
1213 return true;
1214 }
1215 return false;
1216}
1217
1218static bool _dirtyMode2(struct GBAVideoGLRenderer* renderer, struct GBAVideoGLBackground* background, int y) {
1219 UNUSED(y);
1220 if (!background->enabled) {
1221 return false;
1222 }
1223 unsigned screenBase = background->screenBase >> 11; // Lops off one extra bit
1224 unsigned screenMask = (0xF << screenBase) & 0xFFFF;
1225 if (renderer->vramDirty & screenMask) {
1226 return true;
1227 }
1228 unsigned charBase = background->charBase >> 11;
1229 unsigned charMask = (0x3FFF << charBase) & 0xFFFF;
1230 if (renderer->vramDirty & charMask) {
1231 return true;
1232 }
1233 return false;
1234}
1235
1236static bool _dirtyMode3(struct GBAVideoGLRenderer* renderer, struct GBAVideoGLBackground* background, int y) {
1237 UNUSED(y);
1238 if (!background->enabled) {
1239 return false;
1240 }
1241 if (renderer->vramDirty & 0xFFFFF) {
1242 return true;
1243 }
1244 return false;
1245}
1246
1247static bool _dirtyMode45(struct GBAVideoGLRenderer* renderer, struct GBAVideoGLBackground* background, int y) {
1248 UNUSED(y);
1249 if (!background->enabled) {
1250 return false;
1251 }
1252 int start = GBARegisterDISPCNTIsFrameSelect(renderer->dispcnt) ? 5 : 0;
1253 int mask = 0x3FF << start;
1254 if (renderer->vramDirty & mask) {
1255 return true;
1256 }
1257 return false;
1258}
1259
1260static bool _needsVramUpload(struct GBAVideoGLRenderer* renderer, int y) {
1261 if (!renderer->vramDirty) {
1262 return false;
1263 }
1264 if (y == 0) {
1265 return true;
1266 }
1267
1268 if (GBARegisterDISPCNTIsObjEnable(renderer->dispcnt) && renderer->vramDirty & 0xFF0000) {
1269 return true;
1270 }
1271
1272 bool dirty = false;
1273 switch (GBARegisterDISPCNTGetMode(renderer->dispcnt)) {
1274 case 0:
1275 dirty = dirty || _dirtyMode0(renderer, &renderer->bg[0], y);
1276 dirty = dirty || _dirtyMode0(renderer, &renderer->bg[1], y);
1277 dirty = dirty || _dirtyMode0(renderer, &renderer->bg[2], y);
1278 dirty = dirty || _dirtyMode0(renderer, &renderer->bg[3], y);
1279 break;
1280 case 1:
1281 dirty = dirty || _dirtyMode0(renderer, &renderer->bg[0], y);
1282 dirty = dirty || _dirtyMode0(renderer, &renderer->bg[1], y);
1283 dirty = dirty || _dirtyMode2(renderer, &renderer->bg[2], y);
1284 break;
1285 case 2:
1286 dirty = dirty || _dirtyMode2(renderer, &renderer->bg[2], y);
1287 dirty = dirty || _dirtyMode2(renderer, &renderer->bg[3], y);
1288 break;
1289 case 3:
1290 dirty = _dirtyMode3(renderer, &renderer->bg[2], y);
1291 break;
1292 case 4:
1293 dirty = _dirtyMode45(renderer, &renderer->bg[2], y);
1294 break;
1295 case 5:
1296 dirty = _dirtyMode45(renderer, &renderer->bg[2], y);
1297 break;
1298 }
1299 return dirty;
1300}
1301
1302void GBAVideoGLRendererDrawScanline(struct GBAVideoRenderer* renderer, int y) {
1303 struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;
1304
1305 if (GBARegisterDISPCNTGetMode(glRenderer->dispcnt) != 0) {
1306 if (glRenderer->firstAffine < 0) {
1307 glRenderer->firstAffine = y;
1308 }
1309 } else {
1310 glRenderer->firstAffine = -1;
1311 }
1312
1313 if (glRenderer->paletteDirty || _needsVramUpload(glRenderer, y) || glRenderer->oamDirty || glRenderer->regsDirty) {
1314 if (glRenderer->firstY >= 0) {
1315 _drawScanlines(glRenderer, y - 1);
1316 glBindVertexArray(0);
1317 }
1318 }
1319 if (glRenderer->firstY < 0) {
1320 glRenderer->firstY = y;
1321 }
1322
1323 int i;
1324 for (i = 0; i < 0x30; ++i) {
1325 if (!(glRenderer->regsDirty & (1ULL << i))) {
1326 continue;
1327 }
1328 _cleanRegister(glRenderer, i << 1, glRenderer->shadowRegs[i]);
1329 }
1330 glRenderer->regsDirty = 0;
1331
1332 glRenderer->winNHistory[0][y * 4 + 0] = glRenderer->winN[0].h.start;
1333 glRenderer->winNHistory[0][y * 4 + 1] = glRenderer->winN[0].h.end;
1334 glRenderer->winNHistory[0][y * 4 + 2] = glRenderer->winN[0].v.start;
1335 glRenderer->winNHistory[0][y * 4 + 3] = glRenderer->winN[0].v.end;
1336 glRenderer->winNHistory[1][y * 4 + 0] = glRenderer->winN[1].h.start;
1337 glRenderer->winNHistory[1][y * 4 + 1] = glRenderer->winN[1].h.end;
1338 glRenderer->winNHistory[1][y * 4 + 2] = glRenderer->winN[1].v.start;
1339 glRenderer->winNHistory[1][y * 4 + 3] = glRenderer->winN[1].v.end;
1340
1341 glRenderer->bg[0].scanlineOffset[y] = glRenderer->bg[0].x;
1342 glRenderer->bg[0].scanlineOffset[y] |= glRenderer->bg[0].y << 12;
1343 glRenderer->bg[1].scanlineOffset[y] = glRenderer->bg[1].x;
1344 glRenderer->bg[1].scanlineOffset[y] |= glRenderer->bg[1].y << 12;
1345 glRenderer->bg[2].scanlineOffset[y] = glRenderer->bg[2].x;
1346 glRenderer->bg[2].scanlineOffset[y] |= glRenderer->bg[2].y << 12;
1347 glRenderer->bg[2].scanlineAffine[y * 4] = glRenderer->bg[2].affine.dx;
1348 glRenderer->bg[2].scanlineAffine[y * 4 + 1] = glRenderer->bg[2].affine.dy;
1349 glRenderer->bg[2].scanlineAffine[y * 4 + 2] = glRenderer->bg[2].affine.sx;
1350 glRenderer->bg[2].scanlineAffine[y * 4 + 3] = glRenderer->bg[2].affine.sy;
1351 glRenderer->bg[3].scanlineOffset[y] = glRenderer->bg[3].x;
1352 glRenderer->bg[3].scanlineOffset[y] |= glRenderer->bg[3].y << 12;
1353 glRenderer->bg[3].scanlineAffine[y * 4] = glRenderer->bg[3].affine.dx;
1354 glRenderer->bg[3].scanlineAffine[y * 4 + 1] = glRenderer->bg[3].affine.dy;
1355 glRenderer->bg[3].scanlineAffine[y * 4 + 2] = glRenderer->bg[3].affine.sx;
1356 glRenderer->bg[3].scanlineAffine[y * 4 + 3] = glRenderer->bg[3].affine.sy;
1357
1358 if (glRenderer->paletteDirty) {
1359 for (i = 0; i < 512; ++i) {
1360 glRenderer->shadowPalette[i] = glRenderer->d.palette[i];
1361 }
1362 glRenderer->paletteDirty = false;
1363 }
1364
1365 if (_needsVramUpload(glRenderer, y)) {
1366 int first = -1;
1367 glBindTexture(GL_TEXTURE_2D, glRenderer->vramTex);
1368 for (i = 0; i < 25; ++i) {
1369 if (!(glRenderer->vramDirty & (1 << i))) {
1370 if (first >= 0) {
1371 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]);
1372 first = -1;
1373 }
1374 } else if (first < 0) {
1375 first = i;
1376 }
1377 }
1378 glRenderer->vramDirty = 0;
1379 }
1380
1381 if (glRenderer->oamDirty) {
1382 glRenderer->oamMax = GBAVideoRendererCleanOAM(glRenderer->d.oam->obj, glRenderer->sprites, 0);
1383 glRenderer->oamDirty = false;
1384 }
1385
1386 if (y == 0) {
1387 glDisable(GL_SCISSOR_TEST);
1388 glClearColor(0, 0, 0, 0);
1389#ifdef BUILD_GLES3
1390 glClearDepthf(1.f);
1391#else
1392 glClearDepth(1);
1393#endif
1394 glClearStencil(0);
1395 glBindFramebuffer(GL_FRAMEBUFFER, glRenderer->fbo[GBA_GL_FBO_OBJ]);
1396 glDrawBuffers(2, (GLenum[]) { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 });
1397 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
1398
1399 for (i = 0; i < 4; ++i) {
1400 glBindFramebuffer(GL_FRAMEBUFFER, glRenderer->bg[i].fbo);
1401 glDrawBuffers(2, (GLenum[]) { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 });
1402 glClear(GL_COLOR_BUFFER_BIT);
1403 }
1404 }
1405
1406 if (GBARegisterDISPCNTGetMode(glRenderer->dispcnt) != 0) {
1407 glRenderer->bg[2].affine.sx += glRenderer->bg[2].affine.dmx;
1408 glRenderer->bg[2].affine.sy += glRenderer->bg[2].affine.dmy;
1409 glRenderer->bg[3].affine.sx += glRenderer->bg[3].affine.dmx;
1410 glRenderer->bg[3].affine.sy += glRenderer->bg[3].affine.dmy;
1411 }
1412}
1413
1414void _drawScanlines(struct GBAVideoGLRenderer* glRenderer, int y) {
1415 glEnable(GL_SCISSOR_TEST);
1416
1417 uint32_t backdrop = M_RGB5_TO_RGB8(glRenderer->shadowPalette[0]);
1418 glViewport(0, 0, 1, GBA_VIDEO_VERTICAL_PIXELS);
1419 glScissor(0, glRenderer->firstY, 1, y - glRenderer->firstY + 1);
1420 glBindFramebuffer(GL_FRAMEBUFFER, glRenderer->fbo[GBA_GL_FBO_BACKDROP]);
1421 glDrawBuffers(2, (GLenum[]) { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 });
1422 glClearBufferfv(GL_COLOR, 0, (GLfloat[]) { ((backdrop >> 16) & 0xF8) / 248., ((backdrop >> 8) & 0xF8) / 248., (backdrop & 0xF8) / 248., 1.f });
1423 glClearBufferiv(GL_COLOR, 1, (GLint[]) { 32, glRenderer->target1Bd | (glRenderer->target2Bd * 2) | (glRenderer->blendEffect * 4), glRenderer->blda, 0 });
1424 glDrawBuffers(1, (GLenum[]) { GL_COLOR_ATTACHMENT0 });
1425
1426 GBAVideoGLRendererDrawWindow(glRenderer, y);
1427 if (GBARegisterDISPCNTIsObjEnable(glRenderer->dispcnt) && !glRenderer->d.disableOBJ) {
1428 int i;
1429 glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
1430 glEnable(GL_STENCIL_TEST);
1431 glDepthFunc(GL_LESS);
1432 for (i = 0; i < glRenderer->oamMax; ++i) {
1433 struct GBAVideoRendererSprite* sprite = &glRenderer->sprites[i];
1434 if ((y < sprite->y && (sprite->endY - 256 < 0 || glRenderer->firstY >= sprite->endY - 256)) || glRenderer->firstY >= sprite->endY) {
1435 continue;
1436 }
1437
1438 GBAVideoGLRendererDrawSprite(glRenderer, &sprite->obj, y, sprite->y);
1439 }
1440 glDisable(GL_DEPTH_TEST);
1441 glDisable(GL_STENCIL_TEST);
1442 }
1443
1444 if (TEST_LAYER_ENABLED(0) && GBARegisterDISPCNTGetMode(glRenderer->dispcnt) < 2) {
1445 GBAVideoGLRendererDrawBackgroundMode0(glRenderer, &glRenderer->bg[0], y);
1446 }
1447 if (TEST_LAYER_ENABLED(1) && GBARegisterDISPCNTGetMode(glRenderer->dispcnt) < 2) {
1448 GBAVideoGLRendererDrawBackgroundMode0(glRenderer, &glRenderer->bg[1], y);
1449 }
1450 if (TEST_LAYER_ENABLED(2)) {
1451 switch (GBARegisterDISPCNTGetMode(glRenderer->dispcnt)) {
1452 case 0:
1453 GBAVideoGLRendererDrawBackgroundMode0(glRenderer, &glRenderer->bg[2], y);
1454 break;
1455 case 1:
1456 case 2:
1457 GBAVideoGLRendererDrawBackgroundMode2(glRenderer, &glRenderer->bg[2], y);
1458 break;
1459 case 3:
1460 GBAVideoGLRendererDrawBackgroundMode3(glRenderer, &glRenderer->bg[2], y);
1461 break;
1462 case 4:
1463 GBAVideoGLRendererDrawBackgroundMode4(glRenderer, &glRenderer->bg[2], y);
1464 break;
1465 case 5:
1466 GBAVideoGLRendererDrawBackgroundMode5(glRenderer, &glRenderer->bg[2], y);
1467 break;
1468 }
1469 }
1470 if (TEST_LAYER_ENABLED(3)) {
1471 switch (GBARegisterDISPCNTGetMode(glRenderer->dispcnt)) {
1472 case 0:
1473 GBAVideoGLRendererDrawBackgroundMode0(glRenderer, &glRenderer->bg[3], y);
1474 break;
1475 case 2:
1476 GBAVideoGLRendererDrawBackgroundMode2(glRenderer, &glRenderer->bg[3], y);
1477 break;
1478 }
1479 }
1480 glRenderer->firstY = -1;
1481}
1482
1483void GBAVideoGLRendererFinishFrame(struct GBAVideoRenderer* renderer) {
1484 struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;
1485 _drawScanlines(glRenderer, GBA_VIDEO_VERTICAL_PIXELS - 1);
1486 _finalizeLayers(glRenderer);
1487 glDisable(GL_SCISSOR_TEST);
1488 glBindVertexArray(0);
1489 glRenderer->firstAffine = -1;
1490 glRenderer->firstY = -1;
1491 glRenderer->bg[2].affine.sx = glRenderer->bg[2].refx;
1492 glRenderer->bg[2].affine.sy = glRenderer->bg[2].refy;
1493 glRenderer->bg[3].affine.sx = glRenderer->bg[3].refx;
1494 glRenderer->bg[3].affine.sy = glRenderer->bg[3].refy;
1495}
1496
1497void GBAVideoGLRendererGetPixels(struct GBAVideoRenderer* renderer, size_t* stride, const void** pixels) {
1498 struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;
1499 *stride = GBA_VIDEO_HORIZONTAL_PIXELS * glRenderer->scale;
1500 if (!glRenderer->temporaryBuffer) {
1501 glRenderer->temporaryBuffer = anonymousMemoryMap(GBA_VIDEO_HORIZONTAL_PIXELS * GBA_VIDEO_VERTICAL_PIXELS * glRenderer->scale * glRenderer->scale * BYTES_PER_PIXEL);
1502 }
1503 glFinish();
1504 glBindFramebuffer(GL_FRAMEBUFFER, glRenderer->fbo[GBA_GL_FBO_OUTPUT]);
1505 glPixelStorei(GL_PACK_ROW_LENGTH, GBA_VIDEO_HORIZONTAL_PIXELS * glRenderer->scale);
1506 glPixelStorei(GL_PACK_ALIGNMENT, 1);
1507 glReadPixels(0, 0, GBA_VIDEO_HORIZONTAL_PIXELS * glRenderer->scale, GBA_VIDEO_VERTICAL_PIXELS * glRenderer->scale, GL_RGBA, GL_UNSIGNED_BYTE, (void*) glRenderer->temporaryBuffer);
1508 *pixels = glRenderer->temporaryBuffer;
1509}
1510
1511void GBAVideoGLRendererPutPixels(struct GBAVideoRenderer* renderer, size_t stride, const void* pixels) {
1512 // TODO
1513 UNUSED(renderer);
1514 UNUSED(stride);
1515 UNUSED(pixels);
1516}
1517
1518static void _enableBg(struct GBAVideoGLRenderer* renderer, int bg, bool active) {
1519 int wasActive = renderer->bg[bg].enabled;
1520 if (!active) {
1521 renderer->bg[bg].enabled = 0;
1522 } else if (!wasActive && active) {
1523 /*if (renderer->nextY == 0 || GBARegisterDISPCNTGetMode(renderer->dispcnt) > 2) {
1524 // TODO: Investigate in more depth how switching background works in different modes
1525 renderer->bg[bg].enabled = 4;
1526 } else {
1527 renderer->bg[bg].enabled = 1;
1528 }*/
1529 renderer->bg[bg].enabled = 4;
1530 }
1531}
1532
1533static void GBAVideoGLRendererUpdateDISPCNT(struct GBAVideoGLRenderer* renderer) {
1534 _enableBg(renderer, 0, GBARegisterDISPCNTGetBg0Enable(renderer->dispcnt));
1535 _enableBg(renderer, 1, GBARegisterDISPCNTGetBg1Enable(renderer->dispcnt));
1536 _enableBg(renderer, 2, GBARegisterDISPCNTGetBg2Enable(renderer->dispcnt));
1537 _enableBg(renderer, 3, GBARegisterDISPCNTGetBg3Enable(renderer->dispcnt));
1538}
1539
1540static void GBAVideoGLRendererWriteBGCNT(struct GBAVideoGLBackground* bg, uint16_t value) {
1541 bg->priority = GBARegisterBGCNTGetPriority(value);
1542 bg->charBase = GBARegisterBGCNTGetCharBase(value) << 13;
1543 bg->mosaic = GBARegisterBGCNTGetMosaic(value);
1544 bg->multipalette = GBARegisterBGCNTGet256Color(value);
1545 bg->screenBase = GBARegisterBGCNTGetScreenBase(value) << 10;
1546 bg->overflow = GBARegisterBGCNTGetOverflow(value);
1547 bg->size = GBARegisterBGCNTGetSize(value);
1548}
1549
1550static void GBAVideoGLRendererWriteBGX_LO(struct GBAVideoGLBackground* bg, uint16_t value) {
1551 bg->refx = (bg->refx & 0xFFFF0000) | value;
1552 bg->affine.sx = bg->refx;
1553}
1554
1555static void GBAVideoGLRendererWriteBGX_HI(struct GBAVideoGLBackground* bg, uint16_t value) {
1556 bg->refx = (bg->refx & 0x0000FFFF) | (value << 16);
1557 bg->refx <<= 4;
1558 bg->refx >>= 4;
1559 bg->affine.sx = bg->refx;
1560}
1561
1562static void GBAVideoGLRendererWriteBGY_LO(struct GBAVideoGLBackground* bg, uint16_t value) {
1563 bg->refy = (bg->refy & 0xFFFF0000) | value;
1564 bg->affine.sy = bg->refy;
1565}
1566
1567static void GBAVideoGLRendererWriteBGY_HI(struct GBAVideoGLBackground* bg, uint16_t value) {
1568 bg->refy = (bg->refy & 0x0000FFFF) | (value << 16);
1569 bg->refy <<= 4;
1570 bg->refy >>= 4;
1571 bg->affine.sy = bg->refy;
1572}
1573
1574static void GBAVideoGLRendererWriteBLDCNT(struct GBAVideoGLRenderer* renderer, uint16_t value) {
1575 renderer->bg[0].target1 = GBARegisterBLDCNTGetTarget1Bg0(value);
1576 renderer->bg[1].target1 = GBARegisterBLDCNTGetTarget1Bg1(value);
1577 renderer->bg[2].target1 = GBARegisterBLDCNTGetTarget1Bg2(value);
1578 renderer->bg[3].target1 = GBARegisterBLDCNTGetTarget1Bg3(value);
1579 renderer->bg[0].target2 = GBARegisterBLDCNTGetTarget2Bg0(value);
1580 renderer->bg[1].target2 = GBARegisterBLDCNTGetTarget2Bg1(value);
1581 renderer->bg[2].target2 = GBARegisterBLDCNTGetTarget2Bg2(value);
1582 renderer->bg[3].target2 = GBARegisterBLDCNTGetTarget2Bg3(value);
1583
1584 renderer->blendEffect = GBARegisterBLDCNTGetEffect(value);
1585 renderer->target1Obj = GBARegisterBLDCNTGetTarget1Obj(value);
1586 renderer->target1Bd = GBARegisterBLDCNTGetTarget1Bd(value);
1587 renderer->target2Obj = GBARegisterBLDCNTGetTarget2Obj(value);
1588 renderer->target2Bd = GBARegisterBLDCNTGetTarget2Bd(value);
1589}
1590
1591void _finalizeLayers(struct GBAVideoGLRenderer* renderer) {
1592 const GLuint* uniforms = renderer->finalizeShader.uniforms;
1593 glBindFramebuffer(GL_FRAMEBUFFER, renderer->fbo[GBA_GL_FBO_OUTPUT]);
1594 glViewport(0, 0, GBA_VIDEO_HORIZONTAL_PIXELS * renderer->scale, GBA_VIDEO_VERTICAL_PIXELS * renderer->scale);
1595 glScissor(0, 0, GBA_VIDEO_HORIZONTAL_PIXELS * renderer->scale, GBA_VIDEO_VERTICAL_PIXELS * renderer->scale);
1596 if (GBARegisterDISPCNTIsForcedBlank(renderer->dispcnt)) {
1597 glClearColor(1.f, 1.f, 1.f, 1.f);
1598 glClear(GL_COLOR_BUFFER_BIT);
1599 } else {
1600 glUseProgram(renderer->finalizeShader.program);
1601 glBindVertexArray(renderer->finalizeShader.vao);
1602 glActiveTexture(GL_TEXTURE0);
1603 glBindTexture(GL_TEXTURE_2D, renderer->layers[GBA_GL_TEX_WINDOW]);
1604 glActiveTexture(GL_TEXTURE0 + 1);
1605 glBindTexture(GL_TEXTURE_2D, renderer->layers[GBA_GL_TEX_OBJ_COLOR]);
1606 glActiveTexture(GL_TEXTURE0 + 2);
1607 glBindTexture(GL_TEXTURE_2D, renderer->layers[GBA_GL_TEX_OBJ_FLAGS]);
1608 glActiveTexture(GL_TEXTURE0 + 3);
1609 glBindTexture(GL_TEXTURE_2D, renderer->bg[0].tex);
1610 glActiveTexture(GL_TEXTURE0 + 4);
1611 glBindTexture(GL_TEXTURE_2D, renderer->bg[0].flags);
1612 glActiveTexture(GL_TEXTURE0 + 5);
1613 glBindTexture(GL_TEXTURE_2D, renderer->bg[1].tex);
1614 glActiveTexture(GL_TEXTURE0 + 6);
1615 glBindTexture(GL_TEXTURE_2D, renderer->bg[1].flags);
1616 glActiveTexture(GL_TEXTURE0 + 7);
1617 glBindTexture(GL_TEXTURE_2D, renderer->bg[2].tex);
1618 glActiveTexture(GL_TEXTURE0 + 8);
1619 glBindTexture(GL_TEXTURE_2D, renderer->bg[2].flags);
1620 glActiveTexture(GL_TEXTURE0 + 9);
1621 glBindTexture(GL_TEXTURE_2D, renderer->bg[3].tex);
1622 glActiveTexture(GL_TEXTURE0 + 10);
1623 glBindTexture(GL_TEXTURE_2D, renderer->bg[3].flags);
1624 glActiveTexture(GL_TEXTURE0 + 11);
1625 glBindTexture(GL_TEXTURE_2D, renderer->layers[GBA_GL_TEX_BACKDROP_COLOR]);
1626 glActiveTexture(GL_TEXTURE0 + 12);
1627 glBindTexture(GL_TEXTURE_2D, renderer->layers[GBA_GL_TEX_BACKDROP_FLAGS]);
1628
1629 glUniform2i(uniforms[GBA_GL_VS_LOC], GBA_VIDEO_VERTICAL_PIXELS, 0);
1630 glUniform2i(uniforms[GBA_GL_VS_MAXPOS], GBA_VIDEO_HORIZONTAL_PIXELS, GBA_VIDEO_VERTICAL_PIXELS);
1631 glUniform1i(uniforms[GBA_GL_FINALIZE_SCALE], renderer->scale);
1632 glUniform1iv(uniforms[GBA_GL_FINALIZE_LAYERS], 5, (GLint[]) { 3, 5, 7, 9, 1 });
1633 glUniform1iv(uniforms[GBA_GL_FINALIZE_FLAGS], 5, (GLint[]) { 4, 6, 8, 10, 2 });
1634 glUniform1i(uniforms[GBA_GL_FINALIZE_WINDOW], 0);
1635 glUniform1i(uniforms[GBA_GL_FINALIZE_WINDOW], 0);
1636 glUniform1i(uniforms[GBA_GL_FINALIZE_BACKDROP], 11);
1637 glUniform1i(uniforms[GBA_GL_FINALIZE_BACKDROPFLAGS], 12);
1638 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1639 }
1640 glBindFramebuffer(GL_FRAMEBUFFER, 0);
1641}
1642
1643void GBAVideoGLRendererDrawSprite(struct GBAVideoGLRenderer* renderer, struct GBAObj* sprite, int y, int spriteY) {
1644 int width = GBAVideoObjSizes[GBAObjAttributesAGetShape(sprite->a) * 4 + GBAObjAttributesBGetSize(sprite->b)][0];
1645 int height = GBAVideoObjSizes[GBAObjAttributesAGetShape(sprite->a) * 4 + GBAObjAttributesBGetSize(sprite->b)][1];
1646 int32_t x = (uint32_t) GBAObjAttributesBGetX(sprite->b) << 23;
1647 x >>= 23;
1648
1649 int align = GBAObjAttributesAIs256Color(sprite->a) && !GBARegisterDISPCNTIsObjCharacterMapping(renderer->dispcnt);
1650 unsigned charBase = (BASE_TILE >> 1) + (GBAObjAttributesCGetTile(sprite->c) & ~align) * 0x10;
1651 int stride = GBARegisterDISPCNTIsObjCharacterMapping(renderer->dispcnt) ? (width >> 3) : (0x20 >> GBAObjAttributesAGet256Color(sprite->a));
1652
1653 if (spriteY + height >= 256) {
1654 spriteY -= 256;
1655 }
1656
1657 int totalWidth = width;
1658 int totalHeight = height;
1659 if (GBAObjAttributesAIsTransformed(sprite->a) && GBAObjAttributesAIsDoubleSize(sprite->a)) {
1660 totalWidth <<= 1;
1661 totalHeight <<= 1;
1662 }
1663
1664 const struct GBAVideoGLShader* shader = &renderer->objShader[GBAObjAttributesAGet256Color(sprite->a)];
1665 const GLuint* uniforms = shader->uniforms;
1666 glBindFramebuffer(GL_FRAMEBUFFER, renderer->fbo[GBA_GL_FBO_OBJ]);
1667 glViewport(x * renderer->scale, spriteY * renderer->scale, totalWidth * renderer->scale, totalHeight * renderer->scale);
1668 glScissor(x * renderer->scale, renderer->firstY * renderer->scale, totalWidth * renderer->scale, (y - renderer->firstY + 1) * renderer->scale);
1669 glUseProgram(shader->program);
1670 glBindVertexArray(shader->vao);
1671 glActiveTexture(GL_TEXTURE0);
1672 glBindTexture(GL_TEXTURE_2D, renderer->vramTex);
1673 glUniform2i(uniforms[GBA_GL_VS_LOC], totalHeight, 0);
1674 glUniform2i(uniforms[GBA_GL_VS_MAXPOS], totalWidth, totalHeight);
1675 glUniform1i(uniforms[GBA_GL_OBJ_VRAM], 0);
1676 glUniform1iv(uniforms[GBA_GL_OBJ_PALETTE], 256, &renderer->shadowPalette[256]);
1677 glUniform1i(uniforms[GBA_GL_OBJ_CHARBASE], charBase);
1678 glUniform1i(uniforms[GBA_GL_OBJ_STRIDE], stride);
1679 glUniform1i(uniforms[GBA_GL_OBJ_LOCALPALETTE], GBAObjAttributesCGetPalette(sprite->c));
1680 glUniform4i(uniforms[GBA_GL_OBJ_INFLAGS], GBAObjAttributesCGetPriority(sprite->c),
1681 (renderer->target1Obj || GBAObjAttributesAGetMode(sprite->a) == OBJ_MODE_SEMITRANSPARENT) | (renderer->target2Obj * 2) | (renderer->blendEffect * 4),
1682 renderer->blda, GBAObjAttributesAGetMode(sprite->a) == OBJ_MODE_SEMITRANSPARENT);
1683 if (GBAObjAttributesAIsTransformed(sprite->a)) {
1684 struct GBAOAMMatrix mat;
1685 LOAD_16(mat.a, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].a);
1686 LOAD_16(mat.b, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].b);
1687 LOAD_16(mat.c, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].c);
1688 LOAD_16(mat.d, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].d);
1689
1690 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 });
1691 } else {
1692 int flipX = 1;
1693 int flipY = 1;
1694 if (GBAObjAttributesBIsHFlip(sprite->b)) {
1695 flipX = -1;
1696 }
1697 if (GBAObjAttributesBIsVFlip(sprite->b)) {
1698 flipY = -1;
1699 }
1700 glUniformMatrix2fv(uniforms[GBA_GL_OBJ_TRANSFORM], 1, GL_FALSE, (GLfloat[]) { flipX, 0, 0, flipY });
1701 }
1702 glUniform4i(uniforms[GBA_GL_OBJ_DIMS], width, height, totalWidth, totalHeight);
1703 if (GBAObjAttributesAGetMode(sprite->a) == OBJ_MODE_OBJWIN) {
1704 glDisable(GL_DEPTH_TEST);
1705 int window = renderer->objwin & 0x3F;
1706 glUniform4i(uniforms[GBA_GL_OBJ_OBJWIN], 1, window, renderer->bldb, renderer->bldy);
1707 glDrawBuffers(3, (GLenum[]) { GL_NONE, GL_NONE, GL_COLOR_ATTACHMENT2 });
1708 } else {
1709 glEnable(GL_DEPTH_TEST);
1710 glUniform4i(uniforms[GBA_GL_OBJ_OBJWIN], 0, 0, 0, 0);
1711 glDrawBuffers(2, (GLenum[]) { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 });
1712 }
1713 if (GBAObjAttributesAIsMosaic(sprite->a) && GBAObjAttributesAGetMode(sprite->a) != OBJ_MODE_OBJWIN) {
1714 int mosaicH = GBAMosaicControlGetObjH(renderer->mosaic) + 1;
1715 if (GBAObjAttributesBIsHFlip(sprite->b)) {
1716 mosaicH = -mosaicH;
1717 }
1718 glUniform4i(uniforms[GBA_GL_OBJ_MOSAIC], mosaicH, GBAMosaicControlGetObjV(renderer->mosaic) + 1, x, spriteY);
1719 } else {
1720 glUniform4i(uniforms[GBA_GL_OBJ_MOSAIC], 0, 0, 0, 0);
1721 }
1722 glStencilFunc(GL_ALWAYS, 1, 1);
1723 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1724
1725 shader = &renderer->objShader[2];
1726 uniforms = shader->uniforms;
1727 glStencilFunc(GL_EQUAL, 1, 1);
1728 glUseProgram(shader->program);
1729 glDrawBuffers(2, (GLenum[]) { GL_NONE, GL_COLOR_ATTACHMENT1 });
1730 glColorMask(GL_TRUE, GL_FALSE, GL_FALSE, GL_FALSE);
1731 glBindVertexArray(shader->vao);
1732 glUniform2i(uniforms[GBA_GL_VS_LOC], totalHeight, 0);
1733 glUniform2i(uniforms[GBA_GL_VS_MAXPOS], totalWidth, totalHeight);
1734 glUniform4i(uniforms[GBA_GL_OBJ_INFLAGS], GBAObjAttributesCGetPriority(sprite->c), 0, 0, 0);
1735 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1736 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
1737
1738 glDrawBuffers(1, (GLenum[]) { GL_COLOR_ATTACHMENT0 });
1739}
1740
1741void _prepareBackground(struct GBAVideoGLRenderer* renderer, struct GBAVideoGLBackground* background, const GLuint* uniforms) {
1742 glBindFramebuffer(GL_FRAMEBUFFER, background->fbo);
1743 glViewport(0, 0, GBA_VIDEO_HORIZONTAL_PIXELS * renderer->scale, GBA_VIDEO_VERTICAL_PIXELS * renderer->scale);
1744 glActiveTexture(GL_TEXTURE0);
1745 glBindTexture(GL_TEXTURE_2D, renderer->vramTex);
1746 glUniform2i(uniforms[GBA_GL_VS_MAXPOS], GBA_VIDEO_HORIZONTAL_PIXELS, GBA_VIDEO_VERTICAL_PIXELS);
1747 glUniform1i(uniforms[GBA_GL_BG_VRAM], 0);
1748 glUniform1iv(uniforms[GBA_GL_OBJ_PALETTE], 256, renderer->shadowPalette);
1749 if (background->mosaic) {
1750 glUniform2i(uniforms[GBA_GL_BG_MOSAIC], GBAMosaicControlGetBgV(renderer->mosaic) + 1, GBAMosaicControlGetBgH(renderer->mosaic) + 1);
1751 } else {
1752 glUniform2i(uniforms[GBA_GL_BG_MOSAIC], 0, 0);
1753 }
1754 glUniform4i(uniforms[GBA_GL_BG_INFLAGS], background->priority,
1755 background->target1 | (background->target2 * 2) | (renderer->blendEffect * 4),
1756 renderer->blda, 0);
1757 glDrawBuffers(2, (GLenum[]) { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 });
1758}
1759
1760void GBAVideoGLRendererDrawBackgroundMode0(struct GBAVideoGLRenderer* renderer, struct GBAVideoGLBackground* background, int y) {
1761 const struct GBAVideoGLShader* shader = &renderer->bgShader[background->multipalette ? 1 : 0];
1762 const GLuint* uniforms = shader->uniforms;
1763 glUseProgram(shader->program);
1764 glBindVertexArray(shader->vao);
1765 _prepareBackground(renderer, background, uniforms);
1766 glUniform1i(uniforms[GBA_GL_BG_SCREENBASE], background->screenBase);
1767 glUniform1i(uniforms[GBA_GL_BG_CHARBASE], background->charBase);
1768 glUniform1i(uniforms[GBA_GL_BG_SIZE], background->size);
1769 glUniform1iv(uniforms[GBA_GL_BG_OFFSET], GBA_VIDEO_VERTICAL_PIXELS, background->scanlineOffset);
1770
1771 glScissor(0, renderer->firstY * renderer->scale, GBA_VIDEO_HORIZONTAL_PIXELS * renderer->scale, (y - renderer->firstY + 1) * renderer->scale);
1772 glUniform2i(uniforms[GBA_GL_VS_LOC], y - renderer->firstY + 1, renderer->firstY);
1773 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1774
1775 glDrawBuffers(1, (GLenum[]) { GL_COLOR_ATTACHMENT0 });
1776}
1777
1778void _prepareTransform(struct GBAVideoGLRenderer* renderer, struct GBAVideoGLBackground* background, const GLuint* uniforms, int y) {
1779 glScissor(0, renderer->firstY * renderer->scale, GBA_VIDEO_HORIZONTAL_PIXELS * renderer->scale, renderer->scale * (y - renderer->firstY + 1));
1780 glUniform2i(uniforms[GBA_GL_VS_LOC], y - renderer->firstY + 1, renderer->firstY);
1781 glUniform2i(uniforms[GBA_GL_BG_RANGE], renderer->firstAffine, y);
1782
1783 glUniform4iv(uniforms[GBA_GL_BG_TRANSFORM], GBA_VIDEO_VERTICAL_PIXELS, background->scanlineAffine);
1784 _prepareBackground(renderer, background, uniforms);
1785}
1786
1787void GBAVideoGLRendererDrawBackgroundMode2(struct GBAVideoGLRenderer* renderer, struct GBAVideoGLBackground* background, int y) {
1788 const struct GBAVideoGLShader* shader = &renderer->bgShader[background->overflow ? 2 : 3];
1789 const GLuint* uniforms = shader->uniforms;
1790 glUseProgram(shader->program);
1791 glBindVertexArray(shader->vao);
1792 _prepareTransform(renderer, background, uniforms, y);
1793 glUniform1i(uniforms[GBA_GL_BG_SCREENBASE], background->screenBase);
1794 glUniform1i(uniforms[GBA_GL_BG_CHARBASE], background->charBase);
1795 glUniform1i(uniforms[GBA_GL_BG_SIZE], background->size);
1796 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1797 glDrawBuffers(1, (GLenum[]) { GL_COLOR_ATTACHMENT0 });
1798}
1799
1800void GBAVideoGLRendererDrawBackgroundMode3(struct GBAVideoGLRenderer* renderer, struct GBAVideoGLBackground* background, int y) {
1801 const struct GBAVideoGLShader* shader = &renderer->bgShader[5];
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], 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 GBAVideoGLRendererDrawBackgroundMode4(struct GBAVideoGLRenderer* renderer, struct GBAVideoGLBackground* background, int y) {
1815 const struct GBAVideoGLShader* shader = &renderer->bgShader[4];
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) ? 0xA000 : 0);
1823 glUniform2i(uniforms[GBA_GL_BG_SIZE], GBA_VIDEO_HORIZONTAL_PIXELS, GBA_VIDEO_VERTICAL_PIXELS);
1824 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1825 glDrawBuffers(1, (GLenum[]) { GL_COLOR_ATTACHMENT0 });
1826}
1827
1828void GBAVideoGLRendererDrawBackgroundMode5(struct GBAVideoGLRenderer* renderer, struct GBAVideoGLBackground* background, int y) {
1829 const struct GBAVideoGLShader* shader = &renderer->bgShader[5];
1830 const GLuint* uniforms = shader->uniforms;
1831 glBindFramebuffer(GL_FRAMEBUFFER, background->fbo);
1832 glViewport(0, 0, GBA_VIDEO_HORIZONTAL_PIXELS * renderer->scale, GBA_VIDEO_VERTICAL_PIXELS * renderer->scale);
1833 glUseProgram(shader->program);
1834 glBindVertexArray(shader->vao);
1835 _prepareTransform(renderer, background, uniforms, y);
1836 glUniform1i(uniforms[GBA_GL_BG_CHARBASE], GBARegisterDISPCNTIsFrameSelect(renderer->dispcnt) ? 0x5000 : 0);
1837 glUniform2i(uniforms[GBA_GL_BG_SIZE], 160, 128);
1838 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1839 glDrawBuffers(1, (GLenum[]) { GL_COLOR_ATTACHMENT0 });
1840}
1841
1842void GBAVideoGLRendererDrawWindow(struct GBAVideoGLRenderer* renderer, int y) {
1843 const struct GBAVideoGLShader* shader = &renderer->windowShader;
1844 const GLuint* uniforms = shader->uniforms;
1845 glBindFramebuffer(GL_FRAMEBUFFER, renderer->fbo[GBA_GL_FBO_WINDOW]);
1846 glViewport(0, 0, GBA_VIDEO_HORIZONTAL_PIXELS * renderer->scale, GBA_VIDEO_VERTICAL_PIXELS * renderer->scale);
1847 glScissor(0, renderer->firstY * renderer->scale, GBA_VIDEO_HORIZONTAL_PIXELS * renderer->scale, renderer->scale * (y - renderer->firstY + 1));
1848 glUseProgram(shader->program);
1849 glBindVertexArray(shader->vao);
1850 glUniform2i(uniforms[GBA_GL_VS_LOC], y - renderer->firstY + 1, renderer->firstY);
1851 glUniform2i(uniforms[GBA_GL_VS_MAXPOS], GBA_VIDEO_HORIZONTAL_PIXELS, GBA_VIDEO_VERTICAL_PIXELS);
1852 glUniform1i(uniforms[GBA_GL_WIN_DISPCNT], renderer->dispcnt >> 8);
1853 glUniform2i(uniforms[GBA_GL_WIN_BLEND], renderer->bldb, renderer->bldy);
1854 glUniform3i(uniforms[GBA_GL_WIN_FLAGS], renderer->winN[0].control, renderer->winN[1].control, renderer->winout);
1855 glUniform4iv(uniforms[GBA_GL_WIN_WIN0], GBA_VIDEO_VERTICAL_PIXELS, renderer->winNHistory[0]);
1856 glUniform4iv(uniforms[GBA_GL_WIN_WIN1], GBA_VIDEO_VERTICAL_PIXELS, renderer->winNHistory[1]);
1857 glDrawBuffers(1, (GLenum[]) { GL_COLOR_ATTACHMENT0 });
1858 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1859}
1860
1861void GBAVideoGLRendererSetScale(struct GBAVideoGLRenderer* renderer, int scale) {
1862 if (scale == renderer->scale) {
1863 return;
1864 }
1865 if (renderer->temporaryBuffer) {
1866 mappedMemoryFree(renderer->temporaryBuffer, GBA_VIDEO_HORIZONTAL_PIXELS * GBA_VIDEO_VERTICAL_PIXELS * renderer->scale * renderer->scale * BYTES_PER_PIXEL);
1867 renderer->temporaryBuffer = NULL;
1868 }
1869 renderer->scale = scale;
1870 _initFramebuffers(renderer);
1871 renderer->paletteDirty = true;
1872}
1873
1874#endif