From 200d971d35789c1114a671c13c49ebff8d854b47 Mon Sep 17 00:00:00 2001 From: Alug Date: Wed, 27 Mar 2024 18:05:48 +0100 Subject: [PATCH] Toggle to render to screenFBO fixes downsampling from higher resolutions TODO: make this activate automatically if game detects it runs at higher res than desktop res --- src/hardware/hw_defs.h | 1 + src/hardware/hw_main.c | 13 ++ src/hardware/hw_main.h | 1 + src/hardware/r_opengl/r_opengl.c | 213 +++++++++++++++++++++++++++++-- src/hardware/r_opengl/r_opengl.h | 12 ++ src/i_video.h | 2 + src/sdl/i_video.c | 7 + src/sdl/ogl_sdl.c | 44 ++++++- 8 files changed, 278 insertions(+), 15 deletions(-) diff --git a/src/hardware/hw_defs.h b/src/hardware/hw_defs.h index b7c46b2e1..83bb70fde 100644 --- a/src/hardware/hw_defs.h +++ b/src/hardware/hw_defs.h @@ -277,6 +277,7 @@ typedef struct FSurfaceInfo FSurfaceInfo; enum hwdsetspecialstate { + HWD_SET_FRAMEBUFFER, HWD_SET_SHADERS, HWD_SET_TEXTUREFILTERMODE, diff --git a/src/hardware/hw_main.c b/src/hardware/hw_main.c index 131775c53..3634d1e9d 100644 --- a/src/hardware/hw_main.c +++ b/src/hardware/hw_main.c @@ -69,6 +69,7 @@ static boolean gr_palette_rendering_state = false; // Commands and console variables // ========================================================================== +static void CV_grframebuffer_OnChange(void); static void CV_filtermode_ONChange(void); static void CV_anisotropic_ONChange(void); static void CV_screentextures_ONChange(void); @@ -96,6 +97,8 @@ consvar_t cv_grsolvetjoin = {"gr_solvetjoin", "On", 0, CV_OnOff, NULL, 0, NULL, consvar_t cv_grbatching = {"gr_batching", "On", 0, CV_OnOff, NULL, 0, NULL, NULL, 0, 0, NULL}; +consvar_t cv_grframebuffer = {"gr_framebuffer", "Off", CV_SAVE|CV_CALL|CV_NOINIT, CV_OnOff, CV_grframebuffer_OnChange, 0, NULL, NULL, 0, 0, NULL}; + consvar_t cv_grfofcut = {"gr_fofcut", "On", CV_SAVE, CV_OnOff, NULL, 0, NULL, NULL, 0, 0, NULL}; consvar_t cv_fofzfightfix = {"fofzfightfix", "On", CV_SAVE, CV_OnOff, NULL, 0, NULL, NULL, 0, 0, NULL}; @@ -161,6 +164,13 @@ consvar_t cv_grhorizonlines = {"gr_horizonlines", "On", CV_SAVE, CV_OnOff, NULL, #define ONLY_IF_GL_LOADED if (vid.glstate != VID_GL_LIBRARY_LOADED) return; +static void CV_grframebuffer_OnChange(void) +{ + ONLY_IF_GL_LOADED + HWD.pfnSetSpecialState(HWD_SET_FRAMEBUFFER, cv_grframebuffer.value); + RefreshSDLSurface(); +} + static void CV_filtermode_ONChange(void) { ONLY_IF_GL_LOADED @@ -5936,6 +5946,7 @@ void HWR_TogglePaletteRendering(void) //added by Hurdler: console varibale that are saved void HWR_AddCommands(void) { + CV_RegisterVar(&cv_grframebuffer); CV_RegisterVar(&cv_grfiltermode); CV_RegisterVar(&cv_granisotropicmode); CV_RegisterVar(&cv_grsolvetjoin); @@ -5998,6 +6009,8 @@ void HWR_Startup(void) HWR_LoadAllCustomShaders(); HWR_TogglePaletteRendering(); + + HWD.pfnSetSpecialState(HWD_SET_FRAMEBUFFER, cv_grframebuffer.value); if (msaa) { diff --git a/src/hardware/hw_main.h b/src/hardware/hw_main.h index 3530c2915..4a9b1d434 100644 --- a/src/hardware/hw_main.h +++ b/src/hardware/hw_main.h @@ -154,6 +154,7 @@ void HWR_SetShaderState(void); // Console variables extern consvar_t cv_grshaders; +extern consvar_t cv_grframebuffer; extern consvar_t cv_grfofcut; extern consvar_t cv_fofzfightfix; extern consvar_t cv_splitwallfix; diff --git a/src/hardware/r_opengl/r_opengl.c b/src/hardware/r_opengl/r_opengl.c index 9fcf90a62..78b34c63f 100644 --- a/src/hardware/r_opengl/r_opengl.c +++ b/src/hardware/r_opengl/r_opengl.c @@ -114,6 +114,11 @@ static GLfloat modelMatrix[16]; GLfloat projMatrix[16]; static GLint viewport[4]; +static boolean GLFramebuffer_IsFuncAvailible(void); + + +GLuint FramebufferObject, FramebufferTexture, RenderbufferObject; +GLboolean FrameBufferEnabled = GL_FALSE, RenderToFramebuffer = GL_FALSE; // Sryder: NextTexAvail is broken for these because palette changes or changes to the texture filter or antialiasing // flush all of the stored textures, leaving them unavailable at times such as between levels @@ -426,12 +431,32 @@ typedef void (APIENTRY * PFNglCopyTexImage2D) (GLenum target, GLint level, GLenu static PFNglCopyTexImage2D pglCopyTexImage2D; typedef void (APIENTRY * PFNglCopyTexSubImage2D) (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height); static PFNglCopyTexSubImage2D pglCopyTexSubImage2D; -#endif /* 3.0 functions */ -#ifdef GL_VERSION_3_0 typedef void (APIENTRY * PFNglGenerateMipmap) (GLenum target); static PFNglGenerateMipmap pglGenerateMipmap; + +/* 3.0 functions for framebuffers and renderbuffers */ +typedef void (APIENTRY * PFNglGenFramebuffers) (GLsizei n, GLuint *ids); +static PFNglGenFramebuffers pglGenFramebuffers; +typedef void (APIENTRY * PFNglBindFramebuffer) (GLenum target, GLuint framebuffer); +static PFNglBindFramebuffer pglBindFramebuffer; +typedef void (APIENTRY * PFNglDeleteFramebuffers) (GLsizei n, GLuint *ids); +static PFNglDeleteFramebuffers pglDeleteFramebuffers; +typedef void (APIENTRY * PFNglFramebufferTexture2D) (GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level); +static PFNglFramebufferTexture2D pglFramebufferTexture2D; +typedef GLenum (APIENTRY * PFNglCheckFramebufferStatus) (GLenum target); +static PFNglCheckFramebufferStatus pglCheckFramebufferStatus; +typedef void (APIENTRY * PFNglGenRenderbuffers) (GLsizei n, GLuint *renderbuffers); +static PFNglGenRenderbuffers pglGenRenderbuffers; +typedef void (APIENTRY * PFNglBindRenderbuffer) (GLenum target, GLuint renderbuffer); +static PFNglBindRenderbuffer pglBindRenderbuffer; +typedef void (APIENTRY * PFNglDeleteRenderbuffers) (GLsizei n, GLuint *renderbuffers); +static PFNglDeleteRenderbuffers pglDeleteRenderbuffers; +typedef void (APIENTRY * PFNglRenderbufferStorage) (GLenum target, GLenum internalformat, GLsizei width, GLsizei height); +static PFNglRenderbufferStorage pglRenderbufferStorage; +typedef void (APIENTRY * PFNglFramebufferRenderbuffer) (GLenum target, GLenum attachment, GLenum renderbuffertarget, GLenum renderbuffer); +static PFNglFramebufferRenderbuffer pglFramebufferRenderbuffer; #endif /* 1.2 functions for 3D textures */ @@ -717,9 +742,28 @@ void SetupGLFunc4(void) pglUniform3fv = GetGLFunc("glUniform3fv"); pglGetUniformLocation = GetGLFunc("glGetUniformLocation"); -#ifdef GL_VERSION_3_0 pglGenerateMipmap = GetGLFunc("glGenerateMipmap"); -#endif + + if (GLFramebuffer_IsFuncAvailible()) + { + pglGenFramebuffers = GetGLFunc("glGenFramebuffers"); + pglBindFramebuffer = GetGLFunc("glBindFramebuffer"); + pglDeleteFramebuffers = GetGLFunc("glDeleteFramebuffers"); + pglFramebufferTexture2D = GetGLFunc("glFramebufferTexture2D"); + pglCheckFramebufferStatus = GetGLFunc("glCheckFramebufferStatus"); + pglGenRenderbuffers = GetGLFunc("glGenRenderbuffers"); + pglBindRenderbuffer = GetGLFunc("glBindRenderbuffer"); + pglDeleteRenderbuffers = GetGLFunc("glDeleteRenderbuffers"); + pglRenderbufferStorage = GetGLFunc("glRenderbufferStorage"); + pglFramebufferRenderbuffer = GetGLFunc("glFramebufferRenderbuffer"); + } +} + +static boolean GLFramebuffer_IsFuncAvailible(void) +{ + return((isExtAvailable("GL_ARB_framebuffer_no_attachments",gl_extensions)) && + (isExtAvailable("GL_ARB_framebuffer_object",gl_extensions)) && + (isExtAvailable("GL_ARB_framebuffer_sRGB",gl_extensions))); } EXPORT boolean HWRAPI(InitShaders) (void) @@ -897,13 +941,6 @@ void SetModelView(GLint w, GLint h) { //GL_DBG_Printf("SetModelView(): %dx%d\n", (int)w, (int)h); - // The screen textures need to be flushed if the width or height change so that they be remade for the correct size - if (screen_width != w || screen_height != h) - FlushScreenTextures(); - - screen_width = w; - screen_height = h; - pglViewport(0, 0, w, h); pglMatrixMode(GL_PROJECTION); @@ -935,7 +972,7 @@ void SetStates(void) pglEnable(GL_STENCIL_TEST); pglEnable(GL_DEPTH_TEST); // check the depth buffer - pglDepthMask(GL_TRUE); // enable writing to depth buffer + pglDepthMask(GL_TRUE); // enable writing to depth buffer pglClearDepth(1.0f); pglDepthRange(0.0f, 1.0f); pglDepthFunc(GL_LEQUAL); @@ -955,6 +992,151 @@ void SetStates(void) pglGetFloatv(GL_MODELVIEW_MATRIX, modelMatrix); // added for new coronas' code (without depth buffer) } +void GLFramebuffer_Generate(void) +{ + if (!GLFramebuffer_IsFuncAvailible()) + return; + + // Generate the framebuffer + if (FramebufferObject == 0) + pglGenFramebuffers(1, &FramebufferObject); + + if (pglCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE) + GLFramebuffer_GenerateAttachments(); +} + +void GLFramebuffer_Delete(void) +{ + if (!GLFramebuffer_IsFuncAvailible()) + return; + + // Unbind the framebuffer + pglBindFramebuffer(GL_FRAMEBUFFER, 0); + + if (FramebufferObject) + pglDeleteFramebuffers(1, &FramebufferObject); + + GLFramebuffer_DeleteAttachments(); + FramebufferObject = 0; +} + +void GLFramebuffer_GenerateAttachments(void) +{ + if (!GLFramebuffer_IsFuncAvailible()) + return; + + // Bind the framebuffer + pglBindFramebuffer(GL_FRAMEBUFFER, FramebufferObject); + + // Generate the framebuffer texture + if (FramebufferTexture == 0) + { + pglGenTextures(1, &FramebufferTexture); + pglBindTexture(GL_TEXTURE_2D, FramebufferTexture); + pglTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, screen_width, screen_height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL); + pglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + pglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + pglBindTexture(GL_TEXTURE_2D, 0); + + // Attach the framebuffer texture to the framebuffer + pglFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, FramebufferTexture, 0); + } + + // Generate the renderbuffer + if (RenderbufferObject == 0) + { + pglGenRenderbuffers(1, &RenderbufferObject); + + pglBindRenderbuffer(GL_RENDERBUFFER, RenderbufferObject); + pglRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, screen_width, screen_height); + pglFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, RenderbufferObject); + + // Clear the renderbuffer + ClearBuffer(true, true, true, NULL); + + pglBindRenderbuffer(GL_RENDERBUFFER, 0); + } + + // Unbind the framebuffer + pglBindFramebuffer(GL_FRAMEBUFFER, 0); +} + +void GLFramebuffer_DeleteAttachments(void) +{ + if (!GLFramebuffer_IsFuncAvailible()) + return; + + // Unbind the framebuffer + pglBindFramebuffer(GL_FRAMEBUFFER, 0); + + if (FramebufferTexture) + pglDeleteTextures(1, &FramebufferTexture); + + if (RenderbufferObject) + pglDeleteRenderbuffers(1, &RenderbufferObject); + + FramebufferTexture = 0; + RenderbufferObject = 0; +} + +void GLFramebuffer_Enable(void) +{ + if (!GLFramebuffer_IsFuncAvailible()) + return; + + if (FramebufferObject == 0) + GLFramebuffer_Generate(); + else if (FramebufferTexture == 0 || RenderbufferObject == 0) + GLFramebuffer_GenerateAttachments(); + + pglBindFramebuffer(GL_FRAMEBUFFER, FramebufferObject); + pglBindRenderbuffer(GL_RENDERBUFFER, RenderbufferObject); +} + +void GLFramebuffer_Disable(void) +{ + if (!GLFramebuffer_IsFuncAvailible()) + return; + + pglBindFramebuffer(GL_FRAMEBUFFER, 0); + pglBindRenderbuffer(GL_RENDERBUFFER, 0); +} + +// -----------------+ +// DeleteTexture : Deletes a texture from the GPU and frees its data +// -----------------+ +EXPORT void HWRAPI(DeleteTexture) (GLMipmap_t *pTexInfo) +{ + FTextureInfo *head = TexCacheHead; + + if (!pTexInfo) + return; + else if (pTexInfo->downloaded) + pglDeleteTextures(1, (GLuint *)&pTexInfo->downloaded); + + while (head) + { + if (head->downloaded == pTexInfo->downloaded) + { + if (head->next) + head->next->prev = head->prev; + else // no next -> tail is being deleted -> update TexCacheTail + TexCacheTail = head->prev; + if (head->prev) + head->prev->next = head->next; + else // no prev -> head is being deleted -> update TexCacheHead + TexCacheHead = head->next; + free(head); + break; + } + + head = head->next; + } + + pTexInfo->downloaded = 0; +} + + // -----------------+ // Flush : flush OpenGL textures // : Clear list of downloaded mipmaps @@ -2140,6 +2322,13 @@ EXPORT void HWRAPI(SetSpecialState) (hwdspecialstate_t IdState, INT32 Value) case HWD_SET_SHADERS: gl_allowshaders = Value; break; + + case HWD_SET_FRAMEBUFFER: + FrameBufferEnabled = Value ? GL_TRUE : GL_FALSE; + + if (!GLFramebuffer_IsFuncAvailible()) + FrameBufferEnabled = GL_FALSE; + break; case HWD_SET_TEXTUREFILTERMODE: switch (Value) diff --git a/src/hardware/r_opengl/r_opengl.h b/src/hardware/r_opengl/r_opengl.h index 34b2d3cf7..d8b2a12fc 100644 --- a/src/hardware/r_opengl/r_opengl.h +++ b/src/hardware/r_opengl/r_opengl.h @@ -104,6 +104,18 @@ static PFNglEnableClientState pglEnableClientState; #endif #endif +void GLFramebuffer_Generate(void); +void GLFramebuffer_Delete(void); + +void GLFramebuffer_GenerateAttachments(void); +void GLFramebuffer_DeleteAttachments(void); + +void GLFramebuffer_Enable(void); +void GLFramebuffer_Disable(void); + +extern GLuint FramebufferObject, FramebufferTexture, RenderbufferObject; +extern GLboolean FrameBufferEnabled, RenderToFramebuffer; + // ========================================================================== // GLOBAL // ========================================================================== diff --git a/src/i_video.h b/src/i_video.h index 602522871..2f08e4b1f 100644 --- a/src/i_video.h +++ b/src/i_video.h @@ -137,4 +137,6 @@ void I_EndRead(void); UINT32 I_GetRefreshRate(void); +void RefreshSDLSurface(void); + #endif diff --git a/src/sdl/i_video.c b/src/sdl/i_video.c index a1dd8bef6..4591b6ad6 100644 --- a/src/sdl/i_video.c +++ b/src/sdl/i_video.c @@ -203,6 +203,13 @@ static SDL_bool Impl_CreateWindow(SDL_bool fullscreen); //static void Impl_SetWindowName(const char *title); static void Impl_SetWindowIcon(void); +#ifdef HWRENDER +void RefreshSDLSurface(void) +{ + OglSdlSurface(vid.width, vid.height); +} +#endif + static void SDLSetMode(INT32 width, INT32 height, SDL_bool fullscreen) { static SDL_bool wasfullscreen = SDL_FALSE; diff --git a/src/sdl/ogl_sdl.c b/src/sdl/ogl_sdl.c index 00e3896ed..71e7fcdab 100644 --- a/src/sdl/ogl_sdl.c +++ b/src/sdl/ogl_sdl.c @@ -163,11 +163,35 @@ boolean OglSdlSurface(INT32 w, INT32 h) granisotropicmode_cons_t[1].value = maximumAnisotropy; - SDL_GL_SetSwapInterval(cv_vidwait.value ? 1 : 0); + if (cv_vidwait.value) + { + if (SDL_GL_SetSwapInterval(-1) != 0) // try async vsync + SDL_GL_SetSwapInterval(1); // normal vsync + } + else + SDL_GL_SetSwapInterval(0); + + //SDL_GL_SetSwapInterval(cv_vidwait.value ? -1 : 0); + + // The screen textures need to be flushed if the width or height change so that they be remade for the correct size + if (screen_width != w || screen_height != h) + { + FlushScreenTextures(); + GLFramebuffer_DeleteAttachments(); + } + + screen_width = (GLint)w; + screen_height = (GLint)h; SetModelView(w, h); SetStates(); pglClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT); + + RenderToFramebuffer = FrameBufferEnabled; + GLFramebuffer_Disable(); + + if (RenderToFramebuffer) + GLFramebuffer_Enable(); HWR_Startup(); textureformatGL = cbpp > 16 ? GL_RGBA : GL_RGB5_A1; @@ -187,15 +211,29 @@ void OglSdlFinishUpdate(boolean waitvbl) int sdlw, sdlh; if (oldwaitvbl != waitvbl) { - SDL_GL_SetSwapInterval(waitvbl ? 1 : 0); + if (waitvbl) + { + if (SDL_GL_SetSwapInterval(-1) != 0) // try async vsync + SDL_GL_SetSwapInterval(1); // normal vsync + } + else + SDL_GL_SetSwapInterval(0); + //SDL_GL_SetSwapInterval(waitvbl ? -1 : 0); } oldwaitvbl = waitvbl; SDL_GetWindowSize(window, &sdlw, &sdlh); - HWR_MakeScreenFinalTexture(); + + GLFramebuffer_Disable(); + RenderToFramebuffer = FrameBufferEnabled; + HWR_DrawScreenFinalTexture(sdlw, sdlh); + + if (RenderToFramebuffer) + GLFramebuffer_Enable(); + SDL_GL_SwapWindow(window); GClipRect(0, 0, realwidth, realheight, NZCLIP_PLANE, FAR_ZCLIP_DEFAULT);