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