From 1cc94f379ddf4efe9723930686f092d04fb5c407 Mon Sep 17 00:00:00 2001 From: Alug Date: Fri, 1 May 2026 03:49:55 +0200 Subject: [PATCH] move palshader code out of HWR_DrawScreenFinalTexture and make the whole handling safer so we have less chance of ending up with blackscreens should the palette render shader fail --- src/hardware/hw_gl.h | 3 ++- src/hardware/hw_main.h | 4 ++-- src/hardware/r_opengl/r_opengl.c | 27 ++++++++------------------- src/hardware/r_opengl/r_opengl.h | 1 - src/sdl/ogl_sdl.c | 28 ++++++++++++++++++++++------ 5 files changed, 34 insertions(+), 29 deletions(-) diff --git a/src/hardware/hw_gl.h b/src/hardware/hw_gl.h index de5ac6338..351b36353 100644 --- a/src/hardware/hw_gl.h +++ b/src/hardware/hw_gl.h @@ -63,7 +63,7 @@ void GL_FlushScreenTextures(void); void GL_DrawScreenTexture(int tex, FSurfaceInfo *surf, FBITFIELD polyflags); void GL_DoScreenWipe(int wipeStart, int wipeEnd); void GL_RenderVhsEffect(fixed_t upbary, fixed_t downbary, UINT8 updistort, UINT8 downdistort, UINT8 barsize); -void GL_DrawScreenFinalTexture(int tex, INT32 width, INT32 height, boolean useshader); +void GL_DrawScreenFinalTexture(int tex, INT32 width, INT32 height); void GL_PostImgRedraw(float points[SCREENVERTS][SCREENVERTS][2]); @@ -72,6 +72,7 @@ void GL_LoadShader(int slot, char *code, hwdshaderstage_t stage); boolean GL_CompileShader(int slot); void GL_SetShader(int slot); void GL_UnSetShader(void); +void GL_EnableShader(void); void GL_SetShaderInfo(hwdshaderinfo_t info, INT32 value); diff --git a/src/hardware/hw_main.h b/src/hardware/hw_main.h index 509eca8b1..c8285c5bd 100644 --- a/src/hardware/hw_main.h +++ b/src/hardware/hw_main.h @@ -161,9 +161,9 @@ FUNCINLINE static ATTRINLINE void HWR_MakeScreenFinalTexture(void) GL_MakeScreenTexture(HWD_SCREENTEXTURE_GENERIC2); } -FUNCINLINE static ATTRINLINE void HWR_DrawScreenFinalTexture(INT32 width, INT32 height, boolean useshader) +FUNCINLINE static ATTRINLINE void HWR_DrawScreenFinalTexture(INT32 width, INT32 height) { - GL_DrawScreenFinalTexture(HWD_SCREENTEXTURE_GENERIC2, width, height, useshader); + GL_DrawScreenFinalTexture(HWD_SCREENTEXTURE_GENERIC2, width, height); } // hw_main.c: Segs diff --git a/src/hardware/r_opengl/r_opengl.c b/src/hardware/r_opengl/r_opengl.c index 383647d89..18bf2973d 100644 --- a/src/hardware/r_opengl/r_opengl.c +++ b/src/hardware/r_opengl/r_opengl.c @@ -151,7 +151,6 @@ typedef struct static fboobj_t framebufferobject = {}; int supportFBO = FBO_NONE; -boolean fbo_shader = false; // older gl versions might not have these, so prevent compile errors // this should still give a simple depth only rbo @@ -1072,6 +1071,13 @@ void GL_SetShader(int slot) gl_shadersenabled = false; } +void GL_EnableShader(void) +{ + // we can just use GL_Shader_SetUniforms as itll enable the shader for us + // and handle any failures + GL_Shader_SetUniforms(NULL, NULL, NULL, NULL); +} + void GL_UnSetShader(void) { if (gl_shadersenabled) // don't repeatedly call glUseProgram if not needed @@ -3355,8 +3361,6 @@ void GL_Framebuffer_Disable(void) if (!supportFBO || !framebufferobject.init) return; - fbo_shader = false; - pglBindFramebuffer(GL_FRAMEBUFFER_EXT, 0); pglBindRenderbuffer(GL_RENDERBUFFER_EXT, 0); @@ -3742,7 +3746,7 @@ void GL_MakeScreenTexture(int tex) tex_downloaded = screenTextures[tex]; } -void GL_DrawScreenFinalTexture(int tex, INT32 width, INT32 height, boolean useshader) +void GL_DrawScreenFinalTexture(int tex, INT32 width, INT32 height) { float xfix, yfix; float origaspect, newaspect; @@ -3795,18 +3799,6 @@ void GL_DrawScreenFinalTexture(int tex, INT32 width, INT32 height, boolean usesh pglBindTexture(GL_TEXTURE_2D, screenTextures[tex]); - if (useshader) - { - // godawful but you can only ever run ONE shader per renderpass and since i dont want to draw an extra screen texture when you downsample from higher resolution - // so i combined the palette postprocess with this crap -#ifdef USE_FBO_OGL - if (fbo_shader) - pglUseProgram(gl_shaders[SHADER_DOWNSAMPLE].program); - else -#endif - pglUseProgram(gl_shaders[SHADER_PALETTE_POSTPROCESS].program); // Final postprocess step of palette rendering, after everything else has been drawn. - } - pglColor4ubv(white); pglTexCoordPointer(2, GL_FLOAT, 0, fix); @@ -3814,9 +3806,6 @@ void GL_DrawScreenFinalTexture(int tex, INT32 width, INT32 height, boolean usesh pglDrawArrays(GL_TRIANGLE_FAN, 0, 4); - if (useshader) - pglUseProgram(0); - tex_downloaded = screenTextures[tex]; } diff --git a/src/hardware/r_opengl/r_opengl.h b/src/hardware/r_opengl/r_opengl.h index 308d69f92..13ba9f6f5 100644 --- a/src/hardware/r_opengl/r_opengl.h +++ b/src/hardware/r_opengl/r_opengl.h @@ -102,7 +102,6 @@ void GL_Framebuffer_Unbind(void); void GL_Framebuffer_Enable(void); void GL_Framebuffer_Disable(void); extern int supportFBO; -extern boolean fbo_shader; #endif // ========================================================================== diff --git a/src/sdl/ogl_sdl.c b/src/sdl/ogl_sdl.c index 61da733a6..1893fbf86 100644 --- a/src/sdl/ogl_sdl.c +++ b/src/sdl/ogl_sdl.c @@ -233,6 +233,7 @@ void OglSdlFinishUpdate(SDL_Window *window) #ifdef USE_FBO_OGL const boolean usefbo = UseScreenFBO(); + boolean fboshader = (usefbo && !WipeInAction && HWR_UseShader()); #endif SDL_GetWindowSize(window, &sdlw, &sdlh); @@ -242,17 +243,32 @@ void OglSdlFinishUpdate(SDL_Window *window) if (usefbo) { GL_Framebuffer_Unbind(); - fbo_shader = (HWR_UseShader() && !WipeInAction); // this looks awful with wipes } - else - fbo_shader = false; #endif #ifdef USE_FBO_OGL - HWR_DrawScreenFinalTexture(sdlw, sdlh, (HWR_ShouldUsePaletteRendering() || fbo_shader)); + if (HWR_ShouldUsePaletteRendering() || fboshader) #else - HWR_DrawScreenFinalTexture(sdlw, sdlh, HWR_ShouldUsePaletteRendering()); + if (HWR_ShouldUsePaletteRendering()) #endif + { + // godawful but you can only ever run ONE shader per renderpass and since i dont want to draw an extra screen texture when you downsample from higher resolution + // so i combined the palette postprocess with this crap +#ifdef USE_FBO_OGL + if (fboshader) // this looks awful with wipes + GL_SetShader(HWR_GetShaderFromTarget(SHADER_DOWNSAMPLE)); + else +#endif + GL_SetShader(HWR_GetShaderFromTarget(SHADER_PALETTE_POSTPROCESS)); // Final postprocess step of palette rendering, after everything else has been drawn. + + GL_EnableShader(); + HWR_DrawScreenFinalTexture(sdlw, sdlh); + GL_UnSetShader(); + } + else + { + HWR_DrawScreenFinalTexture(sdlw, sdlh); + } #ifdef USE_FBO_OGL if (usefbo) @@ -273,7 +289,7 @@ void OglSdlFinishUpdate(SDL_Window *window) #else if (!I_CheckNativeRes() || WipeInAction) #endif - HWR_DrawScreenFinalTexture(realwidth, realheight, false); + HWR_DrawScreenFinalTexture(realwidth, realheight); } #endif //HWRENDER