GBA Video: New GL palette approach, no more batch splitting on palette edits

This commit is contained in:
Vicki Pfau 2020-11-30 20:53:37 -08:00
parent 60ec3e0e99
commit fc3a6153e2
3 changed files with 83 additions and 56 deletions

View File

@ -93,6 +93,7 @@ Misc:
- GBA BIOS: Division by zero should emit a FATAL error - GBA BIOS: Division by zero should emit a FATAL error
- GBA Video: Convert OpenGL VRAM texture to integer - GBA Video: Convert OpenGL VRAM texture to integer
- GBA Video: Skip attempting to render offscreen sprites in OpenGL - GBA Video: Skip attempting to render offscreen sprites in OpenGL
- GBA Video: New GL palette approach, no more batch splitting on palette edits
- Debugger: Keep track of global cycle count - Debugger: Keep track of global cycle count
- FFmpeg: Add looping option for GIF/APNG - FFmpeg: Add looping option for GIF/APNG
- mGUI: Show battery percentage - mGUI: Show battery percentage

View File

@ -79,8 +79,7 @@ enum {
GBA_GL_TEX_OBJ_COLOR = 0, GBA_GL_TEX_OBJ_COLOR = 0,
GBA_GL_TEX_OBJ_FLAGS, GBA_GL_TEX_OBJ_FLAGS,
GBA_GL_TEX_OBJ_DEPTH, GBA_GL_TEX_OBJ_DEPTH,
GBA_GL_TEX_BACKDROP_COLOR, GBA_GL_TEX_BACKDROP,
GBA_GL_TEX_BACKDROP_FLAGS,
GBA_GL_TEX_WINDOW, GBA_GL_TEX_WINDOW,
GBA_GL_TEX_MAX GBA_GL_TEX_MAX
}; };
@ -121,8 +120,8 @@ enum {
GBA_GL_FINALIZE_LAYERS, GBA_GL_FINALIZE_LAYERS,
GBA_GL_FINALIZE_FLAGS, GBA_GL_FINALIZE_FLAGS,
GBA_GL_FINALIZE_WINDOW, GBA_GL_FINALIZE_WINDOW,
GBA_GL_FINALIZE_PALETTE,
GBA_GL_FINALIZE_BACKDROP, GBA_GL_FINALIZE_BACKDROP,
GBA_GL_FINALIZE_BACKDROPFLAGS,
GBA_GL_UNIFORM_MAX = 14 GBA_GL_UNIFORM_MAX = 14
}; };
@ -150,7 +149,10 @@ struct GBAVideoGLRenderer {
GLuint outputTex; GLuint outputTex;
GLint shadowPalette[512]; GLuint paletteTex;
uint16_t shadowPalette[GBA_VIDEO_VERTICAL_PIXELS][512];
int nextPalette;
int lastPalette;
bool paletteDirty; bool paletteDirty;
GLuint vramTex; GLuint vramTex;

View File

@ -82,29 +82,25 @@ static const char* const _vertexShader =
"}"; "}";
static const char* const _renderTile16 = static const char* const _renderTile16 =
"vec4 renderTile(int tile, int paletteId, ivec2 localCoord) {\n" "int renderTile(int tile, int paletteId, ivec2 localCoord) {\n"
" int address = charBase + tile * 16 + (localCoord.x >> 2) + (localCoord.y << 1);\n" " int address = charBase + tile * 16 + (localCoord.x >> 2) + (localCoord.y << 1);\n"
" int halfrow = texelFetch(vram, ivec2(address & 255, address >> 8), 0).r;\n" " int halfrow = texelFetch(vram, ivec2(address & 255, address >> 8), 0).r;\n"
" int entry = (halfrow >> (4 * (localCoord.x & 3))) & 15;\n" " int entry = (halfrow >> (4 * (localCoord.x & 3))) & 15;\n"
" if (entry == 0) {\n" " if (entry == 0) {\n"
" discard;\n" " discard;\n"
" }\n" " }\n"
" int paletteEntry = palette[paletteId * 16 + entry];\n" " return paletteId * 16 + entry;\n"
" vec4 color = vec4(PALETTE_ENTRY(paletteEntry), 1.);\n"
" return color;\n"
"}"; "}";
static const char* const _renderTile256 = static const char* const _renderTile256 =
"vec4 renderTile(int tile, int paletteId, ivec2 localCoord) {\n" "int renderTile(int tile, int paletteId, ivec2 localCoord) {\n"
" int address = charBase + tile * 32 + (localCoord.x >> 1) + (localCoord.y << 2);\n" " int address = charBase + tile * 32 + (localCoord.x >> 1) + (localCoord.y << 2);\n"
" int halfrow = texelFetch(vram, ivec2(address & 255, address >> 8), 0).r;\n" " int halfrow = texelFetch(vram, ivec2(address & 255, address >> 8), 0).r;\n"
" int entry = (halfrow >> (8 * (localCoord.x & 1))) & 255;\n" " int entry = (halfrow >> (8 * (localCoord.x & 1))) & 255;\n"
" if (entry == 0) {\n" " if (entry == 0) {\n"
" discard;\n" " discard;\n"
" }\n" " }\n"
" int paletteEntry = palette[entry];\n" " return entry;\n"
" vec4 color = vec4(PALETTE_ENTRY(paletteEntry), 1.);\n"
" return color;\n"
"}"; "}";
static const struct GBAVideoGLUniform _uniformsMode0[] = { static const struct GBAVideoGLUniform _uniformsMode0[] = {
@ -123,7 +119,7 @@ static const struct GBAVideoGLUniform _uniformsMode0[] = {
static const char* const _renderMode0 = static const char* const _renderMode0 =
"in vec2 texCoord;\n" "in vec2 texCoord;\n"
"uniform isampler2D vram;\n" "uniform isampler2D vram;\n"
"uniform int palette[256];\n" "uniform sampler2D palette;\n"
"uniform int screenBase;\n" "uniform int screenBase;\n"
"uniform int charBase;\n" "uniform int charBase;\n"
"uniform int size;\n" "uniform int size;\n"
@ -131,7 +127,7 @@ static const char* const _renderMode0 =
"uniform ivec2 mosaic;\n" "uniform ivec2 mosaic;\n"
"OUT(0) out vec4 color;\n" "OUT(0) out vec4 color;\n"
"vec4 renderTile(int tile, int paletteId, ivec2 localCoord);\n" "int renderTile(int tile, int paletteId, ivec2 localCoord);\n"
"void main() {\n" "void main() {\n"
" ivec2 coord = ivec2(texCoord);\n" " ivec2 coord = ivec2(texCoord);\n"
@ -165,18 +161,19 @@ static const char* const _renderMode0 =
" coord.y ^= 7;\n" " coord.y ^= 7;\n"
" }\n" " }\n"
" int tile = map & 1023;\n" " int tile = map & 1023;\n"
" color = renderTile(tile, map >> 12, coord & 7);\n" " int paletteEntry = renderTile(tile, map >> 12, coord & 7);\n"
" color = texelFetch(palette, ivec2(paletteEntry, int(texCoord.y)), 0);\n"
"}"; "}";
static const char* const _fetchTileOverflow = static const char* const _fetchTileOverflow =
"vec4 fetchTile(ivec2 coord) {\n" "int fetchTile(ivec2 coord) {\n"
" int sizeAdjusted = (0x8000 << size) - 1;\n" " int sizeAdjusted = (0x8000 << size) - 1;\n"
" coord &= sizeAdjusted;\n" " coord &= sizeAdjusted;\n"
" return renderTile(coord);\n" " return renderTile(coord);\n"
"}"; "}";
static const char* const _fetchTileNoOverflow = static const char* const _fetchTileNoOverflow =
"vec4 fetchTile(ivec2 coord) {\n" "int fetchTile(ivec2 coord) {\n"
" int sizeAdjusted = (0x8000 << size) - 1;\n" " int sizeAdjusted = (0x8000 << size) - 1;\n"
" ivec2 outerCoord = coord & ~sizeAdjusted;\n" " ivec2 outerCoord = coord & ~sizeAdjusted;\n"
" if ((outerCoord.x | outerCoord.y) != 0) {\n" " if ((outerCoord.x | outerCoord.y) != 0) {\n"
@ -224,7 +221,7 @@ static const char* const _interpolate =
static const char* const _renderMode2 = static const char* const _renderMode2 =
"in vec2 texCoord;\n" "in vec2 texCoord;\n"
"uniform isampler2D vram;\n" "uniform isampler2D vram;\n"
"uniform int palette[256];\n" "uniform sampler2D palette;\n"
"uniform int screenBase;\n" "uniform int screenBase;\n"
"uniform int charBase;\n" "uniform int charBase;\n"
"uniform int size;\n" "uniform int size;\n"
@ -233,11 +230,11 @@ static const char* const _renderMode2 =
"uniform ivec2 mosaic;\n" "uniform ivec2 mosaic;\n"
"OUT(0) out vec4 color;\n" "OUT(0) out vec4 color;\n"
"vec4 fetchTile(ivec2 coord);\n" "int fetchTile(ivec2 coord);\n"
"vec2 interpolate(ivec2 arr[4], float x);\n" "vec2 interpolate(ivec2 arr[4], float x);\n"
"void loadAffine(int y, out ivec2 mat[4], out ivec2 aff[4]);\n" "void loadAffine(int y, out ivec2 mat[4], out ivec2 aff[4]);\n"
"vec4 renderTile(ivec2 coord) {\n" "int renderTile(ivec2 coord) {\n"
" int map = (coord.x >> 11) + (((coord.y >> 7) & 0x7F0) << size);\n" " int map = (coord.x >> 11) + (((coord.y >> 7) & 0x7F0) << size);\n"
" int mapAddress = screenBase + (map >> 1);\n" " int mapAddress = screenBase + (map >> 1);\n"
" int twomaps = texelFetch(vram, ivec2(mapAddress & 255, mapAddress >> 8), 0).r;\n" " int twomaps = texelFetch(vram, ivec2(mapAddress & 255, mapAddress >> 8), 0).r;\n"
@ -248,9 +245,7 @@ static const char* const _renderMode2 =
" if (entry == 0) {\n" " if (entry == 0) {\n"
" discard;\n" " discard;\n"
" }\n" " }\n"
" int paletteEntry = palette[entry];\n" " return entry;\n"
" vec4 color = vec4(PALETTE_ENTRY(paletteEntry), 1.);\n"
" return color;\n"
"}\n" "}\n"
"void main() {\n" "void main() {\n"
@ -273,7 +268,8 @@ static const char* const _renderMode2 =
" float lin = start + y * 0.25;\n" " float lin = start + y * 0.25;\n"
" vec2 mixedTransform = interpolate(mat, lin);\n" " vec2 mixedTransform = interpolate(mat, lin);\n"
" vec2 mixedOffset = interpolate(offset, lin);\n" " vec2 mixedOffset = interpolate(offset, lin);\n"
" color = fetchTile(ivec2(mixedTransform * incoord.x + mixedOffset));\n" " int paletteEntry = fetchTile(ivec2(mixedTransform * incoord.x + mixedOffset));\n"
" color = texelFetch(palette, ivec2(paletteEntry, int(texCoord.y)), 0);\n"
"}"; "}";
static const struct GBAVideoGLUniform _uniformsMode35[] = { static const struct GBAVideoGLUniform _uniformsMode35[] = {
@ -351,7 +347,7 @@ static const struct GBAVideoGLUniform _uniformsMode4[] = {
static const char* const _renderMode4 = static const char* const _renderMode4 =
"in vec2 texCoord;\n" "in vec2 texCoord;\n"
"uniform isampler2D vram;\n" "uniform isampler2D vram;\n"
"uniform int palette[256];\n" "uniform sampler2D palette;\n"
"uniform int charBase;\n" "uniform int charBase;\n"
"uniform ivec2 size;\n" "uniform ivec2 size;\n"
"uniform ivec4 transform[160];\n" "uniform ivec4 transform[160];\n"
@ -395,8 +391,7 @@ static const char* const _renderMode4 =
" if (entry == 0) {\n" " if (entry == 0) {\n"
" discard;\n" " discard;\n"
" }\n" " }\n"
" int paletteEntry = palette[entry];\n" " color = texelFetch(palette, ivec2(entry, int(texCoord.y)), 0);\n"
" color = vec4(PALETTE_ENTRY(paletteEntry), 1.);\n"
"}"; "}";
static const struct GBAVideoGLUniform _uniformsObj[] = { static const struct GBAVideoGLUniform _uniformsObj[] = {
@ -419,7 +414,7 @@ static const struct GBAVideoGLUniform _uniformsObj[] = {
static const char* const _renderObj = static const char* const _renderObj =
"in vec2 texCoord;\n" "in vec2 texCoord;\n"
"uniform isampler2D vram;\n" "uniform isampler2D vram;\n"
"uniform int palette[256];\n" "uniform sampler2D palette;\n"
"uniform int charBase;\n" "uniform int charBase;\n"
"uniform int stride;\n" "uniform int stride;\n"
"uniform int localPalette;\n" "uniform int localPalette;\n"
@ -433,7 +428,7 @@ static const char* const _renderObj =
"OUT(1) out ivec4 flags;\n" "OUT(1) out ivec4 flags;\n"
"OUT(2) out ivec4 window;\n" "OUT(2) out ivec4 window;\n"
"vec4 renderTile(int tile, int paletteId, ivec2 localCoord);\n" "int renderTile(int tile, int paletteId, ivec2 localCoord);\n"
"void main() {\n" "void main() {\n"
" vec2 incoord = texCoord;\n" " vec2 incoord = texCoord;\n"
@ -455,8 +450,8 @@ static const char* const _renderObj =
" if ((coord & ~(dims.xy - 1)) != ivec2(0, 0)) {\n" " if ((coord & ~(dims.xy - 1)) != ivec2(0, 0)) {\n"
" discard;\n" " discard;\n"
" }\n" " }\n"
" vec4 pix = renderTile((coord.x >> 3) + (coord.y >> 3) * stride, localPalette, coord & 7);\n" " int paletteEntry = renderTile((coord.x >> 3) + (coord.y >> 3) * stride, localPalette, coord & 7);\n"
" color = pix;\n" " color = texelFetch(palette, ivec2(paletteEntry + 256, coord.y), 0);\n"
" flags = inflags;\n" " flags = inflags;\n"
" gl_FragDepth = float(flags.x) / 16.;\n" " gl_FragDepth = float(flags.x) / 16.;\n"
" window = ivec4(objwin, 0);\n" " window = ivec4(objwin, 0);\n"
@ -551,8 +546,8 @@ static const struct GBAVideoGLUniform _uniformsFinalize[] = {
{ "layers", GBA_GL_FINALIZE_LAYERS, }, { "layers", GBA_GL_FINALIZE_LAYERS, },
{ "objFlags", GBA_GL_FINALIZE_FLAGS, }, { "objFlags", GBA_GL_FINALIZE_FLAGS, },
{ "window", GBA_GL_FINALIZE_WINDOW, }, { "window", GBA_GL_FINALIZE_WINDOW, },
{ "backdrop", GBA_GL_FINALIZE_BACKDROP, }, { "palette", GBA_GL_FINALIZE_PALETTE, },
{ "backdropFlags", GBA_GL_FINALIZE_BACKDROPFLAGS, }, { "backdropFlags", GBA_GL_FINALIZE_BACKDROP, },
{ 0 } { 0 }
}; };
@ -562,7 +557,7 @@ static const char* const _finalize =
"uniform sampler2D layers[5];\n" "uniform sampler2D layers[5];\n"
"uniform isampler2D objFlags;\n" "uniform isampler2D objFlags;\n"
"uniform isampler2D window;\n" "uniform isampler2D window;\n"
"uniform sampler2D backdrop;\n" "uniform sampler2D palette;\n"
"uniform isampler2D backdropFlags;\n" "uniform isampler2D backdropFlags;\n"
"out vec4 color;\n" "out vec4 color;\n"
@ -582,7 +577,7 @@ static const char* const _finalize =
"}\n" "}\n"
"void main() {\n" "void main() {\n"
" vec4 topPixel = texelFetch(backdrop, ivec2(0, texCoord.y), 0);\n" " vec4 topPixel = texelFetch(palette, ivec2(0, texCoord.y), 0);\n"
" vec4 bottomPixel = topPixel;\n" " vec4 bottomPixel = topPixel;\n"
" ivec4 topFlags = ivec4(texelFetch(backdropFlags, ivec2(0, texCoord.y), 0));\n" " ivec4 topFlags = ivec4(texelFetch(backdropFlags, ivec2(0, texCoord.y), 0));\n"
" ivec4 bottomFlags = topFlags;\n" " ivec4 bottomFlags = topFlags;\n"
@ -745,8 +740,7 @@ static void _initFramebuffers(struct GBAVideoGLRenderer* glRenderer) {
_initFramebufferTextureEx(glRenderer->layers[GBA_GL_TEX_OBJ_DEPTH], GL_DEPTH24_STENCIL8, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, GL_DEPTH_STENCIL_ATTACHMENT, glRenderer->scale); _initFramebufferTextureEx(glRenderer->layers[GBA_GL_TEX_OBJ_DEPTH], GL_DEPTH24_STENCIL8, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, GL_DEPTH_STENCIL_ATTACHMENT, glRenderer->scale);
glBindFramebuffer(GL_FRAMEBUFFER, glRenderer->fbo[GBA_GL_FBO_BACKDROP]); glBindFramebuffer(GL_FRAMEBUFFER, glRenderer->fbo[GBA_GL_FBO_BACKDROP]);
_initFramebufferTexture(glRenderer->layers[GBA_GL_TEX_BACKDROP_COLOR], GL_RGB, GL_COLOR_ATTACHMENT0, 0); _initFramebufferTextureEx(glRenderer->layers[GBA_GL_TEX_BACKDROP], GL_RGBA8I, GL_RGBA_INTEGER, GL_BYTE, GL_COLOR_ATTACHMENT0, 0);
_initFramebufferTextureEx(glRenderer->layers[GBA_GL_TEX_BACKDROP_FLAGS], GL_RGBA8I, GL_RGBA_INTEGER, GL_BYTE, GL_COLOR_ATTACHMENT1, 0);
glBindFramebuffer(GL_FRAMEBUFFER, glRenderer->fbo[GBA_GL_FBO_WINDOW]); glBindFramebuffer(GL_FRAMEBUFFER, glRenderer->fbo[GBA_GL_FBO_WINDOW]);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, glRenderer->layers[GBA_GL_TEX_WINDOW], 0); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, glRenderer->layers[GBA_GL_TEX_WINDOW], 0);
@ -776,6 +770,12 @@ void GBAVideoGLRendererInit(struct GBAVideoRenderer* renderer) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_R16UI, 256, 192, 0, GL_RED_INTEGER, GL_UNSIGNED_SHORT, 0); glTexImage2D(GL_TEXTURE_2D, 0, GL_R16UI, 256, 192, 0, GL_RED_INTEGER, GL_UNSIGNED_SHORT, 0);
glGenTextures(1, &glRenderer->paletteTex);
glBindTexture(GL_TEXTURE_2D, glRenderer->paletteTex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 512, GBA_VIDEO_VERTICAL_PIXELS, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, 0);
glGenBuffers(1, &glRenderer->vbo); glGenBuffers(1, &glRenderer->vbo);
glBindBuffer(GL_ARRAY_BUFFER, glRenderer->vbo); glBindBuffer(GL_ARRAY_BUFFER, glRenderer->vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(_vertices), _vertices, GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, sizeof(_vertices), _vertices, GL_STATIC_DRAW);
@ -889,6 +889,7 @@ void GBAVideoGLRendererDeinit(struct GBAVideoRenderer* renderer) {
glDeleteFramebuffers(GBA_GL_FBO_MAX, glRenderer->fbo); glDeleteFramebuffers(GBA_GL_FBO_MAX, glRenderer->fbo);
glDeleteTextures(GBA_GL_TEX_MAX, glRenderer->layers); glDeleteTextures(GBA_GL_TEX_MAX, glRenderer->layers);
glDeleteTextures(1, &glRenderer->vramTex); glDeleteTextures(1, &glRenderer->vramTex);
glDeleteTextures(1, &glRenderer->paletteTex);
glDeleteBuffers(1, &glRenderer->vbo); glDeleteBuffers(1, &glRenderer->vbo);
_deleteShader(&glRenderer->bgShader[0]); _deleteShader(&glRenderer->bgShader[0]);
@ -918,6 +919,8 @@ void GBAVideoGLRendererReset(struct GBAVideoRenderer* renderer) {
glRenderer->firstY = -1; glRenderer->firstY = -1;
glRenderer->dispcnt = 0x0080; glRenderer->dispcnt = 0x0080;
glRenderer->mosaic = 0; glRenderer->mosaic = 0;
glRenderer->nextPalette = 0;
glRenderer->lastPalette = 1;
memset(glRenderer->shadowRegs, 0, sizeof(glRenderer->shadowRegs)); memset(glRenderer->shadowRegs, 0, sizeof(glRenderer->shadowRegs));
glRenderer->regsDirty = 0xFFFFFFFFFFFEULL; glRenderer->regsDirty = 0xFFFFFFFFFFFEULL;
} }
@ -938,6 +941,16 @@ void GBAVideoGLRendererWritePalette(struct GBAVideoRenderer* renderer, uint32_t
UNUSED(address); UNUSED(address);
UNUSED(value); UNUSED(value);
glRenderer->paletteDirty = true; glRenderer->paletteDirty = true;
int r = M_R5(value);
int g = M_G5(value) << 1;
g |= g >> 5;
int b = M_B5(value);
if (glRenderer->nextPalette) {
glRenderer->lastPalette = glRenderer->nextPalette - 1;
} else {
glRenderer->lastPalette = GBA_VIDEO_VERTICAL_PIXELS - 1;
}
glRenderer->shadowPalette[glRenderer->nextPalette][address >> 1] = (r << 11) | (g << 5) | b;
} }
uint16_t GBAVideoGLRendererWriteVideoRegister(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) { uint16_t GBAVideoGLRendererWriteVideoRegister(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
@ -1291,7 +1304,7 @@ void GBAVideoGLRendererDrawScanline(struct GBAVideoRenderer* renderer, int y) {
glRenderer->firstAffine = -1; glRenderer->firstAffine = -1;
} }
if (glRenderer->paletteDirty || _needsVramUpload(glRenderer, y) || glRenderer->oamDirty || glRenderer->regsDirty) { if (_needsVramUpload(glRenderer, y) || glRenderer->oamDirty || glRenderer->regsDirty) {
if (glRenderer->firstY >= 0) { if (glRenderer->firstY >= 0) {
_drawScanlines(glRenderer, y - 1); _drawScanlines(glRenderer, y - 1);
glBindVertexArray(0); glBindVertexArray(0);
@ -1336,11 +1349,19 @@ void GBAVideoGLRendererDrawScanline(struct GBAVideoRenderer* renderer, int y) {
glRenderer->bg[3].scanlineAffine[y * 4 + 2] = glRenderer->bg[3].affine.sx; glRenderer->bg[3].scanlineAffine[y * 4 + 2] = glRenderer->bg[3].affine.sx;
glRenderer->bg[3].scanlineAffine[y * 4 + 3] = glRenderer->bg[3].affine.sy; glRenderer->bg[3].scanlineAffine[y * 4 + 3] = glRenderer->bg[3].affine.sy;
int oldPalette = glRenderer->nextPalette;
glRenderer->nextPalette = y + 1;
if (glRenderer->nextPalette >= GBA_VIDEO_VERTICAL_PIXELS) {
glRenderer->nextPalette = 0;
}
if (glRenderer->paletteDirty) { if (glRenderer->paletteDirty) {
for (i = 0; i < 512; ++i) { memcpy(glRenderer->shadowPalette[glRenderer->nextPalette], glRenderer->shadowPalette[oldPalette], sizeof(glRenderer->shadowPalette[0]));
glRenderer->shadowPalette[i] = glRenderer->d.palette[i]; if (glRenderer->nextPalette == glRenderer->lastPalette) {
glRenderer->paletteDirty = false;
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, glRenderer->paletteTex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 512, GBA_VIDEO_VERTICAL_PIXELS, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, glRenderer->shadowPalette);
} }
glRenderer->paletteDirty = false;
} }
if (_needsVramUpload(glRenderer, y)) { if (_needsVramUpload(glRenderer, y)) {
@ -1400,24 +1421,24 @@ void GBAVideoGLRendererDrawScanline(struct GBAVideoRenderer* renderer, int y) {
void _drawScanlines(struct GBAVideoGLRenderer* glRenderer, int y) { void _drawScanlines(struct GBAVideoGLRenderer* glRenderer, int y) {
glEnable(GL_SCISSOR_TEST); glEnable(GL_SCISSOR_TEST);
uint32_t backdrop = M_RGB5_TO_RGB8(glRenderer->shadowPalette[0]);
glViewport(0, 0, 1, GBA_VIDEO_VERTICAL_PIXELS); glViewport(0, 0, 1, GBA_VIDEO_VERTICAL_PIXELS);
glScissor(0, glRenderer->firstY, 1, y - glRenderer->firstY + 1); glScissor(0, glRenderer->firstY, 1, y - glRenderer->firstY + 1);
glBindFramebuffer(GL_FRAMEBUFFER, glRenderer->fbo[GBA_GL_FBO_BACKDROP]); glBindFramebuffer(GL_FRAMEBUFFER, glRenderer->fbo[GBA_GL_FBO_BACKDROP]);
glDrawBuffers(2, (GLenum[]) { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 }); glDrawBuffers(1, (GLenum[]) { GL_COLOR_ATTACHMENT0 });
glClearBufferfv(GL_COLOR, 0, (GLfloat[]) { ((backdrop >> 16) & 0xF8) / 248., ((backdrop >> 8) & 0xF8) / 248., (backdrop & 0xF8) / 248., 1.f }); glClearBufferiv(GL_COLOR, 0, (GLint[]) { 32, glRenderer->target1Bd | (glRenderer->target2Bd * 2) | (glRenderer->blendEffect * 4), glRenderer->blda, 0 });
glClearBufferiv(GL_COLOR, 1, (GLint[]) { 32, glRenderer->target1Bd | (glRenderer->target2Bd * 2) | (glRenderer->blendEffect * 4), glRenderer->blda, 0 });
glDrawBuffers(2, (GLenum[]) { GL_NONE, GL_COLOR_ATTACHMENT1 });
int i; int i;
for (i = 0; i < 4; ++i) { for (i = 0; i < 4; ++i) {
glScissor(i + 1, glRenderer->firstY, 1, y - glRenderer->firstY + 1); glScissor(i + 1, glRenderer->firstY, 1, y - glRenderer->firstY + 1);
glClearBufferiv(GL_COLOR, 1, (GLint[]) { glRenderer->bg[i].priority, glClearBufferiv(GL_COLOR, 0, (GLint[]) { glRenderer->bg[i].priority,
glRenderer->bg[i].target1 | (glRenderer->bg[i].target2 << 1) | (glRenderer->blendEffect << 2), glRenderer->bg[i].target1 | (glRenderer->bg[i].target2 << 1) | (glRenderer->blendEffect << 2),
glRenderer->blda, 0 }); glRenderer->blda, 0 });
} }
glDrawBuffers(1, (GLenum[]) { GL_COLOR_ATTACHMENT0 }); if (glRenderer->paletteDirty) {
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, glRenderer->paletteTex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 512, GBA_VIDEO_VERTICAL_PIXELS, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, glRenderer->shadowPalette);
}
GBAVideoGLRendererDrawWindow(glRenderer, y); GBAVideoGLRendererDrawWindow(glRenderer, y);
if (GBARegisterDISPCNTIsObjEnable(glRenderer->dispcnt) && !glRenderer->d.disableOBJ) { if (GBARegisterDISPCNTIsObjEnable(glRenderer->dispcnt) && !glRenderer->d.disableOBJ) {
@ -1626,9 +1647,9 @@ void _finalizeLayers(struct GBAVideoGLRenderer* renderer) {
glActiveTexture(GL_TEXTURE0 + 6); glActiveTexture(GL_TEXTURE0 + 6);
glBindTexture(GL_TEXTURE_2D, renderer->bg[3].tex); glBindTexture(GL_TEXTURE_2D, renderer->bg[3].tex);
glActiveTexture(GL_TEXTURE0 + 7); glActiveTexture(GL_TEXTURE0 + 7);
glBindTexture(GL_TEXTURE_2D, renderer->layers[GBA_GL_TEX_BACKDROP_COLOR]); glBindTexture(GL_TEXTURE_2D, renderer->paletteTex);
glActiveTexture(GL_TEXTURE0 + 8); glActiveTexture(GL_TEXTURE0 + 8);
glBindTexture(GL_TEXTURE_2D, renderer->layers[GBA_GL_TEX_BACKDROP_FLAGS]); glBindTexture(GL_TEXTURE_2D, renderer->layers[GBA_GL_TEX_BACKDROP]);
glUniform2i(uniforms[GBA_GL_VS_LOC], GBA_VIDEO_VERTICAL_PIXELS, 0); glUniform2i(uniforms[GBA_GL_VS_LOC], GBA_VIDEO_VERTICAL_PIXELS, 0);
glUniform2i(uniforms[GBA_GL_VS_MAXPOS], GBA_VIDEO_HORIZONTAL_PIXELS, GBA_VIDEO_VERTICAL_PIXELS); glUniform2i(uniforms[GBA_GL_VS_MAXPOS], GBA_VIDEO_HORIZONTAL_PIXELS, GBA_VIDEO_VERTICAL_PIXELS);
@ -1636,9 +1657,8 @@ void _finalizeLayers(struct GBAVideoGLRenderer* renderer) {
glUniform1iv(uniforms[GBA_GL_FINALIZE_LAYERS], 5, (GLint[]) { 3, 4, 5, 6, 1 }); glUniform1iv(uniforms[GBA_GL_FINALIZE_LAYERS], 5, (GLint[]) { 3, 4, 5, 6, 1 });
glUniform1i(uniforms[GBA_GL_FINALIZE_FLAGS], 2); glUniform1i(uniforms[GBA_GL_FINALIZE_FLAGS], 2);
glUniform1i(uniforms[GBA_GL_FINALIZE_WINDOW], 0); glUniform1i(uniforms[GBA_GL_FINALIZE_WINDOW], 0);
glUniform1i(uniforms[GBA_GL_FINALIZE_WINDOW], 0); glUniform1i(uniforms[GBA_GL_FINALIZE_PALETTE], 7);
glUniform1i(uniforms[GBA_GL_FINALIZE_BACKDROP], 7); glUniform1i(uniforms[GBA_GL_FINALIZE_BACKDROP], 8);
glUniform1i(uniforms[GBA_GL_FINALIZE_BACKDROPFLAGS], 8);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4); glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
} }
glBindFramebuffer(GL_FRAMEBUFFER, 0); glBindFramebuffer(GL_FRAMEBUFFER, 0);
@ -1683,10 +1703,12 @@ void GBAVideoGLRendererDrawSprite(struct GBAVideoGLRenderer* renderer, struct GB
glBindVertexArray(shader->vao); glBindVertexArray(shader->vao);
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, renderer->vramTex); glBindTexture(GL_TEXTURE_2D, renderer->vramTex);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, renderer->paletteTex);
glUniform2i(uniforms[GBA_GL_VS_LOC], totalHeight, 0); glUniform2i(uniforms[GBA_GL_VS_LOC], totalHeight, 0);
glUniform2i(uniforms[GBA_GL_VS_MAXPOS], totalWidth, totalHeight); glUniform2i(uniforms[GBA_GL_VS_MAXPOS], totalWidth, totalHeight);
glUniform1i(uniforms[GBA_GL_OBJ_VRAM], 0); glUniform1i(uniforms[GBA_GL_OBJ_VRAM], 0);
glUniform1iv(uniforms[GBA_GL_OBJ_PALETTE], 256, &renderer->shadowPalette[256]); glUniform1i(uniforms[GBA_GL_OBJ_PALETTE], 1);
glUniform1i(uniforms[GBA_GL_OBJ_CHARBASE], charBase); glUniform1i(uniforms[GBA_GL_OBJ_CHARBASE], charBase);
glUniform1i(uniforms[GBA_GL_OBJ_STRIDE], stride); glUniform1i(uniforms[GBA_GL_OBJ_STRIDE], stride);
glUniform1i(uniforms[GBA_GL_OBJ_LOCALPALETTE], GBAObjAttributesCGetPalette(sprite->c)); glUniform1i(uniforms[GBA_GL_OBJ_LOCALPALETTE], GBAObjAttributesCGetPalette(sprite->c));
@ -1769,9 +1791,11 @@ void _prepareBackground(struct GBAVideoGLRenderer* renderer, struct GBAVideoGLBa
glViewport(0, 0, GBA_VIDEO_HORIZONTAL_PIXELS * renderer->scale, GBA_VIDEO_VERTICAL_PIXELS * renderer->scale); glViewport(0, 0, GBA_VIDEO_HORIZONTAL_PIXELS * renderer->scale, GBA_VIDEO_VERTICAL_PIXELS * renderer->scale);
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, renderer->vramTex); glBindTexture(GL_TEXTURE_2D, renderer->vramTex);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, renderer->paletteTex);
glUniform2i(uniforms[GBA_GL_VS_MAXPOS], GBA_VIDEO_HORIZONTAL_PIXELS, GBA_VIDEO_VERTICAL_PIXELS); glUniform2i(uniforms[GBA_GL_VS_MAXPOS], GBA_VIDEO_HORIZONTAL_PIXELS, GBA_VIDEO_VERTICAL_PIXELS);
glUniform1i(uniforms[GBA_GL_BG_VRAM], 0); glUniform1i(uniforms[GBA_GL_BG_VRAM], 0);
glUniform1iv(uniforms[GBA_GL_OBJ_PALETTE], 256, renderer->shadowPalette); glUniform1i(uniforms[GBA_GL_OBJ_PALETTE], 1);
if (background->mosaic) { if (background->mosaic) {
glUniform2i(uniforms[GBA_GL_BG_MOSAIC], GBAMosaicControlGetBgV(renderer->mosaic) + 1, GBAMosaicControlGetBgH(renderer->mosaic) + 1); glUniform2i(uniforms[GBA_GL_BG_MOSAIC], GBAMosaicControlGetBgV(renderer->mosaic) + 1, GBAMosaicControlGetBgH(renderer->mosaic) + 1);
} else { } else {