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