From 200d971d35789c1114a671c13c49ebff8d854b47 Mon Sep 17 00:00:00 2001 From: Alug Date: Wed, 27 Mar 2024 18:05:48 +0100 Subject: [PATCH 1/6] 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); From 91a3b288eff25ad58c1fcba29fec93f84d0374e9 Mon Sep 17 00:00:00 2001 From: Alug Date: Wed, 27 Mar 2024 18:08:09 +0100 Subject: [PATCH 2/6] conflict moment --- src/hardware/r_opengl/r_opengl.c | 38 ++++---------------------------- src/sdl/ogl_sdl.c | 19 ++-------------- 2 files changed, 6 insertions(+), 51 deletions(-) diff --git a/src/hardware/r_opengl/r_opengl.c b/src/hardware/r_opengl/r_opengl.c index 78b34c63f..d17c29e04 100644 --- a/src/hardware/r_opengl/r_opengl.c +++ b/src/hardware/r_opengl/r_opengl.c @@ -432,9 +432,11 @@ 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; +#ifdef GL_VERSION_3_0 /* 3.0 functions */ typedef void (APIENTRY * PFNglGenerateMipmap) (GLenum target); static PFNglGenerateMipmap pglGenerateMipmap; +#endif /* 3.0 functions for framebuffers and renderbuffers */ typedef void (APIENTRY * PFNglGenFramebuffers) (GLsizei n, GLuint *ids); @@ -742,7 +744,9 @@ void SetupGLFunc4(void) pglUniform3fv = GetGLFunc("glUniform3fv"); pglGetUniformLocation = GetGLFunc("glGetUniformLocation"); +#ifdef GL_VERSION_3_0 pglGenerateMipmap = GetGLFunc("glGenerateMipmap"); +#endif if (GLFramebuffer_IsFuncAvailible()) { @@ -1102,40 +1106,6 @@ void GLFramebuffer_Disable(void) 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 diff --git a/src/sdl/ogl_sdl.c b/src/sdl/ogl_sdl.c index 71e7fcdab..06fb5df27 100644 --- a/src/sdl/ogl_sdl.c +++ b/src/sdl/ogl_sdl.c @@ -162,16 +162,8 @@ boolean OglSdlSurface(INT32 w, INT32 h) SetupGLFunc4(); granisotropicmode_cons_t[1].value = maximumAnisotropy; - - 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); + 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) @@ -211,14 +203,7 @@ void OglSdlFinishUpdate(boolean waitvbl) int sdlw, sdlh; if (oldwaitvbl != waitvbl) { - 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); + SDL_GL_SetSwapInterval(waitvbl ? 1 : 0); } oldwaitvbl = waitvbl; From a28f025d401e92505a470e441940f293d8a6cfe1 Mon Sep 17 00:00:00 2001 From: Alug Date: Wed, 27 Mar 2024 18:33:29 +0100 Subject: [PATCH 3/6] Stuff from displaycrap branch to detect downsampling automatically --- src/i_video.h | 2 ++ src/sdl/i_video.c | 88 ++++++++++++++++++++++++++++++++++++++++++++++- src/sdl/ogl_sdl.c | 29 ++++++++-------- 3 files changed, 104 insertions(+), 15 deletions(-) diff --git a/src/i_video.h b/src/i_video.h index 2f08e4b1f..098d65cf6 100644 --- a/src/i_video.h +++ b/src/i_video.h @@ -139,4 +139,6 @@ UINT32 I_GetRefreshRate(void); void RefreshSDLSurface(void); +extern boolean downsample; + #endif diff --git a/src/sdl/i_video.c b/src/sdl/i_video.c index 4591b6ad6..031842874 100644 --- a/src/sdl/i_video.c +++ b/src/sdl/i_video.c @@ -159,6 +159,11 @@ static SDL_bool exposevideo = SDL_FALSE; static SDL_bool usesdl2soft = SDL_FALSE; static SDL_bool borderlesswindow = SDL_FALSE; +static boolean displayinit = false; +static int currentDisplayIndex = -1; +static int previousDisplayIndex = -1; +static int numDisplays = -1; + // SDL2 vars SDL_Window *window; SDL_Renderer *renderer; @@ -166,6 +171,12 @@ static SDL_Texture *texture; static SDL_bool havefocus = SDL_TRUE; static const char *fallback_resolution_name = "Fallback"; +static void ScaleQuality_OnChange(void) +{ + SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, cv_scalequality.string); + COM_ImmedExecute(va("vid_mode %d", vid.modenum)); // Hack to make it apply without restarting +} + // windowed video modes from which to choose from. static INT32 windowedModes[MAXWINMODES][2] = { @@ -203,7 +214,8 @@ static SDL_bool Impl_CreateWindow(SDL_bool fullscreen); //static void Impl_SetWindowName(const char *title); static void Impl_SetWindowIcon(void); -#ifdef HWRENDER +boolean downsample = false; + void RefreshSDLSurface(void) { OglSdlSurface(vid.width, vid.height); @@ -769,11 +781,23 @@ static INT32 SDLJoyAxis(const Sint16 axis, evtype_t which) return raxis; } +static void SDL_CheckDisplays(void) +{ + if (!displayinit) + { + numDisplays = SDL_GetNumVideoDisplays(); + displayinit = true; + } + + currentDisplayIndex = SDL_GetWindowDisplayIndex(window); +} + static void Impl_HandleWindowEvent(SDL_WindowEvent evt) { static SDL_bool firsttimeonmouse = SDL_TRUE; static SDL_bool mousefocus = SDL_TRUE; static SDL_bool kbfocus = SDL_TRUE; + static SDL_bool windowmoved = SDL_FALSE; switch (evt.event) { @@ -793,6 +817,9 @@ static void Impl_HandleWindowEvent(SDL_WindowEvent evt) break; case SDL_WINDOWEVENT_MAXIMIZED: break; + case SDL_WINDOWEVENT_MOVED: + windowmoved = SDL_TRUE; + break; } if (mousefocus && kbfocus) @@ -832,6 +859,43 @@ static void Impl_HandleWindowEvent(SDL_WindowEvent evt) } } + if (windowmoved) + { + SDL_CheckDisplays(); + if (numDisplays > 1) + { + currentDisplayIndex = SDL_GetWindowDisplayIndex(window); + if (currentDisplayIndex != previousDisplayIndex) + { + for (int i = 0; i < numDisplays; ++i) + { + if (currentDisplayIndex == i) + { + SDL_DisplayMode curmode; + if (SDL_GetCurrentDisplayMode(i, &curmode) == 0) + { + if (cv_grframebuffer.value && ((vid.width > curmode.w) || (vid.height > curmode.h))) //framebuffer downsampler thinge + { + downsample = true; + RefreshSDLSurface(); + } + else + downsample = false; + + if (cv_fullscreen.value == 2) //exclusive + SDLSetMode(curmode.w, curmode.h, true); + else if (cv_fullscreen.value == 1) //borderless sdl thing + SDLSetMode(vid.width, vid.height, true); + else //windowed mode + SDLSetMode(vid.width, vid.height, false); + } + break; // found our current display break outta here + } + } + previousDisplayIndex = currentDisplayIndex; + } + } + } } static void Impl_HandleKeyboardEvent(SDL_KeyboardEvent evt, Uint32 type) @@ -1908,6 +1972,25 @@ INT32 VID_SetMode(INT32 modeNum) } //Impl_SetWindowName("SRB2Kart "VERSIONSTRING); + for (int i = 0; i < numDisplays; ++i) + { + if (currentDisplayIndex == i) + { + SDL_DisplayMode curmode; + if (SDL_GetCurrentDisplayMode(i, &curmode) == 0) + { + if (cv_grframebuffer.value && ((vid.width > curmode.w) || (vid.height > curmode.h))) //framebuffer downsampler thinge + { + downsample = true; + RefreshSDLSurface(); + } + else + downsample = false; + } + break; // found our current display break outta here + } + } + SDLSetMode(vid.width, vid.height, USE_FULLSCREEN); Impl_VideoSetupBuffer(); @@ -2318,6 +2401,9 @@ void I_StartupGraphics(void) // Fury: we do window initialization after GL setup to allow // SDL_GL_LoadLibrary to work well on Windows + //check for display things + SDL_CheckDisplays(); + // Create window //Impl_CreateWindow(USE_FULLSCREEN); //Impl_SetWindowName("SRB2Kart "VERSIONSTRING); diff --git a/src/sdl/ogl_sdl.c b/src/sdl/ogl_sdl.c index 06fb5df27..719bd48d1 100644 --- a/src/sdl/ogl_sdl.c +++ b/src/sdl/ogl_sdl.c @@ -42,6 +42,7 @@ #include "../i_system.h" #include "hwsym_sdl.h" #include "../m_argv.h" +#include "../i_video.h" #ifdef DEBUG_TO_FILE #include @@ -149,22 +150,22 @@ boolean OglSdlSurface(INT32 w, INT32 h) "- GPU vendor has dropped OpenGL support on your GPU and OS. (Old GPU?)\n" "- GPU drivers are missing or broken. You may need to update your drivers."); } + + if (isExtAvailable("GL_EXT_texture_filter_anisotropic", gl_extensions)) + pglGetIntegerv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &maximumAnisotropy); + else + maximumAnisotropy = 1; + + SetupGLInfo(); + + SetupGLFunc4(); + + granisotropicmode_cons_t[1].value = maximumAnisotropy; } first_init = true; - if (isExtAvailable("GL_EXT_texture_filter_anisotropic", gl_extensions)) - pglGetIntegerv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &maximumAnisotropy); - else - maximumAnisotropy = 1; - - SetupGLInfo(); - - SetupGLFunc4(); - - granisotropicmode_cons_t[1].value = maximumAnisotropy; - 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) { @@ -182,7 +183,7 @@ boolean OglSdlSurface(INT32 w, INT32 h) RenderToFramebuffer = FrameBufferEnabled; GLFramebuffer_Disable(); - if (RenderToFramebuffer) + if (RenderToFramebuffer && downsample) GLFramebuffer_Enable(); HWR_Startup(); @@ -216,7 +217,7 @@ void OglSdlFinishUpdate(boolean waitvbl) HWR_DrawScreenFinalTexture(sdlw, sdlh); - if (RenderToFramebuffer) + if (RenderToFramebuffer && downsample) GLFramebuffer_Enable(); SDL_GL_SwapWindow(window); From c7a258f32ff075f1cc86e3ad582b58cce5402b93 Mon Sep 17 00:00:00 2001 From: Alug Date: Wed, 27 Mar 2024 18:34:58 +0100 Subject: [PATCH 4/6] clean up --- src/sdl/i_video.c | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/src/sdl/i_video.c b/src/sdl/i_video.c index 031842874..f62195ebc 100644 --- a/src/sdl/i_video.c +++ b/src/sdl/i_video.c @@ -171,12 +171,6 @@ static SDL_Texture *texture; static SDL_bool havefocus = SDL_TRUE; static const char *fallback_resolution_name = "Fallback"; -static void ScaleQuality_OnChange(void) -{ - SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, cv_scalequality.string); - COM_ImmedExecute(va("vid_mode %d", vid.modenum)); // Hack to make it apply without restarting -} - // windowed video modes from which to choose from. static INT32 windowedModes[MAXWINMODES][2] = { @@ -216,6 +210,7 @@ static void Impl_SetWindowIcon(void); boolean downsample = false; +#ifdef HWRENDER void RefreshSDLSurface(void) { OglSdlSurface(vid.width, vid.height); @@ -881,13 +876,6 @@ static void Impl_HandleWindowEvent(SDL_WindowEvent evt) } else downsample = false; - - if (cv_fullscreen.value == 2) //exclusive - SDLSetMode(curmode.w, curmode.h, true); - else if (cv_fullscreen.value == 1) //borderless sdl thing - SDLSetMode(vid.width, vid.height, true); - else //windowed mode - SDLSetMode(vid.width, vid.height, false); } break; // found our current display break outta here } From 23cc0dfc3a37639bd1379759da2652d58f10af78 Mon Sep 17 00:00:00 2001 From: Alug Date: Wed, 27 Mar 2024 18:44:32 +0100 Subject: [PATCH 5/6] add some comments cause i WILL forger what this crap even does --- src/sdl/i_video.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/sdl/i_video.c b/src/sdl/i_video.c index f62195ebc..6476e3276 100644 --- a/src/sdl/i_video.c +++ b/src/sdl/i_video.c @@ -778,13 +778,13 @@ static INT32 SDLJoyAxis(const Sint16 axis, evtype_t which) static void SDL_CheckDisplays(void) { - if (!displayinit) + if (!displayinit) // only do this once, dont think someone ever unplugs or adds a new display during gaming lul { - numDisplays = SDL_GetNumVideoDisplays(); + numDisplays = SDL_GetNumVideoDisplays(); // get all available displays displayinit = true; } - currentDisplayIndex = SDL_GetWindowDisplayIndex(window); + currentDisplayIndex = SDL_GetWindowDisplayIndex(window); // this is the current display our game window resides in } static void Impl_HandleWindowEvent(SDL_WindowEvent evt) @@ -859,15 +859,15 @@ static void Impl_HandleWindowEvent(SDL_WindowEvent evt) SDL_CheckDisplays(); if (numDisplays > 1) { - currentDisplayIndex = SDL_GetWindowDisplayIndex(window); - if (currentDisplayIndex != previousDisplayIndex) + currentDisplayIndex = SDL_GetWindowDisplayIndex(window); // check the display index + if (currentDisplayIndex != previousDisplayIndex) // did the display change from previous index entry? { - for (int i = 0; i < numDisplays; ++i) + for (int i = 0; i < numDisplays; ++i) // go through all detected displays { - if (currentDisplayIndex == i) + if (currentDisplayIndex == i) // found our current display? { SDL_DisplayMode curmode; - if (SDL_GetCurrentDisplayMode(i, &curmode) == 0) + if (SDL_GetCurrentDisplayMode(i, &curmode) == 0) // get info for the current display { if (cv_grframebuffer.value && ((vid.width > curmode.w) || (vid.height > curmode.h))) //framebuffer downsampler thinge { @@ -880,7 +880,7 @@ static void Impl_HandleWindowEvent(SDL_WindowEvent evt) break; // found our current display break outta here } } - previousDisplayIndex = currentDisplayIndex; + previousDisplayIndex = currentDisplayIndex; // set current display to index } } } @@ -1960,17 +1960,17 @@ INT32 VID_SetMode(INT32 modeNum) } //Impl_SetWindowName("SRB2Kart "VERSIONSTRING); - for (int i = 0; i < numDisplays; ++i) + for (int i = 0; i < numDisplays; ++i) // go through all displays again { - if (currentDisplayIndex == i) + if (currentDisplayIndex == i) // find the current display { SDL_DisplayMode curmode; - if (SDL_GetCurrentDisplayMode(i, &curmode) == 0) + if (SDL_GetCurrentDisplayMode(i, &curmode) == 0) // get the info on current display { if (cv_grframebuffer.value && ((vid.width > curmode.w) || (vid.height > curmode.h))) //framebuffer downsampler thinge { downsample = true; - RefreshSDLSurface(); + RefreshSDLSurface(); // refresh crap so our fbo and screentextures dont break } else downsample = false; From a779191febc3a967b44d602be45c8c68ce5a267a Mon Sep 17 00:00:00 2001 From: Alug Date: Wed, 27 Mar 2024 18:45:59 +0100 Subject: [PATCH 6/6] beter --- src/sdl/i_video.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/sdl/i_video.c b/src/sdl/i_video.c index 6476e3276..8772f2da2 100644 --- a/src/sdl/i_video.c +++ b/src/sdl/i_video.c @@ -1960,7 +1960,7 @@ INT32 VID_SetMode(INT32 modeNum) } //Impl_SetWindowName("SRB2Kart "VERSIONSTRING); - for (int i = 0; i < numDisplays; ++i) // go through all displays again + for (int i = 0; i < numDisplays; ++i) // go through all displays again cause our vidya mode changed { if (currentDisplayIndex == i) // find the current display { @@ -1975,7 +1975,7 @@ INT32 VID_SetMode(INT32 modeNum) else downsample = false; } - break; // found our current display break outta here + break; // did everything so done lul } }