From 69237d6a3a8a1b6948ebb2711af17b17df82a574 Mon Sep 17 00:00:00 2001 From: hayaunderscore Date: Sun, 17 Sep 2023 07:23:09 +0800 Subject: [PATCH] Fix stuttering and gamma --- src/hardware/hw_cache.c | 1 - src/hardware/hw_main.c | 3 +- src/hardware/r_opengl/r_opengl.c | 51 ++++++++++++++++++++++---------- src/hardware/r_opengl/r_opengl.h | 2 +- src/v_video.c | 17 +++++++---- 5 files changed, 48 insertions(+), 26 deletions(-) diff --git a/src/hardware/hw_cache.c b/src/hardware/hw_cache.c index c27e17a16..bfd5fa26d 100644 --- a/src/hardware/hw_cache.c +++ b/src/hardware/hw_cache.c @@ -576,7 +576,6 @@ void HWR_SetPalette(RGBA_t *palette) Z_FreeTags(PU_HWRCACHE_UNLOCKED, PU_HWRCACHE_UNLOCKED); } } - // -------------------------------------------------------------------------- // Make sure texture is downloaded and set it as the source // -------------------------------------------------------------------------- diff --git a/src/hardware/hw_main.c b/src/hardware/hw_main.c index 68241585a..d07c3d4c0 100644 --- a/src/hardware/hw_main.c +++ b/src/hardware/hw_main.c @@ -151,8 +151,7 @@ static void CV_grpaletteshader_OnChange(void) { HWD.pfnSetSpecialState(HWD_SET_PALETTE_SHADER_ENABLED, cv_grpaletteshader.value); gr_use_palette_shader = cv_grpaletteshader.value; - //R_ClearColormaps(); - InitPalette(0); + V_SetPalette(0); } } diff --git a/src/hardware/r_opengl/r_opengl.c b/src/hardware/r_opengl/r_opengl.c index e75ac1e6d..67eec5058 100644 --- a/src/hardware/r_opengl/r_opengl.c +++ b/src/hardware/r_opengl/r_opengl.c @@ -1157,7 +1157,7 @@ EXPORT void HWRAPI(InitCustomShaders) (void) if (gl_use_palette_shader) { - InitPalette(0); + InitPalette(0, false); } #endif } @@ -1205,24 +1205,42 @@ GLuint palette_tex_num; // the +2 in the NearestColor call also needs to be adjusted if LUT_SIZE is changed! // the hardcoded values in the shader also need to be adjusted if LUT_SIZE is changed! -void InitPalette(int flashnum) +void InitPalette(int flashnum, boolean skiplut) { int i, r, g, b; + + //Hudler: 16/10/99: added for OpenGL gamma correction + RGBA_t gamma_correction = {0x7F7F7F7F}; + + //Hurdler 16/10/99: added for OpenGL gamma correction + gamma_correction.s.red = (UINT8)cv_grgammared.value; + gamma_correction.s.green = (UINT8)cv_grgammagreen.value; + gamma_correction.s.blue = (UINT8)cv_grgammablue.value; + // init the palette int flashoffset = flashnum*256; for (i = 0+flashoffset; i < 256+flashoffset; i++) { int fi = i-flashoffset; + + // gamma correction + int pr, pg, pb; + pr = (UINT8)MIN((pLocalPalette[i].s.red*gamma_correction.s.red)/127, 255); + pg = (UINT8)MIN((pLocalPalette[i].s.green*gamma_correction.s.green)/127, 255); + pb = (UINT8)MIN((pLocalPalette[i].s.blue*gamma_correction.s.blue)/127, 255); + // crush to 16-bit rgb565, like software currently does - float fred = (float)(pLocalPalette[i].s.red >> 3); - float fgreen = (float)(pLocalPalette[i].s.green >> 2); - float fblue = (float)(pLocalPalette[i].s.blue >> 3); + float fred = (float)(pr >> 3); + float fgreen = (float)(pg >> 2); + float fblue = (float)(pb >> 3); // restore to rgb888 gl_palette[fi*3] = (GLint)(fred / 31.0f * 255.0f); gl_palette[fi*3+1] = (GLint)(fgreen / 63.0f * 255.0f); gl_palette[fi*3+2] = (GLint)(fblue / 31.0f * 255.0f); } + if (!skiplut) + { // init the palette conversion lookup texture GLubyte *pal_lookup_tex = malloc(LUT_SIZE*LUT_SIZE*LUT_SIZE*sizeof(GLubyte)); @@ -1239,17 +1257,18 @@ void InitPalette(int flashnum) } } } -#undef STEP_SIZE - - pglGenTextures(1, &palette_tex_num); - pglBindTexture(GL_TEXTURE_3D, palette_tex_num); - pglTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - pglTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - if (!pglTexImage3D) - I_Error("pglTexImage3D is NULL!"); - pglTexImage3D(GL_TEXTURE_3D, 0, GL_R8, LUT_SIZE, LUT_SIZE, LUT_SIZE, 0, GL_RED, GL_UNSIGNED_BYTE, pal_lookup_tex); - free(pal_lookup_tex); + pglGenTextures(1, &palette_tex_num); + pglBindTexture(GL_TEXTURE_3D, palette_tex_num); + pglTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + pglTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + if (!pglTexImage3D) + I_Error("pglTexImage3D is NULL!"); + pglTexImage3D(GL_TEXTURE_3D, 0, GL_R8, LUT_SIZE, LUT_SIZE, LUT_SIZE, 0, GL_RED, GL_UNSIGNED_BYTE, pal_lookup_tex); + free(pal_lookup_tex); + } +#undef STEP_SIZE + pglUseProgram(gl_shaderprograms[8].program); pglUniform1i(gl_shaderprograms[8].uniforms[gluniform_color_lookup], 1); // bind sampler to second texture unit pglUniform1iv(gl_shaderprograms[8].uniforms[gluniform_palette], 768, gl_palette); @@ -1607,7 +1626,7 @@ EXPORT void HWRAPI(ClearBuffer) (FBOOLEAN ColorMask, pglEnableClientState(GL_TEXTURE_COORD_ARRAY); // And mostly this one, too if (!gl_palette_initialized) - InitPalette(0); // just gonna put this here for now + InitPalette(0, false); // just gonna put this here for now } diff --git a/src/hardware/r_opengl/r_opengl.h b/src/hardware/r_opengl/r_opengl.h index e8ef58a2c..8bce7d15b 100644 --- a/src/hardware/r_opengl/r_opengl.h +++ b/src/hardware/r_opengl/r_opengl.h @@ -135,6 +135,6 @@ typedef enum // in order for custom playpals n shit to work properly extern boolean gl_palette_initialized; -void InitPalette(int flashnum); +void InitPalette(int flashnum, boolean skiplut); #endif diff --git a/src/v_video.c b/src/v_video.c index c6822e9fa..dbe26a001 100644 --- a/src/v_video.c +++ b/src/v_video.c @@ -226,12 +226,15 @@ void V_SetPalette(INT32 palettenum) #ifdef HWRENDER if (rendermode != render_soft && rendermode != render_none) { - if (cv_grpaletteshader.value == 1){ + // in palette rendering mode, we already manage our palette ourselves + if (cv_grpaletteshader.value == 1) + { // reset our palette lookups n shit gl_palette_initialized = false; - InitPalette(palettenum); + InitPalette(palettenum, true); } - HWR_SetPalette(&pLocalPalette[palettenum*256]); + else + HWR_SetPalette(pLocalPalette); } #if (defined (__unix__) && !defined (MSDOS)) || defined (UNIXCOMMON) || defined (HAVE_SDL) else @@ -246,12 +249,14 @@ void V_SetPaletteLump(const char *pal) LoadPalette(pal); #ifdef HWRENDER if (rendermode != render_soft && rendermode != render_none) { - if (cv_grpaletteshader.value == 1){ + if (cv_grpaletteshader.value == 1) + { // reset our palette lookups n shit gl_palette_initialized = false; - InitPalette(0); + InitPalette(0, false); } - HWR_SetPalette(pLocalPalette); + else + HWR_SetPalette(pLocalPalette); } #if (defined (__unix__) && !defined (MSDOS)) || defined (UNIXCOMMON) || defined (HAVE_SDL) else