From 8a19bda170df49b7ce41ba341b8bdd248c98609c Mon Sep 17 00:00:00 2001 From: Alug Date: Fri, 30 Aug 2024 21:21:21 +0200 Subject: [PATCH] use shader for downsampling with fbo looks alot better and actually makes supersampling worth it will be disabled when shaders also are --- src/hardware/hw_defs.h | 1 + src/hardware/hw_shaders.c | 4 ++++ src/hardware/hw_shaders.h | 19 +++++++++++++++++++ src/hardware/r_opengl/r_opengl.c | 11 +++++++++-- src/hardware/r_opengl/r_opengl.h | 1 + src/sdl/ogl_sdl.c | 11 +++++++++-- src/sdl/ogl_sdl.h | 2 +- 7 files changed, 44 insertions(+), 5 deletions(-) diff --git a/src/hardware/hw_defs.h b/src/hardware/hw_defs.h index 7d2d17532..b222a9e88 100644 --- a/src/hardware/hw_defs.h +++ b/src/hardware/hw_defs.h @@ -182,6 +182,7 @@ enum SHADER_SKY, SHADER_PALETTE_POSTPROCESS, SHADER_UI_COLORMAP_FADE, + SHADER_DOWNSAMPLE, NUMSHADERTARGETS }; diff --git a/src/hardware/hw_shaders.c b/src/hardware/hw_shaders.c index b58ca4137..2c7fdf27a 100644 --- a/src/hardware/hw_shaders.c +++ b/src/hardware/hw_shaders.c @@ -56,6 +56,9 @@ static struct { // UI colormap fade shader {GLSL_DEFAULT_VERTEX_SHADER, GLSL_UI_COLORMAP_FADE_FRAGMENT_SHADER}, + // downsampling + {GLSL_DEFAULT_VERTEX_SHADER, GLSL_DOWNSAMPLE_FRAGMENT_SHADER}, + {NULL, NULL}, }; @@ -441,6 +444,7 @@ customshaderxlat_t shaderxlat[] = {"Sky", SHADER_SKY}, {"PalettePostprocess", SHADER_PALETTE_POSTPROCESS}, {"UIColormapFade", SHADER_UI_COLORMAP_FADE}, + {"Downsampling", SHADER_DOWNSAMPLE}, {NULL, 0}, }; diff --git a/src/hardware/hw_shaders.h b/src/hardware/hw_shaders.h index 85adb6147..7c9717aa7 100644 --- a/src/hardware/hw_shaders.h +++ b/src/hardware/hw_shaders.h @@ -46,6 +46,25 @@ "gl_FragColor = texture2D(tex, gl_TexCoord[0].st) * poly_color;\n" \ "}\0" + +#define GLSL_DOWNSAMPLE_FRAGMENT_SHADER \ + "uniform sampler2D tex;\n" \ + "void main(void) {\n" \ + "vec2 uv = gl_TexCoord[0].st;\n" \ + "vec2 texel_size = 1.0 / textureSize(tex, 0);\n" \ + "vec2 offset1 = vec2(texel_size.x * 0.5, texel_size.y * 0.5);\n" \ + "vec2 offset2 = vec2(-texel_size.x * 0.5, texel_size.y * 0.5);\n" \ + "vec2 offset3 = vec2(texel_size.x * 0.5, -texel_size.y * 0.5);\n" \ + "vec2 offset4 = vec2(-texel_size.x * 0.5, -texel_size.y * 0.5);\n" \ + "vec4 color1 = texture2D(tex, uv + offset1);\n" \ + "vec4 color2 = texture2D(tex, uv + offset2);\n" \ + "vec4 color3 = texture2D(tex, uv + offset3);\n" \ + "vec4 color4 = texture2D(tex, uv + offset4);\n" \ + "vec4 final_color = (color1 + color2 + color3 + color4) * 0.25;\n" \ + "gl_FragColor = final_color;\n" \ + "}\0" + + // // Software fragment shader // diff --git a/src/hardware/r_opengl/r_opengl.c b/src/hardware/r_opengl/r_opengl.c index 644bf9cfa..f830d1d17 100644 --- a/src/hardware/r_opengl/r_opengl.c +++ b/src/hardware/r_opengl/r_opengl.c @@ -24,6 +24,7 @@ #include #include #include "../../r_local.h" // For rendertimefrac, used for the leveltime shader uniform +#include "../../f_finale.h" #include "r_opengl.h" #include "r_vbo.h" #include "../hw_shaders.h" @@ -124,6 +125,7 @@ GLuint FramebufferObject, FramebufferTexture, RenderbufferObject; GLboolean FrameBufferEnabled = GL_FALSE, RenderToFramebuffer = GL_FALSE; boolean supportFBO = false; +boolean fbo_shader = false; #endif // Sryder: NextTexAvail is broken for these because palette changes or changes to the texture filter or antialiasing @@ -1124,6 +1126,8 @@ void GLFramebuffer_Disable(void) if (!supportFBO) return; + fbo_shader = false; + pglBindFramebuffer(GL_FRAMEBUFFER, 0); pglBindRenderbuffer(GL_RENDERBUFFER, 0); @@ -3473,17 +3477,20 @@ EXPORT void HWRAPI(DrawScreenFinalTexture)(int tex, INT32 width, INT32 height) SetBlend(PF_NoDepthTest); pglBindTexture(GL_TEXTURE_2D, screenTextures[tex]); - + if (HWR_ShouldUsePaletteRendering()) pglUseProgram(gl_shaders[SHADER_PALETTE_POSTPROCESS].program); // palette postprocess shader + if (HWR_UseShader() && fbo_shader && !WipeInAction) // this looks awful with wipes + pglUseProgram(gl_shaders[SHADER_DOWNSAMPLE].program); + pglColor4ubv(white); pglTexCoordPointer(2, GL_FLOAT, 0, fix); pglVertexPointer(3, GL_FLOAT, 0, off); pglDrawArrays(GL_TRIANGLE_FAN, 0, 4); - + if (HWR_ShouldUsePaletteRendering()) pglUseProgram(0); diff --git a/src/hardware/r_opengl/r_opengl.h b/src/hardware/r_opengl/r_opengl.h index baab44637..f9f4046cb 100644 --- a/src/hardware/r_opengl/r_opengl.h +++ b/src/hardware/r_opengl/r_opengl.h @@ -114,6 +114,7 @@ void GLFramebuffer_Disable(void); extern GLuint FramebufferObject, FramebufferTexture, RenderbufferObject; extern GLboolean FrameBufferEnabled, RenderToFramebuffer; extern boolean supportFBO; +extern boolean fbo_shader; #endif // ========================================================================== diff --git a/src/sdl/ogl_sdl.c b/src/sdl/ogl_sdl.c index 2ea822e63..d35700381 100644 --- a/src/sdl/ogl_sdl.c +++ b/src/sdl/ogl_sdl.c @@ -68,8 +68,8 @@ PFNglGetIntegerv pglGetIntegerv; PFNglGetString pglGetString; #endif -#if defined (__unix__) #ifdef USE_FBO_OGL +#if defined (__unix__) boolean isnvidiagpu = false; #endif #endif @@ -264,14 +264,20 @@ void OglSdlFinishUpdate(boolean waitvbl) ); if (RenderToFramebuffer) + { GLFramebuffer_Unbind(); + fbo_shader = true; + } #endif - + HWR_DrawScreenFinalTexture(sdlw, sdlh); #ifdef USE_FBO_OGL if (RenderToFramebuffer) + { GLFramebuffer_Enable(); + fbo_shader = false; + } #endif SDL_GL_SwapWindow(window); @@ -280,6 +286,7 @@ void OglSdlFinishUpdate(boolean waitvbl) // Sryder: We need to draw the final screen texture again into the other buffer in the original position so that // effects that want to take the old screen can do so after this + HWR_DrawScreenFinalTexture(realwidth, realheight); } diff --git a/src/sdl/ogl_sdl.h b/src/sdl/ogl_sdl.h index d45420e37..ca67cbbf4 100644 --- a/src/sdl/ogl_sdl.h +++ b/src/sdl/ogl_sdl.h @@ -20,8 +20,8 @@ boolean OglSdlSurface(INT32 w, INT32 h); -#if defined (__unix__) #ifdef USE_FBO_OGL +#if defined (__unix__) extern boolean isnvidiagpu; #endif #endif