From 9e90b0633b09b28f7906ceb6cf643cada1ff69eb Mon Sep 17 00:00:00 2001 From: pogokeen Date: Mon, 25 Jun 2018 14:53:46 +0000 Subject: [PATCH] glsurface.h/.cpp: change how the palette is handled and fix tinting/fading git-svn-id: https://svn.eduke32.com/eduke32@6932 1a8010ca-5511-0410-912e-c29ae57300e0 --- source/build/include/glsurface.h | 10 ++++------ source/build/src/glsurface.cpp | 34 +++++++++++++------------------- source/build/src/palette.cpp | 10 +++++----- source/build/src/sdlayer.cpp | 2 +- 4 files changed, 24 insertions(+), 32 deletions(-) diff --git a/source/build/include/glsurface.h b/source/build/include/glsurface.h index dd04b06ec..880fe9214 100644 --- a/source/build/include/glsurface.h +++ b/source/build/include/glsurface.h @@ -9,7 +9,6 @@ #define GLSURFACE_H_ #include "compat.h" -#include "palette.h" // Initialize the glsurface with the Software renderer's buffer resolution. // If the Software renderer's resolution and the actual resolution don't match, @@ -21,9 +20,9 @@ bool glsurface_initialize(vec2_t inputBufferResolution); // Destroy an existing surface. void glsurface_destroy(); -// Sets the palette at paletteID to contain the byte buffer pointed to by pPalette. +// Sets the palette to contain the RGBA byte buffer pointed to by pPalette. // If the surface is not initialized, the function returns immediately. -void glsurface_setPalette(int32_t paletteID, void* pPalette); +void glsurface_setPalette(void* pPalette); // Returns a pointer to the start of the surface's pixel buffer // Returns NULL if the surface is not initialized. @@ -32,10 +31,9 @@ void* glsurface_getBuffer(); // Returns the resolution of the surface's buffer vec2_t glsurface_getBufferResolution(); -// Blit the surface's pixel buffer to the screen. -// paletteID is the id of a palette previously set with glsurface_setPalette(). +// Blit the surface's pixel buffer to the screen using the palette set with glsurface_setPalette(). // Renders as soon as the data has been uploaded. // If the surface is not initialized, the function returns immediately. -void glsurface_blitBuffer(int32_t paletteID); +void glsurface_blitBuffer(); #endif /* GLSURFACE_H_ */ diff --git a/source/build/src/glsurface.cpp b/source/build/src/glsurface.cpp index 4c861b0f0..5344ae245 100644 --- a/source/build/src/glsurface.cpp +++ b/source/build/src/glsurface.cpp @@ -15,7 +15,7 @@ static void* buffer; static GLuint bufferTexID; static vec2_t bufferRes; -static GLuint paletteTexIDs[MAXBASEPALS]; +static GLuint paletteTexID; static GLuint quadVertsID = 0; @@ -97,6 +97,8 @@ bool glsurface_initialize(vec2_t inputBufferResolution) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, bufferRes.x, bufferRes.y, 0, GL_RED, GL_UNSIGNED_BYTE, 0); + glsurface_setPalette(curpalettefaded); + const char* const VERTEX_SHADER_CODE = "#version 110\n\ \n\ @@ -127,7 +129,7 @@ bool glsurface_initialize(vec2_t inputBufferResolution) {\n\ vec4 color = texture2D(s_texture, v_texCoord.xy);\n\ color.r = c_paletteOffset + c_paletteScale*color.r;\n\ - color = texture2D(s_palette, color.rg);\n\ + color.rgb = texture2D(s_palette, color.rg).rgb;\n\ \n\ // DEBUG \n\ //color = texture2D(s_palette, v_texCoord.xy);\n\ @@ -156,11 +158,6 @@ bool glsurface_initialize(vec2_t inputBufferResolution) glUniform1i(texSamplerLoc, 0); glUniform1i(paletteSamplerLoc, 1); - for (int basepalnum = 0; basepalnum < MAXBASEPALS; ++basepalnum) - { - glsurface_setPalette(basepalnum, basepaltable[basepalnum]); - } - return true; } @@ -176,14 +173,14 @@ void glsurface_destroy() glDeleteTextures(1, &bufferTexID); bufferTexID = 0; - glDeleteTextures(MAXBASEPALS, paletteTexIDs); - memset(paletteTexIDs, 0, sizeof(paletteTexIDs)); + glDeleteTextures(1, &paletteTexID); + paletteTexID = 0; glDeleteProgram(shaderProgramID); shaderProgramID = 0; } -void glsurface_setPalette(int32_t paletteID, void* pPalette) +void glsurface_setPalette(void* pPalette) { if (!buffer) return; @@ -191,15 +188,15 @@ void glsurface_setPalette(int32_t paletteID, void* pPalette) return; glActiveTexture(GL_TEXTURE1); - if (paletteTexIDs[paletteID]) + if (paletteTexID) { - glBindTexture(GL_TEXTURE_2D, paletteTexIDs[paletteID]); - glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 256, 1, GL_RGB, GL_UNSIGNED_BYTE, (void*) buffer); + // assume the texture is already bound to GL_TEXTURE1 + glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 256, 1, GL_RGBA, GL_UNSIGNED_BYTE, (void*) pPalette); } else { - glGenTextures(1, paletteTexIDs+paletteID); - glBindTexture(GL_TEXTURE_2D, paletteTexIDs[paletteID]); + glGenTextures(1, &paletteTexID); + glBindTexture(GL_TEXTURE_2D, paletteTexID); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); @@ -207,7 +204,7 @@ void glsurface_setPalette(int32_t paletteID, void* pPalette) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 1); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, 256, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, pPalette); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, pPalette); } } @@ -221,14 +218,11 @@ vec2_t glsurface_getBufferResolution() return bufferRes; } -void glsurface_blitBuffer(int32_t paletteID) +void glsurface_blitBuffer() { if (!buffer) return; - glActiveTexture(GL_TEXTURE1); - glBindTexture(GL_TEXTURE_2D, paletteTexIDs[paletteID]); - glActiveTexture(GL_TEXTURE0); glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bufferRes.x, bufferRes.y, GL_RED, GL_UNSIGNED_BYTE, (void*) buffer); diff --git a/source/build/src/palette.cpp b/source/build/src/palette.cpp index 6d2ab961f..85f6bb44c 100644 --- a/source/build/src/palette.cpp +++ b/source/build/src/palette.cpp @@ -660,11 +660,7 @@ void paletteSetColorTable(int32_t id, uint8_t const * const table) Bmemcpy(basepaltable[id], table, 768); #ifdef USE_OPENGL - if (videoGetRenderMode() == REND_CLASSIC) - { - glsurface_setPalette(id, basepaltable[id]); - } - else if (videoGetRenderMode() >= REND_POLYMOST) + if (videoGetRenderMode() >= REND_POLYMOST) { uploadbasepalette(id); } @@ -746,6 +742,7 @@ void videoSetPalette(char dabrightness, uint8_t dapalid, uint8_t flags) if (palsumdidchange || newpalettesum != g_lastpalettesum) { + glsurface_setPalette(curpalettefaded); // if ((flags&1) == 0) videoUpdatePalette(0, 256); } @@ -828,7 +825,10 @@ void videoFadePalette(uint8_t r, uint8_t g, uint8_t b, uint8_t offset) uint32_t newpalettesum = XXH32((uint8_t *) curpalettefaded, sizeof(curpalettefaded), sizeof(curpalettefaded)); if (newpalettesum != lastpalettesum || newpalettesum != g_lastpalettesum) + { + glsurface_setPalette(curpalettefaded); videoUpdatePalette(0, 256); + } g_lastpalettesum = lastpalettesum = newpalettesum; } diff --git a/source/build/src/sdlayer.cpp b/source/build/src/sdlayer.cpp index 86712e9b5..c327884ae 100644 --- a/source/build/src/sdlayer.cpp +++ b/source/build/src/sdlayer.cpp @@ -1820,7 +1820,7 @@ void videoShowFrame(int32_t w) } else { - glsurface_blitBuffer(curbasepal); + glsurface_blitBuffer(); } static uint32_t lastSwapTime = 0;