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 _finalizeLayers(struct GBAVideoGLRenderer* renderer, int y);
45
46#define TEST_LAYER_ENABLED(X) !renderer->disableBG[X] && glRenderer->bg[X].enabled == 4
47
48struct GBAVideoGLUniform {
49 const char* name;
50 int type;
51};
52
53static const GLchar* const _gl3Header =
54 "#version 130\n";
55
56static const char* const _vertexShader =
57 "in vec2 position;\n"
58 "uniform ivec2 loc;\n"
59 "uniform ivec2 maxPos;\n"
60 "out vec2 texCoord;\n"
61
62 "void main() {\n"
63 " vec2 local = vec2(position.x, float(position.y * loc.x + loc.y) / abs(maxPos.y));\n"
64 " gl_Position = vec4((local * 2. - 1.) * sign(maxPos), 0., 1.);\n"
65 " texCoord = local * abs(maxPos);\n"
66 "}";
67
68static const char* const _renderTile16 =
69 "vec4 renderTile(int tile, int paletteId, ivec2 localCoord) {\n"
70 " int address = charBase + tile * 16 + (localCoord.x >> 2) + (localCoord.y << 1);\n"
71 " vec4 halfrow = texelFetch(vram, ivec2(address & 255, address >> 8), 0);\n"
72 " int entry = int(halfrow[3 - (localCoord.x & 3)] * 15.9);\n"
73 " vec4 color = texelFetch(palette, ivec2(entry, paletteId), 0);\n"
74 " if (entry == 0) {\n"
75 " discard;\n"
76 " }\n"
77 " color.a = 1;\n"
78 " return color;\n"
79 "}";
80
81static const char* const _renderTile256 =
82 "vec4 renderTile(int tile, int paletteId, ivec2 localCoord) {\n"
83 " int address = charBase + tile * 32 + (localCoord.x >> 1) + (localCoord.y << 2);\n"
84 " vec4 halfrow = texelFetch(vram, ivec2(address & 255, address >> 8), 0);\n"
85 " int entry = int(halfrow[3 - 2 * (localCoord.x & 1)] * 15.9);\n"
86 " int pal2 = int(halfrow[2 - 2 * (localCoord.x & 1)] * 15.9);\n"
87 " vec4 color = texelFetch(palette, ivec2(entry, pal2 + (paletteId & 16)), 0);\n"
88 " if ((pal2 | entry) == 0) {\n"
89 " discard;\n"
90 " }\n"
91 " color.a = 1.;\n"
92 " return color;\n"
93 "}";
94
95static const struct GBAVideoGLUniform _uniformsMode0[] = {
96 { "loc", GBA_GL_VS_LOC, },
97 { "maxPos", GBA_GL_VS_MAXPOS, },
98 { "vram", GBA_GL_BG_VRAM, },
99 { "palette", GBA_GL_BG_PALETTE, },
100 { "screenBase", GBA_GL_BG_SCREENBASE, },
101 { "charBase", GBA_GL_BG_CHARBASE, },
102 { "size", GBA_GL_BG_SIZE, },
103 { "offset", GBA_GL_BG_OFFSET, },
104 { "inflags", GBA_GL_BG_INFLAGS, },
105 { 0 }
106};
107
108static const char* const _renderMode0 =
109 "in vec2 texCoord;\n"
110 "uniform sampler2D vram;\n"
111 "uniform sampler2D palette;\n"
112 "uniform int screenBase;\n"
113 "uniform int charBase;\n"
114 "uniform int size;\n"
115 "uniform ivec2 offset;\n"
116 "uniform ivec4 inflags;\n"
117 "out vec4 color;\n"
118 "out vec4 flags;\n"
119 "const vec4 flagCoeff = vec4(32., 32., 16., 16.);\n"
120
121 "vec4 renderTile(int tile, int paletteId, ivec2 localCoord);\n"
122
123 "void main() {\n"
124 " ivec2 coord = ivec2(texCoord) + offset;\n"
125 " if ((size & 1) == 1) {\n"
126 " coord.y += coord.x & 256;\n"
127 " }\n"
128 " coord.x &= 255;\n"
129 " int mapAddress = screenBase + (coord.x >> 3) + (coord.y >> 3) * 32;\n"
130 " vec4 map = texelFetch(vram, ivec2(mapAddress & 255, mapAddress >> 8), 0);\n"
131 " int tileFlags = int(map.g * 15.9);\n"
132 " if ((tileFlags & 4) == 4) {\n"
133 " coord.x ^= 7;\n"
134 " }\n"
135 " if ((tileFlags & 8) == 8) {\n"
136 " coord.y ^= 7;\n"
137 " }\n"
138 " int tile = int(map.a * 15.9) + int(map.b * 15.9) * 16 + (tileFlags & 0x3) * 256;\n"
139 " color = renderTile(tile, int(map.r * 15.9), coord & 7);\n"
140 " flags = inflags / flagCoeff;\n"
141 "}";
142
143static const char* const _fetchTileOverflow =
144 "vec4 fetchTile(ivec2 coord) {\n"
145 " int sizeAdjusted = (0x8000 << size) - 1;\n"
146 " coord &= sizeAdjusted;\n"
147 " return renderTile(coord);\n"
148 "}";
149
150static const char* const _fetchTileNoOverflow =
151 "vec4 fetchTile(ivec2 coord) {\n"
152 " int sizeAdjusted = (0x8000 << size) - 1;\n"
153 " ivec2 outerCoord = coord & ~sizeAdjusted;\n"
154 " if ((outerCoord.x | outerCoord.y) != 0) {\n"
155 " discard;\n"
156 " }\n"
157 " return renderTile(coord);\n"
158 "}";
159
160static const struct GBAVideoGLUniform _uniformsMode2[] = {
161 { "loc", GBA_GL_VS_LOC, },
162 { "maxPos", GBA_GL_VS_MAXPOS, },
163 { "vram", GBA_GL_BG_VRAM, },
164 { "palette", GBA_GL_BG_PALETTE, },
165 { "screenBase", GBA_GL_BG_SCREENBASE, },
166 { "charBase", GBA_GL_BG_CHARBASE, },
167 { "size", GBA_GL_BG_SIZE, },
168 { "inflags", GBA_GL_BG_INFLAGS, },
169 { "offset", GBA_GL_BG_OFFSET, },
170 { "transform", GBA_GL_BG_TRANSFORM, },
171 { 0 }
172};
173
174static const char* const _renderMode2 =
175 "in vec2 texCoord;\n"
176 "uniform sampler2D vram;\n"
177 "uniform sampler2D palette;\n"
178 "uniform int screenBase;\n"
179 "uniform int charBase;\n"
180 "uniform int size;\n"
181 "uniform ivec4 inflags;\n"
182 "uniform ivec2[4] offset;\n"
183 "uniform ivec2[4] transform;\n"
184 "out vec4 color;\n"
185 "out vec4 flags;\n"
186 "const vec4 flagCoeff = vec4(32., 32., 16., 16.);\n"
187 "precision highp float;\n"
188 "precision highp int;\n"
189
190 "vec4 fetchTile(ivec2 coord);\n"
191
192 "vec4 renderTile(ivec2 coord) {\n"
193 " int map = (coord.x >> 11) + (((coord.y >> 7) & 0x7F0) << size);\n"
194 " int mapAddress = screenBase + (map >> 1);\n"
195 " vec4 twomaps = texelFetch(vram, ivec2(mapAddress & 255, mapAddress >> 8), 0);\n"
196 " int tile = int(twomaps[3 - 2 * (map & 1)] * 15.9) + int(twomaps[2 - 2 * (map & 1)] * 15.9) * 16;\n"
197 " int address = charBase + tile * 32 + ((coord.x >> 9) & 3) + ((coord.y >> 6) & 0x1C);\n"
198 " vec4 halfrow = texelFetch(vram, ivec2(address & 255, address >> 8), 0);\n"
199 " int entry = int(halfrow[3 - ((coord.x >> 7) & 2)] * 15.9);\n"
200 " int pal2 = int(halfrow[2 - ((coord.x >> 7) & 2)] * 15.9);\n"
201 " vec4 color = texelFetch(palette, ivec2(entry, pal2), 0);\n"
202 " if ((pal2 | entry) == 0) {\n"
203 " discard;\n"
204 " }\n"
205 " color.a = 1.;\n"
206 " return color;\n"
207 "}\n"
208
209 "vec2 interpolate(ivec2 arr[4], float x) {\n"
210 " float x1m = 1. - x;\n"
211 " return x1m * x1m * x1m * arr[0] +"
212 " 3 * x1m * x1m * x * arr[1] +"
213 " 3 * x1m * x * x * arr[2] +"
214 " x * x * x * arr[3];\n"
215 "}\n"
216
217 "void main() {\n"
218 " float y = fract(texCoord.y);\n"
219 " float lin = 0.5 - y / ceil(y) * 0.25;\n"
220 " vec2 mixedTransform = interpolate(transform, lin);\n"
221 " vec2 mixedOffset = interpolate(offset, lin);\n"
222 " color = fetchTile(ivec2(mixedTransform * texCoord.x + mixedOffset));\n"
223 " flags = inflags / flagCoeff;\n"
224 "}";
225
226static const struct GBAVideoGLUniform _uniformsObj[] = {
227 { "loc", GBA_GL_VS_LOC, },
228 { "maxPos", GBA_GL_VS_MAXPOS, },
229 { "vram", GBA_GL_OBJ_VRAM, },
230 { "palette", GBA_GL_OBJ_PALETTE, },
231 { "charBase", GBA_GL_OBJ_CHARBASE, },
232 { "stride", GBA_GL_OBJ_STRIDE, },
233 { "localPalette", GBA_GL_OBJ_LOCALPALETTE, },
234 { "inflags", GBA_GL_OBJ_INFLAGS, },
235 { "transform", GBA_GL_OBJ_TRANSFORM, },
236 { "dims", GBA_GL_OBJ_DIMS, },
237 { "objwin", GBA_GL_OBJ_OBJWIN, },
238 { 0 }
239};
240
241static const char* const _renderObj =
242 "in vec2 texCoord;\n"
243 "uniform sampler2D vram;\n"
244 "uniform sampler2D palette;\n"
245 "uniform int charBase;\n"
246 "uniform int stride;\n"
247 "uniform int localPalette;\n"
248 "uniform ivec4 inflags;\n"
249 "uniform mat2x2 transform;\n"
250 "uniform ivec4 dims;\n"
251 "uniform vec3 objwin;\n"
252 "out vec4 color;\n"
253 "out vec4 flags;\n"
254 "out vec2 window;\n"
255 "const vec4 flagCoeff = vec4(32., 32., 16., 16.);\n"
256
257 "vec4 renderTile(int tile, int paletteId, ivec2 localCoord);\n"
258
259 "void main() {\n"
260 " ivec2 coord = ivec2(transform * (texCoord - dims.zw / 2) + dims.xy / 2);\n"
261 " if ((coord & ~(dims.xy - 1)) != ivec2(0, 0)) {\n"
262 " discard;\n"
263 " }\n"
264 " vec4 pix = renderTile((coord.x >> 3) + (coord.y >> 3) * stride, 16 + localPalette, coord & 7);\n"
265 " if (objwin.x > 0) {\n"
266 " pix.a = 0;\n"
267 " }\n"
268 " color = pix;\n"
269 " flags = inflags / flagCoeff;\n"
270 " window = objwin.yz;\n"
271 "}";
272
273static const struct GBAVideoGLUniform _uniformsFinalize[] = {
274 { "loc", GBA_GL_VS_LOC, },
275 { "maxPos", GBA_GL_VS_MAXPOS, },
276 { "scale", GBA_GL_FINALIZE_SCALE, },
277 { "layers", GBA_GL_FINALIZE_LAYERS, },
278 { "flags", GBA_GL_FINALIZE_FLAGS, },
279 { "window", GBA_GL_FINALIZE_WINDOW, },
280 { "backdrop", GBA_GL_FINALIZE_BACKDROP, },
281 { "backdropFlags", GBA_GL_FINALIZE_BACKDROPFLAGS, },
282 { 0 }
283};
284
285static const char* const _finalize =
286 "in vec2 texCoord;\n"
287 "uniform int scale;\n"
288 "uniform sampler2D layers[5];\n"
289 "uniform sampler2D flags[5];\n"
290 "uniform sampler2D window;\n"
291 "uniform vec4 backdrop;\n"
292 "uniform vec4 backdropFlags;\n"
293 "const vec4 flagCoeff = vec4(32., 32., 16., 16.);\n"
294 "out vec4 color;\n"
295
296 "void composite(vec4 pixel, ivec4 flags, inout vec4 topPixel, inout ivec4 topFlags, inout vec4 bottomPixel, inout ivec4 bottomFlags) {\n"
297 " if (pixel.a == 0) {\n"
298 " return;\n"
299 " }\n"
300 " if (flags.x >= topFlags.x) {\n"
301 " if (flags.x >= bottomFlags.x) {\n"
302 " return;\n"
303 " }\n"
304 " bottomFlags = flags;\n"
305 " bottomPixel = pixel;\n"
306 " } else {\n"
307 " bottomFlags = topFlags;\n"
308 " topFlags = flags;\n"
309 " bottomPixel = topPixel;\n"
310 " topPixel = pixel;\n"
311 " }\n"
312 "}\n"
313
314 "void main() {\n"
315 " ivec2 windowFlags = ivec2(texelFetch(window, ivec2(texCoord * scale), 0).xy * 32);\n"
316 " int layerWindow = windowFlags.x | (windowFlags.y << 4);\n"
317 " vec4 topPixel = backdrop;\n"
318 " vec4 bottomPixel = backdrop;\n"
319 " ivec4 topFlags = ivec4(backdropFlags * flagCoeff);\n"
320 " ivec4 bottomFlags = ivec4(backdropFlags * flagCoeff);\n"
321 " if ((layerWindow & 1) == 0) {\n"
322 " vec4 pix = texelFetch(layers[0], ivec2(texCoord * scale), 0);\n"
323 " ivec4 inflags = ivec4(texelFetch(flags[0], ivec2(texCoord * scale), 0) * flagCoeff);\n"
324 " composite(pix, inflags, topPixel, topFlags, bottomPixel, bottomFlags);\n"
325 " }\n"
326 " if ((layerWindow & 2) == 0) {\n"
327 " vec4 pix = texelFetch(layers[1], ivec2(texCoord * scale), 0);\n"
328 " ivec4 inflags = ivec4(texelFetch(flags[1], ivec2(texCoord * scale), 0) * flagCoeff);\n"
329 " composite(pix, inflags, topPixel, topFlags, bottomPixel, bottomFlags);\n"
330 " }\n"
331 " if ((layerWindow & 4) == 0) {\n"
332 " vec4 pix = texelFetch(layers[2], ivec2(texCoord * scale), 0);\n"
333 " ivec4 inflags = ivec4(texelFetch(flags[2], ivec2(texCoord * scale), 0) * flagCoeff);\n"
334 " composite(pix, inflags, topPixel, topFlags, bottomPixel, bottomFlags);\n"
335 " }\n"
336 " if ((layerWindow & 8) == 0) {\n"
337 " vec4 pix = texelFetch(layers[3], ivec2(texCoord * scale), 0);\n"
338 " ivec4 inflags = ivec4(texelFetch(flags[3], ivec2(texCoord * scale), 0) * flagCoeff);\n"
339 " composite(pix, inflags, topPixel, topFlags, bottomPixel, bottomFlags);\n"
340 " }\n"
341 " if ((layerWindow & 32) != 0) {\n"
342 " topFlags.y &= ~1;\n"
343 " }\n"
344 " if ((layerWindow & 16) == 0) {\n"
345 " vec4 pix = texelFetch(layers[4], ivec2(texCoord * scale), 0);\n"
346 " ivec4 inflags = ivec4(texelFetch(flags[4], ivec2(texCoord * scale), 0) * flagCoeff);\n"
347 " composite(pix, inflags, topPixel, topFlags, bottomPixel, bottomFlags);\n"
348 " }\n"
349 " if ((topFlags.y & 13) == 5) {\n"
350 " if ((bottomFlags.y & 2) == 2) {\n"
351 " topPixel *= topFlags.z / 16.;\n"
352 " topPixel += bottomPixel * bottomFlags.w / 16.;\n"
353 " }\n"
354 " } else if ((topFlags.y & 13) == 9) {\n"
355 " topPixel += (1. - topPixel) * topFlags.z / 16.;\n"
356 " } else if ((topFlags.y & 13) == 13) {\n"
357 " topPixel -= topPixel * topFlags.z / 16.;\n"
358 " }\n"
359 " color = topPixel;\n"
360 "}";
361
362static const GLint _vertices[] = {
363 0, 0,
364 0, 1,
365 1, 1,
366 1, 0,
367};
368
369void GBAVideoGLRendererCreate(struct GBAVideoGLRenderer* renderer) {
370 renderer->d.init = GBAVideoGLRendererInit;
371 renderer->d.reset = GBAVideoGLRendererReset;
372 renderer->d.deinit = GBAVideoGLRendererDeinit;
373 renderer->d.writeVideoRegister = GBAVideoGLRendererWriteVideoRegister;
374 renderer->d.writeVRAM = GBAVideoGLRendererWriteVRAM;
375 renderer->d.writeOAM = GBAVideoGLRendererWriteOAM;
376 renderer->d.writePalette = GBAVideoGLRendererWritePalette;
377 renderer->d.drawScanline = GBAVideoGLRendererDrawScanline;
378 renderer->d.finishFrame = GBAVideoGLRendererFinishFrame;
379 renderer->d.getPixels = GBAVideoGLRendererGetPixels;
380 renderer->d.putPixels = GBAVideoGLRendererPutPixels;
381
382 renderer->d.disableBG[0] = false;
383 renderer->d.disableBG[1] = false;
384 renderer->d.disableBG[2] = false;
385 renderer->d.disableBG[3] = false;
386 renderer->d.disableOBJ = false;
387
388 renderer->scale = 1;
389}
390
391void _compileShader(struct GBAVideoGLRenderer* glRenderer, struct GBAVideoGLShader* shader, const char** shaderBuffer, int shaderBufferLines, GLuint vs, const struct GBAVideoGLUniform* uniforms, char* log) {
392 GLuint program = glCreateProgram();
393 shader->program = program;
394
395 GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
396 glAttachShader(program, vs);
397 glAttachShader(program, fs);
398 glShaderSource(fs, shaderBufferLines, shaderBuffer, 0);
399 glCompileShader(fs);
400 glGetShaderInfoLog(fs, 1024, 0, log);
401 if (log[0]) {
402 mLOG(GBA_VIDEO, ERROR, "Fragment shader compilation failure: %s", log);
403 }
404 glLinkProgram(program);
405 glGetProgramInfoLog(program, 1024, 0, log);
406 if (log[0]) {
407 mLOG(GBA_VIDEO, ERROR, "Program link failure: %s", log);
408 }
409 glDeleteShader(fs);
410#ifndef BUILD_GLES3
411 glBindFragDataLocation(program, 0, "color");
412 glBindFragDataLocation(program, 1, "flags");
413#endif
414
415 glGenVertexArrays(1, &shader->vao);
416 glBindVertexArray(shader->vao);
417 glBindBuffer(GL_ARRAY_BUFFER, glRenderer->vbo);
418 GLuint positionLocation = glGetAttribLocation(program, "position");
419 glVertexAttribPointer(positionLocation, 2, GL_INT, GL_FALSE, 0, NULL);
420
421 size_t i;
422 for (i = 0; uniforms[i].name; ++i) {
423 shader->uniforms[uniforms[i].type] = glGetUniformLocation(program, uniforms[i].name);
424 }
425}
426
427static void _initFramebufferTexture(GLuint tex, GLenum format, GLenum attachment, int scale) {
428 glBindTexture(GL_TEXTURE_2D, tex);
429 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
430 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
431 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
432 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
433 glTexImage2D(GL_TEXTURE_2D, 0, format, GBA_VIDEO_HORIZONTAL_PIXELS * scale, GBA_VIDEO_VERTICAL_PIXELS * scale, 0, format, GL_UNSIGNED_BYTE, 0);
434 glFramebufferTexture2D(GL_FRAMEBUFFER, attachment, GL_TEXTURE_2D, tex, 0);
435}
436
437void GBAVideoGLRendererInit(struct GBAVideoRenderer* renderer) {
438 struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;
439 glRenderer->temporaryBuffer = NULL;
440
441 glGenFramebuffers(GBA_GL_FBO_MAX, glRenderer->fbo);
442 glGenTextures(GBA_GL_TEX_MAX, glRenderer->layers);
443
444 glGenTextures(1, &glRenderer->paletteTex);
445 glBindTexture(GL_TEXTURE_2D, glRenderer->paletteTex);
446 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
447 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
448
449 glGenTextures(1, &glRenderer->vramTex);
450 glBindTexture(GL_TEXTURE_2D, glRenderer->vramTex);
451 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
452 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
453 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA4, 256, 192, 0, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, 0);
454
455 glBindFramebuffer(GL_FRAMEBUFFER, glRenderer->fbo[GBA_GL_FBO_OBJ]);
456 _initFramebufferTexture(glRenderer->layers[GBA_GL_TEX_OBJ_COLOR], GL_RGBA, GL_COLOR_ATTACHMENT0, glRenderer->scale);
457 _initFramebufferTexture(glRenderer->layers[GBA_GL_TEX_OBJ_FLAGS], GL_RGBA, GL_COLOR_ATTACHMENT1, glRenderer->scale);
458
459 glBindFramebuffer(GL_FRAMEBUFFER, glRenderer->fbo[GBA_GL_FBO_WINDOW]);
460 _initFramebufferTexture(glRenderer->layers[GBA_GL_TEX_WINDOW], GL_RG, GL_COLOR_ATTACHMENT0, glRenderer->scale);
461
462 glBindFramebuffer(GL_FRAMEBUFFER, glRenderer->fbo[GBA_GL_FBO_OUTPUT]);
463 _initFramebufferTexture(glRenderer->outputTex, GL_RGB, GL_COLOR_ATTACHMENT0, glRenderer->scale);
464
465 glBindFramebuffer(GL_FRAMEBUFFER, 0);
466
467 glGenBuffers(1, &glRenderer->vbo);
468 glBindBuffer(GL_ARRAY_BUFFER, glRenderer->vbo);
469 glBufferData(GL_ARRAY_BUFFER, sizeof(_vertices), _vertices, GL_STATIC_DRAW);
470
471 int i;
472 for (i = 0; i < 4; ++i) {
473 struct GBAVideoGLBackground* bg = &glRenderer->bg[i];
474 bg->index = i;
475 bg->enabled = 0;
476 bg->priority = 0;
477 bg->charBase = 0;
478 bg->mosaic = 0;
479 bg->multipalette = 0;
480 bg->screenBase = 0;
481 bg->overflow = 0;
482 bg->size = 0;
483 bg->target1 = 0;
484 bg->target2 = 0;
485 bg->x = 0;
486 bg->y = 0;
487 bg->refx = 0;
488 bg->refy = 0;
489 bg->affine[0].dx = 256;
490 bg->affine[0].dmx = 0;
491 bg->affine[0].dy = 0;
492 bg->affine[0].dmy = 256;
493 bg->affine[0].sx = 0;
494 bg->affine[0].sy = 0;
495 glGenFramebuffers(1, &bg->fbo);
496 glGenTextures(1, &bg->tex);
497 glGenTextures(1, &bg->flags);
498 glBindFramebuffer(GL_FRAMEBUFFER, bg->fbo);
499 _initFramebufferTexture(bg->tex, GL_RGBA, GL_COLOR_ATTACHMENT0, glRenderer->scale);
500 _initFramebufferTexture(bg->flags, GL_RGBA, GL_COLOR_ATTACHMENT1, glRenderer->scale);
501 glBindFramebuffer(GL_FRAMEBUFFER, 0);
502 }
503
504 char log[1024];
505 const GLchar* shaderBuffer[8];
506 shaderBuffer[0] = _gl3Header;
507
508 GLuint vs = glCreateShader(GL_VERTEX_SHADER);
509 shaderBuffer[1] = _vertexShader;
510 glShaderSource(vs, 2, shaderBuffer, 0);
511 glCompileShader(vs);
512 glGetShaderInfoLog(vs, 1024, 0, log);
513 if (log[0]) {
514 mLOG(GBA_VIDEO, ERROR, "Vertex shader compilation failure: %s", log);
515 }
516
517 shaderBuffer[1] = _renderMode0;
518
519 shaderBuffer[2] = _renderTile16;
520 _compileShader(glRenderer, &glRenderer->bgShader[0], shaderBuffer, 3, vs, _uniformsMode0, log);
521
522 shaderBuffer[2] = _renderTile256;
523 _compileShader(glRenderer, &glRenderer->bgShader[1], shaderBuffer, 3, vs, _uniformsMode0, log);
524
525 shaderBuffer[1] = _renderMode2;
526
527 shaderBuffer[2] = _fetchTileOverflow;
528 _compileShader(glRenderer, &glRenderer->bgShader[2], shaderBuffer, 3, vs, _uniformsMode2, log);
529
530 shaderBuffer[2] = _fetchTileNoOverflow;
531 _compileShader(glRenderer, &glRenderer->bgShader[3], shaderBuffer, 3, vs, _uniformsMode2, log);
532
533 shaderBuffer[1] = _renderObj;
534
535 shaderBuffer[2] = _renderTile16;
536 _compileShader(glRenderer, &glRenderer->objShader[0], shaderBuffer, 3, vs, _uniformsObj, log);
537#ifndef BUILD_GLES3
538 glBindFragDataLocation(glRenderer->objShader[0].program, 2, "window");
539#endif
540
541 shaderBuffer[2] = _renderTile256;
542 _compileShader(glRenderer, &glRenderer->objShader[1], shaderBuffer, 3, vs, _uniformsObj, log);
543#ifndef BUILD_GLES3
544 glBindFragDataLocation(glRenderer->objShader[1].program, 2, "window");
545#endif
546
547 shaderBuffer[1] = _finalize;
548 _compileShader(glRenderer, &glRenderer->finalizeShader, shaderBuffer, 2, vs, _uniformsFinalize, log);
549
550 glBindVertexArray(0);
551 glDeleteShader(vs);
552
553 GBAVideoGLRendererReset(renderer);
554}
555
556void GBAVideoGLRendererDeinit(struct GBAVideoRenderer* renderer) {
557 struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;
558 if (glRenderer->temporaryBuffer) {
559 mappedMemoryFree(glRenderer->temporaryBuffer, GBA_VIDEO_HORIZONTAL_PIXELS * GBA_VIDEO_VERTICAL_PIXELS * glRenderer->scale * glRenderer->scale);
560 }
561 glDeleteFramebuffers(GBA_GL_FBO_MAX, glRenderer->fbo);
562 glDeleteTextures(GBA_GL_TEX_MAX, glRenderer->layers);
563 glDeleteTextures(1, &glRenderer->paletteTex);
564 glDeleteTextures(1, &glRenderer->vramTex);
565}
566
567void GBAVideoGLRendererReset(struct GBAVideoRenderer* renderer) {
568 struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;
569
570 glRenderer->paletteDirty = true;
571 glRenderer->vramDirty = 0xFFFFFF;
572 glRenderer->firstAffine = -1;
573}
574
575void GBAVideoGLRendererWriteVRAM(struct GBAVideoRenderer* renderer, uint32_t address) {
576 struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;
577 glRenderer->vramDirty |= 1 << (address >> 12);
578}
579
580void GBAVideoGLRendererWriteOAM(struct GBAVideoRenderer* renderer, uint32_t oam) {
581 UNUSED(oam);
582 struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;
583 glRenderer->oamDirty = true;
584}
585
586void GBAVideoGLRendererWritePalette(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
587 struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;
588#ifdef BUILD_GLES3
589 glRenderer->shadowPalette[address >> 1] = (value & 0x3F) | ((value & 0x7FE0) << 1);
590#else
591 UNUSED(address);
592 UNUSED(value);
593#endif
594 glRenderer->paletteDirty = true;
595}
596
597uint16_t GBAVideoGLRendererWriteVideoRegister(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
598 struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;
599 if (renderer->cache) {
600 GBAVideoCacheWriteVideoRegister(renderer->cache, address, value);
601 }
602
603 switch (address) {
604 case REG_DISPCNT:
605 value &= 0xFFF7;
606 glRenderer->dispcnt = value;
607 GBAVideoGLRendererUpdateDISPCNT(glRenderer);
608 break;
609 case REG_BG0CNT:
610 value &= 0xDFFF;
611 GBAVideoGLRendererWriteBGCNT(&glRenderer->bg[0], value);
612 break;
613 case REG_BG1CNT:
614 value &= 0xDFFF;
615 GBAVideoGLRendererWriteBGCNT(&glRenderer->bg[1], value);
616 break;
617 case REG_BG2CNT:
618 value &= 0xFFFF;
619 GBAVideoGLRendererWriteBGCNT(&glRenderer->bg[2], value);
620 break;
621 case REG_BG3CNT:
622 value &= 0xFFFF;
623 GBAVideoGLRendererWriteBGCNT(&glRenderer->bg[3], value);
624 break;
625 case REG_BG0HOFS:
626 value &= 0x01FF;
627 glRenderer->bg[0].x = value;
628 break;
629 case REG_BG0VOFS:
630 value &= 0x01FF;
631 glRenderer->bg[0].y = value;
632 break;
633 case REG_BG1HOFS:
634 value &= 0x01FF;
635 glRenderer->bg[1].x = value;
636 break;
637 case REG_BG1VOFS:
638 value &= 0x01FF;
639 glRenderer->bg[1].y = value;
640 break;
641 case REG_BG2HOFS:
642 value &= 0x01FF;
643 glRenderer->bg[2].x = value;
644 break;
645 case REG_BG2VOFS:
646 value &= 0x01FF;
647 glRenderer->bg[2].y = value;
648 break;
649 case REG_BG3HOFS:
650 value &= 0x01FF;
651 glRenderer->bg[3].x = value;
652 break;
653 case REG_BG3VOFS:
654 value &= 0x01FF;
655 glRenderer->bg[3].y = value;
656 break;
657 case REG_BG2PA:
658 glRenderer->bg[2].affine[0].dx = value;
659 break;
660 case REG_BG2PB:
661 glRenderer->bg[2].affine[0].dmx = value;
662 break;
663 case REG_BG2PC:
664 glRenderer->bg[2].affine[0].dy = value;
665 break;
666 case REG_BG2PD:
667 glRenderer->bg[2].affine[0].dmy = value;
668 break;
669 case REG_BG2X_LO:
670 GBAVideoGLRendererWriteBGX_LO(&glRenderer->bg[2], value);
671 break;
672 case REG_BG2X_HI:
673 GBAVideoGLRendererWriteBGX_HI(&glRenderer->bg[2], value);
674 break;
675 case REG_BG2Y_LO:
676 GBAVideoGLRendererWriteBGY_LO(&glRenderer->bg[2], value);
677 break;
678 case REG_BG2Y_HI:
679 GBAVideoGLRendererWriteBGY_HI(&glRenderer->bg[2], value);
680 break;
681 case REG_BG3PA:
682 glRenderer->bg[3].affine[0].dx = value;
683 break;
684 case REG_BG3PB:
685 glRenderer->bg[3].affine[0].dmx = value;
686 break;
687 case REG_BG3PC:
688 glRenderer->bg[3].affine[0].dy = value;
689 break;
690 case REG_BG3PD:
691 glRenderer->bg[3].affine[0].dmy = value;
692 break;
693 case REG_BG3X_LO:
694 GBAVideoGLRendererWriteBGX_LO(&glRenderer->bg[3], value);
695 break;
696 case REG_BG3X_HI:
697 GBAVideoGLRendererWriteBGX_HI(&glRenderer->bg[3], value);
698 break;
699 case REG_BG3Y_LO:
700 GBAVideoGLRendererWriteBGY_LO(&glRenderer->bg[3], value);
701 break;
702 case REG_BG3Y_HI:
703 GBAVideoGLRendererWriteBGY_HI(&glRenderer->bg[3], value);
704 break;
705 case REG_BLDCNT:
706 GBAVideoGLRendererWriteBLDCNT(glRenderer, value);
707 value &= 0x3FFF;
708 break;
709 case REG_BLDALPHA:
710 glRenderer->blda = value & 0x1F;
711 if (glRenderer->blda > 0x10) {
712 glRenderer->blda = 0x10;
713 }
714 glRenderer->bldb = (value >> 8) & 0x1F;
715 if (glRenderer->bldb > 0x10) {
716 glRenderer->bldb = 0x10;
717 }
718 value &= 0x1F1F;
719 break;
720 case REG_BLDY:
721 value &= 0x1F;
722 if (value > 0x10) {
723 value = 0x10;
724 }
725 glRenderer->bldy = value;
726 break;
727 case REG_WIN0H:
728 glRenderer->winN[0].h.end = value;
729 glRenderer->winN[0].h.start = value >> 8;
730 if (glRenderer->winN[0].h.start > GBA_VIDEO_HORIZONTAL_PIXELS && glRenderer->winN[0].h.start > glRenderer->winN[0].h.end) {
731 glRenderer->winN[0].h.start = 0;
732 }
733 if (glRenderer->winN[0].h.end > GBA_VIDEO_HORIZONTAL_PIXELS) {
734 glRenderer->winN[0].h.end = GBA_VIDEO_HORIZONTAL_PIXELS;
735 if (glRenderer->winN[0].h.start > GBA_VIDEO_HORIZONTAL_PIXELS) {
736 glRenderer->winN[0].h.start = GBA_VIDEO_HORIZONTAL_PIXELS;
737 }
738 }
739 break;
740 case REG_WIN1H:
741 glRenderer->winN[1].h.end = value;
742 glRenderer->winN[1].h.start = value >> 8;
743 if (glRenderer->winN[1].h.start > GBA_VIDEO_HORIZONTAL_PIXELS && glRenderer->winN[1].h.start > glRenderer->winN[1].h.end) {
744 glRenderer->winN[1].h.start = 0;
745 }
746 if (glRenderer->winN[1].h.end > GBA_VIDEO_HORIZONTAL_PIXELS) {
747 glRenderer->winN[1].h.end = GBA_VIDEO_HORIZONTAL_PIXELS;
748 if (glRenderer->winN[1].h.start > GBA_VIDEO_HORIZONTAL_PIXELS) {
749 glRenderer->winN[1].h.start = GBA_VIDEO_HORIZONTAL_PIXELS;
750 }
751 }
752 break;
753 case REG_WIN0V:
754 glRenderer->winN[0].v.end = value;
755 glRenderer->winN[0].v.start = value >> 8;
756 if (glRenderer->winN[0].v.start > GBA_VIDEO_VERTICAL_PIXELS && glRenderer->winN[0].v.start > glRenderer->winN[0].v.end) {
757 glRenderer->winN[0].v.start = 0;
758 }
759 if (glRenderer->winN[0].v.end > GBA_VIDEO_VERTICAL_PIXELS) {
760 glRenderer->winN[0].v.end = GBA_VIDEO_VERTICAL_PIXELS;
761 if (glRenderer->winN[0].v.start > GBA_VIDEO_VERTICAL_PIXELS) {
762 glRenderer->winN[0].v.start = GBA_VIDEO_VERTICAL_PIXELS;
763 }
764 }
765 break;
766 case REG_WIN1V:
767 glRenderer->winN[1].v.end = value;
768 glRenderer->winN[1].v.start = value >> 8;
769 if (glRenderer->winN[1].v.start > GBA_VIDEO_VERTICAL_PIXELS && glRenderer->winN[1].v.start > glRenderer->winN[1].v.end) {
770 glRenderer->winN[1].v.start = 0;
771 }
772 if (glRenderer->winN[1].v.end > GBA_VIDEO_VERTICAL_PIXELS) {
773 glRenderer->winN[1].v.end = GBA_VIDEO_VERTICAL_PIXELS;
774 if (glRenderer->winN[1].v.start > GBA_VIDEO_VERTICAL_PIXELS) {
775 glRenderer->winN[1].v.start = GBA_VIDEO_VERTICAL_PIXELS;
776 }
777 }
778 break;
779 case REG_WININ:
780 value &= 0x3F3F;
781 glRenderer->winN[0].control = value;
782 glRenderer->winN[1].control = value >> 8;
783 break;
784 case REG_WINOUT:
785 value &= 0x3F3F;
786 glRenderer->winout = value;
787 glRenderer->objwin = value >> 8;
788 break;
789 case REG_MOSAIC:
790 glRenderer->mosaic = value;
791 break;
792 default:
793 break;
794 }
795 return value;
796}
797
798void GBAVideoGLRendererDrawScanline(struct GBAVideoRenderer* renderer, int y) {
799 struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;
800 if (glRenderer->paletteDirty) {
801 glBindTexture(GL_TEXTURE_2D, glRenderer->paletteTex);
802#ifdef BUILD_GLES3
803 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB565, 16, 32, 0, GL_RGBA, GL_UNSIGNED_SHORT_5_6_5, glRenderer->shadowPalette);
804#else
805 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB5_A1, 16, 32, 0, GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV, glRenderer->d.palette);
806#endif
807 glRenderer->paletteDirty = false;
808 }
809 int i;
810 for (i = 0; i < 24; ++i) {
811 if (!(glRenderer->vramDirty & (1 << i))) {
812 continue;
813 }
814 // TODO: PBOs
815 glBindTexture(GL_TEXTURE_2D, glRenderer->vramTex);
816 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 8 * i, 256, 8, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, &glRenderer->d.vram[2048 * i]);
817 }
818 glRenderer->vramDirty = 0;
819
820 if (y == 0) {
821 glDisable(GL_SCISSOR_TEST);
822 glClearColor(0, 0, 0, 0);
823 glBindFramebuffer(GL_FRAMEBUFFER, glRenderer->fbo[GBA_GL_FBO_OBJ]);
824 glDrawBuffers(2, (GLenum[]) { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 });
825 glClear(GL_COLOR_BUFFER_BIT);
826
827 for (i = 0; i < 4; ++i) {
828 glBindFramebuffer(GL_FRAMEBUFFER, glRenderer->bg[i].fbo);
829 glDrawBuffers(2, (GLenum[]) { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 });
830 glClear(GL_COLOR_BUFFER_BIT);
831 }
832 glDrawBuffers(1, (GLenum[]) { GL_COLOR_ATTACHMENT0 });
833 }
834 glEnable(GL_SCISSOR_TEST);
835
836 if (GBARegisterDISPCNTGetMode(glRenderer->dispcnt) != 0) {
837 if (glRenderer->firstAffine < 0) {
838 memcpy(&glRenderer->bg[2].affine[3], &glRenderer->bg[2].affine[0], sizeof(struct GBAVideoGLAffine));
839 memcpy(&glRenderer->bg[3].affine[3], &glRenderer->bg[3].affine[0], sizeof(struct GBAVideoGLAffine));
840 memcpy(&glRenderer->bg[2].affine[2], &glRenderer->bg[2].affine[0], sizeof(struct GBAVideoGLAffine));
841 memcpy(&glRenderer->bg[3].affine[2], &glRenderer->bg[3].affine[0], sizeof(struct GBAVideoGLAffine));
842 memcpy(&glRenderer->bg[2].affine[1], &glRenderer->bg[2].affine[0], sizeof(struct GBAVideoGLAffine));
843 memcpy(&glRenderer->bg[3].affine[1], &glRenderer->bg[3].affine[0], sizeof(struct GBAVideoGLAffine));
844 glRenderer->firstAffine = y;
845 } else if (y - glRenderer->firstAffine == 1) {
846 memcpy(&glRenderer->bg[2].affine[1], &glRenderer->bg[2].affine[0], sizeof(struct GBAVideoGLAffine));
847 memcpy(&glRenderer->bg[3].affine[1], &glRenderer->bg[3].affine[0], sizeof(struct GBAVideoGLAffine));
848 }
849 } else {
850 glRenderer->firstAffine = -1;
851 }
852
853 GBAVideoGLRendererDrawWindow(glRenderer, y);
854 if (GBARegisterDISPCNTIsObjEnable(glRenderer->dispcnt) && !glRenderer->d.disableOBJ) {
855 if (glRenderer->oamDirty) {
856 glRenderer->oamMax = GBAVideoRendererCleanOAM(glRenderer->d.oam->obj, glRenderer->sprites, 0);
857 glRenderer->oamDirty = false;
858 }
859 int i;
860 for (i = glRenderer->oamMax; i--;) {
861 struct GBAVideoRendererSprite* sprite = &glRenderer->sprites[i];
862 if ((y < sprite->y && (sprite->endY - 256 < 0 || y >= sprite->endY - 256)) || y >= sprite->endY) {
863 continue;
864 }
865
866 GBAVideoGLRendererDrawSprite(glRenderer, &sprite->obj, y, sprite->y);
867 }
868 }
869
870 if (TEST_LAYER_ENABLED(0) && GBARegisterDISPCNTGetMode(glRenderer->dispcnt) < 2) {
871 GBAVideoGLRendererDrawBackgroundMode0(glRenderer, &glRenderer->bg[0], y);
872 }
873 if (TEST_LAYER_ENABLED(1) && GBARegisterDISPCNTGetMode(glRenderer->dispcnt) < 2) {
874 GBAVideoGLRendererDrawBackgroundMode0(glRenderer, &glRenderer->bg[1], y);
875 }
876 if (TEST_LAYER_ENABLED(2)) {
877 switch (GBARegisterDISPCNTGetMode(glRenderer->dispcnt)) {
878 case 0:
879 GBAVideoGLRendererDrawBackgroundMode0(glRenderer, &glRenderer->bg[2], y);
880 break;
881 case 1:
882 case 2:
883 GBAVideoGLRendererDrawBackgroundMode2(glRenderer, &glRenderer->bg[2], y);
884 break;
885 case 3:
886 //GBAVideoGLRendererDrawBackgroundMode3(glRenderer, &glRenderer->bg[2], y);
887 break;
888 case 4:
889 //GBAVideoGLRendererDrawBackgroundMode4(glRenderer, &glRenderer->bg[2], y);
890 break;
891 case 5:
892 //GBAVideoGLRendererDrawBackgroundMode5(glRenderer, &glRenderer->bg[2], y);
893 break;
894 }
895 }
896 if (TEST_LAYER_ENABLED(3)) {
897 switch (GBARegisterDISPCNTGetMode(glRenderer->dispcnt)) {
898 case 0:
899 GBAVideoGLRendererDrawBackgroundMode0(glRenderer, &glRenderer->bg[3], y);
900 break;
901 case 2:
902 GBAVideoGLRendererDrawBackgroundMode2(glRenderer, &glRenderer->bg[3], y);
903 break;
904 }
905 }
906 _finalizeLayers(glRenderer, y);
907
908 if (GBARegisterDISPCNTGetMode(glRenderer->dispcnt) != 0) {
909 memcpy(&glRenderer->bg[2].affine[3], &glRenderer->bg[2].affine[2], sizeof(struct GBAVideoGLAffine));
910 memcpy(&glRenderer->bg[3].affine[3], &glRenderer->bg[3].affine[2], sizeof(struct GBAVideoGLAffine));
911 memcpy(&glRenderer->bg[2].affine[2], &glRenderer->bg[2].affine[1], sizeof(struct GBAVideoGLAffine));
912 memcpy(&glRenderer->bg[3].affine[2], &glRenderer->bg[3].affine[1], sizeof(struct GBAVideoGLAffine));
913 memcpy(&glRenderer->bg[2].affine[1], &glRenderer->bg[2].affine[0], sizeof(struct GBAVideoGLAffine));
914 memcpy(&glRenderer->bg[3].affine[1], &glRenderer->bg[3].affine[0], sizeof(struct GBAVideoGLAffine));
915
916 glRenderer->bg[2].affine[0].sx += glRenderer->bg[2].affine[0].dmx;
917 glRenderer->bg[2].affine[0].sy += glRenderer->bg[2].affine[0].dmy;
918 glRenderer->bg[3].affine[0].sx += glRenderer->bg[3].affine[0].dmx;
919 glRenderer->bg[3].affine[0].sy += glRenderer->bg[3].affine[0].dmy;
920 }
921}
922
923void GBAVideoGLRendererFinishFrame(struct GBAVideoRenderer* renderer) {
924 struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;
925 glRenderer->firstAffine = -1;
926 glRenderer->bg[2].affine[0].sx = glRenderer->bg[2].refx;
927 glRenderer->bg[2].affine[0].sy = glRenderer->bg[2].refy;
928 glRenderer->bg[3].affine[0].sx = glRenderer->bg[3].refx;
929 glRenderer->bg[3].affine[0].sy = glRenderer->bg[3].refy;
930 glFlush();
931}
932
933void GBAVideoGLRendererGetPixels(struct GBAVideoRenderer* renderer, size_t* stride, const void** pixels) {
934 struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;
935 *stride = GBA_VIDEO_HORIZONTAL_PIXELS * glRenderer->scale;
936 if (!glRenderer->temporaryBuffer) {
937 glRenderer->temporaryBuffer = anonymousMemoryMap(GBA_VIDEO_HORIZONTAL_PIXELS * GBA_VIDEO_VERTICAL_PIXELS * glRenderer->scale * glRenderer->scale * BYTES_PER_PIXEL);
938 }
939 glFinish();
940 glBindFramebuffer(GL_FRAMEBUFFER, glRenderer->fbo[GBA_GL_FBO_OUTPUT]);
941 glPixelStorei(GL_PACK_ROW_LENGTH, GBA_VIDEO_HORIZONTAL_PIXELS * glRenderer->scale);
942 glPixelStorei(GL_PACK_ALIGNMENT, 1);
943 glReadPixels(0, 0, GBA_VIDEO_HORIZONTAL_PIXELS * glRenderer->scale, GBA_VIDEO_VERTICAL_PIXELS * glRenderer->scale, GL_RGBA, GL_UNSIGNED_BYTE, (void*) glRenderer->temporaryBuffer);
944 *pixels = glRenderer->temporaryBuffer;
945}
946
947void GBAVideoGLRendererPutPixels(struct GBAVideoRenderer* renderer, size_t stride, const void* pixels) {
948
949}
950
951static void _enableBg(struct GBAVideoGLRenderer* renderer, int bg, bool active) {
952 int wasActive = renderer->bg[bg].enabled;
953 if (!active) {
954 renderer->bg[bg].enabled = 0;
955 } else if (!wasActive && active) {
956 /*if (renderer->nextY == 0 || GBARegisterDISPCNTGetMode(renderer->dispcnt) > 2) {
957 // TODO: Investigate in more depth how switching background works in different modes
958 renderer->bg[bg].enabled = 4;
959 } else {
960 renderer->bg[bg].enabled = 1;
961 }*/
962 renderer->bg[bg].enabled = 4;
963 }
964}
965
966static void GBAVideoGLRendererUpdateDISPCNT(struct GBAVideoGLRenderer* renderer) {
967 _enableBg(renderer, 0, GBARegisterDISPCNTGetBg0Enable(renderer->dispcnt));
968 _enableBg(renderer, 1, GBARegisterDISPCNTGetBg1Enable(renderer->dispcnt));
969 _enableBg(renderer, 2, GBARegisterDISPCNTGetBg2Enable(renderer->dispcnt));
970 _enableBg(renderer, 3, GBARegisterDISPCNTGetBg3Enable(renderer->dispcnt));
971}
972
973static void GBAVideoGLRendererWriteBGCNT(struct GBAVideoGLBackground* bg, uint16_t value) {
974 bg->priority = GBARegisterBGCNTGetPriority(value);
975 bg->charBase = GBARegisterBGCNTGetCharBase(value) << 13;
976 bg->mosaic = GBARegisterBGCNTGetMosaic(value);
977 bg->multipalette = GBARegisterBGCNTGet256Color(value);
978 bg->screenBase = GBARegisterBGCNTGetScreenBase(value) << 10;
979 bg->overflow = GBARegisterBGCNTGetOverflow(value);
980 bg->size = GBARegisterBGCNTGetSize(value);
981}
982
983static void GBAVideoGLRendererWriteBGX_LO(struct GBAVideoGLBackground* bg, uint16_t value) {
984 bg->refx = (bg->refx & 0xFFFF0000) | value;
985 bg->affine[0].sx = bg->refx;
986}
987
988static void GBAVideoGLRendererWriteBGX_HI(struct GBAVideoGLBackground* bg, uint16_t value) {
989 bg->refx = (bg->refx & 0x0000FFFF) | (value << 16);
990 bg->refx <<= 4;
991 bg->refx >>= 4;
992 bg->affine[0].sx = bg->refx;
993}
994
995static void GBAVideoGLRendererWriteBGY_LO(struct GBAVideoGLBackground* bg, uint16_t value) {
996 bg->refy = (bg->refy & 0xFFFF0000) | value;
997 bg->affine[0].sy = bg->refy;
998}
999
1000static void GBAVideoGLRendererWriteBGY_HI(struct GBAVideoGLBackground* bg, uint16_t value) {
1001 bg->refy = (bg->refy & 0x0000FFFF) | (value << 16);
1002 bg->refy <<= 4;
1003 bg->refy >>= 4;
1004 bg->affine[0].sy = bg->refy;
1005}
1006
1007static void GBAVideoGLRendererWriteBLDCNT(struct GBAVideoGLRenderer* renderer, uint16_t value) {
1008 renderer->bg[0].target1 = GBARegisterBLDCNTGetTarget1Bg0(value);
1009 renderer->bg[1].target1 = GBARegisterBLDCNTGetTarget1Bg1(value);
1010 renderer->bg[2].target1 = GBARegisterBLDCNTGetTarget1Bg2(value);
1011 renderer->bg[3].target1 = GBARegisterBLDCNTGetTarget1Bg3(value);
1012 renderer->bg[0].target2 = GBARegisterBLDCNTGetTarget2Bg0(value);
1013 renderer->bg[1].target2 = GBARegisterBLDCNTGetTarget2Bg1(value);
1014 renderer->bg[2].target2 = GBARegisterBLDCNTGetTarget2Bg2(value);
1015 renderer->bg[3].target2 = GBARegisterBLDCNTGetTarget2Bg3(value);
1016
1017 renderer->blendEffect = GBARegisterBLDCNTGetEffect(value);
1018 renderer->target1Obj = GBARegisterBLDCNTGetTarget1Obj(value);
1019 renderer->target1Bd = GBARegisterBLDCNTGetTarget1Bd(value);
1020 renderer->target2Obj = GBARegisterBLDCNTGetTarget2Obj(value);
1021 renderer->target2Bd = GBARegisterBLDCNTGetTarget2Bd(value);
1022}
1023
1024void _finalizeLayers(struct GBAVideoGLRenderer* renderer, int y) {
1025 const GLuint* uniforms = renderer->finalizeShader.uniforms;
1026 glBindFramebuffer(GL_FRAMEBUFFER, renderer->fbo[GBA_GL_FBO_OUTPUT]);
1027 glViewport(0, 0, GBA_VIDEO_HORIZONTAL_PIXELS * renderer->scale, GBA_VIDEO_VERTICAL_PIXELS * renderer->scale);
1028 glScissor(0, y * renderer->scale, GBA_VIDEO_HORIZONTAL_PIXELS * renderer->scale, renderer->scale);
1029 glUseProgram(renderer->finalizeShader.program);
1030 glBindVertexArray(renderer->finalizeShader.vao);
1031 glActiveTexture(GL_TEXTURE0);
1032 glBindTexture(GL_TEXTURE_2D, renderer->layers[GBA_GL_TEX_WINDOW]);
1033 glActiveTexture(GL_TEXTURE0 + 1);
1034 glBindTexture(GL_TEXTURE_2D, renderer->layers[GBA_GL_TEX_OBJ_COLOR]);
1035 glActiveTexture(GL_TEXTURE0 + 2);
1036 glBindTexture(GL_TEXTURE_2D, renderer->layers[GBA_GL_TEX_OBJ_FLAGS]);
1037 glActiveTexture(GL_TEXTURE0 + 3);
1038 glBindTexture(GL_TEXTURE_2D, renderer->bg[0].tex);
1039 glActiveTexture(GL_TEXTURE0 + 4);
1040 glBindTexture(GL_TEXTURE_2D, renderer->bg[0].flags);
1041 glActiveTexture(GL_TEXTURE0 + 5);
1042 glBindTexture(GL_TEXTURE_2D, renderer->bg[1].tex);
1043 glActiveTexture(GL_TEXTURE0 + 6);
1044 glBindTexture(GL_TEXTURE_2D, renderer->bg[1].flags);
1045 glActiveTexture(GL_TEXTURE0 + 7);
1046 glBindTexture(GL_TEXTURE_2D, renderer->bg[2].tex);
1047 glActiveTexture(GL_TEXTURE0 + 8);
1048 glBindTexture(GL_TEXTURE_2D, renderer->bg[2].flags);
1049 glActiveTexture(GL_TEXTURE0 + 9);
1050 glBindTexture(GL_TEXTURE_2D, renderer->bg[3].tex);
1051 glActiveTexture(GL_TEXTURE0 + 10);
1052 glBindTexture(GL_TEXTURE_2D, renderer->bg[3].flags);
1053
1054 uint32_t backdrop = M_RGB5_TO_RGB8(renderer->d.palette[0]);
1055 glUniform2i(uniforms[GBA_GL_VS_LOC], 1, y);
1056 glUniform2i(uniforms[GBA_GL_VS_MAXPOS], GBA_VIDEO_HORIZONTAL_PIXELS, GBA_VIDEO_VERTICAL_PIXELS);
1057 glUniform1i(uniforms[GBA_GL_FINALIZE_SCALE], renderer->scale);
1058 glUniform1iv(uniforms[GBA_GL_FINALIZE_LAYERS], 5, (GLint[]) { 3, 5, 7, 9, 1 });
1059 glUniform1iv(uniforms[GBA_GL_FINALIZE_FLAGS], 5, (GLint[]) { 4, 6, 8, 10, 2 });
1060 glUniform1i(uniforms[GBA_GL_FINALIZE_WINDOW], 0);
1061 glUniform4f(uniforms[GBA_GL_FINALIZE_BACKDROP], ((backdrop >> 16) & 0xFF) / 256., ((backdrop >> 8) & 0xFF) / 256., (backdrop & 0xFF) / 256., 0.f);
1062 glUniform4f(uniforms[GBA_GL_FINALIZE_BACKDROPFLAGS], 1, (renderer->target1Bd | (renderer->target2Bd * 2) | (renderer->blendEffect * 4)) / 32.f,
1063 (renderer->blendEffect == BLEND_ALPHA ? renderer->blda : renderer->bldy) / 16.f, renderer->bldb / 16.f);
1064 glEnableVertexAttribArray(0);
1065 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1066 glBindFramebuffer(GL_FRAMEBUFFER, 0);
1067}
1068
1069void GBAVideoGLRendererDrawSprite(struct GBAVideoGLRenderer* renderer, struct GBAObj* sprite, int y, int spriteY) {
1070 int width = GBAVideoObjSizes[GBAObjAttributesAGetShape(sprite->a) * 4 + GBAObjAttributesBGetSize(sprite->b)][0];
1071 int height = GBAVideoObjSizes[GBAObjAttributesAGetShape(sprite->a) * 4 + GBAObjAttributesBGetSize(sprite->b)][1];
1072 int32_t x = (uint32_t) GBAObjAttributesBGetX(sprite->b) << 23;
1073 x >>= 23;
1074
1075 int align = GBAObjAttributesAIs256Color(sprite->a) && !GBARegisterDISPCNTIsObjCharacterMapping(renderer->dispcnt);
1076 unsigned charBase = (BASE_TILE >> 1) + (GBAObjAttributesCGetTile(sprite->c) & ~align) * 0x10;
1077 int stride = GBARegisterDISPCNTIsObjCharacterMapping(renderer->dispcnt) ? (width >> 3) : (0x20 >> GBAObjAttributesAGet256Color(sprite->a));
1078
1079 if (spriteY + height >= 256) {
1080 spriteY -= 256;
1081 }
1082
1083 if (!GBAObjAttributesAIsTransformed(sprite->a) && GBAObjAttributesBIsVFlip(sprite->b)) {
1084 spriteY = (y - height) + (y - spriteY) + 1;
1085 }
1086
1087 int totalWidth = width;
1088 int totalHeight = height;
1089 if (GBAObjAttributesAIsTransformed(sprite->a) && GBAObjAttributesAIsDoubleSize(sprite->a)) {
1090 totalWidth <<= 1;
1091 totalHeight <<= 1;
1092 }
1093
1094 enum GBAVideoBlendEffect blendEffect = GBAObjAttributesAGetMode(sprite->a) == OBJ_MODE_SEMITRANSPARENT ? BLEND_ALPHA : renderer->blendEffect;
1095
1096 const struct GBAVideoGLShader* shader = &renderer->objShader[GBAObjAttributesAGet256Color(sprite->a)];
1097 const GLuint* uniforms = shader->uniforms;
1098 glBindFramebuffer(GL_FRAMEBUFFER, renderer->fbo[GBA_GL_FBO_OBJ]);
1099 glViewport(x * renderer->scale, spriteY * renderer->scale, totalWidth * renderer->scale, totalHeight * renderer->scale);
1100 glScissor(x * renderer->scale, y * renderer->scale, totalWidth * renderer->scale, renderer->scale);
1101 glUseProgram(shader->program);
1102 glBindVertexArray(shader->vao);
1103 glActiveTexture(GL_TEXTURE0);
1104 glBindTexture(GL_TEXTURE_2D, renderer->vramTex);
1105 glActiveTexture(GL_TEXTURE0 + 1);
1106 glBindTexture(GL_TEXTURE_2D, renderer->paletteTex);
1107 glUniform2i(uniforms[GBA_GL_VS_LOC], 1, y - spriteY);
1108 glUniform2i(uniforms[GBA_GL_VS_MAXPOS], (GBAObjAttributesBIsHFlip(sprite->b) && !GBAObjAttributesAIsTransformed(sprite->a)) ? -totalWidth : totalWidth, totalHeight);
1109 glUniform1i(uniforms[GBA_GL_OBJ_VRAM], 0);
1110 glUniform1i(uniforms[GBA_GL_OBJ_PALETTE], 1);
1111 glUniform1i(uniforms[GBA_GL_OBJ_CHARBASE], charBase);
1112 glUniform1i(uniforms[GBA_GL_OBJ_STRIDE], stride);
1113 glUniform1i(uniforms[GBA_GL_OBJ_LOCALPALETTE], GBAObjAttributesCGetPalette(sprite->c));
1114 glUniform4i(uniforms[GBA_GL_OBJ_INFLAGS], GBAObjAttributesCGetPriority(sprite->c) << 3,
1115 (renderer->target1Obj || GBAObjAttributesAGetMode(sprite->a) == OBJ_MODE_SEMITRANSPARENT) | (renderer->target2Obj * 2) | (blendEffect * 4),
1116 blendEffect == BLEND_ALPHA ? renderer->blda : renderer->bldy, renderer->bldb);
1117 if (GBAObjAttributesAIsTransformed(sprite->a)) {
1118 struct GBAOAMMatrix mat;
1119 LOAD_16(mat.a, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].a);
1120 LOAD_16(mat.b, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].b);
1121 LOAD_16(mat.c, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].c);
1122 LOAD_16(mat.d, 0, &renderer->d.oam->mat[GBAObjAttributesBGetMatIndex(sprite->b)].d);
1123
1124 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 });
1125 } else {
1126 glUniformMatrix2fv(uniforms[GBA_GL_OBJ_TRANSFORM], 1, GL_FALSE, (GLfloat[]) { 1.f, 0, 0, 1.f });
1127 }
1128 glUniform4i(uniforms[GBA_GL_OBJ_DIMS], width, height, totalWidth, totalHeight);
1129 if (GBAObjAttributesAGetMode(sprite->a) == OBJ_MODE_OBJWIN) {
1130 int window = ~renderer->objwin & 0xFF;
1131 glUniform3f(uniforms[GBA_GL_OBJ_OBJWIN], 1, (window & 0xF) / 32.f, (window >> 4) / 32.f);
1132 glDrawBuffers(3, (GLenum[]) { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2 });
1133 } else {
1134 glUniform3f(uniforms[GBA_GL_OBJ_OBJWIN], 0, 0, 0);
1135 glDrawBuffers(2, (GLenum[]) { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 });
1136 }
1137 glEnableVertexAttribArray(0);
1138 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1139 glDrawBuffers(1, (GLenum[]) { GL_COLOR_ATTACHMENT0 });
1140}
1141
1142void GBAVideoGLRendererDrawBackgroundMode0(struct GBAVideoGLRenderer* renderer, struct GBAVideoGLBackground* background, int y) {
1143 int inY = y + background->y;
1144 int yBase = inY & 0xFF;
1145 if (background->size == 2) {
1146 yBase += inY & 0x100;
1147 } else if (background->size == 3) {
1148 yBase += (inY & 0x100) << 1;
1149 }
1150
1151 const struct GBAVideoGLShader* shader = &renderer->bgShader[background->multipalette ? 1 : 0];
1152 const GLuint* uniforms = shader->uniforms;
1153 glBindFramebuffer(GL_FRAMEBUFFER, background->fbo);
1154 glViewport(0, 0, GBA_VIDEO_HORIZONTAL_PIXELS * renderer->scale, GBA_VIDEO_VERTICAL_PIXELS * renderer->scale);
1155 glScissor(0, y * renderer->scale, GBA_VIDEO_HORIZONTAL_PIXELS * renderer->scale, renderer->scale);
1156 glUseProgram(shader->program);
1157 glBindVertexArray(shader->vao);
1158 glActiveTexture(GL_TEXTURE0);
1159 glBindTexture(GL_TEXTURE_2D, renderer->vramTex);
1160 glActiveTexture(GL_TEXTURE0 + 1);
1161 glBindTexture(GL_TEXTURE_2D, renderer->paletteTex);
1162 glUniform2i(uniforms[GBA_GL_VS_LOC], 1, y);
1163 glUniform2i(uniforms[GBA_GL_VS_MAXPOS], GBA_VIDEO_HORIZONTAL_PIXELS, GBA_VIDEO_VERTICAL_PIXELS);
1164 glUniform1i(uniforms[GBA_GL_BG_VRAM], 0);
1165 glUniform1i(uniforms[GBA_GL_BG_PALETTE], 1);
1166 glUniform1i(uniforms[GBA_GL_BG_SCREENBASE], background->screenBase);
1167 glUniform1i(uniforms[GBA_GL_BG_CHARBASE], background->charBase);
1168 glUniform1i(uniforms[GBA_GL_BG_SIZE], background->size);
1169 glUniform2i(uniforms[GBA_GL_BG_OFFSET], background->x, yBase - y);
1170 glUniform4i(uniforms[GBA_GL_BG_INFLAGS], (background->priority << 3) + (background->index << 1) + 1,
1171 background->target1 | (background->target2 * 2) | (renderer->blendEffect * 4),
1172 renderer->blendEffect == BLEND_ALPHA ? renderer->blda : renderer->bldy, renderer->bldb);
1173 glDrawBuffers(2, (GLenum[]) { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 });
1174 glEnableVertexAttribArray(0);
1175 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1176 glDrawBuffers(1, (GLenum[]) { GL_COLOR_ATTACHMENT0 });
1177}
1178
1179void GBAVideoGLRendererDrawBackgroundMode2(struct GBAVideoGLRenderer* renderer, struct GBAVideoGLBackground* background, int y) {
1180 const struct GBAVideoGLShader* shader = &renderer->bgShader[background->overflow ? 2 : 3];
1181 const GLuint* uniforms = shader->uniforms;
1182 glBindFramebuffer(GL_FRAMEBUFFER, background->fbo);
1183 glViewport(0, 0, GBA_VIDEO_HORIZONTAL_PIXELS * renderer->scale, GBA_VIDEO_VERTICAL_PIXELS * renderer->scale);
1184 glScissor(0, y * renderer->scale, GBA_VIDEO_HORIZONTAL_PIXELS * renderer->scale, renderer->scale);
1185 glUseProgram(shader->program);
1186 glBindVertexArray(shader->vao);
1187 glActiveTexture(GL_TEXTURE0);
1188 glBindTexture(GL_TEXTURE_2D, renderer->vramTex);
1189 glActiveTexture(GL_TEXTURE0 + 1);
1190 glBindTexture(GL_TEXTURE_2D, renderer->paletteTex);
1191 glUniform2i(uniforms[GBA_GL_VS_LOC], 1, y);
1192 glUniform2i(uniforms[GBA_GL_VS_MAXPOS], GBA_VIDEO_HORIZONTAL_PIXELS, GBA_VIDEO_VERTICAL_PIXELS);
1193 glUniform1i(uniforms[GBA_GL_BG_VRAM], 0);
1194 glUniform1i(uniforms[GBA_GL_BG_PALETTE], 1);
1195 glUniform1i(uniforms[GBA_GL_BG_SCREENBASE], background->screenBase);
1196 glUniform1i(uniforms[GBA_GL_BG_CHARBASE], background->charBase);
1197 glUniform1i(uniforms[GBA_GL_BG_SIZE], background->size);
1198 glUniform4i(uniforms[GBA_GL_BG_INFLAGS], (background->priority << 3) + (background->index << 1) + 1,
1199 background->target1 | (background->target2 * 2) | (renderer->blendEffect * 4),
1200 renderer->blendEffect == BLEND_ALPHA ? renderer->blda : renderer->bldy, renderer->bldb);
1201 if (renderer->scale > 1) {
1202 glUniform2iv(uniforms[GBA_GL_BG_OFFSET], 4, (GLint[]) {
1203 background->affine[0].sx, background->affine[0].sy,
1204 background->affine[1].sx, background->affine[1].sy,
1205 background->affine[2].sx, background->affine[2].sy,
1206 background->affine[3].sx, background->affine[3].sy,
1207 });
1208 glUniform2iv(uniforms[GBA_GL_BG_TRANSFORM], 4, (GLint[]) {
1209 background->affine[0].dx, background->affine[0].dy,
1210 background->affine[1].dx, background->affine[1].dy,
1211 background->affine[2].dx, background->affine[2].dy,
1212 background->affine[3].dx, background->affine[3].dy,
1213 });
1214 } else {
1215 glUniform2iv(uniforms[GBA_GL_BG_OFFSET], 4, (GLint[]) {
1216 background->affine[0].sx, background->affine[0].sy,
1217 background->affine[0].sx, background->affine[0].sy,
1218 background->affine[0].sx, background->affine[0].sy,
1219 background->affine[0].sx, background->affine[0].sy,
1220 });
1221 glUniform2iv(uniforms[GBA_GL_BG_TRANSFORM], 4, (GLint[]) {
1222 background->affine[0].dx, background->affine[0].dy,
1223 background->affine[0].dx, background->affine[0].dy,
1224 background->affine[0].dx, background->affine[0].dy,
1225 background->affine[0].dx, background->affine[0].dy,
1226 });
1227 }
1228 glDrawBuffers(2, (GLenum[]) { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 });
1229 glEnableVertexAttribArray(0);
1230 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1231 glDrawBuffers(1, (GLenum[]) { GL_COLOR_ATTACHMENT0 });
1232}
1233
1234static void _clearWindow(GBAWindowControl window, int start, int end, int y, int scale) {
1235 glScissor(start, y, end - start, scale);
1236 window = ~window & 0xFF;
1237 glClearColor((window & 0xF) / 32.f, (window >> 4) / 32.f, 0, 0);
1238 glClear(GL_COLOR_BUFFER_BIT);
1239}
1240
1241void GBAVideoGLRendererDrawWindow(struct GBAVideoGLRenderer* renderer, int y) {
1242 glBindFramebuffer(GL_FRAMEBUFFER, renderer->fbo[GBA_GL_FBO_WINDOW]);
1243 int dispcnt = ((renderer->dispcnt >> 8) & 0x1F) | 0x20;
1244 if (!(renderer->dispcnt & 0xE000)) {
1245 _clearWindow(dispcnt, 0, GBA_VIDEO_HORIZONTAL_PIXELS * renderer->scale, y * renderer->scale, renderer->scale);
1246 } else {
1247 _clearWindow(renderer->winout & dispcnt, 0, GBA_VIDEO_HORIZONTAL_PIXELS * renderer->scale, y * renderer->scale, renderer->scale);
1248 if (GBARegisterDISPCNTIsWin1Enable(renderer->dispcnt) && y >= renderer->winN[1].v.start && y < renderer->winN[1].v.end) {
1249 _clearWindow(renderer->winN[1].control & dispcnt, renderer->winN[1].h.start * renderer->scale, renderer->winN[1].h.end * renderer->scale, y * renderer->scale, renderer->scale);
1250 }
1251 if (GBARegisterDISPCNTIsWin0Enable(renderer->dispcnt) && y >= renderer->winN[0].v.start && y < renderer->winN[0].v.end) {
1252 _clearWindow(renderer->winN[0].control & dispcnt, renderer->winN[0].h.start * renderer->scale, renderer->winN[0].h.end * renderer->scale, y * renderer->scale, renderer->scale);
1253 }
1254 }
1255}
1256
1257#endif